STL 용기 의 bitset

저작권 성명: 본 고 는 행자 (http://blog.csdn.net/liuyuan185442111), 전재 출처 를 밝 혀 주세요.
bitset 는 일련의 bit 위 치 를 편리 하 게 관리 하 는 데 사용 되 며, 표준 용기 가 아 닙 니 다.bitset 는 < bitset > 에서 정의 합 니 다:
template <size_t N> class bitset;

bitset 인터페이스
구조 함수
//       ,      0
bitset();
//  val       
bitset(unsigned long val);
//  str pos   n      bitset  n   ,  n      0 1,
//         ,   invalid_argument  ,
// pos    str    out_of_range  
template<class charT, class traits, class Alloc>
  explicit bitset (const basic_string<charT,traits,Alloc>& str,
    typename basic_string<charT,traits,Alloc>::size_type pos = 0,
    typename basic_string<charT,traits,Alloc>::size_type n =
      basic_string<charT,traits,Alloc>::npos);

구조 함수 와 할당 연산 자 를 명시 적 으로 복사 하지 않 았 습 니 다. 기본 값 으로 충분 하기 때 문 입 니 다.
연산 자
//     
bitset& operator&=(const bitset& rhs);
bitset& operator|=(const bitset& rhs);
bitset& operator^=(const bitset& rhs);
bitset& operator<<=(size_t pos);
bitset& operator>>=(size_t pos);
bitset operator~() const;
bitset operator<<(size_t pos) const;
bitset operator>>(size_t pos) const;
bool operator==(const bitset& rhs) const;
bool operator!=(const bitset& rhs) const;
//      
template <size_t N>
  bitset<N> operator&(const bitset<N>& lhs, const bitset<N>& rhs);
template <size_t N>
  bitset<N> operator|(const bitset<N>& lhs, const bitset<N>& rhs);
template <size_t N>
  bitset<N> operator^(const bitset<N>& lhs, const bitset<N>& rhs);
//     
template<class charT, class traits, size_t N>
  basic_istream<charT, traits>&
    operator>> (basic_istream<charT,traits>& is, bitset<N>& rhs);
template<class charT, class traits, size_t N>
  basic_ostream<charT, traits>&
    operator<< (basic_ostream<charT,traits>& os, const bitset<N>& rhs);

구성원 방문
//          ,  test()    
bool operator[](size_t pos) const;
//       ,        
reference operator[](size_t pos);
//   bitset 1   
size_t count() const;
//   bitset   
size_t size() const;
//      1,      out_of_range  
bool test(size_t pos) const;
//         1     true
bool any() const;
//     true,    !any();
bool none() const;

비트 조작
//       1
bitset& set();
//       val,      out_of_range  
bitset& set(size_t pos, bool val = true);
//       
bitset& reset();
//       ,      out_of_range  
bitset& reset(size_t pos);
//      
bitset& flip();
//      ,      out_of_range  
bitset& flip(size_t pos)

바꾸다
//     01   ,  <<          
template <class charT, class traits, class Alloc>
  basic_string<charT,traits,Alloc> to_string() const;
//     unsigned long,   bitset         ,   overflow_error  
unsigned long to_ulong() const;

해석 하 다.
내부 데이터 구조
bitset 내 부 는 unsigned long 배열 로 각 비트 비트 를 저장 하기 때문에 unsigned long 이 4 바이트 를 차지 하면 sizeof (bitset 대상) 의 값 은 항상 4 의 정수 배 입 니 다.오, sizeof (bitset < 0 >) 를 제외 하고, 이때 unsigned long 배열 의 길 이 는 0 이 고, bitset < 0 > 은 데이터 구성원 이 없 는 클래스 이 며, 크기 는 1 입 니 다.sgi 의 실현 에서 정의Base_bitset 의 템 플 릿 클래스, bitset 계승, bitset 의 대부분 작업 은Base_bitset 에서 완료, unsigned long 배열 도 로 정의 합 니 다.Base_bitset 에서:
unsigned long M_w[Nw];

그 중에서 Nw 는 필요 한 unsigned long 의 숫자 로 (n + 31) / 32 로 계산 할 수 있 습 니 다.M_w 배열 의 아래 표 시 는 클 수록 비트 서열 을 나타 내 는 자릿수 가 높다.비트 시퀀스: 높 은 32 비트 낮은 32 비트 정형 배열: 요소 0 요소 1 요소 0 저장 낮은 32 비트, 요소 1 저장 높 은 32 비트.Base_bitset 는 또 하나의 unsigned long 만 있 는 상황 을 특수 화 시 켜 효율 을 높 였 다.
검표 법
count () 내부 에서 차 트 법 을 사 용 했 습 니 다.256 길이 의 unsigned char 배열 을 정의 하여 모든 unsigned char 에 대응 하 는 1 의 개 수 를 저장 합 니 다.모든 unsigned long 을 4 개의 unsigned char 로 나 눈 다음 unsigned char 를 배열 로 표시 하면 1 의 개 수 를 얻 을 수 있 고 순서대로 누적 하면 최종 결 과 를 얻 을 수 있 습 니 다.
미사 용 부분
unsigned long 배열 이 꼭 다 쓴 것 은 아니 기 때문에 사용 하지 않 은 부분 은 내부 함수 에 의 해 제거 되 지 않 습 니 다.구조 대상 이나 사용 하지 않 은 부분 을 변경 할 수 있 는 작업 을 할 때 이 함 수 를 호출 합 니 다.그래서 사용 하지 않 은 부분 은 항상 0 이다.
참조 형식
bitset 내부 에서 reference 클래스 를 정 의 했 습 니 다. operator [] 의 한 버 전의 반환 값 입 니 다. operator [] 는 두 가지 버 전이 있 습 니 다.
bool operator[] (size_t pos) const;
reference operator[] (size_t pos);

첫 번 째 버 전 은 읽 기만 하고 두 번 째 버 전 은 쓸 수 있다.reference 는 operator [] 가 쓴 기능 을 실현 하기 위해 서 입 니 다.
reference operator[](size_t pos) { return reference(*this, pos); }

reference 에 unsigned long * M 이 있 습 니 다.wp 데이터 멤버, 통과
reference( bitset& b, size_t pos );

구 조 를 진행 할 때 Mwp 는 pos 에 대응 하 는 unsigned long 을 가리 키 며 M 을 통과 할 수 있 습 니 다.wp 에서 b 를 조작 하 러 왔 습 니 다.b [i] 에 대해 할당 작업 을 할 수 있 을 뿐만 아니 라 4. 567914 도 할 수 있다.구체 적 으로 소스 코드 를 보 세 요.
전체 원본 코드 (std bitset. h 에서 따 서 약간 수정):
//    n bitset       __BITSET_WORDS(n)
//   unsigned long 4     , bitset<0> 1  , bitset<1> 4  , bitset<32> 4  , bitset<33> 8  
//    unsigned long      ,         sizeof(unsigned long)    
//         0
#define _GLIBCPP_BITSET_BITS_PER_WORD (CHAR_BIT*sizeof(unsigned long))
#define __BITSET_WORDS(__n) \
 ((__n) < 1 ? 1 : ((__n) + _GLIBCPP_BITSET_BITS_PER_WORD - 1)/_GLIBCPP_BITSET_BITS_PER_WORD)
/** // gcc 4.7.1      #define _GLIBCXX_BITSET_WORDS(__n) \ ((__n) / _GLIBCXX_BITSET_BITS_PER_WORD + \ ((__n) % _GLIBCXX_BITSET_BITS_PER_WORD == 0 ? 0 : 1))      n 0   ,    1,    0    ,  n 0  , _Base_bitset _M_w     0  ,          1   n<1      : #define __BITSET_WORDS(__n) \ (((__n) + _GLIBCPP_BITSET_BITS_PER_WORD - 1)/_GLIBCPP_BITSET_BITS_PER_WORD) */

//      unsigned char 1   ,  structure  ,        ,      
template<bool __dummy>
struct _Bit_count {
  static unsigned char _S_bit_count[256];
};

// Mapping from 8 bit unsigned integers to the index of the first one
// bit:
template<bool __dummy>
struct _First_one {
  static unsigned char _S_first_one[256];
};


// _Nw bitset  Word   
template<size_t _Nw>
struct _Base_bitset {
  typedef unsigned long _WordT;

  _WordT _M_w[_Nw]; //        ,       

  _Base_bitset( void ) { _M_do_reset(); }
  _Base_bitset(unsigned long __val) {
    _M_do_reset();
    _M_w[0] = __val;
  }
  void _M_do_reset() { memset(_M_w, 0, _Nw * sizeof(_WordT)); }


  static size_t _S_whichword( size_t __pos )
    { return __pos / _GLIBCPP_BITSET_BITS_PER_WORD; }
  static size_t _S_whichbyte( size_t __pos )
    { return (__pos % _GLIBCPP_BITSET_BITS_PER_WORD) / CHAR_BIT; }
  static size_t _S_whichbit( size_t __pos )
    { return __pos % _GLIBCPP_BITSET_BITS_PER_WORD; }
    //     ,  Word     
  static _WordT _S_maskbit( size_t __pos )
    { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }

  _WordT& _M_getword(size_t __pos)       { return _M_w[_S_whichword(__pos)]; }
  _WordT  _M_getword(size_t __pos) const { return _M_w[_S_whichword(__pos)]; }

  //    Word
  _WordT& _M_hiword()       { return _M_w[_Nw - 1]; }
  _WordT  _M_hiword() const { return _M_w[_Nw - 1]; }

  void _M_do_and(const _Base_bitset<_Nw>& __x) {
    for ( size_t __i = 0; __i < _Nw; __i++ ) {
      _M_w[__i] &= __x._M_w[__i];
    }
  }

  void _M_do_or(const _Base_bitset<_Nw>& __x) {
    for ( size_t __i = 0; __i < _Nw; __i++ ) {
      _M_w[__i] |= __x._M_w[__i];
    }
  }

  void _M_do_xor(const _Base_bitset<_Nw>& __x) {
    for ( size_t __i = 0; __i < _Nw; __i++ ) {
      _M_w[__i] ^= __x._M_w[__i];
    }
  }

  void _M_do_left_shift(size_t __shift);
  void _M_do_right_shift(size_t __shift);

  void _M_do_flip() {
    for ( size_t __i = 0; __i < _Nw; __i++ ) {
      _M_w[__i] = ~_M_w[__i];
    }
  }

  void _M_do_set() {
    for ( size_t __i = 0; __i < _Nw; __i++ ) {
      _M_w[__i] = ~static_cast<_WordT>(0);
    }
  }

  bool _M_is_equal(const _Base_bitset<_Nw>& __x) const {
    for (size_t __i = 0; __i < _Nw; ++__i) {
      if (_M_w[__i] != __x._M_w[__i])
        return false;
    }
    return true;
  }

  bool _M_is_any() const {
    for ( size_t __i = 0; __i < _Nw; __i++ ) {
      if ( _M_w[__i] != static_cast<_WordT>(0) )
        return true;
    }
    return false;
  }

  //   1   ,      char*,     
  size_t _M_do_count() const {
    size_t __result = 0;
    const unsigned char* __byte_ptr = (const unsigned char*)_M_w;
    const unsigned char* __end_ptr = (const unsigned char*)(_M_w+_Nw);

    while ( __byte_ptr < __end_ptr ) {
      //       unsigned char 1   
      __result += _Bit_count<true>::_S_bit_count[*__byte_ptr];
      __byte_ptr++;
    }
    return __result;
  }

  unsigned long _M_do_to_ulong() const;

  // find first "on" bit
  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;
};


/// _Base_bitset         ///
template<size_t _Nw>
void _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift)
{
  if (__shift != 0) {
    const size_t __wshift = __shift / _GLIBCPP_BITSET_BITS_PER_WORD;
    const size_t __offset = __shift % _GLIBCPP_BITSET_BITS_PER_WORD;

    if (__offset == 0)
      for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
        _M_w[__n] = _M_w[__n - __wshift];

    else {
      const size_t __sub_offset = _GLIBCPP_BITSET_BITS_PER_WORD - __offset;
      for (size_t __n = _Nw - 1; __n > __wshift; --__n)
        _M_w[__n] = (_M_w[__n - __wshift] << __offset) |
                    (_M_w[__n - __wshift - 1] >> __sub_offset);
      _M_w[__wshift] = _M_w[0] << __offset;
    }

    fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
  }
}

