-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparse_sets.h
643 lines (546 loc) · 16.1 KB
/
sparse_sets.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
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
// <author>Mikhail Semenov</author>
// <date>2015-01-03</date>
// <summary>Contains implementation of Sparse Sets of Integers</summary>
#include <vector>
#include <string>
#include <iterator>
#include <limits>
#include <algorithm>
namespace bits
{
template <class T>
static int lsb(T x)
{
static const int bval[] = { 0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 }; // the first index is not used!!!
if (x == 0)
return -1;
int r = 0;
switch (sizeof(x))
{
case 8: if ((x & 0xFFFFFFFF) == 0) { r += 32; x >>= 32; };
case 4: if ((x & 0x0000FFFF) == 0) { r += 16; x >>= 16; };
case 2: if ((x & 0x000000FF) == 0) { r += 8; x >>= 8; };
case 1: if ((x & 0x0000000F) == 0) { r += 4; x >>= 4; };
}
return r + bval[x & 0xF];
}
template<class T>
static unsigned count_bits(T x)
{
static const unsigned bcount[] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
unsigned count = 0;
while (x != 0)
{
count += bcount[x & 0xF];
x >>= 4;
}
return count;
}
} // bits
///
/// Fast operations for a collection of integer values in the range [0; size-1]
/// Whenever an iterator is required an array of values is generated.
/// It outperform boost::dynamic_bitset and bounded_set (see below) in repeated iterations over the same set of values.
/// In comparison, boost::dynamic_bitset and bounded_set use less memory and outperform the sparse set if repeated iterations are
/// not required.
///
class sparse_set
{
static constexpr std::size_t EmptyIndex = static_cast<std::size_t>(-1);
typedef std::size_t base_type;
static constexpr unsigned unsigned_bits = sizeof(base_type) * 8;
static constexpr unsigned unsigned_bits_log2 = (unsigned_bits == 64 ? 6 : unsigned_bits == 32 ? 5 : unsigned_bits == 16 ? 4 : 3);
static constexpr unsigned unsigned_bits_log2_mask = 0xFFFFFFFF >> (32 - unsigned_bits_log2);
static constexpr std::size_t one_bit = 1;
unsigned m_size;
std::vector<base_type> m_bit_array;
mutable std::vector<std::size_t> m_sequence;
mutable bool m_iterator_present;
void create_iteration_sequence() const
{
//if (!m_sequence.empty())
// return;
if (m_sequence.capacity() != m_size)
{
m_sequence.reserve(m_size);
}
std::size_t k = 0;
for (auto x : m_bit_array)
{
if (x)
{
unsigned bit = k;
do
{
unsigned p = bits::lsb(x);
bit += p;
x >>= p;
m_sequence.push_back(bit);
if (x == 1)
break;
x >>= 1;
bit++;
} while (true);
}
k += unsigned_bits;
}
}
public:
typedef std::size_t value_type;
typedef std::size_t key_type;
typedef std::size_t size_type;
typedef std::vector<std::size_t>::const_iterator iterator;
typedef iterator const_iterator;
typedef std::vector<std::size_t>::const_reverse_iterator reverse_iterator;
typedef reverse_iterator const_reverse_iterator;
sparse_set(std::size_t size) :m_size(size),
m_bit_array((m_size + unsigned_bits - 1) / unsigned_bits), m_sequence(), m_iterator_present(false)
{
}
sparse_set() :m_size(0),
m_bit_array(0), m_sequence(), m_iterator_present(false)
{
}
void swap(sparse_set& s)
{
m_bit_array.swap(s.m_bit_array);
m_sequence.swap(s.m_sequence);
std::swap(m_iterator_present, s.m_iterator_present);
std::swap(m_size, s.m_size);
}
void resize(std::size_t size)
{
m_size = size;
m_bit_array.resize((m_size + unsigned_bits - 1) / unsigned_bits);
if (m_iterator_present)
{
m_iterator_present = false;
m_sequence.clear();
}
}
bool insert(value_type i)
{
if (m_iterator_present)
{
m_iterator_present = false;
m_sequence.clear();
}
base_type& v = m_bit_array[i >> unsigned_bits_log2];
base_type x = v;
v |= (one_bit << (i & unsigned_bits_log2_mask));
return x != v;
}
void erase(value_type i)
{
m_bit_array[i >> unsigned_bits_log2] &= ~(one_bit << (i & unsigned_bits_log2_mask));
if (m_iterator_present)
{
m_iterator_present = false;
m_sequence.clear();
}
}
bool test(std::size_t i) const
{
return ((m_bit_array[i >> unsigned_bits_log2] >> (i & unsigned_bits_log2_mask)) & 1) != 0;
}
bool empty() const
{
for (auto x : m_bit_array)
{
if (x != 0)
return false;
}
return true;
}
void clear()
{
if (m_iterator_present)
{
m_iterator_present = false;
m_sequence.clear();
}
std::fill(m_bit_array.begin(), m_bit_array.end(), 0);
}
std::size_t size() const
{
return m_size;
}
std::size_t count() const
{
if (m_iterator_present)
return m_sequence.size();
std::size_t count = 0;
for (auto x : m_bit_array)
{
count += bits::count_bits(x);
}
return count;
}
iterator begin() const
{
if (!m_iterator_present)
{
create_iteration_sequence();
m_iterator_present = true;
}
return m_sequence.begin();
}
iterator end() const
{
if (!m_iterator_present)
{
create_iteration_sequence();
m_iterator_present = true;
}
return m_sequence.end();
}
reverse_iterator rbegin() const
{
if (!m_iterator_present)
{
create_iteration_sequence();
m_iterator_present = true;
}
return m_sequence.rbegin();
}
reverse_iterator rend() const
{
if (!m_iterator_present)
{
create_iteration_sequence();
m_iterator_present = true;
}
return m_sequence.rend();
}
iterator lower_bound(value_type i) const
{
return std::lower_bound(m_sequence.begin(), m_sequence.end(), i);
}
iterator upper_bound(value_type i) const
{
return std::upper_bound(m_sequence.begin(), m_sequence.end(), i);
}
};
///
/// The unordered sparse set is slower than sparse set, except for iteration over the whole set of values.
/// It uses more memory than sparse set.
///
class unordered_sparse_set
{
public:
typedef std::vector<std::size_t*> direct_access_sequence;
typedef std::vector<std::size_t> iteration_sequence;
direct_access_sequence m_sparse;
iteration_sequence m_dense;
public:
typedef std::size_t value_type;
typedef std::size_t size_type;
typedef std::size_t key_type;
typedef iteration_sequence::const_iterator iterator;
typedef iterator const_iterator;
typedef iteration_sequence::const_reverse_iterator reverse_iterator;
typedef reverse_iterator const_reverse_iterator;
unordered_sparse_set(std::size_t size) :m_sparse(size), m_dense()
{
m_dense.reserve(size);
}
unordered_sparse_set() :m_sparse(), m_dense() {}
void resize(std::size_t size)
{
m_sparse.resize(size);
m_dense.reserve(size);
m_dense.clear();
}
void swap(unordered_sparse_set& s)
{
m_sparse.swap(s.m_sparse);
m_dense.swap(s.m_dense);
}
bool test(std::size_t i) const
{
return m_sparse[i] != nullptr;
}
bool insert(std::size_t i)
{
if (m_sparse[i] != nullptr)
return false;
m_dense.push_back(i);
m_sparse[i] = &m_dense.back();
return true;
}
void erase(std::size_t i)
{
if (m_sparse[i] == nullptr)
return;
std::size_t* v = m_sparse[i];
std::size_t p_index = m_dense.back();
if (*v != p_index)
{
*v = p_index;
m_sparse[p_index] = v;
}
m_sparse[i] = nullptr;
m_dense.erase(m_dense.end() - 1);
}
void clear()
{
std::fill(m_sparse.begin(), m_sparse.end(), nullptr);
m_dense.clear();
}
std::size_t size() const
{
return m_sparse.size();
}
std::size_t count() const
{
return m_dense.size();
}
bool empty() const
{
return m_dense.empty();
}
iterator begin() const
{
return m_dense.begin();
}
iterator end() const
{
return m_dense.end();
}
reverse_iterator rbegin() const
{
return m_dense.rbegin();
}
reverse_iterator rend() const
{
return m_dense.rend();
}
iterator lower_bound(value_type i) const
{
return std::lower_bound(m_dense.begin(), m_dense.end(), i);
}
iterator upper_bound(value_type i) const
{
return std::upper_bound(m_dense.begin(), m_dense.end(), i);
}
};
///
/// The bounded set is very close to boost::dynamic_bitset
/// It performs practically with the same speed
/// In terms of functinality, it has different member functions (which are similar to those in std::set) and provides an iterator
/// The speed is similar to that of the sparse set, but the repeated iterations over the same set of values are slower.
/// It also uses less memory than sparse set: there is no memory allocation for a vector of values, which is need for the sparse set iterator
///
class bounded_set
{
private:
static constexpr std::size_t EmptyIndex = static_cast<std::size_t>(-1);
typedef std::size_t base_type;
static constexpr unsigned unsigned_bits = sizeof(base_type) * 8;
static constexpr unsigned unsigned_bits_log2 = (unsigned_bits == 64 ? 6 : unsigned_bits == 32 ? 5 : unsigned_bits == 16 ? 4 : 3);
static constexpr unsigned unsigned_bits_log2_mask = 0xFFFFFFFF >> (32 - unsigned_bits_log2);
static constexpr base_type one_bit = 1;
unsigned m_size;
std::vector<base_type> m_bit_array;
public:
typedef std::size_t value_type;
typedef std::size_t key_type;
typedef std::size_t size_type;
bounded_set(std::size_t size) :m_size(size),
m_bit_array((m_size + unsigned_bits - 1) / unsigned_bits)
{
}
bounded_set() :m_size(0),
m_bit_array(0)
{
}
void swap(bounded_set& s)
{
m_bit_array.swap(s.m_bit_array);
std::swap(m_size, s.m_size);
}
void resize(std::size_t size)
{
m_size = size;
m_bit_array.resize((m_size + unsigned_bits - 1) / unsigned_bits);
}
bool insert(std::size_t i)
{
base_type& v = m_bit_array[i >> unsigned_bits_log2];
base_type x = v;
v |= (one_bit << (i & unsigned_bits_log2_mask));
return x != v;
}
void erase(std::size_t i)
{
m_bit_array[i >> unsigned_bits_log2] &= ~(one_bit << (i & unsigned_bits_log2_mask));
}
bool test(std::size_t i) const
{
return ((m_bit_array[i >> unsigned_bits_log2] >> (i & unsigned_bits_log2_mask)) & 1) != 0;
}
struct iterator
{
friend bounded_set;
typedef std::forward_iterator_tag
iterator_category;
typedef unsigned value_type;
typedef std::ptrdiff_t
difference_type;
private:
bool test() const
{
return ((m_bit_array[m_slot_index] >> m_bit_index) & 1) != 0;
}
bool next()
{
m_current_slot >>= 1;
if (m_current_slot == 0)
{
m_bit_index = 0;
++m_slot_index;
while (m_slot_index < m_bit_array_size && (m_current_slot = m_bit_array[m_slot_index]) == 0)
{
++m_slot_index;
}
if (m_current_slot == 0)
{
m_slot_index = EmptyIndex;
m_bit_index = 0;
m_current_slot = 0;
return false;
}
}
else
{
++m_bit_index;
if (m_slot_index == m_bit_array_size - 1 && m_bit_index > m_last_bit)
{
m_slot_index = EmptyIndex;
m_bit_index = 0;
m_current_slot = 0;
return false;
}
}
unsigned shift = bits::lsb(m_current_slot);
m_current_slot >>= shift;
m_bit_index += shift;
return true;
}
iterator(unsigned size, const std::vector<base_type>& bit_array, std::size_t pos)
: m_size(size), m_bit_array_size(bit_array.size()), m_bit_array(size ? &bit_array[0] : nullptr),
m_slot_index(pos >> unsigned_bits_log2), m_current_slot(0), m_bit_index(pos & unsigned_bits_log2_mask),
m_last_bit((size - 1) & unsigned_bits_log2_mask)
{
if (pos < m_size)
m_current_slot = m_bit_array[m_slot_index] >> m_bit_index;
if (!test())
{
next();
}
}
public:
iterator(unsigned size, const std::vector<base_type>& bit_array)
: m_size(size), m_bit_array_size(bit_array.size()), m_bit_array(size ? &bit_array[0] : nullptr), m_slot_index(0), m_current_slot(0), m_bit_index(0),
m_last_bit((size - 1) & unsigned_bits_log2_mask)
{
if (m_size != 0)
m_current_slot = m_bit_array[0];
if (m_size != 0 && !test())
{
next();
}
}
iterator() // empty
: m_size(0), m_bit_array(nullptr), m_slot_index(EmptyIndex), m_current_slot(0), m_bit_index(0), m_last_bit(0)
{}
unsigned operator*() const
{
return (m_slot_index << unsigned_bits_log2) + m_bit_index;
}
iterator& operator++()
{
next();
return *this;
}
iterator operator++(int)
{
iterator tmp(*this);
++*this;
return tmp;
}
bool operator==(const iterator& y)
{
return (m_slot_index == y.m_slot_index) && (m_bit_index == y.m_bit_index);
}
bool operator!=(const iterator& y)
{
return (m_slot_index != y.m_slot_index) || (m_bit_index != y.m_bit_index);
}
private:
unsigned m_size;
std::size_t m_bit_array_size;
const base_type* m_bit_array;
std::size_t m_slot_index;
base_type m_current_slot;
unsigned m_bit_index;
unsigned m_last_bit;
};
typedef iterator const_iterator;
iterator begin() const
{
return iterator(m_size, m_bit_array);
}
iterator end() const
{
return iterator();
}
iterator find(unsigned i) const
{
if (test(i))
{
return iterator(m_size, m_bit_array, i);
}
return iterator();
}
iterator lower_bound(std::size_t i) const
{
return iterator(m_size, m_bit_array, i);
}
iterator upper_bound(std::size_t i) const
{
if (i + 1 >= m_size)
return iterator();
return iterator(m_size, m_bit_array, i+1);
}
void erase(const iterator& it)
{
erase(*it);
}
bool empty() const
{
for (auto x : m_bit_array)
{
if (x != 0)
return false;
}
return true;
}
void clear()
{
std::fill(m_bit_array.begin(), m_bit_array.end(), 0);
}
std::size_t size() const
{
return m_size;
}
std::size_t count() const
{
std::size_t count = 0;
for (auto x : m_bit_array)
{
count += bits::count_bits(x);
}
return count;
}
}; // bounded set