__split_buffer 22 KB

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