bitset 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  1. // <bitset> -*- C++ -*-
  2. // Copyright (C) 2001-2016 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. * @brief The %bitset class represents a @e fixed-size sequence of bits.
  565. * @ingroup utilities
  566. *
  567. * (Note that %bitset does @e not meet the formal requirements of a
  568. * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
  569. *
  570. * The template argument, @a Nb, may be any non-negative number,
  571. * specifying the number of bits (e.g., "0", "12", "1024*1024").
  572. *
  573. * In the general unoptimized case, storage is allocated in word-sized
  574. * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
  575. * words will be used for storage. B - Nb%B bits are unused. (They are
  576. * the high-order bits in the highest word.) It is a class invariant
  577. * that those unused bits are always zero.
  578. *
  579. * If you think of %bitset as <em>a simple array of bits</em>, be
  580. * aware that your mental picture is reversed: a %bitset behaves
  581. * the same way as bits in integers do, with the bit at index 0 in
  582. * the <em>least significant / right-hand</em> position, and the bit at
  583. * index Nb-1 in the <em>most significant / left-hand</em> position.
  584. * Thus, unlike other containers, a %bitset's index <em>counts from
  585. * right to left</em>, to put it very loosely.
  586. *
  587. * This behavior is preserved when translating to and from strings. For
  588. * example, the first line of the following program probably prints
  589. * <em>b(&apos;a&apos;) is 0001100001</em> on a modern ASCII system.
  590. *
  591. * @code
  592. * #include <bitset>
  593. * #include <iostream>
  594. * #include <sstream>
  595. *
  596. * using namespace std;
  597. *
  598. * int main()
  599. * {
  600. * long a = 'a';
  601. * bitset<10> b(a);
  602. *
  603. * cout << "b('a') is " << b << endl;
  604. *
  605. * ostringstream s;
  606. * s << b;
  607. * string str = s.str();
  608. * cout << "index 3 in the string is " << str[3] << " but\n"
  609. * << "index 3 in the bitset is " << b[3] << endl;
  610. * }
  611. * @endcode
  612. *
  613. * Also see:
  614. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_containers.html
  615. * for a description of extensions.
  616. *
  617. * Most of the actual code isn't contained in %bitset<> itself, but in the
  618. * base class _Base_bitset. The base class works with whole words, not with
  619. * individual bits. This allows us to specialize _Base_bitset for the
  620. * important special case where the %bitset is only a single word.
  621. *
  622. * Extra confusion can result due to the fact that the storage for
  623. * _Base_bitset @e is a regular array, and is indexed as such. This is
  624. * carefully encapsulated.
  625. */
  626. template<size_t _Nb>
  627. class bitset
  628. : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
  629. {
  630. private:
  631. typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
  632. typedef unsigned long _WordT;
  633. template<class _CharT, class _Traits, class _Alloc>
  634. void
  635. _M_check_initial_position(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
  636. size_t __position) const
  637. {
  638. if (__position > __s.size())
  639. __throw_out_of_range_fmt(__N("bitset::bitset: __position "
  640. "(which is %zu) > __s.size() "
  641. "(which is %zu)"),
  642. __position, __s.size());
  643. }
  644. void _M_check(size_t __position, const char *__s) const
  645. {
  646. if (__position >= _Nb)
  647. __throw_out_of_range_fmt(__N("%s: __position (which is %zu) "
  648. ">= _Nb (which is %zu)"),
  649. __s, __position, _Nb);
  650. }
  651. void
  652. _M_do_sanitize() _GLIBCXX_NOEXCEPT
  653. {
  654. typedef _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD> __sanitize_type;
  655. __sanitize_type::_S_do_sanitize(this->_M_hiword());
  656. }
  657. #if __cplusplus >= 201103L
  658. template<typename> friend struct hash;
  659. #endif
  660. public:
  661. /**
  662. * This encapsulates the concept of a single bit. An instance of this
  663. * class is a proxy for an actual bit; this way the individual bit
  664. * operations are done as faster word-size bitwise instructions.
  665. *
  666. * Most users will never need to use this class directly; conversions
  667. * to and from bool are automatic and should be transparent. Overloaded
  668. * operators help to preserve the illusion.
  669. *
  670. * (On a typical system, this <em>bit %reference</em> is 64
  671. * times the size of an actual bit. Ha.)
  672. */
  673. class reference
  674. {
  675. friend class bitset;
  676. _WordT* _M_wp;
  677. size_t _M_bpos;
  678. // left undefined
  679. reference();
  680. public:
  681. reference(bitset& __b, size_t __pos) _GLIBCXX_NOEXCEPT
  682. {
  683. _M_wp = &__b._M_getword(__pos);
  684. _M_bpos = _Base::_S_whichbit(__pos);
  685. }
  686. ~reference() _GLIBCXX_NOEXCEPT
  687. { }
  688. // For b[i] = __x;
  689. reference&
  690. operator=(bool __x) _GLIBCXX_NOEXCEPT
  691. {
  692. if (__x)
  693. *_M_wp |= _Base::_S_maskbit(_M_bpos);
  694. else
  695. *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
  696. return *this;
  697. }
  698. // For b[i] = b[__j];
  699. reference&
  700. operator=(const reference& __j) _GLIBCXX_NOEXCEPT
  701. {
  702. if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
  703. *_M_wp |= _Base::_S_maskbit(_M_bpos);
  704. else
  705. *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
  706. return *this;
  707. }
  708. // Flips the bit
  709. bool
  710. operator~() const _GLIBCXX_NOEXCEPT
  711. { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
  712. // For __x = b[i];
  713. operator bool() const _GLIBCXX_NOEXCEPT
  714. { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
  715. // For b[i].flip();
  716. reference&
  717. flip() _GLIBCXX_NOEXCEPT
  718. {
  719. *_M_wp ^= _Base::_S_maskbit(_M_bpos);
  720. return *this;
  721. }
  722. };
  723. friend class reference;
  724. // 23.3.5.1 constructors:
  725. /// All bits set to zero.
  726. _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
  727. { }
  728. /// Initial bits bitwise-copied from a single word (others set to zero).
  729. #if __cplusplus >= 201103L
  730. constexpr bitset(unsigned long long __val) noexcept
  731. : _Base(_Sanitize_val<_Nb>::_S_do_sanitize_val(__val)) { }
  732. #else
  733. bitset(unsigned long __val)
  734. : _Base(__val)
  735. { _M_do_sanitize(); }
  736. #endif
  737. /**
  738. * Use a subset of a string.
  739. * @param __s A string of @a 0 and @a 1 characters.
  740. * @param __position Index of the first character in @a __s to use;
  741. * defaults to zero.
  742. * @throw std::out_of_range If @a pos is bigger the size of @a __s.
  743. * @throw std::invalid_argument If a character appears in the string
  744. * which is neither @a 0 nor @a 1.
  745. */
  746. template<class _CharT, class _Traits, class _Alloc>
  747. explicit
  748. bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
  749. size_t __position = 0)
  750. : _Base()
  751. {
  752. _M_check_initial_position(__s, __position);
  753. _M_copy_from_string(__s, __position,
  754. std::basic_string<_CharT, _Traits, _Alloc>::npos,
  755. _CharT('0'), _CharT('1'));
  756. }
  757. /**
  758. * Use a subset of a string.
  759. * @param __s A string of @a 0 and @a 1 characters.
  760. * @param __position Index of the first character in @a __s to use.
  761. * @param __n The number of characters to copy.
  762. * @throw std::out_of_range If @a __position is bigger the size
  763. * of @a __s.
  764. * @throw std::invalid_argument If a character appears in the string
  765. * which is neither @a 0 nor @a 1.
  766. */
  767. template<class _CharT, class _Traits, class _Alloc>
  768. bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
  769. size_t __position, size_t __n)
  770. : _Base()
  771. {
  772. _M_check_initial_position(__s, __position);
  773. _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
  774. }
  775. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  776. // 396. what are characters zero and one.
  777. template<class _CharT, class _Traits, class _Alloc>
  778. bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
  779. size_t __position, size_t __n,
  780. _CharT __zero, _CharT __one = _CharT('1'))
  781. : _Base()
  782. {
  783. _M_check_initial_position(__s, __position);
  784. _M_copy_from_string(__s, __position, __n, __zero, __one);
  785. }
  786. #if __cplusplus >= 201103L
  787. /**
  788. * Construct from a character %array.
  789. * @param __str An %array of characters @a zero and @a one.
  790. * @param __n The number of characters to use.
  791. * @param __zero The character corresponding to the value 0.
  792. * @param __one The character corresponding to the value 1.
  793. * @throw std::invalid_argument If a character appears in the string
  794. * which is neither @a __zero nor @a __one.
  795. */
  796. template<typename _CharT>
  797. explicit
  798. bitset(const _CharT* __str,
  799. typename std::basic_string<_CharT>::size_type __n
  800. = std::basic_string<_CharT>::npos,
  801. _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
  802. : _Base()
  803. {
  804. if (!__str)
  805. __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)"));
  806. if (__n == std::basic_string<_CharT>::npos)
  807. __n = std::char_traits<_CharT>::length(__str);
  808. _M_copy_from_ptr<_CharT, std::char_traits<_CharT>>(__str, __n, 0,
  809. __n, __zero,
  810. __one);
  811. }
  812. #endif
  813. // 23.3.5.2 bitset operations:
  814. //@{
  815. /**
  816. * Operations on bitsets.
  817. * @param __rhs A same-sized bitset.
  818. *
  819. * These should be self-explanatory.
  820. */
  821. bitset<_Nb>&
  822. operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
  823. {
  824. this->_M_do_and(__rhs);
  825. return *this;
  826. }
  827. bitset<_Nb>&
  828. operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
  829. {
  830. this->_M_do_or(__rhs);
  831. return *this;
  832. }
  833. bitset<_Nb>&
  834. operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
  835. {
  836. this->_M_do_xor(__rhs);
  837. return *this;
  838. }
  839. //@}
  840. //@{
  841. /**
  842. * Operations on bitsets.
  843. * @param __position The number of places to shift.
  844. *
  845. * These should be self-explanatory.
  846. */
  847. bitset<_Nb>&
  848. operator<<=(size_t __position) _GLIBCXX_NOEXCEPT
  849. {
  850. if (__builtin_expect(__position < _Nb, 1))
  851. {
  852. this->_M_do_left_shift(__position);
  853. this->_M_do_sanitize();
  854. }
  855. else
  856. this->_M_do_reset();
  857. return *this;
  858. }
  859. bitset<_Nb>&
  860. operator>>=(size_t __position) _GLIBCXX_NOEXCEPT
  861. {
  862. if (__builtin_expect(__position < _Nb, 1))
  863. {
  864. this->_M_do_right_shift(__position);
  865. this->_M_do_sanitize();
  866. }
  867. else
  868. this->_M_do_reset();
  869. return *this;
  870. }
  871. //@}
  872. //@{
  873. /**
  874. * These versions of single-bit set, reset, flip, and test are
  875. * extensions from the SGI version. They do no range checking.
  876. * @ingroup SGIextensions
  877. */
  878. bitset<_Nb>&
  879. _Unchecked_set(size_t __pos) _GLIBCXX_NOEXCEPT
  880. {
  881. this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
  882. return *this;
  883. }
  884. bitset<_Nb>&
  885. _Unchecked_set(size_t __pos, int __val) _GLIBCXX_NOEXCEPT
  886. {
  887. if (__val)
  888. this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
  889. else
  890. this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
  891. return *this;
  892. }
  893. bitset<_Nb>&
  894. _Unchecked_reset(size_t __pos) _GLIBCXX_NOEXCEPT
  895. {
  896. this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
  897. return *this;
  898. }
  899. bitset<_Nb>&
  900. _Unchecked_flip(size_t __pos) _GLIBCXX_NOEXCEPT
  901. {
  902. this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
  903. return *this;
  904. }
  905. _GLIBCXX_CONSTEXPR bool
  906. _Unchecked_test(size_t __pos) const _GLIBCXX_NOEXCEPT
  907. { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
  908. != static_cast<_WordT>(0)); }
  909. //@}
  910. // Set, reset, and flip.
  911. /**
  912. * @brief Sets every bit to true.
  913. */
  914. bitset<_Nb>&
  915. set() _GLIBCXX_NOEXCEPT
  916. {
  917. this->_M_do_set();
  918. this->_M_do_sanitize();
  919. return *this;
  920. }
  921. /**
  922. * @brief Sets a given bit to a particular value.
  923. * @param __position The index of the bit.
  924. * @param __val Either true or false, defaults to true.
  925. * @throw std::out_of_range If @a pos is bigger the size of the %set.
  926. */
  927. bitset<_Nb>&
  928. set(size_t __position, bool __val = true)
  929. {
  930. this->_M_check(__position, __N("bitset::set"));
  931. return _Unchecked_set(__position, __val);
  932. }
  933. /**
  934. * @brief Sets every bit to false.
  935. */
  936. bitset<_Nb>&
  937. reset() _GLIBCXX_NOEXCEPT
  938. {
  939. this->_M_do_reset();
  940. return *this;
  941. }
  942. /**
  943. * @brief Sets a given bit to false.
  944. * @param __position The index of the bit.
  945. * @throw std::out_of_range If @a pos is bigger the size of the %set.
  946. *
  947. * Same as writing @c set(pos,false).
  948. */
  949. bitset<_Nb>&
  950. reset(size_t __position)
  951. {
  952. this->_M_check(__position, __N("bitset::reset"));
  953. return _Unchecked_reset(__position);
  954. }
  955. /**
  956. * @brief Toggles every bit to its opposite value.
  957. */
  958. bitset<_Nb>&
  959. flip() _GLIBCXX_NOEXCEPT
  960. {
  961. this->_M_do_flip();
  962. this->_M_do_sanitize();
  963. return *this;
  964. }
  965. /**
  966. * @brief Toggles a given bit to its opposite value.
  967. * @param __position The index of the bit.
  968. * @throw std::out_of_range If @a pos is bigger the size of the %set.
  969. */
  970. bitset<_Nb>&
  971. flip(size_t __position)
  972. {
  973. this->_M_check(__position, __N("bitset::flip"));
  974. return _Unchecked_flip(__position);
  975. }
  976. /// See the no-argument flip().
  977. bitset<_Nb>
  978. operator~() const _GLIBCXX_NOEXCEPT
  979. { return bitset<_Nb>(*this).flip(); }
  980. //@{
  981. /**
  982. * @brief Array-indexing support.
  983. * @param __position Index into the %bitset.
  984. * @return A bool for a <em>const %bitset</em>. For non-const
  985. * bitsets, an instance of the reference proxy class.
  986. * @note These operators do no range checking and throw no exceptions,
  987. * as required by DR 11 to the standard.
  988. *
  989. * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
  990. * resolves DR 11 (items 1 and 2), but does not do the range-checking
  991. * required by that DR's resolution. -pme
  992. * The DR has since been changed: range-checking is a precondition
  993. * (users' responsibility), and these functions must not throw. -pme
  994. */
  995. reference
  996. operator[](size_t __position)
  997. { return reference(*this, __position); }
  998. _GLIBCXX_CONSTEXPR bool
  999. operator[](size_t __position) const
  1000. { return _Unchecked_test(__position); }
  1001. //@}
  1002. /**
  1003. * @brief Returns a numerical interpretation of the %bitset.
  1004. * @return The integral equivalent of the bits.
  1005. * @throw std::overflow_error If there are too many bits to be
  1006. * represented in an @c unsigned @c long.
  1007. */
  1008. unsigned long
  1009. to_ulong() const
  1010. { return this->_M_do_to_ulong(); }
  1011. #if __cplusplus >= 201103L
  1012. unsigned long long
  1013. to_ullong() const
  1014. { return this->_M_do_to_ullong(); }
  1015. #endif
  1016. /**
  1017. * @brief Returns a character interpretation of the %bitset.
  1018. * @return The string equivalent of the bits.
  1019. *
  1020. * Note the ordering of the bits: decreasing character positions
  1021. * correspond to increasing bit positions (see the main class notes for
  1022. * an example).
  1023. */
  1024. template<class _CharT, class _Traits, class _Alloc>
  1025. std::basic_string<_CharT, _Traits, _Alloc>
  1026. to_string() const
  1027. {
  1028. std::basic_string<_CharT, _Traits, _Alloc> __result;
  1029. _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
  1030. return __result;
  1031. }
  1032. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1033. // 396. what are characters zero and one.
  1034. template<class _CharT, class _Traits, class _Alloc>
  1035. std::basic_string<_CharT, _Traits, _Alloc>
  1036. to_string(_CharT __zero, _CharT __one = _CharT('1')) const
  1037. {
  1038. std::basic_string<_CharT, _Traits, _Alloc> __result;
  1039. _M_copy_to_string(__result, __zero, __one);
  1040. return __result;
  1041. }
  1042. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1043. // 434. bitset::to_string() hard to use.
  1044. template<class _CharT, class _Traits>
  1045. std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
  1046. to_string() const
  1047. { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
  1048. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1049. // 853. to_string needs updating with zero and one.
  1050. template<class _CharT, class _Traits>
  1051. std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
  1052. to_string(_CharT __zero, _CharT __one = _CharT('1')) const
  1053. { return to_string<_CharT, _Traits,
  1054. std::allocator<_CharT> >(__zero, __one); }
  1055. template<class _CharT>
  1056. std::basic_string<_CharT, std::char_traits<_CharT>,
  1057. std::allocator<_CharT> >
  1058. to_string() const
  1059. {
  1060. return to_string<_CharT, std::char_traits<_CharT>,
  1061. std::allocator<_CharT> >();
  1062. }
  1063. template<class _CharT>
  1064. std::basic_string<_CharT, std::char_traits<_CharT>,
  1065. std::allocator<_CharT> >
  1066. to_string(_CharT __zero, _CharT __one = _CharT('1')) const
  1067. {
  1068. return to_string<_CharT, std::char_traits<_CharT>,
  1069. std::allocator<_CharT> >(__zero, __one);
  1070. }
  1071. std::basic_string<char, std::char_traits<char>, std::allocator<char> >
  1072. to_string() const
  1073. {
  1074. return to_string<char, std::char_traits<char>,
  1075. std::allocator<char> >();
  1076. }
  1077. std::basic_string<char, std::char_traits<char>, std::allocator<char> >
  1078. to_string(char __zero, char __one = '1') const
  1079. {
  1080. return to_string<char, std::char_traits<char>,
  1081. std::allocator<char> >(__zero, __one);
  1082. }
  1083. // Helper functions for string operations.
  1084. template<class _CharT, class _Traits>
  1085. void
  1086. _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
  1087. _CharT, _CharT);
  1088. template<class _CharT, class _Traits, class _Alloc>
  1089. void
  1090. _M_copy_from_string(const std::basic_string<_CharT,
  1091. _Traits, _Alloc>& __s, size_t __pos, size_t __n,
  1092. _CharT __zero, _CharT __one)
  1093. { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
  1094. __zero, __one); }
  1095. template<class _CharT, class _Traits, class _Alloc>
  1096. void
  1097. _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&,
  1098. _CharT, _CharT) const;
  1099. // NB: Backward compat.
  1100. template<class _CharT, class _Traits, class _Alloc>
  1101. void
  1102. _M_copy_from_string(const std::basic_string<_CharT,
  1103. _Traits, _Alloc>& __s, size_t __pos, size_t __n)
  1104. { _M_copy_from_string(__s, __pos, __n, _CharT('0'), _CharT('1')); }
  1105. template<class _CharT, class _Traits, class _Alloc>
  1106. void
  1107. _M_copy_to_string(std::basic_string<_CharT, _Traits,_Alloc>& __s) const
  1108. { _M_copy_to_string(__s, _CharT('0'), _CharT('1')); }
  1109. /// Returns the number of bits which are set.
  1110. size_t
  1111. count() const _GLIBCXX_NOEXCEPT
  1112. { return this->_M_do_count(); }
  1113. /// Returns the total number of bits.
  1114. _GLIBCXX_CONSTEXPR size_t
  1115. size() const _GLIBCXX_NOEXCEPT
  1116. { return _Nb; }
  1117. //@{
  1118. /// These comparisons for equality/inequality are, well, @e bitwise.
  1119. bool
  1120. operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
  1121. { return this->_M_is_equal(__rhs); }
  1122. bool
  1123. operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
  1124. { return !this->_M_is_equal(__rhs); }
  1125. //@}
  1126. /**
  1127. * @brief Tests the value of a bit.
  1128. * @param __position The index of a bit.
  1129. * @return The value at @a pos.
  1130. * @throw std::out_of_range If @a pos is bigger the size of the %set.
  1131. */
  1132. bool
  1133. test(size_t __position) const
  1134. {
  1135. this->_M_check(__position, __N("bitset::test"));
  1136. return _Unchecked_test(__position);
  1137. }
  1138. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1139. // DR 693. std::bitset::all() missing.
  1140. /**
  1141. * @brief Tests whether all the bits are on.
  1142. * @return True if all the bits are set.
  1143. */
  1144. bool
  1145. all() const _GLIBCXX_NOEXCEPT
  1146. { return this->template _M_are_all<_Nb>(); }
  1147. /**
  1148. * @brief Tests whether any of the bits are on.
  1149. * @return True if at least one bit is set.
  1150. */
  1151. bool
  1152. any() const _GLIBCXX_NOEXCEPT
  1153. { return this->_M_is_any(); }
  1154. /**
  1155. * @brief Tests whether any of the bits are on.
  1156. * @return True if none of the bits are set.
  1157. */
  1158. bool
  1159. none() const _GLIBCXX_NOEXCEPT
  1160. { return !this->_M_is_any(); }
  1161. //@{
  1162. /// Self-explanatory.
  1163. bitset<_Nb>
  1164. operator<<(size_t __position) const _GLIBCXX_NOEXCEPT
  1165. { return bitset<_Nb>(*this) <<= __position; }
  1166. bitset<_Nb>
  1167. operator>>(size_t __position) const _GLIBCXX_NOEXCEPT
  1168. { return bitset<_Nb>(*this) >>= __position; }
  1169. //@}
  1170. /**
  1171. * @brief Finds the index of the first "on" bit.
  1172. * @return The index of the first bit set, or size() if not found.
  1173. * @ingroup SGIextensions
  1174. * @sa _Find_next
  1175. */
  1176. size_t
  1177. _Find_first() const _GLIBCXX_NOEXCEPT
  1178. { return this->_M_do_find_first(_Nb); }
  1179. /**
  1180. * @brief Finds the index of the next "on" bit after prev.
  1181. * @return The index of the next bit set, or size() if not found.
  1182. * @param __prev Where to start searching.
  1183. * @ingroup SGIextensions
  1184. * @sa _Find_first
  1185. */
  1186. size_t
  1187. _Find_next(size_t __prev) const _GLIBCXX_NOEXCEPT
  1188. { return this->_M_do_find_next(__prev, _Nb); }
  1189. };
  1190. // Definitions of non-inline member functions.
  1191. template<size_t _Nb>
  1192. template<class _CharT, class _Traits>
  1193. void
  1194. bitset<_Nb>::
  1195. _M_copy_from_ptr(const _CharT* __s, size_t __len,
  1196. size_t __pos, size_t __n, _CharT __zero, _CharT __one)
  1197. {
  1198. reset();
  1199. const size_t __nbits = std::min(_Nb, std::min(__n, size_t(__len - __pos)));
  1200. for (size_t __i = __nbits; __i > 0; --__i)
  1201. {
  1202. const _CharT __c = __s[__pos + __nbits - __i];
  1203. if (_Traits::eq(__c, __zero))
  1204. ;
  1205. else if (_Traits::eq(__c, __one))
  1206. _Unchecked_set(__i - 1);
  1207. else
  1208. __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
  1209. }
  1210. }
  1211. template<size_t _Nb>
  1212. template<class _CharT, class _Traits, class _Alloc>
  1213. void
  1214. bitset<_Nb>::
  1215. _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
  1216. _CharT __zero, _CharT __one) const
  1217. {
  1218. __s.assign(_Nb, __zero);
  1219. for (size_t __i = _Nb; __i > 0; --__i)
  1220. if (_Unchecked_test(__i - 1))
  1221. _Traits::assign(__s[_Nb - __i], __one);
  1222. }
  1223. // 23.3.5.3 bitset operations:
  1224. //@{
  1225. /**
  1226. * @brief Global bitwise operations on bitsets.
  1227. * @param __x A bitset.
  1228. * @param __y A bitset of the same size as @a __x.
  1229. * @return A new bitset.
  1230. *
  1231. * These should be self-explanatory.
  1232. */
  1233. template<size_t _Nb>
  1234. inline bitset<_Nb>
  1235. operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
  1236. {
  1237. bitset<_Nb> __result(__x);
  1238. __result &= __y;
  1239. return __result;
  1240. }
  1241. template<size_t _Nb>
  1242. inline bitset<_Nb>
  1243. operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
  1244. {
  1245. bitset<_Nb> __result(__x);
  1246. __result |= __y;
  1247. return __result;
  1248. }
  1249. template <size_t _Nb>
  1250. inline bitset<_Nb>
  1251. operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
  1252. {
  1253. bitset<_Nb> __result(__x);
  1254. __result ^= __y;
  1255. return __result;
  1256. }
  1257. //@}
  1258. //@{
  1259. /**
  1260. * @brief Global I/O operators for bitsets.
  1261. *
  1262. * Direct I/O between streams and bitsets is supported. Output is
  1263. * straightforward. Input will skip whitespace, only accept @a 0 and @a 1
  1264. * characters, and will only extract as many digits as the %bitset will
  1265. * hold.
  1266. */
  1267. template<class _CharT, class _Traits, size_t _Nb>
  1268. std::basic_istream<_CharT, _Traits>&
  1269. operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
  1270. {
  1271. typedef typename _Traits::char_type char_type;
  1272. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  1273. typedef typename __istream_type::ios_base __ios_base;
  1274. std::basic_string<_CharT, _Traits> __tmp;
  1275. __tmp.reserve(_Nb);
  1276. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1277. // 303. Bitset input operator underspecified
  1278. const char_type __zero = __is.widen('0');
  1279. const char_type __one = __is.widen('1');
  1280. typename __ios_base::iostate __state = __ios_base::goodbit;
  1281. typename __istream_type::sentry __sentry(__is);
  1282. if (__sentry)
  1283. {
  1284. __try
  1285. {
  1286. for (size_t __i = _Nb; __i > 0; --__i)
  1287. {
  1288. static typename _Traits::int_type __eof = _Traits::eof();
  1289. typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
  1290. if (_Traits::eq_int_type(__c1, __eof))
  1291. {
  1292. __state |= __ios_base::eofbit;
  1293. break;
  1294. }
  1295. else
  1296. {
  1297. const char_type __c2 = _Traits::to_char_type(__c1);
  1298. if (_Traits::eq(__c2, __zero))
  1299. __tmp.push_back(__zero);
  1300. else if (_Traits::eq(__c2, __one))
  1301. __tmp.push_back(__one);
  1302. else if (_Traits::
  1303. eq_int_type(__is.rdbuf()->sputbackc(__c2),
  1304. __eof))
  1305. {
  1306. __state |= __ios_base::failbit;
  1307. break;
  1308. }
  1309. }
  1310. }
  1311. }
  1312. __catch(__cxxabiv1::__forced_unwind&)
  1313. {
  1314. __is._M_setstate(__ios_base::badbit);
  1315. __throw_exception_again;
  1316. }
  1317. __catch(...)
  1318. { __is._M_setstate(__ios_base::badbit); }
  1319. }
  1320. if (__tmp.empty() && _Nb)
  1321. __state |= __ios_base::failbit;
  1322. else
  1323. __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb,
  1324. __zero, __one);
  1325. if (__state)
  1326. __is.setstate(__state);
  1327. return __is;
  1328. }
  1329. template <class _CharT, class _Traits, size_t _Nb>
  1330. std::basic_ostream<_CharT, _Traits>&
  1331. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1332. const bitset<_Nb>& __x)
  1333. {
  1334. std::basic_string<_CharT, _Traits> __tmp;
  1335. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1336. // 396. what are characters zero and one.
  1337. const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
  1338. __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
  1339. return __os << __tmp;
  1340. }
  1341. //@}
  1342. _GLIBCXX_END_NAMESPACE_CONTAINER
  1343. } // namespace std
  1344. #undef _GLIBCXX_BITSET_WORDS
  1345. #undef _GLIBCXX_BITSET_BITS_PER_WORD
  1346. #undef _GLIBCXX_BITSET_BITS_PER_ULL
  1347. #if __cplusplus >= 201103L
  1348. #include <bits/functional_hash.h>
  1349. namespace std _GLIBCXX_VISIBILITY(default)
  1350. {
  1351. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  1352. // DR 1182.
  1353. /// std::hash specialization for bitset.
  1354. template<size_t _Nb>
  1355. struct hash<_GLIBCXX_STD_C::bitset<_Nb>>
  1356. : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<_Nb>>
  1357. {
  1358. size_t
  1359. operator()(const _GLIBCXX_STD_C::bitset<_Nb>& __b) const noexcept
  1360. {
  1361. const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
  1362. return std::_Hash_impl::hash(__b._M_getdata(), __clength);
  1363. }
  1364. };
  1365. template<>
  1366. struct hash<_GLIBCXX_STD_C::bitset<0>>
  1367. : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<0>>
  1368. {
  1369. size_t
  1370. operator()(const _GLIBCXX_STD_C::bitset<0>&) const noexcept
  1371. { return 0; }
  1372. };
  1373. _GLIBCXX_END_NAMESPACE_VERSION
  1374. } // namespace
  1375. #endif // C++11
  1376. #ifdef _GLIBCXX_DEBUG
  1377. # include <debug/bitset>
  1378. #endif
  1379. #ifdef _GLIBCXX_PROFILE
  1380. # include <profile/bitset>
  1381. #endif
  1382. #endif /* _GLIBCXX_BITSET */