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) {
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));
139 if (thisvar->listhead==NULL) {
140 thisvar->listhead=node;
141 thisvar->listtail=node;
146 node->lnext=thisvar->listhead;
147 thisvar->listhead->lprev=node;
148 thisvar->listhead=node;
152 thisvar->numelements++;
156 bool ObjectHashcontainskey(struct ObjectHash *thisvar,int key) {
157 unsigned int hashkey = (unsigned int)key % thisvar->size;
159 struct ObjectNode *ptr = thisvar->bucket[hashkey];
161 if (ptr->key == key) {
162 /* we already have thisvar object
163 stored in the hash so just return */
171 bool ObjectHashcontainskeydata(struct ObjectHash *thisvar, int key, int data) {
172 unsigned int hashkey = (unsigned int)key % thisvar->size;
174 struct ObjectNode *ptr = thisvar->bucket[hashkey];
176 if (ptr->key == key && ptr->data == data) {
177 /* we already have thisvar object
178 stored in the hash so just return*/
186 int ObjectHashcount(struct ObjectHash *thisvar,int key) {
187 unsigned int hashkey = (unsigned int)key % thisvar->size;
190 struct ObjectNode *ptr = thisvar->bucket[hashkey];
192 if (ptr->key == key) {
200 int ObjectHashget(struct ObjectHash *thisvar, int key, int *data, int *data2) {
201 unsigned int hashkey = (unsigned int)key % thisvar->size;
203 struct ObjectNode *ptr = thisvar->bucket[hashkey];
205 if (ptr->key == key) {
208 return 1; /* success */
213 return 0; /* failure */
216 inline struct ObjectIterator * noargallocateObjectIterator() {
217 return (struct ObjectIterator*)RUNMALLOC(sizeof(struct ObjectIterator));
220 inline struct ObjectIterator * allocateObjectIterator(struct ObjectNode *start) {
221 struct ObjectIterator *thisvar=(struct ObjectIterator*)RUNMALLOC(sizeof(struct ObjectIterator));
222 thisvar->cur = start;
226 inline int ObjhasNext(struct ObjectIterator *thisvar) {
227 return (thisvar->cur!=NULL);
230 inline int Objnext(struct ObjectIterator *thisvar) {
231 int curr=thisvar->cur->data;
232 thisvar->cur=thisvar->cur->next;
235 inline int Objkey(struct ObjectIterator *thisvar) {
236 return thisvar->cur->key;