-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdynamic_bitset.h
1785 lines (1429 loc) · 51.8 KB
/
dynamic_bitset.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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -----------------------------------------------------------
//
// Copyright (c) 2001-2002 Chuck Allison and Jeremy Siek
// Copyright (c) 2003-2006, 2008 Gennaro Prota
// Copyright (c) 2014 Ahmed Charles
//
// Copyright (c) 2014 Glen Joseph Fernandes
// glenfe at live dot com
// Copyright (c) 2014 Riccardo Marcangelo
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// -----------------------------------------------------------
#pragma once
#include <assert.h>
#include <string>
#include <stdexcept>
#include <algorithm>
#include <vector>
#include <climits> // for CHAR_BIT
#include <locale>
#include <istream>
#include <ostream>
#include "base/dynamic_bitset_detail.h"
namespace base {
template <typename Block = unsigned long,
typename Allocator = std::allocator<Block> >
class dynamic_bitset;
template <typename Block, typename Allocator>
class dynamic_bitset
{
// Portability note: member function templates are defined inside
// this class definition to avoid problems with VC++. Similarly,
// with the member functions of nested classes.
//
// [October 2008: the note above is mostly historical; new versions
// of VC++ are likely able to digest a more drinking form of the
// code; but changing it now is probably not worth the risks...]
static_assert((bool)detail::dynamic_bitset_impl::allowed_block_type<Block>::value);
typedef std::vector<Block, Allocator> buffer_type;
public:
typedef Block block_type;
typedef Allocator allocator_type;
typedef std::size_t size_type;
typedef typename buffer_type::size_type block_width_type;
static const block_width_type bits_per_block = (std::numeric_limits<Block>::digits);
static const size_type npos = static_cast<size_type>(-1);
public:
// A proxy class to simulate lvalues of bit type.
//
class reference
{
friend class dynamic_bitset<Block, Allocator>;
// the one and only non-copy ctor
reference(block_type & b, block_width_type pos)
:m_block(b),
m_mask( (assert(pos < bits_per_block),
block_type(1) << pos )
)
{ }
void operator&(); // left undefined
public:
// copy constructor: compiler generated
operator bool() const { return (m_block & m_mask) != 0; }
bool operator~() const { return (m_block & m_mask) == 0; }
reference& flip() { do_flip(); return *this; }
reference& operator=(bool x) { do_assign(x); return *this; } // for b[i] = x
reference& operator=(const reference& rhs) { do_assign(rhs); return *this; } // for b[i] = b[j]
reference& operator|=(bool x) { if (x) do_set(); return *this; }
reference& operator&=(bool x) { if (!x) do_reset(); return *this; }
reference& operator^=(bool x) { if (x) do_flip(); return *this; }
reference& operator-=(bool x) { if (x) do_reset(); return *this; }
private:
block_type & m_block;
const block_type m_mask;
void do_set() { m_block |= m_mask; }
void do_reset() { m_block &= ~m_mask; }
void do_flip() { m_block ^= m_mask; }
void do_assign(bool x) { x? do_set() : do_reset(); }
};
typedef bool const_reference;
// constructors, etc.
explicit
dynamic_bitset(const Allocator& alloc = Allocator());
explicit
dynamic_bitset(size_type num_bits, unsigned long value = 0,
const Allocator& alloc = Allocator());
explicit
dynamic_bitset(size_type num_bits, uint64_t value,
const Allocator& alloc = Allocator());
// WARNING: you should avoid using this constructor.
//
// A conversion from string is, in most cases, formatting,
// and should be performed by using operator>>.
//
// NOTE:
// Leave the parentheses around std::basic_string<CharT, Traits, Alloc>::npos.
// g++ 3.2 requires them and probably the standard will - see core issue 325
// NOTE 2:
// split into two constructors because of bugs in MSVC 6.0sp5 with STLport
template <typename CharT, typename Traits, typename Alloc>
dynamic_bitset(const std::basic_string<CharT, Traits, Alloc>& s,
typename std::basic_string<CharT, Traits, Alloc>::size_type pos,
typename std::basic_string<CharT, Traits, Alloc>::size_type n,
size_type num_bits = npos,
const Allocator& alloc = Allocator())
:m_bits(alloc),
m_num_bits(0)
{
init_from_string(s, pos, n, num_bits);
}
template <typename CharT, typename Traits, typename Alloc>
explicit
dynamic_bitset(const std::basic_string<CharT, Traits, Alloc>& s,
typename std::basic_string<CharT, Traits, Alloc>::size_type pos = 0)
:m_bits(Allocator()),
m_num_bits(0)
{
init_from_string(s, pos, (std::basic_string<CharT, Traits, Alloc>::npos),
npos);
}
// The first bit in *first is the least significant bit, and the
// last bit in the block just before *last is the most significant bit.
template <typename BlockInputIterator>
dynamic_bitset(BlockInputIterator first, BlockInputIterator last,
const Allocator& alloc = Allocator())
:m_bits(alloc),
m_num_bits(0)
{
using base::detail::dynamic_bitset_impl::value_to_type;
using base::detail::dynamic_bitset_impl::is_numeric;
const value_to_type<
is_numeric<BlockInputIterator>::value> selector;
dispatch_init(first, last, selector);
}
template <typename T>
void dispatch_init(T num_bits, unsigned long value,
detail::dynamic_bitset_impl::value_to_type<true>)
{
init_from_unsigned_long(static_cast<size_type>(num_bits), value);
}
template <typename T>
void dispatch_init(T num_bits, uint64_t value,
detail::dynamic_bitset_impl::value_to_type<true>)
{
init_from_uint64(static_cast<size_type>(num_bits), value);
}
template <typename T>
void dispatch_init(T first, T last,
detail::dynamic_bitset_impl::value_to_type<false>)
{
init_from_block_range(first, last);
}
template <typename BlockIter>
void init_from_block_range(BlockIter first, BlockIter last)
{
assert(m_bits.size() == 0);
m_bits.insert(m_bits.end(), first, last);
m_num_bits = m_bits.size() * bits_per_block;
}
// copy constructor
dynamic_bitset(const dynamic_bitset& b);
~dynamic_bitset();
void swap(dynamic_bitset& b);
dynamic_bitset& operator=(const dynamic_bitset& b);
dynamic_bitset(dynamic_bitset&& src);
dynamic_bitset& operator=(dynamic_bitset&& src);
allocator_type get_allocator() const;
// size changing operations
void resize(size_type num_bits, bool value = false);
void clear();
void push_back(bool bit);
void pop_back();
void append(Block block);
template <typename BlockInputIterator>
void m_append(BlockInputIterator first, BlockInputIterator last, std::input_iterator_tag)
{
std::vector<Block, Allocator> v(first, last);
m_append(v.begin(), v.end(), std::random_access_iterator_tag());
}
template <typename BlockInputIterator>
void m_append(BlockInputIterator first, BlockInputIterator last, std::forward_iterator_tag)
{
assert(first != last);
block_width_type r = count_extra_bits();
std::size_t d = base::detail::distance(first, last);
m_bits.reserve(num_blocks() + d);
if (r == 0) {
for( ; first != last; ++first)
m_bits.push_back(*first); // could use vector<>::insert()
}
else {
m_highest_block() |= (*first << r);
do {
Block b = *first >> (bits_per_block - r);
++first;
m_bits.push_back(b | (first==last? 0 : *first << r));
} while (first != last);
}
m_num_bits += bits_per_block * d;
}
template <typename BlockInputIterator>
void append(BlockInputIterator first, BlockInputIterator last) // strong guarantee
{
if (first != last) {
typename detail::iterator_traits<BlockInputIterator>::iterator_category cat;
m_append(first, last, cat);
}
}
// bitset operations
dynamic_bitset& operator&=(const dynamic_bitset& b);
dynamic_bitset& operator|=(const dynamic_bitset& b);
dynamic_bitset& operator^=(const dynamic_bitset& b);
dynamic_bitset& operator-=(const dynamic_bitset& b);
dynamic_bitset& operator<<=(size_type n);
dynamic_bitset& operator>>=(size_type n);
dynamic_bitset operator<<(size_type n) const;
dynamic_bitset operator>>(size_type n) const;
// basic bit operations
dynamic_bitset& set(size_type n, bool val = true);
dynamic_bitset& set();
dynamic_bitset& reset(size_type n);
dynamic_bitset& reset();
dynamic_bitset& flip(size_type n);
dynamic_bitset& flip();
bool test(size_type n) const;
bool test_set(size_type n, bool val = true);
bool all() const;
bool any() const;
bool none() const;
dynamic_bitset operator~() const;
size_type count() const noexcept;
// subscript
reference operator[](size_type pos) {
return reference(m_bits[block_index(pos)], bit_index(pos));
}
bool operator[](size_type pos) const { return test(pos); }
unsigned long to_ulong() const;
size_type size() const noexcept;
size_type num_blocks() const noexcept;
size_type max_size() const noexcept;
bool empty() const noexcept;
size_type capacity() const noexcept;
void reserve(size_type num_bits);
void shrink_to_fit();
bool is_subset_of(const dynamic_bitset& a) const;
bool is_proper_subset_of(const dynamic_bitset& a) const;
bool intersects(const dynamic_bitset & a) const;
// lookup
size_type find_first() const;
size_type find_next(size_type pos) const;
// lexicographical comparison
template <typename B, typename A>
friend bool operator==(const dynamic_bitset<B, A>& a,
const dynamic_bitset<B, A>& b);
template <typename B, typename A>
friend bool operator<(const dynamic_bitset<B, A>& a,
const dynamic_bitset<B, A>& b);
template <typename B, typename A>
friend bool oplessthan(const dynamic_bitset<B, A>& a,
const dynamic_bitset<B, A>& b);
template <typename B, typename A, typename BlockOutputIterator>
friend void to_block_range(const dynamic_bitset<B, A>& b,
BlockOutputIterator result);
template <typename BlockIterator, typename B, typename A>
friend void from_block_range(BlockIterator first, BlockIterator last,
dynamic_bitset<B, A>& result);
template <typename CharT, typename Traits, typename B, typename A>
friend std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is,
dynamic_bitset<B, A>& b);
template <typename B, typename A, typename stringT>
friend void to_string_helper(const dynamic_bitset<B, A> & b, stringT & s, bool dump_all);
public:
// forward declaration for optional zero-copy serialization support
class serialize_impl;
friend class serialize_impl;
private:
static const block_width_type ulong_width = std::numeric_limits<unsigned long>::digits;
void m_zero_unused_bits();
bool m_check_invariants() const;
size_type m_do_find_from(size_type first_block) const;
block_width_type count_extra_bits() const noexcept { return bit_index(size()); }
static size_type block_index(size_type pos) noexcept { return pos / bits_per_block; }
static block_width_type bit_index(size_type pos) noexcept { return static_cast<block_width_type>(pos % bits_per_block); }
static Block bit_mask(size_type pos) noexcept { return Block(1) << bit_index(pos); }
template <typename CharT, typename Traits, typename Alloc>
void init_from_string(const std::basic_string<CharT, Traits, Alloc>& s,
typename std::basic_string<CharT, Traits, Alloc>::size_type pos,
typename std::basic_string<CharT, Traits, Alloc>::size_type n,
size_type num_bits)
{
assert(pos <= s.size());
typedef typename std::basic_string<CharT, Traits, Alloc> StrT;
typedef typename StrT::traits_type Tr;
const typename StrT::size_type rlen = (std::min)(n, s.size() - pos);
const size_type sz = ( num_bits != npos? num_bits : rlen);
m_bits.resize(calc_num_blocks(sz));
m_num_bits = sz;
const std::ctype<CharT> & fac = std::use_facet<std::ctype<CharT>>(std::locale());
const CharT one = fac.widen('1');
const size_type m = num_bits < rlen ? num_bits : rlen;
typename StrT::size_type i = 0;
for( ; i < m; ++i) {
const CharT c = s[(pos + m - 1) - i];
assert( Tr::eq(c, one)
|| Tr::eq(c, fac.widen('0')) );
if (Tr::eq(c, one))
set(i);
}
}
void init_from_unsigned_long(size_type num_bits,
unsigned long value/*,
const Allocator& alloc*/)
{
assert(m_bits.size() == 0);
m_bits.resize(calc_num_blocks(num_bits));
m_num_bits = num_bits;
typedef unsigned long num_type;
typedef base::detail::dynamic_bitset_impl
::shifter<num_type, bits_per_block, ulong_width> shifter;
//if (num_bits == 0)
// return;
// zero out all bits at pos >= num_bits, if any;
// note that: num_bits == 0 implies value == 0
if (num_bits < static_cast<size_type>(ulong_width)) {
const num_type mask = (num_type(1) << num_bits) - 1;
value &= mask;
}
typename buffer_type::iterator it = m_bits.begin();
for( ; value; shifter::left_shift(value), ++it) {
*it = static_cast<block_type>(value);
}
}
void init_from_uint64(size_type num_bits, uint64_t value)
{
assert(m_bits.size() == 0);
m_bits.resize(calc_num_blocks(num_bits));
m_num_bits = num_bits;
typedef uint64_t num_type;
typedef base::detail::dynamic_bitset_impl
::shifter<num_type, bits_per_block, 64> shifter;
// zero out all bits at pos >= num_bits, if any;
// note that: num_bits == 0 implies value == 0
if (num_bits < static_cast<size_type>(64)) {
const num_type mask = (num_type(1) << num_bits) - 1;
value &= mask;
}
typename buffer_type::iterator it = m_bits.begin();
for (; value; shifter::left_shift(value), ++it) {
*it = static_cast<block_type>(value);
}
}
private:
bool m_unchecked_test(size_type pos) const;
static size_type calc_num_blocks(size_type num_bits);
Block& m_highest_block();
const Block& m_highest_block() const;
buffer_type m_bits;
size_type m_num_bits;
class bit_appender;
friend class bit_appender;
class bit_appender {
// helper for stream >>
// Supplies to the lack of an efficient append at the less
// significant end: bits are actually appended "at left" but
// rearranged in the destructor. From the perspective of
// client code everything works *as if* dynamic_bitset<> had
// an append_at_right() function (eventually throwing the same
// exceptions as push_back) except that the function is in fact
// called bit_appender::do_append().
//
dynamic_bitset & bs;
size_type n;
Block mask;
Block * current;
// not implemented
bit_appender(const bit_appender &);
bit_appender & operator=(const bit_appender &);
public:
bit_appender(dynamic_bitset & r) : bs(r), n(0), mask(0), current(0) {}
~bit_appender() {
// reverse the order of blocks, shift
// if needed, and then resize
//
std::reverse(bs.m_bits.begin(), bs.m_bits.end());
const block_width_type offs = bit_index(n);
if (offs)
bs >>= (bits_per_block - offs);
bs.resize(n); // doesn't enlarge, so can't throw
assert(bs.m_check_invariants());
}
inline void do_append(bool value) {
if (mask == 0) {
bs.append(Block(0));
current = &bs.m_highest_block();
mask = Block(1) << (bits_per_block - 1);
}
if(value)
*current |= mask;
mask /= 2;
++n;
}
size_type get_count() const { return n; }
};
};
template <typename Block, typename Allocator>
const typename dynamic_bitset<Block, Allocator>::block_width_type
dynamic_bitset<Block, Allocator>::bits_per_block;
template <typename Block, typename Allocator>
const typename dynamic_bitset<Block, Allocator>::size_type
dynamic_bitset<Block, Allocator>::npos;
template <typename Block, typename Allocator>
const typename dynamic_bitset<Block, Allocator>::block_width_type
dynamic_bitset<Block, Allocator>::ulong_width;
// Global Functions:
// comparison
template <typename Block, typename Allocator>
bool operator!=(const dynamic_bitset<Block, Allocator>& a,
const dynamic_bitset<Block, Allocator>& b);
template <typename Block, typename Allocator>
bool operator<=(const dynamic_bitset<Block, Allocator>& a,
const dynamic_bitset<Block, Allocator>& b);
template <typename Block, typename Allocator>
bool operator>(const dynamic_bitset<Block, Allocator>& a,
const dynamic_bitset<Block, Allocator>& b);
template <typename Block, typename Allocator>
bool operator>=(const dynamic_bitset<Block, Allocator>& a,
const dynamic_bitset<Block, Allocator>& b);
// bitset operations
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>
operator&(const dynamic_bitset<Block, Allocator>& b1,
const dynamic_bitset<Block, Allocator>& b2);
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>
operator|(const dynamic_bitset<Block, Allocator>& b1,
const dynamic_bitset<Block, Allocator>& b2);
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>
operator^(const dynamic_bitset<Block, Allocator>& b1,
const dynamic_bitset<Block, Allocator>& b2);
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>
operator-(const dynamic_bitset<Block, Allocator>& b1,
const dynamic_bitset<Block, Allocator>& b2);
// namespace scope swap
template<typename Block, typename Allocator>
void swap(dynamic_bitset<Block, Allocator>& b1,
dynamic_bitset<Block, Allocator>& b2);
template <typename Block, typename Allocator, typename stringT>
void
to_string(const dynamic_bitset<Block, Allocator>& b, stringT & s);
template <typename Block, typename Allocator, typename BlockOutputIterator>
void
to_block_range(const dynamic_bitset<Block, Allocator>& b,
BlockOutputIterator result);
template <typename BlockIterator, typename B, typename A>
inline void
from_block_range(BlockIterator first, BlockIterator last,
dynamic_bitset<B, A>& result)
{
// PRE: distance(first, last) <= numblocks()
std::copy (first, last, result.m_bits.begin());
}
//=============================================================================
// dynamic_bitset implementation
//-----------------------------------------------------------------------------
// constructors, etc.
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>::dynamic_bitset(const Allocator& alloc)
: m_bits(alloc), m_num_bits(0)
{
}
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>::
dynamic_bitset(size_type num_bits, unsigned long value, const Allocator& alloc)
: m_bits(alloc),
m_num_bits(0)
{
init_from_unsigned_long(num_bits, value);
}
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>::
dynamic_bitset(size_type num_bits, uint64_t value, const Allocator& alloc)
: m_bits(alloc),
m_num_bits(0)
{
init_from_uint64(num_bits, value);
}
// copy constructor
template <typename Block, typename Allocator>
inline dynamic_bitset<Block, Allocator>::
dynamic_bitset(const dynamic_bitset& b)
: m_bits(b.m_bits), m_num_bits(b.m_num_bits)
{
}
template <typename Block, typename Allocator>
inline dynamic_bitset<Block, Allocator>::
~dynamic_bitset()
{
assert(m_check_invariants());
}
template <typename Block, typename Allocator>
inline void dynamic_bitset<Block, Allocator>::
swap(dynamic_bitset<Block, Allocator>& b) // no throw
{
std::swap(m_bits, b.m_bits);
std::swap(m_num_bits, b.m_num_bits);
}
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>& dynamic_bitset<Block, Allocator>::
operator=(const dynamic_bitset<Block, Allocator>& b)
{
m_bits = b.m_bits;
m_num_bits = b.m_num_bits;
return *this;
}
template <typename Block, typename Allocator>
inline dynamic_bitset<Block, Allocator>::
dynamic_bitset(dynamic_bitset<Block, Allocator>&& b)
: m_bits(std::move(b.m_bits)), m_num_bits(std::move(b.m_num_bits))
{
// Required so that assert(m_check_invariants()); works.
assert((b.m_bits = buffer_type()).empty());
b.m_num_bits = 0;
}
template <typename Block, typename Allocator>
inline dynamic_bitset<Block, Allocator>& dynamic_bitset<Block, Allocator>::
operator=(dynamic_bitset<Block, Allocator>&& b)
{
if (std::addressof(b) == this) { return *this; }
m_bits = std::move(b.m_bits);
m_num_bits = std::move(b.m_num_bits);
// Required so that assert(m_check_invariants()); works.
assert((b.m_bits = buffer_type()).empty());
b.m_num_bits = 0;
return *this;
}
template <typename Block, typename Allocator>
inline typename dynamic_bitset<Block, Allocator>::allocator_type
dynamic_bitset<Block, Allocator>::get_allocator() const
{
return m_bits.get_allocator();
}
//-----------------------------------------------------------------------------
// size changing operations
template <typename Block, typename Allocator>
void dynamic_bitset<Block, Allocator>::
resize(size_type num_bits, bool value) // strong guarantee
{
const size_type old_num_blocks = num_blocks();
const size_type required_blocks = calc_num_blocks(num_bits);
const block_type v = value? ~Block(0) : Block(0);
if (required_blocks != old_num_blocks) {
m_bits.resize(required_blocks, v); // s.g. (copy)
}
// At this point:
//
// - if the buffer was shrunk, we have nothing more to do,
// except a call to m_zero_unused_bits()
//
// - if it was enlarged, all the (used) bits in the new blocks have
// the correct value, but we have not yet touched those bits, if
// any, that were 'unused bits' before enlarging: if value == true,
// they must be set.
if (value && (num_bits > m_num_bits)) {
const block_width_type extra_bits = count_extra_bits();
if (extra_bits) {
assert(old_num_blocks >= 1 && old_num_blocks <= m_bits.size());
// Set them.
m_bits[old_num_blocks - 1] |= (v << extra_bits);
}
}
m_num_bits = num_bits;
m_zero_unused_bits();
}
template <typename Block, typename Allocator>
void dynamic_bitset<Block, Allocator>::
clear() // no throw
{
m_bits.clear();
m_num_bits = 0;
}
template <typename Block, typename Allocator>
void dynamic_bitset<Block, Allocator>::
push_back(bool bit)
{
const size_type sz = size();
resize(sz + 1);
set(sz, bit);
}
template <typename Block, typename Allocator>
void dynamic_bitset<Block, Allocator>::
pop_back()
{
const size_type old_num_blocks = num_blocks();
const size_type required_blocks = calc_num_blocks(m_num_bits - 1);
if (required_blocks != old_num_blocks) {
m_bits.pop_back();
}
--m_num_bits;
m_zero_unused_bits();
}
template <typename Block, typename Allocator>
void dynamic_bitset<Block, Allocator>::
append(Block value) // strong guarantee
{
const block_width_type r = count_extra_bits();
if (r == 0) {
// the buffer is empty, or all blocks are filled
m_bits.push_back(value);
}
else {
m_bits.push_back(value >> (bits_per_block - r));
m_bits[m_bits.size() - 2] |= (value << r); // m_bits.size() >= 2
}
m_num_bits += bits_per_block;
assert(m_check_invariants());
}
//-----------------------------------------------------------------------------
// bitset operations
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>&
dynamic_bitset<Block, Allocator>::operator&=(const dynamic_bitset& rhs)
{
assert(size() == rhs.size());
for (size_type i = 0; i < num_blocks(); ++i)
m_bits[i] &= rhs.m_bits[i];
return *this;
}
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>&
dynamic_bitset<Block, Allocator>::operator|=(const dynamic_bitset& rhs)
{
assert(size() == rhs.size());
for (size_type i = 0; i < num_blocks(); ++i)
m_bits[i] |= rhs.m_bits[i];
//m_zero_unused_bits();
return *this;
}
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>&
dynamic_bitset<Block, Allocator>::operator^=(const dynamic_bitset& rhs)
{
assert(size() == rhs.size());
for (size_type i = 0; i < this->num_blocks(); ++i)
m_bits[i] ^= rhs.m_bits[i];
//m_zero_unused_bits();
return *this;
}
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>&
dynamic_bitset<Block, Allocator>::operator-=(const dynamic_bitset& rhs)
{
assert(size() == rhs.size());
for (size_type i = 0; i < num_blocks(); ++i)
m_bits[i] &= ~rhs.m_bits[i];
//m_zero_unused_bits();
return *this;
}
//
// NOTE:
// Note that the 'if (r != 0)' is crucial to avoid undefined
// behavior when the left hand operand of >> isn't promoted to a
// wider type (because rs would be too large).
//
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>&
dynamic_bitset<Block, Allocator>::operator<<=(size_type n)
{
if (n >= m_num_bits)
return reset();
//else
if (n > 0) {
size_type const last = num_blocks() - 1; // num_blocks() is >= 1
size_type const div = n / bits_per_block; // div is <= last
block_width_type const r = bit_index(n);
block_type * const b = &m_bits[0];
if (r != 0) {
block_width_type const rs = bits_per_block - r;
for (size_type i = last-div; i>0; --i) {
b[i+div] = (b[i] << r) | (b[i-1] >> rs);
}
b[div] = b[0] << r;
}
else {
for (size_type i = last-div; i>0; --i) {
b[i+div] = b[i];
}
b[div] = b[0];
}
// zero out div blocks at the less significant end
std::fill_n(m_bits.begin(), div, static_cast<block_type>(0));
// zero out any 1 bit that flowed into the unused part
m_zero_unused_bits(); // thanks to Lester Gong
}
return *this;
}
//
// NOTE:
// see the comments to operator <<=
//
template <typename B, typename A>
dynamic_bitset<B, A> & dynamic_bitset<B, A>::operator>>=(size_type n) {
if (n >= m_num_bits) {
return reset();
}
//else
if (n>0) {
size_type const last = num_blocks() - 1; // num_blocks() is >= 1
size_type const div = n / bits_per_block; // div is <= last
block_width_type const r = bit_index(n);
block_type * const b = &m_bits[0];
if (r != 0) {
block_width_type const ls = bits_per_block - r;
for (size_type i = div; i < last; ++i) {
b[i-div] = (b[i] >> r) | (b[i+1] << ls);
}
// r bits go to zero
b[last-div] = b[last] >> r;
}
else {
for (size_type i = div; i <= last; ++i) {
b[i-div] = b[i];
}
// note the '<=': the last iteration 'absorbs'
// b[last-div] = b[last] >> 0;
}
// div blocks are zero filled at the most significant end
std::fill_n(m_bits.begin() + (num_blocks()-div), div, static_cast<block_type>(0));
}
return *this;
}
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>
dynamic_bitset<Block, Allocator>::operator<<(size_type n) const
{
dynamic_bitset r(*this);
return r <<= n;
}
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>
dynamic_bitset<Block, Allocator>::operator>>(size_type n) const
{
dynamic_bitset r(*this);
return r >>= n;
}
//-----------------------------------------------------------------------------
// basic bit operations
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>&
dynamic_bitset<Block, Allocator>::set(size_type pos, bool val)
{
assert(pos < m_num_bits);
if (val)
m_bits[block_index(pos)] |= bit_mask(pos);
else
reset(pos);
return *this;
}
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>&
dynamic_bitset<Block, Allocator>::set()
{
std::fill(m_bits.begin(), m_bits.end(), ~Block(0));
m_zero_unused_bits();
return *this;
}
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>&
dynamic_bitset<Block, Allocator>::reset(size_type pos)
{
assert(pos < m_num_bits);
m_bits[block_index(pos)] &= ~bit_mask(pos);
return *this;
}
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>&
dynamic_bitset<Block, Allocator>::reset()
{
std::fill(m_bits.begin(), m_bits.end(), Block(0));
return *this;
}
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>&
dynamic_bitset<Block, Allocator>::flip(size_type pos)
{
assert(pos < m_num_bits);
m_bits[block_index(pos)] ^= bit_mask(pos);
return *this;
}
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>&