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

📄 std_bitset.h

📁 gcc-you can use this code to learn something about gcc, and inquire further into linux,
💻 H
📖 第 1 页 / 共 3 页
字号:
    /**     *  These versions of single-bit set, reset, flip, and test are     *  extensions from the SGI version.  They do no range checking.     *  @ingroup SGIextensions    */    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.    /**     *  @brief Sets every bit to true.    */    bitset<_Nb>&    set()    {      this->_M_do_set();      this->_M_do_sanitize();      return *this;    }    /**     *  @brief Sets a given bit to a particular value.     *  @param  pos  The index of the bit.     *  @param  val  Either true or false, defaults to true.     *  @throw  std::out_of_range  If @a pos is bigger the size of the %set.    */    bitset<_Nb>&    set(size_t __pos, bool __val = true)    {      if (__pos >= _Nb)	__throw_out_of_range("bitset -- set() argument too large");      return _Unchecked_set(__pos, __val);    }    /**     *  @brief Sets every bit to false.    */    bitset<_Nb>&    reset()    {      this->_M_do_reset();      return *this;    }    /**     *  @brief Sets a given bit to false.     *  @param  pos  The index of the bit.     *  @throw  std::out_of_range  If @a pos is bigger the size of the %set.     *     *  Same as writing @c set(pos,false).    */    bitset<_Nb>&    reset(size_t __pos)    {      if (__pos >= _Nb)	__throw_out_of_range("bitset -- reset() argument too large");      return _Unchecked_reset(__pos);    }    /**     *  @brief Toggles every bit to its opposite value.    */    bitset<_Nb>&    flip()    {      this->_M_do_flip();      this->_M_do_sanitize();      return *this;    }    /**     *  @brief Toggles a given bit to its opposite value.     *  @param  pos  The index of the bit.     *  @throw  std::out_of_range  If @a pos is bigger the size of the %set.    */    bitset<_Nb>&    flip(size_t __pos)    {      if (__pos >= _Nb)	__throw_out_of_range("bitset -- flip() argument too large");      return _Unchecked_flip(__pos);    }    /// See the no-argument flip().    bitset<_Nb>    operator~() const { return bitset<_Nb>(*this).flip(); }    //@{    /**     *  @brief  Array-indexing support.     *  @param  pos  Index into the %bitset.     *  @return  A bool for a 'const %bitset'.  For non-const bitsets, an     *           instance of the reference proxy class.     *  @note  These operators do no range checking and throw no exceptions,     *         as required by DR 11 to the standard.     *     *  @if maint     *  _GLIBCPP_RESOLVE_LIB_DEFECTS Note that this implementation already     *  resolves DR 11 (items 1 and 2), but does not do the range-checking     *  required by that DR's resolution.  -pme     *  The DR has since been changed:  range-checking is a precondition     *  (users' responsibility), and these functions must not throw.  -pme     *  @endif    */    reference    operator[](size_t __pos) { return reference(*this,__pos); }    bool    operator[](size_t __pos) const { return _Unchecked_test(__pos); }    //@}    /**     *  @brief Retuns a numerical interpretation of the %bitset.     *  @return  The integral equivalent of the bits.     *  @throw  std::overflow_error  If there are too many bits to be     *                               represented in an @c unsigned @c long.    */    unsigned long    to_ulong() const { return this->_M_do_to_ulong(); }    /**     *  @brief Retuns a character interpretation of the %bitset.     *  @return  The string equivalent of the bits.     *     *  Note the ordering of the bits:  decreasing character positions     *  correspond to increasing bit positions (see the main class notes for     *  an example).     *     *  Also note that you must specify the string's template parameters     *  explicitly.  Given a bitset @c bs and a string @s:     *  @code     *     s = bs.to_string<char,char_traits<char>,allocator<char> >();     *  @endcode    */    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;    /// Returns the number of bits which are set.    size_t    count() const { return this->_M_do_count(); }    /// Returns the total number of bits.    size_t    size() const { return _Nb; }    //@{    /// These comparisons for equality/inequality are, well, @e bitwise.    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); }    //@}    /**     *  @brief Tests the value of a bit.     *  @param  pos  The index of a bit.     *  @return  The value at @a pos.     *  @throw  std::out_of_range  If @a pos is bigger the size of the %set.    */    bool    test(size_t __pos) const    {      if (__pos >= _Nb)	__throw_out_of_range("bitset -- test() argument too large");      return _Unchecked_test(__pos);    }    /**     *  @brief Tests whether any of the bits are on.     *  @return  True if at least one bit is set.    */    bool    any() const { return this->_M_is_any(); }    /**     *  @brief Tests whether any of the bits are on.     *  @return  True if none of the bits are set.    */    bool    none() const { return !this->_M_is_any(); }    //@{    /// Self-explanatory.    bitset<_Nb>    operator<<(size_t __pos) const    { return bitset<_Nb>(*this) <<= __pos; }    bitset<_Nb>    operator>>(size_t __pos) const    { return bitset<_Nb>(*this) >>= __pos; }    //@}    /**     *  @brief  Finds the index of the first "on" bit.     *  @return  The index of the first bit set, or size() if not found.     *  @ingroup SGIextensions     *  @sa  _Find_next    */    size_t    _Find_first() const    { return this->_M_do_find_first(_Nb); }    /**     *  @brief  Finds the index of the next "on" bit after prev.     *  @return  The index of the next bit set, or size() if not found.     *  @param  prev  Where to start searching.     *  @ingroup SGIextensions     *  @sa  _Find_first    */    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:	      __throw_invalid_argument("bitset -- string contains characters "	                               "which are neither 0 nor 1");	    }	}    }  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:  //@{  /**   *  @brief  Global bitwise operations on bitsets.   *  @param  x  A bitset.   *  @param  y  A bitset of the same size as @a x.   *  @return  A new bitset.   *   *  These should be self-explanatory.  */  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;    }  //@}  //@{  /**   *  @brief Global I/O operators for bitsets.   *   *  Direct I/O between streams and bitsets is supported.  Output is   *  straightforward.  Input will skip whitespace, only accept '0' and '1'   *  characters, and will only extract as many digits as the %bitset will   *  hold.  */  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)	{	  ios_base::iostate  __state = ios_base::goodbit;	  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))		{		  __state |= 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))		    {		      __state |= ios_base::failbit;		      break;		    }		}	    }	  if (__tmp.empty() && !_Nb)	    __state |= ios_base::failbit;	  else	    __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb);	  if (__state != ios_base::goodbit)	    __is.setstate(__state);    // may throw an exception	}      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 _GLIBCPP_BITSET_WORDS#endif /* _GLIBCPP_BITSET_H */

⌨️ 快捷键说明

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