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