You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1599 lines
45KB

  1. // <bitset> -*- C++ -*-
  2. // Copyright (C) 2001-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. * Copyright (c) 1998
  22. * Silicon Graphics Computer Systems, Inc.
  23. *
  24. * Permission to use, copy, modify, distribute and sell this software
  25. * and its documentation for any purpose is hereby granted without fee,
  26. * provided that the above copyright notice appear in all copies and
  27. * that both that copyright notice and this permission notice appear
  28. * in supporting documentation. Silicon Graphics makes no
  29. * representations about the suitability of this software for any
  30. * purpose. It is provided "as is" without express or implied warranty.
  31. */
  32. /** @file include/bitset
  33. * This is a Standard C++ Library header.
  34. */
  35. #ifndef _GLIBCXX_BITSET
  36. #define _GLIBCXX_BITSET 1
  37. #pragma GCC system_header
  38. #include <string>
  39. #include <bits/functexcept.h> // For invalid_argument, out_of_range,
  40. // overflow_error
  41. #include <iosfwd>
  42. #include <bits/cxxabi_forced.h>
  43. #if __cplusplus >= 201103L
  44. # include <bits/functional_hash.h>
  45. #endif
  46. #define _GLIBCXX_BITSET_BITS_PER_WORD (__CHAR_BIT__ * __SIZEOF_LONG__)
  47. #define _GLIBCXX_BITSET_WORDS(__n) \
  48. ((__n) / _GLIBCXX_BITSET_BITS_PER_WORD + \
  49. ((__n) % _GLIBCXX_BITSET_BITS_PER_WORD == 0 ? 0 : 1))
  50. #define _GLIBCXX_BITSET_BITS_PER_ULL (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
  51. namespace std _GLIBCXX_VISIBILITY(default)
  52. {
  53. _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
  54. /**
  55. * Base class, general case. It is a class invariant that _Nw will be
  56. * nonnegative.
  57. *
  58. * See documentation for bitset.
  59. */
  60. template<size_t _Nw>
  61. struct _Base_bitset
  62. {
  63. typedef unsigned long _WordT;
  64. /// 0 is the least significant word.
  65. _WordT _M_w[_Nw];
  66. _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
  67. : _M_w() { }
  68. #if __cplusplus >= 201103L
  69. constexpr _Base_bitset(unsigned long long __val) noexcept
  70. : _M_w{ _WordT(__val)
  71. #if __SIZEOF_LONG_LONG__ > __SIZEOF_LONG__
  72. , _WordT(__val >> _GLIBCXX_BITSET_BITS_PER_WORD)
  73. #endif
  74. } { }
  75. #else
  76. _Base_bitset(unsigned long __val)
  77. : _M_w()
  78. { _M_w[0] = __val; }
  79. #endif
  80. static _GLIBCXX_CONSTEXPR size_t
  81. _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
  82. { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
  83. static _GLIBCXX_CONSTEXPR size_t
  84. _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
  85. { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
  86. static _GLIBCXX_CONSTEXPR size_t
  87. _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
  88. { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
  89. static _GLIBCXX_CONSTEXPR _WordT
  90. _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
  91. { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
  92. _WordT&
  93. _M_getword(size_t __pos) _GLIBCXX_NOEXCEPT
  94. { return _M_w[_S_whichword(__pos)]; }
  95. _GLIBCXX_CONSTEXPR _WordT
  96. _M_getword(size_t __pos) const _GLIBCXX_NOEXCEPT
  97. { return _M_w[_S_whichword(__pos)]; }
  98. #if __cplusplus >= 201103L
  99. const _WordT*
  100. _M_getdata() const noexcept
  101. { return _M_w; }
  102. #endif
  103. _WordT&
  104. _M_hiword() _GLIBCXX_NOEXCEPT
  105. { return _M_w[_Nw - 1]; }
  106. _GLIBCXX_CONSTEXPR _WordT
  107. _M_hiword() const _GLIBCXX_NOEXCEPT
  108. { return _M_w[_Nw - 1]; }
  109. void
  110. _M_do_and(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
  111. {
  112. for (size_t __i = 0; __i < _Nw; __i++)
  113. _M_w[__i] &= __x._M_w[__i];
  114. }
  115. void
  116. _M_do_or(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
  117. {
  118. for (size_t __i = 0; __i < _Nw; __i++)
  119. _M_w[__i] |= __x._M_w[__i];
  120. }
  121. void
  122. _M_do_xor(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
  123. {
  124. for (size_t __i = 0; __i < _Nw; __i++)
  125. _M_w[__i] ^= __x._M_w[__i];
  126. }
  127. void
  128. _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT;
  129. void
  130. _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT;
  131. void
  132. _M_do_flip() _GLIBCXX_NOEXCEPT
  133. {
  134. for (size_t __i = 0; __i < _Nw; __i++)
  135. _M_w[__i] = ~_M_w[__i];
  136. }
  137. void
  138. _M_do_set() _GLIBCXX_NOEXCEPT
  139. {
  140. for (size_t __i = 0; __i < _Nw; __i++)
  141. _M_w[__i] = ~static_cast<_WordT>(0);
  142. }
  143. void
  144. _M_do_reset() _GLIBCXX_NOEXCEPT
  145. { __builtin_memset(_M_w, 0, _Nw * sizeof(_WordT)); }
  146. bool
  147. _M_is_equal(const _Base_bitset<_Nw>& __x) const _GLIBCXX_NOEXCEPT
  148. {
  149. for (size_t __i = 0; __i < _Nw; ++__i)
  150. if (_M_w[__i] != __x._M_w[__i])
  151. return false;
  152. return true;
  153. }
  154. template<size_t _Nb>
  155. bool
  156. _M_are_all() const _GLIBCXX_NOEXCEPT
  157. {
  158. for (size_t __i = 0; __i < _Nw - 1; __i++)
  159. if (_M_w[__i] != ~static_cast<_WordT>(0))
  160. return false;
  161. return _M_hiword() == (~static_cast<_WordT>(0)
  162. >> (_Nw * _GLIBCXX_BITSET_BITS_PER_WORD
  163. - _Nb));
  164. }
  165. bool
  166. _M_is_any() const _GLIBCXX_NOEXCEPT
  167. {
  168. for (size_t __i = 0; __i < _Nw; __i++)
  169. if (_M_w[__i] != static_cast<_WordT>(0))
  170. return true;
  171. return false;
  172. }
  173. size_t
  174. _M_do_count() const _GLIBCXX_NOEXCEPT
  175. {
  176. size_t __result = 0;
  177. for (size_t __i = 0; __i < _Nw; __i++)
  178. __result += __builtin_popcountl(_M_w[__i]);
  179. return __result;
  180. }
  181. unsigned long
  182. _M_do_to_ulong() const;
  183. #if __cplusplus >= 201103L
  184. unsigned long long
  185. _M_do_to_ullong() const;
  186. #endif
  187. // find first "on" bit
  188. size_t
  189. _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT;
  190. // find the next "on" bit that follows "prev"
  191. size_t
  192. _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT;
  193. };
  194. // Definitions of non-inline functions from _Base_bitset.
  195. template<size_t _Nw>
  196. void
  197. _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
  198. {
  199. if (__builtin_expect(__shift != 0, 1))
  200. {
  201. const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
  202. const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
  203. if (__offset == 0)
  204. for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
  205. _M_w[__n] = _M_w[__n - __wshift];
  206. else
  207. {
  208. const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
  209. - __offset);
  210. for (size_t __n = _Nw - 1; __n > __wshift; --__n)
  211. _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
  212. | (_M_w[__n - __wshift - 1] >> __sub_offset));
  213. _M_w[__wshift] = _M_w[0] << __offset;
  214. }
  215. std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
  216. }
  217. }
  218. template<size_t _Nw>
  219. void
  220. _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
  221. {
  222. if (__builtin_expect(__shift != 0, 1))
  223. {
  224. const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
  225. const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
  226. const size_t __limit = _Nw - __wshift - 1;
  227. if (__offset == 0)
  228. for (size_t __n = 0; __n <= __limit; ++__n)
  229. _M_w[__n] = _M_w[__n + __wshift];
  230. else
  231. {
  232. const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
  233. - __offset);
  234. for (size_t __n = 0; __n < __limit; ++__n)
  235. _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
  236. | (_M_w[__n + __wshift + 1] << __sub_offset));
  237. _M_w[__limit] = _M_w[_Nw-1] >> __offset;
  238. }
  239. std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
  240. }
  241. }
  242. template<size_t _Nw>
  243. unsigned long
  244. _Base_bitset<_Nw>::_M_do_to_ulong() const
  245. {
  246. for (size_t __i = 1; __i < _Nw; ++__i)
  247. if (_M_w[__i])
  248. __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
  249. return _M_w[0];
  250. }
  251. #if __cplusplus >= 201103L
  252. template<size_t _Nw>
  253. unsigned long long
  254. _Base_bitset<_Nw>::_M_do_to_ullong() const
  255. {
  256. const bool __dw = sizeof(unsigned long long) > sizeof(unsigned long);
  257. for (size_t __i = 1 + __dw; __i < _Nw; ++__i)
  258. if (_M_w[__i])
  259. __throw_overflow_error(__N("_Base_bitset::_M_do_to_ullong"));
  260. if (__dw)
  261. return _M_w[0] + (static_cast<unsigned long long>(_M_w[1])
  262. << _GLIBCXX_BITSET_BITS_PER_WORD);
  263. return _M_w[0];
  264. }
  265. #endif
  266. template<size_t _Nw>
  267. size_t
  268. _Base_bitset<_Nw>::
  269. _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
  270. {
  271. for (size_t __i = 0; __i < _Nw; __i++)
  272. {
  273. _WordT __thisword = _M_w[__i];
  274. if (__thisword != static_cast<_WordT>(0))
  275. return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
  276. + __builtin_ctzl(__thisword));
  277. }
  278. // not found, so return an indication of failure.
  279. return __not_found;
  280. }
  281. template<size_t _Nw>
  282. size_t
  283. _Base_bitset<_Nw>::
  284. _M_do_find_next(size_t __prev, size_t __not_found) const _GLIBCXX_NOEXCEPT
  285. {
  286. // make bound inclusive
  287. ++__prev;
  288. // check out of bounds
  289. if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
  290. return __not_found;
  291. // search first word
  292. size_t __i = _S_whichword(__prev);
  293. _WordT __thisword = _M_w[__i];
  294. // mask off bits below bound
  295. __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
  296. if (__thisword != static_cast<_WordT>(0))
  297. return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
  298. + __builtin_ctzl(__thisword));
  299. // check subsequent words
  300. __i++;
  301. for (; __i < _Nw; __i++)
  302. {
  303. __thisword = _M_w[__i];
  304. if (__thisword != static_cast<_WordT>(0))
  305. return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
  306. + __builtin_ctzl(__thisword));
  307. }
  308. // not found, so return an indication of failure.
  309. return __not_found;
  310. } // end _M_do_find_next
  311. /**
  312. * Base class, specialization for a single word.
  313. *
  314. * See documentation for bitset.
  315. */
  316. template<>
  317. struct _Base_bitset<1>
  318. {
  319. typedef unsigned long _WordT;
  320. _WordT _M_w;
  321. _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
  322. : _M_w(0)
  323. { }
  324. #if __cplusplus >= 201103L
  325. constexpr _Base_bitset(unsigned long long __val) noexcept
  326. #else
  327. _Base_bitset(unsigned long __val)
  328. #endif
  329. : _M_w(__val)
  330. { }
  331. static _GLIBCXX_CONSTEXPR size_t
  332. _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
  333. { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
  334. static _GLIBCXX_CONSTEXPR size_t
  335. _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
  336. { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
  337. static _GLIBCXX_CONSTEXPR size_t
  338. _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
  339. { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
  340. static _GLIBCXX_CONSTEXPR _WordT
  341. _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
  342. { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
  343. _WordT&
  344. _M_getword(size_t) _GLIBCXX_NOEXCEPT
  345. { return _M_w; }
  346. _GLIBCXX_CONSTEXPR _WordT
  347. _M_getword(size_t) const _GLIBCXX_NOEXCEPT
  348. { return _M_w; }
  349. #if __cplusplus >= 201103L
  350. const _WordT*
  351. _M_getdata() const noexcept
  352. { return &_M_w; }
  353. #endif
  354. _WordT&
  355. _M_hiword() _GLIBCXX_NOEXCEPT
  356. { return _M_w; }
  357. _GLIBCXX_CONSTEXPR _WordT
  358. _M_hiword() const _GLIBCXX_NOEXCEPT
  359. { return _M_w; }
  360. void
  361. _M_do_and(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
  362. { _M_w &= __x._M_w; }
  363. void
  364. _M_do_or(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
  365. { _M_w |= __x._M_w; }
  366. void
  367. _M_do_xor(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
  368. { _M_w ^= __x._M_w; }
  369. void
  370. _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
  371. { _M_w <<= __shift; }
  372. void
  373. _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
  374. { _M_w >>= __shift; }
  375. void
  376. _M_do_flip() _GLIBCXX_NOEXCEPT
  377. { _M_w = ~_M_w; }
  378. void
  379. _M_do_set() _GLIBCXX_NOEXCEPT
  380. { _M_w = ~static_cast<_WordT>(0); }
  381. void
  382. _M_do_reset() _GLIBCXX_NOEXCEPT
  383. { _M_w = 0; }
  384. bool
  385. _M_is_equal(const _Base_bitset<1>& __x) const _GLIBCXX_NOEXCEPT
  386. { return _M_w == __x._M_w; }
  387. template<size_t _Nb>
  388. bool
  389. _M_are_all() const _GLIBCXX_NOEXCEPT
  390. { return _M_w == (~static_cast<_WordT>(0)
  391. >> (_GLIBCXX_BITSET_BITS_PER_WORD - _Nb)); }
  392. bool
  393. _M_is_any() const _GLIBCXX_NOEXCEPT
  394. { return _M_w != 0; }
  395. size_t
  396. _M_do_count() const _GLIBCXX_NOEXCEPT
  397. { return __builtin_popcountl(_M_w); }
  398. unsigned long
  399. _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
  400. { return _M_w; }
  401. #if __cplusplus >= 201103L
  402. unsigned long long
  403. _M_do_to_ullong() const noexcept
  404. { return _M_w; }
  405. #endif
  406. size_t
  407. _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
  408. {
  409. if (_M_w != 0)
  410. return __builtin_ctzl(_M_w);
  411. else
  412. return __not_found;
  413. }
  414. // find the next "on" bit that follows "prev"
  415. size_t
  416. _M_do_find_next(size_t __prev, size_t __not_found) const
  417. _GLIBCXX_NOEXCEPT
  418. {
  419. ++__prev;
  420. if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
  421. return __not_found;
  422. _WordT __x = _M_w >> __prev;
  423. if (__x != 0)
  424. return __builtin_ctzl(__x) + __prev;
  425. else
  426. return __not_found;
  427. }
  428. };
  429. /**
  430. * Base class, specialization for no storage (zero-length %bitset).
  431. *
  432. * See documentation for bitset.
  433. */
  434. template<>
  435. struct _Base_bitset<0>
  436. {
  437. typedef unsigned long _WordT;
  438. _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
  439. { }
  440. #if __cplusplus >= 201103L
  441. constexpr _Base_bitset(unsigned long long) noexcept
  442. #else
  443. _Base_bitset(unsigned long)
  444. #endif
  445. { }
  446. static _GLIBCXX_CONSTEXPR size_t
  447. _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
  448. { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
  449. static _GLIBCXX_CONSTEXPR size_t
  450. _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
  451. { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
  452. static _GLIBCXX_CONSTEXPR size_t
  453. _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
  454. { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
  455. static _GLIBCXX_CONSTEXPR _WordT
  456. _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
  457. { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
  458. // This would normally give access to the data. The bounds-checking
  459. // in the bitset class will prevent the user from getting this far,
  460. // but (1) it must still return an lvalue to compile, and (2) the
  461. // user might call _Unchecked_set directly, in which case this /needs/
  462. // to fail. Let's not penalize zero-length users unless they actually
  463. // make an unchecked call; all the memory ugliness is therefore
  464. // localized to this single should-never-get-this-far function.
  465. _WordT&
  466. _M_getword(size_t) _GLIBCXX_NOEXCEPT
  467. {
  468. __throw_out_of_range(__N("_Base_bitset::_M_getword"));
  469. return *new _WordT;
  470. }
  471. _GLIBCXX_CONSTEXPR _WordT
  472. _M_getword(size_t) const _GLIBCXX_NOEXCEPT
  473. { return 0; }
  474. _GLIBCXX_CONSTEXPR _WordT
  475. _M_hiword() const _GLIBCXX_NOEXCEPT
  476. { return 0; }
  477. void
  478. _M_do_and(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
  479. { }
  480. void
  481. _M_do_or(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
  482. { }
  483. void
  484. _M_do_xor(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
  485. { }
  486. void
  487. _M_do_left_shift(size_t) _GLIBCXX_NOEXCEPT
  488. { }
  489. void
  490. _M_do_right_shift(size_t) _GLIBCXX_NOEXCEPT
  491. { }
  492. void
  493. _M_do_flip() _GLIBCXX_NOEXCEPT
  494. { }
  495. void
  496. _M_do_set() _GLIBCXX_NOEXCEPT
  497. { }
  498. void
  499. _M_do_reset() _GLIBCXX_NOEXCEPT
  500. { }
  501. // Are all empty bitsets equal to each other? Are they equal to
  502. // themselves? How to compare a thing which has no state? What is
  503. // the sound of one zero-length bitset clapping?
  504. bool
  505. _M_is_equal(const _Base_bitset<0>&) const _GLIBCXX_NOEXCEPT
  506. { return true; }
  507. template<size_t _Nb>
  508. bool
  509. _M_are_all() const _GLIBCXX_NOEXCEPT
  510. { return true; }
  511. bool
  512. _M_is_any() const _GLIBCXX_NOEXCEPT
  513. { return false; }
  514. size_t
  515. _M_do_count() const _GLIBCXX_NOEXCEPT
  516. { return 0; }
  517. unsigned long
  518. _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
  519. { return 0; }
  520. #if __cplusplus >= 201103L
  521. unsigned long long
  522. _M_do_to_ullong() const noexcept
  523. { return 0; }
  524. #endif
  525. // Normally "not found" is the size, but that could also be
  526. // misinterpreted as an index in this corner case. Oh well.
  527. size_t
  528. _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT
  529. { return 0; }
  530. size_t
  531. _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT
  532. { return 0; }
  533. };
  534. // Helper class to zero out the unused high-order bits in the highest word.
  535. template<size_t _Extrabits>
  536. struct _Sanitize
  537. {
  538. typedef unsigned long _WordT;
  539. static void
  540. _S_do_sanitize(_WordT& __val) _GLIBCXX_NOEXCEPT
  541. { __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); }
  542. };
  543. template<>
  544. struct _Sanitize<0>
  545. {
  546. typedef unsigned long _WordT;
  547. static void
  548. _S_do_sanitize(_WordT) _GLIBCXX_NOEXCEPT { }
  549. };
  550. #if __cplusplus >= 201103L
  551. template<size_t _Nb, bool = (_Nb < _GLIBCXX_BITSET_BITS_PER_ULL)>
  552. struct _Sanitize_val
  553. {
  554. static constexpr unsigned long long
  555. _S_do_sanitize_val(unsigned long long __val)
  556. { return __val; }
  557. };
  558. template<size_t _Nb>
  559. struct _Sanitize_val<_Nb, true>
  560. {
  561. static constexpr unsigned long long
  562. _S_do_sanitize_val(unsigned long long __val)
  563. { return __val & ~((~static_cast<unsigned long long>(0)) << _Nb); }
  564. };
  565. #endif
  566. /**
  567. * @brief The %bitset class represents a @e fixed-size sequence of bits.
  568. * @ingroup utilities
  569. *
  570. * (Note that %bitset does @e not meet the formal requirements of a
  571. * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
  572. *
  573. * The template argument, @a Nb, may be any non-negative number,
  574. * specifying the number of bits (e.g., "0", "12", "1024*1024").
  575. *
  576. * In the general unoptimized case, storage is allocated in word-sized
  577. * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
  578. * words will be used for storage. B - Nb%B bits are unused. (They are
  579. * the high-order bits in the highest word.) It is a class invariant
  580. * that those unused bits are always zero.
  581. *
  582. * If you think of %bitset as <em>a simple array of bits</em>, be
  583. * aware that your mental picture is reversed: a %bitset behaves
  584. * the same way as bits in integers do, with the bit at index 0 in
  585. * the <em>least significant / right-hand</em> position, and the bit at
  586. * index Nb-1 in the <em>most significant / left-hand</em> position.
  587. * Thus, unlike other containers, a %bitset's index <em>counts from
  588. * right to left</em>, to put it very loosely.
  589. *
  590. * This behavior is preserved when translating to and from strings. For
  591. * example, the first line of the following program probably prints
  592. * <em>b(&apos;a&apos;) is 0001100001</em> on a modern ASCII system.
  593. *
  594. * @code
  595. * #include <bitset>
  596. * #include <iostream>
  597. * #include <sstream>
  598. *
  599. * using namespace std;
  600. *
  601. * int main()
  602. * {
  603. * long a = 'a';
  604. * bitset<10> b(a);
  605. *
  606. * cout << "b('a') is " << b << endl;
  607. *
  608. * ostringstream s;
  609. * s << b;
  610. * string str = s.str();
  611. * cout << "index 3 in the string is " << str[3] << " but\n"
  612. * << "index 3 in the bitset is " << b[3] << endl;
  613. * }
  614. * @endcode
  615. *
  616. * Also see:
  617. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_containers.html
  618. * for a description of extensions.
  619. *
  620. * Most of the actual code isn't contained in %bitset<> itself, but in the
  621. * base class _Base_bitset. The base class works with whole words, not with
  622. * individual bits. This allows us to specialize _Base_bitset for the
  623. * important special case where the %bitset is only a single word.
  624. *
  625. * Extra confusion can result due to the fact that the storage for
  626. * _Base_bitset @e is a regular array, and is indexed as such. This is
  627. * carefully encapsulated.
  628. */
  629. template<size_t _Nb>
  630. class bitset
  631. : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
  632. {
  633. private:
  634. typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
  635. typedef unsigned long _WordT;
  636. template<class _CharT, class _Traits, class _Alloc>
  637. void
  638. _M_check_initial_position(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
  639. size_t __position) const
  640. {
  641. if (__position > __s.size())
  642. __throw_out_of_range_fmt(__N("bitset::bitset: __position "
  643. "(which is %zu) > __s.size() "
  644. "(which is %zu)"),
  645. __position, __s.size());
  646. }
  647. void _M_check(size_t __position, const char *__s) const
  648. {
  649. if (__position >= _Nb)
  650. __throw_out_of_range_fmt(__N("%s: __position (which is %zu) "
  651. ">= _Nb (which is %zu)"),
  652. __s, __position, _Nb);
  653. }
  654. void
  655. _M_do_sanitize() _GLIBCXX_NOEXCEPT
  656. {
  657. typedef _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD> __sanitize_type;
  658. __sanitize_type::_S_do_sanitize(this->_M_hiword());
  659. }
  660. #if __cplusplus >= 201103L
  661. friend struct std::hash<bitset>;
  662. #endif
  663. public:
  664. /**
  665. * This encapsulates the concept of a single bit. An instance of this
  666. * class is a proxy for an actual bit; this way the individual bit
  667. * operations are done as faster word-size bitwise instructions.
  668. *
  669. * Most users will never need to use this class directly; conversions
  670. * to and from bool are automatic and should be transparent. Overloaded
  671. * operators help to preserve the illusion.
  672. *
  673. * (On a typical system, this <em>bit %reference</em> is 64
  674. * times the size of an actual bit. Ha.)
  675. */
  676. class reference
  677. {
  678. friend class bitset;
  679. _WordT* _M_wp;
  680. size_t _M_bpos;
  681. // left undefined
  682. reference();
  683. public:
  684. reference(bitset& __b, size_t __pos) _GLIBCXX_NOEXCEPT
  685. {
  686. _M_wp = &__b._M_getword(__pos);
  687. _M_bpos = _Base::_S_whichbit(__pos);
  688. }
  689. #if __cplusplus >= 201103L
  690. reference(const reference&) = default;
  691. #endif
  692. ~reference() _GLIBCXX_NOEXCEPT
  693. { }
  694. // For b[i] = __x;
  695. reference&
  696. operator=(bool __x) _GLIBCXX_NOEXCEPT
  697. {
  698. if (__x)
  699. *_M_wp |= _Base::_S_maskbit(_M_bpos);
  700. else
  701. *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
  702. return *this;
  703. }
  704. // For b[i] = b[__j];
  705. reference&
  706. operator=(const reference& __j) _GLIBCXX_NOEXCEPT
  707. {
  708. if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
  709. *_M_wp |= _Base::_S_maskbit(_M_bpos);
  710. else
  711. *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
  712. return *this;
  713. }
  714. // Flips the bit
  715. bool
  716. operator~() const _GLIBCXX_NOEXCEPT
  717. { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
  718. // For __x = b[i];
  719. operator bool() const _GLIBCXX_NOEXCEPT
  720. { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
  721. // For b[i].flip();
  722. reference&
  723. flip() _GLIBCXX_NOEXCEPT
  724. {
  725. *_M_wp ^= _Base::_S_maskbit(_M_bpos);
  726. return *this;
  727. }
  728. };
  729. friend class reference;
  730. // 23.3.5.1 constructors:
  731. /// All bits set to zero.
  732. _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
  733. { }
  734. /// Initial bits bitwise-copied from a single word (others set to zero).
  735. #if __cplusplus >= 201103L
  736. constexpr bitset(unsigned long long __val) noexcept
  737. : _Base(_Sanitize_val<_Nb>::_S_do_sanitize_val(__val)) { }
  738. #else
  739. bitset(unsigned long __val)
  740. : _Base(__val)
  741. { _M_do_sanitize(); }
  742. #endif
  743. /**
  744. * Use a subset of a string.
  745. * @param __s A string of @a 0 and @a 1 characters.
  746. * @param __position Index of the first character in @a __s to use;
  747. * defaults to zero.
  748. * @throw std::out_of_range If @a pos is bigger the size of @a __s.
  749. * @throw std::invalid_argument If a character appears in the string
  750. * which is neither @a 0 nor @a 1.
  751. */
  752. template<class _CharT, class _Traits, class _Alloc>
  753. explicit
  754. bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
  755. size_t __position = 0)
  756. : _Base()
  757. {
  758. _M_check_initial_position(__s, __position);
  759. _M_copy_from_string(__s, __position,
  760. std::basic_string<_CharT, _Traits, _Alloc>::npos,
  761. _CharT('0'), _CharT('1'));
  762. }
  763. /**
  764. * Use a subset of a string.
  765. * @param __s A string of @a 0 and @a 1 characters.
  766. * @param __position Index of the first character in @a __s to use.
  767. * @param __n The number of characters to copy.
  768. * @throw std::out_of_range If @a __position is bigger the size
  769. * of @a __s.
  770. * @throw std::invalid_argument If a character appears in the string
  771. * which is neither @a 0 nor @a 1.
  772. */
  773. template<class _CharT, class _Traits, class _Alloc>
  774. bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
  775. size_t __position, size_t __n)
  776. : _Base()
  777. {
  778. _M_check_initial_position(__s, __position);
  779. _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
  780. }
  781. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  782. // 396. what are characters zero and one.
  783. template<class _CharT, class _Traits, class _Alloc>
  784. bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
  785. size_t __position, size_t __n,
  786. _CharT __zero, _CharT __one = _CharT('1'))
  787. : _Base()
  788. {
  789. _M_check_initial_position(__s, __position);
  790. _M_copy_from_string(__s, __position, __n, __zero, __one);
  791. }
  792. #if __cplusplus >= 201103L
  793. /**
  794. * Construct from a character %array.
  795. * @param __str An %array of characters @a zero and @a one.
  796. * @param __n The number of characters to use.
  797. * @param __zero The character corresponding to the value 0.
  798. * @param __one The character corresponding to the value 1.
  799. * @throw std::invalid_argument If a character appears in the string
  800. * which is neither @a __zero nor @a __one.
  801. */
  802. template<typename _CharT>
  803. explicit
  804. bitset(const _CharT* __str,
  805. typename std::basic_string<_CharT>::size_type __n
  806. = std::basic_string<_CharT>::npos,
  807. _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
  808. : _Base()
  809. {
  810. if (!__str)
  811. __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)"));
  812. if (__n == std::basic_string<_CharT>::npos)
  813. __n = std::char_traits<_CharT>::length(__str);
  814. _M_copy_from_ptr<_CharT, std::char_traits<_CharT>>(__str, __n, 0,
  815. __n, __zero,
  816. __one);
  817. }
  818. #endif
  819. // 23.3.5.2 bitset operations:
  820. //@{
  821. /**
  822. * Operations on bitsets.
  823. * @param __rhs A same-sized bitset.
  824. *
  825. * These should be self-explanatory.
  826. */
  827. bitset<_Nb>&
  828. operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
  829. {
  830. this->_M_do_and(__rhs);
  831. return *this;
  832. }
  833. bitset<_Nb>&
  834. operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
  835. {
  836. this->_M_do_or(__rhs);
  837. return *this;
  838. }
  839. bitset<_Nb>&
  840. operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
  841. {
  842. this->_M_do_xor(__rhs);
  843. return *this;
  844. }
  845. //@}
  846. //@{
  847. /**
  848. * Operations on bitsets.
  849. * @param __position The number of places to shift.
  850. *
  851. * These should be self-explanatory.
  852. */
  853. bitset<_Nb>&
  854. operator<<=(size_t __position) _GLIBCXX_NOEXCEPT
  855. {
  856. if (__builtin_expect(__position < _Nb, 1))
  857. {
  858. this->_M_do_left_shift(__position);
  859. this->_M_do_sanitize();
  860. }
  861. else
  862. this->_M_do_reset();
  863. return *this;
  864. }
  865. bitset<_Nb>&
  866. operator>>=(size_t __position) _GLIBCXX_NOEXCEPT
  867. {
  868. if (__builtin_expect(__position < _Nb, 1))
  869. {
  870. this->_M_do_right_shift(__position);
  871. this->_M_do_sanitize();
  872. }
  873. else
  874. this->_M_do_reset();
  875. return *this;
  876. }
  877. //@}
  878. //@{
  879. /**
  880. * These versions of single-bit set, reset, flip, and test are
  881. * extensions from the SGI version. They do no range checking.
  882. * @ingroup SGIextensions
  883. */
  884. bitset<_Nb>&
  885. _Unchecked_set(size_t __pos) _GLIBCXX_NOEXCEPT
  886. {
  887. this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
  888. return *this;
  889. }
  890. bitset<_Nb>&
  891. _Unchecked_set(size_t __pos, int __val) _GLIBCXX_NOEXCEPT
  892. {
  893. if (__val)
  894. this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
  895. else
  896. this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
  897. return *this;
  898. }
  899. bitset<_Nb>&
  900. _Unchecked_reset(size_t __pos) _GLIBCXX_NOEXCEPT
  901. {
  902. this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
  903. return *this;
  904. }
  905. bitset<_Nb>&
  906. _Unchecked_flip(size_t __pos) _GLIBCXX_NOEXCEPT
  907. {
  908. this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
  909. return *this;
  910. }
  911. _GLIBCXX_CONSTEXPR bool
  912. _Unchecked_test(size_t __pos) const _GLIBCXX_NOEXCEPT
  913. { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
  914. != static_cast<_WordT>(0)); }
  915. //@}
  916. // Set, reset, and flip.
  917. /**
  918. * @brief Sets every bit to true.
  919. */
  920. bitset<_Nb>&
  921. set() _GLIBCXX_NOEXCEPT
  922. {
  923. this->_M_do_set();
  924. this->_M_do_sanitize();
  925. return *this;
  926. }
  927. /**
  928. * @brief Sets a given bit to a particular value.
  929. * @param __position The index of the bit.
  930. * @param __val Either true or false, defaults to true.
  931. * @throw std::out_of_range If @a pos is bigger the size of the %set.
  932. */
  933. bitset<_Nb>&
  934. set(size_t __position, bool __val = true)
  935. {
  936. this->_M_check(__position, __N("bitset::set"));
  937. return _Unchecked_set(__position, __val);
  938. }
  939. /**
  940. * @brief Sets every bit to false.
  941. */
  942. bitset<_Nb>&
  943. reset() _GLIBCXX_NOEXCEPT
  944. {
  945. this->_M_do_reset();
  946. return *this;
  947. }
  948. /**
  949. * @brief Sets a given bit to false.
  950. * @param __position The index of the bit.
  951. * @throw std::out_of_range If @a pos is bigger the size of the %set.
  952. *
  953. * Same as writing @c set(pos,false).
  954. */
  955. bitset<_Nb>&
  956. reset(size_t __position)
  957. {
  958. this->_M_check(__position, __N("bitset::reset"));
  959. return _Unchecked_reset(__position);
  960. }
  961. /**
  962. * @brief Toggles every bit to its opposite value.
  963. */
  964. bitset<_Nb>&
  965. flip() _GLIBCXX_NOEXCEPT
  966. {
  967. this->_M_do_flip();
  968. this->_M_do_sanitize();
  969. return *this;
  970. }
  971. /**
  972. * @brief Toggles a given bit to its opposite value.
  973. * @param __position The index of the bit.
  974. * @throw std::out_of_range If @a pos is bigger the size of the %set.
  975. */
  976. bitset<_Nb>&
  977. flip(size_t __position)
  978. {
  979. this->_M_check(__position, __N("bitset::flip"));
  980. return _Unchecked_flip(__position);
  981. }
  982. /// See the no-argument flip().
  983. bitset<_Nb>
  984. operator~() const _GLIBCXX_NOEXCEPT
  985. { return bitset<_Nb>(*this).flip(); }
  986. //@{
  987. /**
  988. * @brief Array-indexing support.
  989. * @param __position Index into the %bitset.
  990. * @return A bool for a <em>const %bitset</em>. For non-const
  991. * bitsets, an instance of the reference proxy class.
  992. * @note These operators do no range checking and throw no exceptions,
  993. * as required by DR 11 to the standard.
  994. *
  995. * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
  996. * resolves DR 11 (items 1 and 2), but does not do the range-checking
  997. * required by that DR's resolution. -pme
  998. * The DR has since been changed: range-checking is a precondition
  999. * (users' responsibility), and these functions must not throw. -pme
  1000. */
  1001. reference
  1002. operator[](size_t __position)
  1003. { return reference(*this, __position); }
  1004. _GLIBCXX_CONSTEXPR bool
  1005. operator[](size_t __position) const
  1006. { return _Unchecked_test(__position); }
  1007. //@}
  1008. /**
  1009. * @brief Returns a numerical interpretation of the %bitset.
  1010. * @return The integral equivalent of the bits.
  1011. * @throw std::overflow_error If there are too many bits to be
  1012. * represented in an @c unsigned @c long.
  1013. */
  1014. unsigned long
  1015. to_ulong() const
  1016. { return this->_M_do_to_ulong(); }
  1017. #if __cplusplus >= 201103L
  1018. unsigned long long
  1019. to_ullong() const
  1020. { return this->_M_do_to_ullong(); }
  1021. #endif
  1022. /**
  1023. * @brief Returns a character interpretation of the %bitset.
  1024. * @return The string equivalent of the bits.
  1025. *
  1026. * Note the ordering of the bits: decreasing character positions
  1027. * correspond to increasing bit positions (see the main class notes for
  1028. * an example).
  1029. */
  1030. template<class _CharT, class _Traits, class _Alloc>
  1031. std::basic_string<_CharT, _Traits, _Alloc>
  1032. to_string() const
  1033. {
  1034. std::basic_string<_CharT, _Traits, _Alloc> __result;
  1035. _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
  1036. return __result;
  1037. }
  1038. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1039. // 396. what are characters zero and one.
  1040. template<class _CharT, class _Traits, class _Alloc>
  1041. std::basic_string<_CharT, _Traits, _Alloc>
  1042. to_string(_CharT __zero, _CharT __one = _CharT('1')) const
  1043. {
  1044. std::basic_string<_CharT, _Traits, _Alloc> __result;
  1045. _M_copy_to_string(__result, __zero, __one);
  1046. return __result;
  1047. }
  1048. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1049. // 434. bitset::to_string() hard to use.
  1050. template<class _CharT, class _Traits>
  1051. std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
  1052. to_string() const
  1053. { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
  1054. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1055. // 853. to_string needs updating with zero and one.
  1056. template<class _CharT, class _Traits>
  1057. std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
  1058. to_string(_CharT __zero, _CharT __one = _CharT('1')) const
  1059. { return to_string<_CharT, _Traits,
  1060. std::allocator<_CharT> >(__zero, __one); }
  1061. template<class _CharT>
  1062. std::basic_string<_CharT, std::char_traits<_CharT>,
  1063. std::allocator<_CharT> >
  1064. to_string() const
  1065. {
  1066. return to_string<_CharT, std::char_traits<_CharT>,
  1067. std::allocator<_CharT> >();
  1068. }
  1069. template<class _CharT>
  1070. std::basic_string<_CharT, std::char_traits<_CharT>,
  1071. std::allocator<_CharT> >
  1072. to_string(_CharT __zero, _CharT __one = _CharT('1')) const
  1073. {
  1074. return to_string<_CharT, std::char_traits<_CharT>,
  1075. std::allocator<_CharT> >(__zero, __one);
  1076. }
  1077. std::basic_string<char, std::char_traits<char>, std::allocator<char> >
  1078. to_string() const
  1079. {
  1080. return to_string<char, std::char_traits<char>,
  1081. std::allocator<char> >();
  1082. }
  1083. std::basic_string<char, std::char_traits<char>, std::allocator<char> >
  1084. to_string(char __zero, char __one = '1') const
  1085. {
  1086. return to_string<char, std::char_traits<char>,
  1087. std::allocator<char> >(__zero, __one);
  1088. }
  1089. // Helper functions for string operations.
  1090. template<class _CharT, class _Traits>
  1091. void
  1092. _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
  1093. _CharT, _CharT);
  1094. template<class _CharT, class _Traits, class _Alloc>
  1095. void
  1096. _M_copy_from_string(const std::basic_string<_CharT,
  1097. _Traits, _Alloc>& __s, size_t __pos, size_t __n,
  1098. _CharT __zero, _CharT __one)
  1099. { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
  1100. __zero, __one); }
  1101. template<class _CharT, class _Traits, class _Alloc>
  1102. void
  1103. _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&,
  1104. _CharT, _CharT) const;
  1105. // NB: Backward compat.
  1106. template<class _CharT, class _Traits, class _Alloc>
  1107. void
  1108. _M_copy_from_string(const std::basic_string<_CharT,
  1109. _Traits, _Alloc>& __s, size_t __pos, size_t __n)
  1110. { _M_copy_from_string(__s, __pos, __n, _CharT('0'), _CharT('1')); }
  1111. template<class _CharT, class _Traits, class _Alloc>
  1112. void
  1113. _M_copy_to_string(std::basic_string<_CharT, _Traits,_Alloc>& __s) const
  1114. { _M_copy_to_string(__s, _CharT('0'), _CharT('1')); }
  1115. /// Returns the number of bits which are set.
  1116. size_t
  1117. count() const _GLIBCXX_NOEXCEPT
  1118. { return this->_M_do_count(); }
  1119. /// Returns the total number of bits.
  1120. _GLIBCXX_CONSTEXPR size_t
  1121. size() const _GLIBCXX_NOEXCEPT
  1122. { return _Nb; }
  1123. //@{
  1124. /// These comparisons for equality/inequality are, well, @e bitwise.
  1125. bool
  1126. operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
  1127. { return this->_M_is_equal(__rhs); }
  1128. #if __cpp_impl_three_way_comparison < 201907L
  1129. bool
  1130. operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
  1131. { return !this->_M_is_equal(__rhs); }
  1132. #endif
  1133. //@}
  1134. /**
  1135. * @brief Tests the value of a bit.
  1136. * @param __position The index of a bit.
  1137. * @return The value at @a pos.
  1138. * @throw std::out_of_range If @a pos is bigger the size of the %set.
  1139. */
  1140. bool
  1141. test(size_t __position) const
  1142. {
  1143. this->_M_check(__position, __N("bitset::test"));
  1144. return _Unchecked_test(__position);
  1145. }
  1146. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1147. // DR 693. std::bitset::all() missing.
  1148. /**
  1149. * @brief Tests whether all the bits are on.
  1150. * @return True if all the bits are set.
  1151. */
  1152. bool
  1153. all() const _GLIBCXX_NOEXCEPT
  1154. { return this->template _M_are_all<_Nb>(); }
  1155. /**
  1156. * @brief Tests whether any of the bits are on.
  1157. * @return True if at least one bit is set.
  1158. */
  1159. bool
  1160. any() const _GLIBCXX_NOEXCEPT
  1161. { return this->_M_is_any(); }
  1162. /**
  1163. * @brief Tests whether any of the bits are on.
  1164. * @return True if none of the bits are set.
  1165. */
  1166. bool
  1167. none() const _GLIBCXX_NOEXCEPT
  1168. { return !this->_M_is_any(); }
  1169. //@{
  1170. /// Self-explanatory.
  1171. bitset<_Nb>
  1172. operator<<(size_t __position) const _GLIBCXX_NOEXCEPT
  1173. { return bitset<_Nb>(*this) <<= __position; }
  1174. bitset<_Nb>
  1175. operator>>(size_t __position) const _GLIBCXX_NOEXCEPT
  1176. { return bitset<_Nb>(*this) >>= __position; }
  1177. //@}
  1178. /**
  1179. * @brief Finds the index of the first "on" bit.
  1180. * @return The index of the first bit set, or size() if not found.
  1181. * @ingroup SGIextensions
  1182. * @sa _Find_next
  1183. */
  1184. size_t
  1185. _Find_first() const _GLIBCXX_NOEXCEPT
  1186. { return this->_M_do_find_first(_Nb); }
  1187. /**
  1188. * @brief Finds the index of the next "on" bit after prev.
  1189. * @return The index of the next bit set, or size() if not found.
  1190. * @param __prev Where to start searching.
  1191. * @ingroup SGIextensions
  1192. * @sa _Find_first
  1193. */
  1194. size_t
  1195. _Find_next(size_t __prev) const _GLIBCXX_NOEXCEPT
  1196. { return this->_M_do_find_next(__prev, _Nb); }
  1197. };
  1198. // Definitions of non-inline member functions.
  1199. template<size_t _Nb>
  1200. template<class _CharT, class _Traits>
  1201. void
  1202. bitset<_Nb>::
  1203. _M_copy_from_ptr(const _CharT* __s, size_t __len,
  1204. size_t __pos, size_t __n, _CharT __zero, _CharT __one)
  1205. {
  1206. reset();
  1207. const size_t __nbits = std::min(_Nb, std::min(__n, size_t(__len - __pos)));
  1208. for (size_t __i = __nbits; __i > 0; --__i)
  1209. {
  1210. const _CharT __c = __s[__pos + __nbits - __i];
  1211. if (_Traits::eq(__c, __zero))
  1212. ;
  1213. else if (_Traits::eq(__c, __one))
  1214. _Unchecked_set(__i - 1);
  1215. else
  1216. __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
  1217. }
  1218. }
  1219. template<size_t _Nb>
  1220. template<class _CharT, class _Traits, class _Alloc>
  1221. void
  1222. bitset<_Nb>::
  1223. _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
  1224. _CharT __zero, _CharT __one) const
  1225. {
  1226. __s.assign(_Nb, __zero);
  1227. for (size_t __i = _Nb; __i > 0; --__i)
  1228. if (_Unchecked_test(__i - 1))
  1229. _Traits::assign(__s[_Nb - __i], __one);
  1230. }
  1231. // 23.3.5.3 bitset operations:
  1232. //@{
  1233. /**
  1234. * @brief Global bitwise operations on bitsets.
  1235. * @param __x A bitset.
  1236. * @param __y A bitset of the same size as @a __x.
  1237. * @return A new bitset.
  1238. *
  1239. * These should be self-explanatory.
  1240. */
  1241. template<size_t _Nb>
  1242. inline bitset<_Nb>
  1243. operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
  1244. {
  1245. bitset<_Nb> __result(__x);
  1246. __result &= __y;
  1247. return __result;
  1248. }
  1249. template<size_t _Nb>
  1250. inline bitset<_Nb>
  1251. operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
  1252. {
  1253. bitset<_Nb> __result(__x);
  1254. __result |= __y;
  1255. return __result;
  1256. }
  1257. template <size_t _Nb>
  1258. inline bitset<_Nb>
  1259. operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
  1260. {
  1261. bitset<_Nb> __result(__x);
  1262. __result ^= __y;
  1263. return __result;
  1264. }
  1265. //@}
  1266. //@{
  1267. /**
  1268. * @brief Global I/O operators for bitsets.
  1269. *
  1270. * Direct I/O between streams and bitsets is supported. Output is
  1271. * straightforward. Input will skip whitespace, only accept @a 0 and @a 1
  1272. * characters, and will only extract as many digits as the %bitset will
  1273. * hold.
  1274. */
  1275. template<class _CharT, class _Traits, size_t _Nb>
  1276. std::basic_istream<_CharT, _Traits>&
  1277. operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
  1278. {
  1279. typedef typename _Traits::char_type char_type;
  1280. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  1281. typedef typename __istream_type::ios_base __ios_base;
  1282. std::basic_string<_CharT, _Traits> __tmp;
  1283. __tmp.reserve(_Nb);
  1284. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1285. // 303. Bitset input operator underspecified
  1286. const char_type __zero = __is.widen('0');
  1287. const char_type __one = __is.widen('1');
  1288. typename __ios_base::iostate __state = __ios_base::goodbit;
  1289. typename __istream_type::sentry __sentry(__is);
  1290. if (__sentry)
  1291. {
  1292. __try
  1293. {
  1294. for (size_t __i = _Nb; __i > 0; --__i)
  1295. {
  1296. static typename _Traits::int_type __eof = _Traits::eof();
  1297. typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
  1298. if (_Traits::eq_int_type(__c1, __eof))
  1299. {
  1300. __state |= __ios_base::eofbit;
  1301. break;
  1302. }
  1303. else
  1304. {
  1305. const char_type __c2 = _Traits::to_char_type(__c1);
  1306. if (_Traits::eq(__c2, __zero))
  1307. __tmp.push_back(__zero);
  1308. else if (_Traits::eq(__c2, __one))
  1309. __tmp.push_back(__one);
  1310. else if (_Traits::
  1311. eq_int_type(__is.rdbuf()->sputbackc(__c2),
  1312. __eof))
  1313. {
  1314. __state |= __ios_base::failbit;
  1315. break;
  1316. }
  1317. }
  1318. }
  1319. }
  1320. __catch(__cxxabiv1::__forced_unwind&)
  1321. {
  1322. __is._M_setstate(__ios_base::badbit);
  1323. __throw_exception_again;
  1324. }
  1325. __catch(...)
  1326. { __is._M_setstate(__ios_base::badbit); }
  1327. }
  1328. if (__tmp.empty() && _Nb)
  1329. __state |= __ios_base::failbit;
  1330. else
  1331. __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb,
  1332. __zero, __one);
  1333. if (__state)
  1334. __is.setstate(__state);
  1335. return __is;
  1336. }
  1337. template <class _CharT, class _Traits, size_t _Nb>
  1338. std::basic_ostream<_CharT, _Traits>&
  1339. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1340. const bitset<_Nb>& __x)
  1341. {
  1342. std::basic_string<_CharT, _Traits> __tmp;
  1343. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1344. // 396. what are characters zero and one.
  1345. const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
  1346. __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
  1347. return __os << __tmp;
  1348. }
  1349. //@}
  1350. _GLIBCXX_END_NAMESPACE_CONTAINER
  1351. } // namespace std
  1352. #undef _GLIBCXX_BITSET_WORDS
  1353. #undef _GLIBCXX_BITSET_BITS_PER_WORD
  1354. #undef _GLIBCXX_BITSET_BITS_PER_ULL
  1355. #if __cplusplus >= 201103L
  1356. namespace std _GLIBCXX_VISIBILITY(default)
  1357. {
  1358. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  1359. // DR 1182.
  1360. /// std::hash specialization for bitset.
  1361. template<size_t _Nb>
  1362. struct hash<_GLIBCXX_STD_C::bitset<_Nb>>
  1363. : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<_Nb>>
  1364. {
  1365. size_t
  1366. operator()(const _GLIBCXX_STD_C::bitset<_Nb>& __b) const noexcept
  1367. {
  1368. const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
  1369. return std::_Hash_impl::hash(__b._M_getdata(), __clength);
  1370. }
  1371. };
  1372. template<>
  1373. struct hash<_GLIBCXX_STD_C::bitset<0>>
  1374. : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<0>>
  1375. {
  1376. size_t
  1377. operator()(const _GLIBCXX_STD_C::bitset<0>&) const noexcept
  1378. { return 0; }
  1379. };
  1380. _GLIBCXX_END_NAMESPACE_VERSION
  1381. } // namespace
  1382. #endif // C++11
  1383. #ifdef _GLIBCXX_DEBUG
  1384. # include <debug/bitset>
  1385. #endif
  1386. #endif /* _GLIBCXX_BITSET */