12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127 |
- // -*- C++ -*-
- //===---------------------------- bitset ----------------------------------===//
- //
- // The LLVM Compiler Infrastructure
- //
- // This file is dual licensed under the MIT and the University of Illinois Open
- // Source Licenses. See LICENSE.TXT for details.
- //
- //===----------------------------------------------------------------------===//
- #ifndef _LIBCPP_BITSET
- #define _LIBCPP_BITSET
- /*
- bitset synopsis
- namespace std
- {
- namespace std {
- template <size_t N>
- class bitset
- {
- public:
- // bit reference:
- class reference
- {
- friend class bitset;
- reference() noexcept;
- public:
- ~reference() noexcept;
- reference& operator=(bool x) noexcept; // for b[i] = x;
- reference& operator=(const reference&) noexcept; // for b[i] = b[j];
- bool operator~() const noexcept; // flips the bit
- operator bool() const noexcept; // for x = b[i];
- reference& flip() noexcept; // for b[i].flip();
- };
- // 23.3.5.1 constructors:
- constexpr bitset() noexcept;
- constexpr bitset(unsigned long long val) noexcept;
- template <class charT>
- explicit bitset(const charT* str,
- typename basic_string<charT>::size_type n = basic_string<charT>::npos,
- charT zero = charT('0'), charT one = charT('1'));
- template<class charT, class traits, class Allocator>
- explicit bitset(const basic_string<charT,traits,Allocator>& str,
- typename basic_string<charT,traits,Allocator>::size_type pos = 0,
- typename basic_string<charT,traits,Allocator>::size_type n =
- basic_string<charT,traits,Allocator>::npos,
- charT zero = charT('0'), charT one = charT('1'));
- // 23.3.5.2 bitset operations:
- bitset& operator&=(const bitset& rhs) noexcept;
- bitset& operator|=(const bitset& rhs) noexcept;
- bitset& operator^=(const bitset& rhs) noexcept;
- bitset& operator<<=(size_t pos) noexcept;
- bitset& operator>>=(size_t pos) noexcept;
- bitset& set() noexcept;
- bitset& set(size_t pos, bool val = true);
- bitset& reset() noexcept;
- bitset& reset(size_t pos);
- bitset operator~() const noexcept;
- bitset& flip() noexcept;
- bitset& flip(size_t pos);
- // element access:
- constexpr bool operator[](size_t pos) const; // for b[i];
- reference operator[](size_t pos); // for b[i];
- unsigned long to_ulong() const;
- unsigned long long to_ullong() const;
- template <class charT, class traits, class Allocator>
- basic_string<charT, traits, Allocator> to_string(charT zero = charT('0'), charT one = charT('1')) const;
- template <class charT, class traits>
- basic_string<charT, traits, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;
- template <class charT>
- basic_string<charT, char_traits<charT>, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;
- basic_string<char, char_traits<char>, allocator<char> > to_string(char zero = '0', char one = '1') const;
- size_t count() const noexcept;
- constexpr size_t size() const noexcept;
- bool operator==(const bitset& rhs) const noexcept;
- bool operator!=(const bitset& rhs) const noexcept;
- bool test(size_t pos) const;
- bool all() const noexcept;
- bool any() const noexcept;
- bool none() const noexcept;
- bitset operator<<(size_t pos) const noexcept;
- bitset operator>>(size_t pos) const noexcept;
- };
- // 23.3.5.3 bitset operators:
- template <size_t N>
- bitset<N> operator&(const bitset<N>&, const bitset<N>&) noexcept;
- template <size_t N>
- bitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept;
- template <size_t N>
- bitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept;
- template <class charT, class traits, size_t N>
- basic_istream<charT, traits>&
- operator>>(basic_istream<charT, traits>& is, bitset<N>& x);
- template <class charT, class traits, size_t N>
- basic_ostream<charT, traits>&
- operator<<(basic_ostream<charT, traits>& os, const bitset<N>& x);
- template <size_t N> struct hash<std::bitset<N>>;
- } // std
- */
- #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
- #pragma GCC system_header
- #endif
- #include <__config>
- #include <__bit_reference>
- #include <cstddef>
- #include <climits>
- #include <string>
- #include <stdexcept>
- #if !defined(_LIBCPP_SGX_NO_IOSTREAMS)
- #include <iosfwd>
- #endif // !defined(_LIBCPP_SGX_NO_IOSTREAMS)
- #include <__functional_base>
- #if defined(_LIBCPP_NO_EXCEPTIONS)
- #include <cassert>
- #endif
- #include <__undef_min_max>
- _LIBCPP_BEGIN_NAMESPACE_STD
- template <size_t _N_words, size_t _Size>
- class __bitset;
- template <size_t _N_words, size_t _Size>
- struct __has_storage_type<__bitset<_N_words, _Size> >
- {
- static const bool value = true;
- };
- template <size_t _N_words, size_t _Size>
- class __bitset
- {
- public:
- typedef ptrdiff_t difference_type;
- typedef size_t size_type;
- typedef size_type __storage_type;
- protected:
- typedef __bitset __self;
- typedef __storage_type* __storage_pointer;
- typedef const __storage_type* __const_storage_pointer;
- static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
- friend class __bit_reference<__bitset>;
- friend class __bit_const_reference<__bitset>;
- friend class __bit_iterator<__bitset, false>;
- friend class __bit_iterator<__bitset, true>;
- friend struct __bit_array<__bitset>;
- __storage_type __first_[_N_words];
- typedef __bit_reference<__bitset> reference;
- typedef __bit_const_reference<__bitset> const_reference;
- typedef __bit_iterator<__bitset, false> iterator;
- typedef __bit_iterator<__bitset, true> const_iterator;
- _LIBCPP_INLINE_VISIBILITY
- _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
- {return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
- {return const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
- _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
- {return iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
- _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
- {return const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
- _LIBCPP_INLINE_VISIBILITY
- void operator&=(const __bitset& __v) _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- void operator|=(const __bitset& __v) _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- void operator^=(const __bitset& __v) _NOEXCEPT;
- void flip() _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const
- {return to_ulong(integral_constant<bool, _Size < sizeof(unsigned long) * CHAR_BIT>());}
- _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const
- {return to_ullong(integral_constant<bool, _Size < sizeof(unsigned long long) * CHAR_BIT>());}
- bool all() const _NOEXCEPT;
- bool any() const _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- size_t __hash_code() const _NOEXCEPT;
- private:
- #ifdef _LIBCPP_HAS_NO_CONSTEXPR
- void __init(unsigned long long __v, false_type) _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- void __init(unsigned long long __v, true_type) _NOEXCEPT;
- #endif // _LIBCPP_HAS_NO_CONSTEXPR
- unsigned long to_ulong(false_type) const;
- _LIBCPP_INLINE_VISIBILITY
- unsigned long to_ulong(true_type) const;
- unsigned long long to_ullong(false_type) const;
- _LIBCPP_INLINE_VISIBILITY
- unsigned long long to_ullong(true_type) const;
- _LIBCPP_INLINE_VISIBILITY
- unsigned long long to_ullong(true_type, false_type) const;
- unsigned long long to_ullong(true_type, true_type) const;
- };
- template <size_t _N_words, size_t _Size>
- inline
- _LIBCPP_CONSTEXPR
- __bitset<_N_words, _Size>::__bitset() _NOEXCEPT
- #ifndef _LIBCPP_HAS_NO_CONSTEXPR
- : __first_{0}
- #endif
- {
- #ifdef _LIBCPP_HAS_NO_CONSTEXPR
- _VSTD::fill_n(__first_, _N_words, __storage_type(0));
- #endif
- }
- #ifdef _LIBCPP_HAS_NO_CONSTEXPR
- template <size_t _N_words, size_t _Size>
- void
- __bitset<_N_words, _Size>::__init(unsigned long long __v, false_type) _NOEXCEPT
- {
- __storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)];
- for (size_t __i = 0; __i < sizeof(__t)/sizeof(__t[0]); ++__i, __v >>= __bits_per_word)
- __t[__i] = static_cast<__storage_type>(__v);
- _VSTD::copy(__t, __t + sizeof(__t)/sizeof(__t[0]), __first_);
- _VSTD::fill(__first_ + sizeof(__t)/sizeof(__t[0]), __first_ + sizeof(__first_)/sizeof(__first_[0]),
- __storage_type(0));
- }
- template <size_t _N_words, size_t _Size>
- inline _LIBCPP_INLINE_VISIBILITY
- void
- __bitset<_N_words, _Size>::__init(unsigned long long __v, true_type) _NOEXCEPT
- {
- __first_[0] = __v;
- _VSTD::fill(__first_ + 1, __first_ + sizeof(__first_)/sizeof(__first_[0]), __storage_type(0));
- }
- #endif // _LIBCPP_HAS_NO_CONSTEXPR
- template <size_t _N_words, size_t _Size>
- inline
- _LIBCPP_CONSTEXPR
- __bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
- #ifndef _LIBCPP_HAS_NO_CONSTEXPR
- #if __SIZEOF_SIZE_T__ == 8
- : __first_{__v}
- #elif __SIZEOF_SIZE_T__ == 4
- : __first_{__v, __v >> __bits_per_word}
- #else
- #error This constructor has not been ported to this platform
- #endif
- #endif
- {
- #ifdef _LIBCPP_HAS_NO_CONSTEXPR
- __init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>());
- #endif
- }
- template <size_t _N_words, size_t _Size>
- inline
- void
- __bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
- {
- for (size_type __i = 0; __i < _N_words; ++__i)
- __first_[__i] &= __v.__first_[__i];
- }
- template <size_t _N_words, size_t _Size>
- inline
- void
- __bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
- {
- for (size_type __i = 0; __i < _N_words; ++__i)
- __first_[__i] |= __v.__first_[__i];
- }
- template <size_t _N_words, size_t _Size>
- inline
- void
- __bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
- {
- for (size_type __i = 0; __i < _N_words; ++__i)
- __first_[__i] ^= __v.__first_[__i];
- }
- template <size_t _N_words, size_t _Size>
- void
- __bitset<_N_words, _Size>::flip() _NOEXCEPT
- {
- // do middle whole words
- size_type __n = _Size;
- __storage_pointer __p = __first_;
- for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
- *__p = ~*__p;
- // do last partial word
- if (__n > 0)
- {
- __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
- __storage_type __b = *__p & __m;
- *__p &= ~__m;
- *__p |= ~__b & __m;
- }
- }
- template <size_t _N_words, size_t _Size>
- unsigned long
- __bitset<_N_words, _Size>::to_ulong(false_type) const
- {
- const_iterator __e = __make_iter(_Size);
- const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true);
- if (__i != __e)
- #ifndef _LIBCPP_NO_EXCEPTIONS
- throw overflow_error("bitset to_ulong overflow error");
- #else
- assert(!"bitset to_ulong overflow error");
- #endif
- return __first_[0];
- }
- template <size_t _N_words, size_t _Size>
- inline
- unsigned long
- __bitset<_N_words, _Size>::to_ulong(true_type) const
- {
- return __first_[0];
- }
- template <size_t _N_words, size_t _Size>
- unsigned long long
- __bitset<_N_words, _Size>::to_ullong(false_type) const
- {
- const_iterator __e = __make_iter(_Size);
- const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true);
- if (__i != __e)
- #ifndef _LIBCPP_NO_EXCEPTIONS
- throw overflow_error("bitset to_ullong overflow error");
- #else
- assert(!"bitset to_ullong overflow error");
- #endif
- return to_ullong(true_type());
- }
- template <size_t _N_words, size_t _Size>
- inline
- unsigned long long
- __bitset<_N_words, _Size>::to_ullong(true_type) const
- {
- return to_ullong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long long)>());
- }
- template <size_t _N_words, size_t _Size>
- inline
- unsigned long long
- __bitset<_N_words, _Size>::to_ullong(true_type, false_type) const
- {
- return __first_[0];
- }
- template <size_t _N_words, size_t _Size>
- unsigned long long
- __bitset<_N_words, _Size>::to_ullong(true_type, true_type) const
- {
- unsigned long long __r = __first_[0];
- for (std::size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i)
- __r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT);
- return __r;
- }
- template <size_t _N_words, size_t _Size>
- bool
- __bitset<_N_words, _Size>::all() const _NOEXCEPT
- {
- // do middle whole words
- size_type __n = _Size;
- __const_storage_pointer __p = __first_;
- for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
- if (~*__p)
- return false;
- // do last partial word
- if (__n > 0)
- {
- __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
- if (~*__p & __m)
- return false;
- }
- return true;
- }
- template <size_t _N_words, size_t _Size>
- bool
- __bitset<_N_words, _Size>::any() const _NOEXCEPT
- {
- // do middle whole words
- size_type __n = _Size;
- __const_storage_pointer __p = __first_;
- for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
- if (*__p)
- return true;
- // do last partial word
- if (__n > 0)
- {
- __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
- if (*__p & __m)
- return true;
- }
- return false;
- }
- template <size_t _N_words, size_t _Size>
- inline
- size_t
- __bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT
- {
- size_t __h = 0;
- for (size_type __i = 0; __i < _N_words; ++__i)
- __h ^= __first_[__i];
- return __h;
- }
- template <size_t _Size>
- class __bitset<1, _Size>
- {
- public:
- typedef ptrdiff_t difference_type;
- typedef size_t size_type;
- typedef size_type __storage_type;
- protected:
- typedef __bitset __self;
- typedef __storage_type* __storage_pointer;
- typedef const __storage_type* __const_storage_pointer;
- static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
- friend class __bit_reference<__bitset>;
- friend class __bit_const_reference<__bitset>;
- friend class __bit_iterator<__bitset, false>;
- friend class __bit_iterator<__bitset, true>;
- friend struct __bit_array<__bitset>;
- __storage_type __first_;
- typedef __bit_reference<__bitset> reference;
- typedef __bit_const_reference<__bitset> const_reference;
- typedef __bit_iterator<__bitset, false> iterator;
- typedef __bit_iterator<__bitset, true> const_iterator;
- _LIBCPP_INLINE_VISIBILITY
- _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long __v) _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
- {return reference(&__first_, __storage_type(1) << __pos);}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t __pos) const _NOEXCEPT
- {return const_reference(&__first_, __storage_type(1) << __pos);}
- _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
- {return iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
- _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
- {return const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
- _LIBCPP_INLINE_VISIBILITY
- void operator&=(const __bitset& __v) _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- void operator|=(const __bitset& __v) _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- void operator^=(const __bitset& __v) _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- void flip() _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- unsigned long to_ulong() const;
- _LIBCPP_INLINE_VISIBILITY
- unsigned long long to_ullong() const;
- _LIBCPP_INLINE_VISIBILITY
- bool all() const _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- bool any() const _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- size_t __hash_code() const _NOEXCEPT;
- };
- template <size_t _Size>
- inline
- _LIBCPP_CONSTEXPR
- __bitset<1, _Size>::__bitset() _NOEXCEPT
- : __first_(0)
- {
- }
- template <size_t _Size>
- inline
- _LIBCPP_CONSTEXPR
- __bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
- : __first_(static_cast<__storage_type>(__v))
- {
- }
- template <size_t _Size>
- inline
- void
- __bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
- {
- __first_ &= __v.__first_;
- }
- template <size_t _Size>
- inline
- void
- __bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
- {
- __first_ |= __v.__first_;
- }
- template <size_t _Size>
- inline
- void
- __bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
- {
- __first_ ^= __v.__first_;
- }
- template <size_t _Size>
- inline
- void
- __bitset<1, _Size>::flip() _NOEXCEPT
- {
- __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
- __first_ = ~__first_;
- __first_ &= __m;
- }
- template <size_t _Size>
- inline
- unsigned long
- __bitset<1, _Size>::to_ulong() const
- {
- return __first_;
- }
- template <size_t _Size>
- inline
- unsigned long long
- __bitset<1, _Size>::to_ullong() const
- {
- return __first_;
- }
- template <size_t _Size>
- inline
- bool
- __bitset<1, _Size>::all() const _NOEXCEPT
- {
- __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
- return !(~__first_ & __m);
- }
- template <size_t _Size>
- inline
- bool
- __bitset<1, _Size>::any() const _NOEXCEPT
- {
- __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
- return __first_ & __m;
- }
- template <size_t _Size>
- inline
- size_t
- __bitset<1, _Size>::__hash_code() const _NOEXCEPT
- {
- return __first_;
- }
- template <>
- class __bitset<0, 0>
- {
- public:
- typedef ptrdiff_t difference_type;
- typedef size_t size_type;
- typedef size_type __storage_type;
- protected:
- typedef __bitset __self;
- typedef __storage_type* __storage_pointer;
- typedef const __storage_type* __const_storage_pointer;
- static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
- friend class __bit_reference<__bitset>;
- friend class __bit_const_reference<__bitset>;
- friend class __bit_iterator<__bitset, false>;
- friend class __bit_iterator<__bitset, true>;
- friend struct __bit_array<__bitset>;
- typedef __bit_reference<__bitset> reference;
- typedef __bit_const_reference<__bitset> const_reference;
- typedef __bit_iterator<__bitset, false> iterator;
- typedef __bit_iterator<__bitset, true> const_iterator;
- _LIBCPP_INLINE_VISIBILITY
- _LIBCPP_CONSTEXPR __bitset() _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- explicit _LIBCPP_CONSTEXPR __bitset(unsigned long long) _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t) _NOEXCEPT
- {return reference(0, 1);}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR const_reference __make_ref(size_t) const _NOEXCEPT
- {return const_reference(0, 1);}
- _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t) _NOEXCEPT
- {return iterator(0, 0);}
- _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t) const _NOEXCEPT
- {return const_iterator(0, 0);}
- _LIBCPP_INLINE_VISIBILITY void operator&=(const __bitset&) _NOEXCEPT {}
- _LIBCPP_INLINE_VISIBILITY void operator|=(const __bitset&) _NOEXCEPT {}
- _LIBCPP_INLINE_VISIBILITY void operator^=(const __bitset&) _NOEXCEPT {}
- _LIBCPP_INLINE_VISIBILITY void flip() _NOEXCEPT {}
- _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const {return 0;}
- _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const {return 0;}
- _LIBCPP_INLINE_VISIBILITY bool all() const _NOEXCEPT {return true;}
- _LIBCPP_INLINE_VISIBILITY bool any() const _NOEXCEPT {return false;}
- _LIBCPP_INLINE_VISIBILITY size_t __hash_code() const _NOEXCEPT {return 0;}
- };
- inline
- _LIBCPP_CONSTEXPR
- __bitset<0, 0>::__bitset() _NOEXCEPT
- {
- }
- inline
- _LIBCPP_CONSTEXPR
- __bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT
- {
- }
- template <size_t _Size> class _LIBCPP_TYPE_VIS_ONLY bitset;
- template <size_t _Size> struct hash<bitset<_Size> >;
- template <size_t _Size>
- class _LIBCPP_TYPE_VIS_ONLY bitset
- : private __bitset<_Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1, _Size>
- {
- public:
- static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1;
- typedef __bitset<__n_words, _Size> base;
- public:
- typedef typename base::reference reference;
- typedef typename base::const_reference const_reference;
- // 23.3.5.1 constructors:
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
- bitset(unsigned long long __v) _NOEXCEPT : base(__v) {}
- template<class _CharT>
- explicit bitset(const _CharT* __str,
- typename basic_string<_CharT>::size_type __n = basic_string<_CharT>::npos,
- _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
- template<class _CharT, class _Traits, class _Allocator>
- explicit bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
- typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos = 0,
- typename basic_string<_CharT,_Traits,_Allocator>::size_type __n =
- (basic_string<_CharT,_Traits,_Allocator>::npos),
- _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
- // 23.3.5.2 bitset operations:
- _LIBCPP_INLINE_VISIBILITY
- bitset& operator&=(const bitset& __rhs) _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- bitset& operator|=(const bitset& __rhs) _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- bitset& operator^=(const bitset& __rhs) _NOEXCEPT;
- bitset& operator<<=(size_t __pos) _NOEXCEPT;
- bitset& operator>>=(size_t __pos) _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- bitset& set() _NOEXCEPT;
- bitset& set(size_t __pos, bool __val = true);
- _LIBCPP_INLINE_VISIBILITY
- bitset& reset() _NOEXCEPT;
- bitset& reset(size_t __pos);
- _LIBCPP_INLINE_VISIBILITY
- bitset operator~() const _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- bitset& flip() _NOEXCEPT;
- bitset& flip(size_t __pos);
- // element access:
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
- const_reference operator[](size_t __p) const {return base::__make_ref(__p);}
- _LIBCPP_INLINE_VISIBILITY reference operator[](size_t __p) {return base::__make_ref(__p);}
- _LIBCPP_INLINE_VISIBILITY
- unsigned long to_ulong() const;
- _LIBCPP_INLINE_VISIBILITY
- unsigned long long to_ullong() const;
- template <class _CharT, class _Traits, class _Allocator>
- basic_string<_CharT, _Traits, _Allocator> to_string(_CharT __zero = _CharT('0'),
- _CharT __one = _CharT('1')) const;
- template <class _CharT, class _Traits>
- _LIBCPP_INLINE_VISIBILITY
- basic_string<_CharT, _Traits, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
- _CharT __one = _CharT('1')) const;
- template <class _CharT>
- _LIBCPP_INLINE_VISIBILITY
- basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
- _CharT __one = _CharT('1')) const;
- _LIBCPP_INLINE_VISIBILITY
- basic_string<char, char_traits<char>, allocator<char> > to_string(char __zero = '0',
- char __one = '1') const;
- _LIBCPP_INLINE_VISIBILITY
- size_t count() const _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR size_t size() const _NOEXCEPT {return _Size;}
- _LIBCPP_INLINE_VISIBILITY
- bool operator==(const bitset& __rhs) const _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- bool operator!=(const bitset& __rhs) const _NOEXCEPT;
- bool test(size_t __pos) const;
- _LIBCPP_INLINE_VISIBILITY
- bool all() const _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- bool any() const _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY bool none() const _NOEXCEPT {return !any();}
- _LIBCPP_INLINE_VISIBILITY
- bitset operator<<(size_t __pos) const _NOEXCEPT;
- _LIBCPP_INLINE_VISIBILITY
- bitset operator>>(size_t __pos) const _NOEXCEPT;
- private:
- _LIBCPP_INLINE_VISIBILITY
- size_t __hash_code() const _NOEXCEPT {return base::__hash_code();}
- friend struct hash<bitset>;
- };
- template <size_t _Size>
- template<class _CharT>
- bitset<_Size>::bitset(const _CharT* __str,
- typename basic_string<_CharT>::size_type __n,
- _CharT __zero, _CharT __one)
- {
- size_t __rlen = _VSTD::min(__n, char_traits<_CharT>::length(__str));
- for (size_t __i = 0; __i < __rlen; ++__i)
- if (__str[__i] != __zero && __str[__i] != __one)
- #ifndef _LIBCPP_NO_EXCEPTIONS
- throw invalid_argument("bitset string ctor has invalid argument");
- #else
- assert(!"bitset string ctor has invalid argument");
- #endif
- size_t _Mp = _VSTD::min(__rlen, _Size);
- size_t __i = 0;
- for (; __i < _Mp; ++__i)
- {
- _CharT __c = __str[_Mp - 1 - __i];
- if (__c == __zero)
- (*this)[__i] = false;
- else
- (*this)[__i] = true;
- }
- _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
- }
- template <size_t _Size>
- template<class _CharT, class _Traits, class _Allocator>
- bitset<_Size>::bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
- typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos,
- typename basic_string<_CharT,_Traits,_Allocator>::size_type __n,
- _CharT __zero, _CharT __one)
- {
- if (__pos > __str.size())
- #ifndef _LIBCPP_NO_EXCEPTIONS
- throw out_of_range("bitset string pos out of range");
- #else
- assert(!"bitset string pos out of range");
- #endif
- size_t __rlen = _VSTD::min(__n, __str.size() - __pos);
- for (size_t __i = __pos; __i < __pos + __rlen; ++__i)
- if (!_Traits::eq(__str[__i], __zero) && !_Traits::eq(__str[__i], __one))
- #ifndef _LIBCPP_NO_EXCEPTIONS
- throw invalid_argument("bitset string ctor has invalid argument");
- #else
- assert(!"bitset string ctor has invalid argument");
- #endif
- size_t _Mp = _VSTD::min(__rlen, _Size);
- size_t __i = 0;
- for (; __i < _Mp; ++__i)
- {
- _CharT __c = __str[__pos + _Mp - 1 - __i];
- if (_Traits::eq(__c, __zero))
- (*this)[__i] = false;
- else
- (*this)[__i] = true;
- }
- _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
- }
- template <size_t _Size>
- inline
- bitset<_Size>&
- bitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT
- {
- base::operator&=(__rhs);
- return *this;
- }
- template <size_t _Size>
- inline
- bitset<_Size>&
- bitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT
- {
- base::operator|=(__rhs);
- return *this;
- }
- template <size_t _Size>
- inline
- bitset<_Size>&
- bitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT
- {
- base::operator^=(__rhs);
- return *this;
- }
- template <size_t _Size>
- bitset<_Size>&
- bitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT
- {
- __pos = _VSTD::min(__pos, _Size);
- _VSTD::copy_backward(base::__make_iter(0), base::__make_iter(_Size - __pos), base::__make_iter(_Size));
- _VSTD::fill_n(base::__make_iter(0), __pos, false);
- return *this;
- }
- template <size_t _Size>
- bitset<_Size>&
- bitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT
- {
- __pos = _VSTD::min(__pos, _Size);
- _VSTD::copy(base::__make_iter(__pos), base::__make_iter(_Size), base::__make_iter(0));
- _VSTD::fill_n(base::__make_iter(_Size - __pos), __pos, false);
- return *this;
- }
- template <size_t _Size>
- inline
- bitset<_Size>&
- bitset<_Size>::set() _NOEXCEPT
- {
- _VSTD::fill_n(base::__make_iter(0), _Size, true);
- return *this;
- }
- template <size_t _Size>
- bitset<_Size>&
- bitset<_Size>::set(size_t __pos, bool __val)
- {
- if (__pos >= _Size)
- #ifndef _LIBCPP_NO_EXCEPTIONS
- throw out_of_range("bitset set argument out of range");
- #else
- assert(!"bitset set argument out of range");
- #endif
- (*this)[__pos] = __val;
- return *this;
- }
- template <size_t _Size>
- inline
- bitset<_Size>&
- bitset<_Size>::reset() _NOEXCEPT
- {
- _VSTD::fill_n(base::__make_iter(0), _Size, false);
- return *this;
- }
- template <size_t _Size>
- bitset<_Size>&
- bitset<_Size>::reset(size_t __pos)
- {
- if (__pos >= _Size)
- #ifndef _LIBCPP_NO_EXCEPTIONS
- throw out_of_range("bitset reset argument out of range");
- #else
- assert(!"bitset reset argument out of range");
- #endif
- (*this)[__pos] = false;
- return *this;
- }
- template <size_t _Size>
- inline
- bitset<_Size>
- bitset<_Size>::operator~() const _NOEXCEPT
- {
- bitset __x(*this);
- __x.flip();
- return __x;
- }
- template <size_t _Size>
- inline
- bitset<_Size>&
- bitset<_Size>::flip() _NOEXCEPT
- {
- base::flip();
- return *this;
- }
- template <size_t _Size>
- bitset<_Size>&
- bitset<_Size>::flip(size_t __pos)
- {
- if (__pos >= _Size)
- #ifndef _LIBCPP_NO_EXCEPTIONS
- throw out_of_range("bitset flip argument out of range");
- #else
- assert(!"bitset flip argument out of range");
- #endif
- reference r = base::__make_ref(__pos);
- r = ~r;
- return *this;
- }
- template <size_t _Size>
- inline
- unsigned long
- bitset<_Size>::to_ulong() const
- {
- return base::to_ulong();
- }
- template <size_t _Size>
- inline
- unsigned long long
- bitset<_Size>::to_ullong() const
- {
- return base::to_ullong();
- }
- template <size_t _Size>
- template <class _CharT, class _Traits, class _Allocator>
- basic_string<_CharT, _Traits, _Allocator>
- bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
- {
- basic_string<_CharT, _Traits, _Allocator> __r(_Size, __zero);
- for (size_t __i = 0; __i < _Size; ++__i)
- {
- if ((*this)[__i])
- __r[_Size - 1 - __i] = __one;
- }
- return __r;
- }
- template <size_t _Size>
- template <class _CharT, class _Traits>
- inline
- basic_string<_CharT, _Traits, allocator<_CharT> >
- bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
- {
- return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one);
- }
- template <size_t _Size>
- template <class _CharT>
- inline
- basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
- bitset<_Size>::to_string(_CharT __zero, _CharT __one) const
- {
- return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one);
- }
- template <size_t _Size>
- inline
- basic_string<char, char_traits<char>, allocator<char> >
- bitset<_Size>::to_string(char __zero, char __one) const
- {
- return to_string<char, char_traits<char>, allocator<char> >(__zero, __one);
- }
- template <size_t _Size>
- inline
- size_t
- bitset<_Size>::count() const _NOEXCEPT
- {
- return static_cast<size_t>(_VSTD::count(base::__make_iter(0), base::__make_iter(_Size), true));
- }
- template <size_t _Size>
- inline
- bool
- bitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT
- {
- return _VSTD::equal(base::__make_iter(0), base::__make_iter(_Size), __rhs.__make_iter(0));
- }
- template <size_t _Size>
- inline
- bool
- bitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT
- {
- return !(*this == __rhs);
- }
- template <size_t _Size>
- bool
- bitset<_Size>::test(size_t __pos) const
- {
- if (__pos >= _Size)
- #ifndef _LIBCPP_NO_EXCEPTIONS
- throw out_of_range("bitset test argument out of range");
- #else
- assert(!"bitset test argument out of range");
- #endif
- return (*this)[__pos];
- }
- template <size_t _Size>
- inline
- bool
- bitset<_Size>::all() const _NOEXCEPT
- {
- return base::all();
- }
- template <size_t _Size>
- inline
- bool
- bitset<_Size>::any() const _NOEXCEPT
- {
- return base::any();
- }
- template <size_t _Size>
- inline
- bitset<_Size>
- bitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT
- {
- bitset __r = *this;
- __r <<= __pos;
- return __r;
- }
- template <size_t _Size>
- inline
- bitset<_Size>
- bitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT
- {
- bitset __r = *this;
- __r >>= __pos;
- return __r;
- }
- template <size_t _Size>
- inline _LIBCPP_INLINE_VISIBILITY
- bitset<_Size>
- operator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
- {
- bitset<_Size> __r = __x;
- __r &= __y;
- return __r;
- }
- template <size_t _Size>
- inline _LIBCPP_INLINE_VISIBILITY
- bitset<_Size>
- operator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
- {
- bitset<_Size> __r = __x;
- __r |= __y;
- return __r;
- }
- template <size_t _Size>
- inline _LIBCPP_INLINE_VISIBILITY
- bitset<_Size>
- operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
- {
- bitset<_Size> __r = __x;
- __r ^= __y;
- return __r;
- }
- template <size_t _Size>
- struct _LIBCPP_TYPE_VIS_ONLY hash<bitset<_Size> >
- : public unary_function<bitset<_Size>, size_t>
- {
- _LIBCPP_INLINE_VISIBILITY
- size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT
- {return __bs.__hash_code();}
- };
- #if !defined(_LIBCPP_SGX_NO_IOSTREAMS)
- template <class _CharT, class _Traits, size_t _Size>
- basic_istream<_CharT, _Traits>&
- operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x);
- template <class _CharT, class _Traits, size_t _Size>
- basic_ostream<_CharT, _Traits>&
- operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x);
- #endif // !defined(_LIBCPP_SGX_NO_IOSTREAMS)
- _LIBCPP_END_NAMESPACE_STD
- #endif // _LIBCPP_BITSET
|