8 #include "GenericHashtable.h"
10 //#include "dmalloc.h"
13 int genputtable(struct genhashtable *ht, void * key, void * object) {
14 unsigned int bin=genhashfunction(ht,key);
15 struct genpointerlist * newptrlist=(struct genpointerlist *) RUNMALLOC(sizeof(struct genpointerlist));
17 newptrlist->object=object;
18 newptrlist->next=ht->bins[bin];
19 newptrlist->inext=NULL;
20 /* maintain linked list of ht entries for iteration*/
24 newptrlist->iprev=NULL;
26 ht->last->inext=newptrlist;
27 newptrlist->iprev=ht->last;
30 ht->bins[bin]=newptrlist;
32 if(ht->counter>ht->currentsize&&ht->currentsize!=INT_MAX) {
33 /* Expand hashtable */
34 long newcurrentsize=(ht->currentsize<(INT_MAX/2))?ht->currentsize*2:INT_MAX;
35 long oldcurrentsize=ht->currentsize;
36 struct genpointerlist **newbins=(struct genpointerlist **) RUNMALLOC(sizeof (struct genpointerlist *)*newcurrentsize);
37 struct genpointerlist **oldbins=ht->bins;
39 for(j=0;j<newcurrentsize;j++) newbins[j]=NULL;
40 ht->currentsize=newcurrentsize;
41 for(i=0;i<oldcurrentsize;i++) {
42 struct genpointerlist * tmpptr=oldbins[i];
44 int hashcode=genhashfunction(ht, tmpptr->src);
45 struct genpointerlist *nextptr=tmpptr->next;
46 tmpptr->next=newbins[hashcode];
47 newbins[hashcode]=tmpptr;
57 int hashsize(struct genhashtable *ht) {
61 void genrehash(struct genhashtable * ht) {
62 /* Expand hashtable */
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 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) {