bitset 45 KB

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