__split_buffer 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. // -*- C++ -*-
  2. #ifndef _LIBCPP_SPLIT_BUFFER
  3. #define _LIBCPP_SPLIT_BUFFER
  4. #include <__config>
  5. #include <type_traits>
  6. #include <algorithm>
  7. #include <__undef_min_max>
  8. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  9. #pragma GCC system_header
  10. #endif
  11. _LIBCPP_BEGIN_NAMESPACE_STD
  12. template <bool>
  13. class __split_buffer_common
  14. {
  15. protected:
  16. void __throw_length_error() const;
  17. void __throw_out_of_range() const;
  18. };
  19. template <class _Tp, class _Allocator = allocator<_Tp> >
  20. struct __split_buffer
  21. : private __split_buffer_common<true>
  22. {
  23. private:
  24. __split_buffer(const __split_buffer&);
  25. __split_buffer& operator=(const __split_buffer&);
  26. public:
  27. typedef _Tp value_type;
  28. typedef _Allocator allocator_type;
  29. typedef typename remove_reference<allocator_type>::type __alloc_rr;
  30. typedef allocator_traits<__alloc_rr> __alloc_traits;
  31. typedef value_type& reference;
  32. typedef const value_type& const_reference;
  33. typedef typename __alloc_traits::size_type size_type;
  34. typedef typename __alloc_traits::difference_type difference_type;
  35. typedef typename __alloc_traits::pointer pointer;
  36. typedef typename __alloc_traits::const_pointer const_pointer;
  37. typedef pointer iterator;
  38. typedef const_pointer const_iterator;
  39. pointer __first_;
  40. pointer __begin_;
  41. pointer __end_;
  42. __compressed_pair<pointer, allocator_type> __end_cap_;
  43. typedef typename add_lvalue_reference<allocator_type>::type __alloc_ref;
  44. typedef typename add_lvalue_reference<allocator_type>::type __alloc_const_ref;
  45. _LIBCPP_INLINE_VISIBILITY __alloc_rr& __alloc() _NOEXCEPT {return __end_cap_.second();}
  46. _LIBCPP_INLINE_VISIBILITY const __alloc_rr& __alloc() const _NOEXCEPT {return __end_cap_.second();}
  47. _LIBCPP_INLINE_VISIBILITY pointer& __end_cap() _NOEXCEPT {return __end_cap_.first();}
  48. _LIBCPP_INLINE_VISIBILITY const pointer& __end_cap() const _NOEXCEPT {return __end_cap_.first();}
  49. _LIBCPP_INLINE_VISIBILITY
  50. __split_buffer()
  51. _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
  52. _LIBCPP_INLINE_VISIBILITY
  53. explicit __split_buffer(__alloc_rr& __a);
  54. _LIBCPP_INLINE_VISIBILITY
  55. explicit __split_buffer(const __alloc_rr& __a);
  56. __split_buffer(size_type __cap, size_type __start, __alloc_rr& __a);
  57. ~__split_buffer();
  58. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  59. __split_buffer(__split_buffer&& __c)
  60. _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
  61. __split_buffer(__split_buffer&& __c, const __alloc_rr& __a);
  62. __split_buffer& operator=(__split_buffer&& __c)
  63. _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
  64. is_nothrow_move_assignable<allocator_type>::value) ||
  65. !__alloc_traits::propagate_on_container_move_assignment::value);
  66. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  67. _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT {return __begin_;}
  68. _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT {return __begin_;}
  69. _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT {return __end_;}
  70. _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT {return __end_;}
  71. _LIBCPP_INLINE_VISIBILITY
  72. void clear() _NOEXCEPT
  73. {__destruct_at_end(__begin_);}
  74. _LIBCPP_INLINE_VISIBILITY size_type size() const {return static_cast<size_type>(__end_ - __begin_);}
  75. _LIBCPP_INLINE_VISIBILITY bool empty() const {return __end_ == __begin_;}
  76. _LIBCPP_INLINE_VISIBILITY size_type capacity() const {return static_cast<size_type>(__end_cap() - __first_);}
  77. _LIBCPP_INLINE_VISIBILITY size_type __front_spare() const {return static_cast<size_type>(__begin_ - __first_);}
  78. _LIBCPP_INLINE_VISIBILITY size_type __back_spare() const {return static_cast<size_type>(__end_cap() - __end_);}
  79. _LIBCPP_INLINE_VISIBILITY reference front() {return *__begin_;}
  80. _LIBCPP_INLINE_VISIBILITY const_reference front() const {return *__begin_;}
  81. _LIBCPP_INLINE_VISIBILITY reference back() {return *(__end_ - 1);}
  82. _LIBCPP_INLINE_VISIBILITY const_reference back() const {return *(__end_ - 1);}
  83. void reserve(size_type __n);
  84. void shrink_to_fit() _NOEXCEPT;
  85. void push_front(const_reference __x);
  86. _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
  87. #if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
  88. void push_front(value_type&& __x);
  89. void push_back(value_type&& __x);
  90. #if !defined(_LIBCPP_HAS_NO_VARIADICS)
  91. template <class... _Args>
  92. void emplace_back(_Args&&... __args);
  93. #endif // !defined(_LIBCPP_HAS_NO_VARIADICS)
  94. #endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
  95. _LIBCPP_INLINE_VISIBILITY void pop_front() {__destruct_at_begin(__begin_+1);}
  96. _LIBCPP_INLINE_VISIBILITY void pop_back() {__destruct_at_end(__end_-1);}
  97. void __construct_at_end(size_type __n);
  98. void __construct_at_end(size_type __n, const_reference __x);
  99. template <class _InputIter>
  100. typename enable_if
  101. <
  102. __is_input_iterator<_InputIter>::value &&
  103. !__is_forward_iterator<_InputIter>::value,
  104. void
  105. >::type
  106. __construct_at_end(_InputIter __first, _InputIter __last);
  107. template <class _ForwardIterator>
  108. typename enable_if
  109. <
  110. __is_forward_iterator<_ForwardIterator>::value,
  111. void
  112. >::type
  113. __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
  114. _LIBCPP_INLINE_VISIBILITY void __destruct_at_begin(pointer __new_begin)
  115. {__destruct_at_begin(__new_begin, is_trivially_destructible<value_type>());}
  116. _LIBCPP_INLINE_VISIBILITY
  117. void __destruct_at_begin(pointer __new_begin, false_type);
  118. _LIBCPP_INLINE_VISIBILITY
  119. void __destruct_at_begin(pointer __new_begin, true_type);
  120. _LIBCPP_INLINE_VISIBILITY
  121. void __destruct_at_end(pointer __new_last) _NOEXCEPT
  122. {__destruct_at_end(__new_last, false_type());}
  123. _LIBCPP_INLINE_VISIBILITY
  124. void __destruct_at_end(pointer __new_last, false_type) _NOEXCEPT;
  125. _LIBCPP_INLINE_VISIBILITY
  126. void __destruct_at_end(pointer __new_last, true_type) _NOEXCEPT;
  127. void swap(__split_buffer& __x)
  128. _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
  129. __is_nothrow_swappable<__alloc_rr>::value);
  130. bool __invariants() const;
  131. private:
  132. _LIBCPP_INLINE_VISIBILITY
  133. void __move_assign_alloc(__split_buffer& __c, true_type)
  134. _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
  135. {
  136. __alloc() = _VSTD::move(__c.__alloc());
  137. }
  138. _LIBCPP_INLINE_VISIBILITY
  139. void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT
  140. {}
  141. };
  142. template <class _Tp, class _Allocator>
  143. bool
  144. __split_buffer<_Tp, _Allocator>::__invariants() const
  145. {
  146. if (__first_ == nullptr)
  147. {
  148. if (__begin_ != nullptr)
  149. return false;
  150. if (__end_ != nullptr)
  151. return false;
  152. if (__end_cap() != nullptr)
  153. return false;
  154. }
  155. else
  156. {
  157. if (__begin_ < __first_)
  158. return false;
  159. if (__end_ < __begin_)
  160. return false;
  161. if (__end_cap() < __end_)
  162. return false;
  163. }
  164. return true;
  165. }
  166. // Default constructs __n objects starting at __end_
  167. // throws if construction throws
  168. // Precondition: __n > 0
  169. // Precondition: size() + __n <= capacity()
  170. // Postcondition: size() == size() + __n
  171. template <class _Tp, class _Allocator>
  172. void
  173. __split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n)
  174. {
  175. __alloc_rr& __a = this->__alloc();
  176. do
  177. {
  178. __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
  179. ++this->__end_;
  180. --__n;
  181. } while (__n > 0);
  182. }
  183. // Copy constructs __n objects starting at __end_ from __x
  184. // throws if construction throws
  185. // Precondition: __n > 0
  186. // Precondition: size() + __n <= capacity()
  187. // Postcondition: size() == old size() + __n
  188. // Postcondition: [i] == __x for all i in [size() - __n, __n)
  189. template <class _Tp, class _Allocator>
  190. void
  191. __split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
  192. {
  193. __alloc_rr& __a = this->__alloc();
  194. do
  195. {
  196. __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x);
  197. ++this->__end_;
  198. --__n;
  199. } while (__n > 0);
  200. }
  201. template <class _Tp, class _Allocator>
  202. template <class _InputIter>
  203. typename enable_if
  204. <
  205. __is_input_iterator<_InputIter>::value &&
  206. !__is_forward_iterator<_InputIter>::value,
  207. void
  208. >::type
  209. __split_buffer<_Tp, _Allocator>::__construct_at_end(_InputIter __first, _InputIter __last)
  210. {
  211. __alloc_rr& __a = this->__alloc();
  212. for (; __first != __last; ++__first)
  213. {
  214. if (__end_ == __end_cap())
  215. {
  216. size_type __old_cap = __end_cap() - __first_;
  217. size_type __new_cap = _VSTD::max<size_type>(2 * __old_cap, 8);
  218. __split_buffer __buf(__new_cap, 0, __a);
  219. for (pointer __p = __begin_; __p != __end_; ++__p, ++__buf.__end_)
  220. __alloc_traits::construct(__buf.__alloc(),
  221. _VSTD::__to_raw_pointer(__buf.__end_), _VSTD::move(*__p));
  222. swap(__buf);
  223. }
  224. __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
  225. ++this->__end_;
  226. }
  227. }
  228. template <class _Tp, class _Allocator>
  229. template <class _ForwardIterator>
  230. typename enable_if
  231. <
  232. __is_forward_iterator<_ForwardIterator>::value,
  233. void
  234. >::type
  235. __split_buffer<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
  236. {
  237. __alloc_rr& __a = this->__alloc();
  238. for (; __first != __last; ++__first)
  239. {
  240. __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), *__first);
  241. ++this->__end_;
  242. }
  243. }
  244. template <class _Tp, class _Allocator>
  245. inline
  246. void
  247. __split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type)
  248. {
  249. while (__begin_ != __new_begin)
  250. __alloc_traits::destroy(__alloc(), __to_raw_pointer(__begin_++));
  251. }
  252. template <class _Tp, class _Allocator>
  253. inline
  254. void
  255. __split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, true_type)
  256. {
  257. __begin_ = __new_begin;
  258. }
  259. template <class _Tp, class _Allocator>
  260. inline _LIBCPP_INLINE_VISIBILITY
  261. void
  262. __split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT
  263. {
  264. while (__new_last != __end_)
  265. __alloc_traits::destroy(__alloc(), __to_raw_pointer(--__end_));
  266. }
  267. template <class _Tp, class _Allocator>
  268. inline _LIBCPP_INLINE_VISIBILITY
  269. void
  270. __split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, true_type) _NOEXCEPT
  271. {
  272. __end_ = __new_last;
  273. }
  274. template <class _Tp, class _Allocator>
  275. __split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a)
  276. : __end_cap_(nullptr, __a)
  277. {
  278. __first_ = __cap != 0 ? __alloc_traits::allocate(__alloc(), __cap) : nullptr;
  279. __begin_ = __end_ = __first_ + __start;
  280. __end_cap() = __first_ + __cap;
  281. }
  282. template <class _Tp, class _Allocator>
  283. inline
  284. __split_buffer<_Tp, _Allocator>::__split_buffer()
  285. _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
  286. : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr)
  287. {
  288. }
  289. template <class _Tp, class _Allocator>
  290. inline
  291. __split_buffer<_Tp, _Allocator>::__split_buffer(__alloc_rr& __a)
  292. : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a)
  293. {
  294. }
  295. template <class _Tp, class _Allocator>
  296. inline
  297. __split_buffer<_Tp, _Allocator>::__split_buffer(const __alloc_rr& __a)
  298. : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a)
  299. {
  300. }
  301. template <class _Tp, class _Allocator>
  302. __split_buffer<_Tp, _Allocator>::~__split_buffer()
  303. {
  304. clear();
  305. if (__first_)
  306. __alloc_traits::deallocate(__alloc(), __first_, capacity());
  307. }
  308. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  309. template <class _Tp, class _Allocator>
  310. __split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c)
  311. _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
  312. : __first_(_VSTD::move(__c.__first_)),
  313. __begin_(_VSTD::move(__c.__begin_)),
  314. __end_(_VSTD::move(__c.__end_)),
  315. __end_cap_(_VSTD::move(__c.__end_cap_))
  316. {
  317. __c.__first_ = nullptr;
  318. __c.__begin_ = nullptr;
  319. __c.__end_ = nullptr;
  320. __c.__end_cap() = nullptr;
  321. }
  322. template <class _Tp, class _Allocator>
  323. __split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a)
  324. : __end_cap_(__a)
  325. {
  326. if (__a == __c.__alloc())
  327. {
  328. __first_ = __c.__first_;
  329. __begin_ = __c.__begin_;
  330. __end_ = __c.__end_;
  331. __end_cap() = __c.__end_cap();
  332. __c.__first_ = nullptr;
  333. __c.__begin_ = nullptr;
  334. __c.__end_ = nullptr;
  335. __c.__end_cap() = nullptr;
  336. }
  337. else
  338. {
  339. size_type __cap = __c.size();
  340. __first_ = __alloc_traits::allocate(__alloc(), __cap);
  341. __begin_ = __end_ = __first_;
  342. __end_cap() = __first_ + __cap;
  343. typedef move_iterator<iterator> _Ip;
  344. __construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
  345. }
  346. }
  347. template <class _Tp, class _Allocator>
  348. __split_buffer<_Tp, _Allocator>&
  349. __split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c)
  350. _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
  351. is_nothrow_move_assignable<allocator_type>::value) ||
  352. !__alloc_traits::propagate_on_container_move_assignment::value)
  353. {
  354. clear();
  355. shrink_to_fit();
  356. __first_ = __c.__first_;
  357. __begin_ = __c.__begin_;
  358. __end_ = __c.__end_;
  359. __end_cap() = __c.__end_cap();
  360. __move_assign_alloc(__c,
  361. integral_constant<bool,
  362. __alloc_traits::propagate_on_container_move_assignment::value>());
  363. __c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
  364. return *this;
  365. }
  366. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  367. template <class _Tp, class _Allocator>
  368. void
  369. __split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x)
  370. _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
  371. __is_nothrow_swappable<__alloc_rr>::value)
  372. {
  373. _VSTD::swap(__first_, __x.__first_);
  374. _VSTD::swap(__begin_, __x.__begin_);
  375. _VSTD::swap(__end_, __x.__end_);
  376. _VSTD::swap(__end_cap(), __x.__end_cap());
  377. __swap_allocator(__alloc(), __x.__alloc());
  378. }
  379. template <class _Tp, class _Allocator>
  380. void
  381. __split_buffer<_Tp, _Allocator>::reserve(size_type __n)
  382. {
  383. if (__n < capacity())
  384. {
  385. __split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc());
  386. __t.__construct_at_end(move_iterator<pointer>(__begin_),
  387. move_iterator<pointer>(__end_));
  388. _VSTD::swap(__first_, __t.__first_);
  389. _VSTD::swap(__begin_, __t.__begin_);
  390. _VSTD::swap(__end_, __t.__end_);
  391. _VSTD::swap(__end_cap(), __t.__end_cap());
  392. }
  393. }
  394. template <class _Tp, class _Allocator>
  395. void
  396. __split_buffer<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
  397. {
  398. if (capacity() > size())
  399. {
  400. #ifndef _LIBCPP_NO_EXCEPTIONS
  401. try
  402. {
  403. #endif // _LIBCPP_NO_EXCEPTIONS
  404. __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc());
  405. __t.__construct_at_end(move_iterator<pointer>(__begin_),
  406. move_iterator<pointer>(__end_));
  407. __t.__end_ = __t.__begin_ + (__end_ - __begin_);
  408. _VSTD::swap(__first_, __t.__first_);
  409. _VSTD::swap(__begin_, __t.__begin_);
  410. _VSTD::swap(__end_, __t.__end_);
  411. _VSTD::swap(__end_cap(), __t.__end_cap());
  412. #ifndef _LIBCPP_NO_EXCEPTIONS
  413. }
  414. catch (...)
  415. {
  416. }
  417. #endif // _LIBCPP_NO_EXCEPTIONS
  418. }
  419. }
  420. template <class _Tp, class _Allocator>
  421. void
  422. __split_buffer<_Tp, _Allocator>::push_front(const_reference __x)
  423. {
  424. if (__begin_ == __first_)
  425. {
  426. if (__end_ < __end_cap())
  427. {
  428. difference_type __d = __end_cap() - __end_;
  429. __d = (__d + 1) / 2;
  430. __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
  431. __end_ += __d;
  432. }
  433. else
  434. {
  435. size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
  436. __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
  437. __t.__construct_at_end(move_iterator<pointer>(__begin_),
  438. move_iterator<pointer>(__end_));
  439. _VSTD::swap(__first_, __t.__first_);
  440. _VSTD::swap(__begin_, __t.__begin_);
  441. _VSTD::swap(__end_, __t.__end_);
  442. _VSTD::swap(__end_cap(), __t.__end_cap());
  443. }
  444. }
  445. __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__begin_-1), __x);
  446. --__begin_;
  447. }
  448. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  449. template <class _Tp, class _Allocator>
  450. void
  451. __split_buffer<_Tp, _Allocator>::push_front(value_type&& __x)
  452. {
  453. if (__begin_ == __first_)
  454. {
  455. if (__end_ < __end_cap())
  456. {
  457. difference_type __d = __end_cap() - __end_;
  458. __d = (__d + 1) / 2;
  459. __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
  460. __end_ += __d;
  461. }
  462. else
  463. {
  464. size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
  465. __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
  466. __t.__construct_at_end(move_iterator<pointer>(__begin_),
  467. move_iterator<pointer>(__end_));
  468. _VSTD::swap(__first_, __t.__first_);
  469. _VSTD::swap(__begin_, __t.__begin_);
  470. _VSTD::swap(__end_, __t.__end_);
  471. _VSTD::swap(__end_cap(), __t.__end_cap());
  472. }
  473. }
  474. __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__begin_-1),
  475. _VSTD::move(__x));
  476. --__begin_;
  477. }
  478. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  479. template <class _Tp, class _Allocator>
  480. inline _LIBCPP_INLINE_VISIBILITY
  481. void
  482. __split_buffer<_Tp, _Allocator>::push_back(const_reference __x)
  483. {
  484. if (__end_ == __end_cap())
  485. {
  486. if (__begin_ > __first_)
  487. {
  488. difference_type __d = __begin_ - __first_;
  489. __d = (__d + 1) / 2;
  490. __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
  491. __begin_ -= __d;
  492. }
  493. else
  494. {
  495. size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
  496. __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
  497. __t.__construct_at_end(move_iterator<pointer>(__begin_),
  498. move_iterator<pointer>(__end_));
  499. _VSTD::swap(__first_, __t.__first_);
  500. _VSTD::swap(__begin_, __t.__begin_);
  501. _VSTD::swap(__end_, __t.__end_);
  502. _VSTD::swap(__end_cap(), __t.__end_cap());
  503. }
  504. }
  505. __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__end_), __x);
  506. ++__end_;
  507. }
  508. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  509. template <class _Tp, class _Allocator>
  510. void
  511. __split_buffer<_Tp, _Allocator>::push_back(value_type&& __x)
  512. {
  513. if (__end_ == __end_cap())
  514. {
  515. if (__begin_ > __first_)
  516. {
  517. difference_type __d = __begin_ - __first_;
  518. __d = (__d + 1) / 2;
  519. __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
  520. __begin_ -= __d;
  521. }
  522. else
  523. {
  524. size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
  525. __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
  526. __t.__construct_at_end(move_iterator<pointer>(__begin_),
  527. move_iterator<pointer>(__end_));
  528. _VSTD::swap(__first_, __t.__first_);
  529. _VSTD::swap(__begin_, __t.__begin_);
  530. _VSTD::swap(__end_, __t.__end_);
  531. _VSTD::swap(__end_cap(), __t.__end_cap());
  532. }
  533. }
  534. __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__end_),
  535. _VSTD::move(__x));
  536. ++__end_;
  537. }
  538. #ifndef _LIBCPP_HAS_NO_VARIADICS
  539. template <class _Tp, class _Allocator>
  540. template <class... _Args>
  541. void
  542. __split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args)
  543. {
  544. if (__end_ == __end_cap())
  545. {
  546. if (__begin_ > __first_)
  547. {
  548. difference_type __d = __begin_ - __first_;
  549. __d = (__d + 1) / 2;
  550. __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
  551. __begin_ -= __d;
  552. }
  553. else
  554. {
  555. size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
  556. __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
  557. __t.__construct_at_end(move_iterator<pointer>(__begin_),
  558. move_iterator<pointer>(__end_));
  559. _VSTD::swap(__first_, __t.__first_);
  560. _VSTD::swap(__begin_, __t.__begin_);
  561. _VSTD::swap(__end_, __t.__end_);
  562. _VSTD::swap(__end_cap(), __t.__end_cap());
  563. }
  564. }
  565. __alloc_traits::construct(__alloc(), _VSTD::__to_raw_pointer(__end_),
  566. _VSTD::forward<_Args>(__args)...);
  567. ++__end_;
  568. }
  569. #endif // _LIBCPP_HAS_NO_VARIADICS
  570. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  571. template <class _Tp, class _Allocator>
  572. inline _LIBCPP_INLINE_VISIBILITY
  573. void
  574. swap(__split_buffer<_Tp, _Allocator>& __x, __split_buffer<_Tp, _Allocator>& __y)
  575. _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
  576. {
  577. __x.swap(__y);
  578. }
  579. _LIBCPP_END_NAMESPACE_STD
  580. #endif // _LIBCPP_SPLIT_BUFFER