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