__bit_reference 51 KB

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