boost::dynamic_bitset::dynamic_bitset

Constructs a bitset from a range of blocks or from an integer.

Synopsis

Declared in <boost/dynamic_bitset/dynamic_bitset.hpp>

template<typename BlockInputIterator>
dynamic_bitset(
    BlockInputIterator first,
    BlockInputIterator last,
    allocator_type const& alloc = allocator_type());

Description

If this constructor is called with a type BlockInputIterator which is actually an integral type or an unscoped enumeration type, the library behaves as if the constructor from a size and a value (which takes the value as an unsigned long long) were called, with arguments static_cast< size_type >( first ), last and alloc, in that order. If BlockInputIterator is a floating‐point type or a scoped enumeration type, the program is ill‐formed (a static_assert fails).

Example

Given:

dynamic_bitset< unsigned short > b( 8, 7 );

b is constructed as if by calling the constructor:

dynamic_bitset(size_type num_bits,
               unsigned long long value,
               const allocator_type & alloc = allocator_type())

with arguments:

static_cast< dynamic_bitset< unsigned short >::size_type >( 8 ),
7,
allocator_type()

Note: At the time of writing (October 2008) this is aligned with the proposed resolution for library issue 438. That is a post C++03 change, and is currently in the working paper for C++0x. Informally speaking, the critical changes with respect to C++03 are the drop of a static_cast on the second argument, and more leeway as to when the templated constructor should have the same effect as the (size, value) one: Only when InputIterator is an integral type, in C++03; 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 dynamic_bitset we adopt the first of these two changes, and we use the leeway given by the second only to take the enumeration types, too, as integers (as the major implementations of the standard containers do).

Otherwise (i.e. if the template argument is neither an arithmetic type nor an enumeration type), constructs a bitset based on a range of blocks. Each element of the range is converted to Block. 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)]). Note that each element makes a whole block, even if the value_type of the iterator is narrower than Block: e.g., a range of two unsigned char`s makes a `dynamic_bitset< unsigned long > of 2 * bits_per_block bits.

Like the range constructors of the standard containers, this constructor isn't explicit.

Throws

An allocation error if memory is exhausted (std::bad_alloc if allocator_type is a std::allocator).

Parameters

Name

Description

first

num_bits if the template argument is an integral or enumeration type, otherwise the start of the range.

last

value if the template argument is an integral or enumeration type, otherwise the end of the range.

alloc

The allocator to use.

Preconditions

  • BlockInputIterator must be either an integral type, an unscoped enumeration type or a model of LegacyInputIterator whose value_type is implicitly convertible to Block and is neither bool nor a floating‐point type (a static_assert checks this): in particular, a range of `bool`s is not a range of blocks.

Created with MrDocs