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