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){
332 this->setrelation = setrelation;
341 // ************************************************************
343 RepairHash::RepairHash(int size) {
345 throw SimpleHashException();
348 this->bucket = new RepairHashNode* [size];
349 for (int i=0;i<size;i++)
352 this->numelements = 0;
355 RepairHash::RepairHash() {
359 RepairHash::~RepairHash() {
360 delete [] this->bucket;
361 RepairHashNode *ptr=nodelist;
363 RepairHashNode *next=ptr->lnext;
369 #define SETFLAG (1<<31)
371 int RepairHash::addset(int setv, int rule, int value, int data) {
372 return addrelation(setv||SETFLAG, rule, value,0,data);
375 int RepairHash::addrelation(int relation, int rule, int lvalue, int rvalue, int data) {
376 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
378 RepairHashNode **ptr = &bucket[hashkey];
380 /* check that this key/object pair isn't already here */
381 // TBD can be optimized for set v. relation */
383 if ((*ptr)->setrelation == relation &&
384 (*ptr)->rule==rule &&
385 (*ptr)->lvalue==lvalue &&
386 (*ptr)->rvalue==rvalue &&
387 (*ptr)->data == data) {
390 ptr = &((*ptr)->next);
393 *ptr = new RepairHashNode(relation,rule,lvalue,rvalue, data);
394 (*ptr)->lnext=nodelist;
400 bool RepairHash::containsset(int setv, int rule, int value) {
401 return containsrelation(setv||SETFLAG, rule, value,0);
404 bool RepairHash::containsrelation(int relation, int rule, int lvalue, int rvalue) {
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) {
418 ptr = &((*ptr)->next);
423 int RepairHash::getset(int setv, int rule, int value) {
424 return getrelation(setv||SETFLAG, rule, value,0);
427 int RepairHash::getrelation(int relation, int rule, int lvalue,int rvalue) {
428 unsigned int hashkey = ((unsigned int)(relation ^ rule ^ lvalue ^ rvalue)) % size;
430 RepairHashNode **ptr = &bucket[hashkey];
432 /* check that this key/object pair isn't already here */
433 // TBD can be optimized for set v. relation */
435 if ((*ptr)->setrelation == relation &&
436 (*ptr)->rule==rule &&
437 (*ptr)->lvalue==lvalue &&
438 (*ptr)->rvalue==rvalue) {
441 ptr = &((*ptr)->next);