7 #include "GenericHashtable.h"
10 int genputtable(struct genhashtable *ht, void * key, void * object) {
11 unsigned int bin=genhashfunction(ht,key);
12 struct genpointerlist * newptrlist=(struct genpointerlist *) new genpointerlist;
14 newptrlist->object=object;
15 newptrlist->next=ht->bins[bin];
16 newptrlist->inext=NULL;
17 /* maintain linked list of ht entries for iteration*/
21 newptrlist->iprev=NULL;
23 ht->last->inext=newptrlist;
24 newptrlist->iprev=ht->last;
27 ht->bins[bin]=newptrlist;
29 if(ht->counter>ht->currentsize&&ht->currentsize!=MAXINT) {
30 /* Expand hashtable */
31 long newcurrentsize=(ht->currentsize<(MAXINT/2))?ht->currentsize*2:MAXINT;
32 long oldcurrentsize=ht->currentsize;
33 struct genpointerlist **newbins=(struct genpointerlist **) new genpointerlist*[newcurrentsize];
34 struct genpointerlist **oldbins=ht->bins;
35 for(long j=0;j<newcurrentsize;j++) newbins[j]=NULL;
37 ht->currentsize=newcurrentsize;
38 for(i=0;i<oldcurrentsize;i++) {
39 struct genpointerlist * tmpptr=oldbins[i];
41 int hashcode=genhashfunction(ht, tmpptr->src);
42 struct genpointerlist *nextptr=tmpptr->next;
43 tmpptr->next=newbins[hashcode];
44 newbins[hashcode]=tmpptr;
54 int hashsize(struct genhashtable *ht) {
58 void * gengettable(struct genhashtable *ht, void * key) {
59 struct genpointerlist * ptr=ht->bins[genhashfunction(ht,key)];
61 if (((ht->comp_function==NULL)&&(ptr->src==key))||((ht->comp_function!=NULL)&&(*ht->comp_function)(ptr->src,key)))
65 printf("XXXXXXXXX: COULDN'T FIND ENTRY FOR KEY %p\n",key);
69 void * getnext(struct genhashtable *ht, void * key) {
70 struct genpointerlist * ptr=ht->bins[genhashfunction(ht,key)];
72 if (((ht->comp_function==NULL)&&(ptr->src==key))||((ht->comp_function!=NULL)&&(*ht->comp_function)(ptr->src,key)))
73 if (ptr->inext!=NULL) {
74 return ptr->inext->src;
79 printf("XXXXXXXXX: COULDN'T FIND ENTRY FOR KEY %p...\n Likely concurrent removal--bad user!!!\n",key);
83 int gencontains(struct genhashtable *ht, void * key) {
84 struct genpointerlist * ptr=ht->bins[genhashfunction(ht,key)];
85 //printf("In gencontains2\n");fflush(NULL);
87 if (((ht->comp_function==NULL)&&(ptr->src==key))||((ht->comp_function!=NULL)&&(*ht->comp_function)(ptr->src,key)))
95 void genfreekey(struct genhashtable *ht, void * key) {
96 struct genpointerlist * ptr=ht->bins[genhashfunction(ht,key)];
98 if (((ht->comp_function==NULL)&&(ptr->src==key))||((ht->comp_function!=NULL)&&(*ht->comp_function)(ptr->src,key))) {
99 ht->bins[genhashfunction(ht,key)]=ptr->next;
107 if (ptr->iprev!=NULL)
108 ptr->iprev->inext=ptr->inext;
109 if (ptr->inext!=NULL)
110 ptr->inext->iprev=ptr->iprev;
116 while(ptr->next!=NULL) {
117 if (((ht->comp_function==NULL)&&(ptr->next->src==key))||((ht->comp_function!=NULL)&&(*ht->comp_function)(ptr->next->src,key))) {
118 struct genpointerlist *tmpptr=ptr->next;
119 ptr->next=tmpptr->next;
120 if (tmpptr==ht->list)
121 ht->list=tmpptr->inext;
122 if (tmpptr==ht->last)
123 ht->last=tmpptr->iprev;
124 if (tmpptr->iprev!=NULL)
125 tmpptr->iprev->inext=tmpptr->inext;
126 if (tmpptr->inext!=NULL)
127 tmpptr->inext->iprev=tmpptr->iprev;
134 printf("XXXXXXXXX: COULDN'T FIND ENTRY FOR KEY %p\n",key);
137 unsigned int genhashfunction(struct genhashtable *ht, void * key) {
138 if (ht->hash_function==NULL)
139 return ((long unsigned int)key) % ht->currentsize;
141 return ((*ht->hash_function)(key)) % ht->currentsize;
144 struct genhashtable * genallocatehashtable(unsigned int (*hash_function)(void *),int (*comp_function)(void *, void *)) {
145 struct genhashtable *ght=(struct genhashtable *) new genhashtable;
146 struct genpointerlist **gpl=(struct genpointerlist **) new genpointerlist *[geninitialnumbins];
147 for(int i=0;i<geninitialnumbins;i++)
149 ght->hash_function=hash_function;
150 ght->comp_function=comp_function;
151 ght->currentsize=geninitialnumbins;
159 void genfreehashtable(struct genhashtable * ht) {
161 for (i=0;i<ht->currentsize;i++) {
162 if (ht->bins[i]!=NULL) {
163 struct genpointerlist *genptr=ht->bins[i];
164 while(genptr!=NULL) {
165 struct genpointerlist *tmpptr=genptr->next;
175 struct geniterator * gengetiterator(struct genhashtable *ht) {
176 struct geniterator *gi=new geniterator();
181 void * gennext(struct geniterator *it) {
182 struct genpointerlist *curr=it->ptr;
185 if (it->finished&&(curr->inext==NULL))
191 if(curr->inext!=NULL)
194 it->finished=true; /* change offsetting scheme */
198 void genfreeiterator(struct geniterator *it) {