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