xref: /freebsd/contrib/llvm-project/libcxx/include/deque (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
10b57cec5SDimitry Andric// -*- C++ -*-
2349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
30b57cec5SDimitry Andric//
40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70b57cec5SDimitry Andric//
80b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric
100b57cec5SDimitry Andric#ifndef _LIBCPP_DEQUE
110b57cec5SDimitry Andric#define _LIBCPP_DEQUE
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric/*
140b57cec5SDimitry Andric    deque synopsis
150b57cec5SDimitry Andric
160b57cec5SDimitry Andricnamespace std
170b57cec5SDimitry Andric{
180b57cec5SDimitry Andric
190b57cec5SDimitry Andrictemplate <class T, class Allocator = allocator<T> >
200b57cec5SDimitry Andricclass deque
210b57cec5SDimitry Andric{
220b57cec5SDimitry Andricpublic:
230b57cec5SDimitry Andric    // types:
240b57cec5SDimitry Andric    typedef T value_type;
250b57cec5SDimitry Andric    typedef Allocator allocator_type;
260b57cec5SDimitry Andric
270b57cec5SDimitry Andric    typedef typename allocator_type::reference       reference;
280b57cec5SDimitry Andric    typedef typename allocator_type::const_reference const_reference;
290b57cec5SDimitry Andric    typedef implementation-defined                   iterator;
300b57cec5SDimitry Andric    typedef implementation-defined                   const_iterator;
310b57cec5SDimitry Andric    typedef typename allocator_type::size_type       size_type;
320b57cec5SDimitry Andric    typedef typename allocator_type::difference_type difference_type;
330b57cec5SDimitry Andric
340b57cec5SDimitry Andric    typedef typename allocator_type::pointer         pointer;
350b57cec5SDimitry Andric    typedef typename allocator_type::const_pointer   const_pointer;
360b57cec5SDimitry Andric    typedef std::reverse_iterator<iterator>          reverse_iterator;
370b57cec5SDimitry Andric    typedef std::reverse_iterator<const_iterator>    const_reverse_iterator;
380b57cec5SDimitry Andric
390b57cec5SDimitry Andric    // construct/copy/destroy:
400b57cec5SDimitry Andric    deque() noexcept(is_nothrow_default_constructible<allocator_type>::value);
410b57cec5SDimitry Andric    explicit deque(const allocator_type& a);
420b57cec5SDimitry Andric    explicit deque(size_type n);
430b57cec5SDimitry Andric    explicit deque(size_type n, const allocator_type& a); // C++14
440b57cec5SDimitry Andric    deque(size_type n, const value_type& v);
450b57cec5SDimitry Andric    deque(size_type n, const value_type& v, const allocator_type& a);
460b57cec5SDimitry Andric    template <class InputIterator>
470b57cec5SDimitry Andric        deque(InputIterator f, InputIterator l);
480b57cec5SDimitry Andric    template <class InputIterator>
490b57cec5SDimitry Andric        deque(InputIterator f, InputIterator l, const allocator_type& a);
500b57cec5SDimitry Andric    deque(const deque& c);
510b57cec5SDimitry Andric    deque(deque&& c)
520b57cec5SDimitry Andric        noexcept(is_nothrow_move_constructible<allocator_type>::value);
530b57cec5SDimitry Andric    deque(initializer_list<value_type> il, const Allocator& a = allocator_type());
540b57cec5SDimitry Andric    deque(const deque& c, const allocator_type& a);
550b57cec5SDimitry Andric    deque(deque&& c, const allocator_type& a);
560b57cec5SDimitry Andric    ~deque();
570b57cec5SDimitry Andric
580b57cec5SDimitry Andric    deque& operator=(const deque& c);
590b57cec5SDimitry Andric    deque& operator=(deque&& c)
600b57cec5SDimitry Andric        noexcept(
610b57cec5SDimitry Andric             allocator_type::propagate_on_container_move_assignment::value &&
620b57cec5SDimitry Andric             is_nothrow_move_assignable<allocator_type>::value);
630b57cec5SDimitry Andric    deque& operator=(initializer_list<value_type> il);
640b57cec5SDimitry Andric
650b57cec5SDimitry Andric    template <class InputIterator>
660b57cec5SDimitry Andric        void assign(InputIterator f, InputIterator l);
670b57cec5SDimitry Andric    void assign(size_type n, const value_type& v);
680b57cec5SDimitry Andric    void assign(initializer_list<value_type> il);
690b57cec5SDimitry Andric
700b57cec5SDimitry Andric    allocator_type get_allocator() const noexcept;
710b57cec5SDimitry Andric
720b57cec5SDimitry Andric    // iterators:
730b57cec5SDimitry Andric
740b57cec5SDimitry Andric    iterator       begin() noexcept;
750b57cec5SDimitry Andric    const_iterator begin() const noexcept;
760b57cec5SDimitry Andric    iterator       end() noexcept;
770b57cec5SDimitry Andric    const_iterator end() const noexcept;
780b57cec5SDimitry Andric
790b57cec5SDimitry Andric    reverse_iterator       rbegin() noexcept;
800b57cec5SDimitry Andric    const_reverse_iterator rbegin() const noexcept;
810b57cec5SDimitry Andric    reverse_iterator       rend() noexcept;
820b57cec5SDimitry Andric    const_reverse_iterator rend() const noexcept;
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric    const_iterator         cbegin() const noexcept;
850b57cec5SDimitry Andric    const_iterator         cend() const noexcept;
860b57cec5SDimitry Andric    const_reverse_iterator crbegin() const noexcept;
870b57cec5SDimitry Andric    const_reverse_iterator crend() const noexcept;
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric    // capacity:
900b57cec5SDimitry Andric    size_type size() const noexcept;
910b57cec5SDimitry Andric    size_type max_size() const noexcept;
920b57cec5SDimitry Andric    void resize(size_type n);
930b57cec5SDimitry Andric    void resize(size_type n, const value_type& v);
940b57cec5SDimitry Andric    void shrink_to_fit();
950b57cec5SDimitry Andric    bool empty() const noexcept;
960b57cec5SDimitry Andric
970b57cec5SDimitry Andric    // element access:
980b57cec5SDimitry Andric    reference operator[](size_type i);
990b57cec5SDimitry Andric    const_reference operator[](size_type i) const;
1000b57cec5SDimitry Andric    reference at(size_type i);
1010b57cec5SDimitry Andric    const_reference at(size_type i) const;
1020b57cec5SDimitry Andric    reference front();
1030b57cec5SDimitry Andric    const_reference front() const;
1040b57cec5SDimitry Andric    reference back();
1050b57cec5SDimitry Andric    const_reference back() const;
1060b57cec5SDimitry Andric
1070b57cec5SDimitry Andric    // modifiers:
1080b57cec5SDimitry Andric    void push_front(const value_type& v);
1090b57cec5SDimitry Andric    void push_front(value_type&& v);
1100b57cec5SDimitry Andric    void push_back(const value_type& v);
1110b57cec5SDimitry Andric    void push_back(value_type&& v);
1120b57cec5SDimitry Andric    template <class... Args> reference emplace_front(Args&&... args);  // reference in C++17
1130b57cec5SDimitry Andric    template <class... Args> reference emplace_back(Args&&... args);   // reference in C++17
1140b57cec5SDimitry Andric    template <class... Args> iterator emplace(const_iterator p, Args&&... args);
1150b57cec5SDimitry Andric    iterator insert(const_iterator p, const value_type& v);
1160b57cec5SDimitry Andric    iterator insert(const_iterator p, value_type&& v);
1170b57cec5SDimitry Andric    iterator insert(const_iterator p, size_type n, const value_type& v);
1180b57cec5SDimitry Andric    template <class InputIterator>
1190b57cec5SDimitry Andric        iterator insert(const_iterator p, InputIterator f, InputIterator l);
1200b57cec5SDimitry Andric    iterator insert(const_iterator p, initializer_list<value_type> il);
1210b57cec5SDimitry Andric    void pop_front();
1220b57cec5SDimitry Andric    void pop_back();
1230b57cec5SDimitry Andric    iterator erase(const_iterator p);
1240b57cec5SDimitry Andric    iterator erase(const_iterator f, const_iterator l);
1250b57cec5SDimitry Andric    void swap(deque& c)
1260b57cec5SDimitry Andric        noexcept(allocator_traits<allocator_type>::is_always_equal::value);  // C++17
1270b57cec5SDimitry Andric    void clear() noexcept;
1280b57cec5SDimitry Andric};
1290b57cec5SDimitry Andric
1300b57cec5SDimitry Andrictemplate <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
1310b57cec5SDimitry Andric   deque(InputIterator, InputIterator, Allocator = Allocator())
132349cc55cSDimitry Andric   -> deque<typename iterator_traits<InputIterator>::value_type, Allocator>; // C++17
1330b57cec5SDimitry Andric
1340b57cec5SDimitry Andrictemplate <class T, class Allocator>
1350b57cec5SDimitry Andric    bool operator==(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
1360b57cec5SDimitry Andrictemplate <class T, class Allocator>
1370b57cec5SDimitry Andric    bool operator< (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
1380b57cec5SDimitry Andrictemplate <class T, class Allocator>
1390b57cec5SDimitry Andric    bool operator!=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
1400b57cec5SDimitry Andrictemplate <class T, class Allocator>
1410b57cec5SDimitry Andric    bool operator> (const deque<T,Allocator>& x, const deque<T,Allocator>& y);
1420b57cec5SDimitry Andrictemplate <class T, class Allocator>
1430b57cec5SDimitry Andric    bool operator>=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
1440b57cec5SDimitry Andrictemplate <class T, class Allocator>
1450b57cec5SDimitry Andric    bool operator<=(const deque<T,Allocator>& x, const deque<T,Allocator>& y);
1460b57cec5SDimitry Andric
1470b57cec5SDimitry Andric// specialized algorithms:
1480b57cec5SDimitry Andrictemplate <class T, class Allocator>
1490b57cec5SDimitry Andric    void swap(deque<T,Allocator>& x, deque<T,Allocator>& y)
1500b57cec5SDimitry Andric         noexcept(noexcept(x.swap(y)));
1510b57cec5SDimitry Andric
1520b57cec5SDimitry Andrictemplate <class T, class Allocator, class U>
1535ffd83dbSDimitry Andric    typename deque<T, Allocator>::size_type
1545ffd83dbSDimitry Andric    erase(deque<T, Allocator>& c, const U& value);       // C++20
1550b57cec5SDimitry Andrictemplate <class T, class Allocator, class Predicate>
1565ffd83dbSDimitry Andric    typename deque<T, Allocator>::size_type
1575ffd83dbSDimitry Andric    erase_if(deque<T, Allocator>& c, Predicate pred);    // C++20
1580b57cec5SDimitry Andric
1590b57cec5SDimitry Andric}  // std
1600b57cec5SDimitry Andric
1610b57cec5SDimitry Andric*/
1620b57cec5SDimitry Andric
163*81ad6265SDimitry Andric#include <__algorithm/copy.h>
164*81ad6265SDimitry Andric#include <__algorithm/copy_backward.h>
165*81ad6265SDimitry Andric#include <__algorithm/equal.h>
166*81ad6265SDimitry Andric#include <__algorithm/fill_n.h>
167*81ad6265SDimitry Andric#include <__algorithm/lexicographical_compare.h>
168*81ad6265SDimitry Andric#include <__algorithm/min.h>
169*81ad6265SDimitry Andric#include <__algorithm/remove.h>
170*81ad6265SDimitry Andric#include <__algorithm/remove_if.h>
171*81ad6265SDimitry Andric#include <__algorithm/unwrap_iter.h>
172*81ad6265SDimitry Andric#include <__assert> // all public C++ headers provide the assertion handler
1730b57cec5SDimitry Andric#include <__config>
174*81ad6265SDimitry Andric#include <__format/enable_insertable.h>
175349cc55cSDimitry Andric#include <__iterator/iterator_traits.h>
176*81ad6265SDimitry Andric#include <__iterator/next.h>
177*81ad6265SDimitry Andric#include <__iterator/prev.h>
178*81ad6265SDimitry Andric#include <__iterator/reverse_iterator.h>
1790b57cec5SDimitry Andric#include <__split_buffer>
180fe6060f1SDimitry Andric#include <__utility/forward.h>
181*81ad6265SDimitry Andric#include <__utility/move.h>
182*81ad6265SDimitry Andric#include <__utility/swap.h>
183fe6060f1SDimitry Andric#include <limits>
1840b57cec5SDimitry Andric#include <stdexcept>
185fe6060f1SDimitry Andric#include <type_traits>
1860b57cec5SDimitry Andric#include <version>
1870b57cec5SDimitry Andric
188*81ad6265SDimitry Andric#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
189*81ad6265SDimitry Andric#  include <algorithm>
190*81ad6265SDimitry Andric#  include <functional>
191*81ad6265SDimitry Andric#  include <iterator>
192*81ad6265SDimitry Andric#endif
193*81ad6265SDimitry Andric
194*81ad6265SDimitry Andric// standard-mandated includes
195*81ad6265SDimitry Andric
196*81ad6265SDimitry Andric// [iterator.range]
197*81ad6265SDimitry Andric#include <__iterator/access.h>
198*81ad6265SDimitry Andric#include <__iterator/data.h>
199*81ad6265SDimitry Andric#include <__iterator/empty.h>
200*81ad6265SDimitry Andric#include <__iterator/reverse_access.h>
201*81ad6265SDimitry Andric#include <__iterator/size.h>
202*81ad6265SDimitry Andric
203*81ad6265SDimitry Andric// [deque.syn]
204*81ad6265SDimitry Andric#include <compare>
205*81ad6265SDimitry Andric#include <initializer_list>
206*81ad6265SDimitry Andric
2070b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2080b57cec5SDimitry Andric#  pragma GCC system_header
2090b57cec5SDimitry Andric#endif
2100b57cec5SDimitry Andric
2110b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS
2120b57cec5SDimitry Andric#include <__undef_macros>
2130b57cec5SDimitry Andric
2140b57cec5SDimitry Andric
2150b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
2160b57cec5SDimitry Andric
2170b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> class __deque_base;
2180b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator = allocator<_Tp> > class _LIBCPP_TEMPLATE_VIS deque;
2190b57cec5SDimitry Andric
2200b57cec5SDimitry Andrictemplate <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
2210b57cec5SDimitry Andric          class _DiffType, _DiffType _BlockSize>
2220b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __deque_iterator;
2230b57cec5SDimitry Andric
2240b57cec5SDimitry Andrictemplate <class _RAIter,
2250b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
2260b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
2270b57cec5SDimitry Andriccopy(_RAIter __f,
2280b57cec5SDimitry Andric     _RAIter __l,
2290b57cec5SDimitry Andric     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
230480093f4SDimitry Andric     typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
2310b57cec5SDimitry Andric
2320b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
2330b57cec5SDimitry Andric          class _OutputIterator>
2340b57cec5SDimitry Andric_OutputIterator
2350b57cec5SDimitry Andriccopy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
2360b57cec5SDimitry Andric     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
2370b57cec5SDimitry Andric     _OutputIterator __r);
2380b57cec5SDimitry Andric
2390b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
2400b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
2410b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
2420b57cec5SDimitry Andriccopy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
2430b57cec5SDimitry Andric     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
2440b57cec5SDimitry Andric     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
2450b57cec5SDimitry Andric
2460b57cec5SDimitry Andrictemplate <class _RAIter,
2470b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
2480b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
2490b57cec5SDimitry Andriccopy_backward(_RAIter __f,
2500b57cec5SDimitry Andric              _RAIter __l,
2510b57cec5SDimitry Andric              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
252480093f4SDimitry Andric              typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
2530b57cec5SDimitry Andric
2540b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
2550b57cec5SDimitry Andric          class _OutputIterator>
2560b57cec5SDimitry Andric_OutputIterator
2570b57cec5SDimitry Andriccopy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
2580b57cec5SDimitry Andric              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
2590b57cec5SDimitry Andric              _OutputIterator __r);
2600b57cec5SDimitry Andric
2610b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
2620b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
2630b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
2640b57cec5SDimitry Andriccopy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
2650b57cec5SDimitry Andric              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
2660b57cec5SDimitry Andric              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
2670b57cec5SDimitry Andric
2680b57cec5SDimitry Andrictemplate <class _RAIter,
2690b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
2700b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
2710b57cec5SDimitry Andricmove(_RAIter __f,
2720b57cec5SDimitry Andric     _RAIter __l,
2730b57cec5SDimitry Andric     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
274480093f4SDimitry Andric     typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
2750b57cec5SDimitry Andric
2760b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
2770b57cec5SDimitry Andric          class _OutputIterator>
2780b57cec5SDimitry Andric_OutputIterator
2790b57cec5SDimitry Andricmove(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
2800b57cec5SDimitry Andric     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
2810b57cec5SDimitry Andric     _OutputIterator __r);
2820b57cec5SDimitry Andric
2830b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
2840b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
2850b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
2860b57cec5SDimitry Andricmove(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
2870b57cec5SDimitry Andric     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
2880b57cec5SDimitry Andric     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
2890b57cec5SDimitry Andric
2900b57cec5SDimitry Andrictemplate <class _RAIter,
2910b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
2920b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
2930b57cec5SDimitry Andricmove_backward(_RAIter __f,
2940b57cec5SDimitry Andric              _RAIter __l,
2950b57cec5SDimitry Andric              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
296480093f4SDimitry Andric              typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
2970b57cec5SDimitry Andric
2980b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
2990b57cec5SDimitry Andric          class _OutputIterator>
3000b57cec5SDimitry Andric_OutputIterator
3010b57cec5SDimitry Andricmove_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
3020b57cec5SDimitry Andric              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
3030b57cec5SDimitry Andric              _OutputIterator __r);
3040b57cec5SDimitry Andric
3050b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
3060b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
3070b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
3080b57cec5SDimitry Andricmove_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
3090b57cec5SDimitry Andric              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
3100b57cec5SDimitry Andric              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
3110b57cec5SDimitry Andric
3120b57cec5SDimitry Andrictemplate <class _ValueType, class _DiffType>
3130b57cec5SDimitry Andricstruct __deque_block_size {
3140b57cec5SDimitry Andric  static const _DiffType value = sizeof(_ValueType) < 256 ? 4096 / sizeof(_ValueType) : 16;
3150b57cec5SDimitry Andric};
3160b57cec5SDimitry Andric
3170b57cec5SDimitry Andrictemplate <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
3180b57cec5SDimitry Andric          class _DiffType, _DiffType _BS =
3190b57cec5SDimitry Andric#ifdef _LIBCPP_ABI_INCOMPLETE_TYPES_IN_DEQUE
3200b57cec5SDimitry Andric// Keep template parameter to avoid changing all template declarations thoughout
3210b57cec5SDimitry Andric// this file.
3220b57cec5SDimitry Andric                               0
3230b57cec5SDimitry Andric#else
3240b57cec5SDimitry Andric                               __deque_block_size<_ValueType, _DiffType>::value
3250b57cec5SDimitry Andric#endif
3260b57cec5SDimitry Andric          >
3270b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __deque_iterator
3280b57cec5SDimitry Andric{
3290b57cec5SDimitry Andric    typedef _MapPointer __map_iterator;
3300b57cec5SDimitry Andricpublic:
3310b57cec5SDimitry Andric    typedef _Pointer  pointer;
3320b57cec5SDimitry Andric    typedef _DiffType difference_type;
3330b57cec5SDimitry Andricprivate:
3340b57cec5SDimitry Andric    __map_iterator __m_iter_;
3350b57cec5SDimitry Andric    pointer        __ptr_;
3360b57cec5SDimitry Andric
3370b57cec5SDimitry Andric    static const difference_type __block_size;
3380b57cec5SDimitry Andricpublic:
3390b57cec5SDimitry Andric    typedef _ValueType                  value_type;
3400b57cec5SDimitry Andric    typedef random_access_iterator_tag  iterator_category;
3410b57cec5SDimitry Andric    typedef _Reference                  reference;
3420b57cec5SDimitry Andric
3430b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __deque_iterator() _NOEXCEPT
3440b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
3450b57cec5SDimitry Andric     : __m_iter_(nullptr), __ptr_(nullptr)
3460b57cec5SDimitry Andric#endif
3470b57cec5SDimitry Andric     {}
3480b57cec5SDimitry Andric
3490b57cec5SDimitry Andric    template <class _Pp, class _Rp, class _MP>
3500b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3510b57cec5SDimitry Andric    __deque_iterator(const __deque_iterator<value_type, _Pp, _Rp, _MP, difference_type, _BS>& __it,
3520b57cec5SDimitry Andric                typename enable_if<is_convertible<_Pp, pointer>::value>::type* = 0) _NOEXCEPT
3530b57cec5SDimitry Andric        : __m_iter_(__it.__m_iter_), __ptr_(__it.__ptr_) {}
3540b57cec5SDimitry Andric
3550b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *__ptr_;}
3560b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_;}
3570b57cec5SDimitry Andric
3580b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator++()
3590b57cec5SDimitry Andric    {
3600b57cec5SDimitry Andric        if (++__ptr_ - *__m_iter_ == __block_size)
3610b57cec5SDimitry Andric        {
3620b57cec5SDimitry Andric            ++__m_iter_;
3630b57cec5SDimitry Andric            __ptr_ = *__m_iter_;
3640b57cec5SDimitry Andric        }
3650b57cec5SDimitry Andric        return *this;
3660b57cec5SDimitry Andric    }
3670b57cec5SDimitry Andric
3680b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __deque_iterator operator++(int)
3690b57cec5SDimitry Andric    {
3700b57cec5SDimitry Andric        __deque_iterator __tmp = *this;
3710b57cec5SDimitry Andric        ++(*this);
3720b57cec5SDimitry Andric        return __tmp;
3730b57cec5SDimitry Andric    }
3740b57cec5SDimitry Andric
3750b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator--()
3760b57cec5SDimitry Andric    {
3770b57cec5SDimitry Andric        if (__ptr_ == *__m_iter_)
3780b57cec5SDimitry Andric        {
3790b57cec5SDimitry Andric            --__m_iter_;
3800b57cec5SDimitry Andric            __ptr_ = *__m_iter_ + __block_size;
3810b57cec5SDimitry Andric        }
3820b57cec5SDimitry Andric        --__ptr_;
3830b57cec5SDimitry Andric        return *this;
3840b57cec5SDimitry Andric    }
3850b57cec5SDimitry Andric
3860b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __deque_iterator operator--(int)
3870b57cec5SDimitry Andric    {
3880b57cec5SDimitry Andric        __deque_iterator __tmp = *this;
3890b57cec5SDimitry Andric        --(*this);
3900b57cec5SDimitry Andric        return __tmp;
3910b57cec5SDimitry Andric    }
3920b57cec5SDimitry Andric
3930b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator+=(difference_type __n)
3940b57cec5SDimitry Andric    {
3950b57cec5SDimitry Andric        if (__n != 0)
3960b57cec5SDimitry Andric        {
3970b57cec5SDimitry Andric            __n += __ptr_ - *__m_iter_;
3980b57cec5SDimitry Andric            if (__n > 0)
3990b57cec5SDimitry Andric            {
4000b57cec5SDimitry Andric                __m_iter_ += __n / __block_size;
4010b57cec5SDimitry Andric                __ptr_ = *__m_iter_ + __n % __block_size;
4020b57cec5SDimitry Andric            }
4030b57cec5SDimitry Andric            else // (__n < 0)
4040b57cec5SDimitry Andric            {
4050b57cec5SDimitry Andric                difference_type __z = __block_size - 1 - __n;
4060b57cec5SDimitry Andric                __m_iter_ -= __z / __block_size;
4070b57cec5SDimitry Andric                __ptr_ = *__m_iter_ + (__block_size - 1 - __z % __block_size);
4080b57cec5SDimitry Andric            }
4090b57cec5SDimitry Andric        }
4100b57cec5SDimitry Andric        return *this;
4110b57cec5SDimitry Andric    }
4120b57cec5SDimitry Andric
4130b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator-=(difference_type __n)
4140b57cec5SDimitry Andric    {
4150b57cec5SDimitry Andric        return *this += -__n;
4160b57cec5SDimitry Andric    }
4170b57cec5SDimitry Andric
4180b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __deque_iterator operator+(difference_type __n) const
4190b57cec5SDimitry Andric    {
4200b57cec5SDimitry Andric        __deque_iterator __t(*this);
4210b57cec5SDimitry Andric        __t += __n;
4220b57cec5SDimitry Andric        return __t;
4230b57cec5SDimitry Andric    }
4240b57cec5SDimitry Andric
4250b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __deque_iterator operator-(difference_type __n) const
4260b57cec5SDimitry Andric    {
4270b57cec5SDimitry Andric        __deque_iterator __t(*this);
4280b57cec5SDimitry Andric        __t -= __n;
4290b57cec5SDimitry Andric        return __t;
4300b57cec5SDimitry Andric    }
4310b57cec5SDimitry Andric
4320b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
4330b57cec5SDimitry Andric    friend __deque_iterator operator+(difference_type __n, const __deque_iterator& __it)
4340b57cec5SDimitry Andric        {return __it + __n;}
4350b57cec5SDimitry Andric
4360b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
4370b57cec5SDimitry Andric    friend difference_type operator-(const __deque_iterator& __x, const __deque_iterator& __y)
4380b57cec5SDimitry Andric    {
4390b57cec5SDimitry Andric        if (__x != __y)
4400b57cec5SDimitry Andric            return (__x.__m_iter_ - __y.__m_iter_) * __block_size
4410b57cec5SDimitry Andric                 + (__x.__ptr_ - *__x.__m_iter_)
4420b57cec5SDimitry Andric                 - (__y.__ptr_ - *__y.__m_iter_);
4430b57cec5SDimitry Andric        return 0;
4440b57cec5SDimitry Andric    }
4450b57cec5SDimitry Andric
4460b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const
4470b57cec5SDimitry Andric        {return *(*this + __n);}
4480b57cec5SDimitry Andric
4490b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY friend
4500b57cec5SDimitry Andric        bool operator==(const __deque_iterator& __x, const __deque_iterator& __y)
4510b57cec5SDimitry Andric        {return __x.__ptr_ == __y.__ptr_;}
4520b57cec5SDimitry Andric
4530b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY friend
4540b57cec5SDimitry Andric        bool operator!=(const __deque_iterator& __x, const __deque_iterator& __y)
4550b57cec5SDimitry Andric        {return !(__x == __y);}
4560b57cec5SDimitry Andric
4570b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY friend
4580b57cec5SDimitry Andric        bool operator<(const __deque_iterator& __x, const __deque_iterator& __y)
4590b57cec5SDimitry Andric        {return __x.__m_iter_ < __y.__m_iter_ ||
4600b57cec5SDimitry Andric               (__x.__m_iter_ == __y.__m_iter_ && __x.__ptr_ < __y.__ptr_);}
4610b57cec5SDimitry Andric
4620b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY friend
4630b57cec5SDimitry Andric        bool operator>(const __deque_iterator& __x, const __deque_iterator& __y)
4640b57cec5SDimitry Andric        {return __y < __x;}
4650b57cec5SDimitry Andric
4660b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY friend
4670b57cec5SDimitry Andric        bool operator<=(const __deque_iterator& __x, const __deque_iterator& __y)
4680b57cec5SDimitry Andric        {return !(__y < __x);}
4690b57cec5SDimitry Andric
4700b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY friend
4710b57cec5SDimitry Andric        bool operator>=(const __deque_iterator& __x, const __deque_iterator& __y)
4720b57cec5SDimitry Andric        {return !(__x < __y);}
4730b57cec5SDimitry Andric
4740b57cec5SDimitry Andricprivate:
475*81ad6265SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
4760b57cec5SDimitry Andric        : __m_iter_(__m), __ptr_(__p) {}
4770b57cec5SDimitry Andric
4780b57cec5SDimitry Andric    template <class _Tp, class _Ap> friend class __deque_base;
4790b57cec5SDimitry Andric    template <class _Tp, class _Ap> friend class _LIBCPP_TEMPLATE_VIS deque;
4800b57cec5SDimitry Andric    template <class _Vp, class _Pp, class _Rp, class _MP, class _Dp, _Dp>
4810b57cec5SDimitry Andric        friend class _LIBCPP_TEMPLATE_VIS __deque_iterator;
4820b57cec5SDimitry Andric
4830b57cec5SDimitry Andric    template <class _RAIter,
4840b57cec5SDimitry Andric              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
4850b57cec5SDimitry Andric    friend
4860b57cec5SDimitry Andric    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
4870b57cec5SDimitry Andric    copy(_RAIter __f,
4880b57cec5SDimitry Andric         _RAIter __l,
4890b57cec5SDimitry Andric         __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
490480093f4SDimitry Andric         typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*);
4910b57cec5SDimitry Andric
4920b57cec5SDimitry Andric    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
4930b57cec5SDimitry Andric              class _OutputIterator>
4940b57cec5SDimitry Andric    friend
4950b57cec5SDimitry Andric    _OutputIterator
4960b57cec5SDimitry Andric    copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
4970b57cec5SDimitry Andric         __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
4980b57cec5SDimitry Andric         _OutputIterator __r);
4990b57cec5SDimitry Andric
5000b57cec5SDimitry Andric    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
5010b57cec5SDimitry Andric              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
5020b57cec5SDimitry Andric    friend
5030b57cec5SDimitry Andric    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
5040b57cec5SDimitry Andric    copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
5050b57cec5SDimitry Andric         __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
5060b57cec5SDimitry Andric         __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
5070b57cec5SDimitry Andric
5080b57cec5SDimitry Andric    template <class _RAIter,
5090b57cec5SDimitry Andric              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
5100b57cec5SDimitry Andric    friend
5110b57cec5SDimitry Andric    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
5120b57cec5SDimitry Andric    copy_backward(_RAIter __f,
5130b57cec5SDimitry Andric                  _RAIter __l,
5140b57cec5SDimitry Andric                  __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
515480093f4SDimitry Andric                  typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*);
5160b57cec5SDimitry Andric
5170b57cec5SDimitry Andric    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
5180b57cec5SDimitry Andric              class _OutputIterator>
5190b57cec5SDimitry Andric    friend
5200b57cec5SDimitry Andric    _OutputIterator
5210b57cec5SDimitry Andric    copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
5220b57cec5SDimitry Andric                  __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
5230b57cec5SDimitry Andric                  _OutputIterator __r);
5240b57cec5SDimitry Andric
5250b57cec5SDimitry Andric    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
5260b57cec5SDimitry Andric              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
5270b57cec5SDimitry Andric    friend
5280b57cec5SDimitry Andric    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
5290b57cec5SDimitry Andric    copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
5300b57cec5SDimitry Andric                  __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
5310b57cec5SDimitry Andric                  __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
5320b57cec5SDimitry Andric
5330b57cec5SDimitry Andric    template <class _RAIter,
5340b57cec5SDimitry Andric              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
5350b57cec5SDimitry Andric    friend
5360b57cec5SDimitry Andric    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
5370b57cec5SDimitry Andric    move(_RAIter __f,
5380b57cec5SDimitry Andric         _RAIter __l,
5390b57cec5SDimitry Andric         __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
540480093f4SDimitry Andric         typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*);
5410b57cec5SDimitry Andric
5420b57cec5SDimitry Andric    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
5430b57cec5SDimitry Andric              class _OutputIterator>
5440b57cec5SDimitry Andric    friend
5450b57cec5SDimitry Andric    _OutputIterator
5460b57cec5SDimitry Andric    move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
5470b57cec5SDimitry Andric         __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
5480b57cec5SDimitry Andric         _OutputIterator __r);
5490b57cec5SDimitry Andric
5500b57cec5SDimitry Andric    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
5510b57cec5SDimitry Andric              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
5520b57cec5SDimitry Andric    friend
5530b57cec5SDimitry Andric    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
5540b57cec5SDimitry Andric    move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
5550b57cec5SDimitry Andric         __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
5560b57cec5SDimitry Andric         __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
5570b57cec5SDimitry Andric
5580b57cec5SDimitry Andric    template <class _RAIter,
5590b57cec5SDimitry Andric              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
5600b57cec5SDimitry Andric    friend
5610b57cec5SDimitry Andric    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
5620b57cec5SDimitry Andric    move_backward(_RAIter __f,
5630b57cec5SDimitry Andric                  _RAIter __l,
5640b57cec5SDimitry Andric                  __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
565480093f4SDimitry Andric                  typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*);
5660b57cec5SDimitry Andric
5670b57cec5SDimitry Andric    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
5680b57cec5SDimitry Andric              class _OutputIterator>
5690b57cec5SDimitry Andric    friend
5700b57cec5SDimitry Andric    _OutputIterator
5710b57cec5SDimitry Andric    move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
5720b57cec5SDimitry Andric                  __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
5730b57cec5SDimitry Andric                  _OutputIterator __r);
5740b57cec5SDimitry Andric
5750b57cec5SDimitry Andric    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
5760b57cec5SDimitry Andric              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
5770b57cec5SDimitry Andric    friend
5780b57cec5SDimitry Andric    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
5790b57cec5SDimitry Andric    move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
5800b57cec5SDimitry Andric                  __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
5810b57cec5SDimitry Andric                  __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
5820b57cec5SDimitry Andric};
5830b57cec5SDimitry Andric
5840b57cec5SDimitry Andrictemplate <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
5850b57cec5SDimitry Andric          class _DiffType, _DiffType _BlockSize>
5860b57cec5SDimitry Andricconst _DiffType __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer,
5870b57cec5SDimitry Andric                                 _DiffType, _BlockSize>::__block_size =
5880b57cec5SDimitry Andric    __deque_block_size<_ValueType, _DiffType>::value;
5890b57cec5SDimitry Andric
5900b57cec5SDimitry Andric// copy
5910b57cec5SDimitry Andric
5920b57cec5SDimitry Andrictemplate <class _RAIter,
5930b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
5940b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
5950b57cec5SDimitry Andriccopy(_RAIter __f,
5960b57cec5SDimitry Andric     _RAIter __l,
5970b57cec5SDimitry Andric     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
598480093f4SDimitry Andric     typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
5990b57cec5SDimitry Andric{
6000b57cec5SDimitry Andric    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
6010b57cec5SDimitry Andric    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
6020b57cec5SDimitry Andric    const difference_type __block_size = __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::__block_size;
6030b57cec5SDimitry Andric    while (__f != __l)
6040b57cec5SDimitry Andric    {
6050b57cec5SDimitry Andric        pointer __rb = __r.__ptr_;
6060b57cec5SDimitry Andric        pointer __re = *__r.__m_iter_ + __block_size;
6070b57cec5SDimitry Andric        difference_type __bs = __re - __rb;
6080b57cec5SDimitry Andric        difference_type __n = __l - __f;
6090b57cec5SDimitry Andric        _RAIter __m = __l;
6100b57cec5SDimitry Andric        if (__n > __bs)
6110b57cec5SDimitry Andric        {
6120b57cec5SDimitry Andric            __n = __bs;
6130b57cec5SDimitry Andric            __m = __f + __n;
6140b57cec5SDimitry Andric        }
6150b57cec5SDimitry Andric        _VSTD::copy(__f, __m, __rb);
6160b57cec5SDimitry Andric        __f = __m;
6170b57cec5SDimitry Andric        __r += __n;
6180b57cec5SDimitry Andric    }
6190b57cec5SDimitry Andric    return __r;
6200b57cec5SDimitry Andric}
6210b57cec5SDimitry Andric
6220b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
6230b57cec5SDimitry Andric          class _OutputIterator>
6240b57cec5SDimitry Andric_OutputIterator
6250b57cec5SDimitry Andriccopy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
6260b57cec5SDimitry Andric     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
6270b57cec5SDimitry Andric     _OutputIterator __r)
6280b57cec5SDimitry Andric{
6290b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
6300b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
6310b57cec5SDimitry Andric    const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
6320b57cec5SDimitry Andric    difference_type __n = __l - __f;
6330b57cec5SDimitry Andric    while (__n > 0)
6340b57cec5SDimitry Andric    {
6350b57cec5SDimitry Andric        pointer __fb = __f.__ptr_;
6360b57cec5SDimitry Andric        pointer __fe = *__f.__m_iter_ + __block_size;
6370b57cec5SDimitry Andric        difference_type __bs = __fe - __fb;
6380b57cec5SDimitry Andric        if (__bs > __n)
6390b57cec5SDimitry Andric        {
6400b57cec5SDimitry Andric            __bs = __n;
6410b57cec5SDimitry Andric            __fe = __fb + __bs;
6420b57cec5SDimitry Andric        }
6430b57cec5SDimitry Andric        __r = _VSTD::copy(__fb, __fe, __r);
6440b57cec5SDimitry Andric        __n -= __bs;
6450b57cec5SDimitry Andric        __f += __bs;
6460b57cec5SDimitry Andric    }
6470b57cec5SDimitry Andric    return __r;
6480b57cec5SDimitry Andric}
6490b57cec5SDimitry Andric
6500b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
6510b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
6520b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
6530b57cec5SDimitry Andriccopy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
6540b57cec5SDimitry Andric     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
6550b57cec5SDimitry Andric     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
6560b57cec5SDimitry Andric{
6570b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
6580b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
6590b57cec5SDimitry Andric    const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
6600b57cec5SDimitry Andric    difference_type __n = __l - __f;
6610b57cec5SDimitry Andric    while (__n > 0)
6620b57cec5SDimitry Andric    {
6630b57cec5SDimitry Andric        pointer __fb = __f.__ptr_;
6640b57cec5SDimitry Andric        pointer __fe = *__f.__m_iter_ + __block_size;
6650b57cec5SDimitry Andric        difference_type __bs = __fe - __fb;
6660b57cec5SDimitry Andric        if (__bs > __n)
6670b57cec5SDimitry Andric        {
6680b57cec5SDimitry Andric            __bs = __n;
6690b57cec5SDimitry Andric            __fe = __fb + __bs;
6700b57cec5SDimitry Andric        }
6710b57cec5SDimitry Andric        __r = _VSTD::copy(__fb, __fe, __r);
6720b57cec5SDimitry Andric        __n -= __bs;
6730b57cec5SDimitry Andric        __f += __bs;
6740b57cec5SDimitry Andric    }
6750b57cec5SDimitry Andric    return __r;
6760b57cec5SDimitry Andric}
6770b57cec5SDimitry Andric
6780b57cec5SDimitry Andric// copy_backward
6790b57cec5SDimitry Andric
6800b57cec5SDimitry Andrictemplate <class _RAIter,
6810b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
6820b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
6830b57cec5SDimitry Andriccopy_backward(_RAIter __f,
6840b57cec5SDimitry Andric              _RAIter __l,
6850b57cec5SDimitry Andric              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
686480093f4SDimitry Andric              typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
6870b57cec5SDimitry Andric{
6880b57cec5SDimitry Andric    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
6890b57cec5SDimitry Andric    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
6900b57cec5SDimitry Andric    while (__f != __l)
6910b57cec5SDimitry Andric    {
6920b57cec5SDimitry Andric        __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
6930b57cec5SDimitry Andric        pointer __rb = *__rp.__m_iter_;
6940b57cec5SDimitry Andric        pointer __re = __rp.__ptr_ + 1;
6950b57cec5SDimitry Andric        difference_type __bs = __re - __rb;
6960b57cec5SDimitry Andric        difference_type __n = __l - __f;
6970b57cec5SDimitry Andric        _RAIter __m = __f;
6980b57cec5SDimitry Andric        if (__n > __bs)
6990b57cec5SDimitry Andric        {
7000b57cec5SDimitry Andric            __n = __bs;
7010b57cec5SDimitry Andric            __m = __l - __n;
7020b57cec5SDimitry Andric        }
7030b57cec5SDimitry Andric        _VSTD::copy_backward(__m, __l, __re);
7040b57cec5SDimitry Andric        __l = __m;
7050b57cec5SDimitry Andric        __r -= __n;
7060b57cec5SDimitry Andric    }
7070b57cec5SDimitry Andric    return __r;
7080b57cec5SDimitry Andric}
7090b57cec5SDimitry Andric
7100b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
7110b57cec5SDimitry Andric          class _OutputIterator>
7120b57cec5SDimitry Andric_OutputIterator
7130b57cec5SDimitry Andriccopy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
7140b57cec5SDimitry Andric              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
7150b57cec5SDimitry Andric              _OutputIterator __r)
7160b57cec5SDimitry Andric{
7170b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
7180b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
7190b57cec5SDimitry Andric    difference_type __n = __l - __f;
7200b57cec5SDimitry Andric    while (__n > 0)
7210b57cec5SDimitry Andric    {
7220b57cec5SDimitry Andric        --__l;
7230b57cec5SDimitry Andric        pointer __lb = *__l.__m_iter_;
7240b57cec5SDimitry Andric        pointer __le = __l.__ptr_ + 1;
7250b57cec5SDimitry Andric        difference_type __bs = __le - __lb;
7260b57cec5SDimitry Andric        if (__bs > __n)
7270b57cec5SDimitry Andric        {
7280b57cec5SDimitry Andric            __bs = __n;
7290b57cec5SDimitry Andric            __lb = __le - __bs;
7300b57cec5SDimitry Andric        }
7310b57cec5SDimitry Andric        __r = _VSTD::copy_backward(__lb, __le, __r);
7320b57cec5SDimitry Andric        __n -= __bs;
7330b57cec5SDimitry Andric        __l -= __bs - 1;
7340b57cec5SDimitry Andric    }
7350b57cec5SDimitry Andric    return __r;
7360b57cec5SDimitry Andric}
7370b57cec5SDimitry Andric
7380b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
7390b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
7400b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
7410b57cec5SDimitry Andriccopy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
7420b57cec5SDimitry Andric              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
7430b57cec5SDimitry Andric              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
7440b57cec5SDimitry Andric{
7450b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
7460b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
7470b57cec5SDimitry Andric    difference_type __n = __l - __f;
7480b57cec5SDimitry Andric    while (__n > 0)
7490b57cec5SDimitry Andric    {
7500b57cec5SDimitry Andric        --__l;
7510b57cec5SDimitry Andric        pointer __lb = *__l.__m_iter_;
7520b57cec5SDimitry Andric        pointer __le = __l.__ptr_ + 1;
7530b57cec5SDimitry Andric        difference_type __bs = __le - __lb;
7540b57cec5SDimitry Andric        if (__bs > __n)
7550b57cec5SDimitry Andric        {
7560b57cec5SDimitry Andric            __bs = __n;
7570b57cec5SDimitry Andric            __lb = __le - __bs;
7580b57cec5SDimitry Andric        }
7590b57cec5SDimitry Andric        __r = _VSTD::copy_backward(__lb, __le, __r);
7600b57cec5SDimitry Andric        __n -= __bs;
7610b57cec5SDimitry Andric        __l -= __bs - 1;
7620b57cec5SDimitry Andric    }
7630b57cec5SDimitry Andric    return __r;
7640b57cec5SDimitry Andric}
7650b57cec5SDimitry Andric
7660b57cec5SDimitry Andric// move
7670b57cec5SDimitry Andric
7680b57cec5SDimitry Andrictemplate <class _RAIter,
7690b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
7700b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
7710b57cec5SDimitry Andricmove(_RAIter __f,
7720b57cec5SDimitry Andric     _RAIter __l,
7730b57cec5SDimitry Andric     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
774480093f4SDimitry Andric     typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
7750b57cec5SDimitry Andric{
7760b57cec5SDimitry Andric    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
7770b57cec5SDimitry Andric    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
7780b57cec5SDimitry Andric    const difference_type __block_size = __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::__block_size;
7790b57cec5SDimitry Andric    while (__f != __l)
7800b57cec5SDimitry Andric    {
7810b57cec5SDimitry Andric        pointer __rb = __r.__ptr_;
7820b57cec5SDimitry Andric        pointer __re = *__r.__m_iter_ + __block_size;
7830b57cec5SDimitry Andric        difference_type __bs = __re - __rb;
7840b57cec5SDimitry Andric        difference_type __n = __l - __f;
7850b57cec5SDimitry Andric        _RAIter __m = __l;
7860b57cec5SDimitry Andric        if (__n > __bs)
7870b57cec5SDimitry Andric        {
7880b57cec5SDimitry Andric            __n = __bs;
7890b57cec5SDimitry Andric            __m = __f + __n;
7900b57cec5SDimitry Andric        }
7910b57cec5SDimitry Andric        _VSTD::move(__f, __m, __rb);
7920b57cec5SDimitry Andric        __f = __m;
7930b57cec5SDimitry Andric        __r += __n;
7940b57cec5SDimitry Andric    }
7950b57cec5SDimitry Andric    return __r;
7960b57cec5SDimitry Andric}
7970b57cec5SDimitry Andric
7980b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
7990b57cec5SDimitry Andric          class _OutputIterator>
8000b57cec5SDimitry Andric_OutputIterator
8010b57cec5SDimitry Andricmove(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
8020b57cec5SDimitry Andric     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
8030b57cec5SDimitry Andric     _OutputIterator __r)
8040b57cec5SDimitry Andric{
8050b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
8060b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
8070b57cec5SDimitry Andric    const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
8080b57cec5SDimitry Andric    difference_type __n = __l - __f;
8090b57cec5SDimitry Andric    while (__n > 0)
8100b57cec5SDimitry Andric    {
8110b57cec5SDimitry Andric        pointer __fb = __f.__ptr_;
8120b57cec5SDimitry Andric        pointer __fe = *__f.__m_iter_ + __block_size;
8130b57cec5SDimitry Andric        difference_type __bs = __fe - __fb;
8140b57cec5SDimitry Andric        if (__bs > __n)
8150b57cec5SDimitry Andric        {
8160b57cec5SDimitry Andric            __bs = __n;
8170b57cec5SDimitry Andric            __fe = __fb + __bs;
8180b57cec5SDimitry Andric        }
8190b57cec5SDimitry Andric        __r = _VSTD::move(__fb, __fe, __r);
8200b57cec5SDimitry Andric        __n -= __bs;
8210b57cec5SDimitry Andric        __f += __bs;
8220b57cec5SDimitry Andric    }
8230b57cec5SDimitry Andric    return __r;
8240b57cec5SDimitry Andric}
8250b57cec5SDimitry Andric
8260b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
8270b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
8280b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
8290b57cec5SDimitry Andricmove(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
8300b57cec5SDimitry Andric     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
8310b57cec5SDimitry Andric     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
8320b57cec5SDimitry Andric{
8330b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
8340b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
8350b57cec5SDimitry Andric    const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
8360b57cec5SDimitry Andric    difference_type __n = __l - __f;
8370b57cec5SDimitry Andric    while (__n > 0)
8380b57cec5SDimitry Andric    {
8390b57cec5SDimitry Andric        pointer __fb = __f.__ptr_;
8400b57cec5SDimitry Andric        pointer __fe = *__f.__m_iter_ + __block_size;
8410b57cec5SDimitry Andric        difference_type __bs = __fe - __fb;
8420b57cec5SDimitry Andric        if (__bs > __n)
8430b57cec5SDimitry Andric        {
8440b57cec5SDimitry Andric            __bs = __n;
8450b57cec5SDimitry Andric            __fe = __fb + __bs;
8460b57cec5SDimitry Andric        }
8470b57cec5SDimitry Andric        __r = _VSTD::move(__fb, __fe, __r);
8480b57cec5SDimitry Andric        __n -= __bs;
8490b57cec5SDimitry Andric        __f += __bs;
8500b57cec5SDimitry Andric    }
8510b57cec5SDimitry Andric    return __r;
8520b57cec5SDimitry Andric}
8530b57cec5SDimitry Andric
8540b57cec5SDimitry Andric// move_backward
8550b57cec5SDimitry Andric
8560b57cec5SDimitry Andrictemplate <class _RAIter,
8570b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
8580b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
8590b57cec5SDimitry Andricmove_backward(_RAIter __f,
8600b57cec5SDimitry Andric              _RAIter __l,
8610b57cec5SDimitry Andric              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
862480093f4SDimitry Andric              typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
8630b57cec5SDimitry Andric{
8640b57cec5SDimitry Andric    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
8650b57cec5SDimitry Andric    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
8660b57cec5SDimitry Andric    while (__f != __l)
8670b57cec5SDimitry Andric    {
8680b57cec5SDimitry Andric        __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
8690b57cec5SDimitry Andric        pointer __rb = *__rp.__m_iter_;
8700b57cec5SDimitry Andric        pointer __re = __rp.__ptr_ + 1;
8710b57cec5SDimitry Andric        difference_type __bs = __re - __rb;
8720b57cec5SDimitry Andric        difference_type __n = __l - __f;
8730b57cec5SDimitry Andric        _RAIter __m = __f;
8740b57cec5SDimitry Andric        if (__n > __bs)
8750b57cec5SDimitry Andric        {
8760b57cec5SDimitry Andric            __n = __bs;
8770b57cec5SDimitry Andric            __m = __l - __n;
8780b57cec5SDimitry Andric        }
8790b57cec5SDimitry Andric        _VSTD::move_backward(__m, __l, __re);
8800b57cec5SDimitry Andric        __l = __m;
8810b57cec5SDimitry Andric        __r -= __n;
8820b57cec5SDimitry Andric    }
8830b57cec5SDimitry Andric    return __r;
8840b57cec5SDimitry Andric}
8850b57cec5SDimitry Andric
8860b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
8870b57cec5SDimitry Andric          class _OutputIterator>
8880b57cec5SDimitry Andric_OutputIterator
8890b57cec5SDimitry Andricmove_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
8900b57cec5SDimitry Andric              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
8910b57cec5SDimitry Andric              _OutputIterator __r)
8920b57cec5SDimitry Andric{
8930b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
8940b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
8950b57cec5SDimitry Andric    difference_type __n = __l - __f;
8960b57cec5SDimitry Andric    while (__n > 0)
8970b57cec5SDimitry Andric    {
8980b57cec5SDimitry Andric        --__l;
8990b57cec5SDimitry Andric        pointer __lb = *__l.__m_iter_;
9000b57cec5SDimitry Andric        pointer __le = __l.__ptr_ + 1;
9010b57cec5SDimitry Andric        difference_type __bs = __le - __lb;
9020b57cec5SDimitry Andric        if (__bs > __n)
9030b57cec5SDimitry Andric        {
9040b57cec5SDimitry Andric            __bs = __n;
9050b57cec5SDimitry Andric            __lb = __le - __bs;
9060b57cec5SDimitry Andric        }
9070b57cec5SDimitry Andric        __r = _VSTD::move_backward(__lb, __le, __r);
9080b57cec5SDimitry Andric        __n -= __bs;
9090b57cec5SDimitry Andric        __l -= __bs - 1;
9100b57cec5SDimitry Andric    }
9110b57cec5SDimitry Andric    return __r;
9120b57cec5SDimitry Andric}
9130b57cec5SDimitry Andric
9140b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
9150b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
9160b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
9170b57cec5SDimitry Andricmove_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
9180b57cec5SDimitry Andric              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
9190b57cec5SDimitry Andric              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
9200b57cec5SDimitry Andric{
9210b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
9220b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
9230b57cec5SDimitry Andric    difference_type __n = __l - __f;
9240b57cec5SDimitry Andric    while (__n > 0)
9250b57cec5SDimitry Andric    {
9260b57cec5SDimitry Andric        --__l;
9270b57cec5SDimitry Andric        pointer __lb = *__l.__m_iter_;
9280b57cec5SDimitry Andric        pointer __le = __l.__ptr_ + 1;
9290b57cec5SDimitry Andric        difference_type __bs = __le - __lb;
9300b57cec5SDimitry Andric        if (__bs > __n)
9310b57cec5SDimitry Andric        {
9320b57cec5SDimitry Andric            __bs = __n;
9330b57cec5SDimitry Andric            __lb = __le - __bs;
9340b57cec5SDimitry Andric        }
9350b57cec5SDimitry Andric        __r = _VSTD::move_backward(__lb, __le, __r);
9360b57cec5SDimitry Andric        __n -= __bs;
9370b57cec5SDimitry Andric        __l -= __bs - 1;
9380b57cec5SDimitry Andric    }
9390b57cec5SDimitry Andric    return __r;
9400b57cec5SDimitry Andric}
9410b57cec5SDimitry Andric
9420b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
9430b57cec5SDimitry Andricclass __deque_base
9440b57cec5SDimitry Andric{
9450b57cec5SDimitry Andric    __deque_base(const __deque_base& __c);
9460b57cec5SDimitry Andric    __deque_base& operator=(const __deque_base& __c);
9470b57cec5SDimitry Andricpublic:
9480b57cec5SDimitry Andric    typedef _Allocator                                allocator_type;
9490b57cec5SDimitry Andric    typedef allocator_traits<allocator_type>          __alloc_traits;
9500b57cec5SDimitry Andric    typedef typename __alloc_traits::size_type        size_type;
951e40139ffSDimitry Andric
9520b57cec5SDimitry Andric    typedef _Tp                                       value_type;
9530b57cec5SDimitry Andric    typedef value_type&                               reference;
9540b57cec5SDimitry Andric    typedef const value_type&                         const_reference;
9550b57cec5SDimitry Andric    typedef typename __alloc_traits::difference_type  difference_type;
9560b57cec5SDimitry Andric    typedef typename __alloc_traits::pointer          pointer;
9570b57cec5SDimitry Andric    typedef typename __alloc_traits::const_pointer    const_pointer;
9580b57cec5SDimitry Andric
9590b57cec5SDimitry Andric    static const difference_type __block_size;
9600b57cec5SDimitry Andric
9610b57cec5SDimitry Andric    typedef typename __rebind_alloc_helper<__alloc_traits, pointer>::type __pointer_allocator;
9620b57cec5SDimitry Andric    typedef allocator_traits<__pointer_allocator>        __map_traits;
9630b57cec5SDimitry Andric    typedef typename __map_traits::pointer               __map_pointer;
9640b57cec5SDimitry Andric    typedef typename __rebind_alloc_helper<__alloc_traits, const_pointer>::type __const_pointer_allocator;
9650b57cec5SDimitry Andric    typedef typename allocator_traits<__const_pointer_allocator>::const_pointer __map_const_pointer;
9660b57cec5SDimitry Andric    typedef __split_buffer<pointer, __pointer_allocator> __map;
9670b57cec5SDimitry Andric
9680b57cec5SDimitry Andric    typedef __deque_iterator<value_type, pointer, reference, __map_pointer,
9690b57cec5SDimitry Andric                             difference_type>    iterator;
9700b57cec5SDimitry Andric    typedef __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer,
9710b57cec5SDimitry Andric                             difference_type>    const_iterator;
9720b57cec5SDimitry Andric
973e40139ffSDimitry Andric    struct __deque_block_range {
974e40139ffSDimitry Andric      explicit __deque_block_range(pointer __b, pointer __e) _NOEXCEPT : __begin_(__b), __end_(__e) {}
975e40139ffSDimitry Andric      const pointer __begin_;
976e40139ffSDimitry Andric      const pointer __end_;
977e40139ffSDimitry Andric    };
978e40139ffSDimitry Andric
979e40139ffSDimitry Andric    struct __deque_range {
980e40139ffSDimitry Andric      iterator __pos_;
981e40139ffSDimitry Andric      const iterator __end_;
982e40139ffSDimitry Andric
983e40139ffSDimitry Andric      __deque_range(iterator __pos, iterator __e) _NOEXCEPT
984e40139ffSDimitry Andric        : __pos_(__pos), __end_(__e) {}
985e40139ffSDimitry Andric
986e40139ffSDimitry Andric      explicit operator bool() const _NOEXCEPT {
987e40139ffSDimitry Andric        return __pos_ != __end_;
988e40139ffSDimitry Andric      }
989e40139ffSDimitry Andric
990e40139ffSDimitry Andric      __deque_range begin() const {
991e40139ffSDimitry Andric        return *this;
992e40139ffSDimitry Andric      }
993e40139ffSDimitry Andric
994e40139ffSDimitry Andric      __deque_range end() const {
995e40139ffSDimitry Andric        return __deque_range(__end_, __end_);
996e40139ffSDimitry Andric      }
997e40139ffSDimitry Andric      __deque_block_range operator*() const _NOEXCEPT {
998e40139ffSDimitry Andric         if (__pos_.__m_iter_ == __end_.__m_iter_) {
999e40139ffSDimitry Andric          return __deque_block_range(__pos_.__ptr_, __end_.__ptr_);
1000e40139ffSDimitry Andric        }
1001e40139ffSDimitry Andric        return __deque_block_range(__pos_.__ptr_, *__pos_.__m_iter_ + __block_size);
1002e40139ffSDimitry Andric      }
1003e40139ffSDimitry Andric
1004e40139ffSDimitry Andric      __deque_range& operator++() _NOEXCEPT {
1005e40139ffSDimitry Andric        if (__pos_.__m_iter_ == __end_.__m_iter_) {
1006e40139ffSDimitry Andric          __pos_ = __end_;
1007e40139ffSDimitry Andric        } else {
1008e40139ffSDimitry Andric          ++__pos_.__m_iter_;
1009e40139ffSDimitry Andric          __pos_.__ptr_ = *__pos_.__m_iter_;
1010e40139ffSDimitry Andric        }
1011e40139ffSDimitry Andric        return *this;
1012e40139ffSDimitry Andric      }
1013e40139ffSDimitry Andric
1014e40139ffSDimitry Andric
1015e40139ffSDimitry Andric      friend bool operator==(__deque_range const& __lhs, __deque_range const& __rhs) {
1016e40139ffSDimitry Andric        return __lhs.__pos_ == __rhs.__pos_;
1017e40139ffSDimitry Andric      }
1018e40139ffSDimitry Andric      friend bool operator!=(__deque_range const& __lhs, __deque_range const& __rhs) {
1019e40139ffSDimitry Andric        return !(__lhs == __rhs);
1020e40139ffSDimitry Andric      }
1021e40139ffSDimitry Andric    };
1022e40139ffSDimitry Andric
1023e40139ffSDimitry Andric
1024e40139ffSDimitry Andric
1025e40139ffSDimitry Andric    struct _ConstructTransaction {
1026e40139ffSDimitry Andric      _ConstructTransaction(__deque_base* __db, __deque_block_range& __r)
1027e40139ffSDimitry Andric        : __pos_(__r.__begin_), __end_(__r.__end_), __begin_(__r.__begin_), __base_(__db) {}
1028e40139ffSDimitry Andric
1029e40139ffSDimitry Andric
1030e40139ffSDimitry Andric      ~_ConstructTransaction() {
1031e40139ffSDimitry Andric        __base_->size() += (__pos_ - __begin_);
1032e40139ffSDimitry Andric      }
1033e40139ffSDimitry Andric
1034e40139ffSDimitry Andric      pointer __pos_;
1035e40139ffSDimitry Andric      const pointer __end_;
1036e40139ffSDimitry Andric    private:
1037e40139ffSDimitry Andric      const pointer __begin_;
1038e40139ffSDimitry Andric      __deque_base * const __base_;
1039e40139ffSDimitry Andric    };
1040e40139ffSDimitry Andric
10410b57cec5SDimitry Andricprotected:
10420b57cec5SDimitry Andric    __map __map_;
10430b57cec5SDimitry Andric    size_type __start_;
10440b57cec5SDimitry Andric    __compressed_pair<size_type, allocator_type> __size_;
10450b57cec5SDimitry Andric
10460b57cec5SDimitry Andric    iterator       begin() _NOEXCEPT;
10470b57cec5SDimitry Andric    const_iterator begin() const _NOEXCEPT;
10480b57cec5SDimitry Andric    iterator       end() _NOEXCEPT;
10490b57cec5SDimitry Andric    const_iterator end() const _NOEXCEPT;
10500b57cec5SDimitry Andric
10510b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY size_type&            size()          {return __size_.first();}
10520b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
10530b57cec5SDimitry Andric    const size_type& size() const _NOEXCEPT {return __size_.first();}
10540b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY allocator_type&       __alloc()       {return __size_.second();}
10550b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
10560b57cec5SDimitry Andric    const allocator_type& __alloc() const _NOEXCEPT {return __size_.second();}
10570b57cec5SDimitry Andric
10580b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
10590b57cec5SDimitry Andric    __deque_base()
10600b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
10610b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
10620b57cec5SDimitry Andric    explicit __deque_base(const allocator_type& __a);
10630b57cec5SDimitry Andricpublic:
10640b57cec5SDimitry Andric    ~__deque_base();
10650b57cec5SDimitry Andric
10660b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
10670b57cec5SDimitry Andric    __deque_base(__deque_base&& __c)
10680b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
10690b57cec5SDimitry Andric    __deque_base(__deque_base&& __c, const allocator_type& __a);
10700b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
10710b57cec5SDimitry Andric
10720b57cec5SDimitry Andric    void swap(__deque_base& __c)
10730b57cec5SDimitry Andric#if _LIBCPP_STD_VER >= 14
10740b57cec5SDimitry Andric        _NOEXCEPT;
10750b57cec5SDimitry Andric#else
10760b57cec5SDimitry Andric        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
10770b57cec5SDimitry Andric                    __is_nothrow_swappable<allocator_type>::value);
10780b57cec5SDimitry Andric#endif
10790b57cec5SDimitry Andricprotected:
10800b57cec5SDimitry Andric    void clear() _NOEXCEPT;
10810b57cec5SDimitry Andric
10820b57cec5SDimitry Andric    bool __invariants() const;
10830b57cec5SDimitry Andric
10840b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
10850b57cec5SDimitry Andric    void __move_assign(__deque_base& __c)
10860b57cec5SDimitry Andric        _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
10870b57cec5SDimitry Andric                   is_nothrow_move_assignable<allocator_type>::value)
10880b57cec5SDimitry Andric    {
10890b57cec5SDimitry Andric        __map_ = _VSTD::move(__c.__map_);
10900b57cec5SDimitry Andric        __start_ = __c.__start_;
10910b57cec5SDimitry Andric        size() = __c.size();
10920b57cec5SDimitry Andric        __move_assign_alloc(__c);
10930b57cec5SDimitry Andric        __c.__start_ = __c.size() = 0;
10940b57cec5SDimitry Andric    }
10950b57cec5SDimitry Andric
10960b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
10970b57cec5SDimitry Andric    void __move_assign_alloc(__deque_base& __c)
10980b57cec5SDimitry Andric        _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
10990b57cec5SDimitry Andric                   is_nothrow_move_assignable<allocator_type>::value)
11000b57cec5SDimitry Andric        {__move_assign_alloc(__c, integral_constant<bool,
11010b57cec5SDimitry Andric                      __alloc_traits::propagate_on_container_move_assignment::value>());}
11020b57cec5SDimitry Andric
11030b57cec5SDimitry Andricprivate:
11040b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
11050b57cec5SDimitry Andric    void __move_assign_alloc(__deque_base& __c, true_type)
11060b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
11070b57cec5SDimitry Andric        {
11080b57cec5SDimitry Andric            __alloc() = _VSTD::move(__c.__alloc());
11090b57cec5SDimitry Andric        }
11100b57cec5SDimitry Andric
11110b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
11120b57cec5SDimitry Andric    void __move_assign_alloc(__deque_base&, false_type) _NOEXCEPT
11130b57cec5SDimitry Andric        {}
11140b57cec5SDimitry Andric};
11150b57cec5SDimitry Andric
11160b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11170b57cec5SDimitry Andricconst typename __deque_base<_Tp, _Allocator>::difference_type
11180b57cec5SDimitry Andric    __deque_base<_Tp, _Allocator>::__block_size =
11190b57cec5SDimitry Andric        __deque_block_size<value_type, difference_type>::value;
11200b57cec5SDimitry Andric
11210b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11220b57cec5SDimitry Andricbool
11230b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::__invariants() const
11240b57cec5SDimitry Andric{
11250b57cec5SDimitry Andric    if (!__map_.__invariants())
11260b57cec5SDimitry Andric        return false;
11270b57cec5SDimitry Andric    if (__map_.size() >= size_type(-1) / __block_size)
11280b57cec5SDimitry Andric        return false;
11290b57cec5SDimitry Andric    for (typename __map::const_iterator __i = __map_.begin(), __e = __map_.end();
11300b57cec5SDimitry Andric         __i != __e; ++__i)
11310b57cec5SDimitry Andric        if (*__i == nullptr)
11320b57cec5SDimitry Andric            return false;
11330b57cec5SDimitry Andric    if (__map_.size() != 0)
11340b57cec5SDimitry Andric    {
11350b57cec5SDimitry Andric        if (size() >= __map_.size() * __block_size)
11360b57cec5SDimitry Andric            return false;
11370b57cec5SDimitry Andric        if (__start_ >= __map_.size() * __block_size - size())
11380b57cec5SDimitry Andric            return false;
11390b57cec5SDimitry Andric    }
11400b57cec5SDimitry Andric    else
11410b57cec5SDimitry Andric    {
11420b57cec5SDimitry Andric        if (size() != 0)
11430b57cec5SDimitry Andric            return false;
11440b57cec5SDimitry Andric        if (__start_ != 0)
11450b57cec5SDimitry Andric            return false;
11460b57cec5SDimitry Andric    }
11470b57cec5SDimitry Andric    return true;
11480b57cec5SDimitry Andric}
11490b57cec5SDimitry Andric
11500b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11510b57cec5SDimitry Andrictypename __deque_base<_Tp, _Allocator>::iterator
11520b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::begin() _NOEXCEPT
11530b57cec5SDimitry Andric{
11540b57cec5SDimitry Andric    __map_pointer __mp = __map_.begin() + __start_ / __block_size;
11550b57cec5SDimitry Andric    return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
11560b57cec5SDimitry Andric}
11570b57cec5SDimitry Andric
11580b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11590b57cec5SDimitry Andrictypename __deque_base<_Tp, _Allocator>::const_iterator
11600b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::begin() const _NOEXCEPT
11610b57cec5SDimitry Andric{
11620b57cec5SDimitry Andric    __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __start_ / __block_size);
11630b57cec5SDimitry Andric    return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
11640b57cec5SDimitry Andric}
11650b57cec5SDimitry Andric
11660b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11670b57cec5SDimitry Andrictypename __deque_base<_Tp, _Allocator>::iterator
11680b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::end() _NOEXCEPT
11690b57cec5SDimitry Andric{
11700b57cec5SDimitry Andric    size_type __p = size() + __start_;
11710b57cec5SDimitry Andric    __map_pointer __mp = __map_.begin() + __p / __block_size;
11720b57cec5SDimitry Andric    return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
11730b57cec5SDimitry Andric}
11740b57cec5SDimitry Andric
11750b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11760b57cec5SDimitry Andrictypename __deque_base<_Tp, _Allocator>::const_iterator
11770b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::end() const _NOEXCEPT
11780b57cec5SDimitry Andric{
11790b57cec5SDimitry Andric    size_type __p = size() + __start_;
11800b57cec5SDimitry Andric    __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __p / __block_size);
11810b57cec5SDimitry Andric    return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
11820b57cec5SDimitry Andric}
11830b57cec5SDimitry Andric
11840b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11850b57cec5SDimitry Andricinline
11860b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::__deque_base()
11870b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
1188480093f4SDimitry Andric    : __start_(0), __size_(0, __default_init_tag()) {}
11890b57cec5SDimitry Andric
11900b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11910b57cec5SDimitry Andricinline
11920b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::__deque_base(const allocator_type& __a)
11930b57cec5SDimitry Andric    : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {}
11940b57cec5SDimitry Andric
11950b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11960b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::~__deque_base()
11970b57cec5SDimitry Andric{
11980b57cec5SDimitry Andric    clear();
11990b57cec5SDimitry Andric    typename __map::iterator __i = __map_.begin();
12000b57cec5SDimitry Andric    typename __map::iterator __e = __map_.end();
12010b57cec5SDimitry Andric    for (; __i != __e; ++__i)
12020b57cec5SDimitry Andric        __alloc_traits::deallocate(__alloc(), *__i, __block_size);
12030b57cec5SDimitry Andric}
12040b57cec5SDimitry Andric
12050b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
12060b57cec5SDimitry Andric
12070b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
12080b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c)
12090b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
12100b57cec5SDimitry Andric    : __map_(_VSTD::move(__c.__map_)),
12110b57cec5SDimitry Andric      __start_(_VSTD::move(__c.__start_)),
12120b57cec5SDimitry Andric      __size_(_VSTD::move(__c.__size_))
12130b57cec5SDimitry Andric{
12140b57cec5SDimitry Andric    __c.__start_ = 0;
12150b57cec5SDimitry Andric    __c.size() = 0;
12160b57cec5SDimitry Andric}
12170b57cec5SDimitry Andric
12180b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
12190b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c, const allocator_type& __a)
12200b57cec5SDimitry Andric    : __map_(_VSTD::move(__c.__map_), __pointer_allocator(__a)),
12210b57cec5SDimitry Andric      __start_(_VSTD::move(__c.__start_)),
12220b57cec5SDimitry Andric      __size_(_VSTD::move(__c.size()), __a)
12230b57cec5SDimitry Andric{
12240b57cec5SDimitry Andric    if (__a == __c.__alloc())
12250b57cec5SDimitry Andric    {
12260b57cec5SDimitry Andric        __c.__start_ = 0;
12270b57cec5SDimitry Andric        __c.size() = 0;
12280b57cec5SDimitry Andric    }
12290b57cec5SDimitry Andric    else
12300b57cec5SDimitry Andric    {
12310b57cec5SDimitry Andric        __map_.clear();
12320b57cec5SDimitry Andric        __start_ = 0;
12330b57cec5SDimitry Andric        size() = 0;
12340b57cec5SDimitry Andric    }
12350b57cec5SDimitry Andric}
12360b57cec5SDimitry Andric
12370b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
12380b57cec5SDimitry Andric
12390b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
12400b57cec5SDimitry Andricvoid
12410b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::swap(__deque_base& __c)
12420b57cec5SDimitry Andric#if _LIBCPP_STD_VER >= 14
12430b57cec5SDimitry Andric        _NOEXCEPT
12440b57cec5SDimitry Andric#else
12450b57cec5SDimitry Andric        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
12460b57cec5SDimitry Andric                    __is_nothrow_swappable<allocator_type>::value)
12470b57cec5SDimitry Andric#endif
12480b57cec5SDimitry Andric{
12490b57cec5SDimitry Andric    __map_.swap(__c.__map_);
12500b57cec5SDimitry Andric    _VSTD::swap(__start_, __c.__start_);
12510b57cec5SDimitry Andric    _VSTD::swap(size(), __c.size());
1252e8d8bef9SDimitry Andric    _VSTD::__swap_allocator(__alloc(), __c.__alloc());
12530b57cec5SDimitry Andric}
12540b57cec5SDimitry Andric
12550b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
12560b57cec5SDimitry Andricvoid
12570b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::clear() _NOEXCEPT
12580b57cec5SDimitry Andric{
12590b57cec5SDimitry Andric    allocator_type& __a = __alloc();
12600b57cec5SDimitry Andric    for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
12610b57cec5SDimitry Andric        __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
12620b57cec5SDimitry Andric    size() = 0;
12630b57cec5SDimitry Andric    while (__map_.size() > 2)
12640b57cec5SDimitry Andric    {
12650b57cec5SDimitry Andric        __alloc_traits::deallocate(__a, __map_.front(), __block_size);
12660b57cec5SDimitry Andric        __map_.pop_front();
12670b57cec5SDimitry Andric    }
12680b57cec5SDimitry Andric    switch (__map_.size())
12690b57cec5SDimitry Andric    {
12700b57cec5SDimitry Andric    case 1:
12710b57cec5SDimitry Andric        __start_ = __block_size / 2;
12720b57cec5SDimitry Andric        break;
12730b57cec5SDimitry Andric    case 2:
12740b57cec5SDimitry Andric        __start_ = __block_size;
12750b57cec5SDimitry Andric        break;
12760b57cec5SDimitry Andric    }
12770b57cec5SDimitry Andric}
12780b57cec5SDimitry Andric
12790b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator /*= allocator<_Tp>*/>
12800b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS deque
12810b57cec5SDimitry Andric    : private __deque_base<_Tp, _Allocator>
12820b57cec5SDimitry Andric{
12830b57cec5SDimitry Andricpublic:
12840b57cec5SDimitry Andric    // types:
12850b57cec5SDimitry Andric
12860b57cec5SDimitry Andric    typedef _Tp value_type;
12870b57cec5SDimitry Andric    typedef _Allocator allocator_type;
12880b57cec5SDimitry Andric
12890b57cec5SDimitry Andric    static_assert((is_same<typename allocator_type::value_type, value_type>::value),
12900b57cec5SDimitry Andric                  "Allocator::value_type must be same type as value_type");
12910b57cec5SDimitry Andric
12920b57cec5SDimitry Andric    typedef __deque_base<value_type, allocator_type>      __base;
12930b57cec5SDimitry Andric
12940b57cec5SDimitry Andric    typedef typename __base::__alloc_traits               __alloc_traits;
12950b57cec5SDimitry Andric    typedef typename __base::reference                    reference;
12960b57cec5SDimitry Andric    typedef typename __base::const_reference              const_reference;
12970b57cec5SDimitry Andric    typedef typename __base::iterator                     iterator;
12980b57cec5SDimitry Andric    typedef typename __base::const_iterator               const_iterator;
12994824e7fdSDimitry Andric    typedef typename __base::size_type                    size_type;
13000b57cec5SDimitry Andric    typedef typename __base::difference_type              difference_type;
13010b57cec5SDimitry Andric
13020b57cec5SDimitry Andric    typedef typename __base::pointer                      pointer;
13030b57cec5SDimitry Andric    typedef typename __base::const_pointer                const_pointer;
13040b57cec5SDimitry Andric    typedef _VSTD::reverse_iterator<iterator>             reverse_iterator;
13050b57cec5SDimitry Andric    typedef _VSTD::reverse_iterator<const_iterator>       const_reverse_iterator;
13060b57cec5SDimitry Andric
1307e40139ffSDimitry Andric    using typename __base::__deque_range;
1308e40139ffSDimitry Andric    using typename __base::__deque_block_range;
1309e40139ffSDimitry Andric    using typename __base::_ConstructTransaction;
1310e40139ffSDimitry Andric
13110b57cec5SDimitry Andric    // construct/copy/destroy:
13120b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13130b57cec5SDimitry Andric    deque()
13140b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
13150b57cec5SDimitry Andric        {}
13160b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit deque(const allocator_type& __a) : __base(__a) {}
13170b57cec5SDimitry Andric    explicit deque(size_type __n);
13180b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
13190b57cec5SDimitry Andric    explicit deque(size_type __n, const _Allocator& __a);
13200b57cec5SDimitry Andric#endif
13210b57cec5SDimitry Andric    deque(size_type __n, const value_type& __v);
13224824e7fdSDimitry Andric
13234824e7fdSDimitry Andric    template <class = __enable_if_t<__is_allocator<_Allocator>::value> >
13244824e7fdSDimitry Andric    deque(size_type __n, const value_type& __v, const allocator_type& __a) : __base(__a)
13254824e7fdSDimitry Andric    {
13264824e7fdSDimitry Andric        if (__n > 0)
13274824e7fdSDimitry Andric            __append(__n, __v);
13284824e7fdSDimitry Andric    }
13294824e7fdSDimitry Andric
13300b57cec5SDimitry Andric    template <class _InputIter>
13310b57cec5SDimitry Andric        deque(_InputIter __f, _InputIter __l,
1332480093f4SDimitry Andric              typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type* = 0);
13330b57cec5SDimitry Andric    template <class _InputIter>
13340b57cec5SDimitry Andric        deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1335480093f4SDimitry Andric              typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type* = 0);
13360b57cec5SDimitry Andric    deque(const deque& __c);
1337*81ad6265SDimitry Andric    deque(const deque& __c, const __type_identity_t<allocator_type>& __a);
13380b57cec5SDimitry Andric
13390b57cec5SDimitry Andric    deque& operator=(const deque& __c);
13400b57cec5SDimitry Andric
13410b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
13420b57cec5SDimitry Andric    deque(initializer_list<value_type> __il);
13430b57cec5SDimitry Andric    deque(initializer_list<value_type> __il, const allocator_type& __a);
13440b57cec5SDimitry Andric
13450b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13460b57cec5SDimitry Andric    deque& operator=(initializer_list<value_type> __il) {assign(__il); return *this;}
13470b57cec5SDimitry Andric
13480b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13490b57cec5SDimitry Andric    deque(deque&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__base>::value);
13500b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1351*81ad6265SDimitry Andric    deque(deque&& __c, const __type_identity_t<allocator_type>& __a);
13520b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13530b57cec5SDimitry Andric    deque& operator=(deque&& __c)
13540b57cec5SDimitry Andric        _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
13550b57cec5SDimitry Andric                   is_nothrow_move_assignable<allocator_type>::value);
13560b57cec5SDimitry Andric
13570b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13580b57cec5SDimitry Andric    void assign(initializer_list<value_type> __il) {assign(__il.begin(), __il.end());}
13590b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
13600b57cec5SDimitry Andric
13610b57cec5SDimitry Andric    template <class _InputIter>
13620b57cec5SDimitry Andric        void assign(_InputIter __f, _InputIter __l,
1363480093f4SDimitry Andric                    typename enable_if<__is_cpp17_input_iterator<_InputIter>::value &&
1364480093f4SDimitry Andric                                      !__is_cpp17_random_access_iterator<_InputIter>::value>::type* = 0);
13650b57cec5SDimitry Andric    template <class _RAIter>
13660b57cec5SDimitry Andric        void assign(_RAIter __f, _RAIter __l,
1367480093f4SDimitry Andric                    typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
13680b57cec5SDimitry Andric    void assign(size_type __n, const value_type& __v);
13690b57cec5SDimitry Andric
13700b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13710b57cec5SDimitry Andric    allocator_type get_allocator() const _NOEXCEPT;
13720b57cec5SDimitry Andric
13730b57cec5SDimitry Andric    // iterators:
13740b57cec5SDimitry Andric
13750b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13760b57cec5SDimitry Andric    iterator       begin() _NOEXCEPT       {return __base::begin();}
13770b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13780b57cec5SDimitry Andric    const_iterator begin() const _NOEXCEPT {return __base::begin();}
13790b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13800b57cec5SDimitry Andric    iterator       end() _NOEXCEPT         {return __base::end();}
13810b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13820b57cec5SDimitry Andric    const_iterator end()   const _NOEXCEPT {return __base::end();}
13830b57cec5SDimitry Andric
13840b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13850b57cec5SDimitry Andric    reverse_iterator       rbegin() _NOEXCEPT
13860b57cec5SDimitry Andric        {return       reverse_iterator(__base::end());}
13870b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13880b57cec5SDimitry Andric    const_reverse_iterator rbegin() const _NOEXCEPT
13890b57cec5SDimitry Andric        {return const_reverse_iterator(__base::end());}
13900b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13910b57cec5SDimitry Andric    reverse_iterator       rend() _NOEXCEPT
13920b57cec5SDimitry Andric        {return       reverse_iterator(__base::begin());}
13930b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13940b57cec5SDimitry Andric    const_reverse_iterator rend()   const _NOEXCEPT
13950b57cec5SDimitry Andric        {return const_reverse_iterator(__base::begin());}
13960b57cec5SDimitry Andric
13970b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13980b57cec5SDimitry Andric    const_iterator         cbegin()  const _NOEXCEPT
13990b57cec5SDimitry Andric        {return __base::begin();}
14000b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14010b57cec5SDimitry Andric    const_iterator         cend()    const _NOEXCEPT
14020b57cec5SDimitry Andric        {return __base::end();}
14030b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14040b57cec5SDimitry Andric    const_reverse_iterator crbegin() const _NOEXCEPT
14050b57cec5SDimitry Andric        {return const_reverse_iterator(__base::end());}
14060b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14070b57cec5SDimitry Andric    const_reverse_iterator crend()   const _NOEXCEPT
14080b57cec5SDimitry Andric        {return const_reverse_iterator(__base::begin());}
14090b57cec5SDimitry Andric
14100b57cec5SDimitry Andric    // capacity:
14110b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14120b57cec5SDimitry Andric    size_type size() const _NOEXCEPT {return __base::size();}
14130b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14140b57cec5SDimitry Andric    size_type max_size() const _NOEXCEPT
1415e8d8bef9SDimitry Andric        {return _VSTD::min<size_type>(
14160b57cec5SDimitry Andric            __alloc_traits::max_size(__base::__alloc()),
14170b57cec5SDimitry Andric            numeric_limits<difference_type>::max());}
14180b57cec5SDimitry Andric    void resize(size_type __n);
14190b57cec5SDimitry Andric    void resize(size_type __n, const value_type& __v);
14200b57cec5SDimitry Andric    void shrink_to_fit() _NOEXCEPT;
14210b57cec5SDimitry Andric    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
14220b57cec5SDimitry Andric    bool empty() const _NOEXCEPT {return __base::size() == 0;}
14230b57cec5SDimitry Andric
14240b57cec5SDimitry Andric    // element access:
14250b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14260b57cec5SDimitry Andric    reference operator[](size_type __i) _NOEXCEPT;
14270b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14280b57cec5SDimitry Andric    const_reference operator[](size_type __i) const _NOEXCEPT;
14290b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14300b57cec5SDimitry Andric    reference at(size_type __i);
14310b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14320b57cec5SDimitry Andric    const_reference at(size_type __i) const;
14330b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14340b57cec5SDimitry Andric    reference front() _NOEXCEPT;
14350b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14360b57cec5SDimitry Andric    const_reference front() const _NOEXCEPT;
14370b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14380b57cec5SDimitry Andric    reference back() _NOEXCEPT;
14390b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14400b57cec5SDimitry Andric    const_reference back() const _NOEXCEPT;
14410b57cec5SDimitry Andric
14420b57cec5SDimitry Andric    // 23.2.2.3 modifiers:
14430b57cec5SDimitry Andric    void push_front(const value_type& __v);
14440b57cec5SDimitry Andric    void push_back(const value_type& __v);
14450b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
14460b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
14470b57cec5SDimitry Andric    template <class... _Args> reference emplace_front(_Args&&... __args);
14480b57cec5SDimitry Andric    template <class... _Args> reference emplace_back (_Args&&... __args);
14490b57cec5SDimitry Andric#else
14500b57cec5SDimitry Andric    template <class... _Args> void      emplace_front(_Args&&... __args);
14510b57cec5SDimitry Andric    template <class... _Args> void      emplace_back (_Args&&... __args);
14520b57cec5SDimitry Andric#endif
14530b57cec5SDimitry Andric    template <class... _Args> iterator emplace(const_iterator __p, _Args&&... __args);
14540b57cec5SDimitry Andric
14550b57cec5SDimitry Andric    void push_front(value_type&& __v);
14560b57cec5SDimitry Andric    void push_back(value_type&& __v);
14570b57cec5SDimitry Andric    iterator insert(const_iterator __p, value_type&& __v);
14580b57cec5SDimitry Andric
14590b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14600b57cec5SDimitry Andric    iterator insert(const_iterator __p, initializer_list<value_type> __il)
14610b57cec5SDimitry Andric        {return insert(__p, __il.begin(), __il.end());}
14620b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
14630b57cec5SDimitry Andric    iterator insert(const_iterator __p, const value_type& __v);
14640b57cec5SDimitry Andric    iterator insert(const_iterator __p, size_type __n, const value_type& __v);
14650b57cec5SDimitry Andric    template <class _InputIter>
14660b57cec5SDimitry Andric        iterator insert(const_iterator __p, _InputIter __f, _InputIter __l,
1467480093f4SDimitry Andric                         typename enable_if<__is_cpp17_input_iterator<_InputIter>::value
1468480093f4SDimitry Andric                                         &&!__is_cpp17_forward_iterator<_InputIter>::value>::type* = 0);
14690b57cec5SDimitry Andric    template <class _ForwardIterator>
14700b57cec5SDimitry Andric        iterator insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
1471480093f4SDimitry Andric                               typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value
1472480093f4SDimitry Andric                                         &&!__is_cpp17_bidirectional_iterator<_ForwardIterator>::value>::type* = 0);
14730b57cec5SDimitry Andric    template <class _BiIter>
14740b57cec5SDimitry Andric        iterator insert(const_iterator __p, _BiIter __f, _BiIter __l,
1475480093f4SDimitry Andric                         typename enable_if<__is_cpp17_bidirectional_iterator<_BiIter>::value>::type* = 0);
14760b57cec5SDimitry Andric
14770b57cec5SDimitry Andric    void pop_front();
14780b57cec5SDimitry Andric    void pop_back();
14790b57cec5SDimitry Andric    iterator erase(const_iterator __p);
14800b57cec5SDimitry Andric    iterator erase(const_iterator __f, const_iterator __l);
14810b57cec5SDimitry Andric
14820b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14830b57cec5SDimitry Andric    void swap(deque& __c)
14840b57cec5SDimitry Andric#if _LIBCPP_STD_VER >= 14
14850b57cec5SDimitry Andric        _NOEXCEPT;
14860b57cec5SDimitry Andric#else
14870b57cec5SDimitry Andric        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
14880b57cec5SDimitry Andric                   __is_nothrow_swappable<allocator_type>::value);
14890b57cec5SDimitry Andric#endif
14900b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14910b57cec5SDimitry Andric    void clear() _NOEXCEPT;
14920b57cec5SDimitry Andric
14930b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14940b57cec5SDimitry Andric    bool __invariants() const {return __base::__invariants();}
1495e40139ffSDimitry Andric
14960b57cec5SDimitry Andric    typedef typename __base::__map_const_pointer __map_const_pointer;
14970b57cec5SDimitry Andric
14980b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14990b57cec5SDimitry Andric    static size_type __recommend_blocks(size_type __n)
15000b57cec5SDimitry Andric    {
15010b57cec5SDimitry Andric        return __n / __base::__block_size + (__n % __base::__block_size != 0);
15020b57cec5SDimitry Andric    }
15030b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
15040b57cec5SDimitry Andric    size_type __capacity() const
15050b57cec5SDimitry Andric    {
15060b57cec5SDimitry Andric        return __base::__map_.size() == 0 ? 0 : __base::__map_.size() * __base::__block_size - 1;
15070b57cec5SDimitry Andric    }
15080b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1509e40139ffSDimitry Andric    size_type __block_count() const
1510e40139ffSDimitry Andric    {
1511e40139ffSDimitry Andric        return __base::__map_.size();
1512e40139ffSDimitry Andric    }
1513e40139ffSDimitry Andric
1514e40139ffSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
15150b57cec5SDimitry Andric    size_type __front_spare() const
15160b57cec5SDimitry Andric    {
15170b57cec5SDimitry Andric        return __base::__start_;
15180b57cec5SDimitry Andric    }
15190b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1520e40139ffSDimitry Andric    size_type __front_spare_blocks() const {
1521e40139ffSDimitry Andric      return __front_spare() / __base::__block_size;
1522e40139ffSDimitry Andric    }
1523e40139ffSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
15240b57cec5SDimitry Andric    size_type __back_spare() const
15250b57cec5SDimitry Andric    {
15260b57cec5SDimitry Andric        return __capacity() - (__base::__start_ + __base::size());
15270b57cec5SDimitry Andric    }
1528e40139ffSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1529e40139ffSDimitry Andric    size_type __back_spare_blocks() const {
1530e40139ffSDimitry Andric      return __back_spare() / __base::__block_size;
1531e40139ffSDimitry Andric    }
1532e40139ffSDimitry Andric
1533e40139ffSDimitry Andric private:
1534e40139ffSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1535e40139ffSDimitry Andric    bool __maybe_remove_front_spare(bool __keep_one = true) {
1536e40139ffSDimitry Andric      if (__front_spare_blocks() >= 2 || (!__keep_one && __front_spare_blocks())) {
1537e40139ffSDimitry Andric        __alloc_traits::deallocate(__base::__alloc(), __base::__map_.front(),
1538e40139ffSDimitry Andric                                   __base::__block_size);
1539e40139ffSDimitry Andric        __base::__map_.pop_front();
1540e40139ffSDimitry Andric        __base::__start_ -= __base::__block_size;
1541e40139ffSDimitry Andric        return true;
1542e40139ffSDimitry Andric      }
1543e40139ffSDimitry Andric      return false;
1544e40139ffSDimitry Andric    }
1545e40139ffSDimitry Andric
1546e40139ffSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1547e40139ffSDimitry Andric    bool __maybe_remove_back_spare(bool __keep_one = true) {
1548e40139ffSDimitry Andric      if (__back_spare_blocks() >= 2 || (!__keep_one && __back_spare_blocks())) {
1549e40139ffSDimitry Andric        __alloc_traits::deallocate(__base::__alloc(), __base::__map_.back(),
1550e40139ffSDimitry Andric                                   __base::__block_size);
1551e40139ffSDimitry Andric        __base::__map_.pop_back();
1552e40139ffSDimitry Andric        return true;
1553e40139ffSDimitry Andric      }
1554e40139ffSDimitry Andric      return false;
1555e40139ffSDimitry Andric    }
15560b57cec5SDimitry Andric
15570b57cec5SDimitry Andric    template <class _InpIter>
15580b57cec5SDimitry Andric        void __append(_InpIter __f, _InpIter __l,
1559480093f4SDimitry Andric                 typename enable_if<__is_cpp17_input_iterator<_InpIter>::value &&
1560480093f4SDimitry Andric                                   !__is_cpp17_forward_iterator<_InpIter>::value>::type* = 0);
15610b57cec5SDimitry Andric    template <class _ForIter>
15620b57cec5SDimitry Andric        void __append(_ForIter __f, _ForIter __l,
1563480093f4SDimitry Andric                      typename enable_if<__is_cpp17_forward_iterator<_ForIter>::value>::type* = 0);
15640b57cec5SDimitry Andric    void __append(size_type __n);
15650b57cec5SDimitry Andric    void __append(size_type __n, const value_type& __v);
15660b57cec5SDimitry Andric    void __erase_to_end(const_iterator __f);
15670b57cec5SDimitry Andric    void __add_front_capacity();
15680b57cec5SDimitry Andric    void __add_front_capacity(size_type __n);
15690b57cec5SDimitry Andric    void __add_back_capacity();
15700b57cec5SDimitry Andric    void __add_back_capacity(size_type __n);
15710b57cec5SDimitry Andric    iterator __move_and_check(iterator __f, iterator __l, iterator __r,
15720b57cec5SDimitry Andric                              const_pointer& __vt);
15730b57cec5SDimitry Andric    iterator __move_backward_and_check(iterator __f, iterator __l, iterator __r,
15740b57cec5SDimitry Andric                                       const_pointer& __vt);
15750b57cec5SDimitry Andric    void __move_construct_and_check(iterator __f, iterator __l,
15760b57cec5SDimitry Andric                                    iterator __r, const_pointer& __vt);
15770b57cec5SDimitry Andric    void __move_construct_backward_and_check(iterator __f, iterator __l,
15780b57cec5SDimitry Andric                                             iterator __r, const_pointer& __vt);
15790b57cec5SDimitry Andric
15800b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
15810b57cec5SDimitry Andric    void __copy_assign_alloc(const deque& __c)
15820b57cec5SDimitry Andric        {__copy_assign_alloc(__c, integral_constant<bool,
15830b57cec5SDimitry Andric                      __alloc_traits::propagate_on_container_copy_assignment::value>());}
15840b57cec5SDimitry Andric
15850b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
15860b57cec5SDimitry Andric    void __copy_assign_alloc(const deque& __c, true_type)
15870b57cec5SDimitry Andric        {
15880b57cec5SDimitry Andric            if (__base::__alloc() != __c.__alloc())
15890b57cec5SDimitry Andric            {
15900b57cec5SDimitry Andric                clear();
15910b57cec5SDimitry Andric                shrink_to_fit();
15920b57cec5SDimitry Andric            }
15930b57cec5SDimitry Andric            __base::__alloc() = __c.__alloc();
15940b57cec5SDimitry Andric            __base::__map_.__alloc() = __c.__map_.__alloc();
15950b57cec5SDimitry Andric        }
15960b57cec5SDimitry Andric
15970b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
15980b57cec5SDimitry Andric    void __copy_assign_alloc(const deque&, false_type)
15990b57cec5SDimitry Andric        {}
16000b57cec5SDimitry Andric
16010b57cec5SDimitry Andric    void __move_assign(deque& __c, true_type)
16020b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
16030b57cec5SDimitry Andric    void __move_assign(deque& __c, false_type);
16040b57cec5SDimitry Andric};
16050b57cec5SDimitry Andric
1606349cc55cSDimitry Andric#if _LIBCPP_STD_VER >= 17
16070b57cec5SDimitry Andrictemplate<class _InputIterator,
1608fe6060f1SDimitry Andric         class _Alloc = allocator<__iter_value_type<_InputIterator>>,
1609349cc55cSDimitry Andric         class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1610349cc55cSDimitry Andric         class = enable_if_t<__is_allocator<_Alloc>::value>
16110b57cec5SDimitry Andric         >
16120b57cec5SDimitry Andricdeque(_InputIterator, _InputIterator)
1613fe6060f1SDimitry Andric  -> deque<__iter_value_type<_InputIterator>, _Alloc>;
16140b57cec5SDimitry Andric
16150b57cec5SDimitry Andrictemplate<class _InputIterator,
16160b57cec5SDimitry Andric         class _Alloc,
1617349cc55cSDimitry Andric         class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1618349cc55cSDimitry Andric         class = enable_if_t<__is_allocator<_Alloc>::value>
16190b57cec5SDimitry Andric         >
16200b57cec5SDimitry Andricdeque(_InputIterator, _InputIterator, _Alloc)
1621fe6060f1SDimitry Andric  -> deque<__iter_value_type<_InputIterator>, _Alloc>;
16220b57cec5SDimitry Andric#endif
16230b57cec5SDimitry Andric
16240b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16250b57cec5SDimitry Andricdeque<_Tp, _Allocator>::deque(size_type __n)
16260b57cec5SDimitry Andric{
16270b57cec5SDimitry Andric    if (__n > 0)
16280b57cec5SDimitry Andric        __append(__n);
16290b57cec5SDimitry Andric}
16300b57cec5SDimitry Andric
16310b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
16320b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16330b57cec5SDimitry Andricdeque<_Tp, _Allocator>::deque(size_type __n, const _Allocator& __a)
16340b57cec5SDimitry Andric    : __base(__a)
16350b57cec5SDimitry Andric{
16360b57cec5SDimitry Andric    if (__n > 0)
16370b57cec5SDimitry Andric        __append(__n);
16380b57cec5SDimitry Andric}
16390b57cec5SDimitry Andric#endif
16400b57cec5SDimitry Andric
16410b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16420b57cec5SDimitry Andricdeque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v)
16430b57cec5SDimitry Andric{
16440b57cec5SDimitry Andric    if (__n > 0)
16450b57cec5SDimitry Andric        __append(__n, __v);
16460b57cec5SDimitry Andric}
16470b57cec5SDimitry Andric
16480b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16490b57cec5SDimitry Andrictemplate <class _InputIter>
16500b57cec5SDimitry Andricdeque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l,
1651480093f4SDimitry Andric              typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type*)
16520b57cec5SDimitry Andric{
16530b57cec5SDimitry Andric    __append(__f, __l);
16540b57cec5SDimitry Andric}
16550b57cec5SDimitry Andric
16560b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16570b57cec5SDimitry Andrictemplate <class _InputIter>
16580b57cec5SDimitry Andricdeque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1659480093f4SDimitry Andric              typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type*)
16600b57cec5SDimitry Andric    : __base(__a)
16610b57cec5SDimitry Andric{
16620b57cec5SDimitry Andric    __append(__f, __l);
16630b57cec5SDimitry Andric}
16640b57cec5SDimitry Andric
16650b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16660b57cec5SDimitry Andricdeque<_Tp, _Allocator>::deque(const deque& __c)
16670b57cec5SDimitry Andric    : __base(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))
16680b57cec5SDimitry Andric{
16690b57cec5SDimitry Andric    __append(__c.begin(), __c.end());
16700b57cec5SDimitry Andric}
16710b57cec5SDimitry Andric
16720b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
1673*81ad6265SDimitry Andricdeque<_Tp, _Allocator>::deque(const deque& __c, const __type_identity_t<allocator_type>& __a)
16740b57cec5SDimitry Andric    : __base(__a)
16750b57cec5SDimitry Andric{
16760b57cec5SDimitry Andric    __append(__c.begin(), __c.end());
16770b57cec5SDimitry Andric}
16780b57cec5SDimitry Andric
16790b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16800b57cec5SDimitry Andricdeque<_Tp, _Allocator>&
16810b57cec5SDimitry Andricdeque<_Tp, _Allocator>::operator=(const deque& __c)
16820b57cec5SDimitry Andric{
1683349cc55cSDimitry Andric    if (this != _VSTD::addressof(__c))
16840b57cec5SDimitry Andric    {
16850b57cec5SDimitry Andric        __copy_assign_alloc(__c);
16860b57cec5SDimitry Andric        assign(__c.begin(), __c.end());
16870b57cec5SDimitry Andric    }
16880b57cec5SDimitry Andric    return *this;
16890b57cec5SDimitry Andric}
16900b57cec5SDimitry Andric
16910b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
16920b57cec5SDimitry Andric
16930b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16940b57cec5SDimitry Andricdeque<_Tp, _Allocator>::deque(initializer_list<value_type> __il)
16950b57cec5SDimitry Andric{
16960b57cec5SDimitry Andric    __append(__il.begin(), __il.end());
16970b57cec5SDimitry Andric}
16980b57cec5SDimitry Andric
16990b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
17000b57cec5SDimitry Andricdeque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
17010b57cec5SDimitry Andric    : __base(__a)
17020b57cec5SDimitry Andric{
17030b57cec5SDimitry Andric    __append(__il.begin(), __il.end());
17040b57cec5SDimitry Andric}
17050b57cec5SDimitry Andric
17060b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
17070b57cec5SDimitry Andricinline
17080b57cec5SDimitry Andricdeque<_Tp, _Allocator>::deque(deque&& __c)
17090b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
17100b57cec5SDimitry Andric    : __base(_VSTD::move(__c))
17110b57cec5SDimitry Andric{
17120b57cec5SDimitry Andric}
17130b57cec5SDimitry Andric
17140b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
17150b57cec5SDimitry Andricinline
1716*81ad6265SDimitry Andricdeque<_Tp, _Allocator>::deque(deque&& __c, const __type_identity_t<allocator_type>& __a)
17170b57cec5SDimitry Andric    : __base(_VSTD::move(__c), __a)
17180b57cec5SDimitry Andric{
17190b57cec5SDimitry Andric    if (__a != __c.__alloc())
17200b57cec5SDimitry Andric    {
17210b57cec5SDimitry Andric        typedef move_iterator<iterator> _Ip;
17220b57cec5SDimitry Andric        assign(_Ip(__c.begin()), _Ip(__c.end()));
17230b57cec5SDimitry Andric    }
17240b57cec5SDimitry Andric}
17250b57cec5SDimitry Andric
17260b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
17270b57cec5SDimitry Andricinline
17280b57cec5SDimitry Andricdeque<_Tp, _Allocator>&
17290b57cec5SDimitry Andricdeque<_Tp, _Allocator>::operator=(deque&& __c)
17300b57cec5SDimitry Andric        _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
17310b57cec5SDimitry Andric                   is_nothrow_move_assignable<allocator_type>::value)
17320b57cec5SDimitry Andric{
17330b57cec5SDimitry Andric    __move_assign(__c, integral_constant<bool,
17340b57cec5SDimitry Andric          __alloc_traits::propagate_on_container_move_assignment::value>());
17350b57cec5SDimitry Andric    return *this;
17360b57cec5SDimitry Andric}
17370b57cec5SDimitry Andric
17380b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
17390b57cec5SDimitry Andricvoid
17400b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__move_assign(deque& __c, false_type)
17410b57cec5SDimitry Andric{
17420b57cec5SDimitry Andric    if (__base::__alloc() != __c.__alloc())
17430b57cec5SDimitry Andric    {
17440b57cec5SDimitry Andric        typedef move_iterator<iterator> _Ip;
17450b57cec5SDimitry Andric        assign(_Ip(__c.begin()), _Ip(__c.end()));
17460b57cec5SDimitry Andric    }
17470b57cec5SDimitry Andric    else
17480b57cec5SDimitry Andric        __move_assign(__c, true_type());
17490b57cec5SDimitry Andric}
17500b57cec5SDimitry Andric
17510b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
17520b57cec5SDimitry Andricvoid
17530b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__move_assign(deque& __c, true_type)
17540b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
17550b57cec5SDimitry Andric{
17560b57cec5SDimitry Andric    clear();
17570b57cec5SDimitry Andric    shrink_to_fit();
17580b57cec5SDimitry Andric    __base::__move_assign(__c);
17590b57cec5SDimitry Andric}
17600b57cec5SDimitry Andric
17610b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
17620b57cec5SDimitry Andric
17630b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
17640b57cec5SDimitry Andrictemplate <class _InputIter>
17650b57cec5SDimitry Andricvoid
17660b57cec5SDimitry Andricdeque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l,
1767480093f4SDimitry Andric                               typename enable_if<__is_cpp17_input_iterator<_InputIter>::value &&
1768480093f4SDimitry Andric                                                 !__is_cpp17_random_access_iterator<_InputIter>::value>::type*)
17690b57cec5SDimitry Andric{
17700b57cec5SDimitry Andric    iterator __i = __base::begin();
17710b57cec5SDimitry Andric    iterator __e = __base::end();
17720b57cec5SDimitry Andric    for (; __f != __l && __i != __e; ++__f, (void) ++__i)
17730b57cec5SDimitry Andric        *__i = *__f;
17740b57cec5SDimitry Andric    if (__f != __l)
17750b57cec5SDimitry Andric        __append(__f, __l);
17760b57cec5SDimitry Andric    else
17770b57cec5SDimitry Andric        __erase_to_end(__i);
17780b57cec5SDimitry Andric}
17790b57cec5SDimitry Andric
17800b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
17810b57cec5SDimitry Andrictemplate <class _RAIter>
17820b57cec5SDimitry Andricvoid
17830b57cec5SDimitry Andricdeque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l,
1784480093f4SDimitry Andric                               typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
17850b57cec5SDimitry Andric{
17860b57cec5SDimitry Andric    if (static_cast<size_type>(__l - __f) > __base::size())
17870b57cec5SDimitry Andric    {
17880b57cec5SDimitry Andric        _RAIter __m = __f + __base::size();
17890b57cec5SDimitry Andric        _VSTD::copy(__f, __m, __base::begin());
17900b57cec5SDimitry Andric        __append(__m, __l);
17910b57cec5SDimitry Andric    }
17920b57cec5SDimitry Andric    else
17930b57cec5SDimitry Andric        __erase_to_end(_VSTD::copy(__f, __l, __base::begin()));
17940b57cec5SDimitry Andric}
17950b57cec5SDimitry Andric
17960b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
17970b57cec5SDimitry Andricvoid
17980b57cec5SDimitry Andricdeque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v)
17990b57cec5SDimitry Andric{
18000b57cec5SDimitry Andric    if (__n > __base::size())
18010b57cec5SDimitry Andric    {
18020b57cec5SDimitry Andric        _VSTD::fill_n(__base::begin(), __base::size(), __v);
18030b57cec5SDimitry Andric        __n -= __base::size();
18040b57cec5SDimitry Andric        __append(__n, __v);
18050b57cec5SDimitry Andric    }
18060b57cec5SDimitry Andric    else
18070b57cec5SDimitry Andric        __erase_to_end(_VSTD::fill_n(__base::begin(), __n, __v));
18080b57cec5SDimitry Andric}
18090b57cec5SDimitry Andric
18100b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
18110b57cec5SDimitry Andricinline
18120b57cec5SDimitry Andric_Allocator
18130b57cec5SDimitry Andricdeque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT
18140b57cec5SDimitry Andric{
18150b57cec5SDimitry Andric    return __base::__alloc();
18160b57cec5SDimitry Andric}
18170b57cec5SDimitry Andric
18180b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
18190b57cec5SDimitry Andricvoid
18200b57cec5SDimitry Andricdeque<_Tp, _Allocator>::resize(size_type __n)
18210b57cec5SDimitry Andric{
18220b57cec5SDimitry Andric    if (__n > __base::size())
18230b57cec5SDimitry Andric        __append(__n - __base::size());
18240b57cec5SDimitry Andric    else if (__n < __base::size())
18250b57cec5SDimitry Andric        __erase_to_end(__base::begin() + __n);
18260b57cec5SDimitry Andric}
18270b57cec5SDimitry Andric
18280b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
18290b57cec5SDimitry Andricvoid
18300b57cec5SDimitry Andricdeque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v)
18310b57cec5SDimitry Andric{
18320b57cec5SDimitry Andric    if (__n > __base::size())
18330b57cec5SDimitry Andric        __append(__n - __base::size(), __v);
18340b57cec5SDimitry Andric    else if (__n < __base::size())
18350b57cec5SDimitry Andric        __erase_to_end(__base::begin() + __n);
18360b57cec5SDimitry Andric}
18370b57cec5SDimitry Andric
18380b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
18390b57cec5SDimitry Andricvoid
18400b57cec5SDimitry Andricdeque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
18410b57cec5SDimitry Andric{
18420b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
18430b57cec5SDimitry Andric    if (empty())
18440b57cec5SDimitry Andric    {
18450b57cec5SDimitry Andric        while (__base::__map_.size() > 0)
18460b57cec5SDimitry Andric        {
18470b57cec5SDimitry Andric            __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
18480b57cec5SDimitry Andric            __base::__map_.pop_back();
18490b57cec5SDimitry Andric        }
18500b57cec5SDimitry Andric        __base::__start_ = 0;
18510b57cec5SDimitry Andric    }
18520b57cec5SDimitry Andric    else
18530b57cec5SDimitry Andric    {
1854e40139ffSDimitry Andric      __maybe_remove_front_spare(/*__keep_one=*/false);
1855e40139ffSDimitry Andric      __maybe_remove_back_spare(/*__keep_one=*/false);
18560b57cec5SDimitry Andric    }
18570b57cec5SDimitry Andric    __base::__map_.shrink_to_fit();
18580b57cec5SDimitry Andric}
18590b57cec5SDimitry Andric
18600b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
18610b57cec5SDimitry Andricinline
18620b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::reference
18630b57cec5SDimitry Andricdeque<_Tp, _Allocator>::operator[](size_type __i) _NOEXCEPT
18640b57cec5SDimitry Andric{
18650b57cec5SDimitry Andric    size_type __p = __base::__start_ + __i;
18660b57cec5SDimitry Andric    return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
18670b57cec5SDimitry Andric}
18680b57cec5SDimitry Andric
18690b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
18700b57cec5SDimitry Andricinline
18710b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::const_reference
18720b57cec5SDimitry Andricdeque<_Tp, _Allocator>::operator[](size_type __i) const _NOEXCEPT
18730b57cec5SDimitry Andric{
18740b57cec5SDimitry Andric    size_type __p = __base::__start_ + __i;
18750b57cec5SDimitry Andric    return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
18760b57cec5SDimitry Andric}
18770b57cec5SDimitry Andric
18780b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
18790b57cec5SDimitry Andricinline
18800b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::reference
18810b57cec5SDimitry Andricdeque<_Tp, _Allocator>::at(size_type __i)
18820b57cec5SDimitry Andric{
18830b57cec5SDimitry Andric    if (__i >= __base::size())
1884349cc55cSDimitry Andric        _VSTD::__throw_out_of_range("deque");
18850b57cec5SDimitry Andric    size_type __p = __base::__start_ + __i;
18860b57cec5SDimitry Andric    return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
18870b57cec5SDimitry Andric}
18880b57cec5SDimitry Andric
18890b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
18900b57cec5SDimitry Andricinline
18910b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::const_reference
18920b57cec5SDimitry Andricdeque<_Tp, _Allocator>::at(size_type __i) const
18930b57cec5SDimitry Andric{
18940b57cec5SDimitry Andric    if (__i >= __base::size())
1895349cc55cSDimitry Andric        _VSTD::__throw_out_of_range("deque");
18960b57cec5SDimitry Andric    size_type __p = __base::__start_ + __i;
18970b57cec5SDimitry Andric    return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
18980b57cec5SDimitry Andric}
18990b57cec5SDimitry Andric
19000b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
19010b57cec5SDimitry Andricinline
19020b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::reference
19030b57cec5SDimitry Andricdeque<_Tp, _Allocator>::front() _NOEXCEPT
19040b57cec5SDimitry Andric{
19050b57cec5SDimitry Andric    return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
19060b57cec5SDimitry Andric                                      + __base::__start_ % __base::__block_size);
19070b57cec5SDimitry Andric}
19080b57cec5SDimitry Andric
19090b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
19100b57cec5SDimitry Andricinline
19110b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::const_reference
19120b57cec5SDimitry Andricdeque<_Tp, _Allocator>::front() const _NOEXCEPT
19130b57cec5SDimitry Andric{
19140b57cec5SDimitry Andric    return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
19150b57cec5SDimitry Andric                                      + __base::__start_ % __base::__block_size);
19160b57cec5SDimitry Andric}
19170b57cec5SDimitry Andric
19180b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
19190b57cec5SDimitry Andricinline
19200b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::reference
19210b57cec5SDimitry Andricdeque<_Tp, _Allocator>::back() _NOEXCEPT
19220b57cec5SDimitry Andric{
19230b57cec5SDimitry Andric    size_type __p = __base::size() + __base::__start_ - 1;
19240b57cec5SDimitry Andric    return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
19250b57cec5SDimitry Andric}
19260b57cec5SDimitry Andric
19270b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
19280b57cec5SDimitry Andricinline
19290b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::const_reference
19300b57cec5SDimitry Andricdeque<_Tp, _Allocator>::back() const _NOEXCEPT
19310b57cec5SDimitry Andric{
19320b57cec5SDimitry Andric    size_type __p = __base::size() + __base::__start_ - 1;
19330b57cec5SDimitry Andric    return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
19340b57cec5SDimitry Andric}
19350b57cec5SDimitry Andric
19360b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
19370b57cec5SDimitry Andricvoid
19380b57cec5SDimitry Andricdeque<_Tp, _Allocator>::push_back(const value_type& __v)
19390b57cec5SDimitry Andric{
19400b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
19410b57cec5SDimitry Andric    if (__back_spare() == 0)
19420b57cec5SDimitry Andric        __add_back_capacity();
19430b57cec5SDimitry Andric    // __back_spare() >= 1
19440b57cec5SDimitry Andric    __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
19450b57cec5SDimitry Andric    ++__base::size();
19460b57cec5SDimitry Andric}
19470b57cec5SDimitry Andric
19480b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
19490b57cec5SDimitry Andricvoid
19500b57cec5SDimitry Andricdeque<_Tp, _Allocator>::push_front(const value_type& __v)
19510b57cec5SDimitry Andric{
19520b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
19530b57cec5SDimitry Andric    if (__front_spare() == 0)
19540b57cec5SDimitry Andric        __add_front_capacity();
19550b57cec5SDimitry Andric    // __front_spare() >= 1
19560b57cec5SDimitry Andric    __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
19570b57cec5SDimitry Andric    --__base::__start_;
19580b57cec5SDimitry Andric    ++__base::size();
19590b57cec5SDimitry Andric}
19600b57cec5SDimitry Andric
19610b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
19620b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
19630b57cec5SDimitry Andricvoid
19640b57cec5SDimitry Andricdeque<_Tp, _Allocator>::push_back(value_type&& __v)
19650b57cec5SDimitry Andric{
19660b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
19670b57cec5SDimitry Andric    if (__back_spare() == 0)
19680b57cec5SDimitry Andric        __add_back_capacity();
19690b57cec5SDimitry Andric    // __back_spare() >= 1
19700b57cec5SDimitry Andric    __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
19710b57cec5SDimitry Andric    ++__base::size();
19720b57cec5SDimitry Andric}
19730b57cec5SDimitry Andric
19740b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
19750b57cec5SDimitry Andrictemplate <class... _Args>
19760b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
19770b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::reference
19780b57cec5SDimitry Andric#else
19790b57cec5SDimitry Andricvoid
19800b57cec5SDimitry Andric#endif
19810b57cec5SDimitry Andricdeque<_Tp, _Allocator>::emplace_back(_Args&&... __args)
19820b57cec5SDimitry Andric{
19830b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
19840b57cec5SDimitry Andric    if (__back_spare() == 0)
19850b57cec5SDimitry Andric        __add_back_capacity();
19860b57cec5SDimitry Andric    // __back_spare() >= 1
19870b57cec5SDimitry Andric    __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()),
19880b57cec5SDimitry Andric                              _VSTD::forward<_Args>(__args)...);
19890b57cec5SDimitry Andric    ++__base::size();
19900b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
19910b57cec5SDimitry Andric    return *--__base::end();
19920b57cec5SDimitry Andric#endif
19930b57cec5SDimitry Andric}
19940b57cec5SDimitry Andric
19950b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
19960b57cec5SDimitry Andricvoid
19970b57cec5SDimitry Andricdeque<_Tp, _Allocator>::push_front(value_type&& __v)
19980b57cec5SDimitry Andric{
19990b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
20000b57cec5SDimitry Andric    if (__front_spare() == 0)
20010b57cec5SDimitry Andric        __add_front_capacity();
20020b57cec5SDimitry Andric    // __front_spare() >= 1
20030b57cec5SDimitry Andric    __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
20040b57cec5SDimitry Andric    --__base::__start_;
20050b57cec5SDimitry Andric    ++__base::size();
20060b57cec5SDimitry Andric}
20070b57cec5SDimitry Andric
20080b57cec5SDimitry Andric
20090b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
20100b57cec5SDimitry Andrictemplate <class... _Args>
20110b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
20120b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::reference
20130b57cec5SDimitry Andric#else
20140b57cec5SDimitry Andricvoid
20150b57cec5SDimitry Andric#endif
20160b57cec5SDimitry Andricdeque<_Tp, _Allocator>::emplace_front(_Args&&... __args)
20170b57cec5SDimitry Andric{
20180b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
20190b57cec5SDimitry Andric    if (__front_spare() == 0)
20200b57cec5SDimitry Andric        __add_front_capacity();
20210b57cec5SDimitry Andric    // __front_spare() >= 1
20220b57cec5SDimitry Andric    __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
20230b57cec5SDimitry Andric    --__base::__start_;
20240b57cec5SDimitry Andric    ++__base::size();
20250b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
20260b57cec5SDimitry Andric    return *__base::begin();
20270b57cec5SDimitry Andric#endif
20280b57cec5SDimitry Andric}
20290b57cec5SDimitry Andric
20300b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
20310b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::iterator
20320b57cec5SDimitry Andricdeque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v)
20330b57cec5SDimitry Andric{
20340b57cec5SDimitry Andric    size_type __pos = __p - __base::begin();
20350b57cec5SDimitry Andric    size_type __to_end = __base::size() - __pos;
20360b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
20370b57cec5SDimitry Andric    if (__pos < __to_end)
20380b57cec5SDimitry Andric    {   // insert by shifting things backward
20390b57cec5SDimitry Andric        if (__front_spare() == 0)
20400b57cec5SDimitry Andric            __add_front_capacity();
20410b57cec5SDimitry Andric        // __front_spare() >= 1
20420b57cec5SDimitry Andric        if (__pos == 0)
20430b57cec5SDimitry Andric        {
20440b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
20450b57cec5SDimitry Andric            --__base::__start_;
20460b57cec5SDimitry Andric            ++__base::size();
20470b57cec5SDimitry Andric        }
20480b57cec5SDimitry Andric        else
20490b57cec5SDimitry Andric        {
20500b57cec5SDimitry Andric            iterator __b = __base::begin();
20510b57cec5SDimitry Andric            iterator __bm1 = _VSTD::prev(__b);
20520b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
20530b57cec5SDimitry Andric            --__base::__start_;
20540b57cec5SDimitry Andric            ++__base::size();
20550b57cec5SDimitry Andric            if (__pos > 1)
20560b57cec5SDimitry Andric                __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
20570b57cec5SDimitry Andric            *__b = _VSTD::move(__v);
20580b57cec5SDimitry Andric        }
20590b57cec5SDimitry Andric    }
20600b57cec5SDimitry Andric    else
20610b57cec5SDimitry Andric    {   // insert by shifting things forward
20620b57cec5SDimitry Andric        if (__back_spare() == 0)
20630b57cec5SDimitry Andric            __add_back_capacity();
20640b57cec5SDimitry Andric        // __back_capacity >= 1
20650b57cec5SDimitry Andric        size_type __de = __base::size() - __pos;
20660b57cec5SDimitry Andric        if (__de == 0)
20670b57cec5SDimitry Andric        {
20680b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
20690b57cec5SDimitry Andric            ++__base::size();
20700b57cec5SDimitry Andric        }
20710b57cec5SDimitry Andric        else
20720b57cec5SDimitry Andric        {
20730b57cec5SDimitry Andric            iterator __e = __base::end();
20740b57cec5SDimitry Andric            iterator __em1 = _VSTD::prev(__e);
20750b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
20760b57cec5SDimitry Andric            ++__base::size();
20770b57cec5SDimitry Andric            if (__de > 1)
20780b57cec5SDimitry Andric                __e = _VSTD::move_backward(__e - __de, __em1, __e);
20790b57cec5SDimitry Andric            *--__e = _VSTD::move(__v);
20800b57cec5SDimitry Andric        }
20810b57cec5SDimitry Andric    }
20820b57cec5SDimitry Andric    return __base::begin() + __pos;
20830b57cec5SDimitry Andric}
20840b57cec5SDimitry Andric
20850b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
20860b57cec5SDimitry Andrictemplate <class... _Args>
20870b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::iterator
20880b57cec5SDimitry Andricdeque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args)
20890b57cec5SDimitry Andric{
20900b57cec5SDimitry Andric    size_type __pos = __p - __base::begin();
20910b57cec5SDimitry Andric    size_type __to_end = __base::size() - __pos;
20920b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
20930b57cec5SDimitry Andric    if (__pos < __to_end)
20940b57cec5SDimitry Andric    {   // insert by shifting things backward
20950b57cec5SDimitry Andric        if (__front_spare() == 0)
20960b57cec5SDimitry Andric            __add_front_capacity();
20970b57cec5SDimitry Andric        // __front_spare() >= 1
20980b57cec5SDimitry Andric        if (__pos == 0)
20990b57cec5SDimitry Andric        {
21000b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
21010b57cec5SDimitry Andric            --__base::__start_;
21020b57cec5SDimitry Andric            ++__base::size();
21030b57cec5SDimitry Andric        }
21040b57cec5SDimitry Andric        else
21050b57cec5SDimitry Andric        {
21060b57cec5SDimitry Andric            __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
21070b57cec5SDimitry Andric            iterator __b = __base::begin();
21080b57cec5SDimitry Andric            iterator __bm1 = _VSTD::prev(__b);
21090b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
21100b57cec5SDimitry Andric            --__base::__start_;
21110b57cec5SDimitry Andric            ++__base::size();
21120b57cec5SDimitry Andric            if (__pos > 1)
21130b57cec5SDimitry Andric                __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
21140b57cec5SDimitry Andric            *__b = _VSTD::move(__tmp.get());
21150b57cec5SDimitry Andric        }
21160b57cec5SDimitry Andric    }
21170b57cec5SDimitry Andric    else
21180b57cec5SDimitry Andric    {   // insert by shifting things forward
21190b57cec5SDimitry Andric        if (__back_spare() == 0)
21200b57cec5SDimitry Andric            __add_back_capacity();
21210b57cec5SDimitry Andric        // __back_capacity >= 1
21220b57cec5SDimitry Andric        size_type __de = __base::size() - __pos;
21230b57cec5SDimitry Andric        if (__de == 0)
21240b57cec5SDimitry Andric        {
21250b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::forward<_Args>(__args)...);
21260b57cec5SDimitry Andric            ++__base::size();
21270b57cec5SDimitry Andric        }
21280b57cec5SDimitry Andric        else
21290b57cec5SDimitry Andric        {
21300b57cec5SDimitry Andric            __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
21310b57cec5SDimitry Andric            iterator __e = __base::end();
21320b57cec5SDimitry Andric            iterator __em1 = _VSTD::prev(__e);
21330b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
21340b57cec5SDimitry Andric            ++__base::size();
21350b57cec5SDimitry Andric            if (__de > 1)
21360b57cec5SDimitry Andric                __e = _VSTD::move_backward(__e - __de, __em1, __e);
21370b57cec5SDimitry Andric            *--__e = _VSTD::move(__tmp.get());
21380b57cec5SDimitry Andric        }
21390b57cec5SDimitry Andric    }
21400b57cec5SDimitry Andric    return __base::begin() + __pos;
21410b57cec5SDimitry Andric}
21420b57cec5SDimitry Andric
21430b57cec5SDimitry Andric#endif // _LIBCPP_CXX03_LANG
21440b57cec5SDimitry Andric
21450b57cec5SDimitry Andric
21460b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
21470b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::iterator
21480b57cec5SDimitry Andricdeque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v)
21490b57cec5SDimitry Andric{
21500b57cec5SDimitry Andric    size_type __pos = __p - __base::begin();
21510b57cec5SDimitry Andric    size_type __to_end = __base::size() - __pos;
21520b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
21530b57cec5SDimitry Andric    if (__pos < __to_end)
21540b57cec5SDimitry Andric    {   // insert by shifting things backward
21550b57cec5SDimitry Andric        if (__front_spare() == 0)
21560b57cec5SDimitry Andric            __add_front_capacity();
21570b57cec5SDimitry Andric        // __front_spare() >= 1
21580b57cec5SDimitry Andric        if (__pos == 0)
21590b57cec5SDimitry Andric        {
21600b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
21610b57cec5SDimitry Andric            --__base::__start_;
21620b57cec5SDimitry Andric            ++__base::size();
21630b57cec5SDimitry Andric        }
21640b57cec5SDimitry Andric        else
21650b57cec5SDimitry Andric        {
21660b57cec5SDimitry Andric            const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
21670b57cec5SDimitry Andric            iterator __b = __base::begin();
21680b57cec5SDimitry Andric            iterator __bm1 = _VSTD::prev(__b);
21690b57cec5SDimitry Andric            if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
21700b57cec5SDimitry Andric                __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
21710b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
21720b57cec5SDimitry Andric            --__base::__start_;
21730b57cec5SDimitry Andric            ++__base::size();
21740b57cec5SDimitry Andric            if (__pos > 1)
21750b57cec5SDimitry Andric                __b = __move_and_check(_VSTD::next(__b), __b + __pos, __b, __vt);
21760b57cec5SDimitry Andric            *__b = *__vt;
21770b57cec5SDimitry Andric        }
21780b57cec5SDimitry Andric    }
21790b57cec5SDimitry Andric    else
21800b57cec5SDimitry Andric    {   // insert by shifting things forward
21810b57cec5SDimitry Andric        if (__back_spare() == 0)
21820b57cec5SDimitry Andric            __add_back_capacity();
21830b57cec5SDimitry Andric        // __back_capacity >= 1
21840b57cec5SDimitry Andric        size_type __de = __base::size() - __pos;
21850b57cec5SDimitry Andric        if (__de == 0)
21860b57cec5SDimitry Andric        {
21870b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
21880b57cec5SDimitry Andric            ++__base::size();
21890b57cec5SDimitry Andric        }
21900b57cec5SDimitry Andric        else
21910b57cec5SDimitry Andric        {
21920b57cec5SDimitry Andric            const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
21930b57cec5SDimitry Andric            iterator __e = __base::end();
21940b57cec5SDimitry Andric            iterator __em1 = _VSTD::prev(__e);
21950b57cec5SDimitry Andric            if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
21960b57cec5SDimitry Andric                __vt = pointer_traits<const_pointer>::pointer_to(*__e);
21970b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
21980b57cec5SDimitry Andric            ++__base::size();
21990b57cec5SDimitry Andric            if (__de > 1)
22000b57cec5SDimitry Andric                __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
22010b57cec5SDimitry Andric            *--__e = *__vt;
22020b57cec5SDimitry Andric        }
22030b57cec5SDimitry Andric    }
22040b57cec5SDimitry Andric    return __base::begin() + __pos;
22050b57cec5SDimitry Andric}
22060b57cec5SDimitry Andric
22070b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
22080b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::iterator
22090b57cec5SDimitry Andricdeque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v)
22100b57cec5SDimitry Andric{
22110b57cec5SDimitry Andric    size_type __pos = __p - __base::begin();
22120b57cec5SDimitry Andric    size_type __to_end = __base::size() - __pos;
22130b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
22140b57cec5SDimitry Andric    if (__pos < __to_end)
22150b57cec5SDimitry Andric    {   // insert by shifting things backward
22160b57cec5SDimitry Andric        if (__n > __front_spare())
22170b57cec5SDimitry Andric            __add_front_capacity(__n - __front_spare());
22180b57cec5SDimitry Andric        // __n <= __front_spare()
22190b57cec5SDimitry Andric        iterator __old_begin = __base::begin();
22200b57cec5SDimitry Andric        iterator __i = __old_begin;
22210b57cec5SDimitry Andric        if (__n > __pos)
22220b57cec5SDimitry Andric        {
22230b57cec5SDimitry Andric            for (size_type __m = __n - __pos; __m; --__m, --__base::__start_, ++__base::size())
22240b57cec5SDimitry Andric                __alloc_traits::construct(__a, _VSTD::addressof(*--__i), __v);
22250b57cec5SDimitry Andric            __n = __pos;
22260b57cec5SDimitry Andric        }
22270b57cec5SDimitry Andric        if (__n > 0)
22280b57cec5SDimitry Andric        {
22290b57cec5SDimitry Andric            const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
22300b57cec5SDimitry Andric            iterator __obn = __old_begin + __n;
22310b57cec5SDimitry Andric            __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
22320b57cec5SDimitry Andric            if (__n < __pos)
22330b57cec5SDimitry Andric                __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
22340b57cec5SDimitry Andric            _VSTD::fill_n(__old_begin, __n, *__vt);
22350b57cec5SDimitry Andric        }
22360b57cec5SDimitry Andric    }
22370b57cec5SDimitry Andric    else
22380b57cec5SDimitry Andric    {   // insert by shifting things forward
22390b57cec5SDimitry Andric        size_type __back_capacity = __back_spare();
22400b57cec5SDimitry Andric        if (__n > __back_capacity)
22410b57cec5SDimitry Andric            __add_back_capacity(__n - __back_capacity);
22420b57cec5SDimitry Andric        // __n <= __back_capacity
22430b57cec5SDimitry Andric        iterator __old_end = __base::end();
22440b57cec5SDimitry Andric        iterator __i = __old_end;
22450b57cec5SDimitry Andric        size_type __de = __base::size() - __pos;
22460b57cec5SDimitry Andric        if (__n > __de)
22470b57cec5SDimitry Andric        {
2248349cc55cSDimitry Andric            for (size_type __m = __n - __de; __m; --__m, (void) ++__i, ++__base::size())
22490b57cec5SDimitry Andric                __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
22500b57cec5SDimitry Andric            __n = __de;
22510b57cec5SDimitry Andric        }
22520b57cec5SDimitry Andric        if (__n > 0)
22530b57cec5SDimitry Andric        {
22540b57cec5SDimitry Andric            const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
22550b57cec5SDimitry Andric            iterator __oen = __old_end - __n;
22560b57cec5SDimitry Andric            __move_construct_and_check(__oen, __old_end, __i, __vt);
22570b57cec5SDimitry Andric            if (__n < __de)
22580b57cec5SDimitry Andric                __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
22590b57cec5SDimitry Andric            _VSTD::fill_n(__old_end - __n, __n, *__vt);
22600b57cec5SDimitry Andric        }
22610b57cec5SDimitry Andric    }
22620b57cec5SDimitry Andric    return __base::begin() + __pos;
22630b57cec5SDimitry Andric}
22640b57cec5SDimitry Andric
22650b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
22660b57cec5SDimitry Andrictemplate <class _InputIter>
22670b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::iterator
22680b57cec5SDimitry Andricdeque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l,
2269480093f4SDimitry Andric                               typename enable_if<__is_cpp17_input_iterator<_InputIter>::value
2270480093f4SDimitry Andric                                               &&!__is_cpp17_forward_iterator<_InputIter>::value>::type*)
22710b57cec5SDimitry Andric{
22720b57cec5SDimitry Andric    __split_buffer<value_type, allocator_type&> __buf(__base::__alloc());
22730b57cec5SDimitry Andric    __buf.__construct_at_end(__f, __l);
22740b57cec5SDimitry Andric    typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
22750b57cec5SDimitry Andric    return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
22760b57cec5SDimitry Andric}
22770b57cec5SDimitry Andric
22780b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
22790b57cec5SDimitry Andrictemplate <class _ForwardIterator>
22800b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::iterator
22810b57cec5SDimitry Andricdeque<_Tp, _Allocator>::insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
2282480093f4SDimitry Andric                               typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value
2283480093f4SDimitry Andric                                               &&!__is_cpp17_bidirectional_iterator<_ForwardIterator>::value>::type*)
22840b57cec5SDimitry Andric{
22850b57cec5SDimitry Andric    size_type __n = _VSTD::distance(__f, __l);
22860b57cec5SDimitry Andric    __split_buffer<value_type, allocator_type&> __buf(__n, 0, __base::__alloc());
22870b57cec5SDimitry Andric    __buf.__construct_at_end(__f, __l);
22880b57cec5SDimitry Andric    typedef typename __split_buffer<value_type, allocator_type&>::iterator __fwd;
22890b57cec5SDimitry Andric    return insert(__p, move_iterator<__fwd>(__buf.begin()), move_iterator<__fwd>(__buf.end()));
22900b57cec5SDimitry Andric}
22910b57cec5SDimitry Andric
22920b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
22930b57cec5SDimitry Andrictemplate <class _BiIter>
22940b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::iterator
22950b57cec5SDimitry Andricdeque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l,
2296480093f4SDimitry Andric                               typename enable_if<__is_cpp17_bidirectional_iterator<_BiIter>::value>::type*)
22970b57cec5SDimitry Andric{
22980b57cec5SDimitry Andric    size_type __n = _VSTD::distance(__f, __l);
22990b57cec5SDimitry Andric    size_type __pos = __p - __base::begin();
23000b57cec5SDimitry Andric    size_type __to_end = __base::size() - __pos;
23010b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
23020b57cec5SDimitry Andric    if (__pos < __to_end)
23030b57cec5SDimitry Andric    {   // insert by shifting things backward
23040b57cec5SDimitry Andric        if (__n > __front_spare())
23050b57cec5SDimitry Andric            __add_front_capacity(__n - __front_spare());
23060b57cec5SDimitry Andric        // __n <= __front_spare()
23070b57cec5SDimitry Andric        iterator __old_begin = __base::begin();
23080b57cec5SDimitry Andric        iterator __i = __old_begin;
23090b57cec5SDimitry Andric        _BiIter __m = __f;
23100b57cec5SDimitry Andric        if (__n > __pos)
23110b57cec5SDimitry Andric        {
23120b57cec5SDimitry Andric            __m = __pos < __n / 2 ? _VSTD::prev(__l, __pos) : _VSTD::next(__f, __n - __pos);
23130b57cec5SDimitry Andric            for (_BiIter __j = __m; __j != __f; --__base::__start_, ++__base::size())
23140b57cec5SDimitry Andric                __alloc_traits::construct(__a, _VSTD::addressof(*--__i), *--__j);
23150b57cec5SDimitry Andric            __n = __pos;
23160b57cec5SDimitry Andric        }
23170b57cec5SDimitry Andric        if (__n > 0)
23180b57cec5SDimitry Andric        {
23190b57cec5SDimitry Andric            iterator __obn = __old_begin + __n;
23200b57cec5SDimitry Andric            for (iterator __j = __obn; __j != __old_begin;)
23210b57cec5SDimitry Andric            {
23220b57cec5SDimitry Andric                __alloc_traits::construct(__a, _VSTD::addressof(*--__i), _VSTD::move(*--__j));
23230b57cec5SDimitry Andric                --__base::__start_;
23240b57cec5SDimitry Andric                ++__base::size();
23250b57cec5SDimitry Andric            }
23260b57cec5SDimitry Andric            if (__n < __pos)
23270b57cec5SDimitry Andric                __old_begin = _VSTD::move(__obn, __old_begin + __pos, __old_begin);
23280b57cec5SDimitry Andric            _VSTD::copy(__m, __l, __old_begin);
23290b57cec5SDimitry Andric        }
23300b57cec5SDimitry Andric    }
23310b57cec5SDimitry Andric    else
23320b57cec5SDimitry Andric    {   // insert by shifting things forward
23330b57cec5SDimitry Andric        size_type __back_capacity = __back_spare();
23340b57cec5SDimitry Andric        if (__n > __back_capacity)
23350b57cec5SDimitry Andric            __add_back_capacity(__n - __back_capacity);
23360b57cec5SDimitry Andric        // __n <= __back_capacity
23370b57cec5SDimitry Andric        iterator __old_end = __base::end();
23380b57cec5SDimitry Andric        iterator __i = __old_end;
23390b57cec5SDimitry Andric        _BiIter __m = __l;
23400b57cec5SDimitry Andric        size_type __de = __base::size() - __pos;
23410b57cec5SDimitry Andric        if (__n > __de)
23420b57cec5SDimitry Andric        {
23430b57cec5SDimitry Andric            __m = __de < __n / 2 ? _VSTD::next(__f, __de) : _VSTD::prev(__l, __n - __de);
23440b57cec5SDimitry Andric            for (_BiIter __j = __m; __j != __l; ++__i, (void) ++__j, ++__base::size())
23450b57cec5SDimitry Andric                __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__j);
23460b57cec5SDimitry Andric            __n = __de;
23470b57cec5SDimitry Andric        }
23480b57cec5SDimitry Andric        if (__n > 0)
23490b57cec5SDimitry Andric        {
23500b57cec5SDimitry Andric            iterator __oen = __old_end - __n;
2351349cc55cSDimitry Andric            for (iterator __j = __oen; __j != __old_end; ++__i, (void) ++__j, ++__base::size())
23520b57cec5SDimitry Andric                __alloc_traits::construct(__a, _VSTD::addressof(*__i), _VSTD::move(*__j));
23530b57cec5SDimitry Andric            if (__n < __de)
23540b57cec5SDimitry Andric                __old_end = _VSTD::move_backward(__old_end - __de, __oen, __old_end);
23550b57cec5SDimitry Andric            _VSTD::copy_backward(__f, __m, __old_end);
23560b57cec5SDimitry Andric        }
23570b57cec5SDimitry Andric    }
23580b57cec5SDimitry Andric    return __base::begin() + __pos;
23590b57cec5SDimitry Andric}
23600b57cec5SDimitry Andric
23610b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
23620b57cec5SDimitry Andrictemplate <class _InpIter>
23630b57cec5SDimitry Andricvoid
23640b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l,
2365480093f4SDimitry Andric                                 typename enable_if<__is_cpp17_input_iterator<_InpIter>::value &&
2366480093f4SDimitry Andric                                                   !__is_cpp17_forward_iterator<_InpIter>::value>::type*)
23670b57cec5SDimitry Andric{
23680b57cec5SDimitry Andric    for (; __f != __l; ++__f)
23690b57cec5SDimitry Andric#ifdef _LIBCPP_CXX03_LANG
23700b57cec5SDimitry Andric        push_back(*__f);
23710b57cec5SDimitry Andric#else
23720b57cec5SDimitry Andric        emplace_back(*__f);
23730b57cec5SDimitry Andric#endif
23740b57cec5SDimitry Andric}
23750b57cec5SDimitry Andric
23760b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
23770b57cec5SDimitry Andrictemplate <class _ForIter>
23780b57cec5SDimitry Andricvoid
23790b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l,
2380480093f4SDimitry Andric                                 typename enable_if<__is_cpp17_forward_iterator<_ForIter>::value>::type*)
23810b57cec5SDimitry Andric{
23820b57cec5SDimitry Andric    size_type __n = _VSTD::distance(__f, __l);
23830b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
23840b57cec5SDimitry Andric    size_type __back_capacity = __back_spare();
23850b57cec5SDimitry Andric    if (__n > __back_capacity)
23860b57cec5SDimitry Andric        __add_back_capacity(__n - __back_capacity);
23870b57cec5SDimitry Andric    // __n <= __back_capacity
2388e40139ffSDimitry Andric    for (__deque_block_range __br : __deque_range(__base::end(), __base::end() + __n)) {
2389e40139ffSDimitry Andric      _ConstructTransaction __tx(this, __br);
2390e40139ffSDimitry Andric      for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void)++__f) {
2391e8d8bef9SDimitry Andric        __alloc_traits::construct(__a, _VSTD::__to_address(__tx.__pos_), *__f);
2392e40139ffSDimitry Andric      }
2393e40139ffSDimitry Andric    }
23940b57cec5SDimitry Andric}
23950b57cec5SDimitry Andric
23960b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
23970b57cec5SDimitry Andricvoid
23980b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__append(size_type __n)
23990b57cec5SDimitry Andric{
24000b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
24010b57cec5SDimitry Andric    size_type __back_capacity = __back_spare();
24020b57cec5SDimitry Andric    if (__n > __back_capacity)
24030b57cec5SDimitry Andric        __add_back_capacity(__n - __back_capacity);
24040b57cec5SDimitry Andric    // __n <= __back_capacity
2405e40139ffSDimitry Andric    for (__deque_block_range __br : __deque_range(__base::end(), __base::end() + __n)) {
2406e40139ffSDimitry Andric      _ConstructTransaction __tx(this, __br);
2407e40139ffSDimitry Andric      for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
2408e8d8bef9SDimitry Andric        __alloc_traits::construct(__a, _VSTD::__to_address(__tx.__pos_));
2409e40139ffSDimitry Andric      }
2410e40139ffSDimitry Andric    }
24110b57cec5SDimitry Andric}
24120b57cec5SDimitry Andric
24130b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
24140b57cec5SDimitry Andricvoid
24150b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v)
24160b57cec5SDimitry Andric{
24170b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
24180b57cec5SDimitry Andric    size_type __back_capacity = __back_spare();
24190b57cec5SDimitry Andric    if (__n > __back_capacity)
24200b57cec5SDimitry Andric        __add_back_capacity(__n - __back_capacity);
24210b57cec5SDimitry Andric    // __n <= __back_capacity
2422e40139ffSDimitry Andric    for (__deque_block_range __br : __deque_range(__base::end(), __base::end() + __n)) {
2423e40139ffSDimitry Andric      _ConstructTransaction __tx(this, __br);
2424e40139ffSDimitry Andric      for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
2425e8d8bef9SDimitry Andric        __alloc_traits::construct(__a, _VSTD::__to_address(__tx.__pos_), __v);
2426e40139ffSDimitry Andric      }
2427e40139ffSDimitry Andric    }
2428e40139ffSDimitry Andric
24290b57cec5SDimitry Andric}
24300b57cec5SDimitry Andric
24310b57cec5SDimitry Andric// Create front capacity for one block of elements.
24320b57cec5SDimitry Andric// Strong guarantee.  Either do it or don't touch anything.
24330b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
24340b57cec5SDimitry Andricvoid
24350b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__add_front_capacity()
24360b57cec5SDimitry Andric{
24370b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
24380b57cec5SDimitry Andric    if (__back_spare() >= __base::__block_size)
24390b57cec5SDimitry Andric    {
24400b57cec5SDimitry Andric        __base::__start_ += __base::__block_size;
24410b57cec5SDimitry Andric        pointer __pt = __base::__map_.back();
24420b57cec5SDimitry Andric        __base::__map_.pop_back();
24430b57cec5SDimitry Andric        __base::__map_.push_front(__pt);
24440b57cec5SDimitry Andric    }
24450b57cec5SDimitry Andric    // Else if __base::__map_.size() < __base::__map_.capacity() then we need to allocate 1 buffer
24460b57cec5SDimitry Andric    else if (__base::__map_.size() < __base::__map_.capacity())
24470b57cec5SDimitry Andric    {   // we can put the new buffer into the map, but don't shift things around
24480b57cec5SDimitry Andric        // until all buffers are allocated.  If we throw, we don't need to fix
24490b57cec5SDimitry Andric        // anything up (any added buffers are undetectible)
24500b57cec5SDimitry Andric        if (__base::__map_.__front_spare() > 0)
24510b57cec5SDimitry Andric            __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
24520b57cec5SDimitry Andric        else
24530b57cec5SDimitry Andric        {
24540b57cec5SDimitry Andric            __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
24550b57cec5SDimitry Andric            // Done allocating, reorder capacity
24560b57cec5SDimitry Andric            pointer __pt = __base::__map_.back();
24570b57cec5SDimitry Andric            __base::__map_.pop_back();
24580b57cec5SDimitry Andric            __base::__map_.push_front(__pt);
24590b57cec5SDimitry Andric        }
24600b57cec5SDimitry Andric        __base::__start_ = __base::__map_.size() == 1 ?
24610b57cec5SDimitry Andric                               __base::__block_size / 2 :
24620b57cec5SDimitry Andric                               __base::__start_ + __base::__block_size;
24630b57cec5SDimitry Andric    }
24640b57cec5SDimitry Andric    // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
24650b57cec5SDimitry Andric    else
24660b57cec5SDimitry Andric    {
24670b57cec5SDimitry Andric        __split_buffer<pointer, typename __base::__pointer_allocator&>
24680b57cec5SDimitry Andric            __buf(max<size_type>(2 * __base::__map_.capacity(), 1),
24690b57cec5SDimitry Andric                  0, __base::__map_.__alloc());
24700b57cec5SDimitry Andric
24710b57cec5SDimitry Andric        typedef __allocator_destructor<_Allocator> _Dp;
24720b57cec5SDimitry Andric        unique_ptr<pointer, _Dp> __hold(
24730b57cec5SDimitry Andric            __alloc_traits::allocate(__a, __base::__block_size),
24740b57cec5SDimitry Andric                _Dp(__a, __base::__block_size));
24750b57cec5SDimitry Andric        __buf.push_back(__hold.get());
24760b57cec5SDimitry Andric        __hold.release();
24770b57cec5SDimitry Andric
24780b57cec5SDimitry Andric        for (typename __base::__map_pointer __i = __base::__map_.begin();
24790b57cec5SDimitry Andric                __i != __base::__map_.end(); ++__i)
24800b57cec5SDimitry Andric            __buf.push_back(*__i);
24810b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__first_, __buf.__first_);
24820b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
24830b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__end_, __buf.__end_);
24840b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
24850b57cec5SDimitry Andric        __base::__start_ = __base::__map_.size() == 1 ?
24860b57cec5SDimitry Andric                               __base::__block_size / 2 :
24870b57cec5SDimitry Andric                               __base::__start_ + __base::__block_size;
24880b57cec5SDimitry Andric    }
24890b57cec5SDimitry Andric}
24900b57cec5SDimitry Andric
24910b57cec5SDimitry Andric// Create front capacity for __n elements.
24920b57cec5SDimitry Andric// Strong guarantee.  Either do it or don't touch anything.
24930b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
24940b57cec5SDimitry Andricvoid
24950b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__add_front_capacity(size_type __n)
24960b57cec5SDimitry Andric{
24970b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
24980b57cec5SDimitry Andric    size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
24990b57cec5SDimitry Andric    // Number of unused blocks at back:
25000b57cec5SDimitry Andric    size_type __back_capacity = __back_spare() / __base::__block_size;
25010b57cec5SDimitry Andric    __back_capacity = _VSTD::min(__back_capacity, __nb);  // don't take more than you need
25020b57cec5SDimitry Andric    __nb -= __back_capacity;  // number of blocks need to allocate
25030b57cec5SDimitry Andric    // If __nb == 0, then we have sufficient capacity.
25040b57cec5SDimitry Andric    if (__nb == 0)
25050b57cec5SDimitry Andric    {
25060b57cec5SDimitry Andric        __base::__start_ += __base::__block_size * __back_capacity;
25070b57cec5SDimitry Andric        for (; __back_capacity > 0; --__back_capacity)
25080b57cec5SDimitry Andric        {
25090b57cec5SDimitry Andric            pointer __pt = __base::__map_.back();
25100b57cec5SDimitry Andric            __base::__map_.pop_back();
25110b57cec5SDimitry Andric            __base::__map_.push_front(__pt);
25120b57cec5SDimitry Andric        }
25130b57cec5SDimitry Andric    }
25140b57cec5SDimitry Andric    // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
25150b57cec5SDimitry Andric    else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
25160b57cec5SDimitry Andric    {   // we can put the new buffers into the map, but don't shift things around
25170b57cec5SDimitry Andric        // until all buffers are allocated.  If we throw, we don't need to fix
25180b57cec5SDimitry Andric        // anything up (any added buffers are undetectible)
25190b57cec5SDimitry Andric        for (; __nb > 0; --__nb, __base::__start_ += __base::__block_size - (__base::__map_.size() == 1))
25200b57cec5SDimitry Andric        {
25210b57cec5SDimitry Andric            if (__base::__map_.__front_spare() == 0)
25220b57cec5SDimitry Andric                break;
25230b57cec5SDimitry Andric            __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
25240b57cec5SDimitry Andric        }
25250b57cec5SDimitry Andric        for (; __nb > 0; --__nb, ++__back_capacity)
25260b57cec5SDimitry Andric            __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
25270b57cec5SDimitry Andric        // Done allocating, reorder capacity
25280b57cec5SDimitry Andric        __base::__start_ += __back_capacity * __base::__block_size;
25290b57cec5SDimitry Andric        for (; __back_capacity > 0; --__back_capacity)
25300b57cec5SDimitry Andric        {
25310b57cec5SDimitry Andric            pointer __pt = __base::__map_.back();
25320b57cec5SDimitry Andric            __base::__map_.pop_back();
25330b57cec5SDimitry Andric            __base::__map_.push_front(__pt);
25340b57cec5SDimitry Andric        }
25350b57cec5SDimitry Andric    }
25360b57cec5SDimitry Andric    // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
25370b57cec5SDimitry Andric    else
25380b57cec5SDimitry Andric    {
25390b57cec5SDimitry Andric        size_type __ds = (__nb + __back_capacity) * __base::__block_size - __base::__map_.empty();
25400b57cec5SDimitry Andric        __split_buffer<pointer, typename __base::__pointer_allocator&>
25410b57cec5SDimitry Andric            __buf(max<size_type>(2* __base::__map_.capacity(),
25420b57cec5SDimitry Andric                                 __nb + __base::__map_.size()),
25430b57cec5SDimitry Andric                  0, __base::__map_.__alloc());
25440b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
25450b57cec5SDimitry Andric        try
25460b57cec5SDimitry Andric        {
25470b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS
25480b57cec5SDimitry Andric            for (; __nb > 0; --__nb)
25490b57cec5SDimitry Andric                __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
25500b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
25510b57cec5SDimitry Andric        }
25520b57cec5SDimitry Andric        catch (...)
25530b57cec5SDimitry Andric        {
25540b57cec5SDimitry Andric            for (typename __base::__map_pointer __i = __buf.begin();
25550b57cec5SDimitry Andric                    __i != __buf.end(); ++__i)
25560b57cec5SDimitry Andric                __alloc_traits::deallocate(__a, *__i, __base::__block_size);
25570b57cec5SDimitry Andric            throw;
25580b57cec5SDimitry Andric        }
25590b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS
25600b57cec5SDimitry Andric        for (; __back_capacity > 0; --__back_capacity)
25610b57cec5SDimitry Andric        {
25620b57cec5SDimitry Andric            __buf.push_back(__base::__map_.back());
25630b57cec5SDimitry Andric            __base::__map_.pop_back();
25640b57cec5SDimitry Andric        }
25650b57cec5SDimitry Andric        for (typename __base::__map_pointer __i = __base::__map_.begin();
25660b57cec5SDimitry Andric                __i != __base::__map_.end(); ++__i)
25670b57cec5SDimitry Andric            __buf.push_back(*__i);
25680b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__first_, __buf.__first_);
25690b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
25700b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__end_, __buf.__end_);
25710b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
25720b57cec5SDimitry Andric        __base::__start_ += __ds;
25730b57cec5SDimitry Andric    }
25740b57cec5SDimitry Andric}
25750b57cec5SDimitry Andric
25760b57cec5SDimitry Andric// Create back capacity for one block of elements.
25770b57cec5SDimitry Andric// Strong guarantee.  Either do it or don't touch anything.
25780b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
25790b57cec5SDimitry Andricvoid
25800b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__add_back_capacity()
25810b57cec5SDimitry Andric{
25820b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
25830b57cec5SDimitry Andric    if (__front_spare() >= __base::__block_size)
25840b57cec5SDimitry Andric    {
25850b57cec5SDimitry Andric        __base::__start_ -= __base::__block_size;
25860b57cec5SDimitry Andric        pointer __pt = __base::__map_.front();
25870b57cec5SDimitry Andric        __base::__map_.pop_front();
25880b57cec5SDimitry Andric        __base::__map_.push_back(__pt);
25890b57cec5SDimitry Andric    }
25900b57cec5SDimitry Andric    // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
25910b57cec5SDimitry Andric    else if (__base::__map_.size() < __base::__map_.capacity())
25920b57cec5SDimitry Andric    {   // we can put the new buffer into the map, but don't shift things around
25930b57cec5SDimitry Andric        // until it is allocated.  If we throw, we don't need to fix
25940b57cec5SDimitry Andric        // anything up (any added buffers are undetectible)
25950b57cec5SDimitry Andric        if (__base::__map_.__back_spare() != 0)
25960b57cec5SDimitry Andric            __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
25970b57cec5SDimitry Andric        else
25980b57cec5SDimitry Andric        {
25990b57cec5SDimitry Andric            __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
26000b57cec5SDimitry Andric            // Done allocating, reorder capacity
26010b57cec5SDimitry Andric            pointer __pt = __base::__map_.front();
26020b57cec5SDimitry Andric            __base::__map_.pop_front();
26030b57cec5SDimitry Andric            __base::__map_.push_back(__pt);
26040b57cec5SDimitry Andric        }
26050b57cec5SDimitry Andric    }
26060b57cec5SDimitry Andric    // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
26070b57cec5SDimitry Andric    else
26080b57cec5SDimitry Andric    {
26090b57cec5SDimitry Andric        __split_buffer<pointer, typename __base::__pointer_allocator&>
26100b57cec5SDimitry Andric            __buf(max<size_type>(2* __base::__map_.capacity(), 1),
26110b57cec5SDimitry Andric                  __base::__map_.size(),
26120b57cec5SDimitry Andric                  __base::__map_.__alloc());
26130b57cec5SDimitry Andric
26140b57cec5SDimitry Andric        typedef __allocator_destructor<_Allocator> _Dp;
26150b57cec5SDimitry Andric        unique_ptr<pointer, _Dp> __hold(
26160b57cec5SDimitry Andric            __alloc_traits::allocate(__a, __base::__block_size),
26170b57cec5SDimitry Andric                _Dp(__a, __base::__block_size));
26180b57cec5SDimitry Andric        __buf.push_back(__hold.get());
26190b57cec5SDimitry Andric        __hold.release();
26200b57cec5SDimitry Andric
26210b57cec5SDimitry Andric        for (typename __base::__map_pointer __i = __base::__map_.end();
26220b57cec5SDimitry Andric                __i != __base::__map_.begin();)
26230b57cec5SDimitry Andric            __buf.push_front(*--__i);
26240b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__first_, __buf.__first_);
26250b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
26260b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__end_, __buf.__end_);
26270b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
26280b57cec5SDimitry Andric    }
26290b57cec5SDimitry Andric}
26300b57cec5SDimitry Andric
26310b57cec5SDimitry Andric// Create back capacity for __n elements.
26320b57cec5SDimitry Andric// Strong guarantee.  Either do it or don't touch anything.
26330b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
26340b57cec5SDimitry Andricvoid
26350b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__add_back_capacity(size_type __n)
26360b57cec5SDimitry Andric{
26370b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
26380b57cec5SDimitry Andric    size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
26390b57cec5SDimitry Andric    // Number of unused blocks at front:
26400b57cec5SDimitry Andric    size_type __front_capacity = __front_spare() / __base::__block_size;
26410b57cec5SDimitry Andric    __front_capacity = _VSTD::min(__front_capacity, __nb);  // don't take more than you need
26420b57cec5SDimitry Andric    __nb -= __front_capacity;  // number of blocks need to allocate
26430b57cec5SDimitry Andric    // If __nb == 0, then we have sufficient capacity.
26440b57cec5SDimitry Andric    if (__nb == 0)
26450b57cec5SDimitry Andric    {
26460b57cec5SDimitry Andric        __base::__start_ -= __base::__block_size * __front_capacity;
26470b57cec5SDimitry Andric        for (; __front_capacity > 0; --__front_capacity)
26480b57cec5SDimitry Andric        {
26490b57cec5SDimitry Andric            pointer __pt = __base::__map_.front();
26500b57cec5SDimitry Andric            __base::__map_.pop_front();
26510b57cec5SDimitry Andric            __base::__map_.push_back(__pt);
26520b57cec5SDimitry Andric        }
26530b57cec5SDimitry Andric    }
26540b57cec5SDimitry Andric    // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
26550b57cec5SDimitry Andric    else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
26560b57cec5SDimitry Andric    {   // we can put the new buffers into the map, but don't shift things around
26570b57cec5SDimitry Andric        // until all buffers are allocated.  If we throw, we don't need to fix
26580b57cec5SDimitry Andric        // anything up (any added buffers are undetectible)
26590b57cec5SDimitry Andric        for (; __nb > 0; --__nb)
26600b57cec5SDimitry Andric        {
26610b57cec5SDimitry Andric            if (__base::__map_.__back_spare() == 0)
26620b57cec5SDimitry Andric                break;
26630b57cec5SDimitry Andric            __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
26640b57cec5SDimitry Andric        }
26650b57cec5SDimitry Andric        for (; __nb > 0; --__nb, ++__front_capacity, __base::__start_ +=
26660b57cec5SDimitry Andric                                 __base::__block_size - (__base::__map_.size() == 1))
26670b57cec5SDimitry Andric            __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
26680b57cec5SDimitry Andric        // Done allocating, reorder capacity
26690b57cec5SDimitry Andric        __base::__start_ -= __base::__block_size * __front_capacity;
26700b57cec5SDimitry Andric        for (; __front_capacity > 0; --__front_capacity)
26710b57cec5SDimitry Andric        {
26720b57cec5SDimitry Andric            pointer __pt = __base::__map_.front();
26730b57cec5SDimitry Andric            __base::__map_.pop_front();
26740b57cec5SDimitry Andric            __base::__map_.push_back(__pt);
26750b57cec5SDimitry Andric        }
26760b57cec5SDimitry Andric    }
26770b57cec5SDimitry Andric    // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
26780b57cec5SDimitry Andric    else
26790b57cec5SDimitry Andric    {
26800b57cec5SDimitry Andric        size_type __ds = __front_capacity * __base::__block_size;
26810b57cec5SDimitry Andric        __split_buffer<pointer, typename __base::__pointer_allocator&>
26820b57cec5SDimitry Andric            __buf(max<size_type>(2* __base::__map_.capacity(),
26830b57cec5SDimitry Andric                                 __nb + __base::__map_.size()),
26840b57cec5SDimitry Andric                  __base::__map_.size() - __front_capacity,
26850b57cec5SDimitry Andric                  __base::__map_.__alloc());
26860b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
26870b57cec5SDimitry Andric        try
26880b57cec5SDimitry Andric        {
26890b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS
26900b57cec5SDimitry Andric            for (; __nb > 0; --__nb)
26910b57cec5SDimitry Andric                __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
26920b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
26930b57cec5SDimitry Andric        }
26940b57cec5SDimitry Andric        catch (...)
26950b57cec5SDimitry Andric        {
26960b57cec5SDimitry Andric            for (typename __base::__map_pointer __i = __buf.begin();
26970b57cec5SDimitry Andric                    __i != __buf.end(); ++__i)
26980b57cec5SDimitry Andric                __alloc_traits::deallocate(__a, *__i, __base::__block_size);
26990b57cec5SDimitry Andric            throw;
27000b57cec5SDimitry Andric        }
27010b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS
27020b57cec5SDimitry Andric        for (; __front_capacity > 0; --__front_capacity)
27030b57cec5SDimitry Andric        {
27040b57cec5SDimitry Andric            __buf.push_back(__base::__map_.front());
27050b57cec5SDimitry Andric            __base::__map_.pop_front();
27060b57cec5SDimitry Andric        }
27070b57cec5SDimitry Andric        for (typename __base::__map_pointer __i = __base::__map_.end();
27080b57cec5SDimitry Andric                __i != __base::__map_.begin();)
27090b57cec5SDimitry Andric            __buf.push_front(*--__i);
27100b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__first_, __buf.__first_);
27110b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
27120b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__end_, __buf.__end_);
27130b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
27140b57cec5SDimitry Andric        __base::__start_ -= __ds;
27150b57cec5SDimitry Andric    }
27160b57cec5SDimitry Andric}
27170b57cec5SDimitry Andric
27180b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
27190b57cec5SDimitry Andricvoid
27200b57cec5SDimitry Andricdeque<_Tp, _Allocator>::pop_front()
27210b57cec5SDimitry Andric{
27220b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
2723e8d8bef9SDimitry Andric    __alloc_traits::destroy(__a, _VSTD::__to_address(*(__base::__map_.begin() +
27240b57cec5SDimitry Andric                                                    __base::__start_ / __base::__block_size) +
27250b57cec5SDimitry Andric                                                    __base::__start_ % __base::__block_size));
27260b57cec5SDimitry Andric    --__base::size();
2727e40139ffSDimitry Andric    ++__base::__start_;
2728e40139ffSDimitry Andric    __maybe_remove_front_spare();
27290b57cec5SDimitry Andric}
27300b57cec5SDimitry Andric
27310b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
27320b57cec5SDimitry Andricvoid
27330b57cec5SDimitry Andricdeque<_Tp, _Allocator>::pop_back()
27340b57cec5SDimitry Andric{
2735fe6060f1SDimitry Andric    _LIBCPP_ASSERT(!empty(), "deque::pop_back called on an empty deque");
27360b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
27370b57cec5SDimitry Andric    size_type __p = __base::size() + __base::__start_ - 1;
2738e8d8bef9SDimitry Andric    __alloc_traits::destroy(__a, _VSTD::__to_address(*(__base::__map_.begin() +
27390b57cec5SDimitry Andric                                                    __p / __base::__block_size) +
27400b57cec5SDimitry Andric                                                    __p % __base::__block_size));
27410b57cec5SDimitry Andric    --__base::size();
2742e40139ffSDimitry Andric    __maybe_remove_back_spare();
27430b57cec5SDimitry Andric}
27440b57cec5SDimitry Andric
27450b57cec5SDimitry Andric// move assign [__f, __l) to [__r, __r + (__l-__f)).
27460b57cec5SDimitry Andric// If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
27470b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
27480b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::iterator
27490b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r,
27500b57cec5SDimitry Andric                                         const_pointer& __vt)
27510b57cec5SDimitry Andric{
27520b57cec5SDimitry Andric    // as if
27530b57cec5SDimitry Andric    //   for (; __f != __l; ++__f, ++__r)
27540b57cec5SDimitry Andric    //       *__r = _VSTD::move(*__f);
27550b57cec5SDimitry Andric    difference_type __n = __l - __f;
27560b57cec5SDimitry Andric    while (__n > 0)
27570b57cec5SDimitry Andric    {
27580b57cec5SDimitry Andric        pointer __fb = __f.__ptr_;
27590b57cec5SDimitry Andric        pointer __fe = *__f.__m_iter_ + __base::__block_size;
27600b57cec5SDimitry Andric        difference_type __bs = __fe - __fb;
27610b57cec5SDimitry Andric        if (__bs > __n)
27620b57cec5SDimitry Andric        {
27630b57cec5SDimitry Andric            __bs = __n;
27640b57cec5SDimitry Andric            __fe = __fb + __bs;
27650b57cec5SDimitry Andric        }
27660b57cec5SDimitry Andric        if (__fb <= __vt && __vt < __fe)
27670b57cec5SDimitry Andric            __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) -= __f - __r).__ptr_;
27680b57cec5SDimitry Andric        __r = _VSTD::move(__fb, __fe, __r);
27690b57cec5SDimitry Andric        __n -= __bs;
27700b57cec5SDimitry Andric        __f += __bs;
27710b57cec5SDimitry Andric    }
27720b57cec5SDimitry Andric    return __r;
27730b57cec5SDimitry Andric}
27740b57cec5SDimitry Andric
27750b57cec5SDimitry Andric// move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
27760b57cec5SDimitry Andric// If __vt points into [__f, __l), then add (__r - __l) to __vt.
27770b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
27780b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::iterator
27790b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r,
27800b57cec5SDimitry Andric                                                  const_pointer& __vt)
27810b57cec5SDimitry Andric{
27820b57cec5SDimitry Andric    // as if
27830b57cec5SDimitry Andric    //   while (__f != __l)
27840b57cec5SDimitry Andric    //       *--__r = _VSTD::move(*--__l);
27850b57cec5SDimitry Andric    difference_type __n = __l - __f;
27860b57cec5SDimitry Andric    while (__n > 0)
27870b57cec5SDimitry Andric    {
27880b57cec5SDimitry Andric        --__l;
27890b57cec5SDimitry Andric        pointer __lb = *__l.__m_iter_;
27900b57cec5SDimitry Andric        pointer __le = __l.__ptr_ + 1;
27910b57cec5SDimitry Andric        difference_type __bs = __le - __lb;
27920b57cec5SDimitry Andric        if (__bs > __n)
27930b57cec5SDimitry Andric        {
27940b57cec5SDimitry Andric            __bs = __n;
27950b57cec5SDimitry Andric            __lb = __le - __bs;
27960b57cec5SDimitry Andric        }
27970b57cec5SDimitry Andric        if (__lb <= __vt && __vt < __le)
27980b57cec5SDimitry Andric            __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) += __r - __l - 1).__ptr_;
27990b57cec5SDimitry Andric        __r = _VSTD::move_backward(__lb, __le, __r);
28000b57cec5SDimitry Andric        __n -= __bs;
28010b57cec5SDimitry Andric        __l -= __bs - 1;
28020b57cec5SDimitry Andric    }
28030b57cec5SDimitry Andric    return __r;
28040b57cec5SDimitry Andric}
28050b57cec5SDimitry Andric
28060b57cec5SDimitry Andric// move construct [__f, __l) to [__r, __r + (__l-__f)).
28070b57cec5SDimitry Andric// If __vt points into [__f, __l), then add (__r - __f) to __vt.
28080b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
28090b57cec5SDimitry Andricvoid
28100b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l,
28110b57cec5SDimitry Andric                                                   iterator __r, const_pointer& __vt)
28120b57cec5SDimitry Andric{
28130b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
28140b57cec5SDimitry Andric    // as if
28150b57cec5SDimitry Andric    //   for (; __f != __l; ++__r, ++__f, ++__base::size())
28160b57cec5SDimitry Andric    //       __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__f));
28170b57cec5SDimitry Andric    difference_type __n = __l - __f;
28180b57cec5SDimitry Andric    while (__n > 0)
28190b57cec5SDimitry Andric    {
28200b57cec5SDimitry Andric        pointer __fb = __f.__ptr_;
28210b57cec5SDimitry Andric        pointer __fe = *__f.__m_iter_ + __base::__block_size;
28220b57cec5SDimitry Andric        difference_type __bs = __fe - __fb;
28230b57cec5SDimitry Andric        if (__bs > __n)
28240b57cec5SDimitry Andric        {
28250b57cec5SDimitry Andric            __bs = __n;
28260b57cec5SDimitry Andric            __fe = __fb + __bs;
28270b57cec5SDimitry Andric        }
28280b57cec5SDimitry Andric        if (__fb <= __vt && __vt < __fe)
28290b57cec5SDimitry Andric            __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) += __r - __f).__ptr_;
28300b57cec5SDimitry Andric        for (; __fb != __fe; ++__fb, ++__r, ++__base::size())
28310b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__fb));
28320b57cec5SDimitry Andric        __n -= __bs;
28330b57cec5SDimitry Andric        __f += __bs;
28340b57cec5SDimitry Andric    }
28350b57cec5SDimitry Andric}
28360b57cec5SDimitry Andric
28370b57cec5SDimitry Andric// move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
28380b57cec5SDimitry Andric// If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
28390b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
28400b57cec5SDimitry Andricvoid
28410b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__move_construct_backward_and_check(iterator __f, iterator __l,
28420b57cec5SDimitry Andric                                                            iterator __r, const_pointer& __vt)
28430b57cec5SDimitry Andric{
28440b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
28450b57cec5SDimitry Andric    // as if
28460b57cec5SDimitry Andric    //   for (iterator __j = __l; __j != __f;)
28470b57cec5SDimitry Andric    //   {
28480b57cec5SDimitry Andric    //       __alloc_traitsconstruct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__j));
28490b57cec5SDimitry Andric    //       --__base::__start_;
28500b57cec5SDimitry Andric    //       ++__base::size();
28510b57cec5SDimitry Andric    //   }
28520b57cec5SDimitry Andric    difference_type __n = __l - __f;
28530b57cec5SDimitry Andric    while (__n > 0)
28540b57cec5SDimitry Andric    {
28550b57cec5SDimitry Andric        --__l;
28560b57cec5SDimitry Andric        pointer __lb = *__l.__m_iter_;
28570b57cec5SDimitry Andric        pointer __le = __l.__ptr_ + 1;
28580b57cec5SDimitry Andric        difference_type __bs = __le - __lb;
28590b57cec5SDimitry Andric        if (__bs > __n)
28600b57cec5SDimitry Andric        {
28610b57cec5SDimitry Andric            __bs = __n;
28620b57cec5SDimitry Andric            __lb = __le - __bs;
28630b57cec5SDimitry Andric        }
28640b57cec5SDimitry Andric        if (__lb <= __vt && __vt < __le)
28650b57cec5SDimitry Andric            __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) -= __l - __r + 1).__ptr_;
28660b57cec5SDimitry Andric        while (__le != __lb)
28670b57cec5SDimitry Andric        {
28680b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__le));
28690b57cec5SDimitry Andric            --__base::__start_;
28700b57cec5SDimitry Andric            ++__base::size();
28710b57cec5SDimitry Andric        }
28720b57cec5SDimitry Andric        __n -= __bs;
28730b57cec5SDimitry Andric        __l -= __bs - 1;
28740b57cec5SDimitry Andric    }
28750b57cec5SDimitry Andric}
28760b57cec5SDimitry Andric
28770b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
28780b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::iterator
28790b57cec5SDimitry Andricdeque<_Tp, _Allocator>::erase(const_iterator __f)
28800b57cec5SDimitry Andric{
28810b57cec5SDimitry Andric    iterator __b = __base::begin();
28820b57cec5SDimitry Andric    difference_type __pos = __f - __b;
28830b57cec5SDimitry Andric    iterator __p = __b + __pos;
28840b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
28850b57cec5SDimitry Andric    if (static_cast<size_t>(__pos) <= (__base::size() - 1) / 2)
28860b57cec5SDimitry Andric    {   // erase from front
28870b57cec5SDimitry Andric        _VSTD::move_backward(__b, __p, _VSTD::next(__p));
28880b57cec5SDimitry Andric        __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
28890b57cec5SDimitry Andric        --__base::size();
28900b57cec5SDimitry Andric        ++__base::__start_;
2891e40139ffSDimitry Andric        __maybe_remove_front_spare();
28920b57cec5SDimitry Andric    }
28930b57cec5SDimitry Andric    else
28940b57cec5SDimitry Andric    {   // erase from back
28950b57cec5SDimitry Andric        iterator __i = _VSTD::move(_VSTD::next(__p), __base::end(), __p);
28960b57cec5SDimitry Andric        __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
28970b57cec5SDimitry Andric        --__base::size();
2898e40139ffSDimitry Andric        __maybe_remove_back_spare();
28990b57cec5SDimitry Andric    }
29000b57cec5SDimitry Andric    return __base::begin() + __pos;
29010b57cec5SDimitry Andric}
29020b57cec5SDimitry Andric
29030b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
29040b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::iterator
29050b57cec5SDimitry Andricdeque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l)
29060b57cec5SDimitry Andric{
29070b57cec5SDimitry Andric    difference_type __n = __l - __f;
29080b57cec5SDimitry Andric    iterator __b = __base::begin();
29090b57cec5SDimitry Andric    difference_type __pos = __f - __b;
29100b57cec5SDimitry Andric    iterator __p = __b + __pos;
29110b57cec5SDimitry Andric    if (__n > 0)
29120b57cec5SDimitry Andric    {
29130b57cec5SDimitry Andric        allocator_type& __a = __base::__alloc();
29140b57cec5SDimitry Andric        if (static_cast<size_t>(__pos) <= (__base::size() - __n) / 2)
29150b57cec5SDimitry Andric        {   // erase from front
29160b57cec5SDimitry Andric            iterator __i = _VSTD::move_backward(__b, __p, __p + __n);
29170b57cec5SDimitry Andric            for (; __b != __i; ++__b)
29180b57cec5SDimitry Andric                __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
29190b57cec5SDimitry Andric            __base::size() -= __n;
29200b57cec5SDimitry Andric            __base::__start_ += __n;
2921e40139ffSDimitry Andric            while (__maybe_remove_front_spare()) {
29220b57cec5SDimitry Andric            }
29230b57cec5SDimitry Andric        }
29240b57cec5SDimitry Andric        else
29250b57cec5SDimitry Andric        {   // erase from back
29260b57cec5SDimitry Andric            iterator __i = _VSTD::move(__p + __n, __base::end(), __p);
29270b57cec5SDimitry Andric            for (iterator __e = __base::end(); __i != __e; ++__i)
29280b57cec5SDimitry Andric                __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
29290b57cec5SDimitry Andric            __base::size() -= __n;
2930e40139ffSDimitry Andric            while (__maybe_remove_back_spare()) {
29310b57cec5SDimitry Andric            }
29320b57cec5SDimitry Andric        }
29330b57cec5SDimitry Andric    }
29340b57cec5SDimitry Andric    return __base::begin() + __pos;
29350b57cec5SDimitry Andric}
29360b57cec5SDimitry Andric
29370b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
29380b57cec5SDimitry Andricvoid
29390b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__erase_to_end(const_iterator __f)
29400b57cec5SDimitry Andric{
29410b57cec5SDimitry Andric    iterator __e = __base::end();
29420b57cec5SDimitry Andric    difference_type __n = __e - __f;
29430b57cec5SDimitry Andric    if (__n > 0)
29440b57cec5SDimitry Andric    {
29450b57cec5SDimitry Andric        allocator_type& __a = __base::__alloc();
29460b57cec5SDimitry Andric        iterator __b = __base::begin();
29470b57cec5SDimitry Andric        difference_type __pos = __f - __b;
29480b57cec5SDimitry Andric        for (iterator __p = __b + __pos; __p != __e; ++__p)
29490b57cec5SDimitry Andric            __alloc_traits::destroy(__a, _VSTD::addressof(*__p));
29500b57cec5SDimitry Andric        __base::size() -= __n;
2951e40139ffSDimitry Andric        while (__maybe_remove_back_spare()) {
29520b57cec5SDimitry Andric        }
29530b57cec5SDimitry Andric    }
29540b57cec5SDimitry Andric}
29550b57cec5SDimitry Andric
29560b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
29570b57cec5SDimitry Andricinline
29580b57cec5SDimitry Andricvoid
29590b57cec5SDimitry Andricdeque<_Tp, _Allocator>::swap(deque& __c)
29600b57cec5SDimitry Andric#if _LIBCPP_STD_VER >= 14
29610b57cec5SDimitry Andric        _NOEXCEPT
29620b57cec5SDimitry Andric#else
29630b57cec5SDimitry Andric        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
29640b57cec5SDimitry Andric                    __is_nothrow_swappable<allocator_type>::value)
29650b57cec5SDimitry Andric#endif
29660b57cec5SDimitry Andric{
29670b57cec5SDimitry Andric    __base::swap(__c);
29680b57cec5SDimitry Andric}
29690b57cec5SDimitry Andric
29700b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
29710b57cec5SDimitry Andricinline
29720b57cec5SDimitry Andricvoid
29730b57cec5SDimitry Andricdeque<_Tp, _Allocator>::clear() _NOEXCEPT
29740b57cec5SDimitry Andric{
29750b57cec5SDimitry Andric    __base::clear();
29760b57cec5SDimitry Andric}
29770b57cec5SDimitry Andric
29780b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
29790b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
29800b57cec5SDimitry Andricbool
29810b57cec5SDimitry Andricoperator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
29820b57cec5SDimitry Andric{
29830b57cec5SDimitry Andric    const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
29840b57cec5SDimitry Andric    return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
29850b57cec5SDimitry Andric}
29860b57cec5SDimitry Andric
29870b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
29880b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
29890b57cec5SDimitry Andricbool
29900b57cec5SDimitry Andricoperator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
29910b57cec5SDimitry Andric{
29920b57cec5SDimitry Andric    return !(__x == __y);
29930b57cec5SDimitry Andric}
29940b57cec5SDimitry Andric
29950b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
29960b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
29970b57cec5SDimitry Andricbool
29980b57cec5SDimitry Andricoperator< (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
29990b57cec5SDimitry Andric{
30000b57cec5SDimitry Andric    return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
30010b57cec5SDimitry Andric}
30020b57cec5SDimitry Andric
30030b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
30040b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
30050b57cec5SDimitry Andricbool
30060b57cec5SDimitry Andricoperator> (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
30070b57cec5SDimitry Andric{
30080b57cec5SDimitry Andric    return __y < __x;
30090b57cec5SDimitry Andric}
30100b57cec5SDimitry Andric
30110b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
30120b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
30130b57cec5SDimitry Andricbool
30140b57cec5SDimitry Andricoperator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
30150b57cec5SDimitry Andric{
30160b57cec5SDimitry Andric    return !(__x < __y);
30170b57cec5SDimitry Andric}
30180b57cec5SDimitry Andric
30190b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
30200b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
30210b57cec5SDimitry Andricbool
30220b57cec5SDimitry Andricoperator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
30230b57cec5SDimitry Andric{
30240b57cec5SDimitry Andric    return !(__y < __x);
30250b57cec5SDimitry Andric}
30260b57cec5SDimitry Andric
30270b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
30280b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
30290b57cec5SDimitry Andricvoid
30300b57cec5SDimitry Andricswap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
30310b57cec5SDimitry Andric    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
30320b57cec5SDimitry Andric{
30330b57cec5SDimitry Andric    __x.swap(__y);
30340b57cec5SDimitry Andric}
30350b57cec5SDimitry Andric
30360b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 17
30370b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator, class _Up>
30385ffd83dbSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY typename deque<_Tp, _Allocator>::size_type
30395ffd83dbSDimitry Andricerase(deque<_Tp, _Allocator>& __c, const _Up& __v) {
30405ffd83dbSDimitry Andric  auto __old_size = __c.size();
30415ffd83dbSDimitry Andric  __c.erase(_VSTD::remove(__c.begin(), __c.end(), __v), __c.end());
30425ffd83dbSDimitry Andric  return __old_size - __c.size();
30435ffd83dbSDimitry Andric}
30440b57cec5SDimitry Andric
30450b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator, class _Predicate>
30465ffd83dbSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY typename deque<_Tp, _Allocator>::size_type
30475ffd83dbSDimitry Andricerase_if(deque<_Tp, _Allocator>& __c, _Predicate __pred) {
30485ffd83dbSDimitry Andric  auto __old_size = __c.size();
30495ffd83dbSDimitry Andric  __c.erase(_VSTD::remove_if(__c.begin(), __c.end(), __pred), __c.end());
30505ffd83dbSDimitry Andric  return __old_size - __c.size();
30515ffd83dbSDimitry Andric}
3052*81ad6265SDimitry Andric
3053*81ad6265SDimitry Andrictemplate <>
3054*81ad6265SDimitry Andricinline constexpr bool __format::__enable_insertable<std::deque<char>> = true;
3055*81ad6265SDimitry Andric#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
3056*81ad6265SDimitry Andrictemplate <>
3057*81ad6265SDimitry Andricinline constexpr bool __format::__enable_insertable<std::deque<wchar_t>> = true;
30580b57cec5SDimitry Andric#endif
30590b57cec5SDimitry Andric
3060*81ad6265SDimitry Andric#endif // _LIBCPP_STD_VER > 17
30610b57cec5SDimitry Andric
30620b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
30630b57cec5SDimitry Andric
30640b57cec5SDimitry Andric_LIBCPP_POP_MACROS
30650b57cec5SDimitry Andric
30660b57cec5SDimitry Andric#endif // _LIBCPP_DEQUE
3067