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 int WorkList::hasMoreElements() {
84 return ((head!=tail)||(headoffset!=tailoffset));
87 int WorkList::getid() {
88 return tail->data[tailoffset];
91 int WorkList::gettype() {
92 return tail->data[tailoffset+1];
95 int WorkList::getlvalue() {
96 return tail->data[tailoffset+2];
99 int WorkList::getrvalue() {
100 return tail->data[tailoffset+3];
103 WorkList::~WorkList() {
104 struct ListNode *ptr=tail;
106 struct ListNode *oldptr=ptr;
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;
122 tailoffset=newoffset;
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));
132 head->data[headoffset++]=id;
133 head->data[headoffset++]=type;
134 head->data[headoffset++]=lvalue;
135 head->data[headoffset++]=rvalue;
138 /* SIMPLE HASH ********************************************************/
139 SimpleIterator* SimpleHash::iterator() {
140 return new SimpleIterator(listhead,listtail,tailindex/*,this*/);
143 void SimpleHash::iterator(SimpleIterator & it) {
147 it.tailindex=tailindex;
151 SimpleHash::SimpleHash(int size) {
153 throw SimpleHashException();
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;
162 this->numparents = 0;
163 this->numchildren = 0;
164 this->numelements = 0;
167 SimpleHash::~SimpleHash() {
169 struct ArraySimple *ptr=listhead;
171 struct ArraySimple *next=ptr->nextarray;
177 void SimpleHash::addParent(SimpleHash* parent) {
178 parents[numparents++] = parent;
179 parent->addChild(this);
182 void SimpleHash::addChild(SimpleHash *child) {
183 children[numchildren++]=child;
186 int SimpleHash::remove(int key, int data) {
187 unsigned int hashkey = (unsigned int)key % size;
189 struct SimpleNode **ptr = &bucket[hashkey];
191 for (int i = 0; i < numchildren; i++) {
192 children[i]->remove(key, data);
196 if ((*ptr)->key == key && (*ptr)->data == data) {
197 struct SimpleNode *toremove=*ptr;
200 toremove->inuse=0; /* Marked as unused */
205 ptr = &((*ptr)->next);
213 int SimpleHash::add(int key, int data) {
214 unsigned int hashkey = (unsigned int)key % size;
216 struct SimpleNode **ptr = &bucket[hashkey];
218 /* check that this key/object pair isn't already here */
219 // TBD can be optimized for set v. relation */
221 if ((*ptr)->key == key && (*ptr)->data == data) {
224 ptr = &((*ptr)->next);
226 if (tailindex==ARRAYSIZE) {
227 listtail->nextarray=(struct ArraySimple *) calloc(sizeof(struct ArraySimple),1);
229 listtail=listtail->nextarray;
232 *ptr = &listtail->nodes[tailindex++];
239 for (int i = 0; i < numparents; i++) {
240 parents[i]->add(key, data);
246 bool SimpleHash::contains(int key) {
247 unsigned int hashkey = (unsigned int)key % size;
249 struct SimpleNode *ptr = bucket[hashkey];
251 if (ptr->key == key) {
252 // we already have this object
253 // stored in the hash so just return
261 bool SimpleHash::contains(int key, int data) {
262 unsigned int hashkey = (unsigned int)key % size;
264 struct SimpleNode *ptr = bucket[hashkey];
266 if (ptr->key == key && ptr->data == data) {
267 // we already have this object
268 // stored in the hash so just return
276 int SimpleHash::count(int key) {
277 unsigned int hashkey = (unsigned int)key % size;
280 struct SimpleNode *ptr = bucket[hashkey];
282 if (ptr->key == key) {
290 int SimpleHash::get(int key, int&data) {
291 unsigned int hashkey = (unsigned int)key % size;
293 struct SimpleNode *ptr = bucket[hashkey];
295 if (ptr->key == key) {
305 int SimpleHash::countdata(int data) {
307 struct ArraySimple *ptr = listhead;
309 if (ptr->nextarray) {
310 for(int i=0;i<ARRAYSIZE;i++)
311 if (ptr->nodes[i].data == data
312 &&ptr->nodes[i].inuse) {
316 for(int i=0;i<tailindex;i++)
317 if (ptr->nodes[i].data == data
318 &&ptr->nodes[i].inuse) {
322 ptr = ptr->nextarray;
327 SimpleHashException::SimpleHashException() {}
328 // ************************************************************
331 RepairHashNode::RepairHashNode(int setrelation, int rule, int lvalue, int rvalue, int data, int data2){
332 this->setrelation = setrelation;
342 // ************************************************************
344 RepairHash::RepairHash(int size) {
346 throw SimpleHashException();
349 this->bucket = new RepairHashNode* [size];
350 for (int i=0;i<size;i++)
353 this->numelements = 0;
356 RepairHash::RepairHash() {
360 RepairHash::~RepairHash() {
361 delete [] this->bucket;
362 RepairHashNode *ptr=nodelist;
364 RepairHashNode *next=ptr->lnext;
370 #define SETFLAG (1<<31)
372 int RepairHash::addset(int setv, int rule, int value, int data) {
373 return addrelation(setv||SETFLAG, rule, value,0,data);
376 int RepairHash::addrelation(int relation, int rule, int lvalue, int rvalue, int data) {
377 return addrelation(relation,rule,lvalue,rvalue,data, 0);
380 int RepairHash::addrelation(int relation, int rule, int lvalue, int rvalue, int data, int data2) {
381 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
383 RepairHashNode **ptr = &bucket[hashkey];
385 /* check that this key/object pair isn't already here */
386 // TBD can be optimized for set v. relation */
388 if ((*ptr)->setrelation == relation &&
389 (*ptr)->rule==rule &&
390 (*ptr)->lvalue==lvalue &&
391 (*ptr)->rvalue==rvalue &&
392 (*ptr)->data == data &&
393 (*ptr)->data2 == data2) {
396 ptr = &((*ptr)->next);
399 *ptr = new RepairHashNode(relation,rule,lvalue,rvalue, data,data2);
400 (*ptr)->lnext=nodelist;
406 bool RepairHash::containsset(int setv, int rule, int value) {
407 return containsrelation(setv||SETFLAG, rule, value,0);
410 bool RepairHash::containsrelation(int relation, int rule, int lvalue, int rvalue) {
411 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
413 RepairHashNode **ptr = &bucket[hashkey];
415 /* check that this key/object pair isn't already here */
416 // TBD can be optimized for set v. relation */
418 if ((*ptr)->setrelation == relation &&
419 (*ptr)->rule==rule &&
420 (*ptr)->lvalue==lvalue &&
421 (*ptr)->rvalue==rvalue) {
424 ptr = &((*ptr)->next);
429 int RepairHash::getset(int setv, int rule, int value) {
430 return getrelation(setv||SETFLAG, rule, value,0);
433 int RepairHash::getrelation2(int relation, int rule, int lvalue,int rvalue) {
434 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
436 RepairHashNode **ptr = &bucket[hashkey];
438 /* check that this key/object pair isn't already here */
439 // TBD can be optimized for set v. relation */
441 if ((*ptr)->setrelation == relation &&
442 (*ptr)->rule==rule &&
443 (*ptr)->lvalue==lvalue &&
444 (*ptr)->rvalue==rvalue) {
445 return (*ptr)->data2;
447 ptr = &((*ptr)->next);
451 int RepairHash::getrelation(int relation, int rule, int lvalue,int rvalue) {
452 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
454 RepairHashNode **ptr = &bucket[hashkey];
456 /* check that this key/object pair isn't already here */
457 // TBD can be optimized for set v. relation */
459 if ((*ptr)->setrelation == relation &&
460 (*ptr)->rule==rule &&
461 (*ptr)->lvalue==lvalue &&
462 (*ptr)->rvalue==rvalue) {
465 ptr = &((*ptr)->next);