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