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 * gengettable(struct genhashtable *ht, void * key) {
62 struct genpointerlist * ptr=ht->bins[genhashfunction(ht,key)];
64 if (((ht->comp_function==NULL)&&(ptr->src==key))||((ht->comp_function!=NULL)&&(*ht->comp_function)(ptr->src,key)))
68 printf("XXXXXXXXX: COULDN'T FIND ENTRY FOR KEY %p\n",key);
72 void * getnext(struct genhashtable *ht, void * key) {
73 struct genpointerlist * ptr=ht->bins[genhashfunction(ht,key)];
75 if (((ht->comp_function==NULL)&&(ptr->src==key))||((ht->comp_function!=NULL)&&(*ht->comp_function)(ptr->src,key)))
76 if (ptr->inext!=NULL) {
77 return ptr->inext->src;
82 printf("XXXXXXXXX: COULDN'T FIND ENTRY FOR KEY %p...\n Likely concurrent removal--bad user!!!\n",key);
86 int gencontains(struct genhashtable *ht, void * key) {
87 struct genpointerlist * ptr=ht->bins[genhashfunction(ht,key)];
88 //printf("In gencontains2\n");fflush(NULL);
90 if (((ht->comp_function==NULL)&&(ptr->src==key))||((ht->comp_function!=NULL)&&(*ht->comp_function)(ptr->src,key)))
98 void genfreekey(struct genhashtable *ht, void * key) {
99 struct genpointerlist * ptr=ht->bins[genhashfunction(ht,key)];
101 if (((ht->comp_function==NULL)&&(ptr->src==key))||((ht->comp_function!=NULL)&&(*ht->comp_function)(ptr->src,key))) {
102 ht->bins[genhashfunction(ht,key)]=ptr->next;
110 if (ptr->iprev!=NULL)
111 ptr->iprev->inext=ptr->inext;
112 if (ptr->inext!=NULL)
113 ptr->inext->iprev=ptr->iprev;
119 while(ptr->next!=NULL) {
120 if (((ht->comp_function==NULL)&&(ptr->next->src==key))||((ht->comp_function!=NULL)&&(*ht->comp_function)(ptr->next->src,key))) {
121 struct genpointerlist *tmpptr=ptr->next;
122 ptr->next=tmpptr->next;
123 if (tmpptr==ht->list)
124 ht->list=tmpptr->inext;
125 if (tmpptr==ht->last)
126 ht->last=tmpptr->iprev;
127 if (tmpptr->iprev!=NULL)
128 tmpptr->iprev->inext=tmpptr->inext;
129 if (tmpptr->inext!=NULL)
130 tmpptr->inext->iprev=tmpptr->iprev;
137 printf("XXXXXXXXX: COULDN'T FIND ENTRY FOR KEY %p\n",key);
140 unsigned int genhashfunction(struct genhashtable *ht, void * key) {
141 if (ht->hash_function==NULL)
142 return ((long unsigned int)key) % ht->currentsize;
144 return ((*ht->hash_function)(key)) % ht->currentsize;
147 struct genhashtable * genallocatehashtable(unsigned int (*hash_function)(void *),int (*comp_function)(void *, void *)) {
148 struct genhashtable *ght=(struct genhashtable *) RUNMALLOC(sizeof(struct genhashtable));
149 struct genpointerlist **gpl=(struct genpointerlist **) RUNMALLOC(sizeof(struct genpointerlist *)*geninitialnumbins);
151 for(i=0;i<geninitialnumbins;i++)
153 ght->hash_function=hash_function;
154 ght->comp_function=comp_function;
155 ght->currentsize=geninitialnumbins;
163 void genfreehashtable(struct genhashtable * ht) {
165 for (i=0;i<ht->currentsize;i++) {
166 if (ht->bins[i]!=NULL) {
167 struct genpointerlist *genptr=ht->bins[i];
168 while(genptr!=NULL) {
169 struct genpointerlist *tmpptr=genptr->next;
179 struct geniterator * gengetiterator(struct genhashtable *ht) {
180 struct geniterator *gi=(struct geniterator*)RUNMALLOC(sizeof(struct geniterator));
185 void * gennext(struct geniterator *it) {
186 struct genpointerlist *curr=it->ptr;
189 if (it->finished&&(curr->inext==NULL))
195 if(curr->inext!=NULL)
198 it->finished=1; /* change offsetting scheme */
202 void genfreeiterator(struct geniterator *it) {