-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashList.h
433 lines (363 loc) · 12.6 KB
/
HashList.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#ifndef HashList_D679BA40CEC24336AE47CA16782DFFBA
#define HashList_D679BA40CEC24336AE47CA16782DFFBA
#include <cstring>
#include <iostream>
#include <stdexcept>
#include <functional>
#include <iterator>
#include <utility>
#include <memory>
template <typename Tk, typename Ti, class Hasher = std::hash<Tk>>
class HashList {
/*
* It would be more efficient to use a "Node" array to reduce data fragmentation and decrease allocation calls count and increase perfomance.
* However, this method is more descriptive and simple.
* In addition, this approach allowed not to use raw memory manipulation functions (like memcpy).
*/
std::shared_ptr<std::shared_ptr<std::shared_ptr<std::pair<Tk, Ti>>[]>[]> chunks;
std::unique_ptr<size_t[]> chunks_icount;
size_t chunks_count = 1,
chunk_capacity = 1024, chunk_items_count_threshold = 768;
Hasher ghash;
inline void HashIncrement(size_t &hash) const {
if (++hash == chunk_capacity)
hash = 0;
}
inline size_t CalcCount() const {
size_t summ = 0;
for (size_t i = 0; i != chunks_count; ++i) {
summ += chunks_icount[i];
}
return summ;
}
public:
HashList(const HashList &arg) = delete;
HashList &operator=(const HashList &arg) = delete;
HashList(float fulness_coefficient = 0.75, size_t capacity = 1024): chunk_capacity(capacity) {
chunks = std::shared_ptr<std::shared_ptr<std::shared_ptr<std::pair<Tk, Ti>>[]>[]>(new std::shared_ptr<std::shared_ptr<std::pair<Tk, Ti>>[]>[chunks_count]);
chunks[0] = std::shared_ptr<std::shared_ptr<std::pair<Tk, Ti>>[]>(new std::shared_ptr<std::pair<Tk, Ti>>[chunk_capacity]);
chunks_icount = std::unique_ptr<size_t[]>(new size_t[1]);
chunks_icount[0] = 0;
if (fulness_coefficient <= 0 || fulness_coefficient >= 1) throw std::out_of_range("Coefficient must be between 0 and 1!");
chunk_items_count_threshold = capacity * fulness_coefficient;
}
HashList(const std::initializer_list<std::pair<Tk, Ti>> &list) {
chunks = std::shared_ptr<std::shared_ptr<std::shared_ptr<std::pair<Tk, Ti>>[]>[]>(new std::shared_ptr<std::shared_ptr<std::pair<Tk, Ti>>[]>[chunks_count]);
if (list.size() * 4 < 1024) {
chunks[0] = std::shared_ptr<std::shared_ptr<std::pair<Tk, Ti>>[]>(new std::shared_ptr<std::pair<Tk, Ti>>[chunk_capacity]);
} else {
chunk_capacity = list.size() << 2;
chunk_items_count_threshold = chunk_capacity * 0.75;
chunks[0] = std::shared_ptr<std::shared_ptr<std::pair<Tk, Ti>>[]>(new std::shared_ptr<std::pair<Tk, Ti>>[chunk_capacity]);
}
chunks_icount = std::unique_ptr<size_t[]>(new size_t[1]);
chunks_icount[0] = 0;
for (auto pitem : list) {
size_t ohash = ghash(pitem.first) % chunk_capacity;
bool need_more = true;
if (chunks[0][ohash].get() == nullptr) {
chunks[0][ohash] = std::make_unique<std::pair<Tk, Ti>>(pitem.first, pitem.second);
++chunks_icount[0];
need_more = false;
} else if (chunks[0][ohash]->first == pitem.first) {
chunks[0][ohash] = std::make_unique<std::pair<Tk, Ti>>(pitem.first, pitem.second);
need_more = false;
}
if (need_more) {
//Search for existing
size_t nhash = ohash;
HashIncrement(nhash);
while (chunks[0][nhash].get() != nullptr && nhash != ohash) {
if (chunks[0][nhash]->first == pitem.first) {
chunks[0][nhash] = std::make_unique<std::pair<Tk, Ti>>(pitem.first, pitem.second);
return;
}
HashIncrement(nhash);
}
//Insert
chunks[0][nhash] = std::make_unique<std::pair<Tk, Ti>>(pitem.first, pitem.second);
++chunks_icount[0];
}
}
}
void emplace(const Tk &key, const Ti &item) {
size_t ohash = ghash(key) % chunk_capacity;
size_t min_items_in = 0;
for (size_t chi = 0, min_items_count = SIZE_MAX; chi != chunks_count; ++chi) {
if (chunks[chi][ohash].get() == nullptr) {
chunks[chi][ohash] = std::make_unique<std::pair<Tk, Ti>>(key, item);
++chunks_icount[chi];
return;
} else if (chunks[chi][ohash]->first == key) {
chunks[chi][ohash] = std::make_unique<std::pair<Tk, Ti>>(key, item);
return;
}
if (chunks_icount[chi] < min_items_count)
min_items_in = chi;
}
if (chunks_icount[min_items_in] > chunk_items_count_threshold) {
size_t new_chunks_count = chunks_count + 1;
std::shared_ptr<std::shared_ptr<std::shared_ptr<std::pair<Tk, Ti>>[]>[]> new_chunks =
std::shared_ptr<std::shared_ptr<std::shared_ptr<std::pair<Tk, Ti>>[]>[]>(new std::shared_ptr<std::shared_ptr<std::pair<Tk, Ti>>[]>[new_chunks_count]);
std::unique_ptr<size_t[]>new_chunks_icount = std::unique_ptr<size_t[]>(new size_t[new_chunks_count]);
for (size_t chi = 0; chi != chunks_count; ++chi) {
new_chunks[chi] = std::shared_ptr<std::shared_ptr<std::pair<Tk, Ti>>[]>(new std::shared_ptr<std::pair<Tk, Ti>>[chunk_capacity]);
for (size_t i = 0; i != chunk_capacity; ++i) {
new_chunks[chi][i] = chunks[chi][i];
}
new_chunks_icount[chi] = chunks_icount[chi];
}
new_chunks[chunks_count] = std::shared_ptr<std::shared_ptr<std::pair<Tk, Ti>>[]>(new std::shared_ptr<std::pair<Tk, Ti>>[chunk_capacity]);
new_chunks_icount[chunks_count] = 0;
chunks_count = new_chunks_count;
chunks = new_chunks;
chunks_icount = std::move(new_chunks_icount);
}
//Search for existing
size_t nhash = ohash;
HashIncrement(nhash);
for (size_t chi = 0, min_items_count = SIZE_MAX; chi != chunks_count; ++chi) {
while (chunks[chi][nhash].get() != nullptr && nhash != ohash) {
if (chunks[chi][nhash]->first == key) {
chunks[chi][nhash] = std::make_unique<std::pair<Tk, Ti>>(key, item);
return;
}
HashIncrement(nhash);
}
nhash = ohash;
HashIncrement(nhash);
if (chunks_icount[chi] < min_items_count)
min_items_in = chi;
}
//Insert
while (true) {
if (chunks[min_items_in][ohash].get() == nullptr) {
chunks[min_items_in][ohash] = std::make_unique<std::pair<Tk, Ti>>(key, item);
++chunks_icount[min_items_in];
return;
}
HashIncrement(ohash);
}
}
Ti* find(const Tk& key) const {
size_t ohash = ghash(key) % chunk_capacity;
for (size_t chi = 0; chi != chunks_count; ++chi) {
if (chunks[chi][ohash].get() == nullptr) {
continue;
} else if (chunks[chi][ohash]->first == key) {
return &(chunks[chi][ohash]->second);
}
}
//Search for existing
size_t nhash = ohash;
HashIncrement(nhash);
for (size_t chi = 0; chi != chunks_count; ++chi) {
while (chunks[chi][nhash].get() != nullptr && nhash != ohash) {
if (chunks[chi][nhash]->first == key) {
return &(chunks[chi][nhash]->second);
}
HashIncrement(nhash);
}
nhash = ohash;
HashIncrement(nhash);
}
return nullptr;
}
bool erase(const Tk &key) {
size_t ohash = ghash(key) % chunk_capacity;
for (size_t chi = 0; chi != chunks_count; ++chi) {
if (chunks[chi][ohash].get() == nullptr) {
continue;
} else if (chunks[chi][ohash]->first == key) {
chunks[chi][ohash].reset();
--chunks_icount[chi];
return true;
}
}
//Search for existing
size_t nhash = ohash;
HashIncrement(nhash);
for (size_t chi = 0; chi != chunks_count; ++chi) {
while (chunks[chi][nhash].get() != nullptr && nhash != ohash) {
if (chunks[chi][nhash]->first == key) {
chunks[chi][nhash].reset();
--chunks_icount[chi];
return true;
}
HashIncrement(nhash);
}
nhash = ohash;
HashIncrement(nhash);
}
return false;
}
size_t count() const {
return CalcCount();
}
class iterator {
friend class HashList;
//Base info
std::weak_ptr<std::shared_ptr<std::shared_ptr<std::pair<Tk, Ti>>[]>[]> chunks;
size_t chunks_count, chunk_capacity;
//Uniq info
size_t current_chunk, current_item;
//First valid item
size_t valid_chunk_min, valid_item_min;
iterator(std::weak_ptr<std::shared_ptr<std::shared_ptr<std::pair<Tk, Ti>>[]>[]> chunks, size_t chunks_count, size_t chunk_capacity, size_t current_chunk, size_t current_item, bool pure):
chunks(chunks), chunks_count(chunks_count), chunk_capacity(chunk_capacity), current_chunk(current_chunk), current_item(current_item) {
if (pure && current_item == 0) {
while (!chunks.lock()[this->current_chunk][this->current_item].get()) {
if (++this->current_item == chunk_capacity) {
if (++this->current_chunk == chunks_count) {
--this->current_chunk;
break;
}
this->current_item = 0;
}
};
valid_chunk_min = this->current_chunk;
valid_item_min = this->current_item;
} else {
current_chunk = 0;
current_item = 0;
while (!chunks.lock()[current_chunk][current_item].get()) {
if (++current_item == chunk_capacity) {
if (++current_chunk == chunks_count) {
--current_chunk;
break;
}
current_item = 0;
}
};
valid_chunk_min = current_chunk;
valid_item_min = current_item;
}
}
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = std::pair<Tk, Ti>;
using difference_type = std::ptrdiff_t;
using pointer = std::pair<Tk, Ti> *;
using reference = std::pair<Tk, Ti> &;
bool operator!=(iterator const &other) const {
return !(current_item == other.current_item && current_chunk == other.current_chunk);
}
bool operator==(iterator const &other) const {
return current_item == other.current_item && current_chunk == other.current_chunk;
}
std::pair<Tk, Ti> &operator*() const {
if (current_item == chunk_capacity) throw std::logic_error("Can not dereference end iterator!");
return *(chunks.lock()[current_chunk][current_item]);
}
std::pair<Tk, Ti> *operator->() const {
if (current_item == chunk_capacity) throw std::logic_error("Can not dereference end iterator!");
return chunks.lock()[current_chunk][current_item].get();
}
iterator &operator++() {
if (current_item == chunk_capacity) throw std::out_of_range("Iterator out of range!");
do {
if (++current_item == chunk_capacity) {
if (++current_chunk == chunks_count) {
--current_chunk;
break;
}
current_item = 0;
}
} while (!chunks.lock()[current_chunk][current_item].get());
return *this;
}
iterator operator++(int) {
if (current_item == chunk_capacity) throw std::out_of_range("Iterator out of range!");
iterator temp(*this);
do {
if (++current_item == chunk_capacity) {
if (++current_chunk == chunks_count) {
--current_chunk;
break;
}
current_item = 0;
}
} while (!chunks.lock()[current_chunk][current_item].get());
return temp;
}
iterator &operator--() {
if (current_item == valid_item_min && current_chunk == valid_chunk_min) throw std::out_of_range("Iterator out of range!");
do {
if (current_item-- == 0) {
--current_chunk;
current_item = chunk_capacity - 1;
}
} while (!chunks.lock()[current_chunk][current_item].get());
return *this;
}
iterator operator--(int) {
if (current_item == valid_item_min && current_chunk == valid_chunk_min) throw std::out_of_range("Iterator out of range!");
iterator temp(*this);
do {
if (current_item-- == 0) {
--current_chunk;
current_item = chunk_capacity - 1;
}
} while (!chunks.lock()[current_chunk][current_item].get());
return temp;
}
};
iterator begin() const {
return iterator(chunks, chunks_count, chunk_capacity, 0, 0, true);
}
iterator end() const {
return iterator(chunks, chunks_count, chunk_capacity, chunks_count - 1, chunk_capacity, false);
}
bool operator==(const HashList &arg) const {
auto my_count = CalcCount(), other_count = arg.CalcCount();
if (my_count != other_count)
return false;
if (my_count) {
auto my = begin(), mye = end(), other = arg.begin();
do {
if (*my != *other)
return false;
++my; ++other;
} while (my != mye);
}
return true;
}
bool operator>(const HashList &arg) const {
auto my_count = CalcCount(), other_count = arg.CalcCount();
if (my_count != 0) {
if (other_count != 0) {
auto my = begin(), mye = end(), other = arg.begin(), othere = arg.end();
while (true) {
if (*my > *other)
return true;
else if (*my < *other)
return false;
if (++my == mye) return false;
if (++other == othere) return true;
}
} else
return true;
}
return false;
}
bool operator!=(const HashList &arg) const {
return !(*this == arg);
}
bool operator>=(const HashList &arg) const {
return *this > arg || *this == arg;
}
bool operator<=(const HashList &arg) const {
return !(*this > arg);
}
bool operator<(const HashList &arg) const {
return *this <= arg && *this != arg;
}
};
template<typename Ti, typename Ts>
std::ostream &operator<<(std::ostream &out, const HashList<Ti, Ts> &buffer) {
for (auto item : buffer) {
out << item.first << " : " << item.second << std::endl;
}
return out;
}
#endif