Home | History | Annotate | Line # | Download | only in include
      1 // -*- C++ -*-
      2 //===-------------------------- iterator ----------------------------------===//
      3 //
      4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
      5 // See https://llvm.org/LICENSE.txt for license information.
      6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #ifndef _LIBCPP_ITERATOR
     11 #define _LIBCPP_ITERATOR
     12 
     13 /*
     14     iterator synopsis
     15 
     16 #include <concepts>
     17 
     18 namespace std
     19 {
     20 template<class> struct incrementable_traits;       // since C++20
     21 template<class T>
     22   using iter_difference_t = see below;             // since C++20
     23 
     24 template<class> struct indirectly_readable_traits; // since C++20
     25 template<class T>
     26   using iter_value_t = see below;                  // since C++20
     27 
     28 template<class Iterator>
     29 struct iterator_traits;
     30 
     31 template<class T>
     32   requires is_object_v<T>                    // since C++20
     33 struct iterator_traits<T*>;
     34 
     35 template<dereferenceable T>
     36   using iter_reference_t = decltype(*declval<T&>());
     37 
     38 namespace ranges::inline unspecified {
     39     inline constexpr unspecified iter_move = unspecified; // since C++20, nodiscard as an extension
     40 }}
     41 
     42 template<dereferenceable T>
     43   requires ...
     44 using iter_rvalue_reference_t = decltype(ranges::iter_move(declval<T&>())); // since C++20
     45 
     46 // [iterator.concepts], iterator concepts
     47 // [iterator.concept.readable], concept indirectly_readable
     48 template<class In>
     49   concept indirectly_readable = see below;                // since C++20
     50 
     51 // [iterator.concept.writable], concept indirectly_writable
     52 template<class Out, class T>
     53   concept indirectly_writable = see below;                // since C++20
     54 
     55 // [iterator.concept.winc], concept weakly_incrementable
     56 template<class I>
     57   concept weakly_incrementable = see below;                // since C++20
     58 
     59 // [iterator.concept.inc], concept incrementable
     60 template<class I>
     61   concept incrementable = see below;                       // since C++20
     62 
     63 // [iterator.concept.iterator], concept input_or_output_iterator
     64   template<class I>
     65     concept input_or_output_iterator = see below;          // since C++20
     66 
     67 // [iterator.concept.sentinel], concept sentinel_for
     68 template<class S, class I>
     69   concept sentinel_for = see below;                        // since C++20
     70 
     71 // [iterator.concept.sizedsentinel], concept sized_sentinel_for
     72 template<class S, class I>
     73   inline constexpr bool disable_sized_sentinel_for = false;
     74 
     75 template<class S, class I>
     76   concept sized_sentinel_for = see below;
     77 
     78 // [iterator.concept.input], concept input_iterator
     79 template<class I>
     80   concept input_iterator = see below;                      // since C++20
     81 
     82 // [iterator.concept.forward], concept forward_iterator
     83 template<class I>
     84   concept forward_iterator = see below;                    // since C++20
     85 
     86 // [iterator.concept.bidir], concept bidirectional_iterator
     87 template<class I>
     88   concept bidirectional_iterator = see below;              // since C++20
     89 
     90 // [iterator.concept.random.access], concept random_access_iterator
     91 template<class I>
     92   concept random_access_iterator = see below;              // since C++20
     93 
     94 template<class Category, class T, class Distance = ptrdiff_t,
     95          class Pointer = T*, class Reference = T&>
     96 struct iterator
     97 {
     98     typedef T         value_type;
     99     typedef Distance  difference_type;
    100     typedef Pointer   pointer;
    101     typedef Reference reference;
    102     typedef Category  iterator_category;
    103 };
    104 
    105 struct input_iterator_tag  {};
    106 struct output_iterator_tag {};
    107 struct forward_iterator_tag       : public input_iterator_tag         {};
    108 struct bidirectional_iterator_tag : public forward_iterator_tag       {};
    109 struct random_access_iterator_tag : public bidirectional_iterator_tag {};
    110 
    111 // 27.4.3, iterator operations
    112 template <class InputIterator, class Distance>  // constexpr in C++17
    113   constexpr void advance(InputIterator& i, Distance n);
    114 
    115 template <class InputIterator>  // constexpr in C++17
    116   constexpr typename iterator_traits<InputIterator>::difference_type
    117     distance(InputIterator first, InputIterator last);
    118 
    119 template <class InputIterator>  // constexpr in C++17
    120   constexpr InputIterator next(InputIterator x,
    121 typename iterator_traits<InputIterator>::difference_type n = 1);
    122 
    123 template <class BidirectionalIterator>  // constexpr in C++17
    124   constexpr BidirectionalIterator prev(BidirectionalIterator x,
    125     typename iterator_traits<BidirectionalIterator>::difference_type n = 1);
    126 
    127 template <class Iterator>
    128 class reverse_iterator
    129     : public iterator<typename iterator_traits<Iterator>::iterator_category,
    130                       typename iterator_traits<Iterator>::value_type,
    131                       typename iterator_traits<Iterator>::difference_type,
    132                       typename iterator_traits<Iterator>::pointer,
    133                       typename iterator_traits<Iterator>::reference>
    134 {
    135 protected:
    136     Iterator current;
    137 public:
    138     typedef Iterator                                            iterator_type;
    139     typedef typename iterator_traits<Iterator>::difference_type difference_type;
    140     typedef typename iterator_traits<Iterator>::reference       reference;
    141     typedef typename iterator_traits<Iterator>::pointer         pointer;
    142 
    143     constexpr reverse_iterator();
    144     constexpr explicit reverse_iterator(Iterator x);
    145     template <class U> constexpr reverse_iterator(const reverse_iterator<U>& u);
    146     template <class U> constexpr reverse_iterator& operator=(const reverse_iterator<U>& u);
    147     constexpr Iterator base() const;
    148     constexpr reference operator*() const;
    149     constexpr pointer   operator->() const;
    150     constexpr reverse_iterator& operator++();
    151     constexpr reverse_iterator  operator++(int);
    152     constexpr reverse_iterator& operator--();
    153     constexpr reverse_iterator  operator--(int);
    154     constexpr reverse_iterator  operator+ (difference_type n) const;
    155     constexpr reverse_iterator& operator+=(difference_type n);
    156     constexpr reverse_iterator  operator- (difference_type n) const;
    157     constexpr reverse_iterator& operator-=(difference_type n);
    158     constexpr reference         operator[](difference_type n) const;
    159 };
    160 
    161 template <class Iterator1, class Iterator2>
    162 constexpr bool                          // constexpr in C++17
    163 operator==(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
    164 
    165 template <class Iterator1, class Iterator2>
    166 constexpr bool                          // constexpr in C++17
    167 operator<(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
    168 
    169 template <class Iterator1, class Iterator2>
    170 constexpr bool                          // constexpr in C++17
    171 operator!=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
    172 
    173 template <class Iterator1, class Iterator2>
    174 constexpr bool                          // constexpr in C++17
    175 operator>(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
    176 
    177 template <class Iterator1, class Iterator2>
    178 constexpr bool                          // constexpr in C++17
    179 operator>=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
    180 
    181 template <class Iterator1, class Iterator2>
    182 constexpr bool                          // constexpr in C++17
    183 operator<=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
    184 
    185 template <class Iterator1, class Iterator2>
    186 constexpr auto
    187 operator-(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y)
    188 -> decltype(__y.base() - __x.base());   // constexpr in C++17
    189 
    190 template <class Iterator>
    191 constexpr reverse_iterator<Iterator>
    192 operator+(typename reverse_iterator<Iterator>::difference_type n,
    193           const reverse_iterator<Iterator>& x);   // constexpr in C++17
    194 
    195 template <class Iterator>
    196 constexpr reverse_iterator<Iterator> make_reverse_iterator(Iterator i); // C++14, constexpr in C++17
    197 
    198 template <class Container>
    199 class back_insert_iterator
    200 {
    201 protected:
    202     Container* container;
    203 public:
    204     typedef Container                   container_type;
    205     typedef void                        value_type;
    206     typedef void                        difference_type;
    207     typedef void                        reference;
    208     typedef void                        pointer;
    209 
    210     explicit back_insert_iterator(Container& x);  // constexpr in C++20
    211     back_insert_iterator& operator=(const typename Container::value_type& value);  // constexpr in C++20
    212     back_insert_iterator& operator*();  // constexpr in C++20
    213     back_insert_iterator& operator++();  // constexpr in C++20
    214     back_insert_iterator  operator++(int);  // constexpr in C++20
    215 };
    216 
    217 template <class Container> back_insert_iterator<Container> back_inserter(Container& x);  // constexpr in C++20
    218 
    219 template <class Container>
    220 class front_insert_iterator
    221 {
    222 protected:
    223     Container* container;
    224 public:
    225     typedef Container                    container_type;
    226     typedef void                         value_type;
    227     typedef void                         difference_type;
    228     typedef void                         reference;
    229     typedef void                         pointer;
    230 
    231     explicit front_insert_iterator(Container& x);  // constexpr in C++20
    232     front_insert_iterator& operator=(const typename Container::value_type& value);  // constexpr in C++20
    233     front_insert_iterator& operator*();  // constexpr in C++20
    234     front_insert_iterator& operator++();  // constexpr in C++20
    235     front_insert_iterator  operator++(int);  // constexpr in C++20
    236 };
    237 
    238 template <class Container> front_insert_iterator<Container> front_inserter(Container& x);  // constexpr in C++20
    239 
    240 template <class Container>
    241 class insert_iterator
    242 {
    243 protected:
    244     Container* container;
    245     typename Container::iterator iter;
    246 public:
    247     typedef Container              container_type;
    248     typedef void                   value_type;
    249     typedef void                   difference_type;
    250     typedef void                   reference;
    251     typedef void                   pointer;
    252 
    253     insert_iterator(Container& x, typename Container::iterator i);  // constexpr in C++20
    254     insert_iterator& operator=(const typename Container::value_type& value);  // constexpr in C++20
    255     insert_iterator& operator*();  // constexpr in C++20
    256     insert_iterator& operator++();  // constexpr in C++20
    257     insert_iterator& operator++(int);  // constexpr in C++20
    258 };
    259 
    260 template <class Container, class Iterator>
    261 insert_iterator<Container> inserter(Container& x, Iterator i);  // constexpr in C++20
    262 
    263 template <class Iterator>
    264 class move_iterator {
    265 public:
    266     typedef Iterator                                              iterator_type;
    267     typedef typename iterator_traits<Iterator>::difference_type   difference_type;
    268     typedef Iterator                                              pointer;
    269     typedef typename iterator_traits<Iterator>::value_type        value_type;
    270     typedef typename iterator_traits<Iterator>::iterator_category iterator_category;
    271     typedef value_type&&                                          reference;
    272 
    273     constexpr move_iterator();  // all the constexprs are in C++17
    274     constexpr explicit move_iterator(Iterator i);
    275     template <class U>
    276       constexpr move_iterator(const move_iterator<U>& u);
    277     template <class U>
    278       constexpr move_iterator& operator=(const move_iterator<U>& u);
    279     constexpr iterator_type base() const;
    280     constexpr reference operator*() const;
    281     constexpr pointer operator->() const;
    282     constexpr move_iterator& operator++();
    283     constexpr move_iterator operator++(int);
    284     constexpr move_iterator& operator--();
    285     constexpr move_iterator operator--(int);
    286     constexpr move_iterator operator+(difference_type n) const;
    287     constexpr move_iterator& operator+=(difference_type n);
    288     constexpr move_iterator operator-(difference_type n) const;
    289     constexpr move_iterator& operator-=(difference_type n);
    290     constexpr unspecified operator[](difference_type n) const;
    291 private:
    292     Iterator current; // exposition only
    293 };
    294 
    295 template <class Iterator1, class Iterator2>
    296 constexpr bool   // constexpr in C++17
    297 operator==(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
    298 
    299 template <class Iterator1, class Iterator2>
    300 constexpr bool   // constexpr in C++17
    301 operator!=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
    302 
    303 template <class Iterator1, class Iterator2>
    304 constexpr bool   // constexpr in C++17
    305 operator<(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
    306 
    307 template <class Iterator1, class Iterator2>
    308 constexpr bool   // constexpr in C++17
    309 operator<=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
    310 
    311 template <class Iterator1, class Iterator2>
    312 constexpr bool   // constexpr in C++17
    313 operator>(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
    314 
    315 template <class Iterator1, class Iterator2>
    316 constexpr bool   // constexpr in C++17
    317 operator>=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
    318 
    319 template <class Iterator1, class Iterator2>
    320 constexpr auto   // constexpr in C++17
    321 operator-(const move_iterator<Iterator1>& x,
    322           const move_iterator<Iterator2>& y) -> decltype(x.base() - y.base());
    323 
    324 template <class Iterator>
    325 constexpr move_iterator<Iterator> operator+(   // constexpr in C++17
    326             typename move_iterator<Iterator>::difference_type n,
    327             const move_iterator<Iterator>& x);
    328 
    329 template <class Iterator>   // constexpr in C++17
    330 constexpr  move_iterator<Iterator> make_move_iterator(const Iterator& i);
    331 
    332 
    333 template <class T, class charT = char, class traits = char_traits<charT>, class Distance = ptrdiff_t>
    334 class istream_iterator
    335     : public iterator<input_iterator_tag, T, Distance, const T*, const T&>
    336 {
    337 public:
    338     typedef charT char_type;
    339     typedef traits traits_type;
    340     typedef basic_istream<charT,traits> istream_type;
    341 
    342     constexpr istream_iterator();
    343     istream_iterator(istream_type& s);
    344     istream_iterator(const istream_iterator& x);
    345     ~istream_iterator();
    346 
    347     const T& operator*() const;
    348     const T* operator->() const;
    349     istream_iterator& operator++();
    350     istream_iterator  operator++(int);
    351 };
    352 
    353 template <class T, class charT, class traits, class Distance>
    354 bool operator==(const istream_iterator<T,charT,traits,Distance>& x,
    355                 const istream_iterator<T,charT,traits,Distance>& y);
    356 template <class T, class charT, class traits, class Distance>
    357 bool operator!=(const istream_iterator<T,charT,traits,Distance>& x,
    358                 const istream_iterator<T,charT,traits,Distance>& y);
    359 
    360 template <class T, class charT = char, class traits = char_traits<charT> >
    361 class ostream_iterator
    362     : public iterator<output_iterator_tag, void, void, void ,void>
    363 {
    364 public:
    365     typedef charT char_type;
    366     typedef traits traits_type;
    367     typedef basic_ostream<charT,traits> ostream_type;
    368 
    369     ostream_iterator(ostream_type& s);
    370     ostream_iterator(ostream_type& s, const charT* delimiter);
    371     ostream_iterator(const ostream_iterator& x);
    372     ~ostream_iterator();
    373     ostream_iterator& operator=(const T& value);
    374 
    375     ostream_iterator& operator*();
    376     ostream_iterator& operator++();
    377     ostream_iterator& operator++(int);
    378 };
    379 
    380 template<class charT, class traits = char_traits<charT> >
    381 class istreambuf_iterator
    382     : public iterator<input_iterator_tag, charT,
    383                       typename traits::off_type, unspecified,
    384                       charT>
    385 {
    386 public:
    387     typedef charT                         char_type;
    388     typedef traits                        traits_type;
    389     typedef typename traits::int_type     int_type;
    390     typedef basic_streambuf<charT,traits> streambuf_type;
    391     typedef basic_istream<charT,traits>   istream_type;
    392 
    393     istreambuf_iterator() noexcept;
    394     istreambuf_iterator(istream_type& s) noexcept;
    395     istreambuf_iterator(streambuf_type* s) noexcept;
    396     istreambuf_iterator(a-private-type) noexcept;
    397 
    398     charT                operator*() const;
    399     pointer operator->() const;
    400     istreambuf_iterator& operator++();
    401     a-private-type       operator++(int);
    402 
    403     bool equal(const istreambuf_iterator& b) const;
    404 };
    405 
    406 template <class charT, class traits>
    407 bool operator==(const istreambuf_iterator<charT,traits>& a,
    408                 const istreambuf_iterator<charT,traits>& b);
    409 template <class charT, class traits>
    410 bool operator!=(const istreambuf_iterator<charT,traits>& a,
    411                 const istreambuf_iterator<charT,traits>& b);
    412 
    413 template <class charT, class traits = char_traits<charT> >
    414 class ostreambuf_iterator
    415     : public iterator<output_iterator_tag, void, void, void, void>
    416 {
    417 public:
    418     typedef charT                         char_type;
    419     typedef traits                        traits_type;
    420     typedef basic_streambuf<charT,traits> streambuf_type;
    421     typedef basic_ostream<charT,traits>   ostream_type;
    422 
    423     ostreambuf_iterator(ostream_type& s) noexcept;
    424     ostreambuf_iterator(streambuf_type* s) noexcept;
    425     ostreambuf_iterator& operator=(charT c);
    426     ostreambuf_iterator& operator*();
    427     ostreambuf_iterator& operator++();
    428     ostreambuf_iterator& operator++(int);
    429     bool failed() const noexcept;
    430 };
    431 
    432 template <class C> constexpr auto begin(C& c) -> decltype(c.begin());
    433 template <class C> constexpr auto begin(const C& c) -> decltype(c.begin());
    434 template <class C> constexpr auto end(C& c) -> decltype(c.end());
    435 template <class C> constexpr auto end(const C& c) -> decltype(c.end());
    436 template <class T, size_t N> constexpr T* begin(T (&array)[N]);
    437 template <class T, size_t N> constexpr T* end(T (&array)[N]);
    438 
    439 template <class C> auto constexpr cbegin(const C& c) -> decltype(std::begin(c));        // C++14
    440 template <class C> auto constexpr cend(const C& c) -> decltype(std::end(c));            // C++14
    441 template <class C> auto constexpr rbegin(C& c) -> decltype(c.rbegin());                 // C++14
    442 template <class C> auto constexpr rbegin(const C& c) -> decltype(c.rbegin());           // C++14
    443 template <class C> auto constexpr rend(C& c) -> decltype(c.rend());                     // C++14
    444 template <class C> constexpr auto rend(const C& c) -> decltype(c.rend());               // C++14
    445 template <class E> reverse_iterator<const E*> constexpr rbegin(initializer_list<E> il); // C++14
    446 template <class E> reverse_iterator<const E*> constexpr rend(initializer_list<E> il);   // C++14
    447 template <class T, size_t N> reverse_iterator<T*> constexpr rbegin(T (&array)[N]);      // C++14
    448 template <class T, size_t N> reverse_iterator<T*> constexpr rend(T (&array)[N]);        // C++14
    449 template <class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c));      // C++14
    450 template <class C> constexpr auto crend(const C& c) -> decltype(std::rend(c));          // C++14
    451 
    452 // 24.8, container access:
    453 template <class C> constexpr auto size(const C& c) -> decltype(c.size());         // C++17
    454 template <class T, size_t N> constexpr size_t size(const T (&array)[N]) noexcept; // C++17
    455 
    456 template <class C> constexpr auto ssize(const C& c)
    457     -> common_type_t<ptrdiff_t, make_signed_t<decltype(c.size())>>;                    // C++20
    458 template <class T, ptrdiff_t> constexpr ptrdiff_t ssize(const T (&array)[N]) noexcept; // C++20
    459 
    460 template <class C> constexpr auto empty(const C& c) -> decltype(c.empty());       // C++17
    461 template <class T, size_t N> constexpr bool empty(const T (&array)[N]) noexcept;  // C++17
    462 template <class E> constexpr bool empty(initializer_list<E> il) noexcept;         // C++17
    463 template <class C> constexpr auto data(C& c) -> decltype(c.data());               // C++17
    464 template <class C> constexpr auto data(const C& c) -> decltype(c.data());         // C++17
    465 template <class T, size_t N> constexpr T* data(T (&array)[N]) noexcept;           // C++17
    466 template <class E> constexpr const E* data(initializer_list<E> il) noexcept;      // C++17
    467 
    468 }  // std
    469 
    470 */
    471 
    472 #include <__config>
    473 #include <__debug>
    474 #include <__functional_base>
    475 #include <__iterator/concepts.h>
    476 #include <__iterator/incrementable_traits.h>
    477 #include <__iterator/iter_move.h>
    478 #include <__iterator/iterator_traits.h>
    479 #include <__iterator/readable_traits.h>
    480 #include <__memory/addressof.h>
    481 #include <__memory/pointer_traits.h>
    482 #include <compare>
    483 #include <concepts> // Mandated by the Standard.
    484 #include <cstddef>
    485 #include <initializer_list>
    486 #include <iosfwd> // for forward declarations of vector and string
    487 #include <type_traits>
    488 #include <version>
    489 
    490 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
    491 #pragma GCC system_header
    492 #endif
    493 
    494 _LIBCPP_BEGIN_NAMESPACE_STD
    495 
    496 template<class _Category, class _Tp, class _Distance = ptrdiff_t,
    497          class _Pointer = _Tp*, class _Reference = _Tp&>
    498 struct _LIBCPP_TEMPLATE_VIS iterator
    499 {
    500     typedef _Tp        value_type;
    501     typedef _Distance  difference_type;
    502     typedef _Pointer   pointer;
    503     typedef _Reference reference;
    504     typedef _Category  iterator_category;
    505 };
    506 
    507 template <class _InputIter>
    508 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    509 void __advance(_InputIter& __i,
    510              typename iterator_traits<_InputIter>::difference_type __n, input_iterator_tag)
    511 {
    512     for (; __n > 0; --__n)
    513         ++__i;
    514 }
    515 
    516 template <class _BiDirIter>
    517 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    518 void __advance(_BiDirIter& __i,
    519              typename iterator_traits<_BiDirIter>::difference_type __n, bidirectional_iterator_tag)
    520 {
    521     if (__n >= 0)
    522         for (; __n > 0; --__n)
    523             ++__i;
    524     else
    525         for (; __n < 0; ++__n)
    526             --__i;
    527 }
    528 
    529 template <class _RandIter>
    530 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    531 void __advance(_RandIter& __i,
    532              typename iterator_traits<_RandIter>::difference_type __n, random_access_iterator_tag)
    533 {
    534    __i += __n;
    535 }
    536 
    537 template <class _InputIter, class _Distance>
    538 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    539 void advance(_InputIter& __i, _Distance __orig_n)
    540 {
    541     typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
    542     _IntegralSize __n = __orig_n;
    543     _LIBCPP_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
    544                    "Attempt to advance(it, n) with negative n on a non-bidirectional iterator");
    545     _VSTD::__advance(__i, __n, typename iterator_traits<_InputIter>::iterator_category());
    546 }
    547 
    548 template <class _InputIter>
    549 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    550 typename iterator_traits<_InputIter>::difference_type
    551 __distance(_InputIter __first, _InputIter __last, input_iterator_tag)
    552 {
    553     typename iterator_traits<_InputIter>::difference_type __r(0);
    554     for (; __first != __last; ++__first)
    555         ++__r;
    556     return __r;
    557 }
    558 
    559 template <class _RandIter>
    560 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    561 typename iterator_traits<_RandIter>::difference_type
    562 __distance(_RandIter __first, _RandIter __last, random_access_iterator_tag)
    563 {
    564     return __last - __first;
    565 }
    566 
    567 template <class _InputIter>
    568 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    569 typename iterator_traits<_InputIter>::difference_type
    570 distance(_InputIter __first, _InputIter __last)
    571 {
    572     return _VSTD::__distance(__first, __last, typename iterator_traits<_InputIter>::iterator_category());
    573 }
    574 
    575 template <class _InputIter>
    576 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    577 typename enable_if
    578 <
    579     __is_cpp17_input_iterator<_InputIter>::value,
    580     _InputIter
    581 >::type
    582 next(_InputIter __x,
    583      typename iterator_traits<_InputIter>::difference_type __n = 1)
    584 {
    585     _LIBCPP_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
    586                        "Attempt to next(it, n) with negative n on a non-bidirectional iterator");
    587 
    588     _VSTD::advance(__x, __n);
    589     return __x;
    590 }
    591 
    592 template <class _InputIter>
    593 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    594 typename enable_if
    595 <
    596     __is_cpp17_input_iterator<_InputIter>::value,
    597     _InputIter
    598 >::type
    599 prev(_InputIter __x,
    600      typename iterator_traits<_InputIter>::difference_type __n = 1)
    601 {
    602     _LIBCPP_ASSERT(__n <= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
    603                        "Attempt to prev(it, n) with a positive n on a non-bidirectional iterator");
    604     _VSTD::advance(__x, -__n);
    605     return __x;
    606 }
    607 
    608 
    609 template <class _Tp, class = void>
    610 struct __is_stashing_iterator : false_type {};
    611 
    612 template <class _Tp>
    613 struct __is_stashing_iterator<_Tp, typename __void_t<typename _Tp::__stashing_iterator_tag>::type>
    614   : true_type {};
    615 
    616 template <class _Iter>
    617 class _LIBCPP_TEMPLATE_VIS reverse_iterator
    618     : public iterator<typename iterator_traits<_Iter>::iterator_category,
    619                       typename iterator_traits<_Iter>::value_type,
    620                       typename iterator_traits<_Iter>::difference_type,
    621                       typename iterator_traits<_Iter>::pointer,
    622                       typename iterator_traits<_Iter>::reference>
    623 {
    624 private:
    625     /*mutable*/ _Iter __t;  // no longer used as of LWG #2360, not removed due to ABI break
    626 
    627     static_assert(!__is_stashing_iterator<_Iter>::value,
    628       "The specified iterator type cannot be used with reverse_iterator; "
    629       "Using stashing iterators with reverse_iterator causes undefined behavior");
    630 
    631 protected:
    632     _Iter current;
    633 public:
    634     typedef _Iter                                            iterator_type;
    635     typedef typename iterator_traits<_Iter>::difference_type difference_type;
    636     typedef typename iterator_traits<_Iter>::reference       reference;
    637     typedef typename iterator_traits<_Iter>::pointer         pointer;
    638     typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
    639         random_access_iterator_tag,
    640         typename iterator_traits<_Iter>::iterator_category>  iterator_category;
    641 #if _LIBCPP_STD_VER > 17
    642     typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
    643         random_access_iterator_tag,
    644         bidirectional_iterator_tag>                          iterator_concept;
    645 #endif
    646 
    647     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    648     reverse_iterator() : __t(), current() {}
    649     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    650     explicit reverse_iterator(_Iter __x) : __t(__x), current(__x) {}
    651     template <class _Up>
    652         _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    653         reverse_iterator(const reverse_iterator<_Up>& __u) : __t(__u.base()), current(__u.base()) {}
    654     template <class _Up>
    655         _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    656         reverse_iterator& operator=(const reverse_iterator<_Up>& __u)
    657             { __t = current = __u.base(); return *this; }
    658     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    659     _Iter base() const {return current;}
    660     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    661     reference operator*() const {_Iter __tmp = current; return *--__tmp;}
    662     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    663     pointer  operator->() const {return _VSTD::addressof(operator*());}
    664     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    665     reverse_iterator& operator++() {--current; return *this;}
    666     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    667     reverse_iterator  operator++(int) {reverse_iterator __tmp(*this); --current; return __tmp;}
    668     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    669     reverse_iterator& operator--() {++current; return *this;}
    670     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    671     reverse_iterator  operator--(int) {reverse_iterator __tmp(*this); ++current; return __tmp;}
    672     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    673     reverse_iterator  operator+ (difference_type __n) const {return reverse_iterator(current - __n);}
    674     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    675     reverse_iterator& operator+=(difference_type __n) {current -= __n; return *this;}
    676     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    677     reverse_iterator  operator- (difference_type __n) const {return reverse_iterator(current + __n);}
    678     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    679     reverse_iterator& operator-=(difference_type __n) {current += __n; return *this;}
    680     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    681     reference         operator[](difference_type __n) const {return *(*this + __n);}
    682 };
    683 
    684 template <class _Iter1, class _Iter2>
    685 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    686 bool
    687 operator==(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
    688 {
    689     return __x.base() == __y.base();
    690 }
    691 
    692 template <class _Iter1, class _Iter2>
    693 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    694 bool
    695 operator<(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
    696 {
    697     return __x.base() > __y.base();
    698 }
    699 
    700 template <class _Iter1, class _Iter2>
    701 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    702 bool
    703 operator!=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
    704 {
    705     return __x.base() != __y.base();
    706 }
    707 
    708 template <class _Iter1, class _Iter2>
    709 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    710 bool
    711 operator>(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
    712 {
    713     return __x.base() < __y.base();
    714 }
    715 
    716 template <class _Iter1, class _Iter2>
    717 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    718 bool
    719 operator>=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
    720 {
    721     return __x.base() <= __y.base();
    722 }
    723 
    724 template <class _Iter1, class _Iter2>
    725 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    726 bool
    727 operator<=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
    728 {
    729     return __x.base() >= __y.base();
    730 }
    731 
    732 #ifndef _LIBCPP_CXX03_LANG
    733 template <class _Iter1, class _Iter2>
    734 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    735 auto
    736 operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
    737 -> decltype(__y.base() - __x.base())
    738 {
    739     return __y.base() - __x.base();
    740 }
    741 #else
    742 template <class _Iter1, class _Iter2>
    743 inline _LIBCPP_INLINE_VISIBILITY
    744 typename reverse_iterator<_Iter1>::difference_type
    745 operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
    746 {
    747     return __y.base() - __x.base();
    748 }
    749 #endif
    750 
    751 template <class _Iter>
    752 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    753 reverse_iterator<_Iter>
    754 operator+(typename reverse_iterator<_Iter>::difference_type __n, const reverse_iterator<_Iter>& __x)
    755 {
    756     return reverse_iterator<_Iter>(__x.base() - __n);
    757 }
    758 
    759 #if _LIBCPP_STD_VER > 11
    760 template <class _Iter>
    761 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
    762 reverse_iterator<_Iter> make_reverse_iterator(_Iter __i)
    763 {
    764     return reverse_iterator<_Iter>(__i);
    765 }
    766 #endif
    767 
    768 template <class _Container>
    769 class _LIBCPP_TEMPLATE_VIS back_insert_iterator
    770     : public iterator<output_iterator_tag,
    771                       void,
    772                       void,
    773                       void,
    774                       void>
    775 {
    776 protected:
    777     _Container* container;
    778 public:
    779     typedef _Container container_type;
    780 
    781     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit back_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
    782     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator=(const typename _Container::value_type& __value_)
    783         {container->push_back(__value_); return *this;}
    784 #ifndef _LIBCPP_CXX03_LANG
    785     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator=(typename _Container::value_type&& __value_)
    786         {container->push_back(_VSTD::move(__value_)); return *this;}
    787 #endif // _LIBCPP_CXX03_LANG
    788     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator*()     {return *this;}
    789     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator++()    {return *this;}
    790     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator  operator++(int) {return *this;}
    791 };
    792 
    793 template <class _Container>
    794 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    795 back_insert_iterator<_Container>
    796 back_inserter(_Container& __x)
    797 {
    798     return back_insert_iterator<_Container>(__x);
    799 }
    800 
    801 template <class _Container>
    802 class _LIBCPP_TEMPLATE_VIS front_insert_iterator
    803     : public iterator<output_iterator_tag,
    804                       void,
    805                       void,
    806                       void,
    807                       void>
    808 {
    809 protected:
    810     _Container* container;
    811 public:
    812     typedef _Container container_type;
    813 
    814     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit front_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
    815     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator=(const typename _Container::value_type& __value_)
    816         {container->push_front(__value_); return *this;}
    817 #ifndef _LIBCPP_CXX03_LANG
    818     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator=(typename _Container::value_type&& __value_)
    819         {container->push_front(_VSTD::move(__value_)); return *this;}
    820 #endif // _LIBCPP_CXX03_LANG
    821     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator*()     {return *this;}
    822     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator++()    {return *this;}
    823     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator  operator++(int) {return *this;}
    824 };
    825 
    826 template <class _Container>
    827 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    828 front_insert_iterator<_Container>
    829 front_inserter(_Container& __x)
    830 {
    831     return front_insert_iterator<_Container>(__x);
    832 }
    833 
    834 template <class _Container>
    835 class _LIBCPP_TEMPLATE_VIS insert_iterator
    836     : public iterator<output_iterator_tag,
    837                       void,
    838                       void,
    839                       void,
    840                       void>
    841 {
    842 protected:
    843     _Container* container;
    844     typename _Container::iterator iter;
    845 public:
    846     typedef _Container container_type;
    847 
    848     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator(_Container& __x, typename _Container::iterator __i)
    849         : container(_VSTD::addressof(__x)), iter(__i) {}
    850     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator=(const typename _Container::value_type& __value_)
    851         {iter = container->insert(iter, __value_); ++iter; return *this;}
    852 #ifndef _LIBCPP_CXX03_LANG
    853     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator=(typename _Container::value_type&& __value_)
    854         {iter = container->insert(iter, _VSTD::move(__value_)); ++iter; return *this;}
    855 #endif // _LIBCPP_CXX03_LANG
    856     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator*()        {return *this;}
    857     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator++()       {return *this;}
    858     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator++(int)    {return *this;}
    859 };
    860 
    861 template <class _Container>
    862 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
    863 insert_iterator<_Container>
    864 inserter(_Container& __x, typename _Container::iterator __i)
    865 {
    866     return insert_iterator<_Container>(__x, __i);
    867 }
    868 
    869 template <class _Tp, class _CharT = char,
    870           class _Traits = char_traits<_CharT>, class _Distance = ptrdiff_t>
    871 class _LIBCPP_TEMPLATE_VIS istream_iterator
    872     : public iterator<input_iterator_tag, _Tp, _Distance, const _Tp*, const _Tp&>
    873 {
    874 public:
    875     typedef _CharT char_type;
    876     typedef _Traits traits_type;
    877     typedef basic_istream<_CharT,_Traits> istream_type;
    878 private:
    879     istream_type* __in_stream_;
    880     _Tp __value_;
    881 public:
    882     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istream_iterator() : __in_stream_(nullptr), __value_() {}
    883     _LIBCPP_INLINE_VISIBILITY istream_iterator(istream_type& __s) : __in_stream_(_VSTD::addressof(__s))
    884         {
    885             if (!(*__in_stream_ >> __value_))
    886                 __in_stream_ = nullptr;
    887         }
    888 
    889     _LIBCPP_INLINE_VISIBILITY const _Tp& operator*() const {return __value_;}
    890     _LIBCPP_INLINE_VISIBILITY const _Tp* operator->() const {return _VSTD::addressof((operator*()));}
    891     _LIBCPP_INLINE_VISIBILITY istream_iterator& operator++()
    892         {
    893             if (!(*__in_stream_ >> __value_))
    894                 __in_stream_ = nullptr;
    895             return *this;
    896         }
    897     _LIBCPP_INLINE_VISIBILITY istream_iterator  operator++(int)
    898         {istream_iterator __t(*this); ++(*this); return __t;}
    899 
    900     template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
    901     friend _LIBCPP_INLINE_VISIBILITY
    902     bool
    903     operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
    904                const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
    905 
    906     template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
    907     friend _LIBCPP_INLINE_VISIBILITY
    908     bool
    909     operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
    910                const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
    911 };
    912 
    913 template <class _Tp, class _CharT, class _Traits, class _Distance>
    914 inline _LIBCPP_INLINE_VISIBILITY
    915 bool
    916 operator==(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
    917            const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
    918 {
    919     return __x.__in_stream_ == __y.__in_stream_;
    920 }
    921 
    922 template <class _Tp, class _CharT, class _Traits, class _Distance>
    923 inline _LIBCPP_INLINE_VISIBILITY
    924 bool
    925 operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
    926            const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
    927 {
    928     return !(__x == __y);
    929 }
    930 
    931 template <class _Tp, class _CharT = char, class _Traits = char_traits<_CharT> >
    932 class _LIBCPP_TEMPLATE_VIS ostream_iterator
    933     : public iterator<output_iterator_tag, void, void, void, void>
    934 {
    935 public:
    936     typedef output_iterator_tag             iterator_category;
    937     typedef void                            value_type;
    938 #if _LIBCPP_STD_VER > 17
    939     typedef std::ptrdiff_t                  difference_type;
    940 #else
    941     typedef void                            difference_type;
    942 #endif
    943     typedef void                            pointer;
    944     typedef void                            reference;
    945     typedef _CharT                          char_type;
    946     typedef _Traits                         traits_type;
    947     typedef basic_ostream<_CharT, _Traits>  ostream_type;
    948 
    949 private:
    950     ostream_type* __out_stream_;
    951     const char_type* __delim_;
    952 public:
    953     _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s) _NOEXCEPT
    954         : __out_stream_(_VSTD::addressof(__s)), __delim_(nullptr) {}
    955     _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s, const _CharT* __delimiter) _NOEXCEPT
    956         : __out_stream_(_VSTD::addressof(__s)), __delim_(__delimiter) {}
    957     _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator=(const _Tp& __value_)
    958         {
    959             *__out_stream_ << __value_;
    960             if (__delim_)
    961                 *__out_stream_ << __delim_;
    962             return *this;
    963         }
    964 
    965     _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator*()     {return *this;}
    966     _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++()    {return *this;}
    967     _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++(int) {return *this;}
    968 };
    969 
    970 template<class _CharT, class _Traits>
    971 class _LIBCPP_TEMPLATE_VIS istreambuf_iterator
    972     : public iterator<input_iterator_tag, _CharT,
    973                       typename _Traits::off_type, _CharT*,
    974                       _CharT>
    975 {
    976 public:
    977     typedef _CharT                          char_type;
    978     typedef _Traits                         traits_type;
    979     typedef typename _Traits::int_type      int_type;
    980     typedef basic_streambuf<_CharT,_Traits> streambuf_type;
    981     typedef basic_istream<_CharT,_Traits>   istream_type;
    982 private:
    983     mutable streambuf_type* __sbuf_;
    984 
    985     class __proxy
    986     {
    987         char_type __keep_;
    988         streambuf_type* __sbuf_;
    989         _LIBCPP_INLINE_VISIBILITY __proxy(char_type __c, streambuf_type* __s)
    990             : __keep_(__c), __sbuf_(__s) {}
    991         friend class istreambuf_iterator;
    992     public:
    993         _LIBCPP_INLINE_VISIBILITY char_type operator*() const {return __keep_;}
    994     };
    995 
    996     _LIBCPP_INLINE_VISIBILITY
    997     bool __test_for_eof() const
    998     {
    999         if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof()))
   1000             __sbuf_ = nullptr;
   1001         return __sbuf_ == nullptr;
   1002     }
   1003 public:
   1004     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istreambuf_iterator() _NOEXCEPT : __sbuf_(nullptr) {}
   1005     _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(istream_type& __s) _NOEXCEPT
   1006         : __sbuf_(__s.rdbuf()) {}
   1007     _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(streambuf_type* __s) _NOEXCEPT
   1008         : __sbuf_(__s) {}
   1009     _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(const __proxy& __p) _NOEXCEPT
   1010         : __sbuf_(__p.__sbuf_) {}
   1011 
   1012     _LIBCPP_INLINE_VISIBILITY char_type  operator*() const
   1013         {return static_cast<char_type>(__sbuf_->sgetc());}
   1014     _LIBCPP_INLINE_VISIBILITY istreambuf_iterator& operator++()
   1015         {
   1016             __sbuf_->sbumpc();
   1017             return *this;
   1018         }
   1019     _LIBCPP_INLINE_VISIBILITY __proxy              operator++(int)
   1020         {
   1021             return __proxy(__sbuf_->sbumpc(), __sbuf_);
   1022         }
   1023 
   1024     _LIBCPP_INLINE_VISIBILITY bool equal(const istreambuf_iterator& __b) const
   1025         {return __test_for_eof() == __b.__test_for_eof();}
   1026 };
   1027 
   1028 template <class _CharT, class _Traits>
   1029 inline _LIBCPP_INLINE_VISIBILITY
   1030 bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,
   1031                 const istreambuf_iterator<_CharT,_Traits>& __b)
   1032                 {return __a.equal(__b);}
   1033 
   1034 template <class _CharT, class _Traits>
   1035 inline _LIBCPP_INLINE_VISIBILITY
   1036 bool operator!=(const istreambuf_iterator<_CharT,_Traits>& __a,
   1037                 const istreambuf_iterator<_CharT,_Traits>& __b)
   1038                 {return !__a.equal(__b);}
   1039 
   1040 template <class _CharT, class _Traits>
   1041 class _LIBCPP_TEMPLATE_VIS ostreambuf_iterator
   1042     : public iterator<output_iterator_tag, void, void, void, void>
   1043 {
   1044 public:
   1045     typedef output_iterator_tag                 iterator_category;
   1046     typedef void                                value_type;
   1047 #if _LIBCPP_STD_VER > 17
   1048     typedef std::ptrdiff_t                      difference_type;
   1049 #else
   1050     typedef void                                difference_type;
   1051 #endif
   1052     typedef void                                pointer;
   1053     typedef void                                reference;
   1054     typedef _CharT                              char_type;
   1055     typedef _Traits                             traits_type;
   1056     typedef basic_streambuf<_CharT, _Traits>    streambuf_type;
   1057     typedef basic_ostream<_CharT, _Traits>      ostream_type;
   1058 
   1059 private:
   1060     streambuf_type* __sbuf_;
   1061 public:
   1062     _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(ostream_type& __s) _NOEXCEPT
   1063         : __sbuf_(__s.rdbuf()) {}
   1064     _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(streambuf_type* __s) _NOEXCEPT
   1065         : __sbuf_(__s) {}
   1066     _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator=(_CharT __c)
   1067         {
   1068             if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sputc(__c), traits_type::eof()))
   1069                 __sbuf_ = nullptr;
   1070             return *this;
   1071         }
   1072     _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator*()     {return *this;}
   1073     _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++()    {return *this;}
   1074     _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++(int) {return *this;}
   1075     _LIBCPP_INLINE_VISIBILITY bool failed() const _NOEXCEPT {return __sbuf_ == nullptr;}
   1076 
   1077     template <class _Ch, class _Tr>
   1078     friend
   1079     _LIBCPP_HIDDEN
   1080     ostreambuf_iterator<_Ch, _Tr>
   1081     __pad_and_output(ostreambuf_iterator<_Ch, _Tr> __s,
   1082                      const _Ch* __ob, const _Ch* __op, const _Ch* __oe,
   1083                      ios_base& __iob, _Ch __fl);
   1084 };
   1085 
   1086 template <class _Iter>
   1087 class _LIBCPP_TEMPLATE_VIS move_iterator
   1088 {
   1089 private:
   1090     _Iter __i;
   1091 public:
   1092     typedef _Iter                                            iterator_type;
   1093     typedef typename iterator_traits<iterator_type>::value_type value_type;
   1094     typedef typename iterator_traits<iterator_type>::difference_type difference_type;
   1095     typedef iterator_type pointer;
   1096     typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
   1097         random_access_iterator_tag,
   1098         typename iterator_traits<_Iter>::iterator_category>  iterator_category;
   1099 #if _LIBCPP_STD_VER > 17
   1100     typedef input_iterator_tag                               iterator_concept;
   1101 #endif
   1102 
   1103 #ifndef _LIBCPP_CXX03_LANG
   1104     typedef typename iterator_traits<iterator_type>::reference __reference;
   1105     typedef typename conditional<
   1106             is_reference<__reference>::value,
   1107             typename remove_reference<__reference>::type&&,
   1108             __reference
   1109         >::type reference;
   1110 #else
   1111     typedef typename iterator_traits<iterator_type>::reference reference;
   1112 #endif
   1113 
   1114     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1115     move_iterator() : __i() {}
   1116     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1117     explicit move_iterator(_Iter __x) : __i(__x) {}
   1118     template <class _Up>
   1119       _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1120       move_iterator(const move_iterator<_Up>& __u) : __i(__u.base()) {}
   1121     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 _Iter base() const {return __i;}
   1122     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1123     reference operator*() const { return static_cast<reference>(*__i); }
   1124     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1125     pointer  operator->() const { return __i;}
   1126     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1127     move_iterator& operator++() {++__i; return *this;}
   1128     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1129     move_iterator  operator++(int) {move_iterator __tmp(*this); ++__i; return __tmp;}
   1130     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1131     move_iterator& operator--() {--__i; return *this;}
   1132     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1133     move_iterator  operator--(int) {move_iterator __tmp(*this); --__i; return __tmp;}
   1134     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1135     move_iterator  operator+ (difference_type __n) const {return move_iterator(__i + __n);}
   1136     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1137     move_iterator& operator+=(difference_type __n) {__i += __n; return *this;}
   1138     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1139     move_iterator  operator- (difference_type __n) const {return move_iterator(__i - __n);}
   1140     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1141     move_iterator& operator-=(difference_type __n) {__i -= __n; return *this;}
   1142     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1143     reference operator[](difference_type __n) const { return static_cast<reference>(__i[__n]); }
   1144 };
   1145 
   1146 template <class _Iter1, class _Iter2>
   1147 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1148 bool
   1149 operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
   1150 {
   1151     return __x.base() == __y.base();
   1152 }
   1153 
   1154 template <class _Iter1, class _Iter2>
   1155 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1156 bool
   1157 operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
   1158 {
   1159     return __x.base() < __y.base();
   1160 }
   1161 
   1162 template <class _Iter1, class _Iter2>
   1163 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1164 bool
   1165 operator!=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
   1166 {
   1167     return __x.base() != __y.base();
   1168 }
   1169 
   1170 template <class _Iter1, class _Iter2>
   1171 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1172 bool
   1173 operator>(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
   1174 {
   1175     return __x.base() > __y.base();
   1176 }
   1177 
   1178 template <class _Iter1, class _Iter2>
   1179 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1180 bool
   1181 operator>=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
   1182 {
   1183     return __x.base() >= __y.base();
   1184 }
   1185 
   1186 template <class _Iter1, class _Iter2>
   1187 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1188 bool
   1189 operator<=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
   1190 {
   1191     return __x.base() <= __y.base();
   1192 }
   1193 
   1194 #ifndef _LIBCPP_CXX03_LANG
   1195 template <class _Iter1, class _Iter2>
   1196 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1197 auto
   1198 operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
   1199 -> decltype(__x.base() - __y.base())
   1200 {
   1201     return __x.base() - __y.base();
   1202 }
   1203 #else
   1204 template <class _Iter1, class _Iter2>
   1205 inline _LIBCPP_INLINE_VISIBILITY
   1206 typename move_iterator<_Iter1>::difference_type
   1207 operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
   1208 {
   1209     return __x.base() - __y.base();
   1210 }
   1211 #endif
   1212 
   1213 template <class _Iter>
   1214 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1215 move_iterator<_Iter>
   1216 operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
   1217 {
   1218     return move_iterator<_Iter>(__x.base() + __n);
   1219 }
   1220 
   1221 template <class _Iter>
   1222 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1223 move_iterator<_Iter>
   1224 make_move_iterator(_Iter __i)
   1225 {
   1226     return move_iterator<_Iter>(__i);
   1227 }
   1228 
   1229 // __wrap_iter
   1230 
   1231 template <class _Iter> class __wrap_iter;
   1232 
   1233 template <class _Iter1, class _Iter2>
   1234 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1235 bool
   1236 operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
   1237 
   1238 template <class _Iter1, class _Iter2>
   1239 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1240 bool
   1241 operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
   1242 
   1243 template <class _Iter1, class _Iter2>
   1244 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1245 bool
   1246 operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
   1247 
   1248 template <class _Iter1, class _Iter2>
   1249 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1250 bool
   1251 operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
   1252 
   1253 template <class _Iter1, class _Iter2>
   1254 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1255 bool
   1256 operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
   1257 
   1258 template <class _Iter1, class _Iter2>
   1259 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1260 bool
   1261 operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
   1262 
   1263 #ifndef _LIBCPP_CXX03_LANG
   1264 template <class _Iter1, class _Iter2>
   1265 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1266 auto
   1267 operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
   1268 -> decltype(__x.base() - __y.base());
   1269 #else
   1270 template <class _Iter1, class _Iter2>
   1271 _LIBCPP_INLINE_VISIBILITY
   1272 typename __wrap_iter<_Iter1>::difference_type
   1273 operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
   1274 #endif
   1275 
   1276 template <class _Iter>
   1277 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1278 __wrap_iter<_Iter>
   1279 operator+(typename __wrap_iter<_Iter>::difference_type, __wrap_iter<_Iter>) _NOEXCEPT;
   1280 
   1281 template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 copy(_Ip, _Ip, _Op);
   1282 template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 copy_backward(_B1, _B1, _B2);
   1283 template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 move(_Ip, _Ip, _Op);
   1284 template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 move_backward(_B1, _B1, _B2);
   1285 
   1286 template <class _Iter>
   1287 class __wrap_iter
   1288 {
   1289 public:
   1290     typedef _Iter                                                      iterator_type;
   1291     typedef typename iterator_traits<iterator_type>::value_type        value_type;
   1292     typedef typename iterator_traits<iterator_type>::difference_type   difference_type;
   1293     typedef typename iterator_traits<iterator_type>::pointer           pointer;
   1294     typedef typename iterator_traits<iterator_type>::reference         reference;
   1295     typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
   1296 #if _LIBCPP_STD_VER > 17
   1297     typedef _If<__is_cpp17_contiguous_iterator<_Iter>::value,
   1298                 contiguous_iterator_tag, iterator_category>            iterator_concept;
   1299 #endif
   1300 
   1301 private:
   1302     iterator_type __i;
   1303 public:
   1304     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter() _NOEXCEPT
   1305 #if _LIBCPP_STD_VER > 11
   1306                 : __i{}
   1307 #endif
   1308     {
   1309 #if _LIBCPP_DEBUG_LEVEL == 2
   1310         __get_db()->__insert_i(this);
   1311 #endif
   1312     }
   1313     template <class _Up> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1314         __wrap_iter(const __wrap_iter<_Up>& __u,
   1315             typename enable_if<is_convertible<_Up, iterator_type>::value>::type* = nullptr) _NOEXCEPT
   1316             : __i(__u.base())
   1317     {
   1318 #if _LIBCPP_DEBUG_LEVEL == 2
   1319         __get_db()->__iterator_copy(this, &__u);
   1320 #endif
   1321     }
   1322 #if _LIBCPP_DEBUG_LEVEL == 2
   1323     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1324     __wrap_iter(const __wrap_iter& __x)
   1325         : __i(__x.base())
   1326     {
   1327         __get_db()->__iterator_copy(this, &__x);
   1328     }
   1329     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1330     __wrap_iter& operator=(const __wrap_iter& __x)
   1331     {
   1332         if (this != &__x)
   1333         {
   1334             __get_db()->__iterator_copy(this, &__x);
   1335             __i = __x.__i;
   1336         }
   1337         return *this;
   1338     }
   1339     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1340     ~__wrap_iter()
   1341     {
   1342         __get_db()->__erase_i(this);
   1343     }
   1344 #endif
   1345     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference operator*() const _NOEXCEPT
   1346     {
   1347 #if _LIBCPP_DEBUG_LEVEL == 2
   1348         _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
   1349                        "Attempted to dereference a non-dereferenceable iterator");
   1350 #endif
   1351         return *__i;
   1352     }
   1353     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG pointer  operator->() const _NOEXCEPT
   1354     {
   1355 #if _LIBCPP_DEBUG_LEVEL == 2
   1356         _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
   1357                        "Attempted to dereference a non-dereferenceable iterator");
   1358 #endif
   1359         return _VSTD::__to_address(__i);
   1360     }
   1361     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator++() _NOEXCEPT
   1362     {
   1363 #if _LIBCPP_DEBUG_LEVEL == 2
   1364         _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
   1365                        "Attempted to increment a non-incrementable iterator");
   1366 #endif
   1367         ++__i;
   1368         return *this;
   1369     }
   1370     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter  operator++(int) _NOEXCEPT
   1371         {__wrap_iter __tmp(*this); ++(*this); return __tmp;}
   1372 
   1373     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator--() _NOEXCEPT
   1374     {
   1375 #if _LIBCPP_DEBUG_LEVEL == 2
   1376         _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
   1377                        "Attempted to decrement a non-decrementable iterator");
   1378 #endif
   1379         --__i;
   1380         return *this;
   1381     }
   1382     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter  operator--(int) _NOEXCEPT
   1383         {__wrap_iter __tmp(*this); --(*this); return __tmp;}
   1384     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter  operator+ (difference_type __n) const _NOEXCEPT
   1385         {__wrap_iter __w(*this); __w += __n; return __w;}
   1386     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator+=(difference_type __n) _NOEXCEPT
   1387     {
   1388 #if _LIBCPP_DEBUG_LEVEL == 2
   1389         _LIBCPP_ASSERT(__get_const_db()->__addable(this, __n),
   1390                    "Attempted to add/subtract an iterator outside its valid range");
   1391 #endif
   1392         __i += __n;
   1393         return *this;
   1394     }
   1395     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter  operator- (difference_type __n) const _NOEXCEPT
   1396         {return *this + (-__n);}
   1397     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator-=(difference_type __n) _NOEXCEPT
   1398         {*this += -__n; return *this;}
   1399     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference    operator[](difference_type __n) const _NOEXCEPT
   1400     {
   1401 #if _LIBCPP_DEBUG_LEVEL == 2
   1402         _LIBCPP_ASSERT(__get_const_db()->__subscriptable(this, __n),
   1403                    "Attempted to subscript an iterator outside its valid range");
   1404 #endif
   1405         return __i[__n];
   1406     }
   1407 
   1408     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG iterator_type base() const _NOEXCEPT {return __i;}
   1409 
   1410 private:
   1411 #if _LIBCPP_DEBUG_LEVEL == 2
   1412     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(const void* __p, iterator_type __x) : __i(__x)
   1413     {
   1414         __get_db()->__insert_ic(this, __p);
   1415     }
   1416 #else
   1417     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(iterator_type __x) _NOEXCEPT : __i(__x) {}
   1418 #endif
   1419 
   1420     template <class _Up> friend class __wrap_iter;
   1421     template <class _CharT, class _Traits, class _Alloc> friend class basic_string;
   1422     template <class _Tp, class _Alloc> friend class _LIBCPP_TEMPLATE_VIS vector;
   1423     template <class _Tp, size_t> friend class _LIBCPP_TEMPLATE_VIS span;
   1424 
   1425     template <class _Iter1, class _Iter2>
   1426     _LIBCPP_CONSTEXPR_IF_NODEBUG friend
   1427     bool
   1428     operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
   1429 
   1430     template <class _Iter1, class _Iter2>
   1431     _LIBCPP_CONSTEXPR_IF_NODEBUG friend
   1432     bool
   1433     operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
   1434 
   1435     template <class _Iter1, class _Iter2>
   1436     _LIBCPP_CONSTEXPR_IF_NODEBUG friend
   1437     bool
   1438     operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
   1439 
   1440     template <class _Iter1, class _Iter2>
   1441     _LIBCPP_CONSTEXPR_IF_NODEBUG friend
   1442     bool
   1443     operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
   1444 
   1445     template <class _Iter1, class _Iter2>
   1446     _LIBCPP_CONSTEXPR_IF_NODEBUG friend
   1447     bool
   1448     operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
   1449 
   1450     template <class _Iter1, class _Iter2>
   1451     _LIBCPP_CONSTEXPR_IF_NODEBUG friend
   1452     bool
   1453     operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
   1454 
   1455 #ifndef _LIBCPP_CXX03_LANG
   1456     template <class _Iter1, class _Iter2>
   1457     _LIBCPP_CONSTEXPR_IF_NODEBUG friend
   1458     auto
   1459     operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
   1460     -> decltype(__x.base() - __y.base());
   1461 #else
   1462     template <class _Iter1, class _Iter2>
   1463     _LIBCPP_CONSTEXPR_IF_NODEBUG friend
   1464     typename __wrap_iter<_Iter1>::difference_type
   1465     operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
   1466 #endif
   1467 
   1468     template <class _Iter1>
   1469     _LIBCPP_CONSTEXPR_IF_NODEBUG friend
   1470     __wrap_iter<_Iter1>
   1471     operator+(typename __wrap_iter<_Iter1>::difference_type, __wrap_iter<_Iter1>) _NOEXCEPT;
   1472 };
   1473 
   1474 #if _LIBCPP_STD_VER <= 17
   1475 template <class _It>
   1476 struct __is_cpp17_contiguous_iterator<__wrap_iter<_It> > : __is_cpp17_contiguous_iterator<_It> {};
   1477 #endif
   1478 
   1479 template <class _Iter>
   1480 _LIBCPP_CONSTEXPR
   1481 _EnableIf<__is_cpp17_contiguous_iterator<_Iter>::value, decltype(_VSTD::__to_address(declval<_Iter>()))>
   1482 __to_address(__wrap_iter<_Iter> __w) _NOEXCEPT {
   1483     return _VSTD::__to_address(__w.base());
   1484 }
   1485 
   1486 template <class _Iter1, class _Iter2>
   1487 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1488 bool
   1489 operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
   1490 {
   1491     return __x.base() == __y.base();
   1492 }
   1493 
   1494 template <class _Iter1, class _Iter2>
   1495 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1496 bool
   1497 operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
   1498 {
   1499 #if _LIBCPP_DEBUG_LEVEL == 2
   1500     _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
   1501                    "Attempted to compare incomparable iterators");
   1502 #endif
   1503     return __x.base() < __y.base();
   1504 }
   1505 
   1506 template <class _Iter1, class _Iter2>
   1507 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1508 bool
   1509 operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
   1510 {
   1511     return !(__x == __y);
   1512 }
   1513 
   1514 template <class _Iter1, class _Iter2>
   1515 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1516 bool
   1517 operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
   1518 {
   1519     return __y < __x;
   1520 }
   1521 
   1522 template <class _Iter1, class _Iter2>
   1523 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1524 bool
   1525 operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
   1526 {
   1527     return !(__x < __y);
   1528 }
   1529 
   1530 template <class _Iter1, class _Iter2>
   1531 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1532 bool
   1533 operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
   1534 {
   1535     return !(__y < __x);
   1536 }
   1537 
   1538 template <class _Iter1>
   1539 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1540 bool
   1541 operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
   1542 {
   1543     return !(__x == __y);
   1544 }
   1545 
   1546 template <class _Iter1>
   1547 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1548 bool
   1549 operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
   1550 {
   1551     return __y < __x;
   1552 }
   1553 
   1554 template <class _Iter1>
   1555 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1556 bool
   1557 operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
   1558 {
   1559     return !(__x < __y);
   1560 }
   1561 
   1562 template <class _Iter1>
   1563 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1564 bool
   1565 operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
   1566 {
   1567     return !(__y < __x);
   1568 }
   1569 
   1570 #ifndef _LIBCPP_CXX03_LANG
   1571 template <class _Iter1, class _Iter2>
   1572 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1573 auto
   1574 operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
   1575 -> decltype(__x.base() - __y.base())
   1576 {
   1577 #if _LIBCPP_DEBUG_LEVEL == 2
   1578     _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
   1579                    "Attempted to subtract incompatible iterators");
   1580 #endif
   1581     return __x.base() - __y.base();
   1582 }
   1583 #else
   1584 template <class _Iter1, class _Iter2>
   1585 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1586 typename __wrap_iter<_Iter1>::difference_type
   1587 operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
   1588 {
   1589 #if _LIBCPP_DEBUG_LEVEL == 2
   1590     _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
   1591                    "Attempted to subtract incompatible iterators");
   1592 #endif
   1593     return __x.base() - __y.base();
   1594 }
   1595 #endif
   1596 
   1597 template <class _Iter>
   1598 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
   1599 __wrap_iter<_Iter>
   1600 operator+(typename __wrap_iter<_Iter>::difference_type __n,
   1601           __wrap_iter<_Iter> __x) _NOEXCEPT
   1602 {
   1603     __x += __n;
   1604     return __x;
   1605 }
   1606 
   1607 template <class _Tp, size_t _Np>
   1608 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
   1609 _Tp*
   1610 begin(_Tp (&__array)[_Np])
   1611 {
   1612     return __array;
   1613 }
   1614 
   1615 template <class _Tp, size_t _Np>
   1616 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
   1617 _Tp*
   1618 end(_Tp (&__array)[_Np])
   1619 {
   1620     return __array + _Np;
   1621 }
   1622 
   1623 #if !defined(_LIBCPP_CXX03_LANG)
   1624 
   1625 template <class _Cp>
   1626 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1627 auto
   1628 begin(_Cp& __c) -> decltype(__c.begin())
   1629 {
   1630     return __c.begin();
   1631 }
   1632 
   1633 template <class _Cp>
   1634 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1635 auto
   1636 begin(const _Cp& __c) -> decltype(__c.begin())
   1637 {
   1638     return __c.begin();
   1639 }
   1640 
   1641 template <class _Cp>
   1642 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1643 auto
   1644 end(_Cp& __c) -> decltype(__c.end())
   1645 {
   1646     return __c.end();
   1647 }
   1648 
   1649 template <class _Cp>
   1650 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1651 auto
   1652 end(const _Cp& __c) -> decltype(__c.end())
   1653 {
   1654     return __c.end();
   1655 }
   1656 
   1657 #if _LIBCPP_STD_VER > 11
   1658 
   1659 template <class _Tp, size_t _Np>
   1660 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1661 reverse_iterator<_Tp*> rbegin(_Tp (&__array)[_Np])
   1662 {
   1663     return reverse_iterator<_Tp*>(__array + _Np);
   1664 }
   1665 
   1666 template <class _Tp, size_t _Np>
   1667 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1668 reverse_iterator<_Tp*> rend(_Tp (&__array)[_Np])
   1669 {
   1670     return reverse_iterator<_Tp*>(__array);
   1671 }
   1672 
   1673 template <class _Ep>
   1674 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1675 reverse_iterator<const _Ep*> rbegin(initializer_list<_Ep> __il)
   1676 {
   1677     return reverse_iterator<const _Ep*>(__il.end());
   1678 }
   1679 
   1680 template <class _Ep>
   1681 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1682 reverse_iterator<const _Ep*> rend(initializer_list<_Ep> __il)
   1683 {
   1684     return reverse_iterator<const _Ep*>(__il.begin());
   1685 }
   1686 
   1687 template <class _Cp>
   1688 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
   1689 auto cbegin(const _Cp& __c) -> decltype(_VSTD::begin(__c))
   1690 {
   1691     return _VSTD::begin(__c);
   1692 }
   1693 
   1694 template <class _Cp>
   1695 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
   1696 auto cend(const _Cp& __c) -> decltype(_VSTD::end(__c))
   1697 {
   1698     return _VSTD::end(__c);
   1699 }
   1700 
   1701 template <class _Cp>
   1702 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1703 auto rbegin(_Cp& __c) -> decltype(__c.rbegin())
   1704 {
   1705     return __c.rbegin();
   1706 }
   1707 
   1708 template <class _Cp>
   1709 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1710 auto rbegin(const _Cp& __c) -> decltype(__c.rbegin())
   1711 {
   1712     return __c.rbegin();
   1713 }
   1714 
   1715 template <class _Cp>
   1716 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1717 auto rend(_Cp& __c) -> decltype(__c.rend())
   1718 {
   1719     return __c.rend();
   1720 }
   1721 
   1722 template <class _Cp>
   1723 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1724 auto rend(const _Cp& __c) -> decltype(__c.rend())
   1725 {
   1726     return __c.rend();
   1727 }
   1728 
   1729 template <class _Cp>
   1730 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1731 auto crbegin(const _Cp& __c) -> decltype(_VSTD::rbegin(__c))
   1732 {
   1733     return _VSTD::rbegin(__c);
   1734 }
   1735 
   1736 template <class _Cp>
   1737 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
   1738 auto crend(const _Cp& __c) -> decltype(_VSTD::rend(__c))
   1739 {
   1740     return _VSTD::rend(__c);
   1741 }
   1742 
   1743 #endif
   1744 
   1745 
   1746 #else  // defined(_LIBCPP_CXX03_LANG)
   1747 
   1748 template <class _Cp>
   1749 _LIBCPP_INLINE_VISIBILITY
   1750 typename _Cp::iterator
   1751 begin(_Cp& __c)
   1752 {
   1753     return __c.begin();
   1754 }
   1755 
   1756 template <class _Cp>
   1757 _LIBCPP_INLINE_VISIBILITY
   1758 typename _Cp::const_iterator
   1759 begin(const _Cp& __c)
   1760 {
   1761     return __c.begin();
   1762 }
   1763 
   1764 template <class _Cp>
   1765 _LIBCPP_INLINE_VISIBILITY
   1766 typename _Cp::iterator
   1767 end(_Cp& __c)
   1768 {
   1769     return __c.end();
   1770 }
   1771 
   1772 template <class _Cp>
   1773 _LIBCPP_INLINE_VISIBILITY
   1774 typename _Cp::const_iterator
   1775 end(const _Cp& __c)
   1776 {
   1777     return __c.end();
   1778 }
   1779 
   1780 #endif // !defined(_LIBCPP_CXX03_LANG)
   1781 
   1782 #if _LIBCPP_STD_VER > 14
   1783 
   1784 // #if _LIBCPP_STD_VER > 11
   1785 // template <>
   1786 // struct _LIBCPP_TEMPLATE_VIS plus<void>
   1787 // {
   1788 //     template <class _T1, class _T2>
   1789 //     _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
   1790 //     auto operator()(_T1&& __t, _T2&& __u) const
   1791 //     _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
   1792 //     -> decltype        (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
   1793 //         { return        _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
   1794 //     typedef void is_transparent;
   1795 // };
   1796 // #endif
   1797 
   1798 template <class _Cont>
   1799 _LIBCPP_INLINE_VISIBILITY
   1800 constexpr auto size(const _Cont& __c)
   1801 _NOEXCEPT_(noexcept(__c.size()))
   1802 -> decltype        (__c.size())
   1803 { return            __c.size(); }
   1804 
   1805 template <class _Tp, size_t _Sz>
   1806 _LIBCPP_INLINE_VISIBILITY
   1807 constexpr size_t size(const _Tp (&)[_Sz]) noexcept { return _Sz; }
   1808 
   1809 #if _LIBCPP_STD_VER > 17
   1810 template <class _Cont>
   1811 _LIBCPP_INLINE_VISIBILITY
   1812 constexpr auto ssize(const _Cont& __c)
   1813 _NOEXCEPT_(noexcept(static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size())))
   1814 ->                              common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>
   1815 { return            static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size()); }
   1816 
   1817 template <class _Tp, ptrdiff_t _Sz>
   1818 _LIBCPP_INLINE_VISIBILITY
   1819 constexpr ptrdiff_t ssize(const _Tp (&)[_Sz]) noexcept { return _Sz; }
   1820 #endif
   1821 
   1822 template <class _Cont>
   1823 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
   1824 constexpr auto empty(const _Cont& __c)
   1825 _NOEXCEPT_(noexcept(__c.empty()))
   1826 -> decltype        (__c.empty())
   1827 { return            __c.empty(); }
   1828 
   1829 template <class _Tp, size_t _Sz>
   1830 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
   1831 constexpr bool empty(const _Tp (&)[_Sz]) noexcept { return false; }
   1832 
   1833 template <class _Ep>
   1834 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
   1835 constexpr bool empty(initializer_list<_Ep> __il) noexcept { return __il.size() == 0; }
   1836 
   1837 template <class _Cont> constexpr
   1838 _LIBCPP_INLINE_VISIBILITY
   1839 auto data(_Cont& __c)
   1840 _NOEXCEPT_(noexcept(__c.data()))
   1841 -> decltype        (__c.data())
   1842 { return            __c.data(); }
   1843 
   1844 template <class _Cont> constexpr
   1845 _LIBCPP_INLINE_VISIBILITY
   1846 auto data(const _Cont& __c)
   1847 _NOEXCEPT_(noexcept(__c.data()))
   1848 -> decltype        (__c.data())
   1849 { return            __c.data(); }
   1850 
   1851 template <class _Tp, size_t _Sz>
   1852 _LIBCPP_INLINE_VISIBILITY
   1853 constexpr _Tp* data(_Tp (&__array)[_Sz]) noexcept { return __array; }
   1854 
   1855 template <class _Ep>
   1856 _LIBCPP_INLINE_VISIBILITY
   1857 constexpr const _Ep* data(initializer_list<_Ep> __il) noexcept { return __il.begin(); }
   1858 #endif
   1859 
   1860 template <class _Container, class _Predicate>
   1861 typename _Container::size_type
   1862 __libcpp_erase_if_container(_Container& __c, _Predicate& __pred) {
   1863   typename _Container::size_type __old_size = __c.size();
   1864 
   1865   const typename _Container::iterator __last = __c.end();
   1866   for (typename _Container::iterator __iter = __c.begin(); __iter != __last;) {
   1867     if (__pred(*__iter))
   1868       __iter = __c.erase(__iter);
   1869     else
   1870       ++__iter;
   1871   }
   1872 
   1873   return __old_size - __c.size();
   1874 }
   1875 
   1876 _LIBCPP_END_NAMESPACE_STD
   1877 
   1878 #endif // _LIBCPP_ITERATOR
   1879