-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.cc
More file actions
243 lines (188 loc) · 6.19 KB
/
hash.cc
File metadata and controls
243 lines (188 loc) · 6.19 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
#include "hash.h"
#include <cassert>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using jnp1::const_sequence_t;
using jnp1::element_t;
using jnp1::hash_function_t;
using jnp1::table_id_t;
using vector_t = ::std::vector<element_t>;
using function_vector_pair_t = ::std::pair<hash_function_t, vector_t>;
namespace {
#ifdef NDEBUG
const bool debug = false;
#else
const bool debug = true;
#endif
struct HashingStruct {
hash_function_t hash_function;
size_t operator()(const vector_t &v) const {
return hash_function(v.data(), v.size());
}
};
using hash_set_t = ::std::unordered_set<vector_t, HashingStruct>;
using function_set_pair_t = ::std::pair<hash_function_t, hash_set_t>;
using tables_t = ::std::unordered_map<table_id_t, function_set_pair_t>;
tables_t &tables() {
// To prevent order initalization fiasco, the object is created on first use.
static tables_t *x = new tables_t();
return *x;
}
table_id_t next_id = 0;
std::string table_id_to_string(table_id_t id) {
return "hash table #" + std::to_string(id);
}
bool table_contains_sequence(table_id_t id, const_sequence_t seq, size_t size) {
auto pair = tables().at(id);
return pair.second.find({seq, seq + size}) != pair.second.end();
}
bool check_if_table_exists(table_id_t id, std::string source_function) {
bool result = tables().count(id);
if (!result && debug) {
if (!source_function.empty()) {
std::cerr << source_function << ": " << table_id_to_string(id);
}
std::cerr << " does not exist\n";
}
return result;
}
bool check_if_sequence_is_valid(const_sequence_t seq, size_t size,
std::string source_function) {
bool result = true;
if (seq == NULL) {
result = false;
if (debug) {
std::cerr << source_function << ": invalid pointer (NULL)\n";
}
}
if (size == 0) {
result = false;
if (debug) {
std::cerr << source_function << ": invalid size (0)\n";
}
}
return result;
}
bool check_if_sequence_is_valid_and_table_exists(table_id_t id,
const_sequence_t seq,
size_t size,
std::string source_function) {
if (!check_if_sequence_is_valid(seq, size, source_function)) return false;
if (!check_if_table_exists(id, source_function)) return false;
return true;
}
std::string sequence_to_string(const_sequence_t seq, size_t size) {
if (seq == NULL) return "NULL";
std::stringstream ss("");
ss << '"';
for (size_t i = 0; i < size; i++) {
if (i > 0) ss << " ";
ss << seq[i];
}
ss << '"';
return ss.str();
}
void print_table_and_sequence_arguments(table_id_t id, const_sequence_t seq,
size_t size) {
std::cerr << "(" << id << ", " << sequence_to_string(seq, size) << ", "
<< size << ")\n";
}
} // namespace
namespace jnp1 {
table_id_t hash_create(hash_function_t hash_function) {
assert(hash_function != NULL);
if (debug) std::cerr << __func__ << "(" << hash_function << ")\n";
table_id_t id = next_id++;
tables()[id] = {hash_function, hash_set_t(0, {hash_function})};
if (debug)
std::cerr << __func__ << ": " << table_id_to_string(id) << " created\n";
return id;
}
void hash_delete(table_id_t id) {
if (debug) std::cerr << __func__ << "(" << id << ")\n";
if (!check_if_table_exists(id, __func__)) return;
if (debug)
std::cerr << __func__ << ": " << table_id_to_string(id) << " deleted\n";
tables().erase(id);
}
size_t hash_size(table_id_t id) {
if (debug) std::cerr << __func__ << "(" << id << ")\n";
if (!check_if_table_exists(id, __func__)) return 0;
size_t size = tables()[id].second.size();
if (debug)
std::cerr << __func__ << ": " << table_id_to_string(id) << " contains "
<< size << " element(s)\n";
return size;
}
bool hash_insert(table_id_t id, const_sequence_t seq, size_t size) {
if (debug) {
std::cerr << "hash_insert";
print_table_and_sequence_arguments(id, seq, size);
}
if (!check_if_sequence_is_valid_and_table_exists(id, seq, size, __func__))
return false;
if (debug)
std::cerr << "hash_insert: " << table_id_to_string(id) << ", sequence "
<< sequence_to_string(seq, size);
if (table_contains_sequence(id, seq, size)) {
if (debug) std::cerr << " was present\n";
return false;
}
function_set_pair_t pair = tables().at(id);
tables()[id].second.insert({seq, seq + size});
if (debug) std::cerr << " inserted\n";
return true;
}
bool hash_remove(table_id_t id, const_sequence_t seq, size_t size) {
if (debug) {
std::cerr << __func__;
print_table_and_sequence_arguments(id, seq, size);
}
if (!check_if_sequence_is_valid_and_table_exists(id, seq, size, __func__))
return false;
if (debug)
std::cerr << __func__ << ": " << table_id_to_string(id) << ", sequence "
<< sequence_to_string(seq, size);
if (!table_contains_sequence(id, seq, size)) {
if (debug) std::cerr << " was not present\n";
return false;
}
function_set_pair_t pair = tables().at(id);
tables().at(id).second.erase({seq, seq + size});
if (debug) std::cerr << " removed\n";
return true;
}
void hash_clear(table_id_t id) {
if (debug)
std::cerr << __func__ << "(" << id << ")\n"
<< __func__ << ": " << table_id_to_string(id);
if (check_if_table_exists(id, "")) {
if (tables().at(id).second.empty()) {
if (debug) std::cerr << " was empty\n";
return;
}
tables().at(id).second.clear();
if (debug) std::cerr << " cleared\n";
}
}
bool hash_test(table_id_t id, const_sequence_t seq, size_t size) {
if (debug) {
std::cerr << __func__;
print_table_and_sequence_arguments(id, seq, size);
}
if (!check_if_sequence_is_valid_and_table_exists(id, seq, size, __func__))
return false;
if (debug)
std::cerr << __func__ << ": " << table_id_to_string(id) << ", sequence "
<< sequence_to_string(seq, size);
if (table_contains_sequence(id, seq, size)) {
if (debug) std::cerr << " is present\n";
return true;
}
if (debug) std::cerr << " is not present\n";
return false;
}
} // namespace jnp1