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