9 #pragma pack(push,_CRT_PACKING)
10 #pragma warning(push,3)
11 #pragma push_macro("new")
14 #pragma warning(disable: 4127)
16 #pragma warning(disable: 6294)
24 typedef typename _If<_Bits <= 32, _Uint32t, _ULonglong>::type _Ty;
30 friend class bitset<_Bits>;
33 ~reference() _NOEXCEPT
39 _Pbitset->set(_Mypos, _Val);
43 reference&
operator=(
const reference& _Bitref) _NOEXCEPT
45 _Pbitset->set(_Mypos,
bool(_Bitref));
49 reference&
flip() _NOEXCEPT
51 _Pbitset->flip(_Mypos);
57 return (!_Pbitset->test(_Mypos));
60 operator bool() const _NOEXCEPT
62 return (_Pbitset->test(_Mypos));
67 : _Pbitset(0), _Mypos(0)
72 : _Pbitset(&_Bitset), _Mypos(_Pos)
80 #if _ITERATOR_DEBUG_LEVEL == 2
81 static void _Validate(
size_t _Pos)
84 _DEBUG_ERROR(
"bitset index outside range");
87 #elif _ITERATOR_DEBUG_LEVEL == 1
88 static void _Validate(
size_t _Pos)
90 _SCL_SECURE_VALIDATE_RANGE(_Pos < _Bits);
94 static void _Validate(
size_t)
99 constexpr bool _Subscript(
size_t _Pos)
const
101 return ((_Array[_Pos /
size_t(_Bitsperword)]
102 & ((_Ty)1 << _Pos % _Bitsperword)) != 0);
105 constexpr bool operator[](
size_t _Pos)
const
107 #if _ITERATOR_DEBUG_LEVEL == 0
108 return (_Subscript(_Pos));
111 return (_Bits <= _Pos
112 ? (_Validate(_Pos),
false)
117 reference operator[](
size_t _Pos)
120 return (reference(*
this, _Pos));
123 constexpr bitset() _NOEXCEPT
128 static constexpr bool _Need_mask
129 = _Bits < CHAR_BIT *
sizeof (_ULonglong);
131 static constexpr _ULonglong _Mask
132 = (1ULL << (_Need_mask ? _Bits : 0)) - 1ULL;
134 constexpr bitset(_ULonglong _Val) _NOEXCEPT
135 : _Array{
static_cast<_Ty
>(_Need_mask ? _Val & _Mask : _Val)}
139 #define _BITSET_SIZE_TYPE \
140 typename basic_string<_Elem, _Tr, _Alloc>::size_type
142 template<
class _Elem,
145 explicit bitset(
const basic_string<_Elem, _Tr, _Alloc>& _Str,
148 _Elem _E0 = (_Elem)
'0',
149 _Elem _E1 = (_Elem)
'1')
151 _Construct(_Str, _Pos, _Count, _E0, _E1);
154 template<
class _Elem>
155 explicit bitset(
const _Elem *_Ptr,
156 typename basic_string<_Elem>::size_type _Count =
157 basic_string<_Elem>::npos,
158 _Elem _E0 = (_Elem)
'0',
159 _Elem _E1 = (_Elem)
'1')
162 _Count == basic_string<_Elem>::npos
163 ? basic_string<_Elem>(_Ptr)
164 : basic_string<_Elem>(_Ptr, _Count),
165 0, _Count, _E0, _E1);
168 template<
class _Elem,
172 const basic_string<_Elem, _Tr, _Alloc>& _Str,
178 if (_Str.size() < _Pos)
180 if (_Str.size() - _Pos < _Count)
181 _Count = _Str.size() - _Pos;
183 typename basic_string<_Elem, _Tr, _Alloc>::size_type _Num;
184 for (_Num = 0; _Num < _Count; ++_Num)
185 if (!_Tr::eq(_Str[_Pos + _Num], _E0)
186 && !_Tr::eq(_Str[_Pos + _Num], _E1))
193 for (_Pos += _Count, _Num = 0; _Num < _Count; ++_Num)
194 if (_Tr::eq(_Str[--_Pos], _E1))
200 for (ptrdiff_t _Wpos = _Words; 0 <= _Wpos; --_Wpos)
201 _Array[_Wpos] &= _Right._Getword(_Wpos);
207 for (ptrdiff_t _Wpos = _Words; 0 <= _Wpos; --_Wpos)
208 _Array[_Wpos] |= _Right._Getword(_Wpos);
214 for (ptrdiff_t _Wpos = _Words; 0 <= _Wpos; --_Wpos)
215 _Array[_Wpos] ^= _Right._Getword(_Wpos);
219 bitset& operator<<=(
size_t _Pos) _NOEXCEPT
221 const ptrdiff_t _Wordshift = (ptrdiff_t)(_Pos / _Bitsperword);
223 for (ptrdiff_t _Wpos = _Words; 0 <= _Wpos; --_Wpos)
224 _Array[_Wpos] = _Wordshift <= _Wpos
225 ? _Array[_Wpos - _Wordshift] : (_Ty)0;
227 if ((_Pos %= _Bitsperword) != 0)
229 for (ptrdiff_t _Wpos = _Words; 0 < _Wpos; --_Wpos)
230 _Array[_Wpos] = (_Ty)((_Array[_Wpos] << _Pos)
231 | (_Array[_Wpos - 1] >> (_Bitsperword - _Pos)));
238 bitset& operator>>=(
size_t _Pos) _NOEXCEPT
240 const ptrdiff_t _Wordshift = (ptrdiff_t)(_Pos / _Bitsperword);
242 for (ptrdiff_t _Wpos = 0; _Wpos <= _Words; ++_Wpos)
243 _Array[_Wpos] = _Wordshift <= _Words - _Wpos
244 ? _Array[_Wpos + _Wordshift] : (_Ty)0;
246 if ((_Pos %= _Bitsperword) != 0)
248 for (ptrdiff_t _Wpos = 0; _Wpos < _Words; ++_Wpos)
249 _Array[_Wpos] = (_Ty)((_Array[_Wpos] >> _Pos)
250 | (_Array[_Wpos + 1] << (_Bitsperword - _Pos)));
251 _Array[_Words] >>= _Pos;
268 _Array[_Pos / size_t(_Bitsperword)] |= (_Ty)1 << _Pos % _Bitsperword;
270 _Array[_Pos / size_t(_Bitsperword)] &= ~((_Ty)1 << _Pos % _Bitsperword);
280 bitset& reset(
size_t _Pos)
282 return (
set(_Pos,
false));
292 for (ptrdiff_t _Wpos = _Words; 0 <= _Wpos; --_Wpos)
293 _Array[_Wpos] = (_Ty)~_Array[_Wpos];
303 _Array[_Pos / _Bitsperword] ^= (_Ty)1 << _Pos % _Bitsperword;
307 unsigned long to_ulong()
const
309 _ULonglong _Val = to_ullong();
310 unsigned long _Ans = (
unsigned long)_Val;
316 _ULonglong to_ullong()
const
318 static_assert(
sizeof (_ULonglong) %
sizeof (_Ty) == 0,
319 "unsigned long long not multiple of _Ty");
321 ptrdiff_t _Wpos = _Words;
322 for (; (ptrdiff_t)(
sizeof (_ULonglong) /
sizeof (_Ty)) <= _Wpos;
324 if (_Array[_Wpos] != 0)
327 _ULonglong _Val = _Array[_Wpos];
328 for (; 0 <= --_Wpos; )
329 _Val = ((_Val << (_Bitsperword - 1)) << 1) | _Array[_Wpos];
333 template<
class _Elem = char,
334 class _Tr = char_traits<_Elem>,
335 class _Alloc = allocator<_Elem> >
336 basic_string<_Elem, _Tr, _Alloc>
337 to_string(_Elem _E0 = (_Elem)
'0',
338 _Elem _E1 = (_Elem)
'1')
const
340 basic_string<_Elem, _Tr, _Alloc> _Str;
341 typename basic_string<_Elem, _Tr, _Alloc>::size_type _Pos;
344 for (_Pos = _Bits; 0 < _Pos; )
352 size_t count() const _NOEXCEPT
354 const char *
const _Bitsperbyte =
355 "\0\1\1\2\1\2\2\3\1\2\2\3\2\3\3\4"
356 "\1\2\2\3\2\3\3\4\2\3\3\4\3\4\4\5"
357 "\1\2\2\3\2\3\3\4\2\3\3\4\3\4\4\5"
358 "\2\3\3\4\3\4\4\5\3\4\4\5\4\5\5\6"
359 "\1\2\2\3\2\3\3\4\2\3\3\4\3\4\4\5"
360 "\2\3\3\4\3\4\4\5\3\4\4\5\4\5\5\6"
361 "\2\3\3\4\3\4\4\5\3\4\4\5\4\5\5\6"
362 "\3\4\4\5\4\5\5\6\4\5\5\6\5\6\6\7"
363 "\1\2\2\3\2\3\3\4\2\3\3\4\3\4\4\5"
364 "\2\3\3\4\3\4\4\5\3\4\4\5\4\5\5\6"
365 "\2\3\3\4\3\4\4\5\3\4\4\5\4\5\5\6"
366 "\3\4\4\5\4\5\5\6\4\5\5\6\5\6\6\7"
367 "\2\3\3\4\3\4\4\5\3\4\4\5\4\5\5\6"
368 "\3\4\4\5\4\5\5\6\4\5\5\6\5\6\6\7"
369 "\3\4\4\5\4\5\5\6\4\5\5\6\5\6\6\7"
370 "\4\5\5\6\5\6\6\7\5\6\6\7\6\7\7\x8";
371 const unsigned char *_Ptr =
372 (
const unsigned char *)(
const void *)_Array;
373 const unsigned char *
const _End = _Ptr +
sizeof (_Array);
375 for ( ; _Ptr != _End; ++_Ptr)
376 _Val += _Bitsperbyte[*_Ptr];
380 constexpr size_t size() const _NOEXCEPT
387 return (_Hash_seq((
const unsigned char *)_Array,
391 bool operator==(
const bitset& _Right)
const _NOEXCEPT
393 for (ptrdiff_t _Wpos = _Words; 0 <= _Wpos; --_Wpos)
394 if (_Array[_Wpos] != _Right._Getword(_Wpos))
399 bool operator!=(
const bitset& _Right)
const _NOEXCEPT
401 return (!(*
this == _Right));
404 bool test(
size_t _Pos)
const
408 return (_Subscript(_Pos));
411 bool any() const _NOEXCEPT
413 for (ptrdiff_t _Wpos = _Words; 0 <= _Wpos; --_Wpos)
414 if (_Array[_Wpos] != 0)
419 bool none() const _NOEXCEPT
424 bool all() const _NOEXCEPT
426 return (count() == size());
429 bitset operator<<(
size_t _Pos)
const _NOEXCEPT
431 return (
bitset(*
this) <<= _Pos);
434 bitset operator>>(
size_t _Pos)
const _NOEXCEPT
436 return (
bitset(*
this) >>= _Pos);
439 _Ty _Getword(
size_t _Wpos)
const
441 return (_Array[_Wpos]);
448 _Bitsperword = (ptrdiff_t)(CHAR_BIT *
sizeof (_Ty)),
449 _Words = (ptrdiff_t)(_Bits == 0
450 ? 0 : (_Bits - 1) / _Bitsperword)};
452 void _Tidy(_Ty _Wordval = 0)
454 for (ptrdiff_t _Wpos = _Words; 0 <= _Wpos; --_Wpos)
455 _Array[_Wpos] = _Wordval;
462 _Trim_if(bool_constant<_Bits == 0 || _Bits % _Bitsperword != 0>());
465 void _Trim_if(true_type)
467 _Array[_Words] &= ((_Ty)1 << _Bits % _Bitsperword) - 1;
470 void _Trim_if(false_type)
474 [[noreturn]]
void _Xinv()
const
476 _Xinvalid_argument(
"invalid bitset<N> char");
479 [[noreturn]]
void _Xoflo()
const
481 _Xoverflow_error(
"bitset<N> overflow");
484 [[noreturn]]
void _Xran()
const
486 _Xout_of_range(
"invalid bitset<N> position");
489 _Ty _Array[_Words + 1];
493template<
size_t _Bits>
inline
498 return (_Ans &= _Right);
501template<
size_t _Bits>
inline
506 return (_Ans |= _Right);
509template<
size_t _Bits>
inline
514 return (_Ans ^= _Right);
521 basic_ostream<_Elem, _Tr>& _Ostr,
const bitset<_Bits>& _Right)
523 const ctype<_Elem>& _Ctype_fac = _USE(_Ostr.getloc(), ctype<_Elem>);
524 const _Elem _E0 = _Ctype_fac.widen(
'0');
525 const _Elem _E1 = _Ctype_fac.widen(
'1');
528 << _Right.template to_string<_Elem, _Tr, allocator<_Elem> >(
539 const ctype<_Elem>& _Ctype_fac = _USE(_Istr.getloc(), ctype<_Elem>);
540 const _Elem _E0 = _Ctype_fac.widen(
'0');
541 const _Elem _E1 = _Ctype_fac.widen(
'1');
542 ios_base::iostate _State = ios_base::goodbit;
543 bool _Changed =
false;
545 const typename basic_istream<_Elem, _Tr>::sentry _Ok(_Istr);
550 typename _Tr::int_type _Meta = _Istr.rdbuf()->sgetc();
551 for (
size_t _Count = _Right.size(); 0 < _Count;
552 _Meta = _Istr.rdbuf()->snextc(), --_Count)
555 if (_Tr::eq_int_type(_Tr::eof(), _Meta))
557 _State |= ios_base::eofbit;
560 else if ((_Char = _Tr::to_char_type(_Meta)) != _E0
563 else if (_Str.max_size() <= _Str.size())
565 _State |= ios_base::failbit;
581 _State |= ios_base::failbit;
582 _Istr.setstate(_State);
588template<
size_t _Bits>
596 return (_Keyval.hash());
601 #pragma pop_macro("new")
#define _BITSET_SIZE_TYPE
Definition: bitset.h:139
bitset< _Bits > operator|(const bitset< _Bits > &_Left, const bitset< _Bits > &_Right) _NOEXCEPT
Definition: bitset.h:502
basic_istream< _Elem, _Tr > & operator>>(basic_istream< _Elem, _Tr > &_Istr, bitset< _Bits > &_Right)
Definition: bitset.h:536
basic_ostream< _Elem, _Tr > & operator<<(basic_ostream< _Elem, _Tr > &_Ostr, const bitset< _Bits > &_Right)
Definition: bitset.h:520
bitset< _Bits > operator&(const bitset< _Bits > &_Left, const bitset< _Bits > &_Right) _NOEXCEPT
Definition: bitset.h:494
bitset< _Bits > operator^(const bitset< _Bits > &_Left, const bitset< _Bits > &_Right) _NOEXCEPT
Definition: bitset.h:510
bool operator~() const _NOEXCEPT
Definition: bitset.h:55
reference & operator=(const reference &_Bitref) _NOEXCEPT
Definition: bitset.h:43
reference & flip() _NOEXCEPT
Definition: bitset.h:49
_If< _Bits<=32, _Uint32t, _ULonglong >::type _Ty;class reference { friend class bitset< _Bits >;public:~reference() _NOEXCEPT { } reference &operator=(bool _Val) _NOEXCEPT { _Pbitset-> set(_Mypos, _Val)
Definition: bitset.h:39
size_t result_type
Definition: bitset.h:592
bitset< _Bits > argument_type
Definition: bitset.h:591
size_t operator()(const argument_type &_Keyval) const
Definition: bitset.h:594