bitset 45 KB

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