-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdynamic_bitset.h
3618 lines (3315 loc) · 118 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 © 2019 Maxime Pinard (MIT License)
//
// 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.
//
// This code is mostly from https://github.com/pinam45/dynamic_bitset, with
// several modifications
#pragma once
/** @file
* @brief @ref yacl::dynamic_bitset declaration and implementation.
*
* @details Standalone file, does not depend on other implementation files or
* dependencies other than the standard library, which 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.
*
* @since 1.0.0
*/
#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <limits>
#include <memory>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>
#include "yacl/base/exception.h"
#include "yacl/base/int128.h"
// define DYNAMIC_BITSET_CAN_USE_LIBPOPCNT
#if !defined(DYNAMIC_BITSET_NO_LIBPOPCNT)
// https://github.com/kimwalisch/libpopcnt
#if __has_include(<libpopcnt.h>)
#include <libpopcnt.h>
#define DYNAMIC_BITSET_CAN_USE_LIBPOPCNT true
#endif
#endif
#if !defined(DYNAMIC_BITSET_CAN_USE_LIBPOPCNT)
#define DYNAMIC_BITSET_CAN_USE_LIBPOPCNT false
#endif
// define DYNAMIC_BITSET_CAN_USE_STD_BITOPS
#if !defined(DYNAMIC_BITSET_NO_STD_BITOPS)
// https://en.cppreference.com/w/cpp/header/bit
#if __has_include(<bit>)
#include <bit>
#ifdef __cpp_lib_bitops
#define DYNAMIC_BITSET_CAN_USE_STD_BITOPS true
#endif
#endif
#endif
#if !defined(DYNAMIC_BITSET_CAN_USE_STD_BITOPS)
#define DYNAMIC_BITSET_CAN_USE_STD_BITOPS false
#endif
// define DYNAMIC_BITSET_CAN_USE_CLANG_BUILTIN_POPCOUNT
// define DYNAMIC_BITSET_CAN_USE_CLANG_BUILTIN_CTZ
// define DYNAMIC_BITSET_CAN_USE_GCC_BUILTIN
// define DYNAMIC_BITSET_CAN_USE_MSVC_BUILTIN_BITSCANFORWARD
// define DYNAMIC_BITSET_CAN_USE_MSVC_BUILTIN_BITSCANFORWARD64
#if !DYNAMIC_BITSET_CAN_USE_STD_BITOPS && \
!defined(DYNAMIC_BITSET_NO_COMPILER_BUILTIN)
#if defined(__clang__)
// https://clang.llvm.org/docs/LanguageExtensions.html#feature-checking-macros
// https://clang.llvm.org/docs/LanguageExtensions.html#intrinsics-support-within-constant-expressions
#ifdef __has_builtin
#if __has_builtin(__builtin_popcount) && __has_builtin(__builtin_popcountl) && \
__has_builtin(__builtin_popcountll)
#define DYNAMIC_BITSET_CAN_USE_CLANG_BUILTIN_POPCOUNT true
#endif
#if __has_builtin(__builtin_ctz) && __has_builtin(__builtin_ctzl) && \
__has_builtin(__builtin_ctzll)
#define DYNAMIC_BITSET_CAN_USE_CLANG_BUILTIN_CTZ true
#endif
#endif
#elif defined(__GNUC__) // also defined by clang
// https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
#define DYNAMIC_BITSET_CAN_USE_GCC_BUILTIN true
#elif defined(_MSC_VER)
// https://docs.microsoft.com/en-us/cpp/intrinsics/bitscanforward-bitscanforward64
// __popcnt16, __popcnt, __popcnt64 not used because it require to check the
// hardware support at runtime
// (https://docs.microsoft.com/fr-fr/cpp/intrinsics/popcnt16-popcnt-popcnt64?view=msvc-160#remarks)
#if defined(_M_IX86) || defined(_M_ARM) || defined(_M_X64) || defined(_M_ARM64)
#include <intrin.h>
#pragma intrinsic(_BitScanForward)
#define DYNAMIC_BITSET_CAN_USE_MSVC_BUILTIN_BITSCANFORWARD true
#endif
#if (defined(_M_X64) || defined(_M_ARM64)) && \
!defined(DYNAMIC_BITSET_NO_MSVC_BUILTIN_BITSCANFORWARD64) // for testing
// purposes
#pragma intrinsic(_BitScanForward64)
#define DYNAMIC_BITSET_CAN_USE_MSVC_BUILTIN_BITSCANFORWARD64 true
#endif
#endif
#endif
#if !defined(DYNAMIC_BITSET_CAN_USE_CLANG_BUILTIN_POPCOUNT)
#define DYNAMIC_BITSET_CAN_USE_CLANG_BUILTIN_POPCOUNT false
#endif
#if !defined(DYNAMIC_BITSET_CAN_USE_CLANG_BUILTIN_CTZ)
#define DYNAMIC_BITSET_CAN_USE_CLANG_BUILTIN_CTZ false
#endif
#if !defined(DYNAMIC_BITSET_CAN_USE_GCC_BUILTIN)
#define DYNAMIC_BITSET_CAN_USE_GCC_BUILTIN false
#endif
#if !defined(DYNAMIC_BITSET_CAN_USE_MSVC_BUILTIN_BITSCANFORWARD)
#define DYNAMIC_BITSET_CAN_USE_MSVC_BUILTIN_BITSCANFORWARD false
#endif
#if !defined(DYNAMIC_BITSET_CAN_USE_MSVC_BUILTIN_BITSCANFORWARD64)
#define DYNAMIC_BITSET_CAN_USE_MSVC_BUILTIN_BITSCANFORWARD64 false
#endif
#if !defined(DYNAMIC_BITSET_CAN_USE_MSVC_BUILTIN)
#define DYNAMIC_BITSET_CAN_USE_MSVC_BUILTIN false
#endif
/**
* @brief Simple Useful Libraries.
*
* @since 1.0.0
*/
namespace yacl {
/**
* @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
*
* @since 1.0.0
*/
template <typename T>
struct template_parameter;
template <template <typename...> class C, typename T>
struct template_parameter<C<T>> {
using type = T;
};
template <typename T>
using template_parameter_t = typename template_parameter<T>::type;
template <typename T>
struct is_dynamic_bitset_block_type
: public std::disjunction<std::is_unsigned<T>, std::is_same<T, uint128_t>> {
};
template <typename T>
struct is_dynamic_bitset_type
: public is_dynamic_bitset_block_type<template_parameter_t<T>> {};
template <typename Block = uint128_t,
typename Allocator = std::allocator<Block>>
class dynamic_bitset {
static_assert(is_dynamic_bitset_block_type<Block>::value,
"Block is not an unsigned integral type");
public:
/**
* @brief Type used to represent the size of a @ref yacl::dynamic_bitset.
*
* @since 1.0.0
*/
using size_type = size_t;
/**
* @brief Same type as @p Block.
*
* @since 1.0.0
*/
using block_type = Block;
/**
* @brief Same type as @p Allocator.
*
* @since 1.0.0
*/
using allocator_type = Allocator;
/**
* @brief Number of bits that can be stored in a block.
*
* @since 1.0.0
*/
static constexpr size_type bits_per_block =
std::numeric_limits<block_type>::digits;
/**
* @brief Maximum value of @ref size_type, returned for invalid
* positions.
*
* @since 1.0.0
*/
static constexpr size_type npos = std::numeric_limits<size_type>::max();
/**
* @brief Reference to a @ref yacl::dynamic_bitset bit.
*
* @details As the bits in the @ref yacl::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 yacl::dynamic_bitset class
* to be used as if it was an array of bools.
*
* @since 1.0.0
*/
class reference {
public:
/**
* @brief Constructs a @ref reference to a bit from a @ref
* yacl::dynamic_bitset and a bit position.
*
* @param bitset @ref yacl::dynamic_bitset containing the bit
* @param[in] bit_pos Position of the bit to reference in the @ref
* yacl::dynamic_bitset
*
* @complexity Constant.
*
* @since 1.0.0
*/
constexpr reference(dynamic_bitset<Block, Allocator>& bitset,
size_type bit_pos);
/**
* @brief Copy constructor.
*
* @since 1.0.0
*/
constexpr reference(const reference&) noexcept = default;
/**
* @brief Move constructor.
*
* @since 1.0.0
*/
constexpr reference(reference&&) noexcept = default;
/**
* @brief Destructor.
*
* @since 1.0.0
*/
~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.
*
* @since 1.0.0
*/
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.
*
* @since 1.0.0
*/
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.
*
* @since 1.0.0
*/
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.
*
* @since 1.0.0
*/
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.
*
* @since 1.0.0
*/
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.
*
* @since 1.0.0
*/
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.
*
* @since 1.0.0
*/
constexpr reference& operator-=(bool v);
/**
* @brief Return the result of applying unary NOT operator.
*
* @return The opposite of the referenced bit
*
* @complexity Constant.
*
* @since 1.0.0
*/
[[nodiscard]] constexpr bool operator~() const;
/**
* @brief bool conversion operator.
*
* @complexity Constant.
*
* @since 1.0.0
*/
[[nodiscard]] constexpr operator bool() const;
/**
* @brief Deleted to avoid taking the address of a temporary proxy
* object.
*
* @since 1.0.0
*/
constexpr void operator&() = delete;
/**
* @brief Set the referenced bit to @a true.
*
* @return The @ref reference
*
* @complexity Constant.
*
* @since 1.0.0
*/
constexpr reference& set();
/**
* @brief Reset the referenced bit to @a false.
*
* @return The @ref reference
*
* @complexity Constant.
*
* @since 1.0.0
*/
constexpr reference& reset();
/**
* @brief Flip the referenced bit.
*
* @return The @ref reference
*
* @complexity Constant.
*
* @since 1.0.0
*/
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.
*
* @since 1.0.0
*/
constexpr reference& assign(bool v);
private:
block_type& m_block;
block_type m_mask;
};
/**
* @brief Const reference to a @ref yacl::dynamic_bitset bit, type bool.
*
* @since 1.0.0
*/
using const_reference = bool;
/**
* @brief Copy constructor.
*
* @since 1.0.0
*/
constexpr dynamic_bitset(const dynamic_bitset<Block, Allocator>& other) =
default;
/**
* @brief Move constructor.
*
* @since 1.0.0
*/
constexpr dynamic_bitset(dynamic_bitset<Block, Allocator>&& other) noexcept =
default;
/**
* @brief Copy assignment operator.
*
* @since 1.0.0
*/
constexpr dynamic_bitset<Block, Allocator>& operator=(
const dynamic_bitset<Block, Allocator>& other) = default;
/**
* @brief Move assignment operator.
*
* @since 1.0.0
*/
constexpr dynamic_bitset<Block, Allocator>& operator=(
dynamic_bitset<Block, Allocator>&& other) noexcept = default;
/**
* @brief Constructs an empty @ref yacl::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.
*
* @since 1.0.0
*/
constexpr explicit dynamic_bitset(
const allocator_type& allocator = allocator_type());
/**
* @brief Constructs a @ref yacl::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\<uint128_t\>\::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 yacl::dynamic_bitset
* @param[in] init_val Value to initialize the @ref yacl::dynamic_bitset
* with
* @param[in] allocator Allocator to use for memory management
*
* @complexity Linear in @p nbits / @ref bits_per_block.
*
* @since 1.0.0
*/
constexpr explicit dynamic_bitset(
size_type nbits, uint128_t init_val = 0,
const allocator_type& allocator = allocator_type());
/**
* @brief Constructs a @ref yacl::dynamic_bitset using @p init_vals to
* initialize the first blocks.
*
* @details The size of the newly created @ref yacl::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 yacl::dynamic_bitset first blocks
* @param[in] allocator Allocator to use for memory management
*
* @complexity Linear in @p init_vals.size().
*
* @since 1.0.0
*/
constexpr dynamic_bitset(std::initializer_list<block_type> init_vals,
const allocator_type& allocator = allocator_type());
/**
* @brief Constructs a @ref yacl::dynamic_bitset from a string or a part
* of a string.
*
* @details Construct the @ref yacl::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).
*
* @since 1.0.0
*/
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 yacl::dynamic_bitset from a string or a part
* of a string.
*
* @details Construct the @ref yacl::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).
*
* @since 1.0.0
*/
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 yacl::dynamic_bitset from a string or a part
* of a string.
*
* @details Construct the @ref yacl::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).
*
* @since 1.0.0
*/
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.
*
* @since 1.0.0
*/
~dynamic_bitset() noexcept = default;
/**
* @brief Resize the @ref yacl::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 yacl::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.
*
* @since 1.0.0
*/
constexpr void resize(size_type nbits, bool value = false);
/**
* @brief Clears the @ref yacl::dynamic_bitset, resize it to 0.
*
* @details Equivalent to:
* @code
* this.resize(0);
* @endcode
*
* @complexity Linear in the size of the @ref yacl::dynamic_bitset.
*
* @since 1.0.0
*/
constexpr void clear();
/**
* @brief Add a new bit with the value @p value at the end of the @ref
* yacl::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.
*
* @since 1.0.0
*/
constexpr void push_back(bool value);
/**
* @brief Remove the last bit of the @ref yacl::dynamic_bitset.
*
* @details Decrease the size of the @ref yacl::dynamic_bitset by one, does
* nothing if the
* @ref dynamic_bitset is empty.
*
* @complexity Constant.
*
* @since 1.0.0
*/
constexpr void pop_back();
/**
* @brief Append a block of bits @p block at the end of the @ref
* yacl::dynamic_bitset.
*
* @details Increase the size of the @ref yacl::dynamic_bitset by @ref
* bits_per_block.
*
* @param[in] block Block of bits to add
*
* @complexity Amortized constant.
*
* @since 1.0.0
*/
constexpr void append(block_type block);
/**
* @brief Append blocks of bits from @p blocks at the end of the @ref
* yacl::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.
*
* @since 1.0.0
*/
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.
*
* @since 1.0.0
*/
template <typename BlockInputIterator>
constexpr void append(BlockInputIterator first, BlockInputIterator last);
constexpr void append(const dynamic_bitset<Block, Allocator>& other);
/**
* @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 yacl::dynamic_bitset of the operator
*
* @return A reference to the @ref yacl::dynamic_bitset *this
*
* @pre @code
* size() == rhs.size()
* @endcode
*
* @complexity Linear in the size of the @ref yacl::dynamic_bitset.
*
* @since 1.0.0
*/
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 yacl::dynamic_bitset of the operator
*
* @return A reference to the @ref yacl::dynamic_bitset *this
*
* @pre @code
* size() == rhs.size()
* @endcode
*
* @complexity Linear in the size of the @ref yacl::dynamic_bitset.
*
* @since 1.0.0
*/
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 yacl::dynamic_bitset of the operator
*
* @return A reference to the @ref yacl::dynamic_bitset *this
*
* @pre @code
* size() == rhs.size()
* @endcode
*
* @complexity Linear in the size of the @ref yacl::dynamic_bitset.
*
* @since 1.0.0
*/
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 yacl::dynamic_bitset of the operator
*
* @return A reference to the @ref yacl::dynamic_bitset *this
*
* @pre @code
* size() == rhs.size()
* @endcode
*
* @complexity Linear in the size of the @ref yacl::dynamic_bitset.
*
* @since 1.0.0
*/
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 yacl::dynamic_bitset *this
*
* @complexity Linear in the size of the @ref yacl::dynamic_bitset.
*
* @since 1.0.0
*/
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 yacl::dynamic_bitset *this
*
* @complexity Linear in the size of the @ref yacl::dynamic_bitset.
*
* @since 1.0.0
*/
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 yacl::dynamic_bitset containing the shifted bits
*
* @complexity Linear in the size of the @ref yacl::dynamic_bitset.
*
* @since 1.0.0
*/
[[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 yacl::dynamic_bitset containing the shifted bits
*
* @complexity Linear in the size of the @ref yacl::dynamic_bitset.
*
* @since 1.0.0
*/
[[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 yacl::dynamic_bitset.
*
* @since 1.0.0
*/
[[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 yacl::dynamic_bitset *this
*
* @pre @code
* (pos < size()) && ((len == 0) || (pos + len - 1 < size()))
* @endcode
*
* @complexity Linear in @p len.
*
* @since 1.0.0
*/
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 yacl::dynamic_bitset *this
*
* @pre @code
* pos < size()
* @endcode
*
* @complexity Constant.