bitset 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_BITSET
  10. #define _LIBCPP_BITSET
  11. /*
  12. bitset synopsis
  13. namespace std
  14. {
  15. namespace std {
  16. template <size_t N>
  17. class bitset
  18. {
  19. public:
  20. // bit reference:
  21. class reference
  22. {
  23. friend class bitset;
  24. reference() noexcept;
  25. public:
  26. ~reference() noexcept;
  27. reference& operator=(bool x) noexcept; // for b[i] = x;
  28. reference& operator=(const reference&) noexcept; // for b[i] = b[j];
  29. bool operator~() const noexcept; // flips the bit
  30. operator bool() const noexcept; // for x = b[i];
  31. reference& flip() noexcept; // for b[i].flip();
  32. };
  33. // 23.3.5.1 constructors:
  34. constexpr bitset() noexcept;
  35. constexpr bitset(unsigned long long val) noexcept;
  36. template <class charT>
  37. constexpr explicit bitset(const charT* str,
  38. typename basic_string<charT>::size_type n = basic_string<charT>::npos,
  39. charT zero = charT('0'), charT one = charT('1')); // until C++26, constexpr since C++23
  40. template <class charT>
  41. constexpr explicit bitset(const charT* str,
  42. typename basic_string_view<charT>::size_type n = basic_string_view<charT>::npos,
  43. charT zero = charT('0'), charT one = charT('1')); // since C++26
  44. template<class charT, class traits>
  45. explicit bitset(
  46. const basic_string_view<charT,traits>& str,
  47. typename basic_string_view<charT,traits>::size_type pos = 0,
  48. typename basic_string_view<charT,traits>::size_type n = basic_string_view<charT,traits>::npos,
  49. charT zero = charT('0'), charT one = charT('1')); // since C++26
  50. template<class charT, class traits, class Allocator>
  51. constexpr explicit bitset(
  52. const basic_string<charT,traits,Allocator>& str,
  53. typename basic_string<charT,traits,Allocator>::size_type pos = 0,
  54. typename basic_string<charT,traits,Allocator>::size_type n = basic_string<charT,traits,Allocator>::npos,
  55. charT zero = charT('0'), charT one = charT('1')); // constexpr since C++23
  56. // 23.3.5.2 bitset operations:
  57. bitset& operator&=(const bitset& rhs) noexcept; // constexpr since C++23
  58. bitset& operator|=(const bitset& rhs) noexcept; // constexpr since C++23
  59. bitset& operator^=(const bitset& rhs) noexcept; // constexpr since C++23
  60. bitset& operator<<=(size_t pos) noexcept; // constexpr since C++23
  61. bitset& operator>>=(size_t pos) noexcept; // constexpr since C++23
  62. bitset& set() noexcept; // constexpr since C++23
  63. bitset& set(size_t pos, bool val = true); // constexpr since C++23
  64. bitset& reset() noexcept; // constexpr since C++23
  65. bitset& reset(size_t pos); // constexpr since C++23
  66. bitset operator~() const noexcept; // constexpr since C++23
  67. bitset& flip() noexcept; // constexpr since C++23
  68. bitset& flip(size_t pos); // constexpr since C++23
  69. // element access:
  70. constexpr bool operator[](size_t pos) const;
  71. reference operator[](size_t pos); // constexpr since C++23
  72. unsigned long to_ulong() const; // constexpr since C++23
  73. unsigned long long to_ullong() const; // constexpr since C++23
  74. template <class charT, class traits, class Allocator> // constexpr since C++23
  75. basic_string<charT, traits, Allocator> to_string(charT zero = charT('0'), charT one = charT('1')) const;
  76. template <class charT, class traits> // constexpr since C++23
  77. basic_string<charT, traits, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;
  78. template <class charT> // constexpr since C++23
  79. basic_string<charT, char_traits<charT>, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;
  80. basic_string<char, char_traits<char>, allocator<char> > to_string(char zero = '0', char one = '1') const; // constexpr since C++23
  81. size_t count() const noexcept; // constexpr since C++23
  82. constexpr size_t size() const noexcept; // constexpr since C++23
  83. bool operator==(const bitset& rhs) const noexcept; // constexpr since C++23
  84. bool operator!=(const bitset& rhs) const noexcept; // removed in C++20
  85. bool test(size_t pos) const; // constexpr since C++23
  86. bool all() const noexcept; // constexpr since C++23
  87. bool any() const noexcept; // constexpr since C++23
  88. bool none() const noexcept; // constexpr since C++23
  89. bitset<N> operator<<(size_t pos) const noexcept; // constexpr since C++23
  90. bitset<N> operator>>(size_t pos) const noexcept; // constexpr since C++23
  91. };
  92. // 23.3.5.3 bitset operators:
  93. template <size_t N>
  94. bitset<N> operator&(const bitset<N>&, const bitset<N>&) noexcept; // constexpr since C++23
  95. template <size_t N>
  96. bitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept; // constexpr since C++23
  97. template <size_t N>
  98. bitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept; // constexpr since C++23
  99. template <class charT, class traits, size_t N>
  100. basic_istream<charT, traits>&
  101. operator>>(basic_istream<charT, traits>& is, bitset<N>& x);
  102. template <class charT, class traits, size_t N>
  103. basic_ostream<charT, traits>&
  104. operator<<(basic_ostream<charT, traits>& os, const bitset<N>& x);
  105. template <size_t N> struct hash<std::bitset<N>>;
  106. } // std
  107. */
  108. #include <__algorithm/count.h>
  109. #include <__algorithm/fill.h>
  110. #include <__algorithm/find.h>
  111. #include <__assert> // all public C++ headers provide the assertion handler
  112. #include <__bit_reference>
  113. #include <__config>
  114. #include <__functional/hash.h>
  115. #include <__functional/unary_function.h>
  116. #include <__type_traits/is_char_like_type.h>
  117. #include <climits>
  118. #include <cstddef>
  119. #include <stdexcept>
  120. #include <string_view>
  121. #include <version>
  122. // standard-mandated includes
  123. // [bitset.syn]
  124. #include <iosfwd>
  125. #include <string>
  126. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  127. # pragma GCC system_header
  128. #endif
  129. _LIBCPP_PUSH_MACROS
  130. #include <__undef_macros>
  131. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  132. #pragma GCC system_header
  133. #endif
  134. _LIBCPP_BEGIN_NAMESPACE_STD
  135. template <size_t _N_words, size_t _Size>
  136. class __bitset;
  137. template <size_t _N_words, size_t _Size>
  138. struct __has_storage_type<__bitset<_N_words, _Size> >
  139. {
  140. static const bool value = true;
  141. };
  142. template <size_t _N_words, size_t _Size>
  143. class __bitset
  144. {
  145. public:
  146. typedef ptrdiff_t difference_type;
  147. typedef size_t size_type;
  148. typedef size_type __storage_type;
  149. protected:
  150. typedef __bitset __self;
  151. typedef __storage_type* __storage_pointer;
  152. typedef const __storage_type* __const_storage_pointer;
  153. static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
  154. friend class __bit_reference<__bitset>;
  155. friend class __bit_const_reference<__bitset>;
  156. friend class __bit_iterator<__bitset, false>;
  157. friend class __bit_iterator<__bitset, true>;
  158. friend struct __bit_array<__bitset>;
  159. __storage_type __first_[_N_words];
  160. typedef __bit_reference<__bitset> reference;
  161. typedef __bit_const_reference<__bitset> const_reference;
  162. typedef __bit_iterator<__bitset, false> iterator;
  163. typedef __bit_iterator<__bitset, true> const_iterator;
  164. _LIBCPP_INLINE_VISIBILITY
  165. _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
  166. _LIBCPP_INLINE_VISIBILITY
  167. explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
  168. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 reference __make_ref(size_t __pos) _NOEXCEPT
  169. {return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
  170. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
  171. {return const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
  172. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 iterator __make_iter(size_t __pos) _NOEXCEPT
  173. {return iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
  174. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 const_iterator __make_iter(size_t __pos) const _NOEXCEPT
  175. {return const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
  176. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  177. void operator&=(const __bitset& __v) _NOEXCEPT;
  178. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  179. void operator|=(const __bitset& __v) _NOEXCEPT;
  180. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  181. void operator^=(const __bitset& __v) _NOEXCEPT;
  182. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT;
  183. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const
  184. {return to_ulong(integral_constant<bool, _Size < sizeof(unsigned long) * CHAR_BIT>());}
  185. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const
  186. {return to_ullong(integral_constant<bool, _Size < sizeof(unsigned long long) * CHAR_BIT>());}
  187. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT;
  188. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT;
  189. _LIBCPP_INLINE_VISIBILITY
  190. size_t __hash_code() const _NOEXCEPT;
  191. private:
  192. #ifdef _LIBCPP_CXX03_LANG
  193. void __init(unsigned long long __v, false_type) _NOEXCEPT;
  194. _LIBCPP_INLINE_VISIBILITY
  195. void __init(unsigned long long __v, true_type) _NOEXCEPT;
  196. #endif // _LIBCPP_CXX03_LANG
  197. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  198. unsigned long to_ulong(false_type) const;
  199. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  200. unsigned long to_ulong(true_type) const;
  201. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  202. unsigned long long to_ullong(false_type) const;
  203. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  204. unsigned long long to_ullong(true_type) const;
  205. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  206. unsigned long long to_ullong(true_type, false_type) const;
  207. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  208. unsigned long long to_ullong(true_type, true_type) const;
  209. };
  210. template <size_t _N_words, size_t _Size>
  211. inline
  212. _LIBCPP_CONSTEXPR
  213. __bitset<_N_words, _Size>::__bitset() _NOEXCEPT
  214. #ifndef _LIBCPP_CXX03_LANG
  215. : __first_{0}
  216. #endif
  217. {
  218. #ifdef _LIBCPP_CXX03_LANG
  219. _VSTD::fill_n(__first_, _N_words, __storage_type(0));
  220. #endif
  221. }
  222. #ifdef _LIBCPP_CXX03_LANG
  223. template <size_t _N_words, size_t _Size>
  224. void
  225. __bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT
  226. {
  227. __storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)];
  228. size_t __sz = _Size;
  229. for (size_t __i = 0; __i < sizeof(__t)/sizeof(__t[0]); ++__i, __v >>= __bits_per_word, __sz -= __bits_per_word )
  230. if ( __sz < __bits_per_word)
  231. __t[__i] = static_cast<__storage_type>(__v) & ( 1ULL << __sz ) - 1;
  232. else
  233. __t[__i] = static_cast<__storage_type>(__v);
  234. _VSTD::copy(__t, __t + sizeof(__t)/sizeof(__t[0]), __first_);
  235. _VSTD::fill(__first_ + sizeof(__t)/sizeof(__t[0]), __first_ + sizeof(__first_)/sizeof(__first_[0]),
  236. __storage_type(0));
  237. }
  238. template <size_t _N_words, size_t _Size>
  239. inline _LIBCPP_INLINE_VISIBILITY
  240. void
  241. __bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT
  242. {
  243. __first_[0] = __v;
  244. if (_Size < __bits_per_word)
  245. __first_[0] &= ( 1ULL << _Size ) - 1;
  246. _VSTD::fill(__first_ + 1, __first_ + sizeof(__first_)/sizeof(__first_[0]), __storage_type(0));
  247. }
  248. #endif // _LIBCPP_CXX03_LANG
  249. template <size_t _N_words, size_t _Size>
  250. inline
  251. _LIBCPP_CONSTEXPR
  252. __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
  253. #ifndef _LIBCPP_CXX03_LANG
  254. #if SIZE_MAX == 0xffffffffffffffffULL
  255. : __first_{__v}
  256. #elif SIZE_MAX == 0xffffffffULL
  257. : __first_{static_cast<__storage_type>(__v),
  258. _Size >= 2 * __bits_per_word ? static_cast<__storage_type>(__v >> __bits_per_word)
  259. : static_cast<__storage_type>((__v >> __bits_per_word) & (__storage_type(1) << (_Size - __bits_per_word)) - 1)}
  260. #else
  261. #error This constructor has not been ported to this platform
  262. #endif
  263. #endif
  264. {
  265. #ifdef _LIBCPP_CXX03_LANG
  266. __init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>());
  267. #endif
  268. }
  269. template <size_t _N_words, size_t _Size>
  270. inline
  271. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
  272. __bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
  273. {
  274. for (size_type __i = 0; __i < _N_words; ++__i)
  275. __first_[__i] &= __v.__first_[__i];
  276. }
  277. template <size_t _N_words, size_t _Size>
  278. inline
  279. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
  280. __bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
  281. {
  282. for (size_type __i = 0; __i < _N_words; ++__i)
  283. __first_[__i] |= __v.__first_[__i];
  284. }
  285. template <size_t _N_words, size_t _Size>
  286. inline
  287. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
  288. __bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
  289. {
  290. for (size_type __i = 0; __i < _N_words; ++__i)
  291. __first_[__i] ^= __v.__first_[__i];
  292. }
  293. template <size_t _N_words, size_t _Size>
  294. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
  295. __bitset<_N_words, _Size>::flip() _NOEXCEPT
  296. {
  297. // do middle whole words
  298. size_type __n = _Size;
  299. __storage_pointer __p = __first_;
  300. for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
  301. *__p = ~*__p;
  302. // do last partial word
  303. if (__n > 0)
  304. {
  305. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
  306. __storage_type __b = *__p & __m;
  307. *__p &= ~__m;
  308. *__p |= ~__b & __m;
  309. }
  310. }
  311. template <size_t _N_words, size_t _Size>
  312. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
  313. __bitset<_N_words, _Size>::to_ulong(false_type) const
  314. {
  315. const_iterator __e = __make_iter(_Size);
  316. const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true);
  317. if (__i != __e)
  318. __throw_overflow_error("bitset to_ulong overflow error");
  319. return __first_[0];
  320. }
  321. template <size_t _N_words, size_t _Size>
  322. inline
  323. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
  324. __bitset<_N_words, _Size>::to_ulong(true_type) const
  325. {
  326. return __first_[0];
  327. }
  328. template <size_t _N_words, size_t _Size>
  329. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
  330. __bitset<_N_words, _Size>::to_ullong(false_type) const
  331. {
  332. const_iterator __e = __make_iter(_Size);
  333. const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true);
  334. if (__i != __e)
  335. __throw_overflow_error("bitset to_ullong overflow error");
  336. return to_ullong(true_type());
  337. }
  338. template <size_t _N_words, size_t _Size>
  339. inline
  340. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
  341. __bitset<_N_words, _Size>::to_ullong(true_type) const
  342. {
  343. return to_ullong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long long)>());
  344. }
  345. template <size_t _N_words, size_t _Size>
  346. inline
  347. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
  348. __bitset<_N_words, _Size>::to_ullong(true_type, false_type) const
  349. {
  350. return __first_[0];
  351. }
  352. template <size_t _N_words, size_t _Size>
  353. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
  354. __bitset<_N_words, _Size>::to_ullong(true_type, true_type) const
  355. {
  356. unsigned long long __r = __first_[0];
  357. for (size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i)
  358. __r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT);
  359. return __r;
  360. }
  361. template <size_t _N_words, size_t _Size>
  362. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool
  363. __bitset<_N_words, _Size>::all() const _NOEXCEPT
  364. {
  365. // do middle whole words
  366. size_type __n = _Size;
  367. __const_storage_pointer __p = __first_;
  368. for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
  369. if (~*__p)
  370. return false;
  371. // do last partial word
  372. if (__n > 0)
  373. {
  374. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
  375. if (~*__p & __m)
  376. return false;
  377. }
  378. return true;
  379. }
  380. template <size_t _N_words, size_t _Size>
  381. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool
  382. __bitset<_N_words, _Size>::any() const _NOEXCEPT
  383. {
  384. // do middle whole words
  385. size_type __n = _Size;
  386. __const_storage_pointer __p = __first_;
  387. for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
  388. if (*__p)
  389. return true;
  390. // do last partial word
  391. if (__n > 0)
  392. {
  393. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
  394. if (*__p & __m)
  395. return true;
  396. }
  397. return false;
  398. }
  399. template <size_t _N_words, size_t _Size>
  400. inline
  401. size_t
  402. __bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT
  403. {
  404. size_t __h = 0;
  405. for (size_type __i = 0; __i < _N_words; ++__i)
  406. __h ^= __first_[__i];
  407. return __h;
  408. }
  409. template <size_t _Size>
  410. class __bitset<1, _Size>
  411. {
  412. public:
  413. typedef ptrdiff_t difference_type;
  414. typedef size_t size_type;
  415. typedef size_type __storage_type;
  416. protected:
  417. typedef __bitset __self;
  418. typedef __storage_type* __storage_pointer;
  419. typedef const __storage_type* __const_storage_pointer;
  420. static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
  421. friend class __bit_reference<__bitset>;
  422. friend class __bit_const_reference<__bitset>;
  423. friend class __bit_iterator<__bitset, false>;
  424. friend class __bit_iterator<__bitset, true>;
  425. friend struct __bit_array<__bitset>;
  426. __storage_type __first_;
  427. typedef __bit_reference<__bitset> reference;
  428. typedef __bit_const_reference<__bitset> const_reference;
  429. typedef __bit_iterator<__bitset, false> iterator;
  430. typedef __bit_iterator<__bitset, true> const_iterator;
  431. _LIBCPP_INLINE_VISIBILITY
  432. _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
  433. _LIBCPP_INLINE_VISIBILITY
  434. explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
  435. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 reference __make_ref(size_t __pos) _NOEXCEPT
  436. {return reference(&__first_, __storage_type(1) << __pos);}
  437. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
  438. {return const_reference(&__first_, __storage_type(1) << __pos);}
  439. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 iterator __make_iter(size_t __pos) _NOEXCEPT
  440. {return iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
  441. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 const_iterator __make_iter(size_t __pos) const _NOEXCEPT
  442. {return const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
  443. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  444. void operator&=(const __bitset& __v) _NOEXCEPT;
  445. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  446. void operator|=(const __bitset& __v) _NOEXCEPT;
  447. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  448. void operator^=(const __bitset& __v) _NOEXCEPT;
  449. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  450. void flip() _NOEXCEPT;
  451. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  452. unsigned long to_ulong() const;
  453. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  454. unsigned long long to_ullong() const;
  455. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  456. bool all() const _NOEXCEPT;
  457. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  458. bool any() const _NOEXCEPT;
  459. _LIBCPP_INLINE_VISIBILITY
  460. size_t __hash_code() const _NOEXCEPT;
  461. };
  462. template <size_t _Size>
  463. inline
  464. _LIBCPP_CONSTEXPR
  465. __bitset<1, _Size>::__bitset() _NOEXCEPT
  466. : __first_(0)
  467. {
  468. }
  469. template <size_t _Size>
  470. inline
  471. _LIBCPP_CONSTEXPR
  472. __bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
  473. : __first_(
  474. _Size == __bits_per_word ? static_cast<__storage_type>(__v)
  475. : static_cast<__storage_type>(__v) & ((__storage_type(1) << _Size) - 1)
  476. )
  477. {
  478. }
  479. template <size_t _Size>
  480. inline
  481. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
  482. __bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
  483. {
  484. __first_ &= __v.__first_;
  485. }
  486. template <size_t _Size>
  487. inline
  488. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
  489. __bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
  490. {
  491. __first_ |= __v.__first_;
  492. }
  493. template <size_t _Size>
  494. inline
  495. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
  496. __bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
  497. {
  498. __first_ ^= __v.__first_;
  499. }
  500. template <size_t _Size>
  501. inline
  502. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
  503. __bitset<1, _Size>::flip() _NOEXCEPT
  504. {
  505. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
  506. __first_ = ~__first_;
  507. __first_ &= __m;
  508. }
  509. template <size_t _Size>
  510. inline
  511. _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
  512. __bitset<1, _Size>::to_ulong() const
  513. {
  514. return __first_;
  515. }
  516. template <size_t _Size>
  517. inline
  518. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
  519. __bitset<1, _Size>::to_ullong() const
  520. {
  521. return __first_;
  522. }
  523. template <size_t _Size>
  524. inline
  525. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool
  526. __bitset<1, _Size>::all() const _NOEXCEPT
  527. {
  528. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
  529. return !(~__first_ & __m);
  530. }
  531. template <size_t _Size>
  532. inline
  533. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool
  534. __bitset<1, _Size>::any() const _NOEXCEPT
  535. {
  536. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
  537. return __first_ & __m;
  538. }
  539. template <size_t _Size>
  540. inline
  541. size_t
  542. __bitset<1, _Size>::__hash_code() const _NOEXCEPT
  543. {
  544. return __first_;
  545. }
  546. template <>
  547. class __bitset<0, 0>
  548. {
  549. public:
  550. typedef ptrdiff_t difference_type;
  551. typedef size_t size_type;
  552. typedef size_type __storage_type;
  553. protected:
  554. typedef __bitset __self;
  555. typedef __storage_type* __storage_pointer;
  556. typedef const __storage_type* __const_storage_pointer;
  557. static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
  558. friend class __bit_reference<__bitset>;
  559. friend class __bit_const_reference<__bitset>;
  560. friend class __bit_iterator<__bitset, false>;
  561. friend class __bit_iterator<__bitset, true>;
  562. friend struct __bit_array<__bitset>;
  563. typedef __bit_reference<__bitset> reference;
  564. typedef __bit_const_reference<__bitset> const_reference;
  565. typedef __bit_iterator<__bitset, false> iterator;
  566. typedef __bit_iterator<__bitset, true> const_iterator;
  567. _LIBCPP_INLINE_VISIBILITY
  568. _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
  569. _LIBCPP_INLINE_VISIBILITY
  570. explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long) _NOEXCEPT;
  571. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 reference __make_ref(size_t) _NOEXCEPT
  572. {return reference(nullptr, 1);}
  573. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t) const _NOEXCEPT
  574. {return const_reference(nullptr, 1);}
  575. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 iterator __make_iter(size_t) _NOEXCEPT
  576. {return iterator(nullptr, 0);}
  577. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 const_iterator __make_iter(size_t) const _NOEXCEPT
  578. {return const_iterator(nullptr, 0);}
  579. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator&=(const __bitset&) _NOEXCEPT {}
  580. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator|=(const __bitset&) _NOEXCEPT {}
  581. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator^=(const __bitset&) _NOEXCEPT {}
  582. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT {}
  583. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const {return 0;}
  584. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const {return 0;}
  585. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT {return true;}
  586. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT {return false;}
  587. _LIBCPP_INLINE_VISIBILITY size_t __hash_code() const _NOEXCEPT {return 0;}
  588. };
  589. inline
  590. _LIBCPP_CONSTEXPR
  591. __bitset<0, 0>::__bitset() _NOEXCEPT
  592. {
  593. }
  594. inline
  595. _LIBCPP_CONSTEXPR
  596. __bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT
  597. {
  598. }
  599. template <size_t _Size> class _LIBCPP_TEMPLATE_VIS bitset;
  600. template <size_t _Size> struct hash<bitset<_Size> >;
  601. template <size_t _Size>
  602. class _LIBCPP_TEMPLATE_VIS bitset
  603. : private __bitset<_Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1, _Size>
  604. {
  605. public:
  606. static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1;
  607. typedef __bitset<__n_words, _Size> base;
  608. public:
  609. typedef typename base::reference reference;
  610. typedef typename base::const_reference const_reference;
  611. // 23.3.5.1 constructors:
  612. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {}
  613. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  614. bitset(unsigned long long __v) _NOEXCEPT : base(__v) {}
  615. template <class _CharT, class = __enable_if_t<_IsCharLikeType<_CharT>::value> >
  616. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit bitset(
  617. const _CharT* __str,
  618. # if _LIBCPP_STD_VER >= 26
  619. typename basic_string_view<_CharT>::size_type __n = basic_string_view<_CharT>::npos,
  620. # else
  621. typename basic_string<_CharT>::size_type __n = basic_string<_CharT>::npos,
  622. # endif
  623. _CharT __zero = _CharT('0'),
  624. _CharT __one = _CharT('1')) {
  625. size_t __rlen = std::min(__n, char_traits<_CharT>::length(__str));
  626. __init_from_string_view(basic_string_view<_CharT>(__str, __rlen), __zero, __one);
  627. }
  628. #if _LIBCPP_STD_VER >= 26
  629. template <class _CharT, class _Traits>
  630. _LIBCPP_HIDE_FROM_ABI constexpr explicit bitset(
  631. basic_string_view<_CharT, _Traits> __str,
  632. typename basic_string_view<_CharT, _Traits>::size_type __pos = 0,
  633. typename basic_string_view<_CharT, _Traits>::size_type __n = basic_string_view<_CharT, _Traits>::npos,
  634. _CharT __zero = _CharT('0'),
  635. _CharT __one = _CharT('1')) {
  636. if (__pos > __str.size())
  637. __throw_out_of_range("bitset string pos out of range");
  638. size_t __rlen = std::min(__n, __str.size() - __pos);
  639. __init_from_string_view(basic_string_view<_CharT, _Traits>(__str.data() + __pos, __rlen), __zero, __one);
  640. }
  641. #endif
  642. template <class _CharT, class _Traits, class _Allocator>
  643. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit bitset(
  644. const basic_string<_CharT, _Traits, _Allocator>& __str,
  645. typename basic_string<_CharT, _Traits, _Allocator>::size_type __pos = 0,
  646. typename basic_string<_CharT, _Traits, _Allocator>::size_type __n =
  647. basic_string<_CharT, _Traits, _Allocator>::npos,
  648. _CharT __zero = _CharT('0'),
  649. _CharT __one = _CharT('1')) {
  650. if (__pos > __str.size())
  651. std::__throw_out_of_range("bitset string pos out of range");
  652. size_t __rlen = std::min(__n, __str.size() - __pos);
  653. __init_from_string_view(basic_string_view<_CharT, _Traits>(__str.data() + __pos, __rlen), __zero, __one);
  654. }
  655. // 23.3.5.2 bitset operations:
  656. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  657. bitset& operator&=(const bitset& __rhs) _NOEXCEPT;
  658. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  659. bitset& operator|=(const bitset& __rhs) _NOEXCEPT;
  660. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  661. bitset& operator^=(const bitset& __rhs) _NOEXCEPT;
  662. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  663. bitset& operator<<=(size_t __pos) _NOEXCEPT;
  664. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  665. bitset& operator>>=(size_t __pos) _NOEXCEPT;
  666. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  667. bitset& set() _NOEXCEPT;
  668. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  669. bitset& set(size_t __pos, bool __val = true);
  670. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  671. bitset& reset() _NOEXCEPT;
  672. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  673. bitset& reset(size_t __pos);
  674. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  675. bitset operator~() const _NOEXCEPT;
  676. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  677. bitset& flip() _NOEXCEPT;
  678. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  679. bitset& flip(size_t __pos);
  680. // element access:
  681. #ifdef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL
  682. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator[](size_t __p) const {return base::__make_ref(__p);}
  683. #else
  684. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference operator[](size_t __p) const {return base::__make_ref(__p);}
  685. #endif
  686. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 reference operator[](size_t __p) {return base::__make_ref(__p);}
  687. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  688. unsigned long to_ulong() const;
  689. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  690. unsigned long long to_ullong() const;
  691. template <class _CharT, class _Traits, class _Allocator>
  692. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  693. basic_string<_CharT, _Traits, _Allocator> to_string(_CharT __zero = _CharT('0'),
  694. _CharT __one = _CharT('1')) const;
  695. template <class _CharT, class _Traits>
  696. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  697. basic_string<_CharT, _Traits, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
  698. _CharT __one = _CharT('1')) const;
  699. template <class _CharT>
  700. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  701. basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
  702. _CharT __one = _CharT('1')) const;
  703. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  704. basic_string<char, char_traits<char>, allocator<char> > to_string(char __zero = '0',
  705. char __one = '1') const;
  706. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  707. size_t count() const _NOEXCEPT;
  708. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR size_t size() const _NOEXCEPT {return _Size;}
  709. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  710. bool operator==(const bitset& __rhs) const _NOEXCEPT;
  711. #if _LIBCPP_STD_VER <= 17
  712. _LIBCPP_INLINE_VISIBILITY
  713. bool operator!=(const bitset& __rhs) const _NOEXCEPT;
  714. #endif
  715. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  716. bool test(size_t __pos) const;
  717. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  718. bool all() const _NOEXCEPT;
  719. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  720. bool any() const _NOEXCEPT;
  721. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23 bool none() const _NOEXCEPT {return !any();}
  722. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  723. bitset operator<<(size_t __pos) const _NOEXCEPT;
  724. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  725. bitset operator>>(size_t __pos) const _NOEXCEPT;
  726. private:
  727. template <class _CharT, class _Traits>
  728. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
  729. __init_from_string_view(basic_string_view<_CharT, _Traits> __str, _CharT __zero, _CharT __one) {
  730. for (size_t __i = 0; __i < __str.size(); ++__i)
  731. if (!_Traits::eq(__str[__i], __zero) && !_Traits::eq(__str[__i], __one))
  732. std::__throw_invalid_argument("bitset string ctor has invalid argument");
  733. size_t __mp = std::min(__str.size(), _Size);
  734. size_t __i = 0;
  735. for (; __i < __mp; ++__i) {
  736. _CharT __c = __str[__mp - 1 - __i];
  737. (*this)[__i] = _Traits::eq(__c, __one);
  738. }
  739. std::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
  740. }
  741. _LIBCPP_INLINE_VISIBILITY
  742. size_t __hash_code() const _NOEXCEPT {return base::__hash_code();}
  743. friend struct hash<bitset>;
  744. };
  745. template <size_t _Size>
  746. inline
  747. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  748. bitset<_Size>&
  749. bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT
  750. {
  751. base::operator&=(__rhs);
  752. return *this;
  753. }
  754. template <size_t _Size>
  755. inline
  756. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  757. bitset<_Size>&
  758. bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT
  759. {
  760. base::operator|=(__rhs);
  761. return *this;
  762. }
  763. template <size_t _Size>
  764. inline
  765. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  766. bitset<_Size>&
  767. bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT
  768. {
  769. base::operator^=(__rhs);
  770. return *this;
  771. }
  772. template <size_t _Size>
  773. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  774. bitset<_Size>&
  775. bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT
  776. {
  777. __pos = _VSTD::min(__pos, _Size);
  778. _VSTD::copy_backward(base::__make_iter(0), base::__make_iter(_Size - __pos), base::__make_iter(_Size));
  779. _VSTD::fill_n(base::__make_iter(0), __pos, false);
  780. return *this;
  781. }
  782. template <size_t _Size>
  783. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  784. bitset<_Size>&
  785. bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT
  786. {
  787. __pos = _VSTD::min(__pos, _Size);
  788. _VSTD::copy(base::__make_iter(__pos), base::__make_iter(_Size), base::__make_iter(0));
  789. _VSTD::fill_n(base::__make_iter(_Size - __pos), __pos, false);
  790. return *this;
  791. }
  792. template <size_t _Size>
  793. inline
  794. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  795. bitset<_Size>&
  796. bitset<_Size>::set() _NOEXCEPT
  797. {
  798. _VSTD::fill_n(base::__make_iter(0), _Size, true);
  799. return *this;
  800. }
  801. template <size_t _Size>
  802. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  803. bitset<_Size>&
  804. bitset<_Size>::set(size_t __pos, bool __val)
  805. {
  806. if (__pos >= _Size)
  807. __throw_out_of_range("bitset set argument out of range");
  808. (*this)[__pos] = __val;
  809. return *this;
  810. }
  811. template <size_t _Size>
  812. inline
  813. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  814. bitset<_Size>&
  815. bitset<_Size>::reset() _NOEXCEPT
  816. {
  817. _VSTD::fill_n(base::__make_iter(0), _Size, false);
  818. return *this;
  819. }
  820. template <size_t _Size>
  821. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  822. bitset<_Size>&
  823. bitset<_Size>::reset(size_t __pos)
  824. {
  825. if (__pos >= _Size)
  826. __throw_out_of_range("bitset reset argument out of range");
  827. (*this)[__pos] = false;
  828. return *this;
  829. }
  830. template <size_t _Size>
  831. inline
  832. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  833. bitset<_Size>
  834. bitset<_Size>::operator~() const _NOEXCEPT
  835. {
  836. bitset __x(*this);
  837. __x.flip();
  838. return __x;
  839. }
  840. template <size_t _Size>
  841. inline
  842. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  843. bitset<_Size>&
  844. bitset<_Size>::flip() _NOEXCEPT
  845. {
  846. base::flip();
  847. return *this;
  848. }
  849. template <size_t _Size>
  850. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  851. bitset<_Size>&
  852. bitset<_Size>::flip(size_t __pos)
  853. {
  854. if (__pos >= _Size)
  855. __throw_out_of_range("bitset flip argument out of range");
  856. reference __r = base::__make_ref(__pos);
  857. __r = ~__r;
  858. return *this;
  859. }
  860. template <size_t _Size>
  861. inline
  862. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  863. unsigned long
  864. bitset<_Size>::to_ulong() const
  865. {
  866. return base::to_ulong();
  867. }
  868. template <size_t _Size>
  869. inline
  870. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  871. unsigned long long
  872. bitset<_Size>::to_ullong() const
  873. {
  874. return base::to_ullong();
  875. }
  876. template <size_t _Size>
  877. template <class _CharT, class _Traits, class _Allocator>
  878. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  879. basic_string<_CharT, _Traits, _Allocator>
  880. bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
  881. {
  882. basic_string<_CharT, _Traits, _Allocator> __r(_Size, __zero);
  883. for (size_t __i = 0; __i != _Size; ++__i)
  884. {
  885. if ((*this)[__i])
  886. __r[_Size - 1 - __i] = __one;
  887. }
  888. return __r;
  889. }
  890. template <size_t _Size>
  891. template <class _CharT, class _Traits>
  892. inline
  893. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  894. basic_string<_CharT, _Traits, allocator<_CharT> >
  895. bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
  896. {
  897. return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one);
  898. }
  899. template <size_t _Size>
  900. template <class _CharT>
  901. inline
  902. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  903. basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
  904. bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
  905. {
  906. return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one);
  907. }
  908. template <size_t _Size>
  909. inline
  910. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  911. basic_string<char, char_traits<char>, allocator<char> >
  912. bitset<_Size>::to_string(char __zero, char __one) const
  913. {
  914. return to_string<char, char_traits<char>, allocator<char> >(__zero, __one);
  915. }
  916. template <size_t _Size>
  917. inline
  918. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  919. size_t
  920. bitset<_Size>::count() const _NOEXCEPT
  921. {
  922. return static_cast<size_t>(std::count(base::__make_iter(0), base::__make_iter(_Size), true));
  923. }
  924. template <size_t _Size>
  925. inline
  926. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  927. bool
  928. bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT
  929. {
  930. return _VSTD::equal(base::__make_iter(0), base::__make_iter(_Size), __rhs.__make_iter(0));
  931. }
  932. #if _LIBCPP_STD_VER <= 17
  933. template <size_t _Size>
  934. inline
  935. _LIBCPP_HIDE_FROM_ABI
  936. bool
  937. bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT
  938. {
  939. return !(*this == __rhs);
  940. }
  941. #endif
  942. template <size_t _Size>
  943. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  944. bool
  945. bitset<_Size>::test(size_t __pos) const
  946. {
  947. if (__pos >= _Size)
  948. __throw_out_of_range("bitset test argument out of range");
  949. return (*this)[__pos];
  950. }
  951. template <size_t _Size>
  952. inline
  953. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  954. bool
  955. bitset<_Size>::all() const _NOEXCEPT
  956. {
  957. return base::all();
  958. }
  959. template <size_t _Size>
  960. inline
  961. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  962. bool
  963. bitset<_Size>::any() const _NOEXCEPT
  964. {
  965. return base::any();
  966. }
  967. template <size_t _Size>
  968. inline
  969. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  970. bitset<_Size>
  971. bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT
  972. {
  973. bitset __r = *this;
  974. __r <<= __pos;
  975. return __r;
  976. }
  977. template <size_t _Size>
  978. inline
  979. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  980. bitset<_Size>
  981. bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT
  982. {
  983. bitset __r = *this;
  984. __r >>= __pos;
  985. return __r;
  986. }
  987. template <size_t _Size>
  988. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  989. bitset<_Size>
  990. operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
  991. {
  992. bitset<_Size> __r = __x;
  993. __r &= __y;
  994. return __r;
  995. }
  996. template <size_t _Size>
  997. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  998. bitset<_Size>
  999. operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
  1000. {
  1001. bitset<_Size> __r = __x;
  1002. __r |= __y;
  1003. return __r;
  1004. }
  1005. template <size_t _Size>
  1006. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
  1007. bitset<_Size>
  1008. operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
  1009. {
  1010. bitset<_Size> __r = __x;
  1011. __r ^= __y;
  1012. return __r;
  1013. }
  1014. template <size_t _Size>
  1015. struct _LIBCPP_TEMPLATE_VIS hash<bitset<_Size> >
  1016. : public __unary_function<bitset<_Size>, size_t>
  1017. {
  1018. _LIBCPP_INLINE_VISIBILITY
  1019. size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT
  1020. {return __bs.__hash_code();}
  1021. };
  1022. template <class _CharT, class _Traits, size_t _Size>
  1023. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  1024. operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x);
  1025. template <class _CharT, class _Traits, size_t _Size>
  1026. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  1027. operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x);
  1028. _LIBCPP_END_NAMESPACE_STD
  1029. _LIBCPP_POP_MACROS
  1030. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  1031. # include <concepts>
  1032. # include <cstdlib>
  1033. # include <type_traits>
  1034. #endif
  1035. #endif // _LIBCPP_BITSET