sl@0: //======================================================================= sl@0: // Copyright 2001 Jeremy G. Siek sl@0: // Authors: Jeremy G. Siek sl@0: // sl@0: // Distributed under the Boost Software License, Version 1.0. (See sl@0: // accompanying file LICENSE_1_0.txt or copy at sl@0: // http://www.boost.org/LICENSE_1_0.txt) sl@0: //======================================================================= sl@0: sl@0: /* sl@0: * Copyright (c) 1998 sl@0: * Silicon Graphics Computer Systems, Inc. sl@0: * sl@0: * Permission to use, copy, modify, distribute and sell this software sl@0: * and its documentation for any purpose is hereby granted without fee, sl@0: * provided that the above copyright notice appear in all copies and sl@0: * that both that copyright notice and this permission notice appear sl@0: * in supporting documentation. Silicon Graphics makes no sl@0: * representations about the suitability of this software for any sl@0: * purpose. It is provided "as is" without express or implied warranty. sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: // This provides versions of std::bitset with both static and dynamic size. sl@0: sl@0: // UNDER CONSTRUCTION sl@0: sl@0: sl@0: // replace this later sl@0: #include sl@0: #define BOOST_ASSERT_THROW(expr, except) assert(expr) sl@0: sl@0: namespace boost { sl@0: sl@0: namespace detail { sl@0: // structure to aid in counting bits sl@0: template sl@0: struct bit_count { sl@0: static unsigned char value[256]; sl@0: }; sl@0: sl@0: // Mapping from 8 bit unsigned integers to the index of the first bit sl@0: template sl@0: struct first_bit_location { sl@0: static unsigned char value[256]; sl@0: }; sl@0: sl@0: template // this size is in bits sl@0: struct word_traits { sl@0: typedef WordType word_type; sl@0: static const std::size_t word_size = CHAR_BIT * sizeof(word_type); sl@0: }; sl@0: sl@0: //========================================================================= sl@0: template sl@0: class bitset_base sl@0: : public bitset_adaptor< SizeType, sl@0: bitset_base > sl@0: { sl@0: // private: sl@0: public: sl@0: typedef SizeType size_type; sl@0: typedef typename WordTraits::word_type word_type; sl@0: sl@0: static size_type s_which_word(size_type pos) { sl@0: return pos / WordTraits::word_size; sl@0: } sl@0: static size_type s_which_byte(size_type pos) { sl@0: return (pos % WordTraits::word_size) / CHAR_BIT; sl@0: } sl@0: static size_type s_which_bit(size_type pos) { sl@0: return pos % WordTraits::word_size; sl@0: } sl@0: static word_type s_mask_bit(size_type pos) { sl@0: return (static_cast(1)) << s_which_bit(pos); sl@0: } sl@0: word_type& m_get_word(size_type pos) { sl@0: return data()[s_which_word(pos)]; sl@0: } sl@0: word_type m_get_word(size_type pos) const { sl@0: return data()[s_which_word(pos)]; sl@0: } sl@0: word_type& m_hi_word() { return data()[num_words() - 1]; } sl@0: word_type m_hi_word() const { return data()[num_words() - 1]; } sl@0: sl@0: void m_sanitize_highest() { sl@0: size_type extra_bits = size() % WordTraits::word_size; sl@0: if (extra_bits) sl@0: m_hi_word() &= ~((~static_cast(0)) << extra_bits); sl@0: } sl@0: public: sl@0: sl@0: class reference { sl@0: friend class bitset_base; sl@0: sl@0: word_type *m_word_ptr; sl@0: size_type m_bit_pos; sl@0: sl@0: // left undefined sl@0: reference(); sl@0: sl@0: reference(bitset_base& b, size_type pos ) { sl@0: m_word_ptr = &b.m_get_word(pos); sl@0: m_bit_pos = s_which_bit(pos); sl@0: } sl@0: sl@0: public: sl@0: ~reference() {} sl@0: sl@0: // for b[i] = x; sl@0: reference& operator=(bool x) { sl@0: if ( x ) sl@0: *m_word_ptr |= s_mask_bit(m_bit_pos); sl@0: else sl@0: *m_word_ptr &= ~s_mask_bit(m_bit_pos); sl@0: sl@0: return *this; sl@0: } sl@0: // for b[i] = b[j]; sl@0: reference& operator=(const reference& j) { sl@0: if ( (*(j.m_word_ptr) & s_mask_bit(j.m_bit_pos)) ) sl@0: *m_word_ptr |= s_mask_bit(m_bit_pos); sl@0: else sl@0: *m_word_ptr &= ~s_mask_bit(m_bit_pos); sl@0: sl@0: return *this; sl@0: } sl@0: // flips the bit sl@0: bool operator~() const { sl@0: return (*(m_word_ptr) & s_mask_bit(m_bit_pos)) == 0; sl@0: } sl@0: // for x = b[i]; sl@0: operator bool() const { sl@0: return (*(m_word_ptr) & s_mask_bit(m_bit_pos)) != 0; sl@0: } sl@0: // for b[i].flip(); sl@0: reference& flip() { sl@0: *m_word_ptr ^= s_mask_bit(m_bit_pos); sl@0: return *this; sl@0: } sl@0: }; sl@0: sl@0: void init_from_ulong(unsigned long val) { sl@0: reset(); sl@0: const size_type n = (std::min)(sizeof(unsigned long) * CHAR_BIT, sl@0: WordTraits::word_size * num_words()); sl@0: for(size_type i = 0; i < n; ++i, val >>= 1) sl@0: if ( val & 0x1 ) sl@0: m_get_word(i) |= s_mask_bit(i); sl@0: } sl@0: sl@0: // intersection: this = this & x sl@0: Derived& operator&=(const Derived& x) { sl@0: for (size_type i = 0; i < num_words(); ++i) sl@0: data()[i] &= x.data()[i]; sl@0: return static_cast(*this); sl@0: } sl@0: // union: this = this | x sl@0: Derived& operator|=(const Derived& x) { sl@0: for (size_type i = 0; i < num_words(); ++i) sl@0: data()[i] |= x.data()[i]; sl@0: return static_cast(*this); sl@0: } sl@0: // exclusive or: this = this ^ x sl@0: Derived& operator^=(const Derived& x) { sl@0: for (size_type i = 0; i < num_words(); ++i) sl@0: data()[i] ^= x.data()[i]; sl@0: return static_cast(*this); sl@0: } sl@0: // left shift sl@0: Derived& operator<<=(size_type pos); sl@0: sl@0: // right shift sl@0: Derived& operator>>=(size_type pos); sl@0: sl@0: Derived& set() { sl@0: for (size_type i = 0; i < num_words(); ++i) sl@0: data()[i] = ~static_cast(0); sl@0: m_sanitize_highest(); sl@0: return static_cast(*this); sl@0: } sl@0: sl@0: Derived& set(size_type pos, int val = true) sl@0: { sl@0: BOOST_ASSERT_THROW(pos < size(), std::out_of_range("boost::bitset::set(pos,value)")); sl@0: if (val) sl@0: m_get_word(pos) |= s_mask_bit(pos); sl@0: else sl@0: m_get_word(pos) &= ~s_mask_bit(pos); sl@0: return static_cast(*this); sl@0: } sl@0: sl@0: Derived& reset() { sl@0: for (size_type i = 0; i < num_words(); ++i) sl@0: data()[i] = 0; sl@0: return static_cast(*this); sl@0: } sl@0: sl@0: Derived& reset(size_type pos) { sl@0: BOOST_ASSERT_THROW(pos < size(), std::out_of_range("boost::bitset::reset(pos)")); sl@0: m_get_word(pos) &= ~s_mask_bit(pos); sl@0: return static_cast(*this); sl@0: } sl@0: sl@0: // compliment sl@0: Derived operator~() const { sl@0: return Derived(static_cast(*this)).flip(); sl@0: } sl@0: sl@0: Derived& flip() { sl@0: for (size_type i = 0; i < num_words(); ++i) sl@0: data()[i] = ~data()[i]; sl@0: m_sanitize_highest(); sl@0: return static_cast(*this); sl@0: } sl@0: Derived& flip(size_type pos) { sl@0: BOOST_ASSERT_THROW(pos < size(), std::out_of_range("boost::bitset::flip(pos)")); sl@0: m_get_word(pos) ^= s_mask_bit(pos); sl@0: return static_cast(*this); sl@0: } sl@0: sl@0: // element access sl@0: reference operator[](size_type pos) { return reference(*this, pos); } sl@0: bool operator[](size_type pos) const { return test(pos); } sl@0: sl@0: unsigned long to_ulong() const; sl@0: sl@0: // to_string sl@0: sl@0: sl@0: size_type count() const { sl@0: size_type result = 0; sl@0: const unsigned char* byte_ptr = (const unsigned char*)data(); sl@0: const unsigned char* end_ptr = sl@0: (const unsigned char*)(data() + num_words()); sl@0: while ( byte_ptr < end_ptr ) { sl@0: result += bit_count<>::value[*byte_ptr]; sl@0: byte_ptr++; sl@0: } sl@0: return result; sl@0: } sl@0: sl@0: // size() must be provided by Derived class sl@0: sl@0: bool operator==(const Derived& x) const { sl@0: return std::equal(data(), data() + num_words(), x.data()); sl@0: } sl@0: sl@0: bool operator!=(const Derived& x) const { sl@0: return ! this->operator==(x); sl@0: } sl@0: sl@0: bool test(size_type pos) const { sl@0: BOOST_ASSERT_THROW(pos < size(), std::out_of_range("boost::bitset::test(pos)")); sl@0: return (m_get_word(pos) & s_mask_bit(pos)) sl@0: != static_cast(0); sl@0: } sl@0: sl@0: bool any() const { sl@0: for (size_type i = 0; i < num_words(); ++i) { sl@0: if ( data()[i] != static_cast(0) ) sl@0: return true; sl@0: } sl@0: return false; sl@0: } sl@0: bool none() const { sl@0: return !any(); sl@0: } sl@0: sl@0: Derived operator<<(size_type pos) const sl@0: { return Derived(static_cast(*this)) <<= pos; } sl@0: sl@0: Derived operator>>(size_type pos) const sl@0: { return Derived(static_cast(*this)) >>= pos; } sl@0: sl@0: template sl@0: void m_copy_from_string(const basic_string& s, sl@0: size_type pos, size_type n) sl@0: { sl@0: reset(); sl@0: const size_type nbits = (std::min)(size(), (std::min)(n, s.size() - pos)); sl@0: for (size_type i = 0; i < nbits; ++i) { sl@0: switch(s[pos + nbits - i - 1]) { sl@0: case '0': sl@0: break; sl@0: case '1': sl@0: this->set(i); sl@0: break; sl@0: default: sl@0: throw std::invalid_argument sl@0: ("boost::bitset_base::m_copy_from_string(s, pos, n)"); sl@0: } sl@0: } sl@0: } sl@0: sl@0: template sl@0: void m_copy_to_string(basic_string& s) const sl@0: { sl@0: s.assign(size(), '0'); sl@0: sl@0: for (size_type i = 0; i < size(); ++i) sl@0: if (test(i)) sl@0: s[size() - 1 - i] = '1'; sl@0: } sl@0: sl@0: //----------------------------------------------------------------------- sl@0: // Stuff not in std::bitset sl@0: sl@0: // difference: this = this - x sl@0: Derived& operator-=(const Derived& x) { sl@0: for (size_type i = 0; i < num_words(); ++i) sl@0: data()[i] &= ~x.data()[i]; sl@0: return static_cast(*this); sl@0: } sl@0: sl@0: // this wasn't working, why? sl@0: int compare_3way(const Derived& x) const { sl@0: return std::lexicographical_compare_3way sl@0: (data(), data() + num_words(), x.data(), x.data() + x.num_words()); sl@0: } sl@0: sl@0: // less-than compare sl@0: bool operator<(const Derived& x) const { sl@0: return std::lexicographical_compare sl@0: (data(), data() + num_words(), x.data(), x.data() + x.num_words()); sl@0: } sl@0: sl@0: // find the index of the first "on" bit sl@0: size_type find_first() const; sl@0: sl@0: // find the index of the next "on" bit after prev sl@0: size_type find_next(size_type prev) const; sl@0: sl@0: sl@0: size_type _Find_first() const { return find_first(); } sl@0: sl@0: // find the index of the next "on" bit after prev sl@0: size_type _Find_next(size_type prev) const { return find_next(prev); } sl@0: sl@0: // private: sl@0: word_type* data() sl@0: { return static_cast(this)->data(); } sl@0: sl@0: const word_type* data() const sl@0: { return static_cast(this)->data(); } sl@0: sl@0: size_type num_words() const sl@0: { return static_cast(this)->num_words(); } sl@0: sl@0: size_type size() const sl@0: { return static_cast(this)->size(); } sl@0: }; sl@0: sl@0: // 23.3.5.3 bitset operations: sl@0: template sl@0: inline D operator&(const bitset_base& x, sl@0: const bitset_base& y) { sl@0: D result(static_cast(x)); sl@0: result &= static_cast(y); sl@0: return result; sl@0: } sl@0: sl@0: template sl@0: inline D operator|(const bitset_base& x, sl@0: const bitset_base& y) { sl@0: D result(static_cast(x)); sl@0: result |= static_cast(y); sl@0: return result; sl@0: } sl@0: sl@0: template sl@0: inline D operator^(const bitset_base& x, sl@0: const bitset_base& y) { sl@0: D result(static_cast(x)); sl@0: result ^= static_cast(y); sl@0: return result; sl@0: } sl@0: sl@0: // this one is an extension sl@0: template sl@0: inline D operator-(const bitset_base& x, sl@0: const bitset_base& y) { sl@0: D result(static_cast(x)); sl@0: result -= static_cast(y); sl@0: return result; sl@0: } sl@0: sl@0: template sl@0: inline int compare_3way(const bitset_base& x, sl@0: const bitset_base& y) { sl@0: return std::lexicographical_compare_3way sl@0: (x.data(), x.data() + x.num_words(), sl@0: y.data(), y.data() + y.num_words()); sl@0: } sl@0: sl@0: sl@0: template sl@0: std::istream& sl@0: operator>>(std::istream& is, bitset_base& x) { sl@0: std::string tmp; sl@0: tmp.reserve(x.size()); sl@0: sl@0: // In new templatized iostreams, use istream::sentry sl@0: if (is.flags() & ios::skipws) { sl@0: char c; sl@0: do sl@0: is.get(c); sl@0: while (is && isspace(c)); sl@0: if (is) sl@0: is.putback(c); sl@0: } sl@0: sl@0: for (S i = 0; i < x.size(); ++i) { sl@0: char c; sl@0: is.get(c); sl@0: sl@0: if (!is) sl@0: break; sl@0: else if (c != '0' && c != '1') { sl@0: is.putback(c); sl@0: break; sl@0: } sl@0: else sl@0: // tmp.push_back(c); sl@0: tmp += c; sl@0: } sl@0: sl@0: if (tmp.empty()) sl@0: is.clear(is.rdstate() | ios::failbit); sl@0: else sl@0: x.m_copy_from_string(tmp, static_cast(0), x.size()); sl@0: sl@0: return is; sl@0: } sl@0: sl@0: template sl@0: std::ostream& operator<<(std::ostream& os, sl@0: const bitset_base& x) { sl@0: std::string tmp; sl@0: x.m_copy_to_string(tmp); sl@0: return os << tmp; sl@0: } sl@0: sl@0: //========================================================================= sl@0: template sl@0: > sl@0: class dyn_size_bitset sl@0: : public bitset_base, SizeType, sl@0: dyn_size_bitset > sl@0: { sl@0: typedef dyn_size_bitset self; sl@0: public: sl@0: typedef SizeType size_type; sl@0: private: sl@0: typedef word_traits WordTraits; sl@0: static const size_type word_size = WordTraits::word_size; sl@0: sl@0: public: sl@0: dyn_size_bitset(unsigned long val, sl@0: size_type n, sl@0: const Allocator& alloc = Allocator()) sl@0: : m_data(alloc.allocate((n + word_size - 1) / word_size)), sl@0: m_size(n), sl@0: m_num_words((n + word_size - 1) / word_size), sl@0: m_alloc(alloc) sl@0: { sl@0: init_from_ulong(val); sl@0: } sl@0: sl@0: dyn_size_bitset(size_type n, // size of the set's "universe" sl@0: const Allocator& alloc = Allocator()) sl@0: : m_data(alloc.allocate((n + word_size - 1) / word_size)), sl@0: m_size(n), m_num_words((n + word_size - 1) / word_size), sl@0: m_alloc(alloc) sl@0: { } sl@0: sl@0: template sl@0: explicit dyn_size_bitset sl@0: (const basic_string& s, sl@0: std::size_t pos = 0, sl@0: std::size_t n = std::size_t(basic_string::npos), sl@0: const Allocator& alloc = Allocator()) sl@0: : m_data(alloc.allocate((n + word_size - 1) / word_size)), sl@0: m_size(n), m_num_words((n + word_size - 1) / word_size), sl@0: m_alloc(alloc) sl@0: { sl@0: BOOST_ASSERT_THROW(pos < s.size(), std::out_of_range("dyn_size_bitset::dyn_size_bitset(s,pos,n,alloc)")); sl@0: m_copy_from_string(s, pos, n); sl@0: } sl@0: sl@0: template sl@0: explicit dyn_size_bitset sl@0: (InputIterator first, InputIterator last, sl@0: size_type n, // size of the set's "universe" sl@0: const Allocator& alloc = Allocator()) sl@0: : m_data(alloc.allocate((n + word_size - 1) / word_size)), sl@0: m_size(N), m_num_words((n + word_size - 1) / word_size), sl@0: m_alloc(alloc) sl@0: { sl@0: while (first != last) sl@0: this->set(*first++); sl@0: } sl@0: sl@0: ~dyn_size_bitset() { sl@0: m_alloc.deallocate(m_data, m_num_words); sl@0: } sl@0: sl@0: size_type size() const { return m_size; } sl@0: sl@0: // protected: sl@0: size_type num_words() const { return m_num_words; } sl@0: sl@0: word_type* data() { return m_data; } sl@0: const word_type* data() const { return m_data; } sl@0: sl@0: protected: sl@0: word_type* m_data; sl@0: SizeType m_size; sl@0: SizeType m_num_words; sl@0: Allocator m_alloc; sl@0: }; sl@0: sl@0: //========================================================================= sl@0: template sl@0: class bitset sl@0: : public bitset_base, SizeType, sl@0: bitset > sl@0: { sl@0: typedef bitset self; sl@0: static const std::size_t word_size = word_traits::word_size; sl@0: public: sl@0: // 23.3.5.1 constructors: sl@0: bitset() { sl@0: #if defined(__GNUC__) sl@0: for (size_type i = 0; i < num_words(); ++i) sl@0: m_data[i] = static_cast(0); sl@0: #endif sl@0: } sl@0: sl@0: bitset(unsigned long val) { sl@0: init_from_ulong(val); sl@0: } sl@0: sl@0: template sl@0: explicit bitset sl@0: (const basic_string& s, sl@0: std::size_t pos = 0, sl@0: std::size_t n = std::size_t(basic_string::npos)) sl@0: { sl@0: BOOST_ASSERT_THROW sl@0: (pos < s.size(), std::out_of_range("bitset::bitset(s,pos,n)")); sl@0: m_copy_from_string(s, pos, n); sl@0: } sl@0: sl@0: size_type size() const { return N; } sl@0: sl@0: // protected: sl@0: size_type num_words() const { return (N + word_size - 1) / word_size; } sl@0: sl@0: word_type* data() { return m_data; } sl@0: const word_type* data() const { return m_data; } sl@0: protected: sl@0: word_type m_data[(N + word_size - 1) / word_size]; sl@0: }; sl@0: sl@0: //========================================================================= sl@0: struct select_static_bitset { sl@0: template sl@0: struct bind_ { sl@0: typedef bitset type; sl@0: }; sl@0: }; sl@0: struct select_dyn_size_bitset { sl@0: template sl@0: struct bind_ { sl@0: typedef dyn_size_bitset type; sl@0: }; sl@0: }; sl@0: sl@0: template sl@0: > sl@0: class bitset_generator { sl@0: typedef typename ct_if::type selector; sl@0: public: sl@0: typedef typename selector sl@0: ::template bind_::type type; sl@0: }; sl@0: sl@0: sl@0: //========================================================================= sl@0: // bitset_base non-inline member function implementations sl@0: sl@0: template sl@0: Derived& sl@0: bitset_base:: sl@0: operator<<=(size_type shift) sl@0: { sl@0: typedef typename WordTraits::word_type word_type; sl@0: typedef SizeType size_type; sl@0: if (shift != 0) { sl@0: const size_type wshift = shift / WordTraits::word_size; sl@0: const size_type offset = shift % WordTraits::word_size; sl@0: const size_type sub_offset = WordTraits::word_size - offset; sl@0: size_type n = num_words() - 1; sl@0: for ( ; n > wshift; --n) sl@0: data()[n] = (data()[n - wshift] << offset) | sl@0: (data()[n - wshift - 1] >> sub_offset); sl@0: if (n == wshift) sl@0: data()[n] = data()[0] << offset; sl@0: for (size_type n1 = 0; n1 < n; ++n1) sl@0: data()[n1] = static_cast(0); sl@0: } sl@0: m_sanitize_highest(); sl@0: return static_cast(*this); sl@0: } // end operator<<= sl@0: sl@0: sl@0: template sl@0: Derived& sl@0: bitset_base:: sl@0: operator>>=(size_type shift) sl@0: { sl@0: typedef typename WordTraits::word_type word_type; sl@0: typedef SizeType size_type; sl@0: if (shift != 0) { sl@0: const size_type wshift = shift / WordTraits::word_size; sl@0: const size_type offset = shift % WordTraits::word_size; sl@0: const size_type sub_offset = WordTraits::word_size - offset; sl@0: const size_type limit = num_words() - wshift - 1; sl@0: size_type n = 0; sl@0: for ( ; n < limit; ++n) sl@0: data()[n] = (data()[n + wshift] >> offset) | sl@0: (data()[n + wshift + 1] << sub_offset); sl@0: data()[limit] = data()[num_words()-1] >> offset; sl@0: for (size_type n1 = limit + 1; n1 < num_words(); ++n1) sl@0: data()[n1] = static_cast(0); sl@0: } sl@0: m_sanitize_highest(); sl@0: return static_cast(*this); sl@0: } // end operator>>= sl@0: sl@0: sl@0: template sl@0: unsigned long bitset_base:: sl@0: to_ulong() const sl@0: { sl@0: typedef typename WordTraits::word_type word_type; sl@0: typedef SizeType size_type; sl@0: const std::overflow_error sl@0: overflow("boost::bit_set::operator unsigned long()"); sl@0: sl@0: if (sizeof(word_type) >= sizeof(unsigned long)) { sl@0: for (size_type i = 1; i < num_words(); ++i) sl@0: BOOST_ASSERT_THROW(! data()[i], overflow); sl@0: sl@0: const word_type mask sl@0: = static_cast(static_cast(-1)); sl@0: BOOST_ASSERT_THROW(! (data()[0] & ~mask), overflow); sl@0: sl@0: return static_cast(data()[0] & mask); sl@0: } sl@0: else { // sizeof(word_type) < sizeof(unsigned long). sl@0: const size_type nwords = sl@0: (sizeof(unsigned long) + sizeof(word_type) - 1) / sizeof(word_type); sl@0: sl@0: size_type min_nwords = nwords; sl@0: if (num_words() > nwords) { sl@0: for (size_type i = nwords; i < num_words(); ++i) sl@0: BOOST_ASSERT_THROW(!data()[i], overflow); sl@0: } sl@0: else sl@0: min_nwords = num_words(); sl@0: sl@0: // If unsigned long is 8 bytes and word_type is 6 bytes, then sl@0: // an unsigned long consists of all of one word plus 2 bytes sl@0: // from another word. sl@0: const size_type part = sizeof(unsigned long) % sizeof(word_type); sl@0: sl@0: #if 0 sl@0: // bug in here? sl@0: // >> to far? sl@0: BOOST_ASSERT_THROW((part != 0 sl@0: && nwords <= num_words() sl@0: && (data()[min_nwords - 1] >> sl@0: ((sizeof(word_type) - part) * CHAR_BIT)) != 0), sl@0: overflow); sl@0: #endif sl@0: sl@0: unsigned long result = 0; sl@0: for (size_type i = 0; i < min_nwords; ++i) { sl@0: result |= static_cast( sl@0: data()[i]) << (i * sizeof(word_type) * CHAR_BIT); sl@0: } sl@0: return result; sl@0: } sl@0: }// end operator unsigned long() sl@0: sl@0: sl@0: template sl@0: SizeType bitset_base:: sl@0: find_first() const sl@0: { sl@0: SizeType not_found = size(); sl@0: for (size_type i = 0; i < num_words(); i++ ) { sl@0: word_type thisword = data()[i]; sl@0: if ( thisword != static_cast(0) ) { sl@0: // find byte within word sl@0: for ( std::size_t j = 0; j < sizeof(word_type); j++ ) { sl@0: unsigned char this_byte sl@0: = static_cast(thisword & (~(unsigned char)0)); sl@0: if ( this_byte ) sl@0: return i * WordTraits::word_size + j * CHAR_BIT + sl@0: first_bit_location<>::value[this_byte]; sl@0: sl@0: thisword >>= CHAR_BIT; sl@0: } sl@0: } sl@0: } sl@0: // not found, so return an indication of failure. sl@0: return not_found; sl@0: } sl@0: sl@0: template sl@0: SizeType bitset_base:: sl@0: bitset_base:: sl@0: find_next(size_type prev) const sl@0: { sl@0: SizeType not_found = size(); sl@0: // make bound inclusive sl@0: ++prev; sl@0: sl@0: // check out of bounds sl@0: if ( prev >= num_words() * WordTraits::word_size ) sl@0: return not_found; sl@0: sl@0: // search first word sl@0: size_type i = s_which_word(prev); sl@0: word_type thisword = data()[i]; sl@0: sl@0: // mask off bits below bound sl@0: thisword &= (~static_cast(0)) << s_which_bit(prev); sl@0: sl@0: if ( thisword != static_cast(0) ) { sl@0: // find byte within word sl@0: // get first byte into place sl@0: thisword >>= s_which_byte(prev) * CHAR_BIT; sl@0: for ( size_type j = s_which_byte(prev); j < sizeof(word_type); j++ ) { sl@0: unsigned char this_byte sl@0: = static_cast(thisword & (~(unsigned char)0)); sl@0: if ( this_byte ) sl@0: return i * WordTraits::word_size + j * CHAR_BIT + sl@0: first_bit_location<>::value[this_byte]; sl@0: sl@0: thisword >>= CHAR_BIT; sl@0: } sl@0: } sl@0: sl@0: // check subsequent words sl@0: i++; sl@0: for ( ; i < num_words(); i++ ) { sl@0: word_type thisword = data()[i]; sl@0: if ( thisword != static_cast(0) ) { sl@0: // find byte within word sl@0: for ( size_type j = 0; j < sizeof(word_type); j++ ) { sl@0: unsigned char this_byte sl@0: = static_cast(thisword & (~(unsigned char)0)); sl@0: if ( this_byte ) sl@0: return i * WordTraits::word_size + j * CHAR_BIT + sl@0: first_bit_location<>::value[this_byte]; sl@0: sl@0: thisword >>= CHAR_BIT; sl@0: } sl@0: } sl@0: } sl@0: sl@0: // not found, so return an indication of failure. sl@0: return not_found; sl@0: } // end find_next sl@0: sl@0: sl@0: template sl@0: unsigned char bit_count::value[] = { sl@0: 0, /* 0 */ 1, /* 1 */ 1, /* 2 */ 2, /* 3 */ 1, /* 4 */ sl@0: 2, /* 5 */ 2, /* 6 */ 3, /* 7 */ 1, /* 8 */ 2, /* 9 */ sl@0: 2, /* 10 */ 3, /* 11 */ 2, /* 12 */ 3, /* 13 */ 3, /* 14 */ sl@0: 4, /* 15 */ 1, /* 16 */ 2, /* 17 */ 2, /* 18 */ 3, /* 19 */ sl@0: 2, /* 20 */ 3, /* 21 */ 3, /* 22 */ 4, /* 23 */ 2, /* 24 */ sl@0: 3, /* 25 */ 3, /* 26 */ 4, /* 27 */ 3, /* 28 */ 4, /* 29 */ sl@0: 4, /* 30 */ 5, /* 31 */ 1, /* 32 */ 2, /* 33 */ 2, /* 34 */ sl@0: 3, /* 35 */ 2, /* 36 */ 3, /* 37 */ 3, /* 38 */ 4, /* 39 */ sl@0: 2, /* 40 */ 3, /* 41 */ 3, /* 42 */ 4, /* 43 */ 3, /* 44 */ sl@0: 4, /* 45 */ 4, /* 46 */ 5, /* 47 */ 2, /* 48 */ 3, /* 49 */ sl@0: 3, /* 50 */ 4, /* 51 */ 3, /* 52 */ 4, /* 53 */ 4, /* 54 */ sl@0: 5, /* 55 */ 3, /* 56 */ 4, /* 57 */ 4, /* 58 */ 5, /* 59 */ sl@0: 4, /* 60 */ 5, /* 61 */ 5, /* 62 */ 6, /* 63 */ 1, /* 64 */ sl@0: 2, /* 65 */ 2, /* 66 */ 3, /* 67 */ 2, /* 68 */ 3, /* 69 */ sl@0: 3, /* 70 */ 4, /* 71 */ 2, /* 72 */ 3, /* 73 */ 3, /* 74 */ sl@0: 4, /* 75 */ 3, /* 76 */ 4, /* 77 */ 4, /* 78 */ 5, /* 79 */ sl@0: 2, /* 80 */ 3, /* 81 */ 3, /* 82 */ 4, /* 83 */ 3, /* 84 */ sl@0: 4, /* 85 */ 4, /* 86 */ 5, /* 87 */ 3, /* 88 */ 4, /* 89 */ sl@0: 4, /* 90 */ 5, /* 91 */ 4, /* 92 */ 5, /* 93 */ 5, /* 94 */ sl@0: 6, /* 95 */ 2, /* 96 */ 3, /* 97 */ 3, /* 98 */ 4, /* 99 */ sl@0: 3, /* 100 */ 4, /* 101 */ 4, /* 102 */ 5, /* 103 */ 3, /* 104 */ sl@0: 4, /* 105 */ 4, /* 106 */ 5, /* 107 */ 4, /* 108 */ 5, /* 109 */ sl@0: 5, /* 110 */ 6, /* 111 */ 3, /* 112 */ 4, /* 113 */ 4, /* 114 */ sl@0: 5, /* 115 */ 4, /* 116 */ 5, /* 117 */ 5, /* 118 */ 6, /* 119 */ sl@0: 4, /* 120 */ 5, /* 121 */ 5, /* 122 */ 6, /* 123 */ 5, /* 124 */ sl@0: 6, /* 125 */ 6, /* 126 */ 7, /* 127 */ 1, /* 128 */ 2, /* 129 */ sl@0: 2, /* 130 */ 3, /* 131 */ 2, /* 132 */ 3, /* 133 */ 3, /* 134 */ sl@0: 4, /* 135 */ 2, /* 136 */ 3, /* 137 */ 3, /* 138 */ 4, /* 139 */ sl@0: 3, /* 140 */ 4, /* 141 */ 4, /* 142 */ 5, /* 143 */ 2, /* 144 */ sl@0: 3, /* 145 */ 3, /* 146 */ 4, /* 147 */ 3, /* 148 */ 4, /* 149 */ sl@0: 4, /* 150 */ 5, /* 151 */ 3, /* 152 */ 4, /* 153 */ 4, /* 154 */ sl@0: 5, /* 155 */ 4, /* 156 */ 5, /* 157 */ 5, /* 158 */ 6, /* 159 */ sl@0: 2, /* 160 */ 3, /* 161 */ 3, /* 162 */ 4, /* 163 */ 3, /* 164 */ sl@0: 4, /* 165 */ 4, /* 166 */ 5, /* 167 */ 3, /* 168 */ 4, /* 169 */ sl@0: 4, /* 170 */ 5, /* 171 */ 4, /* 172 */ 5, /* 173 */ 5, /* 174 */ sl@0: 6, /* 175 */ 3, /* 176 */ 4, /* 177 */ 4, /* 178 */ 5, /* 179 */ sl@0: 4, /* 180 */ 5, /* 181 */ 5, /* 182 */ 6, /* 183 */ 4, /* 184 */ sl@0: 5, /* 185 */ 5, /* 186 */ 6, /* 187 */ 5, /* 188 */ 6, /* 189 */ sl@0: 6, /* 190 */ 7, /* 191 */ 2, /* 192 */ 3, /* 193 */ 3, /* 194 */ sl@0: 4, /* 195 */ 3, /* 196 */ 4, /* 197 */ 4, /* 198 */ 5, /* 199 */ sl@0: 3, /* 200 */ 4, /* 201 */ 4, /* 202 */ 5, /* 203 */ 4, /* 204 */ sl@0: 5, /* 205 */ 5, /* 206 */ 6, /* 207 */ 3, /* 208 */ 4, /* 209 */ sl@0: 4, /* 210 */ 5, /* 211 */ 4, /* 212 */ 5, /* 213 */ 5, /* 214 */ sl@0: 6, /* 215 */ 4, /* 216 */ 5, /* 217 */ 5, /* 218 */ 6, /* 219 */ sl@0: 5, /* 220 */ 6, /* 221 */ 6, /* 222 */ 7, /* 223 */ 3, /* 224 */ sl@0: 4, /* 225 */ 4, /* 226 */ 5, /* 227 */ 4, /* 228 */ 5, /* 229 */ sl@0: 5, /* 230 */ 6, /* 231 */ 4, /* 232 */ 5, /* 233 */ 5, /* 234 */ sl@0: 6, /* 235 */ 5, /* 236 */ 6, /* 237 */ 6, /* 238 */ 7, /* 239 */ sl@0: 4, /* 240 */ 5, /* 241 */ 5, /* 242 */ 6, /* 243 */ 5, /* 244 */ sl@0: 6, /* 245 */ 6, /* 246 */ 7, /* 247 */ 5, /* 248 */ 6, /* 249 */ sl@0: 6, /* 250 */ 7, /* 251 */ 6, /* 252 */ 7, /* 253 */ 7, /* 254 */ sl@0: 8 /* 255 */ sl@0: }; // end _Bit_count sl@0: sl@0: template sl@0: unsigned char first_bit_location::value[] = { sl@0: 0, /* 0 */ 0, /* 1 */ 1, /* 2 */ 0, /* 3 */ 2, /* 4 */ sl@0: 0, /* 5 */ 1, /* 6 */ 0, /* 7 */ 3, /* 8 */ 0, /* 9 */ sl@0: 1, /* 10 */ 0, /* 11 */ 2, /* 12 */ 0, /* 13 */ 1, /* 14 */ sl@0: 0, /* 15 */ 4, /* 16 */ 0, /* 17 */ 1, /* 18 */ 0, /* 19 */ sl@0: 2, /* 20 */ 0, /* 21 */ 1, /* 22 */ 0, /* 23 */ 3, /* 24 */ sl@0: 0, /* 25 */ 1, /* 26 */ 0, /* 27 */ 2, /* 28 */ 0, /* 29 */ sl@0: 1, /* 30 */ 0, /* 31 */ 5, /* 32 */ 0, /* 33 */ 1, /* 34 */ sl@0: 0, /* 35 */ 2, /* 36 */ 0, /* 37 */ 1, /* 38 */ 0, /* 39 */ sl@0: 3, /* 40 */ 0, /* 41 */ 1, /* 42 */ 0, /* 43 */ 2, /* 44 */ sl@0: 0, /* 45 */ 1, /* 46 */ 0, /* 47 */ 4, /* 48 */ 0, /* 49 */ sl@0: 1, /* 50 */ 0, /* 51 */ 2, /* 52 */ 0, /* 53 */ 1, /* 54 */ sl@0: 0, /* 55 */ 3, /* 56 */ 0, /* 57 */ 1, /* 58 */ 0, /* 59 */ sl@0: 2, /* 60 */ 0, /* 61 */ 1, /* 62 */ 0, /* 63 */ 6, /* 64 */ sl@0: 0, /* 65 */ 1, /* 66 */ 0, /* 67 */ 2, /* 68 */ 0, /* 69 */ sl@0: 1, /* 70 */ 0, /* 71 */ 3, /* 72 */ 0, /* 73 */ 1, /* 74 */ sl@0: 0, /* 75 */ 2, /* 76 */ 0, /* 77 */ 1, /* 78 */ 0, /* 79 */ sl@0: 4, /* 80 */ 0, /* 81 */ 1, /* 82 */ 0, /* 83 */ 2, /* 84 */ sl@0: 0, /* 85 */ 1, /* 86 */ 0, /* 87 */ 3, /* 88 */ 0, /* 89 */ sl@0: 1, /* 90 */ 0, /* 91 */ 2, /* 92 */ 0, /* 93 */ 1, /* 94 */ sl@0: 0, /* 95 */ 5, /* 96 */ 0, /* 97 */ 1, /* 98 */ 0, /* 99 */ sl@0: 2, /* 100 */ 0, /* 101 */ 1, /* 102 */ 0, /* 103 */ 3, /* 104 */ sl@0: 0, /* 105 */ 1, /* 106 */ 0, /* 107 */ 2, /* 108 */ 0, /* 109 */ sl@0: 1, /* 110 */ 0, /* 111 */ 4, /* 112 */ 0, /* 113 */ 1, /* 114 */ sl@0: 0, /* 115 */ 2, /* 116 */ 0, /* 117 */ 1, /* 118 */ 0, /* 119 */ sl@0: 3, /* 120 */ 0, /* 121 */ 1, /* 122 */ 0, /* 123 */ 2, /* 124 */ sl@0: 0, /* 125 */ 1, /* 126 */ 0, /* 127 */ 7, /* 128 */ 0, /* 129 */ sl@0: 1, /* 130 */ 0, /* 131 */ 2, /* 132 */ 0, /* 133 */ 1, /* 134 */ sl@0: 0, /* 135 */ 3, /* 136 */ 0, /* 137 */ 1, /* 138 */ 0, /* 139 */ sl@0: 2, /* 140 */ 0, /* 141 */ 1, /* 142 */ 0, /* 143 */ 4, /* 144 */ sl@0: 0, /* 145 */ 1, /* 146 */ 0, /* 147 */ 2, /* 148 */ 0, /* 149 */ sl@0: 1, /* 150 */ 0, /* 151 */ 3, /* 152 */ 0, /* 153 */ 1, /* 154 */ sl@0: 0, /* 155 */ 2, /* 156 */ 0, /* 157 */ 1, /* 158 */ 0, /* 159 */ sl@0: 5, /* 160 */ 0, /* 161 */ 1, /* 162 */ 0, /* 163 */ 2, /* 164 */ sl@0: 0, /* 165 */ 1, /* 166 */ 0, /* 167 */ 3, /* 168 */ 0, /* 169 */ sl@0: 1, /* 170 */ 0, /* 171 */ 2, /* 172 */ 0, /* 173 */ 1, /* 174 */ sl@0: 0, /* 175 */ 4, /* 176 */ 0, /* 177 */ 1, /* 178 */ 0, /* 179 */ sl@0: 2, /* 180 */ 0, /* 181 */ 1, /* 182 */ 0, /* 183 */ 3, /* 184 */ sl@0: 0, /* 185 */ 1, /* 186 */ 0, /* 187 */ 2, /* 188 */ 0, /* 189 */ sl@0: 1, /* 190 */ 0, /* 191 */ 6, /* 192 */ 0, /* 193 */ 1, /* 194 */ sl@0: 0, /* 195 */ 2, /* 196 */ 0, /* 197 */ 1, /* 198 */ 0, /* 199 */ sl@0: 3, /* 200 */ 0, /* 201 */ 1, /* 202 */ 0, /* 203 */ 2, /* 204 */ sl@0: 0, /* 205 */ 1, /* 206 */ 0, /* 207 */ 4, /* 208 */ 0, /* 209 */ sl@0: 1, /* 210 */ 0, /* 211 */ 2, /* 212 */ 0, /* 213 */ 1, /* 214 */ sl@0: 0, /* 215 */ 3, /* 216 */ 0, /* 217 */ 1, /* 218 */ 0, /* 219 */ sl@0: 2, /* 220 */ 0, /* 221 */ 1, /* 222 */ 0, /* 223 */ 5, /* 224 */ sl@0: 0, /* 225 */ 1, /* 226 */ 0, /* 227 */ 2, /* 228 */ 0, /* 229 */ sl@0: 1, /* 230 */ 0, /* 231 */ 3, /* 232 */ 0, /* 233 */ 1, /* 234 */ sl@0: 0, /* 235 */ 2, /* 236 */ 0, /* 237 */ 1, /* 238 */ 0, /* 239 */ sl@0: 4, /* 240 */ 0, /* 241 */ 1, /* 242 */ 0, /* 243 */ 2, /* 244 */ sl@0: 0, /* 245 */ 1, /* 246 */ 0, /* 247 */ 3, /* 248 */ 0, /* 249 */ sl@0: 1, /* 250 */ 0, /* 251 */ 2, /* 252 */ 0, /* 253 */ 1, /* 254 */ sl@0: 0, /* 255 */ sl@0: }; // end _First_one sl@0: sl@0: } // namespace detail sl@0: sl@0: } // namespace boost