bitset 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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. // clang-format off
  12. /*
  13. bitset synopsis
  14. namespace std
  15. {
  16. namespace std {
  17. template <size_t N>
  18. class bitset
  19. {
  20. public:
  21. // bit reference:
  22. class reference
  23. {
  24. friend class bitset;
  25. reference() noexcept;
  26. public:
  27. ~reference() noexcept;
  28. reference& operator=(bool x) noexcept; // for b[i] = x;
  29. reference& operator=(const reference&) noexcept; // for b[i] = b[j];
  30. bool operator~() const noexcept; // flips the bit
  31. operator bool() const noexcept; // for x = b[i];
  32. reference& flip() noexcept; // for b[i].flip();
  33. };
  34. // 23.3.5.1 constructors:
  35. constexpr bitset() noexcept;
  36. constexpr bitset(unsigned long long val) noexcept;
  37. template <class charT>
  38. constexpr explicit bitset(const charT* str,
  39. typename basic_string<charT>::size_type n = basic_string<charT>::npos,
  40. charT zero = charT('0'), charT one = charT('1')); // until C++26, constexpr since C++23
  41. template <class charT>
  42. constexpr explicit bitset(const charT* str,
  43. typename basic_string_view<charT>::size_type n = basic_string_view<charT>::npos,
  44. charT zero = charT('0'), charT one = charT('1')); // since C++26
  45. template<class charT, class traits>
  46. explicit bitset(
  47. const basic_string_view<charT,traits>& str,
  48. typename basic_string_view<charT,traits>::size_type pos = 0,
  49. typename basic_string_view<charT,traits>::size_type n = basic_string_view<charT,traits>::npos,
  50. charT zero = charT('0'), charT one = charT('1')); // since C++26
  51. template<class charT, class traits, class Allocator>
  52. constexpr explicit bitset(
  53. const basic_string<charT,traits,Allocator>& str,
  54. typename basic_string<charT,traits,Allocator>::size_type pos = 0,
  55. typename basic_string<charT,traits,Allocator>::size_type n = basic_string<charT,traits,Allocator>::npos,
  56. charT zero = charT('0'), charT one = charT('1')); // constexpr since C++23
  57. // 23.3.5.2 bitset operations:
  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^=(const bitset& rhs) noexcept; // constexpr since C++23
  61. bitset& operator<<=(size_t pos) noexcept; // constexpr since C++23
  62. bitset& operator>>=(size_t pos) noexcept; // constexpr since C++23
  63. bitset& set() noexcept; // constexpr since C++23
  64. bitset& set(size_t pos, bool val = true); // constexpr since C++23
  65. bitset& reset() noexcept; // constexpr since C++23
  66. bitset& reset(size_t pos); // constexpr since C++23
  67. bitset operator~() const noexcept; // constexpr since C++23
  68. bitset& flip() noexcept; // constexpr since C++23
  69. bitset& flip(size_t pos); // constexpr since C++23
  70. // element access:
  71. constexpr bool operator[](size_t pos) const;
  72. reference operator[](size_t pos); // constexpr since C++23
  73. unsigned long to_ulong() const; // constexpr since C++23
  74. unsigned long long to_ullong() const; // constexpr since C++23
  75. template <class charT, class traits, class Allocator> // constexpr since C++23
  76. basic_string<charT, traits, Allocator> to_string(charT zero = charT('0'), charT one = charT('1')) const;
  77. template <class charT, class traits> // constexpr since C++23
  78. basic_string<charT, traits, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;
  79. template <class charT> // constexpr since C++23
  80. basic_string<charT, char_traits<charT>, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;
  81. basic_string<char, char_traits<char>, allocator<char> > to_string(char zero = '0', char one = '1') const; // constexpr since C++23
  82. size_t count() const noexcept; // constexpr since C++23
  83. constexpr size_t size() const noexcept; // constexpr since C++23
  84. bool operator==(const bitset& rhs) const noexcept; // constexpr since C++23
  85. bool operator!=(const bitset& rhs) const noexcept; // removed in C++20
  86. bool test(size_t pos) const; // constexpr since C++23
  87. bool all() const noexcept; // constexpr since C++23
  88. bool any() const noexcept; // constexpr since C++23
  89. bool none() const noexcept; // constexpr since C++23
  90. bitset<N> operator<<(size_t pos) const noexcept; // constexpr since C++23
  91. bitset<N> operator>>(size_t pos) const noexcept; // constexpr since C++23
  92. };
  93. // 23.3.5.3 bitset operators:
  94. template <size_t N>
  95. bitset<N> operator&(const bitset<N>&, const bitset<N>&) noexcept; // constexpr since C++23
  96. template <size_t N>
  97. bitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept; // constexpr since C++23
  98. template <size_t N>
  99. bitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept; // constexpr since C++23
  100. template <class charT, class traits, size_t N>
  101. basic_istream<charT, traits>&
  102. operator>>(basic_istream<charT, traits>& is, bitset<N>& x);
  103. template <class charT, class traits, size_t N>
  104. basic_ostream<charT, traits>&
  105. operator<<(basic_ostream<charT, traits>& os, const bitset<N>& x);
  106. template <size_t N> struct hash<std::bitset<N>>;
  107. } // std
  108. */
  109. // clang-format on
  110. #include <__algorithm/count.h>
  111. #include <__algorithm/fill.h>
  112. #include <__algorithm/find.h>
  113. #include <__bit_reference>
  114. #include <__config>
  115. #include <__functional/hash.h>
  116. #include <__functional/unary_function.h>
  117. #include <__type_traits/is_char_like_type.h>
  118. #include <climits>
  119. #include <cstddef>
  120. #include <stdexcept>
  121. #include <string_view>
  122. #include <version>
  123. // standard-mandated includes
  124. // [bitset.syn]
  125. #include <iosfwd>
  126. #include <string>
  127. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  128. # pragma GCC system_header
  129. #endif
  130. _LIBCPP_PUSH_MACROS
  131. #include <__undef_macros>
  132. _LIBCPP_BEGIN_NAMESPACE_STD
  133. template <size_t _N_words, size_t _Size>
  134. class __bitset;
  135. template <size_t _N_words, size_t _Size>
  136. struct __has_storage_type<__bitset<_N_words, _Size> > {
  137. static const bool value = true;
  138. };
  139. template <size_t _N_words, size_t _Size>
  140. class __bitset {
  141. public:
  142. typedef ptrdiff_t difference_type;
  143. typedef size_t size_type;
  144. typedef size_type __storage_type;
  145. protected:
  146. typedef __bitset __self;
  147. typedef __storage_type* __storage_pointer;
  148. typedef const __storage_type* __const_storage_pointer;
  149. static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
  150. friend class __bit_reference<__bitset>;
  151. friend class __bit_const_reference<__bitset>;
  152. friend class __bit_iterator<__bitset, false>;
  153. friend class __bit_iterator<__bitset, true>;
  154. friend struct __bit_array<__bitset>;
  155. __storage_type __first_[_N_words];
  156. typedef __bit_reference<__bitset> reference;
  157. typedef __bit_const_reference<__bitset> const_reference;
  158. typedef __bit_iterator<__bitset, false> iterator;
  159. typedef __bit_iterator<__bitset, true> const_iterator;
  160. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
  161. _LIBCPP_HIDE_FROM_ABI explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
  162. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 reference __make_ref(size_t __pos) _NOEXCEPT {
  163. return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);
  164. }
  165. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT {
  166. return const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);
  167. }
  168. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 iterator __make_iter(size_t __pos) _NOEXCEPT {
  169. return iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);
  170. }
  171. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 const_iterator __make_iter(size_t __pos) const _NOEXCEPT {
  172. return const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);
  173. }
  174. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator&=(const __bitset& __v) _NOEXCEPT;
  175. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator|=(const __bitset& __v) _NOEXCEPT;
  176. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator^=(const __bitset& __v) _NOEXCEPT;
  177. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT;
  178. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const {
  179. return to_ulong(integral_constant < bool, _Size< sizeof(unsigned long) * CHAR_BIT>());
  180. }
  181. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const {
  182. return to_ullong(integral_constant < bool, _Size< sizeof(unsigned long long) * CHAR_BIT>());
  183. }
  184. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT;
  185. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT;
  186. _LIBCPP_HIDE_FROM_ABI size_t __hash_code() const _NOEXCEPT;
  187. private:
  188. #ifdef _LIBCPP_CXX03_LANG
  189. void __init(unsigned long long __v, false_type) _NOEXCEPT;
  190. _LIBCPP_HIDE_FROM_ABI void __init(unsigned long long __v, true_type) _NOEXCEPT;
  191. #endif // _LIBCPP_CXX03_LANG
  192. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong(false_type) const;
  193. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong(true_type) const;
  194. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong(false_type) const;
  195. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong(true_type) const;
  196. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong(true_type, false_type) const;
  197. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong(true_type, true_type) const;
  198. };
  199. template <size_t _N_words, size_t _Size>
  200. inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset() _NOEXCEPT
  201. #ifndef _LIBCPP_CXX03_LANG
  202. : __first_{0}
  203. #endif
  204. {
  205. #ifdef _LIBCPP_CXX03_LANG
  206. std::fill_n(__first_, _N_words, __storage_type(0));
  207. #endif
  208. }
  209. #ifdef _LIBCPP_CXX03_LANG
  210. template <size_t _N_words, size_t _Size>
  211. void __bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT {
  212. __storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)];
  213. size_t __sz = _Size;
  214. for (size_t __i = 0; __i < sizeof(__t) / sizeof(__t[0]); ++__i, __v >>= __bits_per_word, __sz -= __bits_per_word)
  215. if (__sz < __bits_per_word)
  216. __t[__i] = static_cast<__storage_type>(__v) & (1ULL << __sz) - 1;
  217. else
  218. __t[__i] = static_cast<__storage_type>(__v);
  219. std::copy(__t, __t + sizeof(__t) / sizeof(__t[0]), __first_);
  220. std::fill(
  221. __first_ + sizeof(__t) / sizeof(__t[0]), __first_ + sizeof(__first_) / sizeof(__first_[0]), __storage_type(0));
  222. }
  223. template <size_t _N_words, size_t _Size>
  224. inline _LIBCPP_HIDE_FROM_ABI void __bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT {
  225. __first_[0] = __v;
  226. if (_Size < __bits_per_word)
  227. __first_[0] &= (1ULL << _Size) - 1;
  228. std::fill(__first_ + 1, __first_ + sizeof(__first_) / sizeof(__first_[0]), __storage_type(0));
  229. }
  230. #endif // _LIBCPP_CXX03_LANG
  231. template <size_t _N_words, size_t _Size>
  232. inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
  233. #ifndef _LIBCPP_CXX03_LANG
  234. # if __SIZEOF_SIZE_T__ == 8
  235. : __first_{__v}
  236. # elif __SIZEOF_SIZE_T__ == 4
  237. : __first_{static_cast<__storage_type>(__v),
  238. _Size >= 2 * __bits_per_word
  239. ? static_cast<__storage_type>(__v >> __bits_per_word)
  240. : static_cast<__storage_type>((__v >> __bits_per_word) &
  241. (__storage_type(1) << (_Size - __bits_per_word)) - 1)}
  242. # else
  243. # error This constructor has not been ported to this platform
  244. # endif
  245. #endif
  246. {
  247. #ifdef _LIBCPP_CXX03_LANG
  248. __init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>());
  249. #endif
  250. }
  251. template <size_t _N_words, size_t _Size>
  252. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
  253. __bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT {
  254. for (size_type __i = 0; __i < _N_words; ++__i)
  255. __first_[__i] &= __v.__first_[__i];
  256. }
  257. template <size_t _N_words, size_t _Size>
  258. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
  259. __bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT {
  260. for (size_type __i = 0; __i < _N_words; ++__i)
  261. __first_[__i] |= __v.__first_[__i];
  262. }
  263. template <size_t _N_words, size_t _Size>
  264. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
  265. __bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT {
  266. for (size_type __i = 0; __i < _N_words; ++__i)
  267. __first_[__i] ^= __v.__first_[__i];
  268. }
  269. template <size_t _N_words, size_t _Size>
  270. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void __bitset<_N_words, _Size>::flip() _NOEXCEPT {
  271. // do middle whole words
  272. size_type __n = _Size;
  273. __storage_pointer __p = __first_;
  274. for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
  275. *__p = ~*__p;
  276. // do last partial word
  277. if (__n > 0) {
  278. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
  279. __storage_type __b = *__p & __m;
  280. *__p &= ~__m;
  281. *__p |= ~__b & __m;
  282. }
  283. }
  284. template <size_t _N_words, size_t _Size>
  285. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
  286. __bitset<_N_words, _Size>::to_ulong(false_type) const {
  287. const_iterator __e = __make_iter(_Size);
  288. const_iterator __i = std::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true);
  289. if (__i != __e)
  290. __throw_overflow_error("bitset to_ulong overflow error");
  291. return __first_[0];
  292. }
  293. template <size_t _N_words, size_t _Size>
  294. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long
  295. __bitset<_N_words, _Size>::to_ulong(true_type) const {
  296. return __first_[0];
  297. }
  298. template <size_t _N_words, size_t _Size>
  299. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
  300. __bitset<_N_words, _Size>::to_ullong(false_type) const {
  301. const_iterator __e = __make_iter(_Size);
  302. const_iterator __i = std::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true);
  303. if (__i != __e)
  304. __throw_overflow_error("bitset to_ullong overflow error");
  305. return to_ullong(true_type());
  306. }
  307. template <size_t _N_words, size_t _Size>
  308. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
  309. __bitset<_N_words, _Size>::to_ullong(true_type) const {
  310. return to_ullong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long long)>());
  311. }
  312. template <size_t _N_words, size_t _Size>
  313. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
  314. __bitset<_N_words, _Size>::to_ullong(true_type, false_type) const {
  315. return __first_[0];
  316. }
  317. template <size_t _N_words, size_t _Size>
  318. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long
  319. __bitset<_N_words, _Size>::to_ullong(true_type, true_type) const {
  320. unsigned long long __r = __first_[0];
  321. for (size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i)
  322. __r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT);
  323. return __r;
  324. }
  325. template <size_t _N_words, size_t _Size>
  326. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __bitset<_N_words, _Size>::all() const _NOEXCEPT {
  327. // do middle whole words
  328. size_type __n = _Size;
  329. __const_storage_pointer __p = __first_;
  330. for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
  331. if (~*__p)
  332. return false;
  333. // do last partial word
  334. if (__n > 0) {
  335. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
  336. if (~*__p & __m)
  337. return false;
  338. }
  339. return true;
  340. }
  341. template <size_t _N_words, size_t _Size>
  342. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __bitset<_N_words, _Size>::any() const _NOEXCEPT {
  343. // do middle whole words
  344. size_type __n = _Size;
  345. __const_storage_pointer __p = __first_;
  346. for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
  347. if (*__p)
  348. return true;
  349. // do last partial word
  350. if (__n > 0) {
  351. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
  352. if (*__p & __m)
  353. return true;
  354. }
  355. return false;
  356. }
  357. template <size_t _N_words, size_t _Size>
  358. inline size_t __bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT {
  359. size_t __h = 0;
  360. for (size_type __i = 0; __i < _N_words; ++__i)
  361. __h ^= __first_[__i];
  362. return __h;
  363. }
  364. template <size_t _Size>
  365. class __bitset<1, _Size> {
  366. public:
  367. typedef ptrdiff_t difference_type;
  368. typedef size_t size_type;
  369. typedef size_type __storage_type;
  370. protected:
  371. typedef __bitset __self;
  372. typedef __storage_type* __storage_pointer;
  373. typedef const __storage_type* __const_storage_pointer;
  374. static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
  375. friend class __bit_reference<__bitset>;
  376. friend class __bit_const_reference<__bitset>;
  377. friend class __bit_iterator<__bitset, false>;
  378. friend class __bit_iterator<__bitset, true>;
  379. friend struct __bit_array<__bitset>;
  380. __storage_type __first_;
  381. typedef __bit_reference<__bitset> reference;
  382. typedef __bit_const_reference<__bitset> const_reference;
  383. typedef __bit_iterator<__bitset, false> iterator;
  384. typedef __bit_iterator<__bitset, true> const_iterator;
  385. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
  386. _LIBCPP_HIDE_FROM_ABI explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
  387. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 reference __make_ref(size_t __pos) _NOEXCEPT {
  388. return reference(&__first_, __storage_type(1) << __pos);
  389. }
  390. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT {
  391. return const_reference(&__first_, __storage_type(1) << __pos);
  392. }
  393. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 iterator __make_iter(size_t __pos) _NOEXCEPT {
  394. return iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);
  395. }
  396. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 const_iterator __make_iter(size_t __pos) const _NOEXCEPT {
  397. return const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);
  398. }
  399. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator&=(const __bitset& __v) _NOEXCEPT;
  400. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator|=(const __bitset& __v) _NOEXCEPT;
  401. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator^=(const __bitset& __v) _NOEXCEPT;
  402. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT;
  403. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const;
  404. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const;
  405. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT;
  406. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT;
  407. _LIBCPP_HIDE_FROM_ABI size_t __hash_code() const _NOEXCEPT;
  408. };
  409. template <size_t _Size>
  410. inline _LIBCPP_CONSTEXPR __bitset<1, _Size>::__bitset() _NOEXCEPT : __first_(0) {}
  411. template <size_t _Size>
  412. inline _LIBCPP_CONSTEXPR __bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
  413. : __first_(_Size == __bits_per_word ? static_cast<__storage_type>(__v)
  414. : static_cast<__storage_type>(__v) & ((__storage_type(1) << _Size) - 1)) {}
  415. template <size_t _Size>
  416. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
  417. __bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT {
  418. __first_ &= __v.__first_;
  419. }
  420. template <size_t _Size>
  421. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
  422. __bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT {
  423. __first_ |= __v.__first_;
  424. }
  425. template <size_t _Size>
  426. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
  427. __bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT {
  428. __first_ ^= __v.__first_;
  429. }
  430. template <size_t _Size>
  431. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void __bitset<1, _Size>::flip() _NOEXCEPT {
  432. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
  433. __first_ = ~__first_;
  434. __first_ &= __m;
  435. }
  436. template <size_t _Size>
  437. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __bitset<1, _Size>::to_ulong() const {
  438. return __first_;
  439. }
  440. template <size_t _Size>
  441. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __bitset<1, _Size>::to_ullong() const {
  442. return __first_;
  443. }
  444. template <size_t _Size>
  445. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __bitset<1, _Size>::all() const _NOEXCEPT {
  446. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
  447. return !(~__first_ & __m);
  448. }
  449. template <size_t _Size>
  450. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __bitset<1, _Size>::any() const _NOEXCEPT {
  451. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
  452. return __first_ & __m;
  453. }
  454. template <size_t _Size>
  455. inline size_t __bitset<1, _Size>::__hash_code() const _NOEXCEPT {
  456. return __first_;
  457. }
  458. template <>
  459. class __bitset<0, 0> {
  460. public:
  461. typedef ptrdiff_t difference_type;
  462. typedef size_t size_type;
  463. typedef size_type __storage_type;
  464. protected:
  465. typedef __bitset __self;
  466. typedef __storage_type* __storage_pointer;
  467. typedef const __storage_type* __const_storage_pointer;
  468. static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
  469. friend class __bit_reference<__bitset>;
  470. friend class __bit_const_reference<__bitset>;
  471. friend class __bit_iterator<__bitset, false>;
  472. friend class __bit_iterator<__bitset, true>;
  473. friend struct __bit_array<__bitset>;
  474. typedef __bit_reference<__bitset> reference;
  475. typedef __bit_const_reference<__bitset> const_reference;
  476. typedef __bit_iterator<__bitset, false> iterator;
  477. typedef __bit_iterator<__bitset, true> const_iterator;
  478. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
  479. _LIBCPP_HIDE_FROM_ABI explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long) _NOEXCEPT;
  480. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 reference __make_ref(size_t) _NOEXCEPT {
  481. return reference(nullptr, 1);
  482. }
  483. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference __make_ref(size_t) const _NOEXCEPT {
  484. return const_reference(nullptr, 1);
  485. }
  486. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 iterator __make_iter(size_t) _NOEXCEPT {
  487. return iterator(nullptr, 0);
  488. }
  489. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 const_iterator __make_iter(size_t) const _NOEXCEPT {
  490. return const_iterator(nullptr, 0);
  491. }
  492. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator&=(const __bitset&) _NOEXCEPT {}
  493. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator|=(const __bitset&) _NOEXCEPT {}
  494. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator^=(const __bitset&) _NOEXCEPT {}
  495. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT {}
  496. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const { return 0; }
  497. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const { return 0; }
  498. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT { return true; }
  499. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT { return false; }
  500. _LIBCPP_HIDE_FROM_ABI size_t __hash_code() const _NOEXCEPT { return 0; }
  501. };
  502. inline _LIBCPP_CONSTEXPR __bitset<0, 0>::__bitset() _NOEXCEPT {}
  503. inline _LIBCPP_CONSTEXPR __bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT {}
  504. template <size_t _Size>
  505. class _LIBCPP_TEMPLATE_VIS bitset;
  506. template <size_t _Size>
  507. struct hash<bitset<_Size> >;
  508. template <size_t _Size>
  509. class _LIBCPP_TEMPLATE_VIS bitset
  510. : private __bitset<_Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1, _Size> {
  511. public:
  512. static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1;
  513. typedef __bitset<__n_words, _Size> base;
  514. public:
  515. typedef typename base::reference reference;
  516. typedef typename base::const_reference const_reference;
  517. // 23.3.5.1 constructors:
  518. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {}
  519. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset(unsigned long long __v) _NOEXCEPT : base(__v) {}
  520. template <class _CharT, __enable_if_t<_IsCharLikeType<_CharT>::value, int> = 0>
  521. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit bitset(
  522. const _CharT* __str,
  523. #if _LIBCPP_STD_VER >= 26
  524. typename basic_string_view<_CharT>::size_type __n = basic_string_view<_CharT>::npos,
  525. #else
  526. typename basic_string<_CharT>::size_type __n = basic_string<_CharT>::npos,
  527. #endif
  528. _CharT __zero = _CharT('0'),
  529. _CharT __one = _CharT('1')) {
  530. size_t __rlen = std::min(__n, char_traits<_CharT>::length(__str));
  531. __init_from_string_view(basic_string_view<_CharT>(__str, __rlen), __zero, __one);
  532. }
  533. #if _LIBCPP_STD_VER >= 26
  534. template <class _CharT, class _Traits>
  535. _LIBCPP_HIDE_FROM_ABI constexpr explicit bitset(
  536. basic_string_view<_CharT, _Traits> __str,
  537. typename basic_string_view<_CharT, _Traits>::size_type __pos = 0,
  538. typename basic_string_view<_CharT, _Traits>::size_type __n = basic_string_view<_CharT, _Traits>::npos,
  539. _CharT __zero = _CharT('0'),
  540. _CharT __one = _CharT('1')) {
  541. if (__pos > __str.size())
  542. __throw_out_of_range("bitset string pos out of range");
  543. size_t __rlen = std::min(__n, __str.size() - __pos);
  544. __init_from_string_view(basic_string_view<_CharT, _Traits>(__str.data() + __pos, __rlen), __zero, __one);
  545. }
  546. #endif
  547. template <class _CharT, class _Traits, class _Allocator>
  548. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit bitset(
  549. const basic_string<_CharT, _Traits, _Allocator>& __str,
  550. typename basic_string<_CharT, _Traits, _Allocator>::size_type __pos = 0,
  551. typename basic_string<_CharT, _Traits, _Allocator>::size_type __n =
  552. basic_string<_CharT, _Traits, _Allocator>::npos,
  553. _CharT __zero = _CharT('0'),
  554. _CharT __one = _CharT('1')) {
  555. if (__pos > __str.size())
  556. std::__throw_out_of_range("bitset string pos out of range");
  557. size_t __rlen = std::min(__n, __str.size() - __pos);
  558. __init_from_string_view(basic_string_view<_CharT, _Traits>(__str.data() + __pos, __rlen), __zero, __one);
  559. }
  560. // 23.3.5.2 bitset operations:
  561. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& operator&=(const bitset& __rhs) _NOEXCEPT;
  562. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& operator|=(const bitset& __rhs) _NOEXCEPT;
  563. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& operator^=(const bitset& __rhs) _NOEXCEPT;
  564. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& operator<<=(size_t __pos) _NOEXCEPT;
  565. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& operator>>=(size_t __pos) _NOEXCEPT;
  566. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& set() _NOEXCEPT;
  567. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& set(size_t __pos, bool __val = true);
  568. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& reset() _NOEXCEPT;
  569. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& reset(size_t __pos);
  570. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset operator~() const _NOEXCEPT;
  571. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& flip() _NOEXCEPT;
  572. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& flip(size_t __pos);
  573. // element access:
  574. #ifdef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL
  575. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator[](size_t __p) const { return base::__make_ref(__p); }
  576. #else
  577. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference operator[](size_t __p) const { return base::__make_ref(__p); }
  578. #endif
  579. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 reference operator[](size_t __p) { return base::__make_ref(__p); }
  580. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const;
  581. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const;
  582. template <class _CharT, class _Traits, class _Allocator>
  583. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
  584. to_string(_CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) const;
  585. template <class _CharT, class _Traits>
  586. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, allocator<_CharT> >
  587. to_string(_CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) const;
  588. template <class _CharT>
  589. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
  590. to_string(_CharT __zero = _CharT('0'), _CharT __one = _CharT('1')) const;
  591. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<char, char_traits<char>, allocator<char> >
  592. to_string(char __zero = '0', char __one = '1') const;
  593. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 size_t count() const _NOEXCEPT;
  594. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR size_t size() const _NOEXCEPT { return _Size; }
  595. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator==(const bitset& __rhs) const _NOEXCEPT;
  596. #if _LIBCPP_STD_VER <= 17
  597. _LIBCPP_HIDE_FROM_ABI bool operator!=(const bitset& __rhs) const _NOEXCEPT;
  598. #endif
  599. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool test(size_t __pos) const;
  600. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool all() const _NOEXCEPT;
  601. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool any() const _NOEXCEPT;
  602. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool none() const _NOEXCEPT { return !any(); }
  603. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset operator<<(size_t __pos) const _NOEXCEPT;
  604. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset operator>>(size_t __pos) const _NOEXCEPT;
  605. private:
  606. template <class _CharT, class _Traits>
  607. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
  608. __init_from_string_view(basic_string_view<_CharT, _Traits> __str, _CharT __zero, _CharT __one) {
  609. for (size_t __i = 0; __i < __str.size(); ++__i)
  610. if (!_Traits::eq(__str[__i], __zero) && !_Traits::eq(__str[__i], __one))
  611. std::__throw_invalid_argument("bitset string ctor has invalid argument");
  612. size_t __mp = std::min(__str.size(), _Size);
  613. size_t __i = 0;
  614. for (; __i < __mp; ++__i) {
  615. _CharT __c = __str[__mp - 1 - __i];
  616. (*this)[__i] = _Traits::eq(__c, __one);
  617. }
  618. std::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
  619. }
  620. _LIBCPP_HIDE_FROM_ABI size_t __hash_code() const _NOEXCEPT { return base::__hash_code(); }
  621. friend struct hash<bitset>;
  622. };
  623. template <size_t _Size>
  624. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>&
  625. bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT {
  626. base::operator&=(__rhs);
  627. return *this;
  628. }
  629. template <size_t _Size>
  630. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>&
  631. bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT {
  632. base::operator|=(__rhs);
  633. return *this;
  634. }
  635. template <size_t _Size>
  636. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>&
  637. bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT {
  638. base::operator^=(__rhs);
  639. return *this;
  640. }
  641. template <size_t _Size>
  642. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>& bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT {
  643. __pos = std::min(__pos, _Size);
  644. std::copy_backward(base::__make_iter(0), base::__make_iter(_Size - __pos), base::__make_iter(_Size));
  645. std::fill_n(base::__make_iter(0), __pos, false);
  646. return *this;
  647. }
  648. template <size_t _Size>
  649. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>& bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT {
  650. __pos = std::min(__pos, _Size);
  651. std::copy(base::__make_iter(__pos), base::__make_iter(_Size), base::__make_iter(0));
  652. std::fill_n(base::__make_iter(_Size - __pos), __pos, false);
  653. return *this;
  654. }
  655. template <size_t _Size>
  656. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>& bitset<_Size>::set() _NOEXCEPT {
  657. std::fill_n(base::__make_iter(0), _Size, true);
  658. return *this;
  659. }
  660. template <size_t _Size>
  661. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>& bitset<_Size>::set(size_t __pos, bool __val) {
  662. if (__pos >= _Size)
  663. __throw_out_of_range("bitset set argument out of range");
  664. (*this)[__pos] = __val;
  665. return *this;
  666. }
  667. template <size_t _Size>
  668. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>& bitset<_Size>::reset() _NOEXCEPT {
  669. std::fill_n(base::__make_iter(0), _Size, false);
  670. return *this;
  671. }
  672. template <size_t _Size>
  673. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>& bitset<_Size>::reset(size_t __pos) {
  674. if (__pos >= _Size)
  675. __throw_out_of_range("bitset reset argument out of range");
  676. (*this)[__pos] = false;
  677. return *this;
  678. }
  679. template <size_t _Size>
  680. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size> bitset<_Size>::operator~() const _NOEXCEPT {
  681. bitset __x(*this);
  682. __x.flip();
  683. return __x;
  684. }
  685. template <size_t _Size>
  686. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>& bitset<_Size>::flip() _NOEXCEPT {
  687. base::flip();
  688. return *this;
  689. }
  690. template <size_t _Size>
  691. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>& bitset<_Size>::flip(size_t __pos) {
  692. if (__pos >= _Size)
  693. __throw_out_of_range("bitset flip argument out of range");
  694. reference __r = base::__make_ref(__pos);
  695. __r = ~__r;
  696. return *this;
  697. }
  698. template <size_t _Size>
  699. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long bitset<_Size>::to_ulong() const {
  700. return base::to_ulong();
  701. }
  702. template <size_t _Size>
  703. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long bitset<_Size>::to_ullong() const {
  704. return base::to_ullong();
  705. }
  706. template <size_t _Size>
  707. template <class _CharT, class _Traits, class _Allocator>
  708. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
  709. bitset<_Size>::to_string(_CharT __zero, _CharT __one) const {
  710. basic_string<_CharT, _Traits, _Allocator> __r(_Size, __zero);
  711. for (size_t __i = 0; __i != _Size; ++__i) {
  712. if ((*this)[__i])
  713. __r[_Size - 1 - __i] = __one;
  714. }
  715. return __r;
  716. }
  717. template <size_t _Size>
  718. template <class _CharT, class _Traits>
  719. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, allocator<_CharT> >
  720. bitset<_Size>::to_string(_CharT __zero, _CharT __one) const {
  721. return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one);
  722. }
  723. template <size_t _Size>
  724. template <class _CharT>
  725. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
  726. bitset<_Size>::to_string(_CharT __zero, _CharT __one) const {
  727. return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one);
  728. }
  729. template <size_t _Size>
  730. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<char, char_traits<char>, allocator<char> >
  731. bitset<_Size>::to_string(char __zero, char __one) const {
  732. return to_string<char, char_traits<char>, allocator<char> >(__zero, __one);
  733. }
  734. template <size_t _Size>
  735. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 size_t bitset<_Size>::count() const _NOEXCEPT {
  736. return static_cast<size_t>(std::count(base::__make_iter(0), base::__make_iter(_Size), true));
  737. }
  738. template <size_t _Size>
  739. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool
  740. bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT {
  741. return std::equal(base::__make_iter(0), base::__make_iter(_Size), __rhs.__make_iter(0));
  742. }
  743. #if _LIBCPP_STD_VER <= 17
  744. template <size_t _Size>
  745. inline _LIBCPP_HIDE_FROM_ABI bool bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT {
  746. return !(*this == __rhs);
  747. }
  748. #endif
  749. template <size_t _Size>
  750. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool bitset<_Size>::test(size_t __pos) const {
  751. if (__pos >= _Size)
  752. __throw_out_of_range("bitset test argument out of range");
  753. return (*this)[__pos];
  754. }
  755. template <size_t _Size>
  756. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool bitset<_Size>::all() const _NOEXCEPT {
  757. return base::all();
  758. }
  759. template <size_t _Size>
  760. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool bitset<_Size>::any() const _NOEXCEPT {
  761. return base::any();
  762. }
  763. template <size_t _Size>
  764. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>
  765. bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT {
  766. bitset __r = *this;
  767. __r <<= __pos;
  768. return __r;
  769. }
  770. template <size_t _Size>
  771. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>
  772. bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT {
  773. bitset __r = *this;
  774. __r >>= __pos;
  775. return __r;
  776. }
  777. template <size_t _Size>
  778. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>
  779. operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT {
  780. bitset<_Size> __r = __x;
  781. __r &= __y;
  782. return __r;
  783. }
  784. template <size_t _Size>
  785. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>
  786. operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT {
  787. bitset<_Size> __r = __x;
  788. __r |= __y;
  789. return __r;
  790. }
  791. template <size_t _Size>
  792. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset<_Size>
  793. operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT {
  794. bitset<_Size> __r = __x;
  795. __r ^= __y;
  796. return __r;
  797. }
  798. template <size_t _Size>
  799. struct _LIBCPP_TEMPLATE_VIS hash<bitset<_Size> > : public __unary_function<bitset<_Size>, size_t> {
  800. _LIBCPP_HIDE_FROM_ABI size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT { return __bs.__hash_code(); }
  801. };
  802. template <class _CharT, class _Traits, size_t _Size>
  803. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  804. operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x);
  805. template <class _CharT, class _Traits, size_t _Size>
  806. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  807. operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x);
  808. _LIBCPP_END_NAMESPACE_STD
  809. _LIBCPP_POP_MACROS
  810. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  811. # include <concepts>
  812. # include <cstdlib>
  813. # include <type_traits>
  814. #endif
  815. #endif // _LIBCPP_BITSET