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));
81 int WorkList::hasMoreElements() {
83 return ((head!=tail)||(headoffset!=tailoffset));
86 int WorkList::getid() {
87 return tail->data[tailoffset];
90 int WorkList::gettype() {
91 return tail->data[tailoffset+1];
94 int WorkList::getlvalue() {
95 return tail->data[tailoffset+2];
98 int WorkList::getrvalue() {
99 return tail->data[tailoffset+3];
102 WorkList::~WorkList() {
103 struct ListNode *ptr=tail;
105 struct ListNode *oldptr=ptr;
111 void WorkList::pop() {
112 int newoffset=tailoffset+4;
113 struct ListNode *ptr=tail;
114 if (newoffset>=WLISTSIZE) {
115 newoffset-=WLISTSIZE;
116 struct ListNode *oldptr=ptr;
121 tailoffset=newoffset;
124 void WorkList::add(int id,int type, int lvalue, int rvalue) {
125 if (headoffset==WLISTSIZE) {
126 head->next=(struct ListNode *)malloc(sizeof(struct ListNode));
130 head->data[headoffset++]=id;
131 head->data[headoffset++]=type;
132 head->data[headoffset++]=lvalue;
133 head->data[headoffset++]=rvalue;
136 /* SIMPLE HASH ********************************************************/
137 SimpleIterator* SimpleHash::iterator() {
138 return new SimpleIterator(listhead,this);
141 SimpleHash::SimpleHash(int size) {
143 throw SimpleHashException();
146 this->bucket = (struct SimpleNode **) calloc(sizeof(struct SimpleNode *)*size,1);
147 /* Set allocation blocks*/
148 this->listhead=(struct ArraySimple *) calloc(sizeof(struct ArraySimple),1);
149 this->listtail=this->listhead;
152 this->numparents = 0;
153 this->numchildren = 0;
154 this->numelements = 0;
157 SimpleHash::~SimpleHash() {
159 struct ArraySimple *ptr=listhead;
161 struct ArraySimple *next=ptr->nextarray;
167 void SimpleHash::addParent(SimpleHash* parent) {
168 parents[numparents++] = parent;
169 parent->addChild(this);
172 void SimpleHash::addChild(SimpleHash *child) {
173 children[numchildren++]=child;
176 int SimpleHash::remove(int key, int data) {
177 unsigned int hashkey = (unsigned int)key % size;
179 struct SimpleNode **ptr = &bucket[hashkey];
181 for (int i = 0; i < numchildren; i++) {
182 children[i]->remove(key, data);
186 if ((*ptr)->key == key && (*ptr)->data == data) {
187 struct SimpleNode *toremove=*ptr;
190 toremove->inuse=0; /* Marked as unused */
195 ptr = &((*ptr)->next);
203 int SimpleHash::add(int key, int data) {
204 unsigned int hashkey = (unsigned int)key % size;
206 struct SimpleNode **ptr = &bucket[hashkey];
208 /* check that this key/object pair isn't already here */
209 // TBD can be optimized for set v. relation */
211 if ((*ptr)->key == key && (*ptr)->data == data) {
214 ptr = &((*ptr)->next);
216 if (tailindex==ARRAYSIZE) {
217 listtail->nextarray=(struct ArraySimple *) calloc(sizeof(struct ArraySimple),1);
219 listtail=listtail->nextarray;
222 *ptr = &listtail->nodes[tailindex++];
229 for (int i = 0; i < numparents; i++) {
230 parents[i]->add(key, data);
236 bool SimpleHash::contains(int key) {
237 unsigned int hashkey = (unsigned int)key % size;
239 struct SimpleNode *ptr = bucket[hashkey];
241 if (ptr->key == key) {
242 // we already have this object
243 // stored in the hash so just return
251 bool SimpleHash::contains(int key, int data) {
252 unsigned int hashkey = (unsigned int)key % size;
254 struct SimpleNode *ptr = bucket[hashkey];
256 if (ptr->key == key && ptr->data == data) {
257 // we already have this object
258 // stored in the hash so just return
266 int SimpleHash::count(int key) {
267 unsigned int hashkey = (unsigned int)key % size;
270 struct SimpleNode *ptr = bucket[hashkey];
272 if (ptr->key == key) {
280 int SimpleHash::get(int key, int&data) {
281 unsigned int hashkey = (unsigned int)key % size;
283 struct SimpleNode *ptr = bucket[hashkey];
285 if (ptr->key == key) {
295 int SimpleHash::countdata(int data) {
297 struct ArraySimple *ptr = listhead;
299 if (ptr->nextarray) {
300 for(int i=0;i<ARRAYSIZE;i++)
301 if (ptr->nodes[i].data == data
302 &&ptr->nodes[i].inuse) {
306 for(int i=0;i<tailindex;i++)
307 if (ptr->nodes[i].data == data
308 &&ptr->nodes[i].inuse) {
312 ptr = ptr->nextarray;
317 SimpleHashException::SimpleHashException() {}
318 // ************************************************************
321 RepairHashNode::RepairHashNode(int setrelation, int rule, int lvalue, int rvalue, int data){
322 this->setrelation = setrelation;
331 // ************************************************************
333 RepairHash::RepairHash(int size) {
335 throw SimpleHashException();
338 this->bucket = new RepairHashNode* [size];
339 for (int i=0;i<size;i++)
342 this->numelements = 0;
345 RepairHash::RepairHash() {
349 RepairHash::~RepairHash() {
350 delete [] this->bucket;
351 RepairHashNode *ptr=nodelist;
353 RepairHashNode *next=ptr->lnext;
359 #define SETFLAG (1<<31)
361 int RepairHash::addset(int setv, int rule, int value, int data) {
362 return addrelation(setv||SETFLAG, rule, value,0,data);
365 int RepairHash::addrelation(int relation, int rule, int lvalue, int rvalue, int data) {
366 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
368 RepairHashNode **ptr = &bucket[hashkey];
370 /* check that this key/object pair isn't already here */
371 // TBD can be optimized for set v. relation */
373 if ((*ptr)->setrelation == relation &&
374 (*ptr)->rule==rule &&
375 (*ptr)->lvalue==lvalue &&
376 (*ptr)->rvalue==rvalue &&
377 (*ptr)->data == data) {
380 ptr = &((*ptr)->next);
383 *ptr = new RepairHashNode(relation,rule,lvalue,rvalue, data);
384 (*ptr)->lnext=nodelist;
390 bool RepairHash::containsset(int setv, int rule, int value) {
391 return containsrelation(setv||SETFLAG, rule, value,0);
394 bool RepairHash::containsrelation(int relation, int rule, int lvalue, int rvalue) {
395 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
397 RepairHashNode **ptr = &bucket[hashkey];
399 /* check that this key/object pair isn't already here */
400 // TBD can be optimized for set v. relation */
402 if ((*ptr)->setrelation == relation &&
403 (*ptr)->rule==rule &&
404 (*ptr)->lvalue==lvalue &&
405 (*ptr)->rvalue==rvalue) {
408 ptr = &((*ptr)->next);
413 int RepairHash::getset(int setv, int rule, int value) {
414 return getrelation(setv||SETFLAG, rule, value,0);
417 int RepairHash::getrelation(int relation, int rule, int lvalue,int rvalue) {
418 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
420 RepairHashNode **ptr = &bucket[hashkey];
422 /* check that this key/object pair isn't already here */
423 // TBD can be optimized for set v. relation */
425 if ((*ptr)->setrelation == relation &&
426 (*ptr)->rule==rule &&
427 (*ptr)->lvalue==lvalue &&
428 (*ptr)->rvalue==rvalue) {
431 ptr = &((*ptr)->next);