1 #include "SimpleHash.h"
4 /* SIMPLE HASH ********************************************************/
5 struct RuntimeIterator* RuntimeHashcreateiterator(struct RuntimeHash * thisvar) {
6 return allocateRuntimeIterator(thisvar->listhead);
9 void RuntimeHashiterator(struct RuntimeHash *thisvar, struct RuntimeIterator * it) {
10 it->cur=thisvar->listhead;
13 struct RuntimeHash * noargallocateRuntimeHash() {
14 return allocateRuntimeHash(100);
17 struct RuntimeHash * allocateRuntimeHash(int size) {
18 struct RuntimeHash *thisvar=(struct RuntimeHash *)RUNMALLOC(sizeof(struct RuntimeHash));
20 printf("Negative Hashtable size Exception\n");
24 thisvar->bucket = (struct RuntimeNode **) RUNMALLOC(sizeof(struct RuntimeNode *)*size);
25 /* Set allocation blocks*/
26 thisvar->listhead=NULL;
27 thisvar->listtail=NULL;
29 thisvar->numelements = 0;
33 void freeRuntimeHash(struct RuntimeHash *thisvar) {
34 struct RuntimeNode *ptr=thisvar->listhead;
35 RUNFREE(thisvar->bucket);
37 struct RuntimeNode *next=ptr->next;
44 inline int RuntimeHashcountset(struct RuntimeHash * thisvar) {
45 return thisvar->numelements;
48 int RuntimeHashfirstkey(struct RuntimeHash *thisvar) {
49 struct RuntimeNode *ptr=thisvar->listhead;
53 int RuntimeHashremove(struct RuntimeHash *thisvar, int key, int data) {
54 unsigned int hashkey = (unsigned int)key % thisvar->size;
56 struct RuntimeNode **ptr = &thisvar->bucket[hashkey];
60 if ((*ptr)->key == key && (*ptr)->data == data) {
61 struct RuntimeNode *toremove=*ptr;
64 if (toremove->lprev!=NULL)
65 toremove->lprev->lnext=toremove->lnext;
67 thisvar->listhead=toremove->lnext;
68 if (toremove->lnext!=NULL)
69 toremove->lnext->lprev=toremove->lprev;
71 thisvar->listtail=toremove->lprev;
74 thisvar->numelements--;
77 ptr = &((*ptr)->next);
83 void RuntimeHashrehash(struct RuntimeHash * thisvar) {
84 int newsize=thisvar->size;
85 struct RuntimeNode ** newbucket = (struct RuntimeNode **) RUNMALLOC(sizeof(struct RuntimeNode *)*newsize);
87 for(i=thisvar->size-1;i>=0;i--) {
88 struct RuntimeNode *ptr;
89 for(ptr=thisvar->bucket[i];ptr!=NULL;) {
90 struct RuntimeNode * nextptr=ptr->next;
91 unsigned int newhashkey=(unsigned int)ptr->key % newsize;
92 ptr->next=newbucket[newhashkey];
93 newbucket[newhashkey]=ptr;
97 thisvar->size=newsize;
98 RUNFREE(thisvar->bucket);
99 thisvar->bucket=newbucket;
102 int RuntimeHashadd(struct RuntimeHash * thisvar,int key, int data) {
104 unsigned int hashkey;
105 struct RuntimeNode **ptr;
107 if (thisvar->numelements>=thisvar->size) {
108 int newsize=2*thisvar->size+1;
109 struct RuntimeNode ** newbucket = (struct RuntimeNode **) RUNMALLOC(sizeof(struct RuntimeNode *)*newsize);
111 for(i=thisvar->size-1;i>=0;i--) {
112 struct RuntimeNode *ptr;
113 for(ptr=thisvar->bucket[i];ptr!=NULL;) {
114 struct RuntimeNode * nextptr=ptr->next;
115 unsigned int newhashkey=(unsigned int)ptr->key % newsize;
116 ptr->next=newbucket[newhashkey];
117 newbucket[newhashkey]=ptr;
121 thisvar->size=newsize;
122 RUNFREE(thisvar->bucket);
123 thisvar->bucket=newbucket;
126 hashkey = (unsigned int)key % thisvar->size;
127 ptr = &thisvar->bucket[hashkey];
129 /* check that thisvar key/object pair isn't already here */
130 /* TBD can be optimized for set v. relation */
133 if ((*ptr)->key == key && (*ptr)->data == data) {
136 ptr = &((*ptr)->next);
140 struct RuntimeNode *node=RUNMALLOC(sizeof(struct RuntimeNode));
145 if (thisvar->listhead==NULL) {
146 thisvar->listhead=node;
147 thisvar->listtail=node;
152 node->lnext=thisvar->listhead;
153 thisvar->listhead->lprev=node;
154 thisvar->listhead=node;
158 thisvar->numelements++;
162 bool RuntimeHashcontainskey(struct RuntimeHash *thisvar,int key) {
163 unsigned int hashkey = (unsigned int)key % thisvar->size;
165 struct RuntimeNode *ptr = thisvar->bucket[hashkey];
167 if (ptr->key == key) {
168 /* we already have thisvar object
169 stored in the hash so just return */
177 bool RuntimeHashcontainskeydata(struct RuntimeHash *thisvar, int key, int data) {
178 unsigned int hashkey = (unsigned int)key % thisvar->size;
180 struct RuntimeNode *ptr = thisvar->bucket[hashkey];
182 if (ptr->key == key && ptr->data == data) {
183 /* we already have thisvar object
184 stored in the hash so just return*/
192 int RuntimeHashcount(struct RuntimeHash *thisvar,int key) {
193 unsigned int hashkey = (unsigned int)key % thisvar->size;
196 struct RuntimeNode *ptr = thisvar->bucket[hashkey];
198 if (ptr->key == key) {
206 struct RuntimeHash * RuntimeHashimageSet(struct RuntimeHash *thisvar, int key) {
207 struct RuntimeHash * newset=allocateRuntimeHash(2*RuntimeHashcount(thisvar,key)+4);
208 unsigned int hashkey = (unsigned int)key % thisvar->size;
210 struct RuntimeNode *ptr = thisvar->bucket[hashkey];
212 if (ptr->key == key) {
213 RuntimeHashadd(newset,ptr->data,ptr->data);
220 int RuntimeHashget(struct RuntimeHash *thisvar, int key, int *data) {
221 unsigned int hashkey = (unsigned int)key % thisvar->size;
223 struct RuntimeNode *ptr = thisvar->bucket[hashkey];
225 if (ptr->key == key) {
227 return 1; /* success */
232 return 0; /* failure */
235 inline struct RuntimeIterator * noargallocateRuntimeIterator() {
236 return (struct RuntimeIterator*)RUNMALLOC(sizeof(struct RuntimeIterator));
239 inline struct RuntimeIterator * allocateRuntimeIterator(struct RuntimeNode *start) {
240 struct RuntimeIterator *thisvar=(struct RuntimeIterator*)RUNMALLOC(sizeof(struct RuntimeIterator));
241 thisvar->cur = start;
245 inline int RunhasNext(struct RuntimeIterator *thisvar) {
246 return (thisvar->cur!=NULL);
249 inline int Runnext(struct RuntimeIterator *thisvar) {
250 int curr=thisvar->cur->data;
251 thisvar->cur=thisvar->cur->next;
254 inline int Runkey(struct RuntimeIterator *thisvar) {
255 return thisvar->cur->key;