-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.cpp
More file actions
194 lines (172 loc) · 4.46 KB
/
HashTable.cpp
File metadata and controls
194 lines (172 loc) · 4.46 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
#include <iostream>
#include "HashTable.h"
#include <vector>
#include <list>
using namespace std;
int hash_func(const Key& s, int table_size){
int hash_result = 0;
for (char i : s)
hash_result = (hash_result + i);
hash_result = (hash_result * 2) % table_size;
return hash_result;
}
HashTable::HashTable(){
average_student.weight = 60;
average_student.age = 19;
ultimate_size = 10;
current_size = 0;
list<pair<Key, Value>>* k = nullptr;
arr.resize(ultimate_size, k);
}
HashTable::~HashTable() {
arr.clear();
ultimate_size = 0;
current_size = 0;
}
HashTable::HashTable(const HashTable& b): HashTable(){
current_size = b.current_size;
ultimate_size = b.ultimate_size;
arr = vector<list<pair<Key, Value>>*>(ultimate_size);
for (int i=0; i<b.arr.size(); i++)
arr[i] = b.arr[i];
}
HashTable::HashTable(HashTable &&b) noexcept{
arr = b.arr;
current_size = b.current_size;
ultimate_size = b.ultimate_size;
for(auto & i : b.arr){
i = nullptr;
}
b.ultimate_size = 0;
b.current_size = 0;
}
HashTable& HashTable::operator=(const HashTable& b) {
if (this == &b){
return *this;
}
current_size = b.current_size;
ultimate_size = b.ultimate_size;
arr.clear();
arr = b.arr;
return *this;
}
HashTable& HashTable::operator=(HashTable &&b){
if (this == &b){
return *this;
}
current_size = b.current_size;
ultimate_size = b.ultimate_size;
arr.clear();
arr = b.arr;
for(auto & i : b.arr){
i = nullptr;
}
}
void HashTable::swap(HashTable &b){
int tmp = current_size;
current_size = b.current_size;
b.current_size = tmp;
tmp = ultimate_size;
ultimate_size = b.ultimate_size;
b.ultimate_size = tmp;
arr.swap(b.arr);
}
void HashTable::clear(){
list<pair<Key, Value>>* emList = nullptr;
arr.clear();
arr.resize(ultimate_size, emList);
}
bool HashTable::erase(const Key& k) {
if (!contains(k))
return false;
int hash = hash_func(k, ultimate_size);
for (auto it = arr[hash]->begin(); it != arr[hash]->end(); it++){
if (it->first == k) {
arr[hash]->erase(it);
current_size--;
return true;
}
}
}
bool HashTable::insert(const Key& k, const Value& v){
if (contains(k))
return false;
int hash = hash_func(k, ultimate_size);
if (arr[hash] == nullptr){
arr[hash] = new list<pair<Key, Value>>;
}
arr[hash]->push_front(make_pair(k, v));
if(current_size/ultimate_size > 0.75){
resize();
}
current_size++;
return true;
}
void HashTable::resize(){
vector<list<pair<Key, Value>>*> copy = arr;
arr.clear();
ultimate_size *= 2;
list<pair<Key, Value>>* emList = nullptr;
arr.resize(ultimate_size, emList);
for (int i = 0; i < ultimate_size / 2; i++){
if (!copy[i]->empty()){
for (auto & it : *copy[i]){
insert(it.first, it.second);
}
}
}
}
bool HashTable::contains(const Key& k) const{
int hash = hash_func(k, ultimate_size);
if(arr[hash] == nullptr){
return false;
}
for (const auto & it : *arr[hash]) {
if (it.first == k){
return true;
}
}
return false;
}
Value& HashTable::operator[](const Key &k) {
if (contains(k)){
int hash = hash_func(k, ultimate_size);
for (auto & it : *arr[hash]){
if (it.first == k){
return it.second;
}
}
}
insert(k, average_student);
return average_student;
}
Value& HashTable::at(const Key &k) {
if (contains(k)){
int hash = hash_func(k, ultimate_size);
for (auto & it : *arr[hash]){
if (it.first == k){
return it.second;
}
}
}
else{
throw invalid_argument("Can't find element on this position");
}
}
size_t HashTable::size() const{
return current_size;
}
bool HashTable::empty() const{
for (const auto it : arr){
if(it!= nullptr){
return false;
}
}
return true;
}
bool operator==(const HashTable& a, const HashTable& b){
return a.arr == b.arr;
}
bool operator!=(const HashTable& a, const HashTable& b){
return a.arr != b.arr;
}