_bitset.h

来自「stl的源码」· C头文件 代码 · 共 881 行 · 第 1/2 页

H
881
字号
    : _STLP_PRIV _Base_bitset<_Words >() {    if (__pos > __s.size())      __stl_throw_out_of_range("bitset");    _M_copy_from_string(__s, __pos, __n);  }#endif /* _STLP_MEMBER_TEMPLATES */  // 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) |= _STLP_PRIV _Base_bitset<_Words > ::_S_maskbit(__pos);    return *this;  }  bitset<_Nb>& _Unchecked_set(size_t __pos, int __val) {    if (__val)      this->_M_getword(__pos) |= this->_S_maskbit(__pos);    else      this->_M_getword(__pos) &= ~ this->_S_maskbit(__pos);    return *this;  }  bitset<_Nb>& _Unchecked_reset(size_t __pos) {    this->_M_getword(__pos) &= ~ this->_S_maskbit(__pos);    return *this;  }  bitset<_Nb>& _Unchecked_flip(size_t __pos) {    this->_M_getword(__pos) ^= this->_S_maskbit(__pos);    return *this;  }  bool _Unchecked_test(size_t __pos) const {    return (this->_M_getword(__pos) & this->_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) {    if (__pos >= _Nb)      __stl_throw_out_of_range("bitset");    return _Unchecked_set(__pos);  }  bitset<_Nb>& set(size_t __pos, int __val) {    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(); }#if defined (_STLP_MEMBER_TEMPLATES) && !defined (_STLP_NO_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);    return __result;  }#else  string to_string() const {    string __result;    _M_copy_to_string(__result);    return __result;  }#endif /* _STLP_EXPLICIT_FUNCTION_TMPL_ARGS */  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 {    bitset<_Nb> __result(*this);    __result <<= __pos ;  return __result;  }  bitset<_Nb> operator>>(size_t __pos) const {    bitset<_Nb> __result(*this);    __result >>= __pos ;  return __result;  }#if !defined (_STLP_NO_EXTENSIONS)  //  // 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); }#endif//// Definitions of should-be non-inline member functions.//#if defined (_STLP_MEMBER_TEMPLATES)  template<class _CharT, class _Traits, class _Alloc>  void _M_copy_from_string(const basic_string<_CharT,_Traits,_Alloc>& __s,                           size_t __pos, size_t __n) {#else  void _M_copy_from_string(const string& __s,                           size_t __pos, size_t __n) {    typedef typename string::traits_type _Traits;#endif    reset();    size_t __tmp = _Nb;    const size_t __Nbits = (min) (__tmp, (min) (__n, __s.size() - __pos));    for ( size_t __i= 0; __i < __Nbits; ++__i) {      typename _Traits::int_type __k = _Traits::to_int_type(__s[__pos + __Nbits - __i - 1]);      // boris : widen() ?      if (__k == '1')        set(__i);      else if (__k != '0')        __stl_throw_invalid_argument("bitset");    }  }#if defined (_STLP_MEMBER_TEMPLATES)  template <class _CharT, class _Traits, class _Alloc>  void _M_copy_to_string(basic_string<_CharT, _Traits, _Alloc>& __s) const#else  void _M_copy_to_string(string& __s) const#endif  {    __s.assign(_Nb, '0');    for (size_t __i = 0; __i < _Nb; ++__i) {      if (_Unchecked_test(__i))        __s[_Nb - 1 - __i] = '1';    }  }#if !defined (_STLP_MEMBER_TEMPLATES) && !defined (_STLP_NO_WCHAR_T)  void _M_copy_to_string(wstring& __s) const {    __s.assign(_Nb, '0');    for (size_t __i = 0; __i < _Nb; ++__i) {      if (_Unchecked_test(__i))        __s[_Nb - 1 - __i] = '1';    }  }#endif#if defined (_STLP_NON_TYPE_TMPL_PARAM_BUG)  bitset<_Nb> operator&(const bitset<_Nb>& __y) const {    bitset<_Nb> __result(*this);    __result &= __y;    return __result;  }  bitset<_Nb> operator|(const bitset<_Nb>& __y) const {    bitset<_Nb> __result(*this);    __result |= __y;    return __result;  }  bitset<_Nb> operator^(const bitset<_Nb>& __y) const {    bitset<_Nb> __result(*this);    __result ^= __y;    return __result;  }#endif};// ------------------------------------------------------------//// 23.3.5.3 bitset operations://#if ! defined (_STLP_NON_TYPE_TMPL_PARAM_BUG)template <size_t _Nb>inline bitset<_Nb>  _STLP_CALLoperator&(const bitset<_Nb>& __x,          const bitset<_Nb>& __y) {  bitset<_Nb> __result(__x);  __result &= __y;  return __result;}template <size_t _Nb>inline bitset<_Nb>  _STLP_CALLoperator|(const bitset<_Nb>& __x,          const bitset<_Nb>& __y) {  bitset<_Nb> __result(__x);  __result |= __y;  return __result;}template <size_t _Nb>inline bitset<_Nb>  _STLP_CALLoperator^(const bitset<_Nb>& __x,          const bitset<_Nb>& __y) {  bitset<_Nb> __result(__x);  __result ^= __y;  return __result;}#if !defined (_STLP_USE_NO_IOSTREAMS)_STLP_END_NAMESPACE#  if !(defined (_STLP_MSVC) && (_STLP_MSVC < 1300)) && \      !(defined(__SUNPRO_CC) && (__SUNPRO_CC < 0x500))#ifndef _STLP_INTERNAL_IOSFWD#  include <stl/_iosfwd.h>#endif_STLP_BEGIN_NAMESPACEtemplate <class _CharT, class _Traits, size_t _Nb>basic_istream<_CharT, _Traits>&  _STLP_CALLoperator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x);template <class _CharT, class _Traits, size_t _Nb>basic_ostream<_CharT, _Traits>& _STLP_CALLoperator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Nb>& __x);#  else#ifndef _STLP_STRING_IO_H#  include <stl/_string_io.h> //includes _istream.h and _ostream.h#endif_STLP_BEGIN_NAMESPACEtemplate <size_t _Nb>istream&  _STLP_CALLoperator>>(istream& __is, bitset<_Nb>& __x) {  typedef typename string::traits_type _Traits;  string __tmp;  __tmp.reserve(_Nb);  // Skip whitespace  typename istream::sentry __sentry(__is);  if (__sentry) {    streambuf* __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 {        typename _Traits::char_type __c2 = _Traits::to_char_type(__c1);        char __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 <size_t _Nb>ostream& _STLP_CALLoperator<<(ostream& __os, const bitset<_Nb>& __x) {  string __tmp;  __x._M_copy_to_string(__tmp);  return __os << __tmp;}#    if !defined (_STLP_NO_WCHAR_T)template <size_t _Nb>wistream&  _STLP_CALLoperator>>(wistream& __is, bitset<_Nb>& __x) {  typedef typename wstring::traits_type _Traits;  wstring __tmp;  __tmp.reserve(_Nb);  // Skip whitespace  typename wistream::sentry __sentry(__is);  if (__sentry) {    wstreambuf* __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 {        typename _Traits::char_type __c2 = _Traits::to_char_type(__c1);        char __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 <size_t _Nb>wostream& _STLP_CALLoperator<<(wostream& __os, const bitset<_Nb>& __x) {  wstring __tmp;  __x._M_copy_to_string(__tmp);  return __os << __tmp;}#    endif /* _STLP_NO_WCHAR_T */#  endif#endif#endif /* _STLP_NON_TYPE_TMPL_PARAM_BUG */#undef  bitset_STLP_END_NAMESPACE#undef __BITS_PER_WORD#undef __BITSET_WORDS#if !defined (_STLP_LINK_TIME_INSTANTIATION)#  include <stl/_bitset.c>#endif#endif /* _STLP_BITSET_H */// Local Variables:// mode:C++// End:

⌨️ 快捷键说明

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