stl_bvector.h 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  1. // vector<bool> specialization -*- C++ -*-
  2. // Copyright (C) 2001-2021 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. template<typename _Alloc>
  353. struct _Bvector_base
  354. {
  355. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  356. rebind<_Bit_type>::other _Bit_alloc_type;
  357. typedef typename __gnu_cxx::__alloc_traits<_Bit_alloc_type>
  358. _Bit_alloc_traits;
  359. typedef typename _Bit_alloc_traits::pointer _Bit_pointer;
  360. struct _Bvector_impl_data
  361. {
  362. #if !_GLIBCXX_INLINE_VERSION
  363. _Bit_iterator _M_start;
  364. #else
  365. // We don't need the offset field for the start, it's always zero.
  366. struct {
  367. _Bit_type* _M_p;
  368. // Allow assignment from iterators (assume offset is zero):
  369. void operator=(_Bit_iterator __it) { _M_p = __it._M_p; }
  370. } _M_start;
  371. #endif
  372. _Bit_iterator _M_finish;
  373. _Bit_pointer _M_end_of_storage;
  374. _Bvector_impl_data() _GLIBCXX_NOEXCEPT
  375. : _M_start(), _M_finish(), _M_end_of_storage()
  376. { }
  377. #if __cplusplus >= 201103L
  378. _Bvector_impl_data(const _Bvector_impl_data&) = default;
  379. _Bvector_impl_data&
  380. operator=(const _Bvector_impl_data&) = default;
  381. _Bvector_impl_data(_Bvector_impl_data&& __x) noexcept
  382. : _Bvector_impl_data(__x)
  383. { __x._M_reset(); }
  384. void
  385. _M_move_data(_Bvector_impl_data&& __x) noexcept
  386. {
  387. *this = __x;
  388. __x._M_reset();
  389. }
  390. #endif
  391. void
  392. _M_reset() _GLIBCXX_NOEXCEPT
  393. { *this = _Bvector_impl_data(); }
  394. void
  395. _M_swap_data(_Bvector_impl_data& __x) _GLIBCXX_NOEXCEPT
  396. {
  397. // Do not use std::swap(_M_start, __x._M_start), etc as it loses
  398. // information used by TBAA.
  399. std::swap(*this, __x);
  400. }
  401. };
  402. struct _Bvector_impl
  403. : public _Bit_alloc_type, public _Bvector_impl_data
  404. {
  405. _Bvector_impl() _GLIBCXX_NOEXCEPT_IF(
  406. is_nothrow_default_constructible<_Bit_alloc_type>::value)
  407. : _Bit_alloc_type()
  408. { }
  409. _Bvector_impl(const _Bit_alloc_type& __a) _GLIBCXX_NOEXCEPT
  410. : _Bit_alloc_type(__a)
  411. { }
  412. #if __cplusplus >= 201103L
  413. // Not defaulted, to enforce noexcept(true) even when
  414. // !is_nothrow_move_constructible<_Bit_alloc_type>.
  415. _Bvector_impl(_Bvector_impl&& __x) noexcept
  416. : _Bit_alloc_type(std::move(__x)), _Bvector_impl_data(std::move(__x))
  417. { }
  418. _Bvector_impl(_Bit_alloc_type&& __a, _Bvector_impl&& __x) noexcept
  419. : _Bit_alloc_type(std::move(__a)), _Bvector_impl_data(std::move(__x))
  420. { }
  421. #endif
  422. _Bit_type*
  423. _M_end_addr() const _GLIBCXX_NOEXCEPT
  424. {
  425. if (this->_M_end_of_storage)
  426. return std::__addressof(this->_M_end_of_storage[-1]) + 1;
  427. return 0;
  428. }
  429. };
  430. public:
  431. typedef _Alloc allocator_type;
  432. _Bit_alloc_type&
  433. _M_get_Bit_allocator() _GLIBCXX_NOEXCEPT
  434. { return this->_M_impl; }
  435. const _Bit_alloc_type&
  436. _M_get_Bit_allocator() const _GLIBCXX_NOEXCEPT
  437. { return this->_M_impl; }
  438. allocator_type
  439. get_allocator() const _GLIBCXX_NOEXCEPT
  440. { return allocator_type(_M_get_Bit_allocator()); }
  441. #if __cplusplus >= 201103L
  442. _Bvector_base() = default;
  443. #else
  444. _Bvector_base() { }
  445. #endif
  446. _Bvector_base(const allocator_type& __a)
  447. : _M_impl(__a) { }
  448. #if __cplusplus >= 201103L
  449. _Bvector_base(_Bvector_base&&) = default;
  450. _Bvector_base(_Bvector_base&& __x, const allocator_type& __a) noexcept
  451. : _M_impl(_Bit_alloc_type(__a), std::move(__x._M_impl))
  452. { }
  453. #endif
  454. ~_Bvector_base()
  455. { this->_M_deallocate(); }
  456. protected:
  457. _Bvector_impl _M_impl;
  458. _Bit_pointer
  459. _M_allocate(size_t __n)
  460. { return _Bit_alloc_traits::allocate(_M_impl, _S_nword(__n)); }
  461. void
  462. _M_deallocate()
  463. {
  464. if (_M_impl._M_start._M_p)
  465. {
  466. const size_t __n = _M_impl._M_end_addr() - _M_impl._M_start._M_p;
  467. _Bit_alloc_traits::deallocate(_M_impl,
  468. _M_impl._M_end_of_storage - __n,
  469. __n);
  470. _M_impl._M_reset();
  471. }
  472. }
  473. #if __cplusplus >= 201103L
  474. void
  475. _M_move_data(_Bvector_base&& __x) noexcept
  476. { _M_impl._M_move_data(std::move(__x._M_impl)); }
  477. #endif
  478. static size_t
  479. _S_nword(size_t __n)
  480. { return (__n + int(_S_word_bit) - 1) / int(_S_word_bit); }
  481. };
  482. /**
  483. * @brief A specialization of vector for booleans which offers fixed time
  484. * access to individual elements in any order.
  485. *
  486. * @ingroup sequences
  487. *
  488. * @tparam _Alloc Allocator type.
  489. *
  490. * Note that vector<bool> does not actually meet the requirements for being
  491. * a container. This is because the reference and pointer types are not
  492. * really references and pointers to bool. See DR96 for details. @see
  493. * vector for function documentation.
  494. *
  495. * In some terminology a %vector can be described as a dynamic
  496. * C-style array, it offers fast and efficient access to individual
  497. * elements in any order and saves the user from worrying about
  498. * memory and size allocation. Subscripting ( @c [] ) access is
  499. * also provided as with C-style arrays.
  500. */
  501. template<typename _Alloc>
  502. class vector<bool, _Alloc> : protected _Bvector_base<_Alloc>
  503. {
  504. typedef _Bvector_base<_Alloc> _Base;
  505. typedef typename _Base::_Bit_pointer _Bit_pointer;
  506. typedef typename _Base::_Bit_alloc_traits _Bit_alloc_traits;
  507. #if __cplusplus >= 201103L
  508. friend struct std::hash<vector>;
  509. #endif
  510. public:
  511. typedef bool value_type;
  512. typedef size_t size_type;
  513. typedef ptrdiff_t difference_type;
  514. typedef _Bit_reference reference;
  515. typedef bool const_reference;
  516. typedef _Bit_reference* pointer;
  517. typedef const bool* const_pointer;
  518. typedef _Bit_iterator iterator;
  519. typedef _Bit_const_iterator const_iterator;
  520. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  521. typedef std::reverse_iterator<iterator> reverse_iterator;
  522. typedef _Alloc allocator_type;
  523. allocator_type
  524. get_allocator() const
  525. { return _Base::get_allocator(); }
  526. protected:
  527. using _Base::_M_allocate;
  528. using _Base::_M_deallocate;
  529. using _Base::_S_nword;
  530. using _Base::_M_get_Bit_allocator;
  531. public:
  532. #if __cplusplus >= 201103L
  533. vector() = default;
  534. #else
  535. vector() { }
  536. #endif
  537. explicit
  538. vector(const allocator_type& __a)
  539. : _Base(__a) { }
  540. #if __cplusplus >= 201103L
  541. explicit
  542. vector(size_type __n, const allocator_type& __a = allocator_type())
  543. : vector(__n, false, __a)
  544. { }
  545. vector(size_type __n, const bool& __value,
  546. const allocator_type& __a = allocator_type())
  547. #else
  548. explicit
  549. vector(size_type __n, const bool& __value = bool(),
  550. const allocator_type& __a = allocator_type())
  551. #endif
  552. : _Base(__a)
  553. {
  554. _M_initialize(__n);
  555. _M_initialize_value(__value);
  556. }
  557. vector(const vector& __x)
  558. : _Base(_Bit_alloc_traits::_S_select_on_copy(__x._M_get_Bit_allocator()))
  559. {
  560. _M_initialize(__x.size());
  561. _M_copy_aligned(__x.begin(), __x.end(), begin());
  562. }
  563. #if __cplusplus >= 201103L
  564. vector(vector&&) = default;
  565. private:
  566. vector(vector&& __x, const allocator_type& __a, true_type) noexcept
  567. : _Base(std::move(__x), __a)
  568. { }
  569. vector(vector&& __x, const allocator_type& __a, false_type)
  570. : _Base(__a)
  571. {
  572. if (__x.get_allocator() == __a)
  573. this->_M_move_data(std::move(__x));
  574. else
  575. {
  576. _M_initialize(__x.size());
  577. _M_copy_aligned(__x.begin(), __x.end(), begin());
  578. __x.clear();
  579. }
  580. }
  581. public:
  582. vector(vector&& __x, const allocator_type& __a)
  583. noexcept(_Bit_alloc_traits::_S_always_equal())
  584. : vector(std::move(__x), __a,
  585. typename _Bit_alloc_traits::is_always_equal{})
  586. { }
  587. vector(const vector& __x, const allocator_type& __a)
  588. : _Base(__a)
  589. {
  590. _M_initialize(__x.size());
  591. _M_copy_aligned(__x.begin(), __x.end(), begin());
  592. }
  593. vector(initializer_list<bool> __l,
  594. const allocator_type& __a = allocator_type())
  595. : _Base(__a)
  596. {
  597. _M_initialize_range(__l.begin(), __l.end(),
  598. random_access_iterator_tag());
  599. }
  600. #endif
  601. #if __cplusplus >= 201103L
  602. template<typename _InputIterator,
  603. typename = std::_RequireInputIter<_InputIterator>>
  604. vector(_InputIterator __first, _InputIterator __last,
  605. const allocator_type& __a = allocator_type())
  606. : _Base(__a)
  607. {
  608. _M_initialize_range(__first, __last,
  609. std::__iterator_category(__first));
  610. }
  611. #else
  612. template<typename _InputIterator>
  613. vector(_InputIterator __first, _InputIterator __last,
  614. const allocator_type& __a = allocator_type())
  615. : _Base(__a)
  616. {
  617. // Check whether it's an integral type. If so, it's not an iterator.
  618. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  619. _M_initialize_dispatch(__first, __last, _Integral());
  620. }
  621. #endif
  622. ~vector() _GLIBCXX_NOEXCEPT { }
  623. vector&
  624. operator=(const vector& __x)
  625. {
  626. if (&__x == this)
  627. return *this;
  628. #if __cplusplus >= 201103L
  629. if (_Bit_alloc_traits::_S_propagate_on_copy_assign())
  630. {
  631. if (this->_M_get_Bit_allocator() != __x._M_get_Bit_allocator())
  632. {
  633. this->_M_deallocate();
  634. std::__alloc_on_copy(_M_get_Bit_allocator(),
  635. __x._M_get_Bit_allocator());
  636. _M_initialize(__x.size());
  637. }
  638. else
  639. std::__alloc_on_copy(_M_get_Bit_allocator(),
  640. __x._M_get_Bit_allocator());
  641. }
  642. #endif
  643. if (__x.size() > capacity())
  644. {
  645. this->_M_deallocate();
  646. _M_initialize(__x.size());
  647. }
  648. this->_M_impl._M_finish = _M_copy_aligned(__x.begin(), __x.end(),
  649. begin());
  650. return *this;
  651. }
  652. #if __cplusplus >= 201103L
  653. vector&
  654. operator=(vector&& __x) noexcept(_Bit_alloc_traits::_S_nothrow_move())
  655. {
  656. if (_Bit_alloc_traits::_S_propagate_on_move_assign()
  657. || this->_M_get_Bit_allocator() == __x._M_get_Bit_allocator())
  658. {
  659. this->_M_deallocate();
  660. this->_M_move_data(std::move(__x));
  661. std::__alloc_on_move(_M_get_Bit_allocator(),
  662. __x._M_get_Bit_allocator());
  663. }
  664. else
  665. {
  666. if (__x.size() > capacity())
  667. {
  668. this->_M_deallocate();
  669. _M_initialize(__x.size());
  670. }
  671. this->_M_impl._M_finish = _M_copy_aligned(__x.begin(), __x.end(),
  672. begin());
  673. __x.clear();
  674. }
  675. return *this;
  676. }
  677. vector&
  678. operator=(initializer_list<bool> __l)
  679. {
  680. this->assign(__l.begin(), __l.end());
  681. return *this;
  682. }
  683. #endif
  684. // assign(), a generalized assignment member function. Two
  685. // versions: one that takes a count, and one that takes a range.
  686. // The range version is a member template, so we dispatch on whether
  687. // or not the type is an integer.
  688. void
  689. assign(size_type __n, const bool& __x)
  690. { _M_fill_assign(__n, __x); }
  691. #if __cplusplus >= 201103L
  692. template<typename _InputIterator,
  693. typename = std::_RequireInputIter<_InputIterator>>
  694. void
  695. assign(_InputIterator __first, _InputIterator __last)
  696. { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
  697. #else
  698. template<typename _InputIterator>
  699. void
  700. assign(_InputIterator __first, _InputIterator __last)
  701. {
  702. // Check whether it's an integral type. If so, it's not an iterator.
  703. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  704. _M_assign_dispatch(__first, __last, _Integral());
  705. }
  706. #endif
  707. #if __cplusplus >= 201103L
  708. void
  709. assign(initializer_list<bool> __l)
  710. { _M_assign_aux(__l.begin(), __l.end(), random_access_iterator_tag()); }
  711. #endif
  712. iterator
  713. begin() _GLIBCXX_NOEXCEPT
  714. { return iterator(this->_M_impl._M_start._M_p, 0); }
  715. const_iterator
  716. begin() const _GLIBCXX_NOEXCEPT
  717. { return const_iterator(this->_M_impl._M_start._M_p, 0); }
  718. iterator
  719. end() _GLIBCXX_NOEXCEPT
  720. { return this->_M_impl._M_finish; }
  721. const_iterator
  722. end() const _GLIBCXX_NOEXCEPT
  723. { return this->_M_impl._M_finish; }
  724. reverse_iterator
  725. rbegin() _GLIBCXX_NOEXCEPT
  726. { return reverse_iterator(end()); }
  727. const_reverse_iterator
  728. rbegin() const _GLIBCXX_NOEXCEPT
  729. { return const_reverse_iterator(end()); }
  730. reverse_iterator
  731. rend() _GLIBCXX_NOEXCEPT
  732. { return reverse_iterator(begin()); }
  733. const_reverse_iterator
  734. rend() const _GLIBCXX_NOEXCEPT
  735. { return const_reverse_iterator(begin()); }
  736. #if __cplusplus >= 201103L
  737. const_iterator
  738. cbegin() const noexcept
  739. { return const_iterator(this->_M_impl._M_start._M_p, 0); }
  740. const_iterator
  741. cend() const noexcept
  742. { return this->_M_impl._M_finish; }
  743. const_reverse_iterator
  744. crbegin() const noexcept
  745. { return const_reverse_iterator(end()); }
  746. const_reverse_iterator
  747. crend() const noexcept
  748. { return const_reverse_iterator(begin()); }
  749. #endif
  750. size_type
  751. size() const _GLIBCXX_NOEXCEPT
  752. { return size_type(end() - begin()); }
  753. size_type
  754. max_size() const _GLIBCXX_NOEXCEPT
  755. {
  756. const size_type __isize =
  757. __gnu_cxx::__numeric_traits<difference_type>::__max
  758. - int(_S_word_bit) + 1;
  759. const size_type __asize
  760. = _Bit_alloc_traits::max_size(_M_get_Bit_allocator());
  761. return (__asize <= __isize / int(_S_word_bit)
  762. ? __asize * int(_S_word_bit) : __isize);
  763. }
  764. size_type
  765. capacity() const _GLIBCXX_NOEXCEPT
  766. { return size_type(const_iterator(this->_M_impl._M_end_addr(), 0)
  767. - begin()); }
  768. _GLIBCXX_NODISCARD bool
  769. empty() const _GLIBCXX_NOEXCEPT
  770. { return begin() == end(); }
  771. reference
  772. operator[](size_type __n)
  773. { return begin()[__n]; }
  774. const_reference
  775. operator[](size_type __n) const
  776. { return begin()[__n]; }
  777. protected:
  778. void
  779. _M_range_check(size_type __n) const
  780. {
  781. if (__n >= this->size())
  782. __throw_out_of_range_fmt(__N("vector<bool>::_M_range_check: __n "
  783. "(which is %zu) >= this->size() "
  784. "(which is %zu)"),
  785. __n, this->size());
  786. }
  787. public:
  788. reference
  789. at(size_type __n)
  790. { _M_range_check(__n); return (*this)[__n]; }
  791. const_reference
  792. at(size_type __n) const
  793. { _M_range_check(__n); return (*this)[__n]; }
  794. void
  795. reserve(size_type __n)
  796. {
  797. if (__n > max_size())
  798. __throw_length_error(__N("vector::reserve"));
  799. if (capacity() < __n)
  800. _M_reallocate(__n);
  801. }
  802. reference
  803. front()
  804. { return *begin(); }
  805. const_reference
  806. front() const
  807. { return *begin(); }
  808. reference
  809. back()
  810. { return *(end() - 1); }
  811. const_reference
  812. back() const
  813. { return *(end() - 1); }
  814. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  815. // DR 464. Suggestion for new member functions in standard containers.
  816. // N.B. DR 464 says nothing about vector<bool> but we need something
  817. // here due to the way we are implementing DR 464 in the debug-mode
  818. // vector class.
  819. void
  820. data() _GLIBCXX_NOEXCEPT { }
  821. void
  822. push_back(bool __x)
  823. {
  824. if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_addr())
  825. *this->_M_impl._M_finish++ = __x;
  826. else
  827. _M_insert_aux(end(), __x);
  828. }
  829. void
  830. swap(vector& __x) _GLIBCXX_NOEXCEPT
  831. {
  832. #if __cplusplus >= 201103L
  833. __glibcxx_assert(_Bit_alloc_traits::propagate_on_container_swap::value
  834. || _M_get_Bit_allocator() == __x._M_get_Bit_allocator());
  835. #endif
  836. this->_M_impl._M_swap_data(__x._M_impl);
  837. _Bit_alloc_traits::_S_on_swap(_M_get_Bit_allocator(),
  838. __x._M_get_Bit_allocator());
  839. }
  840. // [23.2.5]/1, third-to-last entry in synopsis listing
  841. static void
  842. swap(reference __x, reference __y) _GLIBCXX_NOEXCEPT
  843. {
  844. bool __tmp = __x;
  845. __x = __y;
  846. __y = __tmp;
  847. }
  848. iterator
  849. #if __cplusplus >= 201103L
  850. insert(const_iterator __position, const bool& __x = bool())
  851. #else
  852. insert(iterator __position, const bool& __x = bool())
  853. #endif
  854. {
  855. const difference_type __n = __position - begin();
  856. if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_addr()
  857. && __position == end())
  858. *this->_M_impl._M_finish++ = __x;
  859. else
  860. _M_insert_aux(__position._M_const_cast(), __x);
  861. return begin() + __n;
  862. }
  863. #if __cplusplus >= 201103L
  864. template<typename _InputIterator,
  865. typename = std::_RequireInputIter<_InputIterator>>
  866. iterator
  867. insert(const_iterator __position,
  868. _InputIterator __first, _InputIterator __last)
  869. {
  870. difference_type __offset = __position - cbegin();
  871. _M_insert_range(__position._M_const_cast(),
  872. __first, __last,
  873. std::__iterator_category(__first));
  874. return begin() + __offset;
  875. }
  876. #else
  877. template<typename _InputIterator>
  878. void
  879. insert(iterator __position,
  880. _InputIterator __first, _InputIterator __last)
  881. {
  882. // Check whether it's an integral type. If so, it's not an iterator.
  883. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  884. _M_insert_dispatch(__position, __first, __last, _Integral());
  885. }
  886. #endif
  887. #if __cplusplus >= 201103L
  888. iterator
  889. insert(const_iterator __position, size_type __n, const bool& __x)
  890. {
  891. difference_type __offset = __position - cbegin();
  892. _M_fill_insert(__position._M_const_cast(), __n, __x);
  893. return begin() + __offset;
  894. }
  895. #else
  896. void
  897. insert(iterator __position, size_type __n, const bool& __x)
  898. { _M_fill_insert(__position, __n, __x); }
  899. #endif
  900. #if __cplusplus >= 201103L
  901. iterator
  902. insert(const_iterator __p, initializer_list<bool> __l)
  903. { return this->insert(__p, __l.begin(), __l.end()); }
  904. #endif
  905. void
  906. pop_back()
  907. { --this->_M_impl._M_finish; }
  908. iterator
  909. #if __cplusplus >= 201103L
  910. erase(const_iterator __position)
  911. #else
  912. erase(iterator __position)
  913. #endif
  914. { return _M_erase(__position._M_const_cast()); }
  915. iterator
  916. #if __cplusplus >= 201103L
  917. erase(const_iterator __first, const_iterator __last)
  918. #else
  919. erase(iterator __first, iterator __last)
  920. #endif
  921. { return _M_erase(__first._M_const_cast(), __last._M_const_cast()); }
  922. void
  923. resize(size_type __new_size, bool __x = bool())
  924. {
  925. if (__new_size < size())
  926. _M_erase_at_end(begin() + difference_type(__new_size));
  927. else
  928. insert(end(), __new_size - size(), __x);
  929. }
  930. #if __cplusplus >= 201103L
  931. void
  932. shrink_to_fit()
  933. { _M_shrink_to_fit(); }
  934. #endif
  935. void
  936. flip() _GLIBCXX_NOEXCEPT
  937. {
  938. _Bit_type * const __end = this->_M_impl._M_end_addr();
  939. for (_Bit_type * __p = this->_M_impl._M_start._M_p; __p != __end; ++__p)
  940. *__p = ~*__p;
  941. }
  942. void
  943. clear() _GLIBCXX_NOEXCEPT
  944. { _M_erase_at_end(begin()); }
  945. #if __cplusplus >= 201103L
  946. template<typename... _Args>
  947. #if __cplusplus > 201402L
  948. reference
  949. #else
  950. void
  951. #endif
  952. emplace_back(_Args&&... __args)
  953. {
  954. push_back(bool(__args...));
  955. #if __cplusplus > 201402L
  956. return back();
  957. #endif
  958. }
  959. template<typename... _Args>
  960. iterator
  961. emplace(const_iterator __pos, _Args&&... __args)
  962. { return insert(__pos, bool(__args...)); }
  963. #endif
  964. protected:
  965. // Precondition: __first._M_offset == 0 && __result._M_offset == 0.
  966. iterator
  967. _M_copy_aligned(const_iterator __first, const_iterator __last,
  968. iterator __result)
  969. {
  970. _Bit_type* __q = std::copy(__first._M_p, __last._M_p, __result._M_p);
  971. return std::copy(const_iterator(__last._M_p, 0), __last,
  972. iterator(__q, 0));
  973. }
  974. void
  975. _M_initialize(size_type __n)
  976. {
  977. if (__n)
  978. {
  979. _Bit_pointer __q = this->_M_allocate(__n);
  980. this->_M_impl._M_end_of_storage = __q + _S_nword(__n);
  981. iterator __start = iterator(std::__addressof(*__q), 0);
  982. this->_M_impl._M_start = __start;
  983. this->_M_impl._M_finish = __start + difference_type(__n);
  984. }
  985. }
  986. void
  987. _M_initialize_value(bool __x)
  988. {
  989. if (_Bit_type* __p = this->_M_impl._M_start._M_p)
  990. __builtin_memset(__p, __x ? ~0 : 0,
  991. (this->_M_impl._M_end_addr() - __p)
  992. * sizeof(_Bit_type));
  993. }
  994. void
  995. _M_reallocate(size_type __n);
  996. #if __cplusplus >= 201103L
  997. bool
  998. _M_shrink_to_fit();
  999. #endif
  1000. #if __cplusplus < 201103L
  1001. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1002. // 438. Ambiguity in the "do the right thing" clause
  1003. template<typename _Integer>
  1004. void
  1005. _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
  1006. {
  1007. _M_initialize(static_cast<size_type>(__n));
  1008. _M_initialize_value(__x);
  1009. }
  1010. template<typename _InputIterator>
  1011. void
  1012. _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
  1013. __false_type)
  1014. { _M_initialize_range(__first, __last,
  1015. std::__iterator_category(__first)); }
  1016. #endif
  1017. template<typename _InputIterator>
  1018. void
  1019. _M_initialize_range(_InputIterator __first, _InputIterator __last,
  1020. std::input_iterator_tag)
  1021. {
  1022. for (; __first != __last; ++__first)
  1023. push_back(*__first);
  1024. }
  1025. template<typename _ForwardIterator>
  1026. void
  1027. _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,
  1028. std::forward_iterator_tag)
  1029. {
  1030. const size_type __n = std::distance(__first, __last);
  1031. _M_initialize(__n);
  1032. std::copy(__first, __last, begin());
  1033. }
  1034. #if __cplusplus < 201103L
  1035. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1036. // 438. Ambiguity in the "do the right thing" clause
  1037. template<typename _Integer>
  1038. void
  1039. _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
  1040. { _M_fill_assign(__n, __val); }
  1041. template<class _InputIterator>
  1042. void
  1043. _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
  1044. __false_type)
  1045. { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
  1046. #endif
  1047. void
  1048. _M_fill_assign(size_t __n, bool __x)
  1049. {
  1050. if (__n > size())
  1051. {
  1052. _M_initialize_value(__x);
  1053. insert(end(), __n - size(), __x);
  1054. }
  1055. else
  1056. {
  1057. _M_erase_at_end(begin() + __n);
  1058. _M_initialize_value(__x);
  1059. }
  1060. }
  1061. template<typename _InputIterator>
  1062. void
  1063. _M_assign_aux(_InputIterator __first, _InputIterator __last,
  1064. std::input_iterator_tag)
  1065. {
  1066. iterator __cur = begin();
  1067. for (; __first != __last && __cur != end(); ++__cur, (void)++__first)
  1068. *__cur = *__first;
  1069. if (__first == __last)
  1070. _M_erase_at_end(__cur);
  1071. else
  1072. insert(end(), __first, __last);
  1073. }
  1074. template<typename _ForwardIterator>
  1075. void
  1076. _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
  1077. std::forward_iterator_tag)
  1078. {
  1079. const size_type __len = std::distance(__first, __last);
  1080. if (__len < size())
  1081. _M_erase_at_end(std::copy(__first, __last, begin()));
  1082. else
  1083. {
  1084. _ForwardIterator __mid = __first;
  1085. std::advance(__mid, size());
  1086. std::copy(__first, __mid, begin());
  1087. insert(end(), __mid, __last);
  1088. }
  1089. }
  1090. #if __cplusplus < 201103L
  1091. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1092. // 438. Ambiguity in the "do the right thing" clause
  1093. template<typename _Integer>
  1094. void
  1095. _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
  1096. __true_type)
  1097. { _M_fill_insert(__pos, __n, __x); }
  1098. template<typename _InputIterator>
  1099. void
  1100. _M_insert_dispatch(iterator __pos,
  1101. _InputIterator __first, _InputIterator __last,
  1102. __false_type)
  1103. { _M_insert_range(__pos, __first, __last,
  1104. std::__iterator_category(__first)); }
  1105. #endif
  1106. void
  1107. _M_fill_insert(iterator __position, size_type __n, bool __x);
  1108. template<typename _InputIterator>
  1109. void
  1110. _M_insert_range(iterator __pos, _InputIterator __first,
  1111. _InputIterator __last, std::input_iterator_tag)
  1112. {
  1113. for (; __first != __last; ++__first)
  1114. {
  1115. __pos = insert(__pos, *__first);
  1116. ++__pos;
  1117. }
  1118. }
  1119. template<typename _ForwardIterator>
  1120. void
  1121. _M_insert_range(iterator __position, _ForwardIterator __first,
  1122. _ForwardIterator __last, std::forward_iterator_tag);
  1123. void
  1124. _M_insert_aux(iterator __position, bool __x);
  1125. size_type
  1126. _M_check_len(size_type __n, const char* __s) const
  1127. {
  1128. if (max_size() - size() < __n)
  1129. __throw_length_error(__N(__s));
  1130. const size_type __len = size() + std::max(size(), __n);
  1131. return (__len < size() || __len > max_size()) ? max_size() : __len;
  1132. }
  1133. void
  1134. _M_erase_at_end(iterator __pos)
  1135. { this->_M_impl._M_finish = __pos; }
  1136. iterator
  1137. _M_erase(iterator __pos);
  1138. iterator
  1139. _M_erase(iterator __first, iterator __last);
  1140. };
  1141. _GLIBCXX_END_NAMESPACE_CONTAINER
  1142. inline void
  1143. __fill_bvector(_GLIBCXX_STD_C::_Bit_type * __v,
  1144. unsigned int __first, unsigned int __last, bool __x)
  1145. {
  1146. using _GLIBCXX_STD_C::_Bit_type;
  1147. using _GLIBCXX_STD_C::_S_word_bit;
  1148. const _Bit_type __fmask = ~0ul << __first;
  1149. const _Bit_type __lmask = ~0ul >> (_S_word_bit - __last);
  1150. const _Bit_type __mask = __fmask & __lmask;
  1151. if (__x)
  1152. *__v |= __mask;
  1153. else
  1154. *__v &= ~__mask;
  1155. }
  1156. inline void
  1157. __fill_a1(_GLIBCXX_STD_C::_Bit_iterator __first,
  1158. _GLIBCXX_STD_C::_Bit_iterator __last, const bool& __x)
  1159. {
  1160. using _GLIBCXX_STD_C::_Bit_type;
  1161. using _GLIBCXX_STD_C::_S_word_bit;
  1162. if (__first._M_p != __last._M_p)
  1163. {
  1164. _Bit_type* __first_p = __first._M_p;
  1165. if (__first._M_offset != 0)
  1166. __fill_bvector(__first_p++, __first._M_offset, _S_word_bit, __x);
  1167. __builtin_memset(__first_p, __x ? ~0 : 0,
  1168. (__last._M_p - __first_p) * sizeof(_Bit_type));
  1169. if (__last._M_offset != 0)
  1170. __fill_bvector(__last._M_p, 0, __last._M_offset, __x);
  1171. }
  1172. else if (__first._M_offset != __last._M_offset)
  1173. __fill_bvector(__first._M_p, __first._M_offset, __last._M_offset, __x);
  1174. }
  1175. #if __cplusplus >= 201103L
  1176. // DR 1182.
  1177. /// std::hash specialization for vector<bool>.
  1178. template<typename _Alloc>
  1179. struct hash<_GLIBCXX_STD_C::vector<bool, _Alloc>>
  1180. : public __hash_base<size_t, _GLIBCXX_STD_C::vector<bool, _Alloc>>
  1181. {
  1182. size_t
  1183. operator()(const _GLIBCXX_STD_C::vector<bool, _Alloc>&) const noexcept;
  1184. };
  1185. #endif // C++11
  1186. _GLIBCXX_END_NAMESPACE_VERSION
  1187. } // namespace std
  1188. #endif