bitset 33 KB

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