Forgot to implement this method.
[repair.git] / Repair / RepairCompiler / MCC / Runtime / SimpleHash.cc
1 #include "SimpleHash.h"
2 #include <stdarg.h>
3 #include <stdlib.h>
4
5 /* LINKED HASH NODE ****************************************************/
6
7 LinkedHashNode::LinkedHashNode(int key, int data, LinkedHashNode *next) {
8     this->key = key;
9     this->data = data;
10     this->next = next;
11     this->lnext=0;
12     this->lprev=0;
13 }
14
15 LinkedHashNode::LinkedHashNode() {
16     this->key = -1;
17     this->data = -1;
18     this->next = 0;
19     this->lnext=0;
20     this->lprev=0;
21 }
22
23 /* SIMPLE LIST ****************************************************/
24
25 SimpleList::SimpleList() {
26     ptr = 0;
27     head.next = 0;
28 }
29
30 void SimpleList::reset() {
31   // ptr = head.next;
32   ptr = &head;
33 }
34
35 int SimpleList::hasMoreElements() {
36   //  return (ptr != 0);
37   return (ptr->next != 0);
38 }
39
40 int SimpleList::nextElement() {
41   ptr = ptr->next;
42   return ptr->data;
43
44   //int data = ptr->data;
45   //ptr = ptr->next;
46   //return data;
47 }
48
49 void SimpleList::add(int data) {
50     LinkedHashNode *cur = &head;
51     while (cur->next) {
52         cur = cur->next;
53         if (cur->data == data) {
54             return; // no duplicates
55         }
56     }
57     cur->next = new LinkedHashNode(0, data, 0);
58     return;
59 }
60
61 int SimpleList::contains(int data) {
62     LinkedHashNode *cur = &head;
63     while (cur->next) {
64         cur = cur->next;
65         if (cur->data == data) {
66             return 1; // found!
67         }
68     }
69     return 0;    
70 }
71
72 /* WORK LIST ****************************************************/
73
74 WorkList::WorkList() {
75   head=(struct ListNode *) malloc(sizeof(struct ListNode));
76   tail=head;
77   head->next=0;
78   headoffset=0;
79   tailoffset=0;
80 }
81
82 int WorkList::hasMoreElements() {
83   //  return (ptr != 0);
84   return ((head!=tail)||(headoffset!=tailoffset));
85 }
86
87 int WorkList::getid() {
88   return tail->data[tailoffset];
89 }
90
91 int WorkList::gettype() {
92   return tail->data[tailoffset+1];
93 }
94
95 int WorkList::getlvalue() {
96   return tail->data[tailoffset+2];
97 }
98
99 int WorkList::getrvalue() {
100   return tail->data[tailoffset+3];
101 }
102
103 WorkList::~WorkList() {
104   struct ListNode *ptr=tail;
105   while(ptr) {
106     struct ListNode *oldptr=ptr;
107     ptr=ptr->next;
108     free(oldptr);
109   }
110 }
111
112 void WorkList::pop() {
113   int newoffset=tailoffset+4;
114   struct ListNode *ptr=tail;
115   if (newoffset>=WLISTSIZE) {
116     newoffset-=WLISTSIZE;
117     struct ListNode *oldptr=ptr;
118     ptr=ptr->next;
119     free(oldptr);
120   }
121   tail=ptr;
122   tailoffset=newoffset;
123 }
124
125 void WorkList::add(int id,int type, int lvalue, int rvalue) {
126   if (headoffset==WLISTSIZE) {
127     head->next=(struct ListNode *)malloc(sizeof(struct ListNode));
128     headoffset=0;
129     head=head->next;
130     head->next=0;
131   }
132   head->data[headoffset++]=id;
133   head->data[headoffset++]=type;
134   head->data[headoffset++]=lvalue;
135   head->data[headoffset++]=rvalue;
136 }
137
138 /* SIMPLE HASH ********************************************************/
139 SimpleIterator* SimpleHash::iterator() {
140   return new SimpleIterator(listhead,listtail,tailindex/*,this*/);
141 }
142
143 void SimpleHash::iterator(SimpleIterator & it) {
144   //  it.table=this;
145   it.cur=listhead;
146   it.index=0;
147   it.tailindex=tailindex;
148   it.tail=listtail;
149 }
150
151 SimpleHash::SimpleHash(int size) {
152     if (size <= 0) {
153         throw SimpleHashException();
154     }
155     this->size = size;
156     this->bucket = (struct SimpleNode **) calloc(sizeof(struct SimpleNode *)*size,1);
157     /* Set allocation blocks*/
158     this->listhead=(struct ArraySimple *) calloc(sizeof(struct ArraySimple),1);
159     this->listtail=this->listhead;
160     this->tailindex=0;
161     /*Set data counts*/
162     this->numparents = 0;
163     this->numchildren = 0;
164     this->numelements = 0;
165 }
166
167 SimpleHash::~SimpleHash() {
168   free(bucket);
169   struct ArraySimple *ptr=listhead;
170   while(ptr) {
171       struct ArraySimple *next=ptr->nextarray;
172       free(ptr);
173       ptr=next;
174   }
175 }
176
177 int SimpleHash::firstkey() {
178   struct ArraySimple *ptr=listhead;
179   int index=0;
180   while((index==ARRAYSIZE)||!ptr->nodes[index].inuse) {
181     if (index==ARRAYSIZE) {
182       index=0;
183       ptr=ptr->nextarray;
184     } else
185       index++;
186   }
187   return ptr->nodes[index].key;
188 }
189
190 void SimpleHash::addParent(SimpleHash* parent) {
191     parents[numparents++] = parent;
192     parent->addChild(this);
193 }
194
195 void SimpleHash::addChild(SimpleHash *child) {
196   children[numchildren++]=child;
197 }
198
199 int SimpleHash::remove(int key, int data) {
200     unsigned int hashkey = (unsigned int)key % size;
201     
202     struct SimpleNode **ptr = &bucket[hashkey];
203
204     for (int i = 0; i < numchildren; i++) {
205       children[i]->remove(key, data);
206     }
207
208     while (*ptr) {
209         if ((*ptr)->key == key && (*ptr)->data == data) {
210           struct SimpleNode *toremove=*ptr;
211           *ptr=(*ptr)->next;
212
213           toremove->inuse=0; /* Marked as unused */
214
215           numelements--;
216           return 1;
217         }
218         ptr = &((*ptr)->next);
219     }
220
221     return 0;
222 }
223
224
225
226 int SimpleHash::add(int key, int data) {
227     unsigned int hashkey = (unsigned int)key % size;
228     
229     struct SimpleNode **ptr = &bucket[hashkey];
230
231     /* check that this key/object pair isn't already here */
232     // TBD can be optimized for set v. relation */
233     while (*ptr) {
234         if ((*ptr)->key == key && (*ptr)->data == data) {
235             return 0;
236         }
237         ptr = &((*ptr)->next);
238     }
239     if (tailindex==ARRAYSIZE) {
240       listtail->nextarray=(struct ArraySimple *) calloc(sizeof(struct ArraySimple),1);
241       tailindex=0;
242       listtail=listtail->nextarray;
243     }
244     
245     *ptr = &listtail->nodes[tailindex++];
246     (*ptr)->key=key;
247     (*ptr)->data=data;
248     (*ptr)->inuse=1;
249
250     numelements++;
251     
252     for (int i = 0; i < numparents; i++) {
253         parents[i]->add(key, data);
254     }
255
256     return 1;
257 }
258
259 bool SimpleHash::contains(int key) {
260     unsigned int hashkey = (unsigned int)key % size;
261     
262     struct SimpleNode *ptr = bucket[hashkey];
263     while (ptr) {
264         if (ptr->key == key) {
265             // we already have this object 
266             // stored in the hash so just return
267             return true;
268         }
269         ptr = ptr->next;
270     }
271     return false;
272 }
273
274 bool SimpleHash::contains(int key, int data) {
275     unsigned int hashkey = (unsigned int)key % size;
276     
277     struct SimpleNode *ptr = bucket[hashkey];
278     while (ptr) {
279         if (ptr->key == key && ptr->data == data) {
280             // we already have this object 
281             // stored in the hash so just return
282             return true;
283         }
284         ptr = ptr->next;
285     }
286     return false;
287 }
288
289 int SimpleHash::count(int key) {
290     unsigned int hashkey = (unsigned int)key % size;
291     int count = 0;
292
293     struct SimpleNode *ptr = bucket[hashkey];
294     while (ptr) {
295         if (ptr->key == key) {
296             count++;
297         }
298         ptr = ptr->next;
299     }
300     return count;
301 }
302
303 int SimpleHash::get(int key, int&data) {
304     unsigned int hashkey = (unsigned int)key % size;
305     
306     struct SimpleNode *ptr = bucket[hashkey];
307     while (ptr) {
308         if (ptr->key == key) {
309             data = ptr->data;
310             return 1; // success
311         }
312         ptr = ptr->next;
313     }
314         
315     return 0; // failure
316 }
317
318 int SimpleHash::countdata(int data) {
319     int count = 0;
320     struct ArraySimple *ptr = listhead;
321     while(ptr) {
322       if (ptr->nextarray) {
323         for(int i=0;i<ARRAYSIZE;i++)
324           if (ptr->nodes[i].data == data
325               &&ptr->nodes[i].inuse) {
326             count++;
327           }
328       } else {
329         for(int i=0;i<tailindex;i++)
330           if (ptr->nodes[i].data == data
331               &&ptr->nodes[i].inuse) {
332             count++;
333           }
334       }
335       ptr = ptr->nextarray;
336     }
337     return count;
338 }
339
340 SimpleHashException::SimpleHashException() {}
341 // ************************************************************
342
343
344 RepairHashNode::RepairHashNode(int setrelation, int rule, int lvalue, int rvalue, int data, int data2){
345     this->setrelation = setrelation;
346     this->lvalue=lvalue;
347     this->rvalue=rvalue;
348     this->data = data;
349     this->data2 = data2;
350     this->next = 0;
351     this->lnext=0;
352     this->rule=rule;
353 }
354
355 // ************************************************************
356
357 RepairHash::RepairHash(int size) {
358     if (size <= 0) {
359         throw SimpleHashException();
360     }
361     this->size = size;
362     this->bucket = new RepairHashNode* [size];
363     for (int i=0;i<size;i++)
364       bucket[i]=0;
365     this->nodelist=0;
366     this->numelements = 0;
367 }
368
369 RepairHash::RepairHash() {
370   RepairHash(100);
371 }
372
373 RepairHash::~RepairHash() {
374   delete [] this->bucket;
375   RepairHashNode *ptr=nodelist;
376   while(ptr) {
377       RepairHashNode *next=ptr->lnext;
378       delete ptr;
379       ptr=next;
380   }
381 }
382
383 #define SETFLAG (1<<31)
384
385 int RepairHash::addset(int setv, int rule, int value, int data) {
386   return addrelation(setv||SETFLAG, rule, value,0,data);
387 }
388
389 int RepairHash::addrelation(int relation, int rule, int lvalue, int rvalue, int data) {
390   return addrelation(relation,rule,lvalue,rvalue,data, 0);
391 }
392
393 int RepairHash::addrelation(int relation, int rule, int lvalue, int rvalue, int data, int data2) {
394     unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
395     
396     RepairHashNode **ptr = &bucket[hashkey];
397
398     /* check that this key/object pair isn't already here */
399     // TBD can be optimized for set v. relation */
400     while (*ptr) {
401         if ((*ptr)->setrelation == relation && 
402             (*ptr)->rule==rule &&
403             (*ptr)->lvalue==lvalue &&
404             (*ptr)->rvalue==rvalue &&
405             (*ptr)->data == data &&
406             (*ptr)->data2 == data2) {
407             return 0;
408         }
409         ptr = &((*ptr)->next);
410     }
411     
412     *ptr = new RepairHashNode(relation,rule,lvalue,rvalue, data,data2);
413     (*ptr)->lnext=nodelist;
414     nodelist=*ptr;
415     numelements++;
416     return 1;
417 }
418
419 bool RepairHash::containsset(int setv, int rule, int value) {
420   return containsrelation(setv||SETFLAG, rule, value,0);
421 }
422
423 bool RepairHash::containsrelation(int relation, int rule, int lvalue, int rvalue) {
424     unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
425     
426     RepairHashNode **ptr = &bucket[hashkey];
427
428     /* check that this key/object pair isn't already here */
429     // TBD can be optimized for set v. relation */
430     while (*ptr) {
431         if ((*ptr)->setrelation == relation && 
432             (*ptr)->rule==rule &&
433             (*ptr)->lvalue==lvalue &&
434             (*ptr)->rvalue==rvalue) {
435             return true;
436         }
437         ptr = &((*ptr)->next);
438     }
439     return false;
440 }
441
442 int RepairHash::getset(int setv, int rule, int value) {
443   return getrelation(setv||SETFLAG, rule, value,0);
444 }
445
446 int RepairHash::getrelation2(int relation, int rule, int lvalue,int rvalue) {
447     unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
448     
449     RepairHashNode **ptr = &bucket[hashkey];
450
451     /* check that this key/object pair isn't already here */
452     // TBD can be optimized for set v. relation */
453     while (*ptr) {
454         if ((*ptr)->setrelation == relation && 
455             (*ptr)->rule==rule &&
456             (*ptr)->lvalue==lvalue &&
457             (*ptr)->rvalue==rvalue) {
458           return (*ptr)->data2;
459         }
460         ptr = &((*ptr)->next);
461     }
462     return 0;
463 }
464 int RepairHash::getrelation(int relation, int rule, int lvalue,int rvalue) {
465     unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
466     
467     RepairHashNode **ptr = &bucket[hashkey];
468
469     /* check that this key/object pair isn't already here */
470     // TBD can be optimized for set v. relation */
471     while (*ptr) {
472         if ((*ptr)->setrelation == relation && 
473             (*ptr)->rule==rule &&
474             (*ptr)->lvalue==lvalue &&
475             (*ptr)->rvalue==rvalue) {
476           return (*ptr)->data;
477         }
478         ptr = &((*ptr)->next);
479     }
480     return 0;
481 }