std_bitset.h

来自「ARM Linux Tool 各种代码包括MTD」· C头文件 代码 · 共 783 行 · 第 1/2 页

H
783
字号
  typedef unsigned long _WordT;private:  void _M_do_sanitize() {    _Sanitize<_Nb%_GLIBCPP_BITSET_BITS_PER_WORD>::_M_do_sanitize(this->_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 = _Base::_S_whichbit(__pos);    }    ~reference() {}    // for b[i] = __x;    reference& operator=(bool __x) {      if ( __x )        *_M_wp |= _Base::_S_maskbit(_M_bpos);      else        *_M_wp &= ~_Base::_S_maskbit(_M_bpos);      return *this;    }    // for b[i] = b[__j];    reference& operator=(const reference& __j) {      if ( (*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)) )        *_M_wp |= _Base::_S_maskbit(_M_bpos);      else        *_M_wp &= ~_Base::_S_maskbit(_M_bpos);      return *this;    }    // flips the bit    bool operator~() const      { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }    // for __x = b[i];    operator bool() const      { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }    // for b[i].flip();    reference& flip() {      *_M_wp ^= _Base::_S_maskbit(_M_bpos);      return *this;    }  };  // 23.3.5.1 constructors:  bitset() {}  bitset(unsigned long __val) : _Base_bitset<__BITSET_WORDS(_Nb)>(__val)     { _M_do_sanitize(); }  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);  }  // 23.3.5.2 bitset operations:  bitset<_Nb>& operator&=(const bitset<_Nb>& __rhs) {    this->_M_do_and(__rhs);    return *this;  }  bitset<_Nb>& operator|=(const bitset<_Nb>& __rhs) {    this->_M_do_or(__rhs);    return *this;  }  bitset<_Nb>& operator^=(const bitset<_Nb>& __rhs) {    this->_M_do_xor(__rhs);    return *this;  }  bitset<_Nb>& operator<<=(size_t __pos) {    this->_M_do_left_shift(__pos);    this->_M_do_sanitize();    return *this;  }  bitset<_Nb>& operator>>=(size_t __pos) {    this->_M_do_right_shift(__pos);    this->_M_do_sanitize();    return *this;  }  //  // Extension:  // Versions of single-bit set, reset, flip, test with no range checking.  //  bitset<_Nb>& _Unchecked_set(size_t __pos) {    this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);    return *this;  }  bitset<_Nb>& _Unchecked_set(size_t __pos, int __val) {    if (__val)      this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);    else      this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);    return *this;  }  bitset<_Nb>& _Unchecked_reset(size_t __pos) {    this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);    return *this;  }  bitset<_Nb>& _Unchecked_flip(size_t __pos) {    this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);    return *this;  }  bool _Unchecked_test(size_t __pos) const {    return (this->_M_getword(__pos) & _Base::_S_maskbit(__pos))      != static_cast<_WordT>(0);  }  // Set, reset, and flip.  bitset<_Nb>& set() {    this->_M_do_set();    this->_M_do_sanitize();    return *this;  }  bitset<_Nb>& set(size_t __pos, bool __val = true) {    if (__pos >= _Nb)      __STL_THROW(out_of_range("bitset"));    return _Unchecked_set(__pos, __val);  }  bitset<_Nb>& reset() {    this->_M_do_reset();    return *this;  }  bitset<_Nb>& reset(size_t __pos) {    if (__pos >= _Nb)      __STL_THROW(out_of_range("bitset"));    return _Unchecked_reset(__pos);  }  bitset<_Nb>& flip() {    this->_M_do_flip();    this->_M_do_sanitize();    return *this;  }  bitset<_Nb>& flip(size_t __pos) {    if (__pos >= _Nb)      __STL_THROW(out_of_range("bitset"));    return _Unchecked_flip(__pos);  }  bitset<_Nb> operator~() const {     return bitset<_Nb>(*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 this->_M_do_to_ulong(); }  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);    return __result;  }  // Helper functions for string operations.  template<class _CharT, class _Traits, class _Alloc>  void _M_copy_from_string(const basic_string<_CharT,_Traits,_Alloc>& __s,                          size_t,                          size_t);  template<class _CharT, class _Traits, class _Alloc>  void _M_copy_to_string(basic_string<_CharT,_Traits,_Alloc>&) const;  size_t count() const { return this->_M_do_count(); }  size_t size() const { return _Nb; }  bool operator==(const bitset<_Nb>& __rhs) const {    return this->_M_is_equal(__rhs);  }  bool operator!=(const bitset<_Nb>& __rhs) const {    return !this->_M_is_equal(__rhs);  }  bool test(size_t __pos) const {    if (__pos >= _Nb)      __STL_THROW(out_of_range("bitset"));    return _Unchecked_test(__pos);  }  bool any() const { return this->_M_is_any(); }  bool none() const { return !this->_M_is_any(); }  bitset<_Nb> operator<<(size_t __pos) const    { return bitset<_Nb>(*this) <<= __pos; }  bitset<_Nb> operator>>(size_t __pos) const    { return bitset<_Nb>(*this) >>= __pos; }  //  // EXTENSIONS: bit-find operations.  These operations are  // experimental, and are subject to change or removal in future  // versions.  //   // find the index of the first "on" bit  size_t _Find_first() const     { return this->_M_do_find_first(_Nb); }  // find the index of the next "on" bit after prev  size_t _Find_next( size_t __prev ) const     { return this->_M_do_find_next(__prev, _Nb); }};//// Definitions of non-inline member functions.//template <size_t _Nb>template<class _CharT, class _Traits, class _Alloc>void bitset<_Nb>  ::_M_copy_from_string(const basic_string<_CharT,_Traits,_Alloc>& __s,                        size_t __pos,                        size_t __n){  reset();  const size_t __nbits = min(_Nb, min(__n, __s.size() - __pos));  for (size_t __i = 0; __i < __nbits; ++__i) {    switch(__s[__pos + __nbits - __i - 1]) {    case '0':      break;    case '1':      set(__i);      break;    default:      __STL_THROW(invalid_argument("bitset"));    }  }}template <size_t _Nb>template <class _CharT, class _Traits, class _Alloc>void bitset<_Nb>  ::_M_copy_to_string(basic_string<_CharT, _Traits, _Alloc>& __s) const{  __s.assign(_Nb, '0');    for (size_t __i = 0; __i < _Nb; ++__i)     if (_Unchecked_test(__i))      __s[_Nb - 1 - __i] = '1';}// ------------------------------------------------------------//// 23.3.5.3 bitset operations://template <size_t _Nb>inline bitset<_Nb> operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) {  bitset<_Nb> __result(__x);  __result &= __y;  return __result;}template <size_t _Nb>inline bitset<_Nb> operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) {  bitset<_Nb> __result(__x);  __result |= __y;  return __result;}template <size_t _Nb>inline bitset<_Nb> operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) {  bitset<_Nb> __result(__x);  __result ^= __y;  return __result;}template <class _CharT, class _Traits, size_t _Nb>basic_istream<_CharT, _Traits>&operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x){  typedef typename _Traits::char_type char_type;  basic_string<_CharT, _Traits> __tmp;  __tmp.reserve(_Nb);  // Skip whitespace  typename basic_istream<_CharT, _Traits>::sentry __sentry(__is);  if (__sentry) {    basic_streambuf<_CharT, _Traits>* __buf = __is.rdbuf();    for (size_t __i = 0; __i < _Nb; ++__i) {      static typename _Traits::int_type __eof = _Traits::eof();      typename _Traits::int_type __c1 = __buf->sbumpc();      if (_Traits::eq_int_type(__c1, __eof)) {        __is.setstate(ios_base::eofbit);        break;      }      else {        char_type __c2 = _Traits::to_char_type(__c1);        char_type __c  = __is.narrow(__c2, '*');        if (__c == '0' || __c == '1')          __tmp.push_back(__c);        else if (_Traits::eq_int_type(__buf->sputbackc(__c2), __eof)) {          __is.setstate(ios_base::failbit);          break;        }      }    }    if (__tmp.empty())      __is.setstate(ios_base::failbit);    else      __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb);  }  return __is;}template <class _CharT, class _Traits, size_t _Nb>basic_ostream<_CharT, _Traits>&operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Nb>& __x){  basic_string<_CharT, _Traits> __tmp;  __x._M_copy_to_string(__tmp);  return __os << __tmp;}} // namespace std#undef __BITSET_WORDS#endif /* __SGI_STL_BITSET */// Local Variables:// mode:C++// End:

⌨️ 快捷键说明

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