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