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