Various bug fixes.
[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 #define REPAIRSIZE 100
370 RepairHash::RepairHash() {
371     this->size = REPAIRSIZE;
372     this->bucket = new RepairHashNode* [REPAIRSIZE];
373     for (int i=0;i<REPAIRSIZE;i++)
374       bucket[i]=0;
375     this->nodelist=0;
376     this->numelements = 0;
377 }
378
379 RepairHash::~RepairHash() {
380   delete [] this->bucket;
381   RepairHashNode *ptr=nodelist;
382   while(ptr) {
383       RepairHashNode *next=ptr->lnext;
384       delete ptr;
385       ptr=next;
386   }
387 }
388
389 #define SETFLAG (1<<31)
390
391 int RepairHash::addset(int setv, int rule, int value, int data) {
392   return addrelation(setv||SETFLAG, rule, value,0,data);
393 }
394
395 int RepairHash::addrelation(int relation, int rule, int lvalue, int rvalue, int data) {
396   return addrelation(relation,rule,lvalue,rvalue,data, 0);
397 }
398
399 int RepairHash::addrelation(int relation, int rule, int lvalue, int rvalue, int data, int data2) {
400     unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
401     
402     RepairHashNode **ptr = &bucket[hashkey];
403
404     /* check that this key/object pair isn't already here */
405     // TBD can be optimized for set v. relation */
406     while (*ptr) {
407         if ((*ptr)->setrelation == relation && 
408             (*ptr)->rule==rule &&
409             (*ptr)->lvalue==lvalue &&
410             (*ptr)->rvalue==rvalue &&
411             (*ptr)->data == data &&
412             (*ptr)->data2 == data2) {
413             return 0;
414         }
415         ptr = &((*ptr)->next);
416     }
417     
418     *ptr = new RepairHashNode(relation,rule,lvalue,rvalue, data,data2);
419     (*ptr)->lnext=nodelist;
420     nodelist=*ptr;
421     numelements++;
422     return 1;
423 }
424
425 bool RepairHash::containsset(int setv, int rule, int value) {
426   return containsrelation(setv||SETFLAG, rule, value,0);
427 }
428
429 bool RepairHash::containsrelation(int relation, int rule, int lvalue, int rvalue) {
430     unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
431     
432     RepairHashNode **ptr = &bucket[hashkey];
433
434     /* check that this key/object pair isn't already here */
435     // TBD can be optimized for set v. relation */
436     while (*ptr) {
437         if ((*ptr)->setrelation == relation && 
438             (*ptr)->rule==rule &&
439             (*ptr)->lvalue==lvalue &&
440             (*ptr)->rvalue==rvalue) {
441             return true;
442         }
443         ptr = &((*ptr)->next);
444     }
445     return false;
446 }
447
448 int RepairHash::getset(int setv, int rule, int value) {
449   return getrelation(setv||SETFLAG, rule, value,0);
450 }
451
452 int RepairHash::getrelation2(int relation, int rule, int lvalue,int rvalue) {
453     unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
454     
455     RepairHashNode **ptr = &bucket[hashkey];
456
457     /* check that this key/object pair isn't already here */
458     // TBD can be optimized for set v. relation */
459     while (*ptr) {
460         if ((*ptr)->setrelation == relation && 
461             (*ptr)->rule==rule &&
462             (*ptr)->lvalue==lvalue &&
463             (*ptr)->rvalue==rvalue) {
464           return (*ptr)->data2;
465         }
466         ptr = &((*ptr)->next);
467     }
468     return 0;
469 }
470 int RepairHash::getrelation(int relation, int rule, int lvalue,int rvalue) {
471     unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
472     
473     RepairHashNode **ptr = &bucket[hashkey];
474
475     /* check that this key/object pair isn't already here */
476     // TBD can be optimized for set v. relation */
477     while (*ptr) {
478         if ((*ptr)->setrelation == relation && 
479             (*ptr)->rule==rule &&
480             (*ptr)->lvalue==lvalue &&
481             (*ptr)->rvalue==rvalue) {
482           return (*ptr)->data;
483         }
484         ptr = &((*ptr)->next);
485     }
486     return 0;
487 }