stl_bvector.h 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. // vector<bool> specialization -*- C++ -*-
  2. // Copyright (C) 2001-2020 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. _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
  59. typedef unsigned long _Bit_type;
  60. enum { _S_word_bit = int(__CHAR_BIT__ * sizeof(_Bit_type)) };
  61. struct _Bit_reference
  62. {
  63. _Bit_type * _M_p;
  64. _Bit_type _M_mask;
  65. _Bit_reference(_Bit_type * __x, _Bit_type __y)
  66. : _M_p(__x), _M_mask(__y) { }
  67. _Bit_reference() _GLIBCXX_NOEXCEPT : _M_p(0), _M_mask(0) { }
  68. #if __cplusplus >= 201103L
  69. _Bit_reference(const _Bit_reference&) = default;
  70. #endif
  71. operator bool() const _GLIBCXX_NOEXCEPT
  72. { return !!(*_M_p & _M_mask); }
  73. _Bit_reference&
  74. operator=(bool __x) _GLIBCXX_NOEXCEPT
  75. {
  76. if (__x)
  77. *_M_p |= _M_mask;
  78. else
  79. *_M_p &= ~_M_mask;
  80. return *this;
  81. }
  82. _Bit_reference&
  83. operator=(const _Bit_reference& __x) _GLIBCXX_NOEXCEPT
  84. { return *this = bool(__x); }
  85. bool
  86. operator==(const _Bit_reference& __x) const
  87. { return bool(*this) == bool(__x); }
  88. bool
  89. operator<(const _Bit_reference& __x) const
  90. { return !bool(*this) && bool(__x); }
  91. void
  92. flip() _GLIBCXX_NOEXCEPT
  93. { *_M_p ^= _M_mask; }
  94. };
  95. #if __cplusplus >= 201103L
  96. inline void
  97. swap(_Bit_reference __x, _Bit_reference __y) noexcept
  98. {
  99. bool __tmp = __x;
  100. __x = __y;
  101. __y = __tmp;
  102. }
  103. inline void
  104. swap(_Bit_reference __x, bool& __y) noexcept
  105. {
  106. bool __tmp = __x;
  107. __x = __y;
  108. __y = __tmp;
  109. }
  110. inline void
  111. swap(bool& __x, _Bit_reference __y) noexcept
  112. {
  113. bool __tmp = __x;
  114. __x = __y;
  115. __y = __tmp;
  116. }
  117. #endif
  118. struct _Bit_iterator_base
  119. : public std::iterator<std::random_access_iterator_tag, bool>
  120. {
  121. _Bit_type * _M_p;
  122. unsigned int _M_offset;
  123. _Bit_iterator_base(_Bit_type * __x, unsigned int __y)
  124. : _M_p(__x), _M_offset(__y) { }
  125. void
  126. _M_bump_up()
  127. {
  128. if (_M_offset++ == int(_S_word_bit) - 1)
  129. {
  130. _M_offset = 0;
  131. ++_M_p;
  132. }
  133. }
  134. void
  135. _M_bump_down()
  136. {
  137. if (_M_offset-- == 0)
  138. {
  139. _M_offset = int(_S_word_bit) - 1;
  140. --_M_p;
  141. }
  142. }
  143. void
  144. _M_incr(ptrdiff_t __i)
  145. {
  146. difference_type __n = __i + _M_offset;
  147. _M_p += __n / int(_S_word_bit);
  148. __n = __n % int(_S_word_bit);
  149. if (__n < 0)
  150. {
  151. __n += int(_S_word_bit);
  152. --_M_p;
  153. }
  154. _M_offset = static_cast<unsigned int>(__n);
  155. }
  156. friend _GLIBCXX20_CONSTEXPR bool
  157. operator==(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
  158. { return __x._M_p == __y._M_p && __x._M_offset == __y._M_offset; }
  159. #if __cpp_lib_three_way_comparison
  160. friend constexpr strong_ordering
  161. operator<=>(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
  162. noexcept
  163. {
  164. if (const auto __cmp = __x._M_p <=> __y._M_p; __cmp != 0)
  165. return __cmp;
  166. return __x._M_offset <=> __y._M_offset;
  167. }
  168. #else
  169. friend bool
  170. operator<(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
  171. {
  172. return __x._M_p < __y._M_p
  173. || (__x._M_p == __y._M_p && __x._M_offset < __y._M_offset);
  174. }
  175. friend bool
  176. operator!=(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
  177. { return !(__x == __y); }
  178. friend bool
  179. operator>(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
  180. { return __y < __x; }
  181. friend bool
  182. operator<=(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
  183. { return !(__y < __x); }
  184. friend bool
  185. operator>=(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
  186. { return !(__x < __y); }
  187. #endif // three-way comparison
  188. friend ptrdiff_t
  189. operator-(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
  190. {
  191. return (int(_S_word_bit) * (__x._M_p - __y._M_p)
  192. + __x._M_offset - __y._M_offset);
  193. }
  194. };
  195. struct _Bit_iterator : public _Bit_iterator_base
  196. {
  197. typedef _Bit_reference reference;
  198. #if __cplusplus > 201703L
  199. typedef void pointer;
  200. #else
  201. typedef _Bit_reference* pointer;
  202. #endif
  203. typedef _Bit_iterator iterator;
  204. _Bit_iterator() : _Bit_iterator_base(0, 0) { }
  205. _Bit_iterator(_Bit_type * __x, unsigned int __y)
  206. : _Bit_iterator_base(__x, __y) { }
  207. iterator
  208. _M_const_cast() const
  209. { return *this; }
  210. reference
  211. operator*() const
  212. { return reference(_M_p, 1UL << _M_offset); }
  213. iterator&
  214. operator++()
  215. {
  216. _M_bump_up();
  217. return *this;
  218. }
  219. iterator
  220. operator++(int)
  221. {
  222. iterator __tmp = *this;
  223. _M_bump_up();
  224. return __tmp;
  225. }
  226. iterator&
  227. operator--()
  228. {
  229. _M_bump_down();
  230. return *this;
  231. }
  232. iterator
  233. operator--(int)
  234. {
  235. iterator __tmp = *this;
  236. _M_bump_down();
  237. return __tmp;
  238. }
  239. iterator&
  240. operator+=(difference_type __i)
  241. {
  242. _M_incr(__i);
  243. return *this;
  244. }
  245. iterator&
  246. operator-=(difference_type __i)
  247. {
  248. *this += -__i;
  249. return *this;
  250. }
  251. reference
  252. operator[](difference_type __i) const
  253. { return *(*this + __i); }
  254. friend iterator
  255. operator+(const iterator& __x, difference_type __n)
  256. {
  257. iterator __tmp = __x;
  258. __tmp += __n;
  259. return __tmp;
  260. }
  261. friend iterator
  262. operator+(difference_type __n, const iterator& __x)
  263. { return __x + __n; }
  264. friend iterator
  265. operator-(const iterator& __x, difference_type __n)
  266. {
  267. iterator __tmp = __x;
  268. __tmp -= __n;
  269. return __tmp;
  270. }
  271. };
  272. struct _Bit_const_iterator : public _Bit_iterator_base
  273. {
  274. typedef bool reference;
  275. typedef bool const_reference;
  276. #if __cplusplus > 201703L
  277. typedef void pointer;
  278. #else
  279. typedef const bool* pointer;
  280. #endif
  281. typedef _Bit_const_iterator const_iterator;
  282. _Bit_const_iterator() : _Bit_iterator_base(0, 0) { }
  283. _Bit_const_iterator(_Bit_type * __x, unsigned int __y)
  284. : _Bit_iterator_base(__x, __y) { }
  285. _Bit_const_iterator(const _Bit_iterator& __x)
  286. : _Bit_iterator_base(__x._M_p, __x._M_offset) { }
  287. _Bit_iterator
  288. _M_const_cast() const
  289. { return _Bit_iterator(_M_p, _M_offset); }
  290. const_reference
  291. operator*() const
  292. { return _Bit_reference(_M_p, 1UL << _M_offset); }
  293. const_iterator&
  294. operator++()
  295. {
  296. _M_bump_up();
  297. return *this;
  298. }
  299. const_iterator
  300. operator++(int)
  301. {
  302. const_iterator __tmp = *this;
  303. _M_bump_up();
  304. return __tmp;
  305. }
  306. const_iterator&
  307. operator--()
  308. {
  309. _M_bump_down();
  310. return *this;
  311. }
  312. const_iterator
  313. operator--(int)
  314. {
  315. const_iterator __tmp = *this;
  316. _M_bump_down();
  317. return __tmp;
  318. }
  319. const_iterator&
  320. operator+=(difference_type __i)
  321. {
  322. _M_incr(__i);
  323. return *this;
  324. }
  325. const_iterator&
  326. operator-=(difference_type __i)
  327. {
  328. *this += -__i;
  329. return *this;
  330. }
  331. const_reference
  332. operator[](difference_type __i) const
  333. { return *(*this + __i); }
  334. friend const_iterator
  335. operator+(const const_iterator& __x, difference_type __n)
  336. {
  337. const_iterator __tmp = __x;
  338. __tmp += __n;
  339. return __tmp;
  340. }
  341. friend const_iterator
  342. operator-(const const_iterator& __x, difference_type __n)
  343. {
  344. const_iterator __tmp = __x;
  345. __tmp -= __n;
  346. return __tmp;
  347. }
  348. friend const_iterator
  349. operator+(difference_type __n, const const_iterator& __x)
  350. { return __x + __n; }
  351. };
  352. inline void
  353. __fill_bvector(_Bit_type * __v,
  354. unsigned int __first, unsigned int __last, bool __x)
  355. {
  356. const _Bit_type __fmask = ~0ul << __first;
  357. const _Bit_type __lmask = ~0ul >> (_S_word_bit - __last);
  358. const _Bit_type __mask = __fmask & __lmask;
  359. if (__x)
  360. *__v |= __mask;
  361. else
  362. *__v &= ~__mask;
  363. }
  364. inline void
  365. fill(_Bit_iterator __first, _Bit_iterator __last, const bool& __x)
  366. {
  367. if (__first._M_p != __last._M_p)
  368. {
  369. _Bit_type* __first_p = __first._M_p;
  370. if (__first._M_offset != 0)
  371. __fill_bvector(__first_p++, __first._M_offset, _S_word_bit, __x);
  372. __builtin_memset(__first_p, __x ? ~0 : 0,
  373. (__last._M_p - __first_p) * sizeof(_Bit_type));
  374. if (__last._M_offset != 0)
  375. __fill_bvector(__last._M_p, 0, __last._M_offset, __x);
  376. }
  377. else if (__first._M_offset != __last._M_offset)
  378. __fill_bvector(__first._M_p, __first._M_offset, __last._M_offset, __x);
  379. }
  380. template<typename _Alloc>
  381. struct _Bvector_base
  382. {
  383. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  384. rebind<_Bit_type>::other _Bit_alloc_type;
  385. typedef typename __gnu_cxx::__alloc_traits<_Bit_alloc_type>
  386. _Bit_alloc_traits;
  387. typedef typename _Bit_alloc_traits::pointer _Bit_pointer;
  388. struct _Bvector_impl_data
  389. {
  390. _Bit_iterator _M_start;
  391. _Bit_iterator _M_finish;
  392. _Bit_pointer _M_end_of_storage;
  393. _Bvector_impl_data() _GLIBCXX_NOEXCEPT
  394. : _M_start(), _M_finish(), _M_end_of_storage()
  395. { }
  396. #if __cplusplus >= 201103L
  397. _Bvector_impl_data(_Bvector_impl_data&& __x) noexcept
  398. : _M_start(__x._M_start), _M_finish(__x._M_finish)
  399. , _M_end_of_storage(__x._M_end_of_storage)
  400. { __x._M_reset(); }
  401. void
  402. _M_move_data(_Bvector_impl_data&& __x) noexcept
  403. {
  404. this->_M_start = __x._M_start;
  405. this->_M_finish = __x._M_finish;
  406. this->_M_end_of_storage = __x._M_end_of_storage;
  407. __x._M_reset();
  408. }
  409. #endif
  410. void
  411. _M_reset() _GLIBCXX_NOEXCEPT
  412. {
  413. _M_start = _M_finish = _Bit_iterator();
  414. _M_end_of_storage = _Bit_pointer();
  415. }
  416. };
  417. struct _Bvector_impl
  418. : public _Bit_alloc_type, public _Bvector_impl_data
  419. {
  420. public:
  421. _Bvector_impl() _GLIBCXX_NOEXCEPT_IF(
  422. is_nothrow_default_constructible<_Bit_alloc_type>::value)
  423. : _Bit_alloc_type()
  424. { }
  425. _Bvector_impl(const _Bit_alloc_type& __a) _GLIBCXX_NOEXCEPT
  426. : _Bit_alloc_type(__a)
  427. { }
  428. #if __cplusplus >= 201103L
  429. _Bvector_impl(_Bvector_impl&&) = default;
  430. #endif
  431. _Bit_type*
  432. _M_end_addr() const _GLIBCXX_NOEXCEPT
  433. {
  434. if (this->_M_end_of_storage)
  435. return std::__addressof(this->_M_end_of_storage[-1]) + 1;
  436. return 0;
  437. }
  438. };
  439. public:
  440. typedef _Alloc allocator_type;
  441. _Bit_alloc_type&
  442. _M_get_Bit_allocator() _GLIBCXX_NOEXCEPT
  443. { return this->_M_impl; }
  444. const _Bit_alloc_type&
  445. _M_get_Bit_allocator() const _GLIBCXX_NOEXCEPT
  446. { return this->_M_impl; }
  447. allocator_type
  448. get_allocator() const _GLIBCXX_NOEXCEPT
  449. { return allocator_type(_M_get_Bit_allocator()); }
  450. #if __cplusplus >= 201103L
  451. _Bvector_base() = default;
  452. #else
  453. _Bvector_base() { }
  454. #endif
  455. _Bvector_base(const allocator_type& __a)
  456. : _M_impl(__a) { }
  457. #if __cplusplus >= 201103L
  458. _Bvector_base(_Bvector_base&&) = default;
  459. #endif
  460. ~_Bvector_base()
  461. { this->_M_deallocate(); }
  462. protected:
  463. _Bvector_impl _M_impl;
  464. _Bit_pointer
  465. _M_allocate(size_t __n)
  466. { return _Bit_alloc_traits::allocate(_M_impl, _S_nword(__n)); }
  467. void
  468. _M_deallocate()
  469. {
  470. if (_M_impl._M_start._M_p)
  471. {
  472. const size_t __n = _M_impl._M_end_addr() - _M_impl._M_start._M_p;
  473. _Bit_alloc_traits::deallocate(_M_impl,
  474. _M_impl._M_end_of_storage - __n,
  475. __n);
  476. _M_impl._M_reset();
  477. }
  478. }
  479. #if __cplusplus >= 201103L
  480. void
  481. _M_move_data(_Bvector_base&& __x) noexcept
  482. { _M_impl._M_move_data(std::move(__x._M_impl)); }
  483. #endif
  484. static size_t
  485. _S_nword(size_t __n)
  486. { return (__n + int(_S_word_bit) - 1) / int(_S_word_bit); }
  487. };
  488. _GLIBCXX_END_NAMESPACE_CONTAINER
  489. _GLIBCXX_END_NAMESPACE_VERSION
  490. } // namespace std
  491. // Declare a partial specialization of vector<T, Alloc>.
  492. #include <bits/stl_vector.h>
  493. namespace std _GLIBCXX_VISIBILITY(default)
  494. {
  495. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  496. _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
  497. /**
  498. * @brief A specialization of vector for booleans which offers fixed time
  499. * access to individual elements in any order.
  500. *
  501. * @ingroup sequences
  502. *
  503. * @tparam _Alloc Allocator type.
  504. *
  505. * Note that vector<bool> does not actually meet the requirements for being
  506. * a container. This is because the reference and pointer types are not
  507. * really references and pointers to bool. See DR96 for details. @see
  508. * vector for function documentation.
  509. *
  510. * In some terminology a %vector can be described as a dynamic
  511. * C-style array, it offers fast and efficient access to individual
  512. * elements in any order and saves the user from worrying about
  513. * memory and size allocation. Subscripting ( @c [] ) access is
  514. * also provided as with C-style arrays.
  515. */
  516. template<typename _Alloc>
  517. class vector<bool, _Alloc> : protected _Bvector_base<_Alloc>
  518. {
  519. typedef _Bvector_base<_Alloc> _Base;
  520. typedef typename _Base::_Bit_pointer _Bit_pointer;
  521. typedef typename _Base::_Bit_alloc_traits _Bit_alloc_traits;
  522. #if __cplusplus >= 201103L
  523. friend struct std::hash<vector>;
  524. #endif
  525. public:
  526. typedef bool value_type;
  527. typedef size_t size_type;
  528. typedef ptrdiff_t difference_type;
  529. typedef _Bit_reference reference;
  530. typedef bool const_reference;
  531. typedef _Bit_reference* pointer;
  532. typedef const bool* const_pointer;
  533. typedef _Bit_iterator iterator;
  534. typedef _Bit_const_iterator const_iterator;
  535. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  536. typedef std::reverse_iterator<iterator> reverse_iterator;
  537. typedef _Alloc allocator_type;
  538. allocator_type
  539. get_allocator() const
  540. { return _Base::get_allocator(); }
  541. protected:
  542. using _Base::_M_allocate;
  543. using _Base::_M_deallocate;
  544. using _Base::_S_nword;
  545. using _Base::_M_get_Bit_allocator;
  546. public:
  547. #if __cplusplus >= 201103L
  548. vector() = default;
  549. #else
  550. vector() { }
  551. #endif
  552. explicit
  553. vector(const allocator_type& __a)
  554. : _Base(__a) { }
  555. #if __cplusplus >= 201103L
  556. explicit
  557. vector(size_type __n, const allocator_type& __a = allocator_type())
  558. : vector(__n, false, __a)
  559. { }
  560. vector(size_type __n, const bool& __value,
  561. const allocator_type& __a = allocator_type())
  562. #else
  563. explicit
  564. vector(size_type __n, const bool& __value = bool(),
  565. const allocator_type& __a = allocator_type())
  566. #endif
  567. : _Base(__a)
  568. {
  569. _M_initialize(__n);
  570. _M_initialize_value(__value);
  571. }
  572. vector(const vector& __x)
  573. : _Base(_Bit_alloc_traits::_S_select_on_copy(__x._M_get_Bit_allocator()))
  574. {
  575. _M_initialize(__x.size());
  576. _M_copy_aligned(__x.begin(), __x.end(), this->_M_impl._M_start);
  577. }
  578. #if __cplusplus >= 201103L
  579. vector(vector&&) = default;
  580. vector(vector&& __x, const allocator_type& __a)
  581. noexcept(_Bit_alloc_traits::_S_always_equal())
  582. : _Base(__a)
  583. {
  584. if (__x.get_allocator() == __a)
  585. this->_M_move_data(std::move(__x));
  586. else
  587. {
  588. _M_initialize(__x.size());
  589. _M_copy_aligned(__x.begin(), __x.end(), begin());
  590. __x.clear();
  591. }
  592. }
  593. vector(const vector& __x, const allocator_type& __a)
  594. : _Base(__a)
  595. {
  596. _M_initialize(__x.size());
  597. _M_copy_aligned(__x.begin(), __x.end(), this->_M_impl._M_start);
  598. }
  599. vector(initializer_list<bool> __l,
  600. const allocator_type& __a = allocator_type())
  601. : _Base(__a)
  602. {
  603. _M_initialize_range(__l.begin(), __l.end(),
  604. random_access_iterator_tag());
  605. }
  606. #endif
  607. #if __cplusplus >= 201103L
  608. template<typename _InputIterator,
  609. typename = std::_RequireInputIter<_InputIterator>>
  610. vector(_InputIterator __first, _InputIterator __last,
  611. const allocator_type& __a = allocator_type())
  612. : _Base(__a)
  613. { _M_initialize_dispatch(__first, __last, __false_type()); }
  614. #else
  615. template<typename _InputIterator>
  616. vector(_InputIterator __first, _InputIterator __last,
  617. const allocator_type& __a = allocator_type())
  618. : _Base(__a)
  619. {
  620. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  621. _M_initialize_dispatch(__first, __last, _Integral());
  622. }
  623. #endif
  624. ~vector() _GLIBCXX_NOEXCEPT { }
  625. vector&
  626. operator=(const vector& __x)
  627. {
  628. if (&__x == this)
  629. return *this;
  630. #if __cplusplus >= 201103L
  631. if (_Bit_alloc_traits::_S_propagate_on_copy_assign())
  632. {
  633. if (this->_M_get_Bit_allocator() != __x._M_get_Bit_allocator())
  634. {
  635. this->_M_deallocate();
  636. std::__alloc_on_copy(_M_get_Bit_allocator(),
  637. __x._M_get_Bit_allocator());
  638. _M_initialize(__x.size());
  639. }
  640. else
  641. std::__alloc_on_copy(_M_get_Bit_allocator(),
  642. __x._M_get_Bit_allocator());
  643. }
  644. #endif
  645. if (__x.size() > capacity())
  646. {
  647. this->_M_deallocate();
  648. _M_initialize(__x.size());
  649. }
  650. this->_M_impl._M_finish = _M_copy_aligned(__x.begin(), __x.end(),
  651. begin());
  652. return *this;
  653. }
  654. #if __cplusplus >= 201103L
  655. vector&
  656. operator=(vector&& __x) noexcept(_Bit_alloc_traits::_S_nothrow_move())
  657. {
  658. if (_Bit_alloc_traits::_S_propagate_on_move_assign()
  659. || this->_M_get_Bit_allocator() == __x._M_get_Bit_allocator())
  660. {
  661. this->_M_deallocate();
  662. this->_M_move_data(std::move(__x));
  663. std::__alloc_on_move(_M_get_Bit_allocator(),
  664. __x._M_get_Bit_allocator());
  665. }
  666. else
  667. {
  668. if (__x.size() > capacity())
  669. {
  670. this->_M_deallocate();
  671. _M_initialize(__x.size());
  672. }
  673. this->_M_impl._M_finish = _M_copy_aligned(__x.begin(), __x.end(),
  674. begin());
  675. __x.clear();
  676. }
  677. return *this;
  678. }
  679. vector&
  680. operator=(initializer_list<bool> __l)
  681. {
  682. this->assign (__l.begin(), __l.end());
  683. return *this;
  684. }
  685. #endif
  686. // assign(), a generalized assignment member function. Two
  687. // versions: one that takes a count, and one that takes a range.
  688. // The range version is a member template, so we dispatch on whether
  689. // or not the type is an integer.
  690. void
  691. assign(size_type __n, const bool& __x)
  692. { _M_fill_assign(__n, __x); }
  693. #if __cplusplus >= 201103L
  694. template<typename _InputIterator,
  695. typename = std::_RequireInputIter<_InputIterator>>
  696. void
  697. assign(_InputIterator __first, _InputIterator __last)
  698. { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
  699. #else
  700. template<typename _InputIterator>
  701. void
  702. assign(_InputIterator __first, _InputIterator __last)
  703. {
  704. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  705. _M_assign_dispatch(__first, __last, _Integral());
  706. }
  707. #endif
  708. #if __cplusplus >= 201103L
  709. void
  710. assign(initializer_list<bool> __l)
  711. { _M_assign_aux(__l.begin(), __l.end(), random_access_iterator_tag()); }
  712. #endif
  713. iterator
  714. begin() _GLIBCXX_NOEXCEPT
  715. { return iterator(this->_M_impl._M_start._M_p, 0); }
  716. const_iterator
  717. begin() const _GLIBCXX_NOEXCEPT
  718. { return const_iterator(this->_M_impl._M_start._M_p, 0); }
  719. iterator
  720. end() _GLIBCXX_NOEXCEPT
  721. { return this->_M_impl._M_finish; }
  722. const_iterator
  723. end() const _GLIBCXX_NOEXCEPT
  724. { return this->_M_impl._M_finish; }
  725. reverse_iterator
  726. rbegin() _GLIBCXX_NOEXCEPT
  727. { return reverse_iterator(end()); }
  728. const_reverse_iterator
  729. rbegin() const _GLIBCXX_NOEXCEPT
  730. { return const_reverse_iterator(end()); }
  731. reverse_iterator
  732. rend() _GLIBCXX_NOEXCEPT
  733. { return reverse_iterator(begin()); }
  734. const_reverse_iterator
  735. rend() const _GLIBCXX_NOEXCEPT
  736. { return const_reverse_iterator(begin()); }
  737. #if __cplusplus >= 201103L
  738. const_iterator
  739. cbegin() const noexcept
  740. { return const_iterator(this->_M_impl._M_start._M_p, 0); }
  741. const_iterator
  742. cend() const noexcept
  743. { return this->_M_impl._M_finish; }
  744. const_reverse_iterator
  745. crbegin() const noexcept
  746. { return const_reverse_iterator(end()); }
  747. const_reverse_iterator
  748. crend() const noexcept
  749. { return const_reverse_iterator(begin()); }
  750. #endif
  751. size_type
  752. size() const _GLIBCXX_NOEXCEPT
  753. { return size_type(end() - begin()); }
  754. size_type
  755. max_size() const _GLIBCXX_NOEXCEPT
  756. {
  757. const size_type __isize =
  758. __gnu_cxx::__numeric_traits<difference_type>::__max
  759. - int(_S_word_bit) + 1;
  760. const size_type __asize
  761. = _Bit_alloc_traits::max_size(_M_get_Bit_allocator());
  762. return (__asize <= __isize / int(_S_word_bit)
  763. ? __asize * int(_S_word_bit) : __isize);
  764. }
  765. size_type
  766. capacity() const _GLIBCXX_NOEXCEPT
  767. { return size_type(const_iterator(this->_M_impl._M_end_addr(), 0)
  768. - begin()); }
  769. _GLIBCXX_NODISCARD bool
  770. empty() const _GLIBCXX_NOEXCEPT
  771. { return begin() == end(); }
  772. reference
  773. operator[](size_type __n)
  774. {
  775. return *iterator(this->_M_impl._M_start._M_p
  776. + __n / int(_S_word_bit), __n % int(_S_word_bit));
  777. }
  778. const_reference
  779. operator[](size_type __n) const
  780. {
  781. return *const_iterator(this->_M_impl._M_start._M_p
  782. + __n / int(_S_word_bit), __n % int(_S_word_bit));
  783. }
  784. protected:
  785. void
  786. _M_range_check(size_type __n) const
  787. {
  788. if (__n >= this->size())
  789. __throw_out_of_range_fmt(__N("vector<bool>::_M_range_check: __n "
  790. "(which is %zu) >= this->size() "
  791. "(which is %zu)"),
  792. __n, this->size());
  793. }
  794. public:
  795. reference
  796. at(size_type __n)
  797. { _M_range_check(__n); return (*this)[__n]; }
  798. const_reference
  799. at(size_type __n) const
  800. { _M_range_check(__n); return (*this)[__n]; }
  801. void
  802. reserve(size_type __n)
  803. {
  804. if (__n > max_size())
  805. __throw_length_error(__N("vector::reserve"));
  806. if (capacity() < __n)
  807. _M_reallocate(__n);
  808. }
  809. reference
  810. front()
  811. { return *begin(); }
  812. const_reference
  813. front() const
  814. { return *begin(); }
  815. reference
  816. back()
  817. { return *(end() - 1); }
  818. const_reference
  819. back() const
  820. { return *(end() - 1); }
  821. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  822. // DR 464. Suggestion for new member functions in standard containers.
  823. // N.B. DR 464 says nothing about vector<bool> but we need something
  824. // here due to the way we are implementing DR 464 in the debug-mode
  825. // vector class.
  826. void
  827. data() _GLIBCXX_NOEXCEPT { }
  828. void
  829. push_back(bool __x)
  830. {
  831. if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_addr())
  832. *this->_M_impl._M_finish++ = __x;
  833. else
  834. _M_insert_aux(end(), __x);
  835. }
  836. void
  837. swap(vector& __x) _GLIBCXX_NOEXCEPT
  838. {
  839. std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
  840. std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
  841. std::swap(this->_M_impl._M_end_of_storage,
  842. __x._M_impl._M_end_of_storage);
  843. _Bit_alloc_traits::_S_on_swap(_M_get_Bit_allocator(),
  844. __x._M_get_Bit_allocator());
  845. }
  846. // [23.2.5]/1, third-to-last entry in synopsis listing
  847. static void
  848. swap(reference __x, reference __y) _GLIBCXX_NOEXCEPT
  849. {
  850. bool __tmp = __x;
  851. __x = __y;
  852. __y = __tmp;
  853. }
  854. iterator
  855. #if __cplusplus >= 201103L
  856. insert(const_iterator __position, const bool& __x = bool())
  857. #else
  858. insert(iterator __position, const bool& __x = bool())
  859. #endif
  860. {
  861. const difference_type __n = __position - begin();
  862. if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_addr()
  863. && __position == end())
  864. *this->_M_impl._M_finish++ = __x;
  865. else
  866. _M_insert_aux(__position._M_const_cast(), __x);
  867. return begin() + __n;
  868. }
  869. #if __cplusplus >= 201103L
  870. template<typename _InputIterator,
  871. typename = std::_RequireInputIter<_InputIterator>>
  872. iterator
  873. insert(const_iterator __position,
  874. _InputIterator __first, _InputIterator __last)
  875. {
  876. difference_type __offset = __position - cbegin();
  877. _M_insert_dispatch(__position._M_const_cast(),
  878. __first, __last, __false_type());
  879. return begin() + __offset;
  880. }
  881. #else
  882. template<typename _InputIterator>
  883. void
  884. insert(iterator __position,
  885. _InputIterator __first, _InputIterator __last)
  886. {
  887. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  888. _M_insert_dispatch(__position, __first, __last, _Integral());
  889. }
  890. #endif
  891. #if __cplusplus >= 201103L
  892. iterator
  893. insert(const_iterator __position, size_type __n, const bool& __x)
  894. {
  895. difference_type __offset = __position - cbegin();
  896. _M_fill_insert(__position._M_const_cast(), __n, __x);
  897. return begin() + __offset;
  898. }
  899. #else
  900. void
  901. insert(iterator __position, size_type __n, const bool& __x)
  902. { _M_fill_insert(__position, __n, __x); }
  903. #endif
  904. #if __cplusplus >= 201103L
  905. iterator
  906. insert(const_iterator __p, initializer_list<bool> __l)
  907. { return this->insert(__p, __l.begin(), __l.end()); }
  908. #endif
  909. void
  910. pop_back()
  911. { --this->_M_impl._M_finish; }
  912. iterator
  913. #if __cplusplus >= 201103L
  914. erase(const_iterator __position)
  915. #else
  916. erase(iterator __position)
  917. #endif
  918. { return _M_erase(__position._M_const_cast()); }
  919. iterator
  920. #if __cplusplus >= 201103L
  921. erase(const_iterator __first, const_iterator __last)
  922. #else
  923. erase(iterator __first, iterator __last)
  924. #endif
  925. { return _M_erase(__first._M_const_cast(), __last._M_const_cast()); }
  926. void
  927. resize(size_type __new_size, bool __x = bool())
  928. {
  929. if (__new_size < size())
  930. _M_erase_at_end(begin() + difference_type(__new_size));
  931. else
  932. insert(end(), __new_size - size(), __x);
  933. }
  934. #if __cplusplus >= 201103L
  935. void
  936. shrink_to_fit()
  937. { _M_shrink_to_fit(); }
  938. #endif
  939. void
  940. flip() _GLIBCXX_NOEXCEPT
  941. {
  942. _Bit_type * const __end = this->_M_impl._M_end_addr();
  943. for (_Bit_type * __p = this->_M_impl._M_start._M_p; __p != __end; ++__p)
  944. *__p = ~*__p;
  945. }
  946. void
  947. clear() _GLIBCXX_NOEXCEPT
  948. { _M_erase_at_end(begin()); }
  949. #if __cplusplus >= 201103L
  950. template<typename... _Args>
  951. #if __cplusplus > 201402L
  952. reference
  953. #else
  954. void
  955. #endif
  956. emplace_back(_Args&&... __args)
  957. {
  958. push_back(bool(__args...));
  959. #if __cplusplus > 201402L
  960. return back();
  961. #endif
  962. }
  963. template<typename... _Args>
  964. iterator
  965. emplace(const_iterator __pos, _Args&&... __args)
  966. { return insert(__pos, bool(__args...)); }
  967. #endif
  968. protected:
  969. // Precondition: __first._M_offset == 0 && __result._M_offset == 0.
  970. iterator
  971. _M_copy_aligned(const_iterator __first, const_iterator __last,
  972. iterator __result)
  973. {
  974. _Bit_type* __q = std::copy(__first._M_p, __last._M_p, __result._M_p);
  975. return std::copy(const_iterator(__last._M_p, 0), __last,
  976. iterator(__q, 0));
  977. }
  978. void
  979. _M_initialize(size_type __n)
  980. {
  981. if (__n)
  982. {
  983. _Bit_pointer __q = this->_M_allocate(__n);
  984. this->_M_impl._M_end_of_storage = __q + _S_nword(__n);
  985. this->_M_impl._M_start = iterator(std::__addressof(*__q), 0);
  986. }
  987. else
  988. {
  989. this->_M_impl._M_end_of_storage = _Bit_pointer();
  990. this->_M_impl._M_start = iterator(0, 0);
  991. }
  992. this->_M_impl._M_finish = this->_M_impl._M_start + difference_type(__n);
  993. }
  994. void
  995. _M_initialize_value(bool __x)
  996. {
  997. if (_Bit_type* __p = this->_M_impl._M_start._M_p)
  998. __builtin_memset(__p, __x ? ~0 : 0,
  999. (this->_M_impl._M_end_addr() - __p)
  1000. * sizeof(_Bit_type));
  1001. }
  1002. void
  1003. _M_reallocate(size_type __n);
  1004. #if __cplusplus >= 201103L
  1005. bool
  1006. _M_shrink_to_fit();
  1007. #endif
  1008. // Check whether it's an integral type. If so, it's not an iterator.
  1009. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1010. // 438. Ambiguity in the "do the right thing" clause
  1011. template<typename _Integer>
  1012. void
  1013. _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
  1014. {
  1015. _M_initialize(static_cast<size_type>(__n));
  1016. _M_initialize_value(__x);
  1017. }
  1018. template<typename _InputIterator>
  1019. void
  1020. _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
  1021. __false_type)
  1022. { _M_initialize_range(__first, __last,
  1023. std::__iterator_category(__first)); }
  1024. template<typename _InputIterator>
  1025. void
  1026. _M_initialize_range(_InputIterator __first, _InputIterator __last,
  1027. std::input_iterator_tag)
  1028. {
  1029. for (; __first != __last; ++__first)
  1030. push_back(*__first);
  1031. }
  1032. template<typename _ForwardIterator>
  1033. void
  1034. _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,
  1035. std::forward_iterator_tag)
  1036. {
  1037. const size_type __n = std::distance(__first, __last);
  1038. _M_initialize(__n);
  1039. std::copy(__first, __last, this->_M_impl._M_start);
  1040. }
  1041. #if __cplusplus < 201103L
  1042. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1043. // 438. Ambiguity in the "do the right thing" clause
  1044. template<typename _Integer>
  1045. void
  1046. _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
  1047. { _M_fill_assign(__n, __val); }
  1048. template<class _InputIterator>
  1049. void
  1050. _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
  1051. __false_type)
  1052. { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
  1053. #endif
  1054. void
  1055. _M_fill_assign(size_t __n, bool __x)
  1056. {
  1057. if (__n > size())
  1058. {
  1059. _M_initialize_value(__x);
  1060. insert(end(), __n - size(), __x);
  1061. }
  1062. else
  1063. {
  1064. _M_erase_at_end(begin() + __n);
  1065. _M_initialize_value(__x);
  1066. }
  1067. }
  1068. template<typename _InputIterator>
  1069. void
  1070. _M_assign_aux(_InputIterator __first, _InputIterator __last,
  1071. std::input_iterator_tag)
  1072. {
  1073. iterator __cur = begin();
  1074. for (; __first != __last && __cur != end(); ++__cur, (void)++__first)
  1075. *__cur = *__first;
  1076. if (__first == __last)
  1077. _M_erase_at_end(__cur);
  1078. else
  1079. insert(end(), __first, __last);
  1080. }
  1081. template<typename _ForwardIterator>
  1082. void
  1083. _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
  1084. std::forward_iterator_tag)
  1085. {
  1086. const size_type __len = std::distance(__first, __last);
  1087. if (__len < size())
  1088. _M_erase_at_end(std::copy(__first, __last, begin()));
  1089. else
  1090. {
  1091. _ForwardIterator __mid = __first;
  1092. std::advance(__mid, size());
  1093. std::copy(__first, __mid, begin());
  1094. insert(end(), __mid, __last);
  1095. }
  1096. }
  1097. // Check whether it's an integral type. If so, it's not an iterator.
  1098. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1099. // 438. Ambiguity in the "do the right thing" clause
  1100. template<typename _Integer>
  1101. void
  1102. _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
  1103. __true_type)
  1104. { _M_fill_insert(__pos, __n, __x); }
  1105. template<typename _InputIterator>
  1106. void
  1107. _M_insert_dispatch(iterator __pos,
  1108. _InputIterator __first, _InputIterator __last,
  1109. __false_type)
  1110. { _M_insert_range(__pos, __first, __last,
  1111. std::__iterator_category(__first)); }
  1112. void
  1113. _M_fill_insert(iterator __position, size_type __n, bool __x);
  1114. template<typename _InputIterator>
  1115. void
  1116. _M_insert_range(iterator __pos, _InputIterator __first,
  1117. _InputIterator __last, std::input_iterator_tag)
  1118. {
  1119. for (; __first != __last; ++__first)
  1120. {
  1121. __pos = insert(__pos, *__first);
  1122. ++__pos;
  1123. }
  1124. }
  1125. template<typename _ForwardIterator>
  1126. void
  1127. _M_insert_range(iterator __position, _ForwardIterator __first,
  1128. _ForwardIterator __last, std::forward_iterator_tag);
  1129. void
  1130. _M_insert_aux(iterator __position, bool __x);
  1131. size_type
  1132. _M_check_len(size_type __n, const char* __s) const
  1133. {
  1134. if (max_size() - size() < __n)
  1135. __throw_length_error(__N(__s));
  1136. const size_type __len = size() + std::max(size(), __n);
  1137. return (__len < size() || __len > max_size()) ? max_size() : __len;
  1138. }
  1139. void
  1140. _M_erase_at_end(iterator __pos)
  1141. { this->_M_impl._M_finish = __pos; }
  1142. iterator
  1143. _M_erase(iterator __pos);
  1144. iterator
  1145. _M_erase(iterator __first, iterator __last);
  1146. };
  1147. _GLIBCXX_END_NAMESPACE_CONTAINER
  1148. _GLIBCXX_END_NAMESPACE_VERSION
  1149. } // namespace std
  1150. #if __cplusplus >= 201103L
  1151. namespace std _GLIBCXX_VISIBILITY(default)
  1152. {
  1153. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  1154. // DR 1182.
  1155. /// std::hash specialization for vector<bool>.
  1156. template<typename _Alloc>
  1157. struct hash<_GLIBCXX_STD_C::vector<bool, _Alloc>>
  1158. : public __hash_base<size_t, _GLIBCXX_STD_C::vector<bool, _Alloc>>
  1159. {
  1160. size_t
  1161. operator()(const _GLIBCXX_STD_C::vector<bool, _Alloc>&) const noexcept;
  1162. };
  1163. _GLIBCXX_END_NAMESPACE_VERSION
  1164. }// namespace std
  1165. #endif // C++11
  1166. #endif