1 #include "SimpleHash.h"
5 /* LINKED HASH NODE ****************************************************/
7 LinkedHashNode::LinkedHashNode(int key, int data, LinkedHashNode *next) {
15 LinkedHashNode::LinkedHashNode() {
23 /* SIMPLE LIST ****************************************************/
25 SimpleList::SimpleList() {
30 void SimpleList::reset() {
35 int SimpleList::hasMoreElements() {
37 return (ptr->next != 0);
40 int SimpleList::nextElement() {
44 //int data = ptr->data;
49 void SimpleList::add(int data) {
50 LinkedHashNode *cur = &head;
53 if (cur->data == data) {
54 return; // no duplicates
57 cur->next = new LinkedHashNode(0, data, 0);
61 int SimpleList::contains(int data) {
62 LinkedHashNode *cur = &head;
65 if (cur->data == data) {
72 /* WORK LIST ****************************************************/
74 WorkList::WorkList() {
75 head=(struct ListNode *) malloc(sizeof(struct ListNode));
82 void WorkList::reset() {
88 int WorkList::hasMoreElements() {
90 return ((head!=tail)||(headoffset!=tailoffset));
93 int WorkList::getid() {
94 return tail->data[tailoffset];
97 int WorkList::gettype() {
98 return tail->data[tailoffset+1];
101 int WorkList::getlvalue() {
102 return tail->data[tailoffset+2];
105 int WorkList::getrvalue() {
106 return tail->data[tailoffset+3];
109 WorkList::~WorkList() {
110 struct ListNode *ptr=tail;
112 struct ListNode *oldptr=ptr;
118 void WorkList::pop() {
119 int newoffset=tailoffset+4;
120 struct ListNode *ptr=tail;
121 if (newoffset>=WLISTSIZE) {
122 newoffset-=WLISTSIZE;
123 struct ListNode *oldptr=ptr;
128 tailoffset=newoffset;
131 void WorkList::add(int id,int type, int lvalue, int rvalue) {
132 if (headoffset==WLISTSIZE) {
134 head->next=(struct ListNode *)malloc(sizeof(struct ListNode));
140 head->data[headoffset++]=id;
141 head->data[headoffset++]=type;
142 head->data[headoffset++]=lvalue;
143 head->data[headoffset++]=rvalue;
146 /* SIMPLE HASH ********************************************************/
147 SimpleIterator* SimpleHash::iterator() {
148 return new SimpleIterator(listhead,listtail,tailindex/*,this*/);
151 void SimpleHash::iterator(SimpleIterator & it) {
155 it.tailindex=tailindex;
159 SimpleHash::SimpleHash(int size) {
161 throw SimpleHashException();
164 this->bucket = (struct SimpleNode **) calloc(sizeof(struct SimpleNode *)*size,1);
165 /* Set allocation blocks*/
166 this->listhead=(struct ArraySimple *) calloc(sizeof(struct ArraySimple),1);
167 this->listtail=this->listhead;
170 this->numparents = 0;
171 this->numchildren = 0;
172 this->numelements = 0;
175 SimpleHash::~SimpleHash() {
177 struct ArraySimple *ptr=listhead;
179 struct ArraySimple *next=ptr->nextarray;
185 int SimpleHash::firstkey() {
186 struct ArraySimple *ptr=listhead;
188 while((index==ARRAYSIZE)||!ptr->nodes[index].inuse) {
189 if (index==ARRAYSIZE) {
195 return ptr->nodes[index].key;
198 void SimpleHash::addParent(SimpleHash* parent) {
199 parents[numparents++] = parent;
200 parent->addChild(this);
203 void SimpleHash::addChild(SimpleHash *child) {
204 children[numchildren++]=child;
207 int SimpleHash::remove(int key, int data) {
208 unsigned int hashkey = (unsigned int)key % size;
210 struct SimpleNode **ptr = &bucket[hashkey];
212 for (int i = 0; i < numchildren; i++) {
213 children[i]->remove(key, data);
217 if ((*ptr)->key == key && (*ptr)->data == data) {
218 struct SimpleNode *toremove=*ptr;
221 toremove->inuse=0; /* Marked as unused */
226 ptr = &((*ptr)->next);
234 int SimpleHash::add(int key, int data) {
235 unsigned int hashkey = (unsigned int)key % size;
237 struct SimpleNode **ptr = &bucket[hashkey];
239 /* check that this key/object pair isn't already here */
240 // TBD can be optimized for set v. relation */
242 if ((*ptr)->key == key && (*ptr)->data == data) {
245 ptr = &((*ptr)->next);
247 if (tailindex==ARRAYSIZE) {
248 listtail->nextarray=(struct ArraySimple *) calloc(sizeof(struct ArraySimple),1);
250 listtail=listtail->nextarray;
253 *ptr = &listtail->nodes[tailindex++];
260 for (int i = 0; i < numparents; i++) {
261 parents[i]->add(key, data);
267 bool SimpleHash::contains(int key) {
268 unsigned int hashkey = (unsigned int)key % size;
270 struct SimpleNode *ptr = bucket[hashkey];
272 if (ptr->key == key) {
273 // we already have this object
274 // stored in the hash so just return
282 bool SimpleHash::contains(int key, int data) {
283 unsigned int hashkey = (unsigned int)key % size;
285 struct SimpleNode *ptr = bucket[hashkey];
287 if (ptr->key == key && ptr->data == data) {
288 // we already have this object
289 // stored in the hash so just return
297 int SimpleHash::count(int key) {
298 unsigned int hashkey = (unsigned int)key % size;
301 struct SimpleNode *ptr = bucket[hashkey];
303 if (ptr->key == key) {
311 int SimpleHash::get(int key, int&data) {
312 unsigned int hashkey = (unsigned int)key % size;
314 struct SimpleNode *ptr = bucket[hashkey];
316 if (ptr->key == key) {
326 int SimpleHash::countdata(int data) {
328 struct ArraySimple *ptr = listhead;
330 if (ptr->nextarray) {
331 for(int i=0;i<ARRAYSIZE;i++)
332 if (ptr->nodes[i].data == data
333 &&ptr->nodes[i].inuse) {
337 for(int i=0;i<tailindex;i++)
338 if (ptr->nodes[i].data == data
339 &&ptr->nodes[i].inuse) {
343 ptr = ptr->nextarray;
348 SimpleHashException::SimpleHashException() {}
349 // ************************************************************
352 RepairHashNode::RepairHashNode(int setrelation, int rule, int lvalue, int rvalue, int data, int data2, int ismodify){
353 this->setrelation = setrelation;
361 this->ismodify=ismodify;
364 // ************************************************************
366 RepairHash::RepairHash(int size) {
368 throw SimpleHashException();
371 this->bucket = new RepairHashNode* [size];
372 for (int i=0;i<size;i++)
375 this->numelements = 0;
378 #define REPAIRSIZE 100
379 RepairHash::RepairHash() {
380 this->size = REPAIRSIZE;
381 this->bucket = new RepairHashNode* [REPAIRSIZE];
382 for (int i=0;i<REPAIRSIZE;i++)
385 this->numelements = 0;
388 RepairHash::~RepairHash() {
389 delete [] this->bucket;
390 RepairHashNode *ptr=nodelist;
392 RepairHashNode *next=ptr->lnext;
398 #define SETFLAG (1<<31)
400 int RepairHash::addset(int setv, int rule, int value, int data) {
401 return addrelation(setv||SETFLAG, rule, value,0,data);
404 int RepairHash::addrelation(int relation, int rule, int lvalue, int rvalue, int data) {
405 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
407 RepairHashNode **ptr = &bucket[hashkey];
409 /* check that this key/object pair isn't already here */
410 // TBD can be optimized for set v. relation */
412 if ((*ptr)->setrelation == relation &&
413 (*ptr)->rule==rule &&
414 (*ptr)->lvalue==lvalue &&
415 (*ptr)->rvalue==rvalue &&
416 (*ptr)->data == data&&
417 (*ptr)->data2 == 0) {
420 ptr = &((*ptr)->next);
423 *ptr = new RepairHashNode(relation,rule,lvalue,rvalue, data,0,0);
424 (*ptr)->lnext=nodelist;
430 int RepairHash::addrelation(int relation, int rule, int lvalue, int rvalue, int data, int data2) {
431 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
433 RepairHashNode **ptr = &bucket[hashkey];
435 /* check that this key/object pair isn't already here */
436 // TBD can be optimized for set v. relation */
438 if ((*ptr)->setrelation == relation &&
439 (*ptr)->rule==rule &&
440 (*ptr)->lvalue==lvalue &&
441 (*ptr)->rvalue==rvalue &&
442 (*ptr)->data == data &&
443 (*ptr)->data2 == data2) {
446 ptr = &((*ptr)->next);
449 *ptr = new RepairHashNode(relation,rule,lvalue,rvalue, data,data2,1);
450 (*ptr)->lnext=nodelist;
456 bool RepairHash::containsset(int setv, int rule, int value) {
457 return containsrelation(setv||SETFLAG, rule, value,0);
460 bool RepairHash::containsrelation(int relation, int rule, int lvalue, int rvalue) {
461 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
463 RepairHashNode **ptr = &bucket[hashkey];
465 /* check that this key/object pair isn't already here */
466 // TBD can be optimized for set v. relation */
468 if ((*ptr)->setrelation == relation &&
469 (*ptr)->rule==rule &&
470 (*ptr)->lvalue==lvalue &&
471 (*ptr)->rvalue==rvalue) {
474 ptr = &((*ptr)->next);
479 int RepairHash::getset(int setv, int rule, int value) {
480 return getrelation(setv||SETFLAG, rule, value,0);
483 int RepairHash::ismodify(int relation, int rule, int lvalue,int rvalue) {
484 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
486 RepairHashNode **ptr = &bucket[hashkey];
488 /* check that this key/object pair isn't already here */
489 // TBD can be optimized for set v. relation */
491 if ((*ptr)->setrelation == relation &&
492 (*ptr)->rule==rule &&
493 (*ptr)->lvalue==lvalue &&
494 (*ptr)->rvalue==rvalue) {
495 return (*ptr)->ismodify;
497 ptr = &((*ptr)->next);
502 int RepairHash::getrelation2(int relation, int rule, int lvalue,int rvalue) {
503 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
505 RepairHashNode **ptr = &bucket[hashkey];
507 /* check that this key/object pair isn't already here */
508 // TBD can be optimized for set v. relation */
510 if ((*ptr)->setrelation == relation &&
511 (*ptr)->rule==rule &&
512 (*ptr)->lvalue==lvalue &&
513 (*ptr)->rvalue==rvalue) {
514 return (*ptr)->data2;
516 ptr = &((*ptr)->next);
521 int RepairHash::getrelation(int relation, int rule, int lvalue,int rvalue) {
522 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
524 RepairHashNode **ptr = &bucket[hashkey];
526 /* check that this key/object pair isn't already here */
527 // TBD can be optimized for set v. relation */
529 if ((*ptr)->setrelation == relation &&
530 (*ptr)->rule==rule &&
531 (*ptr)->lvalue==lvalue &&
532 (*ptr)->rvalue==rvalue) {
535 ptr = &((*ptr)->next);