xref: /freebsd/contrib/llvm-project/libcxx/include/deque (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
10b57cec5SDimitry Andric// -*- C++ -*-
20b57cec5SDimitry Andric//===---------------------------- deque -----------------------------------===//
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())
1320b57cec5SDimitry Andric   -> deque<typename iterator_traits<InputIterator>::value_type, Allocator>;
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>
153*5ffd83dbSDimitry Andric    typename deque<T, Allocator>::size_type
154*5ffd83dbSDimitry Andric    erase(deque<T, Allocator>& c, const U& value);       // C++20
1550b57cec5SDimitry Andrictemplate <class T, class Allocator, class Predicate>
156*5ffd83dbSDimitry Andric    typename deque<T, Allocator>::size_type
157*5ffd83dbSDimitry Andric    erase_if(deque<T, Allocator>& c, Predicate pred);    // C++20
1580b57cec5SDimitry Andric
1590b57cec5SDimitry Andric}  // std
1600b57cec5SDimitry Andric
1610b57cec5SDimitry Andric*/
1620b57cec5SDimitry Andric
1630b57cec5SDimitry Andric#include <__config>
1640b57cec5SDimitry Andric#include <__split_buffer>
1650b57cec5SDimitry Andric#include <type_traits>
1660b57cec5SDimitry Andric#include <initializer_list>
1670b57cec5SDimitry Andric#include <iterator>
1680b57cec5SDimitry Andric#include <algorithm>
1690b57cec5SDimitry Andric#include <stdexcept>
1700b57cec5SDimitry Andric#include <version>
1710b57cec5SDimitry Andric
1720b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1730b57cec5SDimitry Andric#pragma GCC system_header
1740b57cec5SDimitry Andric#endif
1750b57cec5SDimitry Andric
1760b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS
1770b57cec5SDimitry Andric#include <__undef_macros>
1780b57cec5SDimitry Andric
1790b57cec5SDimitry Andric
1800b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
1810b57cec5SDimitry Andric
1820b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> class __deque_base;
1830b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator = allocator<_Tp> > class _LIBCPP_TEMPLATE_VIS deque;
1840b57cec5SDimitry Andric
1850b57cec5SDimitry Andrictemplate <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
1860b57cec5SDimitry Andric          class _DiffType, _DiffType _BlockSize>
1870b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __deque_iterator;
1880b57cec5SDimitry Andric
1890b57cec5SDimitry Andrictemplate <class _RAIter,
1900b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
1910b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
1920b57cec5SDimitry Andriccopy(_RAIter __f,
1930b57cec5SDimitry Andric     _RAIter __l,
1940b57cec5SDimitry Andric     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
195480093f4SDimitry Andric     typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
1960b57cec5SDimitry Andric
1970b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
1980b57cec5SDimitry Andric          class _OutputIterator>
1990b57cec5SDimitry Andric_OutputIterator
2000b57cec5SDimitry Andriccopy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
2010b57cec5SDimitry Andric     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
2020b57cec5SDimitry Andric     _OutputIterator __r);
2030b57cec5SDimitry Andric
2040b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
2050b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
2060b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
2070b57cec5SDimitry Andriccopy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
2080b57cec5SDimitry Andric     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
2090b57cec5SDimitry Andric     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
2100b57cec5SDimitry Andric
2110b57cec5SDimitry Andrictemplate <class _RAIter,
2120b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
2130b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
2140b57cec5SDimitry Andriccopy_backward(_RAIter __f,
2150b57cec5SDimitry Andric              _RAIter __l,
2160b57cec5SDimitry Andric              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
217480093f4SDimitry Andric              typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
2180b57cec5SDimitry Andric
2190b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
2200b57cec5SDimitry Andric          class _OutputIterator>
2210b57cec5SDimitry Andric_OutputIterator
2220b57cec5SDimitry Andriccopy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
2230b57cec5SDimitry Andric              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
2240b57cec5SDimitry Andric              _OutputIterator __r);
2250b57cec5SDimitry Andric
2260b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
2270b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
2280b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
2290b57cec5SDimitry Andriccopy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
2300b57cec5SDimitry Andric              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
2310b57cec5SDimitry Andric              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
2320b57cec5SDimitry Andric
2330b57cec5SDimitry Andrictemplate <class _RAIter,
2340b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
2350b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
2360b57cec5SDimitry Andricmove(_RAIter __f,
2370b57cec5SDimitry Andric     _RAIter __l,
2380b57cec5SDimitry Andric     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
239480093f4SDimitry Andric     typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
2400b57cec5SDimitry Andric
2410b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
2420b57cec5SDimitry Andric          class _OutputIterator>
2430b57cec5SDimitry Andric_OutputIterator
2440b57cec5SDimitry Andricmove(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
2450b57cec5SDimitry Andric     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
2460b57cec5SDimitry Andric     _OutputIterator __r);
2470b57cec5SDimitry Andric
2480b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
2490b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
2500b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
2510b57cec5SDimitry Andricmove(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
2520b57cec5SDimitry Andric     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
2530b57cec5SDimitry Andric     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
2540b57cec5SDimitry Andric
2550b57cec5SDimitry Andrictemplate <class _RAIter,
2560b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
2570b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
2580b57cec5SDimitry Andricmove_backward(_RAIter __f,
2590b57cec5SDimitry Andric              _RAIter __l,
2600b57cec5SDimitry Andric              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
261480093f4SDimitry Andric              typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
2620b57cec5SDimitry Andric
2630b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
2640b57cec5SDimitry Andric          class _OutputIterator>
2650b57cec5SDimitry Andric_OutputIterator
2660b57cec5SDimitry Andricmove_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
2670b57cec5SDimitry Andric              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
2680b57cec5SDimitry Andric              _OutputIterator __r);
2690b57cec5SDimitry Andric
2700b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
2710b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
2720b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
2730b57cec5SDimitry Andricmove_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
2740b57cec5SDimitry Andric              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
2750b57cec5SDimitry Andric              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
2760b57cec5SDimitry Andric
2770b57cec5SDimitry Andrictemplate <class _ValueType, class _DiffType>
2780b57cec5SDimitry Andricstruct __deque_block_size {
2790b57cec5SDimitry Andric  static const _DiffType value = sizeof(_ValueType) < 256 ? 4096 / sizeof(_ValueType) : 16;
2800b57cec5SDimitry Andric};
2810b57cec5SDimitry Andric
2820b57cec5SDimitry Andrictemplate <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
2830b57cec5SDimitry Andric          class _DiffType, _DiffType _BS =
2840b57cec5SDimitry Andric#ifdef _LIBCPP_ABI_INCOMPLETE_TYPES_IN_DEQUE
2850b57cec5SDimitry Andric// Keep template parameter to avoid changing all template declarations thoughout
2860b57cec5SDimitry Andric// this file.
2870b57cec5SDimitry Andric                               0
2880b57cec5SDimitry Andric#else
2890b57cec5SDimitry Andric                               __deque_block_size<_ValueType, _DiffType>::value
2900b57cec5SDimitry Andric#endif
2910b57cec5SDimitry Andric          >
2920b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __deque_iterator
2930b57cec5SDimitry Andric{
2940b57cec5SDimitry Andric    typedef _MapPointer __map_iterator;
2950b57cec5SDimitry Andricpublic:
2960b57cec5SDimitry Andric    typedef _Pointer  pointer;
2970b57cec5SDimitry Andric    typedef _DiffType difference_type;
2980b57cec5SDimitry Andricprivate:
2990b57cec5SDimitry Andric    __map_iterator __m_iter_;
3000b57cec5SDimitry Andric    pointer        __ptr_;
3010b57cec5SDimitry Andric
3020b57cec5SDimitry Andric    static const difference_type __block_size;
3030b57cec5SDimitry Andricpublic:
3040b57cec5SDimitry Andric    typedef _ValueType                  value_type;
3050b57cec5SDimitry Andric    typedef random_access_iterator_tag  iterator_category;
3060b57cec5SDimitry Andric    typedef _Reference                  reference;
3070b57cec5SDimitry Andric
3080b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __deque_iterator() _NOEXCEPT
3090b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
3100b57cec5SDimitry Andric     : __m_iter_(nullptr), __ptr_(nullptr)
3110b57cec5SDimitry Andric#endif
3120b57cec5SDimitry Andric     {}
3130b57cec5SDimitry Andric
3140b57cec5SDimitry Andric    template <class _Pp, class _Rp, class _MP>
3150b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3160b57cec5SDimitry Andric    __deque_iterator(const __deque_iterator<value_type, _Pp, _Rp, _MP, difference_type, _BS>& __it,
3170b57cec5SDimitry Andric                typename enable_if<is_convertible<_Pp, pointer>::value>::type* = 0) _NOEXCEPT
3180b57cec5SDimitry Andric        : __m_iter_(__it.__m_iter_), __ptr_(__it.__ptr_) {}
3190b57cec5SDimitry Andric
3200b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *__ptr_;}
3210b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return __ptr_;}
3220b57cec5SDimitry Andric
3230b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator++()
3240b57cec5SDimitry Andric    {
3250b57cec5SDimitry Andric        if (++__ptr_ - *__m_iter_ == __block_size)
3260b57cec5SDimitry Andric        {
3270b57cec5SDimitry Andric            ++__m_iter_;
3280b57cec5SDimitry Andric            __ptr_ = *__m_iter_;
3290b57cec5SDimitry Andric        }
3300b57cec5SDimitry Andric        return *this;
3310b57cec5SDimitry Andric    }
3320b57cec5SDimitry Andric
3330b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __deque_iterator operator++(int)
3340b57cec5SDimitry Andric    {
3350b57cec5SDimitry Andric        __deque_iterator __tmp = *this;
3360b57cec5SDimitry Andric        ++(*this);
3370b57cec5SDimitry Andric        return __tmp;
3380b57cec5SDimitry Andric    }
3390b57cec5SDimitry Andric
3400b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator--()
3410b57cec5SDimitry Andric    {
3420b57cec5SDimitry Andric        if (__ptr_ == *__m_iter_)
3430b57cec5SDimitry Andric        {
3440b57cec5SDimitry Andric            --__m_iter_;
3450b57cec5SDimitry Andric            __ptr_ = *__m_iter_ + __block_size;
3460b57cec5SDimitry Andric        }
3470b57cec5SDimitry Andric        --__ptr_;
3480b57cec5SDimitry Andric        return *this;
3490b57cec5SDimitry Andric    }
3500b57cec5SDimitry Andric
3510b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __deque_iterator operator--(int)
3520b57cec5SDimitry Andric    {
3530b57cec5SDimitry Andric        __deque_iterator __tmp = *this;
3540b57cec5SDimitry Andric        --(*this);
3550b57cec5SDimitry Andric        return __tmp;
3560b57cec5SDimitry Andric    }
3570b57cec5SDimitry Andric
3580b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator+=(difference_type __n)
3590b57cec5SDimitry Andric    {
3600b57cec5SDimitry Andric        if (__n != 0)
3610b57cec5SDimitry Andric        {
3620b57cec5SDimitry Andric            __n += __ptr_ - *__m_iter_;
3630b57cec5SDimitry Andric            if (__n > 0)
3640b57cec5SDimitry Andric            {
3650b57cec5SDimitry Andric                __m_iter_ += __n / __block_size;
3660b57cec5SDimitry Andric                __ptr_ = *__m_iter_ + __n % __block_size;
3670b57cec5SDimitry Andric            }
3680b57cec5SDimitry Andric            else // (__n < 0)
3690b57cec5SDimitry Andric            {
3700b57cec5SDimitry Andric                difference_type __z = __block_size - 1 - __n;
3710b57cec5SDimitry Andric                __m_iter_ -= __z / __block_size;
3720b57cec5SDimitry Andric                __ptr_ = *__m_iter_ + (__block_size - 1 - __z % __block_size);
3730b57cec5SDimitry Andric            }
3740b57cec5SDimitry Andric        }
3750b57cec5SDimitry Andric        return *this;
3760b57cec5SDimitry Andric    }
3770b57cec5SDimitry Andric
3780b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __deque_iterator& operator-=(difference_type __n)
3790b57cec5SDimitry Andric    {
3800b57cec5SDimitry Andric        return *this += -__n;
3810b57cec5SDimitry Andric    }
3820b57cec5SDimitry Andric
3830b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __deque_iterator operator+(difference_type __n) const
3840b57cec5SDimitry Andric    {
3850b57cec5SDimitry Andric        __deque_iterator __t(*this);
3860b57cec5SDimitry Andric        __t += __n;
3870b57cec5SDimitry Andric        return __t;
3880b57cec5SDimitry Andric    }
3890b57cec5SDimitry Andric
3900b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __deque_iterator operator-(difference_type __n) const
3910b57cec5SDimitry Andric    {
3920b57cec5SDimitry Andric        __deque_iterator __t(*this);
3930b57cec5SDimitry Andric        __t -= __n;
3940b57cec5SDimitry Andric        return __t;
3950b57cec5SDimitry Andric    }
3960b57cec5SDimitry Andric
3970b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3980b57cec5SDimitry Andric    friend __deque_iterator operator+(difference_type __n, const __deque_iterator& __it)
3990b57cec5SDimitry Andric        {return __it + __n;}
4000b57cec5SDimitry Andric
4010b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
4020b57cec5SDimitry Andric    friend difference_type operator-(const __deque_iterator& __x, const __deque_iterator& __y)
4030b57cec5SDimitry Andric    {
4040b57cec5SDimitry Andric        if (__x != __y)
4050b57cec5SDimitry Andric            return (__x.__m_iter_ - __y.__m_iter_) * __block_size
4060b57cec5SDimitry Andric                 + (__x.__ptr_ - *__x.__m_iter_)
4070b57cec5SDimitry Andric                 - (__y.__ptr_ - *__y.__m_iter_);
4080b57cec5SDimitry Andric        return 0;
4090b57cec5SDimitry Andric    }
4100b57cec5SDimitry Andric
4110b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const
4120b57cec5SDimitry Andric        {return *(*this + __n);}
4130b57cec5SDimitry Andric
4140b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY friend
4150b57cec5SDimitry Andric        bool operator==(const __deque_iterator& __x, const __deque_iterator& __y)
4160b57cec5SDimitry Andric        {return __x.__ptr_ == __y.__ptr_;}
4170b57cec5SDimitry Andric
4180b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY friend
4190b57cec5SDimitry Andric        bool operator!=(const __deque_iterator& __x, const __deque_iterator& __y)
4200b57cec5SDimitry Andric        {return !(__x == __y);}
4210b57cec5SDimitry Andric
4220b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY friend
4230b57cec5SDimitry Andric        bool operator<(const __deque_iterator& __x, const __deque_iterator& __y)
4240b57cec5SDimitry Andric        {return __x.__m_iter_ < __y.__m_iter_ ||
4250b57cec5SDimitry Andric               (__x.__m_iter_ == __y.__m_iter_ && __x.__ptr_ < __y.__ptr_);}
4260b57cec5SDimitry Andric
4270b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY friend
4280b57cec5SDimitry Andric        bool operator>(const __deque_iterator& __x, const __deque_iterator& __y)
4290b57cec5SDimitry Andric        {return __y < __x;}
4300b57cec5SDimitry Andric
4310b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY friend
4320b57cec5SDimitry Andric        bool operator<=(const __deque_iterator& __x, const __deque_iterator& __y)
4330b57cec5SDimitry Andric        {return !(__y < __x);}
4340b57cec5SDimitry Andric
4350b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY friend
4360b57cec5SDimitry Andric        bool operator>=(const __deque_iterator& __x, const __deque_iterator& __y)
4370b57cec5SDimitry Andric        {return !(__x < __y);}
4380b57cec5SDimitry Andric
4390b57cec5SDimitry Andricprivate:
4400b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
4410b57cec5SDimitry Andric        : __m_iter_(__m), __ptr_(__p) {}
4420b57cec5SDimitry Andric
4430b57cec5SDimitry Andric    template <class _Tp, class _Ap> friend class __deque_base;
4440b57cec5SDimitry Andric    template <class _Tp, class _Ap> friend class _LIBCPP_TEMPLATE_VIS deque;
4450b57cec5SDimitry Andric    template <class _Vp, class _Pp, class _Rp, class _MP, class _Dp, _Dp>
4460b57cec5SDimitry Andric        friend class _LIBCPP_TEMPLATE_VIS __deque_iterator;
4470b57cec5SDimitry Andric
4480b57cec5SDimitry Andric    template <class _RAIter,
4490b57cec5SDimitry Andric              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
4500b57cec5SDimitry Andric    friend
4510b57cec5SDimitry Andric    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
4520b57cec5SDimitry Andric    copy(_RAIter __f,
4530b57cec5SDimitry Andric         _RAIter __l,
4540b57cec5SDimitry Andric         __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
455480093f4SDimitry Andric         typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*);
4560b57cec5SDimitry Andric
4570b57cec5SDimitry Andric    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
4580b57cec5SDimitry Andric              class _OutputIterator>
4590b57cec5SDimitry Andric    friend
4600b57cec5SDimitry Andric    _OutputIterator
4610b57cec5SDimitry Andric    copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
4620b57cec5SDimitry Andric         __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
4630b57cec5SDimitry Andric         _OutputIterator __r);
4640b57cec5SDimitry Andric
4650b57cec5SDimitry Andric    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
4660b57cec5SDimitry Andric              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
4670b57cec5SDimitry Andric    friend
4680b57cec5SDimitry Andric    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
4690b57cec5SDimitry Andric    copy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
4700b57cec5SDimitry Andric         __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
4710b57cec5SDimitry Andric         __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
4720b57cec5SDimitry Andric
4730b57cec5SDimitry Andric    template <class _RAIter,
4740b57cec5SDimitry Andric              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
4750b57cec5SDimitry Andric    friend
4760b57cec5SDimitry Andric    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
4770b57cec5SDimitry Andric    copy_backward(_RAIter __f,
4780b57cec5SDimitry Andric                  _RAIter __l,
4790b57cec5SDimitry Andric                  __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
480480093f4SDimitry Andric                  typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*);
4810b57cec5SDimitry Andric
4820b57cec5SDimitry Andric    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
4830b57cec5SDimitry Andric              class _OutputIterator>
4840b57cec5SDimitry Andric    friend
4850b57cec5SDimitry Andric    _OutputIterator
4860b57cec5SDimitry Andric    copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
4870b57cec5SDimitry Andric                  __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
4880b57cec5SDimitry Andric                  _OutputIterator __r);
4890b57cec5SDimitry Andric
4900b57cec5SDimitry Andric    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
4910b57cec5SDimitry Andric              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
4920b57cec5SDimitry Andric    friend
4930b57cec5SDimitry Andric    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
4940b57cec5SDimitry Andric    copy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
4950b57cec5SDimitry Andric                  __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
4960b57cec5SDimitry Andric                  __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
4970b57cec5SDimitry Andric
4980b57cec5SDimitry Andric    template <class _RAIter,
4990b57cec5SDimitry Andric              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
5000b57cec5SDimitry Andric    friend
5010b57cec5SDimitry Andric    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
5020b57cec5SDimitry Andric    move(_RAIter __f,
5030b57cec5SDimitry Andric         _RAIter __l,
5040b57cec5SDimitry Andric         __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
505480093f4SDimitry Andric         typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*);
5060b57cec5SDimitry Andric
5070b57cec5SDimitry Andric    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
5080b57cec5SDimitry Andric              class _OutputIterator>
5090b57cec5SDimitry Andric    friend
5100b57cec5SDimitry Andric    _OutputIterator
5110b57cec5SDimitry Andric    move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
5120b57cec5SDimitry Andric         __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
5130b57cec5SDimitry Andric         _OutputIterator __r);
5140b57cec5SDimitry Andric
5150b57cec5SDimitry Andric    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
5160b57cec5SDimitry Andric              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
5170b57cec5SDimitry Andric    friend
5180b57cec5SDimitry Andric    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
5190b57cec5SDimitry Andric    move(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
5200b57cec5SDimitry Andric         __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
5210b57cec5SDimitry Andric         __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
5220b57cec5SDimitry Andric
5230b57cec5SDimitry Andric    template <class _RAIter,
5240b57cec5SDimitry Andric              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
5250b57cec5SDimitry Andric    friend
5260b57cec5SDimitry Andric    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
5270b57cec5SDimitry Andric    move_backward(_RAIter __f,
5280b57cec5SDimitry Andric                  _RAIter __l,
5290b57cec5SDimitry Andric                  __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
530480093f4SDimitry Andric                  typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*);
5310b57cec5SDimitry Andric
5320b57cec5SDimitry Andric    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
5330b57cec5SDimitry Andric              class _OutputIterator>
5340b57cec5SDimitry Andric    friend
5350b57cec5SDimitry Andric    _OutputIterator
5360b57cec5SDimitry Andric    move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
5370b57cec5SDimitry Andric                  __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
5380b57cec5SDimitry Andric                  _OutputIterator __r);
5390b57cec5SDimitry Andric
5400b57cec5SDimitry Andric    template <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
5410b57cec5SDimitry Andric              class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
5420b57cec5SDimitry Andric    friend
5430b57cec5SDimitry Andric    __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
5440b57cec5SDimitry Andric    move_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
5450b57cec5SDimitry Andric                  __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
5460b57cec5SDimitry Andric                  __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r);
5470b57cec5SDimitry Andric};
5480b57cec5SDimitry Andric
5490b57cec5SDimitry Andrictemplate <class _ValueType, class _Pointer, class _Reference, class _MapPointer,
5500b57cec5SDimitry Andric          class _DiffType, _DiffType _BlockSize>
5510b57cec5SDimitry Andricconst _DiffType __deque_iterator<_ValueType, _Pointer, _Reference, _MapPointer,
5520b57cec5SDimitry Andric                                 _DiffType, _BlockSize>::__block_size =
5530b57cec5SDimitry Andric    __deque_block_size<_ValueType, _DiffType>::value;
5540b57cec5SDimitry Andric
5550b57cec5SDimitry Andric// copy
5560b57cec5SDimitry Andric
5570b57cec5SDimitry Andrictemplate <class _RAIter,
5580b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
5590b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
5600b57cec5SDimitry Andriccopy(_RAIter __f,
5610b57cec5SDimitry Andric     _RAIter __l,
5620b57cec5SDimitry Andric     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
563480093f4SDimitry Andric     typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
5640b57cec5SDimitry Andric{
5650b57cec5SDimitry Andric    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
5660b57cec5SDimitry Andric    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
5670b57cec5SDimitry Andric    const difference_type __block_size = __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::__block_size;
5680b57cec5SDimitry Andric    while (__f != __l)
5690b57cec5SDimitry Andric    {
5700b57cec5SDimitry Andric        pointer __rb = __r.__ptr_;
5710b57cec5SDimitry Andric        pointer __re = *__r.__m_iter_ + __block_size;
5720b57cec5SDimitry Andric        difference_type __bs = __re - __rb;
5730b57cec5SDimitry Andric        difference_type __n = __l - __f;
5740b57cec5SDimitry Andric        _RAIter __m = __l;
5750b57cec5SDimitry Andric        if (__n > __bs)
5760b57cec5SDimitry Andric        {
5770b57cec5SDimitry Andric            __n = __bs;
5780b57cec5SDimitry Andric            __m = __f + __n;
5790b57cec5SDimitry Andric        }
5800b57cec5SDimitry Andric        _VSTD::copy(__f, __m, __rb);
5810b57cec5SDimitry Andric        __f = __m;
5820b57cec5SDimitry Andric        __r += __n;
5830b57cec5SDimitry Andric    }
5840b57cec5SDimitry Andric    return __r;
5850b57cec5SDimitry Andric}
5860b57cec5SDimitry Andric
5870b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
5880b57cec5SDimitry Andric          class _OutputIterator>
5890b57cec5SDimitry Andric_OutputIterator
5900b57cec5SDimitry Andriccopy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
5910b57cec5SDimitry Andric     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
5920b57cec5SDimitry Andric     _OutputIterator __r)
5930b57cec5SDimitry Andric{
5940b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
5950b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
5960b57cec5SDimitry Andric    const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
5970b57cec5SDimitry Andric    difference_type __n = __l - __f;
5980b57cec5SDimitry Andric    while (__n > 0)
5990b57cec5SDimitry Andric    {
6000b57cec5SDimitry Andric        pointer __fb = __f.__ptr_;
6010b57cec5SDimitry Andric        pointer __fe = *__f.__m_iter_ + __block_size;
6020b57cec5SDimitry Andric        difference_type __bs = __fe - __fb;
6030b57cec5SDimitry Andric        if (__bs > __n)
6040b57cec5SDimitry Andric        {
6050b57cec5SDimitry Andric            __bs = __n;
6060b57cec5SDimitry Andric            __fe = __fb + __bs;
6070b57cec5SDimitry Andric        }
6080b57cec5SDimitry Andric        __r = _VSTD::copy(__fb, __fe, __r);
6090b57cec5SDimitry Andric        __n -= __bs;
6100b57cec5SDimitry Andric        __f += __bs;
6110b57cec5SDimitry Andric    }
6120b57cec5SDimitry Andric    return __r;
6130b57cec5SDimitry Andric}
6140b57cec5SDimitry Andric
6150b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
6160b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
6170b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
6180b57cec5SDimitry Andriccopy(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
6190b57cec5SDimitry Andric     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
6200b57cec5SDimitry Andric     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
6210b57cec5SDimitry Andric{
6220b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
6230b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
6240b57cec5SDimitry Andric    const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
6250b57cec5SDimitry Andric    difference_type __n = __l - __f;
6260b57cec5SDimitry Andric    while (__n > 0)
6270b57cec5SDimitry Andric    {
6280b57cec5SDimitry Andric        pointer __fb = __f.__ptr_;
6290b57cec5SDimitry Andric        pointer __fe = *__f.__m_iter_ + __block_size;
6300b57cec5SDimitry Andric        difference_type __bs = __fe - __fb;
6310b57cec5SDimitry Andric        if (__bs > __n)
6320b57cec5SDimitry Andric        {
6330b57cec5SDimitry Andric            __bs = __n;
6340b57cec5SDimitry Andric            __fe = __fb + __bs;
6350b57cec5SDimitry Andric        }
6360b57cec5SDimitry Andric        __r = _VSTD::copy(__fb, __fe, __r);
6370b57cec5SDimitry Andric        __n -= __bs;
6380b57cec5SDimitry Andric        __f += __bs;
6390b57cec5SDimitry Andric    }
6400b57cec5SDimitry Andric    return __r;
6410b57cec5SDimitry Andric}
6420b57cec5SDimitry Andric
6430b57cec5SDimitry Andric// copy_backward
6440b57cec5SDimitry Andric
6450b57cec5SDimitry Andrictemplate <class _RAIter,
6460b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
6470b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
6480b57cec5SDimitry Andriccopy_backward(_RAIter __f,
6490b57cec5SDimitry Andric              _RAIter __l,
6500b57cec5SDimitry Andric              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
651480093f4SDimitry Andric              typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
6520b57cec5SDimitry Andric{
6530b57cec5SDimitry Andric    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
6540b57cec5SDimitry Andric    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
6550b57cec5SDimitry Andric    while (__f != __l)
6560b57cec5SDimitry Andric    {
6570b57cec5SDimitry Andric        __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
6580b57cec5SDimitry Andric        pointer __rb = *__rp.__m_iter_;
6590b57cec5SDimitry Andric        pointer __re = __rp.__ptr_ + 1;
6600b57cec5SDimitry Andric        difference_type __bs = __re - __rb;
6610b57cec5SDimitry Andric        difference_type __n = __l - __f;
6620b57cec5SDimitry Andric        _RAIter __m = __f;
6630b57cec5SDimitry Andric        if (__n > __bs)
6640b57cec5SDimitry Andric        {
6650b57cec5SDimitry Andric            __n = __bs;
6660b57cec5SDimitry Andric            __m = __l - __n;
6670b57cec5SDimitry Andric        }
6680b57cec5SDimitry Andric        _VSTD::copy_backward(__m, __l, __re);
6690b57cec5SDimitry Andric        __l = __m;
6700b57cec5SDimitry Andric        __r -= __n;
6710b57cec5SDimitry Andric    }
6720b57cec5SDimitry Andric    return __r;
6730b57cec5SDimitry Andric}
6740b57cec5SDimitry Andric
6750b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
6760b57cec5SDimitry Andric          class _OutputIterator>
6770b57cec5SDimitry Andric_OutputIterator
6780b57cec5SDimitry Andriccopy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
6790b57cec5SDimitry Andric              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
6800b57cec5SDimitry Andric              _OutputIterator __r)
6810b57cec5SDimitry Andric{
6820b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
6830b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
6840b57cec5SDimitry Andric    difference_type __n = __l - __f;
6850b57cec5SDimitry Andric    while (__n > 0)
6860b57cec5SDimitry Andric    {
6870b57cec5SDimitry Andric        --__l;
6880b57cec5SDimitry Andric        pointer __lb = *__l.__m_iter_;
6890b57cec5SDimitry Andric        pointer __le = __l.__ptr_ + 1;
6900b57cec5SDimitry Andric        difference_type __bs = __le - __lb;
6910b57cec5SDimitry Andric        if (__bs > __n)
6920b57cec5SDimitry Andric        {
6930b57cec5SDimitry Andric            __bs = __n;
6940b57cec5SDimitry Andric            __lb = __le - __bs;
6950b57cec5SDimitry Andric        }
6960b57cec5SDimitry Andric        __r = _VSTD::copy_backward(__lb, __le, __r);
6970b57cec5SDimitry Andric        __n -= __bs;
6980b57cec5SDimitry Andric        __l -= __bs - 1;
6990b57cec5SDimitry Andric    }
7000b57cec5SDimitry Andric    return __r;
7010b57cec5SDimitry Andric}
7020b57cec5SDimitry Andric
7030b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
7040b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
7050b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
7060b57cec5SDimitry Andriccopy_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
7070b57cec5SDimitry Andric              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
7080b57cec5SDimitry Andric              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
7090b57cec5SDimitry Andric{
7100b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
7110b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
7120b57cec5SDimitry Andric    difference_type __n = __l - __f;
7130b57cec5SDimitry Andric    while (__n > 0)
7140b57cec5SDimitry Andric    {
7150b57cec5SDimitry Andric        --__l;
7160b57cec5SDimitry Andric        pointer __lb = *__l.__m_iter_;
7170b57cec5SDimitry Andric        pointer __le = __l.__ptr_ + 1;
7180b57cec5SDimitry Andric        difference_type __bs = __le - __lb;
7190b57cec5SDimitry Andric        if (__bs > __n)
7200b57cec5SDimitry Andric        {
7210b57cec5SDimitry Andric            __bs = __n;
7220b57cec5SDimitry Andric            __lb = __le - __bs;
7230b57cec5SDimitry Andric        }
7240b57cec5SDimitry Andric        __r = _VSTD::copy_backward(__lb, __le, __r);
7250b57cec5SDimitry Andric        __n -= __bs;
7260b57cec5SDimitry Andric        __l -= __bs - 1;
7270b57cec5SDimitry Andric    }
7280b57cec5SDimitry Andric    return __r;
7290b57cec5SDimitry Andric}
7300b57cec5SDimitry Andric
7310b57cec5SDimitry Andric// move
7320b57cec5SDimitry Andric
7330b57cec5SDimitry Andrictemplate <class _RAIter,
7340b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
7350b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
7360b57cec5SDimitry Andricmove(_RAIter __f,
7370b57cec5SDimitry Andric     _RAIter __l,
7380b57cec5SDimitry Andric     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
739480093f4SDimitry Andric     typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
7400b57cec5SDimitry Andric{
7410b57cec5SDimitry Andric    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
7420b57cec5SDimitry Andric    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
7430b57cec5SDimitry Andric    const difference_type __block_size = __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::__block_size;
7440b57cec5SDimitry Andric    while (__f != __l)
7450b57cec5SDimitry Andric    {
7460b57cec5SDimitry Andric        pointer __rb = __r.__ptr_;
7470b57cec5SDimitry Andric        pointer __re = *__r.__m_iter_ + __block_size;
7480b57cec5SDimitry Andric        difference_type __bs = __re - __rb;
7490b57cec5SDimitry Andric        difference_type __n = __l - __f;
7500b57cec5SDimitry Andric        _RAIter __m = __l;
7510b57cec5SDimitry Andric        if (__n > __bs)
7520b57cec5SDimitry Andric        {
7530b57cec5SDimitry Andric            __n = __bs;
7540b57cec5SDimitry Andric            __m = __f + __n;
7550b57cec5SDimitry Andric        }
7560b57cec5SDimitry Andric        _VSTD::move(__f, __m, __rb);
7570b57cec5SDimitry Andric        __f = __m;
7580b57cec5SDimitry Andric        __r += __n;
7590b57cec5SDimitry Andric    }
7600b57cec5SDimitry Andric    return __r;
7610b57cec5SDimitry Andric}
7620b57cec5SDimitry Andric
7630b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
7640b57cec5SDimitry Andric          class _OutputIterator>
7650b57cec5SDimitry Andric_OutputIterator
7660b57cec5SDimitry Andricmove(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
7670b57cec5SDimitry Andric     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
7680b57cec5SDimitry Andric     _OutputIterator __r)
7690b57cec5SDimitry Andric{
7700b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
7710b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
7720b57cec5SDimitry Andric    const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
7730b57cec5SDimitry Andric    difference_type __n = __l - __f;
7740b57cec5SDimitry Andric    while (__n > 0)
7750b57cec5SDimitry Andric    {
7760b57cec5SDimitry Andric        pointer __fb = __f.__ptr_;
7770b57cec5SDimitry Andric        pointer __fe = *__f.__m_iter_ + __block_size;
7780b57cec5SDimitry Andric        difference_type __bs = __fe - __fb;
7790b57cec5SDimitry Andric        if (__bs > __n)
7800b57cec5SDimitry Andric        {
7810b57cec5SDimitry Andric            __bs = __n;
7820b57cec5SDimitry Andric            __fe = __fb + __bs;
7830b57cec5SDimitry Andric        }
7840b57cec5SDimitry Andric        __r = _VSTD::move(__fb, __fe, __r);
7850b57cec5SDimitry Andric        __n -= __bs;
7860b57cec5SDimitry Andric        __f += __bs;
7870b57cec5SDimitry Andric    }
7880b57cec5SDimitry Andric    return __r;
7890b57cec5SDimitry Andric}
7900b57cec5SDimitry Andric
7910b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
7920b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
7930b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
7940b57cec5SDimitry Andricmove(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
7950b57cec5SDimitry Andric     __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
7960b57cec5SDimitry Andric     __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
7970b57cec5SDimitry Andric{
7980b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
7990b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
8000b57cec5SDimitry Andric    const difference_type __block_size = __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::__block_size;
8010b57cec5SDimitry Andric    difference_type __n = __l - __f;
8020b57cec5SDimitry Andric    while (__n > 0)
8030b57cec5SDimitry Andric    {
8040b57cec5SDimitry Andric        pointer __fb = __f.__ptr_;
8050b57cec5SDimitry Andric        pointer __fe = *__f.__m_iter_ + __block_size;
8060b57cec5SDimitry Andric        difference_type __bs = __fe - __fb;
8070b57cec5SDimitry Andric        if (__bs > __n)
8080b57cec5SDimitry Andric        {
8090b57cec5SDimitry Andric            __bs = __n;
8100b57cec5SDimitry Andric            __fe = __fb + __bs;
8110b57cec5SDimitry Andric        }
8120b57cec5SDimitry Andric        __r = _VSTD::move(__fb, __fe, __r);
8130b57cec5SDimitry Andric        __n -= __bs;
8140b57cec5SDimitry Andric        __f += __bs;
8150b57cec5SDimitry Andric    }
8160b57cec5SDimitry Andric    return __r;
8170b57cec5SDimitry Andric}
8180b57cec5SDimitry Andric
8190b57cec5SDimitry Andric// move_backward
8200b57cec5SDimitry Andric
8210b57cec5SDimitry Andrictemplate <class _RAIter,
8220b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
8230b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
8240b57cec5SDimitry Andricmove_backward(_RAIter __f,
8250b57cec5SDimitry Andric              _RAIter __l,
8260b57cec5SDimitry Andric              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r,
827480093f4SDimitry Andric              typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
8280b57cec5SDimitry Andric{
8290b57cec5SDimitry Andric    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::difference_type difference_type;
8300b57cec5SDimitry Andric    typedef typename __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>::pointer pointer;
8310b57cec5SDimitry Andric    while (__f != __l)
8320b57cec5SDimitry Andric    {
8330b57cec5SDimitry Andric        __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __rp = _VSTD::prev(__r);
8340b57cec5SDimitry Andric        pointer __rb = *__rp.__m_iter_;
8350b57cec5SDimitry Andric        pointer __re = __rp.__ptr_ + 1;
8360b57cec5SDimitry Andric        difference_type __bs = __re - __rb;
8370b57cec5SDimitry Andric        difference_type __n = __l - __f;
8380b57cec5SDimitry Andric        _RAIter __m = __f;
8390b57cec5SDimitry Andric        if (__n > __bs)
8400b57cec5SDimitry Andric        {
8410b57cec5SDimitry Andric            __n = __bs;
8420b57cec5SDimitry Andric            __m = __l - __n;
8430b57cec5SDimitry Andric        }
8440b57cec5SDimitry Andric        _VSTD::move_backward(__m, __l, __re);
8450b57cec5SDimitry Andric        __l = __m;
8460b57cec5SDimitry Andric        __r -= __n;
8470b57cec5SDimitry Andric    }
8480b57cec5SDimitry Andric    return __r;
8490b57cec5SDimitry Andric}
8500b57cec5SDimitry Andric
8510b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
8520b57cec5SDimitry Andric          class _OutputIterator>
8530b57cec5SDimitry Andric_OutputIterator
8540b57cec5SDimitry Andricmove_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
8550b57cec5SDimitry Andric              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
8560b57cec5SDimitry Andric              _OutputIterator __r)
8570b57cec5SDimitry Andric{
8580b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
8590b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
8600b57cec5SDimitry Andric    difference_type __n = __l - __f;
8610b57cec5SDimitry Andric    while (__n > 0)
8620b57cec5SDimitry Andric    {
8630b57cec5SDimitry Andric        --__l;
8640b57cec5SDimitry Andric        pointer __lb = *__l.__m_iter_;
8650b57cec5SDimitry Andric        pointer __le = __l.__ptr_ + 1;
8660b57cec5SDimitry Andric        difference_type __bs = __le - __lb;
8670b57cec5SDimitry Andric        if (__bs > __n)
8680b57cec5SDimitry Andric        {
8690b57cec5SDimitry Andric            __bs = __n;
8700b57cec5SDimitry Andric            __lb = __le - __bs;
8710b57cec5SDimitry Andric        }
8720b57cec5SDimitry Andric        __r = _VSTD::move_backward(__lb, __le, __r);
8730b57cec5SDimitry Andric        __n -= __bs;
8740b57cec5SDimitry Andric        __l -= __bs - 1;
8750b57cec5SDimitry Andric    }
8760b57cec5SDimitry Andric    return __r;
8770b57cec5SDimitry Andric}
8780b57cec5SDimitry Andric
8790b57cec5SDimitry Andrictemplate <class _V1, class _P1, class _R1, class _M1, class _D1, _D1 _B1,
8800b57cec5SDimitry Andric          class _V2, class _P2, class _R2, class _M2, class _D2, _D2 _B2>
8810b57cec5SDimitry Andric__deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2>
8820b57cec5SDimitry Andricmove_backward(__deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __f,
8830b57cec5SDimitry Andric              __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1> __l,
8840b57cec5SDimitry Andric              __deque_iterator<_V2, _P2, _R2, _M2, _D2, _B2> __r)
8850b57cec5SDimitry Andric{
8860b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::difference_type difference_type;
8870b57cec5SDimitry Andric    typedef typename __deque_iterator<_V1, _P1, _R1, _M1, _D1, _B1>::pointer pointer;
8880b57cec5SDimitry Andric    difference_type __n = __l - __f;
8890b57cec5SDimitry Andric    while (__n > 0)
8900b57cec5SDimitry Andric    {
8910b57cec5SDimitry Andric        --__l;
8920b57cec5SDimitry Andric        pointer __lb = *__l.__m_iter_;
8930b57cec5SDimitry Andric        pointer __le = __l.__ptr_ + 1;
8940b57cec5SDimitry Andric        difference_type __bs = __le - __lb;
8950b57cec5SDimitry Andric        if (__bs > __n)
8960b57cec5SDimitry Andric        {
8970b57cec5SDimitry Andric            __bs = __n;
8980b57cec5SDimitry Andric            __lb = __le - __bs;
8990b57cec5SDimitry Andric        }
9000b57cec5SDimitry Andric        __r = _VSTD::move_backward(__lb, __le, __r);
9010b57cec5SDimitry Andric        __n -= __bs;
9020b57cec5SDimitry Andric        __l -= __bs - 1;
9030b57cec5SDimitry Andric    }
9040b57cec5SDimitry Andric    return __r;
9050b57cec5SDimitry Andric}
9060b57cec5SDimitry Andric
9070b57cec5SDimitry Andrictemplate <bool>
9080b57cec5SDimitry Andricclass __deque_base_common
9090b57cec5SDimitry Andric{
9100b57cec5SDimitry Andricprotected:
9110b57cec5SDimitry Andric    _LIBCPP_NORETURN void __throw_length_error() const;
9120b57cec5SDimitry Andric    _LIBCPP_NORETURN void __throw_out_of_range() const;
9130b57cec5SDimitry Andric};
9140b57cec5SDimitry Andric
9150b57cec5SDimitry Andrictemplate <bool __b>
9160b57cec5SDimitry Andricvoid
9170b57cec5SDimitry Andric__deque_base_common<__b>::__throw_length_error() const
9180b57cec5SDimitry Andric{
9190b57cec5SDimitry Andric    _VSTD::__throw_length_error("deque");
9200b57cec5SDimitry Andric}
9210b57cec5SDimitry Andric
9220b57cec5SDimitry Andrictemplate <bool __b>
9230b57cec5SDimitry Andricvoid
9240b57cec5SDimitry Andric__deque_base_common<__b>::__throw_out_of_range() const
9250b57cec5SDimitry Andric{
9260b57cec5SDimitry Andric    _VSTD::__throw_out_of_range("deque");
9270b57cec5SDimitry Andric}
9280b57cec5SDimitry Andric
9290b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
9300b57cec5SDimitry Andricclass __deque_base
9310b57cec5SDimitry Andric    : protected __deque_base_common<true>
9320b57cec5SDimitry Andric{
9330b57cec5SDimitry Andric    __deque_base(const __deque_base& __c);
9340b57cec5SDimitry Andric    __deque_base& operator=(const __deque_base& __c);
9350b57cec5SDimitry Andricpublic:
9360b57cec5SDimitry Andric    typedef _Allocator                               allocator_type;
9370b57cec5SDimitry Andric    typedef allocator_traits<allocator_type>         __alloc_traits;
9380b57cec5SDimitry Andric    typedef typename __alloc_traits::size_type       size_type;
939e40139ffSDimitry Andric
9400b57cec5SDimitry Andric    typedef _Tp                                      value_type;
9410b57cec5SDimitry Andric    typedef value_type&                              reference;
9420b57cec5SDimitry Andric    typedef const value_type&                        const_reference;
9430b57cec5SDimitry Andric    typedef typename __alloc_traits::difference_type difference_type;
9440b57cec5SDimitry Andric    typedef typename __alloc_traits::pointer         pointer;
9450b57cec5SDimitry Andric    typedef typename __alloc_traits::const_pointer   const_pointer;
9460b57cec5SDimitry Andric
9470b57cec5SDimitry Andric    static const difference_type __block_size;
9480b57cec5SDimitry Andric
9490b57cec5SDimitry Andric    typedef typename __rebind_alloc_helper<__alloc_traits, pointer>::type __pointer_allocator;
9500b57cec5SDimitry Andric    typedef allocator_traits<__pointer_allocator>        __map_traits;
9510b57cec5SDimitry Andric    typedef typename __map_traits::pointer               __map_pointer;
9520b57cec5SDimitry Andric    typedef typename __rebind_alloc_helper<__alloc_traits, const_pointer>::type __const_pointer_allocator;
9530b57cec5SDimitry Andric    typedef typename allocator_traits<__const_pointer_allocator>::const_pointer __map_const_pointer;
9540b57cec5SDimitry Andric    typedef __split_buffer<pointer, __pointer_allocator> __map;
9550b57cec5SDimitry Andric
9560b57cec5SDimitry Andric    typedef __deque_iterator<value_type, pointer, reference, __map_pointer,
9570b57cec5SDimitry Andric                             difference_type>    iterator;
9580b57cec5SDimitry Andric    typedef __deque_iterator<value_type, const_pointer, const_reference, __map_const_pointer,
9590b57cec5SDimitry Andric                             difference_type>    const_iterator;
9600b57cec5SDimitry Andric
961e40139ffSDimitry Andric    struct __deque_block_range {
962e40139ffSDimitry Andric      explicit __deque_block_range(pointer __b, pointer __e) _NOEXCEPT : __begin_(__b), __end_(__e) {}
963e40139ffSDimitry Andric      const pointer __begin_;
964e40139ffSDimitry Andric      const pointer __end_;
965e40139ffSDimitry Andric    };
966e40139ffSDimitry Andric
967e40139ffSDimitry Andric    struct __deque_range {
968e40139ffSDimitry Andric      iterator __pos_;
969e40139ffSDimitry Andric      const iterator __end_;
970e40139ffSDimitry Andric
971e40139ffSDimitry Andric      __deque_range(iterator __pos, iterator __e) _NOEXCEPT
972e40139ffSDimitry Andric        : __pos_(__pos), __end_(__e) {}
973e40139ffSDimitry Andric
974e40139ffSDimitry Andric      explicit operator bool() const _NOEXCEPT {
975e40139ffSDimitry Andric        return __pos_ != __end_;
976e40139ffSDimitry Andric      }
977e40139ffSDimitry Andric
978e40139ffSDimitry Andric      __deque_range begin() const {
979e40139ffSDimitry Andric        return *this;
980e40139ffSDimitry Andric      }
981e40139ffSDimitry Andric
982e40139ffSDimitry Andric      __deque_range end() const {
983e40139ffSDimitry Andric        return __deque_range(__end_, __end_);
984e40139ffSDimitry Andric      }
985e40139ffSDimitry Andric      __deque_block_range operator*() const _NOEXCEPT {
986e40139ffSDimitry Andric         if (__pos_.__m_iter_ == __end_.__m_iter_) {
987e40139ffSDimitry Andric          return __deque_block_range(__pos_.__ptr_, __end_.__ptr_);
988e40139ffSDimitry Andric        }
989e40139ffSDimitry Andric        return __deque_block_range(__pos_.__ptr_, *__pos_.__m_iter_ + __block_size);
990e40139ffSDimitry Andric      }
991e40139ffSDimitry Andric
992e40139ffSDimitry Andric      __deque_range& operator++() _NOEXCEPT {
993e40139ffSDimitry Andric        if (__pos_.__m_iter_ == __end_.__m_iter_) {
994e40139ffSDimitry Andric          __pos_ = __end_;
995e40139ffSDimitry Andric        } else {
996e40139ffSDimitry Andric          ++__pos_.__m_iter_;
997e40139ffSDimitry Andric          __pos_.__ptr_ = *__pos_.__m_iter_;
998e40139ffSDimitry Andric        }
999e40139ffSDimitry Andric        return *this;
1000e40139ffSDimitry Andric      }
1001e40139ffSDimitry Andric
1002e40139ffSDimitry Andric
1003e40139ffSDimitry Andric      friend bool operator==(__deque_range const& __lhs, __deque_range const& __rhs) {
1004e40139ffSDimitry Andric        return __lhs.__pos_ == __rhs.__pos_;
1005e40139ffSDimitry Andric      }
1006e40139ffSDimitry Andric      friend bool operator!=(__deque_range const& __lhs, __deque_range const& __rhs) {
1007e40139ffSDimitry Andric        return !(__lhs == __rhs);
1008e40139ffSDimitry Andric      }
1009e40139ffSDimitry Andric    };
1010e40139ffSDimitry Andric
1011e40139ffSDimitry Andric
1012e40139ffSDimitry Andric
1013e40139ffSDimitry Andric    struct _ConstructTransaction {
1014e40139ffSDimitry Andric      _ConstructTransaction(__deque_base* __db, __deque_block_range& __r)
1015e40139ffSDimitry Andric        : __pos_(__r.__begin_), __end_(__r.__end_), __begin_(__r.__begin_), __base_(__db) {}
1016e40139ffSDimitry Andric
1017e40139ffSDimitry Andric
1018e40139ffSDimitry Andric      ~_ConstructTransaction() {
1019e40139ffSDimitry Andric        __base_->size() += (__pos_ - __begin_);
1020e40139ffSDimitry Andric      }
1021e40139ffSDimitry Andric
1022e40139ffSDimitry Andric      pointer __pos_;
1023e40139ffSDimitry Andric      const pointer __end_;
1024e40139ffSDimitry Andric    private:
1025e40139ffSDimitry Andric      const pointer __begin_;
1026e40139ffSDimitry Andric      __deque_base * const __base_;
1027e40139ffSDimitry Andric    };
1028e40139ffSDimitry Andric
10290b57cec5SDimitry Andricprotected:
10300b57cec5SDimitry Andric    __map __map_;
10310b57cec5SDimitry Andric    size_type __start_;
10320b57cec5SDimitry Andric    __compressed_pair<size_type, allocator_type> __size_;
10330b57cec5SDimitry Andric
10340b57cec5SDimitry Andric    iterator       begin() _NOEXCEPT;
10350b57cec5SDimitry Andric    const_iterator begin() const _NOEXCEPT;
10360b57cec5SDimitry Andric    iterator       end() _NOEXCEPT;
10370b57cec5SDimitry Andric    const_iterator end() const _NOEXCEPT;
10380b57cec5SDimitry Andric
10390b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY size_type&            size()          {return __size_.first();}
10400b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
10410b57cec5SDimitry Andric    const size_type& size() const _NOEXCEPT {return __size_.first();}
10420b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY allocator_type&       __alloc()       {return __size_.second();}
10430b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
10440b57cec5SDimitry Andric    const allocator_type& __alloc() const _NOEXCEPT {return __size_.second();}
10450b57cec5SDimitry Andric
10460b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
10470b57cec5SDimitry Andric    __deque_base()
10480b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
10490b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
10500b57cec5SDimitry Andric    explicit __deque_base(const allocator_type& __a);
10510b57cec5SDimitry Andricpublic:
10520b57cec5SDimitry Andric    ~__deque_base();
10530b57cec5SDimitry Andric
10540b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
10550b57cec5SDimitry Andric    __deque_base(__deque_base&& __c)
10560b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
10570b57cec5SDimitry Andric    __deque_base(__deque_base&& __c, const allocator_type& __a);
10580b57cec5SDimitry Andric#endif  // _LIBCPP_CXX03_LANG
10590b57cec5SDimitry Andric
10600b57cec5SDimitry Andric    void swap(__deque_base& __c)
10610b57cec5SDimitry Andric#if _LIBCPP_STD_VER >= 14
10620b57cec5SDimitry Andric        _NOEXCEPT;
10630b57cec5SDimitry Andric#else
10640b57cec5SDimitry Andric        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
10650b57cec5SDimitry Andric                    __is_nothrow_swappable<allocator_type>::value);
10660b57cec5SDimitry Andric#endif
10670b57cec5SDimitry Andricprotected:
10680b57cec5SDimitry Andric    void clear() _NOEXCEPT;
10690b57cec5SDimitry Andric
10700b57cec5SDimitry Andric    bool __invariants() const;
10710b57cec5SDimitry Andric
10720b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
10730b57cec5SDimitry Andric    void __move_assign(__deque_base& __c)
10740b57cec5SDimitry Andric        _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
10750b57cec5SDimitry Andric                   is_nothrow_move_assignable<allocator_type>::value)
10760b57cec5SDimitry Andric    {
10770b57cec5SDimitry Andric        __map_ = _VSTD::move(__c.__map_);
10780b57cec5SDimitry Andric        __start_ = __c.__start_;
10790b57cec5SDimitry Andric        size() = __c.size();
10800b57cec5SDimitry Andric        __move_assign_alloc(__c);
10810b57cec5SDimitry Andric        __c.__start_ = __c.size() = 0;
10820b57cec5SDimitry Andric    }
10830b57cec5SDimitry Andric
10840b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
10850b57cec5SDimitry Andric    void __move_assign_alloc(__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        {__move_assign_alloc(__c, integral_constant<bool,
10890b57cec5SDimitry Andric                      __alloc_traits::propagate_on_container_move_assignment::value>());}
10900b57cec5SDimitry Andric
10910b57cec5SDimitry Andricprivate:
10920b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
10930b57cec5SDimitry Andric    void __move_assign_alloc(__deque_base& __c, true_type)
10940b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
10950b57cec5SDimitry Andric        {
10960b57cec5SDimitry Andric            __alloc() = _VSTD::move(__c.__alloc());
10970b57cec5SDimitry Andric        }
10980b57cec5SDimitry Andric
10990b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
11000b57cec5SDimitry Andric    void __move_assign_alloc(__deque_base&, false_type) _NOEXCEPT
11010b57cec5SDimitry Andric        {}
11020b57cec5SDimitry Andric};
11030b57cec5SDimitry Andric
11040b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11050b57cec5SDimitry Andricconst typename __deque_base<_Tp, _Allocator>::difference_type
11060b57cec5SDimitry Andric    __deque_base<_Tp, _Allocator>::__block_size =
11070b57cec5SDimitry Andric        __deque_block_size<value_type, difference_type>::value;
11080b57cec5SDimitry Andric
11090b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11100b57cec5SDimitry Andricbool
11110b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::__invariants() const
11120b57cec5SDimitry Andric{
11130b57cec5SDimitry Andric    if (!__map_.__invariants())
11140b57cec5SDimitry Andric        return false;
11150b57cec5SDimitry Andric    if (__map_.size() >= size_type(-1) / __block_size)
11160b57cec5SDimitry Andric        return false;
11170b57cec5SDimitry Andric    for (typename __map::const_iterator __i = __map_.begin(), __e = __map_.end();
11180b57cec5SDimitry Andric         __i != __e; ++__i)
11190b57cec5SDimitry Andric        if (*__i == nullptr)
11200b57cec5SDimitry Andric            return false;
11210b57cec5SDimitry Andric    if (__map_.size() != 0)
11220b57cec5SDimitry Andric    {
11230b57cec5SDimitry Andric        if (size() >= __map_.size() * __block_size)
11240b57cec5SDimitry Andric            return false;
11250b57cec5SDimitry Andric        if (__start_ >= __map_.size() * __block_size - size())
11260b57cec5SDimitry Andric            return false;
11270b57cec5SDimitry Andric    }
11280b57cec5SDimitry Andric    else
11290b57cec5SDimitry Andric    {
11300b57cec5SDimitry Andric        if (size() != 0)
11310b57cec5SDimitry Andric            return false;
11320b57cec5SDimitry Andric        if (__start_ != 0)
11330b57cec5SDimitry Andric            return false;
11340b57cec5SDimitry Andric    }
11350b57cec5SDimitry Andric    return true;
11360b57cec5SDimitry Andric}
11370b57cec5SDimitry Andric
11380b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11390b57cec5SDimitry Andrictypename __deque_base<_Tp, _Allocator>::iterator
11400b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::begin() _NOEXCEPT
11410b57cec5SDimitry Andric{
11420b57cec5SDimitry Andric    __map_pointer __mp = __map_.begin() + __start_ / __block_size;
11430b57cec5SDimitry Andric    return iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
11440b57cec5SDimitry Andric}
11450b57cec5SDimitry Andric
11460b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11470b57cec5SDimitry Andrictypename __deque_base<_Tp, _Allocator>::const_iterator
11480b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::begin() const _NOEXCEPT
11490b57cec5SDimitry Andric{
11500b57cec5SDimitry Andric    __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __start_ / __block_size);
11510b57cec5SDimitry Andric    return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __start_ % __block_size);
11520b57cec5SDimitry Andric}
11530b57cec5SDimitry Andric
11540b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11550b57cec5SDimitry Andrictypename __deque_base<_Tp, _Allocator>::iterator
11560b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::end() _NOEXCEPT
11570b57cec5SDimitry Andric{
11580b57cec5SDimitry Andric    size_type __p = size() + __start_;
11590b57cec5SDimitry Andric    __map_pointer __mp = __map_.begin() + __p / __block_size;
11600b57cec5SDimitry Andric    return iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
11610b57cec5SDimitry Andric}
11620b57cec5SDimitry Andric
11630b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11640b57cec5SDimitry Andrictypename __deque_base<_Tp, _Allocator>::const_iterator
11650b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::end() const _NOEXCEPT
11660b57cec5SDimitry Andric{
11670b57cec5SDimitry Andric    size_type __p = size() + __start_;
11680b57cec5SDimitry Andric    __map_const_pointer __mp = static_cast<__map_const_pointer>(__map_.begin() + __p / __block_size);
11690b57cec5SDimitry Andric    return const_iterator(__mp, __map_.empty() ? 0 : *__mp + __p % __block_size);
11700b57cec5SDimitry Andric}
11710b57cec5SDimitry Andric
11720b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11730b57cec5SDimitry Andricinline
11740b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::__deque_base()
11750b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
1176480093f4SDimitry Andric    : __start_(0), __size_(0, __default_init_tag()) {}
11770b57cec5SDimitry Andric
11780b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11790b57cec5SDimitry Andricinline
11800b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::__deque_base(const allocator_type& __a)
11810b57cec5SDimitry Andric    : __map_(__pointer_allocator(__a)), __start_(0), __size_(0, __a) {}
11820b57cec5SDimitry Andric
11830b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11840b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::~__deque_base()
11850b57cec5SDimitry Andric{
11860b57cec5SDimitry Andric    clear();
11870b57cec5SDimitry Andric    typename __map::iterator __i = __map_.begin();
11880b57cec5SDimitry Andric    typename __map::iterator __e = __map_.end();
11890b57cec5SDimitry Andric    for (; __i != __e; ++__i)
11900b57cec5SDimitry Andric        __alloc_traits::deallocate(__alloc(), *__i, __block_size);
11910b57cec5SDimitry Andric}
11920b57cec5SDimitry Andric
11930b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
11940b57cec5SDimitry Andric
11950b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
11960b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c)
11970b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
11980b57cec5SDimitry Andric    : __map_(_VSTD::move(__c.__map_)),
11990b57cec5SDimitry Andric      __start_(_VSTD::move(__c.__start_)),
12000b57cec5SDimitry Andric      __size_(_VSTD::move(__c.__size_))
12010b57cec5SDimitry Andric{
12020b57cec5SDimitry Andric    __c.__start_ = 0;
12030b57cec5SDimitry Andric    __c.size() = 0;
12040b57cec5SDimitry Andric}
12050b57cec5SDimitry Andric
12060b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
12070b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::__deque_base(__deque_base&& __c, const allocator_type& __a)
12080b57cec5SDimitry Andric    : __map_(_VSTD::move(__c.__map_), __pointer_allocator(__a)),
12090b57cec5SDimitry Andric      __start_(_VSTD::move(__c.__start_)),
12100b57cec5SDimitry Andric      __size_(_VSTD::move(__c.size()), __a)
12110b57cec5SDimitry Andric{
12120b57cec5SDimitry Andric    if (__a == __c.__alloc())
12130b57cec5SDimitry Andric    {
12140b57cec5SDimitry Andric        __c.__start_ = 0;
12150b57cec5SDimitry Andric        __c.size() = 0;
12160b57cec5SDimitry Andric    }
12170b57cec5SDimitry Andric    else
12180b57cec5SDimitry Andric    {
12190b57cec5SDimitry Andric        __map_.clear();
12200b57cec5SDimitry Andric        __start_ = 0;
12210b57cec5SDimitry Andric        size() = 0;
12220b57cec5SDimitry Andric    }
12230b57cec5SDimitry Andric}
12240b57cec5SDimitry Andric
12250b57cec5SDimitry Andric#endif  // _LIBCPP_CXX03_LANG
12260b57cec5SDimitry Andric
12270b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
12280b57cec5SDimitry Andricvoid
12290b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::swap(__deque_base& __c)
12300b57cec5SDimitry Andric#if _LIBCPP_STD_VER >= 14
12310b57cec5SDimitry Andric        _NOEXCEPT
12320b57cec5SDimitry Andric#else
12330b57cec5SDimitry Andric        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
12340b57cec5SDimitry Andric                    __is_nothrow_swappable<allocator_type>::value)
12350b57cec5SDimitry Andric#endif
12360b57cec5SDimitry Andric{
12370b57cec5SDimitry Andric    __map_.swap(__c.__map_);
12380b57cec5SDimitry Andric    _VSTD::swap(__start_, __c.__start_);
12390b57cec5SDimitry Andric    _VSTD::swap(size(), __c.size());
12400b57cec5SDimitry Andric    __swap_allocator(__alloc(), __c.__alloc());
12410b57cec5SDimitry Andric}
12420b57cec5SDimitry Andric
12430b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
12440b57cec5SDimitry Andricvoid
12450b57cec5SDimitry Andric__deque_base<_Tp, _Allocator>::clear() _NOEXCEPT
12460b57cec5SDimitry Andric{
12470b57cec5SDimitry Andric    allocator_type& __a = __alloc();
12480b57cec5SDimitry Andric    for (iterator __i = begin(), __e = end(); __i != __e; ++__i)
12490b57cec5SDimitry Andric        __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
12500b57cec5SDimitry Andric    size() = 0;
12510b57cec5SDimitry Andric    while (__map_.size() > 2)
12520b57cec5SDimitry Andric    {
12530b57cec5SDimitry Andric        __alloc_traits::deallocate(__a, __map_.front(), __block_size);
12540b57cec5SDimitry Andric        __map_.pop_front();
12550b57cec5SDimitry Andric    }
12560b57cec5SDimitry Andric    switch (__map_.size())
12570b57cec5SDimitry Andric    {
12580b57cec5SDimitry Andric    case 1:
12590b57cec5SDimitry Andric        __start_ = __block_size / 2;
12600b57cec5SDimitry Andric        break;
12610b57cec5SDimitry Andric    case 2:
12620b57cec5SDimitry Andric        __start_ = __block_size;
12630b57cec5SDimitry Andric        break;
12640b57cec5SDimitry Andric    }
12650b57cec5SDimitry Andric}
12660b57cec5SDimitry Andric
12670b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator /*= allocator<_Tp>*/>
12680b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS deque
12690b57cec5SDimitry Andric    : private __deque_base<_Tp, _Allocator>
12700b57cec5SDimitry Andric{
12710b57cec5SDimitry Andricpublic:
12720b57cec5SDimitry Andric    // types:
12730b57cec5SDimitry Andric
12740b57cec5SDimitry Andric    typedef _Tp value_type;
12750b57cec5SDimitry Andric    typedef _Allocator allocator_type;
12760b57cec5SDimitry Andric
12770b57cec5SDimitry Andric    static_assert((is_same<typename allocator_type::value_type, value_type>::value),
12780b57cec5SDimitry Andric                  "Allocator::value_type must be same type as value_type");
12790b57cec5SDimitry Andric
12800b57cec5SDimitry Andric    typedef __deque_base<value_type, allocator_type> __base;
12810b57cec5SDimitry Andric
12820b57cec5SDimitry Andric    typedef typename __base::__alloc_traits        __alloc_traits;
12830b57cec5SDimitry Andric    typedef typename __base::reference             reference;
12840b57cec5SDimitry Andric    typedef typename __base::const_reference       const_reference;
12850b57cec5SDimitry Andric    typedef typename __base::iterator              iterator;
12860b57cec5SDimitry Andric    typedef typename __base::const_iterator        const_iterator;
12870b57cec5SDimitry Andric    typedef typename __base::size_type             size_type;
12880b57cec5SDimitry Andric    typedef typename __base::difference_type       difference_type;
12890b57cec5SDimitry Andric
12900b57cec5SDimitry Andric    typedef typename __base::pointer               pointer;
12910b57cec5SDimitry Andric    typedef typename __base::const_pointer         const_pointer;
12920b57cec5SDimitry Andric    typedef _VSTD::reverse_iterator<iterator>       reverse_iterator;
12930b57cec5SDimitry Andric    typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
12940b57cec5SDimitry Andric
1295e40139ffSDimitry Andric    using typename __base::__deque_range;
1296e40139ffSDimitry Andric    using typename __base::__deque_block_range;
1297e40139ffSDimitry Andric    using typename __base::_ConstructTransaction;
1298e40139ffSDimitry Andric
12990b57cec5SDimitry Andric    // construct/copy/destroy:
13000b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13010b57cec5SDimitry Andric    deque()
13020b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
13030b57cec5SDimitry Andric        {}
13040b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit deque(const allocator_type& __a) : __base(__a) {}
13050b57cec5SDimitry Andric    explicit deque(size_type __n);
13060b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
13070b57cec5SDimitry Andric    explicit deque(size_type __n, const _Allocator& __a);
13080b57cec5SDimitry Andric#endif
13090b57cec5SDimitry Andric    deque(size_type __n, const value_type& __v);
13100b57cec5SDimitry Andric    deque(size_type __n, const value_type& __v, const allocator_type& __a);
13110b57cec5SDimitry Andric    template <class _InputIter>
13120b57cec5SDimitry Andric        deque(_InputIter __f, _InputIter __l,
1313480093f4SDimitry Andric              typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type* = 0);
13140b57cec5SDimitry Andric    template <class _InputIter>
13150b57cec5SDimitry Andric        deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1316480093f4SDimitry Andric              typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type* = 0);
13170b57cec5SDimitry Andric    deque(const deque& __c);
13180b57cec5SDimitry Andric    deque(const deque& __c, const allocator_type& __a);
13190b57cec5SDimitry Andric
13200b57cec5SDimitry Andric    deque& operator=(const deque& __c);
13210b57cec5SDimitry Andric
13220b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
13230b57cec5SDimitry Andric    deque(initializer_list<value_type> __il);
13240b57cec5SDimitry Andric    deque(initializer_list<value_type> __il, const allocator_type& __a);
13250b57cec5SDimitry Andric
13260b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13270b57cec5SDimitry Andric    deque& operator=(initializer_list<value_type> __il) {assign(__il); return *this;}
13280b57cec5SDimitry Andric
13290b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13300b57cec5SDimitry Andric    deque(deque&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__base>::value);
13310b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13320b57cec5SDimitry Andric    deque(deque&& __c, const allocator_type& __a);
13330b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13340b57cec5SDimitry Andric    deque& operator=(deque&& __c)
13350b57cec5SDimitry Andric        _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
13360b57cec5SDimitry Andric                   is_nothrow_move_assignable<allocator_type>::value);
13370b57cec5SDimitry Andric
13380b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13390b57cec5SDimitry Andric    void assign(initializer_list<value_type> __il) {assign(__il.begin(), __il.end());}
13400b57cec5SDimitry Andric#endif  // _LIBCPP_CXX03_LANG
13410b57cec5SDimitry Andric
13420b57cec5SDimitry Andric    template <class _InputIter>
13430b57cec5SDimitry Andric        void assign(_InputIter __f, _InputIter __l,
1344480093f4SDimitry Andric                    typename enable_if<__is_cpp17_input_iterator<_InputIter>::value &&
1345480093f4SDimitry Andric                                      !__is_cpp17_random_access_iterator<_InputIter>::value>::type* = 0);
13460b57cec5SDimitry Andric    template <class _RAIter>
13470b57cec5SDimitry Andric        void assign(_RAIter __f, _RAIter __l,
1348480093f4SDimitry Andric                    typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type* = 0);
13490b57cec5SDimitry Andric    void assign(size_type __n, const value_type& __v);
13500b57cec5SDimitry Andric
13510b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13520b57cec5SDimitry Andric    allocator_type get_allocator() const _NOEXCEPT;
13530b57cec5SDimitry Andric
13540b57cec5SDimitry Andric    // iterators:
13550b57cec5SDimitry Andric
13560b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13570b57cec5SDimitry Andric    iterator       begin() _NOEXCEPT       {return __base::begin();}
13580b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13590b57cec5SDimitry Andric    const_iterator begin() const _NOEXCEPT {return __base::begin();}
13600b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13610b57cec5SDimitry Andric    iterator       end() _NOEXCEPT         {return __base::end();}
13620b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13630b57cec5SDimitry Andric    const_iterator end()   const _NOEXCEPT {return __base::end();}
13640b57cec5SDimitry Andric
13650b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13660b57cec5SDimitry Andric    reverse_iterator       rbegin() _NOEXCEPT
13670b57cec5SDimitry Andric        {return       reverse_iterator(__base::end());}
13680b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13690b57cec5SDimitry Andric    const_reverse_iterator rbegin() const _NOEXCEPT
13700b57cec5SDimitry Andric        {return const_reverse_iterator(__base::end());}
13710b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13720b57cec5SDimitry Andric    reverse_iterator       rend() _NOEXCEPT
13730b57cec5SDimitry Andric        {return       reverse_iterator(__base::begin());}
13740b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13750b57cec5SDimitry Andric    const_reverse_iterator rend()   const _NOEXCEPT
13760b57cec5SDimitry Andric        {return const_reverse_iterator(__base::begin());}
13770b57cec5SDimitry Andric
13780b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13790b57cec5SDimitry Andric    const_iterator         cbegin()  const _NOEXCEPT
13800b57cec5SDimitry Andric        {return __base::begin();}
13810b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13820b57cec5SDimitry Andric    const_iterator         cend()    const _NOEXCEPT
13830b57cec5SDimitry Andric        {return __base::end();}
13840b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13850b57cec5SDimitry Andric    const_reverse_iterator crbegin() const _NOEXCEPT
13860b57cec5SDimitry Andric        {return const_reverse_iterator(__base::end());}
13870b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13880b57cec5SDimitry Andric    const_reverse_iterator crend()   const _NOEXCEPT
13890b57cec5SDimitry Andric        {return const_reverse_iterator(__base::begin());}
13900b57cec5SDimitry Andric
13910b57cec5SDimitry Andric    // capacity:
13920b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13930b57cec5SDimitry Andric    size_type size() const _NOEXCEPT {return __base::size();}
13940b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13950b57cec5SDimitry Andric    size_type max_size() const _NOEXCEPT
13960b57cec5SDimitry Andric        {return std::min<size_type>(
13970b57cec5SDimitry Andric            __alloc_traits::max_size(__base::__alloc()),
13980b57cec5SDimitry Andric            numeric_limits<difference_type>::max());}
13990b57cec5SDimitry Andric    void resize(size_type __n);
14000b57cec5SDimitry Andric    void resize(size_type __n, const value_type& __v);
14010b57cec5SDimitry Andric    void shrink_to_fit() _NOEXCEPT;
14020b57cec5SDimitry Andric    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
14030b57cec5SDimitry Andric    bool empty() const _NOEXCEPT {return __base::size() == 0;}
14040b57cec5SDimitry Andric
14050b57cec5SDimitry Andric    // element access:
14060b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14070b57cec5SDimitry Andric    reference operator[](size_type __i) _NOEXCEPT;
14080b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14090b57cec5SDimitry Andric    const_reference operator[](size_type __i) const _NOEXCEPT;
14100b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14110b57cec5SDimitry Andric    reference at(size_type __i);
14120b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14130b57cec5SDimitry Andric    const_reference at(size_type __i) const;
14140b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14150b57cec5SDimitry Andric    reference front() _NOEXCEPT;
14160b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14170b57cec5SDimitry Andric    const_reference front() const _NOEXCEPT;
14180b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14190b57cec5SDimitry Andric    reference back() _NOEXCEPT;
14200b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14210b57cec5SDimitry Andric    const_reference back() const _NOEXCEPT;
14220b57cec5SDimitry Andric
14230b57cec5SDimitry Andric    // 23.2.2.3 modifiers:
14240b57cec5SDimitry Andric    void push_front(const value_type& __v);
14250b57cec5SDimitry Andric    void push_back(const value_type& __v);
14260b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
14270b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
14280b57cec5SDimitry Andric    template <class... _Args> reference emplace_front(_Args&&... __args);
14290b57cec5SDimitry Andric    template <class... _Args> reference emplace_back (_Args&&... __args);
14300b57cec5SDimitry Andric#else
14310b57cec5SDimitry Andric    template <class... _Args> void      emplace_front(_Args&&... __args);
14320b57cec5SDimitry Andric    template <class... _Args> void      emplace_back (_Args&&... __args);
14330b57cec5SDimitry Andric#endif
14340b57cec5SDimitry Andric    template <class... _Args> iterator emplace(const_iterator __p, _Args&&... __args);
14350b57cec5SDimitry Andric
14360b57cec5SDimitry Andric    void push_front(value_type&& __v);
14370b57cec5SDimitry Andric    void push_back(value_type&& __v);
14380b57cec5SDimitry Andric    iterator insert(const_iterator __p, value_type&& __v);
14390b57cec5SDimitry Andric
14400b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14410b57cec5SDimitry Andric    iterator insert(const_iterator __p, initializer_list<value_type> __il)
14420b57cec5SDimitry Andric        {return insert(__p, __il.begin(), __il.end());}
14430b57cec5SDimitry Andric#endif  // _LIBCPP_CXX03_LANG
14440b57cec5SDimitry Andric    iterator insert(const_iterator __p, const value_type& __v);
14450b57cec5SDimitry Andric    iterator insert(const_iterator __p, size_type __n, const value_type& __v);
14460b57cec5SDimitry Andric    template <class _InputIter>
14470b57cec5SDimitry Andric        iterator insert(const_iterator __p, _InputIter __f, _InputIter __l,
1448480093f4SDimitry Andric                         typename enable_if<__is_cpp17_input_iterator<_InputIter>::value
1449480093f4SDimitry Andric                                         &&!__is_cpp17_forward_iterator<_InputIter>::value>::type* = 0);
14500b57cec5SDimitry Andric    template <class _ForwardIterator>
14510b57cec5SDimitry Andric        iterator insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
1452480093f4SDimitry Andric                               typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value
1453480093f4SDimitry Andric                                         &&!__is_cpp17_bidirectional_iterator<_ForwardIterator>::value>::type* = 0);
14540b57cec5SDimitry Andric    template <class _BiIter>
14550b57cec5SDimitry Andric        iterator insert(const_iterator __p, _BiIter __f, _BiIter __l,
1456480093f4SDimitry Andric                         typename enable_if<__is_cpp17_bidirectional_iterator<_BiIter>::value>::type* = 0);
14570b57cec5SDimitry Andric
14580b57cec5SDimitry Andric    void pop_front();
14590b57cec5SDimitry Andric    void pop_back();
14600b57cec5SDimitry Andric    iterator erase(const_iterator __p);
14610b57cec5SDimitry Andric    iterator erase(const_iterator __f, const_iterator __l);
14620b57cec5SDimitry Andric
14630b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14640b57cec5SDimitry Andric    void swap(deque& __c)
14650b57cec5SDimitry Andric#if _LIBCPP_STD_VER >= 14
14660b57cec5SDimitry Andric        _NOEXCEPT;
14670b57cec5SDimitry Andric#else
14680b57cec5SDimitry Andric        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
14690b57cec5SDimitry Andric                   __is_nothrow_swappable<allocator_type>::value);
14700b57cec5SDimitry Andric#endif
14710b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14720b57cec5SDimitry Andric    void clear() _NOEXCEPT;
14730b57cec5SDimitry Andric
14740b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14750b57cec5SDimitry Andric    bool __invariants() const {return __base::__invariants();}
1476e40139ffSDimitry Andric
14770b57cec5SDimitry Andric    typedef typename __base::__map_const_pointer __map_const_pointer;
14780b57cec5SDimitry Andric
14790b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14800b57cec5SDimitry Andric    static size_type __recommend_blocks(size_type __n)
14810b57cec5SDimitry Andric    {
14820b57cec5SDimitry Andric        return __n / __base::__block_size + (__n % __base::__block_size != 0);
14830b57cec5SDimitry Andric    }
14840b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14850b57cec5SDimitry Andric    size_type __capacity() const
14860b57cec5SDimitry Andric    {
14870b57cec5SDimitry Andric        return __base::__map_.size() == 0 ? 0 : __base::__map_.size() * __base::__block_size - 1;
14880b57cec5SDimitry Andric    }
14890b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1490e40139ffSDimitry Andric    size_type __block_count() const
1491e40139ffSDimitry Andric    {
1492e40139ffSDimitry Andric        return __base::__map_.size();
1493e40139ffSDimitry Andric    }
1494e40139ffSDimitry Andric
1495e40139ffSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
14960b57cec5SDimitry Andric    size_type __front_spare() const
14970b57cec5SDimitry Andric    {
14980b57cec5SDimitry Andric        return __base::__start_;
14990b57cec5SDimitry Andric    }
15000b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1501e40139ffSDimitry Andric    size_type __front_spare_blocks() const {
1502e40139ffSDimitry Andric      return __front_spare() / __base::__block_size;
1503e40139ffSDimitry Andric    }
1504e40139ffSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
15050b57cec5SDimitry Andric    size_type __back_spare() const
15060b57cec5SDimitry Andric    {
15070b57cec5SDimitry Andric        return __capacity() - (__base::__start_ + __base::size());
15080b57cec5SDimitry Andric    }
1509e40139ffSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1510e40139ffSDimitry Andric    size_type __back_spare_blocks() const {
1511e40139ffSDimitry Andric      return __back_spare() / __base::__block_size;
1512e40139ffSDimitry Andric    }
1513e40139ffSDimitry Andric
1514e40139ffSDimitry Andric private:
1515e40139ffSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1516e40139ffSDimitry Andric    bool __maybe_remove_front_spare(bool __keep_one = true) {
1517e40139ffSDimitry Andric      if (__front_spare_blocks() >= 2 || (!__keep_one && __front_spare_blocks())) {
1518e40139ffSDimitry Andric        __alloc_traits::deallocate(__base::__alloc(), __base::__map_.front(),
1519e40139ffSDimitry Andric                                   __base::__block_size);
1520e40139ffSDimitry Andric        __base::__map_.pop_front();
1521e40139ffSDimitry Andric        __base::__start_ -= __base::__block_size;
1522e40139ffSDimitry Andric        return true;
1523e40139ffSDimitry Andric      }
1524e40139ffSDimitry Andric      return false;
1525e40139ffSDimitry Andric    }
1526e40139ffSDimitry Andric
1527e40139ffSDimitry Andric    _LIBCPP_INLINE_VISIBILITY
1528e40139ffSDimitry Andric    bool __maybe_remove_back_spare(bool __keep_one = true) {
1529e40139ffSDimitry Andric      if (__back_spare_blocks() >= 2 || (!__keep_one && __back_spare_blocks())) {
1530e40139ffSDimitry Andric        __alloc_traits::deallocate(__base::__alloc(), __base::__map_.back(),
1531e40139ffSDimitry Andric                                   __base::__block_size);
1532e40139ffSDimitry Andric        __base::__map_.pop_back();
1533e40139ffSDimitry Andric        return true;
1534e40139ffSDimitry Andric      }
1535e40139ffSDimitry Andric      return false;
1536e40139ffSDimitry Andric    }
15370b57cec5SDimitry Andric
15380b57cec5SDimitry Andric    template <class _InpIter>
15390b57cec5SDimitry Andric        void __append(_InpIter __f, _InpIter __l,
1540480093f4SDimitry Andric                 typename enable_if<__is_cpp17_input_iterator<_InpIter>::value &&
1541480093f4SDimitry Andric                                   !__is_cpp17_forward_iterator<_InpIter>::value>::type* = 0);
15420b57cec5SDimitry Andric    template <class _ForIter>
15430b57cec5SDimitry Andric        void __append(_ForIter __f, _ForIter __l,
1544480093f4SDimitry Andric                      typename enable_if<__is_cpp17_forward_iterator<_ForIter>::value>::type* = 0);
15450b57cec5SDimitry Andric    void __append(size_type __n);
15460b57cec5SDimitry Andric    void __append(size_type __n, const value_type& __v);
15470b57cec5SDimitry Andric    void __erase_to_end(const_iterator __f);
15480b57cec5SDimitry Andric    void __add_front_capacity();
15490b57cec5SDimitry Andric    void __add_front_capacity(size_type __n);
15500b57cec5SDimitry Andric    void __add_back_capacity();
15510b57cec5SDimitry Andric    void __add_back_capacity(size_type __n);
15520b57cec5SDimitry Andric    iterator __move_and_check(iterator __f, iterator __l, iterator __r,
15530b57cec5SDimitry Andric                              const_pointer& __vt);
15540b57cec5SDimitry Andric    iterator __move_backward_and_check(iterator __f, iterator __l, iterator __r,
15550b57cec5SDimitry Andric                                       const_pointer& __vt);
15560b57cec5SDimitry Andric    void __move_construct_and_check(iterator __f, iterator __l,
15570b57cec5SDimitry Andric                                    iterator __r, const_pointer& __vt);
15580b57cec5SDimitry Andric    void __move_construct_backward_and_check(iterator __f, iterator __l,
15590b57cec5SDimitry Andric                                             iterator __r, const_pointer& __vt);
15600b57cec5SDimitry Andric
15610b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
15620b57cec5SDimitry Andric    void __copy_assign_alloc(const deque& __c)
15630b57cec5SDimitry Andric        {__copy_assign_alloc(__c, integral_constant<bool,
15640b57cec5SDimitry Andric                      __alloc_traits::propagate_on_container_copy_assignment::value>());}
15650b57cec5SDimitry Andric
15660b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
15670b57cec5SDimitry Andric    void __copy_assign_alloc(const deque& __c, true_type)
15680b57cec5SDimitry Andric        {
15690b57cec5SDimitry Andric            if (__base::__alloc() != __c.__alloc())
15700b57cec5SDimitry Andric            {
15710b57cec5SDimitry Andric                clear();
15720b57cec5SDimitry Andric                shrink_to_fit();
15730b57cec5SDimitry Andric            }
15740b57cec5SDimitry Andric            __base::__alloc() = __c.__alloc();
15750b57cec5SDimitry Andric            __base::__map_.__alloc() = __c.__map_.__alloc();
15760b57cec5SDimitry Andric        }
15770b57cec5SDimitry Andric
15780b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
15790b57cec5SDimitry Andric    void __copy_assign_alloc(const deque&, false_type)
15800b57cec5SDimitry Andric        {}
15810b57cec5SDimitry Andric
15820b57cec5SDimitry Andric    void __move_assign(deque& __c, true_type)
15830b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
15840b57cec5SDimitry Andric    void __move_assign(deque& __c, false_type);
15850b57cec5SDimitry Andric};
15860b57cec5SDimitry Andric
15870b57cec5SDimitry Andric#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
15880b57cec5SDimitry Andrictemplate<class _InputIterator,
15890b57cec5SDimitry Andric         class _Alloc = typename std::allocator<typename iterator_traits<_InputIterator>::value_type>,
15900b57cec5SDimitry Andric         class = typename enable_if<__is_allocator<_Alloc>::value, void>::type
15910b57cec5SDimitry Andric         >
15920b57cec5SDimitry Andricdeque(_InputIterator, _InputIterator)
15930b57cec5SDimitry Andric  -> deque<typename iterator_traits<_InputIterator>::value_type, _Alloc>;
15940b57cec5SDimitry Andric
15950b57cec5SDimitry Andrictemplate<class _InputIterator,
15960b57cec5SDimitry Andric         class _Alloc,
15970b57cec5SDimitry Andric         class = typename enable_if<__is_allocator<_Alloc>::value, void>::type
15980b57cec5SDimitry Andric         >
15990b57cec5SDimitry Andricdeque(_InputIterator, _InputIterator, _Alloc)
16000b57cec5SDimitry Andric  -> deque<typename iterator_traits<_InputIterator>::value_type, _Alloc>;
16010b57cec5SDimitry Andric#endif
16020b57cec5SDimitry Andric
16030b57cec5SDimitry Andric
16040b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16050b57cec5SDimitry Andricdeque<_Tp, _Allocator>::deque(size_type __n)
16060b57cec5SDimitry Andric{
16070b57cec5SDimitry Andric    if (__n > 0)
16080b57cec5SDimitry Andric        __append(__n);
16090b57cec5SDimitry Andric}
16100b57cec5SDimitry Andric
16110b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
16120b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16130b57cec5SDimitry Andricdeque<_Tp, _Allocator>::deque(size_type __n, const _Allocator& __a)
16140b57cec5SDimitry Andric    : __base(__a)
16150b57cec5SDimitry Andric{
16160b57cec5SDimitry Andric    if (__n > 0)
16170b57cec5SDimitry Andric        __append(__n);
16180b57cec5SDimitry Andric}
16190b57cec5SDimitry Andric#endif
16200b57cec5SDimitry Andric
16210b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16220b57cec5SDimitry Andricdeque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v)
16230b57cec5SDimitry Andric{
16240b57cec5SDimitry Andric    if (__n > 0)
16250b57cec5SDimitry Andric        __append(__n, __v);
16260b57cec5SDimitry Andric}
16270b57cec5SDimitry Andric
16280b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16290b57cec5SDimitry Andricdeque<_Tp, _Allocator>::deque(size_type __n, const value_type& __v, const allocator_type& __a)
16300b57cec5SDimitry Andric    : __base(__a)
16310b57cec5SDimitry Andric{
16320b57cec5SDimitry Andric    if (__n > 0)
16330b57cec5SDimitry Andric        __append(__n, __v);
16340b57cec5SDimitry Andric}
16350b57cec5SDimitry Andric
16360b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16370b57cec5SDimitry Andrictemplate <class _InputIter>
16380b57cec5SDimitry Andricdeque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l,
1639480093f4SDimitry Andric              typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type*)
16400b57cec5SDimitry Andric{
16410b57cec5SDimitry Andric    __append(__f, __l);
16420b57cec5SDimitry Andric}
16430b57cec5SDimitry Andric
16440b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16450b57cec5SDimitry Andrictemplate <class _InputIter>
16460b57cec5SDimitry Andricdeque<_Tp, _Allocator>::deque(_InputIter __f, _InputIter __l, const allocator_type& __a,
1647480093f4SDimitry Andric              typename enable_if<__is_cpp17_input_iterator<_InputIter>::value>::type*)
16480b57cec5SDimitry Andric    : __base(__a)
16490b57cec5SDimitry Andric{
16500b57cec5SDimitry Andric    __append(__f, __l);
16510b57cec5SDimitry Andric}
16520b57cec5SDimitry Andric
16530b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16540b57cec5SDimitry Andricdeque<_Tp, _Allocator>::deque(const deque& __c)
16550b57cec5SDimitry Andric    : __base(__alloc_traits::select_on_container_copy_construction(__c.__alloc()))
16560b57cec5SDimitry Andric{
16570b57cec5SDimitry Andric    __append(__c.begin(), __c.end());
16580b57cec5SDimitry Andric}
16590b57cec5SDimitry Andric
16600b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16610b57cec5SDimitry Andricdeque<_Tp, _Allocator>::deque(const deque& __c, const allocator_type& __a)
16620b57cec5SDimitry Andric    : __base(__a)
16630b57cec5SDimitry Andric{
16640b57cec5SDimitry Andric    __append(__c.begin(), __c.end());
16650b57cec5SDimitry Andric}
16660b57cec5SDimitry Andric
16670b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16680b57cec5SDimitry Andricdeque<_Tp, _Allocator>&
16690b57cec5SDimitry Andricdeque<_Tp, _Allocator>::operator=(const deque& __c)
16700b57cec5SDimitry Andric{
16710b57cec5SDimitry Andric    if (this != &__c)
16720b57cec5SDimitry Andric    {
16730b57cec5SDimitry Andric        __copy_assign_alloc(__c);
16740b57cec5SDimitry Andric        assign(__c.begin(), __c.end());
16750b57cec5SDimitry Andric    }
16760b57cec5SDimitry Andric    return *this;
16770b57cec5SDimitry Andric}
16780b57cec5SDimitry Andric
16790b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
16800b57cec5SDimitry Andric
16810b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16820b57cec5SDimitry Andricdeque<_Tp, _Allocator>::deque(initializer_list<value_type> __il)
16830b57cec5SDimitry Andric{
16840b57cec5SDimitry Andric    __append(__il.begin(), __il.end());
16850b57cec5SDimitry Andric}
16860b57cec5SDimitry Andric
16870b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16880b57cec5SDimitry Andricdeque<_Tp, _Allocator>::deque(initializer_list<value_type> __il, const allocator_type& __a)
16890b57cec5SDimitry Andric    : __base(__a)
16900b57cec5SDimitry Andric{
16910b57cec5SDimitry Andric    __append(__il.begin(), __il.end());
16920b57cec5SDimitry Andric}
16930b57cec5SDimitry Andric
16940b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
16950b57cec5SDimitry Andricinline
16960b57cec5SDimitry Andricdeque<_Tp, _Allocator>::deque(deque&& __c)
16970b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
16980b57cec5SDimitry Andric    : __base(_VSTD::move(__c))
16990b57cec5SDimitry Andric{
17000b57cec5SDimitry Andric}
17010b57cec5SDimitry Andric
17020b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
17030b57cec5SDimitry Andricinline
17040b57cec5SDimitry Andricdeque<_Tp, _Allocator>::deque(deque&& __c, const allocator_type& __a)
17050b57cec5SDimitry Andric    : __base(_VSTD::move(__c), __a)
17060b57cec5SDimitry Andric{
17070b57cec5SDimitry Andric    if (__a != __c.__alloc())
17080b57cec5SDimitry Andric    {
17090b57cec5SDimitry Andric        typedef move_iterator<iterator> _Ip;
17100b57cec5SDimitry Andric        assign(_Ip(__c.begin()), _Ip(__c.end()));
17110b57cec5SDimitry Andric    }
17120b57cec5SDimitry Andric}
17130b57cec5SDimitry Andric
17140b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
17150b57cec5SDimitry Andricinline
17160b57cec5SDimitry Andricdeque<_Tp, _Allocator>&
17170b57cec5SDimitry Andricdeque<_Tp, _Allocator>::operator=(deque&& __c)
17180b57cec5SDimitry Andric        _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
17190b57cec5SDimitry Andric                   is_nothrow_move_assignable<allocator_type>::value)
17200b57cec5SDimitry Andric{
17210b57cec5SDimitry Andric    __move_assign(__c, integral_constant<bool,
17220b57cec5SDimitry Andric          __alloc_traits::propagate_on_container_move_assignment::value>());
17230b57cec5SDimitry Andric    return *this;
17240b57cec5SDimitry Andric}
17250b57cec5SDimitry Andric
17260b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
17270b57cec5SDimitry Andricvoid
17280b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__move_assign(deque& __c, false_type)
17290b57cec5SDimitry Andric{
17300b57cec5SDimitry Andric    if (__base::__alloc() != __c.__alloc())
17310b57cec5SDimitry Andric    {
17320b57cec5SDimitry Andric        typedef move_iterator<iterator> _Ip;
17330b57cec5SDimitry Andric        assign(_Ip(__c.begin()), _Ip(__c.end()));
17340b57cec5SDimitry Andric    }
17350b57cec5SDimitry Andric    else
17360b57cec5SDimitry Andric        __move_assign(__c, true_type());
17370b57cec5SDimitry Andric}
17380b57cec5SDimitry Andric
17390b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
17400b57cec5SDimitry Andricvoid
17410b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__move_assign(deque& __c, true_type)
17420b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
17430b57cec5SDimitry Andric{
17440b57cec5SDimitry Andric    clear();
17450b57cec5SDimitry Andric    shrink_to_fit();
17460b57cec5SDimitry Andric    __base::__move_assign(__c);
17470b57cec5SDimitry Andric}
17480b57cec5SDimitry Andric
17490b57cec5SDimitry Andric#endif  // _LIBCPP_CXX03_LANG
17500b57cec5SDimitry Andric
17510b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
17520b57cec5SDimitry Andrictemplate <class _InputIter>
17530b57cec5SDimitry Andricvoid
17540b57cec5SDimitry Andricdeque<_Tp, _Allocator>::assign(_InputIter __f, _InputIter __l,
1755480093f4SDimitry Andric                               typename enable_if<__is_cpp17_input_iterator<_InputIter>::value &&
1756480093f4SDimitry Andric                                                 !__is_cpp17_random_access_iterator<_InputIter>::value>::type*)
17570b57cec5SDimitry Andric{
17580b57cec5SDimitry Andric    iterator __i = __base::begin();
17590b57cec5SDimitry Andric    iterator __e = __base::end();
17600b57cec5SDimitry Andric    for (; __f != __l && __i != __e; ++__f, (void) ++__i)
17610b57cec5SDimitry Andric        *__i = *__f;
17620b57cec5SDimitry Andric    if (__f != __l)
17630b57cec5SDimitry Andric        __append(__f, __l);
17640b57cec5SDimitry Andric    else
17650b57cec5SDimitry Andric        __erase_to_end(__i);
17660b57cec5SDimitry Andric}
17670b57cec5SDimitry Andric
17680b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
17690b57cec5SDimitry Andrictemplate <class _RAIter>
17700b57cec5SDimitry Andricvoid
17710b57cec5SDimitry Andricdeque<_Tp, _Allocator>::assign(_RAIter __f, _RAIter __l,
1772480093f4SDimitry Andric                               typename enable_if<__is_cpp17_random_access_iterator<_RAIter>::value>::type*)
17730b57cec5SDimitry Andric{
17740b57cec5SDimitry Andric    if (static_cast<size_type>(__l - __f) > __base::size())
17750b57cec5SDimitry Andric    {
17760b57cec5SDimitry Andric        _RAIter __m = __f + __base::size();
17770b57cec5SDimitry Andric        _VSTD::copy(__f, __m, __base::begin());
17780b57cec5SDimitry Andric        __append(__m, __l);
17790b57cec5SDimitry Andric    }
17800b57cec5SDimitry Andric    else
17810b57cec5SDimitry Andric        __erase_to_end(_VSTD::copy(__f, __l, __base::begin()));
17820b57cec5SDimitry Andric}
17830b57cec5SDimitry Andric
17840b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
17850b57cec5SDimitry Andricvoid
17860b57cec5SDimitry Andricdeque<_Tp, _Allocator>::assign(size_type __n, const value_type& __v)
17870b57cec5SDimitry Andric{
17880b57cec5SDimitry Andric    if (__n > __base::size())
17890b57cec5SDimitry Andric    {
17900b57cec5SDimitry Andric        _VSTD::fill_n(__base::begin(), __base::size(), __v);
17910b57cec5SDimitry Andric        __n -= __base::size();
17920b57cec5SDimitry Andric        __append(__n, __v);
17930b57cec5SDimitry Andric    }
17940b57cec5SDimitry Andric    else
17950b57cec5SDimitry Andric        __erase_to_end(_VSTD::fill_n(__base::begin(), __n, __v));
17960b57cec5SDimitry Andric}
17970b57cec5SDimitry Andric
17980b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
17990b57cec5SDimitry Andricinline
18000b57cec5SDimitry Andric_Allocator
18010b57cec5SDimitry Andricdeque<_Tp, _Allocator>::get_allocator() const _NOEXCEPT
18020b57cec5SDimitry Andric{
18030b57cec5SDimitry Andric    return __base::__alloc();
18040b57cec5SDimitry Andric}
18050b57cec5SDimitry Andric
18060b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
18070b57cec5SDimitry Andricvoid
18080b57cec5SDimitry Andricdeque<_Tp, _Allocator>::resize(size_type __n)
18090b57cec5SDimitry Andric{
18100b57cec5SDimitry Andric    if (__n > __base::size())
18110b57cec5SDimitry Andric        __append(__n - __base::size());
18120b57cec5SDimitry Andric    else if (__n < __base::size())
18130b57cec5SDimitry Andric        __erase_to_end(__base::begin() + __n);
18140b57cec5SDimitry Andric}
18150b57cec5SDimitry Andric
18160b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
18170b57cec5SDimitry Andricvoid
18180b57cec5SDimitry Andricdeque<_Tp, _Allocator>::resize(size_type __n, const value_type& __v)
18190b57cec5SDimitry Andric{
18200b57cec5SDimitry Andric    if (__n > __base::size())
18210b57cec5SDimitry Andric        __append(__n - __base::size(), __v);
18220b57cec5SDimitry Andric    else if (__n < __base::size())
18230b57cec5SDimitry Andric        __erase_to_end(__base::begin() + __n);
18240b57cec5SDimitry Andric}
18250b57cec5SDimitry Andric
18260b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
18270b57cec5SDimitry Andricvoid
18280b57cec5SDimitry Andricdeque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
18290b57cec5SDimitry Andric{
18300b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
18310b57cec5SDimitry Andric    if (empty())
18320b57cec5SDimitry Andric    {
18330b57cec5SDimitry Andric        while (__base::__map_.size() > 0)
18340b57cec5SDimitry Andric        {
18350b57cec5SDimitry Andric            __alloc_traits::deallocate(__a, __base::__map_.back(), __base::__block_size);
18360b57cec5SDimitry Andric            __base::__map_.pop_back();
18370b57cec5SDimitry Andric        }
18380b57cec5SDimitry Andric        __base::__start_ = 0;
18390b57cec5SDimitry Andric    }
18400b57cec5SDimitry Andric    else
18410b57cec5SDimitry Andric    {
1842e40139ffSDimitry Andric      __maybe_remove_front_spare(/*__keep_one=*/false);
1843e40139ffSDimitry Andric      __maybe_remove_back_spare(/*__keep_one=*/false);
18440b57cec5SDimitry Andric    }
18450b57cec5SDimitry Andric    __base::__map_.shrink_to_fit();
18460b57cec5SDimitry Andric}
18470b57cec5SDimitry Andric
18480b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
18490b57cec5SDimitry Andricinline
18500b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::reference
18510b57cec5SDimitry Andricdeque<_Tp, _Allocator>::operator[](size_type __i) _NOEXCEPT
18520b57cec5SDimitry Andric{
18530b57cec5SDimitry Andric    size_type __p = __base::__start_ + __i;
18540b57cec5SDimitry Andric    return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
18550b57cec5SDimitry Andric}
18560b57cec5SDimitry Andric
18570b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
18580b57cec5SDimitry Andricinline
18590b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::const_reference
18600b57cec5SDimitry Andricdeque<_Tp, _Allocator>::operator[](size_type __i) const _NOEXCEPT
18610b57cec5SDimitry Andric{
18620b57cec5SDimitry Andric    size_type __p = __base::__start_ + __i;
18630b57cec5SDimitry Andric    return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
18640b57cec5SDimitry Andric}
18650b57cec5SDimitry Andric
18660b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
18670b57cec5SDimitry Andricinline
18680b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::reference
18690b57cec5SDimitry Andricdeque<_Tp, _Allocator>::at(size_type __i)
18700b57cec5SDimitry Andric{
18710b57cec5SDimitry Andric    if (__i >= __base::size())
18720b57cec5SDimitry Andric        __base::__throw_out_of_range();
18730b57cec5SDimitry Andric    size_type __p = __base::__start_ + __i;
18740b57cec5SDimitry Andric    return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
18750b57cec5SDimitry Andric}
18760b57cec5SDimitry Andric
18770b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
18780b57cec5SDimitry Andricinline
18790b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::const_reference
18800b57cec5SDimitry Andricdeque<_Tp, _Allocator>::at(size_type __i) const
18810b57cec5SDimitry Andric{
18820b57cec5SDimitry Andric    if (__i >= __base::size())
18830b57cec5SDimitry Andric        __base::__throw_out_of_range();
18840b57cec5SDimitry Andric    size_type __p = __base::__start_ + __i;
18850b57cec5SDimitry Andric    return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
18860b57cec5SDimitry Andric}
18870b57cec5SDimitry Andric
18880b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
18890b57cec5SDimitry Andricinline
18900b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::reference
18910b57cec5SDimitry Andricdeque<_Tp, _Allocator>::front() _NOEXCEPT
18920b57cec5SDimitry Andric{
18930b57cec5SDimitry Andric    return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
18940b57cec5SDimitry Andric                                      + __base::__start_ % __base::__block_size);
18950b57cec5SDimitry Andric}
18960b57cec5SDimitry Andric
18970b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
18980b57cec5SDimitry Andricinline
18990b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::const_reference
19000b57cec5SDimitry Andricdeque<_Tp, _Allocator>::front() const _NOEXCEPT
19010b57cec5SDimitry Andric{
19020b57cec5SDimitry Andric    return *(*(__base::__map_.begin() + __base::__start_ / __base::__block_size)
19030b57cec5SDimitry Andric                                      + __base::__start_ % __base::__block_size);
19040b57cec5SDimitry Andric}
19050b57cec5SDimitry Andric
19060b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
19070b57cec5SDimitry Andricinline
19080b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::reference
19090b57cec5SDimitry Andricdeque<_Tp, _Allocator>::back() _NOEXCEPT
19100b57cec5SDimitry Andric{
19110b57cec5SDimitry Andric    size_type __p = __base::size() + __base::__start_ - 1;
19120b57cec5SDimitry Andric    return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
19130b57cec5SDimitry Andric}
19140b57cec5SDimitry Andric
19150b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
19160b57cec5SDimitry Andricinline
19170b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::const_reference
19180b57cec5SDimitry Andricdeque<_Tp, _Allocator>::back() const _NOEXCEPT
19190b57cec5SDimitry Andric{
19200b57cec5SDimitry Andric    size_type __p = __base::size() + __base::__start_ - 1;
19210b57cec5SDimitry Andric    return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
19220b57cec5SDimitry Andric}
19230b57cec5SDimitry Andric
19240b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
19250b57cec5SDimitry Andricvoid
19260b57cec5SDimitry Andricdeque<_Tp, _Allocator>::push_back(const value_type& __v)
19270b57cec5SDimitry Andric{
19280b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
19290b57cec5SDimitry Andric    if (__back_spare() == 0)
19300b57cec5SDimitry Andric        __add_back_capacity();
19310b57cec5SDimitry Andric    // __back_spare() >= 1
19320b57cec5SDimitry Andric    __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
19330b57cec5SDimitry Andric    ++__base::size();
19340b57cec5SDimitry Andric}
19350b57cec5SDimitry Andric
19360b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
19370b57cec5SDimitry Andricvoid
19380b57cec5SDimitry Andricdeque<_Tp, _Allocator>::push_front(const value_type& __v)
19390b57cec5SDimitry Andric{
19400b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
19410b57cec5SDimitry Andric    if (__front_spare() == 0)
19420b57cec5SDimitry Andric        __add_front_capacity();
19430b57cec5SDimitry Andric    // __front_spare() >= 1
19440b57cec5SDimitry Andric    __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
19450b57cec5SDimitry Andric    --__base::__start_;
19460b57cec5SDimitry Andric    ++__base::size();
19470b57cec5SDimitry Andric}
19480b57cec5SDimitry Andric
19490b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
19500b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
19510b57cec5SDimitry Andricvoid
19520b57cec5SDimitry Andricdeque<_Tp, _Allocator>::push_back(value_type&& __v)
19530b57cec5SDimitry Andric{
19540b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
19550b57cec5SDimitry Andric    if (__back_spare() == 0)
19560b57cec5SDimitry Andric        __add_back_capacity();
19570b57cec5SDimitry Andric    // __back_spare() >= 1
19580b57cec5SDimitry Andric    __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
19590b57cec5SDimitry Andric    ++__base::size();
19600b57cec5SDimitry Andric}
19610b57cec5SDimitry Andric
19620b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
19630b57cec5SDimitry Andrictemplate <class... _Args>
19640b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
19650b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::reference
19660b57cec5SDimitry Andric#else
19670b57cec5SDimitry Andricvoid
19680b57cec5SDimitry Andric#endif
19690b57cec5SDimitry Andricdeque<_Tp, _Allocator>::emplace_back(_Args&&... __args)
19700b57cec5SDimitry Andric{
19710b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
19720b57cec5SDimitry Andric    if (__back_spare() == 0)
19730b57cec5SDimitry Andric        __add_back_capacity();
19740b57cec5SDimitry Andric    // __back_spare() >= 1
19750b57cec5SDimitry Andric    __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()),
19760b57cec5SDimitry Andric                              _VSTD::forward<_Args>(__args)...);
19770b57cec5SDimitry Andric    ++__base::size();
19780b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
19790b57cec5SDimitry Andric    return *--__base::end();
19800b57cec5SDimitry Andric#endif
19810b57cec5SDimitry Andric}
19820b57cec5SDimitry Andric
19830b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
19840b57cec5SDimitry Andricvoid
19850b57cec5SDimitry Andricdeque<_Tp, _Allocator>::push_front(value_type&& __v)
19860b57cec5SDimitry Andric{
19870b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
19880b57cec5SDimitry Andric    if (__front_spare() == 0)
19890b57cec5SDimitry Andric        __add_front_capacity();
19900b57cec5SDimitry Andric    // __front_spare() >= 1
19910b57cec5SDimitry Andric    __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
19920b57cec5SDimitry Andric    --__base::__start_;
19930b57cec5SDimitry Andric    ++__base::size();
19940b57cec5SDimitry Andric}
19950b57cec5SDimitry Andric
19960b57cec5SDimitry Andric
19970b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
19980b57cec5SDimitry Andrictemplate <class... _Args>
19990b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
20000b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::reference
20010b57cec5SDimitry Andric#else
20020b57cec5SDimitry Andricvoid
20030b57cec5SDimitry Andric#endif
20040b57cec5SDimitry Andricdeque<_Tp, _Allocator>::emplace_front(_Args&&... __args)
20050b57cec5SDimitry Andric{
20060b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
20070b57cec5SDimitry Andric    if (__front_spare() == 0)
20080b57cec5SDimitry Andric        __add_front_capacity();
20090b57cec5SDimitry Andric    // __front_spare() >= 1
20100b57cec5SDimitry Andric    __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
20110b57cec5SDimitry Andric    --__base::__start_;
20120b57cec5SDimitry Andric    ++__base::size();
20130b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
20140b57cec5SDimitry Andric    return *__base::begin();
20150b57cec5SDimitry Andric#endif
20160b57cec5SDimitry Andric}
20170b57cec5SDimitry Andric
20180b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
20190b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::iterator
20200b57cec5SDimitry Andricdeque<_Tp, _Allocator>::insert(const_iterator __p, value_type&& __v)
20210b57cec5SDimitry Andric{
20220b57cec5SDimitry Andric    size_type __pos = __p - __base::begin();
20230b57cec5SDimitry Andric    size_type __to_end = __base::size() - __pos;
20240b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
20250b57cec5SDimitry Andric    if (__pos < __to_end)
20260b57cec5SDimitry Andric    {   // insert by shifting things backward
20270b57cec5SDimitry Andric        if (__front_spare() == 0)
20280b57cec5SDimitry Andric            __add_front_capacity();
20290b57cec5SDimitry Andric        // __front_spare() >= 1
20300b57cec5SDimitry Andric        if (__pos == 0)
20310b57cec5SDimitry Andric        {
20320b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::move(__v));
20330b57cec5SDimitry Andric            --__base::__start_;
20340b57cec5SDimitry Andric            ++__base::size();
20350b57cec5SDimitry Andric        }
20360b57cec5SDimitry Andric        else
20370b57cec5SDimitry Andric        {
20380b57cec5SDimitry Andric            iterator __b = __base::begin();
20390b57cec5SDimitry Andric            iterator __bm1 = _VSTD::prev(__b);
20400b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
20410b57cec5SDimitry Andric            --__base::__start_;
20420b57cec5SDimitry Andric            ++__base::size();
20430b57cec5SDimitry Andric            if (__pos > 1)
20440b57cec5SDimitry Andric                __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
20450b57cec5SDimitry Andric            *__b = _VSTD::move(__v);
20460b57cec5SDimitry Andric        }
20470b57cec5SDimitry Andric    }
20480b57cec5SDimitry Andric    else
20490b57cec5SDimitry Andric    {   // insert by shifting things forward
20500b57cec5SDimitry Andric        if (__back_spare() == 0)
20510b57cec5SDimitry Andric            __add_back_capacity();
20520b57cec5SDimitry Andric        // __back_capacity >= 1
20530b57cec5SDimitry Andric        size_type __de = __base::size() - __pos;
20540b57cec5SDimitry Andric        if (__de == 0)
20550b57cec5SDimitry Andric        {
20560b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::move(__v));
20570b57cec5SDimitry Andric            ++__base::size();
20580b57cec5SDimitry Andric        }
20590b57cec5SDimitry Andric        else
20600b57cec5SDimitry Andric        {
20610b57cec5SDimitry Andric            iterator __e = __base::end();
20620b57cec5SDimitry Andric            iterator __em1 = _VSTD::prev(__e);
20630b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
20640b57cec5SDimitry Andric            ++__base::size();
20650b57cec5SDimitry Andric            if (__de > 1)
20660b57cec5SDimitry Andric                __e = _VSTD::move_backward(__e - __de, __em1, __e);
20670b57cec5SDimitry Andric            *--__e = _VSTD::move(__v);
20680b57cec5SDimitry Andric        }
20690b57cec5SDimitry Andric    }
20700b57cec5SDimitry Andric    return __base::begin() + __pos;
20710b57cec5SDimitry Andric}
20720b57cec5SDimitry Andric
20730b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
20740b57cec5SDimitry Andrictemplate <class... _Args>
20750b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::iterator
20760b57cec5SDimitry Andricdeque<_Tp, _Allocator>::emplace(const_iterator __p, _Args&&... __args)
20770b57cec5SDimitry Andric{
20780b57cec5SDimitry Andric    size_type __pos = __p - __base::begin();
20790b57cec5SDimitry Andric    size_type __to_end = __base::size() - __pos;
20800b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
20810b57cec5SDimitry Andric    if (__pos < __to_end)
20820b57cec5SDimitry Andric    {   // insert by shifting things backward
20830b57cec5SDimitry Andric        if (__front_spare() == 0)
20840b57cec5SDimitry Andric            __add_front_capacity();
20850b57cec5SDimitry Andric        // __front_spare() >= 1
20860b57cec5SDimitry Andric        if (__pos == 0)
20870b57cec5SDimitry Andric        {
20880b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), _VSTD::forward<_Args>(__args)...);
20890b57cec5SDimitry Andric            --__base::__start_;
20900b57cec5SDimitry Andric            ++__base::size();
20910b57cec5SDimitry Andric        }
20920b57cec5SDimitry Andric        else
20930b57cec5SDimitry Andric        {
20940b57cec5SDimitry Andric            __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
20950b57cec5SDimitry Andric            iterator __b = __base::begin();
20960b57cec5SDimitry Andric            iterator __bm1 = _VSTD::prev(__b);
20970b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
20980b57cec5SDimitry Andric            --__base::__start_;
20990b57cec5SDimitry Andric            ++__base::size();
21000b57cec5SDimitry Andric            if (__pos > 1)
21010b57cec5SDimitry Andric                __b = _VSTD::move(_VSTD::next(__b), __b + __pos, __b);
21020b57cec5SDimitry Andric            *__b = _VSTD::move(__tmp.get());
21030b57cec5SDimitry Andric        }
21040b57cec5SDimitry Andric    }
21050b57cec5SDimitry Andric    else
21060b57cec5SDimitry Andric    {   // insert by shifting things forward
21070b57cec5SDimitry Andric        if (__back_spare() == 0)
21080b57cec5SDimitry Andric            __add_back_capacity();
21090b57cec5SDimitry Andric        // __back_capacity >= 1
21100b57cec5SDimitry Andric        size_type __de = __base::size() - __pos;
21110b57cec5SDimitry Andric        if (__de == 0)
21120b57cec5SDimitry Andric        {
21130b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), _VSTD::forward<_Args>(__args)...);
21140b57cec5SDimitry Andric            ++__base::size();
21150b57cec5SDimitry Andric        }
21160b57cec5SDimitry Andric        else
21170b57cec5SDimitry Andric        {
21180b57cec5SDimitry Andric            __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...);
21190b57cec5SDimitry Andric            iterator __e = __base::end();
21200b57cec5SDimitry Andric            iterator __em1 = _VSTD::prev(__e);
21210b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
21220b57cec5SDimitry Andric            ++__base::size();
21230b57cec5SDimitry Andric            if (__de > 1)
21240b57cec5SDimitry Andric                __e = _VSTD::move_backward(__e - __de, __em1, __e);
21250b57cec5SDimitry Andric            *--__e = _VSTD::move(__tmp.get());
21260b57cec5SDimitry Andric        }
21270b57cec5SDimitry Andric    }
21280b57cec5SDimitry Andric    return __base::begin() + __pos;
21290b57cec5SDimitry Andric}
21300b57cec5SDimitry Andric
21310b57cec5SDimitry Andric#endif  // _LIBCPP_CXX03_LANG
21320b57cec5SDimitry Andric
21330b57cec5SDimitry Andric
21340b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
21350b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::iterator
21360b57cec5SDimitry Andricdeque<_Tp, _Allocator>::insert(const_iterator __p, const value_type& __v)
21370b57cec5SDimitry Andric{
21380b57cec5SDimitry Andric    size_type __pos = __p - __base::begin();
21390b57cec5SDimitry Andric    size_type __to_end = __base::size() - __pos;
21400b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
21410b57cec5SDimitry Andric    if (__pos < __to_end)
21420b57cec5SDimitry Andric    {   // insert by shifting things backward
21430b57cec5SDimitry Andric        if (__front_spare() == 0)
21440b57cec5SDimitry Andric            __add_front_capacity();
21450b57cec5SDimitry Andric        // __front_spare() >= 1
21460b57cec5SDimitry Andric        if (__pos == 0)
21470b57cec5SDimitry Andric        {
21480b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*--__base::begin()), __v);
21490b57cec5SDimitry Andric            --__base::__start_;
21500b57cec5SDimitry Andric            ++__base::size();
21510b57cec5SDimitry Andric        }
21520b57cec5SDimitry Andric        else
21530b57cec5SDimitry Andric        {
21540b57cec5SDimitry Andric            const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
21550b57cec5SDimitry Andric            iterator __b = __base::begin();
21560b57cec5SDimitry Andric            iterator __bm1 = _VSTD::prev(__b);
21570b57cec5SDimitry Andric            if (__vt == pointer_traits<const_pointer>::pointer_to(*__b))
21580b57cec5SDimitry Andric                __vt = pointer_traits<const_pointer>::pointer_to(*__bm1);
21590b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*__bm1), _VSTD::move(*__b));
21600b57cec5SDimitry Andric            --__base::__start_;
21610b57cec5SDimitry Andric            ++__base::size();
21620b57cec5SDimitry Andric            if (__pos > 1)
21630b57cec5SDimitry Andric                __b = __move_and_check(_VSTD::next(__b), __b + __pos, __b, __vt);
21640b57cec5SDimitry Andric            *__b = *__vt;
21650b57cec5SDimitry Andric        }
21660b57cec5SDimitry Andric    }
21670b57cec5SDimitry Andric    else
21680b57cec5SDimitry Andric    {   // insert by shifting things forward
21690b57cec5SDimitry Andric        if (__back_spare() == 0)
21700b57cec5SDimitry Andric            __add_back_capacity();
21710b57cec5SDimitry Andric        // __back_capacity >= 1
21720b57cec5SDimitry Andric        size_type __de = __base::size() - __pos;
21730b57cec5SDimitry Andric        if (__de == 0)
21740b57cec5SDimitry Andric        {
21750b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*__base::end()), __v);
21760b57cec5SDimitry Andric            ++__base::size();
21770b57cec5SDimitry Andric        }
21780b57cec5SDimitry Andric        else
21790b57cec5SDimitry Andric        {
21800b57cec5SDimitry Andric            const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
21810b57cec5SDimitry Andric            iterator __e = __base::end();
21820b57cec5SDimitry Andric            iterator __em1 = _VSTD::prev(__e);
21830b57cec5SDimitry Andric            if (__vt == pointer_traits<const_pointer>::pointer_to(*__em1))
21840b57cec5SDimitry Andric                __vt = pointer_traits<const_pointer>::pointer_to(*__e);
21850b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*__e), _VSTD::move(*__em1));
21860b57cec5SDimitry Andric            ++__base::size();
21870b57cec5SDimitry Andric            if (__de > 1)
21880b57cec5SDimitry Andric                __e = __move_backward_and_check(__e - __de, __em1, __e, __vt);
21890b57cec5SDimitry Andric            *--__e = *__vt;
21900b57cec5SDimitry Andric        }
21910b57cec5SDimitry Andric    }
21920b57cec5SDimitry Andric    return __base::begin() + __pos;
21930b57cec5SDimitry Andric}
21940b57cec5SDimitry Andric
21950b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
21960b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::iterator
21970b57cec5SDimitry Andricdeque<_Tp, _Allocator>::insert(const_iterator __p, size_type __n, const value_type& __v)
21980b57cec5SDimitry Andric{
21990b57cec5SDimitry Andric    size_type __pos = __p - __base::begin();
22000b57cec5SDimitry Andric    size_type __to_end = __base::size() - __pos;
22010b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
22020b57cec5SDimitry Andric    if (__pos < __to_end)
22030b57cec5SDimitry Andric    {   // insert by shifting things backward
22040b57cec5SDimitry Andric        if (__n > __front_spare())
22050b57cec5SDimitry Andric            __add_front_capacity(__n - __front_spare());
22060b57cec5SDimitry Andric        // __n <= __front_spare()
22070b57cec5SDimitry Andric        iterator __old_begin = __base::begin();
22080b57cec5SDimitry Andric        iterator __i = __old_begin;
22090b57cec5SDimitry Andric        if (__n > __pos)
22100b57cec5SDimitry Andric        {
22110b57cec5SDimitry Andric            for (size_type __m = __n - __pos; __m; --__m, --__base::__start_, ++__base::size())
22120b57cec5SDimitry Andric                __alloc_traits::construct(__a, _VSTD::addressof(*--__i), __v);
22130b57cec5SDimitry Andric            __n = __pos;
22140b57cec5SDimitry Andric        }
22150b57cec5SDimitry Andric        if (__n > 0)
22160b57cec5SDimitry Andric        {
22170b57cec5SDimitry Andric            const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
22180b57cec5SDimitry Andric            iterator __obn = __old_begin + __n;
22190b57cec5SDimitry Andric            __move_construct_backward_and_check(__old_begin, __obn, __i, __vt);
22200b57cec5SDimitry Andric            if (__n < __pos)
22210b57cec5SDimitry Andric                __old_begin = __move_and_check(__obn, __old_begin + __pos, __old_begin, __vt);
22220b57cec5SDimitry Andric            _VSTD::fill_n(__old_begin, __n, *__vt);
22230b57cec5SDimitry Andric        }
22240b57cec5SDimitry Andric    }
22250b57cec5SDimitry Andric    else
22260b57cec5SDimitry Andric    {   // insert by shifting things forward
22270b57cec5SDimitry Andric        size_type __back_capacity = __back_spare();
22280b57cec5SDimitry Andric        if (__n > __back_capacity)
22290b57cec5SDimitry Andric            __add_back_capacity(__n - __back_capacity);
22300b57cec5SDimitry Andric        // __n <= __back_capacity
22310b57cec5SDimitry Andric        iterator __old_end = __base::end();
22320b57cec5SDimitry Andric        iterator __i = __old_end;
22330b57cec5SDimitry Andric        size_type __de = __base::size() - __pos;
22340b57cec5SDimitry Andric        if (__n > __de)
22350b57cec5SDimitry Andric        {
22360b57cec5SDimitry Andric            for (size_type __m = __n - __de; __m; --__m, ++__i, ++__base::size())
22370b57cec5SDimitry Andric                __alloc_traits::construct(__a, _VSTD::addressof(*__i), __v);
22380b57cec5SDimitry Andric            __n = __de;
22390b57cec5SDimitry Andric        }
22400b57cec5SDimitry Andric        if (__n > 0)
22410b57cec5SDimitry Andric        {
22420b57cec5SDimitry Andric            const_pointer __vt = pointer_traits<const_pointer>::pointer_to(__v);
22430b57cec5SDimitry Andric            iterator __oen = __old_end - __n;
22440b57cec5SDimitry Andric            __move_construct_and_check(__oen, __old_end, __i, __vt);
22450b57cec5SDimitry Andric            if (__n < __de)
22460b57cec5SDimitry Andric                __old_end = __move_backward_and_check(__old_end - __de, __oen, __old_end, __vt);
22470b57cec5SDimitry Andric            _VSTD::fill_n(__old_end - __n, __n, *__vt);
22480b57cec5SDimitry Andric        }
22490b57cec5SDimitry Andric    }
22500b57cec5SDimitry Andric    return __base::begin() + __pos;
22510b57cec5SDimitry Andric}
22520b57cec5SDimitry Andric
22530b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
22540b57cec5SDimitry Andrictemplate <class _InputIter>
22550b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::iterator
22560b57cec5SDimitry Andricdeque<_Tp, _Allocator>::insert(const_iterator __p, _InputIter __f, _InputIter __l,
2257480093f4SDimitry Andric                               typename enable_if<__is_cpp17_input_iterator<_InputIter>::value
2258480093f4SDimitry Andric                                               &&!__is_cpp17_forward_iterator<_InputIter>::value>::type*)
22590b57cec5SDimitry Andric{
22600b57cec5SDimitry Andric    __split_buffer<value_type, allocator_type&> __buf(__base::__alloc());
22610b57cec5SDimitry Andric    __buf.__construct_at_end(__f, __l);
22620b57cec5SDimitry Andric    typedef typename __split_buffer<value_type, allocator_type&>::iterator __bi;
22630b57cec5SDimitry Andric    return insert(__p, move_iterator<__bi>(__buf.begin()), move_iterator<__bi>(__buf.end()));
22640b57cec5SDimitry Andric}
22650b57cec5SDimitry Andric
22660b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
22670b57cec5SDimitry Andrictemplate <class _ForwardIterator>
22680b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::iterator
22690b57cec5SDimitry Andricdeque<_Tp, _Allocator>::insert(const_iterator __p, _ForwardIterator __f, _ForwardIterator __l,
2270480093f4SDimitry Andric                               typename enable_if<__is_cpp17_forward_iterator<_ForwardIterator>::value
2271480093f4SDimitry Andric                                               &&!__is_cpp17_bidirectional_iterator<_ForwardIterator>::value>::type*)
22720b57cec5SDimitry Andric{
22730b57cec5SDimitry Andric    size_type __n = _VSTD::distance(__f, __l);
22740b57cec5SDimitry Andric    __split_buffer<value_type, allocator_type&> __buf(__n, 0, __base::__alloc());
22750b57cec5SDimitry Andric    __buf.__construct_at_end(__f, __l);
22760b57cec5SDimitry Andric    typedef typename __split_buffer<value_type, allocator_type&>::iterator __fwd;
22770b57cec5SDimitry Andric    return insert(__p, move_iterator<__fwd>(__buf.begin()), move_iterator<__fwd>(__buf.end()));
22780b57cec5SDimitry Andric}
22790b57cec5SDimitry Andric
22800b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
22810b57cec5SDimitry Andrictemplate <class _BiIter>
22820b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::iterator
22830b57cec5SDimitry Andricdeque<_Tp, _Allocator>::insert(const_iterator __p, _BiIter __f, _BiIter __l,
2284480093f4SDimitry Andric                               typename enable_if<__is_cpp17_bidirectional_iterator<_BiIter>::value>::type*)
22850b57cec5SDimitry Andric{
22860b57cec5SDimitry Andric    size_type __n = _VSTD::distance(__f, __l);
22870b57cec5SDimitry Andric    size_type __pos = __p - __base::begin();
22880b57cec5SDimitry Andric    size_type __to_end = __base::size() - __pos;
22890b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
22900b57cec5SDimitry Andric    if (__pos < __to_end)
22910b57cec5SDimitry Andric    {   // insert by shifting things backward
22920b57cec5SDimitry Andric        if (__n > __front_spare())
22930b57cec5SDimitry Andric            __add_front_capacity(__n - __front_spare());
22940b57cec5SDimitry Andric        // __n <= __front_spare()
22950b57cec5SDimitry Andric        iterator __old_begin = __base::begin();
22960b57cec5SDimitry Andric        iterator __i = __old_begin;
22970b57cec5SDimitry Andric        _BiIter __m = __f;
22980b57cec5SDimitry Andric        if (__n > __pos)
22990b57cec5SDimitry Andric        {
23000b57cec5SDimitry Andric            __m = __pos < __n / 2 ? _VSTD::prev(__l, __pos) : _VSTD::next(__f, __n - __pos);
23010b57cec5SDimitry Andric            for (_BiIter __j = __m; __j != __f; --__base::__start_, ++__base::size())
23020b57cec5SDimitry Andric                __alloc_traits::construct(__a, _VSTD::addressof(*--__i), *--__j);
23030b57cec5SDimitry Andric            __n = __pos;
23040b57cec5SDimitry Andric        }
23050b57cec5SDimitry Andric        if (__n > 0)
23060b57cec5SDimitry Andric        {
23070b57cec5SDimitry Andric            iterator __obn = __old_begin + __n;
23080b57cec5SDimitry Andric            for (iterator __j = __obn; __j != __old_begin;)
23090b57cec5SDimitry Andric            {
23100b57cec5SDimitry Andric                __alloc_traits::construct(__a, _VSTD::addressof(*--__i), _VSTD::move(*--__j));
23110b57cec5SDimitry Andric                --__base::__start_;
23120b57cec5SDimitry Andric                ++__base::size();
23130b57cec5SDimitry Andric            }
23140b57cec5SDimitry Andric            if (__n < __pos)
23150b57cec5SDimitry Andric                __old_begin = _VSTD::move(__obn, __old_begin + __pos, __old_begin);
23160b57cec5SDimitry Andric            _VSTD::copy(__m, __l, __old_begin);
23170b57cec5SDimitry Andric        }
23180b57cec5SDimitry Andric    }
23190b57cec5SDimitry Andric    else
23200b57cec5SDimitry Andric    {   // insert by shifting things forward
23210b57cec5SDimitry Andric        size_type __back_capacity = __back_spare();
23220b57cec5SDimitry Andric        if (__n > __back_capacity)
23230b57cec5SDimitry Andric            __add_back_capacity(__n - __back_capacity);
23240b57cec5SDimitry Andric        // __n <= __back_capacity
23250b57cec5SDimitry Andric        iterator __old_end = __base::end();
23260b57cec5SDimitry Andric        iterator __i = __old_end;
23270b57cec5SDimitry Andric        _BiIter __m = __l;
23280b57cec5SDimitry Andric        size_type __de = __base::size() - __pos;
23290b57cec5SDimitry Andric        if (__n > __de)
23300b57cec5SDimitry Andric        {
23310b57cec5SDimitry Andric            __m = __de < __n / 2 ? _VSTD::next(__f, __de) : _VSTD::prev(__l, __n - __de);
23320b57cec5SDimitry Andric            for (_BiIter __j = __m; __j != __l; ++__i, (void) ++__j, ++__base::size())
23330b57cec5SDimitry Andric                __alloc_traits::construct(__a, _VSTD::addressof(*__i), *__j);
23340b57cec5SDimitry Andric            __n = __de;
23350b57cec5SDimitry Andric        }
23360b57cec5SDimitry Andric        if (__n > 0)
23370b57cec5SDimitry Andric        {
23380b57cec5SDimitry Andric            iterator __oen = __old_end - __n;
23390b57cec5SDimitry Andric            for (iterator __j = __oen; __j != __old_end; ++__i, ++__j, ++__base::size())
23400b57cec5SDimitry Andric                __alloc_traits::construct(__a, _VSTD::addressof(*__i), _VSTD::move(*__j));
23410b57cec5SDimitry Andric            if (__n < __de)
23420b57cec5SDimitry Andric                __old_end = _VSTD::move_backward(__old_end - __de, __oen, __old_end);
23430b57cec5SDimitry Andric            _VSTD::copy_backward(__f, __m, __old_end);
23440b57cec5SDimitry Andric        }
23450b57cec5SDimitry Andric    }
23460b57cec5SDimitry Andric    return __base::begin() + __pos;
23470b57cec5SDimitry Andric}
23480b57cec5SDimitry Andric
23490b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
23500b57cec5SDimitry Andrictemplate <class _InpIter>
23510b57cec5SDimitry Andricvoid
23520b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__append(_InpIter __f, _InpIter __l,
2353480093f4SDimitry Andric                                 typename enable_if<__is_cpp17_input_iterator<_InpIter>::value &&
2354480093f4SDimitry Andric                                                   !__is_cpp17_forward_iterator<_InpIter>::value>::type*)
23550b57cec5SDimitry Andric{
23560b57cec5SDimitry Andric    for (; __f != __l; ++__f)
23570b57cec5SDimitry Andric#ifdef _LIBCPP_CXX03_LANG
23580b57cec5SDimitry Andric        push_back(*__f);
23590b57cec5SDimitry Andric#else
23600b57cec5SDimitry Andric        emplace_back(*__f);
23610b57cec5SDimitry Andric#endif
23620b57cec5SDimitry Andric}
23630b57cec5SDimitry Andric
23640b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
23650b57cec5SDimitry Andrictemplate <class _ForIter>
23660b57cec5SDimitry Andricvoid
23670b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__append(_ForIter __f, _ForIter __l,
2368480093f4SDimitry Andric                                 typename enable_if<__is_cpp17_forward_iterator<_ForIter>::value>::type*)
23690b57cec5SDimitry Andric{
23700b57cec5SDimitry Andric    size_type __n = _VSTD::distance(__f, __l);
23710b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
23720b57cec5SDimitry Andric    size_type __back_capacity = __back_spare();
23730b57cec5SDimitry Andric    if (__n > __back_capacity)
23740b57cec5SDimitry Andric        __add_back_capacity(__n - __back_capacity);
23750b57cec5SDimitry Andric    // __n <= __back_capacity
2376e40139ffSDimitry Andric    for (__deque_block_range __br : __deque_range(__base::end(), __base::end() + __n)) {
2377e40139ffSDimitry Andric      _ConstructTransaction __tx(this, __br);
2378e40139ffSDimitry Andric      for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void)++__f) {
2379480093f4SDimitry Andric        __alloc_traits::construct(__a, std::__to_address(__tx.__pos_), *__f);
2380e40139ffSDimitry Andric      }
2381e40139ffSDimitry Andric    }
23820b57cec5SDimitry Andric}
23830b57cec5SDimitry Andric
23840b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
23850b57cec5SDimitry Andricvoid
23860b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__append(size_type __n)
23870b57cec5SDimitry Andric{
23880b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
23890b57cec5SDimitry Andric    size_type __back_capacity = __back_spare();
23900b57cec5SDimitry Andric    if (__n > __back_capacity)
23910b57cec5SDimitry Andric        __add_back_capacity(__n - __back_capacity);
23920b57cec5SDimitry Andric    // __n <= __back_capacity
2393e40139ffSDimitry Andric    for (__deque_block_range __br : __deque_range(__base::end(), __base::end() + __n)) {
2394e40139ffSDimitry Andric      _ConstructTransaction __tx(this, __br);
2395e40139ffSDimitry Andric      for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
2396480093f4SDimitry Andric        __alloc_traits::construct(__a, std::__to_address(__tx.__pos_));
2397e40139ffSDimitry Andric      }
2398e40139ffSDimitry Andric    }
23990b57cec5SDimitry Andric}
24000b57cec5SDimitry Andric
24010b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
24020b57cec5SDimitry Andricvoid
24030b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__append(size_type __n, const value_type& __v)
24040b57cec5SDimitry Andric{
24050b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
24060b57cec5SDimitry Andric    size_type __back_capacity = __back_spare();
24070b57cec5SDimitry Andric    if (__n > __back_capacity)
24080b57cec5SDimitry Andric        __add_back_capacity(__n - __back_capacity);
24090b57cec5SDimitry Andric    // __n <= __back_capacity
2410e40139ffSDimitry Andric    for (__deque_block_range __br : __deque_range(__base::end(), __base::end() + __n)) {
2411e40139ffSDimitry Andric      _ConstructTransaction __tx(this, __br);
2412e40139ffSDimitry Andric      for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
2413480093f4SDimitry Andric        __alloc_traits::construct(__a, std::__to_address(__tx.__pos_), __v);
2414e40139ffSDimitry Andric      }
2415e40139ffSDimitry Andric    }
2416e40139ffSDimitry Andric
24170b57cec5SDimitry Andric}
24180b57cec5SDimitry Andric
24190b57cec5SDimitry Andric// Create front capacity for one block of elements.
24200b57cec5SDimitry Andric// Strong guarantee.  Either do it or don't touch anything.
24210b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
24220b57cec5SDimitry Andricvoid
24230b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__add_front_capacity()
24240b57cec5SDimitry Andric{
24250b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
24260b57cec5SDimitry Andric    if (__back_spare() >= __base::__block_size)
24270b57cec5SDimitry Andric    {
24280b57cec5SDimitry Andric        __base::__start_ += __base::__block_size;
24290b57cec5SDimitry Andric        pointer __pt = __base::__map_.back();
24300b57cec5SDimitry Andric        __base::__map_.pop_back();
24310b57cec5SDimitry Andric        __base::__map_.push_front(__pt);
24320b57cec5SDimitry Andric    }
24330b57cec5SDimitry Andric    // Else if __base::__map_.size() < __base::__map_.capacity() then we need to allocate 1 buffer
24340b57cec5SDimitry Andric    else if (__base::__map_.size() < __base::__map_.capacity())
24350b57cec5SDimitry Andric    {   // we can put the new buffer into the map, but don't shift things around
24360b57cec5SDimitry Andric        // until all buffers are allocated.  If we throw, we don't need to fix
24370b57cec5SDimitry Andric        // anything up (any added buffers are undetectible)
24380b57cec5SDimitry Andric        if (__base::__map_.__front_spare() > 0)
24390b57cec5SDimitry Andric            __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
24400b57cec5SDimitry Andric        else
24410b57cec5SDimitry Andric        {
24420b57cec5SDimitry Andric            __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
24430b57cec5SDimitry Andric            // Done allocating, reorder capacity
24440b57cec5SDimitry Andric            pointer __pt = __base::__map_.back();
24450b57cec5SDimitry Andric            __base::__map_.pop_back();
24460b57cec5SDimitry Andric            __base::__map_.push_front(__pt);
24470b57cec5SDimitry Andric        }
24480b57cec5SDimitry Andric        __base::__start_ = __base::__map_.size() == 1 ?
24490b57cec5SDimitry Andric                               __base::__block_size / 2 :
24500b57cec5SDimitry Andric                               __base::__start_ + __base::__block_size;
24510b57cec5SDimitry Andric    }
24520b57cec5SDimitry Andric    // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
24530b57cec5SDimitry Andric    else
24540b57cec5SDimitry Andric    {
24550b57cec5SDimitry Andric        __split_buffer<pointer, typename __base::__pointer_allocator&>
24560b57cec5SDimitry Andric            __buf(max<size_type>(2 * __base::__map_.capacity(), 1),
24570b57cec5SDimitry Andric                  0, __base::__map_.__alloc());
24580b57cec5SDimitry Andric
24590b57cec5SDimitry Andric        typedef __allocator_destructor<_Allocator> _Dp;
24600b57cec5SDimitry Andric        unique_ptr<pointer, _Dp> __hold(
24610b57cec5SDimitry Andric            __alloc_traits::allocate(__a, __base::__block_size),
24620b57cec5SDimitry Andric                _Dp(__a, __base::__block_size));
24630b57cec5SDimitry Andric        __buf.push_back(__hold.get());
24640b57cec5SDimitry Andric        __hold.release();
24650b57cec5SDimitry Andric
24660b57cec5SDimitry Andric        for (typename __base::__map_pointer __i = __base::__map_.begin();
24670b57cec5SDimitry Andric                __i != __base::__map_.end(); ++__i)
24680b57cec5SDimitry Andric            __buf.push_back(*__i);
24690b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__first_, __buf.__first_);
24700b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
24710b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__end_, __buf.__end_);
24720b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
24730b57cec5SDimitry Andric        __base::__start_ = __base::__map_.size() == 1 ?
24740b57cec5SDimitry Andric                               __base::__block_size / 2 :
24750b57cec5SDimitry Andric                               __base::__start_ + __base::__block_size;
24760b57cec5SDimitry Andric    }
24770b57cec5SDimitry Andric}
24780b57cec5SDimitry Andric
24790b57cec5SDimitry Andric// Create front capacity for __n elements.
24800b57cec5SDimitry Andric// Strong guarantee.  Either do it or don't touch anything.
24810b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
24820b57cec5SDimitry Andricvoid
24830b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__add_front_capacity(size_type __n)
24840b57cec5SDimitry Andric{
24850b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
24860b57cec5SDimitry Andric    size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
24870b57cec5SDimitry Andric    // Number of unused blocks at back:
24880b57cec5SDimitry Andric    size_type __back_capacity = __back_spare() / __base::__block_size;
24890b57cec5SDimitry Andric    __back_capacity = _VSTD::min(__back_capacity, __nb);  // don't take more than you need
24900b57cec5SDimitry Andric    __nb -= __back_capacity;  // number of blocks need to allocate
24910b57cec5SDimitry Andric    // If __nb == 0, then we have sufficient capacity.
24920b57cec5SDimitry Andric    if (__nb == 0)
24930b57cec5SDimitry Andric    {
24940b57cec5SDimitry Andric        __base::__start_ += __base::__block_size * __back_capacity;
24950b57cec5SDimitry Andric        for (; __back_capacity > 0; --__back_capacity)
24960b57cec5SDimitry Andric        {
24970b57cec5SDimitry Andric            pointer __pt = __base::__map_.back();
24980b57cec5SDimitry Andric            __base::__map_.pop_back();
24990b57cec5SDimitry Andric            __base::__map_.push_front(__pt);
25000b57cec5SDimitry Andric        }
25010b57cec5SDimitry Andric    }
25020b57cec5SDimitry Andric    // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
25030b57cec5SDimitry Andric    else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
25040b57cec5SDimitry Andric    {   // we can put the new buffers into the map, but don't shift things around
25050b57cec5SDimitry Andric        // until all buffers are allocated.  If we throw, we don't need to fix
25060b57cec5SDimitry Andric        // anything up (any added buffers are undetectible)
25070b57cec5SDimitry Andric        for (; __nb > 0; --__nb, __base::__start_ += __base::__block_size - (__base::__map_.size() == 1))
25080b57cec5SDimitry Andric        {
25090b57cec5SDimitry Andric            if (__base::__map_.__front_spare() == 0)
25100b57cec5SDimitry Andric                break;
25110b57cec5SDimitry Andric            __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
25120b57cec5SDimitry Andric        }
25130b57cec5SDimitry Andric        for (; __nb > 0; --__nb, ++__back_capacity)
25140b57cec5SDimitry Andric            __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
25150b57cec5SDimitry Andric        // Done allocating, reorder capacity
25160b57cec5SDimitry Andric        __base::__start_ += __back_capacity * __base::__block_size;
25170b57cec5SDimitry Andric        for (; __back_capacity > 0; --__back_capacity)
25180b57cec5SDimitry Andric        {
25190b57cec5SDimitry Andric            pointer __pt = __base::__map_.back();
25200b57cec5SDimitry Andric            __base::__map_.pop_back();
25210b57cec5SDimitry Andric            __base::__map_.push_front(__pt);
25220b57cec5SDimitry Andric        }
25230b57cec5SDimitry Andric    }
25240b57cec5SDimitry Andric    // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
25250b57cec5SDimitry Andric    else
25260b57cec5SDimitry Andric    {
25270b57cec5SDimitry Andric        size_type __ds = (__nb + __back_capacity) * __base::__block_size - __base::__map_.empty();
25280b57cec5SDimitry Andric        __split_buffer<pointer, typename __base::__pointer_allocator&>
25290b57cec5SDimitry Andric            __buf(max<size_type>(2* __base::__map_.capacity(),
25300b57cec5SDimitry Andric                                 __nb + __base::__map_.size()),
25310b57cec5SDimitry Andric                  0, __base::__map_.__alloc());
25320b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
25330b57cec5SDimitry Andric        try
25340b57cec5SDimitry Andric        {
25350b57cec5SDimitry Andric#endif  // _LIBCPP_NO_EXCEPTIONS
25360b57cec5SDimitry Andric            for (; __nb > 0; --__nb)
25370b57cec5SDimitry Andric                __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
25380b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
25390b57cec5SDimitry Andric        }
25400b57cec5SDimitry Andric        catch (...)
25410b57cec5SDimitry Andric        {
25420b57cec5SDimitry Andric            for (typename __base::__map_pointer __i = __buf.begin();
25430b57cec5SDimitry Andric                    __i != __buf.end(); ++__i)
25440b57cec5SDimitry Andric                __alloc_traits::deallocate(__a, *__i, __base::__block_size);
25450b57cec5SDimitry Andric            throw;
25460b57cec5SDimitry Andric        }
25470b57cec5SDimitry Andric#endif  // _LIBCPP_NO_EXCEPTIONS
25480b57cec5SDimitry Andric        for (; __back_capacity > 0; --__back_capacity)
25490b57cec5SDimitry Andric        {
25500b57cec5SDimitry Andric            __buf.push_back(__base::__map_.back());
25510b57cec5SDimitry Andric            __base::__map_.pop_back();
25520b57cec5SDimitry Andric        }
25530b57cec5SDimitry Andric        for (typename __base::__map_pointer __i = __base::__map_.begin();
25540b57cec5SDimitry Andric                __i != __base::__map_.end(); ++__i)
25550b57cec5SDimitry Andric            __buf.push_back(*__i);
25560b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__first_, __buf.__first_);
25570b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
25580b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__end_, __buf.__end_);
25590b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
25600b57cec5SDimitry Andric        __base::__start_ += __ds;
25610b57cec5SDimitry Andric    }
25620b57cec5SDimitry Andric}
25630b57cec5SDimitry Andric
25640b57cec5SDimitry Andric// Create back capacity for one block of elements.
25650b57cec5SDimitry Andric// Strong guarantee.  Either do it or don't touch anything.
25660b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
25670b57cec5SDimitry Andricvoid
25680b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__add_back_capacity()
25690b57cec5SDimitry Andric{
25700b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
25710b57cec5SDimitry Andric    if (__front_spare() >= __base::__block_size)
25720b57cec5SDimitry Andric    {
25730b57cec5SDimitry Andric        __base::__start_ -= __base::__block_size;
25740b57cec5SDimitry Andric        pointer __pt = __base::__map_.front();
25750b57cec5SDimitry Andric        __base::__map_.pop_front();
25760b57cec5SDimitry Andric        __base::__map_.push_back(__pt);
25770b57cec5SDimitry Andric    }
25780b57cec5SDimitry Andric    // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
25790b57cec5SDimitry Andric    else if (__base::__map_.size() < __base::__map_.capacity())
25800b57cec5SDimitry Andric    {   // we can put the new buffer into the map, but don't shift things around
25810b57cec5SDimitry Andric        // until it is allocated.  If we throw, we don't need to fix
25820b57cec5SDimitry Andric        // anything up (any added buffers are undetectible)
25830b57cec5SDimitry Andric        if (__base::__map_.__back_spare() != 0)
25840b57cec5SDimitry Andric            __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
25850b57cec5SDimitry Andric        else
25860b57cec5SDimitry Andric        {
25870b57cec5SDimitry Andric            __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
25880b57cec5SDimitry Andric            // Done allocating, reorder capacity
25890b57cec5SDimitry Andric            pointer __pt = __base::__map_.front();
25900b57cec5SDimitry Andric            __base::__map_.pop_front();
25910b57cec5SDimitry Andric            __base::__map_.push_back(__pt);
25920b57cec5SDimitry Andric        }
25930b57cec5SDimitry Andric    }
25940b57cec5SDimitry Andric    // Else need to allocate 1 buffer, *and* we need to reallocate __map_.
25950b57cec5SDimitry Andric    else
25960b57cec5SDimitry Andric    {
25970b57cec5SDimitry Andric        __split_buffer<pointer, typename __base::__pointer_allocator&>
25980b57cec5SDimitry Andric            __buf(max<size_type>(2* __base::__map_.capacity(), 1),
25990b57cec5SDimitry Andric                  __base::__map_.size(),
26000b57cec5SDimitry Andric                  __base::__map_.__alloc());
26010b57cec5SDimitry Andric
26020b57cec5SDimitry Andric        typedef __allocator_destructor<_Allocator> _Dp;
26030b57cec5SDimitry Andric        unique_ptr<pointer, _Dp> __hold(
26040b57cec5SDimitry Andric            __alloc_traits::allocate(__a, __base::__block_size),
26050b57cec5SDimitry Andric                _Dp(__a, __base::__block_size));
26060b57cec5SDimitry Andric        __buf.push_back(__hold.get());
26070b57cec5SDimitry Andric        __hold.release();
26080b57cec5SDimitry Andric
26090b57cec5SDimitry Andric        for (typename __base::__map_pointer __i = __base::__map_.end();
26100b57cec5SDimitry Andric                __i != __base::__map_.begin();)
26110b57cec5SDimitry Andric            __buf.push_front(*--__i);
26120b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__first_, __buf.__first_);
26130b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
26140b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__end_, __buf.__end_);
26150b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
26160b57cec5SDimitry Andric    }
26170b57cec5SDimitry Andric}
26180b57cec5SDimitry Andric
26190b57cec5SDimitry Andric// Create back capacity for __n elements.
26200b57cec5SDimitry Andric// Strong guarantee.  Either do it or don't touch anything.
26210b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
26220b57cec5SDimitry Andricvoid
26230b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__add_back_capacity(size_type __n)
26240b57cec5SDimitry Andric{
26250b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
26260b57cec5SDimitry Andric    size_type __nb = __recommend_blocks(__n + __base::__map_.empty());
26270b57cec5SDimitry Andric    // Number of unused blocks at front:
26280b57cec5SDimitry Andric    size_type __front_capacity = __front_spare() / __base::__block_size;
26290b57cec5SDimitry Andric    __front_capacity = _VSTD::min(__front_capacity, __nb);  // don't take more than you need
26300b57cec5SDimitry Andric    __nb -= __front_capacity;  // number of blocks need to allocate
26310b57cec5SDimitry Andric    // If __nb == 0, then we have sufficient capacity.
26320b57cec5SDimitry Andric    if (__nb == 0)
26330b57cec5SDimitry Andric    {
26340b57cec5SDimitry Andric        __base::__start_ -= __base::__block_size * __front_capacity;
26350b57cec5SDimitry Andric        for (; __front_capacity > 0; --__front_capacity)
26360b57cec5SDimitry Andric        {
26370b57cec5SDimitry Andric            pointer __pt = __base::__map_.front();
26380b57cec5SDimitry Andric            __base::__map_.pop_front();
26390b57cec5SDimitry Andric            __base::__map_.push_back(__pt);
26400b57cec5SDimitry Andric        }
26410b57cec5SDimitry Andric    }
26420b57cec5SDimitry Andric    // Else if __nb <= __map_.capacity() - __map_.size() then we need to allocate __nb buffers
26430b57cec5SDimitry Andric    else if (__nb <= __base::__map_.capacity() - __base::__map_.size())
26440b57cec5SDimitry Andric    {   // we can put the new buffers into the map, but don't shift things around
26450b57cec5SDimitry Andric        // until all buffers are allocated.  If we throw, we don't need to fix
26460b57cec5SDimitry Andric        // anything up (any added buffers are undetectible)
26470b57cec5SDimitry Andric        for (; __nb > 0; --__nb)
26480b57cec5SDimitry Andric        {
26490b57cec5SDimitry Andric            if (__base::__map_.__back_spare() == 0)
26500b57cec5SDimitry Andric                break;
26510b57cec5SDimitry Andric            __base::__map_.push_back(__alloc_traits::allocate(__a, __base::__block_size));
26520b57cec5SDimitry Andric        }
26530b57cec5SDimitry Andric        for (; __nb > 0; --__nb, ++__front_capacity, __base::__start_ +=
26540b57cec5SDimitry Andric                                 __base::__block_size - (__base::__map_.size() == 1))
26550b57cec5SDimitry Andric            __base::__map_.push_front(__alloc_traits::allocate(__a, __base::__block_size));
26560b57cec5SDimitry Andric        // Done allocating, reorder capacity
26570b57cec5SDimitry Andric        __base::__start_ -= __base::__block_size * __front_capacity;
26580b57cec5SDimitry Andric        for (; __front_capacity > 0; --__front_capacity)
26590b57cec5SDimitry Andric        {
26600b57cec5SDimitry Andric            pointer __pt = __base::__map_.front();
26610b57cec5SDimitry Andric            __base::__map_.pop_front();
26620b57cec5SDimitry Andric            __base::__map_.push_back(__pt);
26630b57cec5SDimitry Andric        }
26640b57cec5SDimitry Andric    }
26650b57cec5SDimitry Andric    // Else need to allocate __nb buffers, *and* we need to reallocate __map_.
26660b57cec5SDimitry Andric    else
26670b57cec5SDimitry Andric    {
26680b57cec5SDimitry Andric        size_type __ds = __front_capacity * __base::__block_size;
26690b57cec5SDimitry Andric        __split_buffer<pointer, typename __base::__pointer_allocator&>
26700b57cec5SDimitry Andric            __buf(max<size_type>(2* __base::__map_.capacity(),
26710b57cec5SDimitry Andric                                 __nb + __base::__map_.size()),
26720b57cec5SDimitry Andric                  __base::__map_.size() - __front_capacity,
26730b57cec5SDimitry Andric                  __base::__map_.__alloc());
26740b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
26750b57cec5SDimitry Andric        try
26760b57cec5SDimitry Andric        {
26770b57cec5SDimitry Andric#endif  // _LIBCPP_NO_EXCEPTIONS
26780b57cec5SDimitry Andric            for (; __nb > 0; --__nb)
26790b57cec5SDimitry Andric                __buf.push_back(__alloc_traits::allocate(__a, __base::__block_size));
26800b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
26810b57cec5SDimitry Andric        }
26820b57cec5SDimitry Andric        catch (...)
26830b57cec5SDimitry Andric        {
26840b57cec5SDimitry Andric            for (typename __base::__map_pointer __i = __buf.begin();
26850b57cec5SDimitry Andric                    __i != __buf.end(); ++__i)
26860b57cec5SDimitry Andric                __alloc_traits::deallocate(__a, *__i, __base::__block_size);
26870b57cec5SDimitry Andric            throw;
26880b57cec5SDimitry Andric        }
26890b57cec5SDimitry Andric#endif  // _LIBCPP_NO_EXCEPTIONS
26900b57cec5SDimitry Andric        for (; __front_capacity > 0; --__front_capacity)
26910b57cec5SDimitry Andric        {
26920b57cec5SDimitry Andric            __buf.push_back(__base::__map_.front());
26930b57cec5SDimitry Andric            __base::__map_.pop_front();
26940b57cec5SDimitry Andric        }
26950b57cec5SDimitry Andric        for (typename __base::__map_pointer __i = __base::__map_.end();
26960b57cec5SDimitry Andric                __i != __base::__map_.begin();)
26970b57cec5SDimitry Andric            __buf.push_front(*--__i);
26980b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__first_, __buf.__first_);
26990b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__begin_, __buf.__begin_);
27000b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__end_, __buf.__end_);
27010b57cec5SDimitry Andric        _VSTD::swap(__base::__map_.__end_cap(), __buf.__end_cap());
27020b57cec5SDimitry Andric        __base::__start_ -= __ds;
27030b57cec5SDimitry Andric    }
27040b57cec5SDimitry Andric}
27050b57cec5SDimitry Andric
27060b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
27070b57cec5SDimitry Andricvoid
27080b57cec5SDimitry Andricdeque<_Tp, _Allocator>::pop_front()
27090b57cec5SDimitry Andric{
27100b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
2711480093f4SDimitry Andric    __alloc_traits::destroy(__a, __to_address(*(__base::__map_.begin() +
27120b57cec5SDimitry Andric                                                    __base::__start_ / __base::__block_size) +
27130b57cec5SDimitry Andric                                                    __base::__start_ % __base::__block_size));
27140b57cec5SDimitry Andric    --__base::size();
2715e40139ffSDimitry Andric    ++__base::__start_;
2716e40139ffSDimitry Andric    __maybe_remove_front_spare();
27170b57cec5SDimitry Andric}
27180b57cec5SDimitry Andric
27190b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
27200b57cec5SDimitry Andricvoid
27210b57cec5SDimitry Andricdeque<_Tp, _Allocator>::pop_back()
27220b57cec5SDimitry Andric{
27230b57cec5SDimitry Andric    _LIBCPP_ASSERT(!empty(), "deque::pop_back called for empty deque");
27240b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
27250b57cec5SDimitry Andric    size_type __p = __base::size() + __base::__start_ - 1;
2726480093f4SDimitry Andric    __alloc_traits::destroy(__a, __to_address(*(__base::__map_.begin() +
27270b57cec5SDimitry Andric                                                    __p / __base::__block_size) +
27280b57cec5SDimitry Andric                                                    __p % __base::__block_size));
27290b57cec5SDimitry Andric    --__base::size();
2730e40139ffSDimitry Andric    __maybe_remove_back_spare();
27310b57cec5SDimitry Andric}
27320b57cec5SDimitry Andric
27330b57cec5SDimitry Andric// move assign [__f, __l) to [__r, __r + (__l-__f)).
27340b57cec5SDimitry Andric// If __vt points into [__f, __l), then subtract (__f - __r) from __vt.
27350b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
27360b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::iterator
27370b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__move_and_check(iterator __f, iterator __l, iterator __r,
27380b57cec5SDimitry Andric                                         const_pointer& __vt)
27390b57cec5SDimitry Andric{
27400b57cec5SDimitry Andric    // as if
27410b57cec5SDimitry Andric    //   for (; __f != __l; ++__f, ++__r)
27420b57cec5SDimitry Andric    //       *__r = _VSTD::move(*__f);
27430b57cec5SDimitry Andric    difference_type __n = __l - __f;
27440b57cec5SDimitry Andric    while (__n > 0)
27450b57cec5SDimitry Andric    {
27460b57cec5SDimitry Andric        pointer __fb = __f.__ptr_;
27470b57cec5SDimitry Andric        pointer __fe = *__f.__m_iter_ + __base::__block_size;
27480b57cec5SDimitry Andric        difference_type __bs = __fe - __fb;
27490b57cec5SDimitry Andric        if (__bs > __n)
27500b57cec5SDimitry Andric        {
27510b57cec5SDimitry Andric            __bs = __n;
27520b57cec5SDimitry Andric            __fe = __fb + __bs;
27530b57cec5SDimitry Andric        }
27540b57cec5SDimitry Andric        if (__fb <= __vt && __vt < __fe)
27550b57cec5SDimitry Andric            __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) -= __f - __r).__ptr_;
27560b57cec5SDimitry Andric        __r = _VSTD::move(__fb, __fe, __r);
27570b57cec5SDimitry Andric        __n -= __bs;
27580b57cec5SDimitry Andric        __f += __bs;
27590b57cec5SDimitry Andric    }
27600b57cec5SDimitry Andric    return __r;
27610b57cec5SDimitry Andric}
27620b57cec5SDimitry Andric
27630b57cec5SDimitry Andric// move assign [__f, __l) to [__r - (__l-__f), __r) backwards.
27640b57cec5SDimitry Andric// If __vt points into [__f, __l), then add (__r - __l) to __vt.
27650b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
27660b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::iterator
27670b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__move_backward_and_check(iterator __f, iterator __l, iterator __r,
27680b57cec5SDimitry Andric                                                  const_pointer& __vt)
27690b57cec5SDimitry Andric{
27700b57cec5SDimitry Andric    // as if
27710b57cec5SDimitry Andric    //   while (__f != __l)
27720b57cec5SDimitry Andric    //       *--__r = _VSTD::move(*--__l);
27730b57cec5SDimitry Andric    difference_type __n = __l - __f;
27740b57cec5SDimitry Andric    while (__n > 0)
27750b57cec5SDimitry Andric    {
27760b57cec5SDimitry Andric        --__l;
27770b57cec5SDimitry Andric        pointer __lb = *__l.__m_iter_;
27780b57cec5SDimitry Andric        pointer __le = __l.__ptr_ + 1;
27790b57cec5SDimitry Andric        difference_type __bs = __le - __lb;
27800b57cec5SDimitry Andric        if (__bs > __n)
27810b57cec5SDimitry Andric        {
27820b57cec5SDimitry Andric            __bs = __n;
27830b57cec5SDimitry Andric            __lb = __le - __bs;
27840b57cec5SDimitry Andric        }
27850b57cec5SDimitry Andric        if (__lb <= __vt && __vt < __le)
27860b57cec5SDimitry Andric            __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) += __r - __l - 1).__ptr_;
27870b57cec5SDimitry Andric        __r = _VSTD::move_backward(__lb, __le, __r);
27880b57cec5SDimitry Andric        __n -= __bs;
27890b57cec5SDimitry Andric        __l -= __bs - 1;
27900b57cec5SDimitry Andric    }
27910b57cec5SDimitry Andric    return __r;
27920b57cec5SDimitry Andric}
27930b57cec5SDimitry Andric
27940b57cec5SDimitry Andric// move construct [__f, __l) to [__r, __r + (__l-__f)).
27950b57cec5SDimitry Andric// If __vt points into [__f, __l), then add (__r - __f) to __vt.
27960b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
27970b57cec5SDimitry Andricvoid
27980b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__move_construct_and_check(iterator __f, iterator __l,
27990b57cec5SDimitry Andric                                                   iterator __r, const_pointer& __vt)
28000b57cec5SDimitry Andric{
28010b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
28020b57cec5SDimitry Andric    // as if
28030b57cec5SDimitry Andric    //   for (; __f != __l; ++__r, ++__f, ++__base::size())
28040b57cec5SDimitry Andric    //       __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__f));
28050b57cec5SDimitry Andric    difference_type __n = __l - __f;
28060b57cec5SDimitry Andric    while (__n > 0)
28070b57cec5SDimitry Andric    {
28080b57cec5SDimitry Andric        pointer __fb = __f.__ptr_;
28090b57cec5SDimitry Andric        pointer __fe = *__f.__m_iter_ + __base::__block_size;
28100b57cec5SDimitry Andric        difference_type __bs = __fe - __fb;
28110b57cec5SDimitry Andric        if (__bs > __n)
28120b57cec5SDimitry Andric        {
28130b57cec5SDimitry Andric            __bs = __n;
28140b57cec5SDimitry Andric            __fe = __fb + __bs;
28150b57cec5SDimitry Andric        }
28160b57cec5SDimitry Andric        if (__fb <= __vt && __vt < __fe)
28170b57cec5SDimitry Andric            __vt = (const_iterator(static_cast<__map_const_pointer>(__f.__m_iter_), __vt) += __r - __f).__ptr_;
28180b57cec5SDimitry Andric        for (; __fb != __fe; ++__fb, ++__r, ++__base::size())
28190b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*__r), _VSTD::move(*__fb));
28200b57cec5SDimitry Andric        __n -= __bs;
28210b57cec5SDimitry Andric        __f += __bs;
28220b57cec5SDimitry Andric    }
28230b57cec5SDimitry Andric}
28240b57cec5SDimitry Andric
28250b57cec5SDimitry Andric// move construct [__f, __l) to [__r - (__l-__f), __r) backwards.
28260b57cec5SDimitry Andric// If __vt points into [__f, __l), then subtract (__l - __r) from __vt.
28270b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
28280b57cec5SDimitry Andricvoid
28290b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__move_construct_backward_and_check(iterator __f, iterator __l,
28300b57cec5SDimitry Andric                                                            iterator __r, const_pointer& __vt)
28310b57cec5SDimitry Andric{
28320b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
28330b57cec5SDimitry Andric    // as if
28340b57cec5SDimitry Andric    //   for (iterator __j = __l; __j != __f;)
28350b57cec5SDimitry Andric    //   {
28360b57cec5SDimitry Andric    //       __alloc_traitsconstruct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__j));
28370b57cec5SDimitry Andric    //       --__base::__start_;
28380b57cec5SDimitry Andric    //       ++__base::size();
28390b57cec5SDimitry Andric    //   }
28400b57cec5SDimitry Andric    difference_type __n = __l - __f;
28410b57cec5SDimitry Andric    while (__n > 0)
28420b57cec5SDimitry Andric    {
28430b57cec5SDimitry Andric        --__l;
28440b57cec5SDimitry Andric        pointer __lb = *__l.__m_iter_;
28450b57cec5SDimitry Andric        pointer __le = __l.__ptr_ + 1;
28460b57cec5SDimitry Andric        difference_type __bs = __le - __lb;
28470b57cec5SDimitry Andric        if (__bs > __n)
28480b57cec5SDimitry Andric        {
28490b57cec5SDimitry Andric            __bs = __n;
28500b57cec5SDimitry Andric            __lb = __le - __bs;
28510b57cec5SDimitry Andric        }
28520b57cec5SDimitry Andric        if (__lb <= __vt && __vt < __le)
28530b57cec5SDimitry Andric            __vt = (const_iterator(static_cast<__map_const_pointer>(__l.__m_iter_), __vt) -= __l - __r + 1).__ptr_;
28540b57cec5SDimitry Andric        while (__le != __lb)
28550b57cec5SDimitry Andric        {
28560b57cec5SDimitry Andric            __alloc_traits::construct(__a, _VSTD::addressof(*--__r), _VSTD::move(*--__le));
28570b57cec5SDimitry Andric            --__base::__start_;
28580b57cec5SDimitry Andric            ++__base::size();
28590b57cec5SDimitry Andric        }
28600b57cec5SDimitry Andric        __n -= __bs;
28610b57cec5SDimitry Andric        __l -= __bs - 1;
28620b57cec5SDimitry Andric    }
28630b57cec5SDimitry Andric}
28640b57cec5SDimitry Andric
28650b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
28660b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::iterator
28670b57cec5SDimitry Andricdeque<_Tp, _Allocator>::erase(const_iterator __f)
28680b57cec5SDimitry Andric{
28690b57cec5SDimitry Andric    iterator __b = __base::begin();
28700b57cec5SDimitry Andric    difference_type __pos = __f - __b;
28710b57cec5SDimitry Andric    iterator __p = __b + __pos;
28720b57cec5SDimitry Andric    allocator_type& __a = __base::__alloc();
28730b57cec5SDimitry Andric    if (static_cast<size_t>(__pos) <= (__base::size() - 1) / 2)
28740b57cec5SDimitry Andric    {   // erase from front
28750b57cec5SDimitry Andric        _VSTD::move_backward(__b, __p, _VSTD::next(__p));
28760b57cec5SDimitry Andric        __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
28770b57cec5SDimitry Andric        --__base::size();
28780b57cec5SDimitry Andric        ++__base::__start_;
2879e40139ffSDimitry Andric        __maybe_remove_front_spare();
28800b57cec5SDimitry Andric    }
28810b57cec5SDimitry Andric    else
28820b57cec5SDimitry Andric    {   // erase from back
28830b57cec5SDimitry Andric        iterator __i = _VSTD::move(_VSTD::next(__p), __base::end(), __p);
28840b57cec5SDimitry Andric        __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
28850b57cec5SDimitry Andric        --__base::size();
2886e40139ffSDimitry Andric        __maybe_remove_back_spare();
28870b57cec5SDimitry Andric    }
28880b57cec5SDimitry Andric    return __base::begin() + __pos;
28890b57cec5SDimitry Andric}
28900b57cec5SDimitry Andric
28910b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
28920b57cec5SDimitry Andrictypename deque<_Tp, _Allocator>::iterator
28930b57cec5SDimitry Andricdeque<_Tp, _Allocator>::erase(const_iterator __f, const_iterator __l)
28940b57cec5SDimitry Andric{
28950b57cec5SDimitry Andric    difference_type __n = __l - __f;
28960b57cec5SDimitry Andric    iterator __b = __base::begin();
28970b57cec5SDimitry Andric    difference_type __pos = __f - __b;
28980b57cec5SDimitry Andric    iterator __p = __b + __pos;
28990b57cec5SDimitry Andric    if (__n > 0)
29000b57cec5SDimitry Andric    {
29010b57cec5SDimitry Andric        allocator_type& __a = __base::__alloc();
29020b57cec5SDimitry Andric        if (static_cast<size_t>(__pos) <= (__base::size() - __n) / 2)
29030b57cec5SDimitry Andric        {   // erase from front
29040b57cec5SDimitry Andric            iterator __i = _VSTD::move_backward(__b, __p, __p + __n);
29050b57cec5SDimitry Andric            for (; __b != __i; ++__b)
29060b57cec5SDimitry Andric                __alloc_traits::destroy(__a, _VSTD::addressof(*__b));
29070b57cec5SDimitry Andric            __base::size() -= __n;
29080b57cec5SDimitry Andric            __base::__start_ += __n;
2909e40139ffSDimitry Andric            while (__maybe_remove_front_spare()) {
29100b57cec5SDimitry Andric            }
29110b57cec5SDimitry Andric        }
29120b57cec5SDimitry Andric        else
29130b57cec5SDimitry Andric        {   // erase from back
29140b57cec5SDimitry Andric            iterator __i = _VSTD::move(__p + __n, __base::end(), __p);
29150b57cec5SDimitry Andric            for (iterator __e = __base::end(); __i != __e; ++__i)
29160b57cec5SDimitry Andric                __alloc_traits::destroy(__a, _VSTD::addressof(*__i));
29170b57cec5SDimitry Andric            __base::size() -= __n;
2918e40139ffSDimitry Andric            while (__maybe_remove_back_spare()) {
29190b57cec5SDimitry Andric            }
29200b57cec5SDimitry Andric        }
29210b57cec5SDimitry Andric    }
29220b57cec5SDimitry Andric    return __base::begin() + __pos;
29230b57cec5SDimitry Andric}
29240b57cec5SDimitry Andric
29250b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
29260b57cec5SDimitry Andricvoid
29270b57cec5SDimitry Andricdeque<_Tp, _Allocator>::__erase_to_end(const_iterator __f)
29280b57cec5SDimitry Andric{
29290b57cec5SDimitry Andric    iterator __e = __base::end();
29300b57cec5SDimitry Andric    difference_type __n = __e - __f;
29310b57cec5SDimitry Andric    if (__n > 0)
29320b57cec5SDimitry Andric    {
29330b57cec5SDimitry Andric        allocator_type& __a = __base::__alloc();
29340b57cec5SDimitry Andric        iterator __b = __base::begin();
29350b57cec5SDimitry Andric        difference_type __pos = __f - __b;
29360b57cec5SDimitry Andric        for (iterator __p = __b + __pos; __p != __e; ++__p)
29370b57cec5SDimitry Andric            __alloc_traits::destroy(__a, _VSTD::addressof(*__p));
29380b57cec5SDimitry Andric        __base::size() -= __n;
2939e40139ffSDimitry Andric        while (__maybe_remove_back_spare()) {
29400b57cec5SDimitry Andric        }
29410b57cec5SDimitry Andric    }
29420b57cec5SDimitry Andric}
29430b57cec5SDimitry Andric
29440b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
29450b57cec5SDimitry Andricinline
29460b57cec5SDimitry Andricvoid
29470b57cec5SDimitry Andricdeque<_Tp, _Allocator>::swap(deque& __c)
29480b57cec5SDimitry Andric#if _LIBCPP_STD_VER >= 14
29490b57cec5SDimitry Andric        _NOEXCEPT
29500b57cec5SDimitry Andric#else
29510b57cec5SDimitry Andric        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
29520b57cec5SDimitry Andric                    __is_nothrow_swappable<allocator_type>::value)
29530b57cec5SDimitry Andric#endif
29540b57cec5SDimitry Andric{
29550b57cec5SDimitry Andric    __base::swap(__c);
29560b57cec5SDimitry Andric}
29570b57cec5SDimitry Andric
29580b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
29590b57cec5SDimitry Andricinline
29600b57cec5SDimitry Andricvoid
29610b57cec5SDimitry Andricdeque<_Tp, _Allocator>::clear() _NOEXCEPT
29620b57cec5SDimitry Andric{
29630b57cec5SDimitry Andric    __base::clear();
29640b57cec5SDimitry Andric}
29650b57cec5SDimitry Andric
29660b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
29670b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
29680b57cec5SDimitry Andricbool
29690b57cec5SDimitry Andricoperator==(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
29700b57cec5SDimitry Andric{
29710b57cec5SDimitry Andric    const typename deque<_Tp, _Allocator>::size_type __sz = __x.size();
29720b57cec5SDimitry Andric    return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
29730b57cec5SDimitry Andric}
29740b57cec5SDimitry Andric
29750b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
29760b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
29770b57cec5SDimitry Andricbool
29780b57cec5SDimitry Andricoperator!=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
29790b57cec5SDimitry Andric{
29800b57cec5SDimitry Andric    return !(__x == __y);
29810b57cec5SDimitry Andric}
29820b57cec5SDimitry Andric
29830b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
29840b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
29850b57cec5SDimitry Andricbool
29860b57cec5SDimitry Andricoperator< (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
29870b57cec5SDimitry Andric{
29880b57cec5SDimitry Andric    return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
29890b57cec5SDimitry Andric}
29900b57cec5SDimitry Andric
29910b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
29920b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
29930b57cec5SDimitry Andricbool
29940b57cec5SDimitry Andricoperator> (const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
29950b57cec5SDimitry Andric{
29960b57cec5SDimitry Andric    return __y < __x;
29970b57cec5SDimitry Andric}
29980b57cec5SDimitry Andric
29990b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
30000b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
30010b57cec5SDimitry Andricbool
30020b57cec5SDimitry Andricoperator>=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
30030b57cec5SDimitry Andric{
30040b57cec5SDimitry Andric    return !(__x < __y);
30050b57cec5SDimitry Andric}
30060b57cec5SDimitry Andric
30070b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
30080b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
30090b57cec5SDimitry Andricbool
30100b57cec5SDimitry Andricoperator<=(const deque<_Tp, _Allocator>& __x, const deque<_Tp, _Allocator>& __y)
30110b57cec5SDimitry Andric{
30120b57cec5SDimitry Andric    return !(__y < __x);
30130b57cec5SDimitry Andric}
30140b57cec5SDimitry Andric
30150b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
30160b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
30170b57cec5SDimitry Andricvoid
30180b57cec5SDimitry Andricswap(deque<_Tp, _Allocator>& __x, deque<_Tp, _Allocator>& __y)
30190b57cec5SDimitry Andric    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
30200b57cec5SDimitry Andric{
30210b57cec5SDimitry Andric    __x.swap(__y);
30220b57cec5SDimitry Andric}
30230b57cec5SDimitry Andric
30240b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 17
30250b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator, class _Up>
3026*5ffd83dbSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY typename deque<_Tp, _Allocator>::size_type
3027*5ffd83dbSDimitry Andricerase(deque<_Tp, _Allocator>& __c, const _Up& __v) {
3028*5ffd83dbSDimitry Andric  auto __old_size = __c.size();
3029*5ffd83dbSDimitry Andric  __c.erase(_VSTD::remove(__c.begin(), __c.end(), __v), __c.end());
3030*5ffd83dbSDimitry Andric  return __old_size - __c.size();
3031*5ffd83dbSDimitry Andric}
30320b57cec5SDimitry Andric
30330b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator, class _Predicate>
3034*5ffd83dbSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY typename deque<_Tp, _Allocator>::size_type
3035*5ffd83dbSDimitry Andricerase_if(deque<_Tp, _Allocator>& __c, _Predicate __pred) {
3036*5ffd83dbSDimitry Andric  auto __old_size = __c.size();
3037*5ffd83dbSDimitry Andric  __c.erase(_VSTD::remove_if(__c.begin(), __c.end(), __pred), __c.end());
3038*5ffd83dbSDimitry Andric  return __old_size - __c.size();
3039*5ffd83dbSDimitry Andric}
30400b57cec5SDimitry Andric#endif
30410b57cec5SDimitry Andric
30420b57cec5SDimitry Andric
30430b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
30440b57cec5SDimitry Andric
30450b57cec5SDimitry Andric_LIBCPP_POP_MACROS
30460b57cec5SDimitry Andric
30470b57cec5SDimitry Andric#endif  // _LIBCPP_DEQUE
3048