stl_bvector.h 33 KB

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