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,this);
143 void SimpleHash::iterator(SimpleIterator & it) {
149 SimpleHash::SimpleHash(int size) {
151 throw SimpleHashException();
154 this->bucket = (struct SimpleNode **) calloc(sizeof(struct SimpleNode *)*size,1);
155 /* Set allocation blocks*/
156 this->listhead=(struct ArraySimple *) calloc(sizeof(struct ArraySimple),1);
157 this->listtail=this->listhead;
160 this->numparents = 0;
161 this->numchildren = 0;
162 this->numelements = 0;
165 SimpleHash::~SimpleHash() {
167 struct ArraySimple *ptr=listhead;
169 struct ArraySimple *next=ptr->nextarray;
175 void SimpleHash::addParent(SimpleHash* parent) {
176 parents[numparents++] = parent;
177 parent->addChild(this);
180 void SimpleHash::addChild(SimpleHash *child) {
181 children[numchildren++]=child;
184 int SimpleHash::remove(int key, int data) {
185 unsigned int hashkey = (unsigned int)key % size;
187 struct SimpleNode **ptr = &bucket[hashkey];
189 for (int i = 0; i < numchildren; i++) {
190 children[i]->remove(key, data);
194 if ((*ptr)->key == key && (*ptr)->data == data) {
195 struct SimpleNode *toremove=*ptr;
198 toremove->inuse=0; /* Marked as unused */
203 ptr = &((*ptr)->next);
211 int SimpleHash::add(int key, int data) {
212 unsigned int hashkey = (unsigned int)key % size;
214 struct SimpleNode **ptr = &bucket[hashkey];
216 /* check that this key/object pair isn't already here */
217 // TBD can be optimized for set v. relation */
219 if ((*ptr)->key == key && (*ptr)->data == data) {
222 ptr = &((*ptr)->next);
224 if (tailindex==ARRAYSIZE) {
225 listtail->nextarray=(struct ArraySimple *) calloc(sizeof(struct ArraySimple),1);
227 listtail=listtail->nextarray;
230 *ptr = &listtail->nodes[tailindex++];
237 for (int i = 0; i < numparents; i++) {
238 parents[i]->add(key, data);
244 bool SimpleHash::contains(int key) {
245 unsigned int hashkey = (unsigned int)key % size;
247 struct SimpleNode *ptr = bucket[hashkey];
249 if (ptr->key == key) {
250 // we already have this object
251 // stored in the hash so just return
259 bool SimpleHash::contains(int key, int data) {
260 unsigned int hashkey = (unsigned int)key % size;
262 struct SimpleNode *ptr = bucket[hashkey];
264 if (ptr->key == key && ptr->data == data) {
265 // we already have this object
266 // stored in the hash so just return
274 int SimpleHash::count(int key) {
275 unsigned int hashkey = (unsigned int)key % size;
278 struct SimpleNode *ptr = bucket[hashkey];
280 if (ptr->key == key) {
288 int SimpleHash::get(int key, int&data) {
289 unsigned int hashkey = (unsigned int)key % size;
291 struct SimpleNode *ptr = bucket[hashkey];
293 if (ptr->key == key) {
303 int SimpleHash::countdata(int data) {
305 struct ArraySimple *ptr = listhead;
307 if (ptr->nextarray) {
308 for(int i=0;i<ARRAYSIZE;i++)
309 if (ptr->nodes[i].data == data
310 &&ptr->nodes[i].inuse) {
314 for(int i=0;i<tailindex;i++)
315 if (ptr->nodes[i].data == data
316 &&ptr->nodes[i].inuse) {
320 ptr = ptr->nextarray;
325 SimpleHashException::SimpleHashException() {}
326 // ************************************************************
329 RepairHashNode::RepairHashNode(int setrelation, int rule, int lvalue, int rvalue, int data){
330 this->setrelation = setrelation;
339 // ************************************************************
341 RepairHash::RepairHash(int size) {
343 throw SimpleHashException();
346 this->bucket = new RepairHashNode* [size];
347 for (int i=0;i<size;i++)
350 this->numelements = 0;
353 RepairHash::RepairHash() {
357 RepairHash::~RepairHash() {
358 delete [] this->bucket;
359 RepairHashNode *ptr=nodelist;
361 RepairHashNode *next=ptr->lnext;
367 #define SETFLAG (1<<31)
369 int RepairHash::addset(int setv, int rule, int value, int data) {
370 return addrelation(setv||SETFLAG, rule, value,0,data);
373 int RepairHash::addrelation(int relation, int rule, int lvalue, int rvalue, int data) {
374 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
376 RepairHashNode **ptr = &bucket[hashkey];
378 /* check that this key/object pair isn't already here */
379 // TBD can be optimized for set v. relation */
381 if ((*ptr)->setrelation == relation &&
382 (*ptr)->rule==rule &&
383 (*ptr)->lvalue==lvalue &&
384 (*ptr)->rvalue==rvalue &&
385 (*ptr)->data == data) {
388 ptr = &((*ptr)->next);
391 *ptr = new RepairHashNode(relation,rule,lvalue,rvalue, data);
392 (*ptr)->lnext=nodelist;
398 bool RepairHash::containsset(int setv, int rule, int value) {
399 return containsrelation(setv||SETFLAG, rule, value,0);
402 bool RepairHash::containsrelation(int relation, int rule, int lvalue, int rvalue) {
403 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
405 RepairHashNode **ptr = &bucket[hashkey];
407 /* check that this key/object pair isn't already here */
408 // TBD can be optimized for set v. relation */
410 if ((*ptr)->setrelation == relation &&
411 (*ptr)->rule==rule &&
412 (*ptr)->lvalue==lvalue &&
413 (*ptr)->rvalue==rvalue) {
416 ptr = &((*ptr)->next);
421 int RepairHash::getset(int setv, int rule, int value) {
422 return getrelation(setv||SETFLAG, rule, value,0);
425 int RepairHash::getrelation(int relation, int rule, int lvalue,int rvalue) {
426 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
428 RepairHashNode **ptr = &bucket[hashkey];
430 /* check that this key/object pair isn't already here */
431 // TBD can be optimized for set v. relation */
433 if ((*ptr)->setrelation == relation &&
434 (*ptr)->rule==rule &&
435 (*ptr)->lvalue==lvalue &&
436 (*ptr)->rvalue==rvalue) {
439 ptr = &((*ptr)->next);