2 //===----------------------------------------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP___BIT_REFERENCE
12 #define _LIBCPP___BIT_REFERENCE
17 #include <__undef_min_max>
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 #pragma GCC system_header
23 _LIBCPP_BEGIN_NAMESPACE_STD
25 template <class _Cp, bool _IsConst> class __bit_iterator;
26 template <class _Cp> class __bit_const_reference;
29 struct __has_storage_type
31 static const bool value = false;
34 template <class _Cp, bool = __has_storage_type<_Cp>::value>
37 typedef typename _Cp::__storage_type __storage_type;
38 typedef typename _Cp::__storage_pointer __storage_pointer;
40 __storage_pointer __seg_;
41 __storage_type __mask_;
43 #if defined(__clang__)
44 friend typename _Cp::__self;
46 friend class _Cp::__self;
48 friend class __bit_const_reference<_Cp>;
49 friend class __bit_iterator<_Cp, false>;
51 _LIBCPP_INLINE_VISIBILITY operator bool() const _NOEXCEPT
52 {return static_cast<bool>(*__seg_ & __mask_);}
53 _LIBCPP_INLINE_VISIBILITY bool operator ~() const _NOEXCEPT
54 {return !static_cast<bool>(*this);}
56 _LIBCPP_INLINE_VISIBILITY
57 __bit_reference& operator=(bool __x) _NOEXCEPT
66 _LIBCPP_INLINE_VISIBILITY
67 __bit_reference& operator=(const __bit_reference& __x) _NOEXCEPT
68 {return operator=(static_cast<bool>(__x));}
70 _LIBCPP_INLINE_VISIBILITY void flip() _NOEXCEPT {*__seg_ ^= __mask_;}
71 _LIBCPP_INLINE_VISIBILITY __bit_iterator<_Cp, false> operator&() const _NOEXCEPT
72 {return __bit_iterator<_Cp, false>(__seg_, static_cast<unsigned>(__ctz(__mask_)));}
74 _LIBCPP_INLINE_VISIBILITY
75 __bit_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT
76 : __seg_(__s), __mask_(__m) {}
80 class __bit_reference<_Cp, false>
84 template <class _Cp, class _Dp>
85 _LIBCPP_INLINE_VISIBILITY inline
87 swap(__bit_reference<_Cp> __x, __bit_reference<_Dp> __y) _NOEXCEPT
95 _LIBCPP_INLINE_VISIBILITY inline
97 swap(__bit_reference<_Cp> __x, bool& __y) _NOEXCEPT
105 _LIBCPP_INLINE_VISIBILITY inline
107 swap(bool& __x, __bit_reference<_Cp> __y) _NOEXCEPT
115 class __bit_const_reference
117 typedef typename _Cp::__storage_type __storage_type;
118 typedef typename _Cp::__const_storage_pointer __storage_pointer;
120 __storage_pointer __seg_;
121 __storage_type __mask_;
123 #if defined(__clang__)
124 friend typename _Cp::__self;
126 friend class _Cp::__self;
128 friend class __bit_iterator<_Cp, true>;
130 _LIBCPP_INLINE_VISIBILITY
131 __bit_const_reference(const __bit_reference<_Cp>& __x) _NOEXCEPT
132 : __seg_(__x.__seg_), __mask_(__x.__mask_) {}
134 _LIBCPP_INLINE_VISIBILITY operator bool() const _NOEXCEPT
135 {return static_cast<bool>(*__seg_ & __mask_);}
137 _LIBCPP_INLINE_VISIBILITY __bit_iterator<_Cp, true> operator&() const _NOEXCEPT
138 {return __bit_iterator<_Cp, true>(__seg_, static_cast<unsigned>(__ctz(__mask_)));}
140 _LIBCPP_INLINE_VISIBILITY
141 __bit_const_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT
142 : __seg_(__s), __mask_(__m) {}
144 __bit_const_reference& operator=(const __bit_const_reference& __x);
150 __bit_iterator<_Cp, false>
151 __find_bool_true(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n)
153 typedef __bit_iterator<_Cp, false> _It;
154 typedef typename _It::__storage_type __storage_type;
155 static const unsigned __bits_per_word = _It::__bits_per_word;
156 // do first partial word
157 if (__first.__ctz_ != 0)
159 __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
160 __storage_type __dn = _VSTD::min(__clz_f, __n);
161 __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
162 __storage_type __b = *__first.__seg_ & __m;
164 return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(__b)));
168 // do middle whole words
169 for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
171 return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(*__first.__seg_)));
172 // do last partial word
175 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
176 __storage_type __b = *__first.__seg_ & __m;
178 return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(__b)));
180 return _It(__first.__seg_, static_cast<unsigned>(__n));
184 __bit_iterator<_Cp, false>
185 __find_bool_false(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n)
187 typedef __bit_iterator<_Cp, false> _It;
188 typedef typename _It::__storage_type __storage_type;
189 static const unsigned __bits_per_word = _It::__bits_per_word;
190 // do first partial word
191 if (__first.__ctz_ != 0)
193 __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
194 __storage_type __dn = _VSTD::min(__clz_f, __n);
195 __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
196 __storage_type __b = ~(*__first.__seg_ & __m);
198 return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(__b)));
202 // do middle whole words
203 for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
205 __storage_type __b = ~*__first.__seg_;
207 return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(__b)));
209 // do last partial word
212 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
213 __storage_type __b = ~(*__first.__seg_ & __m);
215 return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(__b)));
217 return _It(__first.__seg_, static_cast<unsigned>(__n));
220 template <class _Cp, class _Tp>
221 inline _LIBCPP_INLINE_VISIBILITY
222 __bit_iterator<_Cp, false>
223 find(__bit_iterator<_Cp, false> __first, __bit_iterator<_Cp, false> __last, const _Tp& __value_)
225 if (static_cast<bool>(__value_))
226 return __find_bool_true(__first, static_cast<typename _Cp::size_type>(__last - __first));
227 return __find_bool_false(__first, static_cast<typename _Cp::size_type>(__last - __first));
233 typename __bit_iterator<_Cp, false>::difference_type
234 __count_bool_true(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n)
236 typedef __bit_iterator<_Cp, false> _It;
237 typedef typename _It::__storage_type __storage_type;
238 typedef typename _It::difference_type difference_type;
239 static const unsigned __bits_per_word = _It::__bits_per_word;
240 difference_type __r = 0;
241 // do first partial word
242 if (__first.__ctz_ != 0)
244 __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
245 __storage_type __dn = _VSTD::min(__clz_f, __n);
246 __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
247 __r = _VSTD::__pop_count(*__first.__seg_ & __m);
251 // do middle whole words
252 for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
253 __r += _VSTD::__pop_count(*__first.__seg_);
254 // do last partial word
257 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
258 __r += _VSTD::__pop_count(*__first.__seg_ & __m);
264 typename __bit_iterator<_Cp, false>::difference_type
265 __count_bool_false(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n)
267 typedef __bit_iterator<_Cp, false> _It;
268 typedef typename _It::__storage_type __storage_type;
269 typedef typename _It::difference_type difference_type;
270 static const unsigned __bits_per_word = _It::__bits_per_word;
271 difference_type __r = 0;
272 // do first partial word
273 if (__first.__ctz_ != 0)
275 __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
276 __storage_type __dn = _VSTD::min(__clz_f, __n);
277 __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
278 __r = _VSTD::__pop_count(~(*__first.__seg_ & __m));
282 // do middle whole words
283 for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
284 __r += _VSTD::__pop_count(~*__first.__seg_);
285 // do last partial word
288 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
289 __r += _VSTD::__pop_count(~(*__first.__seg_ & __m));
294 template <class _Cp, class _Tp>
295 inline _LIBCPP_INLINE_VISIBILITY
296 typename __bit_iterator<_Cp, false>::difference_type
297 count(__bit_iterator<_Cp, false> __first, __bit_iterator<_Cp, false> __last, const _Tp& __value_)
299 if (static_cast<bool>(__value_))
300 return __count_bool_true(__first, static_cast<typename _Cp::size_type>(__last - __first));
301 return __count_bool_false(__first, static_cast<typename _Cp::size_type>(__last - __first));
308 __fill_n_false(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n)
310 typedef __bit_iterator<_Cp, false> _It;
311 typedef typename _It::__storage_type __storage_type;
312 static const unsigned __bits_per_word = _It::__bits_per_word;
313 // do first partial word
314 if (__first.__ctz_ != 0)
316 __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
317 __storage_type __dn = _VSTD::min(__clz_f, __n);
318 __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
319 *__first.__seg_ &= ~__m;
323 // do middle whole words
324 __storage_type __nw = __n / __bits_per_word;
325 _VSTD::memset(__first.__seg_, 0, __nw * sizeof(__storage_type));
326 __n -= __nw * __bits_per_word;
327 // do last partial word
330 __first.__seg_ += __nw;
331 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
332 *__first.__seg_ &= ~__m;
338 __fill_n_true(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n)
340 typedef __bit_iterator<_Cp, false> _It;
341 typedef typename _It::__storage_type __storage_type;
342 static const unsigned __bits_per_word = _It::__bits_per_word;
343 // do first partial word
344 if (__first.__ctz_ != 0)
346 __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
347 __storage_type __dn = _VSTD::min(__clz_f, __n);
348 __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
349 *__first.__seg_ |= __m;
353 // do middle whole words
354 __storage_type __nw = __n / __bits_per_word;
355 _VSTD::memset(__first.__seg_, -1, __nw * sizeof(__storage_type));
356 __n -= __nw * __bits_per_word;
357 // do last partial word
360 __first.__seg_ += __nw;
361 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
362 *__first.__seg_ |= __m;
367 _LIBCPP_INLINE_VISIBILITY inline
369 fill_n(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n, bool __value_)
374 __fill_n_true(__first, __n);
376 __fill_n_false(__first, __n);
383 inline _LIBCPP_INLINE_VISIBILITY
385 fill(__bit_iterator<_Cp, false> __first, __bit_iterator<_Cp, false> __last, bool __value_)
387 _VSTD::fill_n(__first, static_cast<typename _Cp::size_type>(__last - __first), __value_);
392 template <class _Cp, bool _IsConst>
393 __bit_iterator<_Cp, false>
394 __copy_aligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last,
395 __bit_iterator<_Cp, false> __result)
397 typedef __bit_iterator<_Cp, _IsConst> _In;
398 typedef typename _In::difference_type difference_type;
399 typedef typename _In::__storage_type __storage_type;
400 static const unsigned __bits_per_word = _In::__bits_per_word;
401 difference_type __n = __last - __first;
405 if (__first.__ctz_ != 0)
407 unsigned __clz = __bits_per_word - __first.__ctz_;
408 difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz), __n);
410 __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz - __dn));
411 __storage_type __b = *__first.__seg_ & __m;
412 *__result.__seg_ &= ~__m;
413 *__result.__seg_ |= __b;
414 __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
415 __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word);
417 // __first.__ctz_ = 0;
419 // __first.__ctz_ == 0;
421 __storage_type __nw = __n / __bits_per_word;
422 _VSTD::memmove(__result.__seg_, __first.__seg_, __nw * sizeof(__storage_type));
423 __n -= __nw * __bits_per_word;
424 __result.__seg_ += __nw;
428 __first.__seg_ += __nw;
429 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
430 __storage_type __b = *__first.__seg_ & __m;
431 *__result.__seg_ &= ~__m;
432 *__result.__seg_ |= __b;
433 __result.__ctz_ = static_cast<unsigned>(__n);
439 template <class _Cp, bool _IsConst>
440 __bit_iterator<_Cp, false>
441 __copy_unaligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last,
442 __bit_iterator<_Cp, false> __result)
444 typedef __bit_iterator<_Cp, _IsConst> _In;
445 typedef typename _In::difference_type difference_type;
446 typedef typename _In::__storage_type __storage_type;
447 static const unsigned __bits_per_word = _In::__bits_per_word;
448 difference_type __n = __last - __first;
452 if (__first.__ctz_ != 0)
454 unsigned __clz_f = __bits_per_word - __first.__ctz_;
455 difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz_f), __n);
457 __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
458 __storage_type __b = *__first.__seg_ & __m;
459 unsigned __clz_r = __bits_per_word - __result.__ctz_;
460 __storage_type __ddn = _VSTD::min<__storage_type>(__dn, __clz_r);
461 __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn));
462 *__result.__seg_ &= ~__m;
463 if (__result.__ctz_ > __first.__ctz_)
464 *__result.__seg_ |= __b << (__result.__ctz_ - __first.__ctz_);
466 *__result.__seg_ |= __b >> (__first.__ctz_ - __result.__ctz_);
467 __result.__seg_ += (__ddn + __result.__ctz_) / __bits_per_word;
468 __result.__ctz_ = static_cast<unsigned>((__ddn + __result.__ctz_) % __bits_per_word);
472 __m = ~__storage_type(0) >> (__bits_per_word - __dn);
473 *__result.__seg_ &= ~__m;
474 *__result.__seg_ |= __b >> (__first.__ctz_ + __ddn);
475 __result.__ctz_ = static_cast<unsigned>(__dn);
478 // __first.__ctz_ = 0;
480 // __first.__ctz_ == 0;
482 unsigned __clz_r = __bits_per_word - __result.__ctz_;
483 __storage_type __m = ~__storage_type(0) << __result.__ctz_;
484 for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_)
486 __storage_type __b = *__first.__seg_;
487 *__result.__seg_ &= ~__m;
488 *__result.__seg_ |= __b << __result.__ctz_;
490 *__result.__seg_ &= __m;
491 *__result.__seg_ |= __b >> __clz_r;
496 __m = ~__storage_type(0) >> (__bits_per_word - __n);
497 __storage_type __b = *__first.__seg_ & __m;
498 __storage_type __dn = _VSTD::min(__n, static_cast<difference_type>(__clz_r));
499 __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn));
500 *__result.__seg_ &= ~__m;
501 *__result.__seg_ |= __b << __result.__ctz_;
502 __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
503 __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word);
507 __m = ~__storage_type(0) >> (__bits_per_word - __n);
508 *__result.__seg_ &= ~__m;
509 *__result.__seg_ |= __b >> __dn;
510 __result.__ctz_ = static_cast<unsigned>(__n);
517 template <class _Cp, bool _IsConst>
518 inline _LIBCPP_INLINE_VISIBILITY
519 __bit_iterator<_Cp, false>
520 copy(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result)
522 if (__first.__ctz_ == __result.__ctz_)
523 return __copy_aligned(__first, __last, __result);
524 return __copy_unaligned(__first, __last, __result);
529 template <class _Cp, bool _IsConst>
530 __bit_iterator<_Cp, false>
531 __copy_backward_aligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last,
532 __bit_iterator<_Cp, false> __result)
534 typedef __bit_iterator<_Cp, _IsConst> _In;
535 typedef typename _In::difference_type difference_type;
536 typedef typename _In::__storage_type __storage_type;
537 static const unsigned __bits_per_word = _In::__bits_per_word;
538 difference_type __n = __last - __first;
542 if (__last.__ctz_ != 0)
544 difference_type __dn = _VSTD::min(static_cast<difference_type>(__last.__ctz_), __n);
546 unsigned __clz = __bits_per_word - __last.__ctz_;
547 __storage_type __m = (~__storage_type(0) << (__last.__ctz_ - __dn)) & (~__storage_type(0) >> __clz);
548 __storage_type __b = *__last.__seg_ & __m;
549 *__result.__seg_ &= ~__m;
550 *__result.__seg_ |= __b;
551 __result.__ctz_ = static_cast<unsigned>(((-__dn & (__bits_per_word - 1)) +
552 __result.__ctz_) % __bits_per_word);
555 // __last.__ctz_ == 0 || __n == 0
556 // __result.__ctz_ == 0 || __n == 0
558 __storage_type __nw = __n / __bits_per_word;
559 __result.__seg_ -= __nw;
560 __last.__seg_ -= __nw;
561 _VSTD::memmove(__result.__seg_, __last.__seg_, __nw * sizeof(__storage_type));
562 __n -= __nw * __bits_per_word;
566 __storage_type __m = ~__storage_type(0) << (__bits_per_word - __n);
567 __storage_type __b = *--__last.__seg_ & __m;
568 *--__result.__seg_ &= ~__m;
569 *__result.__seg_ |= __b;
570 __result.__ctz_ = static_cast<unsigned>(-__n & (__bits_per_word - 1));
576 template <class _Cp, bool _IsConst>
577 __bit_iterator<_Cp, false>
578 __copy_backward_unaligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last,
579 __bit_iterator<_Cp, false> __result)
581 typedef __bit_iterator<_Cp, _IsConst> _In;
582 typedef typename _In::difference_type difference_type;
583 typedef typename _In::__storage_type __storage_type;
584 static const unsigned __bits_per_word = _In::__bits_per_word;
585 difference_type __n = __last - __first;
589 if (__last.__ctz_ != 0)
591 difference_type __dn = _VSTD::min(static_cast<difference_type>(__last.__ctz_), __n);
593 unsigned __clz_l = __bits_per_word - __last.__ctz_;
594 __storage_type __m = (~__storage_type(0) << (__last.__ctz_ - __dn)) & (~__storage_type(0) >> __clz_l);
595 __storage_type __b = *__last.__seg_ & __m;
596 unsigned __clz_r = __bits_per_word - __result.__ctz_;
597 __storage_type __ddn = _VSTD::min(__dn, static_cast<difference_type>(__result.__ctz_));
600 __m = (~__storage_type(0) << (__result.__ctz_ - __ddn)) & (~__storage_type(0) >> __clz_r);
601 *__result.__seg_ &= ~__m;
602 if (__result.__ctz_ > __last.__ctz_)
603 *__result.__seg_ |= __b << (__result.__ctz_ - __last.__ctz_);
605 *__result.__seg_ |= __b >> (__last.__ctz_ - __result.__ctz_);
606 __result.__ctz_ = static_cast<unsigned>(((-__ddn & (__bits_per_word - 1)) +
607 __result.__ctz_) % __bits_per_word);
612 // __result.__ctz_ == 0
614 __result.__ctz_ = static_cast<unsigned>(-__dn & (__bits_per_word - 1));
615 __m = ~__storage_type(0) << __result.__ctz_;
616 *__result.__seg_ &= ~__m;
617 __last.__ctz_ -= __dn + __ddn;
618 *__result.__seg_ |= __b << (__result.__ctz_ - __last.__ctz_);
622 // __last.__ctz_ == 0 || __n == 0
623 // __result.__ctz_ != 0 || __n == 0
625 unsigned __clz_r = __bits_per_word - __result.__ctz_;
626 __storage_type __m = ~__storage_type(0) >> __clz_r;
627 for (; __n >= __bits_per_word; __n -= __bits_per_word)
629 __storage_type __b = *--__last.__seg_;
630 *__result.__seg_ &= ~__m;
631 *__result.__seg_ |= __b >> __clz_r;
632 *--__result.__seg_ &= __m;
633 *__result.__seg_ |= __b << __result.__ctz_;
638 __m = ~__storage_type(0) << (__bits_per_word - __n);
639 __storage_type __b = *--__last.__seg_ & __m;
640 __clz_r = __bits_per_word - __result.__ctz_;
641 __storage_type __dn = _VSTD::min(__n, static_cast<difference_type>(__result.__ctz_));
642 __m = (~__storage_type(0) << (__result.__ctz_ - __dn)) & (~__storage_type(0) >> __clz_r);
643 *__result.__seg_ &= ~__m;
644 *__result.__seg_ |= __b >> (__bits_per_word - __result.__ctz_);
645 __result.__ctz_ = static_cast<unsigned>(((-__dn & (__bits_per_word - 1)) +
646 __result.__ctz_) % __bits_per_word);
650 // __result.__ctz_ == 0
652 __result.__ctz_ = static_cast<unsigned>(-__n & (__bits_per_word - 1));
653 __m = ~__storage_type(0) << __result.__ctz_;
654 *__result.__seg_ &= ~__m;
655 *__result.__seg_ |= __b << (__result.__ctz_ - (__bits_per_word - __n - __dn));
662 template <class _Cp, bool _IsConst>
663 inline _LIBCPP_INLINE_VISIBILITY
664 __bit_iterator<_Cp, false>
665 copy_backward(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result)
667 if (__last.__ctz_ == __result.__ctz_)
668 return __copy_backward_aligned(__first, __last, __result);
669 return __copy_backward_unaligned(__first, __last, __result);
674 template <class _Cp, bool _IsConst>
675 inline _LIBCPP_INLINE_VISIBILITY
676 __bit_iterator<_Cp, false>
677 move(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result)
679 return _VSTD::copy(__first, __last, __result);
684 template <class _Cp, bool _IsConst>
685 inline _LIBCPP_INLINE_VISIBILITY
686 __bit_iterator<_Cp, false>
687 move_backward(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result)
689 return _VSTD::copy(__first, __last, __result);
694 template <class __C1, class __C2>
695 __bit_iterator<__C2, false>
696 __swap_ranges_aligned(__bit_iterator<__C1, false> __first, __bit_iterator<__C1, false> __last,
697 __bit_iterator<__C2, false> __result)
699 typedef __bit_iterator<__C1, false> _I1;
700 typedef typename _I1::difference_type difference_type;
701 typedef typename _I1::__storage_type __storage_type;
702 static const unsigned __bits_per_word = _I1::__bits_per_word;
703 difference_type __n = __last - __first;
707 if (__first.__ctz_ != 0)
709 unsigned __clz = __bits_per_word - __first.__ctz_;
710 difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz), __n);
712 __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz - __dn));
713 __storage_type __b1 = *__first.__seg_ & __m;
714 *__first.__seg_ &= ~__m;
715 __storage_type __b2 = *__result.__seg_ & __m;
716 *__result.__seg_ &= ~__m;
717 *__result.__seg_ |= __b1;
718 *__first.__seg_ |= __b2;
719 __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
720 __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word);
722 // __first.__ctz_ = 0;
724 // __first.__ctz_ == 0;
726 for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_, ++__result.__seg_)
727 swap(*__first.__seg_, *__result.__seg_);
731 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
732 __storage_type __b1 = *__first.__seg_ & __m;
733 *__first.__seg_ &= ~__m;
734 __storage_type __b2 = *__result.__seg_ & __m;
735 *__result.__seg_ &= ~__m;
736 *__result.__seg_ |= __b1;
737 *__first.__seg_ |= __b2;
738 __result.__ctz_ = static_cast<unsigned>(__n);
744 template <class __C1, class __C2>
745 __bit_iterator<__C2, false>
746 __swap_ranges_unaligned(__bit_iterator<__C1, false> __first, __bit_iterator<__C1, false> __last,
747 __bit_iterator<__C2, false> __result)
749 typedef __bit_iterator<__C1, false> _I1;
750 typedef typename _I1::difference_type difference_type;
751 typedef typename _I1::__storage_type __storage_type;
752 static const unsigned __bits_per_word = _I1::__bits_per_word;
753 difference_type __n = __last - __first;
757 if (__first.__ctz_ != 0)
759 unsigned __clz_f = __bits_per_word - __first.__ctz_;
760 difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz_f), __n);
762 __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
763 __storage_type __b1 = *__first.__seg_ & __m;
764 *__first.__seg_ &= ~__m;
765 unsigned __clz_r = __bits_per_word - __result.__ctz_;
766 __storage_type __ddn = _VSTD::min<__storage_type>(__dn, __clz_r);
767 __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn));
768 __storage_type __b2 = *__result.__seg_ & __m;
769 *__result.__seg_ &= ~__m;
770 if (__result.__ctz_ > __first.__ctz_)
772 unsigned __s = __result.__ctz_ - __first.__ctz_;
773 *__result.__seg_ |= __b1 << __s;
774 *__first.__seg_ |= __b2 >> __s;
778 unsigned __s = __first.__ctz_ - __result.__ctz_;
779 *__result.__seg_ |= __b1 >> __s;
780 *__first.__seg_ |= __b2 << __s;
782 __result.__seg_ += (__ddn + __result.__ctz_) / __bits_per_word;
783 __result.__ctz_ = static_cast<unsigned>((__ddn + __result.__ctz_) % __bits_per_word);
787 __m = ~__storage_type(0) >> (__bits_per_word - __dn);
788 __b2 = *__result.__seg_ & __m;
789 *__result.__seg_ &= ~__m;
790 unsigned __s = __first.__ctz_ + __ddn;
791 *__result.__seg_ |= __b1 >> __s;
792 *__first.__seg_ |= __b2 << __s;
793 __result.__ctz_ = static_cast<unsigned>(__dn);
796 // __first.__ctz_ = 0;
798 // __first.__ctz_ == 0;
800 __storage_type __m = ~__storage_type(0) << __result.__ctz_;
801 unsigned __clz_r = __bits_per_word - __result.__ctz_;
802 for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_)
804 __storage_type __b1 = *__first.__seg_;
805 __storage_type __b2 = *__result.__seg_ & __m;
806 *__result.__seg_ &= ~__m;
807 *__result.__seg_ |= __b1 << __result.__ctz_;
808 *__first.__seg_ = __b2 >> __result.__ctz_;
810 __b2 = *__result.__seg_ & ~__m;
811 *__result.__seg_ &= __m;
812 *__result.__seg_ |= __b1 >> __clz_r;
813 *__first.__seg_ |= __b2 << __clz_r;
818 __m = ~__storage_type(0) >> (__bits_per_word - __n);
819 __storage_type __b1 = *__first.__seg_ & __m;
820 *__first.__seg_ &= ~__m;
821 __storage_type __dn = _VSTD::min<__storage_type>(__n, __clz_r);
822 __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn));
823 __storage_type __b2 = *__result.__seg_ & __m;
824 *__result.__seg_ &= ~__m;
825 *__result.__seg_ |= __b1 << __result.__ctz_;
826 *__first.__seg_ |= __b2 >> __result.__ctz_;
827 __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
828 __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word);
832 __m = ~__storage_type(0) >> (__bits_per_word - __n);
833 __b2 = *__result.__seg_ & __m;
834 *__result.__seg_ &= ~__m;
835 *__result.__seg_ |= __b1 >> __dn;
836 *__first.__seg_ |= __b2 << __dn;
837 __result.__ctz_ = static_cast<unsigned>(__n);
844 template <class __C1, class __C2>
845 inline _LIBCPP_INLINE_VISIBILITY
846 __bit_iterator<__C2, false>
847 swap_ranges(__bit_iterator<__C1, false> __first1, __bit_iterator<__C1, false> __last1,
848 __bit_iterator<__C2, false> __first2)
850 if (__first1.__ctz_ == __first2.__ctz_)
851 return __swap_ranges_aligned(__first1, __last1, __first2);
852 return __swap_ranges_unaligned(__first1, __last1, __first2);
860 typedef typename _Cp::difference_type difference_type;
861 typedef typename _Cp::__storage_type __storage_type;
862 typedef typename _Cp::iterator iterator;
863 static const unsigned __bits_per_word = _Cp::__bits_per_word;
864 static const unsigned _Np = 4;
866 difference_type __size_;
867 __storage_type __word_[_Np];
869 _LIBCPP_INLINE_VISIBILITY static difference_type capacity()
870 {return static_cast<difference_type>(_Np * __bits_per_word);}
871 _LIBCPP_INLINE_VISIBILITY explicit __bit_array(difference_type __s) : __size_(__s) {}
872 _LIBCPP_INLINE_VISIBILITY iterator begin() {return iterator(__word_, 0);}
873 _LIBCPP_INLINE_VISIBILITY iterator end() {return iterator(__word_ + __size_ / __bits_per_word,
874 static_cast<unsigned>(__size_ % __bits_per_word));}
878 __bit_iterator<_Cp, false>
879 rotate(__bit_iterator<_Cp, false> __first, __bit_iterator<_Cp, false> __middle, __bit_iterator<_Cp, false> __last)
881 typedef __bit_iterator<_Cp, false> _I1;
882 typedef typename _I1::difference_type difference_type;
883 typedef typename _I1::__storage_type __storage_type;
884 difference_type __d1 = __middle - __first;
885 difference_type __d2 = __last - __middle;
886 _I1 __r = __first + __d2;
887 while (__d1 != 0 && __d2 != 0)
891 if (__d1 <= __bit_array<_Cp>::capacity())
893 __bit_array<_Cp> __b(__d1);
894 _VSTD::copy(__first, __middle, __b.begin());
895 _VSTD::copy(__b.begin(), __b.end(), _VSTD::copy(__middle, __last, __first));
900 __bit_iterator<_Cp, false> __mp = _VSTD::swap_ranges(__first, __middle, __middle);
908 if (__d2 <= __bit_array<_Cp>::capacity())
910 __bit_array<_Cp> __b(__d2);
911 _VSTD::copy(__middle, __last, __b.begin());
912 _VSTD::copy_backward(__b.begin(), __b.end(), _VSTD::copy_backward(__first, __middle, __last));
917 __bit_iterator<_Cp, false> __mp = __first + __d2;
918 _VSTD::swap_ranges(__first, __mp, __middle);
931 __equal_unaligned(__bit_iterator<_Cp, true> __first1, __bit_iterator<_Cp, true> __last1,
932 __bit_iterator<_Cp, true> __first2)
934 typedef __bit_iterator<_Cp, true> _It;
935 typedef typename _It::difference_type difference_type;
936 typedef typename _It::__storage_type __storage_type;
937 static const unsigned __bits_per_word = _It::__bits_per_word;
938 difference_type __n = __last1 - __first1;
942 if (__first1.__ctz_ != 0)
944 unsigned __clz_f = __bits_per_word - __first1.__ctz_;
945 difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz_f), __n);
947 __storage_type __m = (~__storage_type(0) << __first1.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
948 __storage_type __b = *__first1.__seg_ & __m;
949 unsigned __clz_r = __bits_per_word - __first2.__ctz_;
950 __storage_type __ddn = _VSTD::min<__storage_type>(__dn, __clz_r);
951 __m = (~__storage_type(0) << __first2.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn));
952 if (__first2.__ctz_ > __first1.__ctz_)
953 if ((*__first2.__seg_ & __m) != (__b << (__first2.__ctz_ - __first1.__ctz_)))
956 if ((*__first2.__seg_ & __m) != (__b >> (__first1.__ctz_ - __first2.__ctz_)))
958 __first2.__seg_ += (__ddn + __first2.__ctz_) / __bits_per_word;
959 __first2.__ctz_ = static_cast<unsigned>((__ddn + __first2.__ctz_) % __bits_per_word);
963 __m = ~__storage_type(0) >> (__bits_per_word - __dn);
964 if ((*__first2.__seg_ & __m) != (__b >> (__first1.__ctz_ + __ddn)))
966 __first2.__ctz_ = static_cast<unsigned>(__dn);
969 // __first1.__ctz_ = 0;
971 // __first1.__ctz_ == 0;
973 unsigned __clz_r = __bits_per_word - __first2.__ctz_;
974 __storage_type __m = ~__storage_type(0) << __first2.__ctz_;
975 for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first1.__seg_)
977 __storage_type __b = *__first1.__seg_;
978 if ((*__first2.__seg_ & __m) != (__b << __first2.__ctz_))
981 if ((*__first2.__seg_ & ~__m) != (__b >> __clz_r))
987 __m = ~__storage_type(0) >> (__bits_per_word - __n);
988 __storage_type __b = *__first1.__seg_ & __m;
989 __storage_type __dn = _VSTD::min(__n, static_cast<difference_type>(__clz_r));
990 __m = (~__storage_type(0) << __first2.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn));
991 if ((*__first2.__seg_ & __m) != (__b << __first2.__ctz_))
993 __first2.__seg_ += (__dn + __first2.__ctz_) / __bits_per_word;
994 __first2.__ctz_ = static_cast<unsigned>((__dn + __first2.__ctz_) % __bits_per_word);
998 __m = ~__storage_type(0) >> (__bits_per_word - __n);
999 if ((*__first2.__seg_ & __m) != (__b >> __dn))
1007 template <class _Cp>
1009 __equal_aligned(__bit_iterator<_Cp, true> __first1, __bit_iterator<_Cp, true> __last1,
1010 __bit_iterator<_Cp, true> __first2)
1012 typedef __bit_iterator<_Cp, true> _It;
1013 typedef typename _It::difference_type difference_type;
1014 typedef typename _It::__storage_type __storage_type;
1015 static const unsigned __bits_per_word = _It::__bits_per_word;
1016 difference_type __n = __last1 - __first1;
1020 if (__first1.__ctz_ != 0)
1022 unsigned __clz = __bits_per_word - __first1.__ctz_;
1023 difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz), __n);
1025 __storage_type __m = (~__storage_type(0) << __first1.__ctz_) & (~__storage_type(0) >> (__clz - __dn));
1026 if ((*__first2.__seg_ & __m) != (*__first1.__seg_ & __m))
1030 // __first1.__ctz_ = 0;
1031 // __first2.__ctz_ = 0;
1033 // __first1.__ctz_ == 0;
1034 // __first2.__ctz_ == 0;
1036 for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first1.__seg_, ++__first2.__seg_)
1037 if (*__first2.__seg_ != *__first1.__seg_)
1042 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
1043 if ((*__first2.__seg_ & __m) != (*__first1.__seg_ & __m))
1050 template <class _Cp, bool _IC1, bool _IC2>
1051 inline _LIBCPP_INLINE_VISIBILITY
1053 equal(__bit_iterator<_Cp, _IC1> __first1, __bit_iterator<_Cp, _IC1> __last1, __bit_iterator<_Cp, _IC2> __first2)
1055 if (__first1.__ctz_ == __first2.__ctz_)
1056 return __equal_aligned(__first1, __last1, __first2);
1057 return __equal_unaligned(__first1, __last1, __first2);
1060 template <class _Cp, bool _IsConst>
1061 class __bit_iterator
1064 typedef typename _Cp::difference_type difference_type;
1065 typedef bool value_type;
1066 typedef __bit_iterator pointer;
1067 typedef typename conditional<_IsConst, __bit_const_reference<_Cp>, __bit_reference<_Cp> >::type reference;
1068 typedef random_access_iterator_tag iterator_category;
1071 typedef typename _Cp::__storage_type __storage_type;
1072 typedef typename conditional<_IsConst, typename _Cp::__const_storage_pointer,
1073 typename _Cp::__storage_pointer>::type __storage_pointer;
1074 static const unsigned __bits_per_word = _Cp::__bits_per_word;
1076 __storage_pointer __seg_;
1080 _LIBCPP_INLINE_VISIBILITY __bit_iterator() _NOEXCEPT {}
1082 _LIBCPP_INLINE_VISIBILITY
1083 __bit_iterator(const __bit_iterator<_Cp, false>& __it) _NOEXCEPT
1084 : __seg_(__it.__seg_), __ctz_(__it.__ctz_) {}
1086 _LIBCPP_INLINE_VISIBILITY reference operator*() const _NOEXCEPT
1087 {return reference(__seg_, __storage_type(1) << __ctz_);}
1089 _LIBCPP_INLINE_VISIBILITY __bit_iterator& operator++()
1091 if (__ctz_ != __bits_per_word-1)
1101 _LIBCPP_INLINE_VISIBILITY __bit_iterator operator++(int)
1103 __bit_iterator __tmp = *this;
1108 _LIBCPP_INLINE_VISIBILITY __bit_iterator& operator--()
1114 __ctz_ = __bits_per_word - 1;
1120 _LIBCPP_INLINE_VISIBILITY __bit_iterator operator--(int)
1122 __bit_iterator __tmp = *this;
1127 _LIBCPP_INLINE_VISIBILITY __bit_iterator& operator+=(difference_type __n)
1130 __seg_ += (__n + __ctz_) / __bits_per_word;
1132 __seg_ += static_cast<difference_type>(__n - __bits_per_word + __ctz_ + 1)
1133 / static_cast<difference_type>(__bits_per_word);
1134 __n &= (__bits_per_word - 1);
1135 __ctz_ = static_cast<unsigned>((__n + __ctz_) % __bits_per_word);
1139 _LIBCPP_INLINE_VISIBILITY __bit_iterator& operator-=(difference_type __n)
1141 return *this += -__n;
1144 _LIBCPP_INLINE_VISIBILITY __bit_iterator operator+(difference_type __n) const
1146 __bit_iterator __t(*this);
1151 _LIBCPP_INLINE_VISIBILITY __bit_iterator operator-(difference_type __n) const
1153 __bit_iterator __t(*this);
1158 _LIBCPP_INLINE_VISIBILITY
1159 friend __bit_iterator operator+(difference_type __n, const __bit_iterator& __it) {return __it + __n;}
1161 _LIBCPP_INLINE_VISIBILITY
1162 friend difference_type operator-(const __bit_iterator& __x, const __bit_iterator& __y)
1163 {return (__x.__seg_ - __y.__seg_) * __bits_per_word + __x.__ctz_ - __y.__ctz_;}
1165 _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const {return *(*this + __n);}
1167 _LIBCPP_INLINE_VISIBILITY friend bool operator==(const __bit_iterator& __x, const __bit_iterator& __y)
1168 {return __x.__seg_ == __y.__seg_ && __x.__ctz_ == __y.__ctz_;}
1170 _LIBCPP_INLINE_VISIBILITY friend bool operator!=(const __bit_iterator& __x, const __bit_iterator& __y)
1171 {return !(__x == __y);}
1173 _LIBCPP_INLINE_VISIBILITY friend bool operator<(const __bit_iterator& __x, const __bit_iterator& __y)
1174 {return __x.__seg_ < __y.__seg_ || (__x.__seg_ == __y.__seg_ && __x.__ctz_ < __y.__ctz_);}
1176 _LIBCPP_INLINE_VISIBILITY friend bool operator>(const __bit_iterator& __x, const __bit_iterator& __y)
1179 _LIBCPP_INLINE_VISIBILITY friend bool operator<=(const __bit_iterator& __x, const __bit_iterator& __y)
1180 {return !(__y < __x);}
1182 _LIBCPP_INLINE_VISIBILITY friend bool operator>=(const __bit_iterator& __x, const __bit_iterator& __y)
1183 {return !(__x < __y);}
1186 _LIBCPP_INLINE_VISIBILITY
1187 __bit_iterator(__storage_pointer __s, unsigned __ctz) _NOEXCEPT
1188 : __seg_(__s), __ctz_(__ctz) {}
1190 #if defined(__clang__)
1191 friend typename _Cp::__self;
1193 friend class _Cp::__self;
1195 friend class __bit_reference<_Cp>;
1196 friend class __bit_const_reference<_Cp>;
1197 friend class __bit_iterator<_Cp, true>;
1198 template <class _Dp> friend struct __bit_array;
1199 template <class _Dp> friend void __fill_n_false(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n);
1200 template <class _Dp> friend void __fill_n_true(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n);
1201 template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> __copy_aligned(__bit_iterator<_Dp, _IC> __first,
1202 __bit_iterator<_Dp, _IC> __last,
1203 __bit_iterator<_Dp, false> __result);
1204 template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> __copy_unaligned(__bit_iterator<_Dp, _IC> __first,
1205 __bit_iterator<_Dp, _IC> __last,
1206 __bit_iterator<_Dp, false> __result);
1207 template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> copy(__bit_iterator<_Dp, _IC> __first,
1208 __bit_iterator<_Dp, _IC> __last,
1209 __bit_iterator<_Dp, false> __result);
1210 template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> __copy_backward_aligned(__bit_iterator<_Dp, _IC> __first,
1211 __bit_iterator<_Dp, _IC> __last,
1212 __bit_iterator<_Dp, false> __result);
1213 template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> __copy_backward_unaligned(__bit_iterator<_Dp, _IC> __first,
1214 __bit_iterator<_Dp, _IC> __last,
1215 __bit_iterator<_Dp, false> __result);
1216 template <class _Dp, bool _IC> friend __bit_iterator<_Dp, false> copy_backward(__bit_iterator<_Dp, _IC> __first,
1217 __bit_iterator<_Dp, _IC> __last,
1218 __bit_iterator<_Dp, false> __result);
1219 template <class __C1, class __C2>friend __bit_iterator<__C2, false> __swap_ranges_aligned(__bit_iterator<__C1, false>,
1220 __bit_iterator<__C1, false>,
1221 __bit_iterator<__C2, false>);
1222 template <class __C1, class __C2>friend __bit_iterator<__C2, false> __swap_ranges_unaligned(__bit_iterator<__C1, false>,
1223 __bit_iterator<__C1, false>,
1224 __bit_iterator<__C2, false>);
1225 template <class __C1, class __C2>friend __bit_iterator<__C2, false> swap_ranges(__bit_iterator<__C1, false>,
1226 __bit_iterator<__C1, false>,
1227 __bit_iterator<__C2, false>);
1228 template <class _Dp> friend __bit_iterator<_Dp, false> rotate(__bit_iterator<_Dp, false>,
1229 __bit_iterator<_Dp, false>,
1230 __bit_iterator<_Dp, false>);
1231 template <class _Dp> friend bool __equal_aligned(__bit_iterator<_Dp, true>,
1232 __bit_iterator<_Dp, true>,
1233 __bit_iterator<_Dp, true>);
1234 template <class _Dp> friend bool __equal_unaligned(__bit_iterator<_Dp, true>,
1235 __bit_iterator<_Dp, true>,
1236 __bit_iterator<_Dp, true>);
1237 template <class _Dp, bool _IC1, bool _IC2> friend bool equal(__bit_iterator<_Dp, _IC1>,
1238 __bit_iterator<_Dp, _IC1>,
1239 __bit_iterator<_Dp, _IC2>);
1240 template <class _Dp> friend __bit_iterator<_Dp, false> __find_bool_true(__bit_iterator<_Dp, false>,
1241 typename _Dp::size_type);
1242 template <class _Dp> friend __bit_iterator<_Dp, false> __find_bool_false(__bit_iterator<_Dp, false>,
1243 typename _Dp::size_type);
1246 _LIBCPP_END_NAMESPACE_STD
1248 #endif // _LIBCPP___BIT_REFERENCE