bitset 45 KB

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