📄 bitset
字号:
return __result; } unsigned long _M_do_to_ulong() const { return _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.//size_t _Base_bitset<1>::_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;}size_t _Base_bitset<1>::_M_do_find_next(size_t __prev, size_t __not_found ) const{ // make bound inclusive ++__prev; // check out of bounds if ( __prev >= __BITS_PER_WORD ) 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// ------------------------------------------------------------// Helper class to zero out the unused high-order bits in the highest word.template <size_t _Extrabits> struct _Sanitize { static void _M_do_sanitize(unsigned long& __val) { __val &= ~((~static_cast<unsigned long>(0)) << _Extrabits); }};__STL_TEMPLATE_NULL struct _Sanitize<0> { static void _M_do_sanitize(unsigned long) {}};// ------------------------------------------------------------// Class bitset.// _Nb may be any nonzero number of type size_t.template<size_t _Nb>class bitset : private _Base_bitset<__BITSET_WORDS(_Nb)>{private: typedef _Base_bitset<__BITSET_WORDS(_Nb)> _Base; typedef unsigned long _WordT;private: void _M_do_sanitize() { _Sanitize<_Nb%__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(); }#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 /* __STL_MEMBER_TEMPLATES */ 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>& 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) { 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(__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); return __result; }#endif /* member templates and explicit function template args */ // Helper functions for string operations.#ifdef __STL_MEMBER_TEMPLATES 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;#else /* __STL_MEMBER_TEMPLATES */ void _M_copy_from_string(const basic_string<char>&, size_t, size_t); void _M_copy_to_string(basic_string<char>&) const;#endif /* __STL_MEMBER_TEMPLATES */ 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(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -