-
Notifications
You must be signed in to change notification settings - Fork 2
/
Hash_Table_DoubleHash.h
171 lines (147 loc) · 4.1 KB
/
Hash_Table_DoubleHash.h
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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
template <typename ValType>
class HashTable {
private:
class TableCell {
public:
bool isset;
std::string key;
ValType value;
TableCell() {
isset = false;
key = "";
value = ValType();
}
};
std::vector<TableCell> hash_table;
public:
explicit HashTable () {
hash_table.assign(8, TableCell());
insertions = 0;
}
bool contains(
const std::string &key
) {
size_t real_index = getRealIndex(hash_table, key, hash_table.size());
return hash_table[real_index].isset;
}
ValType& get(
const std::string &key
) {
size_t real_index = getRealIndex(hash_table, key, hash_table.size());
return hash_table[real_index].value;
}
void remove(
const std::string& key
) {
size_t real_index = getRealIndex(hash_table, key, hash_table.size());
hash_table[real_index].isset = false;
}
void insert(
const std::string& key,
const ValType& value
) {
if (((double)insertions)/hash_table.size() >= 0.7) {
rehash();
}
size_t real_index = getRealIndex(hash_table, key, hash_table.size());
hash_table[real_index].key = key;
hash_table[real_index].value = value;
if (!hash_table[real_index].isset) {
++insertions;
}
hash_table[real_index].isset = true;
}
bool erase(const std::string& key) {
return false;
}
ValType& operator [] (const std::string &key) {
if (!contains(key)) {
insert(key, ValType());
}
return get(key);
}
const ValType& operator [] (const std::string &key) const {
return get(key);
}
private:
size_t insertions;
size_t getRealIndex (
const std::vector<TableCell> &table,
const std::string& key,
size_t size
) {
size_t i = hash(key, size);
size_t h2 = hash2(key, size);
size_t k = 0;
while (table[i].key != key && table[(i + h2 * k) % size].isset) {
++k;
}
return (i + h2 * k) % size;
}
size_t hash(const std::string &s, size_t size) {
size_t result = 0;
int p = 3;
for (char i : s) {
result += (size_t)i;
result *= p;
result %= size;
}
return result;
}
size_t hash2(const std::string &s, size_t size) {
return (s.length() + (size_t)(s.length() % 2 == 0)) % size;
}
void rehash() {
size_t new_size = 2 * hash_table.size();
std::vector<TableCell> new_table(new_size);
size_t new_insertions = 0;
for (auto& element : hash_table) {
if (element.isset) {
size_t real_index = getRealIndex(new_table, element.key, new_size);
new_table[real_index].key = element.key;
new_table[real_index].value = element.value;
new_table[real_index].isset = true;
++new_insertions;
}
}
hash_table = new_table;
insertions = new_insertions;
}
};
void makeTable(std::istream &in, HashTable<std::string> &ht) {
char line[512];
while (in.getline(line, 512)) {
size_t i = 0;
std::string number;
std::string data;
while (line[i] != ',') {
number += line[i];
++i;
}
++i;
while (line[i] == ' ') {
++i;
}
while (line[i] != '\0') {
data += line[i];
++i;
}
ht[number] = data;
}
}
int main() {
HashTable<std::string> ht;
std::ifstream fs("/home/vh/CLionProjects/DoubleHashF/data.txt");
makeTable(fs, ht);
std::cout << ht["788GGG7"] << std::endl;
std::cout << ht.get("AI9896D") << std::endl;
std::cout << ht["AC57577"] << std::endl;
std::cout << ht.contains("AJ89899") << std::endl;
ht.remove("AJ89899");
std::cout << ht.contains("AJ89899") << std::endl;
return 0;
}