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
541#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
542template<class _InputIterator>
543using __iter_value_type = typename iterator_traits<_InputIterator>::value_type;
544
545template<class _InputIterator>
546using __iter_key_type = remove_const_t<typename iterator_traits<_InputIterator>::value_type::first_type>;
547
548template<class _InputIterator>
549using __iter_mapped_type = typename iterator_traits<_InputIterator>::value_type::second_type;
550
551template<class _InputIterator>
552using __iter_to_alloc_type = pair<
553    add_const_t<typename iterator_traits<_InputIterator>::value_type::first_type>,
554    typename iterator_traits<_InputIterator>::value_type::second_type>;
555#endif
556
557template<class _Category, class _Tp, class _Distance = ptrdiff_t,
558         class _Pointer = _Tp*, class _Reference = _Tp&>
559struct _LIBCPP_TEMPLATE_VIS iterator
560{
561    typedef _Tp        value_type;
562    typedef _Distance  difference_type;
563    typedef _Pointer   pointer;
564    typedef _Reference reference;
565    typedef _Category  iterator_category;
566};
567
568template <class _InputIter>
569inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
570void __advance(_InputIter& __i,
571             typename iterator_traits<_InputIter>::difference_type __n, input_iterator_tag)
572{
573    for (; __n > 0; --__n)
574        ++__i;
575}
576
577template <class _BiDirIter>
578inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
579void __advance(_BiDirIter& __i,
580             typename iterator_traits<_BiDirIter>::difference_type __n, bidirectional_iterator_tag)
581{
582    if (__n >= 0)
583        for (; __n > 0; --__n)
584            ++__i;
585    else
586        for (; __n < 0; ++__n)
587            --__i;
588}
589
590template <class _RandIter>
591inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
592void __advance(_RandIter& __i,
593             typename iterator_traits<_RandIter>::difference_type __n, random_access_iterator_tag)
594{
595   __i += __n;
596}
597
598template <class _InputIter>
599inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
600void advance(_InputIter& __i,
601             typename iterator_traits<_InputIter>::difference_type __n)
602{
603    _LIBCPP_ASSERT(__n >= 0 || __is_bidirectional_iterator<_InputIter>::value,
604                       "Attempt to advance(it, -n) on a non-bidi iterator");
605    __advance(__i, __n, typename iterator_traits<_InputIter>::iterator_category());
606}
607
608template <class _InputIter>
609inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
610typename iterator_traits<_InputIter>::difference_type
611__distance(_InputIter __first, _InputIter __last, input_iterator_tag)
612{
613    typename iterator_traits<_InputIter>::difference_type __r(0);
614    for (; __first != __last; ++__first)
615        ++__r;
616    return __r;
617}
618
619template <class _RandIter>
620inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
621typename iterator_traits<_RandIter>::difference_type
622__distance(_RandIter __first, _RandIter __last, random_access_iterator_tag)
623{
624    return __last - __first;
625}
626
627template <class _InputIter>
628inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
629typename iterator_traits<_InputIter>::difference_type
630distance(_InputIter __first, _InputIter __last)
631{
632    return __distance(__first, __last, typename iterator_traits<_InputIter>::iterator_category());
633}
634
635template <class _InputIter>
636inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
637typename enable_if
638<
639    __is_input_iterator<_InputIter>::value,
640    _InputIter
641>::type
642next(_InputIter __x,
643     typename iterator_traits<_InputIter>::difference_type __n = 1)
644{
645    _LIBCPP_ASSERT(__n >= 0 || __is_bidirectional_iterator<_InputIter>::value,
646                       "Attempt to next(it, -n) on a non-bidi iterator");
647
648    _VSTD::advance(__x, __n);
649    return __x;
650}
651
652template <class _InputIter>
653inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
654typename enable_if
655<
656    __is_input_iterator<_InputIter>::value,
657    _InputIter
658>::type
659prev(_InputIter __x,
660     typename iterator_traits<_InputIter>::difference_type __n = 1)
661{
662    _LIBCPP_ASSERT(__n <= 0 || __is_bidirectional_iterator<_InputIter>::value,
663                       "Attempt to prev(it, +n) on a non-bidi iterator");
664    _VSTD::advance(__x, -__n);
665    return __x;
666}
667
668
669template <class _Tp, class = void>
670struct __is_stashing_iterator : false_type {};
671
672template <class _Tp>
673struct __is_stashing_iterator<_Tp, typename __void_t<typename _Tp::__stashing_iterator_tag>::type>
674  : true_type {};
675
676template <class _Iter>
677class _LIBCPP_TEMPLATE_VIS reverse_iterator
678    : public iterator<typename iterator_traits<_Iter>::iterator_category,
679                      typename iterator_traits<_Iter>::value_type,
680                      typename iterator_traits<_Iter>::difference_type,
681                      typename iterator_traits<_Iter>::pointer,
682                      typename iterator_traits<_Iter>::reference>
683{
684private:
685    /*mutable*/ _Iter __t;  // no longer used as of LWG #2360, not removed due to ABI break
686
687    static_assert(!__is_stashing_iterator<_Iter>::value,
688      "The specified iterator type cannot be used with reverse_iterator; "
689      "Using stashing iterators with reverse_iterator causes undefined behavior");
690
691protected:
692    _Iter current;
693public:
694    typedef _Iter                                            iterator_type;
695    typedef typename iterator_traits<_Iter>::difference_type difference_type;
696    typedef typename iterator_traits<_Iter>::reference       reference;
697    typedef typename iterator_traits<_Iter>::pointer         pointer;
698
699    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
700    reverse_iterator() : __t(), current() {}
701    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
702    explicit reverse_iterator(_Iter __x) : __t(__x), current(__x) {}
703    template <class _Up>
704        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
705        reverse_iterator(const reverse_iterator<_Up>& __u) : __t(__u.base()), current(__u.base()) {}
706    template <class _Up>
707        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
708        reverse_iterator& operator=(const reverse_iterator<_Up>& __u)
709            { __t = current = __u.base(); return *this; }
710    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
711    _Iter base() const {return current;}
712    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
713    reference operator*() const {_Iter __tmp = current; return *--__tmp;}
714    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
715    pointer  operator->() const {return _VSTD::addressof(operator*());}
716    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
717    reverse_iterator& operator++() {--current; return *this;}
718    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
719    reverse_iterator  operator++(int) {reverse_iterator __tmp(*this); --current; return __tmp;}
720    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
721    reverse_iterator& operator--() {++current; return *this;}
722    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
723    reverse_iterator  operator--(int) {reverse_iterator __tmp(*this); ++current; return __tmp;}
724    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
725    reverse_iterator  operator+ (difference_type __n) const {return reverse_iterator(current - __n);}
726    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
727    reverse_iterator& operator+=(difference_type __n) {current -= __n; return *this;}
728    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
729    reverse_iterator  operator- (difference_type __n) const {return reverse_iterator(current + __n);}
730    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
731    reverse_iterator& operator-=(difference_type __n) {current += __n; return *this;}
732    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
733    reference         operator[](difference_type __n) const {return *(*this + __n);}
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
768template <class _Iter1, class _Iter2>
769inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
770bool
771operator>=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
772{
773    return __x.base() <= __y.base();
774}
775
776template <class _Iter1, class _Iter2>
777inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
778bool
779operator<=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
780{
781    return __x.base() >= __y.base();
782}
783
784#ifndef _LIBCPP_CXX03_LANG
785template <class _Iter1, class _Iter2>
786inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
787auto
788operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
789-> decltype(__y.base() - __x.base())
790{
791    return __y.base() - __x.base();
792}
793#else
794template <class _Iter1, class _Iter2>
795inline _LIBCPP_INLINE_VISIBILITY
796typename reverse_iterator<_Iter1>::difference_type
797operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
798{
799    return __y.base() - __x.base();
800}
801#endif
802
803template <class _Iter>
804inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
805reverse_iterator<_Iter>
806operator+(typename reverse_iterator<_Iter>::difference_type __n, const reverse_iterator<_Iter>& __x)
807{
808    return reverse_iterator<_Iter>(__x.base() - __n);
809}
810
811#if _LIBCPP_STD_VER > 11
812template <class _Iter>
813inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
814reverse_iterator<_Iter> make_reverse_iterator(_Iter __i)
815{
816    return reverse_iterator<_Iter>(__i);
817}
818#endif
819
820template <class _Container>
821class _LIBCPP_TEMPLATE_VIS back_insert_iterator
822    : public iterator<output_iterator_tag,
823                      void,
824                      void,
825                      void,
826                      void>
827{
828protected:
829    _Container* container;
830public:
831    typedef _Container container_type;
832
833    _LIBCPP_INLINE_VISIBILITY explicit back_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
834    _LIBCPP_INLINE_VISIBILITY back_insert_iterator& operator=(const typename _Container::value_type& __value_)
835        {container->push_back(__value_); return *this;}
836#ifndef _LIBCPP_CXX03_LANG
837    _LIBCPP_INLINE_VISIBILITY back_insert_iterator& operator=(typename _Container::value_type&& __value_)
838        {container->push_back(_VSTD::move(__value_)); return *this;}
839#endif  // _LIBCPP_CXX03_LANG
840    _LIBCPP_INLINE_VISIBILITY back_insert_iterator& operator*()     {return *this;}
841    _LIBCPP_INLINE_VISIBILITY back_insert_iterator& operator++()    {return *this;}
842    _LIBCPP_INLINE_VISIBILITY back_insert_iterator  operator++(int) {return *this;}
843};
844_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(back_insert_iterator);
845
846template <class _Container>
847inline _LIBCPP_INLINE_VISIBILITY
848back_insert_iterator<_Container>
849back_inserter(_Container& __x)
850{
851    return back_insert_iterator<_Container>(__x);
852}
853
854template <class _Container>
855class _LIBCPP_TEMPLATE_VIS front_insert_iterator
856    : public iterator<output_iterator_tag,
857                      void,
858                      void,
859                      void,
860                      void>
861{
862protected:
863    _Container* container;
864public:
865    typedef _Container container_type;
866
867    _LIBCPP_INLINE_VISIBILITY explicit front_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
868    _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator=(const typename _Container::value_type& __value_)
869        {container->push_front(__value_); return *this;}
870#ifndef _LIBCPP_CXX03_LANG
871    _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator=(typename _Container::value_type&& __value_)
872        {container->push_front(_VSTD::move(__value_)); return *this;}
873#endif  // _LIBCPP_CXX03_LANG
874    _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator*()     {return *this;}
875    _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator++()    {return *this;}
876    _LIBCPP_INLINE_VISIBILITY front_insert_iterator  operator++(int) {return *this;}
877};
878
879template <class _Container>
880inline _LIBCPP_INLINE_VISIBILITY
881front_insert_iterator<_Container>
882front_inserter(_Container& __x)
883{
884    return front_insert_iterator<_Container>(__x);
885}
886
887template <class _Container>
888class _LIBCPP_TEMPLATE_VIS insert_iterator
889    : public iterator<output_iterator_tag,
890                      void,
891                      void,
892                      void,
893                      void>
894{
895protected:
896    _Container* container;
897    typename _Container::iterator iter;
898public:
899    typedef _Container container_type;
900
901    _LIBCPP_INLINE_VISIBILITY insert_iterator(_Container& __x, typename _Container::iterator __i)
902        : container(_VSTD::addressof(__x)), iter(__i) {}
903    _LIBCPP_INLINE_VISIBILITY insert_iterator& operator=(const typename _Container::value_type& __value_)
904        {iter = container->insert(iter, __value_); ++iter; return *this;}
905#ifndef _LIBCPP_CXX03_LANG
906    _LIBCPP_INLINE_VISIBILITY insert_iterator& operator=(typename _Container::value_type&& __value_)
907        {iter = container->insert(iter, _VSTD::move(__value_)); ++iter; return *this;}
908#endif  // _LIBCPP_CXX03_LANG
909    _LIBCPP_INLINE_VISIBILITY insert_iterator& operator*()        {return *this;}
910    _LIBCPP_INLINE_VISIBILITY insert_iterator& operator++()       {return *this;}
911    _LIBCPP_INLINE_VISIBILITY insert_iterator& operator++(int)    {return *this;}
912};
913
914template <class _Container>
915inline _LIBCPP_INLINE_VISIBILITY
916insert_iterator<_Container>
917inserter(_Container& __x, typename _Container::iterator __i)
918{
919    return insert_iterator<_Container>(__x, __i);
920}
921
922template <class _Tp, class _CharT = char,
923          class _Traits = char_traits<_CharT>, class _Distance = ptrdiff_t>
924class _LIBCPP_TEMPLATE_VIS istream_iterator
925    : public iterator<input_iterator_tag, _Tp, _Distance, const _Tp*, const _Tp&>
926{
927public:
928    typedef _CharT char_type;
929    typedef _Traits traits_type;
930    typedef basic_istream<_CharT,_Traits> istream_type;
931private:
932    istream_type* __in_stream_;
933    _Tp __value_;
934public:
935    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istream_iterator() : __in_stream_(0), __value_() {}
936    _LIBCPP_INLINE_VISIBILITY istream_iterator(istream_type& __s) : __in_stream_(_VSTD::addressof(__s))
937        {
938            if (!(*__in_stream_ >> __value_))
939                __in_stream_ = 0;
940        }
941
942    _LIBCPP_INLINE_VISIBILITY const _Tp& operator*() const {return __value_;}
943    _LIBCPP_INLINE_VISIBILITY const _Tp* operator->() const {return _VSTD::addressof((operator*()));}
944    _LIBCPP_INLINE_VISIBILITY istream_iterator& operator++()
945        {
946            if (!(*__in_stream_ >> __value_))
947                __in_stream_ = 0;
948            return *this;
949        }
950    _LIBCPP_INLINE_VISIBILITY istream_iterator  operator++(int)
951        {istream_iterator __t(*this); ++(*this); return __t;}
952
953    template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
954    friend _LIBCPP_INLINE_VISIBILITY
955    bool
956    operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
957               const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
958
959    template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
960    friend _LIBCPP_INLINE_VISIBILITY
961    bool
962    operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
963               const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
964};
965
966template <class _Tp, class _CharT, class _Traits, class _Distance>
967inline _LIBCPP_INLINE_VISIBILITY
968bool
969operator==(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
970           const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
971{
972    return __x.__in_stream_ == __y.__in_stream_;
973}
974
975template <class _Tp, class _CharT, class _Traits, class _Distance>
976inline _LIBCPP_INLINE_VISIBILITY
977bool
978operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
979           const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
980{
981    return !(__x == __y);
982}
983
984template <class _Tp, class _CharT = char, class _Traits = char_traits<_CharT> >
985class _LIBCPP_TEMPLATE_VIS ostream_iterator
986    : public iterator<output_iterator_tag, void, void, void, void>
987{
988public:
989    typedef _CharT char_type;
990    typedef _Traits traits_type;
991    typedef basic_ostream<_CharT,_Traits> ostream_type;
992private:
993    ostream_type* __out_stream_;
994    const char_type* __delim_;
995public:
996    _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s) _NOEXCEPT
997        : __out_stream_(_VSTD::addressof(__s)), __delim_(0) {}
998    _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s, const _CharT* __delimiter) _NOEXCEPT
999        : __out_stream_(_VSTD::addressof(__s)), __delim_(__delimiter) {}
1000    _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator=(const _Tp& __value_)
1001        {
1002            *__out_stream_ << __value_;
1003            if (__delim_)
1004                *__out_stream_ << __delim_;
1005            return *this;
1006        }
1007
1008    _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator*()     {return *this;}
1009    _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++()    {return *this;}
1010    _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++(int) {return *this;}
1011};
1012
1013template<class _CharT, class _Traits>
1014class _LIBCPP_TEMPLATE_VIS istreambuf_iterator
1015    : public iterator<input_iterator_tag, _CharT,
1016                      typename _Traits::off_type, _CharT*,
1017                      _CharT>
1018{
1019public:
1020    typedef _CharT                          char_type;
1021    typedef _Traits                         traits_type;
1022    typedef typename _Traits::int_type      int_type;
1023    typedef basic_streambuf<_CharT,_Traits> streambuf_type;
1024    typedef basic_istream<_CharT,_Traits>   istream_type;
1025private:
1026    mutable streambuf_type* __sbuf_;
1027
1028    class __proxy
1029    {
1030        char_type __keep_;
1031        streambuf_type* __sbuf_;
1032        _LIBCPP_INLINE_VISIBILITY __proxy(char_type __c, streambuf_type* __s)
1033            : __keep_(__c), __sbuf_(__s) {}
1034        friend class istreambuf_iterator;
1035    public:
1036        _LIBCPP_INLINE_VISIBILITY char_type operator*() const {return __keep_;}
1037    };
1038
1039    _LIBCPP_INLINE_VISIBILITY
1040    bool __test_for_eof() const
1041    {
1042        if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof()))
1043            __sbuf_ = 0;
1044        return __sbuf_ == 0;
1045    }
1046public:
1047    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istreambuf_iterator() _NOEXCEPT : __sbuf_(0) {}
1048    _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(istream_type& __s) _NOEXCEPT
1049        : __sbuf_(__s.rdbuf()) {}
1050    _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(streambuf_type* __s) _NOEXCEPT
1051        : __sbuf_(__s) {}
1052    _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(const __proxy& __p) _NOEXCEPT
1053        : __sbuf_(__p.__sbuf_) {}
1054
1055    _LIBCPP_INLINE_VISIBILITY char_type  operator*() const
1056        {return static_cast<char_type>(__sbuf_->sgetc());}
1057    _LIBCPP_INLINE_VISIBILITY istreambuf_iterator& operator++()
1058        {
1059            __sbuf_->sbumpc();
1060            return *this;
1061        }
1062    _LIBCPP_INLINE_VISIBILITY __proxy              operator++(int)
1063        {
1064            return __proxy(__sbuf_->sbumpc(), __sbuf_);
1065        }
1066
1067    _LIBCPP_INLINE_VISIBILITY bool equal(const istreambuf_iterator& __b) const
1068        {return __test_for_eof() == __b.__test_for_eof();}
1069};
1070
1071template <class _CharT, class _Traits>
1072inline _LIBCPP_INLINE_VISIBILITY
1073bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,
1074                const istreambuf_iterator<_CharT,_Traits>& __b)
1075                {return __a.equal(__b);}
1076
1077template <class _CharT, class _Traits>
1078inline _LIBCPP_INLINE_VISIBILITY
1079bool operator!=(const istreambuf_iterator<_CharT,_Traits>& __a,
1080                const istreambuf_iterator<_CharT,_Traits>& __b)
1081                {return !__a.equal(__b);}
1082
1083template <class _CharT, class _Traits>
1084class _LIBCPP_TEMPLATE_VIS ostreambuf_iterator
1085    : public iterator<output_iterator_tag, void, void, void, void>
1086{
1087public:
1088    typedef _CharT                          char_type;
1089    typedef _Traits                         traits_type;
1090    typedef basic_streambuf<_CharT,_Traits> streambuf_type;
1091    typedef basic_ostream<_CharT,_Traits>   ostream_type;
1092private:
1093    streambuf_type* __sbuf_;
1094public:
1095    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(ostream_type& __s) _NOEXCEPT
1096        : __sbuf_(__s.rdbuf()) {}
1097    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(streambuf_type* __s) _NOEXCEPT
1098        : __sbuf_(__s) {}
1099    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator=(_CharT __c)
1100        {
1101            if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sputc(__c), traits_type::eof()))
1102                __sbuf_ = 0;
1103            return *this;
1104        }
1105    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator*()     {return *this;}
1106    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++()    {return *this;}
1107    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++(int) {return *this;}
1108    _LIBCPP_INLINE_VISIBILITY bool failed() const _NOEXCEPT {return __sbuf_ == 0;}
1109
1110    template <class _Ch, class _Tr>
1111    friend
1112    _LIBCPP_HIDDEN
1113    ostreambuf_iterator<_Ch, _Tr>
1114    __pad_and_output(ostreambuf_iterator<_Ch, _Tr> __s,
1115                     const _Ch* __ob, const _Ch* __op, const _Ch* __oe,
1116                     ios_base& __iob, _Ch __fl);
1117};
1118
1119template <class _Iter>
1120class _LIBCPP_TEMPLATE_VIS move_iterator
1121{
1122private:
1123    _Iter __i;
1124public:
1125    typedef _Iter                                            iterator_type;
1126    typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
1127    typedef typename iterator_traits<iterator_type>::value_type value_type;
1128    typedef typename iterator_traits<iterator_type>::difference_type difference_type;
1129    typedef iterator_type pointer;
1130#ifndef _LIBCPP_CXX03_LANG
1131    typedef typename iterator_traits<iterator_type>::reference __reference;
1132    typedef typename conditional<
1133            is_reference<__reference>::value,
1134            typename remove_reference<__reference>::type&&,
1135            __reference
1136        >::type reference;
1137#else
1138    typedef typename iterator_traits<iterator_type>::reference reference;
1139#endif
1140
1141    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1142    move_iterator() : __i() {}
1143    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1144    explicit move_iterator(_Iter __x) : __i(__x) {}
1145    template <class _Up>
1146      _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1147      move_iterator(const move_iterator<_Up>& __u) : __i(__u.base()) {}
1148    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 _Iter base() const {return __i;}
1149    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1150    reference operator*() const { return static_cast<reference>(*__i); }
1151    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1152    pointer  operator->() const { return __i;}
1153    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1154    move_iterator& operator++() {++__i; return *this;}
1155    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1156    move_iterator  operator++(int) {move_iterator __tmp(*this); ++__i; return __tmp;}
1157    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1158    move_iterator& operator--() {--__i; return *this;}
1159    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1160    move_iterator  operator--(int) {move_iterator __tmp(*this); --__i; return __tmp;}
1161    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1162    move_iterator  operator+ (difference_type __n) const {return move_iterator(__i + __n);}
1163    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1164    move_iterator& operator+=(difference_type __n) {__i += __n; return *this;}
1165    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1166    move_iterator  operator- (difference_type __n) const {return move_iterator(__i - __n);}
1167    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1168    move_iterator& operator-=(difference_type __n) {__i -= __n; return *this;}
1169    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1170    reference operator[](difference_type __n) const { return static_cast<reference>(__i[__n]); }
1171};
1172
1173template <class _Iter1, class _Iter2>
1174inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1175bool
1176operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1177{
1178    return __x.base() == __y.base();
1179}
1180
1181template <class _Iter1, class _Iter2>
1182inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1183bool
1184operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1185{
1186    return __x.base() < __y.base();
1187}
1188
1189template <class _Iter1, class _Iter2>
1190inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1191bool
1192operator!=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1193{
1194    return __x.base() != __y.base();
1195}
1196
1197template <class _Iter1, class _Iter2>
1198inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1199bool
1200operator>(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1201{
1202    return __x.base() > __y.base();
1203}
1204
1205template <class _Iter1, class _Iter2>
1206inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1207bool
1208operator>=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1209{
1210    return __x.base() >= __y.base();
1211}
1212
1213template <class _Iter1, class _Iter2>
1214inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1215bool
1216operator<=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1217{
1218    return __x.base() <= __y.base();
1219}
1220
1221#ifndef _LIBCPP_CXX03_LANG
1222template <class _Iter1, class _Iter2>
1223inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1224auto
1225operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1226-> decltype(__x.base() - __y.base())
1227{
1228    return __x.base() - __y.base();
1229}
1230#else
1231template <class _Iter1, class _Iter2>
1232inline _LIBCPP_INLINE_VISIBILITY
1233typename move_iterator<_Iter1>::difference_type
1234operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1235{
1236    return __x.base() - __y.base();
1237}
1238#endif
1239
1240template <class _Iter>
1241inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1242move_iterator<_Iter>
1243operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
1244{
1245    return move_iterator<_Iter>(__x.base() + __n);
1246}
1247
1248template <class _Iter>
1249inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1250move_iterator<_Iter>
1251make_move_iterator(_Iter __i)
1252{
1253    return move_iterator<_Iter>(__i);
1254}
1255
1256// __wrap_iter
1257
1258template <class _Iter> class __wrap_iter;
1259
1260template <class _Iter1, class _Iter2>
1261_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1262bool
1263operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1264
1265template <class _Iter1, class _Iter2>
1266_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1267bool
1268operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1269
1270template <class _Iter1, class _Iter2>
1271_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1272bool
1273operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1274
1275template <class _Iter1, class _Iter2>
1276_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1277bool
1278operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1279
1280template <class _Iter1, class _Iter2>
1281_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1282bool
1283operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1284
1285template <class _Iter1, class _Iter2>
1286_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1287bool
1288operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1289
1290#ifndef _LIBCPP_CXX03_LANG
1291template <class _Iter1, class _Iter2>
1292_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1293auto
1294operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1295-> decltype(__x.base() - __y.base());
1296#else
1297template <class _Iter1, class _Iter2>
1298_LIBCPP_INLINE_VISIBILITY
1299typename __wrap_iter<_Iter1>::difference_type
1300operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1301#endif
1302
1303template <class _Iter>
1304_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1305__wrap_iter<_Iter>
1306operator+(typename __wrap_iter<_Iter>::difference_type, __wrap_iter<_Iter>) _NOEXCEPT;
1307
1308template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY copy(_Ip, _Ip, _Op);
1309template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY copy_backward(_B1, _B1, _B2);
1310template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY move(_Ip, _Ip, _Op);
1311template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY move_backward(_B1, _B1, _B2);
1312
1313#if _LIBCPP_DEBUG_LEVEL < 2
1314
1315template <class _Tp>
1316_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1317typename enable_if
1318<
1319    is_trivially_copy_assignable<_Tp>::value,
1320    _Tp*
1321>::type
1322__unwrap_iter(__wrap_iter<_Tp*>);
1323
1324#else
1325
1326template <class _Tp>
1327inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1328typename enable_if
1329<
1330    is_trivially_copy_assignable<_Tp>::value,
1331    __wrap_iter<_Tp*>
1332>::type
1333__unwrap_iter(__wrap_iter<_Tp*> __i);
1334
1335#endif
1336
1337template <class _Iter>
1338class __wrap_iter
1339{
1340public:
1341    typedef _Iter                                                      iterator_type;
1342    typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
1343    typedef typename iterator_traits<iterator_type>::value_type        value_type;
1344    typedef typename iterator_traits<iterator_type>::difference_type   difference_type;
1345    typedef typename iterator_traits<iterator_type>::pointer           pointer;
1346    typedef typename iterator_traits<iterator_type>::reference         reference;
1347private:
1348    iterator_type __i;
1349public:
1350    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter() _NOEXCEPT
1351#if _LIBCPP_STD_VER > 11
1352                : __i{}
1353#endif
1354    {
1355#if _LIBCPP_DEBUG_LEVEL >= 2
1356        __get_db()->__insert_i(this);
1357#endif
1358    }
1359    template <class _Up> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1360        __wrap_iter(const __wrap_iter<_Up>& __u,
1361            typename enable_if<is_convertible<_Up, iterator_type>::value>::type* = 0) _NOEXCEPT
1362            : __i(__u.base())
1363    {
1364#if _LIBCPP_DEBUG_LEVEL >= 2
1365        __get_db()->__iterator_copy(this, &__u);
1366#endif
1367    }
1368#if _LIBCPP_DEBUG_LEVEL >= 2
1369    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1370    __wrap_iter(const __wrap_iter& __x)
1371        : __i(__x.base())
1372    {
1373        __get_db()->__iterator_copy(this, &__x);
1374    }
1375    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1376    __wrap_iter& operator=(const __wrap_iter& __x)
1377    {
1378        if (this != &__x)
1379        {
1380            __get_db()->__iterator_copy(this, &__x);
1381            __i = __x.__i;
1382        }
1383        return *this;
1384    }
1385    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1386    ~__wrap_iter()
1387    {
1388        __get_db()->__erase_i(this);
1389    }
1390#endif
1391    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference operator*() const _NOEXCEPT
1392    {
1393#if _LIBCPP_DEBUG_LEVEL >= 2
1394        _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1395                       "Attempted to dereference a non-dereferenceable iterator");
1396#endif
1397        return *__i;
1398    }
1399    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG pointer  operator->() const _NOEXCEPT
1400    {
1401#if _LIBCPP_DEBUG_LEVEL >= 2
1402        _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1403                       "Attempted to dereference a non-dereferenceable iterator");
1404#endif
1405        return (pointer)_VSTD::addressof(*__i);
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()->__dereferenceable(this),
1411                       "Attempted to increment non-incrementable 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
1419    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator--() _NOEXCEPT
1420    {
1421#if _LIBCPP_DEBUG_LEVEL >= 2
1422        _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
1423                       "Attempted to decrement non-decrementable iterator");
1424#endif
1425        --__i;
1426        return *this;
1427    }
1428    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter  operator--(int) _NOEXCEPT
1429        {__wrap_iter __tmp(*this); --(*this); return __tmp;}
1430    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter  operator+ (difference_type __n) const _NOEXCEPT
1431        {__wrap_iter __w(*this); __w += __n; return __w;}
1432    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator+=(difference_type __n) _NOEXCEPT
1433    {
1434#if _LIBCPP_DEBUG_LEVEL >= 2
1435        _LIBCPP_ASSERT(__get_const_db()->__addable(this, __n),
1436                   "Attempted to add/subtract iterator outside of valid range");
1437#endif
1438        __i += __n;
1439        return *this;
1440    }
1441    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter  operator- (difference_type __n) const _NOEXCEPT
1442        {return *this + (-__n);}
1443    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator-=(difference_type __n) _NOEXCEPT
1444        {*this += -__n; return *this;}
1445    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference    operator[](difference_type __n) const _NOEXCEPT
1446    {
1447#if _LIBCPP_DEBUG_LEVEL >= 2
1448        _LIBCPP_ASSERT(__get_const_db()->__subscriptable(this, __n),
1449                   "Attempted to subscript iterator outside of valid range");
1450#endif
1451        return __i[__n];
1452    }
1453
1454    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG iterator_type base() const _NOEXCEPT {return __i;}
1455
1456private:
1457#if _LIBCPP_DEBUG_LEVEL >= 2
1458    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(const void* __p, iterator_type __x) : __i(__x)
1459    {
1460        __get_db()->__insert_ic(this, __p);
1461    }
1462#else
1463    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(iterator_type __x) _NOEXCEPT : __i(__x) {}
1464#endif
1465
1466    template <class _Up> friend class __wrap_iter;
1467    template <class _CharT, class _Traits, class _Alloc> friend class basic_string;
1468    template <class _Tp, class _Alloc> friend class _LIBCPP_TEMPLATE_VIS vector;
1469    template <class _Tp, size_t> friend class _LIBCPP_TEMPLATE_VIS span;
1470
1471    template <class _Iter1, class _Iter2>
1472    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1473    bool
1474    operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1475
1476    template <class _Iter1, class _Iter2>
1477    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1478    bool
1479    operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1480
1481    template <class _Iter1, class _Iter2>
1482    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1483    bool
1484    operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1485
1486    template <class _Iter1, class _Iter2>
1487    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1488    bool
1489    operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1490
1491    template <class _Iter1, class _Iter2>
1492    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1493    bool
1494    operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1495
1496    template <class _Iter1, class _Iter2>
1497    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1498    bool
1499    operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1500
1501#ifndef _LIBCPP_CXX03_LANG
1502    template <class _Iter1, class _Iter2>
1503    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1504    auto
1505    operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1506    -> decltype(__x.base() - __y.base());
1507#else
1508    template <class _Iter1, class _Iter2>
1509    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1510    typename __wrap_iter<_Iter1>::difference_type
1511    operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1512#endif
1513
1514    template <class _Iter1>
1515    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1516    __wrap_iter<_Iter1>
1517    operator+(typename __wrap_iter<_Iter1>::difference_type, __wrap_iter<_Iter1>) _NOEXCEPT;
1518
1519    template <class _Ip, class _Op> friend _Op copy(_Ip, _Ip, _Op);
1520    template <class _B1, class _B2> friend _B2 copy_backward(_B1, _B1, _B2);
1521    template <class _Ip, class _Op> friend _Op move(_Ip, _Ip, _Op);
1522    template <class _B1, class _B2> friend _B2 move_backward(_B1, _B1, _B2);
1523
1524#if _LIBCPP_DEBUG_LEVEL < 2
1525    template <class _Tp>
1526    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1527    typename enable_if
1528    <
1529        is_trivially_copy_assignable<_Tp>::value,
1530        _Tp*
1531    >::type
1532    __unwrap_iter(__wrap_iter<_Tp*>);
1533#else
1534  template <class _Tp>
1535  inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1536  typename enable_if
1537  <
1538      is_trivially_copy_assignable<_Tp>::value,
1539      __wrap_iter<_Tp*>
1540  >::type
1541  __unwrap_iter(__wrap_iter<_Tp*> __i);
1542#endif
1543};
1544
1545template <class _Iter1, class _Iter2>
1546inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1547bool
1548operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1549{
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#if _LIBCPP_DEBUG_LEVEL >= 2
1559    _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
1560                   "Attempted to compare incomparable iterators");
1561#endif
1562    return __x.base() < __y.base();
1563}
1564
1565template <class _Iter1, class _Iter2>
1566inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1567bool
1568operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1569{
1570    return !(__x == __y);
1571}
1572
1573template <class _Iter1, class _Iter2>
1574inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1575bool
1576operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1577{
1578    return __y < __x;
1579}
1580
1581template <class _Iter1, class _Iter2>
1582inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1583bool
1584operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1585{
1586    return !(__x < __y);
1587}
1588
1589template <class _Iter1, class _Iter2>
1590inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1591bool
1592operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1593{
1594    return !(__y < __x);
1595}
1596
1597template <class _Iter1>
1598inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1599bool
1600operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
1601{
1602    return !(__x == __y);
1603}
1604
1605template <class _Iter1>
1606inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1607bool
1608operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
1609{
1610    return __y < __x;
1611}
1612
1613template <class _Iter1>
1614inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1615bool
1616operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
1617{
1618    return !(__x < __y);
1619}
1620
1621template <class _Iter1>
1622inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1623bool
1624operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
1625{
1626    return !(__y < __x);
1627}
1628
1629#ifndef _LIBCPP_CXX03_LANG
1630template <class _Iter1, class _Iter2>
1631inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1632auto
1633operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1634-> decltype(__x.base() - __y.base())
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#else
1643template <class _Iter1, class _Iter2>
1644inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1645typename __wrap_iter<_Iter1>::difference_type
1646operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1647{
1648#if _LIBCPP_DEBUG_LEVEL >= 2
1649    _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
1650                   "Attempted to subtract incompatible iterators");
1651#endif
1652    return __x.base() - __y.base();
1653}
1654#endif
1655
1656template <class _Iter>
1657inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1658__wrap_iter<_Iter>
1659operator+(typename __wrap_iter<_Iter>::difference_type __n,
1660          __wrap_iter<_Iter> __x) _NOEXCEPT
1661{
1662    __x += __n;
1663    return __x;
1664}
1665
1666template <class _Iter>
1667struct __libcpp_is_trivial_iterator
1668    : public _LIBCPP_BOOL_CONSTANT(is_pointer<_Iter>::value) {};
1669
1670template <class _Iter>
1671struct __libcpp_is_trivial_iterator<move_iterator<_Iter> >
1672    : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
1673
1674template <class _Iter>
1675struct __libcpp_is_trivial_iterator<reverse_iterator<_Iter> >
1676    : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
1677
1678template <class _Iter>
1679struct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> >
1680    : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
1681
1682
1683template <class _Tp, size_t _Np>
1684_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1685_Tp*
1686begin(_Tp (&__array)[_Np])
1687{
1688    return __array;
1689}
1690
1691template <class _Tp, size_t _Np>
1692_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1693_Tp*
1694end(_Tp (&__array)[_Np])
1695{
1696    return __array + _Np;
1697}
1698
1699#if !defined(_LIBCPP_CXX03_LANG)
1700
1701template <class _Cp>
1702_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1703auto
1704begin(_Cp& __c) -> decltype(__c.begin())
1705{
1706    return __c.begin();
1707}
1708
1709template <class _Cp>
1710_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1711auto
1712begin(const _Cp& __c) -> decltype(__c.begin())
1713{
1714    return __c.begin();
1715}
1716
1717template <class _Cp>
1718_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1719auto
1720end(_Cp& __c) -> decltype(__c.end())
1721{
1722    return __c.end();
1723}
1724
1725template <class _Cp>
1726_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1727auto
1728end(const _Cp& __c) -> decltype(__c.end())
1729{
1730    return __c.end();
1731}
1732
1733#if _LIBCPP_STD_VER > 11
1734
1735template <class _Tp, size_t _Np>
1736_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1737reverse_iterator<_Tp*> rbegin(_Tp (&__array)[_Np])
1738{
1739    return reverse_iterator<_Tp*>(__array + _Np);
1740}
1741
1742template <class _Tp, size_t _Np>
1743_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1744reverse_iterator<_Tp*> rend(_Tp (&__array)[_Np])
1745{
1746    return reverse_iterator<_Tp*>(__array);
1747}
1748
1749template <class _Ep>
1750_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1751reverse_iterator<const _Ep*> rbegin(initializer_list<_Ep> __il)
1752{
1753    return reverse_iterator<const _Ep*>(__il.end());
1754}
1755
1756template <class _Ep>
1757_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1758reverse_iterator<const _Ep*> rend(initializer_list<_Ep> __il)
1759{
1760    return reverse_iterator<const _Ep*>(__il.begin());
1761}
1762
1763template <class _Cp>
1764_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1765auto cbegin(const _Cp& __c) -> decltype(_VSTD::begin(__c))
1766{
1767    return _VSTD::begin(__c);
1768}
1769
1770template <class _Cp>
1771_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1772auto cend(const _Cp& __c) -> decltype(_VSTD::end(__c))
1773{
1774    return _VSTD::end(__c);
1775}
1776
1777template <class _Cp>
1778_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1779auto rbegin(_Cp& __c) -> decltype(__c.rbegin())
1780{
1781    return __c.rbegin();
1782}
1783
1784template <class _Cp>
1785_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1786auto rbegin(const _Cp& __c) -> decltype(__c.rbegin())
1787{
1788    return __c.rbegin();
1789}
1790
1791template <class _Cp>
1792_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1793auto rend(_Cp& __c) -> decltype(__c.rend())
1794{
1795    return __c.rend();
1796}
1797
1798template <class _Cp>
1799_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1800auto rend(const _Cp& __c) -> decltype(__c.rend())
1801{
1802    return __c.rend();
1803}
1804
1805template <class _Cp>
1806_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1807auto crbegin(const _Cp& __c) -> decltype(_VSTD::rbegin(__c))
1808{
1809    return _VSTD::rbegin(__c);
1810}
1811
1812template <class _Cp>
1813_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1814auto crend(const _Cp& __c) -> decltype(_VSTD::rend(__c))
1815{
1816    return _VSTD::rend(__c);
1817}
1818
1819#endif
1820
1821
1822#else  // defined(_LIBCPP_CXX03_LANG)
1823
1824template <class _Cp>
1825_LIBCPP_INLINE_VISIBILITY
1826typename _Cp::iterator
1827begin(_Cp& __c)
1828{
1829    return __c.begin();
1830}
1831
1832template <class _Cp>
1833_LIBCPP_INLINE_VISIBILITY
1834typename _Cp::const_iterator
1835begin(const _Cp& __c)
1836{
1837    return __c.begin();
1838}
1839
1840template <class _Cp>
1841_LIBCPP_INLINE_VISIBILITY
1842typename _Cp::iterator
1843end(_Cp& __c)
1844{
1845    return __c.end();
1846}
1847
1848template <class _Cp>
1849_LIBCPP_INLINE_VISIBILITY
1850typename _Cp::const_iterator
1851end(const _Cp& __c)
1852{
1853    return __c.end();
1854}
1855
1856#endif  // !defined(_LIBCPP_CXX03_LANG)
1857
1858#if _LIBCPP_STD_VER > 14
1859
1860// #if _LIBCPP_STD_VER > 11
1861// template <>
1862// struct _LIBCPP_TEMPLATE_VIS plus<void>
1863// {
1864//     template <class _T1, class _T2>
1865//     _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1866//     auto operator()(_T1&& __t, _T2&& __u) const
1867//     _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
1868//     -> decltype        (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
1869//         { return        _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
1870//     typedef void is_transparent;
1871// };
1872// #endif
1873
1874template <class _Cont>
1875_LIBCPP_INLINE_VISIBILITY
1876constexpr auto size(const _Cont& __c)
1877_NOEXCEPT_(noexcept(__c.size()))
1878-> decltype        (__c.size())
1879{ return            __c.size(); }
1880
1881template <class _Tp, size_t _Sz>
1882_LIBCPP_INLINE_VISIBILITY
1883constexpr size_t size(const _Tp (&)[_Sz]) noexcept { return _Sz; }
1884
1885#if _LIBCPP_STD_VER > 17
1886template <class _Cont>
1887_LIBCPP_INLINE_VISIBILITY
1888constexpr auto ssize(const _Cont& __c)
1889_NOEXCEPT_(noexcept(static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size())))
1890->                              common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>
1891{ return            static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size()); }
1892
1893template <class _Tp, ptrdiff_t _Sz>
1894_LIBCPP_INLINE_VISIBILITY
1895constexpr ptrdiff_t ssize(const _Tp (&)[_Sz]) noexcept { return _Sz; }
1896#endif
1897
1898template <class _Cont>
1899_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
1900constexpr auto empty(const _Cont& __c)
1901_NOEXCEPT_(noexcept(__c.empty()))
1902-> decltype        (__c.empty())
1903{ return            __c.empty(); }
1904
1905template <class _Tp, size_t _Sz>
1906_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
1907constexpr bool empty(const _Tp (&)[_Sz]) noexcept { return false; }
1908
1909template <class _Ep>
1910_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
1911constexpr bool empty(initializer_list<_Ep> __il) noexcept { return __il.size() == 0; }
1912
1913template <class _Cont> constexpr
1914_LIBCPP_INLINE_VISIBILITY
1915auto data(_Cont& __c)
1916_NOEXCEPT_(noexcept(__c.data()))
1917-> decltype        (__c.data())
1918{ return            __c.data(); }
1919
1920template <class _Cont> constexpr
1921_LIBCPP_INLINE_VISIBILITY
1922auto data(const _Cont& __c)
1923_NOEXCEPT_(noexcept(__c.data()))
1924-> decltype        (__c.data())
1925{ return            __c.data(); }
1926
1927template <class _Tp, size_t _Sz>
1928_LIBCPP_INLINE_VISIBILITY
1929constexpr _Tp* data(_Tp (&__array)[_Sz]) noexcept { return __array; }
1930
1931template <class _Ep>
1932_LIBCPP_INLINE_VISIBILITY
1933constexpr const _Ep* data(initializer_list<_Ep> __il) noexcept { return __il.begin(); }
1934#endif
1935
1936
1937_LIBCPP_END_NAMESPACE_STD
1938
1939#endif  // _LIBCPP_ITERATOR
1940