-
Notifications
You must be signed in to change notification settings - Fork 59
/
dynamic_bitset.html
1819 lines (1524 loc) · 60.9 KB
/
dynamic_bitset.html
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
<?xml version="1.0" encoding="ascii"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<!--
Copyright (c) 2001 Jeremy Siek
Copyright (c) 2003-2004, 2008 Gennaro Prota
Copyright (c) 2014 Ahmed Charles
Copyright (c) 2014 Riccardo Marcangelo
Copyright (c) 2018 Evgeny Shulgin
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)
-->
<!--
Copyright (c) 1996-1999
Silicon Graphics Computer Systems, Inc.
Permission to use, copy, modify, distribute and sell this software
and its documentation for any purpose is hereby granted without fee,
provided that the above copyright notice appears in all copies and
that both that copyright notice and this permission notice appear
in supporting documentation. Silicon Graphics makes no
representations about the suitability of this software for any
purpose. It is provided "as is" without express or implied warranty.
Copyright (c) 1994
Hewlett-Packard Company
Permission to use, copy, modify, distribute and sell this software
and its documentation for any purpose is hereby granted without fee,
provided that the above copyright notice appears in all copies and
that both that copyright notice and this permission notice appear
in supporting documentation. Hewlett-Packard Company makes no
representations about the suitability of this software for any
purpose. It is provided "as is" without express or implied warranty.
-->
<head>
<title>dynamic_bitset<Block, Allocator></title>
<link rel="stylesheet" type="text/css" href="../../rst.css" />
</head>
<body>
<div id="body">
<div id="body-inner">
<div id="content">
<div class="section" id="docs">
<div class="section-0">
<div class="section-body">
<div id="boost-logo"><img src="../../boost.png" alt="Boost C++ Libraries" /></div>
<h1>dynamic_bitset<Block, Allocator></h1>
<h2>Contents</h2>
<dl class="index">
<dt><a href="#description">Description</a></dt>
<dt><a href="#synopsis">Synopsis</a></dt>
<dt><a href="#definitions">Definitions</a></dt>
<dt><a href="#examples">Examples</a></dt>
<dt><a href="#rationale">Rationale</a></dt>
<dt><a href="#header-files">Header Files</a></dt>
<dt><a href="#template-parameters">Template Parameters</a></dt>
<dt><a href="#concepts-modeled">Concepts modeled</a></dt>
<dt><a href="#type-requirements">Type requirements</a></dt>
<dt><a href="#public-base-classes">Public base classes</a></dt>
<dt><a href="#nested-type-names">Nested type names</a></dt>
<dt><a href="#public-data-members">Public data members</a></dt>
<dt><a href="#constructors">Constructors</a></dt>
<dt><a href="#destructor">Destructor</a></dt>
<dt><a href="#member-functions">Member functions</a></dt>
<dt><a href="#non-member-functions">Non-member functions</a></dt>
<dt><a href="#exception-guarantees">Exception guarantees</a></dt>
<dt><a href="#changes-from-previous-ver"><b>Changes from previous version(s)</b></a></dt>
<dt><a href="#see-also">See also</a></dt>
<dt><a href="#acknowledgements">Acknowledgements</a></dt>
</dl>
<h3><a id="description">Description</a></h3>
<p>The <tt>dynamic_bitset</tt> class represents a set of bits. It
provides accesses to the value of individual bits via an
<tt>operator[]</tt> and provides all of the bitwise operators
that one can apply to builtin integers, such as
<tt>operator&</tt> and <tt>operator<<</tt>. The number
of bits in the set is specified at runtime via a parameter to the
constructor of the <tt>dynamic_bitset</tt>.</p>
<p>The <tt>dynamic_bitset</tt> class is nearly identical to the
<a href=
"https://boost.org/sgi/stl/bitset.html"><tt>std::bitset</tt></a>
class. The difference is that the size of the
<tt>dynamic_bitset</tt> (the number of bits) is specified at
run-time during the construction of a <tt>dynamic_bitset</tt>
object, whereas the size of a <tt>std::bitset</tt> is specified
at compile-time through an integer template parameter.</p>
<p>The main problem that <tt>dynamic_bitset</tt> is designed to
solve is that of representing a subset of a finite set. Each bit
represents whether an element of the finite set is in the subset
or not. As such the bitwise operations of
<tt>dynamic_bitset</tt>, such as <tt>operator&</tt> and
<tt>operator|</tt>, correspond to set operations, such as
intersection and union.</p>
<h3><a id ="synopsis">Synopsis</a></h3>
<pre>
namespace boost {
template <typename Block, typename Allocator>
class dynamic_bitset
{
public:
typedef Block <a href="#block_type">block_type</a>;
typedef Allocator <a href="#allocator_type">allocator_type</a>;
typedef <i>implementation-defined</i> <a href="#size_type">size_type</a>;
static const int <a href=
"#bits_per_block">bits_per_block</a> = <i>implementation-defined</i>;
static const size_type <a href=
"#npos">npos</a> = <i>implementation-defined</i>;
class <a href="#reference">reference</a>
{
void operator&(); // not defined
public:
// An automatically generated copy constructor.
reference& operator=(bool value);
reference& operator=(const reference& rhs);
reference& operator|=(bool value);
reference& operator&=(bool value);
reference& operator^=(bool value);
reference& operator-=(bool value);
bool operator~() const;
operator bool() const;
reference& flip();
};
typedef bool <a href="#const_reference">const_reference</a>;
<a href=
"#cons0">dynamic_bitset</a>();
explicit <a href=
"#cons1">dynamic_bitset</a>(const Allocator& alloc);
explicit <a href=
"#cons2">dynamic_bitset</a>(size_type num_bits, unsigned long value = 0,
const Allocator& alloc = Allocator());
template <typename CharT, typename Traits, typename Alloc>
explicit <a href=
"#cons3">dynamic_bitset</a>(const std::basic_string<CharT, Traits, Alloc>& s,
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,
const Allocator& alloc = Allocator());
template <typename BlockInputIterator>
<a href=
"#cons4">dynamic_bitset</a>(BlockInputIterator first, BlockInputIterator last,
const Allocator& alloc = Allocator());
<a href=
"#cons5">dynamic_bitset</a>(const dynamic_bitset& b);
<a href=
"#move-cons">dynamic_bitset</a>(dynamic_bitset&& b);
void <a href="#swap">swap</a>(dynamic_bitset& b);
dynamic_bitset& <a href=
"#assign">operator=</a>(const dynamic_bitset& b);
dynamic_bitset& <a href=
"#move-assign">operator=</a>(dynamic_bitset&& b);
allocator_type <a href="#get_allocator">get_allocator()</a> const;
void <a href=
"#resize">resize</a>(size_type num_bits, bool value = false);
void <a href="#clear">clear</a>();
void <a href="#pop_back">pop_back</a>();
void <a href="#push_back">push_back</a>(bool bit);
void <a href="#append1">append</a>(Block block);
template <typename BlockInputIterator>
void <a href="#append2">append</a>(BlockInputIterator first, BlockInputIterator last);
dynamic_bitset& <a href="#op-and-assign">operator&=</a>(const dynamic_bitset& b);
dynamic_bitset& <a href="#op-or-assign">operator|=</a>(const dynamic_bitset& b);
dynamic_bitset& <a href="#op-xor-assign">operator^=</a>(const dynamic_bitset& b);
dynamic_bitset& <a href="#op-sub-assign">operator-=</a>(const dynamic_bitset& b);
dynamic_bitset& <a href="#op-sl-assign">operator<<=</a>(size_type n);
dynamic_bitset& <a href="#op-sr-assign">operator>>=</a>(size_type n);
dynamic_bitset <a href="#op-sl">operator<<</a>(size_type n) const;
dynamic_bitset <a href="#op-sr">operator>></a>(size_type n) const;
dynamic_bitset& <a href="#set3">set</a>(size_type n, size_type len, bool val);
dynamic_bitset& <a href="#set2">set</a>(size_type n, bool val = true);
dynamic_bitset& <a href="#set1">set</a>();
dynamic_bitset& <a href="#reset3">reset</a>(size_type n, size_type len);
dynamic_bitset& <a href="#reset2">reset</a>(size_type n);
dynamic_bitset& <a href="#reset1">reset</a>();
dynamic_bitset& <a href="#flip3">flip</a>(size_type n, size_type len);
dynamic_bitset& <a href="#flip2">flip</a>(size_type n);
dynamic_bitset& <a href="#flip1">flip</a>();
reference <a href="#at">at</a>(size_type n);
bool <a href="#const-at">at</a>(size_type n) const;
bool <a href="#test">test</a>(size_type n) const;
bool <a href="#test">test_set</a>(size_type n, bool val = true);
bool <a href="#all">all</a>() const;
bool <a href="#any">any</a>() const;
bool <a href="#none">none</a>() const;
dynamic_bitset <a href="#op-not">operator~</a>() const;
size_type <a href="#count">count</a>() const noexcept;
reference <a href="#bracket">operator[]</a>(size_type pos);
bool <a href="#const-bracket">operator[]</a>(size_type pos) const;
unsigned long <a href="#to_ulong">to_ulong</a>() const;
size_type <a href="#size">size</a>() const noexcept;
size_type <a href="#num_blocks">num_blocks</a>() const noexcept;
size_type <a href="#max_size">max_size</a>() const noexcept;
bool <a href="#empty">empty</a>() const noexcept;
size_type <a href="#capacity">capacity</a>() const noexcept;
void <a href="#reserve">reserve</a>(size_type num_bits);
void <a href="#shrink_to_fit">shrink_to_fit</a>();
bool <a href="#is_subset_of">is_subset_of</a>(const dynamic_bitset& a) const;
bool <a href="#is_proper_subset_of">is_proper_subset_of</a>(const dynamic_bitset& a) const;
bool <a href="#intersects">intersects</a>(const dynamic_bitset& a) const;
size_type <a href="#find_first">find_first</a>() const;
size_type <a href="#find_next">find_next</a>(size_type pos) const;
};
template <typename B, typename A>
bool <a href=
"#op-equal">operator==</a>(const dynamic_bitset<B, A>& a, const dynamic_bitset<B, A>& b);
template <typename Block, typename Allocator>
bool <a href=
"#op-not-equal">operator!=</a>(const dynamic_bitset<Block, Allocator>& a, const dynamic_bitset<Block, Allocator>& b);
template <typename B, typename A>
bool <a href=
"#op-less">operator<</a>(const dynamic_bitset<B, A>& a, const dynamic_bitset<B, A>& b);
template <typename Block, typename Allocator>
bool <a href=
"#op-less-equal">operator<=</a>(const dynamic_bitset<Block, Allocator>& a, const dynamic_bitset<Block, Allocator>& b);
template <typename Block, typename Allocator>
bool <a href=
"#op-greater">operator></a>(const dynamic_bitset<Block, Allocator>& a, const dynamic_bitset<Block, Allocator>& b);
template <typename Block, typename Allocator>
bool <a href=
"#op-greater-equal">operator>=</a>(const dynamic_bitset<Block, Allocator>& a, const dynamic_bitset<Block, Allocator>& b);
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>
<a href=
"#op-and">operator&</a>(const dynamic_bitset<Block, Allocator>& b1, const dynamic_bitset<Block, Allocator>& b2);
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>
<a href=
"#op-or">operator|</a>(const dynamic_bitset<Block, Allocator>& b1, const dynamic_bitset<Block, Allocator>& b2);
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>
<a href=
"#op-xor">operator^</a>(const dynamic_bitset<Block, Allocator>& b1, const dynamic_bitset<Block, Allocator>& b2);
template <typename Block, typename Allocator>
dynamic_bitset<Block, Allocator>
<a href=
"#op-sub">operator-</a>(const dynamic_bitset<Block, Allocator>& b1, const dynamic_bitset<Block, Allocator>& b2);
template <typename Block, typename Allocator, typename CharT, typename Alloc>
void <a href=
"#to_string">to_string</a>(const dynamic_bitset<Block, Allocator>& b,
std::basic_string<CharT, Alloc>& s);
template <typename Block, typename Allocator, typename BlockOutputIterator>
void <a href=
"#to_block_range">to_block_range</a>(const dynamic_bitset<Block, Allocator>& b,
BlockOutputIterator result);
template <typename CharT, typename Traits, typename Block, typename Allocator>
std::basic_ostream<CharT, Traits>&
<a href=
"#op-out">operator<<</a>(std::basic_ostream<CharT, Traits>& os, const dynamic_bitset<Block, Allocator>& b);
template <typename CharT, typename Traits, typename Block, typename Allocator>
std::basic_istream<CharT, Traits>&
<a href=
"#op-in">operator>></a>(std::basic_istream<CharT, Traits>& is, dynamic_bitset<Block, Allocator>& b);
} // namespace boost
</pre>
<h3><a id="definitions">Definitions</a></h3>
<p>Each bit represents either the Boolean value true or false (1
or 0). To <i>set</i> a bit is to assign it 1. To <i>clear</i> or
<i>reset</i> a bit is to assign it 0. To <i>flip</i> a bit is to
change the value to 1 if it was 0 and to 0 if it was 1. Each bit
has a non-negative <i>position</i>. A bitset <tt>x</tt> contains
<tt>x.size()</tt> bits, with each bit assigned a unique position
in the range <tt>[0,x.size())</tt>. The bit at position 0 is
called the <i>least significant bit</i> and the bit at position
<tt>size() - 1</tt> is the <i>most significant bit</i>. When
converting an instance of <tt>dynamic_bitset</tt> to or from an
unsigned long <tt>n</tt>, the bit at position <tt>i</tt> of the
bitset has the same value as <tt>(n >> i) & 1</tt>.</p>
<h3><a id="examples">Examples</a></h3>
<p>
<a href="./example/example1.cpp">Example 1</a> (setting
and reading some bits)
</p>
<p>
<a href="./example/example2.cpp">Example 2</a> (creating
some bitsets from integers)
</p>
<p>
<a href="./example/example3.cpp">Example 3</a> (performing
input/output and some bitwise operations).
</p>
<h3><a id="rationale">Rationale</a></h3>
<p>
<tt>dynamic_bitset</tt> is not a <a href=
"https://boost.org/sgi/stl/Container.html">Container</a> and
does not provide iterators for the following reason:
</p>
<ul>
<li>A container with a proxy <tt>reference</tt> type can not
fulfill the container requirements as specified in the C++
standard (unless one resorts to strange iterator semantics).
<tt>std::vector<bool></tt> has a proxy <tt>reference</tt>
type and does not fulfill the container requirements and as a
result has caused many problems. One common problem is when
people try to use iterators from <tt>std::vector<bool></tt>
with a Standard algorithm such as <tt>std::search</tt>. The
<tt>std::search</tt> requirements say that the iterator must be a
<a href=
"https://boost.org/sgi/stl/ForwardIterator.html">Forward
Iterator</a>, but the <tt>std::vector<bool>::iterator</tt>
does not meet this requirement because of the proxy reference.
Depending on the implementation, they may or not be a compile
error or even a run-time error due to this misuse. For further
discussion of the problem see <i>Effective STL</i> by Scott
Meyers). So <tt>dynamic_bitset</tt> tries to avoid these problems
by not pretending to be a container.</li>
</ul>
<p>Some people prefer the name "toggle" to "flip". The name
"flip" was chosen because that is the name used in <a href=
"https://boost.org/sgi/stl/bitset.html"><tt>std::bitset</tt></a>.
In fact, most of the function names for <tt>dynamic_bitset</tt>
were chosen for this reason.</p>
<p><tt>dynamic_bitset</tt> does not throw exceptions when a
precondition is violated (as is done in <tt>std::bitset</tt>).
Instead <tt>assert</tt> is used. See the guidelines for <a href=
"http://www.boost.org/community/error_handling.html">Error and Exception Handling</a>
for the explanation.</p>
<h3><a id="header-files">Header Files</a></h3>
<p>The class <tt>dynamic_bitset</tt> is defined in the header <a
href=
"../../boost/dynamic_bitset.hpp">boost/dynamic_bitset.hpp</a>.
Also, there is a forward declaration for <tt>dynamic_bitset</tt>
in the header <a href=
"../../boost/dynamic_bitset_fwd.hpp">boost/dynamic_bitset_fwd.hpp</a>.</p>
<h3><a id="template-parameters">Template parameters</a></h3>
<table summary=
"Describes the meaning of the template parameters and lists their corresponding default arguments">
<tr>
<th>Parameter</th>
<th>Description</th>
<th>Default</th>
</tr>
<tr>
<td><tt>Block</tt></td>
<td>The integer type in which the bits are stored.</td>
<td><tt>unsigned long</tt></td>
</tr>
<tr>
<td><tt>Allocator</tt></td>
<td>The allocator type used for all internal memory management.</td>
<td><tt>std::allocator<Block></tt></td>
</tr>
</table>
<h3><a id="concepts-modeled">Concepts Modeled</a></h3>
<a href=
"https://boost.org/sgi/stl/Assignable.html">Assignable</a>, <a
href=
"https://boost.org/sgi/stl/DefaultConstructible.html">Default
Constructible</a>, <a href=
"https://boost.org/sgi/stl/EqualityComparable.html">Equality
Comparable</a>, <a href=
"https://boost.org/sgi/stl/LessThanComparable.html">LessThan
Comparable</a>.
<h3><a id="type-requirements">Type requirements</a></h3>
<tt>Block</tt> is an unsigned integer type. <tt>Allocator</tt>
satisfies the Standard requirements for an allocator.
<h3><a id="public-base-classes">Public base classes</a></h3>
None.
<h3><a id="nested-type-names">Nested type names</a></h3>
<hr />
<pre>
<a id="reference">dynamic_bitset::reference</a>
</pre>
<p>A proxy class that acts as a reference to a single bit. It
contains an assignment operator, a conversion to <tt>bool</tt>,
an <tt>operator~</tt>, and a member function <tt>flip</tt>. It
exists only as a helper class for <tt>dynamic_bitset</tt>'s
<tt>operator[]</tt>. The following table describes the valid
operations on the <tt>reference</tt> type. Assume that <tt>b</tt>
is an instance of <tt>dynamic_bitset</tt>, <tt>i, j</tt> are of
<tt>size_type</tt> and in the range <tt>[0,b.size())</tt>. Also,
note that when we write <tt>b[i]</tt> we mean an object of type
<tt>reference</tt> that was initialized from <tt>b[i]</tt>. The
variable <tt>x</tt> is a <tt>bool</tt>.</p>
<table border="1" summary=
"Semantics of several expressions involving usage of dynamic_bitset::reference">
<tr>
<th>Expression</th>
<th>Semantics</th>
</tr>
<tr>
<td><tt>x = b[i]</tt></td>
<td>Assign the ith bit of <tt>b</tt> to <tt>x</tt>.</td>
</tr>
<tr>
<td><tt>(bool)b[i]</tt></td>
<td>Return the ith bit of <tt>b</tt>.</td>
</tr>
<tr>
<td><tt>b[i] = x</tt></td>
<td>Set the ith bit of <tt>b</tt> to the value of <tt>x</tt> and
return <tt>b[i]</tt>.</td>
</tr>
<tr>
<td><tt>b[i] |= x</tt></td>
<td>Or the ith bit of <tt>b</tt> with the value of <tt>x</tt> and
return <tt>b[i]</tt>.</td>
</tr>
<tr>
<td><tt>b[i] &= x</tt></td>
<td>And the ith bit of <tt>b</tt> with the value of <tt>x</tt>
and return <tt>b[i]</tt>.</td>
</tr>
<tr>
<td><tt>b[i] ^= x</tt></td>
<td>Exclusive-Or the ith bit of <tt>b</tt> with the value of
<tt>x</tt> and return <tt>b[i]</tt>.</td>
</tr>
<tr>
<td><tt>b[i] -= x</tt></td>
<td>If <tt>x==true</tt>, clear the ith bit of <tt>b</tt>. Returns
<tt>b[i]</tt>.</td>
</tr>
<tr>
<td><tt>b[i] = b[j]</tt></td>
<td>Set the ith bit of <tt>b</tt> to the value of the jth bit of
<tt>b</tt> and return <tt>b[i]</tt>.</td>
</tr>
<tr>
<td><tt>b[i] |= b[j]</tt></td>
<td>Or the ith bit of <tt>b</tt> with the jth bit of <tt>b</tt>
and return <tt>b[i]</tt>.</td>
</tr>
<tr>
<td><tt>b[i] &= b[j]</tt></td>
<td>And the ith bit of <tt>b</tt> with the jth bit of <tt>b</tt> and return <tt>b[i]</tt>.</td>
</tr>
<tr>
<td><tt>b[i] ^= b[j]</tt></td>
<td>Exclusive-Or the ith bit of <tt>b</tt> with the jth bit of <tt>b</tt> and return <tt>b[i]</tt>.</td>
</tr>
<tr>
<td><tt>b[i] -= b[j]</tt></td>
<td>If the jth bit of <tt>b</tt> is set, clear the ith bit of <tt>b</tt>. Returns <tt>b[i]</tt>.</td>
</tr>
<tr>
<td><tt>x = ~b[i]</tt></td>
<td>Assign the opposite of the ith bit of <tt>b</tt> to <tt>x</tt>.</td>
</tr>
<tr>
<td><tt>(bool)~b[i]</tt></td>
<td>Return the opposite of the ith bit of <tt>b</tt>.</td>
</tr>
<tr>
<td><tt>b[i].flip()</tt></td>
<td>Flip the ith bit of <tt>b</tt> and return <tt>b[i]</tt>.</td>
</tr>
</table>
<hr />
<pre>
<a id="const_reference">dynamic_bitset::const_reference</a>
</pre>
The type <tt>bool</tt>.
<pre>
<a id="size_type">dynamic_bitset::size_type</a>
</pre>
The unsigned integer type for representing the size of the bit set.
<pre>
<a id="block_type">dynamic_bitset::block_type</a>
</pre>
The same type as <tt>Block</tt>.
<pre>
<a id="allocator_type">dynamic_bitset::allocator_type;</a>
</pre>
The same type as <tt>Allocator</tt>.
<hr />
<h3><a id="public-data-members">Public data members</a></h3>
<pre>
<a id="bits_per_block">dynamic_bitset::bits_per_block</a>
</pre>
The number of bits the type <tt>Block</tt> uses to represent values,
excluding any padding bits. Numerically equal
to <tt>std::numeric_limits<Block>::digits</tt>.
<pre>
<a id="npos">dynamic_bitset::npos</a>
</pre>
The maximum value of <tt>size_type</tt>.
<hr />
<h3><a id="constructors">Constructors</a></h3>
<hr />
<pre>
<a id=
"cons0">dynamic_bitset</a>()
</pre>
<b>Effects:</b> Constructs a bitset of size zero. The allocator
for this bitset is a default-constructed object of type
<tt>Allocator</tt>.<br />
<b>Postconditions:</b> <tt>this->size() == 0</tt>.<br />
<b>Throws:</b> Nothing unless the default constructor of
<tt>Allocator</tt> throws an exception.<br />
(Required by <a href=
"https://boost.org/sgi/stl/DefaultConstructible.html">Default
Constructible</a>.)
<hr />
<pre>
<a id=
"cons1">dynamic_bitset</a>(const Allocator& alloc)
</pre>
<b>Effects:</b> Constructs a bitset of size zero. A copy of the
<tt>alloc</tt> object will be used in subsequent bitset
operations such as <tt>resize</tt> to allocate memory.<br />
<b>Postconditions:</b> <tt>this->size() == 0</tt>.<br />
<b>Throws:</b> nothing.
<hr />
<pre>
<a id="cons2">dynamic_bitset</a>(size_type num_bits,
unsigned long value = 0,
const Allocator& alloc = Allocator())
</pre>
<b>Effects:</b> Constructs a bitset from an integer. The first
<tt>M</tt> bits are initialized to the corresponding bits in
<tt>value</tt> and all other bits, if any, to zero (where <tt>M =
min(num_bits, std::numeric_limits<unsigned long>::digits)</tt>). A copy of
the <tt>alloc</tt> object will be used in subsequent bitset
operations such as <tt>resize</tt> to allocate memory. Note that, e.g., the
following
<br /><br />
<tt>
dynamic_bitset b<>( 16, 7 );
</tt><br /><br />
will match the <a href="#cons4">constructor from an iterator range</a> (not this
one), but the underlying implementation will still "do the right thing" and
construct a bitset of 16 bits, from the value 7.
<br />
<b>Postconditions:</b>
<ul>
<li><tt>this->size() == num_bits</tt></li>
<li>For all <tt>i</tt> in the range <tt>[0,M)</tt>,
<tt>(*this)[i] == (value >> i) & 1</tt>.</li>
<li>For all <tt>i</tt> in the range <tt>[M,num_bits)</tt>,
<tt>(*this)[i] == false</tt>.</li>
</ul>
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if
<tt>Allocator=std::allocator</tt>).<br />
<hr />
<pre>
<a id="cons5">dynamic_bitset</a>(const dynamic_bitset& x)
</pre>
<b>Effects:</b> Constructs a bitset that is a copy of the bitset
<tt>x</tt>. The allocator for this bitset is a copy of the
allocator in <tt>x</tt>. <br />
<b>Postconditions:</b> For all <tt>i</tt> in the range
<tt>[0,x.size())</tt>, <tt>(*this)[i] == x[i]</tt>.<br />
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if
<tt>Allocator=std::allocator</tt>).<br />
(Required by <a href=
"https://boost.org/sgi/stl/Assignable.html">Assignable</a>.)
<hr />
<pre>
<a id="move-cons">dynamic_bitset</a>(dynamic_bitset&& x)
</pre>
<b>Effects:</b> Constructs a bitset that is the same as the bitset
<tt>x</tt>, while using the resources from <tt>x</tt>. The allocator
for this bitset is moved from the allocator in <tt>x</tt>. <br />
<b>Postconditions:</b> For all <tt>i</tt> in the range
<tt>[0,x.size())</tt>, <tt>(*this)[i] == x[i]</tt>.<br />
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if
<tt>Allocator=std::allocator</tt>).
<hr />
<pre>
template <typename BlockInputIterator>
explicit
<a id=
"cons4">dynamic_bitset</a>(BlockInputIterator first, BlockInputIterator last,
const Allocator& alloc = Allocator());
</pre>
<b>Effects:</b>
<ul>
<li>
If this constructor is called with a type <tt>BlockInputIterator</tt> which
<i>is actually an integral type</i>, the library behaves as if the constructor
from <tt>unsigned long</tt> were called, with arguments
<tt>static_cast<size_type>(first), last and alloc</tt>, in that order.
<br /><br />
Example:
<pre>
// b is constructed as if by calling the constructor
//
// dynamic_bitset(size_type num_bits,
// unsigned long value = 0,
// const Allocator& alloc = Allocator())
//
// with arguments
//
// static_cast<dynamic_bitset<unsigned short>::size_type>(8),
// 7,
// Allocator()
//
dynamic_bitset<unsigned short> b(8, 7);
</pre><br />
<i>Note:</i><br/>
At the time of writing (October 2008) this is aligned with the
proposed resolution for <a href=
"http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#438">library
issue 438</a>. That is a <em>post <tt>C++03</tt></em>
change, and is currently in the working paper for
<tt>C++0x</tt>. Informally speaking, the critical changes with
respect to <tt>C++03</tt> are the drop of a <tt>static_cast</tt>
on the second argument, and more leeway as to <em>when</em> the
templated constructor should have the same effect as the (size,
value) one: only when <tt>InputIterator</tt> is an integral
type, in <tt>C++03</tt>; when it is either an integral type or
any other type that the implementation might detect as
impossible to be an input iterator, with the proposed
resolution. For the purposes of <tt>dynamic_bitset</tt> we limit
ourselves to the first of these two changes.<br /><br />
</li>
<li>
<i>Otherwise</i> (<i>i.e.</i> if the template argument is not an
integral type), constructs—under the condition in the
<tt>requires</tt> clause—a bitset based on a range of
blocks. Let <tt>*first</tt> be block number 0, <tt>*++first</tt>
block number 1, etc. Block number <tt>b</tt> is used to
initialize the bits of the dynamic_bitset in the position range
<tt>[b*bits_per_block, (b+1)*bits_per_block)</tt>. For each
block number <tt>b</tt> with value <tt>bval</tt>, the bit
<tt>(bval >> i) & 1</tt> corresponds to the bit at
position <tt>(b * bits_per_block + i)</tt> in the bitset (where
<tt>i</tt> goes through the range <tt>[0, bits_per_block)</tt>).
</li>
</ul>
<br />
<b>Requires:</b> <tt>BlockInputIterator</tt> must be either an
integral type or a model of <a href=
"https://boost.org/sgi/stl/InputIterator.html">Input
Iterator</a> whose <tt>value_type</tt> is the same type as
<tt>Block</tt>.<br /> <b>Throws:</b> An allocation error if
memory is exhausted (<tt>std::bad_alloc</tt> if
<tt>Allocator=std::allocator</tt>).<br />
<hr />
<pre>
template<typename Char, typename Traits, typename Alloc>
explicit
<a id="cons3">dynamic_bitset</a>(const <a href=
"https://boost.org/sgi/stl/basic_string.html">std::basic_string</a><Char,Traits,Alloc>& s,
typename std::basic_string<CharT, Traits, Alloc>::size_type pos = 0,
typename std::basic_string<CharT, Traits, Alloc>::size_type n = <a
href=
"https://boost.org/sgi/stl/basic_string.html">std::basic_string</a><Char,Traits,Alloc>::npos,
const Allocator& alloc = Allocator())
</pre>
<b>Precondition:</b> <tt>pos <= s.size()</tt> and the
characters used to initialize the bits must be <tt>0</tt> or
<tt>1</tt>.<br />
<b>Effects:</b> Constructs a bitset from a string of 0's and
1's. The first <tt>M</tt> bits are initialized to the
corresponding characters in <tt>s</tt>, where <tt>M =
min(s.size() - pos, n)</tt>. Note that the <i>highest</i>
character position in <tt>s</tt>, not the lowest, corresponds to
the least significant bit. That is, character position <tt>pos +
M - 1 - i</tt> corresponds to bit <tt>i</tt>. So, for example,
<tt>dynamic_bitset(string("1101"))</tt> is the same as
<tt>dynamic_bitset(13ul)</tt>.<br />
<b>Throws:</b> an allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>).
<hr />
<h3><a id="destructor">Destructor</a></h3>
<hr />
<pre>
~dynamic_bitset()
</pre>
<b>Effects:</b> Releases the memory associated with this bitset
and destroys the bitset object itself.<br />
<b>Throws:</b> nothing.
<hr />
<h3><a id="member-functions">Member Functions</a></h3>
<hr />
<pre>
void <a id="swap">swap</a>(dynamic_bitset& b);
</pre>
<b>Effects:</b> The contents of this bitset and bitset <tt>b</tt>
are exchanged.<br />
<b>Postconditions:</b> This bitset is equal to the original
<tt>b</tt>, and <tt>b</tt> is equal to the previous version of
this bitset.<br />
<b>Throws:</b> nothing.
<hr />
<pre>
dynamic_bitset& <a id=
"assign">operator=</a>(const dynamic_bitset& x)
</pre>
<b>Effects:</b> This bitset becomes a copy of the bitset
<tt>x</tt>.<br />
<b>Postconditions:</b> For all <tt>i</tt> in the range
<tt>[0,x.size())</tt>, <tt>(*this)[i] == x[i]</tt>.<br />
<b>Returns:</b> <tt>*this</tt>.<br />
<b>Throws:</b> nothing. <br />
(Required by <a href=
"https://boost.org/sgi/stl/Assignable.html">Assignable</a>.)
<hr />
<pre>
dynamic_bitset& <a id=
"move-assign">operator=</a>(dynamic_bitset&& x)
</pre>
<b>Effects:</b> This bitset becomes the same as the bitset
<tt>x</tt>, while using the resources from <tt>x</tt>.<br />
<b>Postconditions:</b> For all <tt>i</tt> in the range
<tt>[0,x.size())</tt>, <tt>(*this)[i] == x[i]</tt>.<br />
<b>Returns:</b> <tt>*this</tt>.<br />
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>).
<hr />
<pre>
allocator_type <a id="get_allocator">get_allocator()</a> const;
</pre>
<b>Returns:</b> A copy of the allocator object used to construct <tt>*this</tt>.
<hr />
<pre>
void <a id=
"resize">resize</a>(size_type num_bits, bool value = false);
</pre>
<b>Effects:</b> Changes the number of bits of the bitset to
<tt>num_bits</tt>. If <tt>num_bits > size()</tt> then the bits
in the range <tt>[0,size())</tt> remain the same, and the bits in
<tt>[size(),num_bits)</tt> are all set to <tt>value</tt>. If
<tt>num_bits < size()</tt> then the bits in the range
<tt>[0,num_bits)</tt> stay the same (and the remaining bits are
discarded).<br />
<b>Postconditions:</b> <tt>this->size() == num_bits</tt>.<br />
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if
<tt>Allocator=std::allocator</tt>).<br />
<hr />
<pre>
void <a id="clear">clear</a>()
</pre>
<b>Effects:</b> The size of the bitset becomes zero.<br />
<b>Throws:</b> nothing.
<hr />
<pre>
void <a id="pop_back">pop_back</a>();
</pre>
<b>Precondition:</b> <tt>!this->empty()</tt>.<br />
<b>Effects:</b> Decreases the size of the bitset by one.<br />
<b>Throws:</b> nothing.
<hr />
<pre>
void <a id="push_back">push_back</a>(bool value);
</pre>
<b>Effects:</b> Increases the size of the bitset by one, and sets
the value of the new most-significant bit to <tt>value</tt>.<br />
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if
<tt>Allocator=std::allocator</tt>).<br />
<hr />
<pre>
void <a id="append1">append</a>(Block value);
</pre>
<b>Effects:</b> Appends the bits in <tt>value</tt> to the bitset
(appends to the most-significant end). This increases the size of
the bitset by <tt>bits_per_block</tt>. Let <tt>s</tt> be the old
size of the bitset, then for <tt>i</tt> in the range
<tt>[0,bits_per_block)</tt>, the bit at position <tt>(s + i)</tt>
is set to <tt>((value >> i) & 1)</tt>.<br />
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if
<tt>Allocator=std::allocator</tt>).<br />
<hr />
<pre>
template <typename BlockInputIterator>
void <a id=
"append2">append</a>(BlockInputIterator first, BlockInputIterator last);
</pre>
<b>Effects:</b> This function provides the same end result as the
following code, but is typically more efficient.
<pre>
for (; first != last; ++first)
append(*first);
</pre>
<b>Requires:</b> The <tt>BlockInputIterator</tt> type must be a
model of <a href=
"https://boost.org/sgi/stl/InputIterator.html">Input
Iterator</a> and the <tt>value_type</tt> must be the same type as
<tt>Block</tt>.<br />
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if
<tt>Allocator=std::allocator</tt>).<br />
<hr />
<pre>
dynamic_bitset& <a id=
"op-and-assign">operator&=</a>(const dynamic_bitset& rhs)
</pre>
<b>Requires:</b> <tt>this->size() == rhs.size()</tt>.<br />
<b>Effects:</b> Bitwise-AND all the bits in <tt>rhs</tt> with
the bits in this bitset. This is equivalent to:
<pre>
for (size_type i = 0; i != this->size(); ++i)
(*this)[i] = (*this)[i] & rhs[i];
</pre>
<b>Returns:</b> <tt>*this</tt>.<br />
<b>Throws:</b> nothing.
<hr />
<pre>
dynamic_bitset& <a id=
"op-or-assign">operator|=</a>(const dynamic_bitset& rhs)
</pre>
<b>Requires:</b> <tt>this->size() == rhs.size()</tt>.<br />
<b>Effects:</b> Bitwise-OR's all the bits in <tt>rhs</tt> with
the bits in this bitset. This is equivalent to:
<pre>
for (size_type i = 0; i != this->size(); ++i)
(*this)[i] = (*this)[i] | rhs[i];
</pre>
<b>Returns:</b> <tt>*this</tt>.<br />
<b>Throws:</b> nothing.
<hr />
<pre>
dynamic_bitset& <a id=
"op-xor-assign">operator^=</a>(const dynamic_bitset& rhs)
</pre>
<b>Requires:</b> <tt>this->size() == rhs.size()</tt>.<br />
<b>Effects:</b> Bitwise-XOR's all the bits in <tt>rhs</tt> with
the bits in this bitset. This is equivalent to:
<pre>
for (size_type i = 0; i != this->size(); ++i)
(*this)[i] = (*this)[i] ^ rhs[i];
</pre>
<b>Returns:</b> <tt>*this</tt>.<br />
<b>Throws:</b> nothing.
<hr />
<pre>
dynamic_bitset& <a id=
"op-sub-assign">operator-=</a>(const dynamic_bitset& rhs)
</pre>
<b>Requires:</b> <tt>this->size() == rhs.size()</tt>.<br />
<b>Effects:</b> Computes the set difference of this bitset and
the <tt>rhs</tt> bitset. This is equivalent to:
<pre>
for (size_type i = 0; i != this->size(); ++i)
(*this)[i] = (*this)[i] && !rhs[i];
</pre>
<b>Returns:</b> <tt>*this</tt>.<br />
<b>Throws:</b> nothing.
<hr />
<pre>
dynamic_bitset& <a id=
"op-sl-assign">operator<<=</a>(size_type n)
</pre>