__bit_reference 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___BIT_REFERENCE
  10. #define _LIBCPP___BIT_REFERENCE
  11. #include <__algorithm/copy_n.h>
  12. #include <__algorithm/fill_n.h>
  13. #include <__algorithm/min.h>
  14. #include <__bit/countr.h>
  15. #include <__bit/popcount.h>
  16. #include <__config>
  17. #include <__iterator/iterator_traits.h>
  18. #include <__memory/construct_at.h>
  19. #include <__memory/pointer_traits.h>
  20. #include <cstring>
  21. #include <type_traits>
  22. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  23. # pragma GCC system_header
  24. #endif
  25. _LIBCPP_PUSH_MACROS
  26. #include <__undef_macros>
  27. _LIBCPP_BEGIN_NAMESPACE_STD
  28. template <class _Cp, bool _IsConst, class = typename _Cp::__storage_type> class __bit_iterator;
  29. template <class _Cp> class __bit_const_reference;
  30. template <class _Tp>
  31. struct __has_storage_type
  32. {
  33. static const bool value = false;
  34. };
  35. template <class _Cp, bool = __has_storage_type<_Cp>::value>
  36. class __bit_reference
  37. {
  38. typedef typename _Cp::__storage_type __storage_type;
  39. typedef typename _Cp::__storage_pointer __storage_pointer;
  40. __storage_pointer __seg_;
  41. __storage_type __mask_;
  42. friend typename _Cp::__self;
  43. friend class __bit_const_reference<_Cp>;
  44. friend class __bit_iterator<_Cp, false>;
  45. public:
  46. using __container = typename _Cp::__self;
  47. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  48. __bit_reference(const __bit_reference&) = default;
  49. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 operator bool() const _NOEXCEPT
  50. {return static_cast<bool>(*__seg_ & __mask_);}
  51. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 bool operator ~() const _NOEXCEPT
  52. {return !static_cast<bool>(*this);}
  53. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  54. __bit_reference& operator=(bool __x) _NOEXCEPT
  55. {
  56. if (__x)
  57. *__seg_ |= __mask_;
  58. else
  59. *__seg_ &= ~__mask_;
  60. return *this;
  61. }
  62. #if _LIBCPP_STD_VER > 20
  63. _LIBCPP_HIDE_FROM_ABI constexpr const __bit_reference& operator=(bool __x) const noexcept {
  64. if (__x)
  65. *__seg_ |= __mask_;
  66. else
  67. *__seg_ &= ~__mask_;
  68. return *this;
  69. }
  70. #endif
  71. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  72. __bit_reference& operator=(const __bit_reference& __x) _NOEXCEPT
  73. {return operator=(static_cast<bool>(__x));}
  74. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void flip() _NOEXCEPT {*__seg_ ^= __mask_;}
  75. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, false> operator&() const _NOEXCEPT
  76. {return __bit_iterator<_Cp, false>(__seg_, static_cast<unsigned>(std::__libcpp_ctz(__mask_)));}
  77. private:
  78. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  79. explicit __bit_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT
  80. : __seg_(__s), __mask_(__m) {}
  81. };
  82. template <class _Cp>
  83. class __bit_reference<_Cp, false>
  84. {
  85. };
  86. template <class _Cp>
  87. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  88. void
  89. swap(__bit_reference<_Cp> __x, __bit_reference<_Cp> __y) _NOEXCEPT
  90. {
  91. bool __t = __x;
  92. __x = __y;
  93. __y = __t;
  94. }
  95. template <class _Cp, class _Dp>
  96. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  97. void
  98. swap(__bit_reference<_Cp> __x, __bit_reference<_Dp> __y) _NOEXCEPT
  99. {
  100. bool __t = __x;
  101. __x = __y;
  102. __y = __t;
  103. }
  104. template <class _Cp>
  105. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  106. void
  107. swap(__bit_reference<_Cp> __x, bool& __y) _NOEXCEPT
  108. {
  109. bool __t = __x;
  110. __x = __y;
  111. __y = __t;
  112. }
  113. template <class _Cp>
  114. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  115. void
  116. swap(bool& __x, __bit_reference<_Cp> __y) _NOEXCEPT
  117. {
  118. bool __t = __x;
  119. __x = __y;
  120. __y = __t;
  121. }
  122. template <class _Cp>
  123. class __bit_const_reference
  124. {
  125. typedef typename _Cp::__storage_type __storage_type;
  126. typedef typename _Cp::__const_storage_pointer __storage_pointer;
  127. __storage_pointer __seg_;
  128. __storage_type __mask_;
  129. friend typename _Cp::__self;
  130. friend class __bit_iterator<_Cp, true>;
  131. public:
  132. _LIBCPP_INLINE_VISIBILITY
  133. __bit_const_reference(const __bit_const_reference&) = default;
  134. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  135. __bit_const_reference(const __bit_reference<_Cp>& __x) _NOEXCEPT
  136. : __seg_(__x.__seg_), __mask_(__x.__mask_) {}
  137. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR operator bool() const _NOEXCEPT
  138. {return static_cast<bool>(*__seg_ & __mask_);}
  139. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, true> operator&() const _NOEXCEPT
  140. {return __bit_iterator<_Cp, true>(__seg_, static_cast<unsigned>(std::__libcpp_ctz(__mask_)));}
  141. private:
  142. _LIBCPP_INLINE_VISIBILITY
  143. _LIBCPP_CONSTEXPR
  144. explicit __bit_const_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT
  145. : __seg_(__s), __mask_(__m) {}
  146. __bit_const_reference& operator=(const __bit_const_reference&) = delete;
  147. };
  148. // find
  149. template <class _Cp, bool _IsConst>
  150. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, _IsConst>
  151. __find_bool_true(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n)
  152. {
  153. typedef __bit_iterator<_Cp, _IsConst> _It;
  154. typedef typename _It::__storage_type __storage_type;
  155. const int __bits_per_word = _It::__bits_per_word;
  156. // do first partial word
  157. if (__first.__ctz_ != 0)
  158. {
  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;
  163. if (__b)
  164. return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__libcpp_ctz(__b)));
  165. if (__n == __dn)
  166. return __first + __n;
  167. __n -= __dn;
  168. ++__first.__seg_;
  169. }
  170. // do middle whole words
  171. for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
  172. if (*__first.__seg_)
  173. return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__libcpp_ctz(*__first.__seg_)));
  174. // do last partial word
  175. if (__n > 0)
  176. {
  177. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
  178. __storage_type __b = *__first.__seg_ & __m;
  179. if (__b)
  180. return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__libcpp_ctz(__b)));
  181. }
  182. return _It(__first.__seg_, static_cast<unsigned>(__n));
  183. }
  184. template <class _Cp, bool _IsConst>
  185. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, _IsConst>
  186. __find_bool_false(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n)
  187. {
  188. typedef __bit_iterator<_Cp, _IsConst> _It;
  189. typedef typename _It::__storage_type __storage_type;
  190. const int __bits_per_word = _It::__bits_per_word;
  191. // do first partial word
  192. if (__first.__ctz_ != 0)
  193. {
  194. __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
  195. __storage_type __dn = _VSTD::min(__clz_f, __n);
  196. __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
  197. __storage_type __b = ~*__first.__seg_ & __m;
  198. if (__b)
  199. return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__libcpp_ctz(__b)));
  200. if (__n == __dn)
  201. return __first + __n;
  202. __n -= __dn;
  203. ++__first.__seg_;
  204. }
  205. // do middle whole words
  206. for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
  207. {
  208. __storage_type __b = ~*__first.__seg_;
  209. if (__b)
  210. return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__libcpp_ctz(__b)));
  211. }
  212. // do last partial word
  213. if (__n > 0)
  214. {
  215. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
  216. __storage_type __b = ~*__first.__seg_ & __m;
  217. if (__b)
  218. return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__libcpp_ctz(__b)));
  219. }
  220. return _It(__first.__seg_, static_cast<unsigned>(__n));
  221. }
  222. template <class _Cp, bool _IsConst, class _Tp>
  223. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  224. __bit_iterator<_Cp, _IsConst>
  225. find(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value)
  226. {
  227. if (static_cast<bool>(__value))
  228. return _VSTD::__find_bool_true(__first, static_cast<typename _Cp::size_type>(__last - __first));
  229. return _VSTD::__find_bool_false(__first, static_cast<typename _Cp::size_type>(__last - __first));
  230. }
  231. // count
  232. template <class _Cp, bool _IsConst>
  233. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 typename __bit_iterator<_Cp, _IsConst>::difference_type
  234. __count_bool_true(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n)
  235. {
  236. typedef __bit_iterator<_Cp, _IsConst> _It;
  237. typedef typename _It::__storage_type __storage_type;
  238. typedef typename _It::difference_type difference_type;
  239. const int __bits_per_word = _It::__bits_per_word;
  240. difference_type __r = 0;
  241. // do first partial word
  242. if (__first.__ctz_ != 0)
  243. {
  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::__libcpp_popcount(*__first.__seg_ & __m);
  248. __n -= __dn;
  249. ++__first.__seg_;
  250. }
  251. // do middle whole words
  252. for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
  253. __r += _VSTD::__libcpp_popcount(*__first.__seg_);
  254. // do last partial word
  255. if (__n > 0)
  256. {
  257. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
  258. __r += _VSTD::__libcpp_popcount(*__first.__seg_ & __m);
  259. }
  260. return __r;
  261. }
  262. template <class _Cp, bool _IsConst>
  263. _LIBCPP_HIDE_FROM_ABI typename __bit_iterator<_Cp, _IsConst>::difference_type
  264. __count_bool_false(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n)
  265. {
  266. typedef __bit_iterator<_Cp, _IsConst> _It;
  267. typedef typename _It::__storage_type __storage_type;
  268. typedef typename _It::difference_type difference_type;
  269. const int __bits_per_word = _It::__bits_per_word;
  270. difference_type __r = 0;
  271. // do first partial word
  272. if (__first.__ctz_ != 0)
  273. {
  274. __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
  275. __storage_type __dn = _VSTD::min(__clz_f, __n);
  276. __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
  277. __r = _VSTD::__libcpp_popcount(~*__first.__seg_ & __m);
  278. __n -= __dn;
  279. ++__first.__seg_;
  280. }
  281. // do middle whole words
  282. for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
  283. __r += _VSTD::__libcpp_popcount(~*__first.__seg_);
  284. // do last partial word
  285. if (__n > 0)
  286. {
  287. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
  288. __r += _VSTD::__libcpp_popcount(~*__first.__seg_ & __m);
  289. }
  290. return __r;
  291. }
  292. template <class _Cp, bool _IsConst, class _Tp>
  293. inline _LIBCPP_INLINE_VISIBILITY
  294. typename __bit_iterator<_Cp, _IsConst>::difference_type
  295. count(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value)
  296. {
  297. if (static_cast<bool>(__value))
  298. return _VSTD::__count_bool_true(__first, static_cast<typename _Cp::size_type>(__last - __first));
  299. return _VSTD::__count_bool_false(__first, static_cast<typename _Cp::size_type>(__last - __first));
  300. }
  301. // fill_n
  302. template <class _Cp>
  303. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
  304. __fill_n_false(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n)
  305. {
  306. typedef __bit_iterator<_Cp, false> _It;
  307. typedef typename _It::__storage_type __storage_type;
  308. const int __bits_per_word = _It::__bits_per_word;
  309. // do first partial word
  310. if (__first.__ctz_ != 0)
  311. {
  312. __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
  313. __storage_type __dn = _VSTD::min(__clz_f, __n);
  314. __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
  315. *__first.__seg_ &= ~__m;
  316. __n -= __dn;
  317. ++__first.__seg_;
  318. }
  319. // do middle whole words
  320. __storage_type __nw = __n / __bits_per_word;
  321. std::fill_n(std::__to_address(__first.__seg_), __nw, 0);
  322. __n -= __nw * __bits_per_word;
  323. // do last partial word
  324. if (__n > 0)
  325. {
  326. __first.__seg_ += __nw;
  327. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
  328. *__first.__seg_ &= ~__m;
  329. }
  330. }
  331. template <class _Cp>
  332. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
  333. __fill_n_true(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n)
  334. {
  335. typedef __bit_iterator<_Cp, false> _It;
  336. typedef typename _It::__storage_type __storage_type;
  337. const int __bits_per_word = _It::__bits_per_word;
  338. // do first partial word
  339. if (__first.__ctz_ != 0)
  340. {
  341. __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
  342. __storage_type __dn = _VSTD::min(__clz_f, __n);
  343. __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
  344. *__first.__seg_ |= __m;
  345. __n -= __dn;
  346. ++__first.__seg_;
  347. }
  348. // do middle whole words
  349. __storage_type __nw = __n / __bits_per_word;
  350. // __storage_type is always an unsigned type, so -1 sets all bits
  351. std::fill_n(std::__to_address(__first.__seg_), __nw, static_cast<__storage_type>(-1));
  352. __n -= __nw * __bits_per_word;
  353. // do last partial word
  354. if (__n > 0)
  355. {
  356. __first.__seg_ += __nw;
  357. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
  358. *__first.__seg_ |= __m;
  359. }
  360. }
  361. template <class _Cp>
  362. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  363. void
  364. fill_n(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n, bool __value)
  365. {
  366. if (__n > 0)
  367. {
  368. if (__value)
  369. _VSTD::__fill_n_true(__first, __n);
  370. else
  371. _VSTD::__fill_n_false(__first, __n);
  372. }
  373. }
  374. // fill
  375. template <class _Cp>
  376. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  377. void
  378. fill(__bit_iterator<_Cp, false> __first, __bit_iterator<_Cp, false> __last, bool __value)
  379. {
  380. _VSTD::fill_n(__first, static_cast<typename _Cp::size_type>(__last - __first), __value);
  381. }
  382. // copy
  383. template <class _Cp, bool _IsConst>
  384. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, false>
  385. __copy_aligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last,
  386. __bit_iterator<_Cp, false> __result)
  387. {
  388. typedef __bit_iterator<_Cp, _IsConst> _In;
  389. typedef typename _In::difference_type difference_type;
  390. typedef typename _In::__storage_type __storage_type;
  391. const int __bits_per_word = _In::__bits_per_word;
  392. difference_type __n = __last - __first;
  393. if (__n > 0)
  394. {
  395. // do first word
  396. if (__first.__ctz_ != 0)
  397. {
  398. unsigned __clz = __bits_per_word - __first.__ctz_;
  399. difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz), __n);
  400. __n -= __dn;
  401. __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz - __dn));
  402. __storage_type __b = *__first.__seg_ & __m;
  403. *__result.__seg_ &= ~__m;
  404. *__result.__seg_ |= __b;
  405. __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
  406. __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word);
  407. ++__first.__seg_;
  408. // __first.__ctz_ = 0;
  409. }
  410. // __first.__ctz_ == 0;
  411. // do middle words
  412. __storage_type __nw = __n / __bits_per_word;
  413. std::copy_n(std::__to_address(__first.__seg_), __nw, std::__to_address(__result.__seg_));
  414. __n -= __nw * __bits_per_word;
  415. __result.__seg_ += __nw;
  416. // do last word
  417. if (__n > 0)
  418. {
  419. __first.__seg_ += __nw;
  420. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
  421. __storage_type __b = *__first.__seg_ & __m;
  422. *__result.__seg_ &= ~__m;
  423. *__result.__seg_ |= __b;
  424. __result.__ctz_ = static_cast<unsigned>(__n);
  425. }
  426. }
  427. return __result;
  428. }
  429. template <class _Cp, bool _IsConst>
  430. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, false>
  431. __copy_unaligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last,
  432. __bit_iterator<_Cp, false> __result)
  433. {
  434. typedef __bit_iterator<_Cp, _IsConst> _In;
  435. typedef typename _In::difference_type difference_type;
  436. typedef typename _In::__storage_type __storage_type;
  437. const int __bits_per_word = _In::__bits_per_word;
  438. difference_type __n = __last - __first;
  439. if (__n > 0)
  440. {
  441. // do first word
  442. if (__first.__ctz_ != 0)
  443. {
  444. unsigned __clz_f = __bits_per_word - __first.__ctz_;
  445. difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz_f), __n);
  446. __n -= __dn;
  447. __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
  448. __storage_type __b = *__first.__seg_ & __m;
  449. unsigned __clz_r = __bits_per_word - __result.__ctz_;
  450. __storage_type __ddn = _VSTD::min<__storage_type>(__dn, __clz_r);
  451. __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn));
  452. *__result.__seg_ &= ~__m;
  453. if (__result.__ctz_ > __first.__ctz_)
  454. *__result.__seg_ |= __b << (__result.__ctz_ - __first.__ctz_);
  455. else
  456. *__result.__seg_ |= __b >> (__first.__ctz_ - __result.__ctz_);
  457. __result.__seg_ += (__ddn + __result.__ctz_) / __bits_per_word;
  458. __result.__ctz_ = static_cast<unsigned>((__ddn + __result.__ctz_) % __bits_per_word);
  459. __dn -= __ddn;
  460. if (__dn > 0)
  461. {
  462. __m = ~__storage_type(0) >> (__bits_per_word - __dn);
  463. *__result.__seg_ &= ~__m;
  464. *__result.__seg_ |= __b >> (__first.__ctz_ + __ddn);
  465. __result.__ctz_ = static_cast<unsigned>(__dn);
  466. }
  467. ++__first.__seg_;
  468. // __first.__ctz_ = 0;
  469. }
  470. // __first.__ctz_ == 0;
  471. // do middle words
  472. unsigned __clz_r = __bits_per_word - __result.__ctz_;
  473. __storage_type __m = ~__storage_type(0) << __result.__ctz_;
  474. for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_)
  475. {
  476. __storage_type __b = *__first.__seg_;
  477. *__result.__seg_ &= ~__m;
  478. *__result.__seg_ |= __b << __result.__ctz_;
  479. ++__result.__seg_;
  480. *__result.__seg_ &= __m;
  481. *__result.__seg_ |= __b >> __clz_r;
  482. }
  483. // do last word
  484. if (__n > 0)
  485. {
  486. __m = ~__storage_type(0) >> (__bits_per_word - __n);
  487. __storage_type __b = *__first.__seg_ & __m;
  488. __storage_type __dn = _VSTD::min(__n, static_cast<difference_type>(__clz_r));
  489. __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn));
  490. *__result.__seg_ &= ~__m;
  491. *__result.__seg_ |= __b << __result.__ctz_;
  492. __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
  493. __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word);
  494. __n -= __dn;
  495. if (__n > 0)
  496. {
  497. __m = ~__storage_type(0) >> (__bits_per_word - __n);
  498. *__result.__seg_ &= ~__m;
  499. *__result.__seg_ |= __b >> __dn;
  500. __result.__ctz_ = static_cast<unsigned>(__n);
  501. }
  502. }
  503. }
  504. return __result;
  505. }
  506. template <class _Cp, bool _IsConst>
  507. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  508. __bit_iterator<_Cp, false>
  509. copy(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result)
  510. {
  511. if (__first.__ctz_ == __result.__ctz_)
  512. return _VSTD::__copy_aligned(__first, __last, __result);
  513. return _VSTD::__copy_unaligned(__first, __last, __result);
  514. }
  515. // copy_backward
  516. template <class _Cp, bool _IsConst>
  517. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, false>
  518. __copy_backward_aligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last,
  519. __bit_iterator<_Cp, false> __result)
  520. {
  521. typedef __bit_iterator<_Cp, _IsConst> _In;
  522. typedef typename _In::difference_type difference_type;
  523. typedef typename _In::__storage_type __storage_type;
  524. const int __bits_per_word = _In::__bits_per_word;
  525. difference_type __n = __last - __first;
  526. if (__n > 0)
  527. {
  528. // do first word
  529. if (__last.__ctz_ != 0)
  530. {
  531. difference_type __dn = _VSTD::min(static_cast<difference_type>(__last.__ctz_), __n);
  532. __n -= __dn;
  533. unsigned __clz = __bits_per_word - __last.__ctz_;
  534. __storage_type __m = (~__storage_type(0) << (__last.__ctz_ - __dn)) & (~__storage_type(0) >> __clz);
  535. __storage_type __b = *__last.__seg_ & __m;
  536. *__result.__seg_ &= ~__m;
  537. *__result.__seg_ |= __b;
  538. __result.__ctz_ = static_cast<unsigned>(((-__dn & (__bits_per_word - 1)) +
  539. __result.__ctz_) % __bits_per_word);
  540. // __last.__ctz_ = 0
  541. }
  542. // __last.__ctz_ == 0 || __n == 0
  543. // __result.__ctz_ == 0 || __n == 0
  544. // do middle words
  545. __storage_type __nw = __n / __bits_per_word;
  546. __result.__seg_ -= __nw;
  547. __last.__seg_ -= __nw;
  548. std::copy_n(std::__to_address(__last.__seg_), __nw, std::__to_address(__result.__seg_));
  549. __n -= __nw * __bits_per_word;
  550. // do last word
  551. if (__n > 0)
  552. {
  553. __storage_type __m = ~__storage_type(0) << (__bits_per_word - __n);
  554. __storage_type __b = *--__last.__seg_ & __m;
  555. *--__result.__seg_ &= ~__m;
  556. *__result.__seg_ |= __b;
  557. __result.__ctz_ = static_cast<unsigned>(-__n & (__bits_per_word - 1));
  558. }
  559. }
  560. return __result;
  561. }
  562. template <class _Cp, bool _IsConst>
  563. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, false>
  564. __copy_backward_unaligned(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last,
  565. __bit_iterator<_Cp, false> __result)
  566. {
  567. typedef __bit_iterator<_Cp, _IsConst> _In;
  568. typedef typename _In::difference_type difference_type;
  569. typedef typename _In::__storage_type __storage_type;
  570. const int __bits_per_word = _In::__bits_per_word;
  571. difference_type __n = __last - __first;
  572. if (__n > 0)
  573. {
  574. // do first word
  575. if (__last.__ctz_ != 0)
  576. {
  577. difference_type __dn = _VSTD::min(static_cast<difference_type>(__last.__ctz_), __n);
  578. __n -= __dn;
  579. unsigned __clz_l = __bits_per_word - __last.__ctz_;
  580. __storage_type __m = (~__storage_type(0) << (__last.__ctz_ - __dn)) & (~__storage_type(0) >> __clz_l);
  581. __storage_type __b = *__last.__seg_ & __m;
  582. unsigned __clz_r = __bits_per_word - __result.__ctz_;
  583. __storage_type __ddn = _VSTD::min(__dn, static_cast<difference_type>(__result.__ctz_));
  584. if (__ddn > 0)
  585. {
  586. __m = (~__storage_type(0) << (__result.__ctz_ - __ddn)) & (~__storage_type(0) >> __clz_r);
  587. *__result.__seg_ &= ~__m;
  588. if (__result.__ctz_ > __last.__ctz_)
  589. *__result.__seg_ |= __b << (__result.__ctz_ - __last.__ctz_);
  590. else
  591. *__result.__seg_ |= __b >> (__last.__ctz_ - __result.__ctz_);
  592. __result.__ctz_ = static_cast<unsigned>(((-__ddn & (__bits_per_word - 1)) +
  593. __result.__ctz_) % __bits_per_word);
  594. __dn -= __ddn;
  595. }
  596. if (__dn > 0)
  597. {
  598. // __result.__ctz_ == 0
  599. --__result.__seg_;
  600. __result.__ctz_ = static_cast<unsigned>(-__dn & (__bits_per_word - 1));
  601. __m = ~__storage_type(0) << __result.__ctz_;
  602. *__result.__seg_ &= ~__m;
  603. __last.__ctz_ -= __dn + __ddn;
  604. *__result.__seg_ |= __b << (__result.__ctz_ - __last.__ctz_);
  605. }
  606. // __last.__ctz_ = 0
  607. }
  608. // __last.__ctz_ == 0 || __n == 0
  609. // __result.__ctz_ != 0 || __n == 0
  610. // do middle words
  611. unsigned __clz_r = __bits_per_word - __result.__ctz_;
  612. __storage_type __m = ~__storage_type(0) >> __clz_r;
  613. for (; __n >= __bits_per_word; __n -= __bits_per_word)
  614. {
  615. __storage_type __b = *--__last.__seg_;
  616. *__result.__seg_ &= ~__m;
  617. *__result.__seg_ |= __b >> __clz_r;
  618. *--__result.__seg_ &= __m;
  619. *__result.__seg_ |= __b << __result.__ctz_;
  620. }
  621. // do last word
  622. if (__n > 0)
  623. {
  624. __m = ~__storage_type(0) << (__bits_per_word - __n);
  625. __storage_type __b = *--__last.__seg_ & __m;
  626. __clz_r = __bits_per_word - __result.__ctz_;
  627. __storage_type __dn = _VSTD::min(__n, static_cast<difference_type>(__result.__ctz_));
  628. __m = (~__storage_type(0) << (__result.__ctz_ - __dn)) & (~__storage_type(0) >> __clz_r);
  629. *__result.__seg_ &= ~__m;
  630. *__result.__seg_ |= __b >> (__bits_per_word - __result.__ctz_);
  631. __result.__ctz_ = static_cast<unsigned>(((-__dn & (__bits_per_word - 1)) +
  632. __result.__ctz_) % __bits_per_word);
  633. __n -= __dn;
  634. if (__n > 0)
  635. {
  636. // __result.__ctz_ == 0
  637. --__result.__seg_;
  638. __result.__ctz_ = static_cast<unsigned>(-__n & (__bits_per_word - 1));
  639. __m = ~__storage_type(0) << __result.__ctz_;
  640. *__result.__seg_ &= ~__m;
  641. *__result.__seg_ |= __b << (__result.__ctz_ - (__bits_per_word - __n - __dn));
  642. }
  643. }
  644. }
  645. return __result;
  646. }
  647. template <class _Cp, bool _IsConst>
  648. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  649. __bit_iterator<_Cp, false>
  650. copy_backward(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result)
  651. {
  652. if (__last.__ctz_ == __result.__ctz_)
  653. return _VSTD::__copy_backward_aligned(__first, __last, __result);
  654. return _VSTD::__copy_backward_unaligned(__first, __last, __result);
  655. }
  656. // move
  657. template <class _Cp, bool _IsConst>
  658. inline _LIBCPP_INLINE_VISIBILITY
  659. __bit_iterator<_Cp, false>
  660. move(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result)
  661. {
  662. return _VSTD::copy(__first, __last, __result);
  663. }
  664. // move_backward
  665. template <class _Cp, bool _IsConst>
  666. inline _LIBCPP_INLINE_VISIBILITY
  667. __bit_iterator<_Cp, false>
  668. move_backward(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, __bit_iterator<_Cp, false> __result)
  669. {
  670. return _VSTD::copy_backward(__first, __last, __result);
  671. }
  672. // swap_ranges
  673. template <class __C1, class __C2>
  674. _LIBCPP_HIDE_FROM_ABI __bit_iterator<__C2, false>
  675. __swap_ranges_aligned(__bit_iterator<__C1, false> __first, __bit_iterator<__C1, false> __last,
  676. __bit_iterator<__C2, false> __result)
  677. {
  678. typedef __bit_iterator<__C1, false> _I1;
  679. typedef typename _I1::difference_type difference_type;
  680. typedef typename _I1::__storage_type __storage_type;
  681. const int __bits_per_word = _I1::__bits_per_word;
  682. difference_type __n = __last - __first;
  683. if (__n > 0)
  684. {
  685. // do first word
  686. if (__first.__ctz_ != 0)
  687. {
  688. unsigned __clz = __bits_per_word - __first.__ctz_;
  689. difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz), __n);
  690. __n -= __dn;
  691. __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz - __dn));
  692. __storage_type __b1 = *__first.__seg_ & __m;
  693. *__first.__seg_ &= ~__m;
  694. __storage_type __b2 = *__result.__seg_ & __m;
  695. *__result.__seg_ &= ~__m;
  696. *__result.__seg_ |= __b1;
  697. *__first.__seg_ |= __b2;
  698. __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
  699. __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word);
  700. ++__first.__seg_;
  701. // __first.__ctz_ = 0;
  702. }
  703. // __first.__ctz_ == 0;
  704. // do middle words
  705. for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_, ++__result.__seg_)
  706. swap(*__first.__seg_, *__result.__seg_);
  707. // do last word
  708. if (__n > 0)
  709. {
  710. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
  711. __storage_type __b1 = *__first.__seg_ & __m;
  712. *__first.__seg_ &= ~__m;
  713. __storage_type __b2 = *__result.__seg_ & __m;
  714. *__result.__seg_ &= ~__m;
  715. *__result.__seg_ |= __b1;
  716. *__first.__seg_ |= __b2;
  717. __result.__ctz_ = static_cast<unsigned>(__n);
  718. }
  719. }
  720. return __result;
  721. }
  722. template <class __C1, class __C2>
  723. _LIBCPP_HIDE_FROM_ABI __bit_iterator<__C2, false>
  724. __swap_ranges_unaligned(__bit_iterator<__C1, false> __first, __bit_iterator<__C1, false> __last,
  725. __bit_iterator<__C2, false> __result)
  726. {
  727. typedef __bit_iterator<__C1, false> _I1;
  728. typedef typename _I1::difference_type difference_type;
  729. typedef typename _I1::__storage_type __storage_type;
  730. const int __bits_per_word = _I1::__bits_per_word;
  731. difference_type __n = __last - __first;
  732. if (__n > 0)
  733. {
  734. // do first word
  735. if (__first.__ctz_ != 0)
  736. {
  737. unsigned __clz_f = __bits_per_word - __first.__ctz_;
  738. difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz_f), __n);
  739. __n -= __dn;
  740. __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
  741. __storage_type __b1 = *__first.__seg_ & __m;
  742. *__first.__seg_ &= ~__m;
  743. unsigned __clz_r = __bits_per_word - __result.__ctz_;
  744. __storage_type __ddn = _VSTD::min<__storage_type>(__dn, __clz_r);
  745. __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn));
  746. __storage_type __b2 = *__result.__seg_ & __m;
  747. *__result.__seg_ &= ~__m;
  748. if (__result.__ctz_ > __first.__ctz_)
  749. {
  750. unsigned __s = __result.__ctz_ - __first.__ctz_;
  751. *__result.__seg_ |= __b1 << __s;
  752. *__first.__seg_ |= __b2 >> __s;
  753. }
  754. else
  755. {
  756. unsigned __s = __first.__ctz_ - __result.__ctz_;
  757. *__result.__seg_ |= __b1 >> __s;
  758. *__first.__seg_ |= __b2 << __s;
  759. }
  760. __result.__seg_ += (__ddn + __result.__ctz_) / __bits_per_word;
  761. __result.__ctz_ = static_cast<unsigned>((__ddn + __result.__ctz_) % __bits_per_word);
  762. __dn -= __ddn;
  763. if (__dn > 0)
  764. {
  765. __m = ~__storage_type(0) >> (__bits_per_word - __dn);
  766. __b2 = *__result.__seg_ & __m;
  767. *__result.__seg_ &= ~__m;
  768. unsigned __s = __first.__ctz_ + __ddn;
  769. *__result.__seg_ |= __b1 >> __s;
  770. *__first.__seg_ |= __b2 << __s;
  771. __result.__ctz_ = static_cast<unsigned>(__dn);
  772. }
  773. ++__first.__seg_;
  774. // __first.__ctz_ = 0;
  775. }
  776. // __first.__ctz_ == 0;
  777. // do middle words
  778. __storage_type __m = ~__storage_type(0) << __result.__ctz_;
  779. unsigned __clz_r = __bits_per_word - __result.__ctz_;
  780. for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first.__seg_)
  781. {
  782. __storage_type __b1 = *__first.__seg_;
  783. __storage_type __b2 = *__result.__seg_ & __m;
  784. *__result.__seg_ &= ~__m;
  785. *__result.__seg_ |= __b1 << __result.__ctz_;
  786. *__first.__seg_ = __b2 >> __result.__ctz_;
  787. ++__result.__seg_;
  788. __b2 = *__result.__seg_ & ~__m;
  789. *__result.__seg_ &= __m;
  790. *__result.__seg_ |= __b1 >> __clz_r;
  791. *__first.__seg_ |= __b2 << __clz_r;
  792. }
  793. // do last word
  794. if (__n > 0)
  795. {
  796. __m = ~__storage_type(0) >> (__bits_per_word - __n);
  797. __storage_type __b1 = *__first.__seg_ & __m;
  798. *__first.__seg_ &= ~__m;
  799. __storage_type __dn = _VSTD::min<__storage_type>(__n, __clz_r);
  800. __m = (~__storage_type(0) << __result.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn));
  801. __storage_type __b2 = *__result.__seg_ & __m;
  802. *__result.__seg_ &= ~__m;
  803. *__result.__seg_ |= __b1 << __result.__ctz_;
  804. *__first.__seg_ |= __b2 >> __result.__ctz_;
  805. __result.__seg_ += (__dn + __result.__ctz_) / __bits_per_word;
  806. __result.__ctz_ = static_cast<unsigned>((__dn + __result.__ctz_) % __bits_per_word);
  807. __n -= __dn;
  808. if (__n > 0)
  809. {
  810. __m = ~__storage_type(0) >> (__bits_per_word - __n);
  811. __b2 = *__result.__seg_ & __m;
  812. *__result.__seg_ &= ~__m;
  813. *__result.__seg_ |= __b1 >> __dn;
  814. *__first.__seg_ |= __b2 << __dn;
  815. __result.__ctz_ = static_cast<unsigned>(__n);
  816. }
  817. }
  818. }
  819. return __result;
  820. }
  821. template <class __C1, class __C2>
  822. inline _LIBCPP_INLINE_VISIBILITY
  823. __bit_iterator<__C2, false>
  824. swap_ranges(__bit_iterator<__C1, false> __first1, __bit_iterator<__C1, false> __last1,
  825. __bit_iterator<__C2, false> __first2)
  826. {
  827. if (__first1.__ctz_ == __first2.__ctz_)
  828. return _VSTD::__swap_ranges_aligned(__first1, __last1, __first2);
  829. return _VSTD::__swap_ranges_unaligned(__first1, __last1, __first2);
  830. }
  831. // rotate
  832. template <class _Cp>
  833. struct __bit_array
  834. {
  835. typedef typename _Cp::difference_type difference_type;
  836. typedef typename _Cp::__storage_type __storage_type;
  837. typedef typename _Cp::__storage_pointer __storage_pointer;
  838. typedef typename _Cp::iterator iterator;
  839. static const unsigned __bits_per_word = _Cp::__bits_per_word;
  840. static const unsigned _Np = 4;
  841. difference_type __size_;
  842. __storage_type __word_[_Np];
  843. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 static difference_type capacity()
  844. {return static_cast<difference_type>(_Np * __bits_per_word);}
  845. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __bit_array(difference_type __s) : __size_(__s) {
  846. if (__libcpp_is_constant_evaluated()) {
  847. for (size_t __i = 0; __i != __bit_array<_Cp>::_Np; ++__i)
  848. std::__construct_at(__word_ + __i, 0);
  849. }
  850. }
  851. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator begin()
  852. {
  853. return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]), 0);
  854. }
  855. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator end()
  856. {
  857. return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]) + __size_ / __bits_per_word,
  858. static_cast<unsigned>(__size_ % __bits_per_word));
  859. }
  860. };
  861. template <class _Cp>
  862. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, false>
  863. rotate(__bit_iterator<_Cp, false> __first, __bit_iterator<_Cp, false> __middle, __bit_iterator<_Cp, false> __last)
  864. {
  865. typedef __bit_iterator<_Cp, false> _I1;
  866. typedef typename _I1::difference_type difference_type;
  867. difference_type __d1 = __middle - __first;
  868. difference_type __d2 = __last - __middle;
  869. _I1 __r = __first + __d2;
  870. while (__d1 != 0 && __d2 != 0)
  871. {
  872. if (__d1 <= __d2)
  873. {
  874. if (__d1 <= __bit_array<_Cp>::capacity())
  875. {
  876. __bit_array<_Cp> __b(__d1);
  877. _VSTD::copy(__first, __middle, __b.begin());
  878. _VSTD::copy(__b.begin(), __b.end(), _VSTD::copy(__middle, __last, __first));
  879. break;
  880. }
  881. else
  882. {
  883. __bit_iterator<_Cp, false> __mp = _VSTD::swap_ranges(__first, __middle, __middle);
  884. __first = __middle;
  885. __middle = __mp;
  886. __d2 -= __d1;
  887. }
  888. }
  889. else
  890. {
  891. if (__d2 <= __bit_array<_Cp>::capacity())
  892. {
  893. __bit_array<_Cp> __b(__d2);
  894. _VSTD::copy(__middle, __last, __b.begin());
  895. _VSTD::copy_backward(__b.begin(), __b.end(), _VSTD::copy_backward(__first, __middle, __last));
  896. break;
  897. }
  898. else
  899. {
  900. __bit_iterator<_Cp, false> __mp = __first + __d2;
  901. _VSTD::swap_ranges(__first, __mp, __middle);
  902. __first = __mp;
  903. __d1 -= __d2;
  904. }
  905. }
  906. }
  907. return __r;
  908. }
  909. // equal
  910. template <class _Cp, bool _IC1, bool _IC2>
  911. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
  912. __equal_unaligned(__bit_iterator<_Cp, _IC1> __first1, __bit_iterator<_Cp, _IC1> __last1,
  913. __bit_iterator<_Cp, _IC2> __first2)
  914. {
  915. typedef __bit_iterator<_Cp, _IC1> _It;
  916. typedef typename _It::difference_type difference_type;
  917. typedef typename _It::__storage_type __storage_type;
  918. const int __bits_per_word = _It::__bits_per_word;
  919. difference_type __n = __last1 - __first1;
  920. if (__n > 0)
  921. {
  922. // do first word
  923. if (__first1.__ctz_ != 0)
  924. {
  925. unsigned __clz_f = __bits_per_word - __first1.__ctz_;
  926. difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz_f), __n);
  927. __n -= __dn;
  928. __storage_type __m = (~__storage_type(0) << __first1.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
  929. __storage_type __b = *__first1.__seg_ & __m;
  930. unsigned __clz_r = __bits_per_word - __first2.__ctz_;
  931. __storage_type __ddn = _VSTD::min<__storage_type>(__dn, __clz_r);
  932. __m = (~__storage_type(0) << __first2.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn));
  933. if (__first2.__ctz_ > __first1.__ctz_)
  934. {
  935. if ((*__first2.__seg_ & __m) != (__b << (__first2.__ctz_ - __first1.__ctz_)))
  936. return false;
  937. }
  938. else
  939. {
  940. if ((*__first2.__seg_ & __m) != (__b >> (__first1.__ctz_ - __first2.__ctz_)))
  941. return false;
  942. }
  943. __first2.__seg_ += (__ddn + __first2.__ctz_) / __bits_per_word;
  944. __first2.__ctz_ = static_cast<unsigned>((__ddn + __first2.__ctz_) % __bits_per_word);
  945. __dn -= __ddn;
  946. if (__dn > 0)
  947. {
  948. __m = ~__storage_type(0) >> (__bits_per_word - __dn);
  949. if ((*__first2.__seg_ & __m) != (__b >> (__first1.__ctz_ + __ddn)))
  950. return false;
  951. __first2.__ctz_ = static_cast<unsigned>(__dn);
  952. }
  953. ++__first1.__seg_;
  954. // __first1.__ctz_ = 0;
  955. }
  956. // __first1.__ctz_ == 0;
  957. // do middle words
  958. unsigned __clz_r = __bits_per_word - __first2.__ctz_;
  959. __storage_type __m = ~__storage_type(0) << __first2.__ctz_;
  960. for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first1.__seg_)
  961. {
  962. __storage_type __b = *__first1.__seg_;
  963. if ((*__first2.__seg_ & __m) != (__b << __first2.__ctz_))
  964. return false;
  965. ++__first2.__seg_;
  966. if ((*__first2.__seg_ & ~__m) != (__b >> __clz_r))
  967. return false;
  968. }
  969. // do last word
  970. if (__n > 0)
  971. {
  972. __m = ~__storage_type(0) >> (__bits_per_word - __n);
  973. __storage_type __b = *__first1.__seg_ & __m;
  974. __storage_type __dn = _VSTD::min(__n, static_cast<difference_type>(__clz_r));
  975. __m = (~__storage_type(0) << __first2.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn));
  976. if ((*__first2.__seg_ & __m) != (__b << __first2.__ctz_))
  977. return false;
  978. __first2.__seg_ += (__dn + __first2.__ctz_) / __bits_per_word;
  979. __first2.__ctz_ = static_cast<unsigned>((__dn + __first2.__ctz_) % __bits_per_word);
  980. __n -= __dn;
  981. if (__n > 0)
  982. {
  983. __m = ~__storage_type(0) >> (__bits_per_word - __n);
  984. if ((*__first2.__seg_ & __m) != (__b >> __dn))
  985. return false;
  986. }
  987. }
  988. }
  989. return true;
  990. }
  991. template <class _Cp, bool _IC1, bool _IC2>
  992. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
  993. __equal_aligned(__bit_iterator<_Cp, _IC1> __first1, __bit_iterator<_Cp, _IC1> __last1,
  994. __bit_iterator<_Cp, _IC2> __first2)
  995. {
  996. typedef __bit_iterator<_Cp, _IC1> _It;
  997. typedef typename _It::difference_type difference_type;
  998. typedef typename _It::__storage_type __storage_type;
  999. const int __bits_per_word = _It::__bits_per_word;
  1000. difference_type __n = __last1 - __first1;
  1001. if (__n > 0)
  1002. {
  1003. // do first word
  1004. if (__first1.__ctz_ != 0)
  1005. {
  1006. unsigned __clz = __bits_per_word - __first1.__ctz_;
  1007. difference_type __dn = _VSTD::min(static_cast<difference_type>(__clz), __n);
  1008. __n -= __dn;
  1009. __storage_type __m = (~__storage_type(0) << __first1.__ctz_) & (~__storage_type(0) >> (__clz - __dn));
  1010. if ((*__first2.__seg_ & __m) != (*__first1.__seg_ & __m))
  1011. return false;
  1012. ++__first2.__seg_;
  1013. ++__first1.__seg_;
  1014. // __first1.__ctz_ = 0;
  1015. // __first2.__ctz_ = 0;
  1016. }
  1017. // __first1.__ctz_ == 0;
  1018. // __first2.__ctz_ == 0;
  1019. // do middle words
  1020. for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first1.__seg_, ++__first2.__seg_)
  1021. if (*__first2.__seg_ != *__first1.__seg_)
  1022. return false;
  1023. // do last word
  1024. if (__n > 0)
  1025. {
  1026. __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
  1027. if ((*__first2.__seg_ & __m) != (*__first1.__seg_ & __m))
  1028. return false;
  1029. }
  1030. }
  1031. return true;
  1032. }
  1033. template <class _Cp, bool _IC1, bool _IC2>
  1034. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  1035. bool
  1036. equal(__bit_iterator<_Cp, _IC1> __first1, __bit_iterator<_Cp, _IC1> __last1, __bit_iterator<_Cp, _IC2> __first2)
  1037. {
  1038. if (__first1.__ctz_ == __first2.__ctz_)
  1039. return _VSTD::__equal_aligned(__first1, __last1, __first2);
  1040. return _VSTD::__equal_unaligned(__first1, __last1, __first2);
  1041. }
  1042. template <class _Cp, bool _IsConst,
  1043. class>
  1044. class __bit_iterator
  1045. {
  1046. public:
  1047. typedef typename _Cp::difference_type difference_type;
  1048. typedef bool value_type;
  1049. typedef __bit_iterator pointer;
  1050. #ifndef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL
  1051. typedef __conditional_t<_IsConst, __bit_const_reference<_Cp>, __bit_reference<_Cp> > reference;
  1052. #else
  1053. using reference = __conditional_t<_IsConst, bool, __bit_reference<_Cp> >;
  1054. #endif
  1055. typedef random_access_iterator_tag iterator_category;
  1056. private:
  1057. typedef typename _Cp::__storage_type __storage_type;
  1058. typedef __conditional_t<_IsConst, typename _Cp::__const_storage_pointer, typename _Cp::__storage_pointer>
  1059. __storage_pointer;
  1060. static const unsigned __bits_per_word = _Cp::__bits_per_word;
  1061. __storage_pointer __seg_;
  1062. unsigned __ctz_;
  1063. public:
  1064. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator() _NOEXCEPT
  1065. #if _LIBCPP_STD_VER > 11
  1066. : __seg_(nullptr), __ctz_(0)
  1067. #endif
  1068. {}
  1069. // When _IsConst=false, this is the copy constructor.
  1070. // It is non-trivial. Making it trivial would break ABI.
  1071. // When _IsConst=true, this is a converting constructor;
  1072. // the copy and move constructors are implicitly generated
  1073. // and trivial.
  1074. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  1075. __bit_iterator(const __bit_iterator<_Cp, false>& __it) _NOEXCEPT
  1076. : __seg_(__it.__seg_), __ctz_(__it.__ctz_) {}
  1077. // When _IsConst=false, we have a user-provided copy constructor,
  1078. // so we must also provide a copy assignment operator because
  1079. // the implicit generation of a defaulted one is deprecated.
  1080. // When _IsConst=true, the assignment operators are
  1081. // implicitly generated and trivial.
  1082. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  1083. __bit_iterator& operator=(const _If<_IsConst, struct __private_nat, __bit_iterator>& __it) {
  1084. __seg_ = __it.__seg_;
  1085. __ctz_ = __it.__ctz_;
  1086. return *this;
  1087. }
  1088. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator*() const _NOEXCEPT {
  1089. return __conditional_t<_IsConst, __bit_const_reference<_Cp>, __bit_reference<_Cp> >(
  1090. __seg_, __storage_type(1) << __ctz_);
  1091. }
  1092. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator& operator++()
  1093. {
  1094. if (__ctz_ != __bits_per_word-1)
  1095. ++__ctz_;
  1096. else
  1097. {
  1098. __ctz_ = 0;
  1099. ++__seg_;
  1100. }
  1101. return *this;
  1102. }
  1103. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator operator++(int)
  1104. {
  1105. __bit_iterator __tmp = *this;
  1106. ++(*this);
  1107. return __tmp;
  1108. }
  1109. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator& operator--()
  1110. {
  1111. if (__ctz_ != 0)
  1112. --__ctz_;
  1113. else
  1114. {
  1115. __ctz_ = __bits_per_word - 1;
  1116. --__seg_;
  1117. }
  1118. return *this;
  1119. }
  1120. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator operator--(int)
  1121. {
  1122. __bit_iterator __tmp = *this;
  1123. --(*this);
  1124. return __tmp;
  1125. }
  1126. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator& operator+=(difference_type __n)
  1127. {
  1128. if (__n >= 0)
  1129. __seg_ += (__n + __ctz_) / __bits_per_word;
  1130. else
  1131. __seg_ += static_cast<difference_type>(__n - __bits_per_word + __ctz_ + 1)
  1132. / static_cast<difference_type>(__bits_per_word);
  1133. __n &= (__bits_per_word - 1);
  1134. __ctz_ = static_cast<unsigned>((__n + __ctz_) % __bits_per_word);
  1135. return *this;
  1136. }
  1137. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator& operator-=(difference_type __n)
  1138. {
  1139. return *this += -__n;
  1140. }
  1141. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator operator+(difference_type __n) const
  1142. {
  1143. __bit_iterator __t(*this);
  1144. __t += __n;
  1145. return __t;
  1146. }
  1147. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator operator-(difference_type __n) const
  1148. {
  1149. __bit_iterator __t(*this);
  1150. __t -= __n;
  1151. return __t;
  1152. }
  1153. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  1154. friend __bit_iterator operator+(difference_type __n, const __bit_iterator& __it) {return __it + __n;}
  1155. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  1156. friend difference_type operator-(const __bit_iterator& __x, const __bit_iterator& __y)
  1157. {return (__x.__seg_ - __y.__seg_) * __bits_per_word + __x.__ctz_ - __y.__ctz_;}
  1158. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator[](difference_type __n) const {return *(*this + __n);}
  1159. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool operator==(const __bit_iterator& __x, const __bit_iterator& __y)
  1160. {return __x.__seg_ == __y.__seg_ && __x.__ctz_ == __y.__ctz_;}
  1161. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool operator!=(const __bit_iterator& __x, const __bit_iterator& __y)
  1162. {return !(__x == __y);}
  1163. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool operator<(const __bit_iterator& __x, const __bit_iterator& __y)
  1164. {return __x.__seg_ < __y.__seg_ || (__x.__seg_ == __y.__seg_ && __x.__ctz_ < __y.__ctz_);}
  1165. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool operator>(const __bit_iterator& __x, const __bit_iterator& __y)
  1166. {return __y < __x;}
  1167. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool operator<=(const __bit_iterator& __x, const __bit_iterator& __y)
  1168. {return !(__y < __x);}
  1169. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool operator>=(const __bit_iterator& __x, const __bit_iterator& __y)
  1170. {return !(__x < __y);}
  1171. private:
  1172. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  1173. explicit __bit_iterator(__storage_pointer __s, unsigned __ctz) _NOEXCEPT
  1174. : __seg_(__s), __ctz_(__ctz) {}
  1175. friend typename _Cp::__self;
  1176. friend class __bit_reference<_Cp>;
  1177. friend class __bit_const_reference<_Cp>;
  1178. friend class __bit_iterator<_Cp, true>;
  1179. template <class _Dp> friend struct __bit_array;
  1180. template <class _Dp>
  1181. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1182. friend void __fill_n_false(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n);
  1183. template <class _Dp>
  1184. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1185. friend void __fill_n_true(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n);
  1186. template <class _Dp, bool _IC>
  1187. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1188. friend __bit_iterator<_Dp, false> __copy_aligned(__bit_iterator<_Dp, _IC> __first,
  1189. __bit_iterator<_Dp, _IC> __last,
  1190. __bit_iterator<_Dp, false> __result);
  1191. template <class _Dp, bool _IC>
  1192. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1193. friend __bit_iterator<_Dp, false> __copy_unaligned(__bit_iterator<_Dp, _IC> __first,
  1194. __bit_iterator<_Dp, _IC> __last,
  1195. __bit_iterator<_Dp, false> __result);
  1196. template <class _Dp, bool _IC>
  1197. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1198. friend __bit_iterator<_Dp, false> copy(__bit_iterator<_Dp, _IC> __first,
  1199. __bit_iterator<_Dp, _IC> __last,
  1200. __bit_iterator<_Dp, false> __result);
  1201. template <class _Dp, bool _IC>
  1202. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1203. friend __bit_iterator<_Dp, false> __copy_backward_aligned(__bit_iterator<_Dp, _IC> __first,
  1204. __bit_iterator<_Dp, _IC> __last,
  1205. __bit_iterator<_Dp, false> __result);
  1206. template <class _Dp, bool _IC>
  1207. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1208. friend __bit_iterator<_Dp, false> __copy_backward_unaligned(__bit_iterator<_Dp, _IC> __first,
  1209. __bit_iterator<_Dp, _IC> __last,
  1210. __bit_iterator<_Dp, false> __result);
  1211. template <class _Dp, bool _IC>
  1212. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1213. friend __bit_iterator<_Dp, false> copy_backward(__bit_iterator<_Dp, _IC> __first,
  1214. __bit_iterator<_Dp, _IC> __last,
  1215. __bit_iterator<_Dp, false> __result);
  1216. template <class __C1, class __C2>friend __bit_iterator<__C2, false> __swap_ranges_aligned(__bit_iterator<__C1, false>,
  1217. __bit_iterator<__C1, false>,
  1218. __bit_iterator<__C2, false>);
  1219. template <class __C1, class __C2>friend __bit_iterator<__C2, false> __swap_ranges_unaligned(__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(__bit_iterator<__C1, false>,
  1223. __bit_iterator<__C1, false>,
  1224. __bit_iterator<__C2, false>);
  1225. template <class _Dp>
  1226. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1227. friend __bit_iterator<_Dp, false> rotate(__bit_iterator<_Dp, false>,
  1228. __bit_iterator<_Dp, false>,
  1229. __bit_iterator<_Dp, false>);
  1230. template <class _Dp, bool _IC1, bool _IC2>
  1231. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1232. friend bool __equal_aligned(__bit_iterator<_Dp, _IC1>,
  1233. __bit_iterator<_Dp, _IC1>,
  1234. __bit_iterator<_Dp, _IC2>);
  1235. template <class _Dp, bool _IC1, bool _IC2>
  1236. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1237. friend bool __equal_unaligned(__bit_iterator<_Dp, _IC1>,
  1238. __bit_iterator<_Dp, _IC1>,
  1239. __bit_iterator<_Dp, _IC2>);
  1240. template <class _Dp, bool _IC1, bool _IC2>
  1241. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1242. friend bool equal(__bit_iterator<_Dp, _IC1>,
  1243. __bit_iterator<_Dp, _IC1>,
  1244. __bit_iterator<_Dp, _IC2>);
  1245. template <class _Dp, bool _IC>
  1246. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1247. friend __bit_iterator<_Dp, _IC> __find_bool_true(__bit_iterator<_Dp, _IC>, typename _Dp::size_type);
  1248. template <class _Dp, bool _IC>
  1249. _LIBCPP_CONSTEXPR_SINCE_CXX20
  1250. friend __bit_iterator<_Dp, _IC> __find_bool_false(__bit_iterator<_Dp, _IC>, typename _Dp::size_type);
  1251. template <class _Dp, bool _IC> friend typename __bit_iterator<_Dp, _IC>::difference_type
  1252. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23
  1253. __count_bool_true(__bit_iterator<_Dp, _IC>, typename _Dp::size_type);
  1254. template <class _Dp, bool _IC> friend typename __bit_iterator<_Dp, _IC>::difference_type
  1255. __count_bool_false(__bit_iterator<_Dp, _IC>, typename _Dp::size_type);
  1256. };
  1257. _LIBCPP_END_NAMESPACE_STD
  1258. _LIBCPP_POP_MACROS
  1259. #endif // _LIBCPP___BIT_REFERENCE