-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.cpp
More file actions
233 lines (210 loc) · 5.25 KB
/
Copy pathHashTable.cpp
File metadata and controls
233 lines (210 loc) · 5.25 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
#include "pch.h"
#include "HashTable.h"
#include <cmath>
#include <iostream>
#include <list>
using namespace std;
HashTable::HashTable() {
table = new ListOfTable[bufferSize];
for (int i = 0; i < bufferSize; i++) {
table[i] = new list<std::pair<Key, Value>>;
}
}
HashTable::~HashTable() {
freeMemory();
}
HashTable::HashTable(const HashTable& old) {
bufferSize = old.bufferSize;
amountOfElements = old.amountOfElements;
table = new ListOfTable[bufferSize];
for (int i = 0; i < bufferSize; i++) {
table[i] = new list<std::pair<Key, Value>>;
for (auto& it : *old.table[i]) { //çâåçäî÷êà
table[i]->push_back(it);
}
}
}
HashTable::HashTable(HashTable&& old) {
bufferSize = old.bufferSize;
amountOfElements = old.amountOfElements;
table = old.table;
old.table = nullptr;
old.bufferSize = 0;
}
HashTable& HashTable::operator=(const HashTable& old) {
HashTable temp(old);
swap(temp);
return *this; //old
}
HashTable& HashTable::operator=(HashTable&& old) {
HashTable temp(old);
swap(temp);
old.table = nullptr;
old.bufferSize = 0;
return *this;
}
void HashTable::clear() {
for (int i = 0; i < bufferSize; i++) {
table[i]->clear();
}
amountOfElements = 0;
}
void HashTable::swap(HashTable& other) {
std::swap(bufferSize, other.bufferSize);
std::swap(amountOfElements, other.amountOfElements);
std::swap(table, other.table);
}
bool HashTable::erase(const Key& key) {
try {
int hash = calculate_hash(key, bufferSize);
ListOfTable list = table[hash];
for (auto it = list->begin(); it != list->end(); ++it) {
if (it->first == key) {
list->erase(it);
break;
}
}
amountOfElements--;
return true;
}
catch (const invalid_argument& e) {
return false;
}
}
bool HashTable::insert(const Key& key, const Value& value) {
if (contains(key))
{
return false;
}
double newFactor = amountOfElements / (double)bufferSize;
if (newFactor > loadFactor) {
resize();
}
int hash = calculate_hash(key, bufferSize);
pair <Key, Value> p1(key, value);
table[hash]->push_back(p1);
amountOfElements++;
return true;
}
bool HashTable::contains(const Key& k) const {
try {
this->at(k);
return true;
}
catch (const invalid_argument& e) {
return false;
}
}
Value& HashTable::operator[](const Key& key) {
if (contains(key)) {
int hash = calculate_hash(key, bufferSize);
ListOfTable list = table[hash];
for (auto& it : *list) {
if (key == it.first) {
return (Value&)(it);
}
}
}
else {
pair <Key, Value> p1(key, Value());
this->insert(p1.first, p1.second);
int hash = calculate_hash(key, bufferSize);
ListOfTable list = table[hash];
for (auto& it : *list) {
if (key == it.first) {
return (Value&)(it);
}
}
}
}
Value& HashTable::at(const Key& key) {
int hash = calculate_hash(key, bufferSize);
ListOfTable list = table[hash];
for (auto it = list->begin(); it != list->end(); ++it) {
if (key == (*it).first) {
return (Value&)((*it).second);
}
}
throw invalid_argument("No value for this key");
}
const Value& HashTable::at(const Key& key) const {
int hash = calculate_hash(key, bufferSize);
ListOfTable list = table[hash];
for (auto& it : *list) {
if (key == it.first) {
return it.second;
}
}
throw invalid_argument("No value for this key");
}
bool operator==(const HashTable& a, const HashTable& b) {
if (a.bufferSize != b.bufferSize && a.amountOfElements != b.amountOfElements) {
return false;
}
for (int i = 0; i < a.bufferSize; i++) {
ListOfTable listA = a.table[i];
ListOfTable listB = b.table[i];
if (listA->size() == listB->size()) {
for (auto itA = listA->begin(); itA != listA->end(); ++itA) {
for (auto itB = listB->begin(); itB != listB->end(); ++itB) {
Value valueA = (*itA).second;
Value valueB = (*itB).second;
if (valueA.age != valueB.age || valueA.weight != valueB.weight || (*itA).first != (*itB).first) {
return false;
}
}
}
}
else {
return false;
}
}
return true;
}
bool operator!=(const HashTable& a, const HashTable& b) {
return !(a == b);
}
bool HashTable::empty() const {
return (amountOfElements == 0);
}
size_t HashTable::size() const {
return amountOfElements;
}
void HashTable::resize() {
size_t newBufferSize = bufferSize * 2;
Table newTable = new ListOfTable[newBufferSize];
for (int i = 0; i < newBufferSize; i++) {
newTable[i] = new list<std::pair<Key, Value>>;
}
for (int i = 0; i < bufferSize; i++) {
ListOfTable list = table[i];
for (auto it = list->begin(); it != list->end(); ++it) {//
//Value value = (*it);
int hash = calculate_hash((*it).first, newBufferSize);
newTable[hash]->push_back(*it);
}
delete table[i];
}
delete[]table;
table = newTable;
bufferSize = newBufferSize;
}
void HashTable::freeMemory() {
if (table == nullptr)
{
return;
}
for (int i = 0; i < bufferSize; i++) {
delete table[i];
}
delete[]table;
}
int HashTable::calculate_hash(const Key& key, int size) const {
size_t leng = key.length();
size_t hash = 0;
for (int i = 0; i < leng; i++) {
int symb = (int)key[i];
hash += (symb % 3) * (int)pow(3, i);
}
return (int)(hash % size);
}