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) {
236 if (numelements>=size) {
237 int newsize=2*size+1;
238 struct SimpleNode ** newbucket = (struct SimpleNode **) calloc(sizeof(struct SimpleNode *)*newsize,1);
239 for(int i=size-1;i>=0;i--) {
240 for(struct SimpleNode *ptr=bucket[i];ptr!=NULL;) {
241 struct SimpleNode * nextptr=ptr->next;
242 unsigned int newhashkey=(unsigned int)ptr->key % newsize;
243 ptr->next=newbucket[newhashkey];
244 newbucket[newhashkey]=ptr;
253 unsigned int hashkey = (unsigned int)key % size;
254 struct SimpleNode **ptr = &bucket[hashkey];
256 /* check that this key/object pair isn't already here */
257 /* TBD can be optimized for set v. relation */
260 if ((*ptr)->key == key && (*ptr)->data == data) {
263 ptr = &((*ptr)->next);
265 if (tailindex==ARRAYSIZE) {
266 listtail->nextarray=(struct ArraySimple *) calloc(sizeof(struct ArraySimple),1);
268 listtail=listtail->nextarray;
271 *ptr = &listtail->nodes[tailindex++];
278 for (int i = 0; i < numparents; i++) {
279 parents[i]->add(key, data);
284 bool SimpleHash::contains(int key) {
285 unsigned int hashkey = (unsigned int)key % size;
287 struct SimpleNode *ptr = bucket[hashkey];
289 if (ptr->key == key) {
290 // we already have this object
291 // stored in the hash so just return
299 bool SimpleHash::contains(int key, int data) {
300 unsigned int hashkey = (unsigned int)key % size;
302 struct SimpleNode *ptr = bucket[hashkey];
304 if (ptr->key == key && ptr->data == data) {
305 // we already have this object
306 // stored in the hash so just return
314 int SimpleHash::count(int key) {
315 unsigned int hashkey = (unsigned int)key % size;
318 struct SimpleNode *ptr = bucket[hashkey];
320 if (ptr->key == key) {
328 int SimpleHash::get(int key, int&data) {
329 unsigned int hashkey = (unsigned int)key % size;
331 struct SimpleNode *ptr = bucket[hashkey];
333 if (ptr->key == key) {
343 int SimpleHash::countdata(int data) {
345 struct ArraySimple *ptr = listhead;
347 if (ptr->nextarray) {
348 for(int i=0;i<ARRAYSIZE;i++)
349 if (ptr->nodes[i].data == data
350 &&ptr->nodes[i].inuse) {
354 for(int i=0;i<tailindex;i++)
355 if (ptr->nodes[i].data == data
356 &&ptr->nodes[i].inuse) {
360 ptr = ptr->nextarray;
365 SimpleHashException::SimpleHashException() {}
366 // ************************************************************
369 RepairHashNode::RepairHashNode(int setrelation, int rule, int lvalue, int rvalue, int data, int data2, int ismodify){
370 this->setrelation = setrelation;
378 this->ismodify=ismodify;
381 // ************************************************************
383 RepairHash::RepairHash(int size) {
385 throw SimpleHashException();
388 this->bucket = new RepairHashNode* [size];
389 for (int i=0;i<size;i++)
392 this->numelements = 0;
395 #define REPAIRSIZE 100
396 RepairHash::RepairHash() {
397 this->size = REPAIRSIZE;
398 this->bucket = new RepairHashNode* [REPAIRSIZE];
399 for (int i=0;i<REPAIRSIZE;i++)
402 this->numelements = 0;
405 RepairHash::~RepairHash() {
406 delete [] this->bucket;
407 RepairHashNode *ptr=nodelist;
409 RepairHashNode *next=ptr->lnext;
415 #define SETFLAG (1<<31)
417 int RepairHash::addset(int setv, int rule, int value, int data) {
418 return addrelation(setv||SETFLAG, rule, value,0,data);
421 int RepairHash::addrelation(int relation, int rule, int lvalue, int rvalue, int data) {
422 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
424 RepairHashNode **ptr = &bucket[hashkey];
426 /* check that this key/object pair isn't already here */
427 // TBD can be optimized for set v. relation */
429 if ((*ptr)->setrelation == relation &&
430 (*ptr)->rule==rule &&
431 (*ptr)->lvalue==lvalue &&
432 (*ptr)->rvalue==rvalue &&
433 (*ptr)->data == data&&
434 (*ptr)->data2 == 0) {
437 ptr = &((*ptr)->next);
440 *ptr = new RepairHashNode(relation,rule,lvalue,rvalue, data,0,0);
441 (*ptr)->lnext=nodelist;
447 int RepairHash::addrelation(int relation, int rule, int lvalue, int rvalue, int data, int data2) {
448 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
450 RepairHashNode **ptr = &bucket[hashkey];
452 /* check that this key/object pair isn't already here */
453 // TBD can be optimized for set v. relation */
455 if ((*ptr)->setrelation == relation &&
456 (*ptr)->rule==rule &&
457 (*ptr)->lvalue==lvalue &&
458 (*ptr)->rvalue==rvalue &&
459 (*ptr)->data == data &&
460 (*ptr)->data2 == data2) {
463 ptr = &((*ptr)->next);
466 *ptr = new RepairHashNode(relation,rule,lvalue,rvalue, data,data2,1);
467 (*ptr)->lnext=nodelist;
473 bool RepairHash::containsset(int setv, int rule, int value) {
474 return containsrelation(setv||SETFLAG, rule, value,0);
477 bool RepairHash::containsrelation(int relation, int rule, int lvalue, int rvalue) {
478 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
480 RepairHashNode **ptr = &bucket[hashkey];
482 /* check that this key/object pair isn't already here */
483 // TBD can be optimized for set v. relation */
485 if ((*ptr)->setrelation == relation &&
486 (*ptr)->rule==rule &&
487 (*ptr)->lvalue==lvalue &&
488 (*ptr)->rvalue==rvalue) {
491 ptr = &((*ptr)->next);
496 int RepairHash::getset(int setv, int rule, int value) {
497 return getrelation(setv||SETFLAG, rule, value,0);
500 int RepairHash::ismodify(int relation, int rule, int lvalue,int rvalue) {
501 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
503 RepairHashNode **ptr = &bucket[hashkey];
505 /* check that this key/object pair isn't already here */
506 // TBD can be optimized for set v. relation */
508 if ((*ptr)->setrelation == relation &&
509 (*ptr)->rule==rule &&
510 (*ptr)->lvalue==lvalue &&
511 (*ptr)->rvalue==rvalue) {
512 return (*ptr)->ismodify;
514 ptr = &((*ptr)->next);
519 int RepairHash::getrelation2(int relation, int rule, int lvalue,int rvalue) {
520 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
522 RepairHashNode **ptr = &bucket[hashkey];
524 /* check that this key/object pair isn't already here */
525 // TBD can be optimized for set v. relation */
527 if ((*ptr)->setrelation == relation &&
528 (*ptr)->rule==rule &&
529 (*ptr)->lvalue==lvalue &&
530 (*ptr)->rvalue==rvalue) {
531 return (*ptr)->data2;
533 ptr = &((*ptr)->next);
538 int RepairHash::getrelation(int relation, int rule, int lvalue,int rvalue) {
539 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
541 RepairHashNode **ptr = &bucket[hashkey];
543 /* check that this key/object pair isn't already here */
544 // TBD can be optimized for set v. relation */
546 if ((*ptr)->setrelation == relation &&
547 (*ptr)->rule==rule &&
548 (*ptr)->lvalue==lvalue &&
549 (*ptr)->rvalue==rvalue) {
552 ptr = &((*ptr)->next);