bitset 41 KB

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