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