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