8 #include "GenericHashtable.h"
14 int genputtable(struct genhashtable *ht, void * key, void * object) {
15 unsigned int bin=genhashfunction(ht,key);
16 struct genpointerlist * newptrlist=(struct genpointerlist *) RUNMALLOC(sizeof(struct genpointerlist));
18 newptrlist->object=object;
19 newptrlist->next=ht->bins[bin];
20 newptrlist->inext=NULL;
21 /* maintain linked list of ht entries for iteration*/
25 newptrlist->iprev=NULL;
27 ht->last->inext=newptrlist;
28 newptrlist->iprev=ht->last;
31 ht->bins[bin]=newptrlist;
33 if(ht->counter>ht->currentsize&&ht->currentsize!=INT_MAX) {
34 /* Expand hashtable */
35 long newcurrentsize=(ht->currentsize<(INT_MAX/2))?ht->currentsize*2:INT_MAX;
36 long oldcurrentsize=ht->currentsize;
37 struct genpointerlist **newbins=(struct genpointerlist **) RUNMALLOC(sizeof (struct genpointerlist *)*newcurrentsize);
38 struct genpointerlist **oldbins=ht->bins;
40 for(j=0;j<newcurrentsize;j++) newbins[j]=NULL;
41 ht->currentsize=newcurrentsize;
42 for(i=0;i<oldcurrentsize;i++) {
43 struct genpointerlist * tmpptr=oldbins[i];
45 int hashcode=genhashfunction(ht, tmpptr->src);
46 struct genpointerlist *nextptr=tmpptr->next;
47 tmpptr->next=newbins[hashcode];
48 newbins[hashcode]=tmpptr;
58 int hashsize(struct genhashtable *ht) {
62 void genrehash(struct genhashtable * ht) {
63 /* Expand hashtable */
64 struct genpointerlist **newbins=(struct genpointerlist **) RUNMALLOC(sizeof (struct genpointerlist *)*ht->currentsize);
65 struct genpointerlist **oldbins=ht->bins;
68 for(i=0;i<ht->currentsize;i++) {
69 struct genpointerlist * tmpptr=oldbins[i];
71 int hashcode=genhashfunction(ht, tmpptr->src);
72 struct genpointerlist *nextptr=tmpptr->next;
73 tmpptr->next=newbins[hashcode];
74 newbins[hashcode]=tmpptr;
82 void * gengettable(struct genhashtable *ht, void * key) {
83 struct genpointerlist * ptr=ht->bins[genhashfunction(ht,key)];
85 if (((ht->comp_function==NULL)&&(ptr->src==key))||((ht->comp_function!=NULL)&&(*ht->comp_function)(ptr->src,key)))
89 printf("XXXXXXXXX: COULDN'T FIND ENTRY FOR KEY %p\n",key);
93 void * getnext(struct genhashtable *ht, void * key) {
94 struct genpointerlist * ptr=ht->bins[genhashfunction(ht,key)];
96 if (((ht->comp_function==NULL)&&(ptr->src==key))||((ht->comp_function!=NULL)&&(*ht->comp_function)(ptr->src,key)))
97 if (ptr->inext!=NULL) {
98 return ptr->inext->src;
103 printf("XXXXXXXXX: COULDN'T FIND ENTRY FOR KEY %p...\n Likely concurrent removal--bad user!!!\n",key);
107 int gencontains(struct genhashtable *ht, void * key) {
108 struct genpointerlist * ptr=ht->bins[genhashfunction(ht,key)];
109 //printf("In gencontains2\n");fflush(NULL);
111 if (((ht->comp_function==NULL)&&(ptr->src==key))||((ht->comp_function!=NULL)&&(*ht->comp_function)(ptr->src,key)))
119 void genfreekey(struct genhashtable *ht, void * key) {
120 struct genpointerlist * ptr=ht->bins[genhashfunction(ht,key)];
122 if (((ht->comp_function==NULL)&&(ptr->src==key))||((ht->comp_function!=NULL)&&(*ht->comp_function)(ptr->src,key))) {
123 ht->bins[genhashfunction(ht,key)]=ptr->next;
131 if (ptr->iprev!=NULL)
132 ptr->iprev->inext=ptr->inext;
133 if (ptr->inext!=NULL)
134 ptr->inext->iprev=ptr->iprev;
140 while(ptr->next!=NULL) {
141 if (((ht->comp_function==NULL)&&(ptr->next->src==key))||((ht->comp_function!=NULL)&&(*ht->comp_function)(ptr->next->src,key))) {
142 struct genpointerlist *tmpptr=ptr->next;
143 ptr->next=tmpptr->next;
144 if (tmpptr==ht->list)
145 ht->list=tmpptr->inext;
146 if (tmpptr==ht->last)
147 ht->last=tmpptr->iprev;
148 if (tmpptr->iprev!=NULL)
149 tmpptr->iprev->inext=tmpptr->inext;
150 if (tmpptr->inext!=NULL)
151 tmpptr->inext->iprev=tmpptr->iprev;
158 printf("XXXXXXXXX: COULDN'T FIND ENTRY FOR KEY %p\n",key);
161 unsigned int genhashfunction(struct genhashtable *ht, void * key) {
162 if (ht->hash_function==NULL)
163 return ((long unsigned int)key) % ht->currentsize;
165 return ((*ht->hash_function)(key)) % ht->currentsize;
168 struct genhashtable * genallocatehashtable(unsigned int (*hash_function)(void *),int (*comp_function)(void *, void *)) {
169 struct genhashtable *ght=(struct genhashtable *) RUNMALLOC(sizeof(struct genhashtable));
170 struct genpointerlist **gpl=(struct genpointerlist **) RUNMALLOC(sizeof(struct genpointerlist *)*geninitialnumbins);
172 for(i=0;i<geninitialnumbins;i++)
174 ght->hash_function=hash_function;
175 ght->comp_function=comp_function;
176 ght->currentsize=geninitialnumbins;
184 void genfreehashtable(struct genhashtable * ht) {
186 for (i=0;i<ht->currentsize;i++) {
187 if (ht->bins[i]!=NULL) {
188 struct genpointerlist *genptr=ht->bins[i];
189 while(genptr!=NULL) {
190 struct genpointerlist *tmpptr=genptr->next;
200 struct geniterator * gengetiterator(struct genhashtable *ht) {
201 struct geniterator *gi=(struct geniterator*)RUNMALLOC(sizeof(struct geniterator));
206 void * gennext(struct geniterator *it) {
207 struct genpointerlist *curr=it->ptr;
210 if (it->finished&&(curr->inext==NULL))
216 if(curr->inext!=NULL)
219 it->finished=1; /* change offsetting scheme */
223 void genfreeiterator(struct geniterator *it) {