-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemlimtiHashTable.cpp
More file actions
187 lines (168 loc) · 5.23 KB
/
Copy pathMemlimtiHashTable.cpp
File metadata and controls
187 lines (168 loc) · 5.23 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
#include <iostream>
#include <vector>
#include <optional>
#include <string>
#include <shared_mutex>
#include <type_traits>
#include <mutex>
template<typename K, typename V, size_t bucket_num = 16>
class HashTable {
private:
struct Node {
K key;
V value;
Node* next;
Node(const K& k, const V& v) : key(k), value(v), next(nullptr) {}
};
std::vector<Node*> _buckets;
size_t _memory_used;
size_t _memory_limit;
mutable std::shared_mutex _mtx;
size_t hash(const K& key) const {
return std::hash<K>{}(key) % bucket_num;
}
size_t key_mem_usage(const K& key) const {
if constexpr (std::is_same<K, std::string>::value) {
return key.capacity();
} else {
return sizeof(K);
}
}
size_t value_mem_usage(const V& value) const {
if constexpr (std::is_same<V, std::string>::value) {
return value.capacity();
} else {
return sizeof(V);
}
}
size_t node_mem_usage(const K& key, const V& value) const {
return sizeof(Node) + key_mem_usage(key) + value_mem_usage(value);
}
size_t node_mem_usage(const Node* node) const {
return node_mem_usage(node->key, node->value);
}
// 释放所有节点
void clear() {
for (size_t i = 0; i < _buckets.size(); ++i) {
Node* curr = _buckets[i];
while (curr) {
Node* next = curr->next;
delete curr;
curr = next;
}
_buckets[i] = nullptr;
}
_memory_used = 0;
}
public:
HashTable(size_t mem_limit)
: _buckets(bucket_num, nullptr), _memory_used(0), _memory_limit(mem_limit) {}
// 禁用拷贝
HashTable(const HashTable&) = delete;
HashTable& operator=(const HashTable&) = delete;
// 移动构造
HashTable(HashTable&& other) noexcept
: _buckets(std::move(other._buckets)),
_memory_used(other._memory_used),
_memory_limit(other._memory_limit) {
// 不能拷贝锁,直接初始化新锁
other._memory_used = 0;
other._memory_limit = 0;
// buckets被move后,other的桶指针已为空
}
// 移动赋值
HashTable& operator=(HashTable&& other) noexcept {
if (this != &other) {
std::unique_lock lock1(_mtx, std::defer_lock);
std::unique_lock lock2(other._mtx, std::defer_lock);
std::lock(lock1, lock2);
clear();
_buckets = std::move(other._buckets);
_memory_used = other._memory_used;
_memory_limit = other._memory_limit;
other._memory_used = 0;
other._memory_limit = 0;
}
return *this;
}
// 析构函数
~HashTable() {
clear();
}
bool insert(const K& key, const V& value) {
std::unique_lock lock(_mtx);
size_t idx = hash(key);
Node* curr = _buckets[idx];
while (curr) {
if (curr->key == key) {
_memory_used -= value_mem_usage(curr->value);
_memory_used += value_mem_usage(value);
curr->value = value;
return true;
}
curr = curr->next;
}
size_t need_mem = node_mem_usage(key, value);
if (_memory_used + need_mem > _memory_limit) {
return false;
}
Node* new_node = new Node(key, value);
new_node->next = _buckets[idx];
_buckets[idx] = new_node;
_memory_used += need_mem;
return true;
}
std::optional<V> find(const K& key) const {
std::shared_lock lock(_mtx);
size_t idx = hash(key);
Node* curr = _buckets[idx];
while (curr) {
if (curr->key == key) {
return curr->value;
}
curr = curr->next;
}
return std::nullopt;
}
bool erase(const K& key) {
std::unique_lock lock(_mtx);
size_t idx = hash(key);
Node* curr = _buckets[idx];
Node* prev = nullptr;
while (curr) {
if (curr->key == key) {
if (prev) {
prev->next = curr->next;
} else {
_buckets[idx] = curr->next;
}
_memory_used -= node_mem_usage(curr);
delete curr;
return true;
}
prev = curr;
curr = curr->next;
}
return false;
}
size_t get_memory_used() const {
std::shared_lock lock(_mtx);
return _memory_used;
}
size_t get_memory_limit() const { return _memory_limit; }
};
int main() {
HashTable<std::string, std::string, 8> ht(256);
int i = 0;
while (ht.insert("key" + std::to_string(i), "val" + std::to_string(i))) {
std::cout << "Inserted: key" << i << ", memory used: " << ht.get_memory_used() << " bytes\n";
++i;
}
std::cout << "Insert failed for key" << i << ", memory limit reached!\n";
ht.erase("key0");
std::cout << "After erase, memory used: " << ht.get_memory_used() << " bytes\n";
if (ht.insert("key100", "val100")) {
std::cout << "Inserted key100 after freeing space!\n";
}
return 0;
}