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