1 <?xml version="1.0" encoding="ascii"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
7 Copyright (c) 2001 Jeremy Siek
8 Copyright (c) 2003-2004, 2008 Gennaro Prota
10 Distributed under the Boost Software License, Version 1.0.
11 (See accompanying file LICENSE_1_0.txt or copy at
12 http://www.boost.org/LICENSE_1_0.txt)
16 Copyright (c) 1996-1999
17 Silicon Graphics Computer Systems, Inc.
19 Permission to use, copy, modify, distribute and sell this software
20 and its documentation for any purpose is hereby granted without fee,
21 provided that the above copyright notice appears in all copies and
22 that both that copyright notice and this permission notice appear
23 in supporting documentation. Silicon Graphics makes no
24 representations about the suitability of this software for any
25 purpose. It is provided "as is" without express or implied warranty.
28 Hewlett-Packard Company
30 Permission to use, copy, modify, distribute and sell this software
31 and its documentation for any purpose is hereby granted without fee,
32 provided that the above copyright notice appears in all copies and
33 that both that copyright notice and this permission notice appear
34 in supporting documentation. Hewlett-Packard Company makes no
35 representations about the suitability of this software for any
36 purpose. It is provided "as is" without express or implied warranty.
40 <title>dynamic_bitset<Block, Allocator></title>
41 <link rel="stylesheet" type="text/css" href="../../rst.css" />
48 <div class="section" id="docs">
49 <div class="section-0">
50 <div class="section-body">
52 <div id="boost-logo"><img src="../../boost.png" alt="Boost C++ Libraries" /></div>
53 <h1>dynamic_bitset<Block, Allocator></h1>
57 <dt><a href="#description">Description</a></dt>
58 <dt><a href="#synopsis">Synopsis</a></dt>
59 <dt><a href="#definitions">Definitions</a></dt>
60 <dt><a href="#examples">Examples</a></dt>
61 <dt><a href="#rationale">Rationale</a></dt>
62 <dt><a href="#header-files">Header Files</a></dt>
63 <dt><a href="#template-parameters">Template Parameters</a></dt>
64 <dt><a href="#concepts-modeled">Concepts modeled</a></dt>
66 <dt><a href="#type-requirements">Type requirements</a></dt>
67 <dt><a href="#public-base-classes">Public base classes</a></dt>
68 <dt><a href="#nested-type-names">Nested type names</a></dt>
69 <dt><a href="#public-data-members">Public data members</a></dt>
70 <dt><a href="#constructors">Constructors</a></dt>
71 <dt><a href="#destructor">Destructor</a></dt>
72 <dt><a href="#member-functions">Member functions</a></dt>
73 <dt><a href="#non-member-functions">Non-member functions</a></dt>
74 <dt><a href="#exception-guarantees">Exception guarantees</a></dt>
76 <dt><a href="#changes-from-previous-ver"><b>Changes from previous version(s)</b></a></dt>
77 <dt><a href="#see-also">See also</a></dt>
78 <dt><a href="#acknowledgements">Acknowledgements</a></dt>
80 <h3><a id="description">Description</a></h3>
81 <p>The <tt>dynamic_bitset</tt> class represents a set of bits. It
82 provides accesses to the value of individual bits via an
83 <tt>operator[]</tt> and provides all of the bitwise operators
84 that one can apply to builtin integers, such as
85 <tt>operator&</tt> and <tt>operator<<</tt>. The number
86 of bits in the set is specified at runtime via a parameter to the
87 constructor of the <tt>dynamic_bitset</tt>.</p>
89 <p>The <tt>dynamic_bitset</tt> class is nearly identical to the
91 "http://www.sgi.com/tech/stl/bitset.html"><tt>std::bitset</tt></a>
92 class. The difference is that the size of the
93 <tt>dynamic_bitset</tt> (the number of bits) is specified at
94 run-time during the construction of a <tt>dynamic_bitset</tt>
95 object, whereas the size of a <tt>std::bitset</tt> is specified
96 at compile-time through an integer template parameter.</p>
98 <p>The main problem that <tt>dynamic_bitset</tt> is designed to
99 solve is that of representing a subset of a finite set. Each bit
100 represents whether an element of the finite set is in the subset
101 or not. As such the bitwise operations of
102 <tt>dynamic_bitset</tt>, such as <tt>operator&</tt> and
103 <tt>operator|</tt>, correspond to set operations, such as
104 intersection and union.</p>
105 <h3><a id ="synopsis">Synopsis</a></h3>
109 template <typename Block, typename Allocator>
113 typedef Block <a href="#block_type">block_type</a>;
114 typedef Allocator <a href="#allocator_type">allocator_type</a>;
115 typedef <i>implementation-defined</i> <a href="#size_type">size_type</a>;
117 static const int <a href=
118 "#bits_per_block">bits_per_block</a> = <i>implementation-defined</i>;
119 static const size_type <a href=
120 "#npos">npos</a> = <i>implementation-defined</i>;
122 class <a href="#reference">reference</a>
124 void operator&(); // not defined
127 // An automatically generated copy constructor.
129 reference& operator=(bool value);
130 reference& operator=(const reference& rhs);
132 reference& operator|=(bool value);
133 reference& operator&=(bool value);
134 reference& operator^=(bool value);
135 reference& operator-=(bool value);
137 bool operator~() const;
138 operator bool() const;
139 reference& flip();
142 typedef bool <a href="#const_reference">const_reference</a>;
145 "#cons1">dynamic_bitset</a>(const Allocator& alloc = Allocator());
148 "#cons2">dynamic_bitset</a>(size_type num_bits, unsigned long value = 0,
149 const Allocator& alloc = Allocator());
151 template <typename CharT, typename Traits, typename Alloc>
153 "#cons3">dynamic_bitset</a>(const std::basic_string<CharT, Traits, Alloc>& s,
154 typename std::basic_string<CharT, Traits, Alloc>::size_type pos = 0,
155 typename std::basic_string<CharT, Traits, Alloc>::size_type n = std::basic_string<CharT, Traits, Alloc>::npos,
156 const Allocator& alloc = Allocator());
158 template <typename BlockInputIterator>
160 "#cons4">dynamic_bitset</a>(BlockInputIterator first, BlockInputIterator last,
161 const Allocator& alloc = Allocator());
164 "#cons5">dynamic_bitset</a>(const dynamic_bitset& b);
166 void <a href="#swap">swap</a>(dynamic_bitset& b);
168 dynamic_bitset& <a href=
169 "#assign">operator=</a>(const dynamic_bitset& b);
171 allocator_type <a href="#get_allocator">get_allocator()</a> const;
174 "#resize">resize</a>(size_type num_bits, bool value = false);
175 void <a href="#clear">clear</a>();
176 void <a href="#push_back">push_back</a>(bool bit);
177 void <a href="#append1">append</a>(Block block);
179 template <typename BlockInputIterator>
180 void <a href="#append2">append</a>(BlockInputIterator first, BlockInputIterator last);
182 dynamic_bitset& <a href="#op-and-assign">operator&=</a>(const dynamic_bitset& b);
183 dynamic_bitset& <a href="#op-or-assign">operator|=</a>(const dynamic_bitset& b);
184 dynamic_bitset& <a href="#op-xor-assign">operator^=</a>(const dynamic_bitset& b);
185 dynamic_bitset& <a href="#op-sub-assign">operator-=</a>(const dynamic_bitset& b);
186 dynamic_bitset& <a href="#op-sl-assign">operator<<=</a>(size_type n);
187 dynamic_bitset& <a href="#op-sr-assign">operator>>=</a>(size_type n);
188 dynamic_bitset <a href="#op-sl">operator<<</a>(size_type n) const;
189 dynamic_bitset <a href="#op-sr">operator>></a>(size_type n) const;
191 dynamic_bitset& <a href="#set2">set</a>(size_type n, bool val = true);
192 dynamic_bitset& <a href="#set1">set</a>();
193 dynamic_bitset& <a href="#reset2">reset</a>(size_type n);
194 dynamic_bitset& <a href="#reset1">reset</a>();
195 dynamic_bitset& <a href="#flip2">flip</a>(size_type n);
196 dynamic_bitset& <a href="#flip1">flip</a>();
197 bool <a href="#test">test</a>(size_type n) const;
198 bool <a href="#any">any</a>() const;
199 bool <a href="#none">none</a>() const;
200 dynamic_bitset <a href="#op-not">operator~</a>() const;
201 size_type <a href="#count">count</a>() const;
203 reference <a href="#bracket">operator[]</a>(size_type pos);
204 bool <a href="#const-bracket">operator[]</a>(size_type pos) const;
206 unsigned long <a href="#to_ulong">to_ulong</a>() const;
208 size_type <a href="#size">size</a>() const;
209 size_type <a href="#num_blocks">num_blocks</a>() const;
210 size_type <a href="#max_size">max_size</a>() const;
211 bool <a href="#empty">empty</a>() const;
213 bool <a href="#is_subset_of">is_subset_of</a>(const dynamic_bitset& a) const;
214 bool <a href="#is_proper_subset_of">is_proper_subset_of</a>(const dynamic_bitset& a) const;
215 bool <a href="#intersects">intersects</a>(const dynamic_bitset& a) const;
217 size_type <a href="#find_first">find_first</a>() const;
218 size_type <a href="#find_next">find_next</a>(size_type pos) const;
223 template <typename B, typename A>
225 "#op-equal">operator==</a>(const dynamic_bitset<B, A>& a, const dynamic_bitset<B, A>& b);
227 template <typename Block, typename Allocator>
229 "#op-not-equal">operator!=</a>(const dynamic_bitset<Block, Allocator>& a, const dynamic_bitset<Block, Allocator>& b);
231 template <typename B, typename A>
233 "#op-less">operator<</a>(const dynamic_bitset<B, A>& a, const dynamic_bitset<B, A>& b);
235 template <typename Block, typename Allocator>
237 "#op-less-equal">operator<=</a>(const dynamic_bitset<Block, Allocator>& a, const dynamic_bitset<Block, Allocator>& b);
239 template <typename Block, typename Allocator>
241 "#op-greater">operator></a>(const dynamic_bitset<Block, Allocator>& a, const dynamic_bitset<Block, Allocator>& b);
243 template <typename Block, typename Allocator>
245 "#op-greater-equal">operator>=</a>(const dynamic_bitset<Block, Allocator>& a, const dynamic_bitset<Block, Allocator>& b);
247 template <typename Block, typename Allocator>
248 dynamic_bitset<Block, Allocator>
250 "#op-and">operator&</a>(const dynamic_bitset<Block, Allocator>& b1, const dynamic_bitset<Block, Allocator>& b2);
252 template <typename Block, typename Allocator>
253 dynamic_bitset<Block, Allocator>
255 "#op-or">operator|</a>(const dynamic_bitset<Block, Allocator>& b1, const dynamic_bitset<Block, Allocator>& b2);
257 template <typename Block, typename Allocator>
258 dynamic_bitset<Block, Allocator>
260 "#op-xor">operator^</a>(const dynamic_bitset<Block, Allocator>& b1, const dynamic_bitset<Block, Allocator>& b2);
262 template <typename Block, typename Allocator>
263 dynamic_bitset<Block, Allocator>
265 "#op-sub">operator-</a>(const dynamic_bitset<Block, Allocator>& b1, const dynamic_bitset<Block, Allocator>& b2);
267 template <typename Block, typename Allocator, typename CharT, typename Alloc>
269 "#to_string">to_string</a>(const dynamic_bitset<Block, Allocator>& b,
270 std::basic_string<CharT, Alloc>& s);
272 template <typename Block, typename Allocator, typename BlockOutputIterator>
274 "#to_block_range">to_block_range</a>(const dynamic_bitset<Block, Allocator>& b,
275 BlockOutputIterator result);
277 template <typename CharT, typename Traits, typename Block, typename Allocator>
278 std::basic_ostream<CharT, Traits>&
280 "#op-out">operator<<</a>(std::basic_ostream<CharT, Traits>& os, const dynamic_bitset<Block, Allocator>& b);
282 template <typename CharT, typename Traits, typename Block, typename Allocator>
283 std::basic_istream<CharT, Traits>&
285 "#op-in">operator>></a>(std::basic_istream<CharT, Traits>& is, dynamic_bitset<Block, Allocator>& b);
290 <h3><a id="definitions">Definitions</a></h3>
292 <p>Each bit represents either the Boolean value true or false (1
293 or 0). To <i>set</i> a bit is to assign it 1. To <i>clear</i> or
294 <i>reset</i> a bit is to assign it 0. To <i>flip</i> a bit is to
295 change the value to 1 if it was 0 and to 0 if it was 1. Each bit
296 has a non-negative <i>position</i>. A bitset <tt>x</tt> contains
297 <tt>x.size()</tt> bits, with each bit assigned a unique position
298 in the range <tt>[0,x.size())</tt>. The bit at position 0 is
299 called the <i>least significant bit</i> and the bit at position
300 <tt>size() - 1</tt> is the <i>most significant bit</i>. When
301 converting an instance of <tt>dynamic_bitset</tt> to or from an
302 unsigned long <tt>n</tt>, the bit at position <tt>i</tt> of the
303 bitset has the same value as <tt>(n >> i) & 1</tt>.</p>
305 <h3><a id="examples">Examples</a></h3>
308 <a href="./example/example1.cpp">Example 1</a> (setting
309 and reading some bits)
312 <a href="./example/example2.cpp">Example 2</a> (creating
313 some bitsets from integers)
317 <a href="./example/example3.cpp">Example 3</a> (performing
318 input/output and some bitwise operations).
322 <h3><a id="rationale">Rationale</a></h3>
324 <tt>dynamic_bitset</tt> is not a <a href=
325 "http://www.sgi.com/tech/stl/Container.html">Container</a> and
326 does not provide iterators for the following reason:
330 <li>A container with a proxy <tt>reference</tt> type can not
331 fulfill the container requirements as specified in the C++
332 standard (unless one resorts to strange iterator semantics).
333 <tt>std::vector<bool></tt> has a proxy <tt>reference</tt>
334 type and does not fulfill the container requirements and as a
335 result has caused many problems. One common problem is when
336 people try to use iterators from <tt>std::vector<bool></tt>
337 with a Standard algorithm such as <tt>std::search</tt>. The
338 <tt>std::search</tt> requirements say that the iterator must be a
340 "http://www.sgi.com/tech/stl/ForwardIterator.html">Forward
341 Iterator</a>, but the <tt>std::vector<bool>::iterator</tt>
342 does not meet this requirement because of the proxy reference.
343 Depending on the implementation, they may or not be a compile
344 error or even a run-time error due to this misuse. For further
345 discussion of the problem see <i>Effective STL</i> by Scott
346 Meyers). So <tt>dynamic_bitset</tt> tries to avoid these problems
347 by not pretending to be a container.</li>
350 <p>Some people prefer the name "toggle" to "flip". The name
351 "flip" was chosen because that is the name used in <a href=
352 "http://www.sgi.com/tech/stl/bitset.html"><tt>std::bitset</tt></a>.
353 In fact, most of the function names for <tt>dynamic_bitset</tt>
354 were chosen for this reason.</p>
356 <p><tt>dynamic_bitset</tt> does not throw exceptions when a
357 precondition is violated (as is done in <tt>std::bitset</tt>).
358 Instead <tt>assert</tt> is used. See the guidelines for <a href=
359 "http://www.boost.org/more/error_handling.html">Error and Exception Handling</a>
360 for the explanation.</p>
362 <h3><a id="header-files">Header Files</a></h3>
364 <p>The class <tt>dynamic_bitset</tt> is defined in the header <a
366 "../../boost/dynamic_bitset.hpp">boost/dynamic_bitset.hpp</a>.
367 Also, there is a forward declaration for <tt>dynamic_bitset</tt>
368 in the header <a href=
369 "../../boost/dynamic_bitset_fwd.hpp">boost/dynamic_bitset_fwd.hpp</a>.</p>
371 <h3><a id="template-parameters">Template parameters</a></h3>
374 "Describes the meaning of the template parameters and lists their corresponding default arguments">
381 <td><tt>Block</tt></td>
382 <td>The integer type in which the bits are stored.</td>
383 <td><tt>unsigned long</tt></td>
387 <td><tt>Allocator</tt></td>
388 <td>The allocator type used for all internal memory management.</td>
389 <td><tt>std::allocator<Block></tt></td>
392 <h3><a id="concepts-modeled">Concepts Modeled</a></h3>
394 "http://www.sgi.com/tech/stl/Assignable.html">Assignable</a>, <a
396 "http://www.sgi.com/tech/stl/DefaultConstructible.html">Default
397 Constructible</a>, <a href=
398 "http://www.sgi.com/tech/stl/EqualityComparable.html">Equality
399 Comparable</a>, <a href=
400 "http://www.sgi.com/tech/stl/LessThanComparable.html">LessThan
403 <h3><a id="type-requirements">Type requirements</a></h3>
405 <tt>Block</tt> is an unsigned integer type. <tt>Allocator</tt>
406 satisfies the Standard requirements for an allocator.
408 <h3><a id="public-base-classes">Public base classes</a></h3>
411 <h3><a id="nested-type-names">Nested type names</a></h3>
415 <a id="reference">dynamic_bitset::reference</a>
418 <p>A proxy class that acts as a reference to a single bit. It
419 contains an assignment operator, a conversion to <tt>bool</tt>,
420 an <tt>operator~</tt>, and a member function <tt>flip</tt>. It
421 exists only as a helper class for <tt>dynamic_bitset</tt>'s
422 <tt>operator[]</tt>. The following table describes the valid
423 operations on the <tt>reference</tt> type. Assume that <tt>b</tt>
424 is an instance of <tt>dynamic_bitset</tt>, <tt>i, j</tt> are of
425 <tt>size_type</tt> and in the range <tt>[0,b.size())</tt>. Also,
426 note that when we write <tt>b[i]</tt> we mean an object of type
427 <tt>reference</tt> that was initialized from <tt>b[i]</tt>. The
428 variable <tt>x</tt> is a <tt>bool</tt>.</p>
430 <table border="1" summary=
431 "Semantics of several expressions involving usage of dynamic_bitset::reference">
437 <td><tt>x = b[i]</tt></td>
438 <td>Assign the ith bit of <tt>b</tt> to <tt>x</tt>.</td>
441 <td><tt>(bool)b[i]</tt></td>
442 <td>Return the ith bit of <tt>b</tt>.</td>
445 <td><tt>b[i] = x</tt></td>
446 <td>Set the ith bit of <tt>b</tt> to the value of <tt>x</tt> and
447 return <tt>b[i]</tt>.</td>
450 <td><tt>b[i] |= x</tt></td>
451 <td>Or the ith bit of <tt>b</tt> with the value of <tt>x</tt> and
452 return <tt>b[i]</tt>.</td>
455 <td><tt>b[i] &= x</tt></td>
456 <td>And the ith bit of <tt>b</tt> with the value of <tt>x</tt>
457 and return <tt>b[i]</tt>.</td>
461 <td><tt>b[i] ^= x</tt></td>
462 <td>Exclusive-Or the ith bit of <tt>b</tt> with the value of
463 <tt>x</tt> and return <tt>b[i]</tt>.</td>
467 <td><tt>b[i] -= x</tt></td>
468 <td>If <tt>x==true</tt>, clear the ith bit of <tt>b</tt>. Returns
473 <td><tt>b[i] = b[j]</tt></td>
474 <td>Set the ith bit of <tt>b</tt> to the value of the jth bit of
475 <tt>b</tt> and return <tt>b[i]</tt>.</td>
479 <td><tt>b[i] |= b[j]</tt></td>
480 <td>Or the ith bit of <tt>b</tt> with the jth bit of <tt>b</tt>
481 and return <tt>b[i]</tt>.</td>
485 <td><tt>b[i] &= b[j]</tt></td>
486 <td>And the ith bit of <tt>b</tt> with the jth bit of <tt>b</tt> and return <tt>b[i]</tt>.</td>
489 <td><tt>b[i] ^= b[j]</tt></td>
490 <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>
494 <td><tt>b[i] -= b[j]</tt></td>
495 <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>
498 <td><tt>x = ~b[i]</tt></td>
500 <td>Assign the opposite of the ith bit of <tt>b</tt> to <tt>x</tt>.</td>
503 <td><tt>(bool)~b[i]</tt></td>
504 <td>Return the opposite of the ith bit of <tt>b</tt>.</td>
508 <td><tt>b[i].flip()</tt></td>
509 <td>Flip the ith bit of <tt>b</tt> and return <tt>b[i]</tt>.</td>
514 <a id="const_reference">dynamic_bitset::const_reference</a>
516 The type <tt>bool</tt>.
519 <a id="size_type">dynamic_bitset::size_type</a>
521 The unsigned integer type for representing the size of the bit set.
524 <a id="block_type">dynamic_bitset::block_type</a>
526 The same type as <tt>Block</tt>.
529 <a id="allocator_type">dynamic_bitset::allocator_type;</a>
531 The same type as <tt>Allocator</tt>.
535 <h3><a id="public-data-members">Public data members</a></h3>
538 <a id="bits_per_block">dynamic_bitset::bits_per_block</a>
540 The number of bits the type <tt>Block</tt> uses to represent values,
541 excluding any padding bits. Numerically equal
542 to <tt>std::numeric_limits<Block>::digits</tt>.
545 <a id="npos">dynamic_bitset::npos</a>
547 The maximum value of <tt>size_type</tt>.
550 <h3><a id="constructors">Constructors</a></h3>
555 "cons1">dynamic_bitset</a>(const Allocator& alloc = Allocator())
558 <b>Effects:</b> Constructs a bitset of size zero. A copy of the
559 <tt>alloc</tt> object will be used in subsequent bitset
560 operations such as <tt>resize</tt> to allocate memory.<br />
561 <b>Postconditions:</b> <tt>this->size() == 0</tt>.<br />
562 <b>Throws:</b> An allocation error if memory is exhausted
563 (<tt>std::bad_alloc</tt> if
564 <tt>Allocator=std::allocator</tt>).<br />
565 (Required by <a href=
566 "http://www.sgi.com/tech/stl/DefaultConstructible.html">Default
571 <a id="cons2">dynamic_bitset</a>(size_type num_bits,
572 unsigned long value = 0,
573 const Allocator& alloc = Allocator())
576 <b>Effects:</b> Constructs a bitset from an integer. The first
577 <tt>M</tt> bits are initialized to the corresponding bits in
578 <tt>value</tt> and all other bits, if any, to zero (where <tt>M =
579 min(num_bits, std::numeric_limits<unsigned long>::digits)</tt>). A copy of
580 the <tt>alloc</tt> object will be used in subsequent bitset
581 operations such as <tt>resize</tt> to allocate memory. Note that, e.g., the
585 dynamic_bitset b<>( 16, 7 );
587 will match the <a href="#cons4">constructor from an iterator range</a> (not this
588 one), but the underlying implementation will still "do the right thing" and
589 construct a bitset of 16 bits, from the value 7.
591 <b>Postconditions:</b>
594 <li><tt>this->size() == num_bits</tt></li>
596 <li>For all <tt>i</tt> in the range <tt>[0,M)</tt>,
597 <tt>(*this)[i] == (value >> i) & 1</tt>.</li>
599 <li>For all <tt>i</tt> in the range <tt>[M,num_bits)</tt>,
600 <tt>(*this)[i] == false</tt>.</li>
603 <b>Throws:</b> An allocation error if memory is exhausted
604 (<tt>std::bad_alloc</tt> if
605 <tt>Allocator=std::allocator</tt>).<br />
610 <a id="cons5">dynamic_bitset</a>(const dynamic_bitset& x)
613 <b>Effects:</b> Constructs a bitset that is a copy of the bitset
614 <tt>x</tt>. The allocator for this bitset is a copy of the
615 allocator in <tt>x</tt>. <br />
616 <b>Postconditions:</b> For all <tt>i</tt> in the range
617 <tt>[0,x.size())</tt>, <tt>(*this)[i] == x[i]</tt>.<br />
618 <b>Throws:</b> An allocation error if memory is exhausted
619 (<tt>std::bad_alloc</tt> if
620 <tt>Allocator=std::allocator</tt>).<br />
621 (Required by <a href=
622 "http://www.sgi.com/tech/stl/Assignable.html">Assignable</a>.)
626 template <typename BlockInputIterator>
629 "cons4">dynamic_bitset</a>(BlockInputIterator first, BlockInputIterator last,
630 const Allocator& alloc = Allocator());
636 If this constructor is called with a type <tt>BlockInputIterator</tt> which
637 <i>is actually an integral type</i>, the library behaves as if the constructor
638 from <tt>unsigned long</tt> were called, with arguments
639 <tt>static_cast<size_type>(first), last and alloc</tt>, in that order.
643 // b is constructed as if by calling the constructor
645 // dynamic_bitset(size_type num_bits,
646 // unsigned long value = 0,
647 // const Allocator& alloc = Allocator())
651 // static_cast<dynamic_bitset<unsigned short>::size_type>(8),
655 dynamic_bitset<unsigned short> b(8, 7);
658 At the time of writing (October 2008) this is aligned with the
659 proposed resolution for <a href=
660 "http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#438">library
661 issue 438</a>. That is a <em>post <tt>C++03</tt></em>
662 change, and is currently in the working paper for
663 <tt>C++0x</tt>. Informally speaking, the critical changes with
664 respect to <tt>C++03</tt> are the drop of a <tt>static_cast</tt>
665 on the second argument, and more leeway as to <em>when</em> the
666 templated constructor should have the same effect as the (size,
667 value) one: only when <tt>InputIterator</tt> is an integral
668 type, in <tt>C++03</tt>; when it is either an integral type or
669 any other type that the implementation might detect as
670 impossible to be an input iterator, with the proposed
671 resolution. For the purposes of <tt>dynamic_bitset</tt> we limit
672 ourselves to the first of these two changes.<br /><br />
675 <i>Otherwise</i> (<i>i.e.</i> if the template argument is not an
676 integral type), constructs—under the condition in the
677 <tt>requires</tt> clause—a bitset based on a range of
678 blocks. Let <tt>*first</tt> be block number 0, <tt>*++first</tt>
679 block number 1, etc. Block number <tt>b</tt> is used to
680 initialize the bits of the dynamic_bitset in the position range
681 <tt>[b*bits_per_block, (b+1)*bits_per_block)</tt>. For each
682 block number <tt>b</tt> with value <tt>bval</tt>, the bit
683 <tt>(bval >> i) & 1</tt> corresponds to the bit at
684 position <tt>(b * bits_per_block + i)</tt> in the bitset (where
685 <tt>i</tt> goes through the range <tt>[0, bits_per_block)</tt>).
689 <b>Requires:</b> <tt>BlockInputIterator</tt> must be either an
690 integral type or a model of <a href=
691 "http://www.sgi.com/tech/stl/InputIterator.html">Input
692 Iterator</a> whose <tt>value_type</tt> is the same type as
693 <tt>Block</tt>.<br /> <b>Throws:</b> An allocation error if
694 memory is exhausted (<tt>std::bad_alloc</tt> if
695 <tt>Allocator=std::allocator</tt>).<br />
699 template<typename Char, typename Traits, typename Alloc>
701 <a id="cons3">dynamic_bitset</a>(const <a href=
702 "http://www.sgi.com/tech/stl/basic_string.html">std::basic_string</a><Char,Traits,Alloc>& s,
703 typename std::basic_string<CharT, Traits, Alloc>::size_type pos = 0,
704 typename std::basic_string<CharT, Traits, Alloc>::size_type n = <a
706 "http://www.sgi.com/tech/stl/basic_string.html">std::basic_string</a><Char,Traits,Alloc>::npos,
707 const Allocator& alloc = Allocator())
710 <b>Precondition:</b> <tt>pos <= s.size()</tt> and the
711 characters used to initialize the bits must be <tt>0</tt> or
713 <b>Effects:</b> Constructs a bitset from a string of 0's and
714 1's. The first <tt>M</tt> bits are initialized to the
715 corresponding characters in <tt>s</tt>, where <tt>M =
716 min(s.size() - pos, n)</tt>. Note that the <i>highest</i>
717 character position in <tt>s</tt>, not the lowest, corresponds to
718 the least significant bit. That is, character position <tt>pos +
719 M - 1 - i</tt> corresponds to bit <tt>i</tt>. So, for example,
720 <tt>dynamic_bitset(string("1101"))</tt> is the same as
721 <tt>dynamic_bitset(13ul)</tt>.<br />
722 <b>Throws:</b> an allocation error if memory is exhausted
723 (<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>).
726 <h3><a id="destructor">Destructor</a></h3>
733 <b>Effects:</b> Releases the memory associated with this bitset
734 and destroys the bitset object itself.<br />
735 <b>Throws:</b> nothing.
738 <h3><a id="member-functions">Member Functions</a></h3>
742 void <a id="swap">swap</a>(dynamic_bitset& b);
745 <b>Effects:</b> The contents of this bitset and bitset <tt>b</tt>
747 <b>Postconditions:</b> This bitset is equal to the original
748 <tt>b</tt>, and <tt>b</tt> is equal to the previous version of
750 <b>Throws:</b> nothing.
754 dynamic_bitset& <a id=
755 "assign">operator=</a>(const dynamic_bitset& x)
758 <b>Effects:</b> This bitset becomes a copy of the bitset
760 <b>Postconditions:</b> For all <tt>i</tt> in the range
761 <tt>[0,x.size())</tt>, <tt>(*this)[i] == x[i]</tt>.<br />
762 <b>Returns:</b> <tt>*this</tt>.<br />
763 <b>Throws:</b> nothing. <br />
764 (Required by <a href=
765 "http://www.sgi.com/tech/stl/Assignable.html">Assignable</a>.)
769 allocator_type <a id="get_allocator">get_allocator()</a> const;
771 <b>Returns:</b> A copy of the allocator object used to construct <tt>*this</tt>.
776 "resize">resize</a>(size_type num_bits, bool value = false);
779 <b>Effects:</b> Changes the number of bits of the bitset to
780 <tt>num_bits</tt>. If <tt>num_bits > size()</tt> then the bits
781 in the range <tt>[0,size())</tt> remain the same, and the bits in
782 <tt>[size(),num_bits)</tt> are all set to <tt>value</tt>. If
783 <tt>num_bits < size()</tt> then the bits in the range
784 <tt>[0,num_bits)</tt> stay the same (and the remaining bits are
786 <b>Postconditions:</b> <tt>this->size() == num_bits</tt>.<br />
787 <b>Throws:</b> An allocation error if memory is exhausted
788 (<tt>std::bad_alloc</tt> if
789 <tt>Allocator=std::allocator</tt>).<br />
794 void <a id="clear">clear</a>()
797 <b>Effects:</b> The size of the bitset becomes zero.<br />
798 <b>Throws:</b> nothing.
802 void <a id="push_back">push_back</a>(bool value);
805 <b>Effects:</b> Increases the size of the bitset by one, and sets
806 the value of the new most-significant bit to <tt>value</tt>.<br />
807 <b>Throws:</b> An allocation error if memory is exhausted
808 (<tt>std::bad_alloc</tt> if
809 <tt>Allocator=std::allocator</tt>).<br />
814 void <a id="append1">append</a>(Block value);
817 <b>Effects:</b> Appends the bits in <tt>value</tt> to the bitset
818 (appends to the most-significant end). This increases the size of
819 the bitset by <tt>bits_per_block</tt>. Let <tt>s</tt> be the old
820 size of the bitset, then for <tt>i</tt> in the range
821 <tt>[0,bits_per_block)</tt>, the bit at position <tt>(s + i)</tt>
822 is set to <tt>((value >> i) & 1)</tt>.<br />
823 <b>Throws:</b> An allocation error if memory is exhausted
824 (<tt>std::bad_alloc</tt> if
825 <tt>Allocator=std::allocator</tt>).<br />
830 template <typename BlockInputIterator>
832 "append2">append</a>(BlockInputIterator first, BlockInputIterator last);
835 <b>Effects:</b> This function provides the same end result as the
836 following code, but is typically more efficient.
839 for (; first != last; ++first)
843 <b>Requires:</b> The <tt>BlockInputIterator</tt> type must be a
845 "http://www.sgi.com/tech/stl/InputIterator.html">Input
846 Iterator</a> and the <tt>value_type</tt> must be the same type as
847 <tt>Block</tt>.<br />
848 <b>Throws:</b> An allocation error if memory is exhausted
849 (<tt>std::bad_alloc</tt> if
850 <tt>Allocator=std::allocator</tt>).<br />
855 dynamic_bitset& <a id=
856 "op-and-assign">operator&=</a>(const dynamic_bitset& rhs)
859 <b>Requires:</b> <tt>this->size() == rhs.size()</tt>.<br />
860 <b>Effects:</b> Bitwise-AND all the bits in <tt>rhs</tt> with
861 the bits in this bitset. This is equivalent to:
864 for (size_type i = 0; i != this->size(); ++i)
865 (*this)[i] = (*this)[i] & rhs[i];
868 <b>Returns:</b> <tt>*this</tt>.<br />
869 <b>Throws:</b> nothing.
873 dynamic_bitset& <a id=
874 "op-or-assign">operator|=</a>(const dynamic_bitset& rhs)
877 <b>Requires:</b> <tt>this->size() == rhs.size()</tt>.<br />
878 <b>Effects:</b> Bitwise-OR's all the bits in <tt>rhs</tt> with
879 the bits in this bitset. This is equivalent to:
882 for (size_type i = 0; i != this->size(); ++i)
883 (*this)[i] = (*this)[i] | rhs[i];
886 <b>Returns:</b> <tt>*this</tt>.<br />
887 <b>Throws:</b> nothing.
891 dynamic_bitset& <a id=
892 "op-xor-assign">operator^=</a>(const dynamic_bitset& rhs)
895 <b>Requires:</b> <tt>this->size() == rhs.size()</tt>.<br />
896 <b>Effects:</b> Bitwise-XOR's all the bits in <tt>rhs</tt> with
897 the bits in this bitset. This is equivalent to:
900 for (size_type i = 0; i != this->size(); ++i)
901 (*this)[i] = (*this)[i] ^ rhs[i];
904 <b>Returns:</b> <tt>*this</tt>.<br />
905 <b>Throws:</b> nothing.
909 dynamic_bitset& <a id=
910 "op-sub-assign">operator-=</a>(const dynamic_bitset& rhs)
913 <b>Requires:</b> <tt>this->size() == rhs.size()</tt>.<br />
914 <b>Effects:</b> Computes the set difference of this bitset and
915 the <tt>rhs</tt> bitset. This is equivalent to:
918 for (size_type i = 0; i != this->size(); ++i)
919 (*this)[i] = (*this)[i] && !rhs[i];
922 <b>Returns:</b> <tt>*this</tt>.<br />
923 <b>Throws:</b> nothing.
927 dynamic_bitset& <a id=
928 "op-sl-assign">operator<<=</a>(size_type n)
931 <b>Effects:</b> Shifts the bits in this bitset to the left by
932 <tt>n</tt> bits. For each bit in the bitset, the bit at position
933 pos takes on the previous value of the bit at position <tt>pos -
934 n</tt>, or zero if no such bit exists.<br />
935 <b>Returns:</b> <tt>*this</tt>.<br />
936 <b>Throws:</b> nothing.
940 dynamic_bitset& <a id=
941 "op-sr-assign">operator>>=</a>(size_type n)
944 <b>Effects:</b> Shifts the bits in this bitset to the right by
945 <tt>n</tt> bits. For each bit in the bitset, the bit at position
946 <tt>pos</tt> takes on the previous value of bit <tt>pos + n</tt>,
947 or zero if no such bit exists.<br />
948 <b>Returns:</b> <tt>*this</tt>.<br />
949 <b>Throws:</b> nothing.
953 dynamic_bitset <a id=
954 "op-sl">operator<<</a>(size_type n) const
957 <b>Returns:</b> a copy of <tt>*this</tt> shifted to the left by
958 <tt>n</tt> bits. For each bit in the returned bitset, the bit at
959 position pos takes on the value of the bit at position <tt>pos -
960 n</tt> of this bitset, or zero if no such bit exists.<br />
961 <b>Throws:</b> An allocation error if memory is exhausted
962 (<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>).
966 dynamic_bitset <a id=
967 "op-sr">operator>></a>(size_type n) const
970 <b>Returns:</b> a copy of <tt>*this</tt> shifted to the right by
971 <tt>n</tt> bits. For each bit in the returned bitset, the bit at
972 position pos takes on the value of the bit at position <tt>pos +
973 n</tt> of this bitset, or zero if no such bit exists.<br />
974 <b>Throws:</b> An allocation error if memory is exhausted
975 (<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>).
979 dynamic_bitset& <a id="set1">set</a>()
982 <b>Effects:</b> Sets every bit in this bitset to 1.<br />
983 <b>Returns:</b> <tt>*this</tt><br />
984 <b>Throws:</b> nothing.
988 dynamic_bitset& <a id="flip1">flip</a>()
991 <b>Effects:</b> Flips the value of every bit in this bitset.<br />
992 <b>Returns:</b> <tt>*this</tt><br />
993 <b>Throws:</b> nothing.
997 dynamic_bitset <a id="op-not">operator~</a>() const
1000 <b>Returns:</b> a copy of <tt>*this</tt> with all of its bits
1002 <b>Throws:</b> An allocation error if memory is exhausted
1003 (<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>).
1007 dynamic_bitset& <a id="reset1">reset</a>()
1010 <b>Effects:</b> Clears every bit in this bitset.<br />
1011 <b>Returns:</b> <tt>*this</tt><br />
1012 <b>Throws:</b> nothing.
1016 dynamic_bitset& <a id=
1017 "set2">set</a>(size_type n, bool val = true)
1020 <b>Precondition:</b> <tt>n < this->size()</tt>.<br />
1021 <b>Effects:</b> Sets bit <tt>n</tt> if <tt>val</tt> is
1022 <tt>true</tt>, and clears bit <tt>n</tt> if <tt>val</tt> is
1023 <tt>false</tt>. <br />
1024 <b>Returns:</b> <tt>*this</tt>
1028 dynamic_bitset& <a id="reset2">reset</a>(size_type n)
1031 <b>Precondition:</b> <tt>n < this->size()</tt>.<br />
1032 <b>Effects:</b> Clears bit <tt>n</tt>.<br />
1033 <b>Returns:</b> <tt>*this</tt>
1037 dynamic_bitset& <a id="flip2">flip</a>(size_type n)
1040 <b>Precondition:</b> <tt>n < this->size()</tt>.<br />
1041 <b>Effects:</b> Flips bit <tt>n</tt>.<br />
1042 <b>Returns:</b> <tt>*this</tt>
1046 size_type <a id="size">size</a>() const
1049 <b>Returns:</b> the number of bits in this bitset.<br />
1050 <b>Throws:</b> nothing.
1054 size_type <a id="num_blocks">num_blocks</a>() const
1057 <b>Returns:</b> the number of blocks in this bitset.<br />
1058 <b>Throws:</b> nothing.
1062 size_type <a id="max_size">max_size</a>() const;
1065 <b>Returns:</b> the maximum size of a <tt>dynamic_bitset</tt>
1066 object having the same type as <tt>*this</tt>. Note that if
1067 any <tt>dynamic_bitset</tt> operation causes <tt>size()</tt> to
1068 exceed <tt>max_size()</tt> then the <i>behavior is undefined</i>.
1069 <br /><br />[The semantics of this function could change slightly
1070 when lib issue 197 will be closed]<br />
1074 bool <a id="empty">empty</a>() const;
1077 <b>Returns:</b> <tt>true</tt> if <tt>this->size() == 0</tt>, <tt>false</tt>
1078 otherwise. <i>Note</i>: not to be confused with <tt>none()</tt>, that has
1079 different semantics.
1083 size_type <a id="count">count</a>() const
1086 <b>Returns:</b> the number of bits in this bitset that are
1088 <b>Throws:</b> nothing.
1092 bool <a id="any">any</a>() const
1095 <b>Returns:</b> <tt>true</tt> if any bits in this bitset are set,
1096 and otherwise returns <tt>false</tt>.<br />
1097 <b>Throws:</b> nothing.
1101 bool <a id="none">none</a>() const
1104 <b>Returns:</b> <tt>true</tt> if no bits are set, and otherwise
1105 returns <tt>false</tt>.<br />
1106 <b>Throws:</b> nothing.
1110 bool <a id="test">test</a>(size_type n) const
1113 <b>Precondition:</b> <tt>n < this->size()</tt>.<br />
1114 <b>Returns:</b> <tt>true</tt> if bit <tt>n</tt> is set and
1115 <tt>false</tt> is bit <tt>n</tt> is 0.
1119 reference <a id="bracket">operator[]</a>(size_type n)
1122 <b>Precondition:</b> <tt>n < this->size()</tt>.<br />
1123 <b>Returns:</b> a <tt>reference</tt> to bit <tt>n</tt>. Note
1124 that <tt>reference</tt> is a proxy class with an assignment
1125 operator and a conversion to <tt>bool</tt>, which allows you to
1126 use <tt>operator[]</tt> for assignment. That is, you can write
1127 both <tt>x = b[n]</tt> and <tt>b[n] = x</tt>. However, in many
1128 other respects the proxy is not the same as the true reference
1129 type <tt>bool&</tt>.
1133 bool <a id="const-bracket">operator[]</a>(size_type n) const
1136 <b>Precondition:</b> <tt>n < this->size()</tt>.<br />
1137 <b>Returns:</b> The same as <tt>test(n)</tt>.
1141 unsigned long <a id="to_ulong">to_ulong</a>() const
1144 <b>Returns:</b> The numeric value corresponding to the bits in <tt>*this</tt>.
1146 <b>Throws:</b> <tt>std::overflow_error</tt> if that value is too large to
1147 be represented in an <tt>unsigned long</tt>, i.e. if <tt>*this</tt> has
1148 any non-zero bit at a position <tt>>=
1149 std::numeric_limits<unsigned long>::digits</tt>.
1154 "is_subset_of">is_subset_of</a>(const dynamic_bitset& a) const
1157 <b>Requires:</b> <tt>this->size() == a.size()</tt><br />
1158 <b>Returns:</b> true if this bitset is a subset of bitset
1159 <tt>a</tt>. That is, it returns true if, for every bit that is
1160 set in this bitset, the corresponding bit in bitset <tt>a</tt> is
1161 also set. Otherwise this function returns false.<br />
1162 <b>Throws:</b> nothing.
1167 "is_proper_subset_of">is_proper_subset_of</a>(const dynamic_bitset& a) const
1170 <b>Requires:</b> <tt>this->size() == a.size()</tt><br />
1171 <b>Returns:</b> true if this bitset is a proper subset of bitset
1172 <tt>a</tt>. That is, it returns true if, for every bit that is
1173 set in this bitset, the corresponding bit in bitset <tt>a</tt> is
1174 also set and if <tt>this->count() < a.count()</tt>.
1175 Otherwise this function returns false.<br />
1176 <b>Throws:</b> nothing.
1181 "intersects">intersects</a>(const dynamic_bitset& a) const
1184 <b>Requires:</b> <tt>this->size() == a.size()</tt><br />
1185 <b>Returns:</b> true if this bitset and <tt>a</tt> intersect.
1186 That is, it returns true if, there is a bit which is set in this
1187 bitset, such that the corresponding bit in bitset <tt>a</tt> is
1188 also set. Otherwise this function returns false.<br />
1189 <b>Throws:</b> nothing.
1193 size_type <a id = "find_first">find_first</a>() const;
1196 <b>Returns:</b> the lowest index <tt>i</tt> such as bit <tt>i</tt>
1197 is set, or <tt>npos</tt> if <tt>*this</tt> has no on bits.
1201 size_type <a id="find_next">find_next</a>(size_type pos) const;
1204 <b>Returns:</b> the lowest index <tt>i</tt> greater than
1205 <tt>pos</tt> such as bit <tt>i</tt> is set, or <tt>npos</tt> if
1206 no such index exists.
1211 "op-equal">operator==</a>(const dynamic_bitset& rhs) const
1214 <b>Returns:</b> <tt>true</tt> if <tt>this->size() ==
1215 rhs.size()</tt> and if for all <tt>i</tt> in the range
1216 <tt>[0,rhs.size())</tt>, <tt>(*this)[i] == rhs[i]</tt>. Otherwise
1217 returns <tt>false</tt>.<br />
1218 <b>Throws:</b> nothing.<br />
1219 (Required by <a href=
1220 "http://www.sgi.com/tech/stl/EqualityComparable.html">Equality
1226 "op-not-equal">operator!=</a>(const dynamic_bitset& rhs) const
1229 <b>Returns:</b> <tt>!((*this) == rhs)</tt><br />
1230 <b>Throws:</b> nothing.<br />
1231 (Required by <a href=
1232 "http://www.sgi.com/tech/stl/EqualityComparable.html">Equality
1238 "op-less">operator<</a>(const dynamic_bitset& rhs) const
1241 <b>Returns:</b> <tt>true</tt> if this bitset is lexicographically
1242 less than <tt>rhs</tt>, and returns <tt>false</tt> otherwise.
1243 (See the description of <a href=
1244 "http://www.sgi.com/tech/stl/lexicographical_compare.html">lexicographical_compare</a>
1245 for a definition of lexicographic ordering). <br />
1246 <b>Throws:</b> nothing.<br />
1247 (Required by <a href=
1248 "http://www.sgi.com/tech/stl/LessThanComparable.html">Less Than
1254 "op-greater">operator></a>(const dynamic_bitset& rhs) const
1257 <b>Returns:</b> <tt>!((*this) < rhs || (*this) ==
1259 <b>Throws:</b> nothing.<br />
1260 (Required by <a href=
1261 "http://www.sgi.com/tech/stl/LessThanComparable.html">Less Than
1267 "op-less-equal">operator<=</a>(const dynamic_bitset& rhs) const
1270 <b>Returns:</b> <tt>(*this) < rhs || (*this) == rhs</tt><br />
1271 <b>Throws:</b> nothing.<br />
1272 (Required by <a href=
1273 "http://www.sgi.com/tech/stl/LessThanComparable.html">Less Than
1279 "op-greater-equal">operator>=</a>(const dynamic_bitset& rhs) const
1282 <b>Returns:</b> <tt>(*this) > rhs || (*this) == rhs</tt><br />
1283 <b>Throws:</b> nothing.<br />
1284 (Required by <a href=
1285 "http://www.sgi.com/tech/stl/LessThanComparable.html">Less Than
1289 <h3><a id="non-member-functions">Non-Member Functions</a></h3>
1293 dynamic_bitset <a id=
1294 "op-and">operator&</a>(const dynamic_bitset& a, const dynamic_bitset& b)
1297 <b>Requires:</b> <tt>a.size() == b.size()</tt><br />
1298 <b>Returns:</b> A new bitset that is the bitwise-AND of the
1299 bitsets <tt>a</tt> and <tt>b</tt>.<br />
1300 <b>Throws:</b> An allocation error if memory is exhausted
1301 (<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>).
1305 dynamic_bitset <a id=
1306 "op-or">operator|</a>(const dynamic_bitset& a, const dynamic_bitset& b)
1309 <b>Requires:</b> <tt>a.size() == b.size()</tt><br />
1310 <b>Returns:</b> A new bitset that is the bitwise-OR of the
1311 bitsets <tt>a</tt> and <tt>b</tt>.<br />
1312 <b>Throws:</b> An allocation error if memory is exhausted
1313 (<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>).
1317 dynamic_bitset <a id=
1318 "op-xor">operator^</a>(const dynamic_bitset& a, const dynamic_bitset& b)
1321 <b>Requires:</b> <tt>a.size() == b.size()</tt><br />
1322 <b>Returns:</b> A new bitset that is the bitwise-XOR of the
1323 bitsets <tt>a</tt> and <tt>b</tt>.<br />
1324 <b>Throws:</b> An allocation error if memory is exhausted
1325 (<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>).
1329 dynamic_bitset <a id=
1330 "op-sub">operator-</a>(const dynamic_bitset& a, const dynamic_bitset& b)
1333 <b>Requires:</b> <tt>a.size() == b.size()</tt><br />
1334 <b>Returns:</b> A new bitset that is the set difference of the
1335 bitsets <tt>a</tt> and <tt>b</tt>.<br />
1336 <b>Throws:</b> An allocation error if memory is exhausted
1337 (<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>).
1341 template <typename CharT, typename Alloc>
1343 "to_string">to_string</a>(const dynamic_bitset<Block, Allocator>& b,
1345 "http://www.sgi.com/tech/stl/basic_string.html">std::basic_string</a><Char,Traits,Alloc>& s)
1348 <b>Effects:</b> Copies a representation of <tt>b</tt> into the
1349 string <tt>s</tt>. A character in the string is <tt>'1'</tt> if
1350 the corresponding bit is set, and <tt>'0'</tt> if it is not.
1351 Character position <tt>i</tt> in the string corresponds to bit
1352 position <tt>b.size() - 1 - i</tt>. <br />
1353 <b>Throws:</b> If memory is exhausted, the string will throw an
1354 allocation error.<br />
1355 <b>Rationale:</b> This function is not a member function taking
1356 zero arguments and returning a string for a couple reasons.
1357 First, this version can be slighly more efficient because the
1358 string is not copied (due to being passed by value). Second, as a
1359 member function, to allow for flexibility with regards to the
1360 template parameters of <tt>basic_string</tt>, the member function
1361 would require explicit template parameters. Few C++ programmers
1362 are familiar with explicit template parameters, and some C++
1363 compilers do not handle them properly.
1367 template <typename Block, typename Alloc, typename BlockOutputIterator>
1369 "to_block_range">to_block_range</a>(const dynamic_bitset<Block, Alloc>& b, BlockOutputIterator result)
1372 <b>Effects:</b> Writes the bits of the bitset into the iterator
1373 <tt>result</tt> a block at a time. The first block written
1374 represents the bits in the position range
1375 <tt>[0,bits_per_block)</tt> in the bitset, the second block
1376 written the bits in the range
1377 <tt>[bits_pre_block,2*bits_per_block)</tt>, and so on. For each
1378 block <tt>bval</tt> written, the bit <tt>(bval >> i) &
1379 1</tt> corresponds to the bit at position <tt>(b * bits_per_block
1380 + i)</tt> in the bitset.<br />
1381 <b>Requires:</b> The type <tt>BlockOutputIterator</tt> must be a
1383 "http://www.sgi.com/tech/stl/OutputIterator.html">Output
1384 Iterator</a> and its <tt>value_type</tt> must be the same type as
1385 <tt>Block</tt>. Further, the size of the output range must be
1386 greater or equal <tt>b.num_blocks()</tt>.
1390 template <typename BlockIterator, typename Block, typename Alloc>
1392 "from_block_range">from_block_range</a>(BlockIterator first,
1393 BlockIterator last, const dynamic_bitset<Block, Alloc>& b)
1396 <b>Effects:</b> Reads blocks from the iterator range into the
1398 <b>Requires:</b> The type <tt>BlockIterator</tt> must be a model
1399 of <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input
1400 Iterator</a> and its <tt>value_type</tt> must be the same type as
1401 <tt>Block</tt>. The size of the iterator range must be less or
1402 equal to <tt>b.num_blocks()</tt>.
1406 template <typename Char, typename Traits, typename Block, typename Alloc>
1407 basic_ostream<Char, Traits>&
1409 "op-out">operator<<</a>(basic_ostream<Char, Traits>& os, const dynamic_bitset<Block, Alloc>& b)
1412 <b>Effects:</b> Inserts a textual representation of b into the stream
1413 <tt>os</tt> (highest bit first). Informally, the output is the same as doing
1416 std::basic_string<Char, Traits> s;
1417 boost::to_string(x, s):
1421 except that the stream inserter takes into accout the locale imbued into
1422 <tt>os</tt>, which <tt>boost::to_string()</tt> can't do. Here is a more
1423 precise specification, given in terms of "as if" rule: first, for each
1424 valid position i into the bitset <tt>b</tt> let's put:
1426 <tt>character_of(b[i)]) = b[i]? os.widen('1') : os.widen('0');</tt>
1428 Let also <tt>s</tt> be a <tt>std::basic_string<Char, Traits></tt>
1429 object, having length <tt>b.size()</tt> and such as, for each <tt>i</tt>
1430 in <tt>[0, b.size())</tt>,
1432 <tt>s[i] is character_of(b[i])</tt>
1434 Then, the output, the effects on <tt>os</tt> and the exception behavior
1435 is the same as outputting the object <tt>s</tt> to <tt>os</tt> (same
1436 width, same exception mask, same padding, same setstate() logic)
1438 <b>Returns:</b> os <br />
1439 <b>Throws:</b> <tt>std::ios_base::failure</tt> if there is a
1440 problem writing to the stream.
1444 template <typename Char, typename Traits, typename Block, typename Alloc>
1445 std::basic_istream<Char,Traits>&
1447 "op-in">operator>></a>(std::basic_istream<Char,Traits>& is, dynamic_bitset<Block, Alloc>& b)
1450 <b>Effects:</b> Extracts a <tt>dynamic_bitset</tt> from an input stream.
1452 <i>Definitions:</i><br /><br />
1453 Let <i>Tr</i> be the traits_type of <i>is</i>. Then:
1456 A (non-eof) character <tt>c</tt> extracted from <tt>is</tt>
1457 is a <i>bitset digit</i> if and only if either Tr::eq(c, is.widen('0')) or
1458 Tr::eq(c, is.widen('1')) return true.
1460 <li>If c is a bitset digit, it's <i>corresponding bit value</i> is 0 if
1461 Tr::eq(c, is.widen('0')) is true, 1 otherwise.
1465 The function begins by constructing a <tt>sentry</tt> object <tt>k</tt> as if <tt>k</tt>
1468 <tt>typename std::basic_istream<Char, Traits>::sentry k(is)</tt>.
1470 If <tt>bool(k)</tt> is true, it calls <tt>b.clear()</tt>
1471 then attempts to extract characters from <tt>is</tt>. For each character c
1472 that is a <i>bitset digit</i> the <i>corresponding bit value</i> is
1473 appended to the less significant end of <tt>b</tt> (appending may throw).
1474 If <tt>is.width()</tt> is greater than zero and smaller than <tt>b.max_size()</tt>
1475 then the maximum number <tt>n</tt> of bits appended is <tt>is.width()</tt>;
1476 otherwise <tt>n</tt> = <tt>b.max_size()</tt>.
1478 Unless the extractor is exited via an exception, characters are extracted (and
1479 corresponding bits appended) until any of the following occurs:<br />
1482 <li> <tt>n</tt> bits are stored into the bitset;</li>
1483 <li> end-of-file, or an error, occurs on the input sequence;</li>
1484 <li> the next available input character isn't a bitset digit</li>
1486 <br /> If no exception caused the function to exit then <tt>is.width(0)</tt> is
1487 called, regardless of how many characters were actually extracted. The
1488 sentry object k is destroyed.
1490 <br />If the function extracts no characters[???], it calls is.setstate(std::ios::failbit),
1491 which may throw <tt>std::ios_base::failure</tt>.
1498 <b>Throws:</b> An allocation error if memory is exhausted
1499 (<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>).
1500 A <tt>std::ios_base::failure</tt> if there is a problem reading
1504 <h3><a id="exception-guarantees">Exception guarantees</a></h3>
1506 All of <tt>dynamic_bitset</tt> functions offer at least the basic
1507 exception guarantee.
1510 <h3><a id="changes-from-previous-ver">Changes from previous version(s)</a></h3>
1512 <h4><i>Changes in Boost 1.37.0</i></h4>
1514 <li>The constructor from a block range implements a "do the right thing"
1515 behavior, a la standard sequences.</li>
1518 <!-- Changes from Boost 1.31.0 -->
1519 <h4><i>Changes from Boost 1.31.0</i></h4>
1522 The stream extractor has completely different semantics: as natural
1523 for a dynamic structure, it now expands the bitset as needed during
1524 extraction. The new behaviour mimics that of the <tt>basic_string</tt>
1525 extractor but there are some differences the user should be aware of;
1526 so, please, check the <a href="#op-in">documentation</a>. (One
1527 difference concerns the case where <code>stream.width() >
1528 bitset.max_size() > 0</code>. In that circumstance the
1529 extractor of <tt>dynamic_bitset</tt> never attempts to extract more
1530 than <tt>max_size()</tt> characters, whereas the extractor of
1531 <tt>basic_string</tt> goes on and, on conforming implementations,
1532 eventually throws a <tt>length_error</tt> exception. Note: That's what
1533 the standard mandates -see especially <a
1534 href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#83">library
1535 issue 83</a>- but not all implementations conform)
1537 The stream extractor is now also "exception-aware" in the sense that
1538 it works correctly when setting exception masks on the stream.
1542 Several member functions (<tt>empty()</tt>, <tt>find_first()</tt>
1543 , <tt>find_next()</tt>, <tt>get_allocator()</tt>, <tt>intersects()</tt>
1544 , <tt>max_size()</tt> <!--, <tt>reserve()</tt>, <tt>capacity()</tt> -->)
1548 The constructor from <tt>basic_string</tt> has a new parameter that was totally
1553 <i>Technicalities and minor changes</i>
1556 The class <tt>reference</tt> has been reimplemented so that
1557 dynamic_bitset's references behave more like references to standard
1558 container elements. In particular it is now guaranteed that they
1559 cannot be invalidated from a standard library swap() function
1560 applied to their corresponding <tt>dynamic_bitset</tt>s.
1563 <i>General improvements</i>
1565 Several optimizations to member and non-member functions and to the
1566 nested class <tt>reference</tt>.
1569 <h3><a id="see-also">See also</a></h3>
1572 "http://www.sgi.com/tech/stl/bitset.html">std::bitset</a></tt>,
1574 "http://www.sgi.com/tech/stl/Vector.html">std::vector</a></tt>,
1576 <h3><a id="acknowledgements">Acknowledgements</a></h3>
1578 <p>We would like to thank the Boost community for putting in the
1579 time to review and accept this library. This library is much
1580 better than it ever would have been due to all the suggestions
1581 from Boost members. We especially thank Matt Marcus for taking on
1582 the task of review manager. Also, a special thanks goes to
1583 James Kanze for his invaluable help with the internationalization
1586 <table summary="Copyright"> <tr> <td>Copyright © 2001</td>
1587 <td><a href="http://www.boost.org/people/jeremy_siek.htm">Jeremy
1588 Siek</a>, Indiana University (<a
1589 href="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</a>)<br /> <a
1590 href="http://freshsources.com">Chuck Allison</a>, Senior Editor,
1591 C/C++ Users Journal (<a
1592 href="mailto:cda@freshsources.com">cda@freshsources.com</a>)<br
1594 <td>Copyright © 2003-2004, 2008</td> <td><a
1595 href="http://gennaro-prota.50webs.com/">Gennaro Prota</a>
1596 (name.surname yahoo.com)</td>
1600 <div class="legalnotice">
1601 Distributed under the Boost Software License, Version 1.0.
1602 (See accompanying file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a>
1603 or copy at <a class="ulink" href="http://www.boost.org/LICENSE_1_0.txt">
1604 http://www.boost.org/LICENSE_1_0.txt</a>)
1615 <!-- LocalWords: dynamic bitset alt gif iostream hpp int bitsets const ul ulong -->
1616 <!-- LocalWords: STL LessThan alloc num typename BlockInputIterator html pos -->
1617 <!-- LocalWords: npos bool rhs OR's XOR's val CharT istream ostream os siek -->
1618 <!-- LocalWords: htm namespace enum sizeof BlockOutputIterator fwd ith jth -->