__split_buffer 25 KB

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