bitset 40 KB

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