__split_buffer 24 KB

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