stl_bvector.h 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  1. // vector<bool> specialization -*- C++ -*-
  2. // Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. *
  22. * Copyright (c) 1994
  23. * Hewlett-Packard Company
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Hewlett-Packard Company makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 1996-1999
  35. * Silicon Graphics Computer Systems, Inc.
  36. *
  37. * Permission to use, copy, modify, distribute and sell this software
  38. * and its documentation for any purpose is hereby granted without fee,
  39. * provided that the above copyright notice appear in all copies and
  40. * that both that copyright notice and this permission notice appear
  41. * in supporting documentation. Silicon Graphics makes no
  42. * representations about the suitability of this software for any
  43. * purpose. It is provided "as is" without express or implied warranty.
  44. */
  45. /** @file bits/stl_bvector.h
  46. * This is an internal header file, included by other library headers.
  47. * Do not attempt to use it directly. @headername{vector}
  48. */
  49. #ifndef _STL_BVECTOR_H
  50. #define _STL_BVECTOR_H 1
  51. #if __cplusplus >= 201103L
  52. #include <initializer_list>
  53. #include <bits/functional_hash.h>
  54. #endif
  55. namespace std _GLIBCXX_VISIBILITY(default)
  56. {
  57. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  58. typedef unsigned long _Bit_type;
  59. enum { _S_word_bit = int(__CHAR_BIT__ * sizeof(_Bit_type)) };
  60. __attribute__((__nonnull__))
  61. _GLIBCXX20_CONSTEXPR
  62. void
  63. __fill_bvector_n(_Bit_type*, size_t, bool) _GLIBCXX_NOEXCEPT;
  64. _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
  65. struct _Bit_reference
  66. {
  67. _Bit_type * _M_p;
  68. _Bit_type _M_mask;
  69. _GLIBCXX20_CONSTEXPR
  70. _Bit_reference(_Bit_type * __x, _Bit_type __y)
  71. : _M_p(__x), _M_mask(__y) { }
  72. _GLIBCXX20_CONSTEXPR
  73. _Bit_reference() _GLIBCXX_NOEXCEPT : _M_p(0), _M_mask(0) { }
  74. #if __cplusplus >= 201103L
  75. _Bit_reference(const _Bit_reference&) = default;
  76. #endif
  77. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  78. operator bool() const _GLIBCXX_NOEXCEPT
  79. { return !!(*_M_p & _M_mask); }
  80. _GLIBCXX20_CONSTEXPR
  81. _Bit_reference&
  82. operator=(bool __x) _GLIBCXX_NOEXCEPT
  83. {
  84. if (__x)
  85. *_M_p |= _M_mask;
  86. else
  87. *_M_p &= ~_M_mask;
  88. return *this;
  89. }
  90. _GLIBCXX20_CONSTEXPR
  91. _Bit_reference&
  92. operator=(const _Bit_reference& __x) _GLIBCXX_NOEXCEPT
  93. { return *this = bool(__x); }
  94. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  95. bool
  96. operator==(const _Bit_reference& __x) const
  97. { return bool(*this) == bool(__x); }
  98. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  99. bool
  100. operator<(const _Bit_reference& __x) const
  101. { return !bool(*this) && bool(__x); }
  102. _GLIBCXX20_CONSTEXPR
  103. void
  104. flip() _GLIBCXX_NOEXCEPT
  105. { *_M_p ^= _M_mask; }
  106. #if __cplusplus >= 201103L
  107. _GLIBCXX20_CONSTEXPR
  108. friend void
  109. swap(_Bit_reference __x, _Bit_reference __y) noexcept
  110. {
  111. bool __tmp = __x;
  112. __x = __y;
  113. __y = __tmp;
  114. }
  115. _GLIBCXX20_CONSTEXPR
  116. friend void
  117. swap(_Bit_reference __x, bool& __y) noexcept
  118. {
  119. bool __tmp = __x;
  120. __x = __y;
  121. __y = __tmp;
  122. }
  123. _GLIBCXX20_CONSTEXPR
  124. friend void
  125. swap(bool& __x, _Bit_reference __y) noexcept
  126. {
  127. bool __tmp = __x;
  128. __x = __y;
  129. __y = __tmp;
  130. }
  131. #endif
  132. };
  133. // Ignore warnings about std::iterator.
  134. #pragma GCC diagnostic push
  135. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  136. struct _Bit_iterator_base
  137. : public std::iterator<std::random_access_iterator_tag, bool>
  138. {
  139. _Bit_type * _M_p;
  140. unsigned int _M_offset;
  141. _GLIBCXX20_CONSTEXPR
  142. _Bit_iterator_base(_Bit_type * __x, unsigned int __y)
  143. : _M_p(__x), _M_offset(__y) { }
  144. _GLIBCXX20_CONSTEXPR
  145. void
  146. _M_bump_up()
  147. {
  148. if (_M_offset++ == int(_S_word_bit) - 1)
  149. {
  150. _M_offset = 0;
  151. ++_M_p;
  152. }
  153. }
  154. _GLIBCXX20_CONSTEXPR
  155. void
  156. _M_bump_down()
  157. {
  158. if (_M_offset-- == 0)
  159. {
  160. _M_offset = int(_S_word_bit) - 1;
  161. --_M_p;
  162. }
  163. }
  164. _GLIBCXX20_CONSTEXPR
  165. void
  166. _M_incr(ptrdiff_t __i)
  167. {
  168. difference_type __n = __i + _M_offset;
  169. _M_p += __n / int(_S_word_bit);
  170. __n = __n % int(_S_word_bit);
  171. if (__n < 0)
  172. {
  173. __n += int(_S_word_bit);
  174. --_M_p;
  175. }
  176. _M_offset = static_cast<unsigned int>(__n);
  177. }
  178. _GLIBCXX_NODISCARD
  179. friend _GLIBCXX20_CONSTEXPR bool
  180. operator==(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
  181. { return __x._M_p == __y._M_p && __x._M_offset == __y._M_offset; }
  182. #if __cpp_lib_three_way_comparison
  183. [[nodiscard]]
  184. friend constexpr strong_ordering
  185. operator<=>(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
  186. noexcept
  187. {
  188. if (const auto __cmp = __x._M_p <=> __y._M_p; __cmp != 0)
  189. return __cmp;
  190. return __x._M_offset <=> __y._M_offset;
  191. }
  192. #else
  193. _GLIBCXX_NODISCARD
  194. friend bool
  195. operator<(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
  196. {
  197. return __x._M_p < __y._M_p
  198. || (__x._M_p == __y._M_p && __x._M_offset < __y._M_offset);
  199. }
  200. _GLIBCXX_NODISCARD
  201. friend bool
  202. operator!=(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
  203. { return !(__x == __y); }
  204. _GLIBCXX_NODISCARD
  205. friend bool
  206. operator>(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
  207. { return __y < __x; }
  208. _GLIBCXX_NODISCARD
  209. friend bool
  210. operator<=(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
  211. { return !(__y < __x); }
  212. _GLIBCXX_NODISCARD
  213. friend bool
  214. operator>=(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
  215. { return !(__x < __y); }
  216. #endif // three-way comparison
  217. friend _GLIBCXX20_CONSTEXPR ptrdiff_t
  218. operator-(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
  219. {
  220. return (int(_S_word_bit) * (__x._M_p - __y._M_p)
  221. + __x._M_offset - __y._M_offset);
  222. }
  223. };
  224. #pragma GCC diagnostic pop
  225. struct _Bit_iterator : public _Bit_iterator_base
  226. {
  227. typedef _Bit_reference reference;
  228. #if __cplusplus > 201703L
  229. typedef void pointer;
  230. #else
  231. typedef _Bit_reference* pointer;
  232. #endif
  233. typedef _Bit_iterator iterator;
  234. _GLIBCXX20_CONSTEXPR
  235. _Bit_iterator() : _Bit_iterator_base(0, 0) { }
  236. _GLIBCXX20_CONSTEXPR
  237. _Bit_iterator(_Bit_type * __x, unsigned int __y)
  238. : _Bit_iterator_base(__x, __y) { }
  239. _GLIBCXX20_CONSTEXPR
  240. iterator
  241. _M_const_cast() const
  242. { return *this; }
  243. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  244. reference
  245. operator*() const
  246. { return reference(_M_p, 1UL << _M_offset); }
  247. _GLIBCXX20_CONSTEXPR
  248. iterator&
  249. operator++()
  250. {
  251. _M_bump_up();
  252. return *this;
  253. }
  254. _GLIBCXX20_CONSTEXPR
  255. iterator
  256. operator++(int)
  257. {
  258. iterator __tmp = *this;
  259. _M_bump_up();
  260. return __tmp;
  261. }
  262. _GLIBCXX20_CONSTEXPR
  263. iterator&
  264. operator--()
  265. {
  266. _M_bump_down();
  267. return *this;
  268. }
  269. _GLIBCXX20_CONSTEXPR
  270. iterator
  271. operator--(int)
  272. {
  273. iterator __tmp = *this;
  274. _M_bump_down();
  275. return __tmp;
  276. }
  277. _GLIBCXX20_CONSTEXPR
  278. iterator&
  279. operator+=(difference_type __i)
  280. {
  281. _M_incr(__i);
  282. return *this;
  283. }
  284. _GLIBCXX20_CONSTEXPR
  285. iterator&
  286. operator-=(difference_type __i)
  287. {
  288. *this += -__i;
  289. return *this;
  290. }
  291. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  292. reference
  293. operator[](difference_type __i) const
  294. { return *(*this + __i); }
  295. _GLIBCXX_NODISCARD
  296. friend _GLIBCXX20_CONSTEXPR iterator
  297. operator+(const iterator& __x, difference_type __n)
  298. {
  299. iterator __tmp = __x;
  300. __tmp += __n;
  301. return __tmp;
  302. }
  303. _GLIBCXX_NODISCARD
  304. friend _GLIBCXX20_CONSTEXPR iterator
  305. operator+(difference_type __n, const iterator& __x)
  306. { return __x + __n; }
  307. _GLIBCXX_NODISCARD
  308. friend _GLIBCXX20_CONSTEXPR iterator
  309. operator-(const iterator& __x, difference_type __n)
  310. {
  311. iterator __tmp = __x;
  312. __tmp -= __n;
  313. return __tmp;
  314. }
  315. };
  316. struct _Bit_const_iterator : public _Bit_iterator_base
  317. {
  318. typedef bool reference;
  319. typedef bool const_reference;
  320. #if __cplusplus > 201703L
  321. typedef void pointer;
  322. #else
  323. typedef const bool* pointer;
  324. #endif
  325. typedef _Bit_const_iterator const_iterator;
  326. _GLIBCXX20_CONSTEXPR
  327. _Bit_const_iterator() : _Bit_iterator_base(0, 0) { }
  328. _GLIBCXX20_CONSTEXPR
  329. _Bit_const_iterator(_Bit_type * __x, unsigned int __y)
  330. : _Bit_iterator_base(__x, __y) { }
  331. _GLIBCXX20_CONSTEXPR
  332. _Bit_const_iterator(const _Bit_iterator& __x)
  333. : _Bit_iterator_base(__x._M_p, __x._M_offset) { }
  334. _GLIBCXX20_CONSTEXPR
  335. _Bit_iterator
  336. _M_const_cast() const
  337. { return _Bit_iterator(_M_p, _M_offset); }
  338. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  339. const_reference
  340. operator*() const
  341. { return _Bit_reference(_M_p, 1UL << _M_offset); }
  342. _GLIBCXX20_CONSTEXPR
  343. const_iterator&
  344. operator++()
  345. {
  346. _M_bump_up();
  347. return *this;
  348. }
  349. _GLIBCXX20_CONSTEXPR
  350. const_iterator
  351. operator++(int)
  352. {
  353. const_iterator __tmp = *this;
  354. _M_bump_up();
  355. return __tmp;
  356. }
  357. _GLIBCXX20_CONSTEXPR
  358. const_iterator&
  359. operator--()
  360. {
  361. _M_bump_down();
  362. return *this;
  363. }
  364. _GLIBCXX20_CONSTEXPR
  365. const_iterator
  366. operator--(int)
  367. {
  368. const_iterator __tmp = *this;
  369. _M_bump_down();
  370. return __tmp;
  371. }
  372. _GLIBCXX20_CONSTEXPR
  373. const_iterator&
  374. operator+=(difference_type __i)
  375. {
  376. _M_incr(__i);
  377. return *this;
  378. }
  379. _GLIBCXX20_CONSTEXPR
  380. const_iterator&
  381. operator-=(difference_type __i)
  382. {
  383. *this += -__i;
  384. return *this;
  385. }
  386. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  387. const_reference
  388. operator[](difference_type __i) const
  389. { return *(*this + __i); }
  390. _GLIBCXX_NODISCARD
  391. friend _GLIBCXX20_CONSTEXPR const_iterator
  392. operator+(const const_iterator& __x, difference_type __n)
  393. {
  394. const_iterator __tmp = __x;
  395. __tmp += __n;
  396. return __tmp;
  397. }
  398. _GLIBCXX_NODISCARD
  399. friend _GLIBCXX20_CONSTEXPR const_iterator
  400. operator-(const const_iterator& __x, difference_type __n)
  401. {
  402. const_iterator __tmp = __x;
  403. __tmp -= __n;
  404. return __tmp;
  405. }
  406. _GLIBCXX_NODISCARD
  407. friend _GLIBCXX20_CONSTEXPR const_iterator
  408. operator+(difference_type __n, const const_iterator& __x)
  409. { return __x + __n; }
  410. };
  411. template<typename _Alloc>
  412. struct _Bvector_base
  413. {
  414. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  415. rebind<_Bit_type>::other _Bit_alloc_type;
  416. typedef typename __gnu_cxx::__alloc_traits<_Bit_alloc_type>
  417. _Bit_alloc_traits;
  418. typedef typename _Bit_alloc_traits::pointer _Bit_pointer;
  419. struct _Bvector_impl_data
  420. {
  421. #if !_GLIBCXX_INLINE_VERSION
  422. _Bit_iterator _M_start;
  423. #else
  424. // We don't need the offset field for the start, it's always zero.
  425. struct {
  426. _Bit_type* _M_p;
  427. // Allow assignment from iterators (assume offset is zero):
  428. _GLIBCXX20_CONSTEXPR
  429. void operator=(_Bit_iterator __it) { _M_p = __it._M_p; }
  430. } _M_start;
  431. #endif
  432. _Bit_iterator _M_finish;
  433. _Bit_pointer _M_end_of_storage;
  434. _GLIBCXX20_CONSTEXPR
  435. _Bvector_impl_data() _GLIBCXX_NOEXCEPT
  436. : _M_start(), _M_finish(), _M_end_of_storage()
  437. { }
  438. #if __cplusplus >= 201103L
  439. _Bvector_impl_data(const _Bvector_impl_data&) = default;
  440. _Bvector_impl_data&
  441. operator=(const _Bvector_impl_data&) = default;
  442. _GLIBCXX20_CONSTEXPR
  443. _Bvector_impl_data(_Bvector_impl_data&& __x) noexcept
  444. : _Bvector_impl_data(__x)
  445. { __x._M_reset(); }
  446. _GLIBCXX20_CONSTEXPR
  447. void
  448. _M_move_data(_Bvector_impl_data&& __x) noexcept
  449. {
  450. *this = __x;
  451. __x._M_reset();
  452. }
  453. #endif
  454. _GLIBCXX20_CONSTEXPR
  455. void
  456. _M_reset() _GLIBCXX_NOEXCEPT
  457. { *this = _Bvector_impl_data(); }
  458. _GLIBCXX20_CONSTEXPR
  459. void
  460. _M_swap_data(_Bvector_impl_data& __x) _GLIBCXX_NOEXCEPT
  461. {
  462. // Do not use std::swap(_M_start, __x._M_start), etc as it loses
  463. // information used by TBAA.
  464. std::swap(*this, __x);
  465. }
  466. };
  467. struct _Bvector_impl
  468. : public _Bit_alloc_type, public _Bvector_impl_data
  469. {
  470. _GLIBCXX20_CONSTEXPR
  471. _Bvector_impl() _GLIBCXX_NOEXCEPT_IF(
  472. is_nothrow_default_constructible<_Bit_alloc_type>::value)
  473. : _Bit_alloc_type()
  474. { }
  475. _GLIBCXX20_CONSTEXPR
  476. _Bvector_impl(const _Bit_alloc_type& __a) _GLIBCXX_NOEXCEPT
  477. : _Bit_alloc_type(__a)
  478. { }
  479. #if __cplusplus >= 201103L
  480. // Not defaulted, to enforce noexcept(true) even when
  481. // !is_nothrow_move_constructible<_Bit_alloc_type>.
  482. _GLIBCXX20_CONSTEXPR
  483. _Bvector_impl(_Bvector_impl&& __x) noexcept
  484. : _Bit_alloc_type(std::move(__x)), _Bvector_impl_data(std::move(__x))
  485. { }
  486. _GLIBCXX20_CONSTEXPR
  487. _Bvector_impl(_Bit_alloc_type&& __a, _Bvector_impl&& __x) noexcept
  488. : _Bit_alloc_type(std::move(__a)), _Bvector_impl_data(std::move(__x))
  489. { }
  490. #endif
  491. _GLIBCXX20_CONSTEXPR
  492. _Bit_type*
  493. _M_end_addr() const _GLIBCXX_NOEXCEPT
  494. {
  495. if (this->_M_end_of_storage)
  496. return std::__addressof(this->_M_end_of_storage[-1]) + 1;
  497. return 0;
  498. }
  499. };
  500. public:
  501. typedef _Alloc allocator_type;
  502. _GLIBCXX20_CONSTEXPR
  503. _Bit_alloc_type&
  504. _M_get_Bit_allocator() _GLIBCXX_NOEXCEPT
  505. { return this->_M_impl; }
  506. _GLIBCXX20_CONSTEXPR
  507. const _Bit_alloc_type&
  508. _M_get_Bit_allocator() const _GLIBCXX_NOEXCEPT
  509. { return this->_M_impl; }
  510. _GLIBCXX20_CONSTEXPR
  511. allocator_type
  512. get_allocator() const _GLIBCXX_NOEXCEPT
  513. { return allocator_type(_M_get_Bit_allocator()); }
  514. #if __cplusplus >= 201103L
  515. _Bvector_base() = default;
  516. #else
  517. _Bvector_base() { }
  518. #endif
  519. _GLIBCXX20_CONSTEXPR
  520. _Bvector_base(const allocator_type& __a)
  521. : _M_impl(__a) { }
  522. #if __cplusplus >= 201103L
  523. _Bvector_base(_Bvector_base&&) = default;
  524. _GLIBCXX20_CONSTEXPR
  525. _Bvector_base(_Bvector_base&& __x, const allocator_type& __a) noexcept
  526. : _M_impl(_Bit_alloc_type(__a), std::move(__x._M_impl))
  527. { }
  528. #endif
  529. _GLIBCXX20_CONSTEXPR
  530. ~_Bvector_base()
  531. { this->_M_deallocate(); }
  532. protected:
  533. _Bvector_impl _M_impl;
  534. _GLIBCXX20_CONSTEXPR
  535. _Bit_pointer
  536. _M_allocate(size_t __n)
  537. {
  538. _Bit_pointer __p = _Bit_alloc_traits::allocate(_M_impl, _S_nword(__n));
  539. #if __cpp_lib_is_constant_evaluated
  540. if (std::is_constant_evaluated())
  541. {
  542. __n = _S_nword(__n);
  543. for (size_t __i = 0; __i < __n; ++__i)
  544. __p[__i] = 0ul;
  545. }
  546. #endif
  547. return __p;
  548. }
  549. _GLIBCXX20_CONSTEXPR
  550. void
  551. _M_deallocate()
  552. {
  553. if (_M_impl._M_start._M_p)
  554. {
  555. const size_t __n = _M_impl._M_end_addr() - _M_impl._M_start._M_p;
  556. _Bit_alloc_traits::deallocate(_M_impl,
  557. _M_impl._M_end_of_storage - __n,
  558. __n);
  559. _M_impl._M_reset();
  560. }
  561. }
  562. #if __cplusplus >= 201103L
  563. _GLIBCXX20_CONSTEXPR
  564. void
  565. _M_move_data(_Bvector_base&& __x) noexcept
  566. { _M_impl._M_move_data(std::move(__x._M_impl)); }
  567. #endif
  568. _GLIBCXX_CONSTEXPR
  569. static size_t
  570. _S_nword(size_t __n)
  571. { return (__n + int(_S_word_bit) - 1) / int(_S_word_bit); }
  572. };
  573. /**
  574. * @brief A specialization of vector for booleans which offers fixed time
  575. * access to individual elements in any order.
  576. *
  577. * @ingroup sequences
  578. *
  579. * @tparam _Alloc Allocator type.
  580. *
  581. * Note that vector<bool> does not actually meet the requirements for being
  582. * a container. This is because the reference and pointer types are not
  583. * really references and pointers to bool. See DR96 for details. @see
  584. * vector for function documentation.
  585. *
  586. * In some terminology a %vector can be described as a dynamic
  587. * C-style array, it offers fast and efficient access to individual
  588. * elements in any order and saves the user from worrying about
  589. * memory and size allocation. Subscripting ( @c [] ) access is
  590. * also provided as with C-style arrays.
  591. */
  592. template<typename _Alloc>
  593. class vector<bool, _Alloc> : protected _Bvector_base<_Alloc>
  594. {
  595. typedef _Bvector_base<_Alloc> _Base;
  596. typedef typename _Base::_Bit_pointer _Bit_pointer;
  597. typedef typename _Base::_Bit_alloc_traits _Bit_alloc_traits;
  598. #if __cplusplus >= 201103L
  599. friend struct std::hash<vector>;
  600. #endif
  601. public:
  602. typedef bool value_type;
  603. typedef size_t size_type;
  604. typedef ptrdiff_t difference_type;
  605. typedef _Bit_reference reference;
  606. typedef bool const_reference;
  607. typedef _Bit_reference* pointer;
  608. typedef const bool* const_pointer;
  609. typedef _Bit_iterator iterator;
  610. typedef _Bit_const_iterator const_iterator;
  611. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  612. typedef std::reverse_iterator<iterator> reverse_iterator;
  613. typedef _Alloc allocator_type;
  614. _GLIBCXX20_CONSTEXPR
  615. allocator_type
  616. get_allocator() const
  617. { return _Base::get_allocator(); }
  618. protected:
  619. using _Base::_M_allocate;
  620. using _Base::_M_deallocate;
  621. using _Base::_S_nword;
  622. using _Base::_M_get_Bit_allocator;
  623. public:
  624. #if __cplusplus >= 201103L
  625. vector() = default;
  626. #else
  627. vector() { }
  628. #endif
  629. _GLIBCXX20_CONSTEXPR
  630. explicit
  631. vector(const allocator_type& __a)
  632. : _Base(__a) { }
  633. #if __cplusplus >= 201103L
  634. _GLIBCXX20_CONSTEXPR
  635. explicit
  636. vector(size_type __n, const allocator_type& __a = allocator_type())
  637. : vector(__n, false, __a)
  638. { }
  639. _GLIBCXX20_CONSTEXPR
  640. vector(size_type __n, const bool& __value,
  641. const allocator_type& __a = allocator_type())
  642. #else
  643. explicit
  644. vector(size_type __n, const bool& __value = bool(),
  645. const allocator_type& __a = allocator_type())
  646. #endif
  647. : _Base(__a)
  648. {
  649. _M_initialize(__n);
  650. _M_initialize_value(__value);
  651. }
  652. _GLIBCXX20_CONSTEXPR
  653. vector(const vector& __x)
  654. : _Base(_Bit_alloc_traits::_S_select_on_copy(__x._M_get_Bit_allocator()))
  655. {
  656. _M_initialize(__x.size());
  657. _M_copy_aligned(__x.begin(), __x.end(), begin());
  658. }
  659. #if __cplusplus >= 201103L
  660. vector(vector&&) = default;
  661. private:
  662. _GLIBCXX20_CONSTEXPR
  663. vector(vector&& __x, const allocator_type& __a, true_type) noexcept
  664. : _Base(std::move(__x), __a)
  665. { }
  666. _GLIBCXX20_CONSTEXPR
  667. vector(vector&& __x, const allocator_type& __a, false_type)
  668. : _Base(__a)
  669. {
  670. if (__x.get_allocator() == __a)
  671. this->_M_move_data(std::move(__x));
  672. else
  673. {
  674. _M_initialize(__x.size());
  675. _M_copy_aligned(__x.begin(), __x.end(), begin());
  676. __x.clear();
  677. }
  678. }
  679. public:
  680. _GLIBCXX20_CONSTEXPR
  681. vector(vector&& __x, const __type_identity_t<allocator_type>& __a)
  682. noexcept(_Bit_alloc_traits::_S_always_equal())
  683. : vector(std::move(__x), __a,
  684. typename _Bit_alloc_traits::is_always_equal{})
  685. { }
  686. _GLIBCXX20_CONSTEXPR
  687. vector(const vector& __x, const __type_identity_t<allocator_type>& __a)
  688. : _Base(__a)
  689. {
  690. _M_initialize(__x.size());
  691. _M_copy_aligned(__x.begin(), __x.end(), begin());
  692. }
  693. _GLIBCXX20_CONSTEXPR
  694. vector(initializer_list<bool> __l,
  695. const allocator_type& __a = allocator_type())
  696. : _Base(__a)
  697. {
  698. _M_initialize_range(__l.begin(), __l.end(),
  699. random_access_iterator_tag());
  700. }
  701. #endif
  702. #if __cplusplus >= 201103L
  703. template<typename _InputIterator,
  704. typename = std::_RequireInputIter<_InputIterator>>
  705. _GLIBCXX20_CONSTEXPR
  706. vector(_InputIterator __first, _InputIterator __last,
  707. const allocator_type& __a = allocator_type())
  708. : _Base(__a)
  709. {
  710. _M_initialize_range(__first, __last,
  711. std::__iterator_category(__first));
  712. }
  713. #else
  714. template<typename _InputIterator>
  715. vector(_InputIterator __first, _InputIterator __last,
  716. const allocator_type& __a = allocator_type())
  717. : _Base(__a)
  718. {
  719. // Check whether it's an integral type. If so, it's not an iterator.
  720. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  721. _M_initialize_dispatch(__first, __last, _Integral());
  722. }
  723. #endif
  724. _GLIBCXX20_CONSTEXPR
  725. ~vector() _GLIBCXX_NOEXCEPT { }
  726. _GLIBCXX20_CONSTEXPR
  727. vector&
  728. operator=(const vector& __x)
  729. {
  730. if (&__x == this)
  731. return *this;
  732. #if __cplusplus >= 201103L
  733. if (_Bit_alloc_traits::_S_propagate_on_copy_assign())
  734. {
  735. if (this->_M_get_Bit_allocator() != __x._M_get_Bit_allocator())
  736. {
  737. this->_M_deallocate();
  738. std::__alloc_on_copy(_M_get_Bit_allocator(),
  739. __x._M_get_Bit_allocator());
  740. _M_initialize(__x.size());
  741. }
  742. else
  743. std::__alloc_on_copy(_M_get_Bit_allocator(),
  744. __x._M_get_Bit_allocator());
  745. }
  746. #endif
  747. if (__x.size() > capacity())
  748. {
  749. this->_M_deallocate();
  750. _M_initialize(__x.size());
  751. }
  752. this->_M_impl._M_finish = _M_copy_aligned(__x.begin(), __x.end(),
  753. begin());
  754. return *this;
  755. }
  756. #if __cplusplus >= 201103L
  757. _GLIBCXX20_CONSTEXPR
  758. vector&
  759. operator=(vector&& __x) noexcept(_Bit_alloc_traits::_S_nothrow_move())
  760. {
  761. if (_Bit_alloc_traits::_S_propagate_on_move_assign()
  762. || this->_M_get_Bit_allocator() == __x._M_get_Bit_allocator())
  763. {
  764. this->_M_deallocate();
  765. this->_M_move_data(std::move(__x));
  766. std::__alloc_on_move(_M_get_Bit_allocator(),
  767. __x._M_get_Bit_allocator());
  768. }
  769. else
  770. {
  771. if (__x.size() > capacity())
  772. {
  773. this->_M_deallocate();
  774. _M_initialize(__x.size());
  775. }
  776. this->_M_impl._M_finish = _M_copy_aligned(__x.begin(), __x.end(),
  777. begin());
  778. __x.clear();
  779. }
  780. return *this;
  781. }
  782. _GLIBCXX20_CONSTEXPR
  783. vector&
  784. operator=(initializer_list<bool> __l)
  785. {
  786. this->assign(__l.begin(), __l.end());
  787. return *this;
  788. }
  789. #endif
  790. // assign(), a generalized assignment member function. Two
  791. // versions: one that takes a count, and one that takes a range.
  792. // The range version is a member template, so we dispatch on whether
  793. // or not the type is an integer.
  794. _GLIBCXX20_CONSTEXPR
  795. void
  796. assign(size_type __n, const bool& __x)
  797. { _M_fill_assign(__n, __x); }
  798. #if __cplusplus >= 201103L
  799. template<typename _InputIterator,
  800. typename = std::_RequireInputIter<_InputIterator>>
  801. _GLIBCXX20_CONSTEXPR
  802. void
  803. assign(_InputIterator __first, _InputIterator __last)
  804. { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
  805. #else
  806. template<typename _InputIterator>
  807. void
  808. assign(_InputIterator __first, _InputIterator __last)
  809. {
  810. // Check whether it's an integral type. If so, it's not an iterator.
  811. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  812. _M_assign_dispatch(__first, __last, _Integral());
  813. }
  814. #endif
  815. #if __cplusplus >= 201103L
  816. _GLIBCXX20_CONSTEXPR
  817. void
  818. assign(initializer_list<bool> __l)
  819. { _M_assign_aux(__l.begin(), __l.end(), random_access_iterator_tag()); }
  820. #endif
  821. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  822. iterator
  823. begin() _GLIBCXX_NOEXCEPT
  824. { return iterator(this->_M_impl._M_start._M_p, 0); }
  825. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  826. const_iterator
  827. begin() const _GLIBCXX_NOEXCEPT
  828. { return const_iterator(this->_M_impl._M_start._M_p, 0); }
  829. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  830. iterator
  831. end() _GLIBCXX_NOEXCEPT
  832. { return this->_M_impl._M_finish; }
  833. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  834. const_iterator
  835. end() const _GLIBCXX_NOEXCEPT
  836. { return this->_M_impl._M_finish; }
  837. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  838. reverse_iterator
  839. rbegin() _GLIBCXX_NOEXCEPT
  840. { return reverse_iterator(end()); }
  841. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  842. const_reverse_iterator
  843. rbegin() const _GLIBCXX_NOEXCEPT
  844. { return const_reverse_iterator(end()); }
  845. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  846. reverse_iterator
  847. rend() _GLIBCXX_NOEXCEPT
  848. { return reverse_iterator(begin()); }
  849. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  850. const_reverse_iterator
  851. rend() const _GLIBCXX_NOEXCEPT
  852. { return const_reverse_iterator(begin()); }
  853. #if __cplusplus >= 201103L
  854. [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
  855. const_iterator
  856. cbegin() const noexcept
  857. { return const_iterator(this->_M_impl._M_start._M_p, 0); }
  858. [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
  859. const_iterator
  860. cend() const noexcept
  861. { return this->_M_impl._M_finish; }
  862. [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
  863. const_reverse_iterator
  864. crbegin() const noexcept
  865. { return const_reverse_iterator(end()); }
  866. [[__nodiscard__]] _GLIBCXX20_CONSTEXPR
  867. const_reverse_iterator
  868. crend() const noexcept
  869. { return const_reverse_iterator(begin()); }
  870. #endif
  871. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  872. size_type
  873. size() const _GLIBCXX_NOEXCEPT
  874. { return size_type(end() - begin()); }
  875. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  876. size_type
  877. max_size() const _GLIBCXX_NOEXCEPT
  878. {
  879. const size_type __isize =
  880. __gnu_cxx::__numeric_traits<difference_type>::__max
  881. - int(_S_word_bit) + 1;
  882. const size_type __asize
  883. = _Bit_alloc_traits::max_size(_M_get_Bit_allocator());
  884. return (__asize <= __isize / int(_S_word_bit)
  885. ? __asize * int(_S_word_bit) : __isize);
  886. }
  887. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  888. size_type
  889. capacity() const _GLIBCXX_NOEXCEPT
  890. { return size_type(const_iterator(this->_M_impl._M_end_addr(), 0)
  891. - begin()); }
  892. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  893. bool
  894. empty() const _GLIBCXX_NOEXCEPT
  895. { return begin() == end(); }
  896. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  897. reference
  898. operator[](size_type __n)
  899. { return begin()[__n]; }
  900. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  901. const_reference
  902. operator[](size_type __n) const
  903. { return begin()[__n]; }
  904. protected:
  905. _GLIBCXX20_CONSTEXPR
  906. void
  907. _M_range_check(size_type __n) const
  908. {
  909. if (__n >= this->size())
  910. __throw_out_of_range_fmt(__N("vector<bool>::_M_range_check: __n "
  911. "(which is %zu) >= this->size() "
  912. "(which is %zu)"),
  913. __n, this->size());
  914. }
  915. public:
  916. _GLIBCXX20_CONSTEXPR
  917. reference
  918. at(size_type __n)
  919. {
  920. _M_range_check(__n);
  921. return (*this)[__n];
  922. }
  923. _GLIBCXX20_CONSTEXPR
  924. const_reference
  925. at(size_type __n) const
  926. {
  927. _M_range_check(__n);
  928. return (*this)[__n];
  929. }
  930. _GLIBCXX20_CONSTEXPR
  931. void
  932. reserve(size_type __n)
  933. {
  934. if (__n > max_size())
  935. __throw_length_error(__N("vector::reserve"));
  936. if (capacity() < __n)
  937. _M_reallocate(__n);
  938. }
  939. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  940. reference
  941. front()
  942. { return *begin(); }
  943. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  944. const_reference
  945. front() const
  946. { return *begin(); }
  947. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  948. reference
  949. back()
  950. { return *(end() - 1); }
  951. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  952. const_reference
  953. back() const
  954. { return *(end() - 1); }
  955. _GLIBCXX20_CONSTEXPR
  956. void
  957. push_back(bool __x)
  958. {
  959. if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_addr())
  960. *this->_M_impl._M_finish++ = __x;
  961. else
  962. _M_insert_aux(end(), __x);
  963. }
  964. _GLIBCXX20_CONSTEXPR
  965. void
  966. swap(vector& __x) _GLIBCXX_NOEXCEPT
  967. {
  968. #if __cplusplus >= 201103L
  969. __glibcxx_assert(_Bit_alloc_traits::propagate_on_container_swap::value
  970. || _M_get_Bit_allocator() == __x._M_get_Bit_allocator());
  971. #endif
  972. this->_M_impl._M_swap_data(__x._M_impl);
  973. _Bit_alloc_traits::_S_on_swap(_M_get_Bit_allocator(),
  974. __x._M_get_Bit_allocator());
  975. }
  976. // [23.2.5]/1, third-to-last entry in synopsis listing
  977. _GLIBCXX20_CONSTEXPR
  978. static void
  979. swap(reference __x, reference __y) _GLIBCXX_NOEXCEPT
  980. {
  981. bool __tmp = __x;
  982. __x = __y;
  983. __y = __tmp;
  984. }
  985. _GLIBCXX20_CONSTEXPR
  986. iterator
  987. #if __cplusplus >= 201103L
  988. insert(const_iterator __position, const bool& __x)
  989. #else
  990. insert(iterator __position, const bool& __x)
  991. #endif
  992. {
  993. const difference_type __n = __position - begin();
  994. if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_addr()
  995. && __position == end())
  996. *this->_M_impl._M_finish++ = __x;
  997. else
  998. _M_insert_aux(__position._M_const_cast(), __x);
  999. return begin() + __n;
  1000. }
  1001. #if _GLIBCXX_USE_DEPRECATED
  1002. _GLIBCXX_DEPRECATED_SUGGEST("insert(position, false)")
  1003. iterator
  1004. insert(const_iterator __position)
  1005. { return this->insert(__position._M_const_cast(), false); }
  1006. #endif
  1007. #if __cplusplus >= 201103L
  1008. template<typename _InputIterator,
  1009. typename = std::_RequireInputIter<_InputIterator>>
  1010. _GLIBCXX20_CONSTEXPR
  1011. iterator
  1012. insert(const_iterator __position,
  1013. _InputIterator __first, _InputIterator __last)
  1014. {
  1015. difference_type __offset = __position - cbegin();
  1016. _M_insert_range(__position._M_const_cast(),
  1017. __first, __last,
  1018. std::__iterator_category(__first));
  1019. return begin() + __offset;
  1020. }
  1021. #else
  1022. template<typename _InputIterator>
  1023. void
  1024. insert(iterator __position,
  1025. _InputIterator __first, _InputIterator __last)
  1026. {
  1027. // Check whether it's an integral type. If so, it's not an iterator.
  1028. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  1029. _M_insert_dispatch(__position, __first, __last, _Integral());
  1030. }
  1031. #endif
  1032. #if __cplusplus >= 201103L
  1033. _GLIBCXX20_CONSTEXPR
  1034. iterator
  1035. insert(const_iterator __position, size_type __n, const bool& __x)
  1036. {
  1037. difference_type __offset = __position - cbegin();
  1038. _M_fill_insert(__position._M_const_cast(), __n, __x);
  1039. return begin() + __offset;
  1040. }
  1041. #else
  1042. void
  1043. insert(iterator __position, size_type __n, const bool& __x)
  1044. { _M_fill_insert(__position, __n, __x); }
  1045. #endif
  1046. #if __cplusplus >= 201103L
  1047. _GLIBCXX20_CONSTEXPR
  1048. iterator
  1049. insert(const_iterator __p, initializer_list<bool> __l)
  1050. { return this->insert(__p, __l.begin(), __l.end()); }
  1051. #endif
  1052. _GLIBCXX20_CONSTEXPR
  1053. void
  1054. pop_back()
  1055. { --this->_M_impl._M_finish; }
  1056. _GLIBCXX20_CONSTEXPR
  1057. iterator
  1058. #if __cplusplus >= 201103L
  1059. erase(const_iterator __position)
  1060. #else
  1061. erase(iterator __position)
  1062. #endif
  1063. { return _M_erase(__position._M_const_cast()); }
  1064. _GLIBCXX20_CONSTEXPR
  1065. iterator
  1066. #if __cplusplus >= 201103L
  1067. erase(const_iterator __first, const_iterator __last)
  1068. #else
  1069. erase(iterator __first, iterator __last)
  1070. #endif
  1071. { return _M_erase(__first._M_const_cast(), __last._M_const_cast()); }
  1072. _GLIBCXX20_CONSTEXPR
  1073. void
  1074. resize(size_type __new_size, bool __x = bool())
  1075. {
  1076. if (__new_size < size())
  1077. _M_erase_at_end(begin() + difference_type(__new_size));
  1078. else
  1079. insert(end(), __new_size - size(), __x);
  1080. }
  1081. #if __cplusplus >= 201103L
  1082. _GLIBCXX20_CONSTEXPR
  1083. void
  1084. shrink_to_fit()
  1085. { _M_shrink_to_fit(); }
  1086. #endif
  1087. _GLIBCXX20_CONSTEXPR
  1088. void
  1089. flip() _GLIBCXX_NOEXCEPT
  1090. {
  1091. _Bit_type * const __end = this->_M_impl._M_end_addr();
  1092. for (_Bit_type * __p = this->_M_impl._M_start._M_p; __p != __end; ++__p)
  1093. *__p = ~*__p;
  1094. }
  1095. _GLIBCXX20_CONSTEXPR
  1096. void
  1097. clear() _GLIBCXX_NOEXCEPT
  1098. { _M_erase_at_end(begin()); }
  1099. #if __cplusplus >= 201103L
  1100. template<typename... _Args>
  1101. #if __cplusplus > 201402L
  1102. _GLIBCXX20_CONSTEXPR
  1103. reference
  1104. #else
  1105. void
  1106. #endif
  1107. emplace_back(_Args&&... __args)
  1108. {
  1109. push_back(bool(__args...));
  1110. #if __cplusplus > 201402L
  1111. return back();
  1112. #endif
  1113. }
  1114. template<typename... _Args>
  1115. _GLIBCXX20_CONSTEXPR
  1116. iterator
  1117. emplace(const_iterator __pos, _Args&&... __args)
  1118. { return insert(__pos, bool(__args...)); }
  1119. #endif
  1120. protected:
  1121. // Precondition: __first._M_offset == 0 && __result._M_offset == 0.
  1122. _GLIBCXX20_CONSTEXPR
  1123. iterator
  1124. _M_copy_aligned(const_iterator __first, const_iterator __last,
  1125. iterator __result)
  1126. {
  1127. _Bit_type* __q = std::copy(__first._M_p, __last._M_p, __result._M_p);
  1128. return std::copy(const_iterator(__last._M_p, 0), __last,
  1129. iterator(__q, 0));
  1130. }
  1131. _GLIBCXX20_CONSTEXPR
  1132. void
  1133. _M_initialize(size_type __n)
  1134. {
  1135. if (__n)
  1136. {
  1137. _Bit_pointer __q = this->_M_allocate(__n);
  1138. this->_M_impl._M_end_of_storage = __q + _S_nword(__n);
  1139. iterator __start = iterator(std::__addressof(*__q), 0);
  1140. this->_M_impl._M_start = __start;
  1141. this->_M_impl._M_finish = __start + difference_type(__n);
  1142. }
  1143. }
  1144. _GLIBCXX20_CONSTEXPR
  1145. void
  1146. _M_initialize_value(bool __x) _GLIBCXX_NOEXCEPT
  1147. {
  1148. if (_Bit_type* __p = this->_M_impl._M_start._M_p)
  1149. __fill_bvector_n(__p, this->_M_impl._M_end_addr() - __p, __x);
  1150. }
  1151. _GLIBCXX20_CONSTEXPR
  1152. void
  1153. _M_reallocate(size_type __n);
  1154. #if __cplusplus >= 201103L
  1155. _GLIBCXX20_CONSTEXPR
  1156. bool
  1157. _M_shrink_to_fit();
  1158. #endif
  1159. #if __cplusplus < 201103L
  1160. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1161. // 438. Ambiguity in the "do the right thing" clause
  1162. template<typename _Integer>
  1163. void
  1164. _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
  1165. {
  1166. _M_initialize(static_cast<size_type>(__n));
  1167. _M_initialize_value(__x);
  1168. }
  1169. template<typename _InputIterator>
  1170. void
  1171. _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
  1172. __false_type)
  1173. { _M_initialize_range(__first, __last,
  1174. std::__iterator_category(__first)); }
  1175. #endif
  1176. template<typename _InputIterator>
  1177. _GLIBCXX20_CONSTEXPR
  1178. void
  1179. _M_initialize_range(_InputIterator __first, _InputIterator __last,
  1180. std::input_iterator_tag)
  1181. {
  1182. for (; __first != __last; ++__first)
  1183. push_back(*__first);
  1184. }
  1185. template<typename _ForwardIterator>
  1186. _GLIBCXX20_CONSTEXPR
  1187. void
  1188. _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,
  1189. std::forward_iterator_tag)
  1190. {
  1191. const size_type __n = std::distance(__first, __last);
  1192. _M_initialize(__n);
  1193. std::copy(__first, __last, begin());
  1194. }
  1195. #if __cplusplus < 201103L
  1196. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1197. // 438. Ambiguity in the "do the right thing" clause
  1198. template<typename _Integer>
  1199. void
  1200. _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
  1201. { _M_fill_assign(__n, __val); }
  1202. template<class _InputIterator>
  1203. void
  1204. _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
  1205. __false_type)
  1206. { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
  1207. #endif
  1208. _GLIBCXX20_CONSTEXPR
  1209. void
  1210. _M_fill_assign(size_t __n, bool __x)
  1211. {
  1212. if (__n > size())
  1213. {
  1214. _M_initialize_value(__x);
  1215. insert(end(), __n - size(), __x);
  1216. }
  1217. else
  1218. {
  1219. _M_erase_at_end(begin() + __n);
  1220. _M_initialize_value(__x);
  1221. }
  1222. }
  1223. template<typename _InputIterator>
  1224. _GLIBCXX20_CONSTEXPR
  1225. void
  1226. _M_assign_aux(_InputIterator __first, _InputIterator __last,
  1227. std::input_iterator_tag)
  1228. {
  1229. iterator __cur = begin();
  1230. for (; __first != __last && __cur != end(); ++__cur, (void)++__first)
  1231. *__cur = *__first;
  1232. if (__first == __last)
  1233. _M_erase_at_end(__cur);
  1234. else
  1235. insert(end(), __first, __last);
  1236. }
  1237. template<typename _ForwardIterator>
  1238. _GLIBCXX20_CONSTEXPR
  1239. void
  1240. _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
  1241. std::forward_iterator_tag)
  1242. {
  1243. const size_type __len = std::distance(__first, __last);
  1244. if (__len < size())
  1245. _M_erase_at_end(std::copy(__first, __last, begin()));
  1246. else
  1247. {
  1248. _ForwardIterator __mid = __first;
  1249. std::advance(__mid, size());
  1250. std::copy(__first, __mid, begin());
  1251. insert(end(), __mid, __last);
  1252. }
  1253. }
  1254. #if __cplusplus < 201103L
  1255. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1256. // 438. Ambiguity in the "do the right thing" clause
  1257. template<typename _Integer>
  1258. void
  1259. _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
  1260. __true_type)
  1261. { _M_fill_insert(__pos, __n, __x); }
  1262. template<typename _InputIterator>
  1263. void
  1264. _M_insert_dispatch(iterator __pos,
  1265. _InputIterator __first, _InputIterator __last,
  1266. __false_type)
  1267. { _M_insert_range(__pos, __first, __last,
  1268. std::__iterator_category(__first)); }
  1269. #endif
  1270. _GLIBCXX20_CONSTEXPR
  1271. void
  1272. _M_fill_insert(iterator __position, size_type __n, bool __x);
  1273. template<typename _InputIterator>
  1274. _GLIBCXX20_CONSTEXPR
  1275. void
  1276. _M_insert_range(iterator __pos, _InputIterator __first,
  1277. _InputIterator __last, std::input_iterator_tag)
  1278. {
  1279. for (; __first != __last; ++__first)
  1280. {
  1281. __pos = insert(__pos, *__first);
  1282. ++__pos;
  1283. }
  1284. }
  1285. template<typename _ForwardIterator>
  1286. _GLIBCXX20_CONSTEXPR
  1287. void
  1288. _M_insert_range(iterator __position, _ForwardIterator __first,
  1289. _ForwardIterator __last, std::forward_iterator_tag);
  1290. _GLIBCXX20_CONSTEXPR
  1291. void
  1292. _M_insert_aux(iterator __position, bool __x);
  1293. _GLIBCXX20_CONSTEXPR
  1294. size_type
  1295. _M_check_len(size_type __n, const char* __s) const
  1296. {
  1297. if (max_size() - size() < __n)
  1298. __throw_length_error(__N(__s));
  1299. const size_type __len = size() + std::max(size(), __n);
  1300. return (__len < size() || __len > max_size()) ? max_size() : __len;
  1301. }
  1302. _GLIBCXX20_CONSTEXPR
  1303. void
  1304. _M_erase_at_end(iterator __pos)
  1305. { this->_M_impl._M_finish = __pos; }
  1306. _GLIBCXX20_CONSTEXPR
  1307. iterator
  1308. _M_erase(iterator __pos);
  1309. _GLIBCXX20_CONSTEXPR
  1310. iterator
  1311. _M_erase(iterator __first, iterator __last);
  1312. protected:
  1313. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1314. // DR 464. Suggestion for new member functions in standard containers.
  1315. // N.B. DR 464 says nothing about vector<bool> but we need something
  1316. // here due to the using-declaration in __gnu_debug::vector.
  1317. // vector class.
  1318. #if __cplusplus >= 201103L
  1319. void data() = delete;
  1320. #else
  1321. void data() { }
  1322. #endif
  1323. };
  1324. _GLIBCXX_END_NAMESPACE_CONTAINER
  1325. // Fill a partial word.
  1326. _GLIBCXX20_CONSTEXPR
  1327. inline void
  1328. __fill_bvector(_Bit_type* __v, unsigned int __first, unsigned int __last,
  1329. bool __x) _GLIBCXX_NOEXCEPT
  1330. {
  1331. const _Bit_type __fmask = ~0ul << __first;
  1332. const _Bit_type __lmask = ~0ul >> (_S_word_bit - __last);
  1333. const _Bit_type __mask = __fmask & __lmask;
  1334. if (__x)
  1335. *__v |= __mask;
  1336. else
  1337. *__v &= ~__mask;
  1338. }
  1339. // Fill N full words, as if using memset, but usable in constant expressions.
  1340. __attribute__((__nonnull__))
  1341. _GLIBCXX20_CONSTEXPR
  1342. inline void
  1343. __fill_bvector_n(_Bit_type* __p, size_t __n, bool __x) _GLIBCXX_NOEXCEPT
  1344. {
  1345. #if __cpp_lib_is_constant_evaluated
  1346. if (std::is_constant_evaluated())
  1347. {
  1348. for (size_t __i = 0; __i < __n; ++__i)
  1349. __p[__i] = __x ? ~0ul : 0ul;
  1350. return;
  1351. }
  1352. #endif
  1353. __builtin_memset(__p, __x ? ~0 : 0, __n * sizeof(_Bit_type));
  1354. }
  1355. _GLIBCXX20_CONSTEXPR
  1356. inline void
  1357. __fill_a1(_GLIBCXX_STD_C::_Bit_iterator __first,
  1358. _GLIBCXX_STD_C::_Bit_iterator __last, const bool& __x)
  1359. {
  1360. if (__first._M_p != __last._M_p)
  1361. {
  1362. _Bit_type* __first_p = __first._M_p;
  1363. if (__first._M_offset != 0)
  1364. __fill_bvector(__first_p++, __first._M_offset, _S_word_bit, __x);
  1365. __fill_bvector_n(__first_p, __last._M_p - __first_p, __x);
  1366. if (__last._M_offset != 0)
  1367. __fill_bvector(__last._M_p, 0, __last._M_offset, __x);
  1368. }
  1369. else if (__first._M_offset != __last._M_offset)
  1370. __fill_bvector(__first._M_p, __first._M_offset, __last._M_offset, __x);
  1371. }
  1372. #if __cplusplus >= 201103L
  1373. // DR 1182.
  1374. /// std::hash specialization for vector<bool>.
  1375. template<typename _Alloc>
  1376. struct hash<_GLIBCXX_STD_C::vector<bool, _Alloc>>
  1377. : public __hash_base<size_t, _GLIBCXX_STD_C::vector<bool, _Alloc>>
  1378. {
  1379. size_t
  1380. operator()(const _GLIBCXX_STD_C::vector<bool, _Alloc>&) const noexcept;
  1381. };
  1382. #endif // C++11
  1383. _GLIBCXX_END_NAMESPACE_VERSION
  1384. } // namespace std
  1385. #endif