⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 std_bitset.h

📁 c++编程宝典源码及Quincy99编译器 是《标准C++编程宝典》电子工业出版社的光盘
💻 H
📖 第 1 页 / 共 3 页
字号:
    while ( __byte_ptr < __end_ptr ) {      __result += _Bit_count<true>::_S_bit_count[*__byte_ptr];      __byte_ptr++;    }    return __result;  }  unsigned long _M_do_to_ulong() const {    if (sizeof(_WordT) <= sizeof(unsigned long))        return static_cast<unsigned long>(_M_w);    else {      const _WordT __mask = static_cast<_WordT>(static_cast<unsigned long>(-1));      if (_M_w & ~__mask)        __STL_THROW(overflow_error("bitset"));      return static_cast<unsigned long>(_M_w);    }  }  size_t _M_do_find_first(size_t __not_found) const;  // find the next "on" bit that follows "prev"  size_t _M_do_find_next(size_t __prev, size_t __not_found) const;};//// Definitions of non-inline functions from the single-word version of//  _Base_bitset.//template <class _WordT>_Base_bitset<1, _WordT>::_Base_bitset(unsigned long __val){  _M_do_reset();  const size_t __n = min(sizeof(unsigned long)*CHAR_BIT,                         __BITS_PER_WORDT(_WordT)*_Nw);  for(size_t __i = 0; __i < __n; ++__i, __val >>= 1)    if ( __val & 0x1 )      _M_w |= _S_maskbit(__i);}template <class _WordT>size_t _Base_bitset<1, _WordT>::_M_do_find_first(size_t __not_found) const{  _WordT __thisword = _M_w;  if ( __thisword != static_cast<_WordT>(0) ) {    // find byte within word    for ( size_t __j = 0; __j < sizeof(_WordT); __j++ ) {      unsigned char __this_byte        = static_cast<unsigned char>(__thisword & (~(unsigned char)0));      if ( __this_byte )        return __j*CHAR_BIT + _First_one<true>::_S_first_one[__this_byte];      __thisword >>= CHAR_BIT;    }  }  // not found, so return a value that indicates failure.  return __not_found;}template <class _WordT>size_t_Base_bitset<1, _WordT>::_M_do_find_next(size_t __prev,                                         size_t __not_found ) const{  // make bound inclusive  ++__prev;  // check out of bounds  if ( __prev >= __BITS_PER_WORDT(_WordT) )    return __not_found;    // search first (and only) word  _WordT __thisword = _M_w;  // mask off bits below bound  __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);  if ( __thisword != static_cast<_WordT>(0) ) {    // find byte within word    // get first byte into place    __thisword >>= _S_whichbyte(__prev) * CHAR_BIT;    for ( size_t __j = _S_whichbyte(__prev); __j < sizeof(_WordT); __j++ ) {      unsigned char __this_byte        = static_cast<unsigned char>(__thisword & (~(unsigned char)0));      if ( __this_byte )        return __j*CHAR_BIT + _First_one<true>::_S_first_one[__this_byte];      __thisword >>= CHAR_BIT;    }  }  // not found, so return a value that indicates failure.  return __not_found;} // end _M_do_find_next//// One last specialization: _M_do_to_ulong() and the constructor from// unsigned long are very simple if the bitset consists of a single// word of type unsigned long.//__STL_TEMPLATE_NULLinline unsigned long_Base_bitset<1, unsigned long>::_M_do_to_ulong() const { return _M_w; }__STL_TEMPLATE_NULLinline _Base_bitset<1, unsigned long>::_Base_bitset(unsigned long __val) {  _M_w = __val;}#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */// ------------------------------------------------------------// Helper class to zero out the unused high-order bits in the highest word.#ifdef __STL_CLASS_PARTIAL_SPECIALIZATIONtemplate <class _WordT, size_t _Extrabits> struct _Sanitize {  static void _M_do_sanitize(_WordT& __val)    { __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); }};template <class _WordT> struct _Sanitize<_WordT, 0> {  static void _M_do_sanitize(_WordT) {}};#else /* __STL_CLASS_PARTIAL_SPECIALIZATION */ template <class _WordT, size_t _Extrabits> struct _Sanitize {  static void _M_do_sanitize(_WordT& __val) {    if (_Extrabits != 0)      __val &= ~((~static_cast<_WordT>(0)) << _Extrabits);  }}; #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */// ------------------------------------------------------------// Class bitset.//   _Nb may be any nonzero number of type size_t.//   Type _WordT may be any unsigned integral type.template<size_t _Nb, class _WordT = unsigned long>class bitset : private _Base_bitset<__BITSET_WORDS(_Nb,_WordT), _WordT>{private:  typedef _Base_bitset<__BITSET_WORDS(_Nb,_WordT), _WordT> _Base;  // Import base's protected interface.  Necessary because of new template  // name resolution rules.#ifdef __STL_HAS_NAMESPACES  using _Base::_S_whichword;  using _Base::_S_whichbyte;  using _Base::_S_whichbit;  using _Base::_S_maskbit;  using _Base::_M_getword;  using _Base::_M_hiword;  using _Base::_M_do_and;  using _Base::_M_do_or;  using _Base::_M_do_xor;  using _Base::_M_do_left_shift;  using _Base::_M_do_right_shift;  using _Base::_M_do_flip;  using _Base::_M_do_set;  using _Base::_M_do_reset;  using _Base::_M_is_equal;  using _Base::_M_is_any;  using _Base::_M_do_count;  using _Base::_M_do_to_ulong;  using _Base::_M_do_find_first;  using _Base::_M_do_find_next;#endif /* __STL_HAS_NAMESPACES */private:  void _M_do_sanitize() {    _Sanitize<_WordT,_Nb%__BITS_PER_WORDT(_WordT) >      ::_M_do_sanitize(_M_hiword());  }public:  // bit reference:  class reference;  friend class reference;    class reference {    friend class bitset;    _WordT *_M_wp;    size_t _M_bpos;    // left undefined    reference();  public:    reference( bitset& __b, size_t __pos ) {      _M_wp = &__b._M_getword(__pos);      _M_bpos = _S_whichbit(__pos);    }    ~reference() {}    // for b[i] = __x;    reference& operator=(bool __x) {      if ( __x )        *_M_wp |= _S_maskbit(_M_bpos);      else        *_M_wp &= ~_S_maskbit(_M_bpos);      return *this;    }    // for b[i] = b[__j];    reference& operator=(const reference& __j) {      if ( (*(__j._M_wp) & _S_maskbit(__j._M_bpos)) )        *_M_wp |= _S_maskbit(_M_bpos);      else        *_M_wp &= ~_S_maskbit(_M_bpos);      return *this;    }    // flips the bit    bool operator~() const { return (*(_M_wp) & _S_maskbit(_M_bpos)) == 0; }    // for __x = b[i];    operator bool() const { return (*(_M_wp) & _S_maskbit(_M_bpos)) != 0; }    // for b[i].flip();    reference& flip() {      *_M_wp ^= _S_maskbit(_M_bpos);      return *this;    }  };  // 23.3.5.1 constructors:  bitset() {}  bitset(unsigned long __val) :    _Base_bitset<__BITSET_WORDS(_Nb,_WordT), _WordT>(__val) {}#ifdef __STL_MEMBER_TEMPLATES  template<class _CharT, class _Traits, class _Alloc>  explicit bitset(const basic_string<_CharT,_Traits,_Alloc>& __s,                  size_t __pos = 0)    : _Base()  {    if (__pos > __s.size())      __STL_THROW(out_of_range("bitset"));    _M_copy_from_string(__s, __pos, 		        basic_string<_CharT,_Traits,_Alloc>::npos);  }  template<class _CharT, class _Traits, class _Alloc>    bitset(const basic_string<_CharT, _Traits, _Alloc>& __s,	   size_t __pos,	   size_t __n)    : _Base()     {      if (__pos > __s.size()) 	__STL_THROW(out_of_range("bitset"));      _M_copy_from_string(__s, __pos, __n);    }#else   explicit bitset(const basic_string<char>& __s,		  size_t __pos = 0,		  size_t __n = basic_string<char>::npos)     : _Base()     {      if (__pos > __s.size())         __STL_THROW(out_of_range("bitset"));      _M_copy_from_string(__s, __pos, __n);    }#endif /* __STL_MEMBER_TEMPLATES */  // 23.3.5.2 bitset operations:  bitset<_Nb,_WordT>& operator&=(const bitset<_Nb,_WordT>& __rhs) {    _M_do_and(__rhs);    return *this;  }  bitset<_Nb,_WordT>& operator|=(const bitset<_Nb,_WordT>& __rhs) {    _M_do_or(__rhs);    return *this;  }  bitset<_Nb,_WordT>& operator^=(const bitset<_Nb,_WordT>& __rhs) {    _M_do_xor(__rhs);    return *this;  }  bitset<_Nb,_WordT>& operator<<=(size_t __pos) {    _M_do_left_shift(__pos);    _M_do_sanitize();    return *this;  }  bitset<_Nb,_WordT>& operator>>=(size_t __pos) {    _M_do_right_shift(__pos);    _M_do_sanitize();    return *this;  }  //  // Extension:  // Versions of single-bit set, reset, flip, test with no range checking.  //  bitset<_Nb,_WordT>& _Unchecked_set(size_t __pos) {    _M_getword(__pos) |= _S_maskbit(__pos);    return *this;  }  bitset<_Nb,_WordT>& _Unchecked_set(size_t __pos, int __val) {    if (__val)      _M_getword(__pos) |= _S_maskbit(__pos);    else      _M_getword(__pos) &= ~_S_maskbit(__pos);    return *this;  }  bitset<_Nb,_WordT>& _Unchecked_reset(size_t __pos) {    _M_getword(__pos) &= ~_S_maskbit(__pos);    return *this;  }  bitset<_Nb,_WordT>& _Unchecked_flip(size_t __pos) {    _M_getword(__pos) ^= _S_maskbit(__pos);    return *this;  }  bool _Unchecked_test(size_t __pos) const {    return (_M_getword(__pos) & _S_maskbit(__pos)) != static_cast<_WordT>(0);  }  // Set, reset, and flip.  bitset<_Nb,_WordT>& set() {    _M_do_set();    _M_do_sanitize();    return *this;  }  bitset<_Nb,_WordT>& set(size_t __pos) {    if (__pos >= _Nb)      __STL_THROW(out_of_range("bitset"));    return _Unchecked_set(__pos);  }  bitset<_Nb,_WordT>& set(size_t __pos, int __val) {    if (__pos >= _Nb)      __STL_THROW(out_of_range("bitset"));    return _Unchecked_set(__pos, __val);  }  bitset<_Nb,_WordT>& reset() {    _M_do_reset();    return *this;  }  bitset<_Nb,_WordT>& reset(size_t __pos) {    if (__pos >= _Nb)      __STL_THROW(out_of_range("bitset"));    return _Unchecked_reset(__pos);  }  bitset<_Nb,_WordT>& flip() {    _M_do_flip();    _M_do_sanitize();    return *this;  }  bitset<_Nb,_WordT>& flip(size_t __pos) {    if (__pos >= _Nb)      __STL_THROW(out_of_range("bitset"));    return _Unchecked_flip(__pos);  }  bitset<_Nb,_WordT> operator~() const {    return bitset<_Nb,_WordT>(*this).flip();  }  // element access:  //for b[i];  reference operator[](size_t __pos) { return reference(*this,__pos); }  bool operator[](size_t __pos) const { return _Unchecked_test(__pos); }  unsigned long to_ulong() const { return _M_do_to_ulong(); }#if  defined(__STL_MEMBER_TEMPLATES) && \     defined(__STL_EXPLICIT_FUNCTION_TMPL_ARGS)  template <class _CharT, class _Traits, class _Alloc>  basic_string<_CharT, _Traits, _Alloc> to_string() const {    basic_string<_CharT, _Traits, _Alloc> __result;    _M_copy_to_string(__result);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -