bitset 33 KB

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