-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashtable_naive.cpp
51 lines (45 loc) · 1.2 KB
/
hashtable_naive.cpp
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
#include <functional>
#include <vector>
#include "benchmark.hpp"
class Hashtable {
public:
spinlock lock;
std::vector<std::vector<data> > buckets;
std::hash<data> hashfn;
Hashtable(): buckets(hashtable_elms / 10) {}
void insert(data item) {
size_t bucket = hashfn(item) % buckets.size();
lock.lock();
buckets[bucket].push_back(item);
lock.unlock();
}
bool remove(data item) {
size_t bucket = hashfn(item) % buckets.size();
lock.lock();
for (auto it = buckets[bucket].begin(); it != buckets[bucket].end(); it++) {
if (*it == item) {
buckets[bucket].erase(it);
lock.unlock();
return true;
}
}
lock.unlock();
return false;
}
bool find(data item) {
size_t bucket = hashfn(item) % buckets.size();
lock.lock();
for (auto it = buckets[bucket].begin(); it != buckets[bucket].end(); it++) {
if (*it == item) {
lock.unlock();
return true;
}
}
lock.unlock();
return false;
}
};
int main() {
Hashtable h;
benchmark_hashtable(h);
}