1 #include "ObjectHash.h"
7 /* SIMPLE HASH ********************************************************/
8 struct ObjectIterator* ObjectHashcreateiterator(struct ObjectHash * thisvar) {
9 return allocateObjectIterator(thisvar->listhead);
12 void ObjectHashiterator(struct ObjectHash *thisvar, struct ObjectIterator * it) {
13 it->cur=thisvar->listhead;
16 struct ObjectHash * noargallocateObjectHash() {
17 return allocateObjectHash(100);
20 struct ObjectHash * allocateObjectHash(int size) {
21 struct ObjectHash *thisvar=(struct ObjectHash *)RUNMALLOC(sizeof(struct ObjectHash));
23 printf("Negative Hashtable size Exception\n");
27 thisvar->bucket = (struct ObjectNode **) RUNMALLOC(sizeof(struct ObjectNode *)*size);
28 /* Set allocation blocks*/
29 thisvar->listhead=NULL;
30 thisvar->listtail=NULL;
32 thisvar->numelements = 0;
36 void freeObjectHash(struct ObjectHash *thisvar) {
37 struct ObjectNode *ptr=thisvar->listhead;
38 RUNFREE(thisvar->bucket);
40 struct ObjectNode *next=ptr->lnext;
47 inline int ObjectHashcountset(struct ObjectHash * thisvar) {
48 return thisvar->numelements;
51 int ObjectHashfirstkey(struct ObjectHash *thisvar) {
52 struct ObjectNode *ptr=thisvar->listhead;
56 int ObjectHashremove(struct ObjectHash *thisvar, int key) {
57 unsigned int hashkey = (unsigned int)key % thisvar->size;
59 struct ObjectNode **ptr = &thisvar->bucket[hashkey];
63 if ((*ptr)->key == key) {
64 struct ObjectNode *toremove=*ptr;
67 if (toremove->lprev!=NULL)
68 toremove->lprev->lnext=toremove->lnext;
70 thisvar->listhead=toremove->lnext;
71 if (toremove->lnext!=NULL)
72 toremove->lnext->lprev=toremove->lprev;
74 thisvar->listtail=toremove->lprev;
77 thisvar->numelements--;
80 ptr = &((*ptr)->next);
86 void ObjectHashrehash(struct ObjectHash * thisvar) {
87 int newsize=thisvar->size;
88 struct ObjectNode ** newbucket = (struct ObjectNode **) RUNMALLOC(sizeof(struct ObjectNode *)*newsize);
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;
100 thisvar->size=newsize;
101 RUNFREE(thisvar->bucket);
102 thisvar->bucket=newbucket;
105 int ObjectHashadd(struct ObjectHash * thisvar,int key, int data, int data2, int data3, int data4) {
107 unsigned int hashkey;
108 struct ObjectNode **ptr;
110 if (thisvar->numelements>=thisvar->size) {
111 int newsize=2*thisvar->size+1;
112 struct ObjectNode ** newbucket = (struct ObjectNode **) RUNMALLOC(sizeof(struct ObjectNode *)*newsize);
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;
124 thisvar->size=newsize;
125 RUNFREE(thisvar->bucket);
126 thisvar->bucket=newbucket;
129 hashkey = (unsigned int)key % thisvar->size;
130 ptr = &thisvar->bucket[hashkey];
133 struct ObjectNode *node=RUNMALLOC(sizeof(struct ObjectNode));
141 if (thisvar->listhead==NULL) {
142 thisvar->listhead=node;
143 thisvar->listtail=node;
148 node->lnext=thisvar->listhead;
149 thisvar->listhead->lprev=node;
150 thisvar->listhead=node;
154 thisvar->numelements++;
158 bool ObjectHashcontainskey(struct ObjectHash *thisvar,int key) {
159 unsigned int hashkey = (unsigned int)key % thisvar->size;
161 struct ObjectNode *ptr = thisvar->bucket[hashkey];
163 if (ptr->key == key) {
164 /* we already have thisvar object
165 stored in the hash so just return */
173 bool ObjectHashcontainskeydata(struct ObjectHash *thisvar, int key, int data) {
174 unsigned int hashkey = (unsigned int)key % thisvar->size;
176 struct ObjectNode *ptr = thisvar->bucket[hashkey];
178 if (ptr->key == key && ptr->data == data) {
179 /* we already have thisvar object
180 stored in the hash so just return*/
188 int ObjectHashcount(struct ObjectHash *thisvar,int key) {
189 unsigned int hashkey = (unsigned int)key % thisvar->size;
192 struct ObjectNode *ptr = thisvar->bucket[hashkey];
194 if (ptr->key == key) {
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;
205 struct ObjectNode *ptr = thisvar->bucket[hashkey];
207 if (ptr->key == key) {
212 return 1; /* success */
217 return 0; /* failure */
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;
223 struct ObjectNode *ptr = thisvar->bucket[hashkey];
225 if (ptr->key == key) {
230 return 1; /* success */
234 return 0; /* failure */
238 inline struct ObjectIterator * noargallocateObjectIterator() {
239 return (struct ObjectIterator*)RUNMALLOC(sizeof(struct ObjectIterator));
242 inline struct ObjectIterator * allocateObjectIterator(struct ObjectNode *start) {
243 struct ObjectIterator *thisvar=(struct ObjectIterator*)RUNMALLOC(sizeof(struct ObjectIterator));
244 thisvar->cur = start;
248 inline int ObjhasNext(struct ObjectIterator *thisvar) {
249 return (thisvar->cur!=NULL);
252 inline int Objnext(struct ObjectIterator *thisvar) {
253 int curr=thisvar->cur->data;
254 thisvar->cur=thisvar->cur->lnext;
258 inline int Objkey(struct ObjectIterator *thisvar) {
259 return thisvar->cur->key;
262 inline int Objdata(struct ObjectIterator *thisvar) {
263 return thisvar->cur->data;
266 inline int Objdata2(struct ObjectIterator *thisvar) {
267 return thisvar->cur->data2;
270 inline int Objdata3(struct ObjectIterator *thisvar) {
271 return thisvar->cur->data3;
274 inline int Objdata4(struct ObjectIterator *thisvar) {
275 return thisvar->cur->data4;