__split_buffer 25 KB

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