bitset 33 KB

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