-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazyflatset.hpp
481 lines (402 loc) · 15 KB
/
lazyflatset.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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Ripcord Software Ltd
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
**/
#ifndef RS_LAZY_FLAT_SET
#define RS_LAZY_FLAT_SET
#include <vector>
#include <algorithm>
#include <type_traits>
#include <functional>
#include <memory>
namespace rs {
template <class Value, class Less>
struct LazyFlatSetQuickSort;
template <class Value, class Less = std::less<Value>, class Equal = std::equal_to<Value>, class Sort = LazyFlatSetQuickSort<Value, Less>, class Alloc = std::allocator<Value>, bool IsPointer = false>
class LazyFlatSet {
public:
template <class T> struct is_shared_ptr : std::false_type {};
template <class T> struct is_shared_ptr<std::shared_ptr<T> > : std::true_type {};
template <class T> struct is_pointer : std::conditional<IsPointer || std::is_pointer<T>::value || is_shared_ptr<T>::value, std::true_type, std::false_type>::type {};
using base_collection = typename std::vector<Value, Alloc>;
using size_type = typename base_collection::size_type;
using iterator = typename base_collection::iterator;
using const_iterator = typename base_collection::const_iterator;
using value_type = Value;
using value_type_ptr = typename std::conditional<is_pointer<value_type>::value, value_type, value_type*>::type;
using reference = typename std::conditional<std::is_fundamental<value_type>::value || std::is_pointer<value_type>::value, value_type, value_type&>::type;
using const_reference = typename std::conditional<std::is_fundamental<value_type>::value || std::is_pointer<value_type>::value, value_type, const value_type&>::type;
using less_type = Less;
using equal_type = Equal;
using sort_type = Sort;
using alloc_type = Alloc;
using compare_type = typename std::function<int(const_reference)>;
using erase_type = typename std::function<void(reference)>;
enum class insert_hint { no_hint = 0, new_item = 1 };
LazyFlatSet(unsigned maxUnsortedEntries = 16, unsigned maxNurseryEntries = 1024) :
maxUnsortedEntries_(maxUnsortedEntries), maxNurseryEntries_(maxNurseryEntries) {
unsorted_.reserve(maxUnsortedEntries);
}
void insert(const value_type& k, insert_hint hint = insert_hint::no_hint) {
if (hint == insert_hint::no_hint) {
auto iter = lower_bound_equals(coll_, k);
if (iter != coll_.end()) {
*iter = k;
} else {
iter = lower_bound_equals(nursery_, k);
if (iter != nursery_.end()) {
*iter = k;
} else {
iter = search_unsorted(unsorted_, k);
if (iter != unsorted_.end()) {
*iter = k;
} else {
if (unsorted_.size() == maxUnsortedEntries_) {
flushUnsorted();
}
unsorted_.push_back(k);
}
}
}
} else {
if (unsorted_.size() == maxUnsortedEntries_) {
flushUnsorted();
}
unsorted_.push_back(k);
}
}
template <typename... Args>
void emplace(Args&&... args) {
if (unsorted_.size() == maxUnsortedEntries_) {
flushUnsorted();
}
unsorted_.emplace_back(std::forward<Args>(args)...);
auto iter = lower_bound_equals(coll_, unsorted_.back());
if (iter != coll_.end()) {
*iter = std::move(unsorted_.back());
unsorted_.pop_back();
} else {
iter = lower_bound_equals(nursery_, unsorted_.back());
if (iter != nursery_.end()) {
*iter = std::move(unsorted_.back());
unsorted_.pop_back();
} else if (unsorted_.size() > 1) {
auto newItemIter = unsorted_.end() - 1;
iter = search_unsorted(unsorted_, unsorted_.back());
if (iter != newItemIter) {
*iter = std::move(unsorted_.back());
unsorted_.pop_back();
}
}
}
}
bool empty() const {
return coll_.empty() && nursery_.empty() && unsorted_.empty();
}
void clear() {
coll_.clear();
nursery_.clear();
unsorted_.clear();
}
void clear_fn(erase_type erase) {
for (auto i : coll_) {
erase(i);
}
coll_.clear();
for (auto i : nursery_) {
erase(i);
}
nursery_.clear();
for (auto i : unsorted_) {
erase(i);
}
unsorted_.clear();
}
void reserve(size_type n) {
coll_.reserve(n);
}
size_type size() const {
return coll_.size() + nursery_.size() + unsorted_.size();
}
void shrink_to_fit() {
flush();
nursery_.shrink_to_fit();
coll_.shrink_to_fit();
}
size_type count(const value_type& k) const {
size_type found = 0;
auto iter = lower_bound_equals(coll_, k);
if (iter != coll_.end()) {
found = 1;
} else {
iter = lower_bound_equals(nursery_, k);
if (iter != nursery_.end()) {
found = 1;
} else {
iter = search_unsorted(unsorted_, k);
if (iter != unsorted_.end()) {
found = 1;
}
}
}
return found;
}
size_type count_fn(compare_type compare) const {
size_type found = 0;
auto index = search(coll_, compare);
if (index != search_end) {
found = 1;
} else {
index = search(nursery_, compare);
if (index != search_end) {
found = 1;
} else {
index = search_unsorted(unsorted_, compare);
if (index != search_end) {
found = 1;
}
}
}
return found;
}
bool find(const value_type& k, value_type& v) const {
auto found = false;
auto iter = lower_bound_equals(coll_, k);
if (iter != coll_.end()) {
v = *iter;
found = true;
} else {
iter = lower_bound_equals(nursery_, k);
if (iter != nursery_.end()) {
v = *iter;
found = true;
} else {
iter = search_unsorted(unsorted_, k);
if (iter != unsorted_.end()) {
v = *iter;
found = true;
}
}
}
return found;
}
value_type_ptr find_fn(compare_type compare) const {
value_type_ptr value = nullptr;
auto index = search(coll_, compare);
if (index != search_end) {
value = getValue(coll_, index, is_pointer<value_type>());
} else {
index = search(nursery_, compare);
if (index != search_end) {
value = getValue(nursery_, index, is_pointer<value_type>());
} else {
index = search_unsorted(unsorted_, compare);
if (index != search_end) {
value = getValue(unsorted_, index, is_pointer<value_type>());
}
}
}
return value;
}
const_reference operator[](size_type n) const {
flush();
return coll_[n];
}
const_iterator cbegin() const {
flush();
return coll_.cbegin();
}
const_iterator cend() const {
flush();
return coll_.cend();
}
const value_type* data() const {
flush();
return coll_.data();
}
size_type erase(const value_type& k) {
size_type count = 0;
auto iter = lower_bound_equals(coll_, k);
if (iter != coll_.end()) {
coll_.erase(iter);
count = 1;
} else {
iter = lower_bound_equals(nursery_, k);
if (iter != nursery_.end()) {
nursery_.erase(iter);
count = 1;
} else {
iter = search_unsorted(unsorted_, k);
if (iter != unsorted_.end()) {
unsorted_.erase(iter);
count = 1;
}
}
}
return count;
}
size_type erase_fn(compare_type compare, erase_type erase = nullptr) {
size_type count = 0;
auto index = search(coll_, compare);
if (index != search_end) {
if (erase != nullptr) {
erase(coll_[index]);
}
coll_.erase(coll_.begin() + index);
count = 1;
} else {
index = search(nursery_, compare);
if (index != search_end) {
if (erase != nullptr) {
erase(nursery_[index]);
}
nursery_.erase(nursery_.begin() + index);
count = 1;
} else {
index = search_unsorted(unsorted_, compare);
if (index != search_end) {
if (erase != nullptr) {
erase(unsorted_[index]);
}
unsorted_.erase(unsorted_.begin() + index);
count = 1;
}
}
}
return count;
}
void copy(std::vector<Value>& coll, bool sort = true) const {
if (sort) {
flush();
}
const auto newSize = coll.size() + coll_.size() + nursery_.size() + unsorted_.size();
coll.reserve(newSize);
coll.insert(coll.end(), coll_.cbegin(), coll_.cend());
coll.insert(coll.end(), nursery_.cbegin(), nursery_.cend());
coll.insert(coll.end(), unsorted_.cbegin(), unsorted_.cend());
}
private:
const size_type search_end = -1;
void sort(base_collection& coll) const {
Sort{}(coll.begin(), coll.end());
}
iterator lower_bound(base_collection& coll, const value_type& k) const {
return std::lower_bound(coll.begin(), coll.end(), k, Less{});
}
iterator lower_bound_equals(base_collection& coll, const value_type& k) const {
auto iter = lower_bound(coll, k);
return iter != coll.end() && Equal{}(*iter, k) ? iter : coll.end();
}
iterator upper_bound(base_collection& coll, const value_type& k) const {
return std::upper_bound(coll.begin(), coll.end(), k, Less{});
}
iterator upper_bound_equals(base_collection& coll, const value_type& k) const {
auto iter = upper_bound(coll, k);
return iter != coll.end() && Equal{}(*iter, k) ? iter : coll.end();
}
size_type search(base_collection& coll, compare_type compare) const {
const auto size = coll.size();
if (size > 0) {
size_type min = 0;
size_type max = size - 1;
const auto data = coll.data();
while (max != search_end && max >= min) {
auto mid = ((max - min) / 2) + min;
const auto& item = data[mid];
auto diff = compare(item);
if (diff == 0) {
return mid;
} else if (diff < 0) {
max = mid - 1;
} else {
min = mid + 1;
}
}
}
return search_end;
}
size_type search_unsorted(base_collection& coll, compare_type compare) const {
const auto data = coll.data();
for (size_type i = 0, size = coll.size(); i < size; ++i) {
if (compare(data[i]) == 0) {
return i;
}
}
return search_end;
}
iterator search_unsorted(base_collection& coll, const value_type& k) const {
return std::search_n(coll.begin(), coll.end(), 1, k, Equal{});
}
void flush() const {
flushUnsorted();
flushNursery();
}
void flushUnsorted() const {
const auto unsortedSize = unsorted_.size();
if (unsortedSize > 0) {
if ((nursery_.size() + unsortedSize) > maxNurseryEntries_) {
flushNursery();
}
sort(unsorted_);
merge(unsorted_, nursery_);
unsorted_.clear();
}
}
void flushNursery() const {
if (nursery_.size() > 0) {
merge(nursery_, coll_);
nursery_.clear();
}
}
void merge(base_collection& source, base_collection& target) const {
if (source.size() > 0) {
Less less;
if (target.size() == 0 || less(target.back(), source.front())) {
target.insert(target.end(), source.cbegin(), source.cend());
} else if (less(source.back(), target.front())) {
target.insert(target.begin(), source.cbegin(), source.cend());
} else {
target.insert(target.end(), source.cbegin(), source.cend());
std::inplace_merge(target.begin(), target.end() - source.size(), target.end(), less);
}
}
}
value_type getValue(base_collection& coll, int index, std::true_type) const {
return coll[index];
}
value_type_ptr getValue(base_collection& coll, int index, std::false_type) const {
return &coll[index];
}
const unsigned maxUnsortedEntries_;
const unsigned maxNurseryEntries_;
mutable base_collection coll_;
mutable base_collection nursery_;
mutable base_collection unsorted_;
};
template <class Value, class Less>
struct LazyFlatSetQuickSort {
void operator()(typename LazyFlatSet<Value>::iterator first, typename LazyFlatSet<Value>::iterator last) {
std::sort(first, last, Less{});
}
};
}
#endif