1 #include "SimpleHash.h"
3 /* LINKED HASH NODE ****************************************************/
5 LinkedHashNode::LinkedHashNode(int key, int data, LinkedHashNode *next) {
11 LinkedHashNode::LinkedHashNode() {
17 /* SIMPLE LIST ****************************************************/
19 SimpleList::SimpleList() {
24 void SimpleList::reset() {
28 int SimpleList::hasMoreElements() {
29 return (ptr->next == 0);
32 int SimpleList::nextElement() {
37 void SimpleList::add(int data) {
38 LinkedHashNode *cur = &head;
41 if (cur->data == data) {
42 return; // no duplicates
45 cur->next = new LinkedHashNode(0, data, 0);
49 int SimpleList::contains(int data) {
50 LinkedHashNode *cur = &head;
53 if (cur->data == data) {
60 /* SIMPLE HASH ********************************************************/
62 SimpleHash::SimpleHash(int size) {
64 throw SimpleHashException();
67 this->bucket = new LinkedHashNode[size];
71 this->numelements = 0;
74 SimpleHash::SimpleHash() {
76 this->bucket = new LinkedHashNode[size];
80 this->numelements = 0;
84 SimpleHash::~SimpleHash() {
87 void SimpleHash::addParent(SimpleHash* parent) {
88 parents[numparents++] = parent;
91 int SimpleHash::add(int key, int data) {
92 unsigned int hashkey = (unsigned int)key % size;
94 LinkedHashNode *ptr = &bucket[hashkey];
96 /* check that this key/object pair isn't already here */
97 // TBD can be optimized for set v. relation */
100 if (ptr->key == key && ptr->data == data) {
105 ptr->next = new LinkedHashNode(key, data, 0);
106 last = last->next = new LinkedHashNode(key, data, 0);
109 for (int i = 0; i < numparents; i++) {
110 parents[i]->add(key, data);
116 bool SimpleHash::contains(int key) {
117 unsigned int hashkey = (unsigned int)key % size;
119 LinkedHashNode *ptr = &bucket[hashkey];
122 if (ptr->key == key) {
123 // we already have this object
124 // stored in the hash so just return
131 bool SimpleHash::contains(int key, int data) {
132 unsigned int hashkey = (unsigned int)key % size;
134 LinkedHashNode *ptr = &bucket[hashkey];
137 if (ptr->key == key && ptr->data == data) {
138 // we already have this object
139 // stored in the hash so just return
146 int SimpleHash::count(int key) {
147 unsigned int hashkey = (unsigned int)key % size;
150 LinkedHashNode *ptr = &bucket[hashkey];
153 if (ptr->key == key) {
160 int SimpleHash::get(int key, int&data) {
161 unsigned int hashkey = (unsigned int)key % size;
163 LinkedHashNode *ptr = &bucket[hashkey];
166 if (ptr->key == key) {
175 int SimpleHash::countdata(int data) {
177 for (int i = 0; i < size ; i++) {
178 LinkedHashNode *ptr = &bucket[i];
181 if (ptr->data == data) {
189 SimpleHashException::SimpleHashException() {}