...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
The dynamic_bitset class represents a set of bits. It provides accesses to the value of individual bits via an operator[] and provides all of the bitwise operators that one can apply to builtin integers, such as operator& and operator<<. The number of bits in the set is specified at runtime via a parameter to the constructor of the dynamic_bitset.
The dynamic_bitset class is nearly identical to the std::bitset class. The difference is that the size of the dynamic_bitset (the number of bits) is specified at run-time during the construction of a dynamic_bitset object, whereas the size of a std::bitset is specified at compile-time through an integer template parameter.
The main problem that dynamic_bitset 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 dynamic_bitset, such as operator& and operator|, correspond to set operations, such as intersection and union.
namespace boost { template <typename Block, typename Allocator> class dynamic_bitset { public: typedef Block block_type; typedef implementation-defined size_type; enum { bits_per_block = CHAR_BIT * sizeof(Block) }; class reference { public: // An automatically generated copy constructor. reference& operator=(bool value); reference& operator|=(bool value); reference& operator&=(bool value); reference& operator^=(bool value); reference& operator-=(bool value); reference& operator=(const reference& j); reference& operator|=(const reference& j); reference& operator&=(const reference& j); reference& operator^=(const reference& j); reference& operator-=(const reference& j); bool operator~() const; operator bool() const; reference& flip(); }; typedef bool const_reference; explicit dynamic_bitset(const Allocator& alloc = Allocator()); explicit dynamic_bitset(size_type num_bits, unsigned long value = 0, const Allocator& alloc = Allocator()); template <typename CharT, typename Traits, typename Alloc> explicit dynamic_bitset(const std::basic_string<CharT, Traits, Alloc>& s, typename std::basic_string<CharT, Traits, Alloc>::size_type pos = 0, typename std::basic_string<CharT, Traits, Alloc>::size_type n = std::basic_string<CharT, Traits, Alloc>::npos, const Allocator& alloc = Allocator()); template <typename BlockInputIterator> dynamic_bitset(BlockInputIterator first, BlockInputIterator last, const Allocator& alloc = Allocator()); dynamic_bitset(const dynamic_bitset& b); void swap(dynamic_bitset& b); dynamic_bitset& operator=(const dynamic_bitset& b); void resize(size_type num_bits, bool value = false); void clear(); void push_back(bool bit); void append(Block block); template <typename BlockInputIterator> void append(BlockInputIterator first, BlockInputIterator last); dynamic_bitset& operator&=(const dynamic_bitset& b); dynamic_bitset& operator|=(const dynamic_bitset& b); dynamic_bitset& operator^=(const dynamic_bitset& b); dynamic_bitset& operator-=(const dynamic_bitset& b); dynamic_bitset& operator<<=(size_type n); dynamic_bitset& operator>>=(size_type n); dynamic_bitset operator<<(size_type n) const; dynamic_bitset operator>>(size_type n) const; dynamic_bitset& set(size_type n, bool val = true); dynamic_bitset& set(); dynamic_bitset& reset(size_type n); dynamic_bitset& reset(); dynamic_bitset& flip(size_type n); dynamic_bitset& flip(); bool test(size_type n) const; bool any() const; bool none() const; dynamic_bitset operator~() const; size_type count() const; reference operator[](size_type pos) { return reference(*this, pos); } bool operator[](size_type pos) const { return test(pos); } unsigned long to_ulong() const; size_type size() const; size_type num_blocks() const; bool is_subset_of(const dynamic_bitset& a) const; bool is_proper_subset_of(const dynamic_bitset& a) const; }; template <typename B, typename A> bool operator==(const dynamic_bitset<B, A>& a, const dynamic_bitset<B, A>& b); template <typename Block, typename Allocator> bool operator!=(const dynamic_bitset<Block, Allocator>& a, const dynamic_bitset<Block, Allocator>& b); template <typename B, typename A> bool operator<(const dynamic_bitset<B, A>& a, const dynamic_bitset<B, A>& b); template <typename Block, typename Allocator> bool operator<=(const dynamic_bitset<Block, Allocator>& a, const dynamic_bitset<Block, Allocator>& b); template <typename Block, typename Allocator> bool operator>(const dynamic_bitset<Block, Allocator>& a, const dynamic_bitset<Block, Allocator>& b); template <typename Block, typename Allocator> bool operator>=(const dynamic_bitset<Block, Allocator>& a, const dynamic_bitset<Block, Allocator>& b); template <typename Block, typename Allocator> dynamic_bitset<Block, Allocator> operator&(const dynamic_bitset<Block, Allocator>& b1, const dynamic_bitset<Block, Allocator>& b2); template <typename Block, typename Allocator> dynamic_bitset<Block, Allocator> operator|(const dynamic_bitset<Block, Allocator>& b1, const dynamic_bitset<Block, Allocator>& b2); template <typename Block, typename Allocator> dynamic_bitset<Block, Allocator> operator^(const dynamic_bitset<Block, Allocator>& b1, const dynamic_bitset<Block, Allocator>& b2); template <typename Block, typename Allocator> dynamic_bitset<Block, Allocator> operator-(const dynamic_bitset<Block, Allocator>& b1, const dynamic_bitset<Block, Allocator>& b2); template <typename Block, typename Allocator, typename CharT, typename Alloc> void to_string(const dynamic_bitset<Block, Allocator>& b, std::basic_string<CharT, Alloc>& s); template <typename Block, typename Allocator, typename BlockOutputIterator> void to_block_range(const dynamic_bitset<Block, Allocator>& b, BlockOutputIterator result); template <typename CharT, typename Traits, typename Block, typename Allocator> std::basic_ostream<CharT, Traits>& operator<<(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>& operator>>(std::basic_istream<CharT, Traits>& is, dynamic_bitset<Block, Allocator>& b); } // namespace boost
Each bit represents either the Boolean value true or false (1 or 0). To set a bit is to assign it 1. To clear or reset a bit is to assign it 0. To flip 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 position. A bitset x contains x.size() bits, with each bit assigned a unique position in the range [0,x.size()). The bit at position 0 is called the least significant bit and the bit at position size() - 1 is the most significant bit. When converting an instance of dynamic_bitset to or from an unsigned long n, the bit at position i of the bitset has the same value as (n >> i) & 1.
An example of setting and reading some bits. Note that operator[] goes from the least-significant bit at 0 to the most significant bit at size()-1. The operator<< for dynamic_bitset prints the bitset from most-significant to least-significant, since that is the format most people are use to reading.
#include <iostream> #include <boost/dynamic_bitset.hpp> int main(int, char*[]) { boost::dynamic_bitset<> x(5); // all 0's by default x[0] = 1; x[1] = 1; x[4] = 1; for (boost::dynamic_bitset<>::size_type i = 0; i < x.size(); ++i) std::cout << x[i]; std::cout << "\n"; std::cout << x << "\n"; return EXIT_SUCCESS; }
The output is
11001 10011
An example of creating some bitsets from integers (unsigned longs).
#include <iostream> #include <boost/dynamic_bitset.hpp> int main(int, char*[]) { const boost::dynamic_bitset<> b0(2, 0ul); std::cout << "bits(0) = " << b0 << std::endl; const boost::dynamic_bitset<> b1(2, 1ul); std::cout << "bits(1) = " << b1 << std::endl; const boost::dynamic_bitset<> b2(2, 2ul); std::cout << "bits(2) = " << b2 << std::endl; const boost::dynamic_bitset<> b3(2, 3ul); std::cout << "bits(3) = " << b3 << std::endl; return EXIT_SUCCESS; }
The output is
bits(0) = 00 bits(1) = 01 bits(2) = 10 bits(3) = 11
An example of performing some bitwise operations.
#include <iostream> #include <boost/dynamic_bitset.hpp> int main(int, char*[]) { const boost::dynamic_bitset<> mask(12, 2730ul); std::cout << "mask = " << mask << std::endl; boost::dynamic_bitset<> x(12); std::cout << "Enter a 12-bit bitset in binary: " << std::flush; if (std::cin >> x) { std::cout << "input number: " << x << std::endl; std::cout << "As unsigned long: " << x.to_ulong() << std::endl; std::cout << "And with mask: " << (x & mask) << std::endl; std::cout << "Or with mask: " << (x | mask) << std::endl; std::cout << "Shifted left: " << (x << 1) << std::endl; std::cout << "Shifted right: " << (x >> 1) << std::endl; } return EXIT_SUCCESS; }
The output is
mask = 101010101010 Enter a 12-bit bitset in binary: 100110101101 input number = 100110101101 As unsigned long: 2477 And with mask: 100010101000 Or with mask: 101110101111 Shifted left: 001101011010 Shifted right: 010011010110
Some people prefer the name "toggle" to "flip". The name "flip" was chosen because that is the name used in std::bitset. In fact, most of the function names for dynamic_bitset were chosen for this reason.
dynamic_bitset does not throw exceptions when a precondition is violated (as is done in std::bitset). Instead assert is used. See the guidelines for Error and Exception Handling for the explanation.
The class dynamic_bitset is defined in the header boost/dynamic_bitset.hpp. Also, there is a forward declaration for dynamic_bitset in the header boost/dynamic_bitset_fwd.hpp.
Parameter | Description | Default |
---|---|---|
Block | The integer type in which the bits are stored. | unsigned long |
Allocator | The allocator type used for all internal memory management. | std::allocator<Block> |
dynamic_bitset::reference
A proxy class that acts as a reference to a single bit. It contains an assignment operator, a conversion to bool, an operator~, and a member function flip. It exists only as a helper class for dynamic_bitset's operator[]. The following table describes the valid operations on the reference type. Assume that b is an instance of dynamic_bitset, i, j are of size_type and in the range [0,b.size()). Also, note that when we write b[i] we mean an object of type reference that was initialized from b[i]. The variable x is a bool.
Expression | Semantics |
---|---|
x = b[i] | Assign the ith bit of b to x. |
(bool)b[i] | Return the ith bit of b. |
b[i] = x | Set the ith bit of b to the value of x and return b[i]. |
b[i] |= x | Or the ith bit of b with the value of x and return b[i]. |
b[i] &= x | And the ith bit of b with the value of x and return b[i]. |
b[i] ^= x | Exclusive-Or the ith bit of b with the value of x and return b[i]. |
b[i] -= x | If x==true, clear the ith bit of b. Returns b[i]. |
b[i] = b[j] | Set the ith bit of b to the value of the jth bit of b and return b[i]. |
b[i] |= b[j] | Or the ith bit of b with the jth bit of b and return b[i]. |
b[i] &= b[j] | And the ith bit of b with the jth bit of b and return b[i]. |
b[i] ^= b[j] | Exclusive-Or the ith bit of b with the jth bit of b and return b[i]. |
b[i] -= b[j] | If the jth bit of b is set, clear the ith bit of b. Returns b[i]. |
x = ~b[i] | Assign the opposite of the ith bit of b to x. |
(bool)~b[i] | Return the opposite of the ith bit of b. |
b[i].flip() | Flip the ith bit of b and return b[i]. |
dynamic_bitset::const_referenceThe type bool.
dynamic_bitset::size_typeThe unsigned integer for representing the size of the bit set.
dynamic_bitset::block_typeThe same type as the Block template parameter.
dynamic_bitset::bits_per_blockThe number of bits in a Block.
dynamic_bitset(const Allocator& alloc = Allocator())Effects: Constructs a bitset of size zero. A copy of the alloc object will be used in subsequent bitset operations such as resize to allocate memory.
dynamic_bitset(size_type num_bits, unsigned long value = 0, const Allocator& alloc = Allocator())Effects: Constructs a bitset from an integer. The first M bits are initialized to the corresponding bits in val and all other bits, if any, to zero (where M = min(num_bits, sizeof(unsigned long) * CHAR_BIT)). A copy of the alloc object will be used in subsequent bitset operations such as resize to allocate memory.
dynamic_bitset(const dynamic_bitset& x)Effects: Constructs a bitset that is a copy of the bitset x. The allocator for this bitset is a copy of the allocator in x.
template <typename BlockInputIterator> explicit dynamic_bitset(BlockInputIterator first, BlockInputIterator last, const Allocator& alloc = Allocator());Effects: Constructs a bitset based on a range of blocks. Let *first be block number 0, *++first block number 1, etc. Block number b is used to initialize the bits of the dynamic_bitset in the position range [b*bits_per_block, (b+1)*bits_per_block). For each block number b with value bval, the bit (bval >> i) & 1 corresponds to the bit at position (b * bits_per_block + i) in the bitset (where i goes through the range [0, bits_per_block)).
template<typename Char, typename Traits, typename Alloc> explicit dynamic_bitset(const std::basic_string<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 = std::basic_string<Char,Traits,Alloc>::npos, const Allocator& alloc = Allocator())Precondition: pos <= s.size() and the characters used to initialize the bits must be 0 or 1.
~dynamic_bitset()Effects: Releases the memory associated with this bitset and destroys the bitset object itself.
void swap(dynamic_bitset& b);Effects: The contents of this bitset and bitset b are exchanged.
dynamic_bitset& operator=(const dynamic_bitset& x)Effects: This bitset becomes a copy of the bitset x.
void resize(size_type num_bits, bool value = false);Effects: Changes the number of bits of the bitset to num_bits. If num_bits > size() then the bits in the range [0,size()) remain the same, and the bits in [size(),num_bits) are all set to value. If num_bits < size() then the bits in the range [0,num_bits) stay the same (and the remaining bits are discarded).
void clear()Effects: The size of the bitset becomes zero.
void push_back(bool value);Effects: Increases the size of the bitset by one, and sets the value of the new most-significant bit to value.
void append(Block value);Effects: Appends the bits in value to the bitset (appends to the most-significant end). This increases the size of the bitset by bits_per_block. Let s be the old size of the bitset, then for i in the range [0,bits_per_block), the bit at position (s + i) is set to ((value >> i) & 1).
template <typename BlockInputIterator> void append(BlockInputIterator first, BlockInputIterator last);Effects: This function provides the same end result as the following code, but is typically more efficient.
for (; first != last; ++first) append(*first);Requires: The BlockInputIterator type must be a model of Input Iterator and the value_type must be the same type as Block.
dynamic_bitset& operator&=(const dynamic_bitset& rhs)Requires: this->size() == rhs.size().
for (size_type i = 0; i != this->size(); ++i) (*this)[i] = (*this)[i] & rhs[i];Returns: *this.
dynamic_bitset& operator|=(const dynamic_bitset& rhs)Requires: this->size() == rhs.size().
for (size_type i = 0; i != this->size(); ++i) (*this)[i] = (*this)[i] | rhs[i];Returns: *this.
dynamic_bitset& operator^=(const dynamic_bitset& rhs)Requires: this->size() == rhs.size().
for (size_type i = 0; i != this->size(); ++i) (*this)[i] = (*this)[i] ^ rhs[i];Returns: *this.
dynamic_bitset& operator-=(const dynamic_bitset& rhs)Requires: this->size() == rhs.size().
for (size_type i = 0; i != this->size(); ++i) (*this)[i] = (*this)[i] && !rhs[i];Returns: *this.
dynamic_bitset& operator<<=(size_type n)Effects: Shifts the bits in this bitset to the left by n bits. For each bit in the bitset, the bit at position pos takes on the previous value of the bit at position pos - n, or zero if no such bit exists.
dynamic_bitset& operator>>=(size_type n)Effects: Shifts the bits in this bitset to the right by n bits. For each bit in the bitset, the bit at position pos takes on the previous value of bit pos + n, or zero if no such bit exists.
dynamic_bitset operator<<(size_type n) constReturns: a copy of *this shifted to the left by n bits. For each bit in the returned bitset, the bit at position pos takes on the value of the bit at position pos - n of this bitset, or zero if no such bit exists. Note that the expression b << n is equivalent to constructing a temporary copy of b and then using operator<<=.
dynamic_bitset operator>>(size_type n) constReturns: a copy of *this shifted to the right by n bits. For each bit in the returned bitset, the bit at position pos takes on the value of the bit at position pos + n of this bitset, or zero if no such bit exists. Note that the expression b >> n is equivalent to constructing a temporary copy of b and then using operator>>=.
dynamic_bitset& set()Effects: Sets every bit in this bitset to 1.
dynamic_bitset& flip()Effects: Flips the value of every bit in this bitset.
dynamic_bitset operator~() constReturns: a copy of *this with all of its bits flipped.
dynamic_bitset& reset()Effects: Clears every bit in this bitset.
dynamic_bitset& set(size_type n, bool val = true)Precondition: n < this->size().
dynamic_bitset& reset(size_type n)Precondition: n < this->size().
dynamic_bitset& flip(size_type n)Precondition: n < this->size().
size_type size() constReturns: the number of bits in this bitset.
size_type num_blocks() constReturns: the number of blocks in this bitset.
size_type count() constReturns: the number of bits in this bitset that are set.
bool any() constReturns: true if any bits in this bitset are set, and otherwise returns false.
bool none() constReturns: true if no bits are set, and otherwise returns false.
bool test(size_type n) constPrecondition: n < this->size().
reference operator[](size_type n)Precondition: n < this->size().
bool operator[](size_type n) constPrecondition: n < this->size().
unsigned long to_ulong() constReturns: An unsigned long integer whose bits corresponds to the bits in this bitset, as long as there are no bits in the bitset that are set to 1 at a position greater than sizeof(unsigned long) * CHAR_BIT. Throws: std::overflow_error if the value of the bitset can not be represented in an unsigned long.
bool is_subset_of(const dynamic_bitset& a) constRequires: this->size() == a.size()
bool is_proper_subset_of(const dynamic_bitset& a) constRequires: this->size() == a.size()
bool operator==(const dynamic_bitset& rhs) constReturns: true if this->size() == rhs.size() and if for all i in the range [0,rhs.size()), (*this)[i] == rhs[i]. Otherwise returns false.
bool operator!=(const dynamic_bitset& rhs) constReturns: !((*this) == rhs)
bool operator<(const dynamic_bitset& rhs) constReturns: true if this bitset is lexicographically less than rhs, and returns false otherwise. (See the description of lexicographical_compare for a definition of lexicographic ordering).
bool operator>(const dynamic_bitset& rhs) constReturns: !((*this) < rhs || (*this) == rhs)
bool operator<=(const dynamic_bitset& rhs) constReturns: (*this) < rhs || (*this) == rhs
bool operator>=(const dynamic_bitset& rhs) constReturns: (*this) > rhs || (*this) == rhs
dynamic_bitset operator&(const dynamic_bitset& a, const dynamic_bitset& b)Requires: a.size() == b.size()
dynamic_bitset operator|(const dynamic_bitset& a, const dynamic_bitset& b)Requires: a.size() == b.size()
dynamic_bitset operator^(const dynamic_bitset& a, const dynamic_bitset& b)Requires: a.size() == b.size()
dynamic_bitset operator-(const dynamic_bitset& a, const dynamic_bitset& b)Requires: a.size() == b.size()
template <typename CharT, typename Alloc> void to_string(const dynamic_bitset<Block, Allocator>& b, std::basic_string<Char,Traits,Alloc>& s)Effects: Copies a representation of b into the string s. A character in the string is '1' if the corresponding bit is set, and '0' if it is not. Character position i in the string corresponds to bit position b.size() - 1 - i.
template <typename Block, typename Alloc, typename BlockOutputIterator> void to_block_range(const dynamic_bitset<Block, Alloc>& b, BlockOutputIterator result)Effects: Writes the bits of the bitset into the iterator result a block at a time. The first block written represents the bits in the position range [0,bits_per_block) in the bitset, the second block written the bits in the range [bits_pre_block,2*bits_per_block), and so on. For each block bval written, the bit (bval >> i) & 1 corresponds to the bit at position (b * bits_per_block + i) in the bitset.
template <typename BlockIterator, typename Block, typename Alloc> void from_block_range(BlockIterator first, BlockIterator last, const dynamic_bitset<Block, Alloc>& b)Effects: Reads blocks from the iterator range into the bitset.
template <typename Char, typename Traits, typename Block, typename Alloc> basic_ostream<Char, Traits>& operator<<(basic_ostream<Char, Traits>& os, const dynamic_bitset<Block, Alloc>& x)Output a dynamic_bitset to an output stream. This function behaves as if it converts the dynamic_bitset to a string and then writes that string to the output stream. That is, it is equivalent to
std::basic_string<Char, Traits> s; to_string(x, s): os << s;Throws: An allocation error if memory is exhausted (std::bad_alloc if Allocator=std::allocator). Also will throw std::ios_base::failure if there is a problem writing to the stream.
template <typename Char, typename Traits, typename Block, typename Alloc> basic_istream<Char,Traits>& operator>>(basic_istream<Char,Traits>& is, dynamic_bitset<Block, Alloc>& x)Effects: Extracts a dynamic_bitset from an input stream. This function first skips whitespace, then extracts up to x.size() characters from the input stream. It stops either when it has successfully extracted x.size() characters, or when extraction fails, or when it sees a character that is something other than 1 (in which case it does not extract that character). If extraction is successful, the function then assigns a value to the bitset in the same way as if it were initializing the bitset from a string. So, for example, if the input stream contains the characters "1100abc", it will assign the value 12ul to the bitset, and the next character read from the input stream will be a.
We would like to thank the Boost community for putting in the time to review and accept this library. This library is much better than it ever would have been due to all the suggestions from Boost members. We especially thank Matt Marcus for taking on the task of review manager. Also, a special thanks goes to Gennaro Prota for work on increasing efficiency for many of dynamic_bitset's functions.
Copyright © 2001 |
Jeremy Siek,
Indiana University (jsiek@osl.iu.edu) Chuck Allison, Senior Editor, C/C++ Users Journal (cda@freshsources.com) |