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 unsigned 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 struct genpointerlist **newbins=(struct genpointerlist **) RUNMALLOC(sizeof (struct genpointerlist *)*ht->currentsize);
64 struct genpointerlist **oldbins=ht->bins;
67 for(i=0;i<ht->currentsize;i++) {
68 struct genpointerlist * tmpptr=oldbins[i];
70 unsigned int hashcode=genhashfunction(ht, tmpptr->src);
71 struct genpointerlist *nextptr=tmpptr->next;
72 tmpptr->next=newbins[hashcode];
73 newbins[hashcode]=tmpptr;
81 void * gengettable(struct genhashtable *ht, void * key) {
82 struct genpointerlist * ptr=ht->bins[genhashfunction(ht,key)];
84 if (((ht->comp_function==NULL)&&(ptr->src==key))||((ht->comp_function!=NULL)&&(*ht->comp_function)(ptr->src,key)))
88 printf("XXXXXXXXX: COULDN'T FIND ENTRY FOR KEY %p\n",key);
92 void * getnext(struct genhashtable *ht, void * key) {
93 struct genpointerlist * ptr=ht->bins[genhashfunction(ht,key)];
95 if (((ht->comp_function==NULL)&&(ptr->src==key))||((ht->comp_function!=NULL)&&(*ht->comp_function)(ptr->src,key)))
96 if (ptr->inext!=NULL) {
97 return ptr->inext->src;
102 printf("XXXXXXXXX: COULDN'T FIND ENTRY FOR KEY %p...\n Likely concurrent removal--bad user!!!\n",key);
106 int gencontains(struct genhashtable *ht, void * key) {
107 struct genpointerlist * ptr=ht->bins[genhashfunction(ht,key)];
108 //printf("In gencontains2\n");fflush(NULL);
110 if (((ht->comp_function==NULL)&&(ptr->src==key))||((ht->comp_function!=NULL)&&(*ht->comp_function)(ptr->src,key)))
118 void genfreekey(struct genhashtable *ht, void * key) {
119 struct genpointerlist * ptr=ht->bins[genhashfunction(ht,key)];
121 if (((ht->comp_function==NULL)&&(ptr->src==key))||((ht->comp_function!=NULL)&&(*ht->comp_function)(ptr->src,key))) {
122 ht->bins[genhashfunction(ht,key)]=ptr->next;
130 if (ptr->iprev!=NULL)
131 ptr->iprev->inext=ptr->inext;
132 if (ptr->inext!=NULL)
133 ptr->inext->iprev=ptr->iprev;
139 while(ptr->next!=NULL) {
140 if (((ht->comp_function==NULL)&&(ptr->next->src==key))||((ht->comp_function!=NULL)&&(*ht->comp_function)(ptr->next->src,key))) {
141 struct genpointerlist *tmpptr=ptr->next;
142 ptr->next=tmpptr->next;
143 if (tmpptr==ht->list)
144 ht->list=tmpptr->inext;
145 if (tmpptr==ht->last)
146 ht->last=tmpptr->iprev;
147 if (tmpptr->iprev!=NULL)
148 tmpptr->iprev->inext=tmpptr->inext;
149 if (tmpptr->inext!=NULL)
150 tmpptr->inext->iprev=tmpptr->iprev;
157 printf("XXXXXXXXX: COULDN'T FIND ENTRY FOR KEY %p\n",key);
160 unsigned int genhashfunction(struct genhashtable *ht, void * key) {
161 if (ht->hash_function==NULL)
162 return ((long unsigned int)key) % ht->currentsize;
164 return ((*ht->hash_function)(key)) % ht->currentsize;
167 struct genhashtable * genallocatehashtable(unsigned int (*hash_function)(void *),int (*comp_function)(void *, void *)) {
168 struct genhashtable *ght=(struct genhashtable *) RUNMALLOC(sizeof(struct genhashtable));
169 struct genpointerlist **gpl=(struct genpointerlist **) RUNMALLOC(sizeof(struct genpointerlist *)*geninitialnumbins);
171 for(i=0;i<geninitialnumbins;i++)
173 ght->hash_function=hash_function;
174 ght->comp_function=comp_function;
175 ght->currentsize=geninitialnumbins;
183 void genfreehashtable(struct genhashtable * ht) {
185 for (i=0;i<ht->currentsize;i++) {
186 if (ht->bins[i]!=NULL) {
187 struct genpointerlist *genptr=ht->bins[i];
188 while(genptr!=NULL) {
189 struct genpointerlist *tmpptr=genptr->next;
199 struct geniterator * gengetiterator(struct genhashtable *ht) {
200 struct geniterator *gi=(struct geniterator*)RUNMALLOC(sizeof(struct geniterator));
205 void * gennext(struct geniterator *it) {
206 struct genpointerlist *curr=it->ptr;
209 if (it->finished&&(curr->inext==NULL))
215 if(curr->inext!=NULL)
218 it->finished=1; /* change offsetting scheme */
222 void genfreeiterator(struct geniterator *it) {