Fix grammar and Sum bug.
[repair.git] / Repair / RepairInterpreter / SimpleHash.cc
1 #include "SimpleHash.h"
2
3 /* LINKED HASH NODE ****************************************************/
4
5 LinkedHashNode::LinkedHashNode(int key, int data, LinkedHashNode *next) {
6     this->key = key;
7     this->data = data;
8     this->next = next;
9 }
10
11 LinkedHashNode::LinkedHashNode() {
12     this->key = -1;
13     this->data = -1;
14     this->next = 0;
15 }
16
17 /* SIMPLE LIST ****************************************************/
18
19 SimpleList::SimpleList() {
20     ptr = 0;
21     head.next = 0;
22 }
23
24 void SimpleList::reset() {
25     ptr = &head;
26 }
27
28 int SimpleList::hasMoreElements() {
29     return (ptr->next == 0);
30 }
31
32 int SimpleList::nextElement() {
33     ptr = ptr->next;
34     return ptr->data;
35 }
36
37 void SimpleList::add(int data) {
38     LinkedHashNode *cur = &head;
39     while (cur->next) {
40         cur = cur->next;
41         if (cur->data == data) {
42             return; // no duplicates
43         }
44     }
45     cur->next = new LinkedHashNode(0, data, 0);
46     return;
47 }
48
49 int SimpleList::contains(int data) {
50     LinkedHashNode *cur = &head;
51     while (cur->next) {
52         cur = cur->next;
53         if (cur->data == data) {
54             return 1; // found!
55         }
56     }
57     return 0;    
58 }
59     
60 /* SIMPLE HASH ********************************************************/
61
62 SimpleHash::SimpleHash(int size) {
63     if (size <= 0) {
64         throw SimpleHashException();
65     }
66     this->size = size;
67     this->bucket = new LinkedHashNode[size];
68     this->last = &all;
69     all.next = 0;
70     this->numparents = 0;
71     this->numelements = 0;
72 }
73
74 SimpleHash::SimpleHash() {
75     this->size = 100;
76     this->bucket = new LinkedHashNode[size];
77     this->last = &all;
78     all.next = 0;
79     this->numparents = 0;
80     this->numelements = 0;
81 }
82
83
84 SimpleHash::~SimpleHash() {
85 }
86
87 void SimpleHash::addParent(SimpleHash* parent) {
88     parents[numparents++] = parent;    
89 }
90
91 int SimpleHash::add(int key, int data) {
92     unsigned int hashkey = (unsigned int)key % size;
93     
94     LinkedHashNode *ptr = &bucket[hashkey];
95
96     /* check that this key/object pair isn't already here */
97     // TBD can be optimized for set v. relation */
98     while (ptr->next) {
99         ptr = ptr->next;
100         if (ptr->key == key && ptr->data == data) {
101             return 0;
102         }
103     }
104     
105     ptr->next = new LinkedHashNode(key, data, 0);
106     last = last->next = new LinkedHashNode(key, data, 0);
107     numelements++;
108     
109     for (int i = 0; i < numparents; i++) {
110         parents[i]->add(key, data);
111     }
112
113     return key;
114 }
115
116 bool SimpleHash::contains(int key) {
117     unsigned int hashkey = (unsigned int)key % size;
118     
119     LinkedHashNode *ptr = &bucket[hashkey];
120     while (ptr->next) {
121         ptr = ptr->next;
122         if (ptr->key == key) {
123             // we already have this object 
124             // stored in the hash so just return
125             return true;
126         }
127     }
128     return false;
129 }
130
131 bool SimpleHash::contains(int key, int data) {
132     unsigned int hashkey = (unsigned int)key % size;
133     
134     LinkedHashNode *ptr = &bucket[hashkey];
135     while (ptr->next) {
136         ptr = ptr->next;
137         if (ptr->key == key && ptr->data == data) {
138             // we already have this object 
139             // stored in the hash so just return
140             return true;
141         }
142     }
143     return false;
144 }
145
146 int SimpleHash::count(int key) {
147     unsigned int hashkey = (unsigned int)key % size;
148     int count = 0;
149
150     LinkedHashNode *ptr = &bucket[hashkey];
151     while (ptr->next) {
152         ptr = ptr->next;
153         if (ptr->key == key) {
154             count++;
155         }
156     }
157     return count;
158 }
159
160 int SimpleHash::get(int key, int&data) {
161     unsigned int hashkey = (unsigned int)key % size;
162     
163     LinkedHashNode *ptr = &bucket[hashkey];
164     while (ptr->next) {
165         ptr = ptr->next;
166         if (ptr->key == key) {
167             data = ptr->data;
168             return 1; // success
169         }
170     }
171         
172     return 0; // failure
173 }
174
175 int SimpleHash::countdata(int data) {
176     int count = 0;
177     for (int i = 0; i < size ; i++) {
178         LinkedHashNode *ptr = &bucket[i];
179         while(ptr->next) {
180             ptr = ptr->next;
181             if (ptr->data == data) {
182                 count++;
183             }
184         }
185     }
186     return count;
187 }
188
189 SimpleHashException::SimpleHashException() {}