__split_buffer 22 KB

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