forked from bbondy/hashset-cpp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhash_set.h
377 lines (336 loc) · 10 KB
/
hash_set.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* Copyright (c) 2015 Brian R. Bondy. Distributed under the MPL2 license.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// You should probably not be using this. This is only useful in environments
// without std lib and having specific serialization and memory requirements.
// Instead consider using `hash_set` which is a more generic implementation
// with templates.
#ifndef HASH_SET_H_
#define HASH_SET_H_
#include <stdint.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <vector>
#include "./base.h"
#include "./hash_item.h"
template<class T>
class HashSet {
public:
typedef uint64_t (*HashSetFnPtr)(const T *hash_item);
/**
* @param bucket_count The number of buckets to create for the hash set
* @param multi_set Allow multiple items with the same hash to be added.
*/
explicit HashSet(uint32_t bucket_count, bool multi_set)
: multi_set_(multi_set) {
Init(bucket_count);
}
~HashSet() {
Cleanup();
}
/**
* Adds the specified data if it doesn't exist
* A copy of the data will be created, so the memory used to do the add
* doesn't need to stick around.
*
* @param item_to_add The node to add
* @param update_if_exists true if the item should be updated if it is already there
* false if the add should fail if it is alraedy there. If multi_set is true and
* upate_if_exists is false than the same hash'ed item will be added again.
* @return true if the data was added
*/
bool Add(const T &item_to_add, bool update_if_exists = true) {
uint64_t hash = item_to_add.GetHash();
HashItem<T> *hash_item = buckets_[hash % bucket_count_];
if (!hash_item) {
hash_item = new HashItem<T>();
hash_item->hash_item_storage_ = new T(item_to_add);
buckets_[hash % bucket_count_] = hash_item;
size_++;
return true;
}
while (true) {
if (*hash_item->hash_item_storage_ == item_to_add) {
if (update_if_exists) {
hash_item->hash_item_storage_->Update(item_to_add);
return false;
} else if (!multi_set_) {
return false;
}
}
if (!hash_item->next_) {
HashItem<T> *created_hash_item = new HashItem<T>();
created_hash_item->hash_item_storage_ = new T(item_to_add);
hash_item->next_ = created_hash_item;
break;
}
hash_item = hash_item->next_;
}
size_++;
return true;
}
/**
* Determines if the specified data exists in the set or not`
* @param data_to_check The data to check
* @return true if the data found
*/
bool Exists(const T &data_to_check) {
uint64_t hash = data_to_check.GetHash();
HashItem<T> *hash_item = buckets_[hash % bucket_count_];
if (!hash_item) {
return false;
}
while (hash_item) {
if (*hash_item->hash_item_storage_ == data_to_check) {
return true;
}
hash_item = hash_item->next_;
}
return false;
}
/**
* Determines if the specified data exists in the set or not`
* @param data_to_check The data to check
* @return true if the data found
*/
size_t GetMatchingCount(const T &data_to_check) {
size_t count = 0;
uint64_t hash = data_to_check.GetHash();
HashItem<T> *hash_item = buckets_[hash % bucket_count_];
if (!hash_item) {
return count;
}
while (hash_item) {
if (*hash_item->hash_item_storage_ == data_to_check) {
count++;
}
hash_item = hash_item->next_;
}
return count;
}
/**
* Finds the specific data in the hash set.
* This is useful because sometimes it contains more context
* than the object used for the lookup.
* @param data_to_check The data to check
* @return The data stored in the hash set or nullptr if none is found.
*/
T * Find(const T &data_to_check) {
uint64_t hash = data_to_check.GetHash();
HashItem<T> *hash_item = buckets_[hash % bucket_count_];
if (!hash_item) {
return nullptr;
}
while (hash_item) {
if (*hash_item->hash_item_storage_ == data_to_check) {
return hash_item->hash_item_storage_;
}
hash_item = hash_item->next_;
}
return nullptr;
}
/**
* Finds the specific data in the hash set.
* This is useful because sometimes it contains more context
* than the object used for the lookup.
* @param data_to_check The data to check
* @return The data stored in the hash set or nullptr if none is found.
*/
void FindAll(const T &data_to_check, std::vector<T*> *result) {
uint64_t hash = data_to_check.GetHash();
HashItem<T> *hash_item = buckets_[hash % bucket_count_];
if (!hash_item) {
return;
}
while (hash_item) {
if (*hash_item->hash_item_storage_ == data_to_check) {
result->push_back(hash_item->hash_item_storage_);
}
hash_item = hash_item->next_;
}
}
/**
* Removes the specific data in the hash set.
* @param data_to_check The data to remove
* @return true if an item matching the data was removed
*/
bool Remove(const T &data_to_check) {
uint64_t hash = data_to_check.GetHash();
HashItem<T> *hash_item = buckets_[hash % bucket_count_];
if (!hash_item) {
return false;
}
HashItem<T> *last_item = nullptr;
while (hash_item) {
if (*hash_item->hash_item_storage_ == data_to_check) {
if (last_item) {
last_item->next_ = hash_item->next_;
delete hash_item;
} else {
buckets_[hash % bucket_count_] = hash_item->next_;
delete hash_item;
}
size_--;
return true;
}
last_item = hash_item;
hash_item = hash_item->next_;
}
return false;
}
/**
* Obtains the number of items in the Hash Set
*/
uint32_t GetSize() {
return size_;
}
/**
* Serializes the parsed data and bloom filter data into a single buffer.
* @param size The size is returned in the out parameter if it's needed to
* write to a file.
* @return The returned buffer should be deleted by the caller.
*/
char * Serialize(uint32_t *size) {
*size = 0;
*size += SerializeBuckets(nullptr);
char *buffer = new char[*size];
memset(buffer, 0, *size);
SerializeBuckets(buffer);
return buffer;
}
/**
* Deserializes the buffer.
* Memory passed in will be used by this instance directly without copying
* it in.
* @param buffer The serialized data to deserialize
* @param buffer_size the size of the buffer to deserialize
* @return true if the operation was successful
*/
bool Deserialize(char *buffer, uint32_t buffer_size) {
Cleanup();
uint32_t pos = 0;
if (!HasNewlineBefore(buffer, buffer_size)) {
return false;
}
uint32_t multi_set = 0;
sscanf(buffer + pos, "%x,%x", &bucket_count_, &multi_set);
multi_set_ = multi_set != 0;
buckets_ = new HashItem<T> *[bucket_count_];
memset(buckets_, 0, sizeof(HashItem<T>*) * bucket_count_);
pos += static_cast<uint32_t>(strlen(buffer + pos)) + 1;
if (pos >= buffer_size) {
return false;
}
for (uint32_t i = 0; i < bucket_count_; i++) {
HashItem<T> *last_hash_item = nullptr;
while (*(buffer + pos) != '\0') {
if (pos >= buffer_size) {
return false;
}
HashItem<T> *hash_item = new HashItem<T>();
hash_item->hash_item_storage_ = new T();
uint32_t deserialize_size =
hash_item->hash_item_storage_->Deserialize(buffer + pos,
buffer_size - pos);
pos += deserialize_size;
if (pos >= buffer_size || deserialize_size == 0) {
return false;
}
size_++;
if (last_hash_item) {
last_hash_item->next_ = hash_item;
} else {
buckets_[i] = hash_item;
}
last_hash_item = hash_item;
}
pos++;
}
return true;
}
/**
* Clears the HashSet back to the original dimensions but
* with no data.
*/
void Clear() {
auto old_bucket_count = bucket_count_;
Cleanup();
Init(old_bucket_count);
}
/**
* Returns true if the hash_set is a multi_set
*/
bool IsMultiSet() const {
return multi_set_;
}
private:
bool HasNewlineBefore(char *buffer, uint32_t buffer_size) {
char *p = buffer;
for (uint32_t i = 0; i < buffer_size; ++i) {
if (*p == '\0')
return true;
p++;
}
return false;
}
void Init(uint32_t num_buckets) {
bucket_count_ = num_buckets;
buckets_ = nullptr;
size_ = 0;
if (bucket_count_ != 0) {
buckets_ = new HashItem<T>*[bucket_count_];
memset(buckets_, 0, sizeof(HashItem<T>*) * bucket_count_);
}
}
void Cleanup() {
if (buckets_) {
for (uint32_t i = 0; i < bucket_count_; i++) {
HashItem<T> *hash_item = buckets_[i];
while (hash_item) {
HashItem<T> *temp_hash_item = hash_item;
hash_item = hash_item->next_;
delete temp_hash_item;
}
}
delete[] buckets_;
buckets_ = nullptr;
bucket_count_ = 0;
size_ = 0;
}
}
uint32_t SerializeBuckets(char *buffer) {
uint32_t total_size = 0;
char sz[512];
total_size += 1 + snprintf(sz, sizeof(sz), "%x,%x",
bucket_count_, multi_set_ ? 1 : 0);
if (buffer) {
memcpy(buffer, sz, total_size);
}
for (uint32_t i = 0; i < bucket_count_; i++) {
HashItem<T> *hash_item = buckets_[i];
while (hash_item) {
if (buffer) {
total_size +=
hash_item->hash_item_storage_->Serialize(buffer + total_size);
} else {
total_size += hash_item->hash_item_storage_->Serialize(nullptr);
}
hash_item = hash_item->next_;
}
if (buffer) {
buffer[total_size] = '\0';
}
// Second null terminator to show next bucket
total_size++;
}
return total_size;
}
protected:
bool multi_set_;
uint32_t bucket_count_;
HashItem<T> **buckets_;
uint32_t size_;
};
#endif // HASH_SET_H_