-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.cpp
More file actions
276 lines (222 loc) · 4.87 KB
/
HashTable.cpp
File metadata and controls
276 lines (222 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "HashTable.h"
#include "HashTableException.h"
#include <stdio.h>
using namespace std;
int HashTable::getHashCode(const char *str) {
int code = 0;
while(*str != 0) {
code += *(str++);
}
return code;
}
int HashTable::getSize() const { return size; }
int HashTable::getCapacity() const {return capacity; }
bool HashTable::isEmpty(int pos) const {
return table[pos]=="";
}
bool HashTable::isTomb(int pos) const {
return table[pos].compare("##tomb##")==0;
}
bool HashTable::isAvailable(int pos) const {
return table[pos].compare("")==0 || table[pos].compare("##tomb##")==0;
}
HashTable::HashTable(int capacity) {
std::bad_alloc ex;
table = new (nothrow) string[capacity];
if(capacity<=0 || table == NULL){
throw ex;
}
for(int i=0;i<capacity; i++){
table[i].assign("");
}
size = 0;
this->capacity = capacity;
}
HashTable::HashTable(const HashTable &ht) {
size = ht.size;
capacity = ht.capacity;
table = new (nothrow) string[capacity];
if(table == NULL){
throw new std::bad_alloc;
}
for(int i=0;i<capacity; i++){
table[i].assign(ht.table[i]);
}
}
bool HashTable::contains(const string &s) const {
return contains(s.c_str());
}
bool HashTable::contains(const char *s) const {
int originalPosition = getHashCode(s)%capacity;
int i = originalPosition;
do{
if(!table[i].compare(s)){
return true;
}
if(isEmpty(i)){
return false;
}
i = (i+1)%capacity;
}while(i!=originalPosition);
return false;
}
string HashTable::print() const {
string str;
char buf[128];
/* Remove the following comment if you want
* to try the iterator version.
*/
// #define __USE_ITERATOR__
#ifdef __USE_ITERATOR__
int j=0;
for(HashTable::Iterator it = begin(); it!=end(); it++) {
sprintf(buf, "%2d. -%s-\n", j++, (*it).c_str());
str.append(buf);
}
#else
for(int i=0, j=0; i<capacity; i++){
if(!isAvailable(i)) {
sprintf(buf, "%2d. -%s-\n", j++, table[i].c_str());
str.append(buf);
}
}
#endif
sprintf(buf, " --- CAPACITY: %d, SIZE: %d ---\n", capacity, size);
str.append(buf);
return str;
}
bool HashTable::add(const string &str) {
return add(str.c_str());
}
bool HashTable::add(const char *s) {
int originalPosition = getHashCode(s)%capacity;
int i = originalPosition;
do{
if(table[i]==s){
return false;
}
if(isAvailable(i)){
table[i].assign(s);
size++;
return true;
}
i = (i+1)%capacity;
}while(i!=originalPosition);
cout<<"THROW Exception!\n";
throw HashTableException();
}
bool HashTable::remove(const string &str) {
return remove(str.c_str());
}
bool HashTable::remove(const char *s) {
if(!contains(s)){
return false;
}
int i = getHashCode(s)%capacity;
while(table[i].compare(s)){
i=(i+1)%capacity;
}
table[i].assign("##tomb##");
size--;
return true;
}
HashTable& HashTable::operator=(const HashTable &ht) {
size = ht.size;
capacity = ht.capacity;
delete []table;
table = new (nothrow) string[capacity];
std::bad_alloc ex;
if(table == NULL){
throw ex;
}
for(int i=0;i<capacity; i++){
table[i].assign(ht.table[i]);
}
return *this;
}
bool HashTable::operator += (const string &str) {
return add(str);
}
bool HashTable::operator += (const char* s) {
return add(s);
}
bool HashTable::operator -= (const string &str) {
return remove(str);
}
bool HashTable::operator -= (const char *s) {
return remove(s);
}
HashTable HashTable::operator + (const string &str) const {
HashTable n (*this);
try{
n.add(str);
}
catch(HashTableException &ex) {
throw HashTableException();
}
return n;
}
HashTable HashTable::operator + (const char* s) const {
HashTable n (*this);
try{
n.add(s);
}
catch(HashTableException &ex) {
throw HashTableException();
}
return n;
}
HashTable HashTable::operator - (const string &str) const {
HashTable n (*this);
n.remove(str);
return n;
}
HashTable HashTable::operator - (const char *s) const {
HashTable n (*this);
n.remove(s);
return n;
}
HashTable HashTable::operator + (const HashTable &t) const {
HashTable n (this->capacity + t.capacity);
for(int i=0; i<this->capacity; i++){
if(!this->isAvailable(i)){
n.add(this->table[i]);
}
}
for(int i=0; i<t.capacity; i++){
if(!t.isAvailable(i)){
n.add(t.table[i]);
}
}
return n;
}
HashTable& HashTable::operator += (const HashTable &t) {
HashTable n (*this);
delete[] table;
this->table = new (nothrow) string[this->capacity + t.capacity];
std::bad_alloc ex;
if(this->table==NULL){
throw ex;
}
this->capacity += t.capacity;
for(int i=0; i<n.capacity; i++){
if(!n.isAvailable(i)){
this->add(n.table[i]);
}
}
for(int i=0; i<t.capacity; i++){
if(!t.isAvailable(i)){
this->add(t.table[i]);
}
}
return *this;
}
std::ostream& operator<<(std::ostream &os, HashTable &t) {
return os.write(t.print().c_str(), t.print().length());
}
HashTable::Iterator HashTable::begin() const {
return Iterator(this);
}
HashTable::Iterator HashTable::end() const {
return Iterator(this, false);
}