template<size_t _Nw>
void _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift)
{
  if (__shift != 0) {
    const size_t __wshift = __shift / _GLIBCPP_BITSET_BITS_PER_WORD;
    const size_t __offset = __shift % _GLIBCPP_BITSET_BITS_PER_WORD;
    const size_t __limit = _Nw - __wshift - 1;

    if (__offset == 0)
      for (size_t __n = 0; __n <= __limit; ++__n)
        _M_w[__n] = _M_w[__n + __wshift];

    else {
      const size_t __sub_offset = _GLIBCPP_BITSET_BITS_PER_WORD - __offset;
      for (size_t __n = 0; __n < __limit; ++__n)
        _M_w[__n] = (_M_w[__n + __wshift] >> __offset) |
                    (_M_w[__n + __wshift + 1] << __sub_offset);
      _M_w[__limit] = _M_w[_Nw-1] >> __offset;
    }

    fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
  }
}

//       ,       ,   overflow_error  
template<size_t _Nw>
unsigned long _Base_bitset<_Nw>::_M_do_to_ulong() const
{
  for (size_t __i = 1; __i < _Nw; ++__i)
    if (_M_w[__i])
      __STL_THROW(overflow_error("bitset"));

  return _M_w[0];
}

//         
template<size_t _Nw>
size_t _Base_bitset<_Nw>::_M_do_find_first(size_t __not_found) const
{
  for ( size_t __i = 0; __i < _Nw; __i++ ) {
    _WordT __thisword = _M_w[__i];
    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 __i*_GLIBCPP_BITSET_BITS_PER_WORD + __j*CHAR_BIT +
            _First_one<true>::_S_first_one[__this_byte];

        __thisword >>= CHAR_BIT;
      }
    }
  }
  // not found, so return an indication of failure.
  return __not_found;
}

