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