dynamic_bitset 33 KB

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