Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

1221 lines
34KB

  1. // TR2 <dynamic_bitset> -*- C++ -*-
  2. // Copyright (C) 2009-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. /** @file tr2/dynamic_bitset
  21. * This is a TR2 C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_TR2_DYNAMIC_BITSET
  24. #define _GLIBCXX_TR2_DYNAMIC_BITSET 1
  25. #pragma GCC system_header
  26. #include <limits>
  27. #include <vector>
  28. #include <string>
  29. #include <istream>
  30. #include <bits/functexcept.h>
  31. #include <bits/stl_algo.h> // For fill
  32. #include <bits/cxxabi_forced.h>
  33. namespace std _GLIBCXX_VISIBILITY(default)
  34. {
  35. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  36. namespace tr2
  37. {
  38. /**
  39. * @defgroup dynamic_bitset Dynamic Bitset.
  40. * @ingroup extensions
  41. *
  42. * @{
  43. */
  44. /**
  45. * Base class, general case.
  46. *
  47. * See documentation for dynamic_bitset.
  48. */
  49. template<typename _WordT = unsigned long long,
  50. typename _Alloc = std::allocator<_WordT>>
  51. struct __dynamic_bitset_base
  52. {
  53. static_assert(std::is_unsigned<_WordT>::value, "template argument "
  54. "_WordT not an unsigned integral type");
  55. typedef _WordT block_type;
  56. typedef _Alloc allocator_type;
  57. typedef size_t size_type;
  58. static const size_type _S_bits_per_block = __CHAR_BIT__ * sizeof(block_type);
  59. static const size_type npos = static_cast<size_type>(-1);
  60. /// 0 is the least significant word.
  61. std::vector<block_type, allocator_type> _M_w;
  62. explicit
  63. __dynamic_bitset_base(const allocator_type& __alloc)
  64. : _M_w(__alloc)
  65. { }
  66. __dynamic_bitset_base() = default;
  67. __dynamic_bitset_base(const __dynamic_bitset_base&) = default;
  68. __dynamic_bitset_base(__dynamic_bitset_base&& __b) = default;
  69. __dynamic_bitset_base& operator=(const __dynamic_bitset_base&) = default;
  70. __dynamic_bitset_base& operator=(__dynamic_bitset_base&&) = default;
  71. ~__dynamic_bitset_base() = default;
  72. explicit
  73. __dynamic_bitset_base(size_type __nbits, unsigned long long __val = 0ULL,
  74. const allocator_type& __alloc = allocator_type())
  75. : _M_w(__nbits / _S_bits_per_block + (__nbits % _S_bits_per_block > 0),
  76. block_type(0), __alloc)
  77. {
  78. if (__nbits < std::numeric_limits<decltype(__val)>::digits)
  79. __val &= ~(-1ULL << __nbits);
  80. if (__val == 0)
  81. return;
  82. if _GLIBCXX17_CONSTEXPR (sizeof(__val) == sizeof(block_type))
  83. _M_w[0] = __val;
  84. else
  85. {
  86. const size_t __n
  87. = std::min(_M_w.size(), sizeof(__val) / sizeof(block_type));
  88. for (size_t __i = 0; __val && __i < __n; ++__i)
  89. {
  90. _M_w[__i] = static_cast<block_type>(__val);
  91. __val >>= _S_bits_per_block;
  92. }
  93. }
  94. }
  95. void
  96. _M_swap(__dynamic_bitset_base& __b) noexcept
  97. { this->_M_w.swap(__b._M_w); }
  98. void
  99. _M_clear() noexcept
  100. { this->_M_w.clear(); }
  101. void
  102. _M_resize(size_t __nbits, bool __value)
  103. {
  104. size_t __sz = __nbits / _S_bits_per_block;
  105. if (__nbits % _S_bits_per_block > 0)
  106. ++__sz;
  107. if (__sz != this->_M_w.size())
  108. {
  109. block_type __val = 0;
  110. if (__value)
  111. __val = std::numeric_limits<block_type>::max();
  112. this->_M_w.resize(__sz, __val);
  113. }
  114. }
  115. allocator_type
  116. _M_get_allocator() const noexcept
  117. { return this->_M_w.get_allocator(); }
  118. static size_type
  119. _S_whichword(size_type __pos) noexcept
  120. { return __pos / _S_bits_per_block; }
  121. static size_type
  122. _S_whichbyte(size_type __pos) noexcept
  123. { return (__pos % _S_bits_per_block) / __CHAR_BIT__; }
  124. static size_type
  125. _S_whichbit(size_type __pos) noexcept
  126. { return __pos % _S_bits_per_block; }
  127. static block_type
  128. _S_maskbit(size_type __pos) noexcept
  129. { return (static_cast<block_type>(1)) << _S_whichbit(__pos); }
  130. block_type&
  131. _M_getword(size_type __pos) noexcept
  132. { return this->_M_w[_S_whichword(__pos)]; }
  133. block_type
  134. _M_getword(size_type __pos) const noexcept
  135. { return this->_M_w[_S_whichword(__pos)]; }
  136. block_type&
  137. _M_hiword() noexcept
  138. { return this->_M_w[_M_w.size() - 1]; }
  139. block_type
  140. _M_hiword() const noexcept
  141. { return this->_M_w[_M_w.size() - 1]; }
  142. void
  143. _M_do_and(const __dynamic_bitset_base& __x) noexcept
  144. {
  145. if (__x._M_w.size() == this->_M_w.size())
  146. for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
  147. this->_M_w[__i] &= __x._M_w[__i];
  148. else
  149. return;
  150. }
  151. void
  152. _M_do_or(const __dynamic_bitset_base& __x) noexcept
  153. {
  154. if (__x._M_w.size() == this->_M_w.size())
  155. for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
  156. this->_M_w[__i] |= __x._M_w[__i];
  157. else
  158. return;
  159. }
  160. void
  161. _M_do_xor(const __dynamic_bitset_base& __x) noexcept
  162. {
  163. if (__x._M_w.size() == this->_M_w.size())
  164. for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
  165. this->_M_w[__i] ^= __x._M_w[__i];
  166. else
  167. return;
  168. }
  169. void
  170. _M_do_dif(const __dynamic_bitset_base& __x) noexcept
  171. {
  172. if (__x._M_w.size() == this->_M_w.size())
  173. for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
  174. this->_M_w[__i] &= ~__x._M_w[__i];
  175. else
  176. return;
  177. }
  178. void
  179. _M_do_left_shift(size_t __shift);
  180. void
  181. _M_do_right_shift(size_t __shift);
  182. void
  183. _M_do_flip() noexcept
  184. {
  185. for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
  186. this->_M_w[__i] = ~this->_M_w[__i];
  187. }
  188. void
  189. _M_do_set() noexcept
  190. {
  191. for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
  192. this->_M_w[__i] = static_cast<block_type>(-1);
  193. }
  194. void
  195. _M_do_reset() noexcept
  196. {
  197. std::fill(_M_w.begin(), _M_w.end(), static_cast<block_type>(0));
  198. }
  199. bool
  200. _M_is_equal(const __dynamic_bitset_base& __x) const noexcept
  201. {
  202. if (__x._M_w.size() == this->_M_w.size())
  203. {
  204. for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
  205. if (this->_M_w[__i] != __x._M_w[__i])
  206. return false;
  207. return true;
  208. }
  209. else
  210. return false;
  211. }
  212. bool
  213. _M_is_less(const __dynamic_bitset_base& __x) const noexcept
  214. {
  215. if (__x._M_w.size() == this->_M_w.size())
  216. {
  217. for (size_t __i = this->_M_w.size(); __i > 0; --__i)
  218. {
  219. if (this->_M_w[__i-1] < __x._M_w[__i-1])
  220. return true;
  221. else if (this->_M_w[__i-1] > __x._M_w[__i-1])
  222. return false;
  223. }
  224. return false;
  225. }
  226. else
  227. return false;
  228. }
  229. size_t
  230. _M_are_all_aux() const noexcept
  231. {
  232. for (size_t __i = 0; __i < this->_M_w.size() - 1; ++__i)
  233. if (_M_w[__i] != static_cast<block_type>(-1))
  234. return 0;
  235. return ((this->_M_w.size() - 1) * _S_bits_per_block
  236. + __builtin_popcountll(this->_M_hiword()));
  237. }
  238. bool
  239. _M_is_any() const noexcept
  240. {
  241. for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
  242. if (this->_M_w[__i] != static_cast<block_type>(0))
  243. return true;
  244. return false;
  245. }
  246. bool
  247. _M_is_subset_of(const __dynamic_bitset_base& __b) noexcept
  248. {
  249. if (__b._M_w.size() == this->_M_w.size())
  250. {
  251. for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
  252. if (this->_M_w[__i] != (this->_M_w[__i] | __b._M_w[__i]))
  253. return false;
  254. return true;
  255. }
  256. else
  257. return false;
  258. }
  259. bool
  260. _M_is_proper_subset_of(const __dynamic_bitset_base& __b) const noexcept
  261. {
  262. if (this->is_subset_of(__b))
  263. {
  264. if (*this == __b)
  265. return false;
  266. else
  267. return true;
  268. }
  269. else
  270. return false;
  271. }
  272. size_t
  273. _M_do_count() const noexcept
  274. {
  275. size_t __result = 0;
  276. for (size_t __i = 0; __i < this->_M_w.size(); ++__i)
  277. __result += __builtin_popcountll(this->_M_w[__i]);
  278. return __result;
  279. }
  280. size_type
  281. _M_size() const noexcept
  282. { return this->_M_w.size(); }
  283. unsigned long
  284. _M_do_to_ulong() const;
  285. unsigned long long
  286. _M_do_to_ullong() const;
  287. // find first "on" bit
  288. size_type
  289. _M_do_find_first(size_t __not_found) const;
  290. // find the next "on" bit that follows "prev"
  291. size_type
  292. _M_do_find_next(size_t __prev, size_t __not_found) const;
  293. // do append of block
  294. void
  295. _M_do_append_block(block_type __block, size_type __pos)
  296. {
  297. size_t __offset = __pos % _S_bits_per_block;
  298. if (__offset == 0)
  299. this->_M_w.push_back(__block);
  300. else
  301. {
  302. this->_M_hiword() |= (__block << __offset);
  303. this->_M_w.push_back(__block >> (_S_bits_per_block - __offset));
  304. }
  305. }
  306. };
  307. /**
  308. * @brief The %dynamic_bitset class represents a sequence of bits.
  309. *
  310. * See N2050,
  311. * Proposal to Add a Dynamically Sizeable Bitset to the Standard Library.
  312. * http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2050.pdf
  313. *
  314. * In the general unoptimized case, storage is allocated in
  315. * word-sized blocks. Let B be the number of bits in a word, then
  316. * (Nb+(B-1))/B words will be used for storage. B - Nb%B bits are
  317. * unused. (They are the high-order bits in the highest word.) It
  318. * is a class invariant that those unused bits are always zero.
  319. *
  320. * If you think of %dynamic_bitset as "a simple array of bits," be
  321. * aware that your mental picture is reversed: a %dynamic_bitset
  322. * behaves the same way as bits in integers do, with the bit at
  323. * index 0 in the "least significant / right-hand" position, and
  324. * the bit at index Nb-1 in the "most significant / left-hand"
  325. * position. Thus, unlike other containers, a %dynamic_bitset's
  326. * index "counts from right to left," to put it very loosely.
  327. *
  328. * This behavior is preserved when translating to and from strings.
  329. * For example, the first line of the following program probably
  330. * prints "b('a') is 0001100001" on a modern ASCII system.
  331. *
  332. * @code
  333. * #include <dynamic_bitset>
  334. * #include <iostream>
  335. * #include <sstream>
  336. *
  337. * using namespace std;
  338. *
  339. * int main()
  340. * {
  341. * long a = 'a';
  342. * dynamic_bitset<> b(a);
  343. *
  344. * cout << "b('a') is " << b << endl;
  345. *
  346. * ostringstream s;
  347. * s << b;
  348. * string str = s.str();
  349. * cout << "index 3 in the string is " << str[3] << " but\n"
  350. * << "index 3 in the bitset is " << b[3] << endl;
  351. * }
  352. * @endcode
  353. *
  354. * Most of the actual code isn't contained in %dynamic_bitset<>
  355. * itself, but in the base class __dynamic_bitset_base. The base
  356. * class works with whole words, not with individual bits. This
  357. * allows us to specialize __dynamic_bitset_base for the important
  358. * special case where the %dynamic_bitset is only a single word.
  359. *
  360. * Extra confusion can result due to the fact that the storage for
  361. * __dynamic_bitset_base @e is a vector, and is indexed as such. This is
  362. * carefully encapsulated.
  363. */
  364. template<typename _WordT = unsigned long long,
  365. typename _Alloc = std::allocator<_WordT>>
  366. class dynamic_bitset
  367. : private __dynamic_bitset_base<_WordT, _Alloc>
  368. {
  369. static_assert(std::is_unsigned<_WordT>::value, "template argument "
  370. "_WordT not an unsigned integral type");
  371. public:
  372. typedef __dynamic_bitset_base<_WordT, _Alloc> _Base;
  373. typedef _WordT block_type;
  374. typedef _Alloc allocator_type;
  375. typedef size_t size_type;
  376. static const size_type bits_per_block = __CHAR_BIT__ * sizeof(block_type);
  377. // Use this: constexpr size_type std::numeric_limits<size_type>::max().
  378. static const size_type npos = static_cast<size_type>(-1);
  379. private:
  380. // Clear the unused bits in the uppermost word.
  381. void
  382. _M_do_sanitize()
  383. {
  384. size_type __shift = this->_M_Nb % bits_per_block;
  385. if (__shift > 0)
  386. this->_M_hiword() &= block_type(~(block_type(-1) << __shift));
  387. }
  388. // Set the unused bits in the uppermost word.
  389. void
  390. _M_do_fill()
  391. {
  392. size_type __shift = this->_M_Nb % bits_per_block;
  393. if (__shift > 0)
  394. this->_M_hiword() |= block_type(block_type(-1) << __shift);
  395. }
  396. /**
  397. * These versions of single-bit set, reset, flip, and test
  398. * do no range checking.
  399. */
  400. dynamic_bitset&
  401. _M_unchecked_set(size_type __pos) noexcept
  402. {
  403. this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
  404. return *this;
  405. }
  406. dynamic_bitset&
  407. _M_unchecked_set(size_type __pos, int __val) noexcept
  408. {
  409. if (__val)
  410. this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
  411. else
  412. this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
  413. return *this;
  414. }
  415. dynamic_bitset&
  416. _M_unchecked_reset(size_type __pos) noexcept
  417. {
  418. this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
  419. return *this;
  420. }
  421. dynamic_bitset&
  422. _M_unchecked_flip(size_type __pos) noexcept
  423. {
  424. this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
  425. return *this;
  426. }
  427. bool
  428. _M_unchecked_test(size_type __pos) const noexcept
  429. { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
  430. != static_cast<_WordT>(0)); }
  431. size_type _M_Nb = 0;
  432. public:
  433. /**
  434. * This encapsulates the concept of a single bit. An instance
  435. * of this class is a proxy for an actual bit; this way the
  436. * individual bit operations are done as faster word-size
  437. * bitwise instructions.
  438. *
  439. * Most users will never need to use this class directly;
  440. * conversions to and from bool are automatic and should be
  441. * transparent. Overloaded operators help to preserve the
  442. * illusion.
  443. *
  444. * (On a typical system, this "bit %reference" is 64 times the
  445. * size of an actual bit. Ha.)
  446. */
  447. class reference
  448. {
  449. friend class dynamic_bitset;
  450. block_type *_M_wp;
  451. size_type _M_bpos;
  452. public:
  453. reference(dynamic_bitset& __b, size_type __pos) noexcept
  454. {
  455. this->_M_wp = &__b._M_getword(__pos);
  456. this->_M_bpos = _Base::_S_whichbit(__pos);
  457. }
  458. // For b[i] = __x;
  459. reference&
  460. operator=(bool __x) noexcept
  461. {
  462. if (__x)
  463. *this->_M_wp |= _Base::_S_maskbit(this->_M_bpos);
  464. else
  465. *this->_M_wp &= ~_Base::_S_maskbit(this->_M_bpos);
  466. return *this;
  467. }
  468. // For b[i] = b[__j];
  469. reference&
  470. operator=(const reference& __j) noexcept
  471. {
  472. if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
  473. *this->_M_wp |= _Base::_S_maskbit(this->_M_bpos);
  474. else
  475. *this->_M_wp &= ~_Base::_S_maskbit(this->_M_bpos);
  476. return *this;
  477. }
  478. // Flips the bit
  479. bool
  480. operator~() const noexcept
  481. { return (*(_M_wp) & _Base::_S_maskbit(this->_M_bpos)) == 0; }
  482. // For __x = b[i];
  483. operator bool() const noexcept
  484. { return (*(this->_M_wp) & _Base::_S_maskbit(this->_M_bpos)) != 0; }
  485. // For b[i].flip();
  486. reference&
  487. flip() noexcept
  488. {
  489. *this->_M_wp ^= _Base::_S_maskbit(this->_M_bpos);
  490. return *this;
  491. }
  492. };
  493. friend class reference;
  494. typedef bool const_reference;
  495. // 23.3.5.1 constructors:
  496. /// All bits set to zero.
  497. dynamic_bitset() = default;
  498. /// All bits set to zero.
  499. explicit
  500. dynamic_bitset(const allocator_type& __alloc)
  501. : _Base(__alloc)
  502. { }
  503. /// Initial bits bitwise-copied from a single word (others set to zero).
  504. explicit
  505. dynamic_bitset(size_type __nbits, unsigned long long __val = 0ULL,
  506. const allocator_type& __alloc = allocator_type())
  507. : _Base(__nbits, __val, __alloc),
  508. _M_Nb(__nbits)
  509. { }
  510. dynamic_bitset(initializer_list<block_type> __il,
  511. const allocator_type& __alloc = allocator_type())
  512. : _Base(__alloc)
  513. { this->append(__il); }
  514. /**
  515. * @brief Use a subset of a string.
  516. * @param __str A string of '0' and '1' characters.
  517. * @param __pos Index of the first character in @p __str to use.
  518. * @param __n The number of characters to copy.
  519. * @param __zero The character to use for unset bits.
  520. * @param __one The character to use for set bits.
  521. * @param __alloc An allocator.
  522. * @throw std::out_of_range If @p __pos is bigger the size of @p __str.
  523. * @throw std::invalid_argument If a character appears in the string
  524. * which is neither '0' nor '1'.
  525. */
  526. template<typename _CharT, typename _Traits, typename _Alloc1>
  527. explicit
  528. dynamic_bitset(const std::basic_string<_CharT, _Traits, _Alloc1>& __str,
  529. typename basic_string<_CharT,_Traits,_Alloc1>::size_type
  530. __pos = 0,
  531. typename basic_string<_CharT,_Traits,_Alloc1>::size_type
  532. __n = std::basic_string<_CharT, _Traits, _Alloc1>::npos,
  533. _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'),
  534. const allocator_type& __alloc = allocator_type())
  535. : _Base(__alloc)
  536. {
  537. if (__pos > __str.size())
  538. __throw_out_of_range(__N("dynamic_bitset::bitset initial position "
  539. "not valid"));
  540. // Watch for npos.
  541. this->_M_Nb = (__n > __str.size() ? __str.size() - __pos : __n);
  542. this->resize(this->_M_Nb);
  543. this->_M_copy_from_string(__str, __pos, __n);
  544. }
  545. /**
  546. * @brief Construct from a string.
  547. * @param __str A string of '0' and '1' characters.
  548. * @param __alloc An allocator.
  549. * @throw std::invalid_argument If a character appears in the string
  550. * which is neither '0' nor '1'.
  551. */
  552. explicit
  553. dynamic_bitset(const char* __str,
  554. const allocator_type& __alloc = allocator_type())
  555. : _Base(__builtin_strlen(__str), 0ULL, __alloc),
  556. _M_Nb(__builtin_strlen(__str))
  557. {
  558. this->_M_copy_from_ptr(__str, _M_Nb, 0, _M_Nb);
  559. }
  560. /// Copy constructor.
  561. dynamic_bitset(const dynamic_bitset&) = default;
  562. /// Move constructor.
  563. dynamic_bitset(dynamic_bitset&& __b) noexcept
  564. : _Base(std::move(__b)), _M_Nb(__b._M_Nb)
  565. { __b.clear(); }
  566. /// Swap with another bitset.
  567. void
  568. swap(dynamic_bitset& __b) noexcept
  569. {
  570. this->_M_swap(__b);
  571. std::swap(this->_M_Nb, __b._M_Nb);
  572. }
  573. /// Copy assignment operator.
  574. dynamic_bitset& operator=(const dynamic_bitset&) = default;
  575. /// Move assignment operator.
  576. dynamic_bitset&
  577. operator=(dynamic_bitset&& __b)
  578. noexcept(std::is_nothrow_move_assignable<_Base>::value)
  579. {
  580. static_cast<_Base&>(*this) = static_cast<_Base&&>(__b);
  581. _M_Nb = __b._M_Nb;
  582. if _GLIBCXX17_CONSTEXPR (std::is_nothrow_move_assignable<_Base>::value)
  583. __b._M_Nb = 0;
  584. else if (get_allocator() == __b.get_allocator())
  585. __b._M_Nb = 0;
  586. return *this;
  587. }
  588. /**
  589. * @brief Return the allocator for the bitset.
  590. */
  591. allocator_type
  592. get_allocator() const noexcept
  593. { return this->_M_get_allocator(); }
  594. /**
  595. * @brief Resize the bitset.
  596. */
  597. void
  598. resize(size_type __nbits, bool __value = false)
  599. {
  600. if (__value)
  601. this->_M_do_fill();
  602. this->_M_resize(__nbits, __value);
  603. this->_M_Nb = __nbits;
  604. this->_M_do_sanitize();
  605. }
  606. /**
  607. * @brief Clear the bitset.
  608. */
  609. void
  610. clear()
  611. {
  612. this->_M_clear();
  613. this->_M_Nb = 0;
  614. }
  615. /**
  616. * @brief Push a bit onto the high end of the bitset.
  617. */
  618. void
  619. push_back(bool __bit)
  620. {
  621. if (this->size() % bits_per_block == 0)
  622. this->_M_do_append_block(block_type(__bit), this->_M_Nb);
  623. else
  624. this->_M_unchecked_set(this->_M_Nb, __bit);
  625. ++this->_M_Nb;
  626. }
  627. // XXX why is there no pop_back() member in the proposal?
  628. /**
  629. * @brief Append a block.
  630. */
  631. void
  632. append(block_type __block)
  633. {
  634. this->_M_do_append_block(__block, this->_M_Nb);
  635. this->_M_Nb += bits_per_block;
  636. }
  637. /**
  638. * @brief
  639. */
  640. void
  641. append(initializer_list<block_type> __il)
  642. { this->append(__il.begin(), __il.end()); }
  643. /**
  644. * @brief Append an iterator range of blocks.
  645. */
  646. template <typename _BlockInputIterator>
  647. void
  648. append(_BlockInputIterator __first, _BlockInputIterator __last)
  649. {
  650. for (; __first != __last; ++__first)
  651. this->append(*__first);
  652. }
  653. // 23.3.5.2 dynamic_bitset operations:
  654. //@{
  655. /**
  656. * @brief Operations on dynamic_bitsets.
  657. * @param __rhs A same-sized dynamic_bitset.
  658. *
  659. * These should be self-explanatory.
  660. */
  661. dynamic_bitset&
  662. operator&=(const dynamic_bitset& __rhs)
  663. {
  664. this->_M_do_and(__rhs);
  665. return *this;
  666. }
  667. dynamic_bitset&
  668. operator&=(dynamic_bitset&& __rhs)
  669. {
  670. this->_M_do_and(std::move(__rhs));
  671. return *this;
  672. }
  673. dynamic_bitset&
  674. operator|=(const dynamic_bitset& __rhs)
  675. {
  676. this->_M_do_or(__rhs);
  677. return *this;
  678. }
  679. dynamic_bitset&
  680. operator^=(const dynamic_bitset& __rhs)
  681. {
  682. this->_M_do_xor(__rhs);
  683. return *this;
  684. }
  685. dynamic_bitset&
  686. operator-=(const dynamic_bitset& __rhs)
  687. {
  688. this->_M_do_dif(__rhs);
  689. return *this;
  690. }
  691. //@}
  692. //@{
  693. /**
  694. * @brief Operations on dynamic_bitsets.
  695. * @param __pos The number of places to shift.
  696. *
  697. * These should be self-explanatory.
  698. */
  699. dynamic_bitset&
  700. operator<<=(size_type __pos)
  701. {
  702. if (__builtin_expect(__pos < this->_M_Nb, 1))
  703. {
  704. this->_M_do_left_shift(__pos);
  705. this->_M_do_sanitize();
  706. }
  707. else
  708. this->_M_do_reset();
  709. return *this;
  710. }
  711. dynamic_bitset&
  712. operator>>=(size_type __pos)
  713. {
  714. if (__builtin_expect(__pos < this->_M_Nb, 1))
  715. {
  716. this->_M_do_right_shift(__pos);
  717. this->_M_do_sanitize();
  718. }
  719. else
  720. this->_M_do_reset();
  721. return *this;
  722. }
  723. //@}
  724. // Set, reset, and flip.
  725. /**
  726. * @brief Sets every bit to true.
  727. */
  728. dynamic_bitset&
  729. set()
  730. {
  731. this->_M_do_set();
  732. this->_M_do_sanitize();
  733. return *this;
  734. }
  735. /**
  736. * @brief Sets a given bit to a particular value.
  737. * @param __pos The index of the bit.
  738. * @param __val Either true or false, defaults to true.
  739. * @throw std::out_of_range If @a __pos is bigger the size of the %set.
  740. */
  741. dynamic_bitset&
  742. set(size_type __pos, bool __val = true)
  743. {
  744. if (__pos >= _M_Nb)
  745. __throw_out_of_range(__N("dynamic_bitset::set"));
  746. return this->_M_unchecked_set(__pos, __val);
  747. }
  748. /**
  749. * @brief Sets every bit to false.
  750. */
  751. dynamic_bitset&
  752. reset()
  753. {
  754. this->_M_do_reset();
  755. return *this;
  756. }
  757. /**
  758. * @brief Sets a given bit to false.
  759. * @param __pos The index of the bit.
  760. * @throw std::out_of_range If @a __pos is bigger the size of the %set.
  761. *
  762. * Same as writing @c set(__pos, false).
  763. */
  764. dynamic_bitset&
  765. reset(size_type __pos)
  766. {
  767. if (__pos >= _M_Nb)
  768. __throw_out_of_range(__N("dynamic_bitset::reset"));
  769. return this->_M_unchecked_reset(__pos);
  770. }
  771. /**
  772. * @brief Toggles every bit to its opposite value.
  773. */
  774. dynamic_bitset&
  775. flip()
  776. {
  777. this->_M_do_flip();
  778. this->_M_do_sanitize();
  779. return *this;
  780. }
  781. /**
  782. * @brief Toggles a given bit to its opposite value.
  783. * @param __pos The index of the bit.
  784. * @throw std::out_of_range If @a __pos is bigger the size of the %set.
  785. */
  786. dynamic_bitset&
  787. flip(size_type __pos)
  788. {
  789. if (__pos >= _M_Nb)
  790. __throw_out_of_range(__N("dynamic_bitset::flip"));
  791. return this->_M_unchecked_flip(__pos);
  792. }
  793. /// See the no-argument flip().
  794. dynamic_bitset
  795. operator~() const
  796. { return dynamic_bitset<_WordT, _Alloc>(*this).flip(); }
  797. //@{
  798. /**
  799. * @brief Array-indexing support.
  800. * @param __pos Index into the %dynamic_bitset.
  801. * @return A bool for a 'const %dynamic_bitset'. For non-const
  802. * bitsets, an instance of the reference proxy class.
  803. * @note These operators do no range checking and throw no
  804. * exceptions, as required by DR 11 to the standard.
  805. */
  806. reference
  807. operator[](size_type __pos)
  808. { return reference(*this,__pos); }
  809. const_reference
  810. operator[](size_type __pos) const
  811. { return _M_unchecked_test(__pos); }
  812. //@}
  813. /**
  814. * @brief Returns a numerical interpretation of the %dynamic_bitset.
  815. * @return The integral equivalent of the bits.
  816. * @throw std::overflow_error If there are too many bits to be
  817. * represented in an @c unsigned @c long.
  818. */
  819. unsigned long
  820. to_ulong() const
  821. { return this->_M_do_to_ulong(); }
  822. /**
  823. * @brief Returns a numerical interpretation of the %dynamic_bitset.
  824. * @return The integral equivalent of the bits.
  825. * @throw std::overflow_error If there are too many bits to be
  826. * represented in an @c unsigned @c long.
  827. */
  828. unsigned long long
  829. to_ullong() const
  830. { return this->_M_do_to_ullong(); }
  831. /**
  832. * @brief Returns a character interpretation of the %dynamic_bitset.
  833. * @return The string equivalent of the bits.
  834. *
  835. * Note the ordering of the bits: decreasing character positions
  836. * correspond to increasing bit positions (see the main class notes for
  837. * an example).
  838. */
  839. template<typename _CharT = char,
  840. typename _Traits = std::char_traits<_CharT>,
  841. typename _Alloc1 = std::allocator<_CharT>>
  842. std::basic_string<_CharT, _Traits, _Alloc1>
  843. to_string(_CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) const
  844. {
  845. std::basic_string<_CharT, _Traits, _Alloc1> __result;
  846. _M_copy_to_string(__result, __zero, __one);
  847. return __result;
  848. }
  849. // Helper functions for string operations.
  850. template<typename _Traits = std::char_traits<char>,
  851. typename _CharT = typename _Traits::char_type>
  852. void
  853. _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
  854. _CharT __zero = _CharT('0'),
  855. _CharT __one = _CharT('1'));
  856. template<typename _CharT, typename _Traits, typename _Alloc1>
  857. void
  858. _M_copy_from_string(const basic_string<_CharT, _Traits, _Alloc1>& __str,
  859. size_t __pos, size_t __n,
  860. _CharT __zero = _CharT('0'),
  861. _CharT __one = _CharT('1'))
  862. {
  863. _M_copy_from_ptr<_Traits>(__str.data(), __str.size(), __pos, __n,
  864. __zero, __one);
  865. }
  866. template<typename _CharT, typename _Traits, typename _Alloc1>
  867. void
  868. _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc1>& __str,
  869. _CharT __zero = _CharT('0'),
  870. _CharT __one = _CharT('1')) const;
  871. /// Returns the number of bits which are set.
  872. size_type
  873. count() const noexcept
  874. { return this->_M_do_count(); }
  875. /// Returns the total number of bits.
  876. size_type
  877. size() const noexcept
  878. { return this->_M_Nb; }
  879. /// Returns the total number of blocks.
  880. size_type
  881. num_blocks() const noexcept
  882. { return this->_M_size(); }
  883. /// Returns true if the dynamic_bitset is empty.
  884. _GLIBCXX_NODISCARD bool
  885. empty() const noexcept
  886. { return (this->_M_Nb == 0); }
  887. /// Returns the maximum size of a dynamic_bitset object having the same
  888. /// type as *this.
  889. /// The real answer is max() * bits_per_block but is likely to overflow.
  890. constexpr size_type
  891. max_size() noexcept
  892. { return std::numeric_limits<block_type>::max(); }
  893. /**
  894. * @brief Tests the value of a bit.
  895. * @param __pos The index of a bit.
  896. * @return The value at @a __pos.
  897. * @throw std::out_of_range If @a __pos is bigger the size of the %set.
  898. */
  899. bool
  900. test(size_type __pos) const
  901. {
  902. if (__pos >= _M_Nb)
  903. __throw_out_of_range(__N("dynamic_bitset::test"));
  904. return _M_unchecked_test(__pos);
  905. }
  906. /**
  907. * @brief Tests whether all the bits are on.
  908. * @return True if all the bits are set.
  909. */
  910. bool
  911. all() const
  912. { return this->_M_are_all_aux() == _M_Nb; }
  913. /**
  914. * @brief Tests whether any of the bits are on.
  915. * @return True if at least one bit is set.
  916. */
  917. bool
  918. any() const
  919. { return this->_M_is_any(); }
  920. /**
  921. * @brief Tests whether any of the bits are on.
  922. * @return True if none of the bits are set.
  923. */
  924. bool
  925. none() const
  926. { return !this->_M_is_any(); }
  927. //@{
  928. /// Self-explanatory.
  929. dynamic_bitset
  930. operator<<(size_type __pos) const
  931. { return dynamic_bitset(*this) <<= __pos; }
  932. dynamic_bitset
  933. operator>>(size_type __pos) const
  934. { return dynamic_bitset(*this) >>= __pos; }
  935. //@}
  936. /**
  937. * @brief Finds the index of the first "on" bit.
  938. * @return The index of the first bit set, or size() if not found.
  939. * @sa find_next
  940. */
  941. size_type
  942. find_first() const
  943. { return this->_M_do_find_first(this->_M_Nb); }
  944. /**
  945. * @brief Finds the index of the next "on" bit after prev.
  946. * @return The index of the next bit set, or size() if not found.
  947. * @param __prev Where to start searching.
  948. * @sa find_first
  949. */
  950. size_type
  951. find_next(size_t __prev) const
  952. { return this->_M_do_find_next(__prev, this->_M_Nb); }
  953. bool
  954. is_subset_of(const dynamic_bitset& __b) const
  955. { return this->_M_is_subset_of(__b); }
  956. bool
  957. is_proper_subset_of(const dynamic_bitset& __b) const
  958. { return this->_M_is_proper_subset_of(__b); }
  959. friend bool
  960. operator==(const dynamic_bitset& __lhs,
  961. const dynamic_bitset& __rhs) noexcept
  962. { return __lhs._M_Nb == __rhs._M_Nb && __lhs._M_is_equal(__rhs); }
  963. friend bool
  964. operator<(const dynamic_bitset& __lhs,
  965. const dynamic_bitset& __rhs) noexcept
  966. { return __lhs._M_is_less(__rhs) || __lhs._M_Nb < __rhs._M_Nb; }
  967. };
  968. template<typename _WordT, typename _Alloc>
  969. template<typename _CharT, typename _Traits, typename _Alloc1>
  970. inline void
  971. dynamic_bitset<_WordT, _Alloc>::
  972. _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc1>& __str,
  973. _CharT __zero, _CharT __one) const
  974. {
  975. __str.assign(_M_Nb, __zero);
  976. for (size_t __i = _M_Nb; __i > 0; --__i)
  977. if (_M_unchecked_test(__i - 1))
  978. _Traits::assign(__str[_M_Nb - __i], __one);
  979. }
  980. //@{
  981. /// These comparisons for equality/inequality are, well, @e bitwise.
  982. template<typename _WordT, typename _Alloc>
  983. inline bool
  984. operator!=(const dynamic_bitset<_WordT, _Alloc>& __lhs,
  985. const dynamic_bitset<_WordT, _Alloc>& __rhs)
  986. { return !(__lhs == __rhs); }
  987. template<typename _WordT, typename _Alloc>
  988. inline bool
  989. operator<=(const dynamic_bitset<_WordT, _Alloc>& __lhs,
  990. const dynamic_bitset<_WordT, _Alloc>& __rhs)
  991. { return !(__lhs > __rhs); }
  992. template<typename _WordT, typename _Alloc>
  993. inline bool
  994. operator>(const dynamic_bitset<_WordT, _Alloc>& __lhs,
  995. const dynamic_bitset<_WordT, _Alloc>& __rhs)
  996. { return __rhs < __lhs; }
  997. template<typename _WordT, typename _Alloc>
  998. inline bool
  999. operator>=(const dynamic_bitset<_WordT, _Alloc>& __lhs,
  1000. const dynamic_bitset<_WordT, _Alloc>& __rhs)
  1001. { return !(__lhs < __rhs); }
  1002. //@}
  1003. // 23.3.5.3 bitset operations:
  1004. //@{
  1005. /**
  1006. * @brief Global bitwise operations on bitsets.
  1007. * @param __x A bitset.
  1008. * @param __y A bitset of the same size as @a __x.
  1009. * @return A new bitset.
  1010. *
  1011. * These should be self-explanatory.
  1012. */
  1013. template<typename _WordT, typename _Alloc>
  1014. inline dynamic_bitset<_WordT, _Alloc>
  1015. operator&(const dynamic_bitset<_WordT, _Alloc>& __x,
  1016. const dynamic_bitset<_WordT, _Alloc>& __y)
  1017. {
  1018. dynamic_bitset<_WordT, _Alloc> __result(__x);
  1019. __result &= __y;
  1020. return __result;
  1021. }
  1022. template<typename _WordT, typename _Alloc>
  1023. inline dynamic_bitset<_WordT, _Alloc>
  1024. operator|(const dynamic_bitset<_WordT, _Alloc>& __x,
  1025. const dynamic_bitset<_WordT, _Alloc>& __y)
  1026. {
  1027. dynamic_bitset<_WordT, _Alloc> __result(__x);
  1028. __result |= __y;
  1029. return __result;
  1030. }
  1031. template <typename _WordT, typename _Alloc>
  1032. inline dynamic_bitset<_WordT, _Alloc>
  1033. operator^(const dynamic_bitset<_WordT, _Alloc>& __x,
  1034. const dynamic_bitset<_WordT, _Alloc>& __y)
  1035. {
  1036. dynamic_bitset<_WordT, _Alloc> __result(__x);
  1037. __result ^= __y;
  1038. return __result;
  1039. }
  1040. template <typename _WordT, typename _Alloc>
  1041. inline dynamic_bitset<_WordT, _Alloc>
  1042. operator-(const dynamic_bitset<_WordT, _Alloc>& __x,
  1043. const dynamic_bitset<_WordT, _Alloc>& __y)
  1044. {
  1045. dynamic_bitset<_WordT, _Alloc> __result(__x);
  1046. __result -= __y;
  1047. return __result;
  1048. }
  1049. //@}
  1050. /// Stream output operator for dynamic_bitset.
  1051. template <typename _CharT, typename _Traits,
  1052. typename _WordT, typename _Alloc>
  1053. inline std::basic_ostream<_CharT, _Traits>&
  1054. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1055. const dynamic_bitset<_WordT, _Alloc>& __x)
  1056. {
  1057. std::basic_string<_CharT, _Traits> __tmp;
  1058. const ctype<_CharT>& __ct = use_facet<ctype<_CharT>>(__os.getloc());
  1059. __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
  1060. return __os << __tmp;
  1061. }
  1062. /**
  1063. * @}
  1064. */
  1065. } // tr2
  1066. _GLIBCXX_END_NAMESPACE_VERSION
  1067. } // std
  1068. #include <tr2/dynamic_bitset.tcc>
  1069. #endif /* _GLIBCXX_TR2_DYNAMIC_BITSET */