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