__split_buffer 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. public:
  48. using value_type = _Tp;
  49. using allocator_type = _Allocator;
  50. using __alloc_rr = __libcpp_remove_reference_t<allocator_type>;
  51. using __alloc_traits = allocator_traits<__alloc_rr>;
  52. using reference = value_type&;
  53. using const_reference = const value_type&;
  54. using size_type = typename __alloc_traits::size_type;
  55. using difference_type = typename __alloc_traits::difference_type;
  56. using pointer = typename __alloc_traits::pointer;
  57. using const_pointer = typename __alloc_traits::const_pointer;
  58. using iterator = pointer;
  59. using const_iterator = const_pointer;
  60. pointer __first_;
  61. pointer __begin_;
  62. pointer __end_;
  63. __compressed_pair<pointer, allocator_type> __end_cap_;
  64. using __alloc_ref = __add_lvalue_reference_t<allocator_type>;
  65. using __alloc_const_ref = __add_lvalue_reference_t<allocator_type>;
  66. __split_buffer(const __split_buffer&) = delete;
  67. __split_buffer& operator=(const __split_buffer&) = delete;
  68. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer()
  69. _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
  70. : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __default_init_tag()) {}
  71. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit __split_buffer(__alloc_rr& __a)
  72. : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a) {}
  73. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit __split_buffer(const __alloc_rr& __a)
  74. : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a) {}
  75. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
  76. __split_buffer(size_type __cap, size_type __start, __alloc_rr& __a);
  77. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer(__split_buffer&& __c)
  78. _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
  79. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer(__split_buffer&& __c, const __alloc_rr& __a);
  80. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer& operator=(__split_buffer&& __c)
  81. _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
  82. is_nothrow_move_assignable<allocator_type>::value) ||
  83. !__alloc_traits::propagate_on_container_move_assignment::value);
  84. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~__split_buffer();
  85. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __alloc_rr& __alloc() _NOEXCEPT { return __end_cap_.second(); }
  86. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const __alloc_rr& __alloc() const _NOEXCEPT {
  87. return __end_cap_.second();
  88. }
  89. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer& __end_cap() _NOEXCEPT { return __end_cap_.first(); }
  90. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const pointer& __end_cap() const _NOEXCEPT {
  91. return __end_cap_.first();
  92. }
  93. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return __begin_; }
  94. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return __begin_; }
  95. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return __end_; }
  96. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return __end_; }
  97. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT { __destruct_at_end(__begin_); }
  98. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type size() const {
  99. return static_cast<size_type>(__end_ - __begin_);
  100. }
  101. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool empty() const { return __end_ == __begin_; }
  102. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type capacity() const {
  103. return static_cast<size_type>(__end_cap() - __first_);
  104. }
  105. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __front_spare() const {
  106. return static_cast<size_type>(__begin_ - __first_);
  107. }
  108. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __back_spare() const {
  109. return static_cast<size_type>(__end_cap() - __end_);
  110. }
  111. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference front() { return *__begin_; }
  112. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference front() const { return *__begin_; }
  113. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference back() { return *(__end_ - 1); }
  114. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference back() const { return *(__end_ - 1); }
  115. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve(size_type __n);
  116. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void shrink_to_fit() _NOEXCEPT;
  117. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_front(const_reference __x);
  118. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(const_reference __x);
  119. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_front(value_type&& __x);
  120. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x);
  121. template <class... _Args>
  122. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void emplace_back(_Args&&... __args);
  123. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void pop_front() { __destruct_at_begin(__begin_ + 1); }
  124. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void pop_back() { __destruct_at_end(__end_ - 1); }
  125. void __uninitialized_at_end(size_type __n);
  126. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n);
  127. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n, const_reference __x);
  128. template <class _InputIter, __enable_if_t<__has_exactly_input_iterator_category<_InputIter>::value, int> = 0>
  129. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(_InputIter __first, _InputIter __last);
  130. template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
  131. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
  132. __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
  133. template <class _Iterator, class _Sentinel>
  134. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
  135. __construct_at_end_with_sentinel(_Iterator __first, _Sentinel __last);
  136. template <class _Iterator>
  137. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
  138. __construct_at_end_with_size(_Iterator __first, size_type __n);
  139. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin) {
  140. __destruct_at_begin(__new_begin, is_trivially_destructible<value_type>());
  141. }
  142. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin, false_type);
  143. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin, true_type);
  144. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last) _NOEXCEPT {
  145. __destruct_at_end(__new_last, false_type());
  146. }
  147. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last, false_type) _NOEXCEPT;
  148. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last, true_type) _NOEXCEPT;
  149. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void swap(__split_buffer& __x)
  150. _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable<__alloc_rr>::value);
  151. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool __invariants() const;
  152. private:
  153. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer& __c, true_type)
  154. _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
  155. __alloc() = std::move(__c.__alloc());
  156. }
  157. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT {}
  158. struct _ConstructTransaction {
  159. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit _ConstructTransaction(
  160. pointer* __p, size_type __n) _NOEXCEPT
  161. : __pos_(*__p),
  162. __end_(*__p + __n),
  163. __dest_(__p) {}
  164. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~_ConstructTransaction() { *__dest_ = __pos_; }
  165. pointer __pos_;
  166. const pointer __end_;
  167. private:
  168. pointer* __dest_;
  169. };
  170. };
  171. template <class _Tp, class _Allocator>
  172. _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __split_buffer<_Tp, _Allocator>::__invariants() const {
  173. if (__first_ == nullptr) {
  174. if (__begin_ != nullptr)
  175. return false;
  176. if (__end_ != nullptr)
  177. return false;
  178. if (__end_cap() != nullptr)
  179. return false;
  180. } else {
  181. if (__begin_ < __first_)
  182. return false;
  183. if (__end_ < __begin_)
  184. return false;
  185. if (__end_cap() < __end_)
  186. return false;
  187. }
  188. return true;
  189. }
  190. template <class _Tp, class _Allocator>
  191. void __split_buffer<_Tp, _Allocator>::__uninitialized_at_end(size_type __n) {
  192. this->__end_ += __n;
  193. }
  194. // Default constructs __n objects starting at __end_
  195. // throws if construction throws
  196. // Precondition: __n > 0
  197. // Precondition: size() + __n <= capacity()
  198. // Postcondition: size() == size() + __n
  199. template <class _Tp, class _Allocator>
  200. _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n) {
  201. _ConstructTransaction __tx(&this->__end_, __n);
  202. for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
  203. __alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_));
  204. }
  205. }
  206. // Copy constructs __n objects starting at __end_ from __x
  207. // throws if construction throws
  208. // Precondition: __n > 0
  209. // Precondition: size() + __n <= capacity()
  210. // Postcondition: size() == old size() + __n
  211. // Postcondition: [i] == __x for all i in [size() - __n, __n)
  212. template <class _Tp, class _Allocator>
  213. _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  214. __split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x) {
  215. _ConstructTransaction __tx(&this->__end_, __n);
  216. for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
  217. __alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_), __x);
  218. }
  219. }
  220. template <class _Tp, class _Allocator>
  221. template <class _InputIter, __enable_if_t<__has_exactly_input_iterator_category<_InputIter>::value, int> >
  222. _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  223. __split_buffer<_Tp, _Allocator>::__construct_at_end(_InputIter __first, _InputIter __last) {
  224. __construct_at_end_with_sentinel(__first, __last);
  225. }
  226. template <class _Tp, class _Allocator>
  227. template <class _Iterator, class _Sentinel>
  228. _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  229. __split_buffer<_Tp, _Allocator>::__construct_at_end_with_sentinel(_Iterator __first, _Sentinel __last) {
  230. __alloc_rr& __a = this->__alloc();
  231. for (; __first != __last; ++__first) {
  232. if (__end_ == __end_cap()) {
  233. size_type __old_cap = __end_cap() - __first_;
  234. size_type __new_cap = std::max<size_type>(2 * __old_cap, 8);
  235. __split_buffer __buf(__new_cap, 0, __a);
  236. for (pointer __p = __begin_; __p != __end_; ++__p, (void)++__buf.__end_)
  237. __alloc_traits::construct(__buf.__alloc(), std::__to_address(__buf.__end_), std::move(*__p));
  238. swap(__buf);
  239. }
  240. __alloc_traits::construct(__a, std::__to_address(this->__end_), *__first);
  241. ++this->__end_;
  242. }
  243. }
  244. template <class _Tp, class _Allocator>
  245. template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
  246. _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  247. __split_buffer<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last) {
  248. __construct_at_end_with_size(__first, std::distance(__first, __last));
  249. }
  250. template <class _Tp, class _Allocator>
  251. template <class _ForwardIterator>
  252. _LIBCPP_CONSTEXPR_SINCE_CXX20 void
  253. __split_buffer<_Tp, _Allocator>::__construct_at_end_with_size(_ForwardIterator __first, size_type __n) {
  254. _ConstructTransaction __tx(&this->__end_, __n);
  255. for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void)++__first) {
  256. __alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_), *__first);
  257. }
  258. }
  259. template <class _Tp, class _Allocator>
  260. _LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
  261. __split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type) {
  262. while (__begin_ != __new_begin)
  263. __alloc_traits::destroy(__alloc(), std::__to_address(__begin_++));
  264. }
  265. template <class _Tp, class _Allocator>
  266. _LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
  267. __split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, true_type) {
  268. __begin_ = __new_begin;
  269. }
  270. template <class _Tp, class _Allocator>
  271. _LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
  272. __split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT {
  273. while (__new_last != __end_)
  274. __alloc_traits::destroy(__alloc(), std::__to_address(--__end_));
  275. }
  276. template <class _Tp, class _Allocator>
  277. _LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
  278. __split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, true_type) _NOEXCEPT {
  279. __end_ = __new_last;
  280. }
  281. template <class _Tp, class _Allocator>
  282. _LIBCPP_CONSTEXPR_SINCE_CXX20
  283. __split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a)
  284. : __end_cap_(nullptr, __a) {
  285. if (__cap == 0) {
  286. __first_ = nullptr;
  287. } else {
  288. auto __allocation = std::__allocate_at_least(__alloc(), __cap);
  289. __first_ = __allocation.ptr;
  290. __cap = __allocation.count;
  291. }
  292. __begin_ = __end_ = __first_ + __start;
  293. __end_cap() = __first_ + __cap;
  294. }
  295. template <class _Tp, class _Allocator>
  296. _LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>::~__split_buffer() {
  297. clear();
  298. if (__first_)
  299. __alloc_traits::deallocate(__alloc(), __first_, capacity());
  300. }
  301. template <class _Tp, class _Allocator>
  302. _LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c)
  303. _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
  304. : __first_(std::move(__c.__first_)),
  305. __begin_(std::move(__c.__begin_)),
  306. __end_(std::move(__c.__end_)),
  307. __end_cap_(std::move(__c.__end_cap_)) {
  308. __c.__first_ = nullptr;
  309. __c.__begin_ = nullptr;
  310. __c.__end_ = nullptr;
  311. __c.__end_cap() = nullptr;
  312. }
  313. template <class _Tp, class _Allocator>
  314. _LIBCPP_CONSTEXPR_SINCE_CXX20
  315. __split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a)
  316. : __end_cap_(nullptr, __a) {
  317. if (__a == __c.__alloc()) {
  318. __first_ = __c.__first_;
  319. __begin_ = __c.__begin_;
  320. __end_ = __c.__end_;
  321. __end_cap() = __c.__end_cap();
  322. __c.__first_ = nullptr;
  323. __c.__begin_ = nullptr;
  324. __c.__end_ = nullptr;
  325. __c.__end_cap() = nullptr;
  326. } else {
  327. auto __allocation = std::__allocate_at_least(__alloc(), __c.size());
  328. __first_ = __allocation.ptr;
  329. __begin_ = __end_ = __first_;
  330. __end_cap() = __first_ + __allocation.count;
  331. typedef move_iterator<iterator> _Ip;
  332. __construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
  333. }
  334. }
  335. template <class _Tp, class _Allocator>
  336. _LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>&
  337. __split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c)
  338. _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
  339. is_nothrow_move_assignable<allocator_type>::value) ||
  340. !__alloc_traits::propagate_on_container_move_assignment::value) {
  341. clear();
  342. shrink_to_fit();
  343. __first_ = __c.__first_;
  344. __begin_ = __c.__begin_;
  345. __end_ = __c.__end_;
  346. __end_cap() = __c.__end_cap();
  347. __move_assign_alloc(__c, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
  348. __c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
  349. return *this;
  350. }
  351. template <class _Tp, class _Allocator>
  352. _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x)
  353. _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable<__alloc_rr>::value) {
  354. std::swap(__first_, __x.__first_);
  355. std::swap(__begin_, __x.__begin_);
  356. std::swap(__end_, __x.__end_);
  357. std::swap(__end_cap(), __x.__end_cap());
  358. std::__swap_allocator(__alloc(), __x.__alloc());
  359. }
  360. template <class _Tp, class _Allocator>
  361. _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::reserve(size_type __n) {
  362. if (__n < capacity()) {
  363. __split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc());
  364. __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
  365. std::swap(__first_, __t.__first_);
  366. std::swap(__begin_, __t.__begin_);
  367. std::swap(__end_, __t.__end_);
  368. std::swap(__end_cap(), __t.__end_cap());
  369. }
  370. }
  371. template <class _Tp, class _Allocator>
  372. _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT {
  373. if (capacity() > size()) {
  374. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  375. try {
  376. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  377. __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc());
  378. __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
  379. __t.__end_ = __t.__begin_ + (__end_ - __begin_);
  380. std::swap(__first_, __t.__first_);
  381. std::swap(__begin_, __t.__begin_);
  382. std::swap(__end_, __t.__end_);
  383. std::swap(__end_cap(), __t.__end_cap());
  384. #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
  385. } catch (...) {
  386. }
  387. #endif // _LIBCPP_HAS_NO_EXCEPTIONS
  388. }
  389. }
  390. template <class _Tp, class _Allocator>
  391. _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::push_front(const_reference __x) {
  392. if (__begin_ == __first_) {
  393. if (__end_ < __end_cap()) {
  394. difference_type __d = __end_cap() - __end_;
  395. __d = (__d + 1) / 2;
  396. __begin_ = std::move_backward(__begin_, __end_, __end_ + __d);
  397. __end_ += __d;
  398. } else {
  399. size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
  400. __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
  401. __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
  402. std::swap(__first_, __t.__first_);
  403. std::swap(__begin_, __t.__begin_);
  404. std::swap(__end_, __t.__end_);
  405. std::swap(__end_cap(), __t.__end_cap());
  406. }
  407. }
  408. __alloc_traits::construct(__alloc(), std::__to_address(__begin_ - 1), __x);
  409. --__begin_;
  410. }
  411. template <class _Tp, class _Allocator>
  412. _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::push_front(value_type&& __x) {
  413. if (__begin_ == __first_) {
  414. if (__end_ < __end_cap()) {
  415. difference_type __d = __end_cap() - __end_;
  416. __d = (__d + 1) / 2;
  417. __begin_ = std::move_backward(__begin_, __end_, __end_ + __d);
  418. __end_ += __d;
  419. } else {
  420. size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
  421. __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
  422. __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
  423. std::swap(__first_, __t.__first_);
  424. std::swap(__begin_, __t.__begin_);
  425. std::swap(__end_, __t.__end_);
  426. std::swap(__end_cap(), __t.__end_cap());
  427. }
  428. }
  429. __alloc_traits::construct(__alloc(), std::__to_address(__begin_ - 1), std::move(__x));
  430. --__begin_;
  431. }
  432. template <class _Tp, class _Allocator>
  433. _LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
  434. __split_buffer<_Tp, _Allocator>::push_back(const_reference __x) {
  435. if (__end_ == __end_cap()) {
  436. if (__begin_ > __first_) {
  437. difference_type __d = __begin_ - __first_;
  438. __d = (__d + 1) / 2;
  439. __end_ = std::move(__begin_, __end_, __begin_ - __d);
  440. __begin_ -= __d;
  441. } else {
  442. size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
  443. __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
  444. __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
  445. std::swap(__first_, __t.__first_);
  446. std::swap(__begin_, __t.__begin_);
  447. std::swap(__end_, __t.__end_);
  448. std::swap(__end_cap(), __t.__end_cap());
  449. }
  450. }
  451. __alloc_traits::construct(__alloc(), std::__to_address(__end_), __x);
  452. ++__end_;
  453. }
  454. template <class _Tp, class _Allocator>
  455. _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::push_back(value_type&& __x) {
  456. if (__end_ == __end_cap()) {
  457. if (__begin_ > __first_) {
  458. difference_type __d = __begin_ - __first_;
  459. __d = (__d + 1) / 2;
  460. __end_ = std::move(__begin_, __end_, __begin_ - __d);
  461. __begin_ -= __d;
  462. } else {
  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 / 4, __alloc());
  465. __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
  466. std::swap(__first_, __t.__first_);
  467. std::swap(__begin_, __t.__begin_);
  468. std::swap(__end_, __t.__end_);
  469. std::swap(__end_cap(), __t.__end_cap());
  470. }
  471. }
  472. __alloc_traits::construct(__alloc(), std::__to_address(__end_), std::move(__x));
  473. ++__end_;
  474. }
  475. template <class _Tp, class _Allocator>
  476. template <class... _Args>
  477. _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
  478. if (__end_ == __end_cap()) {
  479. if (__begin_ > __first_) {
  480. difference_type __d = __begin_ - __first_;
  481. __d = (__d + 1) / 2;
  482. __end_ = std::move(__begin_, __end_, __begin_ - __d);
  483. __begin_ -= __d;
  484. } else {
  485. size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
  486. __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
  487. __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
  488. std::swap(__first_, __t.__first_);
  489. std::swap(__begin_, __t.__begin_);
  490. std::swap(__end_, __t.__end_);
  491. std::swap(__end_cap(), __t.__end_cap());
  492. }
  493. }
  494. __alloc_traits::construct(__alloc(), std::__to_address(__end_), std::forward<_Args>(__args)...);
  495. ++__end_;
  496. }
  497. template <class _Tp, class _Allocator>
  498. _LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
  499. swap(__split_buffer<_Tp, _Allocator>& __x, __split_buffer<_Tp, _Allocator>& __y) _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
  500. __x.swap(__y);
  501. }
  502. _LIBCPP_END_NAMESPACE_STD
  503. _LIBCPP_POP_MACROS
  504. #endif // _LIBCPP___SPLIT_BUFFER