1 #include "ObjectHash.h"
3 #include "runtime_arch.h"
11 /* SIMPLE HASH ********************************************************/
12 struct ObjectIterator* ObjectHashcreateiterator(struct ObjectHash * thisvar) {
13 return allocateObjectIterator(thisvar->listhead);
16 void ObjectHashiterator(struct ObjectHash *thisvar, struct ObjectIterator * it) {
17 it->cur=thisvar->listhead;
20 struct ObjectHash * noargallocateObjectHash() {
21 return allocateObjectHash(100);
24 struct ObjectHash * allocateObjectHash(int size) {
25 struct ObjectHash *thisvar; //=(struct ObjectHash *)RUNMALLOC(sizeof(struct ObjectHash));
30 printf("Negative Hashtable size Exception\n");
34 thisvar=(struct ObjectHash *)RUNMALLOC(sizeof(struct ObjectHash));
36 thisvar->bucket = (struct ObjectNode **) RUNMALLOC(sizeof(struct ObjectNode *)*size);
37 /* Set allocation blocks*/
38 thisvar->listhead=NULL;
39 thisvar->listtail=NULL;
41 thisvar->numelements = 0;
45 void freeObjectHash(struct ObjectHash *thisvar) {
46 struct ObjectNode *ptr=thisvar->listhead;
47 RUNFREE(thisvar->bucket);
49 struct ObjectNode *next=ptr->lnext;
56 inline int ObjectHashcountset(struct ObjectHash * thisvar) {
57 return thisvar->numelements;
60 int ObjectHashfirstkey(struct ObjectHash *thisvar) {
61 struct ObjectNode *ptr=thisvar->listhead;
65 int ObjectHashremove(struct ObjectHash *thisvar, int key) {
66 unsigned int hashkey = (unsigned int)key % thisvar->size;
68 struct ObjectNode **ptr = &thisvar->bucket[hashkey];
72 if ((*ptr)->key == key) {
73 struct ObjectNode *toremove=*ptr;
76 if (toremove->lprev!=NULL) {
77 toremove->lprev->lnext=toremove->lnext;
79 thisvar->listhead=toremove->lnext;
81 if (toremove->lnext!=NULL) {
82 toremove->lnext->lprev=toremove->lprev;
84 thisvar->listtail=toremove->lprev;
88 thisvar->numelements--;
91 ptr = &((*ptr)->next);
97 void ObjectHashrehash(struct ObjectHash * thisvar) {
98 int newsize=thisvar->size;
99 struct ObjectNode ** newbucket = (struct ObjectNode **) RUNMALLOC(sizeof(struct ObjectNode *)*newsize);
101 for(i=thisvar->size-1; i>=0; i--) {
102 struct ObjectNode *ptr;
103 for(ptr=thisvar->bucket[i]; ptr!=NULL;) {
104 struct ObjectNode * nextptr=ptr->next;
105 unsigned int newhashkey=(unsigned int)ptr->key % newsize;
106 ptr->next=newbucket[newhashkey];
107 newbucket[newhashkey]=ptr;
111 thisvar->size=newsize;
112 RUNFREE(thisvar->bucket);
113 thisvar->bucket=newbucket;
116 int ObjectHashadd(struct ObjectHash * thisvar,int key, int data, int data2, int data3, int data4) {
118 unsigned int hashkey;
119 struct ObjectNode **ptr;
121 if (thisvar->numelements>=thisvar->size) {
122 int newsize=2*thisvar->size+1;
123 struct ObjectNode ** newbucket = (struct ObjectNode **) RUNMALLOC(sizeof(struct ObjectNode *)*newsize);
125 for(i=thisvar->size-1; i>=0; i--) {
126 struct ObjectNode *ptr;
127 for(ptr=thisvar->bucket[i]; ptr!=NULL;) {
128 struct ObjectNode * nextptr=ptr->next;
129 unsigned int newhashkey=(unsigned int)ptr->key % newsize;
130 ptr->next=newbucket[newhashkey];
131 newbucket[newhashkey]=ptr;
135 thisvar->size=newsize;
136 RUNFREE(thisvar->bucket);
137 thisvar->bucket=newbucket;
140 hashkey = (unsigned int)key % thisvar->size;
141 ptr = &thisvar->bucket[hashkey];
144 struct ObjectNode *node=RUNMALLOC(sizeof(struct ObjectNode));
152 if (thisvar->listhead==NULL) {
153 thisvar->listhead=node;
154 thisvar->listtail=node;
159 node->lnext=thisvar->listhead;
160 thisvar->listhead->lprev=node;
161 thisvar->listhead=node;
165 thisvar->numelements++;
170 int ObjectHashadd_I(struct ObjectHash * thisvar,int key, int data, int data2, int data3, int data4) {
172 unsigned int hashkey;
173 struct ObjectNode **ptr;
175 if (thisvar->numelements>=thisvar->size) {
176 int newsize=2*thisvar->size+1;
177 struct ObjectNode ** newbucket = (struct ObjectNode **) RUNMALLOC_I(sizeof(struct ObjectNode *)*newsize);
179 for(i=thisvar->size-1; i>=0; i--) {
180 struct ObjectNode *ptr;
181 for(ptr=thisvar->bucket[i]; ptr!=NULL;) {
182 struct ObjectNode * nextptr=ptr->next;
183 unsigned int newhashkey=(unsigned int)ptr->key % newsize;
184 ptr->next=newbucket[newhashkey];
185 newbucket[newhashkey]=ptr;
189 thisvar->size=newsize;
190 RUNFREE(thisvar->bucket);
191 thisvar->bucket=newbucket;
194 hashkey = (unsigned int)key % thisvar->size;
195 ptr = &thisvar->bucket[hashkey];
198 struct ObjectNode *node=RUNMALLOC_I(sizeof(struct ObjectNode));
206 if (thisvar->listhead==NULL) {
207 thisvar->listhead=node;
208 thisvar->listtail=node;
213 node->lnext=thisvar->listhead;
214 thisvar->listhead->lprev=node;
215 thisvar->listhead=node;
219 thisvar->numelements++;
224 bool ObjectHashcontainskey(struct ObjectHash *thisvar,int key) {
225 unsigned int hashkey = (unsigned int)key % thisvar->size;
227 struct ObjectNode *ptr = thisvar->bucket[hashkey];
229 if (ptr->key == key) {
230 /* we already have thisvar object
231 stored in the hash so just return */
239 bool ObjectHashcontainskeydata(struct ObjectHash *thisvar, int key, int data) {
240 unsigned int hashkey = (unsigned int)key % thisvar->size;
242 struct ObjectNode *ptr = thisvar->bucket[hashkey];
244 if (ptr->key == key && ptr->data == data) {
245 /* we already have thisvar object
246 stored in the hash so just return*/
254 int ObjectHashcount(struct ObjectHash *thisvar,int key) {
255 unsigned int hashkey = (unsigned int)key % thisvar->size;
258 struct ObjectNode *ptr = thisvar->bucket[hashkey];
260 if (ptr->key == key) {
268 int ObjectHashget(struct ObjectHash *thisvar, int key, int *data, int *data2, int *data3, int *data4) {
269 unsigned int hashkey = (unsigned int)key % thisvar->size;
271 struct ObjectNode *ptr = thisvar->bucket[hashkey];
273 if (ptr->key == key) {
278 return 1; /* success */
283 return 0; /* failure */
286 int ObjectHashupdate(struct ObjectHash *thisvar, int key, int data, int data2, int data3, int data4) {
287 unsigned int hashkey = (unsigned int)key % thisvar->size;
289 struct ObjectNode *ptr = thisvar->bucket[hashkey];
291 if (ptr->key == key) {
296 return 1; /* success */
300 return 0; /* failure */
304 inline struct ObjectIterator * noargallocateObjectIterator() {
305 return (struct ObjectIterator*)RUNMALLOC(sizeof(struct ObjectIterator));
308 inline struct ObjectIterator * allocateObjectIterator(struct ObjectNode *start) {
309 struct ObjectIterator *thisvar=(struct ObjectIterator*)RUNMALLOC(sizeof(struct ObjectIterator));
310 thisvar->cur = start;
314 inline int ObjhasNext(struct ObjectIterator *thisvar) {
315 return (thisvar->cur!=NULL);
318 inline int Objnext(struct ObjectIterator *thisvar) {
319 int curr=thisvar->cur->data;
320 thisvar->cur=thisvar->cur->lnext;
324 inline int Objkey(struct ObjectIterator *thisvar) {
325 return thisvar->cur->key;
328 inline int Objdata(struct ObjectIterator *thisvar) {
329 return thisvar->cur->data;
332 inline int Objdata2(struct ObjectIterator *thisvar) {
333 return thisvar->cur->data2;
336 inline int Objdata3(struct ObjectIterator *thisvar) {
337 return thisvar->cur->data3;
340 inline int Objdata4(struct ObjectIterator *thisvar) {
341 return thisvar->cur->data4;