PageRenderTime 52ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/freebsd/contrib/libstdc++/include/std/std_bitset.h

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