changes
[IRC.git] / Robust / src / Runtime / ObjectHash.c
1 #include "ObjectHash.h"
2 #ifdef MULTICORE
3 #include "runtime_arch.h"
4 #else
5 #include <stdio.h>
6 #endif
7 #ifdef DMALLOC
8 #include "dmalloc.h"
9 #endif
10
11 /* SIMPLE HASH ********************************************************/
12 struct ObjectIterator* ObjectHashcreateiterator(struct ObjectHash * thisvar) {
13   return allocateObjectIterator(thisvar->listhead);
14 }
15
16 void ObjectHashiterator(struct ObjectHash *thisvar, struct ObjectIterator * it) {
17   it->cur=thisvar->listhead;
18 }
19
20 struct ObjectHash * noargallocateObjectHash() {
21   return allocateObjectHash(100);
22 }
23
24 struct ObjectHash * allocateObjectHash(int size) {
25   struct ObjectHash *thisvar;  //=(struct ObjectHash *)RUNMALLOC(sizeof(struct ObjectHash));
26   if (size <= 0) {
27 #ifdef MULTICORE
28     BAMBOO_EXIT(0xf001);
29 #else
30     printf("Negative Hashtable size Exception\n");
31     exit(-1);
32 #endif
33   }
34   thisvar=(struct ObjectHash *)RUNMALLOC(sizeof(struct ObjectHash));
35   thisvar->size = size;
36   thisvar->bucket = (struct ObjectNode **) RUNMALLOC(sizeof(struct ObjectNode *)*size);
37   /* Set allocation blocks*/
38   thisvar->listhead=NULL;
39   thisvar->listtail=NULL;
40   /*Set data counts*/
41   thisvar->numelements = 0;
42   return thisvar;
43 }
44
45 void freeObjectHash(struct ObjectHash *thisvar) {
46   struct ObjectNode *ptr=thisvar->listhead;
47   RUNFREE(thisvar->bucket);
48   while(ptr) {
49     struct ObjectNode *next=ptr->lnext;
50     RUNFREE(ptr);
51     ptr=next;
52   }
53   RUNFREE(thisvar);
54 }
55
56 inline int ObjectHashcountset(struct ObjectHash * thisvar) {
57   return thisvar->numelements;
58 }
59
60 int ObjectHashfirstkey(struct ObjectHash *thisvar) {
61   struct ObjectNode *ptr=thisvar->listhead;
62   return ptr->key;
63 }
64
65 int ObjectHashremove(struct ObjectHash *thisvar, int key) {
66   unsigned int hashkey = (unsigned int)key % thisvar->size;
67
68   struct ObjectNode **ptr = &thisvar->bucket[hashkey];
69
70   while (*ptr) {
71     if ((*ptr)->key == key) {
72       struct ObjectNode *toremove=*ptr;
73       *ptr=(*ptr)->next;
74
75       if (toremove->lprev!=NULL) {
76         toremove->lprev->lnext=toremove->lnext;
77       } else {
78         thisvar->listhead=toremove->lnext;
79       }
80       if (toremove->lnext!=NULL) {
81         toremove->lnext->lprev=toremove->lprev;
82       } else {
83         thisvar->listtail=toremove->lprev;
84       }
85       RUNFREE(toremove);
86
87       thisvar->numelements--;
88       return 1;
89     }
90     ptr = &((*ptr)->next);
91   }
92
93   return 0;
94 }
95
96 void ObjectHashrehash(struct ObjectHash * thisvar) {
97   int newsize=thisvar->size;
98   struct ObjectNode ** newbucket = (struct ObjectNode **) RUNMALLOC(sizeof(struct ObjectNode *)*newsize);
99   int i;
100   for(i=thisvar->size-1; i>=0; i--) {
101     struct ObjectNode *ptr;
102     for(ptr=thisvar->bucket[i]; ptr!=NULL;) {
103       struct ObjectNode * nextptr=ptr->next;
104       unsigned int newhashkey=(unsigned int)ptr->key % newsize;
105       ptr->next=newbucket[newhashkey];
106       newbucket[newhashkey]=ptr;
107       ptr=nextptr;
108     }
109   }
110   thisvar->size=newsize;
111   RUNFREE(thisvar->bucket);
112   thisvar->bucket=newbucket;
113 }
114
115 int ObjectHashadd(struct ObjectHash * thisvar,int key, int data, int data2, int data3, int data4) {
116   /* Rehash code */
117   unsigned int hashkey;
118   struct ObjectNode **ptr;
119
120   if (thisvar->numelements>=thisvar->size) {
121     int newsize=2*thisvar->size+1;
122     struct ObjectNode ** newbucket = (struct ObjectNode **) RUNMALLOC(sizeof(struct ObjectNode *)*newsize);
123     int i;
124     for(i=thisvar->size-1; i>=0; i--) {
125       struct ObjectNode *ptr;
126       for(ptr=thisvar->bucket[i]; ptr!=NULL;) {
127         struct ObjectNode * nextptr=ptr->next;
128         unsigned int newhashkey=(unsigned int)ptr->key % newsize;
129         ptr->next=newbucket[newhashkey];
130         newbucket[newhashkey]=ptr;
131         ptr=nextptr;
132       }
133     }
134     thisvar->size=newsize;
135     RUNFREE(thisvar->bucket);
136     thisvar->bucket=newbucket;
137   }
138
139   hashkey = (unsigned int)key % thisvar->size;
140   ptr = &thisvar->bucket[hashkey];
141
142   {
143     struct ObjectNode *node=RUNMALLOC(sizeof(struct ObjectNode));
144     node->data=data;
145     node->data2=data2;
146     node->data3=data3;
147     node->data4=data4;
148     node->key=key;
149     node->next=(*ptr);
150     *ptr=node;
151     if (thisvar->listhead==NULL) {
152       thisvar->listhead=node;
153       thisvar->listtail=node;
154       node->lnext=NULL;
155       node->lprev=NULL;
156     } else {
157       node->lprev=NULL;
158       node->lnext=thisvar->listhead;
159       thisvar->listhead->lprev=node;
160       thisvar->listhead=node;
161     }
162   }
163
164   thisvar->numelements++;
165   return 1;
166 }
167
168 #ifdef MULTICORE
169 int ObjectHashadd_I(struct ObjectHash * thisvar,int key, int data, int data2, int data3, int data4) {
170   /* Rehash code */
171   unsigned int hashkey;
172   struct ObjectNode **ptr;
173
174   if (thisvar->numelements>=thisvar->size) {
175     int newsize=2*thisvar->size+1;
176     struct ObjectNode ** newbucket = (struct ObjectNode **) RUNMALLOC_I(sizeof(struct ObjectNode *)*newsize);
177     int i;
178     for(i=thisvar->size-1; i>=0; i--) {
179       struct ObjectNode *ptr;
180       for(ptr=thisvar->bucket[i]; ptr!=NULL;) {
181         struct ObjectNode * nextptr=ptr->next;
182         unsigned int newhashkey=(unsigned int)ptr->key % newsize;
183         ptr->next=newbucket[newhashkey];
184         newbucket[newhashkey]=ptr;
185         ptr=nextptr;
186       }
187     }
188     thisvar->size=newsize;
189     RUNFREE(thisvar->bucket);
190     thisvar->bucket=newbucket;
191   }
192
193   hashkey = (unsigned int)key % thisvar->size;
194   ptr = &thisvar->bucket[hashkey];
195
196   {
197     struct ObjectNode *node=RUNMALLOC_I(sizeof(struct ObjectNode));
198     node->data=data;
199     node->data2=data2;
200     node->data3=data3;
201     node->data4=data4;
202     node->key=key;
203     node->next=(*ptr);
204     *ptr=node;
205     if (thisvar->listhead==NULL) {
206       thisvar->listhead=node;
207       thisvar->listtail=node;
208       node->lnext=NULL;
209       node->lprev=NULL;
210     } else {
211       node->lprev=NULL;
212       node->lnext=thisvar->listhead;
213       thisvar->listhead->lprev=node;
214       thisvar->listhead=node;
215     }
216   }
217
218   thisvar->numelements++;
219   return 1;
220 }
221 #endif
222
223 bool ObjectHashcontainskey(struct ObjectHash *thisvar,int key) {
224   unsigned int hashkey = (unsigned int)key % thisvar->size;
225
226   struct ObjectNode *ptr = thisvar->bucket[hashkey];
227   while (ptr) {
228     if (ptr->key == key) {
229       /* we already have thisvar object
230          stored in the hash so just return */
231       return true;
232     }
233     ptr = ptr->next;
234   }
235   return false;
236 }
237
238 bool ObjectHashcontainskeydata(struct ObjectHash *thisvar, int key, int data) {
239   unsigned int hashkey = (unsigned int)key % thisvar->size;
240
241   struct ObjectNode *ptr = thisvar->bucket[hashkey];
242   while (ptr) {
243     if (ptr->key == key && ptr->data == data) {
244       /* we already have thisvar object
245          stored in the hash so just return*/
246       return true;
247     }
248     ptr = ptr->next;
249   }
250   return false;
251 }
252
253 int ObjectHashcount(struct ObjectHash *thisvar,int key) {
254   unsigned int hashkey = (unsigned int)key % thisvar->size;
255   int count = 0;
256
257   struct ObjectNode *ptr = thisvar->bucket[hashkey];
258   while (ptr) {
259     if (ptr->key == key) {
260       count++;
261     }
262     ptr = ptr->next;
263   }
264   return count;
265 }
266
267 int ObjectHashget(struct ObjectHash *thisvar, int key, int *data, int *data2, int *data3, int *data4) {
268   unsigned int hashkey = (unsigned int)key % thisvar->size;
269
270   struct ObjectNode *ptr = thisvar->bucket[hashkey];
271   while (ptr) {
272     if (ptr->key == key) {
273       *data = ptr->data;
274       *data2 = ptr->data2;
275       *data3 = ptr->data3;
276       *data4 = ptr->data4;
277       return 1;       /* success */
278     }
279     ptr = ptr->next;
280   }
281
282   return 0;   /* failure */
283 }
284
285 int ObjectHashupdate(struct ObjectHash *thisvar, int key, int data, int data2, int data3, int data4) {
286   unsigned int hashkey = (unsigned int)key % thisvar->size;
287
288   struct ObjectNode *ptr = thisvar->bucket[hashkey];
289   while (ptr) {
290     if (ptr->key == key) {
291       ptr->data=data;
292       ptr->data2=data2;
293       ptr->data3=data3;
294       ptr->data4=data4;
295       return 1;     /* success */
296     }
297     ptr = ptr->next;
298   }
299   return 0;   /* failure */
300 }
301
302
303 inline struct ObjectIterator * noargallocateObjectIterator() {
304   return (struct ObjectIterator*)RUNMALLOC(sizeof(struct ObjectIterator));
305 }
306
307 inline struct ObjectIterator * allocateObjectIterator(struct ObjectNode *start) {
308   struct ObjectIterator *thisvar=(struct ObjectIterator*)RUNMALLOC(sizeof(struct ObjectIterator));
309   thisvar->cur = start;
310   return thisvar;
311 }
312
313 inline int ObjhasNext(struct ObjectIterator *thisvar) {
314   return (thisvar->cur!=NULL);
315 }
316
317 inline int Objnext(struct ObjectIterator *thisvar) {
318   int curr=thisvar->cur->data;
319   thisvar->cur=thisvar->cur->lnext;
320   return curr;
321 }
322
323 inline int Objkey(struct ObjectIterator *thisvar) {
324   return thisvar->cur->key;
325 }
326
327 inline int Objdata(struct ObjectIterator *thisvar) {
328   return thisvar->cur->data;
329 }
330
331 inline int Objdata2(struct ObjectIterator *thisvar) {
332   return thisvar->cur->data2;
333 }
334
335 inline int Objdata3(struct ObjectIterator *thisvar) {
336   return thisvar->cur->data3;
337 }
338
339 inline int Objdata4(struct ObjectIterator *thisvar) {
340   return thisvar->cur->data4;
341 }