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