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