template<size_t _Nw>
size_t
_Base_bitset<_Nw>::_M_do_find_next(size_t __prev, size_t __not_found) const
{
  // make bound inclusive
  ++__prev;

  // check out of bounds
  if ( __prev >= _Nw * _GLIBCPP_BITSET_BITS_PER_WORD )
    return __not_found;

    // search first word
  size_t __i = _S_whichword(__prev);
  _WordT __thisword = _M_w[__i];

    // 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 __i*_GLIBCPP_BITSET_BITS_PER_WORD + __j*CHAR_BIT +
          _First_one<true>::_S_first_one[__this_byte];

      __thisword >>= CHAR_BIT;
    }
  }

  // check subsequent words
  __i++;
  for ( ; __i < _Nw; __i++ ) {
    __thisword = _M_w[__i];
    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 __i*_GLIBCPP_BITSET_BITS_PER_WORD + __j*CHAR_BIT +
            _First_one<true>::_S_first_one[__this_byte];

        __thisword >>= CHAR_BIT;
      }
    }
  }

  // not found, so return an indication of failure.
  return __not_found;
}


///------------------------------------------------------------
//     word     
template<> struct _Base_bitset<1> {
  typedef unsigned long _WordT;
  _WordT _M_w;

  _Base_bitset( void ) : _M_w(0) {}
  _Base_bitset(unsigned long __val) : _M_w(__val) {}

  static size_t _S_whichword( size_t __pos )
    { return __pos / _GLIBCPP_BITSET_BITS_PER_WORD; }
  static size_t _S_whichbyte( size_t __pos )
    { return (__pos % _GLIBCPP_BITSET_BITS_PER_WORD) / CHAR_BIT; }
  static size_t _S_whichbit( size_t __pos )
    {  return __pos % _GLIBCPP_BITSET_BITS_PER_WORD; }
  static _WordT _S_maskbit( size_t __pos )
    { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }

  _WordT& _M_getword(size_t)       { return _M_w; }
  _WordT  _M_getword(size_t) const { return _M_w; }

  _WordT& _M_hiword()       { return _M_w; }
  _WordT  _M_hiword() const { return _M_w; }

  void _M_do_and(const _Base_bitset<1>& __x) { _M_w &= __x._M_w; }
  void _M_do_or(const _Base_bitset<1>& __x)  { _M_w |= __x._M_w; }
  void _M_do_xor(const _Base_bitset<1>& __x) { _M_w ^= __x._M_w; }
  void _M_do_left_shift(size_t __shift)     { _M_w <<= __shift; }
  void _M_do_right_shift(size_t __shift)    { _M_w >>= __shift; }
  void _M_do_flip()                       { _M_w = ~_M_w; }
  void _M_do_set()                        { _M_w = ~static_cast<_WordT>(0); }
  void _M_do_reset()                      { _M_w = 0; }

  bool _M_is_equal(const _Base_bitset<1>& __x) const
    { return _M_w == __x._M_w; }
  bool _M_is_any() const
    { return _M_w != 0; }

  size_t _M_do_count() const {
    size_t __result = 0;
    const unsigned char* __byte_ptr = (const unsigned char*)&_M_w;
    const unsigned char* __end_ptr
      = ((const unsigned char*)&_M_w)+sizeof(_M_w);
    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 { 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;

};


// ------------------------------------------------------------
//    ,      word         
template <size_t _Extrabits> struct _Sanitize {
  static void _M_do_sanitize(unsigned long& __val)
    { __val &= ~((~static_cast<unsigned long>(0)) << _Extrabits); }
};

template<> struct _Sanitize<0> {
  static void _M_do_sanitize(unsigned long) {}
};



// ------------------------------------------------------------
// Class bitset.

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:
  //    word         
  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;
    }
  };

  // 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);
  }

  // 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;
  }

  //      

  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:
  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; }

  //     ,        ,            
  // 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); }
};

// bitset string       
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';
}


///      : , ,  ,<<,>> ////////////////////////////////////
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;
}

//              ,         bitset,      invalid_argument  
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;
}

좋은 웹페이지 즐겨찾기