1 // -----------------------------------------------------------
3 // Copyright (c) 2001-2002 Chuck Allison and Jeremy Siek
4 // Copyright (c) 2003-2006, 2008 Gennaro Prota
5 // Copyright (c) 2014 Ahmed Charles
7 // Copyright (c) 2014 Glen Joseph Fernandes
8 // glenfe at live dot com
9 // Copyright (c) 2014 Riccardo Marcangelo
11 // Distributed under the Boost Software License, Version 1.0.
12 // (See accompanying file LICENSE_1_0.txt or copy at
13 // http://www.boost.org/LICENSE_1_0.txt)
15 // -----------------------------------------------------------
17 #ifndef BOOST_DYNAMIC_BITSET_DYNAMIC_BITSET_HPP
18 #define BOOST_DYNAMIC_BITSET_DYNAMIC_BITSET_HPP
25 #include <climits> // for CHAR_BIT
27 #include "boost/dynamic_bitset/config.hpp"
29 #ifndef BOOST_NO_STD_LOCALE
33 #if defined(BOOST_OLD_IOSTREAMS)
34 # include <iostream.h>
35 # include <ctype.h> // for isspace
41 #include "boost/dynamic_bitset_fwd.hpp"
42 #include "boost/detail/dynamic_bitset.hpp"
43 #include "boost/detail/iterator.hpp" // used to implement append(Iter, Iter)
44 #include "boost/move/move.hpp"
45 #include "boost/limits.hpp"
46 #include "boost/pending/lowest_bit.hpp"
47 #include "boost/static_assert.hpp"
48 #include "boost/utility/addressof.hpp"
49 #include "boost/detail/no_exceptions_support.hpp"
50 #include "boost/throw_exception.hpp"
55 template <typename Block, typename Allocator>
58 // Portability note: member function templates are defined inside
59 // this class definition to avoid problems with VC++. Similarly,
60 // with the member functions of nested classes.
62 // [October 2008: the note above is mostly historical; new versions
63 // of VC++ are likely able to digest a more drinking form of the
64 // code; but changing it now is probably not worth the risks...]
66 BOOST_STATIC_ASSERT((bool)detail::dynamic_bitset_impl::allowed_block_type<Block>::value);
67 typedef std::vector<Block, Allocator> buffer_type;
70 typedef Block block_type;
71 typedef Allocator allocator_type;
72 typedef std::size_t size_type;
73 typedef typename buffer_type::size_type block_width_type;
75 BOOST_STATIC_CONSTANT(block_width_type, bits_per_block = (std::numeric_limits<Block>::digits));
76 BOOST_STATIC_CONSTANT(size_type, npos = static_cast<size_type>(-1));
81 // A proxy class to simulate lvalues of bit type.
85 friend class dynamic_bitset<Block, Allocator>;
88 // the one and only non-copy ctor
89 reference(block_type & b, block_type pos)
91 m_mask( (assert(pos < bits_per_block),
92 block_type(1) << pos )
96 void operator&(); // left undefined
100 // copy constructor: compiler generated
102 operator bool() const { return (m_block & m_mask) != 0; }
103 bool operator~() const { return (m_block & m_mask) == 0; }
105 reference& flip() { do_flip(); return *this; }
107 reference& operator=(bool x) { do_assign(x); return *this; } // for b[i] = x
108 reference& operator=(const reference& rhs) { do_assign(rhs); return *this; } // for b[i] = b[j]
110 reference& operator|=(bool x) { if (x) do_set(); return *this; }
111 reference& operator&=(bool x) { if (!x) do_reset(); return *this; }
112 reference& operator^=(bool x) { if (x) do_flip(); return *this; }
113 reference& operator-=(bool x) { if (x) do_reset(); return *this; }
116 block_type & m_block;
117 const block_type m_mask;
119 void do_set() { m_block |= m_mask; }
120 void do_reset() { m_block &= ~m_mask; }
121 void do_flip() { m_block ^= m_mask; }
122 void do_assign(bool x) { x? do_set() : do_reset(); }
125 typedef bool const_reference;
127 // constructors, etc.
129 dynamic_bitset(const Allocator& alloc = Allocator());
132 dynamic_bitset(size_type num_bits, unsigned long value = 0,
133 const Allocator& alloc = Allocator());
136 // WARNING: you should avoid using this constructor.
138 // A conversion from string is, in most cases, formatting,
139 // and should be performed by using operator>>.
142 // Leave the parentheses around std::basic_string<CharT, Traits, Alloc>::npos.
143 // g++ 3.2 requires them and probably the standard will - see core issue 325
145 // split into two constructors because of bugs in MSVC 6.0sp5 with STLport
147 template <typename CharT, typename Traits, typename Alloc>
148 dynamic_bitset(const std::basic_string<CharT, Traits, Alloc>& s,
149 typename std::basic_string<CharT, Traits, Alloc>::size_type pos,
150 typename std::basic_string<CharT, Traits, Alloc>::size_type n,
151 size_type num_bits = npos,
152 const Allocator& alloc = Allocator())
157 init_from_string(s, pos, n, num_bits);
160 template <typename CharT, typename Traits, typename Alloc>
162 dynamic_bitset(const std::basic_string<CharT, Traits, Alloc>& s,
163 typename std::basic_string<CharT, Traits, Alloc>::size_type pos = 0)
165 :m_bits(Allocator()),
168 init_from_string(s, pos, (std::basic_string<CharT, Traits, Alloc>::npos),
172 // The first bit in *first is the least significant bit, and the
173 // last bit in the block just before *last is the most significant bit.
174 template <typename BlockInputIterator>
175 dynamic_bitset(BlockInputIterator first, BlockInputIterator last,
176 const Allocator& alloc = Allocator())
181 using boost::detail::dynamic_bitset_impl::value_to_type;
182 using boost::detail::dynamic_bitset_impl::is_numeric;
185 is_numeric<BlockInputIterator>::value> selector;
187 dispatch_init(first, last, selector);
190 template <typename T>
191 void dispatch_init(T num_bits, unsigned long value,
192 detail::dynamic_bitset_impl::value_to_type<true>)
194 init_from_unsigned_long(static_cast<size_type>(num_bits), value);
197 template <typename T>
198 void dispatch_init(T first, T last,
199 detail::dynamic_bitset_impl::value_to_type<false>)
201 init_from_block_range(first, last);
204 template <typename BlockIter>
205 void init_from_block_range(BlockIter first, BlockIter last)
207 assert(m_bits.size() == 0);
208 m_bits.insert(m_bits.end(), first, last);
209 m_num_bits = m_bits.size() * bits_per_block;
213 dynamic_bitset(const dynamic_bitset& b);
217 void swap(dynamic_bitset& b);
218 dynamic_bitset& operator=(const dynamic_bitset& b);
220 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
221 dynamic_bitset(dynamic_bitset&& src);
222 dynamic_bitset& operator=(dynamic_bitset&& src);
223 #endif // BOOST_NO_CXX11_RVALUE_REFERENCES
225 allocator_type get_allocator() const;
227 // size changing operations
228 void resize(size_type num_bits, bool value = false);
230 void push_back(bool bit);
232 void append(Block block);
234 template <typename BlockInputIterator>
235 void m_append(BlockInputIterator first, BlockInputIterator last, std::input_iterator_tag)
237 std::vector<Block, Allocator> v(first, last);
238 m_append(v.begin(), v.end(), std::random_access_iterator_tag());
240 template <typename BlockInputIterator>
241 void m_append(BlockInputIterator first, BlockInputIterator last, std::forward_iterator_tag)
243 assert(first != last);
244 block_width_type r = count_extra_bits();
245 std::size_t d = boost::detail::distance(first, last);
246 m_bits.reserve(num_blocks() + d);
248 for( ; first != last; ++first)
249 m_bits.push_back(*first); // could use vector<>::insert()
252 m_highest_block() |= (*first << r);
254 Block b = *first >> (bits_per_block - r);
256 m_bits.push_back(b | (first==last? 0 : *first << r));
257 } while (first != last);
259 m_num_bits += bits_per_block * d;
261 template <typename BlockInputIterator>
262 void append(BlockInputIterator first, BlockInputIterator last) // strong guarantee
265 typename detail::iterator_traits<BlockInputIterator>::iterator_category cat;
266 m_append(first, last, cat);
272 dynamic_bitset& operator&=(const dynamic_bitset& b);
273 dynamic_bitset& operator|=(const dynamic_bitset& b);
274 dynamic_bitset& operator^=(const dynamic_bitset& b);
275 dynamic_bitset& operator-=(const dynamic_bitset& b);
276 dynamic_bitset& operator<<=(size_type n);
277 dynamic_bitset& operator>>=(size_type n);
278 dynamic_bitset operator<<(size_type n) const;
279 dynamic_bitset operator>>(size_type n) const;
281 // basic bit operations
282 dynamic_bitset& set(size_type n, bool val = true);
283 dynamic_bitset& set();
284 dynamic_bitset& reset(size_type n);
285 dynamic_bitset& reset();
286 dynamic_bitset& flip(size_type n);
287 dynamic_bitset& flip();
288 bool test(size_type n) const;
289 bool test_set(size_type n, bool val = true);
293 dynamic_bitset operator~() const;
294 size_type count() const BOOST_NOEXCEPT;
297 reference operator[](size_type pos) {
298 return reference(m_bits[block_index(pos)], bit_index(pos));
300 bool operator[](size_type pos) const { return test(pos); }
302 unsigned long to_ulong() const;
304 size_type size() const BOOST_NOEXCEPT;
305 size_type num_blocks() const BOOST_NOEXCEPT;
306 size_type max_size() const BOOST_NOEXCEPT;
307 bool empty() const BOOST_NOEXCEPT;
308 size_type capacity() const BOOST_NOEXCEPT;
309 void reserve(size_type num_bits);
310 void shrink_to_fit();
312 bool is_subset_of(const dynamic_bitset& a) const;
313 bool is_proper_subset_of(const dynamic_bitset& a) const;
314 bool intersects(const dynamic_bitset & a) const;
317 size_type find_first() const;
318 size_type find_next(size_type pos) const;
321 #if !defined BOOST_DYNAMIC_BITSET_DONT_USE_FRIENDS
322 // lexicographical comparison
323 template <typename B, typename A>
324 friend bool operator==(const dynamic_bitset<B, A>& a,
325 const dynamic_bitset<B, A>& b);
327 template <typename B, typename A>
328 friend bool operator<(const dynamic_bitset<B, A>& a,
329 const dynamic_bitset<B, A>& b);
332 template <typename B, typename A, typename BlockOutputIterator>
333 friend void to_block_range(const dynamic_bitset<B, A>& b,
334 BlockOutputIterator result);
336 template <typename BlockIterator, typename B, typename A>
337 friend void from_block_range(BlockIterator first, BlockIterator last,
338 dynamic_bitset<B, A>& result);
341 template <typename CharT, typename Traits, typename B, typename A>
342 friend std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is,
343 dynamic_bitset<B, A>& b);
345 template <typename B, typename A, typename stringT>
346 friend void to_string_helper(const dynamic_bitset<B, A> & b, stringT & s, bool dump_all);
353 BOOST_STATIC_CONSTANT(block_width_type, ulong_width = std::numeric_limits<unsigned long>::digits);
355 void m_zero_unused_bits();
356 bool m_check_invariants() const;
358 size_type m_do_find_from(size_type first_block) const;
360 block_width_type count_extra_bits() const BOOST_NOEXCEPT { return bit_index(size()); }
361 static size_type block_index(size_type pos) BOOST_NOEXCEPT { return pos / bits_per_block; }
362 static block_width_type bit_index(size_type pos) BOOST_NOEXCEPT { return static_cast<block_width_type>(pos % bits_per_block); }
363 static Block bit_mask(size_type pos) BOOST_NOEXCEPT { return Block(1) << bit_index(pos); }
365 template <typename CharT, typename Traits, typename Alloc>
366 void init_from_string(const std::basic_string<CharT, Traits, Alloc>& s,
367 typename std::basic_string<CharT, Traits, Alloc>::size_type pos,
368 typename std::basic_string<CharT, Traits, Alloc>::size_type n,
371 assert(pos <= s.size());
373 typedef typename std::basic_string<CharT, Traits, Alloc> StrT;
374 typedef typename StrT::traits_type Tr;
376 const typename StrT::size_type rlen = (std::min)(n, s.size() - pos);
377 const size_type sz = ( num_bits != npos? num_bits : rlen);
378 m_bits.resize(calc_num_blocks(sz));
382 BOOST_DYNAMIC_BITSET_CTYPE_FACET(CharT, fac, std::locale());
383 const CharT one = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '1');
385 const size_type m = num_bits < rlen ? num_bits : rlen;
386 typename StrT::size_type i = 0;
389 const CharT c = s[(pos + m - 1) - i];
391 assert( Tr::eq(c, one)
392 || Tr::eq(c, BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '0')) );
401 void init_from_unsigned_long(size_type num_bits,
402 unsigned long value/*,
403 const Allocator& alloc*/)
406 assert(m_bits.size() == 0);
408 m_bits.resize(calc_num_blocks(num_bits));
409 m_num_bits = num_bits;
411 typedef unsigned long num_type;
412 typedef boost::detail::dynamic_bitset_impl
413 ::shifter<num_type, bits_per_block, ulong_width> shifter;
418 // zero out all bits at pos >= num_bits, if any;
419 // note that: num_bits == 0 implies value == 0
420 if (num_bits < static_cast<size_type>(ulong_width)) {
421 const num_type mask = (num_type(1) << num_bits) - 1;
425 typename buffer_type::iterator it = m_bits.begin();
426 for( ; value; shifter::left_shift(value), ++it) {
427 *it = static_cast<block_type>(value);
434 BOOST_DYNAMIC_BITSET_PRIVATE:
436 bool m_unchecked_test(size_type pos) const;
437 static size_type calc_num_blocks(size_type num_bits);
439 Block& m_highest_block();
440 const Block& m_highest_block() const;
443 size_type m_num_bits;
447 friend class bit_appender;
449 // helper for stream >>
450 // Supplies to the lack of an efficient append at the less
451 // significant end: bits are actually appended "at left" but
452 // rearranged in the destructor. From the perspective of
453 // client code everything works *as if* dynamic_bitset<> had
454 // an append_at_right() function (eventually throwing the same
455 // exceptions as push_back) except that the function is in fact
456 // called bit_appender::do_append().
464 bit_appender(const bit_appender &);
465 bit_appender & operator=(const bit_appender &);
468 bit_appender(dynamic_bitset & r) : bs(r), n(0), mask(0), current(0) {}
470 // reverse the order of blocks, shift
471 // if needed, and then resize
473 std::reverse(bs.m_bits.begin(), bs.m_bits.end());
474 const block_width_type offs = bit_index(n);
476 bs >>= (bits_per_block - offs);
477 bs.resize(n); // doesn't enlarge, so can't throw
478 assert(bs.m_check_invariants());
480 inline void do_append(bool value) {
484 current = &bs.m_highest_block();
485 mask = Block(1) << (bits_per_block - 1);
494 size_type get_count() const { return n; }
499 #if !defined BOOST_NO_INCLASS_MEMBER_INITIALIZATION
501 template <typename Block, typename Allocator>
502 const typename dynamic_bitset<Block, Allocator>::block_width_type
503 dynamic_bitset<Block, Allocator>::bits_per_block;
505 template <typename Block, typename Allocator>
506 const typename dynamic_bitset<Block, Allocator>::size_type
507 dynamic_bitset<Block, Allocator>::npos;
509 template <typename Block, typename Allocator>
510 const typename dynamic_bitset<Block, Allocator>::block_width_type
511 dynamic_bitset<Block, Allocator>::ulong_width;
518 template <typename Block, typename Allocator>
519 bool operator!=(const dynamic_bitset<Block, Allocator>& a,
520 const dynamic_bitset<Block, Allocator>& b);
522 template <typename Block, typename Allocator>
523 bool operator<=(const dynamic_bitset<Block, Allocator>& a,
524 const dynamic_bitset<Block, Allocator>& b);
526 template <typename Block, typename Allocator>
527 bool operator>(const dynamic_bitset<Block, Allocator>& a,
528 const dynamic_bitset<Block, Allocator>& b);
530 template <typename Block, typename Allocator>
531 bool operator>=(const dynamic_bitset<Block, Allocator>& a,
532 const dynamic_bitset<Block, Allocator>& b);
535 #ifdef BOOST_OLD_IOSTREAMS
536 template <typename Block, typename Allocator>
537 std::ostream& operator<<(std::ostream& os,
538 const dynamic_bitset<Block, Allocator>& b);
540 template <typename Block, typename Allocator>
541 std::istream& operator>>(std::istream& is, dynamic_bitset<Block,Allocator>& b);
543 template <typename CharT, typename Traits, typename Block, typename Allocator>
544 std::basic_ostream<CharT, Traits>&
545 operator<<(std::basic_ostream<CharT, Traits>& os,
546 const dynamic_bitset<Block, Allocator>& b);
548 template <typename CharT, typename Traits, typename Block, typename Allocator>
549 std::basic_istream<CharT, Traits>&
550 operator>>(std::basic_istream<CharT, Traits>& is,
551 dynamic_bitset<Block, Allocator>& b);
555 template <typename Block, typename Allocator>
556 dynamic_bitset<Block, Allocator>
557 operator&(const dynamic_bitset<Block, Allocator>& b1,
558 const dynamic_bitset<Block, Allocator>& b2);
560 template <typename Block, typename Allocator>
561 dynamic_bitset<Block, Allocator>
562 operator|(const dynamic_bitset<Block, Allocator>& b1,
563 const dynamic_bitset<Block, Allocator>& b2);
565 template <typename Block, typename Allocator>
566 dynamic_bitset<Block, Allocator>
567 operator^(const dynamic_bitset<Block, Allocator>& b1,
568 const dynamic_bitset<Block, Allocator>& b2);
570 template <typename Block, typename Allocator>
571 dynamic_bitset<Block, Allocator>
572 operator-(const dynamic_bitset<Block, Allocator>& b1,
573 const dynamic_bitset<Block, Allocator>& b2);
575 // namespace scope swap
576 template<typename Block, typename Allocator>
577 void swap(dynamic_bitset<Block, Allocator>& b1,
578 dynamic_bitset<Block, Allocator>& b2);
581 template <typename Block, typename Allocator, typename stringT>
583 to_string(const dynamic_bitset<Block, Allocator>& b, stringT & s);
585 template <typename Block, typename Allocator, typename BlockOutputIterator>
587 to_block_range(const dynamic_bitset<Block, Allocator>& b,
588 BlockOutputIterator result);
591 template <typename BlockIterator, typename B, typename A>
593 from_block_range(BlockIterator first, BlockIterator last,
594 dynamic_bitset<B, A>& result)
596 // PRE: distance(first, last) <= numblocks()
597 std::copy (first, last, result.m_bits.begin());
600 //=============================================================================
601 // dynamic_bitset implementation
604 //-----------------------------------------------------------------------------
605 // constructors, etc.
607 template <typename Block, typename Allocator>
608 dynamic_bitset<Block, Allocator>::dynamic_bitset(const Allocator& alloc)
609 : m_bits(alloc), m_num_bits(0)
614 template <typename Block, typename Allocator>
615 dynamic_bitset<Block, Allocator>::
616 dynamic_bitset(size_type num_bits, unsigned long value, const Allocator& alloc)
620 init_from_unsigned_long(num_bits, value);
624 template <typename Block, typename Allocator>
625 inline dynamic_bitset<Block, Allocator>::
626 dynamic_bitset(const dynamic_bitset& b)
627 : m_bits(b.m_bits), m_num_bits(b.m_num_bits)
632 template <typename Block, typename Allocator>
633 inline dynamic_bitset<Block, Allocator>::
636 assert(m_check_invariants());
639 template <typename Block, typename Allocator>
640 inline void dynamic_bitset<Block, Allocator>::
641 swap(dynamic_bitset<Block, Allocator>& b) // no throw
643 std::swap(m_bits, b.m_bits);
644 std::swap(m_num_bits, b.m_num_bits);
647 template <typename Block, typename Allocator>
648 dynamic_bitset<Block, Allocator>& dynamic_bitset<Block, Allocator>::
649 operator=(const dynamic_bitset<Block, Allocator>& b)
652 m_num_bits = b.m_num_bits;
656 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
658 template <typename Block, typename Allocator>
659 inline dynamic_bitset<Block, Allocator>::
660 dynamic_bitset(dynamic_bitset<Block, Allocator>&& b)
661 : m_bits(boost::move(b.m_bits)), m_num_bits(boost::move(b.m_num_bits))
663 // Required so that assert(m_check_invariants()); works.
664 assert((b.m_bits = buffer_type()).empty());
668 template <typename Block, typename Allocator>
669 inline dynamic_bitset<Block, Allocator>& dynamic_bitset<Block, Allocator>::
670 operator=(dynamic_bitset<Block, Allocator>&& b)
672 if (boost::addressof(b) == this) { return *this; }
674 m_bits = boost::move(b.m_bits);
675 m_num_bits = boost::move(b.m_num_bits);
676 // Required so that assert(m_check_invariants()); works.
677 assert((b.m_bits = buffer_type()).empty());
682 #endif // BOOST_NO_CXX11_RVALUE_REFERENCES
684 template <typename Block, typename Allocator>
685 inline typename dynamic_bitset<Block, Allocator>::allocator_type
686 dynamic_bitset<Block, Allocator>::get_allocator() const
688 return m_bits.get_allocator();
691 //-----------------------------------------------------------------------------
692 // size changing operations
694 template <typename Block, typename Allocator>
695 void dynamic_bitset<Block, Allocator>::
696 resize(size_type num_bits, bool value) // strong guarantee
699 const size_type old_num_blocks = num_blocks();
700 const size_type required_blocks = calc_num_blocks(num_bits);
702 const block_type v = value? ~Block(0) : Block(0);
704 if (required_blocks != old_num_blocks) {
705 m_bits.resize(required_blocks, v); // s.g. (copy)
711 // - if the buffer was shrunk, we have nothing more to do,
712 // except a call to m_zero_unused_bits()
714 // - if it was enlarged, all the (used) bits in the new blocks have
715 // the correct value, but we have not yet touched those bits, if
716 // any, that were 'unused bits' before enlarging: if value == true,
719 if (value && (num_bits > m_num_bits)) {
721 const block_width_type extra_bits = count_extra_bits();
723 assert(old_num_blocks >= 1 && old_num_blocks <= m_bits.size());
726 m_bits[old_num_blocks - 1] |= (v << extra_bits);
731 m_num_bits = num_bits;
732 m_zero_unused_bits();
736 template <typename Block, typename Allocator>
737 void dynamic_bitset<Block, Allocator>::
745 template <typename Block, typename Allocator>
746 void dynamic_bitset<Block, Allocator>::
749 const size_type sz = size();
754 template <typename Block, typename Allocator>
755 void dynamic_bitset<Block, Allocator>::
758 const size_type old_num_blocks = num_blocks();
759 const size_type required_blocks = calc_num_blocks(m_num_bits - 1);
761 if (required_blocks != old_num_blocks) {
766 m_zero_unused_bits();
770 template <typename Block, typename Allocator>
771 void dynamic_bitset<Block, Allocator>::
772 append(Block value) // strong guarantee
774 const block_width_type r = count_extra_bits();
777 // the buffer is empty, or all blocks are filled
778 m_bits.push_back(value);
781 m_bits.push_back(value >> (bits_per_block - r));
782 m_bits[m_bits.size() - 2] |= (value << r); // m_bits.size() >= 2
785 m_num_bits += bits_per_block;
786 assert(m_check_invariants());
791 //-----------------------------------------------------------------------------
793 template <typename Block, typename Allocator>
794 dynamic_bitset<Block, Allocator>&
795 dynamic_bitset<Block, Allocator>::operator&=(const dynamic_bitset& rhs)
797 assert(size() == rhs.size());
798 for (size_type i = 0; i < num_blocks(); ++i)
799 m_bits[i] &= rhs.m_bits[i];
803 template <typename Block, typename Allocator>
804 dynamic_bitset<Block, Allocator>&
805 dynamic_bitset<Block, Allocator>::operator|=(const dynamic_bitset& rhs)
807 assert(size() == rhs.size());
808 for (size_type i = 0; i < num_blocks(); ++i)
809 m_bits[i] |= rhs.m_bits[i];
810 //m_zero_unused_bits();
814 template <typename Block, typename Allocator>
815 dynamic_bitset<Block, Allocator>&
816 dynamic_bitset<Block, Allocator>::operator^=(const dynamic_bitset& rhs)
818 assert(size() == rhs.size());
819 for (size_type i = 0; i < this->num_blocks(); ++i)
820 m_bits[i] ^= rhs.m_bits[i];
821 //m_zero_unused_bits();
825 template <typename Block, typename Allocator>
826 dynamic_bitset<Block, Allocator>&
827 dynamic_bitset<Block, Allocator>::operator-=(const dynamic_bitset& rhs)
829 assert(size() == rhs.size());
830 for (size_type i = 0; i < num_blocks(); ++i)
831 m_bits[i] &= ~rhs.m_bits[i];
832 //m_zero_unused_bits();
838 // Note that the 'if (r != 0)' is crucial to avoid undefined
839 // behavior when the left hand operand of >> isn't promoted to a
840 // wider type (because rs would be too large).
842 template <typename Block, typename Allocator>
843 dynamic_bitset<Block, Allocator>&
844 dynamic_bitset<Block, Allocator>::operator<<=(size_type n)
851 size_type const last = num_blocks() - 1; // num_blocks() is >= 1
852 size_type const div = n / bits_per_block; // div is <= last
853 block_width_type const r = bit_index(n);
854 block_type * const b = &m_bits[0];
858 block_width_type const rs = bits_per_block - r;
860 for (size_type i = last-div; i>0; --i) {
861 b[i+div] = (b[i] << r) | (b[i-1] >> rs);
867 for (size_type i = last-div; i>0; --i) {
873 // zero out div blocks at the less significant end
874 std::fill_n(m_bits.begin(), div, static_cast<block_type>(0));
876 // zero out any 1 bit that flowed into the unused part
877 m_zero_unused_bits(); // thanks to Lester Gong
889 // see the comments to operator <<=
891 template <typename B, typename A>
892 dynamic_bitset<B, A> & dynamic_bitset<B, A>::operator>>=(size_type n) {
893 if (n >= m_num_bits) {
899 size_type const last = num_blocks() - 1; // num_blocks() is >= 1
900 size_type const div = n / bits_per_block; // div is <= last
901 block_width_type const r = bit_index(n);
902 block_type * const b = &m_bits[0];
907 block_width_type const ls = bits_per_block - r;
909 for (size_type i = div; i < last; ++i) {
910 b[i-div] = (b[i] >> r) | (b[i+1] << ls);
913 b[last-div] = b[last] >> r;
917 for (size_type i = div; i <= last; ++i) {
920 // note the '<=': the last iteration 'absorbs'
921 // b[last-div] = b[last] >> 0;
926 // div blocks are zero filled at the most significant end
927 std::fill_n(m_bits.begin() + (num_blocks()-div), div, static_cast<block_type>(0));
934 template <typename Block, typename Allocator>
935 dynamic_bitset<Block, Allocator>
936 dynamic_bitset<Block, Allocator>::operator<<(size_type n) const
938 dynamic_bitset r(*this);
942 template <typename Block, typename Allocator>
943 dynamic_bitset<Block, Allocator>
944 dynamic_bitset<Block, Allocator>::operator>>(size_type n) const
946 dynamic_bitset r(*this);
951 //-----------------------------------------------------------------------------
952 // basic bit operations
954 template <typename Block, typename Allocator>
955 dynamic_bitset<Block, Allocator>&
956 dynamic_bitset<Block, Allocator>::set(size_type pos, bool val)
958 assert(pos < m_num_bits);
961 m_bits[block_index(pos)] |= bit_mask(pos);
968 template <typename Block, typename Allocator>
969 dynamic_bitset<Block, Allocator>&
970 dynamic_bitset<Block, Allocator>::set()
972 std::fill(m_bits.begin(), m_bits.end(), ~Block(0));
973 m_zero_unused_bits();
977 template <typename Block, typename Allocator>
978 dynamic_bitset<Block, Allocator>&
979 dynamic_bitset<Block, Allocator>::reset(size_type pos)
981 assert(pos < m_num_bits);
982 #if defined __MWERKS__ && BOOST_WORKAROUND(__MWERKS__, <= 0x3003) // 8.x
983 // CodeWarrior 8 generates incorrect code when the &=~ is compiled,
984 // use the |^ variation instead.. <grafik>
985 m_bits[block_index(pos)] |= bit_mask(pos);
986 m_bits[block_index(pos)] ^= bit_mask(pos);
988 m_bits[block_index(pos)] &= ~bit_mask(pos);
993 template <typename Block, typename Allocator>
994 dynamic_bitset<Block, Allocator>&
995 dynamic_bitset<Block, Allocator>::reset()
997 std::fill(m_bits.begin(), m_bits.end(), Block(0));
1001 template <typename Block, typename Allocator>
1002 dynamic_bitset<Block, Allocator>&
1003 dynamic_bitset<Block, Allocator>::flip(size_type pos)
1005 assert(pos < m_num_bits);
1006 m_bits[block_index(pos)] ^= bit_mask(pos);
1010 template <typename Block, typename Allocator>
1011 dynamic_bitset<Block, Allocator>&
1012 dynamic_bitset<Block, Allocator>::flip()
1014 for (size_type i = 0; i < num_blocks(); ++i)
1015 m_bits[i] = ~m_bits[i];
1016 m_zero_unused_bits();
1020 template <typename Block, typename Allocator>
1021 bool dynamic_bitset<Block, Allocator>::m_unchecked_test(size_type pos) const
1023 return (m_bits[block_index(pos)] & bit_mask(pos)) != 0;
1026 template <typename Block, typename Allocator>
1027 bool dynamic_bitset<Block, Allocator>::test(size_type pos) const
1029 assert(pos < m_num_bits);
1030 return m_unchecked_test(pos);
1033 template <typename Block, typename Allocator>
1034 bool dynamic_bitset<Block, Allocator>::test_set(size_type pos, bool val)
1036 bool const b = test(pos);
1043 template <typename Block, typename Allocator>
1044 bool dynamic_bitset<Block, Allocator>::all() const
1050 const block_width_type extra_bits = count_extra_bits();
1051 block_type const all_ones = ~static_cast<Block>(0);
1053 if (extra_bits == 0) {
1054 for (size_type i = 0, e = num_blocks(); i < e; ++i) {
1055 if (m_bits[i] != all_ones) {
1060 for (size_type i = 0, e = num_blocks() - 1; i < e; ++i) {
1061 if (m_bits[i] != all_ones) {
1065 block_type const mask = ~(~static_cast<Block>(0) << extra_bits);
1066 if (m_highest_block() != mask) {
1073 template <typename Block, typename Allocator>
1074 bool dynamic_bitset<Block, Allocator>::any() const
1076 for (size_type i = 0; i < num_blocks(); ++i)
1082 template <typename Block, typename Allocator>
1083 inline bool dynamic_bitset<Block, Allocator>::none() const
1088 template <typename Block, typename Allocator>
1089 dynamic_bitset<Block, Allocator>
1090 dynamic_bitset<Block, Allocator>::operator~() const
1092 dynamic_bitset b(*this);
1097 template <typename Block, typename Allocator>
1098 typename dynamic_bitset<Block, Allocator>::size_type
1099 dynamic_bitset<Block, Allocator>::count() const BOOST_NOEXCEPT
1101 using detail::dynamic_bitset_impl::table_width;
1102 using detail::dynamic_bitset_impl::access_by_bytes;
1103 using detail::dynamic_bitset_impl::access_by_blocks;
1104 using detail::dynamic_bitset_impl::value_to_type;
1106 #if BOOST_WORKAROUND(__GNUC__, == 4) && (__GNUC_MINOR__ == 3) && (__GNUC_PATCHLEVEL__ == 3)
1107 // NOTE: Explicit qualification of "bits_per_block"
1108 // breaks compilation on gcc 4.3.3
1109 enum { no_padding = bits_per_block == CHAR_BIT * sizeof(Block) };
1111 // NOTE: Explicitly qualifying "bits_per_block" to workaround
1112 // regressions of gcc 3.4.x
1114 dynamic_bitset<Block, Allocator>::bits_per_block
1115 == CHAR_BIT * sizeof(Block) };
1118 enum { enough_table_width = table_width >= CHAR_BIT };
1120 enum { mode = (no_padding && enough_table_width)
1122 : access_by_blocks };
1124 return do_count(m_bits.begin(), num_blocks(), Block(0),
1125 static_cast<value_to_type<(bool)mode> *>(0));
1129 //-----------------------------------------------------------------------------
1133 template <typename B, typename A, typename stringT>
1134 void to_string_helper(const dynamic_bitset<B, A> & b, stringT & s,
1137 typedef typename stringT::traits_type Tr;
1138 typedef typename stringT::value_type Ch;
1140 BOOST_DYNAMIC_BITSET_CTYPE_FACET(Ch, fac, std::locale());
1141 const Ch zero = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '0');
1142 const Ch one = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '1');
1144 // Note that this function may access (when
1145 // dump_all == true) bits beyond position size() - 1
1147 typedef typename dynamic_bitset<B, A>::size_type size_type;
1149 const size_type len = dump_all?
1150 dynamic_bitset<B, A>::bits_per_block * b.num_blocks():
1152 s.assign (len, zero);
1154 for (size_type i = 0; i < len; ++i) {
1155 if (b.m_unchecked_test(i))
1156 Tr::assign(s[len - 1 - i], one);
1163 // A comment similar to the one about the constructor from
1164 // basic_string can be done here. Thanks to James Kanze for
1165 // making me (Gennaro) realize this important separation of
1166 // concerns issue, as well as many things about i18n.
1168 template <typename Block, typename Allocator, typename stringT>
1170 to_string(const dynamic_bitset<Block, Allocator>& b, stringT& s)
1172 to_string_helper(b, s, false);
1176 // Differently from to_string this function dumps out
1177 // every bit of the internal representation (may be
1178 // useful for debugging purposes)
1180 template <typename B, typename A, typename stringT>
1182 dump_to_string(const dynamic_bitset<B, A>& b, stringT& s)
1184 to_string_helper(b, s, true /* =dump_all*/);
1187 template <typename Block, typename Allocator, typename BlockOutputIterator>
1189 to_block_range(const dynamic_bitset<Block, Allocator>& b,
1190 BlockOutputIterator result)
1192 // note how this copies *all* bits, including the
1193 // unused ones in the last block (which are zero)
1194 std::copy(b.m_bits.begin(), b.m_bits.end(), result);
1197 template <typename Block, typename Allocator>
1198 unsigned long dynamic_bitset<Block, Allocator>::
1202 if (m_num_bits == 0)
1203 return 0; // convention
1205 // Check for overflows. This may be a performance burden on very
1206 // large bitsets but is required by the specification, sorry
1207 if (find_next(ulong_width - 1) != npos)
1208 BOOST_THROW_EXCEPTION(std::overflow_error("boost::dynamic_bitset::to_ulong overflow"));
1211 // Ok, from now on we can be sure there's no "on" bit
1212 // beyond the "allowed" positions
1213 typedef unsigned long result_type;
1215 const size_type maximum_size =
1216 (std::min)(m_num_bits, static_cast<size_type>(ulong_width));
1218 const size_type last_block = block_index( maximum_size - 1 );
1220 assert((last_block * bits_per_block) < static_cast<size_type>(ulong_width));
1222 result_type result = 0;
1223 for (size_type i = 0; i <= last_block; ++i) {
1224 const size_type offset = i * bits_per_block;
1225 result |= (static_cast<result_type>(m_bits[i]) << offset);
1231 template <typename Block, typename Allocator>
1232 inline typename dynamic_bitset<Block, Allocator>::size_type
1233 dynamic_bitset<Block, Allocator>::size() const BOOST_NOEXCEPT
1238 template <typename Block, typename Allocator>
1239 inline typename dynamic_bitset<Block, Allocator>::size_type
1240 dynamic_bitset<Block, Allocator>::num_blocks() const BOOST_NOEXCEPT
1242 return m_bits.size();
1245 template <typename Block, typename Allocator>
1246 inline typename dynamic_bitset<Block, Allocator>::size_type
1247 dynamic_bitset<Block, Allocator>::max_size() const BOOST_NOEXCEPT
1249 // Semantics of vector<>::max_size() aren't very clear
1250 // (see lib issue 197) and many library implementations
1251 // simply return dummy values, _unrelated_ to the underlying
1254 // Given these problems, I was tempted to not provide this
1255 // function at all but the user could need it if he provides
1256 // his own allocator.
1259 const size_type m = detail::dynamic_bitset_impl::
1260 vector_max_size_workaround(m_bits);
1262 return m <= (size_type(-1)/bits_per_block) ?
1263 m * bits_per_block :
1267 template <typename Block, typename Allocator>
1268 inline bool dynamic_bitset<Block, Allocator>::empty() const BOOST_NOEXCEPT
1273 template <typename Block, typename Allocator>
1274 inline typename dynamic_bitset<Block, Allocator>::size_type
1275 dynamic_bitset<Block, Allocator>::capacity() const BOOST_NOEXCEPT
1277 return m_bits.capacity() * bits_per_block;
1280 template <typename Block, typename Allocator>
1281 inline void dynamic_bitset<Block, Allocator>::reserve(size_type num_bits)
1283 m_bits.reserve(calc_num_blocks(num_bits));
1286 template <typename Block, typename Allocator>
1287 void dynamic_bitset<Block, Allocator>::shrink_to_fit()
1289 if (m_bits.size() < m_bits.capacity()) {
1290 buffer_type(m_bits).swap(m_bits);
1294 template <typename Block, typename Allocator>
1295 bool dynamic_bitset<Block, Allocator>::
1296 is_subset_of(const dynamic_bitset<Block, Allocator>& a) const
1298 assert(size() == a.size());
1299 for (size_type i = 0; i < num_blocks(); ++i)
1300 if (m_bits[i] & ~a.m_bits[i])
1305 template <typename Block, typename Allocator>
1306 bool dynamic_bitset<Block, Allocator>::
1307 is_proper_subset_of(const dynamic_bitset<Block, Allocator>& a) const
1309 assert(size() == a.size());
1310 assert(num_blocks() == a.num_blocks());
1312 bool proper = false;
1313 for (size_type i = 0; i < num_blocks(); ++i) {
1314 const Block & bt = m_bits[i];
1315 const Block & ba = a.m_bits[i];
1318 return false; // not a subset at all
1325 template <typename Block, typename Allocator>
1326 bool dynamic_bitset<Block, Allocator>::intersects(const dynamic_bitset & b) const
1328 size_type common_blocks = num_blocks() < b.num_blocks()
1329 ? num_blocks() : b.num_blocks();
1331 for(size_type i = 0; i < common_blocks; ++i) {
1332 if(m_bits[i] & b.m_bits[i])
1338 // --------------------------------
1342 // look for the first bit "on", starting
1343 // from the block with index first_block
1345 template <typename Block, typename Allocator>
1346 typename dynamic_bitset<Block, Allocator>::size_type
1347 dynamic_bitset<Block, Allocator>::m_do_find_from(size_type first_block) const
1349 size_type i = first_block;
1352 while (i < num_blocks() && m_bits[i] == 0)
1355 if (i >= num_blocks())
1356 return npos; // not found
1358 return i * bits_per_block + static_cast<size_type>(boost::lowest_bit(m_bits[i]));
1363 template <typename Block, typename Allocator>
1364 typename dynamic_bitset<Block, Allocator>::size_type
1365 dynamic_bitset<Block, Allocator>::find_first() const
1367 return m_do_find_from(0);
1371 template <typename Block, typename Allocator>
1372 typename dynamic_bitset<Block, Allocator>::size_type
1373 dynamic_bitset<Block, Allocator>::find_next(size_type pos) const
1376 const size_type sz = size();
1377 if (pos >= (sz-1) || sz == 0)
1382 const size_type blk = block_index(pos);
1383 const block_width_type ind = bit_index(pos);
1385 // shift bits upto one immediately after current
1386 const Block fore = m_bits[blk] >> ind;
1389 pos + static_cast<size_type>(lowest_bit(fore))
1391 m_do_find_from(blk + 1);
1397 //-----------------------------------------------------------------------------
1400 template <typename Block, typename Allocator>
1401 bool operator==(const dynamic_bitset<Block, Allocator>& a,
1402 const dynamic_bitset<Block, Allocator>& b)
1404 return (a.m_num_bits == b.m_num_bits)
1405 && (a.m_bits == b.m_bits);
1408 template <typename Block, typename Allocator>
1409 inline bool operator!=(const dynamic_bitset<Block, Allocator>& a,
1410 const dynamic_bitset<Block, Allocator>& b)
1415 template <typename Block, typename Allocator>
1416 bool operator<(const dynamic_bitset<Block, Allocator>& a,
1417 const dynamic_bitset<Block, Allocator>& b)
1419 assert(a.size() == b.size());
1420 typedef typename dynamic_bitset<Block, Allocator>::size_type size_type;
1422 //if (a.size() == 0)
1425 // Since we are storing the most significant bit
1426 // at pos == size() - 1, we need to do the comparisons in reverse.
1428 for (size_type ii = a.num_blocks(); ii > 0; --ii) {
1430 if (a.m_bits[i] < b.m_bits[i])
1432 else if (a.m_bits[i] > b.m_bits[i])
1438 template <typename Block, typename Allocator>
1439 inline bool operator<=(const dynamic_bitset<Block, Allocator>& a,
1440 const dynamic_bitset<Block, Allocator>& b)
1445 template <typename Block, typename Allocator>
1446 inline bool operator>(const dynamic_bitset<Block, Allocator>& a,
1447 const dynamic_bitset<Block, Allocator>& b)
1452 template <typename Block, typename Allocator>
1453 inline bool operator>=(const dynamic_bitset<Block, Allocator>& a,
1454 const dynamic_bitset<Block, Allocator>& b)
1459 //-----------------------------------------------------------------------------
1460 // stream operations
1462 #ifdef BOOST_OLD_IOSTREAMS
1463 template < typename Block, typename Alloc>
1465 operator<<(std::ostream& os, const dynamic_bitset<Block, Alloc>& b)
1467 // NOTE: since this is aimed at "classic" iostreams, exception
1468 // masks on the stream are not supported. The library that
1469 // ships with gcc 2.95 has an exceptions() member function but
1470 // nothing is actually implemented; not even the class ios::failure.
1472 using namespace std;
1474 const ios::iostate ok = ios::goodbit;
1475 ios::iostate err = ok;
1480 typedef typename dynamic_bitset<Block, Alloc>::size_type bitsetsize_type;
1482 const bitsetsize_type sz = b.size();
1483 std::streambuf * buf = os.rdbuf();
1484 size_t npad = os.width() <= 0 // careful: os.width() is signed (and can be < 0)
1485 || (bitsetsize_type) os.width() <= sz? 0 : os.width() - sz;
1487 const char fill_char = os.fill();
1488 const ios::fmtflags adjustfield = os.flags() & ios::adjustfield;
1490 // if needed fill at left; pad is decresed along the way
1491 if (adjustfield != ios::left) {
1492 for (; 0 < npad; --npad)
1493 if (fill_char != buf->sputc(fill_char)) {
1494 err |= ios::failbit;
1500 // output the bitset
1501 for (bitsetsize_type i = b.size(); 0 < i; --i) {
1502 const char dig = b.test(i-1)? '1' : '0';
1503 if (EOF == buf->sputc(dig)) {
1504 err |= ios::failbit;
1511 // if needed fill at right
1512 for (; 0 < npad; --npad) {
1513 if (fill_char != buf->sputc(fill_char)) {
1514 err |= ios::failbit;
1526 os.setstate(err); // assume this does NOT throw
1532 template <typename Ch, typename Tr, typename Block, typename Alloc>
1533 std::basic_ostream<Ch, Tr>&
1534 operator<<(std::basic_ostream<Ch, Tr>& os,
1535 const dynamic_bitset<Block, Alloc>& b)
1538 using namespace std;
1540 const ios_base::iostate ok = ios_base::goodbit;
1541 ios_base::iostate err = ok;
1543 typename basic_ostream<Ch, Tr>::sentry cerberos(os);
1546 BOOST_DYNAMIC_BITSET_CTYPE_FACET(Ch, fac, os.getloc());
1547 const Ch zero = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '0');
1548 const Ch one = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '1');
1552 typedef typename dynamic_bitset<Block, Alloc>::size_type bitset_size_type;
1553 typedef basic_streambuf<Ch, Tr> buffer_type;
1555 buffer_type * buf = os.rdbuf();
1556 // careful: os.width() is signed (and can be < 0)
1557 const bitset_size_type width = (os.width() <= 0) ? 0 : static_cast<bitset_size_type>(os.width());
1558 streamsize npad = (width <= b.size()) ? 0 : width - b.size();
1560 const Ch fill_char = os.fill();
1561 const ios_base::fmtflags adjustfield = os.flags() & ios_base::adjustfield;
1563 // if needed fill at left; pad is decreased along the way
1564 if (adjustfield != ios_base::left) {
1565 for (; 0 < npad; --npad)
1566 if (Tr::eq_int_type(Tr::eof(), buf->sputc(fill_char))) {
1567 err |= ios_base::failbit;
1573 // output the bitset
1574 for (bitset_size_type i = b.size(); 0 < i; --i) {
1575 typename buffer_type::int_type
1576 ret = buf->sputc(b.test(i-1)? one : zero);
1577 if (Tr::eq_int_type(Tr::eof(), ret)) {
1578 err |= ios_base::failbit;
1585 // if needed fill at right
1586 for (; 0 < npad; --npad) {
1587 if (Tr::eq_int_type(Tr::eof(), buf->sputc(fill_char))) {
1588 err |= ios_base::failbit;
1597 } BOOST_CATCH (...) { // see std 27.6.1.1/4
1598 bool rethrow = false;
1599 BOOST_TRY { os.setstate(ios_base::failbit); } BOOST_CATCH (...) { rethrow = true; } BOOST_CATCH_END
1608 os.setstate(err); // may throw exception
1615 #ifdef BOOST_OLD_IOSTREAMS
1617 // A sentry-like class that calls isfx in its destructor.
1618 // "Necessary" because bit_appender::do_append may throw.
1619 class pseudo_sentry {
1623 explicit pseudo_sentry(std::istream & r) : m_r(r), m_ok(r.ipfx(0)) { }
1624 ~pseudo_sentry() { m_r.isfx(); }
1625 operator bool() const { return m_ok; }
1628 template <typename Block, typename Alloc>
1630 operator>>(std::istream& is, dynamic_bitset<Block, Alloc>& b)
1633 // Extractor for classic IO streams (libstdc++ < 3.0)
1634 // ----------------------------------------------------//
1635 // It's assumed that the stream buffer functions, and
1636 // the stream's setstate() _cannot_ throw.
1639 typedef dynamic_bitset<Block, Alloc> bitset_type;
1640 typedef typename bitset_type::size_type size_type;
1642 std::ios::iostate err = std::ios::goodbit;
1643 pseudo_sentry cerberos(is); // skips whitespaces
1648 const std::streamsize w = is.width();
1649 const size_type limit = w > 0 && static_cast<size_type>(w) < b.max_size()
1650 ? static_cast<size_type>(w) : b.max_size();
1651 typename bitset_type::bit_appender appender(b);
1652 std::streambuf * buf = is.rdbuf();
1653 for(int c = buf->sgetc(); appender.get_count() < limit; c = buf->snextc() ) {
1656 err |= std::ios::eofbit;
1659 else if (char(c) != '0' && char(c) != '1')
1660 break; // non digit character
1664 appender.do_append(char(c) == '1');
1667 is.setstate(std::ios::failbit); // assume this can't throw
1678 err |= std::ios::failbit;
1679 if (err != std::ios::goodbit)
1680 is.setstate (err); // may throw
1685 #else // BOOST_OLD_IOSTREAMS
1687 template <typename Ch, typename Tr, typename Block, typename Alloc>
1688 std::basic_istream<Ch, Tr>&
1689 operator>>(std::basic_istream<Ch, Tr>& is, dynamic_bitset<Block, Alloc>& b)
1692 using namespace std;
1694 typedef dynamic_bitset<Block, Alloc> bitset_type;
1695 typedef typename bitset_type::size_type size_type;
1697 const streamsize w = is.width();
1698 const size_type limit = 0 < w && static_cast<size_type>(w) < b.max_size()?
1699 static_cast<size_type>(w) : b.max_size();
1701 ios_base::iostate err = ios_base::goodbit;
1702 typename basic_istream<Ch, Tr>::sentry cerberos(is); // skips whitespaces
1705 // in accordance with prop. resol. of lib DR 303 [last checked 4 Feb 2004]
1706 BOOST_DYNAMIC_BITSET_CTYPE_FACET(Ch, fac, is.getloc());
1707 const Ch zero = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '0');
1708 const Ch one = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '1');
1712 typename bitset_type::bit_appender appender(b);
1713 basic_streambuf <Ch, Tr> * buf = is.rdbuf();
1714 typename Tr::int_type c = buf->sgetc();
1715 for( ; appender.get_count() < limit; c = buf->snextc() ) {
1717 if (Tr::eq_int_type(Tr::eof(), c)) {
1718 err |= ios_base::eofbit;
1722 const Ch to_c = Tr::to_char_type(c);
1723 const bool is_one = Tr::eq(to_c, one);
1725 if (!is_one && !Tr::eq(to_c, zero))
1726 break; // non digit character
1728 appender.do_append(is_one);
1735 // catches from stream buf, or from vector:
1737 // bits_stored bits have been extracted and stored, and
1738 // either no further character is extractable or we can't
1739 // append to the underlying vector (out of memory)
1741 bool rethrow = false; // see std 27.6.1.1/4
1742 BOOST_TRY { is.setstate(ios_base::badbit); }
1743 BOOST_CATCH(...) { rethrow = true; }
1754 if (b.size() == 0 /*|| !cerberos*/)
1755 err |= ios_base::failbit;
1756 if (err != ios_base::goodbit)
1757 is.setstate (err); // may throw
1767 //-----------------------------------------------------------------------------
1768 // bitset operations
1770 template <typename Block, typename Allocator>
1771 dynamic_bitset<Block, Allocator>
1772 operator&(const dynamic_bitset<Block, Allocator>& x,
1773 const dynamic_bitset<Block, Allocator>& y)
1775 dynamic_bitset<Block, Allocator> b(x);
1779 template <typename Block, typename Allocator>
1780 dynamic_bitset<Block, Allocator>
1781 operator|(const dynamic_bitset<Block, Allocator>& x,
1782 const dynamic_bitset<Block, Allocator>& y)
1784 dynamic_bitset<Block, Allocator> b(x);
1788 template <typename Block, typename Allocator>
1789 dynamic_bitset<Block, Allocator>
1790 operator^(const dynamic_bitset<Block, Allocator>& x,
1791 const dynamic_bitset<Block, Allocator>& y)
1793 dynamic_bitset<Block, Allocator> b(x);
1797 template <typename Block, typename Allocator>
1798 dynamic_bitset<Block, Allocator>
1799 operator-(const dynamic_bitset<Block, Allocator>& x,
1800 const dynamic_bitset<Block, Allocator>& y)
1802 dynamic_bitset<Block, Allocator> b(x);
1806 //-----------------------------------------------------------------------------
1807 // namespace scope swap
1809 template<typename Block, typename Allocator>
1811 swap(dynamic_bitset<Block, Allocator>& left,
1812 dynamic_bitset<Block, Allocator>& right) // no throw
1818 //-----------------------------------------------------------------------------
1819 // private (on conforming compilers) member functions
1822 template <typename Block, typename Allocator>
1823 inline typename dynamic_bitset<Block, Allocator>::size_type
1824 dynamic_bitset<Block, Allocator>::calc_num_blocks(size_type num_bits)
1826 return num_bits / bits_per_block
1827 + static_cast<size_type>( num_bits % bits_per_block != 0 );
1830 // gives a reference to the highest block
1832 template <typename Block, typename Allocator>
1833 inline Block& dynamic_bitset<Block, Allocator>::m_highest_block()
1835 return const_cast<Block &>
1836 (static_cast<const dynamic_bitset *>(this)->m_highest_block());
1839 // gives a const-reference to the highest block
1841 template <typename Block, typename Allocator>
1842 inline const Block& dynamic_bitset<Block, Allocator>::m_highest_block() const
1844 assert(size() > 0 && num_blocks() > 0);
1845 return m_bits.back();
1849 // If size() is not a multiple of bits_per_block
1850 // then not all the bits in the last block are used.
1851 // This function resets the unused bits (convenient
1852 // for the implementation of many member functions)
1854 template <typename Block, typename Allocator>
1855 inline void dynamic_bitset<Block, Allocator>::m_zero_unused_bits()
1857 assert (num_blocks() == calc_num_blocks(m_num_bits));
1859 // if != 0 this is the number of bits used in the last block
1860 const block_width_type extra_bits = count_extra_bits();
1862 if (extra_bits != 0)
1863 m_highest_block() &= ~(~static_cast<Block>(0) << extra_bits);
1867 // check class invariants
1868 template <typename Block, typename Allocator>
1869 bool dynamic_bitset<Block, Allocator>::m_check_invariants() const
1871 const block_width_type extra_bits = count_extra_bits();
1872 if (extra_bits > 0) {
1873 block_type const mask = (~static_cast<Block>(0) << extra_bits);
1874 if ((m_highest_block() & mask) != 0)
1877 if (m_bits.size() > m_bits.capacity() || num_blocks() != calc_num_blocks(size()))
1885 } // namespace boost
1888 #undef BOOST_BITSET_CHAR
1890 #endif // include guard