10b57cec5SDimitry Andric// -*- C++ -*- 20b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 30b57cec5SDimitry Andric// 40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information. 60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 70b57cec5SDimitry Andric// 80b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 90b57cec5SDimitry Andric 100b57cec5SDimitry Andric#ifndef _LIBCPP___BIT_REFERENCE 110b57cec5SDimitry Andric#define _LIBCPP___BIT_REFERENCE 120b57cec5SDimitry Andric 13*61cfbce3SDimitry Andric#include <__algorithm/copy_n.h> 14*61cfbce3SDimitry Andric#include <__algorithm/fill_n.h> 1581ad6265SDimitry Andric#include <__algorithm/min.h> 16e8d8bef9SDimitry Andric#include <__bits> 1704eeddc0SDimitry Andric#include <__config> 1881ad6265SDimitry Andric#include <__iterator/iterator_traits.h> 19*61cfbce3SDimitry Andric#include <__memory/construct_at.h> 2081ad6265SDimitry Andric#include <__memory/pointer_traits.h> 2181ad6265SDimitry Andric#include <cstring> 2281ad6265SDimitry Andric#include <type_traits> 230b57cec5SDimitry Andric 240b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 250b57cec5SDimitry Andric# pragma GCC system_header 260b57cec5SDimitry Andric#endif 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS 290b57cec5SDimitry Andric#include <__undef_macros> 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric 320b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD 330b57cec5SDimitry Andric 340b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst, typename _Cp::__storage_type = 0> class __bit_iterator; 350b57cec5SDimitry Andrictemplate <class _Cp> class __bit_const_reference; 360b57cec5SDimitry Andric 370b57cec5SDimitry Andrictemplate <class _Tp> 380b57cec5SDimitry Andricstruct __has_storage_type 390b57cec5SDimitry Andric{ 400b57cec5SDimitry Andric static const bool value = false; 410b57cec5SDimitry Andric}; 420b57cec5SDimitry Andric 430b57cec5SDimitry Andrictemplate <class _Cp, bool = __has_storage_type<_Cp>::value> 440b57cec5SDimitry Andricclass __bit_reference 450b57cec5SDimitry Andric{ 460b57cec5SDimitry Andric typedef typename _Cp::__storage_type __storage_type; 470b57cec5SDimitry Andric typedef typename _Cp::__storage_pointer __storage_pointer; 480b57cec5SDimitry Andric 490b57cec5SDimitry Andric __storage_pointer __seg_; 500b57cec5SDimitry Andric __storage_type __mask_; 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric friend typename _Cp::__self; 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric friend class __bit_const_reference<_Cp>; 550b57cec5SDimitry Andric friend class __bit_iterator<_Cp, false>; 560b57cec5SDimitry Andricpublic: 57*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 58480093f4SDimitry Andric __bit_reference(const __bit_reference&) = default; 59480093f4SDimitry Andric 60*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 operator bool() const _NOEXCEPT 610b57cec5SDimitry Andric {return static_cast<bool>(*__seg_ & __mask_);} 62*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 bool operator ~() const _NOEXCEPT 630b57cec5SDimitry Andric {return !static_cast<bool>(*this);} 640b57cec5SDimitry Andric 65*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 660b57cec5SDimitry Andric __bit_reference& operator=(bool __x) _NOEXCEPT 670b57cec5SDimitry Andric { 680b57cec5SDimitry Andric if (__x) 690b57cec5SDimitry Andric *__seg_ |= __mask_; 700b57cec5SDimitry Andric else 710b57cec5SDimitry Andric *__seg_ &= ~__mask_; 720b57cec5SDimitry Andric return *this; 730b57cec5SDimitry Andric } 740b57cec5SDimitry Andric 7581ad6265SDimitry Andric#if _LIBCPP_STD_VER > 20 76*61cfbce3SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr const __bit_reference& operator=(bool __x) const noexcept { 7781ad6265SDimitry Andric if (__x) 7881ad6265SDimitry Andric *__seg_ |= __mask_; 7981ad6265SDimitry Andric else 8081ad6265SDimitry Andric *__seg_ &= ~__mask_; 8181ad6265SDimitry Andric return *this; 8281ad6265SDimitry Andric } 8381ad6265SDimitry Andric#endif 8481ad6265SDimitry Andric 85*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 860b57cec5SDimitry Andric __bit_reference& operator=(const __bit_reference& __x) _NOEXCEPT 870b57cec5SDimitry Andric {return operator=(static_cast<bool>(__x));} 880b57cec5SDimitry Andric 89*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 void flip() _NOEXCEPT {*__seg_ ^= __mask_;} 90*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 __bit_iterator<_Cp, false> operator&() const _NOEXCEPT 910b57cec5SDimitry Andric {return __bit_iterator<_Cp, false>(__seg_, static_cast<unsigned>(__libcpp_ctz(__mask_)));} 920b57cec5SDimitry Andricprivate: 93*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 9481ad6265SDimitry Andric explicit __bit_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT 950b57cec5SDimitry Andric : __seg_(__s), __mask_(__m) {} 960b57cec5SDimitry Andric}; 970b57cec5SDimitry Andric 980b57cec5SDimitry Andrictemplate <class _Cp> 990b57cec5SDimitry Andricclass __bit_reference<_Cp, false> 1000b57cec5SDimitry Andric{ 1010b57cec5SDimitry Andric}; 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andrictemplate <class _Cp> 104*61cfbce3SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 1050b57cec5SDimitry Andricvoid 1060b57cec5SDimitry Andricswap(__bit_reference<_Cp> __x, __bit_reference<_Cp> __y) _NOEXCEPT 1070b57cec5SDimitry Andric{ 1080b57cec5SDimitry Andric bool __t = __x; 1090b57cec5SDimitry Andric __x = __y; 1100b57cec5SDimitry Andric __y = __t; 1110b57cec5SDimitry Andric} 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andrictemplate <class _Cp, class _Dp> 114*61cfbce3SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 1150b57cec5SDimitry Andricvoid 1160b57cec5SDimitry Andricswap(__bit_reference<_Cp> __x, __bit_reference<_Dp> __y) _NOEXCEPT 1170b57cec5SDimitry Andric{ 1180b57cec5SDimitry Andric bool __t = __x; 1190b57cec5SDimitry Andric __x = __y; 1200b57cec5SDimitry Andric __y = __t; 1210b57cec5SDimitry Andric} 1220b57cec5SDimitry Andric 1230b57cec5SDimitry Andrictemplate <class _Cp> 124*61cfbce3SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 1250b57cec5SDimitry Andricvoid 1260b57cec5SDimitry Andricswap(__bit_reference<_Cp> __x, bool& __y) _NOEXCEPT 1270b57cec5SDimitry Andric{ 1280b57cec5SDimitry Andric bool __t = __x; 1290b57cec5SDimitry Andric __x = __y; 1300b57cec5SDimitry Andric __y = __t; 1310b57cec5SDimitry Andric} 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andrictemplate <class _Cp> 134*61cfbce3SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 1350b57cec5SDimitry Andricvoid 1360b57cec5SDimitry Andricswap(bool& __x, __bit_reference<_Cp> __y) _NOEXCEPT 1370b57cec5SDimitry Andric{ 1380b57cec5SDimitry Andric bool __t = __x; 1390b57cec5SDimitry Andric __x = __y; 1400b57cec5SDimitry Andric __y = __t; 1410b57cec5SDimitry Andric} 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andrictemplate <class _Cp> 1440b57cec5SDimitry Andricclass __bit_const_reference 1450b57cec5SDimitry Andric{ 1460b57cec5SDimitry Andric typedef typename _Cp::__storage_type __storage_type; 1470b57cec5SDimitry Andric typedef typename _Cp::__const_storage_pointer __storage_pointer; 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andric __storage_pointer __seg_; 1500b57cec5SDimitry Andric __storage_type __mask_; 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric friend typename _Cp::__self; 1530b57cec5SDimitry Andric friend class __bit_iterator<_Cp, true>; 1540b57cec5SDimitry Andricpublic: 1550b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 156480093f4SDimitry Andric __bit_const_reference(const __bit_const_reference&) = default; 157480093f4SDimitry Andric 158*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 1590b57cec5SDimitry Andric __bit_const_reference(const __bit_reference<_Cp>& __x) _NOEXCEPT 1600b57cec5SDimitry Andric : __seg_(__x.__seg_), __mask_(__x.__mask_) {} 1610b57cec5SDimitry Andric 1620b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR operator bool() const _NOEXCEPT 1630b57cec5SDimitry Andric {return static_cast<bool>(*__seg_ & __mask_);} 1640b57cec5SDimitry Andric 165*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 __bit_iterator<_Cp, true> operator&() const _NOEXCEPT 1660b57cec5SDimitry Andric {return __bit_iterator<_Cp, true>(__seg_, static_cast<unsigned>(__libcpp_ctz(__mask_)));} 1670b57cec5SDimitry Andricprivate: 1680b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 1690b57cec5SDimitry Andric _LIBCPP_CONSTEXPR 17081ad6265SDimitry Andric explicit __bit_const_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT 1710b57cec5SDimitry Andric : __seg_(__s), __mask_(__m) {} 1720b57cec5SDimitry Andric 173480093f4SDimitry Andric __bit_const_reference& operator=(const __bit_const_reference&) = delete; 1740b57cec5SDimitry Andric}; 1750b57cec5SDimitry Andric 1760b57cec5SDimitry Andric// find 1770b57cec5SDimitry Andric 1780b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst> 179*61cfbce3SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17 __bit_iterator<_Cp, _IsConst> 1800b57cec5SDimitry Andric__find_bool_true(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) 1810b57cec5SDimitry Andric{ 1820b57cec5SDimitry Andric typedef __bit_iterator<_Cp, _IsConst> _It; 1830b57cec5SDimitry Andric typedef typename _It::__storage_type __storage_type; 184*61cfbce3SDimitry Andric const int __bits_per_word = _It::__bits_per_word; 1850b57cec5SDimitry Andric // do first partial word 1860b57cec5SDimitry Andric if (__first.__ctz_ != 0) 1870b57cec5SDimitry Andric { 1880b57cec5SDimitry Andric __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); 1890b57cec5SDimitry Andric __storage_type __dn = _VSTD::min(__clz_f, __n); 1900b57cec5SDimitry Andric __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); 1910b57cec5SDimitry Andric __storage_type __b = *__first.__seg_ & __m; 1920b57cec5SDimitry Andric if (__b) 1930b57cec5SDimitry Andric return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__libcpp_ctz(__b))); 1940b57cec5SDimitry Andric if (__n == __dn) 1950b57cec5SDimitry Andric return __first + __n; 1960b57cec5SDimitry Andric __n -= __dn; 1970b57cec5SDimitry Andric ++__first.__seg_; 1980b57cec5SDimitry Andric } 1990b57cec5SDimitry Andric // do middle whole words 2000b57cec5SDimitry Andric for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) 2010b57cec5SDimitry Andric if (*__first.__seg_) 2020b57cec5SDimitry Andric return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__libcpp_ctz(*__first.__seg_))); 2030b57cec5SDimitry Andric // do last partial word 2040b57cec5SDimitry Andric if (__n > 0) 2050b57cec5SDimitry Andric { 2060b57cec5SDimitry Andric __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 2070b57cec5SDimitry Andric __storage_type __b = *__first.__seg_ & __m; 2080b57cec5SDimitry Andric if (__b) 2090b57cec5SDimitry Andric return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__libcpp_ctz(__b))); 2100b57cec5SDimitry Andric } 2110b57cec5SDimitry Andric return _It(__first.__seg_, static_cast<unsigned>(__n)); 2120b57cec5SDimitry Andric} 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst> 215*61cfbce3SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17 __bit_iterator<_Cp, _IsConst> 2160b57cec5SDimitry Andric__find_bool_false(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) 2170b57cec5SDimitry Andric{ 2180b57cec5SDimitry Andric typedef __bit_iterator<_Cp, _IsConst> _It; 2190b57cec5SDimitry Andric typedef typename _It::__storage_type __storage_type; 2200b57cec5SDimitry Andric const int __bits_per_word = _It::__bits_per_word; 2210b57cec5SDimitry Andric // do first partial word 2220b57cec5SDimitry Andric if (__first.__ctz_ != 0) 2230b57cec5SDimitry Andric { 2240b57cec5SDimitry Andric __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); 2250b57cec5SDimitry Andric __storage_type __dn = _VSTD::min(__clz_f, __n); 2260b57cec5SDimitry Andric __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); 2270b57cec5SDimitry Andric __storage_type __b = ~*__first.__seg_ & __m; 2280b57cec5SDimitry Andric if (__b) 2290b57cec5SDimitry Andric return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__libcpp_ctz(__b))); 2300b57cec5SDimitry Andric if (__n == __dn) 2310b57cec5SDimitry Andric return __first + __n; 2320b57cec5SDimitry Andric __n -= __dn; 2330b57cec5SDimitry Andric ++__first.__seg_; 2340b57cec5SDimitry Andric } 2350b57cec5SDimitry Andric // do middle whole words 2360b57cec5SDimitry Andric for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) 2370b57cec5SDimitry Andric { 2380b57cec5SDimitry Andric __storage_type __b = ~*__first.__seg_; 2390b57cec5SDimitry Andric if (__b) 2400b57cec5SDimitry Andric return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__libcpp_ctz(__b))); 2410b57cec5SDimitry Andric } 2420b57cec5SDimitry Andric // do last partial word 2430b57cec5SDimitry Andric if (__n > 0) 2440b57cec5SDimitry Andric { 2450b57cec5SDimitry Andric __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 2460b57cec5SDimitry Andric __storage_type __b = ~*__first.__seg_ & __m; 2470b57cec5SDimitry Andric if (__b) 2480b57cec5SDimitry Andric return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__libcpp_ctz(__b))); 2490b57cec5SDimitry Andric } 2500b57cec5SDimitry Andric return _It(__first.__seg_, static_cast<unsigned>(__n)); 2510b57cec5SDimitry Andric} 2520b57cec5SDimitry Andric 2530b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst, class _Tp> 254*61cfbce3SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 2550b57cec5SDimitry Andric__bit_iterator<_Cp, _IsConst> 256753f127fSDimitry Andricfind(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value) 2570b57cec5SDimitry Andric{ 258753f127fSDimitry Andric if (static_cast<bool>(__value)) 259e8d8bef9SDimitry Andric return _VSTD::__find_bool_true(__first, static_cast<typename _Cp::size_type>(__last - __first)); 260e8d8bef9SDimitry Andric return _VSTD::__find_bool_false(__first, static_cast<typename _Cp::size_type>(__last - __first)); 2610b57cec5SDimitry Andric} 2620b57cec5SDimitry Andric 2630b57cec5SDimitry Andric// count 2640b57cec5SDimitry Andric 2650b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst> 2660b57cec5SDimitry Andrictypename __bit_iterator<_Cp, _IsConst>::difference_type 2670b57cec5SDimitry Andric__count_bool_true(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) 2680b57cec5SDimitry Andric{ 2690b57cec5SDimitry Andric typedef __bit_iterator<_Cp, _IsConst> _It; 2700b57cec5SDimitry Andric typedef typename _It::__storage_type __storage_type; 2710b57cec5SDimitry Andric typedef typename _It::difference_type difference_type; 2720b57cec5SDimitry Andric const int __bits_per_word = _It::__bits_per_word; 2730b57cec5SDimitry Andric difference_type __r = 0; 2740b57cec5SDimitry Andric // do first partial word 2750b57cec5SDimitry Andric if (__first.__ctz_ != 0) 2760b57cec5SDimitry Andric { 2770b57cec5SDimitry Andric __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); 2780b57cec5SDimitry Andric __storage_type __dn = _VSTD::min(__clz_f, __n); 2790b57cec5SDimitry Andric __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); 2800b57cec5SDimitry Andric __r = _VSTD::__libcpp_popcount(*__first.__seg_ & __m); 2810b57cec5SDimitry Andric __n -= __dn; 2820b57cec5SDimitry Andric ++__first.__seg_; 2830b57cec5SDimitry Andric } 2840b57cec5SDimitry Andric // do middle whole words 2850b57cec5SDimitry Andric for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) 2860b57cec5SDimitry Andric __r += _VSTD::__libcpp_popcount(*__first.__seg_); 2870b57cec5SDimitry Andric // do last partial word 2880b57cec5SDimitry Andric if (__n > 0) 2890b57cec5SDimitry Andric { 2900b57cec5SDimitry Andric __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 2910b57cec5SDimitry Andric __r += _VSTD::__libcpp_popcount(*__first.__seg_ & __m); 2920b57cec5SDimitry Andric } 2930b57cec5SDimitry Andric return __r; 2940b57cec5SDimitry Andric} 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst> 2970b57cec5SDimitry Andrictypename __bit_iterator<_Cp, _IsConst>::difference_type 2980b57cec5SDimitry Andric__count_bool_false(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) 2990b57cec5SDimitry Andric{ 3000b57cec5SDimitry Andric typedef __bit_iterator<_Cp, _IsConst> _It; 3010b57cec5SDimitry Andric typedef typename _It::__storage_type __storage_type; 3020b57cec5SDimitry Andric typedef typename _It::difference_type difference_type; 3030b57cec5SDimitry Andric const int __bits_per_word = _It::__bits_per_word; 3040b57cec5SDimitry Andric difference_type __r = 0; 3050b57cec5SDimitry Andric // do first partial word 3060b57cec5SDimitry Andric if (__first.__ctz_ != 0) 3070b57cec5SDimitry Andric { 3080b57cec5SDimitry Andric __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); 3090b57cec5SDimitry Andric __storage_type __dn = _VSTD::min(__clz_f, __n); 3100b57cec5SDimitry Andric __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); 3110b57cec5SDimitry Andric __r = _VSTD::__libcpp_popcount(~*__first.__seg_ & __m); 3120b57cec5SDimitry Andric __n -= __dn; 3130b57cec5SDimitry Andric ++__first.__seg_; 3140b57cec5SDimitry Andric } 3150b57cec5SDimitry Andric // do middle whole words 3160b57cec5SDimitry Andric for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) 3170b57cec5SDimitry Andric __r += _VSTD::__libcpp_popcount(~*__first.__seg_); 3180b57cec5SDimitry Andric // do last partial word 3190b57cec5SDimitry Andric if (__n > 0) 3200b57cec5SDimitry Andric { 3210b57cec5SDimitry Andric __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 3220b57cec5SDimitry Andric __r += _VSTD::__libcpp_popcount(~*__first.__seg_ & __m); 3230b57cec5SDimitry Andric } 3240b57cec5SDimitry Andric return __r; 3250b57cec5SDimitry Andric} 3260b57cec5SDimitry Andric 3270b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst, class _Tp> 3280b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 3290b57cec5SDimitry Andrictypename __bit_iterator<_Cp, _IsConst>::difference_type 330753f127fSDimitry Andriccount(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value) 3310b57cec5SDimitry Andric{ 332753f127fSDimitry Andric if (static_cast<bool>(__value)) 333e8d8bef9SDimitry Andric return _VSTD::__count_bool_true(__first, static_cast<typename _Cp::size_type>(__last - __first)); 334e8d8bef9SDimitry Andric return _VSTD::__count_bool_false(__first, static_cast<typename _Cp::size_type>(__last - __first)); 3350b57cec5SDimitry Andric} 3360b57cec5SDimitry Andric 3370b57cec5SDimitry Andric// fill_n 3380b57cec5SDimitry Andric 3390b57cec5SDimitry Andrictemplate <class _Cp> 340*61cfbce3SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17 void 3410b57cec5SDimitry Andric__fill_n_false(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n) 3420b57cec5SDimitry Andric{ 3430b57cec5SDimitry Andric typedef __bit_iterator<_Cp, false> _It; 3440b57cec5SDimitry Andric typedef typename _It::__storage_type __storage_type; 3450b57cec5SDimitry Andric const int __bits_per_word = _It::__bits_per_word; 3460b57cec5SDimitry Andric // do first partial word 3470b57cec5SDimitry Andric if (__first.__ctz_ != 0) 3480b57cec5SDimitry Andric { 3490b57cec5SDimitry Andric __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); 3500b57cec5SDimitry Andric __storage_type __dn = _VSTD::min(__clz_f, __n); 3510b57cec5SDimitry Andric __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); 3520b57cec5SDimitry Andric *__first.__seg_ &= ~__m; 3530b57cec5SDimitry Andric __n -= __dn; 3540b57cec5SDimitry Andric ++__first.__seg_; 3550b57cec5SDimitry Andric } 3560b57cec5SDimitry Andric // do middle whole words 3570b57cec5SDimitry Andric __storage_type __nw = __n / __bits_per_word; 358*61cfbce3SDimitry Andric std::fill_n(std::__to_address(__first.__seg_), __nw, 0); 3590b57cec5SDimitry Andric __n -= __nw * __bits_per_word; 3600b57cec5SDimitry Andric // do last partial word 3610b57cec5SDimitry Andric if (__n > 0) 3620b57cec5SDimitry Andric { 3630b57cec5SDimitry Andric __first.__seg_ += __nw; 3640b57cec5SDimitry Andric __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 3650b57cec5SDimitry Andric *__first.__seg_ &= ~__m; 3660b57cec5SDimitry Andric } 3670b57cec5SDimitry Andric} 3680b57cec5SDimitry Andric 3690b57cec5SDimitry Andrictemplate <class _Cp> 370*61cfbce3SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17 void 3710b57cec5SDimitry Andric__fill_n_true(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n) 3720b57cec5SDimitry Andric{ 3730b57cec5SDimitry Andric typedef __bit_iterator<_Cp, false> _It; 3740b57cec5SDimitry Andric typedef typename _It::__storage_type __storage_type; 3750b57cec5SDimitry Andric const int __bits_per_word = _It::__bits_per_word; 3760b57cec5SDimitry Andric // do first partial word 3770b57cec5SDimitry Andric if (__first.__ctz_ != 0) 3780b57cec5SDimitry Andric { 3790b57cec5SDimitry Andric __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); 3800b57cec5SDimitry Andric __storage_type __dn = _VSTD::min(__clz_f, __n); 3810b57cec5SDimitry Andric __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); 3820b57cec5SDimitry Andric *__first.__seg_ |= __m; 3830b57cec5SDimitry Andric __n -= __dn; 3840b57cec5SDimitry Andric ++__first.__seg_; 3850b57cec5SDimitry Andric } 3860b57cec5SDimitry Andric // do middle whole words 3870b57cec5SDimitry Andric __storage_type __nw = __n / __bits_per_word; 388*61cfbce3SDimitry Andric // __storage_type is always an unsigned type, so -1 sets all bits 389*61cfbce3SDimitry Andric std::fill_n(std::__to_address(__first.__seg_), __nw, static_cast<__storage_type>(-1)); 3900b57cec5SDimitry Andric __n -= __nw * __bits_per_word; 3910b57cec5SDimitry Andric // do last partial word 3920b57cec5SDimitry Andric if (__n > 0) 3930b57cec5SDimitry Andric { 3940b57cec5SDimitry Andric __first.__seg_ += __nw; 3950b57cec5SDimitry Andric __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 3960b57cec5SDimitry Andric *__first.__seg_ |= __m; 3970b57cec5SDimitry Andric } 3980b57cec5SDimitry Andric} 3990b57cec5SDimitry Andric 4000b57cec5SDimitry Andrictemplate <class _Cp> 401*61cfbce3SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 4020b57cec5SDimitry Andricvoid 403753f127fSDimitry Andricfill_n(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n, bool __value) 4040b57cec5SDimitry Andric{ 4050b57cec5SDimitry Andric if (__n > 0) 4060b57cec5SDimitry Andric { 407753f127fSDimitry Andric if (__value) 408e8d8bef9SDimitry Andric _VSTD::__fill_n_true(__first, __n); 4090b57cec5SDimitry Andric else 410e8d8bef9SDimitry Andric _VSTD::__fill_n_false(__first, __n); 4110b57cec5SDimitry Andric } 4120b57cec5SDimitry Andric} 4130b57cec5SDimitry Andric 4140b57cec5SDimitry Andric// fill 4150b57cec5SDimitry Andric 4160b57cec5SDimitry Andrictemplate <class _Cp> 417*61cfbce3SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 4180b57cec5SDimitry Andricvoid 419753f127fSDimitry Andricfill(__bit_iterator<_Cp, false> __first, __bit_iterator<_Cp, false> __last, bool __value) 4200b57cec5SDimitry Andric{ 421753f127fSDimitry Andric _VSTD::fill_n(__first, static_cast<typename _Cp::size_type>(__last - __first), __value); 4220b57cec5SDimitry Andric} 4230b57cec5SDimitry Andric 4240b57cec5SDimitry Andric// copy 4250b57cec5SDimitry Andric 4260b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst> 427*61cfbce3SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17 4280b57cec5SDimitry Andric__bit_iterator<_Cp, false> 4290b57cec5SDimitry Andric__copy_aligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, 4300b57cec5SDimitry Andric __bit_iterator<_Cp, false> __result) 4310b57cec5SDimitry Andric{ 4320b57cec5SDimitry Andric typedef __bit_iterator<_Cp, _IsConst> _In; 4330b57cec5SDimitry Andric typedef typename _In::difference_type difference_type; 4340b57cec5SDimitry Andric typedef typename _In::__storage_type __storage_type; 4350b57cec5SDimitry Andric const int __bits_per_word = _In::__bits_per_word; 4360b57cec5SDimitry Andric difference_type __n = __last - __first; 4370b57cec5SDimitry Andric if (__n > 0) 4380b57cec5SDimitry Andric { 4390b57cec5SDimitry Andric // do first word 4400b57cec5SDimitry Andric if (__first.__ctz_ != 0) 4410b57cec5SDimitry Andric { 4420b57cec5SDimitry Andric unsigned __clz = __bits_per_word - __first.__ctz_; 4430b57cec5SDimitry Andric difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz), __n); 4440b57cec5SDimitry Andric __n -= __dn; 4450b57cec5SDimitry Andric __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz - __dn)); 4460b57cec5SDimitry Andric __storage_type __b = *__first.__seg_ & __m; 4470b57cec5SDimitry Andric *__result.__seg_ &= ~__m; 4480b57cec5SDimitry Andric *__result.__seg_ |= __b; 4490b57cec5SDimitry Andric __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word; 4500b57cec5SDimitry Andric __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word); 4510b57cec5SDimitry Andric ++__first.__seg_; 4520b57cec5SDimitry Andric // __first.__ctz_ = 0; 4530b57cec5SDimitry Andric } 4540b57cec5SDimitry Andric // __first.__ctz_ == 0; 4550b57cec5SDimitry Andric // do middle words 4560b57cec5SDimitry Andric __storage_type __nw = __n / __bits_per_word; 457*61cfbce3SDimitry Andric std::copy_n(std::__to_address(__first.__seg_), __nw, std::__to_address(__result.__seg_)); 4580b57cec5SDimitry Andric __n -= __nw * __bits_per_word; 4590b57cec5SDimitry Andric __result.__seg_ += __nw; 4600b57cec5SDimitry Andric // do last word 4610b57cec5SDimitry Andric if (__n > 0) 4620b57cec5SDimitry Andric { 4630b57cec5SDimitry Andric __first.__seg_ += __nw; 4640b57cec5SDimitry Andric __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 4650b57cec5SDimitry Andric __storage_type __b = *__first.__seg_ & __m; 4660b57cec5SDimitry Andric *__result.__seg_ &= ~__m; 4670b57cec5SDimitry Andric *__result.__seg_ |= __b; 4680b57cec5SDimitry Andric __result.__ctz_ = static_cast<unsigned>(__n); 4690b57cec5SDimitry Andric } 4700b57cec5SDimitry Andric } 4710b57cec5SDimitry Andric return __result; 4720b57cec5SDimitry Andric} 4730b57cec5SDimitry Andric 4740b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst> 475*61cfbce3SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17 4760b57cec5SDimitry Andric__bit_iterator<_Cp, false> 4770b57cec5SDimitry Andric__copy_unaligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, 4780b57cec5SDimitry Andric __bit_iterator<_Cp, false> __result) 4790b57cec5SDimitry Andric{ 4800b57cec5SDimitry Andric typedef __bit_iterator<_Cp, _IsConst> _In; 4810b57cec5SDimitry Andric typedef typename _In::difference_type difference_type; 4820b57cec5SDimitry Andric typedef typename _In::__storage_type __storage_type; 483*61cfbce3SDimitry Andric const int __bits_per_word = _In::__bits_per_word; 4840b57cec5SDimitry Andric difference_type __n = __last - __first; 4850b57cec5SDimitry Andric if (__n > 0) 4860b57cec5SDimitry Andric { 4870b57cec5SDimitry Andric // do first word 4880b57cec5SDimitry Andric if (__first.__ctz_ != 0) 4890b57cec5SDimitry Andric { 4900b57cec5SDimitry Andric unsigned __clz_f = __bits_per_word - __first.__ctz_; 4910b57cec5SDimitry Andric difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz_f), __n); 4920b57cec5SDimitry Andric __n -= __dn; 4930b57cec5SDimitry Andric __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); 4940b57cec5SDimitry Andric __storage_type __b = *__first.__seg_ & __m; 4950b57cec5SDimitry Andric unsigned __clz_r = __bits_per_word - __result.__ctz_; 4960b57cec5SDimitry Andric __storage_type __ddn = _VSTD::min<__storage_type>(__dn, __clz_r); 4970b57cec5SDimitry Andric __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn)); 4980b57cec5SDimitry Andric *__result.__seg_ &= ~__m; 4990b57cec5SDimitry Andric if (__result.__ctz_ > __first.__ctz_) 5000b57cec5SDimitry Andric *__result.__seg_ |= __b << (__result.__ctz_ - __first.__ctz_); 5010b57cec5SDimitry Andric else 5020b57cec5SDimitry Andric *__result.__seg_ |= __b >> (__first.__ctz_ - __result.__ctz_); 5030b57cec5SDimitry Andric __result.__seg_ += (__ddn + __result.__ctz_) / __bits_per_word; 5040b57cec5SDimitry Andric __result.__ctz_ = static_cast<unsigned>((__ddn + __result.__ctz_) % __bits_per_word); 5050b57cec5SDimitry Andric __dn -= __ddn; 5060b57cec5SDimitry Andric if (__dn > 0) 5070b57cec5SDimitry Andric { 5080b57cec5SDimitry Andric __m = ~__storage_type(0) >> (__bits_per_word - __dn); 5090b57cec5SDimitry Andric *__result.__seg_ &= ~__m; 5100b57cec5SDimitry Andric *__result.__seg_ |= __b >> (__first.__ctz_ + __ddn); 5110b57cec5SDimitry Andric __result.__ctz_ = static_cast<unsigned>(__dn); 5120b57cec5SDimitry Andric } 5130b57cec5SDimitry Andric ++__first.__seg_; 5140b57cec5SDimitry Andric // __first.__ctz_ = 0; 5150b57cec5SDimitry Andric } 5160b57cec5SDimitry Andric // __first.__ctz_ == 0; 5170b57cec5SDimitry Andric // do middle words 5180b57cec5SDimitry Andric unsigned __clz_r = __bits_per_word - __result.__ctz_; 5190b57cec5SDimitry Andric __storage_type __m = ~__storage_type(0) << __result.__ctz_; 5200b57cec5SDimitry Andric for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_) 5210b57cec5SDimitry Andric { 5220b57cec5SDimitry Andric __storage_type __b = *__first.__seg_; 5230b57cec5SDimitry Andric *__result.__seg_ &= ~__m; 5240b57cec5SDimitry Andric *__result.__seg_ |= __b << __result.__ctz_; 5250b57cec5SDimitry Andric ++__result.__seg_; 5260b57cec5SDimitry Andric *__result.__seg_ &= __m; 5270b57cec5SDimitry Andric *__result.__seg_ |= __b >> __clz_r; 5280b57cec5SDimitry Andric } 5290b57cec5SDimitry Andric // do last word 5300b57cec5SDimitry Andric if (__n > 0) 5310b57cec5SDimitry Andric { 5320b57cec5SDimitry Andric __m = ~__storage_type(0) >> (__bits_per_word - __n); 5330b57cec5SDimitry Andric __storage_type __b = *__first.__seg_ & __m; 5340b57cec5SDimitry Andric __storage_type __dn = _VSTD::min(__n, static_cast<difference_type>(__clz_r)); 5350b57cec5SDimitry Andric __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn)); 5360b57cec5SDimitry Andric *__result.__seg_ &= ~__m; 5370b57cec5SDimitry Andric *__result.__seg_ |= __b << __result.__ctz_; 5380b57cec5SDimitry Andric __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word; 5390b57cec5SDimitry Andric __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word); 5400b57cec5SDimitry Andric __n -= __dn; 5410b57cec5SDimitry Andric if (__n > 0) 5420b57cec5SDimitry Andric { 5430b57cec5SDimitry Andric __m = ~__storage_type(0) >> (__bits_per_word - __n); 5440b57cec5SDimitry Andric *__result.__seg_ &= ~__m; 5450b57cec5SDimitry Andric *__result.__seg_ |= __b >> __dn; 5460b57cec5SDimitry Andric __result.__ctz_ = static_cast<unsigned>(__n); 5470b57cec5SDimitry Andric } 5480b57cec5SDimitry Andric } 5490b57cec5SDimitry Andric } 5500b57cec5SDimitry Andric return __result; 5510b57cec5SDimitry Andric} 5520b57cec5SDimitry Andric 5530b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst> 554*61cfbce3SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 5550b57cec5SDimitry Andric__bit_iterator<_Cp, false> 5560b57cec5SDimitry Andriccopy(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result) 5570b57cec5SDimitry Andric{ 5580b57cec5SDimitry Andric if (__first.__ctz_ == __result.__ctz_) 559e8d8bef9SDimitry Andric return _VSTD::__copy_aligned(__first, __last, __result); 560e8d8bef9SDimitry Andric return _VSTD::__copy_unaligned(__first, __last, __result); 5610b57cec5SDimitry Andric} 5620b57cec5SDimitry Andric 5630b57cec5SDimitry Andric// copy_backward 5640b57cec5SDimitry Andric 5650b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst> 566*61cfbce3SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17 __bit_iterator<_Cp, false> 5670b57cec5SDimitry Andric__copy_backward_aligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, 5680b57cec5SDimitry Andric __bit_iterator<_Cp, false> __result) 5690b57cec5SDimitry Andric{ 5700b57cec5SDimitry Andric typedef __bit_iterator<_Cp, _IsConst> _In; 5710b57cec5SDimitry Andric typedef typename _In::difference_type difference_type; 5720b57cec5SDimitry Andric typedef typename _In::__storage_type __storage_type; 5730b57cec5SDimitry Andric const int __bits_per_word = _In::__bits_per_word; 5740b57cec5SDimitry Andric difference_type __n = __last - __first; 5750b57cec5SDimitry Andric if (__n > 0) 5760b57cec5SDimitry Andric { 5770b57cec5SDimitry Andric // do first word 5780b57cec5SDimitry Andric if (__last.__ctz_ != 0) 5790b57cec5SDimitry Andric { 5800b57cec5SDimitry Andric difference_type __dn = _VSTD::min(static_cast<difference_type>(__last.__ctz_), __n); 5810b57cec5SDimitry Andric __n -= __dn; 5820b57cec5SDimitry Andric unsigned __clz = __bits_per_word - __last.__ctz_; 5830b57cec5SDimitry Andric __storage_type __m = (~__storage_type(0) << (__last.__ctz_ - __dn)) & (~__storage_type(0) >> __clz); 5840b57cec5SDimitry Andric __storage_type __b = *__last.__seg_ & __m; 5850b57cec5SDimitry Andric *__result.__seg_ &= ~__m; 5860b57cec5SDimitry Andric *__result.__seg_ |= __b; 5870b57cec5SDimitry Andric __result.__ctz_ = static_cast<unsigned>(((-__dn & (__bits_per_word - 1)) + 5880b57cec5SDimitry Andric __result.__ctz_) % __bits_per_word); 5890b57cec5SDimitry Andric // __last.__ctz_ = 0 5900b57cec5SDimitry Andric } 5910b57cec5SDimitry Andric // __last.__ctz_ == 0 || __n == 0 5920b57cec5SDimitry Andric // __result.__ctz_ == 0 || __n == 0 5930b57cec5SDimitry Andric // do middle words 5940b57cec5SDimitry Andric __storage_type __nw = __n / __bits_per_word; 5950b57cec5SDimitry Andric __result.__seg_ -= __nw; 5960b57cec5SDimitry Andric __last.__seg_ -= __nw; 597*61cfbce3SDimitry Andric std::copy_n(std::__to_address(__last.__seg_), __nw, std::__to_address(__result.__seg_)); 5980b57cec5SDimitry Andric __n -= __nw * __bits_per_word; 5990b57cec5SDimitry Andric // do last word 6000b57cec5SDimitry Andric if (__n > 0) 6010b57cec5SDimitry Andric { 6020b57cec5SDimitry Andric __storage_type __m = ~__storage_type(0) << (__bits_per_word - __n); 6030b57cec5SDimitry Andric __storage_type __b = *--__last.__seg_ & __m; 6040b57cec5SDimitry Andric *--__result.__seg_ &= ~__m; 6050b57cec5SDimitry Andric *__result.__seg_ |= __b; 6060b57cec5SDimitry Andric __result.__ctz_ = static_cast<unsigned>(-__n & (__bits_per_word - 1)); 6070b57cec5SDimitry Andric } 6080b57cec5SDimitry Andric } 6090b57cec5SDimitry Andric return __result; 6100b57cec5SDimitry Andric} 6110b57cec5SDimitry Andric 6120b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst> 613*61cfbce3SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17 __bit_iterator<_Cp, false> 6140b57cec5SDimitry Andric__copy_backward_unaligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, 6150b57cec5SDimitry Andric __bit_iterator<_Cp, false> __result) 6160b57cec5SDimitry Andric{ 6170b57cec5SDimitry Andric typedef __bit_iterator<_Cp, _IsConst> _In; 6180b57cec5SDimitry Andric typedef typename _In::difference_type difference_type; 6190b57cec5SDimitry Andric typedef typename _In::__storage_type __storage_type; 6200b57cec5SDimitry Andric const int __bits_per_word = _In::__bits_per_word; 6210b57cec5SDimitry Andric difference_type __n = __last - __first; 6220b57cec5SDimitry Andric if (__n > 0) 6230b57cec5SDimitry Andric { 6240b57cec5SDimitry Andric // do first word 6250b57cec5SDimitry Andric if (__last.__ctz_ != 0) 6260b57cec5SDimitry Andric { 6270b57cec5SDimitry Andric difference_type __dn = _VSTD::min(static_cast<difference_type>(__last.__ctz_), __n); 6280b57cec5SDimitry Andric __n -= __dn; 6290b57cec5SDimitry Andric unsigned __clz_l = __bits_per_word - __last.__ctz_; 6300b57cec5SDimitry Andric __storage_type __m = (~__storage_type(0) << (__last.__ctz_ - __dn)) & (~__storage_type(0) >> __clz_l); 6310b57cec5SDimitry Andric __storage_type __b = *__last.__seg_ & __m; 6320b57cec5SDimitry Andric unsigned __clz_r = __bits_per_word - __result.__ctz_; 6330b57cec5SDimitry Andric __storage_type __ddn = _VSTD::min(__dn, static_cast<difference_type>(__result.__ctz_)); 6340b57cec5SDimitry Andric if (__ddn > 0) 6350b57cec5SDimitry Andric { 6360b57cec5SDimitry Andric __m = (~__storage_type(0) << (__result.__ctz_ - __ddn)) & (~__storage_type(0) >> __clz_r); 6370b57cec5SDimitry Andric *__result.__seg_ &= ~__m; 6380b57cec5SDimitry Andric if (__result.__ctz_ > __last.__ctz_) 6390b57cec5SDimitry Andric *__result.__seg_ |= __b << (__result.__ctz_ - __last.__ctz_); 6400b57cec5SDimitry Andric else 6410b57cec5SDimitry Andric *__result.__seg_ |= __b >> (__last.__ctz_ - __result.__ctz_); 6420b57cec5SDimitry Andric __result.__ctz_ = static_cast<unsigned>(((-__ddn & (__bits_per_word - 1)) + 6430b57cec5SDimitry Andric __result.__ctz_) % __bits_per_word); 6440b57cec5SDimitry Andric __dn -= __ddn; 6450b57cec5SDimitry Andric } 6460b57cec5SDimitry Andric if (__dn > 0) 6470b57cec5SDimitry Andric { 6480b57cec5SDimitry Andric // __result.__ctz_ == 0 6490b57cec5SDimitry Andric --__result.__seg_; 6500b57cec5SDimitry Andric __result.__ctz_ = static_cast<unsigned>(-__dn & (__bits_per_word - 1)); 6510b57cec5SDimitry Andric __m = ~__storage_type(0) << __result.__ctz_; 6520b57cec5SDimitry Andric *__result.__seg_ &= ~__m; 6530b57cec5SDimitry Andric __last.__ctz_ -= __dn + __ddn; 6540b57cec5SDimitry Andric *__result.__seg_ |= __b << (__result.__ctz_ - __last.__ctz_); 6550b57cec5SDimitry Andric } 6560b57cec5SDimitry Andric // __last.__ctz_ = 0 6570b57cec5SDimitry Andric } 6580b57cec5SDimitry Andric // __last.__ctz_ == 0 || __n == 0 6590b57cec5SDimitry Andric // __result.__ctz_ != 0 || __n == 0 6600b57cec5SDimitry Andric // do middle words 6610b57cec5SDimitry Andric unsigned __clz_r = __bits_per_word - __result.__ctz_; 6620b57cec5SDimitry Andric __storage_type __m = ~__storage_type(0) >> __clz_r; 6630b57cec5SDimitry Andric for (; __n >= __bits_per_word; __n -= __bits_per_word) 6640b57cec5SDimitry Andric { 6650b57cec5SDimitry Andric __storage_type __b = *--__last.__seg_; 6660b57cec5SDimitry Andric *__result.__seg_ &= ~__m; 6670b57cec5SDimitry Andric *__result.__seg_ |= __b >> __clz_r; 6680b57cec5SDimitry Andric *--__result.__seg_ &= __m; 6690b57cec5SDimitry Andric *__result.__seg_ |= __b << __result.__ctz_; 6700b57cec5SDimitry Andric } 6710b57cec5SDimitry Andric // do last word 6720b57cec5SDimitry Andric if (__n > 0) 6730b57cec5SDimitry Andric { 6740b57cec5SDimitry Andric __m = ~__storage_type(0) << (__bits_per_word - __n); 6750b57cec5SDimitry Andric __storage_type __b = *--__last.__seg_ & __m; 6760b57cec5SDimitry Andric __clz_r = __bits_per_word - __result.__ctz_; 6770b57cec5SDimitry Andric __storage_type __dn = _VSTD::min(__n, static_cast<difference_type>(__result.__ctz_)); 6780b57cec5SDimitry Andric __m = (~__storage_type(0) << (__result.__ctz_ - __dn)) & (~__storage_type(0) >> __clz_r); 6790b57cec5SDimitry Andric *__result.__seg_ &= ~__m; 6800b57cec5SDimitry Andric *__result.__seg_ |= __b >> (__bits_per_word - __result.__ctz_); 6810b57cec5SDimitry Andric __result.__ctz_ = static_cast<unsigned>(((-__dn & (__bits_per_word - 1)) + 6820b57cec5SDimitry Andric __result.__ctz_) % __bits_per_word); 6830b57cec5SDimitry Andric __n -= __dn; 6840b57cec5SDimitry Andric if (__n > 0) 6850b57cec5SDimitry Andric { 6860b57cec5SDimitry Andric // __result.__ctz_ == 0 6870b57cec5SDimitry Andric --__result.__seg_; 6880b57cec5SDimitry Andric __result.__ctz_ = static_cast<unsigned>(-__n & (__bits_per_word - 1)); 6890b57cec5SDimitry Andric __m = ~__storage_type(0) << __result.__ctz_; 6900b57cec5SDimitry Andric *__result.__seg_ &= ~__m; 6910b57cec5SDimitry Andric *__result.__seg_ |= __b << (__result.__ctz_ - (__bits_per_word - __n - __dn)); 6920b57cec5SDimitry Andric } 6930b57cec5SDimitry Andric } 6940b57cec5SDimitry Andric } 6950b57cec5SDimitry Andric return __result; 6960b57cec5SDimitry Andric} 6970b57cec5SDimitry Andric 6980b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst> 699*61cfbce3SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 7000b57cec5SDimitry Andric__bit_iterator<_Cp, false> 7010b57cec5SDimitry Andriccopy_backward(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result) 7020b57cec5SDimitry Andric{ 7030b57cec5SDimitry Andric if (__last.__ctz_ == __result.__ctz_) 704e8d8bef9SDimitry Andric return _VSTD::__copy_backward_aligned(__first, __last, __result); 705e8d8bef9SDimitry Andric return _VSTD::__copy_backward_unaligned(__first, __last, __result); 7060b57cec5SDimitry Andric} 7070b57cec5SDimitry Andric 7080b57cec5SDimitry Andric// move 7090b57cec5SDimitry Andric 7100b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst> 7110b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 7120b57cec5SDimitry Andric__bit_iterator<_Cp, false> 7130b57cec5SDimitry Andricmove(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result) 7140b57cec5SDimitry Andric{ 7150b57cec5SDimitry Andric return _VSTD::copy(__first, __last, __result); 7160b57cec5SDimitry Andric} 7170b57cec5SDimitry Andric 7180b57cec5SDimitry Andric// move_backward 7190b57cec5SDimitry Andric 7200b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst> 7210b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 7220b57cec5SDimitry Andric__bit_iterator<_Cp, false> 7230b57cec5SDimitry Andricmove_backward(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result) 7240b57cec5SDimitry Andric{ 7250b57cec5SDimitry Andric return _VSTD::copy_backward(__first, __last, __result); 7260b57cec5SDimitry Andric} 7270b57cec5SDimitry Andric 7280b57cec5SDimitry Andric// swap_ranges 7290b57cec5SDimitry Andric 7300b57cec5SDimitry Andrictemplate <class __C1, class __C2> 7310b57cec5SDimitry Andric__bit_iterator<__C2, false> 7320b57cec5SDimitry Andric__swap_ranges_aligned(__bit_iterator<__C1, false> __first, __bit_iterator<__C1, false> __last, 7330b57cec5SDimitry Andric __bit_iterator<__C2, false> __result) 7340b57cec5SDimitry Andric{ 7350b57cec5SDimitry Andric typedef __bit_iterator<__C1, false> _I1; 7360b57cec5SDimitry Andric typedef typename _I1::difference_type difference_type; 7370b57cec5SDimitry Andric typedef typename _I1::__storage_type __storage_type; 7380b57cec5SDimitry Andric const int __bits_per_word = _I1::__bits_per_word; 7390b57cec5SDimitry Andric difference_type __n = __last - __first; 7400b57cec5SDimitry Andric if (__n > 0) 7410b57cec5SDimitry Andric { 7420b57cec5SDimitry Andric // do first word 7430b57cec5SDimitry Andric if (__first.__ctz_ != 0) 7440b57cec5SDimitry Andric { 7450b57cec5SDimitry Andric unsigned __clz = __bits_per_word - __first.__ctz_; 7460b57cec5SDimitry Andric difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz), __n); 7470b57cec5SDimitry Andric __n -= __dn; 7480b57cec5SDimitry Andric __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz - __dn)); 7490b57cec5SDimitry Andric __storage_type __b1 = *__first.__seg_ & __m; 7500b57cec5SDimitry Andric *__first.__seg_ &= ~__m; 7510b57cec5SDimitry Andric __storage_type __b2 = *__result.__seg_ & __m; 7520b57cec5SDimitry Andric *__result.__seg_ &= ~__m; 7530b57cec5SDimitry Andric *__result.__seg_ |= __b1; 7540b57cec5SDimitry Andric *__first.__seg_ |= __b2; 7550b57cec5SDimitry Andric __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word; 7560b57cec5SDimitry Andric __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word); 7570b57cec5SDimitry Andric ++__first.__seg_; 7580b57cec5SDimitry Andric // __first.__ctz_ = 0; 7590b57cec5SDimitry Andric } 7600b57cec5SDimitry Andric // __first.__ctz_ == 0; 7610b57cec5SDimitry Andric // do middle words 7620b57cec5SDimitry Andric for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_, ++__result.__seg_) 7630b57cec5SDimitry Andric swap(*__first.__seg_, *__result.__seg_); 7640b57cec5SDimitry Andric // do last word 7650b57cec5SDimitry Andric if (__n > 0) 7660b57cec5SDimitry Andric { 7670b57cec5SDimitry Andric __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 7680b57cec5SDimitry Andric __storage_type __b1 = *__first.__seg_ & __m; 7690b57cec5SDimitry Andric *__first.__seg_ &= ~__m; 7700b57cec5SDimitry Andric __storage_type __b2 = *__result.__seg_ & __m; 7710b57cec5SDimitry Andric *__result.__seg_ &= ~__m; 7720b57cec5SDimitry Andric *__result.__seg_ |= __b1; 7730b57cec5SDimitry Andric *__first.__seg_ |= __b2; 7740b57cec5SDimitry Andric __result.__ctz_ = static_cast<unsigned>(__n); 7750b57cec5SDimitry Andric } 7760b57cec5SDimitry Andric } 7770b57cec5SDimitry Andric return __result; 7780b57cec5SDimitry Andric} 7790b57cec5SDimitry Andric 7800b57cec5SDimitry Andrictemplate <class __C1, class __C2> 7810b57cec5SDimitry Andric__bit_iterator<__C2, false> 7820b57cec5SDimitry Andric__swap_ranges_unaligned(__bit_iterator<__C1, false> __first, __bit_iterator<__C1, false> __last, 7830b57cec5SDimitry Andric __bit_iterator<__C2, false> __result) 7840b57cec5SDimitry Andric{ 7850b57cec5SDimitry Andric typedef __bit_iterator<__C1, false> _I1; 7860b57cec5SDimitry Andric typedef typename _I1::difference_type difference_type; 7870b57cec5SDimitry Andric typedef typename _I1::__storage_type __storage_type; 7880b57cec5SDimitry Andric const int __bits_per_word = _I1::__bits_per_word; 7890b57cec5SDimitry Andric difference_type __n = __last - __first; 7900b57cec5SDimitry Andric if (__n > 0) 7910b57cec5SDimitry Andric { 7920b57cec5SDimitry Andric // do first word 7930b57cec5SDimitry Andric if (__first.__ctz_ != 0) 7940b57cec5SDimitry Andric { 7950b57cec5SDimitry Andric unsigned __clz_f = __bits_per_word - __first.__ctz_; 7960b57cec5SDimitry Andric difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz_f), __n); 7970b57cec5SDimitry Andric __n -= __dn; 7980b57cec5SDimitry Andric __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); 7990b57cec5SDimitry Andric __storage_type __b1 = *__first.__seg_ & __m; 8000b57cec5SDimitry Andric *__first.__seg_ &= ~__m; 8010b57cec5SDimitry Andric unsigned __clz_r = __bits_per_word - __result.__ctz_; 8020b57cec5SDimitry Andric __storage_type __ddn = _VSTD::min<__storage_type>(__dn, __clz_r); 8030b57cec5SDimitry Andric __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn)); 8040b57cec5SDimitry Andric __storage_type __b2 = *__result.__seg_ & __m; 8050b57cec5SDimitry Andric *__result.__seg_ &= ~__m; 8060b57cec5SDimitry Andric if (__result.__ctz_ > __first.__ctz_) 8070b57cec5SDimitry Andric { 8080b57cec5SDimitry Andric unsigned __s = __result.__ctz_ - __first.__ctz_; 8090b57cec5SDimitry Andric *__result.__seg_ |= __b1 << __s; 8100b57cec5SDimitry Andric *__first.__seg_ |= __b2 >> __s; 8110b57cec5SDimitry Andric } 8120b57cec5SDimitry Andric else 8130b57cec5SDimitry Andric { 8140b57cec5SDimitry Andric unsigned __s = __first.__ctz_ - __result.__ctz_; 8150b57cec5SDimitry Andric *__result.__seg_ |= __b1 >> __s; 8160b57cec5SDimitry Andric *__first.__seg_ |= __b2 << __s; 8170b57cec5SDimitry Andric } 8180b57cec5SDimitry Andric __result.__seg_ += (__ddn + __result.__ctz_) / __bits_per_word; 8190b57cec5SDimitry Andric __result.__ctz_ = static_cast<unsigned>((__ddn + __result.__ctz_) % __bits_per_word); 8200b57cec5SDimitry Andric __dn -= __ddn; 8210b57cec5SDimitry Andric if (__dn > 0) 8220b57cec5SDimitry Andric { 8230b57cec5SDimitry Andric __m = ~__storage_type(0) >> (__bits_per_word - __dn); 8240b57cec5SDimitry Andric __b2 = *__result.__seg_ & __m; 8250b57cec5SDimitry Andric *__result.__seg_ &= ~__m; 8260b57cec5SDimitry Andric unsigned __s = __first.__ctz_ + __ddn; 8270b57cec5SDimitry Andric *__result.__seg_ |= __b1 >> __s; 8280b57cec5SDimitry Andric *__first.__seg_ |= __b2 << __s; 8290b57cec5SDimitry Andric __result.__ctz_ = static_cast<unsigned>(__dn); 8300b57cec5SDimitry Andric } 8310b57cec5SDimitry Andric ++__first.__seg_; 8320b57cec5SDimitry Andric // __first.__ctz_ = 0; 8330b57cec5SDimitry Andric } 8340b57cec5SDimitry Andric // __first.__ctz_ == 0; 8350b57cec5SDimitry Andric // do middle words 8360b57cec5SDimitry Andric __storage_type __m = ~__storage_type(0) << __result.__ctz_; 8370b57cec5SDimitry Andric unsigned __clz_r = __bits_per_word - __result.__ctz_; 8380b57cec5SDimitry Andric for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_) 8390b57cec5SDimitry Andric { 8400b57cec5SDimitry Andric __storage_type __b1 = *__first.__seg_; 8410b57cec5SDimitry Andric __storage_type __b2 = *__result.__seg_ & __m; 8420b57cec5SDimitry Andric *__result.__seg_ &= ~__m; 8430b57cec5SDimitry Andric *__result.__seg_ |= __b1 << __result.__ctz_; 8440b57cec5SDimitry Andric *__first.__seg_ = __b2 >> __result.__ctz_; 8450b57cec5SDimitry Andric ++__result.__seg_; 8460b57cec5SDimitry Andric __b2 = *__result.__seg_ & ~__m; 8470b57cec5SDimitry Andric *__result.__seg_ &= __m; 8480b57cec5SDimitry Andric *__result.__seg_ |= __b1 >> __clz_r; 8490b57cec5SDimitry Andric *__first.__seg_ |= __b2 << __clz_r; 8500b57cec5SDimitry Andric } 8510b57cec5SDimitry Andric // do last word 8520b57cec5SDimitry Andric if (__n > 0) 8530b57cec5SDimitry Andric { 8540b57cec5SDimitry Andric __m = ~__storage_type(0) >> (__bits_per_word - __n); 8550b57cec5SDimitry Andric __storage_type __b1 = *__first.__seg_ & __m; 8560b57cec5SDimitry Andric *__first.__seg_ &= ~__m; 8570b57cec5SDimitry Andric __storage_type __dn = _VSTD::min<__storage_type>(__n, __clz_r); 8580b57cec5SDimitry Andric __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn)); 8590b57cec5SDimitry Andric __storage_type __b2 = *__result.__seg_ & __m; 8600b57cec5SDimitry Andric *__result.__seg_ &= ~__m; 8610b57cec5SDimitry Andric *__result.__seg_ |= __b1 << __result.__ctz_; 8620b57cec5SDimitry Andric *__first.__seg_ |= __b2 >> __result.__ctz_; 8630b57cec5SDimitry Andric __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word; 8640b57cec5SDimitry Andric __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word); 8650b57cec5SDimitry Andric __n -= __dn; 8660b57cec5SDimitry Andric if (__n > 0) 8670b57cec5SDimitry Andric { 8680b57cec5SDimitry Andric __m = ~__storage_type(0) >> (__bits_per_word - __n); 8690b57cec5SDimitry Andric __b2 = *__result.__seg_ & __m; 8700b57cec5SDimitry Andric *__result.__seg_ &= ~__m; 8710b57cec5SDimitry Andric *__result.__seg_ |= __b1 >> __dn; 8720b57cec5SDimitry Andric *__first.__seg_ |= __b2 << __dn; 8730b57cec5SDimitry Andric __result.__ctz_ = static_cast<unsigned>(__n); 8740b57cec5SDimitry Andric } 8750b57cec5SDimitry Andric } 8760b57cec5SDimitry Andric } 8770b57cec5SDimitry Andric return __result; 8780b57cec5SDimitry Andric} 8790b57cec5SDimitry Andric 8800b57cec5SDimitry Andrictemplate <class __C1, class __C2> 8810b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 8820b57cec5SDimitry Andric__bit_iterator<__C2, false> 8830b57cec5SDimitry Andricswap_ranges(__bit_iterator<__C1, false> __first1, __bit_iterator<__C1, false> __last1, 8840b57cec5SDimitry Andric __bit_iterator<__C2, false> __first2) 8850b57cec5SDimitry Andric{ 8860b57cec5SDimitry Andric if (__first1.__ctz_ == __first2.__ctz_) 887e8d8bef9SDimitry Andric return _VSTD::__swap_ranges_aligned(__first1, __last1, __first2); 888e8d8bef9SDimitry Andric return _VSTD::__swap_ranges_unaligned(__first1, __last1, __first2); 8890b57cec5SDimitry Andric} 8900b57cec5SDimitry Andric 8910b57cec5SDimitry Andric// rotate 8920b57cec5SDimitry Andric 8930b57cec5SDimitry Andrictemplate <class _Cp> 8940b57cec5SDimitry Andricstruct __bit_array 8950b57cec5SDimitry Andric{ 8960b57cec5SDimitry Andric typedef typename _Cp::difference_type difference_type; 8970b57cec5SDimitry Andric typedef typename _Cp::__storage_type __storage_type; 8980b57cec5SDimitry Andric typedef typename _Cp::__storage_pointer __storage_pointer; 8990b57cec5SDimitry Andric typedef typename _Cp::iterator iterator; 9000b57cec5SDimitry Andric static const unsigned __bits_per_word = _Cp::__bits_per_word; 9010b57cec5SDimitry Andric static const unsigned _Np = 4; 9020b57cec5SDimitry Andric 9030b57cec5SDimitry Andric difference_type __size_; 9040b57cec5SDimitry Andric __storage_type __word_[_Np]; 9050b57cec5SDimitry Andric 906*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 static difference_type capacity() 9070b57cec5SDimitry Andric {return static_cast<difference_type>(_Np * __bits_per_word);} 908*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit __bit_array(difference_type __s) : __size_(__s) { 909*61cfbce3SDimitry Andric if (__libcpp_is_constant_evaluated()) { 910*61cfbce3SDimitry Andric for (size_t __i = 0; __i != __bit_array<_Cp>::_Np; ++__i) 911*61cfbce3SDimitry Andric std::__construct_at(__word_ + __i, 0); 912*61cfbce3SDimitry Andric } 913*61cfbce3SDimitry Andric } 914*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 iterator begin() 9150b57cec5SDimitry Andric { 9160b57cec5SDimitry Andric return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]), 0); 9170b57cec5SDimitry Andric } 918*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 iterator end() 9190b57cec5SDimitry Andric { 9200b57cec5SDimitry Andric return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]) + __size_ / __bits_per_word, 9210b57cec5SDimitry Andric static_cast<unsigned>(__size_ % __bits_per_word)); 9220b57cec5SDimitry Andric } 9230b57cec5SDimitry Andric}; 9240b57cec5SDimitry Andric 9250b57cec5SDimitry Andrictemplate <class _Cp> 926*61cfbce3SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17 __bit_iterator<_Cp, false> 9270b57cec5SDimitry Andricrotate(__bit_iterator<_Cp, false> __first, __bit_iterator<_Cp, false> __middle, __bit_iterator<_Cp, false> __last) 9280b57cec5SDimitry Andric{ 9290b57cec5SDimitry Andric typedef __bit_iterator<_Cp, false> _I1; 9300b57cec5SDimitry Andric typedef typename _I1::difference_type difference_type; 9310b57cec5SDimitry Andric difference_type __d1 = __middle - __first; 9320b57cec5SDimitry Andric difference_type __d2 = __last - __middle; 9330b57cec5SDimitry Andric _I1 __r = __first + __d2; 9340b57cec5SDimitry Andric while (__d1 != 0 && __d2 != 0) 9350b57cec5SDimitry Andric { 9360b57cec5SDimitry Andric if (__d1 <= __d2) 9370b57cec5SDimitry Andric { 9380b57cec5SDimitry Andric if (__d1 <= __bit_array<_Cp>::capacity()) 9390b57cec5SDimitry Andric { 9400b57cec5SDimitry Andric __bit_array<_Cp> __b(__d1); 9410b57cec5SDimitry Andric _VSTD::copy(__first, __middle, __b.begin()); 9420b57cec5SDimitry Andric _VSTD::copy(__b.begin(), __b.end(), _VSTD::copy(__middle, __last, __first)); 9430b57cec5SDimitry Andric break; 9440b57cec5SDimitry Andric } 9450b57cec5SDimitry Andric else 9460b57cec5SDimitry Andric { 9470b57cec5SDimitry Andric __bit_iterator<_Cp, false> __mp = _VSTD::swap_ranges(__first, __middle, __middle); 9480b57cec5SDimitry Andric __first = __middle; 9490b57cec5SDimitry Andric __middle = __mp; 9500b57cec5SDimitry Andric __d2 -= __d1; 9510b57cec5SDimitry Andric } 9520b57cec5SDimitry Andric } 9530b57cec5SDimitry Andric else 9540b57cec5SDimitry Andric { 9550b57cec5SDimitry Andric if (__d2 <= __bit_array<_Cp>::capacity()) 9560b57cec5SDimitry Andric { 9570b57cec5SDimitry Andric __bit_array<_Cp> __b(__d2); 9580b57cec5SDimitry Andric _VSTD::copy(__middle, __last, __b.begin()); 9590b57cec5SDimitry Andric _VSTD::copy_backward(__b.begin(), __b.end(), _VSTD::copy_backward(__first, __middle, __last)); 9600b57cec5SDimitry Andric break; 9610b57cec5SDimitry Andric } 9620b57cec5SDimitry Andric else 9630b57cec5SDimitry Andric { 9640b57cec5SDimitry Andric __bit_iterator<_Cp, false> __mp = __first + __d2; 9650b57cec5SDimitry Andric _VSTD::swap_ranges(__first, __mp, __middle); 9660b57cec5SDimitry Andric __first = __mp; 9670b57cec5SDimitry Andric __d1 -= __d2; 9680b57cec5SDimitry Andric } 9690b57cec5SDimitry Andric } 9700b57cec5SDimitry Andric } 9710b57cec5SDimitry Andric return __r; 9720b57cec5SDimitry Andric} 9730b57cec5SDimitry Andric 9740b57cec5SDimitry Andric// equal 9750b57cec5SDimitry Andric 9760b57cec5SDimitry Andrictemplate <class _Cp, bool _IC1, bool _IC2> 977*61cfbce3SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17 bool 9780b57cec5SDimitry Andric__equal_unaligned(__bit_iterator<_Cp, _IC1> __first1, __bit_iterator<_Cp, _IC1> __last1, 9790b57cec5SDimitry Andric __bit_iterator<_Cp, _IC2> __first2) 9800b57cec5SDimitry Andric{ 9810b57cec5SDimitry Andric typedef __bit_iterator<_Cp, _IC1> _It; 9820b57cec5SDimitry Andric typedef typename _It::difference_type difference_type; 9830b57cec5SDimitry Andric typedef typename _It::__storage_type __storage_type; 984*61cfbce3SDimitry Andric const int __bits_per_word = _It::__bits_per_word; 9850b57cec5SDimitry Andric difference_type __n = __last1 - __first1; 9860b57cec5SDimitry Andric if (__n > 0) 9870b57cec5SDimitry Andric { 9880b57cec5SDimitry Andric // do first word 9890b57cec5SDimitry Andric if (__first1.__ctz_ != 0) 9900b57cec5SDimitry Andric { 9910b57cec5SDimitry Andric unsigned __clz_f = __bits_per_word - __first1.__ctz_; 9920b57cec5SDimitry Andric difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz_f), __n); 9930b57cec5SDimitry Andric __n -= __dn; 9940b57cec5SDimitry Andric __storage_type __m = (~__storage_type(0) << __first1.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); 9950b57cec5SDimitry Andric __storage_type __b = *__first1.__seg_ & __m; 9960b57cec5SDimitry Andric unsigned __clz_r = __bits_per_word - __first2.__ctz_; 9970b57cec5SDimitry Andric __storage_type __ddn = _VSTD::min<__storage_type>(__dn, __clz_r); 9980b57cec5SDimitry Andric __m = (~__storage_type(0) << __first2.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn)); 9990b57cec5SDimitry Andric if (__first2.__ctz_ > __first1.__ctz_) 10000b57cec5SDimitry Andric { 10010b57cec5SDimitry Andric if ((*__first2.__seg_ & __m) != (__b << (__first2.__ctz_ - __first1.__ctz_))) 10020b57cec5SDimitry Andric return false; 10030b57cec5SDimitry Andric } 10040b57cec5SDimitry Andric else 10050b57cec5SDimitry Andric { 10060b57cec5SDimitry Andric if ((*__first2.__seg_ & __m) != (__b >> (__first1.__ctz_ - __first2.__ctz_))) 10070b57cec5SDimitry Andric return false; 10080b57cec5SDimitry Andric } 10090b57cec5SDimitry Andric __first2.__seg_ += (__ddn + __first2.__ctz_) / __bits_per_word; 10100b57cec5SDimitry Andric __first2.__ctz_ = static_cast<unsigned>((__ddn + __first2.__ctz_) % __bits_per_word); 10110b57cec5SDimitry Andric __dn -= __ddn; 10120b57cec5SDimitry Andric if (__dn > 0) 10130b57cec5SDimitry Andric { 10140b57cec5SDimitry Andric __m = ~__storage_type(0) >> (__bits_per_word - __dn); 10150b57cec5SDimitry Andric if ((*__first2.__seg_ & __m) != (__b >> (__first1.__ctz_ + __ddn))) 10160b57cec5SDimitry Andric return false; 10170b57cec5SDimitry Andric __first2.__ctz_ = static_cast<unsigned>(__dn); 10180b57cec5SDimitry Andric } 10190b57cec5SDimitry Andric ++__first1.__seg_; 10200b57cec5SDimitry Andric // __first1.__ctz_ = 0; 10210b57cec5SDimitry Andric } 10220b57cec5SDimitry Andric // __first1.__ctz_ == 0; 10230b57cec5SDimitry Andric // do middle words 10240b57cec5SDimitry Andric unsigned __clz_r = __bits_per_word - __first2.__ctz_; 10250b57cec5SDimitry Andric __storage_type __m = ~__storage_type(0) << __first2.__ctz_; 10260b57cec5SDimitry Andric for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first1.__seg_) 10270b57cec5SDimitry Andric { 10280b57cec5SDimitry Andric __storage_type __b = *__first1.__seg_; 10290b57cec5SDimitry Andric if ((*__first2.__seg_ & __m) != (__b << __first2.__ctz_)) 10300b57cec5SDimitry Andric return false; 10310b57cec5SDimitry Andric ++__first2.__seg_; 10320b57cec5SDimitry Andric if ((*__first2.__seg_ & ~__m) != (__b >> __clz_r)) 10330b57cec5SDimitry Andric return false; 10340b57cec5SDimitry Andric } 10350b57cec5SDimitry Andric // do last word 10360b57cec5SDimitry Andric if (__n > 0) 10370b57cec5SDimitry Andric { 10380b57cec5SDimitry Andric __m = ~__storage_type(0) >> (__bits_per_word - __n); 10390b57cec5SDimitry Andric __storage_type __b = *__first1.__seg_ & __m; 10400b57cec5SDimitry Andric __storage_type __dn = _VSTD::min(__n, static_cast<difference_type>(__clz_r)); 10410b57cec5SDimitry Andric __m = (~__storage_type(0) << __first2.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn)); 10420b57cec5SDimitry Andric if ((*__first2.__seg_ & __m) != (__b << __first2.__ctz_)) 10430b57cec5SDimitry Andric return false; 10440b57cec5SDimitry Andric __first2.__seg_ += (__dn + __first2.__ctz_) / __bits_per_word; 10450b57cec5SDimitry Andric __first2.__ctz_ = static_cast<unsigned>((__dn + __first2.__ctz_) % __bits_per_word); 10460b57cec5SDimitry Andric __n -= __dn; 10470b57cec5SDimitry Andric if (__n > 0) 10480b57cec5SDimitry Andric { 10490b57cec5SDimitry Andric __m = ~__storage_type(0) >> (__bits_per_word - __n); 10500b57cec5SDimitry Andric if ((*__first2.__seg_ & __m) != (__b >> __dn)) 10510b57cec5SDimitry Andric return false; 10520b57cec5SDimitry Andric } 10530b57cec5SDimitry Andric } 10540b57cec5SDimitry Andric } 10550b57cec5SDimitry Andric return true; 10560b57cec5SDimitry Andric} 10570b57cec5SDimitry Andric 10580b57cec5SDimitry Andrictemplate <class _Cp, bool _IC1, bool _IC2> 1059*61cfbce3SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX17 bool 10600b57cec5SDimitry Andric__equal_aligned(__bit_iterator<_Cp, _IC1> __first1, __bit_iterator<_Cp, _IC1> __last1, 10610b57cec5SDimitry Andric __bit_iterator<_Cp, _IC2> __first2) 10620b57cec5SDimitry Andric{ 10630b57cec5SDimitry Andric typedef __bit_iterator<_Cp, _IC1> _It; 10640b57cec5SDimitry Andric typedef typename _It::difference_type difference_type; 10650b57cec5SDimitry Andric typedef typename _It::__storage_type __storage_type; 1066*61cfbce3SDimitry Andric const int __bits_per_word = _It::__bits_per_word; 10670b57cec5SDimitry Andric difference_type __n = __last1 - __first1; 10680b57cec5SDimitry Andric if (__n > 0) 10690b57cec5SDimitry Andric { 10700b57cec5SDimitry Andric // do first word 10710b57cec5SDimitry Andric if (__first1.__ctz_ != 0) 10720b57cec5SDimitry Andric { 10730b57cec5SDimitry Andric unsigned __clz = __bits_per_word - __first1.__ctz_; 10740b57cec5SDimitry Andric difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz), __n); 10750b57cec5SDimitry Andric __n -= __dn; 10760b57cec5SDimitry Andric __storage_type __m = (~__storage_type(0) << __first1.__ctz_) & (~__storage_type(0) >> (__clz - __dn)); 10770b57cec5SDimitry Andric if ((*__first2.__seg_ & __m) != (*__first1.__seg_ & __m)) 10780b57cec5SDimitry Andric return false; 10790b57cec5SDimitry Andric ++__first2.__seg_; 10800b57cec5SDimitry Andric ++__first1.__seg_; 10810b57cec5SDimitry Andric // __first1.__ctz_ = 0; 10820b57cec5SDimitry Andric // __first2.__ctz_ = 0; 10830b57cec5SDimitry Andric } 10840b57cec5SDimitry Andric // __first1.__ctz_ == 0; 10850b57cec5SDimitry Andric // __first2.__ctz_ == 0; 10860b57cec5SDimitry Andric // do middle words 10870b57cec5SDimitry Andric for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first1.__seg_, ++__first2.__seg_) 10880b57cec5SDimitry Andric if (*__first2.__seg_ != *__first1.__seg_) 10890b57cec5SDimitry Andric return false; 10900b57cec5SDimitry Andric // do last word 10910b57cec5SDimitry Andric if (__n > 0) 10920b57cec5SDimitry Andric { 10930b57cec5SDimitry Andric __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 10940b57cec5SDimitry Andric if ((*__first2.__seg_ & __m) != (*__first1.__seg_ & __m)) 10950b57cec5SDimitry Andric return false; 10960b57cec5SDimitry Andric } 10970b57cec5SDimitry Andric } 10980b57cec5SDimitry Andric return true; 10990b57cec5SDimitry Andric} 11000b57cec5SDimitry Andric 11010b57cec5SDimitry Andrictemplate <class _Cp, bool _IC1, bool _IC2> 1102*61cfbce3SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 11030b57cec5SDimitry Andricbool 11040b57cec5SDimitry Andricequal(__bit_iterator<_Cp, _IC1> __first1, __bit_iterator<_Cp, _IC1> __last1, __bit_iterator<_Cp, _IC2> __first2) 11050b57cec5SDimitry Andric{ 11060b57cec5SDimitry Andric if (__first1.__ctz_ == __first2.__ctz_) 1107e8d8bef9SDimitry Andric return _VSTD::__equal_aligned(__first1, __last1, __first2); 1108e8d8bef9SDimitry Andric return _VSTD::__equal_unaligned(__first1, __last1, __first2); 11090b57cec5SDimitry Andric} 11100b57cec5SDimitry Andric 11110b57cec5SDimitry Andrictemplate <class _Cp, bool _IsConst, 11120b57cec5SDimitry Andric typename _Cp::__storage_type> 11130b57cec5SDimitry Andricclass __bit_iterator 11140b57cec5SDimitry Andric{ 11150b57cec5SDimitry Andricpublic: 11160b57cec5SDimitry Andric typedef typename _Cp::difference_type difference_type; 11170b57cec5SDimitry Andric typedef bool value_type; 11180b57cec5SDimitry Andric typedef __bit_iterator pointer; 111981ad6265SDimitry Andric#ifndef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL 11200b57cec5SDimitry Andric typedef typename conditional<_IsConst, __bit_const_reference<_Cp>, __bit_reference<_Cp> >::type reference; 112181ad6265SDimitry Andric#else 112281ad6265SDimitry Andric using reference = typename conditional<_IsConst, bool, __bit_reference<_Cp> >::type; 112381ad6265SDimitry Andric#endif 11240b57cec5SDimitry Andric typedef random_access_iterator_tag iterator_category; 11250b57cec5SDimitry Andric 11260b57cec5SDimitry Andricprivate: 11270b57cec5SDimitry Andric typedef typename _Cp::__storage_type __storage_type; 11280b57cec5SDimitry Andric typedef typename conditional<_IsConst, typename _Cp::__const_storage_pointer, 11290b57cec5SDimitry Andric typename _Cp::__storage_pointer>::type __storage_pointer; 11300b57cec5SDimitry Andric static const unsigned __bits_per_word = _Cp::__bits_per_word; 11310b57cec5SDimitry Andric 11320b57cec5SDimitry Andric __storage_pointer __seg_; 11330b57cec5SDimitry Andric unsigned __ctz_; 11340b57cec5SDimitry Andric 11350b57cec5SDimitry Andricpublic: 1136*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 __bit_iterator() _NOEXCEPT 11370b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11 11380b57cec5SDimitry Andric : __seg_(nullptr), __ctz_(0) 11390b57cec5SDimitry Andric#endif 11400b57cec5SDimitry Andric {} 11410b57cec5SDimitry Andric 11424652422eSDimitry Andric // When _IsConst=false, this is the copy constructor. 11434652422eSDimitry Andric // It is non-trivial. Making it trivial would break ABI. 11444652422eSDimitry Andric // When _IsConst=true, this is a converting constructor; 11454652422eSDimitry Andric // the copy and move constructors are implicitly generated 11464652422eSDimitry Andric // and trivial. 1147*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 11484652422eSDimitry Andric __bit_iterator(const __bit_iterator<_Cp, false>& __it) _NOEXCEPT 11490b57cec5SDimitry Andric : __seg_(__it.__seg_), __ctz_(__it.__ctz_) {} 11500b57cec5SDimitry Andric 11514652422eSDimitry Andric // When _IsConst=false, we have a user-provided copy constructor, 11524652422eSDimitry Andric // so we must also provide a copy assignment operator because 11534652422eSDimitry Andric // the implicit generation of a defaulted one is deprecated. 11544652422eSDimitry Andric // When _IsConst=true, the assignment operators are 11554652422eSDimitry Andric // implicitly generated and trivial. 1156*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 11574652422eSDimitry Andric __bit_iterator& operator=(const _If<_IsConst, struct __private_nat, __bit_iterator>& __it) { 11584652422eSDimitry Andric __seg_ = __it.__seg_; 11594652422eSDimitry Andric __ctz_ = __it.__ctz_; 11604652422eSDimitry Andric return *this; 11614652422eSDimitry Andric } 116247395794SDimitry Andric 1163*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 reference operator*() const _NOEXCEPT { 116481ad6265SDimitry Andric return typename conditional<_IsConst, __bit_const_reference<_Cp>, __bit_reference<_Cp> > 116581ad6265SDimitry Andric ::type(__seg_, __storage_type(1) << __ctz_); 116681ad6265SDimitry Andric } 11670b57cec5SDimitry Andric 1168*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 __bit_iterator& operator++() 11690b57cec5SDimitry Andric { 11700b57cec5SDimitry Andric if (__ctz_ != __bits_per_word-1) 11710b57cec5SDimitry Andric ++__ctz_; 11720b57cec5SDimitry Andric else 11730b57cec5SDimitry Andric { 11740b57cec5SDimitry Andric __ctz_ = 0; 11750b57cec5SDimitry Andric ++__seg_; 11760b57cec5SDimitry Andric } 11770b57cec5SDimitry Andric return *this; 11780b57cec5SDimitry Andric } 11790b57cec5SDimitry Andric 1180*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 __bit_iterator operator++(int) 11810b57cec5SDimitry Andric { 11820b57cec5SDimitry Andric __bit_iterator __tmp = *this; 11830b57cec5SDimitry Andric ++(*this); 11840b57cec5SDimitry Andric return __tmp; 11850b57cec5SDimitry Andric } 11860b57cec5SDimitry Andric 1187*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 __bit_iterator& operator--() 11880b57cec5SDimitry Andric { 11890b57cec5SDimitry Andric if (__ctz_ != 0) 11900b57cec5SDimitry Andric --__ctz_; 11910b57cec5SDimitry Andric else 11920b57cec5SDimitry Andric { 11930b57cec5SDimitry Andric __ctz_ = __bits_per_word - 1; 11940b57cec5SDimitry Andric --__seg_; 11950b57cec5SDimitry Andric } 11960b57cec5SDimitry Andric return *this; 11970b57cec5SDimitry Andric } 11980b57cec5SDimitry Andric 1199*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 __bit_iterator operator--(int) 12000b57cec5SDimitry Andric { 12010b57cec5SDimitry Andric __bit_iterator __tmp = *this; 12020b57cec5SDimitry Andric --(*this); 12030b57cec5SDimitry Andric return __tmp; 12040b57cec5SDimitry Andric } 12050b57cec5SDimitry Andric 1206*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 __bit_iterator& operator+=(difference_type __n) 12070b57cec5SDimitry Andric { 12080b57cec5SDimitry Andric if (__n >= 0) 12090b57cec5SDimitry Andric __seg_ += (__n + __ctz_) / __bits_per_word; 12100b57cec5SDimitry Andric else 12110b57cec5SDimitry Andric __seg_ += static_cast<difference_type>(__n - __bits_per_word + __ctz_ + 1) 12120b57cec5SDimitry Andric / static_cast<difference_type>(__bits_per_word); 12130b57cec5SDimitry Andric __n &= (__bits_per_word - 1); 12140b57cec5SDimitry Andric __ctz_ = static_cast<unsigned>((__n + __ctz_) % __bits_per_word); 12150b57cec5SDimitry Andric return *this; 12160b57cec5SDimitry Andric } 12170b57cec5SDimitry Andric 1218*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 __bit_iterator& operator-=(difference_type __n) 12190b57cec5SDimitry Andric { 12200b57cec5SDimitry Andric return *this += -__n; 12210b57cec5SDimitry Andric } 12220b57cec5SDimitry Andric 1223*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 __bit_iterator operator+(difference_type __n) const 12240b57cec5SDimitry Andric { 12250b57cec5SDimitry Andric __bit_iterator __t(*this); 12260b57cec5SDimitry Andric __t += __n; 12270b57cec5SDimitry Andric return __t; 12280b57cec5SDimitry Andric } 12290b57cec5SDimitry Andric 1230*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 __bit_iterator operator-(difference_type __n) const 12310b57cec5SDimitry Andric { 12320b57cec5SDimitry Andric __bit_iterator __t(*this); 12330b57cec5SDimitry Andric __t -= __n; 12340b57cec5SDimitry Andric return __t; 12350b57cec5SDimitry Andric } 12360b57cec5SDimitry Andric 1237*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 12380b57cec5SDimitry Andric friend __bit_iterator operator+(difference_type __n, const __bit_iterator& __it) {return __it + __n;} 12390b57cec5SDimitry Andric 1240*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 12410b57cec5SDimitry Andric friend difference_type operator-(const __bit_iterator& __x, const __bit_iterator& __y) 12420b57cec5SDimitry Andric {return (__x.__seg_ - __y.__seg_) * __bits_per_word + __x.__ctz_ - __y.__ctz_;} 12430b57cec5SDimitry Andric 1244*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 reference operator[](difference_type __n) const {return *(*this + __n);} 12450b57cec5SDimitry Andric 1246*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 friend bool operator==(const __bit_iterator& __x, const __bit_iterator& __y) 12470b57cec5SDimitry Andric {return __x.__seg_ == __y.__seg_ && __x.__ctz_ == __y.__ctz_;} 12480b57cec5SDimitry Andric 1249*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 friend bool operator!=(const __bit_iterator& __x, const __bit_iterator& __y) 12500b57cec5SDimitry Andric {return !(__x == __y);} 12510b57cec5SDimitry Andric 1252*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 friend bool operator<(const __bit_iterator& __x, const __bit_iterator& __y) 12530b57cec5SDimitry Andric {return __x.__seg_ < __y.__seg_ || (__x.__seg_ == __y.__seg_ && __x.__ctz_ < __y.__ctz_);} 12540b57cec5SDimitry Andric 1255*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 friend bool operator>(const __bit_iterator& __x, const __bit_iterator& __y) 12560b57cec5SDimitry Andric {return __y < __x;} 12570b57cec5SDimitry Andric 1258*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 friend bool operator<=(const __bit_iterator& __x, const __bit_iterator& __y) 12590b57cec5SDimitry Andric {return !(__y < __x);} 12600b57cec5SDimitry Andric 1261*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 friend bool operator>=(const __bit_iterator& __x, const __bit_iterator& __y) 12620b57cec5SDimitry Andric {return !(__x < __y);} 12630b57cec5SDimitry Andric 12640b57cec5SDimitry Andricprivate: 1265*61cfbce3SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 126681ad6265SDimitry Andric explicit __bit_iterator(__storage_pointer __s, unsigned __ctz) _NOEXCEPT 12670b57cec5SDimitry Andric : __seg_(__s), __ctz_(__ctz) {} 12680b57cec5SDimitry Andric 12690b57cec5SDimitry Andric friend typename _Cp::__self; 12700b57cec5SDimitry Andric 12710b57cec5SDimitry Andric friend class __bit_reference<_Cp>; 12720b57cec5SDimitry Andric friend class __bit_const_reference<_Cp>; 12730b57cec5SDimitry Andric friend class __bit_iterator<_Cp, true>; 12740b57cec5SDimitry Andric template <class _Dp> friend struct __bit_array; 1275*61cfbce3SDimitry Andric template <class _Dp> 1276*61cfbce3SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 1277*61cfbce3SDimitry Andric friend void __fill_n_false(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n); 1278*61cfbce3SDimitry Andric 1279*61cfbce3SDimitry Andric template <class _Dp> 1280*61cfbce3SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 1281*61cfbce3SDimitry Andric friend void __fill_n_true(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n); 1282*61cfbce3SDimitry Andric 1283*61cfbce3SDimitry Andric template <class _Dp, bool _IC> 1284*61cfbce3SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 1285*61cfbce3SDimitry Andric friend __bit_iterator<_Dp, false> __copy_aligned(__bit_iterator<_Dp, _IC> __first, 12860b57cec5SDimitry Andric __bit_iterator<_Dp, _IC> __last, 12870b57cec5SDimitry Andric __bit_iterator<_Dp, false> __result); 1288*61cfbce3SDimitry Andric template <class _Dp, bool _IC> 1289*61cfbce3SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 1290*61cfbce3SDimitry Andric friend __bit_iterator<_Dp, false> __copy_unaligned(__bit_iterator<_Dp, _IC> __first, 12910b57cec5SDimitry Andric __bit_iterator<_Dp, _IC> __last, 12920b57cec5SDimitry Andric __bit_iterator<_Dp, false> __result); 1293*61cfbce3SDimitry Andric template <class _Dp, bool _IC> 1294*61cfbce3SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 1295*61cfbce3SDimitry Andric friend __bit_iterator<_Dp, false> copy(__bit_iterator<_Dp, _IC> __first, 12960b57cec5SDimitry Andric __bit_iterator<_Dp, _IC> __last, 12970b57cec5SDimitry Andric __bit_iterator<_Dp, false> __result); 1298*61cfbce3SDimitry Andric template <class _Dp, bool _IC> 1299*61cfbce3SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 1300*61cfbce3SDimitry Andric friend __bit_iterator<_Dp, false> __copy_backward_aligned(__bit_iterator<_Dp, _IC> __first, 13010b57cec5SDimitry Andric __bit_iterator<_Dp, _IC> __last, 13020b57cec5SDimitry Andric __bit_iterator<_Dp, false> __result); 1303*61cfbce3SDimitry Andric template <class _Dp, bool _IC> 1304*61cfbce3SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 1305*61cfbce3SDimitry Andric friend __bit_iterator<_Dp, false> __copy_backward_unaligned(__bit_iterator<_Dp, _IC> __first, 13060b57cec5SDimitry Andric __bit_iterator<_Dp, _IC> __last, 13070b57cec5SDimitry Andric __bit_iterator<_Dp, false> __result); 1308*61cfbce3SDimitry Andric template <class _Dp, bool _IC> 1309*61cfbce3SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 1310*61cfbce3SDimitry Andric friend __bit_iterator<_Dp, false> copy_backward(__bit_iterator<_Dp, _IC> __first, 13110b57cec5SDimitry Andric __bit_iterator<_Dp, _IC> __last, 13120b57cec5SDimitry Andric __bit_iterator<_Dp, false> __result); 13130b57cec5SDimitry Andric template <class __C1, class __C2>friend __bit_iterator<__C2, false> __swap_ranges_aligned(__bit_iterator<__C1, false>, 13140b57cec5SDimitry Andric __bit_iterator<__C1, false>, 13150b57cec5SDimitry Andric __bit_iterator<__C2, false>); 13160b57cec5SDimitry Andric template <class __C1, class __C2>friend __bit_iterator<__C2, false> __swap_ranges_unaligned(__bit_iterator<__C1, false>, 13170b57cec5SDimitry Andric __bit_iterator<__C1, false>, 13180b57cec5SDimitry Andric __bit_iterator<__C2, false>); 13190b57cec5SDimitry Andric template <class __C1, class __C2>friend __bit_iterator<__C2, false> swap_ranges(__bit_iterator<__C1, false>, 13200b57cec5SDimitry Andric __bit_iterator<__C1, false>, 13210b57cec5SDimitry Andric __bit_iterator<__C2, false>); 1322*61cfbce3SDimitry Andric template <class _Dp> 1323*61cfbce3SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 1324*61cfbce3SDimitry Andric friend __bit_iterator<_Dp, false> rotate(__bit_iterator<_Dp, false>, 13250b57cec5SDimitry Andric __bit_iterator<_Dp, false>, 13260b57cec5SDimitry Andric __bit_iterator<_Dp, false>); 1327*61cfbce3SDimitry Andric template <class _Dp, bool _IC1, bool _IC2> 1328*61cfbce3SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 1329*61cfbce3SDimitry Andric friend bool __equal_aligned(__bit_iterator<_Dp, _IC1>, 13300b57cec5SDimitry Andric __bit_iterator<_Dp, _IC1>, 13310b57cec5SDimitry Andric __bit_iterator<_Dp, _IC2>); 1332*61cfbce3SDimitry Andric template <class _Dp, bool _IC1, bool _IC2> 1333*61cfbce3SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 1334*61cfbce3SDimitry Andric friend bool __equal_unaligned(__bit_iterator<_Dp, _IC1>, 13350b57cec5SDimitry Andric __bit_iterator<_Dp, _IC1>, 13360b57cec5SDimitry Andric __bit_iterator<_Dp, _IC2>); 1337*61cfbce3SDimitry Andric template <class _Dp, bool _IC1, bool _IC2> 1338*61cfbce3SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 1339*61cfbce3SDimitry Andric friend bool equal(__bit_iterator<_Dp, _IC1>, 13400b57cec5SDimitry Andric __bit_iterator<_Dp, _IC1>, 13410b57cec5SDimitry Andric __bit_iterator<_Dp, _IC2>); 1342*61cfbce3SDimitry Andric template <class _Dp, bool _IC> 1343*61cfbce3SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 1344*61cfbce3SDimitry Andric friend __bit_iterator<_Dp, _IC> __find_bool_true(__bit_iterator<_Dp, _IC>, typename _Dp::size_type); 1345*61cfbce3SDimitry Andric template <class _Dp, bool _IC> 1346*61cfbce3SDimitry Andric _LIBCPP_CONSTEXPR_AFTER_CXX17 1347*61cfbce3SDimitry Andric friend __bit_iterator<_Dp, _IC> __find_bool_false(__bit_iterator<_Dp, _IC>, typename _Dp::size_type); 13480b57cec5SDimitry Andric template <class _Dp, bool _IC> friend typename __bit_iterator<_Dp, _IC>::difference_type 13490b57cec5SDimitry Andric __count_bool_true(__bit_iterator<_Dp, _IC>, typename _Dp::size_type); 13500b57cec5SDimitry Andric template <class _Dp, bool _IC> friend typename __bit_iterator<_Dp, _IC>::difference_type 13510b57cec5SDimitry Andric __count_bool_false(__bit_iterator<_Dp, _IC>, typename _Dp::size_type); 13520b57cec5SDimitry Andric}; 13530b57cec5SDimitry Andric 13540b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD 13550b57cec5SDimitry Andric 13560b57cec5SDimitry Andric_LIBCPP_POP_MACROS 13570b57cec5SDimitry Andric 13580b57cec5SDimitry Andric#endif // _LIBCPP___BIT_REFERENCE 1359