bitset 49 KB

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