1 #include "ObjectHash.h"
3 #include "methodheaders.h"
4 #include "runtime_arch.h"
5 #include "multicoreruntime.h"
14 /* SIMPLE HASH ********************************************************/
15 struct ObjectIterator* ObjectHashcreateiterator(struct ObjectHash * thisvar) {
16 return allocateObjectIterator(thisvar->listhead);
19 void ObjectHashiterator(struct ObjectHash *thisvar, struct ObjectIterator * it) {
20 it->cur=thisvar->listhead;
23 struct ObjectHash * noargallocateObjectHash() {
24 return allocateObjectHash(100);
27 struct ObjectHash * allocateObjectHash(int size) {
28 struct ObjectHash *thisvar;
33 printf("Negative Hashtable size Exception\n");
37 thisvar=(struct ObjectHash *)RUNMALLOC(sizeof(struct ObjectHash));
39 thisvar->bucket = (struct ObjectNode **) RUNMALLOC(sizeof(struct ObjectNode *)*size);
40 /* Set allocation blocks*/
41 thisvar->listhead=NULL;
42 thisvar->listtail=NULL;
44 thisvar->numelements = 0;
48 void freeObjectHash(struct ObjectHash *thisvar) {
49 struct ObjectNode *ptr=thisvar->listhead;
50 RUNFREE(thisvar->bucket);
52 struct ObjectNode *next=ptr->lnext;
59 inline int ObjectHashcountset(struct ObjectHash * thisvar) {
60 return thisvar->numelements;
63 int ObjectHashfirstkey(struct ObjectHash *thisvar) {
64 struct ObjectNode *ptr=thisvar->listhead;
68 int ObjectHashremove(struct ObjectHash *thisvar, int key) {
69 unsigned int hashkey = (unsigned int)key % thisvar->size;
71 struct ObjectNode **ptr = &thisvar->bucket[hashkey];
74 if ((*ptr)->key == key) {
75 struct ObjectNode *toremove=*ptr;
78 if (toremove->lprev!=NULL) {
79 toremove->lprev->lnext=toremove->lnext;
81 thisvar->listhead=toremove->lnext;
83 if (toremove->lnext!=NULL) {
84 toremove->lnext->lprev=toremove->lprev;
86 thisvar->listtail=toremove->lprev;
90 thisvar->numelements--;
93 ptr = &((*ptr)->next);
99 void ObjectHashrehash(struct ObjectHash * thisvar) {
100 int newsize=thisvar->size;
101 struct ObjectNode ** newbucket = (struct ObjectNode **) RUNMALLOC(sizeof(struct ObjectNode *)*newsize);
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;
113 thisvar->size=newsize;
114 RUNFREE(thisvar->bucket);
115 thisvar->bucket=newbucket;
118 int ObjectHashadd(struct ObjectHash * thisvar,int key, int data, int data2, int data3, int data4) {
120 unsigned int hashkey;
121 struct ObjectNode **ptr;
123 if (thisvar->numelements>=thisvar->size) {
124 int newsize=2*thisvar->size+1;
125 struct ObjectNode ** newbucket = (struct ObjectNode **) RUNMALLOC(sizeof(struct ObjectNode *)*newsize);
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;
137 thisvar->size=newsize;
138 RUNFREE(thisvar->bucket);
139 thisvar->bucket=newbucket;
142 hashkey = (unsigned int)key % thisvar->size;
143 ptr = &thisvar->bucket[hashkey];
146 struct ObjectNode *node=RUNMALLOC(sizeof(struct ObjectNode));
154 if (thisvar->listhead==NULL) {
155 thisvar->listhead=node;
156 thisvar->listtail=node;
161 node->lnext=thisvar->listhead;
162 thisvar->listhead->lprev=node;
163 thisvar->listhead=node;
167 thisvar->numelements++;
172 int ObjectHashadd_I(struct ObjectHash * thisvar,int key, int data, int data2, int data3, int data4) {
174 unsigned int hashkey;
175 struct ObjectNode **ptr;
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);
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;
191 thisvar->size=newsize;
192 RUNFREE_I(thisvar->bucket);
193 thisvar->bucket=newbucket;
196 hashkey = (unsigned int)key % thisvar->size;
197 ptr = &thisvar->bucket[hashkey];
200 struct ObjectNode *node=RUNMALLOC_I(sizeof(struct ObjectNode));
208 if (thisvar->listhead==NULL) {
209 thisvar->listhead=node;
210 thisvar->listtail=node;
215 node->lnext=thisvar->listhead;
216 thisvar->listhead->lprev=node;
217 thisvar->listhead=node;
221 thisvar->numelements++;
226 bool ObjectHashcontainskey(struct ObjectHash *thisvar,int key) {
227 unsigned int hashkey = (unsigned int)key % thisvar->size;
229 struct ObjectNode *ptr = thisvar->bucket[hashkey];
231 if (ptr->key == key) {
232 /* we already have thisvar object
233 stored in the hash so just return */
241 bool ObjectHashcontainskeydata(struct ObjectHash *thisvar, int key, int data) {
242 unsigned int hashkey = (unsigned int)key % thisvar->size;
244 struct ObjectNode *ptr = thisvar->bucket[hashkey];
246 if (ptr->key == key && ptr->data == data) {
247 /* we already have thisvar object
248 stored in the hash so just return*/
256 int ObjectHashcount(struct ObjectHash *thisvar,int key) {
257 unsigned int hashkey = (unsigned int)key % thisvar->size;
260 struct ObjectNode *ptr = thisvar->bucket[hashkey];
262 if (ptr->key == key) {
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;
273 struct ObjectNode *ptr = thisvar->bucket[hashkey];
275 if (ptr->key == key) {
280 return 1; /* success */
285 return 0; /* failure */
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;
291 struct ObjectNode *ptr = thisvar->bucket[hashkey];
293 if (ptr->key == key) {
298 return 1; /* success */
302 return 0; /* failure */
306 inline struct ObjectIterator * noargallocateObjectIterator() {
307 return (struct ObjectIterator*)RUNMALLOC(sizeof(struct ObjectIterator));
310 inline struct ObjectIterator * allocateObjectIterator(struct ObjectNode *start) {
311 struct ObjectIterator *thisvar=(struct ObjectIterator*)RUNMALLOC(sizeof(struct ObjectIterator));
312 thisvar->cur = start;
316 inline int ObjhasNext(struct ObjectIterator *thisvar) {
317 return (thisvar->cur!=NULL);
320 inline int Objnext(struct ObjectIterator *thisvar) {
321 int curr=thisvar->cur->data;
322 thisvar->cur=thisvar->cur->lnext;
326 inline int Objkey(struct ObjectIterator *thisvar) {
327 return thisvar->cur->key;
330 inline int Objdata(struct ObjectIterator *thisvar) {
331 return thisvar->cur->data;
334 inline int Objdata2(struct ObjectIterator *thisvar) {
335 return thisvar->cur->data2;
338 inline int Objdata3(struct ObjectIterator *thisvar) {
339 return thisvar->cur->data3;
342 inline int Objdata4(struct ObjectIterator *thisvar) {
343 return thisvar->cur->data4;