xref: /freebsd/contrib/llvm-project/libcxx/include/iterator (revision 53071ed1c96db7f89defc99c95b0ad1031d48f45)
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
845template <class _Container>
846inline _LIBCPP_INLINE_VISIBILITY
847back_insert_iterator<_Container>
848back_inserter(_Container& __x)
849{
850    return back_insert_iterator<_Container>(__x);
851}
852
853template <class _Container>
854class _LIBCPP_TEMPLATE_VIS front_insert_iterator
855    : public iterator<output_iterator_tag,
856                      void,
857                      void,
858                      void,
859                      void>
860{
861protected:
862    _Container* container;
863public:
864    typedef _Container container_type;
865
866    _LIBCPP_INLINE_VISIBILITY explicit front_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
867    _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator=(const typename _Container::value_type& __value_)
868        {container->push_front(__value_); return *this;}
869#ifndef _LIBCPP_CXX03_LANG
870    _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator=(typename _Container::value_type&& __value_)
871        {container->push_front(_VSTD::move(__value_)); return *this;}
872#endif  // _LIBCPP_CXX03_LANG
873    _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator*()     {return *this;}
874    _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator++()    {return *this;}
875    _LIBCPP_INLINE_VISIBILITY front_insert_iterator  operator++(int) {return *this;}
876};
877
878template <class _Container>
879inline _LIBCPP_INLINE_VISIBILITY
880front_insert_iterator<_Container>
881front_inserter(_Container& __x)
882{
883    return front_insert_iterator<_Container>(__x);
884}
885
886template <class _Container>
887class _LIBCPP_TEMPLATE_VIS insert_iterator
888    : public iterator<output_iterator_tag,
889                      void,
890                      void,
891                      void,
892                      void>
893{
894protected:
895    _Container* container;
896    typename _Container::iterator iter;
897public:
898    typedef _Container container_type;
899
900    _LIBCPP_INLINE_VISIBILITY insert_iterator(_Container& __x, typename _Container::iterator __i)
901        : container(_VSTD::addressof(__x)), iter(__i) {}
902    _LIBCPP_INLINE_VISIBILITY insert_iterator& operator=(const typename _Container::value_type& __value_)
903        {iter = container->insert(iter, __value_); ++iter; return *this;}
904#ifndef _LIBCPP_CXX03_LANG
905    _LIBCPP_INLINE_VISIBILITY insert_iterator& operator=(typename _Container::value_type&& __value_)
906        {iter = container->insert(iter, _VSTD::move(__value_)); ++iter; return *this;}
907#endif  // _LIBCPP_CXX03_LANG
908    _LIBCPP_INLINE_VISIBILITY insert_iterator& operator*()        {return *this;}
909    _LIBCPP_INLINE_VISIBILITY insert_iterator& operator++()       {return *this;}
910    _LIBCPP_INLINE_VISIBILITY insert_iterator& operator++(int)    {return *this;}
911};
912
913template <class _Container>
914inline _LIBCPP_INLINE_VISIBILITY
915insert_iterator<_Container>
916inserter(_Container& __x, typename _Container::iterator __i)
917{
918    return insert_iterator<_Container>(__x, __i);
919}
920
921template <class _Tp, class _CharT = char,
922          class _Traits = char_traits<_CharT>, class _Distance = ptrdiff_t>
923class _LIBCPP_TEMPLATE_VIS istream_iterator
924    : public iterator<input_iterator_tag, _Tp, _Distance, const _Tp*, const _Tp&>
925{
926public:
927    typedef _CharT char_type;
928    typedef _Traits traits_type;
929    typedef basic_istream<_CharT,_Traits> istream_type;
930private:
931    istream_type* __in_stream_;
932    _Tp __value_;
933public:
934    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istream_iterator() : __in_stream_(0), __value_() {}
935    _LIBCPP_INLINE_VISIBILITY istream_iterator(istream_type& __s) : __in_stream_(_VSTD::addressof(__s))
936        {
937            if (!(*__in_stream_ >> __value_))
938                __in_stream_ = 0;
939        }
940
941    _LIBCPP_INLINE_VISIBILITY const _Tp& operator*() const {return __value_;}
942    _LIBCPP_INLINE_VISIBILITY const _Tp* operator->() const {return _VSTD::addressof((operator*()));}
943    _LIBCPP_INLINE_VISIBILITY istream_iterator& operator++()
944        {
945            if (!(*__in_stream_ >> __value_))
946                __in_stream_ = 0;
947            return *this;
948        }
949    _LIBCPP_INLINE_VISIBILITY istream_iterator  operator++(int)
950        {istream_iterator __t(*this); ++(*this); return __t;}
951
952    template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
953    friend _LIBCPP_INLINE_VISIBILITY
954    bool
955    operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
956               const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
957
958    template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
959    friend _LIBCPP_INLINE_VISIBILITY
960    bool
961    operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
962               const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
963};
964
965template <class _Tp, class _CharT, class _Traits, class _Distance>
966inline _LIBCPP_INLINE_VISIBILITY
967bool
968operator==(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
969           const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
970{
971    return __x.__in_stream_ == __y.__in_stream_;
972}
973
974template <class _Tp, class _CharT, class _Traits, class _Distance>
975inline _LIBCPP_INLINE_VISIBILITY
976bool
977operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
978           const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
979{
980    return !(__x == __y);
981}
982
983template <class _Tp, class _CharT = char, class _Traits = char_traits<_CharT> >
984class _LIBCPP_TEMPLATE_VIS ostream_iterator
985    : public iterator<output_iterator_tag, void, void, void, void>
986{
987public:
988    typedef _CharT char_type;
989    typedef _Traits traits_type;
990    typedef basic_ostream<_CharT,_Traits> ostream_type;
991private:
992    ostream_type* __out_stream_;
993    const char_type* __delim_;
994public:
995    _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s) _NOEXCEPT
996        : __out_stream_(_VSTD::addressof(__s)), __delim_(0) {}
997    _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s, const _CharT* __delimiter) _NOEXCEPT
998        : __out_stream_(_VSTD::addressof(__s)), __delim_(__delimiter) {}
999    _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator=(const _Tp& __value_)
1000        {
1001            *__out_stream_ << __value_;
1002            if (__delim_)
1003                *__out_stream_ << __delim_;
1004            return *this;
1005        }
1006
1007    _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator*()     {return *this;}
1008    _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++()    {return *this;}
1009    _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++(int) {return *this;}
1010};
1011
1012template<class _CharT, class _Traits>
1013class _LIBCPP_TEMPLATE_VIS istreambuf_iterator
1014    : public iterator<input_iterator_tag, _CharT,
1015                      typename _Traits::off_type, _CharT*,
1016                      _CharT>
1017{
1018public:
1019    typedef _CharT                          char_type;
1020    typedef _Traits                         traits_type;
1021    typedef typename _Traits::int_type      int_type;
1022    typedef basic_streambuf<_CharT,_Traits> streambuf_type;
1023    typedef basic_istream<_CharT,_Traits>   istream_type;
1024private:
1025    mutable streambuf_type* __sbuf_;
1026
1027    class __proxy
1028    {
1029        char_type __keep_;
1030        streambuf_type* __sbuf_;
1031        _LIBCPP_INLINE_VISIBILITY __proxy(char_type __c, streambuf_type* __s)
1032            : __keep_(__c), __sbuf_(__s) {}
1033        friend class istreambuf_iterator;
1034    public:
1035        _LIBCPP_INLINE_VISIBILITY char_type operator*() const {return __keep_;}
1036    };
1037
1038    _LIBCPP_INLINE_VISIBILITY
1039    bool __test_for_eof() const
1040    {
1041        if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof()))
1042            __sbuf_ = 0;
1043        return __sbuf_ == 0;
1044    }
1045public:
1046    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istreambuf_iterator() _NOEXCEPT : __sbuf_(0) {}
1047    _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(istream_type& __s) _NOEXCEPT
1048        : __sbuf_(__s.rdbuf()) {}
1049    _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(streambuf_type* __s) _NOEXCEPT
1050        : __sbuf_(__s) {}
1051    _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(const __proxy& __p) _NOEXCEPT
1052        : __sbuf_(__p.__sbuf_) {}
1053
1054    _LIBCPP_INLINE_VISIBILITY char_type  operator*() const
1055        {return static_cast<char_type>(__sbuf_->sgetc());}
1056    _LIBCPP_INLINE_VISIBILITY istreambuf_iterator& operator++()
1057        {
1058            __sbuf_->sbumpc();
1059            return *this;
1060        }
1061    _LIBCPP_INLINE_VISIBILITY __proxy              operator++(int)
1062        {
1063            return __proxy(__sbuf_->sbumpc(), __sbuf_);
1064        }
1065
1066    _LIBCPP_INLINE_VISIBILITY bool equal(const istreambuf_iterator& __b) const
1067        {return __test_for_eof() == __b.__test_for_eof();}
1068};
1069
1070template <class _CharT, class _Traits>
1071inline _LIBCPP_INLINE_VISIBILITY
1072bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,
1073                const istreambuf_iterator<_CharT,_Traits>& __b)
1074                {return __a.equal(__b);}
1075
1076template <class _CharT, class _Traits>
1077inline _LIBCPP_INLINE_VISIBILITY
1078bool operator!=(const istreambuf_iterator<_CharT,_Traits>& __a,
1079                const istreambuf_iterator<_CharT,_Traits>& __b)
1080                {return !__a.equal(__b);}
1081
1082template <class _CharT, class _Traits>
1083class _LIBCPP_TEMPLATE_VIS ostreambuf_iterator
1084    : public iterator<output_iterator_tag, void, void, void, void>
1085{
1086public:
1087    typedef _CharT                          char_type;
1088    typedef _Traits                         traits_type;
1089    typedef basic_streambuf<_CharT,_Traits> streambuf_type;
1090    typedef basic_ostream<_CharT,_Traits>   ostream_type;
1091private:
1092    streambuf_type* __sbuf_;
1093public:
1094    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(ostream_type& __s) _NOEXCEPT
1095        : __sbuf_(__s.rdbuf()) {}
1096    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(streambuf_type* __s) _NOEXCEPT
1097        : __sbuf_(__s) {}
1098    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator=(_CharT __c)
1099        {
1100            if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sputc(__c), traits_type::eof()))
1101                __sbuf_ = 0;
1102            return *this;
1103        }
1104    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator*()     {return *this;}
1105    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++()    {return *this;}
1106    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++(int) {return *this;}
1107    _LIBCPP_INLINE_VISIBILITY bool failed() const _NOEXCEPT {return __sbuf_ == 0;}
1108
1109    template <class _Ch, class _Tr>
1110    friend
1111    _LIBCPP_HIDDEN
1112    ostreambuf_iterator<_Ch, _Tr>
1113    __pad_and_output(ostreambuf_iterator<_Ch, _Tr> __s,
1114                     const _Ch* __ob, const _Ch* __op, const _Ch* __oe,
1115                     ios_base& __iob, _Ch __fl);
1116};
1117
1118template <class _Iter>
1119class _LIBCPP_TEMPLATE_VIS move_iterator
1120{
1121private:
1122    _Iter __i;
1123public:
1124    typedef _Iter                                            iterator_type;
1125    typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
1126    typedef typename iterator_traits<iterator_type>::value_type value_type;
1127    typedef typename iterator_traits<iterator_type>::difference_type difference_type;
1128    typedef iterator_type pointer;
1129#ifndef _LIBCPP_CXX03_LANG
1130    typedef typename iterator_traits<iterator_type>::reference __reference;
1131    typedef typename conditional<
1132            is_reference<__reference>::value,
1133            typename remove_reference<__reference>::type&&,
1134            __reference
1135        >::type reference;
1136#else
1137    typedef typename iterator_traits<iterator_type>::reference reference;
1138#endif
1139
1140    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1141    move_iterator() : __i() {}
1142    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1143    explicit move_iterator(_Iter __x) : __i(__x) {}
1144    template <class _Up>
1145      _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1146      move_iterator(const move_iterator<_Up>& __u) : __i(__u.base()) {}
1147    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 _Iter base() const {return __i;}
1148    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1149    reference operator*() const { return static_cast<reference>(*__i); }
1150    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1151    pointer  operator->() const { return __i;}
1152    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1153    move_iterator& operator++() {++__i; return *this;}
1154    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1155    move_iterator  operator++(int) {move_iterator __tmp(*this); ++__i; return __tmp;}
1156    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1157    move_iterator& operator--() {--__i; return *this;}
1158    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1159    move_iterator  operator--(int) {move_iterator __tmp(*this); --__i; return __tmp;}
1160    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1161    move_iterator  operator+ (difference_type __n) const {return move_iterator(__i + __n);}
1162    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1163    move_iterator& operator+=(difference_type __n) {__i += __n; return *this;}
1164    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1165    move_iterator  operator- (difference_type __n) const {return move_iterator(__i - __n);}
1166    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1167    move_iterator& operator-=(difference_type __n) {__i -= __n; return *this;}
1168    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1169    reference operator[](difference_type __n) const { return static_cast<reference>(__i[__n]); }
1170};
1171
1172template <class _Iter1, class _Iter2>
1173inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1174bool
1175operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1176{
1177    return __x.base() == __y.base();
1178}
1179
1180template <class _Iter1, class _Iter2>
1181inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1182bool
1183operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1184{
1185    return __x.base() < __y.base();
1186}
1187
1188template <class _Iter1, class _Iter2>
1189inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1190bool
1191operator!=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1192{
1193    return __x.base() != __y.base();
1194}
1195
1196template <class _Iter1, class _Iter2>
1197inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1198bool
1199operator>(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1200{
1201    return __x.base() > __y.base();
1202}
1203
1204template <class _Iter1, class _Iter2>
1205inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1206bool
1207operator>=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1208{
1209    return __x.base() >= __y.base();
1210}
1211
1212template <class _Iter1, class _Iter2>
1213inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1214bool
1215operator<=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1216{
1217    return __x.base() <= __y.base();
1218}
1219
1220#ifndef _LIBCPP_CXX03_LANG
1221template <class _Iter1, class _Iter2>
1222inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1223auto
1224operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1225-> decltype(__x.base() - __y.base())
1226{
1227    return __x.base() - __y.base();
1228}
1229#else
1230template <class _Iter1, class _Iter2>
1231inline _LIBCPP_INLINE_VISIBILITY
1232typename move_iterator<_Iter1>::difference_type
1233operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1234{
1235    return __x.base() - __y.base();
1236}
1237#endif
1238
1239template <class _Iter>
1240inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1241move_iterator<_Iter>
1242operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
1243{
1244    return move_iterator<_Iter>(__x.base() + __n);
1245}
1246
1247template <class _Iter>
1248inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1249move_iterator<_Iter>
1250make_move_iterator(_Iter __i)
1251{
1252    return move_iterator<_Iter>(__i);
1253}
1254
1255// __wrap_iter
1256
1257template <class _Iter> class __wrap_iter;
1258
1259template <class _Iter1, class _Iter2>
1260_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1261bool
1262operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1263
1264template <class _Iter1, class _Iter2>
1265_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1266bool
1267operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1268
1269template <class _Iter1, class _Iter2>
1270_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1271bool
1272operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1273
1274template <class _Iter1, class _Iter2>
1275_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1276bool
1277operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1278
1279template <class _Iter1, class _Iter2>
1280_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1281bool
1282operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1283
1284template <class _Iter1, class _Iter2>
1285_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1286bool
1287operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1288
1289#ifndef _LIBCPP_CXX03_LANG
1290template <class _Iter1, class _Iter2>
1291_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1292auto
1293operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1294-> decltype(__x.base() - __y.base());
1295#else
1296template <class _Iter1, class _Iter2>
1297_LIBCPP_INLINE_VISIBILITY
1298typename __wrap_iter<_Iter1>::difference_type
1299operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1300#endif
1301
1302template <class _Iter>
1303_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1304__wrap_iter<_Iter>
1305operator+(typename __wrap_iter<_Iter>::difference_type, __wrap_iter<_Iter>) _NOEXCEPT;
1306
1307template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY copy(_Ip, _Ip, _Op);
1308template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY copy_backward(_B1, _B1, _B2);
1309template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY move(_Ip, _Ip, _Op);
1310template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY move_backward(_B1, _B1, _B2);
1311
1312#if _LIBCPP_DEBUG_LEVEL < 2
1313
1314template <class _Tp>
1315_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1316typename enable_if
1317<
1318    is_trivially_copy_assignable<_Tp>::value,
1319    _Tp*
1320>::type
1321__unwrap_iter(__wrap_iter<_Tp*>);
1322
1323#else
1324
1325template <class _Tp>
1326inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1327typename enable_if
1328<
1329    is_trivially_copy_assignable<_Tp>::value,
1330    __wrap_iter<_Tp*>
1331>::type
1332__unwrap_iter(__wrap_iter<_Tp*> __i);
1333
1334#endif
1335
1336template <class _Iter>
1337class __wrap_iter
1338{
1339public:
1340    typedef _Iter                                                      iterator_type;
1341    typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
1342    typedef typename iterator_traits<iterator_type>::value_type        value_type;
1343    typedef typename iterator_traits<iterator_type>::difference_type   difference_type;
1344    typedef typename iterator_traits<iterator_type>::pointer           pointer;
1345    typedef typename iterator_traits<iterator_type>::reference         reference;
1346private:
1347    iterator_type __i;
1348public:
1349    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter() _NOEXCEPT
1350#if _LIBCPP_STD_VER > 11
1351                : __i{}
1352#endif
1353    {
1354#if _LIBCPP_DEBUG_LEVEL >= 2
1355        __get_db()->__insert_i(this);
1356#endif
1357    }
1358    template <class _Up> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1359        __wrap_iter(const __wrap_iter<_Up>& __u,
1360            typename enable_if<is_convertible<_Up, iterator_type>::value>::type* = 0) _NOEXCEPT
1361            : __i(__u.base())
1362    {
1363#if _LIBCPP_DEBUG_LEVEL >= 2
1364        __get_db()->__iterator_copy(this, &__u);
1365#endif
1366    }
1367#if _LIBCPP_DEBUG_LEVEL >= 2
1368    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1369    __wrap_iter(const __wrap_iter& __x)
1370        : __i(__x.base())
1371    {
1372        __get_db()->__iterator_copy(this, &__x);
1373    }
1374    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1375    __wrap_iter& operator=(const __wrap_iter& __x)
1376    {
1377        if (this != &__x)
1378        {
1379            __get_db()->__iterator_copy(this, &__x);
1380            __i = __x.__i;
1381        }
1382        return *this;
1383    }
1384    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1385    ~__wrap_iter()
1386    {
1387        __get_db()->__erase_i(this);
1388    }
1389#endif
1390    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference operator*() const _NOEXCEPT
1391    {
1392#if _LIBCPP_DEBUG_LEVEL >= 2
1393        _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1394                       "Attempted to dereference a non-dereferenceable iterator");
1395#endif
1396        return *__i;
1397    }
1398    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG pointer  operator->() const _NOEXCEPT
1399    {
1400#if _LIBCPP_DEBUG_LEVEL >= 2
1401        _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1402                       "Attempted to dereference a non-dereferenceable iterator");
1403#endif
1404        return (pointer)_VSTD::addressof(*__i);
1405    }
1406    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator++() _NOEXCEPT
1407    {
1408#if _LIBCPP_DEBUG_LEVEL >= 2
1409        _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1410                       "Attempted to increment non-incrementable iterator");
1411#endif
1412        ++__i;
1413        return *this;
1414    }
1415    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter  operator++(int) _NOEXCEPT
1416        {__wrap_iter __tmp(*this); ++(*this); return __tmp;}
1417
1418    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator--() _NOEXCEPT
1419    {
1420#if _LIBCPP_DEBUG_LEVEL >= 2
1421        _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
1422                       "Attempted to decrement non-decrementable iterator");
1423#endif
1424        --__i;
1425        return *this;
1426    }
1427    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter  operator--(int) _NOEXCEPT
1428        {__wrap_iter __tmp(*this); --(*this); return __tmp;}
1429    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter  operator+ (difference_type __n) const _NOEXCEPT
1430        {__wrap_iter __w(*this); __w += __n; return __w;}
1431    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator+=(difference_type __n) _NOEXCEPT
1432    {
1433#if _LIBCPP_DEBUG_LEVEL >= 2
1434        _LIBCPP_ASSERT(__get_const_db()->__addable(this, __n),
1435                   "Attempted to add/subtract iterator outside of valid range");
1436#endif
1437        __i += __n;
1438        return *this;
1439    }
1440    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter  operator- (difference_type __n) const _NOEXCEPT
1441        {return *this + (-__n);}
1442    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator-=(difference_type __n) _NOEXCEPT
1443        {*this += -__n; return *this;}
1444    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference    operator[](difference_type __n) const _NOEXCEPT
1445    {
1446#if _LIBCPP_DEBUG_LEVEL >= 2
1447        _LIBCPP_ASSERT(__get_const_db()->__subscriptable(this, __n),
1448                   "Attempted to subscript iterator outside of valid range");
1449#endif
1450        return __i[__n];
1451    }
1452
1453    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG iterator_type base() const _NOEXCEPT {return __i;}
1454
1455private:
1456#if _LIBCPP_DEBUG_LEVEL >= 2
1457    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(const void* __p, iterator_type __x) : __i(__x)
1458    {
1459        __get_db()->__insert_ic(this, __p);
1460    }
1461#else
1462    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(iterator_type __x) _NOEXCEPT : __i(__x) {}
1463#endif
1464
1465    template <class _Up> friend class __wrap_iter;
1466    template <class _CharT, class _Traits, class _Alloc> friend class basic_string;
1467    template <class _Tp, class _Alloc> friend class _LIBCPP_TEMPLATE_VIS vector;
1468    template <class _Tp, size_t> friend class _LIBCPP_TEMPLATE_VIS span;
1469
1470    template <class _Iter1, class _Iter2>
1471    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1472    bool
1473    operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1474
1475    template <class _Iter1, class _Iter2>
1476    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1477    bool
1478    operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1479
1480    template <class _Iter1, class _Iter2>
1481    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1482    bool
1483    operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1484
1485    template <class _Iter1, class _Iter2>
1486    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1487    bool
1488    operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1489
1490    template <class _Iter1, class _Iter2>
1491    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1492    bool
1493    operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1494
1495    template <class _Iter1, class _Iter2>
1496    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1497    bool
1498    operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1499
1500#ifndef _LIBCPP_CXX03_LANG
1501    template <class _Iter1, class _Iter2>
1502    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1503    auto
1504    operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1505    -> decltype(__x.base() - __y.base());
1506#else
1507    template <class _Iter1, class _Iter2>
1508    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1509    typename __wrap_iter<_Iter1>::difference_type
1510    operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1511#endif
1512
1513    template <class _Iter1>
1514    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1515    __wrap_iter<_Iter1>
1516    operator+(typename __wrap_iter<_Iter1>::difference_type, __wrap_iter<_Iter1>) _NOEXCEPT;
1517
1518    template <class _Ip, class _Op> friend _Op copy(_Ip, _Ip, _Op);
1519    template <class _B1, class _B2> friend _B2 copy_backward(_B1, _B1, _B2);
1520    template <class _Ip, class _Op> friend _Op move(_Ip, _Ip, _Op);
1521    template <class _B1, class _B2> friend _B2 move_backward(_B1, _B1, _B2);
1522
1523#if _LIBCPP_DEBUG_LEVEL < 2
1524    template <class _Tp>
1525    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1526    typename enable_if
1527    <
1528        is_trivially_copy_assignable<_Tp>::value,
1529        _Tp*
1530    >::type
1531    __unwrap_iter(__wrap_iter<_Tp*>);
1532#else
1533  template <class _Tp>
1534  inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1535  typename enable_if
1536  <
1537      is_trivially_copy_assignable<_Tp>::value,
1538      __wrap_iter<_Tp*>
1539  >::type
1540  __unwrap_iter(__wrap_iter<_Tp*> __i);
1541#endif
1542};
1543
1544template <class _Iter1, class _Iter2>
1545inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1546bool
1547operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1548{
1549    return __x.base() == __y.base();
1550}
1551
1552template <class _Iter1, class _Iter2>
1553inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1554bool
1555operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1556{
1557#if _LIBCPP_DEBUG_LEVEL >= 2
1558    _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
1559                   "Attempted to compare incomparable iterators");
1560#endif
1561    return __x.base() < __y.base();
1562}
1563
1564template <class _Iter1, class _Iter2>
1565inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1566bool
1567operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1568{
1569    return !(__x == __y);
1570}
1571
1572template <class _Iter1, class _Iter2>
1573inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1574bool
1575operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1576{
1577    return __y < __x;
1578}
1579
1580template <class _Iter1, class _Iter2>
1581inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1582bool
1583operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1584{
1585    return !(__x < __y);
1586}
1587
1588template <class _Iter1, class _Iter2>
1589inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1590bool
1591operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1592{
1593    return !(__y < __x);
1594}
1595
1596template <class _Iter1>
1597inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1598bool
1599operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
1600{
1601    return !(__x == __y);
1602}
1603
1604template <class _Iter1>
1605inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1606bool
1607operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
1608{
1609    return __y < __x;
1610}
1611
1612template <class _Iter1>
1613inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1614bool
1615operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
1616{
1617    return !(__x < __y);
1618}
1619
1620template <class _Iter1>
1621inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1622bool
1623operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
1624{
1625    return !(__y < __x);
1626}
1627
1628#ifndef _LIBCPP_CXX03_LANG
1629template <class _Iter1, class _Iter2>
1630inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1631auto
1632operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1633-> decltype(__x.base() - __y.base())
1634{
1635#if _LIBCPP_DEBUG_LEVEL >= 2
1636    _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
1637                   "Attempted to subtract incompatible iterators");
1638#endif
1639    return __x.base() - __y.base();
1640}
1641#else
1642template <class _Iter1, class _Iter2>
1643inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1644typename __wrap_iter<_Iter1>::difference_type
1645operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1646{
1647#if _LIBCPP_DEBUG_LEVEL >= 2
1648    _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
1649                   "Attempted to subtract incompatible iterators");
1650#endif
1651    return __x.base() - __y.base();
1652}
1653#endif
1654
1655template <class _Iter>
1656inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1657__wrap_iter<_Iter>
1658operator+(typename __wrap_iter<_Iter>::difference_type __n,
1659          __wrap_iter<_Iter> __x) _NOEXCEPT
1660{
1661    __x += __n;
1662    return __x;
1663}
1664
1665template <class _Iter>
1666struct __libcpp_is_trivial_iterator
1667    : public _LIBCPP_BOOL_CONSTANT(is_pointer<_Iter>::value) {};
1668
1669template <class _Iter>
1670struct __libcpp_is_trivial_iterator<move_iterator<_Iter> >
1671    : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
1672
1673template <class _Iter>
1674struct __libcpp_is_trivial_iterator<reverse_iterator<_Iter> >
1675    : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
1676
1677template <class _Iter>
1678struct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> >
1679    : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
1680
1681
1682template <class _Tp, size_t _Np>
1683_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1684_Tp*
1685begin(_Tp (&__array)[_Np])
1686{
1687    return __array;
1688}
1689
1690template <class _Tp, size_t _Np>
1691_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1692_Tp*
1693end(_Tp (&__array)[_Np])
1694{
1695    return __array + _Np;
1696}
1697
1698#if !defined(_LIBCPP_CXX03_LANG)
1699
1700template <class _Cp>
1701_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1702auto
1703begin(_Cp& __c) -> decltype(__c.begin())
1704{
1705    return __c.begin();
1706}
1707
1708template <class _Cp>
1709_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1710auto
1711begin(const _Cp& __c) -> decltype(__c.begin())
1712{
1713    return __c.begin();
1714}
1715
1716template <class _Cp>
1717_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1718auto
1719end(_Cp& __c) -> decltype(__c.end())
1720{
1721    return __c.end();
1722}
1723
1724template <class _Cp>
1725_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1726auto
1727end(const _Cp& __c) -> decltype(__c.end())
1728{
1729    return __c.end();
1730}
1731
1732#if _LIBCPP_STD_VER > 11
1733
1734template <class _Tp, size_t _Np>
1735_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1736reverse_iterator<_Tp*> rbegin(_Tp (&__array)[_Np])
1737{
1738    return reverse_iterator<_Tp*>(__array + _Np);
1739}
1740
1741template <class _Tp, size_t _Np>
1742_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1743reverse_iterator<_Tp*> rend(_Tp (&__array)[_Np])
1744{
1745    return reverse_iterator<_Tp*>(__array);
1746}
1747
1748template <class _Ep>
1749_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1750reverse_iterator<const _Ep*> rbegin(initializer_list<_Ep> __il)
1751{
1752    return reverse_iterator<const _Ep*>(__il.end());
1753}
1754
1755template <class _Ep>
1756_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1757reverse_iterator<const _Ep*> rend(initializer_list<_Ep> __il)
1758{
1759    return reverse_iterator<const _Ep*>(__il.begin());
1760}
1761
1762template <class _Cp>
1763_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1764auto cbegin(const _Cp& __c) -> decltype(_VSTD::begin(__c))
1765{
1766    return _VSTD::begin(__c);
1767}
1768
1769template <class _Cp>
1770_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1771auto cend(const _Cp& __c) -> decltype(_VSTD::end(__c))
1772{
1773    return _VSTD::end(__c);
1774}
1775
1776template <class _Cp>
1777_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1778auto rbegin(_Cp& __c) -> decltype(__c.rbegin())
1779{
1780    return __c.rbegin();
1781}
1782
1783template <class _Cp>
1784_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1785auto rbegin(const _Cp& __c) -> decltype(__c.rbegin())
1786{
1787    return __c.rbegin();
1788}
1789
1790template <class _Cp>
1791_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1792auto rend(_Cp& __c) -> decltype(__c.rend())
1793{
1794    return __c.rend();
1795}
1796
1797template <class _Cp>
1798_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1799auto rend(const _Cp& __c) -> decltype(__c.rend())
1800{
1801    return __c.rend();
1802}
1803
1804template <class _Cp>
1805_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1806auto crbegin(const _Cp& __c) -> decltype(_VSTD::rbegin(__c))
1807{
1808    return _VSTD::rbegin(__c);
1809}
1810
1811template <class _Cp>
1812_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1813auto crend(const _Cp& __c) -> decltype(_VSTD::rend(__c))
1814{
1815    return _VSTD::rend(__c);
1816}
1817
1818#endif
1819
1820
1821#else  // defined(_LIBCPP_CXX03_LANG)
1822
1823template <class _Cp>
1824_LIBCPP_INLINE_VISIBILITY
1825typename _Cp::iterator
1826begin(_Cp& __c)
1827{
1828    return __c.begin();
1829}
1830
1831template <class _Cp>
1832_LIBCPP_INLINE_VISIBILITY
1833typename _Cp::const_iterator
1834begin(const _Cp& __c)
1835{
1836    return __c.begin();
1837}
1838
1839template <class _Cp>
1840_LIBCPP_INLINE_VISIBILITY
1841typename _Cp::iterator
1842end(_Cp& __c)
1843{
1844    return __c.end();
1845}
1846
1847template <class _Cp>
1848_LIBCPP_INLINE_VISIBILITY
1849typename _Cp::const_iterator
1850end(const _Cp& __c)
1851{
1852    return __c.end();
1853}
1854
1855#endif  // !defined(_LIBCPP_CXX03_LANG)
1856
1857#if _LIBCPP_STD_VER > 14
1858
1859// #if _LIBCPP_STD_VER > 11
1860// template <>
1861// struct _LIBCPP_TEMPLATE_VIS plus<void>
1862// {
1863//     template <class _T1, class _T2>
1864//     _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1865//     auto operator()(_T1&& __t, _T2&& __u) const
1866//     _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
1867//     -> decltype        (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
1868//         { return        _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
1869//     typedef void is_transparent;
1870// };
1871// #endif
1872
1873template <class _Cont>
1874_LIBCPP_INLINE_VISIBILITY
1875constexpr auto size(const _Cont& __c)
1876_NOEXCEPT_(noexcept(__c.size()))
1877-> decltype        (__c.size())
1878{ return            __c.size(); }
1879
1880template <class _Tp, size_t _Sz>
1881_LIBCPP_INLINE_VISIBILITY
1882constexpr size_t size(const _Tp (&)[_Sz]) noexcept { return _Sz; }
1883
1884#if _LIBCPP_STD_VER > 17
1885template <class _Cont>
1886_LIBCPP_INLINE_VISIBILITY
1887constexpr auto ssize(const _Cont& __c)
1888_NOEXCEPT_(noexcept(static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size())))
1889->                              common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>
1890{ return            static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size()); }
1891
1892template <class _Tp, ptrdiff_t _Sz>
1893_LIBCPP_INLINE_VISIBILITY
1894constexpr ptrdiff_t ssize(const _Tp (&)[_Sz]) noexcept { return _Sz; }
1895#endif
1896
1897template <class _Cont>
1898_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
1899constexpr auto empty(const _Cont& __c)
1900_NOEXCEPT_(noexcept(__c.empty()))
1901-> decltype        (__c.empty())
1902{ return            __c.empty(); }
1903
1904template <class _Tp, size_t _Sz>
1905_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
1906constexpr bool empty(const _Tp (&)[_Sz]) noexcept { return false; }
1907
1908template <class _Ep>
1909_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
1910constexpr bool empty(initializer_list<_Ep> __il) noexcept { return __il.size() == 0; }
1911
1912template <class _Cont> constexpr
1913_LIBCPP_INLINE_VISIBILITY
1914auto data(_Cont& __c)
1915_NOEXCEPT_(noexcept(__c.data()))
1916-> decltype        (__c.data())
1917{ return            __c.data(); }
1918
1919template <class _Cont> constexpr
1920_LIBCPP_INLINE_VISIBILITY
1921auto data(const _Cont& __c)
1922_NOEXCEPT_(noexcept(__c.data()))
1923-> decltype        (__c.data())
1924{ return            __c.data(); }
1925
1926template <class _Tp, size_t _Sz>
1927_LIBCPP_INLINE_VISIBILITY
1928constexpr _Tp* data(_Tp (&__array)[_Sz]) noexcept { return __array; }
1929
1930template <class _Ep>
1931_LIBCPP_INLINE_VISIBILITY
1932constexpr const _Ep* data(initializer_list<_Ep> __il) noexcept { return __il.begin(); }
1933#endif
1934
1935
1936_LIBCPP_END_NAMESPACE_STD
1937
1938#endif  // _LIBCPP_ITERATOR
1939