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];
71 if ((*ptr)->key == key) {
72 struct ObjectNode *toremove=*ptr;
75 if (toremove->lprev!=NULL) {
76 toremove->lprev->lnext=toremove->lnext;
78 thisvar->listhead=toremove->lnext;
80 if (toremove->lnext!=NULL) {
81 toremove->lnext->lprev=toremove->lprev;
83 thisvar->listtail=toremove->lprev;
87 thisvar->numelements--;
90 ptr = &((*ptr)->next);
96 void ObjectHashrehash(struct ObjectHash * thisvar) {
97 int newsize=thisvar->size;
98 struct ObjectNode ** newbucket = (struct ObjectNode **) RUNMALLOC(sizeof(struct ObjectNode *)*newsize);
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;
110 thisvar->size=newsize;
111 RUNFREE(thisvar->bucket);
112 thisvar->bucket=newbucket;
115 int ObjectHashadd(struct ObjectHash * thisvar,int key, int data, int data2, int data3, int data4) {
117 unsigned int hashkey;
118 struct ObjectNode **ptr;
120 if (thisvar->numelements>=thisvar->size) {
121 int newsize=2*thisvar->size+1;
122 struct ObjectNode ** newbucket = (struct ObjectNode **) RUNMALLOC(sizeof(struct ObjectNode *)*newsize);
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;
134 thisvar->size=newsize;
135 RUNFREE(thisvar->bucket);
136 thisvar->bucket=newbucket;
139 hashkey = (unsigned int)key % thisvar->size;
140 ptr = &thisvar->bucket[hashkey];
143 struct ObjectNode *node=RUNMALLOC(sizeof(struct ObjectNode));
151 if (thisvar->listhead==NULL) {
152 thisvar->listhead=node;
153 thisvar->listtail=node;
158 node->lnext=thisvar->listhead;
159 thisvar->listhead->lprev=node;
160 thisvar->listhead=node;
164 thisvar->numelements++;
169 int ObjectHashadd_I(struct ObjectHash * thisvar,int key, int data, int data2, int data3, int data4) {
171 unsigned int hashkey;
172 struct ObjectNode **ptr;
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);
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;
188 thisvar->size=newsize;
189 RUNFREE(thisvar->bucket);
190 thisvar->bucket=newbucket;
193 hashkey = (unsigned int)key % thisvar->size;
194 ptr = &thisvar->bucket[hashkey];
197 struct ObjectNode *node=RUNMALLOC_I(sizeof(struct ObjectNode));
205 if (thisvar->listhead==NULL) {
206 thisvar->listhead=node;
207 thisvar->listtail=node;
212 node->lnext=thisvar->listhead;
213 thisvar->listhead->lprev=node;
214 thisvar->listhead=node;
218 thisvar->numelements++;
223 bool ObjectHashcontainskey(struct ObjectHash *thisvar,int key) {
224 unsigned int hashkey = (unsigned int)key % thisvar->size;
226 struct ObjectNode *ptr = thisvar->bucket[hashkey];
228 if (ptr->key == key) {
229 /* we already have thisvar object
230 stored in the hash so just return */
238 bool ObjectHashcontainskeydata(struct ObjectHash *thisvar, int key, int data) {
239 unsigned int hashkey = (unsigned int)key % thisvar->size;
241 struct ObjectNode *ptr = thisvar->bucket[hashkey];
243 if (ptr->key == key && ptr->data == data) {
244 /* we already have thisvar object
245 stored in the hash so just return*/
253 int ObjectHashcount(struct ObjectHash *thisvar,int key) {
254 unsigned int hashkey = (unsigned int)key % thisvar->size;
257 struct ObjectNode *ptr = thisvar->bucket[hashkey];
259 if (ptr->key == key) {
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;
270 struct ObjectNode *ptr = thisvar->bucket[hashkey];
272 if (ptr->key == key) {
277 return 1; /* success */
282 return 0; /* failure */
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;
288 struct ObjectNode *ptr = thisvar->bucket[hashkey];
290 if (ptr->key == key) {
295 return 1; /* success */
299 return 0; /* failure */
303 inline struct ObjectIterator * noargallocateObjectIterator() {
304 return (struct ObjectIterator*)RUNMALLOC(sizeof(struct ObjectIterator));
307 inline struct ObjectIterator * allocateObjectIterator(struct ObjectNode *start) {
308 struct ObjectIterator *thisvar=(struct ObjectIterator*)RUNMALLOC(sizeof(struct ObjectIterator));
309 thisvar->cur = start;
313 inline int ObjhasNext(struct ObjectIterator *thisvar) {
314 return (thisvar->cur!=NULL);
317 inline int Objnext(struct ObjectIterator *thisvar) {
318 int curr=thisvar->cur->data;
319 thisvar->cur=thisvar->cur->lnext;
323 inline int Objkey(struct ObjectIterator *thisvar) {
324 return thisvar->cur->key;
327 inline int Objdata(struct ObjectIterator *thisvar) {
328 return thisvar->cur->data;
331 inline int Objdata2(struct ObjectIterator *thisvar) {
332 return thisvar->cur->data2;
335 inline int Objdata3(struct ObjectIterator *thisvar) {
336 return thisvar->cur->data3;
339 inline int Objdata4(struct ObjectIterator *thisvar) {
340 return thisvar->cur->data4;