-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_hashmap.hpp
292 lines (250 loc) · 7.58 KB
/
linked_hashmap.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
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
/**
* implement a container like std::linked_hashmap
*/
#ifndef SJTU_LINKEDHASHMAP_HPP
#define SJTU_LINKEDHASHMAP_HPP
// only for std::equal_to<T> and std::hash<T>
#include <functional>
#include <cstddef>
#include "utility.hpp"
#include "exceptions.hpp"
#include "list.hpp"
namespace sjtu {
/**
* In linked_hashmap, iteration ordering is differ from map,
* which is the order in which keys were inserted into the map.
* You should maintain a doubly-linked list running through all
* of its entries to keep the correct iteration order.
*
* Note that insertion order is not affected if a key is re-inserted
* into the map.
*/
template<class T>
class _hashnode_t {
public:
T data;
_hashnode_t* prev, * next, * hash_next;
explicit _hashnode_t(const T& data) :
data(data), prev(nullptr), next(nullptr), hash_next(nullptr) {}
explicit _hashnode_t(T&& data) :
data(data), prev(nullptr), next(nullptr), hash_next(nullptr) {}
};
template<
class Key,
class T,
class Hash = std::hash<Key>,
class Equal = std::equal_to<Key>
> class linked_hashmap : protected list<pair<Key, T>, _hashnode_t<pair<Key, T>>> {
public:
/**
* the internal type of data.
* it should have a default constructor, a copy constructor.
* You can use sjtu::linked_hashmap as value_type by typedef.
*/
using value_type = pair<const Key, T>;
private:
static const size_t INITAL_CAPACITY = 129, LOAD_FACTOR = 75;
using list_t = list<pair<Key, T>, _hashnode_t<pair<Key, T>>>;
using linknode_t = typename list_t::node;
linknode_t** _table;
size_t _table_size;
Hash _hash;
Equal _equal;
// iterate through list and initalize hash table
void _rehash_list() {
std::fill(_table, _table + _table_size, nullptr);
for (linknode_t* nd = this->_begin; nd; nd = nd->next) {
size_t index = _hash(nd->data.first) % _table_size;
nd->hash_next = _table[index], _table[index] = nd;
}
}
// Get the reference of *hash_next*, which has the target key
// Will get a nullptr if not found, dirctly put value in it
linknode_t*& _find(const Key& key) const {
size_t pos = _hash(key) % _table_size;
if (_table[pos] == nullptr || _equal(_table[pos]->data.first, key))
return _table[pos];
linknode_t* node = _table[pos];
while (node->hash_next && !_equal(node->hash_next->data.first, key))
node = node->hash_next;
return node->hash_next;
}
inline void _expand() {
delete[] _table;
_table_size = _table_size * 2 + 3;
_table = new linknode_t * [_table_size];
_rehash_list();
}
public:
using iterator = typename list_t::iterator;
using const_iterator = typename list_t::const_iterator;
/**
* TODO two constructors
*/
linked_hashmap() : list_t::list(), _table_size(INITAL_CAPACITY) {
_table = new linknode_t * [_table_size];
std::fill(_table, _table + _table_size, nullptr);
}
linked_hashmap(const linked_hashmap& other) :
list_t::list(other), _table_size(other._table_size) {
_table = new linknode_t * [_table_size];
_rehash_list();
}
/**
* TODO assignment operator
*/
linked_hashmap& operator=(const linked_hashmap& other) {
if (this == &other) return *this;
static_cast<list_t*>(this)->operator=(other);
delete[] _table;
_table_size = other._table_size;
_table = new linknode_t * [_table_size];
_rehash_list();
return *this;
}
/**
* TODO Destructors
*/
~linked_hashmap() {
delete[] _table;
}
/**
* TODO
* access specified element with bounds checking
* Returns a reference to the mapped value of the element with key equivalent to key.
* If no such element exists, an exception of type `index_out_of_bound'
*/
T& at(const Key& key) {
linknode_t* nd = _find(key);
if (nd) return nd->data.second;
throw sjtu::index_out_of_bound();
}
const T& at(const Key& key) const {
linknode_t* nd = _find(key);
if (nd) return nd->data.second;
throw sjtu::index_out_of_bound();
}
/**
* TODO
* access specified element
* Returns a reference to the value that is mapped to a key equivalent to key,
* performing an insertion if such key does not already exist.
*/
inline T& operator[](const Key& key) {
if (this->_size >= _table_size * LOAD_FACTOR / 100)
_expand();
linknode_t*& node = _find(key);
if (node) return node->data.second;
node = this->_insert(nullptr, new linknode_t(pair<Key, T>(key, T())));
return node->data.second;
}
/**
* behave like at() throw index_out_of_bound if such key does not exist.
*/
inline const T& operator[](const Key& key) const {
return at(key);
}
/**
* return a iterator to the beginning
*/
inline iterator begin() {
return iterator(this->_begin, &this->_end);
}
inline const_iterator cbegin() const {
return const_iterator(this->_begin, &this->_end);
}
/**
* return a iterator to the end
* in fact, it returns past-the-end.
*/
inline iterator end() {
return iterator(nullptr, &this->_end);
}
inline const_iterator cend() const {
return const_iterator(nullptr, &this->_end);
}
/**
* checks whether the container is empty
* return true if empty, otherwise false.
*/
inline bool empty() const {
return this->_size == 0;
}
/**
* returns the number of elements.
*/
inline size_t size() const {
return this->_size;
}
/**
* clears the contents
*/
void clear() {
this->_clear();
std::fill(_table, _table + _table_size, nullptr);
}
/**
* insert an element.
* return a pair, the first of the pair is
* the iterator to the new element (or the element that prevented the insertion),
* the second one is true if insert successfully, or false.
*/
inline pair<iterator, bool> insert(const value_type& value) {
return insert(std::move(value_type(value)));
}
pair<iterator, bool> insert(value_type&& value) {
if (this->_size >= _table_size * LOAD_FACTOR / 100)
_expand();
linknode_t*& pos = _find(value.first);
if (pos && _equal(value.first, pos->data.first)) {
return sjtu::pair<iterator, bool>(iterator(pos, &this->_end), false);
}
pos = this->_insert(nullptr, new linknode_t(value));
return sjtu::pair<iterator, bool>(iterator(pos, &this->_end), true);
}
/**
* erase the element at pos.
*
* throw if pos pointed to a bad element (pos == this->end() || pos points an element out of this)
*/
iterator erase(iterator pos) {
if (!this->_check_iterator(pos) || pos == end()) {
throw sjtu::invalid_iterator();
}
if (this->_size < _table_size / 4 && _table_size > INITAL_CAPACITY) {
_table_size = (_table_size - 3) / 2;
delete[] _table;
_table = new linknode_t * [_table_size];
_rehash_list();
}
linknode_t*& node = _find(pos->first), * tmp_node = node;
if (node == nullptr) return end();
iterator ret_iter(this->_erase(node), &this->_end);
node = node->hash_next;
delete tmp_node;
return ret_iter;
}
/**
* Returns the number of elements with key
* that compares equivalent to the specified argument,
* which is either 1 or 0
* since this container does not allow duplicates.
*/
inline size_t count(const Key& key) const {
return _find(key) ? 1 : 0;
}
/**
* Finds an element with key equivalent to key.
* key value of the element to search for.
* Iterator to an element with key equivalent to key.
* If no such element is found, past-the-end (see end()) iterator is returned.
*/
inline iterator find(const Key& key) {
return iterator(_find(key), &this->_end);
}
inline const_iterator find(const Key& key) const {
return const_iterator(_find(key), &this->_end);
}
};
}
#endif