__split_buffer 22 KB

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