1// -*- C++ -*-
2//===-------------------------- iterator ----------------------------------===//
3//
4//                     The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_ITERATOR
12#define _LIBCPP_ITERATOR
13
14/*
15    iterator synopsis
16
17namespace std
18{
19
20template<class Iterator>
21struct iterator_traits
22{
23    typedef typename Iterator::difference_type difference_type;
24    typedef typename Iterator::value_type value_type;
25    typedef typename Iterator::pointer pointer;
26    typedef typename Iterator::reference reference;
27    typedef typename Iterator::iterator_category iterator_category;
28};
29
30template<class T>
31struct iterator_traits<T*>
32{
33    typedef ptrdiff_t difference_type;
34    typedef T value_type;
35    typedef T* pointer;
36    typedef T& reference;
37    typedef random_access_iterator_tag iterator_category;
38};
39
40template<class T>
41struct iterator_traits<const T*>
42{
43    typedef ptrdiff_t difference_type;
44    typedef T value_type;
45    typedef const T* pointer;
46    typedef const T& reference;
47    typedef random_access_iterator_tag iterator_category;
48};
49
50template<class Category, class T, class Distance = ptrdiff_t,
51         class Pointer = T*, class Reference = T&>
52struct iterator
53{
54    typedef T         value_type;
55    typedef Distance  difference_type;
56    typedef Pointer   pointer;
57    typedef Reference reference;
58    typedef Category  iterator_category;
59};
60
61struct input_iterator_tag  {};
62struct output_iterator_tag {};
63struct forward_iterator_tag       : public input_iterator_tag         {};
64struct bidirectional_iterator_tag : public forward_iterator_tag       {};
65struct random_access_iterator_tag : public bidirectional_iterator_tag {};
66
67// extension: second argument not conforming to C++03
68template <class InputIterator>
69void advance(InputIterator& i,
70             typename iterator_traits<InputIterator>::difference_type n);
71
72template <class InputIterator>
73typename iterator_traits<InputIterator>::difference_type
74distance(InputIterator first, InputIterator last);
75
76template <class Iterator>
77class reverse_iterator
78    : public iterator<typename iterator_traits<Iterator>::iterator_category,
79                      typename iterator_traits<Iterator>::value_type,
80                      typename iterator_traits<Iterator>::difference_type,
81                      typename iterator_traits<Iterator>::pointer,
82                      typename iterator_traits<Iterator>::reference>
83{
84protected:
85    Iterator current;
86public:
87    typedef Iterator                                            iterator_type;
88    typedef typename iterator_traits<Iterator>::difference_type difference_type;
89    typedef typename iterator_traits<Iterator>::reference       reference;
90    typedef typename iterator_traits<Iterator>::pointer         pointer;
91
92    reverse_iterator();
93    explicit reverse_iterator(Iterator x);
94    template <class U> reverse_iterator(const reverse_iterator<U>& u);
95    Iterator base() const;
96    reference operator*() const;
97    pointer   operator->() const;
98    reverse_iterator& operator++();
99    reverse_iterator  operator++(int);
100    reverse_iterator& operator--();
101    reverse_iterator  operator--(int);
102    reverse_iterator  operator+ (difference_type n) const;
103    reverse_iterator& operator+=(difference_type n);
104    reverse_iterator  operator- (difference_type n) const;
105    reverse_iterator& operator-=(difference_type n);
106    reference         operator[](difference_type n) const;
107};
108
109template <class Iterator1, class Iterator2>
110bool
111operator==(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
112
113template <class Iterator1, class Iterator2>
114bool
115operator<(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
116
117template <class Iterator1, class Iterator2>
118bool
119operator!=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
120
121template <class Iterator1, class Iterator2>
122bool
123operator>(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
124
125template <class Iterator1, class Iterator2>
126bool
127operator>=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
128
129template <class Iterator1, class Iterator2>
130bool
131operator<=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
132
133template <class Iterator1, class Iterator2>
134typename reverse_iterator<Iterator1>::difference_type
135operator-(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
136
137template <class Iterator>
138reverse_iterator<Iterator>
139operator+(typename reverse_iterator<Iterator>::difference_type n, const reverse_iterator<Iterator>& x);
140
141template <class Iterator> reverse_iterator<Iterator> make_reverse_iterator(Iterator i); // C++14
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 back_insert_iterator<Cont>& 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 front_insert_iterator<Cont>& 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 insert_iterator<Cont>& 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 T, class charT = char, class traits = char_traits<charT>, class Distance = ptrdiff_t>
209class istream_iterator
210    : public iterator<input_iterator_tag, T, Distance, const T*, const T&>
211{
212public:
213    typedef charT char_type;
214    typedef traits traits_type;
215    typedef basic_istream<charT,traits> istream_type;
216
217    istream_iterator();
218    istream_iterator(istream_type& s);
219    istream_iterator(const istream_iterator& x);
220    ~istream_iterator();
221
222    const T& operator*() const;
223    const T* operator->() const;
224    istream_iterator& operator++();
225    istream_iterator  operator++(int);
226};
227
228template <class T, class charT, class traits, class Distance>
229bool operator==(const istream_iterator<T,charT,traits,Distance>& x,
230                const istream_iterator<T,charT,traits,Distance>& y);
231template <class T, class charT, class traits, class Distance>
232bool operator!=(const istream_iterator<T,charT,traits,Distance>& x,
233                const istream_iterator<T,charT,traits,Distance>& y);
234
235template <class T, class charT = char, class traits = char_traits<charT> >
236class ostream_iterator
237    : public iterator<output_iterator_tag, void, void, void ,void>
238{
239public:
240    typedef charT char_type;
241    typedef traits traits_type;
242    typedef basic_ostream<charT,traits> ostream_type;
243
244    ostream_iterator(ostream_type& s);
245    ostream_iterator(ostream_type& s, const charT* delimiter);
246    ostream_iterator(const ostream_iterator& x);
247    ~ostream_iterator();
248    ostream_iterator& operator=(const T& value);
249
250    ostream_iterator& operator*();
251    ostream_iterator& operator++();
252    ostream_iterator& operator++(int);
253};
254
255template<class charT, class traits = char_traits<charT> >
256class istreambuf_iterator
257    : public iterator<input_iterator_tag, charT,
258                      typename traits::off_type, unspecified,
259                      charT>
260{
261public:
262    typedef charT                         char_type;
263    typedef traits                        traits_type;
264    typedef typename traits::int_type     int_type;
265    typedef basic_streambuf<charT,traits> streambuf_type;
266    typedef basic_istream<charT,traits>   istream_type;
267
268    istreambuf_iterator() noexcept;
269    istreambuf_iterator(istream_type& s) noexcept;
270    istreambuf_iterator(streambuf_type* s) noexcept;
271    istreambuf_iterator(a-private-type) noexcept;
272
273    charT                operator*() const;
274    pointer operator->() const;
275    istreambuf_iterator& operator++();
276    a-private-type       operator++(int);
277
278    bool equal(const istreambuf_iterator& b) const;
279};
280
281template <class charT, class traits>
282bool operator==(const istreambuf_iterator<charT,traits>& a,
283                const istreambuf_iterator<charT,traits>& b);
284template <class charT, class traits>
285bool operator!=(const istreambuf_iterator<charT,traits>& a,
286                const istreambuf_iterator<charT,traits>& b);
287
288template <class charT, class traits = char_traits<charT> >
289class ostreambuf_iterator
290    : public iterator<output_iterator_tag, void, void, void, void>
291{
292public:
293    typedef charT                         char_type;
294    typedef traits                        traits_type;
295    typedef basic_streambuf<charT,traits> streambuf_type;
296    typedef basic_ostream<charT,traits>   ostream_type;
297
298    ostreambuf_iterator(ostream_type& s) noexcept;
299    ostreambuf_iterator(streambuf_type* s) noexcept;
300    ostreambuf_iterator& operator=(charT c);
301    ostreambuf_iterator& operator*();
302    ostreambuf_iterator& operator++();
303    ostreambuf_iterator& operator++(int);
304    bool failed() const noexcept;
305};
306
307template <class C> auto begin(C& c) -> decltype(c.begin());
308template <class C> auto begin(const C& c) -> decltype(c.begin());
309template <class C> auto end(C& c) -> decltype(c.end());
310template <class C> auto end(const C& c) -> decltype(c.end());
311template <class T, size_t N> T* begin(T (&array)[N]);
312template <class T, size_t N> T* end(T (&array)[N]);
313
314template <class C> auto cbegin(const C& c) -> decltype(std::begin(c));        // C++14
315template <class C> auto cend(const C& c) -> decltype(std::end(c));            // C++14
316template <class C> auto rbegin(C& c) -> decltype(c.rbegin());                 // C++14
317template <class C> auto rbegin(const C& c) -> decltype(c.rbegin());           // C++14
318template <class C> auto rend(C& c) -> decltype(c.rend());                     // C++14
319template <class C> auto rend(const C& c) -> decltype(c.rend());               // C++14
320template <class E> reverse_iterator<const E*> rbegin(initializer_list<E> il); // C++14
321template <class E> reverse_iterator<const E*> rend(initializer_list<E> il);   // C++14
322template <class T, size_t N> reverse_iterator<T*> rbegin(T (&array)[N]);      // C++14
323template <class T, size_t N> reverse_iterator<T*> rend(T (&array)[N]);        // C++14
324template <class C> auto crbegin(const C& c) -> decltype(std::rbegin(c));      // C++14
325template <class C> auto crend(const C& c) -> decltype(std::rend(c));          // C++14
326
327}  // std
328
329*/
330
331#include <__config>
332#include <__functional_base>
333#include <type_traits>
334#include <cstddef>
335#include <iosfwd>
336#include <initializer_list>
337#ifdef __APPLE__
338#include <Availability.h>
339#endif
340
341#include <__debug>
342
343#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
344#pragma GCC system_header
345#endif
346
347_LIBCPP_BEGIN_NAMESPACE_STD
348
349struct _LIBCPP_TYPE_VIS_ONLY input_iterator_tag {};
350struct _LIBCPP_TYPE_VIS_ONLY output_iterator_tag {};
351struct _LIBCPP_TYPE_VIS_ONLY forward_iterator_tag       : public input_iterator_tag {};
352struct _LIBCPP_TYPE_VIS_ONLY bidirectional_iterator_tag : public forward_iterator_tag {};
353struct _LIBCPP_TYPE_VIS_ONLY random_access_iterator_tag : public bidirectional_iterator_tag {};
354
355template <class _Tp>
356struct __has_iterator_category
357{
358private:
359    struct __two {char __lx; char __lxx;};
360    template <class _Up> static __two __test(...);
361    template <class _Up> static char __test(typename _Up::iterator_category* = 0);
362public:
363    static const bool value = sizeof(__test<_Tp>(0)) == 1;
364};
365
366template <class _Iter, bool> struct __iterator_traits_impl {};
367
368template <class _Iter>
369struct __iterator_traits_impl<_Iter, true>
370{
371    typedef typename _Iter::difference_type   difference_type;
372    typedef typename _Iter::value_type        value_type;
373    typedef typename _Iter::pointer           pointer;
374    typedef typename _Iter::reference         reference;
375    typedef typename _Iter::iterator_category iterator_category;
376};
377
378template <class _Iter, bool> struct __iterator_traits {};
379
380template <class _Iter>
381struct __iterator_traits<_Iter, true>
382    :  __iterator_traits_impl
383      <
384        _Iter,
385        is_convertible<typename _Iter::iterator_category, input_iterator_tag>::value ||
386        is_convertible<typename _Iter::iterator_category, output_iterator_tag>::value
387      >
388{};
389
390// iterator_traits<Iterator> will only have the nested types if Iterator::iterator_category
391//    exists.  Else iterator_traits<Iterator> will be an empty class.  This is a
392//    conforming extension which allows some programs to compile and behave as
393//    the client expects instead of failing at compile time.
394
395template <class _Iter>
396struct _LIBCPP_TYPE_VIS_ONLY iterator_traits
397    : __iterator_traits<_Iter, __has_iterator_category<_Iter>::value> {};
398
399template<class _Tp>
400struct _LIBCPP_TYPE_VIS_ONLY iterator_traits<_Tp*>
401{
402    typedef ptrdiff_t difference_type;
403    typedef typename remove_const<_Tp>::type value_type;
404    typedef _Tp* pointer;
405    typedef _Tp& reference;
406    typedef random_access_iterator_tag iterator_category;
407};
408
409template <class _Tp, class _Up, bool = __has_iterator_category<iterator_traits<_Tp> >::value>
410struct __has_iterator_category_convertible_to
411    : public integral_constant<bool, is_convertible<typename iterator_traits<_Tp>::iterator_category, _Up>::value>
412{};
413
414template <class _Tp, class _Up>
415struct __has_iterator_category_convertible_to<_Tp, _Up, false> : public false_type {};
416
417template <class _Tp>
418struct __is_input_iterator : public __has_iterator_category_convertible_to<_Tp, input_iterator_tag> {};
419
420template <class _Tp>
421struct __is_forward_iterator : public __has_iterator_category_convertible_to<_Tp, forward_iterator_tag> {};
422
423template <class _Tp>
424struct __is_bidirectional_iterator : public __has_iterator_category_convertible_to<_Tp, bidirectional_iterator_tag> {};
425
426template <class _Tp>
427struct __is_random_access_iterator : public __has_iterator_category_convertible_to<_Tp, random_access_iterator_tag> {};
428
429template<class _Category, class _Tp, class _Distance = ptrdiff_t,
430         class _Pointer = _Tp*, class _Reference = _Tp&>
431struct _LIBCPP_TYPE_VIS_ONLY iterator
432{
433    typedef _Tp        value_type;
434    typedef _Distance  difference_type;
435    typedef _Pointer   pointer;
436    typedef _Reference reference;
437    typedef _Category  iterator_category;
438};
439
440template <class _InputIter>
441inline _LIBCPP_INLINE_VISIBILITY
442void __advance(_InputIter& __i,
443             typename iterator_traits<_InputIter>::difference_type __n, input_iterator_tag)
444{
445    for (; __n > 0; --__n)
446        ++__i;
447}
448
449template <class _BiDirIter>
450inline _LIBCPP_INLINE_VISIBILITY
451void __advance(_BiDirIter& __i,
452             typename iterator_traits<_BiDirIter>::difference_type __n, bidirectional_iterator_tag)
453{
454    if (__n >= 0)
455        for (; __n > 0; --__n)
456            ++__i;
457    else
458        for (; __n < 0; ++__n)
459            --__i;
460}
461
462template <class _RandIter>
463inline _LIBCPP_INLINE_VISIBILITY
464void __advance(_RandIter& __i,
465             typename iterator_traits<_RandIter>::difference_type __n, random_access_iterator_tag)
466{
467   __i += __n;
468}
469
470template <class _InputIter>
471inline _LIBCPP_INLINE_VISIBILITY
472void advance(_InputIter& __i,
473             typename iterator_traits<_InputIter>::difference_type __n)
474{
475    __advance(__i, __n, typename iterator_traits<_InputIter>::iterator_category());
476}
477
478template <class _InputIter>
479inline _LIBCPP_INLINE_VISIBILITY
480typename iterator_traits<_InputIter>::difference_type
481__distance(_InputIter __first, _InputIter __last, input_iterator_tag)
482{
483    typename iterator_traits<_InputIter>::difference_type __r(0);
484    for (; __first != __last; ++__first)
485        ++__r;
486    return __r;
487}
488
489template <class _RandIter>
490inline _LIBCPP_INLINE_VISIBILITY
491typename iterator_traits<_RandIter>::difference_type
492__distance(_RandIter __first, _RandIter __last, random_access_iterator_tag)
493{
494    return __last - __first;
495}
496
497template <class _InputIter>
498inline _LIBCPP_INLINE_VISIBILITY
499typename iterator_traits<_InputIter>::difference_type
500distance(_InputIter __first, _InputIter __last)
501{
502    return __distance(__first, __last, typename iterator_traits<_InputIter>::iterator_category());
503}
504
505template <class _ForwardIter>
506inline _LIBCPP_INLINE_VISIBILITY
507_ForwardIter
508next(_ForwardIter __x,
509     typename iterator_traits<_ForwardIter>::difference_type __n = 1,
510     typename enable_if<__is_forward_iterator<_ForwardIter>::value>::type* = 0)
511{
512    _VSTD::advance(__x, __n);
513    return __x;
514}
515
516template <class _BidiretionalIter>
517inline _LIBCPP_INLINE_VISIBILITY
518_BidiretionalIter
519prev(_BidiretionalIter __x,
520     typename iterator_traits<_BidiretionalIter>::difference_type __n = 1,
521     typename enable_if<__is_bidirectional_iterator<_BidiretionalIter>::value>::type* = 0)
522{
523    _VSTD::advance(__x, -__n);
524    return __x;
525}
526
527template <class _Iter>
528class _LIBCPP_TYPE_VIS_ONLY reverse_iterator
529    : public iterator<typename iterator_traits<_Iter>::iterator_category,
530                      typename iterator_traits<_Iter>::value_type,
531                      typename iterator_traits<_Iter>::difference_type,
532                      typename iterator_traits<_Iter>::pointer,
533                      typename iterator_traits<_Iter>::reference>
534{
535private:
536    mutable _Iter __t;  // no longer used as of LWG #2360, not removed due to ABI break
537protected:
538    _Iter current;
539public:
540    typedef _Iter                                            iterator_type;
541    typedef typename iterator_traits<_Iter>::difference_type difference_type;
542    typedef typename iterator_traits<_Iter>::reference       reference;
543    typedef typename iterator_traits<_Iter>::pointer         pointer;
544
545    _LIBCPP_INLINE_VISIBILITY reverse_iterator() : current() {}
546    _LIBCPP_INLINE_VISIBILITY explicit reverse_iterator(_Iter __x) : __t(__x), current(__x) {}
547    template <class _Up> _LIBCPP_INLINE_VISIBILITY reverse_iterator(const reverse_iterator<_Up>& __u)
548        : __t(__u.base()), current(__u.base()) {}
549    _LIBCPP_INLINE_VISIBILITY _Iter base() const {return current;}
550    _LIBCPP_INLINE_VISIBILITY reference operator*() const {_Iter __tmp = current; return *--__tmp;}
551    _LIBCPP_INLINE_VISIBILITY pointer  operator->() const {return _VSTD::addressof(operator*());}
552    _LIBCPP_INLINE_VISIBILITY reverse_iterator& operator++() {--current; return *this;}
553    _LIBCPP_INLINE_VISIBILITY reverse_iterator  operator++(int)
554        {reverse_iterator __tmp(*this); --current; return __tmp;}
555    _LIBCPP_INLINE_VISIBILITY reverse_iterator& operator--() {++current; return *this;}
556    _LIBCPP_INLINE_VISIBILITY reverse_iterator  operator--(int)
557        {reverse_iterator __tmp(*this); ++current; return __tmp;}
558    _LIBCPP_INLINE_VISIBILITY reverse_iterator  operator+ (difference_type __n) const
559        {return reverse_iterator(current - __n);}
560    _LIBCPP_INLINE_VISIBILITY reverse_iterator& operator+=(difference_type __n)
561        {current -= __n; return *this;}
562    _LIBCPP_INLINE_VISIBILITY reverse_iterator  operator- (difference_type __n) const
563        {return reverse_iterator(current + __n);}
564    _LIBCPP_INLINE_VISIBILITY reverse_iterator& operator-=(difference_type __n)
565        {current += __n; return *this;}
566    _LIBCPP_INLINE_VISIBILITY reference         operator[](difference_type __n) const
567        {return current[-__n-1];}
568};
569
570template <class _Iter1, class _Iter2>
571inline _LIBCPP_INLINE_VISIBILITY
572bool
573operator==(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
574{
575    return __x.base() == __y.base();
576}
577
578template <class _Iter1, class _Iter2>
579inline _LIBCPP_INLINE_VISIBILITY
580bool
581operator<(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
582{
583    return __x.base() > __y.base();
584}
585
586template <class _Iter1, class _Iter2>
587inline _LIBCPP_INLINE_VISIBILITY
588bool
589operator!=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
590{
591    return __x.base() != __y.base();
592}
593
594template <class _Iter1, class _Iter2>
595inline _LIBCPP_INLINE_VISIBILITY
596bool
597operator>(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
598{
599    return __x.base() < __y.base();
600}
601
602template <class _Iter1, class _Iter2>
603inline _LIBCPP_INLINE_VISIBILITY
604bool
605operator>=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
606{
607    return __x.base() <= __y.base();
608}
609
610template <class _Iter1, class _Iter2>
611inline _LIBCPP_INLINE_VISIBILITY
612bool
613operator<=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
614{
615    return __x.base() >= __y.base();
616}
617
618template <class _Iter1, class _Iter2>
619inline _LIBCPP_INLINE_VISIBILITY
620typename reverse_iterator<_Iter1>::difference_type
621operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
622{
623    return __y.base() - __x.base();
624}
625
626template <class _Iter>
627inline _LIBCPP_INLINE_VISIBILITY
628reverse_iterator<_Iter>
629operator+(typename reverse_iterator<_Iter>::difference_type __n, const reverse_iterator<_Iter>& __x)
630{
631    return reverse_iterator<_Iter>(__x.base() - __n);
632}
633
634#if _LIBCPP_STD_VER > 11
635template <class _Iter>
636inline _LIBCPP_INLINE_VISIBILITY
637reverse_iterator<_Iter> make_reverse_iterator(_Iter __i)
638{
639    return reverse_iterator<_Iter>(__i);
640}
641#endif
642
643template <class _Container>
644class _LIBCPP_TYPE_VIS_ONLY back_insert_iterator
645    : public iterator<output_iterator_tag,
646                      void,
647                      void,
648                      void,
649                      back_insert_iterator<_Container>&>
650{
651protected:
652    _Container* container;
653public:
654    typedef _Container container_type;
655
656    _LIBCPP_INLINE_VISIBILITY explicit back_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
657    _LIBCPP_INLINE_VISIBILITY back_insert_iterator& operator=(const typename _Container::value_type& __value_)
658        {container->push_back(__value_); return *this;}
659#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
660    _LIBCPP_INLINE_VISIBILITY back_insert_iterator& operator=(typename _Container::value_type&& __value_)
661        {container->push_back(_VSTD::move(__value_)); return *this;}
662#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
663    _LIBCPP_INLINE_VISIBILITY back_insert_iterator& operator*()     {return *this;}
664    _LIBCPP_INLINE_VISIBILITY back_insert_iterator& operator++()    {return *this;}
665    _LIBCPP_INLINE_VISIBILITY back_insert_iterator  operator++(int) {return *this;}
666};
667
668template <class _Container>
669inline _LIBCPP_INLINE_VISIBILITY
670back_insert_iterator<_Container>
671back_inserter(_Container& __x)
672{
673    return back_insert_iterator<_Container>(__x);
674}
675
676template <class _Container>
677class _LIBCPP_TYPE_VIS_ONLY front_insert_iterator
678    : public iterator<output_iterator_tag,
679                      void,
680                      void,
681                      void,
682                      front_insert_iterator<_Container>&>
683{
684protected:
685    _Container* container;
686public:
687    typedef _Container container_type;
688
689    _LIBCPP_INLINE_VISIBILITY explicit front_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
690    _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator=(const typename _Container::value_type& __value_)
691        {container->push_front(__value_); return *this;}
692#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
693    _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator=(typename _Container::value_type&& __value_)
694        {container->push_front(_VSTD::move(__value_)); return *this;}
695#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
696    _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator*()     {return *this;}
697    _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator++()    {return *this;}
698    _LIBCPP_INLINE_VISIBILITY front_insert_iterator  operator++(int) {return *this;}
699};
700
701template <class _Container>
702inline _LIBCPP_INLINE_VISIBILITY
703front_insert_iterator<_Container>
704front_inserter(_Container& __x)
705{
706    return front_insert_iterator<_Container>(__x);
707}
708
709template <class _Container>
710class _LIBCPP_TYPE_VIS_ONLY insert_iterator
711    : public iterator<output_iterator_tag,
712                      void,
713                      void,
714                      void,
715                      insert_iterator<_Container>&>
716{
717protected:
718    _Container* container;
719    typename _Container::iterator iter;
720public:
721    typedef _Container container_type;
722
723    _LIBCPP_INLINE_VISIBILITY insert_iterator(_Container& __x, typename _Container::iterator __i)
724        : container(_VSTD::addressof(__x)), iter(__i) {}
725    _LIBCPP_INLINE_VISIBILITY insert_iterator& operator=(const typename _Container::value_type& __value_)
726        {iter = container->insert(iter, __value_); ++iter; return *this;}
727#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
728    _LIBCPP_INLINE_VISIBILITY insert_iterator& operator=(typename _Container::value_type&& __value_)
729        {iter = container->insert(iter, _VSTD::move(__value_)); ++iter; return *this;}
730#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
731    _LIBCPP_INLINE_VISIBILITY insert_iterator& operator*()        {return *this;}
732    _LIBCPP_INLINE_VISIBILITY insert_iterator& operator++()       {return *this;}
733    _LIBCPP_INLINE_VISIBILITY insert_iterator& operator++(int)    {return *this;}
734};
735
736template <class _Container>
737inline _LIBCPP_INLINE_VISIBILITY
738insert_iterator<_Container>
739inserter(_Container& __x, typename _Container::iterator __i)
740{
741    return insert_iterator<_Container>(__x, __i);
742}
743
744template <class _Tp, class _CharT = char,
745          class _Traits = char_traits<_CharT>, class _Distance = ptrdiff_t>
746class _LIBCPP_TYPE_VIS_ONLY istream_iterator
747    : public iterator<input_iterator_tag, _Tp, _Distance, const _Tp*, const _Tp&>
748{
749public:
750    typedef _CharT char_type;
751    typedef _Traits traits_type;
752    typedef basic_istream<_CharT,_Traits> istream_type;
753private:
754    istream_type* __in_stream_;
755    _Tp __value_;
756public:
757    _LIBCPP_INLINE_VISIBILITY istream_iterator() : __in_stream_(0) {}
758    _LIBCPP_INLINE_VISIBILITY istream_iterator(istream_type& __s) : __in_stream_(&__s)
759        {
760            if (!(*__in_stream_ >> __value_))
761                __in_stream_ = 0;
762        }
763
764    _LIBCPP_INLINE_VISIBILITY const _Tp& operator*() const {return __value_;}
765    _LIBCPP_INLINE_VISIBILITY const _Tp* operator->() const {return &(operator*());}
766    _LIBCPP_INLINE_VISIBILITY istream_iterator& operator++()
767        {
768            if (!(*__in_stream_ >> __value_))
769                __in_stream_ = 0;
770            return *this;
771        }
772    _LIBCPP_INLINE_VISIBILITY istream_iterator  operator++(int)
773        {istream_iterator __t(*this); ++(*this); return __t;}
774
775    friend _LIBCPP_INLINE_VISIBILITY
776    bool operator==(const istream_iterator& __x, const istream_iterator& __y)
777        {return __x.__in_stream_ == __y.__in_stream_;}
778
779    friend _LIBCPP_INLINE_VISIBILITY
780    bool operator!=(const istream_iterator& __x, const istream_iterator& __y)
781        {return !(__x == __y);}
782};
783
784template <class _Tp, class _CharT = char, class _Traits = char_traits<_CharT> >
785class _LIBCPP_TYPE_VIS_ONLY ostream_iterator
786    : public iterator<output_iterator_tag, void, void, void, void>
787{
788public:
789    typedef _CharT char_type;
790    typedef _Traits traits_type;
791    typedef basic_ostream<_CharT,_Traits> ostream_type;
792private:
793    ostream_type* __out_stream_;
794    const char_type* __delim_;
795public:
796    _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s)
797        : __out_stream_(&__s), __delim_(0) {}
798    _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s, const _CharT* __delimiter)
799        : __out_stream_(&__s), __delim_(__delimiter) {}
800    _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator=(const _Tp& __value_)
801        {
802            *__out_stream_ << __value_;
803            if (__delim_)
804                *__out_stream_ << __delim_;
805            return *this;
806        }
807
808    _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator*()     {return *this;}
809    _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++()    {return *this;}
810    _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++(int) {return *this;}
811};
812
813template<class _CharT, class _Traits>
814class _LIBCPP_TYPE_VIS_ONLY istreambuf_iterator
815    : public iterator<input_iterator_tag, _CharT,
816                      typename _Traits::off_type, _CharT*,
817                      _CharT>
818{
819public:
820    typedef _CharT                          char_type;
821    typedef _Traits                         traits_type;
822    typedef typename _Traits::int_type      int_type;
823    typedef basic_streambuf<_CharT,_Traits> streambuf_type;
824    typedef basic_istream<_CharT,_Traits>   istream_type;
825private:
826    mutable streambuf_type* __sbuf_;
827
828    class __proxy
829    {
830        char_type __keep_;
831        streambuf_type* __sbuf_;
832        _LIBCPP_INLINE_VISIBILITY __proxy(char_type __c, streambuf_type* __s)
833            : __keep_(__c), __sbuf_(__s) {}
834        friend class istreambuf_iterator;
835    public:
836        _LIBCPP_INLINE_VISIBILITY char_type operator*() const {return __keep_;}
837    };
838
839    _LIBCPP_INLINE_VISIBILITY
840    bool __test_for_eof() const
841    {
842        if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof()))
843            __sbuf_ = 0;
844        return __sbuf_ == 0;
845    }
846public:
847    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istreambuf_iterator() _NOEXCEPT : __sbuf_(0) {}
848    _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(istream_type& __s) _NOEXCEPT
849        : __sbuf_(__s.rdbuf()) {}
850    _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(streambuf_type* __s) _NOEXCEPT
851        : __sbuf_(__s) {}
852    _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(const __proxy& __p) _NOEXCEPT
853        : __sbuf_(__p.__sbuf_) {}
854
855    _LIBCPP_INLINE_VISIBILITY char_type  operator*() const
856        {return static_cast<char_type>(__sbuf_->sgetc());}
857    _LIBCPP_INLINE_VISIBILITY char_type* operator->() const {return nullptr;}
858    _LIBCPP_INLINE_VISIBILITY istreambuf_iterator& operator++()
859        {
860            __sbuf_->sbumpc();
861            return *this;
862        }
863    _LIBCPP_INLINE_VISIBILITY __proxy              operator++(int)
864        {
865            return __proxy(__sbuf_->sbumpc(), __sbuf_);
866        }
867
868    _LIBCPP_INLINE_VISIBILITY bool equal(const istreambuf_iterator& __b) const
869        {return __test_for_eof() == __b.__test_for_eof();}
870};
871
872template <class _CharT, class _Traits>
873inline _LIBCPP_INLINE_VISIBILITY
874bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,
875                const istreambuf_iterator<_CharT,_Traits>& __b)
876                {return __a.equal(__b);}
877
878template <class _CharT, class _Traits>
879inline _LIBCPP_INLINE_VISIBILITY
880bool operator!=(const istreambuf_iterator<_CharT,_Traits>& __a,
881                const istreambuf_iterator<_CharT,_Traits>& __b)
882                {return !__a.equal(__b);}
883
884template <class _CharT, class _Traits>
885class _LIBCPP_TYPE_VIS_ONLY ostreambuf_iterator
886    : public iterator<output_iterator_tag, void, void, void, void>
887{
888public:
889    typedef _CharT                          char_type;
890    typedef _Traits                         traits_type;
891    typedef basic_streambuf<_CharT,_Traits> streambuf_type;
892    typedef basic_ostream<_CharT,_Traits>   ostream_type;
893private:
894    streambuf_type* __sbuf_;
895public:
896    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(ostream_type& __s) _NOEXCEPT
897        : __sbuf_(__s.rdbuf()) {}
898    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(streambuf_type* __s) _NOEXCEPT
899        : __sbuf_(__s) {}
900    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator=(_CharT __c)
901        {
902            if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sputc(__c), traits_type::eof()))
903                __sbuf_ = 0;
904            return *this;
905        }
906    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator*()     {return *this;}
907    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++()    {return *this;}
908    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++(int) {return *this;}
909    _LIBCPP_INLINE_VISIBILITY bool failed() const _NOEXCEPT {return __sbuf_ == 0;}
910
911#if !defined(__APPLE__) || \
912    (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED > __MAC_10_8) || \
913    (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_6_0)
914
915    template <class _Ch, class _Tr>
916    friend
917    _LIBCPP_HIDDEN
918    ostreambuf_iterator<_Ch, _Tr>
919    __pad_and_output(ostreambuf_iterator<_Ch, _Tr> __s,
920                     const _Ch* __ob, const _Ch* __op, const _Ch* __oe,
921                     ios_base& __iob, _Ch __fl);
922#endif
923};
924
925template <class _Iter>
926class _LIBCPP_TYPE_VIS_ONLY move_iterator
927{
928private:
929    _Iter __i;
930public:
931    typedef _Iter                                            iterator_type;
932    typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
933    typedef typename iterator_traits<iterator_type>::value_type value_type;
934    typedef typename iterator_traits<iterator_type>::difference_type difference_type;
935    typedef typename iterator_traits<iterator_type>::pointer pointer;
936#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
937    typedef value_type&& reference;
938#else
939    typedef typename iterator_traits<iterator_type>::reference reference;
940#endif
941
942    _LIBCPP_INLINE_VISIBILITY move_iterator() : __i() {}
943    _LIBCPP_INLINE_VISIBILITY explicit move_iterator(_Iter __x) : __i(__x) {}
944    template <class _Up> _LIBCPP_INLINE_VISIBILITY move_iterator(const move_iterator<_Up>& __u)
945        : __i(__u.base()) {}
946    _LIBCPP_INLINE_VISIBILITY _Iter base() const {return __i;}
947    _LIBCPP_INLINE_VISIBILITY reference operator*() const {
948      return static_cast<reference>(*__i);
949    }
950    _LIBCPP_INLINE_VISIBILITY pointer  operator->() const {
951      typename iterator_traits<iterator_type>::reference __ref = *__i;
952      return &__ref;
953    }
954    _LIBCPP_INLINE_VISIBILITY move_iterator& operator++() {++__i; return *this;}
955    _LIBCPP_INLINE_VISIBILITY move_iterator  operator++(int)
956        {move_iterator __tmp(*this); ++__i; return __tmp;}
957    _LIBCPP_INLINE_VISIBILITY move_iterator& operator--() {--__i; return *this;}
958    _LIBCPP_INLINE_VISIBILITY move_iterator  operator--(int)
959        {move_iterator __tmp(*this); --__i; return __tmp;}
960    _LIBCPP_INLINE_VISIBILITY move_iterator  operator+ (difference_type __n) const
961        {return move_iterator(__i + __n);}
962    _LIBCPP_INLINE_VISIBILITY move_iterator& operator+=(difference_type __n)
963        {__i += __n; return *this;}
964    _LIBCPP_INLINE_VISIBILITY move_iterator  operator- (difference_type __n) const
965        {return move_iterator(__i - __n);}
966    _LIBCPP_INLINE_VISIBILITY move_iterator& operator-=(difference_type __n)
967        {__i -= __n; return *this;}
968    _LIBCPP_INLINE_VISIBILITY reference         operator[](difference_type __n) const
969    {
970      return static_cast<reference>(__i[__n]);
971    }
972};
973
974template <class _Iter1, class _Iter2>
975inline _LIBCPP_INLINE_VISIBILITY
976bool
977operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
978{
979    return __x.base() == __y.base();
980}
981
982template <class _Iter1, class _Iter2>
983inline _LIBCPP_INLINE_VISIBILITY
984bool
985operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
986{
987    return __x.base() < __y.base();
988}
989
990template <class _Iter1, class _Iter2>
991inline _LIBCPP_INLINE_VISIBILITY
992bool
993operator!=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
994{
995    return __x.base() != __y.base();
996}
997
998template <class _Iter1, class _Iter2>
999inline _LIBCPP_INLINE_VISIBILITY
1000bool
1001operator>(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1002{
1003    return __x.base() > __y.base();
1004}
1005
1006template <class _Iter1, class _Iter2>
1007inline _LIBCPP_INLINE_VISIBILITY
1008bool
1009operator>=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1010{
1011    return __x.base() >= __y.base();
1012}
1013
1014template <class _Iter1, class _Iter2>
1015inline _LIBCPP_INLINE_VISIBILITY
1016bool
1017operator<=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1018{
1019    return __x.base() <= __y.base();
1020}
1021
1022template <class _Iter1, class _Iter2>
1023inline _LIBCPP_INLINE_VISIBILITY
1024typename move_iterator<_Iter1>::difference_type
1025operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1026{
1027    return __x.base() - __y.base();
1028}
1029
1030template <class _Iter>
1031inline _LIBCPP_INLINE_VISIBILITY
1032move_iterator<_Iter>
1033operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
1034{
1035    return move_iterator<_Iter>(__x.base() + __n);
1036}
1037
1038template <class _Iter>
1039inline _LIBCPP_INLINE_VISIBILITY
1040move_iterator<_Iter>
1041make_move_iterator(_Iter __i)
1042{
1043    return move_iterator<_Iter>(__i);
1044}
1045
1046// __wrap_iter
1047
1048template <class _Iter> class __wrap_iter;
1049
1050template <class _Iter1, class _Iter2>
1051_LIBCPP_INLINE_VISIBILITY
1052bool
1053operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1054
1055template <class _Iter1, class _Iter2>
1056_LIBCPP_INLINE_VISIBILITY
1057bool
1058operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1059
1060template <class _Iter1, class _Iter2>
1061_LIBCPP_INLINE_VISIBILITY
1062bool
1063operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1064
1065template <class _Iter1, class _Iter2>
1066_LIBCPP_INLINE_VISIBILITY
1067bool
1068operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1069
1070template <class _Iter1, class _Iter2>
1071_LIBCPP_INLINE_VISIBILITY
1072bool
1073operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1074
1075template <class _Iter1, class _Iter2>
1076_LIBCPP_INLINE_VISIBILITY
1077bool
1078operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1079
1080template <class _Iter1, class _Iter2>
1081_LIBCPP_INLINE_VISIBILITY
1082typename __wrap_iter<_Iter1>::difference_type
1083operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1084
1085template <class _Iter>
1086_LIBCPP_INLINE_VISIBILITY
1087__wrap_iter<_Iter>
1088operator+(typename __wrap_iter<_Iter>::difference_type, __wrap_iter<_Iter>) _NOEXCEPT;
1089
1090template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY copy(_Ip, _Ip, _Op);
1091template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY copy_backward(_B1, _B1, _B2);
1092template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY move(_Ip, _Ip, _Op);
1093template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY move_backward(_B1, _B1, _B2);
1094
1095template <class _Tp>
1096_LIBCPP_INLINE_VISIBILITY
1097typename enable_if
1098<
1099    is_trivially_copy_assignable<_Tp>::value,
1100    _Tp*
1101>::type
1102__unwrap_iter(__wrap_iter<_Tp*>);
1103
1104template <class _Iter>
1105class __wrap_iter
1106{
1107public:
1108    typedef _Iter                                                      iterator_type;
1109    typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
1110    typedef typename iterator_traits<iterator_type>::value_type        value_type;
1111    typedef typename iterator_traits<iterator_type>::difference_type   difference_type;
1112    typedef typename iterator_traits<iterator_type>::pointer           pointer;
1113    typedef typename iterator_traits<iterator_type>::reference         reference;
1114private:
1115    iterator_type __i;
1116public:
1117    _LIBCPP_INLINE_VISIBILITY __wrap_iter() _NOEXCEPT
1118#if _LIBCPP_STD_VER > 11
1119                : __i{}
1120#endif
1121    {
1122#if _LIBCPP_DEBUG_LEVEL >= 2
1123        __get_db()->__insert_i(this);
1124#endif
1125    }
1126    template <class _Up> _LIBCPP_INLINE_VISIBILITY __wrap_iter(const __wrap_iter<_Up>& __u,
1127        typename enable_if<is_convertible<_Up, iterator_type>::value>::type* = 0) _NOEXCEPT
1128        : __i(__u.base())
1129    {
1130#if _LIBCPP_DEBUG_LEVEL >= 2
1131        __get_db()->__iterator_copy(this, &__u);
1132#endif
1133    }
1134#if _LIBCPP_DEBUG_LEVEL >= 2
1135    _LIBCPP_INLINE_VISIBILITY
1136    __wrap_iter(const __wrap_iter& __x)
1137        : __i(__x.base())
1138    {
1139        __get_db()->__iterator_copy(this, &__x);
1140    }
1141    _LIBCPP_INLINE_VISIBILITY
1142    __wrap_iter& operator=(const __wrap_iter& __x)
1143    {
1144        if (this != &__x)
1145        {
1146            __get_db()->__iterator_copy(this, &__x);
1147            __i = __x.__i;
1148        }
1149        return *this;
1150    }
1151    _LIBCPP_INLINE_VISIBILITY
1152    ~__wrap_iter()
1153    {
1154        __get_db()->__erase_i(this);
1155    }
1156#endif
1157    _LIBCPP_INLINE_VISIBILITY reference operator*() const _NOEXCEPT
1158    {
1159#if _LIBCPP_DEBUG_LEVEL >= 2
1160        _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1161                       "Attempted to dereference a non-dereferenceable iterator");
1162#endif
1163        return *__i;
1164    }
1165    _LIBCPP_INLINE_VISIBILITY pointer  operator->() const _NOEXCEPT
1166    {
1167#if _LIBCPP_DEBUG_LEVEL >= 2
1168        _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1169                       "Attempted to dereference a non-dereferenceable iterator");
1170#endif
1171        return (pointer)&reinterpret_cast<const volatile char&>(*__i);
1172    }
1173    _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator++() _NOEXCEPT
1174    {
1175#if _LIBCPP_DEBUG_LEVEL >= 2
1176        _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1177                       "Attempted to increment non-incrementable iterator");
1178#endif
1179        ++__i;
1180        return *this;
1181    }
1182    _LIBCPP_INLINE_VISIBILITY __wrap_iter  operator++(int) _NOEXCEPT
1183        {__wrap_iter __tmp(*this); ++(*this); return __tmp;}
1184    _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator--() _NOEXCEPT
1185    {
1186#if _LIBCPP_DEBUG_LEVEL >= 2
1187        _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
1188                       "Attempted to decrement non-decrementable iterator");
1189#endif
1190        --__i;
1191        return *this;
1192    }
1193    _LIBCPP_INLINE_VISIBILITY __wrap_iter  operator--(int) _NOEXCEPT
1194        {__wrap_iter __tmp(*this); --(*this); return __tmp;}
1195    _LIBCPP_INLINE_VISIBILITY __wrap_iter  operator+ (difference_type __n) const _NOEXCEPT
1196        {__wrap_iter __w(*this); __w += __n; return __w;}
1197    _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator+=(difference_type __n) _NOEXCEPT
1198    {
1199#if _LIBCPP_DEBUG_LEVEL >= 2
1200        _LIBCPP_ASSERT(__get_const_db()->__addable(this, __n),
1201                   "Attempted to add/subtract iterator outside of valid range");
1202#endif
1203        __i += __n;
1204        return *this;
1205    }
1206    _LIBCPP_INLINE_VISIBILITY __wrap_iter  operator- (difference_type __n) const _NOEXCEPT
1207        {return *this + (-__n);}
1208    _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator-=(difference_type __n) _NOEXCEPT
1209        {*this += -__n; return *this;}
1210    _LIBCPP_INLINE_VISIBILITY reference        operator[](difference_type __n) const _NOEXCEPT
1211    {
1212#if _LIBCPP_DEBUG_LEVEL >= 2
1213        _LIBCPP_ASSERT(__get_const_db()->__subscriptable(this, __n),
1214                   "Attempted to subscript iterator outside of valid range");
1215#endif
1216        return __i[__n];
1217    }
1218
1219    _LIBCPP_INLINE_VISIBILITY iterator_type base() const _NOEXCEPT {return __i;}
1220
1221private:
1222#if _LIBCPP_DEBUG_LEVEL >= 2
1223    _LIBCPP_INLINE_VISIBILITY __wrap_iter(const void* __p, iterator_type __x) : __i(__x)
1224    {
1225        __get_db()->__insert_ic(this, __p);
1226    }
1227#else
1228    _LIBCPP_INLINE_VISIBILITY __wrap_iter(iterator_type __x) _NOEXCEPT : __i(__x) {}
1229#endif
1230
1231    template <class _Up> friend class __wrap_iter;
1232    template <class _CharT, class _Traits, class _Alloc> friend class basic_string;
1233    template <class _Tp, class _Alloc> friend class vector;
1234
1235    template <class _Iter1, class _Iter2>
1236    friend
1237    bool
1238    operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1239
1240    template <class _Iter1, class _Iter2>
1241    friend
1242    bool
1243    operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1244
1245    template <class _Iter1, class _Iter2>
1246    friend
1247    bool
1248    operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1249
1250    template <class _Iter1, class _Iter2>
1251    friend
1252    bool
1253    operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1254
1255    template <class _Iter1, class _Iter2>
1256    friend
1257    bool
1258    operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1259
1260    template <class _Iter1, class _Iter2>
1261    friend
1262    bool
1263    operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1264
1265    template <class _Iter1, class _Iter2>
1266    friend
1267    typename __wrap_iter<_Iter1>::difference_type
1268    operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1269
1270    template <class _Iter1>
1271    friend
1272    __wrap_iter<_Iter1>
1273    operator+(typename __wrap_iter<_Iter1>::difference_type, __wrap_iter<_Iter1>) _NOEXCEPT;
1274
1275    template <class _Ip, class _Op> friend _Op copy(_Ip, _Ip, _Op);
1276    template <class _B1, class _B2> friend _B2 copy_backward(_B1, _B1, _B2);
1277    template <class _Ip, class _Op> friend _Op move(_Ip, _Ip, _Op);
1278    template <class _B1, class _B2> friend _B2 move_backward(_B1, _B1, _B2);
1279
1280    template <class _Tp>
1281    friend
1282    typename enable_if
1283    <
1284        is_trivially_copy_assignable<_Tp>::value,
1285        _Tp*
1286    >::type
1287    __unwrap_iter(__wrap_iter<_Tp*>);
1288};
1289
1290template <class _Iter1, class _Iter2>
1291inline _LIBCPP_INLINE_VISIBILITY
1292bool
1293operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1294{
1295    return __x.base() == __y.base();
1296}
1297
1298template <class _Iter1, class _Iter2>
1299inline _LIBCPP_INLINE_VISIBILITY
1300bool
1301operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1302{
1303#if _LIBCPP_DEBUG_LEVEL >= 2
1304    _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
1305                   "Attempted to compare incomparable iterators");
1306#endif
1307    return __x.base() < __y.base();
1308}
1309
1310template <class _Iter1, class _Iter2>
1311inline _LIBCPP_INLINE_VISIBILITY
1312bool
1313operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1314{
1315    return !(__x == __y);
1316}
1317
1318template <class _Iter1, class _Iter2>
1319inline _LIBCPP_INLINE_VISIBILITY
1320bool
1321operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1322{
1323    return __y < __x;
1324}
1325
1326template <class _Iter1, class _Iter2>
1327inline _LIBCPP_INLINE_VISIBILITY
1328bool
1329operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1330{
1331    return !(__x < __y);
1332}
1333
1334template <class _Iter1, class _Iter2>
1335inline _LIBCPP_INLINE_VISIBILITY
1336bool
1337operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1338{
1339    return !(__y < __x);
1340}
1341
1342template <class _Iter1>
1343inline _LIBCPP_INLINE_VISIBILITY
1344bool
1345operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
1346{
1347    return !(__x == __y);
1348}
1349
1350template <class _Iter1>
1351inline _LIBCPP_INLINE_VISIBILITY
1352bool
1353operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
1354{
1355    return __y < __x;
1356}
1357
1358template <class _Iter1>
1359inline _LIBCPP_INLINE_VISIBILITY
1360bool
1361operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
1362{
1363    return !(__x < __y);
1364}
1365
1366template <class _Iter1>
1367inline _LIBCPP_INLINE_VISIBILITY
1368bool
1369operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
1370{
1371    return !(__y < __x);
1372}
1373
1374template <class _Iter1, class _Iter2>
1375inline _LIBCPP_INLINE_VISIBILITY
1376typename __wrap_iter<_Iter1>::difference_type
1377operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1378{
1379#if _LIBCPP_DEBUG_LEVEL >= 2
1380    _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
1381                   "Attempted to subtract incompatible iterators");
1382#endif
1383    return __x.base() - __y.base();
1384}
1385
1386template <class _Iter>
1387inline _LIBCPP_INLINE_VISIBILITY
1388__wrap_iter<_Iter>
1389operator+(typename __wrap_iter<_Iter>::difference_type __n,
1390          __wrap_iter<_Iter> __x) _NOEXCEPT
1391{
1392    __x += __n;
1393    return __x;
1394}
1395
1396template <class _Tp, size_t _Np>
1397inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1398_Tp*
1399begin(_Tp (&__array)[_Np])
1400{
1401    return __array;
1402}
1403
1404template <class _Tp, size_t _Np>
1405inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1406_Tp*
1407end(_Tp (&__array)[_Np])
1408{
1409    return __array + _Np;
1410}
1411
1412#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_TRAILING_RETURN)
1413
1414template <class _Cp>
1415inline _LIBCPP_INLINE_VISIBILITY
1416auto
1417begin(_Cp& __c) -> decltype(__c.begin())
1418{
1419    return __c.begin();
1420}
1421
1422template <class _Cp>
1423inline _LIBCPP_INLINE_VISIBILITY
1424auto
1425begin(const _Cp& __c) -> decltype(__c.begin())
1426{
1427    return __c.begin();
1428}
1429
1430template <class _Cp>
1431inline _LIBCPP_INLINE_VISIBILITY
1432auto
1433end(_Cp& __c) -> decltype(__c.end())
1434{
1435    return __c.end();
1436}
1437
1438template <class _Cp>
1439inline _LIBCPP_INLINE_VISIBILITY
1440auto
1441end(const _Cp& __c) -> decltype(__c.end())
1442{
1443    return __c.end();
1444}
1445
1446#if _LIBCPP_STD_VER > 11
1447
1448template <class _Tp, size_t _Np>
1449inline _LIBCPP_INLINE_VISIBILITY
1450reverse_iterator<_Tp*> rbegin(_Tp (&__array)[_Np])
1451{
1452    return reverse_iterator<_Tp*>(__array + _Np);
1453}
1454
1455template <class _Tp, size_t _Np>
1456inline _LIBCPP_INLINE_VISIBILITY
1457reverse_iterator<_Tp*> rend(_Tp (&__array)[_Np])
1458{
1459    return reverse_iterator<_Tp*>(__array);
1460}
1461
1462template <class _Ep>
1463inline _LIBCPP_INLINE_VISIBILITY
1464reverse_iterator<const _Ep*> rbegin(initializer_list<_Ep> __il)
1465{
1466    return reverse_iterator<const _Ep*>(__il.end());
1467}
1468
1469template <class _Ep>
1470inline _LIBCPP_INLINE_VISIBILITY
1471reverse_iterator<const _Ep*> rend(initializer_list<_Ep> __il)
1472{
1473    return reverse_iterator<const _Ep*>(__il.begin());
1474}
1475
1476template <class _Cp>
1477inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1478auto cbegin(const _Cp& __c) -> decltype(begin(__c))
1479{
1480    return begin(__c);
1481}
1482
1483template <class _Cp>
1484inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1485auto cend(const _Cp& __c) -> decltype(end(__c))
1486{
1487    return end(__c);
1488}
1489
1490template <class _Cp>
1491inline _LIBCPP_INLINE_VISIBILITY
1492auto rbegin(_Cp& __c) -> decltype(__c.rbegin())
1493{
1494    return __c.rbegin();
1495}
1496
1497template <class _Cp>
1498inline _LIBCPP_INLINE_VISIBILITY
1499auto rbegin(const _Cp& __c) -> decltype(__c.rbegin())
1500{
1501    return __c.rbegin();
1502}
1503
1504template <class _Cp>
1505inline _LIBCPP_INLINE_VISIBILITY
1506auto rend(_Cp& __c) -> decltype(__c.rend())
1507{
1508    return __c.rend();
1509}
1510
1511template <class _Cp>
1512inline _LIBCPP_INLINE_VISIBILITY
1513auto rend(const _Cp& __c) -> decltype(__c.rend())
1514{
1515    return __c.rend();
1516}
1517
1518template <class _Cp>
1519inline _LIBCPP_INLINE_VISIBILITY
1520auto crbegin(const _Cp& __c) -> decltype(rbegin(__c))
1521{
1522    return rbegin(__c);
1523}
1524
1525template <class _Cp>
1526inline _LIBCPP_INLINE_VISIBILITY
1527auto crend(const _Cp& __c) -> decltype(rend(__c))
1528{
1529    return rend(__c);
1530}
1531
1532#endif
1533
1534
1535#else  // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_TRAILING_RETURN)
1536
1537template <class _Cp>
1538inline _LIBCPP_INLINE_VISIBILITY
1539typename _Cp::iterator
1540begin(_Cp& __c)
1541{
1542    return __c.begin();
1543}
1544
1545template <class _Cp>
1546inline _LIBCPP_INLINE_VISIBILITY
1547typename _Cp::const_iterator
1548begin(const _Cp& __c)
1549{
1550    return __c.begin();
1551}
1552
1553template <class _Cp>
1554inline _LIBCPP_INLINE_VISIBILITY
1555typename _Cp::iterator
1556end(_Cp& __c)
1557{
1558    return __c.end();
1559}
1560
1561template <class _Cp>
1562inline _LIBCPP_INLINE_VISIBILITY
1563typename _Cp::const_iterator
1564end(const _Cp& __c)
1565{
1566    return __c.end();
1567}
1568
1569#endif  // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_TRAILING_RETURN)
1570
1571_LIBCPP_END_NAMESPACE_STD
1572
1573#endif  // _LIBCPP_ITERATOR
1574