-
Notifications
You must be signed in to change notification settings - Fork 15
/
dynamic_bitset.hpp
3277 lines (2987 loc) · 104 KB
/
dynamic_bitset.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
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) 2019 Maxime Pinard
//
// Distributed under the MIT license
// See accompanying file LICENSE or copy at
// https://opensource.org/licenses/MIT
//
#ifndef SUL_DYNAMIC_BITSET_HPP
#define SUL_DYNAMIC_BITSET_HPP
/**
* @brief @ref sul::dynamic_bitset version major.
*/
#define SUL_DYNAMIC_BITSET_VERSION_MAJOR 1
/**
* @brief @ref sul::dynamic_bitset version minor.
*/
#define SUL_DYNAMIC_BITSET_VERSION_MINOR 1
/**
* @brief @ref sul::dynamic_bitset version patch.
*/
#define SUL_DYNAMIC_BITSET_VERSION_PATCH 0
/** @file
* @brief @ref sul::dynamic_bitset declaration and implementation.
*
* @details Standalone file, does not depend on other implementation files or dependencies other
* than the standard library, can be taken and used directly.
*
* Can optionally include and use libpopcnt if @a DYNAMIC_BITSET_NO_LIBPOPCNT is not
* defined and @a __has_include(\<libpopcnt.h\>) is @a true.
*
* @remark Include multiple standard library headers and optionally @a libpopcnt.h.
*/
#include <memory>
#include <vector>
#include <algorithm>
#include <string>
#include <string_view>
#include <functional>
#include <type_traits>
#include <limits>
#include <cmath>
#include <cassert>
#ifndef DYNAMIC_BITSET_NO_LIBPOPCNT
# if __has_include(<libpopcnt.h>)
# include <libpopcnt.h>
# define DYNAMIC_BITSET_USE_LIBPOPCNT
# endif
#endif
#if defined(__clang__)
# define DYNAMIC_BITSET_CLANG
# ifdef __has_builtin
# if __has_builtin(__builtin_popcount) && __has_builtin(__builtin_popcountl) \
&& __has_builtin(__builtin_popcountll)
# define DYNAMIC_BITSET_CLANG_builtin_popcount
# endif
# if __has_builtin(__builtin_ctz) && __has_builtin(__builtin_ctzl) \
&& __has_builtin(__builtin_ctzll)
# define DYNAMIC_BITSET_CLANG_builtin_ctz
# endif
# endif
#elif defined(__GNUC__)
# define DYNAMIC_BITSET_GCC
#elif defined(_MSC_VER)
# define DYNAMIC_BITSET_MSVC
# include <intrin.h>
# pragma intrinsic(_BitScanForward)
# if defined(_M_X64) || defined(_M_ARM64)
# define DYNAMIC_BITSET_MSVC_64
# pragma intrinsic(_BitScanForward64)
# else
# endif
#endif
#ifndef DYNAMIC_BITSET_NO_NAMESPACE
/**
* @brief Simple Useful Libraries.
*/
namespace sul
{
#endif
/**
* @brief Dynamic bitset.
*
* @details Data structure used to store a vector of bits and apply binary operations to it. The
* bits are stored in an optimized way in an underling block type. It is highly inspired
* by std\::bitset but with a run-time changeable size.
*
* Preconditions are checked with @a assert but no exception will be thrown if one is
* violated (as with std\::bitset).
*
* @remark It is not a Container as it does not provide iterators because of the reference proxy
* class used to access the bits.
*
* @tparam Block Block type to use for storing the bits, must be an unsigned integral type
* @tparam Allocator Allocator type to use for memory management, must meet the standard
* requirements of @a Allocator
*/
template<typename Block = unsigned long long, typename Allocator = std::allocator<Block>>
class dynamic_bitset
{
static_assert(std::is_unsigned<Block>::value, "Block is not an unsigned integral type");
public:
/**
* @brief Type used to represent the size of a @ref sul::dynamic_bitset.
*/
typedef size_t size_type;
/**
* @brief Same type as @p Block.
*/
typedef Block block_type;
/**
* @brief Same type as @p Allocator.
*/
typedef Allocator allocator_type;
/**
* @brief Number of bits that can be stored in a block.
*/
static constexpr size_type bits_per_block = std::numeric_limits<block_type>::digits;
/**
* @brief Maximum value of @ref size_type, returned for invalid positions.
*/
static constexpr size_type npos = std::numeric_limits<size_type>::max();
/**
* @brief Reference to a @ref sul::dynamic_bitset bit.
*
* @details As the bits in the @ref sul::dynamic_bitset class are stored in an optimized way
* in blocks, it is not possible for the subscript operators to return a reference
* to a boolean. Hence this class is used as a proxy to enable subscript operator of
* the @ref sul::dynamic_bitset class to be used as if it was an array of bools.
*/
class reference
{
public:
/**
* @brief Constructs a @ref reference to a bit from a @ref sul::dynamic_bitset and a
* bit position.
*
* @param bitset @ref sul::dynamic_bitset containing the bit
* @param[in] bit_pos Position of the bit to reference in the @ref sul::dynamic_bitset
*
* @complexity Constant.
*/
constexpr reference(dynamic_bitset<Block, Allocator>& bitset, size_type bit_pos);
/**
* @brief Copy constructor.
*/
constexpr reference(const reference&) noexcept = default;
/**
* @brief Move constructor.
*/
constexpr reference(reference&&) noexcept = default;
/**
* @brief Destructor.
*/
~reference() noexcept = default;
/**
* @brief Assign a value to the referenced bit.
*
* @param[in] v Value to assign to the referenced bit
*
* @return The @ref reference
*
* @complexity Constant.
*/
constexpr reference& operator=(bool v);
/**
* @brief Assign a value to the referenced bit from another @ref reference.
*
* @param[in] rhs @ref reference to the bit to assign value from
*
* @return The @ref reference
*
* @complexity Constant.
*/
constexpr reference& operator=(const reference& rhs);
/**
* @brief Assign a value to the referenced bit from another @ref reference.
*
* @param[in] rhs @ref reference to the bit to assign value from
*
* @return The @ref reference
*
* @complexity Constant.
*/
constexpr reference& operator=(reference&& rhs) noexcept;
/**
* @brief Apply binary operator AND to the referenced bit and a value, and assign the
* result to the referenced bit.
*
* @param[in] v Value to apply binary operator AND with
*
* @return The @ref reference
*
* @complexity Constant.
*/
constexpr reference& operator&=(bool v);
/**
* @brief Apply binary operator OR to the referenced bit and a value, and assign the
* result to the referenced bit.
*
* @param[in] v Value to apply binary operator OR with
*
* @return The @ref reference
*
* @complexity Constant.
*/
constexpr reference& operator|=(bool v);
/**
* @brief Apply binary operator XOR to the referenced bit and a value, and assign the
* result to the referenced bit.
*
* @param[in] v Value to apply binary operator XOR with
*
* @return The @ref reference
*
* @complexity Constant.
*/
constexpr reference& operator^=(bool v);
/**
* @brief Apply binary difference to the referenced bit and a value, and assign the
* result to the referenced bit.
*
* @details Equivalent to:
* @code
* this &= !v;
* @endcode
*
* @param[in] v Value to apply binary difference with
*
* @return The @ref reference
*
* @complexity Constant.
*/
constexpr reference& operator-=(bool v);
/**
* @brief Return the result of applying unary NOT operator.
*
* @return The opposite of the referenced bit
*
* @complexity Constant.
*/
[[nodiscard]] constexpr bool operator~() const;
/**
* @brief bool conversion operator.
*
* @complexity Constant.
*/
[[nodiscard]] constexpr operator bool() const;
/**
* @brief Deleted to avoid taking the address of a temporary proxy object.
*/
constexpr void operator&() = delete;
/**
* @brief Set the referenced bit to @a true.
*
* @return The @ref reference
*
* @complexity Constant.
*/
constexpr reference& set();
/**
* @brief Reset the referenced bit to @a false.
*
* @return The @ref reference
*
* @complexity Constant.
*/
constexpr reference& reset();
/**
* @brief Flip the referenced bit.
*
* @return The @ref reference
*
* @complexity Constant.
*/
constexpr reference& flip();
/**
* @brief Assign the value @p v to the referenced bit.
*
* @param[in] v Value to assign to the bit
*
* @return The @ref reference
*
* @complexity Constant.
*/
constexpr reference& assign(bool v);
private:
block_type& m_block;
block_type m_mask;
};
/**
* @brief Const reference to a @ref sul::dynamic_bitset bit, type bool.
*/
typedef bool const_reference;
/**
* @brief Copy contructor.
*/
constexpr dynamic_bitset(const dynamic_bitset<Block, Allocator>& other) = default;
/**
* @brief Move contructor.
*/
constexpr dynamic_bitset(dynamic_bitset<Block, Allocator>&& other) noexcept = default;
/**
* @brief Copy assignment operator.
*/
constexpr dynamic_bitset<Block, Allocator>& operator=(
const dynamic_bitset<Block, Allocator>& other) = default;
/**
* @brief Move assignment operator.
*/
constexpr dynamic_bitset<Block, Allocator>& operator=(
dynamic_bitset<Block, Allocator>&& other) noexcept = default;
/**
* @brief Constructs an empty @ref sul::dynamic_bitset.
*
* @details A copy of @p allocator will be used for memory management.
*
* @param[in] allocator Allocator to use for memory management
*
* @complexity Constant.
*/
constexpr explicit dynamic_bitset(const allocator_type& allocator = allocator_type());
/**
* @brief Constructs a @ref sul::dynamic_bitset of @p nbits bits from an initial value.
*
* @details The first bits are initialized with the bits from @p init_val, if @p nbits \>
* std\::numeric_limits\<unsigned long long\>\::digits , all other bits are
* initialized to @a false. A copy of @p allocator will be used for memory
* management.
*
* @param[in] nbits Number of bits of the @ref sul::dynamic_bitset
* @param[in] init_val Value to initialize the @ref sul::dynamic_bitset with
* @param[in] allocator Allocator to use for memory management
*
* @complexity Linear in @p nbits / @ref bits_per_block.
*/
constexpr explicit dynamic_bitset(size_type nbits,
unsigned long long init_val = 0,
const allocator_type& allocator = allocator_type());
/**
* @brief Constructs a @ref sul::dynamic_bitset using @p init_vals to initialize the first
* blocks.
*
* @details The size of the newly created @ref sul::dynamic_bitset is @p init_vals.size() *
* @ref bits_per_block. A copy of @p allocator will be used for memory management.
*
* @param[in] init_vals Value of the @ref sul::dynamic_bitset first blocks
* @param[in] allocator Allocator to use for memory management
*
* @complexity Linear in @p init_vals.size().
*/
constexpr dynamic_bitset(std::initializer_list<block_type> init_vals,
const allocator_type& allocator = allocator_type());
/**
* @brief Constructs a @ref sul::dynamic_bitset from a string or a part of a string.
*
* @details Construct the @ref sul::dynamic_bitset using the characters from @p str in the
* range \[@p pos, std\::min(@p pos + @p n, @p str.size())\[.
*
* @param[in] str String containing the part to use
* @param[in] pos Starting position of the string part to use in @p str
* @param[in] n Number of characters of @p str to use from the starting position
* @param[in] zero Character used to represent @a false bits in @p str
* @param[in] one Character used to represent @a true bits in @p str
* @param[in] allocator Allocator to use for memory management
*
* @tparam _CharT Character type of the string
* @tparam _Traits Traits class specifying the operations on the character type of the
* string
*
* @pre @code
* pos < str.size()
* @endcode
*
* @complexity Linear in std\::min(@p n, @p str.size() - @p pos).
*/
template<typename _CharT, typename _Traits>
constexpr explicit dynamic_bitset(
std::basic_string_view<_CharT, _Traits> str,
typename std::basic_string_view<_CharT, _Traits>::size_type pos = 0,
typename std::basic_string_view<_CharT, _Traits>::size_type n =
std::basic_string_view<_CharT, _Traits>::npos,
_CharT zero = _CharT('0'),
_CharT one = _CharT('1'),
const allocator_type& allocator = allocator_type());
/**
* @brief Constructs a @ref sul::dynamic_bitset from a string or a part of a string.
*
* @details Construct the @ref sul::dynamic_bitset using the characters from @p str in the
* range \[@p pos, std\::min(@p pos + @p n, @p str.size())\[.
*
* @param[in] str String containing the part to use
* @param[in] pos Starting position of the string part to use in @p str
* @param[in] n Number of characters of @p str to use from the starting position
* @param[in] zero Character used to represent @a false bits in @p str
* @param[in] one Character used to represent @a true bits in @p str
* @param[in] allocator Allocator to use for memory management
*
* @tparam _CharT Character type of the string
* @tparam _Traits Traits class specifying the operations on the character type of the
* string
* @tparam _Alloc Allocator type used to allocate internal storage of the string
*
* @pre @code
* pos < str.size()
* @endcode
*
* @complexity Linear in std\::min(@p n, @p str.size() - @p pos).
*/
template<typename _CharT, typename _Traits, typename _Alloc>
constexpr explicit dynamic_bitset(
const std::basic_string<_CharT, _Traits, _Alloc>& str,
typename std::basic_string<_CharT, _Traits, _Alloc>::size_type pos = 0,
typename std::basic_string<_CharT, _Traits, _Alloc>::size_type n =
std::basic_string<_CharT, _Traits, _Alloc>::npos,
_CharT zero = _CharT('0'),
_CharT one = _CharT('1'),
const allocator_type& allocator = allocator_type());
/**
* @brief Constructs a @ref sul::dynamic_bitset from a string or a part of a string.
*
* @details Construct the @ref sul::dynamic_bitset using the characters from @p str in the
* range \[@p pos, std\::min(@p pos + @p n, @p _Traits\::length(@p str))\[.
*
* @param[in] str String containing the part to use
* @param[in] pos Starting position of the string part to use
* @param[in] n Number of characters to use from the starting position
* @param[in] zero Character used to represent @a false bits in the string
* @param[in] one Character used to represent 1 @a true bitsn the string
* @param[in] allocator Allocator to use for memory management
*
* @tparam _CharT Character type of the string
* @tparam _Traits Traits class specifying the operations on the character type of the
* string
*
* @pre @code
* pos < _Traits::length(str)
* @endcode
*
* @complexity Linear in std\::min(@p n, @p _Traits\::length(@p str) - @p pos).
*/
template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
constexpr explicit dynamic_bitset(
const _CharT* str,
typename std::basic_string<_CharT>::size_type pos = 0,
typename std::basic_string<_CharT>::size_type n = std::basic_string<_CharT>::npos,
_CharT zero = _CharT('0'),
_CharT one = _CharT('1'),
const allocator_type& allocator = allocator_type());
/**
* @brief Destructor.
*/
~dynamic_bitset() noexcept = default;
/**
* @brief Resize the @ref sul::dynamic_bitset to contain @p nbits bits.
*
* @details Bits keep the value they had before the resize and, if @p nbits is greater than
* the current size, new bit are initialized to @p value.
*
* @param[in] nbits New size of the @ref sul::dynamic_bitset
* @param[in] value Value of the new bits
*
* @complexity Linear in the difference between the current size and @p nbits.
* Additional complexity possible due to reallocation if capacity is less than @p
* nbits.
*/
constexpr void resize(size_type nbits, bool value = false);
/**
* @brief Clears the @ref sul::dynamic_bitset, resize it to 0.
*
* @details Equivalent to:
* @code
* this.resize(0);
* @endcode
*
* @complexity Linear in the size of the @ref sul::dynamic_bitset.
*/
constexpr void clear();
/**
* @brief Add a new bit with the value @p value at the end of the @ref sul::dynamic_bitset.
*
* @details Increase the size of the bitset by one, the added bit becomes the
* most-significant bit.
*
* @param[in] value Value of the bit to add
*
* @complexity Amortized constant.
*/
constexpr void push_back(bool value);
/**
* @brief Remove the last bit of the @ref sul::dynamic_bitset.
*
* @details Decrease the size of the @ref sul::dynamic_bitset by one, does nothing if the
* @ref dynamic_bitset is empty.
*
* @complexity Constant.
*/
constexpr void pop_back();
/**
* @brief Append a block of bits @p block at the end of the @ref sul::dynamic_bitset.
*
* @details Increase the size of the @ref sul::dynamic_bitset by @ref bits_per_block.
*
* @param[in] block Block of bits to add
*
* @complexity Amortized constant.
*/
constexpr void append(block_type block);
/**
* @brief Append blocks of bits from @p blocks at the end of the @ref sul::dynamic_bitset.
*
* @param[in] blocks Blocks of bits to add
*
* @complexity Linear in the size of @p blocks. Additional complexity possible due
* to reallocation if capacity is less than @ref size() + @p blocks.size() * @ref
* bits_per_block.
*/
constexpr void append(std::initializer_list<block_type> blocks);
/**
* @brief Append blocks of bits from the range \[@p first, @p last\[ at the end of the @ref
* dynamic_bitset.
*
* @param[in] first First iterator of the range
* @param[in] last Last iterator of the range (after the last element to add)
*
* @tparam BlockInputIterator Type of the range iterators
*
* @complexity Linear in the size of the range. Additional complexity possible due
* to reallocation if capacity is less than @ref size() + std\::distance(@p first,
* @p last) * @ref bits_per_block.
*/
template<typename BlockInputIterator>
constexpr void append(BlockInputIterator first, BlockInputIterator last);
/**
* @brief Sets the bits to the result of binary AND on corresponding pairs of bits of *this
* and @p rhs.
*
* @param[in] rhs Right hand side @ref sul::dynamic_bitset of the operator
*
* @return A reference to the @ref sul::dynamic_bitset *this
*
* @pre @code
* size() == rhs.size()
* @endcode
*
* @complexity Linear in the size of the @ref sul::dynamic_bitset.
*/
constexpr dynamic_bitset<Block, Allocator>& operator&=(
const dynamic_bitset<Block, Allocator>& rhs);
/**
* @brief Sets the bits to the result of binary OR on corresponding pairs of bits of *this
* and @p rhs.
*
* @param[in] rhs Right hand side @ref sul::dynamic_bitset of the operator
*
* @return A reference to the @ref sul::dynamic_bitset *this
*
* @pre @code
* size() == rhs.size()
* @endcode
*
* @complexity Linear in the size of the @ref sul::dynamic_bitset.
*/
constexpr dynamic_bitset<Block, Allocator>& operator|=(
const dynamic_bitset<Block, Allocator>& rhs);
/**
* @brief Sets the bits to the result of binary XOR on corresponding pairs of bits of *this
* and @p rhs.
*
* @param[in] rhs Right hand side @ref sul::dynamic_bitset of the operator
*
* @return A reference to the @ref sul::dynamic_bitset *this
*
* @pre @code
* size() == rhs.size()
* @endcode
*
* @complexity Linear in the size of the @ref sul::dynamic_bitset.
*/
constexpr dynamic_bitset<Block, Allocator>& operator^=(
const dynamic_bitset<Block, Allocator>& rhs);
/**
* @brief Sets the bits to the result of the binary difference between the bits of *this
* and @p rhs.
*
* @details Less efficient but equivalent way to get this result:
* @code
* this &= ~rhs;
* @endcode
*
* @param[in] rhs Right hand side @ref sul::dynamic_bitset of the operator
*
* @return A reference to the @ref sul::dynamic_bitset *this
*
* @pre @code
* size() == rhs.size()
* @endcode
*
* @complexity Linear in the size of the @ref sul::dynamic_bitset.
*/
constexpr dynamic_bitset<Block, Allocator>& operator-=(
const dynamic_bitset<Block, Allocator>& rhs);
/**
* @brief Performs binary shift left of @p shift bits.
*
* @details Zeroes are shifted in, does nothing if @p shift == 0.
*
* @param[in] shift Number of positions to shift the bits
*
* @return A reference to the @ref sul::dynamic_bitset *this
*
* @complexity Linear in the size of the @ref sul::dynamic_bitset.
*/
constexpr dynamic_bitset<Block, Allocator>& operator<<=(size_type shift);
/**
* @brief Performs binary shift right of @p shift bits.
*
* @details Zeroes are shifted in, does nothing if @p shift == 0.
*
* @param[in] shift Number of positions to shift the bits
*
* @return A reference to the @ref sul::dynamic_bitset *this
*
* @complexity Linear in the size of the @ref sul::dynamic_bitset.
*/
constexpr dynamic_bitset<Block, Allocator>& operator>>=(size_type shift);
/**
* @brief Performs binary shift right of @p shift bits.
*
* @details Zeroes are shifted in. Does nothing if @p shift == 0.\n
* Equivalent to:
* @code
* dynamic_bitset<Block, Allocator> bitset(*this);
* bitset <<= shift;
* @endcode
*
* @param[in] shift Number of positions to shift the bits
*
* @return A new @ref sul::dynamic_bitset containing the shifted bits
*
* @complexity Linear in the size of the @ref sul::dynamic_bitset.
*/
[[nodiscard]] constexpr dynamic_bitset<Block, Allocator> operator<<(size_type shift) const;
/**
* @brief Performs binary shift left of @p shift bits.
*
* @details Zeroes are shifted in. Does nothing if @p shift == 0.\n
* Equivalent to:
* @code
* dynamic_bitset<Block, Allocator> bitset(*this);
* bitset >>= shift;
* @endcode
*
* @param[in] shift Number of positions to shift the bits
*
* @return A new @ref sul::dynamic_bitset containing the shifted bits
*
* @complexity Linear in the size of the @ref sul::dynamic_bitset.
*/
[[nodiscard]] constexpr dynamic_bitset<Block, Allocator> operator>>(size_type shift) const;
/**
* @brief Performs a unary NOT on all bits.
*
* @details Equivalent to:
* @code
* dynamic_bitset<Block, Allocator> bitset(*this);
* bitset.flip();
* @endcode
*
* @return A copy of *this with all bits flipped
*
* @complexity Linear in the size of the @ref sul::dynamic_bitset.
*/
[[nodiscard]] constexpr dynamic_bitset<Block, Allocator> operator~() const;
/**
* @brief Set the bits of the range \[@p pos, @p pos + @p len\[ to value @p value.
*
* @details Does nothing if @p len == 0.
*
* @param[in] pos Position of the first bit of the range
* @param[in] len Length of the range
* @param[in] value Value to set the bits to
*
* @return A reference to the @ref sul::dynamic_bitset *this
*
* @pre @code
* (pos < size()) && ((len == 0) || (pos + len - 1 < size()))
* @endcode
*
* @complexity Linear in @p len.
*/
constexpr dynamic_bitset<Block, Allocator>& set(size_type pos, size_type len, bool value);
/**
* @brief Set the bit at the position @p pos to @a true or value @p value.
*
* @param[in] pos Position of the bit to set
* @param[in] value Value to set the bit to
*
* @return A reference to the @ref sul::dynamic_bitset *this
*
* @pre @code
* pos < size()
* @endcode
*
* @complexity Constant.
*/
constexpr dynamic_bitset<Block, Allocator>& set(size_type pos, bool value = true);
/**
* @brief Set all the bits of the @ref sul::dynamic_bitset to @a true.
*
* @return A reference to the @ref sul::dynamic_bitset *this
*
* @complexity Linear in the size of the @ref sul::dynamic_bitset.
*/
constexpr dynamic_bitset<Block, Allocator>& set();
/**
* @brief Reset the bits of the range \[@p pos, @p pos + @p len\[ to @a false.
*
* @param[in] pos Position of the first bit of the range
* @param[in] len Length of the range
*
* @return A reference to the @ref sul::dynamic_bitset *this
*
* @pre @code
* (pos < size()) && ((len == 0) || (pos + len - 1 < size()))
* @endcode
*
* @complexity Linear in @p len.
*/
constexpr dynamic_bitset<Block, Allocator>& reset(size_type pos, size_type len);
/**
* @brief Reset the bit at the position @p pos to @a false.
*
* @param[in] pos Position of the bit to reset
*
* @return A reference to the @ref sul::dynamic_bitset *this
*
* @pre @code
* pos < size()
* @endcode
*
* @complexity Constant.
*/
constexpr dynamic_bitset<Block, Allocator>& reset(size_type pos);
/**
* @brief Reset all the bits of the @ref sul::dynamic_bitset to @a false.
*
* @return A reference to the @ref sul::dynamic_bitset *this
*
* @complexity Linear in the size of the @ref sul::dynamic_bitset.
*/
constexpr dynamic_bitset<Block, Allocator>& reset();
/**
* @brief Flip the bits of the range \[@p pos, @p pos + @p len\[.
*
* @param[in] pos Position of the first bit of the range
* @param[in] len Length of the range
*
* @return A reference to the @ref sul::dynamic_bitset *this
*
* @pre @code
* (pos < size()) && ((len == 0) || (pos + len - 1 < size()))
* @endcode
*
* @complexity Linear in @p len.
*/
constexpr dynamic_bitset<Block, Allocator>& flip(size_type pos, size_type len);
/**
* @brief Flip the bit at the position @p pos.
*
* @param[in] pos Position of the bit to reset
*
* @return A reference to the @ref sul::dynamic_bitset *this
*
* @pre @code
* pos < size()
* @endcode
*
* @complexity Constant.
*/
constexpr dynamic_bitset<Block, Allocator>& flip(size_type pos);
/**
* @brief Flip all the bits of the @ref sul::dynamic_bitset.
*
* @return A reference to the @ref sul::dynamic_bitset *this
*
* @complexity Linear in the size of the @ref sul::dynamic_bitset.
*/
constexpr dynamic_bitset<Block, Allocator>& flip();
/**
* @brief Test the value of the bit at position @p pos.
*
* @param[in] pos Position of the bit to test
*
* @return The tested bit value
*
* @pre @code
* pos < size()
* @endcode
*
* @complexity Constant.
*/
[[nodiscard]] constexpr bool test(size_type pos) const;
/**
* @brief Test the value of the bit at position @p pos and set it to @a true or value @p
* value.
*
* @param[in] pos Position of the bit to test and set
* @param[in] value Value to set the bit to
*
* @return The tested bit value
*
* @pre @code
* pos < size()
* @endcode
*
* @complexity Constant.
*/
[[nodiscard]] constexpr bool test_set(size_type pos, bool value = true);
/**
* @brief Checks if all bits are set to @a true.
*
* @return @a true if all bits are set to @a true, otherwise @a false
*
* @remark Return @a true if the @ref sul::dynamic_bitset is empty, the logic is that you
* are checking if all bits are set to @a true, meaning none of them is set to @a
* false, and in an empty @ref sul::dynamic_bitset no bits are set to @a false.
*
* @complexity Linear in the size of the @ref sul::dynamic_bitset.
*/
[[nodiscard]] constexpr bool all() const;
/**
* @brief Checks if any bits are set to @a true.
*
* @return @a true if any of the bits is set to @a true, otherwise @a false
*
* @remark Return @a false if the @ref sul::dynamic_bitset is empty, the logic is you are
* checking if there is at least one bit set to @a true and in an empty @ref
* dynamic_bitset there is no bit set to @a true.
*
* @complexity Linear in the size of the @ref sul::dynamic_bitset.
*/
[[nodiscard]] constexpr bool any() const;
/**
* @brief Checks if none of the bits are set to @a true.
*
* @return @a true if none of the bits is set to @a true, otherwise @a false
*
* @remark Return @a true if the @ref sul::dynamic_bitset is empty, the logic is that you
* are checking if there is no bit set to @a true and in an empty @ref
* sul::dynamic_bitset there is no bit that can be set to @a true.
*
* @complexity Linear in the size of the @ref sul::dynamic_bitset.
*/
[[nodiscard]] constexpr bool none() const;
/**
* @brief Count the number of bits set to @a true.
*
* @details Return 0 if the @ref sul::dynamic_bitset is empty.
*
* @return The number of bits that are set to @a true
*
* @complexity Linear in the size of the @ref sul::dynamic_bitset.
*/
[[nodiscard]] constexpr size_type count() const noexcept;
/**
* @brief Accesses the bit at position @p pos.
*
* @param[in] pos Position of the bit to access
*
* @return A @ref reference object which allows writing to the requested bit
*
* @pre @code
* pos < size()
* @endcode
*
* @complexity Constant.
*/
[[nodiscard]] constexpr reference operator[](size_type pos);
/**
* @brief Accesses the bit at position @p pos.
*
* @param[in] pos Position of the bit to access
*
* @return The value of the requested bit
*
* @pre @code
* pos < size()
* @endcode
*
* @complexity Constant.
*/
[[nodiscard]] constexpr const_reference operator[](size_type pos) const;
/**
* @brief Give the number of bits of the @ref sul::dynamic_bitset.
*
* @return The number of bits of the @ref sul::dynamic_bitset
*
* @complexity Constant.
*/
[[nodiscard]] constexpr size_type size() const noexcept;
/**
* @brief Give the number of blocks used by the @ref sul::dynamic_bitset.
*
* @return The number of blocks used by the @ref sul::dynamic_bitset
*
* @complexity Constant.
*/
[[nodiscard]] constexpr size_type num_blocks() const noexcept;
/**
* @brief Checks if the @ref sul::dynamic_bitset is empty.
*
* @details Equivalent to:
* @code
* size() == 0;
* @endcode
*
* @return @a true if the @ref sul::dynamic_bitset is empty, @a false otherwise
*
* @complexity Constant.