-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashTable.hpp
65 lines (54 loc) · 1.48 KB
/
HashTable.hpp
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
#pragma once
#include <list>
#include <optional>
#include <vector>
template <typename Value, class Hash, class EqualsTo>
class HashTable {
public:
HashTable(size_t size) : table_(size), size_(size) {}
bool Insert(const Value& value);
void Delete(const Value& value);
bool Contains(const Value& value);
private:
using Container = std::vector<std::list<Value> >;
Container table_;
Hash hash_;
EqualsTo equals_to_;
size_t size_;
};
template <typename Value, class Hash, class EqualsTo>
bool HashTable<Value, Hash, EqualsTo>::Insert(const Value& value) {
size_t position = hash_(value) % size_;
for (auto& element : table_[position]) {
if (equals_to_(element, value)) {
element = value;
return false;
}
}
table_[position].push_back(value);
return true;
}
template <typename Value, class Hash, class EqualsTo>
void HashTable<Value, Hash, EqualsTo>::Delete(const Value& value) {
size_t position = hash_(value) % size_;
auto it = table_[position].begin();
auto erase_element = it;
while (it != table_[position].end()) {
if (equals_to_(*it, value)) {
erase_element = it;
++it;
table_[position].erase(erase_element);
}
++it;
}
}
template <typename Value, class Hash, class EqualsTo>
bool HashTable<Value, Hash, EqualsTo>::Contains(const Value& value) {
size_t position = hash_(value) % size_;
for (const auto& element : table_[position]) {
if (equals_to_(element, value)) {
return true;
}
}
return false;
}