-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashTable.cc
139 lines (127 loc) · 2.93 KB
/
hashTable.cc
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
#include "hashTable.h"
HashTable::HashTable(int n):size(n){
check(n > 2 && prime(n),
"(HashTable()) Need positive prime number for table size");
table = new Link*[n];
check(table != NULL,"(HashTable(int)) Heap overflow");
for(int j = 0; j < size; j++)
table[j] = NULL;
}
HashTable::HashTable(const HashTable& t):size(t.size){
table = new Link*[size];
check(table != NULL, "(HashTable(HashTable)) Heap overflow");
for(int j = 0; j < size; j++)
table[j] = t.table[j];
}
HashTable::~HashTable(){
/* for(int j = 0; j < size; j++)
if(table[j] != NULL)
delete table[j];
delete [] table;*/
}
HashTable& HashTable::add(string str, string d){
int h;
check(!inTable(str),"(add()) Duplicate key");
h = Hash(str).hash() % size;
table[h] = new Link(str, d, table[h]);
check(table[h] != NULL, "(add()) Heap overflow");
return *this;
}
bool HashTable::inTable(string k) const{
int h;
Link *temp;
h = Hash(k).hash() % size;
temp = table[h];
while(temp != NULL && temp->key!=k)
temp = temp->next;
return temp != NULL;
}
string HashTable::get(string k) const
{
int h;
Link *temp;
h = Hash(k).hash() % size;
temp = table[h];
while(temp!=NULL && temp->key!=k)
{
temp = temp->next;
}
if(temp!=NULL)
{
return temp->key;
}
else
{
return "error";
}
}
string& HashTable::operator [](string str){
int h;
Link *temp;
if(!inTable(str)) // t["Lehigh"] = 5;
*this += str;
h = Hash(str).hash() % size;
temp = table[h];
while(temp != NULL && temp->key != str)
temp = temp->next;
return temp->data;
}
const string HashTable::operator [](string str) const{
int h;
Link *temp; // double x = t["Lehigh"];
check(inTable(str),"([]const) Missing key");
h = Hash(str).hash() % size;
temp = table[h];
while(temp != NULL && temp->key!=str)
temp = temp->next;
return temp->data;
}
HashTable& HashTable::operator +=(string k){
return add(k, "");
}
void HashTable::remove(string str){
int h;
Link *tempa, *tempb;
check(inTable(str),"(remove()) Missing key");
h = Hash(str).hash() % size;
if(table[h]->key==str){
tempa = table[h];
table[h] = table[h]->next;
}
else{
tempb = table[h];
while(tempb->next->key!= str)
tempb = tempb->next;
tempa = tempb->next;
tempb->next = tempb->next->next;
}
delete tempa;
}
ostream& operator <<(ostream& out, const HashTable& h){
Link *temp;
for(int j = 0; j < h.size; j++){
temp = h.table[j];
if(temp != NULL)
out << "-----------[ " << j << "]------\n";
while(temp != NULL){
out << *temp << endl;
temp = temp->next;
}
}
return out;
}
bool HashTable::prime(int k){
int divisor;
if(k < 0)
k = -k;
divisor = 2;
while(divisor * divisor <= k && k % divisor != 0)
divisor++;
return k % divisor != 0;
}
void HashTable::check(bool b, string mess){
if(!b){
cerr << "ERROR[HashTable]: " << mess << endl;
exit(1);
}
}