stl_bvector.h 33 KB

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