bitset revision 227825
1227825Stheraven// -*- C++ -*-
2227825Stheraven//===---------------------------- bitset ----------------------------------===//
3227825Stheraven//
4227825Stheraven//                     The LLVM Compiler Infrastructure
5227825Stheraven//
6227825Stheraven// This file is dual licensed under the MIT and the University of Illinois Open
7227825Stheraven// Source Licenses. See LICENSE.TXT for details.
8227825Stheraven//
9227825Stheraven//===----------------------------------------------------------------------===//
10227825Stheraven
11227825Stheraven#ifndef _LIBCPP_BITSET
12227825Stheraven#define _LIBCPP_BITSET
13227825Stheraven
14227825Stheraven/*
15227825Stheraven    bitset synopsis
16227825Stheraven
17227825Stheravennamespace std
18227825Stheraven{
19227825Stheraven
20227825Stheravennamespace std {
21227825Stheraven
22227825Stheraventemplate <size_t N>
23227825Stheravenclass bitset
24227825Stheraven{
25227825Stheravenpublic:
26227825Stheraven    // bit reference:
27227825Stheraven    class reference
28227825Stheraven    {
29227825Stheraven        friend class bitset;
30227825Stheraven        reference() noexcept;
31227825Stheraven    public:
32227825Stheraven        ~reference() noexcept;
33227825Stheraven        reference& operator=(bool x) noexcept;           // for b[i] = x;
34227825Stheraven        reference& operator=(const reference&) noexcept; // for b[i] = b[j];
35227825Stheraven        bool operator~() const noexcept;                 // flips the bit
36227825Stheraven        operator bool() const noexcept;                  // for x = b[i];
37227825Stheraven        reference& flip() noexcept;                      // for b[i].flip();
38227825Stheraven    };
39227825Stheraven
40227825Stheraven    // 23.3.5.1 constructors:
41227825Stheraven    constexpr bitset() noexcept;
42227825Stheraven    constexpr bitset(unsigned long long val) noexcept;
43227825Stheraven    template <class charT>
44227825Stheraven        explicit bitset(const charT* str,
45227825Stheraven                        typename basic_string<charT>::size_type n = basic_string<charT>::npos,
46227825Stheraven                        charT zero = charT('0'), charT one = charT('1'));
47227825Stheraven    template<class charT, class traits, class Allocator>
48227825Stheraven        explicit bitset(const basic_string<charT,traits,Allocator>& str,
49227825Stheraven                        typename basic_string<charT,traits,Allocator>::size_type pos = 0,
50227825Stheraven                        typename basic_string<charT,traits,Allocator>::size_type n =
51227825Stheraven                                 basic_string<charT,traits,Allocator>::npos,
52227825Stheraven                        charT zero = charT('0'), charT one = charT('1'));
53227825Stheraven
54227825Stheraven    // 23.3.5.2 bitset operations:
55227825Stheraven    bitset& operator&=(const bitset& rhs) noexcept;
56227825Stheraven    bitset& operator|=(const bitset& rhs) noexcept;
57227825Stheraven    bitset& operator^=(const bitset& rhs) noexcept;
58227825Stheraven    bitset& operator<<=(size_t pos) noexcept;
59227825Stheraven    bitset& operator>>=(size_t pos) noexcept;
60227825Stheraven    bitset& set() noexcept;
61227825Stheraven    bitset& set(size_t pos, bool val = true);
62227825Stheraven    bitset& reset() noexcept;
63227825Stheraven    bitset& reset(size_t pos);
64227825Stheraven    bitset operator~() const noexcept;
65227825Stheraven    bitset& flip() noexcept;
66227825Stheraven    bitset& flip(size_t pos);
67227825Stheraven
68227825Stheraven    // element access:
69227825Stheraven    constexpr bool operator[](size_t pos) const; // for b[i];
70227825Stheraven    reference operator[](size_t pos);            // for b[i];
71227825Stheraven    unsigned long to_ulong() const;
72227825Stheraven    unsigned long long to_ullong() const;
73227825Stheraven    template <class charT, class traits, class Allocator>
74227825Stheraven        basic_string<charT, traits, Allocator> to_string(charT zero = charT('0'), charT one = charT('1')) const;
75227825Stheraven    template <class charT, class traits>
76227825Stheraven        basic_string<charT, traits, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;
77227825Stheraven    template <class charT>
78227825Stheraven        basic_string<charT, char_traits<charT>, allocator<charT> > to_string(charT zero = charT('0'), charT one = charT('1')) const;
79227825Stheraven    basic_string<char, char_traits<char>, allocator<char> > to_string(char zero = '0', char one = '1') const;
80227825Stheraven    size_t count() const noexcept;
81227825Stheraven    constexpr size_t size() const noexcept;
82227825Stheraven    bool operator==(const bitset& rhs) const noexcept;
83227825Stheraven    bool operator!=(const bitset& rhs) const noexcept;
84227825Stheraven    bool test(size_t pos) const;
85227825Stheraven    bool all() const noexcept;
86227825Stheraven    bool any() const noexcept;
87227825Stheraven    bool none() const noexcept;
88227825Stheraven    bitset operator<<(size_t pos) const noexcept;
89227825Stheraven    bitset operator>>(size_t pos) const noexcept;
90227825Stheraven};
91227825Stheraven
92227825Stheraven// 23.3.5.3 bitset operators:
93227825Stheraventemplate <size_t N>
94227825Stheravenbitset<N> operator&(const bitset<N>&, const bitset<N>&) noexcept;
95227825Stheraven
96227825Stheraventemplate <size_t N>
97227825Stheravenbitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept;
98227825Stheraven
99227825Stheraventemplate <size_t N>
100227825Stheravenbitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept;
101227825Stheraven
102227825Stheraventemplate <class charT, class traits, size_t N>
103227825Stheravenbasic_istream<charT, traits>&
104227825Stheravenoperator>>(basic_istream<charT, traits>& is, bitset<N>& x);
105227825Stheraven
106227825Stheraventemplate <class charT, class traits, size_t N>
107227825Stheravenbasic_ostream<charT, traits>&
108227825Stheravenoperator<<(basic_ostream<charT, traits>& os, const bitset<N>& x);
109227825Stheraven
110227825Stheraventemplate <size_t N> struct hash<std::bitset<N>>;
111227825Stheraven
112227825Stheraven}  // std
113227825Stheraven
114227825Stheraven*/
115227825Stheraven
116227825Stheraven#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
117227825Stheraven#pragma GCC system_header
118227825Stheraven#endif
119227825Stheraven
120227825Stheraven#include <__config>
121227825Stheraven#include <__bit_reference>
122227825Stheraven#include <cstddef>
123227825Stheraven#include <climits>
124227825Stheraven#include <string>
125227825Stheraven#include <stdexcept>
126227825Stheraven#include <iosfwd>
127227825Stheraven#include <__functional_base>
128227825Stheraven#if defined(_LIBCPP_NO_EXCEPTIONS)
129227825Stheraven    #include <cassert>
130227825Stheraven#endif
131227825Stheraven
132227825Stheraven_LIBCPP_BEGIN_NAMESPACE_STD
133227825Stheraven
134227825Stheraventemplate <size_t _N_words, size_t _Size>
135227825Stheravenclass __bitset;
136227825Stheraven
137227825Stheraventemplate <size_t _N_words, size_t _Size>
138227825Stheravenstruct __has_storage_type<__bitset<_N_words, _Size> >
139227825Stheraven{
140227825Stheraven    static const bool value = true;
141227825Stheraven};
142227825Stheraven
143227825Stheraventemplate <size_t _N_words, size_t _Size>
144227825Stheravenclass __bitset
145227825Stheraven{
146227825Stheravenpublic:
147227825Stheraven    typedef ptrdiff_t              difference_type;
148227825Stheraven    typedef size_t                 size_type;
149227825Stheravenprotected:
150227825Stheraven    typedef __bitset __self;
151227825Stheraven    typedef size_type              __storage_type;
152227825Stheraven    typedef       __storage_type*  __storage_pointer;
153227825Stheraven    typedef const __storage_type*  __const_storage_pointer;
154227825Stheraven    static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
155227825Stheraven
156227825Stheraven    friend class __bit_reference<__bitset>;
157227825Stheraven    friend class __bit_const_reference<__bitset>;
158227825Stheraven    friend class __bit_iterator<__bitset, false>;
159227825Stheraven    friend class __bit_iterator<__bitset, true>;
160227825Stheraven    friend class __bit_array<__bitset>;
161227825Stheraven
162227825Stheraven    __storage_type __first_[_N_words];
163227825Stheraven
164227825Stheraven    typedef __bit_reference<__bitset>                  reference;
165227825Stheraven    typedef __bit_const_reference<__bitset>            const_reference;
166227825Stheraven    typedef __bit_iterator<__bitset, false>            iterator;
167227825Stheraven    typedef __bit_iterator<__bitset, true>             const_iterator;
168227825Stheraven
169227825Stheraven    __bitset() _NOEXCEPT;
170227825Stheraven    explicit __bitset(unsigned long long __v) _NOEXCEPT;
171227825Stheraven
172227825Stheraven    _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
173227825Stheraven        {return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
174227825Stheraven    _LIBCPP_INLINE_VISIBILITY const_reference __make_ref(size_t __pos) const _NOEXCEPT
175227825Stheraven        {return const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);}
176227825Stheraven    _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
177227825Stheraven        {return iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
178227825Stheraven    _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
179227825Stheraven        {return const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
180227825Stheraven
181227825Stheraven    void operator&=(const __bitset& __v) _NOEXCEPT;
182227825Stheraven    void operator|=(const __bitset& __v) _NOEXCEPT;
183227825Stheraven    void operator^=(const __bitset& __v) _NOEXCEPT;
184227825Stheraven
185227825Stheraven    void flip() _NOEXCEPT;
186227825Stheraven    _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const
187227825Stheraven        {return to_ulong(integral_constant<bool, _Size < sizeof(unsigned long) * CHAR_BIT>());}
188227825Stheraven    _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const
189227825Stheraven        {return to_ullong(integral_constant<bool, _Size < sizeof(unsigned long long) * CHAR_BIT>());}
190227825Stheraven
191227825Stheraven    bool all() const _NOEXCEPT;
192227825Stheraven    bool any() const _NOEXCEPT;
193227825Stheraven    size_t __hash_code() const _NOEXCEPT;
194227825Stheravenprivate:
195227825Stheraven    void __init(unsigned long long __v, false_type) _NOEXCEPT;
196227825Stheraven    void __init(unsigned long long __v, true_type) _NOEXCEPT;
197227825Stheraven    unsigned long to_ulong(false_type) const;
198227825Stheraven    unsigned long to_ulong(true_type) const;
199227825Stheraven    unsigned long long to_ullong(false_type) const;
200227825Stheraven    unsigned long long to_ullong(true_type) const;
201227825Stheraven    unsigned long long to_ullong(true_type, false_type) const;
202227825Stheraven    unsigned long long to_ullong(true_type, true_type) const;
203227825Stheraven};
204227825Stheraven
205227825Stheraventemplate <size_t _N_words, size_t _Size>
206227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
207227825Stheraven__bitset<_N_words, _Size>::__bitset() _NOEXCEPT
208227825Stheraven{
209227825Stheraven    _VSTD::fill_n(__first_, _N_words, __storage_type(0));
210227825Stheraven}
211227825Stheraven
212227825Stheraventemplate <size_t _N_words, size_t _Size>
213227825Stheravenvoid
214227825Stheraven__bitset<_N_words, _Size>::__init(unsigned long long __v, false_type)
215227825Stheraven{
216227825Stheraven    __storage_type __t[sizeof(unsigned long long) / sizeof(__storage_type)];
217227825Stheraven    for (size_t __i = 0; __i < sizeof(__t)/sizeof(__t[0]); ++__i, __v >>= __bits_per_word)
218227825Stheraven        __t[__i] = static_cast<__storage_type>(__v);
219227825Stheraven    _VSTD::copy(__t, __t + sizeof(__t)/sizeof(__t[0]), __first_);
220227825Stheraven    _VSTD::fill(__first_ + sizeof(__t)/sizeof(__t[0]), __first_ + sizeof(__first_)/sizeof(__first_[0]),
221227825Stheraven               __storage_type(0));
222227825Stheraven}
223227825Stheraven
224227825Stheraventemplate <size_t _N_words, size_t _Size>
225227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
226227825Stheravenvoid
227227825Stheraven__bitset<_N_words, _Size>::__init(unsigned long long __v, true_type)
228227825Stheraven{
229227825Stheraven    __first_[0] = __v;
230227825Stheraven    _VSTD::fill(__first_ + 1, __first_ + sizeof(__first_)/sizeof(__first_[0]), __storage_type(0));
231227825Stheraven}
232227825Stheraven
233227825Stheraventemplate <size_t _N_words, size_t _Size>
234227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
235227825Stheraven__bitset<_N_words, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
236227825Stheraven{
237227825Stheraven    __init(__v, integral_constant<bool, sizeof(unsigned long long) == sizeof(__storage_type)>());
238227825Stheraven}
239227825Stheraven
240227825Stheraventemplate <size_t _N_words, size_t _Size>
241227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
242227825Stheravenvoid
243227825Stheraven__bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
244227825Stheraven{
245227825Stheraven    for (size_type __i = 0; __i < _N_words; ++__i)
246227825Stheraven        __first_[__i] &= __v.__first_[__i];
247227825Stheraven}
248227825Stheraven
249227825Stheraventemplate <size_t _N_words, size_t _Size>
250227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
251227825Stheravenvoid
252227825Stheraven__bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
253227825Stheraven{
254227825Stheraven    for (size_type __i = 0; __i < _N_words; ++__i)
255227825Stheraven        __first_[__i] |= __v.__first_[__i];
256227825Stheraven}
257227825Stheraven
258227825Stheraventemplate <size_t _N_words, size_t _Size>
259227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
260227825Stheravenvoid
261227825Stheraven__bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
262227825Stheraven{
263227825Stheraven    for (size_type __i = 0; __i < _N_words; ++__i)
264227825Stheraven        __first_[__i] ^= __v.__first_[__i];
265227825Stheraven}
266227825Stheraven
267227825Stheraventemplate <size_t _N_words, size_t _Size>
268227825Stheravenvoid
269227825Stheraven__bitset<_N_words, _Size>::flip() _NOEXCEPT
270227825Stheraven{
271227825Stheraven    // do middle whole words
272227825Stheraven    size_type __n = _Size;
273227825Stheraven    __storage_pointer __p = __first_;
274227825Stheraven    for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
275227825Stheraven        *__p = ~*__p;
276227825Stheraven    // do last partial word
277227825Stheraven    if (__n > 0)
278227825Stheraven    {
279227825Stheraven        __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
280227825Stheraven        __storage_type __b = *__p & __m;
281227825Stheraven        *__p &= ~__m;
282227825Stheraven        *__p |= ~__b & __m;
283227825Stheraven    }
284227825Stheraven}
285227825Stheraven
286227825Stheraventemplate <size_t _N_words, size_t _Size>
287227825Stheravenunsigned long
288227825Stheraven__bitset<_N_words, _Size>::to_ulong(false_type) const
289227825Stheraven{
290227825Stheraven    const_iterator __e = __make_iter(_Size);
291227825Stheraven    const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true);
292227825Stheraven    if (__i != __e)
293227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
294227825Stheraven        throw overflow_error("bitset to_ulong overflow error");
295227825Stheraven#else
296227825Stheraven        assert(!"bitset to_ulong overflow error");
297227825Stheraven#endif
298227825Stheraven    return __first_[0];
299227825Stheraven}
300227825Stheraven
301227825Stheraventemplate <size_t _N_words, size_t _Size>
302227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
303227825Stheravenunsigned long
304227825Stheraven__bitset<_N_words, _Size>::to_ulong(true_type) const
305227825Stheraven{
306227825Stheraven    return __first_[0];
307227825Stheraven}
308227825Stheraven
309227825Stheraventemplate <size_t _N_words, size_t _Size>
310227825Stheravenunsigned long long
311227825Stheraven__bitset<_N_words, _Size>::to_ullong(false_type) const
312227825Stheraven{
313227825Stheraven    const_iterator __e = __make_iter(_Size);
314227825Stheraven    const_iterator __i = _VSTD::find(__make_iter(sizeof(unsigned long long) * CHAR_BIT), __e, true);
315227825Stheraven    if (__i != __e)
316227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
317227825Stheraven        throw overflow_error("bitset to_ullong overflow error");
318227825Stheraven#else
319227825Stheraven        assert(!"bitset to_ullong overflow error");
320227825Stheraven#endif
321227825Stheraven    return to_ullong(true_type());
322227825Stheraven}
323227825Stheraven
324227825Stheraventemplate <size_t _N_words, size_t _Size>
325227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
326227825Stheravenunsigned long long
327227825Stheraven__bitset<_N_words, _Size>::to_ullong(true_type) const
328227825Stheraven{
329227825Stheraven    return to_ullong(true_type(), integral_constant<bool, sizeof(__storage_type) < sizeof(unsigned long long)>());
330227825Stheraven}
331227825Stheraven
332227825Stheraventemplate <size_t _N_words, size_t _Size>
333227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
334227825Stheravenunsigned long long
335227825Stheraven__bitset<_N_words, _Size>::to_ullong(true_type, false_type) const
336227825Stheraven{
337227825Stheraven    return __first_[0];
338227825Stheraven}
339227825Stheraven
340227825Stheraventemplate <size_t _N_words, size_t _Size>
341227825Stheravenunsigned long long
342227825Stheraven__bitset<_N_words, _Size>::to_ullong(true_type, true_type) const
343227825Stheraven{
344227825Stheraven    unsigned long long __r = __first_[0];
345227825Stheraven    for (std::size_t __i = 1; __i < sizeof(unsigned long long) / sizeof(__storage_type); ++__i)
346227825Stheraven        __r |= static_cast<unsigned long long>(__first_[__i]) << (sizeof(__storage_type) * CHAR_BIT);
347227825Stheraven    return __r;
348227825Stheraven}
349227825Stheraven
350227825Stheraventemplate <size_t _N_words, size_t _Size>
351227825Stheravenbool
352227825Stheraven__bitset<_N_words, _Size>::all() const _NOEXCEPT
353227825Stheraven{
354227825Stheraven    // do middle whole words
355227825Stheraven    size_type __n = _Size;
356227825Stheraven    __const_storage_pointer __p = __first_;
357227825Stheraven    for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
358227825Stheraven        if (~*__p)
359227825Stheraven            return false;
360227825Stheraven    // do last partial word
361227825Stheraven    if (__n > 0)
362227825Stheraven    {
363227825Stheraven        __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
364227825Stheraven        if (~*__p & __m)
365227825Stheraven            return false;
366227825Stheraven    }
367227825Stheraven    return true;
368227825Stheraven}
369227825Stheraven
370227825Stheraventemplate <size_t _N_words, size_t _Size>
371227825Stheravenbool
372227825Stheraven__bitset<_N_words, _Size>::any() const _NOEXCEPT
373227825Stheraven{
374227825Stheraven    // do middle whole words
375227825Stheraven    size_type __n = _Size;
376227825Stheraven    __const_storage_pointer __p = __first_;
377227825Stheraven    for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
378227825Stheraven        if (*__p)
379227825Stheraven            return true;
380227825Stheraven    // do last partial word
381227825Stheraven    if (__n > 0)
382227825Stheraven    {
383227825Stheraven        __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
384227825Stheraven        if (*__p & __m)
385227825Stheraven            return true;
386227825Stheraven    }
387227825Stheraven    return false;
388227825Stheraven}
389227825Stheraven
390227825Stheraventemplate <size_t _N_words, size_t _Size>
391227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
392227825Stheravensize_t
393227825Stheraven__bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT
394227825Stheraven{
395227825Stheraven    size_t __h = 0;
396227825Stheraven    for (size_type __i = 0; __i < _N_words; ++__i)
397227825Stheraven        __h ^= __first_[__i];
398227825Stheraven    return __h;
399227825Stheraven}
400227825Stheraven
401227825Stheraventemplate <size_t _Size>
402227825Stheravenclass __bitset<1, _Size>
403227825Stheraven{
404227825Stheravenpublic:
405227825Stheraven    typedef ptrdiff_t              difference_type;
406227825Stheraven    typedef size_t                 size_type;
407227825Stheravenprotected:
408227825Stheraven    typedef __bitset __self;
409227825Stheraven    typedef size_type              __storage_type;
410227825Stheraven    typedef       __storage_type*  __storage_pointer;
411227825Stheraven    typedef const __storage_type*  __const_storage_pointer;
412227825Stheraven    static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
413227825Stheraven
414227825Stheraven    friend class __bit_reference<__bitset>;
415227825Stheraven    friend class __bit_const_reference<__bitset>;
416227825Stheraven    friend class __bit_iterator<__bitset, false>;
417227825Stheraven    friend class __bit_iterator<__bitset, true>;
418227825Stheraven    friend class __bit_array<__bitset>;
419227825Stheraven
420227825Stheraven    __storage_type __first_;
421227825Stheraven
422227825Stheraven    typedef __bit_reference<__bitset>                  reference;
423227825Stheraven    typedef __bit_const_reference<__bitset>            const_reference;
424227825Stheraven    typedef __bit_iterator<__bitset, false>            iterator;
425227825Stheraven    typedef __bit_iterator<__bitset, true>             const_iterator;
426227825Stheraven
427227825Stheraven    __bitset() _NOEXCEPT;
428227825Stheraven    explicit __bitset(unsigned long long __v) _NOEXCEPT;
429227825Stheraven
430227825Stheraven    _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t __pos) _NOEXCEPT
431227825Stheraven        {return reference(&__first_, __storage_type(1) << __pos);}
432227825Stheraven    _LIBCPP_INLINE_VISIBILITY const_reference __make_ref(size_t __pos) const _NOEXCEPT
433227825Stheraven        {return const_reference(&__first_, __storage_type(1) << __pos);}
434227825Stheraven    _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
435227825Stheraven        {return iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
436227825Stheraven    _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
437227825Stheraven        {return const_iterator(&__first_ + __pos / __bits_per_word, __pos % __bits_per_word);}
438227825Stheraven
439227825Stheraven    void operator&=(const __bitset& __v) _NOEXCEPT;
440227825Stheraven    void operator|=(const __bitset& __v) _NOEXCEPT;
441227825Stheraven    void operator^=(const __bitset& __v) _NOEXCEPT;
442227825Stheraven
443227825Stheraven    void flip() _NOEXCEPT;
444227825Stheraven
445227825Stheraven    unsigned long to_ulong() const;
446227825Stheraven    unsigned long long to_ullong() const;
447227825Stheraven
448227825Stheraven    bool all() const _NOEXCEPT;
449227825Stheraven    bool any() const _NOEXCEPT;
450227825Stheraven
451227825Stheraven    size_t __hash_code() const _NOEXCEPT;
452227825Stheraven};
453227825Stheraven
454227825Stheraventemplate <size_t _Size>
455227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
456227825Stheraven__bitset<1, _Size>::__bitset() _NOEXCEPT
457227825Stheraven    : __first_(0)
458227825Stheraven{
459227825Stheraven}
460227825Stheraven
461227825Stheraventemplate <size_t _Size>
462227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
463227825Stheraven__bitset<1, _Size>::__bitset(unsigned long long __v) _NOEXCEPT
464227825Stheraven    : __first_(static_cast<__storage_type>(__v))
465227825Stheraven{
466227825Stheraven}
467227825Stheraven
468227825Stheraventemplate <size_t _Size>
469227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
470227825Stheravenvoid
471227825Stheraven__bitset<1, _Size>::operator&=(const __bitset& __v) _NOEXCEPT
472227825Stheraven{
473227825Stheraven    __first_ &= __v.__first_;
474227825Stheraven}
475227825Stheraven
476227825Stheraventemplate <size_t _Size>
477227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
478227825Stheravenvoid
479227825Stheraven__bitset<1, _Size>::operator|=(const __bitset& __v) _NOEXCEPT
480227825Stheraven{
481227825Stheraven    __first_ |= __v.__first_;
482227825Stheraven}
483227825Stheraven
484227825Stheraventemplate <size_t _Size>
485227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
486227825Stheravenvoid
487227825Stheraven__bitset<1, _Size>::operator^=(const __bitset& __v) _NOEXCEPT
488227825Stheraven{
489227825Stheraven    __first_ ^= __v.__first_;
490227825Stheraven}
491227825Stheraven
492227825Stheraventemplate <size_t _Size>
493227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
494227825Stheravenvoid
495227825Stheraven__bitset<1, _Size>::flip() _NOEXCEPT
496227825Stheraven{
497227825Stheraven    __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
498227825Stheraven    __first_ = ~__first_;
499227825Stheraven    __first_ &= __m;
500227825Stheraven}
501227825Stheraven
502227825Stheraventemplate <size_t _Size>
503227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
504227825Stheravenunsigned long
505227825Stheraven__bitset<1, _Size>::to_ulong() const
506227825Stheraven{
507227825Stheraven    return __first_;
508227825Stheraven}
509227825Stheraven
510227825Stheraventemplate <size_t _Size>
511227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
512227825Stheravenunsigned long long
513227825Stheraven__bitset<1, _Size>::to_ullong() const
514227825Stheraven{
515227825Stheraven    return __first_;
516227825Stheraven}
517227825Stheraven
518227825Stheraventemplate <size_t _Size>
519227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
520227825Stheravenbool
521227825Stheraven__bitset<1, _Size>::all() const _NOEXCEPT
522227825Stheraven{
523227825Stheraven    __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
524227825Stheraven    return !(~__first_ & __m);
525227825Stheraven}
526227825Stheraven
527227825Stheraventemplate <size_t _Size>
528227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
529227825Stheravenbool
530227825Stheraven__bitset<1, _Size>::any() const _NOEXCEPT
531227825Stheraven{
532227825Stheraven    __storage_type __m = ~__storage_type(0) >> (__bits_per_word - _Size);
533227825Stheraven    return __first_ & __m;
534227825Stheraven}
535227825Stheraven
536227825Stheraventemplate <size_t _Size>
537227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
538227825Stheravensize_t
539227825Stheraven__bitset<1, _Size>::__hash_code() const _NOEXCEPT
540227825Stheraven{
541227825Stheraven    return __first_;
542227825Stheraven}
543227825Stheraven
544227825Stheraventemplate <>
545227825Stheravenclass __bitset<0, 0>
546227825Stheraven{
547227825Stheravenpublic:
548227825Stheraven    typedef ptrdiff_t              difference_type;
549227825Stheraven    typedef size_t                 size_type;
550227825Stheravenprotected:
551227825Stheraven    typedef __bitset __self;
552227825Stheraven    typedef size_type              __storage_type;
553227825Stheraven    typedef       __storage_type*  __storage_pointer;
554227825Stheraven    typedef const __storage_type*  __const_storage_pointer;
555227825Stheraven    static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
556227825Stheraven
557227825Stheraven    friend class __bit_reference<__bitset>;
558227825Stheraven    friend class __bit_const_reference<__bitset>;
559227825Stheraven    friend class __bit_iterator<__bitset, false>;
560227825Stheraven    friend class __bit_iterator<__bitset, true>;
561227825Stheraven    friend class __bit_array<__bitset>;
562227825Stheraven
563227825Stheraven    typedef __bit_reference<__bitset>                  reference;
564227825Stheraven    typedef __bit_const_reference<__bitset>            const_reference;
565227825Stheraven    typedef __bit_iterator<__bitset, false>            iterator;
566227825Stheraven    typedef __bit_iterator<__bitset, true>             const_iterator;
567227825Stheraven
568227825Stheraven    __bitset() _NOEXCEPT;
569227825Stheraven    explicit __bitset(unsigned long long) _NOEXCEPT;
570227825Stheraven
571227825Stheraven    _LIBCPP_INLINE_VISIBILITY reference __make_ref(size_t) _NOEXCEPT
572227825Stheraven        {return reference(0, 1);}
573227825Stheraven    _LIBCPP_INLINE_VISIBILITY const_reference __make_ref(size_t) const _NOEXCEPT
574227825Stheraven        {return const_reference(0, 1);}
575227825Stheraven    _LIBCPP_INLINE_VISIBILITY iterator __make_iter(size_t __pos) _NOEXCEPT
576227825Stheraven        {return iterator(0, 0);}
577227825Stheraven    _LIBCPP_INLINE_VISIBILITY const_iterator __make_iter(size_t __pos) const _NOEXCEPT
578227825Stheraven        {return const_iterator(0, 0);}
579227825Stheraven
580227825Stheraven    _LIBCPP_INLINE_VISIBILITY void operator&=(const __bitset&) _NOEXCEPT {}
581227825Stheraven    _LIBCPP_INLINE_VISIBILITY void operator|=(const __bitset&) _NOEXCEPT {}
582227825Stheraven    _LIBCPP_INLINE_VISIBILITY void operator^=(const __bitset&) _NOEXCEPT {}
583227825Stheraven
584227825Stheraven    _LIBCPP_INLINE_VISIBILITY void flip() _NOEXCEPT {}
585227825Stheraven
586227825Stheraven    _LIBCPP_INLINE_VISIBILITY unsigned long to_ulong() const {return 0;}
587227825Stheraven    _LIBCPP_INLINE_VISIBILITY unsigned long long to_ullong() const {return 0;}
588227825Stheraven
589227825Stheraven    _LIBCPP_INLINE_VISIBILITY bool all() const _NOEXCEPT {return true;}
590227825Stheraven    _LIBCPP_INLINE_VISIBILITY bool any() const _NOEXCEPT {return false;}
591227825Stheraven
592227825Stheraven    _LIBCPP_INLINE_VISIBILITY size_t __hash_code() const _NOEXCEPT {return 0;}
593227825Stheraven};
594227825Stheraven
595227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
596227825Stheraven__bitset<0, 0>::__bitset() _NOEXCEPT
597227825Stheraven{
598227825Stheraven}
599227825Stheraven
600227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
601227825Stheraven__bitset<0, 0>::__bitset(unsigned long long) _NOEXCEPT
602227825Stheraven{
603227825Stheraven}
604227825Stheraven
605227825Stheraventemplate <size_t _Size> class bitset;
606227825Stheraventemplate <size_t _Size> struct hash<bitset<_Size> >;
607227825Stheraven
608227825Stheraventemplate <size_t _Size>
609227825Stheravenclass _LIBCPP_VISIBLE bitset
610227825Stheraven    : private __bitset<_Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1, _Size>
611227825Stheraven{
612227825Stheraven    static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1;
613227825Stheraven    typedef __bitset<__n_words, _Size> base;
614227825Stheraven
615227825Stheravenpublic:
616227825Stheraven    typedef typename base::reference       reference;
617227825Stheraven    typedef typename base::const_reference const_reference;
618227825Stheraven
619227825Stheraven    // 23.3.5.1 constructors:
620227825Stheraven    /*constexpr*/ _LIBCPP_INLINE_VISIBILITY bitset() _NOEXCEPT {}
621227825Stheraven    /*constexpr*/ _LIBCPP_INLINE_VISIBILITY bitset(unsigned long long __v) _NOEXCEPT : base(__v) {}
622227825Stheraven    template<class _CharT>
623227825Stheraven        explicit bitset(const _CharT* __str,
624227825Stheraven                        typename basic_string<_CharT>::size_type __n = basic_string<_CharT>::npos,
625227825Stheraven                        _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
626227825Stheraven    template<class _CharT, class _Traits, class _Allocator>
627227825Stheraven        explicit bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
628227825Stheraven                        typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos = 0,
629227825Stheraven                        typename basic_string<_CharT,_Traits,_Allocator>::size_type __n =
630227825Stheraven                                (basic_string<_CharT,_Traits,_Allocator>::npos),
631227825Stheraven                        _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'));
632227825Stheraven
633227825Stheraven    // 23.3.5.2 bitset operations:
634227825Stheraven    bitset& operator&=(const bitset& __rhs) _NOEXCEPT;
635227825Stheraven    bitset& operator|=(const bitset& __rhs) _NOEXCEPT;
636227825Stheraven    bitset& operator^=(const bitset& __rhs) _NOEXCEPT;
637227825Stheraven    bitset& operator<<=(size_t __pos) _NOEXCEPT;
638227825Stheraven    bitset& operator>>=(size_t __pos) _NOEXCEPT;
639227825Stheraven    bitset& set() _NOEXCEPT;
640227825Stheraven    bitset& set(size_t __pos, bool __val = true);
641227825Stheraven    bitset& reset() _NOEXCEPT;
642227825Stheraven    bitset& reset(size_t __pos);
643227825Stheraven    bitset  operator~() const _NOEXCEPT;
644227825Stheraven    bitset& flip() _NOEXCEPT;
645227825Stheraven    bitset& flip(size_t __pos);
646227825Stheraven
647227825Stheraven    // element access:
648227825Stheraven    _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_t __p) const {return base::__make_ref(__p);}
649227825Stheraven    _LIBCPP_INLINE_VISIBILITY       reference operator[](size_t __p)       {return base::__make_ref(__p);}
650227825Stheraven    unsigned long to_ulong() const;
651227825Stheraven    unsigned long long to_ullong() const;
652227825Stheraven    template <class _CharT, class _Traits, class _Allocator>
653227825Stheraven        basic_string<_CharT, _Traits, _Allocator> to_string(_CharT __zero = _CharT('0'),
654227825Stheraven                                                            _CharT __one = _CharT('1')) const;
655227825Stheraven    template <class _CharT, class _Traits>
656227825Stheraven        basic_string<_CharT, _Traits, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
657227825Stheraven                                                                    _CharT __one = _CharT('1')) const;
658227825Stheraven    template <class _CharT>
659227825Stheraven        basic_string<_CharT, char_traits<_CharT>, allocator<_CharT> > to_string(_CharT __zero = _CharT('0'),
660227825Stheraven                                                                                _CharT __one = _CharT('1')) const;
661227825Stheraven    basic_string<char, char_traits<char>, allocator<char> > to_string(char __zero = '0',
662227825Stheraven                                                                      char __one = '1') const;
663227825Stheraven    size_t count() const _NOEXCEPT;
664227825Stheraven    /*constexpr*/ _LIBCPP_INLINE_VISIBILITY size_t size() const _NOEXCEPT {return _Size;}
665227825Stheraven    bool operator==(const bitset& __rhs) const _NOEXCEPT;
666227825Stheraven    bool operator!=(const bitset& __rhs) const _NOEXCEPT;
667227825Stheraven    bool test(size_t __pos) const;
668227825Stheraven    bool all() const _NOEXCEPT;
669227825Stheraven    bool any() const _NOEXCEPT;
670227825Stheraven    _LIBCPP_INLINE_VISIBILITY bool none() const _NOEXCEPT {return !any();}
671227825Stheraven    bitset operator<<(size_t __pos) const _NOEXCEPT;
672227825Stheraven    bitset operator>>(size_t __pos) const _NOEXCEPT;
673227825Stheraven
674227825Stheravenprivate:
675227825Stheraven
676227825Stheraven    _LIBCPP_INLINE_VISIBILITY
677227825Stheraven    size_t __hash_code() const _NOEXCEPT {return base::__hash_code();}
678227825Stheraven
679227825Stheraven    friend struct hash<bitset>;
680227825Stheraven};
681227825Stheraven
682227825Stheraventemplate <size_t _Size>
683227825Stheraventemplate<class _CharT>
684227825Stheravenbitset<_Size>::bitset(const _CharT* __str,
685227825Stheraven                      typename basic_string<_CharT>::size_type __n,
686227825Stheraven                      _CharT __zero, _CharT __one)
687227825Stheraven{
688227825Stheraven    size_t __rlen = _VSTD::min(__n, char_traits<_CharT>::length(__str));
689227825Stheraven    for (size_t __i = 0; __i < __rlen; ++__i)
690227825Stheraven        if (__str[__i] != __zero && __str[__i] != __one)
691227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
692227825Stheraven            throw invalid_argument("bitset string ctor has invalid argument");
693227825Stheraven#else
694227825Stheraven            assert(!"bitset string ctor has invalid argument");
695227825Stheraven#endif
696227825Stheraven    size_t _M = _VSTD::min(__rlen, _Size);
697227825Stheraven    size_t __i = 0;
698227825Stheraven    for (; __i < _M; ++__i)
699227825Stheraven    {
700227825Stheraven        _CharT __c = __str[_M - 1 - __i];
701227825Stheraven        if (__c == __zero)
702227825Stheraven            (*this)[__i] = false;
703227825Stheraven        else
704227825Stheraven            (*this)[__i] = true;
705227825Stheraven    }
706227825Stheraven    _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
707227825Stheraven}
708227825Stheraven
709227825Stheraventemplate <size_t _Size>
710227825Stheraventemplate<class _CharT, class _Traits, class _Allocator>
711227825Stheravenbitset<_Size>::bitset(const basic_string<_CharT,_Traits,_Allocator>& __str,
712227825Stheraven       typename basic_string<_CharT,_Traits,_Allocator>::size_type __pos,
713227825Stheraven       typename basic_string<_CharT,_Traits,_Allocator>::size_type __n,
714227825Stheraven       _CharT __zero, _CharT __one)
715227825Stheraven{
716227825Stheraven    if (__pos > __str.size())
717227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
718227825Stheraven        throw out_of_range("bitset string pos out of range");
719227825Stheraven#else
720227825Stheraven        assert(!"bitset string pos out of range");
721227825Stheraven#endif
722227825Stheraven    size_t __rlen = _VSTD::min(__n, __str.size() - __pos);
723227825Stheraven    for (size_t __i = __pos; __i < __pos + __rlen; ++__i)
724227825Stheraven        if (!_Traits::eq(__str[__i], __zero) && !_Traits::eq(__str[__i], __one))
725227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
726227825Stheraven            throw invalid_argument("bitset string ctor has invalid argument");
727227825Stheraven#else
728227825Stheraven            assert(!"bitset string ctor has invalid argument");
729227825Stheraven#endif
730227825Stheraven    size_t _M = _VSTD::min(__rlen, _Size);
731227825Stheraven    size_t __i = 0;
732227825Stheraven    for (; __i < _M; ++__i)
733227825Stheraven    {
734227825Stheraven        _CharT __c = __str[__pos + _M - 1 - __i];
735227825Stheraven        if (_Traits::eq(__c, __zero))
736227825Stheraven            (*this)[__i] = false;
737227825Stheraven        else
738227825Stheraven            (*this)[__i] = true;
739227825Stheraven    }
740227825Stheraven    _VSTD::fill(base::__make_iter(__i), base::__make_iter(_Size), false);
741227825Stheraven}
742227825Stheraven
743227825Stheraventemplate <size_t _Size>
744227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
745227825Stheravenbitset<_Size>&
746227825Stheravenbitset<_Size>::operator&=(const bitset& __rhs) _NOEXCEPT
747227825Stheraven{
748227825Stheraven    base::operator&=(__rhs);
749227825Stheraven    return *this;
750227825Stheraven}
751227825Stheraven
752227825Stheraventemplate <size_t _Size>
753227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
754227825Stheravenbitset<_Size>&
755227825Stheravenbitset<_Size>::operator|=(const bitset& __rhs) _NOEXCEPT
756227825Stheraven{
757227825Stheraven    base::operator|=(__rhs);
758227825Stheraven    return *this;
759227825Stheraven}
760227825Stheraven
761227825Stheraventemplate <size_t _Size>
762227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
763227825Stheravenbitset<_Size>&
764227825Stheravenbitset<_Size>::operator^=(const bitset& __rhs) _NOEXCEPT
765227825Stheraven{
766227825Stheraven    base::operator^=(__rhs);
767227825Stheraven    return *this;
768227825Stheraven}
769227825Stheraven
770227825Stheraventemplate <size_t _Size>
771227825Stheravenbitset<_Size>&
772227825Stheravenbitset<_Size>::operator<<=(size_t __pos) _NOEXCEPT
773227825Stheraven{
774227825Stheraven    __pos = _VSTD::min(__pos, _Size);
775227825Stheraven    _VSTD::copy_backward(base::__make_iter(0), base::__make_iter(_Size - __pos), base::__make_iter(_Size));
776227825Stheraven    _VSTD::fill_n(base::__make_iter(0), __pos, false);
777227825Stheraven    return *this;
778227825Stheraven}
779227825Stheraven
780227825Stheraventemplate <size_t _Size>
781227825Stheravenbitset<_Size>&
782227825Stheravenbitset<_Size>::operator>>=(size_t __pos) _NOEXCEPT
783227825Stheraven{
784227825Stheraven    __pos = _VSTD::min(__pos, _Size);
785227825Stheraven    _VSTD::copy(base::__make_iter(__pos), base::__make_iter(_Size), base::__make_iter(0));
786227825Stheraven    _VSTD::fill_n(base::__make_iter(_Size - __pos), __pos, false);
787227825Stheraven    return *this;
788227825Stheraven}
789227825Stheraven
790227825Stheraventemplate <size_t _Size>
791227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
792227825Stheravenbitset<_Size>&
793227825Stheravenbitset<_Size>::set() _NOEXCEPT
794227825Stheraven{
795227825Stheraven    _VSTD::fill_n(base::__make_iter(0), _Size, true);
796227825Stheraven    return *this;
797227825Stheraven}
798227825Stheraven
799227825Stheraventemplate <size_t _Size>
800227825Stheravenbitset<_Size>&
801227825Stheravenbitset<_Size>::set(size_t __pos, bool __val)
802227825Stheraven{
803227825Stheraven    if (__pos >= _Size)
804227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
805227825Stheraven        throw out_of_range("bitset set argument out of range");
806227825Stheraven#else
807227825Stheraven        assert(!"bitset set argument out of range");
808227825Stheraven#endif
809227825Stheraven    (*this)[__pos] = __val;
810227825Stheraven    return *this;
811227825Stheraven}
812227825Stheraven
813227825Stheraventemplate <size_t _Size>
814227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
815227825Stheravenbitset<_Size>&
816227825Stheravenbitset<_Size>::reset() _NOEXCEPT
817227825Stheraven{
818227825Stheraven    _VSTD::fill_n(base::__make_iter(0), _Size, false);
819227825Stheraven    return *this;
820227825Stheraven}
821227825Stheraven
822227825Stheraventemplate <size_t _Size>
823227825Stheravenbitset<_Size>&
824227825Stheravenbitset<_Size>::reset(size_t __pos)
825227825Stheraven{
826227825Stheraven    if (__pos >= _Size)
827227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
828227825Stheraven        throw out_of_range("bitset reset argument out of range");
829227825Stheraven#else
830227825Stheraven        assert(!"bitset reset argument out of range");
831227825Stheraven#endif
832227825Stheraven    (*this)[__pos] = false;
833227825Stheraven    return *this;
834227825Stheraven}
835227825Stheraven
836227825Stheraventemplate <size_t _Size>
837227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
838227825Stheravenbitset<_Size>
839227825Stheravenbitset<_Size>::operator~() const _NOEXCEPT
840227825Stheraven{
841227825Stheraven    bitset __x(*this);
842227825Stheraven    __x.flip();
843227825Stheraven    return __x;
844227825Stheraven}
845227825Stheraven
846227825Stheraventemplate <size_t _Size>
847227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
848227825Stheravenbitset<_Size>&
849227825Stheravenbitset<_Size>::flip() _NOEXCEPT
850227825Stheraven{
851227825Stheraven    base::flip();
852227825Stheraven    return *this;
853227825Stheraven}
854227825Stheraven
855227825Stheraventemplate <size_t _Size>
856227825Stheravenbitset<_Size>&
857227825Stheravenbitset<_Size>::flip(size_t __pos)
858227825Stheraven{
859227825Stheraven    if (__pos >= _Size)
860227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
861227825Stheraven        throw out_of_range("bitset flip argument out of range");
862227825Stheraven#else
863227825Stheraven        assert(!"bitset flip argument out of range");
864227825Stheraven#endif
865227825Stheraven    reference r = base::__make_ref(__pos);
866227825Stheraven    r = ~r;
867227825Stheraven    return *this;
868227825Stheraven}
869227825Stheraven
870227825Stheraventemplate <size_t _Size>
871227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
872227825Stheravenunsigned long
873227825Stheravenbitset<_Size>::to_ulong() const
874227825Stheraven{
875227825Stheraven    return base::to_ulong();
876227825Stheraven}
877227825Stheraven
878227825Stheraventemplate <size_t _Size>
879227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
880227825Stheravenunsigned long long
881227825Stheravenbitset<_Size>::to_ullong() const
882227825Stheraven{
883227825Stheraven    return base::to_ullong();
884227825Stheraven}
885227825Stheraven
886227825Stheraventemplate <size_t _Size>
887227825Stheraventemplate <class _CharT, class _Traits, class _Allocator>
888227825Stheravenbasic_string<_CharT, _Traits, _Allocator>
889227825Stheravenbitset<_Size>::to_string(_CharT __zero, _CharT __one) const
890227825Stheraven{
891227825Stheraven    basic_string<_CharT, _Traits, _Allocator> __r(_Size, __zero);
892227825Stheraven    for (size_t __i = 0; __i < _Size; ++__i)
893227825Stheraven    {
894227825Stheraven        if ((*this)[__i])
895227825Stheraven            __r[_Size - 1 - __i] = __one;
896227825Stheraven    }
897227825Stheraven    return __r;
898227825Stheraven}
899227825Stheraven
900227825Stheraventemplate <size_t _Size>
901227825Stheraventemplate <class _CharT, class _Traits>
902227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
903227825Stheravenbasic_string<_CharT, _Traits, allocator<_CharT> >
904227825Stheravenbitset<_Size>::to_string(_CharT __zero, _CharT __one) const
905227825Stheraven{
906227825Stheraven    return to_string<_CharT, _Traits, allocator<_CharT> >(__zero, __one);
907227825Stheraven}
908227825Stheraven
909227825Stheraventemplate <size_t _Size>
910227825Stheraventemplate <class _CharT>
911227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
912227825Stheravenbasic_string<_CharT, char_traits<_CharT>, allocator<_CharT> >
913227825Stheravenbitset<_Size>::to_string(_CharT __zero, _CharT __one) const
914227825Stheraven{
915227825Stheraven    return to_string<_CharT, char_traits<_CharT>, allocator<_CharT> >(__zero, __one);
916227825Stheraven}
917227825Stheraven
918227825Stheraventemplate <size_t _Size>
919227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
920227825Stheravenbasic_string<char, char_traits<char>, allocator<char> >
921227825Stheravenbitset<_Size>::to_string(char __zero, char __one) const
922227825Stheraven{
923227825Stheraven    return to_string<char, char_traits<char>, allocator<char> >(__zero, __one);
924227825Stheraven}
925227825Stheraven
926227825Stheraventemplate <size_t _Size>
927227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
928227825Stheravensize_t
929227825Stheravenbitset<_Size>::count() const _NOEXCEPT
930227825Stheraven{
931227825Stheraven    return static_cast<size_t>(_VSTD::count(base::__make_iter(0), base::__make_iter(_Size), true));
932227825Stheraven}
933227825Stheraven
934227825Stheraventemplate <size_t _Size>
935227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
936227825Stheravenbool
937227825Stheravenbitset<_Size>::operator==(const bitset& __rhs) const _NOEXCEPT
938227825Stheraven{
939227825Stheraven    return _VSTD::equal(base::__make_iter(0), base::__make_iter(_Size), __rhs.__make_iter(0));
940227825Stheraven}
941227825Stheraven
942227825Stheraventemplate <size_t _Size>
943227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
944227825Stheravenbool
945227825Stheravenbitset<_Size>::operator!=(const bitset& __rhs) const _NOEXCEPT
946227825Stheraven{
947227825Stheraven    return !(*this == __rhs);
948227825Stheraven}
949227825Stheraven
950227825Stheraventemplate <size_t _Size>
951227825Stheravenbool
952227825Stheravenbitset<_Size>::test(size_t __pos) const
953227825Stheraven{
954227825Stheraven    if (__pos >= _Size)
955227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
956227825Stheraven        throw out_of_range("bitset test argument out of range");
957227825Stheraven#else
958227825Stheraven        assert(!"bitset test argument out of range");
959227825Stheraven#endif
960227825Stheraven    return (*this)[__pos];
961227825Stheraven}
962227825Stheraven
963227825Stheraventemplate <size_t _Size>
964227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
965227825Stheravenbool
966227825Stheravenbitset<_Size>::all() const _NOEXCEPT
967227825Stheraven{
968227825Stheraven    return base::all();
969227825Stheraven}
970227825Stheraven
971227825Stheraventemplate <size_t _Size>
972227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
973227825Stheravenbool
974227825Stheravenbitset<_Size>::any() const _NOEXCEPT
975227825Stheraven{
976227825Stheraven    return base::any();
977227825Stheraven}
978227825Stheraven
979227825Stheraventemplate <size_t _Size>
980227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
981227825Stheravenbitset<_Size>
982227825Stheravenbitset<_Size>::operator<<(size_t __pos) const _NOEXCEPT
983227825Stheraven{
984227825Stheraven    bitset __r = *this;
985227825Stheraven    __r <<= __pos;
986227825Stheraven    return __r;
987227825Stheraven}
988227825Stheraven
989227825Stheraventemplate <size_t _Size>
990227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
991227825Stheravenbitset<_Size>
992227825Stheravenbitset<_Size>::operator>>(size_t __pos) const _NOEXCEPT
993227825Stheraven{
994227825Stheraven    bitset __r = *this;
995227825Stheraven    __r >>= __pos;
996227825Stheraven    return __r;
997227825Stheraven}
998227825Stheraven
999227825Stheraventemplate <size_t _Size>
1000227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1001227825Stheravenbitset<_Size>
1002227825Stheravenoperator&(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1003227825Stheraven{
1004227825Stheraven    bitset<_Size> __r = __x;
1005227825Stheraven    __r &= __y;
1006227825Stheraven    return __r;
1007227825Stheraven}
1008227825Stheraven
1009227825Stheraventemplate <size_t _Size>
1010227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1011227825Stheravenbitset<_Size>
1012227825Stheravenoperator|(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1013227825Stheraven{
1014227825Stheraven    bitset<_Size> __r = __x;
1015227825Stheraven    __r |= __y;
1016227825Stheraven    return __r;
1017227825Stheraven}
1018227825Stheraven
1019227825Stheraventemplate <size_t _Size>
1020227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
1021227825Stheravenbitset<_Size>
1022227825Stheravenoperator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT
1023227825Stheraven{
1024227825Stheraven    bitset<_Size> __r = __x;
1025227825Stheraven    __r ^= __y;
1026227825Stheraven    return __r;
1027227825Stheraven}
1028227825Stheraven
1029227825Stheraventemplate <size_t _Size>
1030227825Stheravenstruct _LIBCPP_VISIBLE hash<bitset<_Size> >
1031227825Stheraven    : public unary_function<bitset<_Size>, size_t>
1032227825Stheraven{
1033227825Stheraven    _LIBCPP_INLINE_VISIBILITY
1034227825Stheraven    size_t operator()(const bitset<_Size>& __bs) const _NOEXCEPT
1035227825Stheraven        {return __bs.__hash_code();}
1036227825Stheraven};
1037227825Stheraven
1038227825Stheraventemplate <class _CharT, class _Traits, size_t _Size>
1039227825Stheravenbasic_istream<_CharT, _Traits>&
1040227825Stheravenoperator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x);
1041227825Stheraven
1042227825Stheraventemplate <class _CharT, class _Traits, size_t _Size>
1043227825Stheravenbasic_ostream<_CharT, _Traits>&
1044227825Stheravenoperator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x);
1045227825Stheraven
1046227825Stheraven_LIBCPP_END_NAMESPACE_STD
1047227825Stheraven
1048227825Stheraven#endif  // _LIBCPP_BITSET
1049