1// -*- C++ -*-
2//===-------------------------- iterator ----------------------------------===//
3//
4//                     The LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. 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 Container>
142class back_insert_iterator
143{
144protected:
145    Container* container;
146public:
147    typedef Container                   container_type;
148    typedef void                        value_type;
149    typedef void                        difference_type;
150    typedef back_insert_iterator<Cont>& reference;
151    typedef void                        pointer;
152
153    explicit back_insert_iterator(Container& x);
154    back_insert_iterator& operator=(typename Container::const_reference value);
155    back_insert_iterator& operator*();
156    back_insert_iterator& operator++();
157    back_insert_iterator  operator++(int);
158};
159
160template <class Container> back_insert_iterator<Container> back_inserter(Container& x);
161
162template <class Container>
163class front_insert_iterator
164{
165protected:
166    Container* container;
167public:
168    typedef Container                    container_type;
169    typedef void                         value_type;
170    typedef void                         difference_type;
171    typedef front_insert_iterator<Cont>& reference;
172    typedef void                         pointer;
173
174    explicit front_insert_iterator(Container& x);
175    front_insert_iterator& operator=(typename Container::const_reference value);
176    front_insert_iterator& operator*();
177    front_insert_iterator& operator++();
178    front_insert_iterator  operator++(int);
179};
180
181template <class Container> front_insert_iterator<Container> front_inserter(Container& x);
182
183template <class Container>
184class insert_iterator
185{
186protected:
187    Container* container;
188    typename Container::iterator iter;
189public:
190    typedef Container              container_type;
191    typedef void                   value_type;
192    typedef void                   difference_type;
193    typedef insert_iterator<Cont>& reference;
194    typedef void                   pointer;
195
196    insert_iterator(Container& x, typename Container::iterator i);
197    insert_iterator& operator=(typename Container::const_reference value);
198    insert_iterator& operator*();
199    insert_iterator& operator++();
200    insert_iterator& operator++(int);
201};
202
203template <class Container, class Iterator>
204insert_iterator<Container> inserter(Container& x, Iterator i);
205
206template <class T, class charT = char, class traits = char_traits<charT>, class Distance = ptrdiff_t>
207class istream_iterator
208    : public iterator<input_iterator_tag, T, Distance, const T*, const T&>
209{
210public:
211    typedef charT char_type;
212    typedef traits traits_type;
213    typedef basic_istream<charT,traits> istream_type;
214
215    istream_iterator();
216    istream_iterator(istream_type& s);
217    istream_iterator(const istream_iterator& x);
218    ~istream_iterator();
219
220    const T& operator*() const;
221    const T* operator->() const;
222    istream_iterator& operator++();
223    istream_iterator  operator++(int);
224};
225
226template <class T, class charT, class traits, class Distance>
227bool operator==(const istream_iterator<T,charT,traits,Distance>& x,
228                const istream_iterator<T,charT,traits,Distance>& y);
229template <class T, class charT, class traits, class Distance>
230bool operator!=(const istream_iterator<T,charT,traits,Distance>& x,
231                const istream_iterator<T,charT,traits,Distance>& y);
232
233template <class T, class charT = char, class traits = char_traits<charT> >
234class ostream_iterator
235    : public iterator<output_iterator_tag, void, void, void ,void>
236{
237public:
238    typedef charT char_type;
239    typedef traits traits_type;
240    typedef basic_ostream<charT,traits> ostream_type;
241
242    ostream_iterator(ostream_type& s);
243    ostream_iterator(ostream_type& s, const charT* delimiter);
244    ostream_iterator(const ostream_iterator& x);
245    ~ostream_iterator();
246    ostream_iterator& operator=(const T& value);
247
248    ostream_iterator& operator*();
249    ostream_iterator& operator++();
250    ostream_iterator& operator++(int);
251};
252
253template<class charT, class traits = char_traits<charT> >
254class istreambuf_iterator
255    : public iterator<input_iterator_tag, charT,
256                      typename traits::off_type, unspecified,
257                      charT>
258{
259public:
260    typedef charT                         char_type;
261    typedef traits                        traits_type;
262    typedef typename traits::int_type     int_type;
263    typedef basic_streambuf<charT,traits> streambuf_type;
264    typedef basic_istream<charT,traits>   istream_type;
265
266    istreambuf_iterator() throw();
267    istreambuf_iterator(istream_type& s) throw();
268    istreambuf_iterator(streambuf_type* s) throw();
269    istreambuf_iterator(a-private-type) throw();
270
271    charT                operator*() const;
272    pointer operator->() const;
273    istreambuf_iterator& operator++();
274    a-private-type       operator++(int);
275
276    bool equal(const istreambuf_iterator& b) const;
277};
278
279template <class charT, class traits>
280bool operator==(const istreambuf_iterator<charT,traits>& a,
281                const istreambuf_iterator<charT,traits>& b);
282template <class charT, class traits>
283bool operator!=(const istreambuf_iterator<charT,traits>& a,
284                const istreambuf_iterator<charT,traits>& b);
285
286template <class charT, class traits = char_traits<charT> >
287class ostreambuf_iterator
288    : public iterator<output_iterator_tag, void, void, void, void>
289{
290public:
291    typedef charT                         char_type;
292    typedef traits                        traits_type;
293    typedef basic_streambuf<charT,traits> streambuf_type;
294    typedef basic_ostream<charT,traits>   ostream_type;
295
296    ostreambuf_iterator(ostream_type& s) throw();
297    ostreambuf_iterator(streambuf_type* s) throw();
298    ostreambuf_iterator& operator=(charT c);
299    ostreambuf_iterator& operator*();
300    ostreambuf_iterator& operator++();
301    ostreambuf_iterator& operator++(int);
302    bool failed() const throw();
303};
304
305template <class C> auto begin(C& c) -> decltype(c.begin());
306template <class C> auto begin(const C& c) -> decltype(c.begin());
307template <class C> auto end(C& c) -> decltype(c.end());
308template <class C> auto end(const C& c) -> decltype(c.end());
309template <class T, size_t N> T* begin(T (&array)[N]);
310template <class T, size_t N> T* end(T (&array)[N]);
311
312}  // std
313
314*/
315
316#include <__config>
317#include <type_traits>
318#include <cstddef>
319#include <iosfwd>
320#ifdef _LIBCPP_DEBUG
321#include <cassert>
322#endif
323
324#pragma GCC system_header
325
326_LIBCPP_BEGIN_NAMESPACE_STD
327
328struct input_iterator_tag {};
329struct output_iterator_tag {};
330struct forward_iterator_tag       : public input_iterator_tag {};
331struct bidirectional_iterator_tag : public forward_iterator_tag {};
332struct random_access_iterator_tag : public bidirectional_iterator_tag {};
333
334template <class _Tp>
335struct __has_iterator_category
336{
337private:
338    struct __two {char _; char __;};
339    template <class _Up> static __two __test(...);
340    template <class _Up> static char __test(typename _Up::iterator_category* = 0);
341public:
342    static const bool value = sizeof(__test<_Tp>(0)) == 1;
343};
344
345template <class _Iter, bool> struct ____iterator_traits {};
346
347template <class _Iter>
348struct ____iterator_traits<_Iter, true>
349{
350    typedef typename _Iter::difference_type   difference_type;
351    typedef typename _Iter::value_type        value_type;
352    typedef typename _Iter::pointer           pointer;
353    typedef typename _Iter::reference         reference;
354    typedef typename _Iter::iterator_category iterator_category;
355};
356
357template <class _Iter, bool> struct __iterator_traits {};
358
359template <class _Iter>
360struct __iterator_traits<_Iter, true>
361    :  ____iterator_traits
362      <
363        _Iter,
364        is_convertible<typename _Iter::iterator_category, input_iterator_tag>::value ||
365        is_convertible<typename _Iter::iterator_category, output_iterator_tag>::value
366      >
367{};
368
369// iterator_traits<Iterator> will only have the nested types if Iterator::iterator_category
370//    exists.  Else iterator_traits<Iterator> will be an empty class.  This is a
371//    conforming extension which allows some programs to compile and behave as
372//    the client expects instead of failing at compile time.
373
374template <class _Iter>
375struct iterator_traits
376    : __iterator_traits<_Iter, __has_iterator_category<_Iter>::value> {};
377
378template<class _Tp>
379struct iterator_traits<_Tp*>
380{
381    typedef ptrdiff_t difference_type;
382    typedef typename remove_const<_Tp>::type value_type;
383    typedef _Tp* pointer;
384    typedef _Tp& reference;
385    typedef random_access_iterator_tag iterator_category;
386};
387
388template <class _Tp, class _Up, bool = __has_iterator_category<iterator_traits<_Tp> >::value>
389struct __has_iterator_category_convertible_to
390    : public integral_constant<bool, is_convertible<typename iterator_traits<_Tp>::iterator_category, _Up>::value>
391{};
392
393template <class _Tp, class _Up>
394struct __has_iterator_category_convertible_to<_Tp, _Up, false> : public false_type {};
395
396template <class _Tp>
397struct __is_input_iterator : public __has_iterator_category_convertible_to<_Tp, input_iterator_tag> {};
398
399template <class _Tp>
400struct __is_forward_iterator : public __has_iterator_category_convertible_to<_Tp, forward_iterator_tag> {};
401
402template <class _Tp>
403struct __is_bidirectional_iterator : public __has_iterator_category_convertible_to<_Tp, bidirectional_iterator_tag> {};
404
405template <class _Tp>
406struct __is_random_access_iterator : public __has_iterator_category_convertible_to<_Tp, random_access_iterator_tag> {};
407
408template<class _Category, class _Tp, class _Distance = ptrdiff_t,
409         class _Pointer = _Tp*, class _Reference = _Tp&>
410struct iterator
411{
412    typedef _Tp        value_type;
413    typedef _Distance  difference_type;
414    typedef _Pointer   pointer;
415    typedef _Reference reference;
416    typedef _Category  iterator_category;
417};
418
419template <class _InputIter>
420inline _LIBCPP_INLINE_VISIBILITY
421void __advance(_InputIter& __i,
422             typename iterator_traits<_InputIter>::difference_type __n, input_iterator_tag)
423{
424    for (; __n > 0; --__n)
425        ++__i;
426}
427
428template <class _BiDirIter>
429inline _LIBCPP_INLINE_VISIBILITY
430void __advance(_BiDirIter& __i,
431             typename iterator_traits<_BiDirIter>::difference_type __n, bidirectional_iterator_tag)
432{
433    if (__n >= 0)
434        for (; __n > 0; --__n)
435            ++__i;
436    else
437        for (; __n < 0; ++__n)
438            --__i;
439}
440
441template <class _RandIter>
442inline _LIBCPP_INLINE_VISIBILITY
443void __advance(_RandIter& __i,
444             typename iterator_traits<_RandIter>::difference_type __n, random_access_iterator_tag)
445{
446   __i += __n;
447}
448
449template <class _InputIter>
450inline _LIBCPP_INLINE_VISIBILITY
451void advance(_InputIter& __i,
452             typename iterator_traits<_InputIter>::difference_type __n)
453{
454    __advance(__i, __n, typename iterator_traits<_InputIter>::iterator_category());
455}
456
457template <class _InputIter>
458inline _LIBCPP_INLINE_VISIBILITY
459typename iterator_traits<_InputIter>::difference_type
460__distance(_InputIter __first, _InputIter __last, input_iterator_tag)
461{
462    typename iterator_traits<_InputIter>::difference_type __r(0);
463    for (; __first != __last; ++__first)
464        ++__r;
465    return __r;
466}
467
468template <class _RandIter>
469inline _LIBCPP_INLINE_VISIBILITY
470typename iterator_traits<_RandIter>::difference_type
471__distance(_RandIter __first, _RandIter __last, random_access_iterator_tag)
472{
473    return __last - __first;
474}
475
476template <class _InputIter>
477inline _LIBCPP_INLINE_VISIBILITY
478typename iterator_traits<_InputIter>::difference_type
479distance(_InputIter __first, _InputIter __last)
480{
481    return __distance(__first, __last, typename iterator_traits<_InputIter>::iterator_category());
482}
483
484template <class _ForwardIter>
485inline
486_ForwardIter
487next(_ForwardIter __x,
488     typename iterator_traits<_ForwardIter>::difference_type __n = 1,
489     typename enable_if<__is_forward_iterator<_ForwardIter>::value>::type* = 0)
490{
491    advance(__x, __n);
492    return __x;
493}
494
495template <class _BidiretionalIter>
496inline
497_BidiretionalIter
498prev(_BidiretionalIter __x,
499     typename iterator_traits<_BidiretionalIter>::difference_type __n = 1,
500     typename enable_if<__is_bidirectional_iterator<_BidiretionalIter>::value>::type* = 0)
501{
502    advance(__x, -__n);
503    return __x;
504}
505
506template <class _Iter>
507class reverse_iterator
508    : public iterator<typename iterator_traits<_Iter>::iterator_category,
509                      typename iterator_traits<_Iter>::value_type,
510                      typename iterator_traits<_Iter>::difference_type,
511                      typename iterator_traits<_Iter>::pointer,
512                      typename iterator_traits<_Iter>::reference>
513{
514private:
515    mutable _Iter __t;
516protected:
517    _Iter current;
518public:
519    typedef _Iter                                            iterator_type;
520    typedef typename iterator_traits<_Iter>::difference_type difference_type;
521    typedef typename iterator_traits<_Iter>::reference       reference;
522    typedef typename iterator_traits<_Iter>::pointer         pointer;
523
524    _LIBCPP_INLINE_VISIBILITY reverse_iterator() : current() {}
525    _LIBCPP_INLINE_VISIBILITY explicit reverse_iterator(_Iter __x) : __t(__x), current(__x) {}
526    template <class _Up> _LIBCPP_INLINE_VISIBILITY reverse_iterator(const reverse_iterator<_Up>& __u)
527        : __t(__u.base()), current(__u.base()) {}
528    _LIBCPP_INLINE_VISIBILITY _Iter base() const {return current;}
529    _LIBCPP_INLINE_VISIBILITY reference operator*() const {__t = current; return *--__t;}
530    _LIBCPP_INLINE_VISIBILITY pointer  operator->() const {return &(operator*());}
531    _LIBCPP_INLINE_VISIBILITY reverse_iterator& operator++() {--current; return *this;}
532    _LIBCPP_INLINE_VISIBILITY reverse_iterator  operator++(int)
533        {reverse_iterator __tmp(*this); --current; return __tmp;}
534    _LIBCPP_INLINE_VISIBILITY reverse_iterator& operator--() {++current; return *this;}
535    _LIBCPP_INLINE_VISIBILITY reverse_iterator  operator--(int)
536        {reverse_iterator __tmp(*this); ++current; return __tmp;}
537    _LIBCPP_INLINE_VISIBILITY reverse_iterator  operator+ (difference_type __n) const
538        {return reverse_iterator(current - __n);}
539    _LIBCPP_INLINE_VISIBILITY reverse_iterator& operator+=(difference_type __n)
540        {current -= __n; return *this;}
541    _LIBCPP_INLINE_VISIBILITY reverse_iterator  operator- (difference_type __n) const
542        {return reverse_iterator(current + __n);}
543    _LIBCPP_INLINE_VISIBILITY reverse_iterator& operator-=(difference_type __n)
544        {current += __n; return *this;}
545    _LIBCPP_INLINE_VISIBILITY reference         operator[](difference_type __n) const
546        {return current[-__n-1];}
547};
548
549template <class _Iter1, class _Iter2>
550inline _LIBCPP_INLINE_VISIBILITY
551bool
552operator==(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
553{
554    return __x.base() == __y.base();
555}
556
557template <class _Iter1, class _Iter2>
558inline _LIBCPP_INLINE_VISIBILITY
559bool
560operator<(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
561{
562    return __x.base() > __y.base();
563}
564
565template <class _Iter1, class _Iter2>
566inline _LIBCPP_INLINE_VISIBILITY
567bool
568operator!=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
569{
570    return __x.base() != __y.base();
571}
572
573template <class _Iter1, class _Iter2>
574inline _LIBCPP_INLINE_VISIBILITY
575bool
576operator>(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
577{
578    return __x.base() < __y.base();
579}
580
581template <class _Iter1, class _Iter2>
582inline _LIBCPP_INLINE_VISIBILITY
583bool
584operator>=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
585{
586    return __x.base() <= __y.base();
587}
588
589template <class _Iter1, class _Iter2>
590inline _LIBCPP_INLINE_VISIBILITY
591bool
592operator<=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
593{
594    return __x.base() >= __y.base();
595}
596
597template <class _Iter1, class _Iter2>
598inline _LIBCPP_INLINE_VISIBILITY
599typename reverse_iterator<_Iter1>::difference_type
600operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
601{
602    return __y.base() - __x.base();
603}
604
605template <class _Iter>
606inline _LIBCPP_INLINE_VISIBILITY
607reverse_iterator<_Iter>
608operator+(typename reverse_iterator<_Iter>::difference_type __n, const reverse_iterator<_Iter>& __x)
609{
610    return reverse_iterator<_Iter>(__x.base() - __n);
611}
612
613template <class _Container>
614class back_insert_iterator
615    : public iterator<output_iterator_tag,
616                      void,
617                      void,
618                      void,
619                      back_insert_iterator<_Container>&>
620{
621protected:
622    _Container* container;
623public:
624    typedef _Container container_type;
625
626    _LIBCPP_INLINE_VISIBILITY explicit back_insert_iterator(_Container& __x) : container(&__x) {}
627    _LIBCPP_INLINE_VISIBILITY back_insert_iterator& operator=(typename _Container::const_reference __value)
628        {container->push_back(__value); return *this;}
629#ifdef _LIBCPP_MOVE
630    _LIBCPP_INLINE_VISIBILITY back_insert_iterator& operator=(typename _Container::value_type&& __value)
631        {container->push_back(_STD::move(__value)); return *this;}
632#endif
633    _LIBCPP_INLINE_VISIBILITY back_insert_iterator& operator*()     {return *this;}
634    _LIBCPP_INLINE_VISIBILITY back_insert_iterator& operator++()    {return *this;}
635    _LIBCPP_INLINE_VISIBILITY back_insert_iterator  operator++(int) {return *this;}
636};
637
638template <class _Container>
639inline _LIBCPP_INLINE_VISIBILITY
640back_insert_iterator<_Container>
641back_inserter(_Container& __x)
642{
643    return back_insert_iterator<_Container>(__x);
644}
645
646template <class _Container>
647class front_insert_iterator
648    : public iterator<output_iterator_tag,
649                      void,
650                      void,
651                      void,
652                      front_insert_iterator<_Container>&>
653{
654protected:
655    _Container* container;
656public:
657    typedef _Container container_type;
658
659    _LIBCPP_INLINE_VISIBILITY explicit front_insert_iterator(_Container& __x) : container(&__x) {}
660    _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator=(typename _Container::const_reference __value)
661        {container->push_front(__value); return *this;}
662#ifdef _LIBCPP_MOVE
663    _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator=(typename _Container::value_type&& __value)
664        {container->push_front(_STD::move(__value)); return *this;}
665#endif
666    _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator*()     {return *this;}
667    _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator++()    {return *this;}
668    _LIBCPP_INLINE_VISIBILITY front_insert_iterator  operator++(int) {return *this;}
669};
670
671template <class _Container>
672inline _LIBCPP_INLINE_VISIBILITY
673front_insert_iterator<_Container>
674front_inserter(_Container& __x)
675{
676    return front_insert_iterator<_Container>(__x);
677}
678
679template <class _Container>
680class insert_iterator
681    : public iterator<output_iterator_tag,
682                      void,
683                      void,
684                      void,
685                      insert_iterator<_Container>&>
686{
687protected:
688    _Container* container;
689    typename _Container::iterator iter;
690public:
691    typedef _Container container_type;
692
693    _LIBCPP_INLINE_VISIBILITY insert_iterator(_Container& __x, typename _Container::iterator __i)
694        : container(&__x), iter(__i) {}
695    _LIBCPP_INLINE_VISIBILITY insert_iterator& operator=(typename _Container::const_reference __value)
696        {iter = container->insert(iter, __value); ++iter; return *this;}
697#ifdef _LIBCPP_MOVE
698    _LIBCPP_INLINE_VISIBILITY insert_iterator& operator=(typename _Container::value_type&& __value)
699        {iter = container->insert(iter, _STD::move(__value)); ++iter; return *this;}
700#endif
701    _LIBCPP_INLINE_VISIBILITY insert_iterator& operator*()        {return *this;}
702    _LIBCPP_INLINE_VISIBILITY insert_iterator& operator++()       {return *this;}
703    _LIBCPP_INLINE_VISIBILITY insert_iterator& operator++(int)    {return *this;}
704};
705
706template <class _Container>
707inline _LIBCPP_INLINE_VISIBILITY
708insert_iterator<_Container>
709inserter(_Container& __x, typename _Container::iterator __i)
710{
711    return insert_iterator<_Container>(__x, __i);
712}
713
714template <class _Tp, class _CharT = char,
715          class _Traits = char_traits<_CharT>, class _Distance = ptrdiff_t>
716class istream_iterator
717    : public iterator<input_iterator_tag, _Tp, _Distance, const _Tp*, const _Tp&>
718{
719public:
720    typedef _CharT char_type;
721    typedef _Traits traits_type;
722    typedef basic_istream<_CharT,_Traits> istream_type;
723private:
724    istream_type* __in_stream_;
725    _Tp __value_;
726public:
727    _LIBCPP_INLINE_VISIBILITY istream_iterator() : __in_stream_(0) {}
728    _LIBCPP_INLINE_VISIBILITY istream_iterator(istream_type& __s) : __in_stream_(&__s)
729        {
730            if (!(*__in_stream_ >> __value_))
731                __in_stream_ = 0;
732        }
733
734    _LIBCPP_INLINE_VISIBILITY const _Tp& operator*() const {return __value_;}
735    _LIBCPP_INLINE_VISIBILITY const _Tp* operator->() const {return &(operator*());}
736    _LIBCPP_INLINE_VISIBILITY istream_iterator& operator++()
737        {
738            if (!(*__in_stream_ >> __value_))
739                __in_stream_ = 0;
740            return *this;
741        }
742    _LIBCPP_INLINE_VISIBILITY istream_iterator  operator++(int)
743        {istream_iterator __t(*this); ++(*this); return __t;}
744
745    friend _LIBCPP_INLINE_VISIBILITY
746    bool operator==(const istream_iterator& __x, const istream_iterator& __y)
747        {return __x.__in_stream_ == __y.__in_stream_;}
748
749    friend _LIBCPP_INLINE_VISIBILITY
750    bool operator!=(const istream_iterator& __x, const istream_iterator& __y)
751        {return !(__x == __y);}
752};
753
754template <class _Tp, class _CharT = char, class _Traits = char_traits<_CharT> >
755class ostream_iterator
756    : public iterator<output_iterator_tag, void, void, void, void>
757{
758public:
759    typedef _CharT char_type;
760    typedef _Traits traits_type;
761    typedef basic_ostream<_CharT,_Traits> ostream_type;
762private:
763    ostream_type* __out_stream_;
764    const char_type* __delim_;
765public:
766    _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s)
767        : __out_stream_(&__s), __delim_(0) {}
768    _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s, const _CharT* __delimiter)
769        : __out_stream_(&__s), __delim_(__delimiter) {}
770    _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator=(const _Tp& __value)
771        {
772            *__out_stream_ << __value;
773            if (__delim_)
774                *__out_stream_ << __delim_;
775            return *this;
776        }
777
778    _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator*()     {return *this;}
779    _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++()    {return *this;}
780    _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++(int) {return *this;}
781};
782
783template<class _CharT, class _Traits>
784class istreambuf_iterator
785    : public iterator<input_iterator_tag, _CharT,
786                      typename _Traits::off_type, _CharT*,
787                      _CharT>
788{
789public:
790    typedef _CharT                          char_type;
791    typedef _Traits                         traits_type;
792    typedef typename _Traits::int_type      int_type;
793    typedef basic_streambuf<_CharT,_Traits> streambuf_type;
794    typedef basic_istream<_CharT,_Traits>   istream_type;
795private:
796    streambuf_type* __sbuf_;
797
798    class __proxy
799    {
800        char_type __keep_;
801        streambuf_type* __sbuf_;
802        _LIBCPP_INLINE_VISIBILITY __proxy(char_type __c, streambuf_type* __s)
803            : __keep_(__c), __sbuf_(__s) {}
804        friend class istreambuf_iterator;
805    public:
806        _LIBCPP_INLINE_VISIBILITY char_type operator*() const {return __keep_;}
807    };
808
809    void __test_for_eof()
810    {
811        if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof()))
812            __sbuf_ = 0;
813    }
814public:
815    _LIBCPP_INLINE_VISIBILITY istreambuf_iterator() throw() : __sbuf_(0) {}
816    _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(istream_type& __s) throw()
817        : __sbuf_(__s.rdbuf()) {__test_for_eof();}
818    _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(streambuf_type* __s) throw()
819        : __sbuf_(__s) {__test_for_eof();}
820    _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(const __proxy& __p) throw()
821        : __sbuf_(__p.__sbuf_) {}
822
823    _LIBCPP_INLINE_VISIBILITY _CharT               operator*() const {return __sbuf_->sgetc();}
824    _LIBCPP_INLINE_VISIBILITY char_type* operator->() const {return nullptr;}
825    _LIBCPP_INLINE_VISIBILITY istreambuf_iterator& operator++()
826        {
827            if (traits_type::eq_int_type(__sbuf_->snextc(), traits_type::eof()))
828                __sbuf_ = 0;
829            return *this;
830        }
831    _LIBCPP_INLINE_VISIBILITY __proxy              operator++(int)
832        {
833            char_type __c = __sbuf_->sgetc();
834            ++(*this);
835            return __proxy(__c, __sbuf_);
836        }
837
838    _LIBCPP_INLINE_VISIBILITY bool equal(const istreambuf_iterator& __b) const
839        {return (__sbuf_ == 0) == (__b.__sbuf_ == 0);}
840};
841
842template <class _CharT, class _Traits>
843inline _LIBCPP_INLINE_VISIBILITY
844bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,
845                const istreambuf_iterator<_CharT,_Traits>& __b)
846                {return __a.equal(__b);}
847
848template <class _CharT, class _Traits>
849inline _LIBCPP_INLINE_VISIBILITY
850bool operator!=(const istreambuf_iterator<_CharT,_Traits>& __a,
851                const istreambuf_iterator<_CharT,_Traits>& __b)
852                {return !__a.equal(__b);}
853
854template <class _CharT, class _Traits>
855class ostreambuf_iterator
856    : public iterator<output_iterator_tag, void, void, void, void>
857{
858public:
859    typedef _CharT                          char_type;
860    typedef _Traits                         traits_type;
861    typedef basic_streambuf<_CharT,_Traits> streambuf_type;
862    typedef basic_ostream<_CharT,_Traits>   ostream_type;
863private:
864    streambuf_type* __sbuf_;
865public:
866    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(ostream_type& __s) throw()
867        : __sbuf_(__s.rdbuf()) {}
868    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(streambuf_type* __s) throw()
869        : __sbuf_(__s) {}
870    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator=(_CharT __c)
871        {
872            if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sputc(__c), traits_type::eof()))
873                __sbuf_ = 0;
874            return *this;
875        }
876    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator*()     {return *this;}
877    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++()    {return *this;}
878    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++(int) {return *this;}
879    _LIBCPP_INLINE_VISIBILITY bool failed() const throw() {return __sbuf_ == 0;}
880};
881
882template <class _Iter>
883class move_iterator
884{
885private:
886    _Iter __i;
887public:
888    typedef _Iter                                            iterator_type;
889    typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
890    typedef typename iterator_traits<iterator_type>::value_type value_type;
891    typedef typename iterator_traits<iterator_type>::difference_type difference_type;
892    typedef typename iterator_traits<iterator_type>::pointer pointer;
893#ifdef _LIBCPP_MOVE
894    typedef value_type&& reference;
895#else
896    typedef typename iterator_traits<iterator_type>::reference reference;
897#endif
898
899    _LIBCPP_INLINE_VISIBILITY move_iterator() : __i() {}
900    _LIBCPP_INLINE_VISIBILITY explicit move_iterator(_Iter __x) : __i(__x) {}
901    template <class _Up> _LIBCPP_INLINE_VISIBILITY move_iterator(const move_iterator<_Up>& __u)
902        : __i(__u.base()) {}
903    _LIBCPP_INLINE_VISIBILITY _Iter base() const {return __i;}
904    _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *__i;}
905    _LIBCPP_INLINE_VISIBILITY pointer  operator->() const {return &(operator*());}
906    _LIBCPP_INLINE_VISIBILITY move_iterator& operator++() {++__i; return *this;}
907    _LIBCPP_INLINE_VISIBILITY move_iterator  operator++(int)
908        {move_iterator __tmp(*this); ++__i; return __tmp;}
909    _LIBCPP_INLINE_VISIBILITY move_iterator& operator--() {--__i; return *this;}
910    _LIBCPP_INLINE_VISIBILITY move_iterator  operator--(int)
911        {move_iterator __tmp(*this); --__i; return __tmp;}
912    _LIBCPP_INLINE_VISIBILITY move_iterator  operator+ (difference_type __n) const
913        {return move_iterator(__i + __n);}
914    _LIBCPP_INLINE_VISIBILITY move_iterator& operator+=(difference_type __n)
915        {__i += __n; return *this;}
916    _LIBCPP_INLINE_VISIBILITY move_iterator  operator- (difference_type __n) const
917        {return move_iterator(__i - __n);}
918    _LIBCPP_INLINE_VISIBILITY move_iterator& operator-=(difference_type __n)
919        {__i -= __n; return *this;}
920    _LIBCPP_INLINE_VISIBILITY reference         operator[](difference_type __n) const
921        {return __i[__n];}
922};
923
924template <class _Iter1, class _Iter2>
925inline _LIBCPP_INLINE_VISIBILITY
926bool
927operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
928{
929    return __x.base() == __y.base();
930}
931
932template <class _Iter1, class _Iter2>
933inline _LIBCPP_INLINE_VISIBILITY
934bool
935operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
936{
937    return __x.base() < __y.base();
938}
939
940template <class _Iter1, class _Iter2>
941inline _LIBCPP_INLINE_VISIBILITY
942bool
943operator!=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
944{
945    return __x.base() != __y.base();
946}
947
948template <class _Iter1, class _Iter2>
949inline _LIBCPP_INLINE_VISIBILITY
950bool
951operator>(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
952{
953    return __x.base() > __y.base();
954}
955
956template <class _Iter1, class _Iter2>
957inline _LIBCPP_INLINE_VISIBILITY
958bool
959operator>=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
960{
961    return __x.base() >= __y.base();
962}
963
964template <class _Iter1, class _Iter2>
965inline _LIBCPP_INLINE_VISIBILITY
966bool
967operator<=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
968{
969    return __x.base() <= __y.base();
970}
971
972template <class _Iter1, class _Iter2>
973inline _LIBCPP_INLINE_VISIBILITY
974typename move_iterator<_Iter1>::difference_type
975operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
976{
977    return __x.base() - __y.base();
978}
979
980template <class _Iter>
981inline _LIBCPP_INLINE_VISIBILITY
982move_iterator<_Iter>
983operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
984{
985    return move_iterator<_Iter>(__x.base() + __n);
986}
987
988template <class _Iter>
989inline _LIBCPP_INLINE_VISIBILITY
990move_iterator<_Iter>
991make_move_iterator(const _Iter& __i)
992{
993    return move_iterator<_Iter>(__i);
994}
995
996// __wrap_iter
997
998template <class _Iter> class __wrap_iter;
999
1000template <class _Iter1, class _Iter2>
1001bool
1002operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&);
1003
1004template <class _Iter1, class _Iter2>
1005bool
1006operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&);
1007
1008template <class _Iter1, class _Iter2>
1009bool
1010operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&);
1011
1012template <class _Iter1, class _Iter2>
1013bool
1014operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&);
1015
1016template <class _Iter1, class _Iter2>
1017bool
1018operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&);
1019
1020template <class _Iter1, class _Iter2>
1021bool
1022operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&);
1023
1024template <class _Iter1, class _Iter2>
1025typename __wrap_iter<_Iter1>::difference_type
1026operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&);
1027
1028template <class _Iter>
1029__wrap_iter<_Iter>
1030operator+(typename __wrap_iter<_Iter>::difference_type, const __wrap_iter<_Iter>&);
1031
1032template <class _I, class _O> _O copy(_I, _I, _O);
1033template <class _B1, class _B2> _B2 copy_backward(_B1, _B1, _B2);
1034template <class _I, class _O> _O move(_I, _I, _O);
1035template <class _B1, class _B2> _B2 move_backward(_B1, _B1, _B2);
1036
1037template <class _Tp>
1038typename enable_if
1039<
1040    has_trivial_copy_assign<_Tp>::value,
1041    _Tp*
1042>::type
1043__unwrap_iter(__wrap_iter<_Tp*>);
1044
1045template <class _Iter>
1046class __wrap_iter
1047{
1048public:
1049    typedef _Iter                                                      iterator_type;
1050    typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
1051    typedef typename iterator_traits<iterator_type>::value_type        value_type;
1052    typedef typename iterator_traits<iterator_type>::difference_type   difference_type;
1053    typedef typename iterator_traits<iterator_type>::pointer           pointer;
1054    typedef typename iterator_traits<iterator_type>::reference         reference;
1055private:
1056    iterator_type __i;
1057public:
1058    _LIBCPP_INLINE_VISIBILITY __wrap_iter() {}
1059    template <class _Up> _LIBCPP_INLINE_VISIBILITY __wrap_iter(const __wrap_iter<_Up>& __u,
1060        typename enable_if<is_convertible<_Up, iterator_type>::value>::type* = 0)
1061        : __i(__u.base()) {}
1062    _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *__i;}
1063    _LIBCPP_INLINE_VISIBILITY pointer  operator->() const {return &(operator*());}
1064    _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator++() {++__i; return *this;}
1065    _LIBCPP_INLINE_VISIBILITY __wrap_iter  operator++(int)
1066        {__wrap_iter __tmp(*this); ++__i; return __tmp;}
1067    _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator--() {--__i; return *this;}
1068    _LIBCPP_INLINE_VISIBILITY __wrap_iter  operator--(int)
1069        {__wrap_iter __tmp(*this); --__i; return __tmp;}
1070    _LIBCPP_INLINE_VISIBILITY __wrap_iter  operator+ (difference_type __n) const
1071        {return __wrap_iter(__i + __n);}
1072    _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator+=(difference_type __n)
1073        {__i += __n; return *this;}
1074    _LIBCPP_INLINE_VISIBILITY __wrap_iter  operator- (difference_type __n) const
1075        {return __wrap_iter(__i - __n);}
1076    _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator-=(difference_type __n)
1077        {__i -= __n; return *this;}
1078    _LIBCPP_INLINE_VISIBILITY reference        operator[](difference_type __n) const
1079        {return __i[__n];}
1080
1081    _LIBCPP_INLINE_VISIBILITY iterator_type base() const {return __i;}
1082
1083private:
1084    _LIBCPP_INLINE_VISIBILITY __wrap_iter(iterator_type __x) : __i(__x) {}
1085
1086    template <class _Up> friend class __wrap_iter;
1087    template <class _CharT, class _Traits, class _Alloc> friend class basic_string;
1088    template <class _Tp, class _Alloc> friend class vector;
1089
1090    template <class _Iter1, class _Iter2>
1091    friend
1092    bool
1093    operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&);
1094
1095    template <class _Iter1, class _Iter2>
1096    friend
1097    bool
1098    operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&);
1099
1100    template <class _Iter1, class _Iter2>
1101    friend
1102    bool
1103    operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&);
1104
1105    template <class _Iter1, class _Iter2>
1106    friend
1107    bool
1108    operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&);
1109
1110    template <class _Iter1, class _Iter2>
1111    friend
1112    bool
1113    operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&);
1114
1115    template <class _Iter1, class _Iter2>
1116    friend
1117    bool
1118    operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&);
1119
1120    template <class _Iter1, class _Iter2>
1121    friend
1122    typename __wrap_iter<_Iter1>::difference_type
1123    operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&);
1124
1125    template <class _Iter1>
1126    friend
1127    __wrap_iter<_Iter1>
1128    operator+(typename __wrap_iter<_Iter1>::difference_type, const __wrap_iter<_Iter1>&);
1129
1130    template <class _I, class _O> friend _O copy(_I, _I, _O);
1131    template <class _B1, class _B2> friend _B2 copy_backward(_B1, _B1, _B2);
1132    template <class _I, class _O> friend _O move(_I, _I, _O);
1133    template <class _B1, class _B2> friend _B2 move_backward(_B1, _B1, _B2);
1134
1135    template <class _Tp>
1136    friend
1137    typename enable_if
1138    <
1139        has_trivial_copy_assign<_Tp>::value,
1140        _Tp*
1141    >::type
1142    __unwrap_iter(__wrap_iter<_Tp*>);
1143};
1144
1145template <class _Iter1, class _Iter2>
1146inline _LIBCPP_INLINE_VISIBILITY
1147bool
1148operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y)
1149{
1150    return __x.base() == __y.base();
1151}
1152
1153template <class _Iter1, class _Iter2>
1154inline _LIBCPP_INLINE_VISIBILITY
1155bool
1156operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y)
1157{
1158    return __x.base() < __y.base();
1159}
1160
1161template <class _Iter1, class _Iter2>
1162inline _LIBCPP_INLINE_VISIBILITY
1163bool
1164operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y)
1165{
1166    return __x.base() != __y.base();
1167}
1168
1169template <class _Iter1, class _Iter2>
1170inline _LIBCPP_INLINE_VISIBILITY
1171bool
1172operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y)
1173{
1174    return __x.base() > __y.base();
1175}
1176
1177template <class _Iter1, class _Iter2>
1178inline _LIBCPP_INLINE_VISIBILITY
1179bool
1180operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y)
1181{
1182    return __x.base() >= __y.base();
1183}
1184
1185template <class _Iter1, class _Iter2>
1186inline _LIBCPP_INLINE_VISIBILITY
1187bool
1188operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y)
1189{
1190    return __x.base() <= __y.base();
1191}
1192
1193template <class _Iter1, class _Iter2>
1194inline _LIBCPP_INLINE_VISIBILITY
1195typename __wrap_iter<_Iter1>::difference_type
1196operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y)
1197{
1198    return __x.base() - __y.base();
1199}
1200
1201template <class _Iter>
1202inline _LIBCPP_INLINE_VISIBILITY
1203__wrap_iter<_Iter>
1204operator+(typename __wrap_iter<_Iter>::difference_type __n,
1205          const __wrap_iter<_Iter>& __x)
1206{
1207    return __wrap_iter<_Iter>(__x.base() + __n);
1208}
1209
1210#ifdef _LIBCPP_DEBUG
1211
1212// __debug_iter
1213
1214template <class _Container, class _Iter> class __debug_iter;
1215
1216template <class _Container, class _Iter1, class _Iter2>
1217bool
1218operator==(const __debug_iter<_Container, _Iter1>&, const __debug_iter<_Container, _Iter2>&);
1219
1220template <class _Container, class _Iter1, class _Iter2>
1221bool
1222operator<(const __debug_iter<_Container, _Iter1>&, const __debug_iter<_Container, _Iter2>&);
1223
1224template <class _Container, class _Iter1, class _Iter2>
1225bool
1226operator!=(const __debug_iter<_Container, _Iter1>&, const __debug_iter<_Container, _Iter2>&);
1227
1228template <class _Container, class _Iter1, class _Iter2>
1229bool
1230operator>(const __debug_iter<_Container, _Iter1>&, const __debug_iter<_Container, _Iter2>&);
1231
1232template <class _Container, class _Iter1, class _Iter2>
1233bool
1234operator>=(const __debug_iter<_Container, _Iter1>&, const __debug_iter<_Container, _Iter2>&);
1235
1236template <class _Container, class _Iter1, class _Iter2>
1237bool
1238operator<=(const __debug_iter<_Container, _Iter1>&, const __debug_iter<_Container, _Iter2>&);
1239
1240template <class _Container, class _Iter1, class _Iter2>
1241typename __debug_iter<_Container, _Iter1>::difference_type
1242operator-(const __debug_iter<_Container, _Iter1>&, const __debug_iter<_Container, _Iter2>&);
1243
1244template <class _Container, class _Iter>
1245__debug_iter<_Container, _Iter>
1246operator+(typename __debug_iter<_Container, _Iter>::difference_type, const __debug_iter<_Container, _Iter>&);
1247
1248template <class _Container, class _Iter>
1249class __debug_iter
1250{
1251public:
1252    typedef _Iter                                                      iterator_type;
1253    typedef _Container                                                 __container_type;
1254    typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
1255    typedef typename iterator_traits<iterator_type>::value_type        value_type;
1256    typedef typename iterator_traits<iterator_type>::difference_type   difference_type;
1257    typedef typename iterator_traits<iterator_type>::pointer           pointer;
1258    typedef typename iterator_traits<iterator_type>::reference         reference;
1259private:
1260    iterator_type __i;
1261    __debug_iter* __next;
1262    __container_type* __cont;
1263
1264public:
1265    _LIBCPP_INLINE_VISIBILITY __debug_iter() : __next(0), __cont(0) {}
1266    _LIBCPP_INLINE_VISIBILITY __debug_iter(const __debug_iter& __x)
1267        : __i(__x.base()), __next(0), __cont(0) {__set_owner(__x.__cont);}
1268    __debug_iter& operator=(const __debug_iter& __x);
1269    template <class _Up> _LIBCPP_INLINE_VISIBILITY __debug_iter(const __debug_iter<_Container, _Up>& __u,
1270        typename enable_if<is_convertible<_Up, iterator_type>::value>::type* = 0)
1271        : __i(__u.base()), __next(0), __cont(0) {__set_owner(__u.__cont);}
1272    _LIBCPP_INLINE_VISIBILITY ~__debug_iter() {__remove_owner();}
1273    _LIBCPP_INLINE_VISIBILITY reference operator*() const {assert(__is_deref()); return *__i;}
1274    _LIBCPP_INLINE_VISIBILITY pointer  operator->() const {return &(operator*());}
1275    _LIBCPP_INLINE_VISIBILITY __debug_iter& operator++() {assert(__can_increment()); ++__i; return *this;}
1276    _LIBCPP_INLINE_VISIBILITY __debug_iter  operator++(int)
1277        {__debug_iter __tmp(*this); operator++(); return __tmp;}
1278    _LIBCPP_INLINE_VISIBILITY __debug_iter& operator--() {assert(__can_decrement()); --__i; return *this;}
1279    _LIBCPP_INLINE_VISIBILITY __debug_iter  operator--(int)
1280        {__debug_iter __tmp(*this); operator--(); return __tmp;}
1281    _LIBCPP_INLINE_VISIBILITY __debug_iter  operator+ (difference_type __n) const
1282        {__debug_iter __t(*this); __t += __n; return __t;}
1283    __debug_iter& operator+=(difference_type __n);
1284    _LIBCPP_INLINE_VISIBILITY __debug_iter  operator- (difference_type __n) const
1285        {__debug_iter __t(*this); __t -= __n; return __t;}
1286    _LIBCPP_INLINE_VISIBILITY __debug_iter& operator-=(difference_type __n)
1287        {*this += -__n; return *this;}
1288    _LIBCPP_INLINE_VISIBILITY reference        operator[](difference_type __n) const
1289        {return *(*this + __n);}
1290
1291private:
1292    _LIBCPP_INLINE_VISIBILITY __debug_iter(const __container_type* __c, iterator_type __x)
1293        : __i(__x), __next(0), __cont(0) {__set_owner(__c);}
1294    _LIBCPP_INLINE_VISIBILITY iterator_type base() const {return __i;}
1295
1296    void __set_owner(const __container_type* __c);
1297    void __remove_owner();
1298    static void __remove_all(__container_type* __c);
1299    static void swap(__container_type* __x, __container_type* __y);
1300
1301    _LIBCPP_INLINE_VISIBILITY bool __is_deref() const
1302        {return __is_deref(__is_random_access_iterator<iterator_type>());}
1303    bool __is_deref(false_type) const;
1304    bool __is_deref(true_type) const;
1305    _LIBCPP_INLINE_VISIBILITY bool __can_decrement() const
1306        {return __can_decrement(integral_constant<int, is_pointer<iterator_type>::value ? 2:
1307                                                       __is_random_access_iterator<iterator_type>::value ? 1 : 0>());}
1308    bool __can_decrement(integral_constant<int, 0>) const;
1309    bool __can_decrement(integral_constant<int, 1>) const;
1310    bool __can_decrement(integral_constant<int, 2>) const;
1311    _LIBCPP_INLINE_VISIBILITY bool __can_increment() const
1312        {return __can_increment(integral_constant<int, is_pointer<iterator_type>::value ? 2:
1313                                                       __is_random_access_iterator<iterator_type>::value ? 1 : 0>());}
1314    bool __can_increment(integral_constant<int, 0>) const;
1315    bool __can_increment(integral_constant<int, 1>) const;
1316    bool __can_increment(integral_constant<int, 2>) const;
1317
1318    _LIBCPP_INLINE_VISIBILITY bool __can_add(difference_type __n) const
1319        {return __can_add(__n, is_pointer<iterator_type>());}
1320    bool __can_add(difference_type __n, false_type) const;
1321    bool __can_add(difference_type __n, true_type) const;
1322
1323    template <class _Cp, class _Up> friend class __debug_iter;
1324    friend class _Container::__self;
1325
1326    template <class _Cp, class _Iter1, class _Iter2>
1327    friend
1328    bool
1329    operator==(const __debug_iter<_Cp, _Iter1>&, const __debug_iter<_Cp, _Iter2>&);
1330
1331    template <class _Cp, class _Iter1, class _Iter2>
1332    friend
1333    bool
1334    operator<(const __debug_iter<_Cp, _Iter1>&, const __debug_iter<_Cp, _Iter2>&);
1335
1336    template <class _Cp, class _Iter1, class _Iter2>
1337    friend
1338    bool
1339    operator!=(const __debug_iter<_Cp, _Iter1>&, const __debug_iter<_Cp, _Iter2>&);
1340
1341    template <class _Cp, class _Iter1, class _Iter2>
1342    friend
1343    bool
1344    operator>(const __debug_iter<_Cp, _Iter1>&, const __debug_iter<_Cp, _Iter2>&);
1345
1346    template <class _Cp, class _Iter1, class _Iter2>
1347    friend
1348    bool
1349    operator>=(const __debug_iter<_Cp, _Iter1>&, const __debug_iter<_Cp, _Iter2>&);
1350
1351    template <class _Cp, class _Iter1, class _Iter2>
1352    friend
1353    bool
1354    operator<=(const __debug_iter<_Cp, _Iter1>&, const __debug_iter<_Cp, _Iter2>&);
1355
1356    template <class _Cp, class _Iter1, class _Iter2>
1357    friend
1358    typename __debug_iter<_Cp, _Iter1>::difference_type
1359    operator-(const __debug_iter<_Cp, _Iter1>&, const __debug_iter<_Cp, _Iter2>&);
1360
1361    template <class _Cp, class _Iter1>
1362    friend
1363    __debug_iter<_Cp, _Iter1>
1364    operator+(typename __debug_iter<_Cp, _Iter1>::difference_type, const __debug_iter<_Cp, _Iter1>&);
1365};
1366
1367template <class _Container, class _Iter>
1368__debug_iter<_Container, _Iter>&
1369__debug_iter<_Container, _Iter>::operator=(const __debug_iter& __x)
1370{
1371    if (this != &__x)
1372    {
1373        __remove_owner();
1374        __i = __x.__i;
1375        __set_owner(__x.__cont);
1376    }
1377    return *this;
1378}
1379
1380template <class _Container, class _Iter>
1381void
1382__debug_iter<_Container, _Iter>::__set_owner(const __container_type* __c)
1383{
1384    __cont = const_cast<__container_type*>(__c);
1385    __debug_iter*& __head = __cont->__get_iterator_list(this);
1386    __next = __head;
1387    __head = this;
1388}
1389
1390template <class _Container, class _Iter>
1391void
1392__debug_iter<_Container, _Iter>::__remove_owner()
1393{
1394    if (__cont)
1395    {
1396        __debug_iter*& __head = __cont->__get_iterator_list(this);
1397        if (__head == this)
1398            __head = __next;
1399        else
1400        {
1401            __debug_iter* __prev = __head;
1402            for (__debug_iter* __p = __head->__next; __p != this; __p = __p->__next)
1403                __prev = __p;
1404            __prev->__next = __next;
1405        }
1406        __cont = 0;
1407    }
1408}
1409
1410template <class _Container, class _Iter>
1411void
1412__debug_iter<_Container, _Iter>::__remove_all(__container_type* __c)
1413{
1414    __debug_iter*& __head = __c->__get_iterator_list((__debug_iter*)0);
1415    __debug_iter* __p = __head;
1416    __head = 0;
1417    while (__p)
1418    {
1419        __p->__cont = 0;
1420        __debug_iter* __n = __p->__next;
1421        __p->__next = 0;
1422        __p = __n;
1423    }
1424}
1425
1426template <class _Container, class _Iter>
1427void
1428__debug_iter<_Container, _Iter>::swap(__container_type* __x, __container_type* __y)
1429{
1430    __debug_iter*& __head_x = __x->__get_iterator_list((__debug_iter*)0);
1431    __debug_iter*& __head_y = __y->__get_iterator_list((__debug_iter*)0);
1432    __debug_iter* __p = __head_x;
1433    __head_x = __head_y;
1434    __head_y = __p;
1435    for (__p = __head_x; __p; __p = __p->__next)
1436        __p->__cont = __x;
1437    for (__p = __head_y; __p; __p = __p->__next)
1438        __p->__cont = __y;
1439}
1440
1441template <class _Container, class _Iter>
1442bool
1443__debug_iter<_Container, _Iter>::__is_deref(false_type) const
1444{
1445    if (__cont == 0)
1446        return false;
1447    return __i != __cont->end().base();
1448}
1449
1450template <class _Container, class _Iter>
1451bool
1452__debug_iter<_Container, _Iter>::__is_deref(true_type) const
1453{
1454    if (__cont == 0)
1455        return false;
1456    return __i < __cont->end().base();
1457}
1458
1459template <class _Container, class _Iter>
1460bool
1461__debug_iter<_Container, _Iter>::__can_decrement(integral_constant<int, 0>) const
1462{
1463    if (__cont == 0)
1464        return false;
1465    return __i != __cont->begin().base();
1466}
1467
1468template <class _Container, class _Iter>
1469bool
1470__debug_iter<_Container, _Iter>::__can_decrement(integral_constant<int, 1>) const
1471{
1472    if (__cont == 0)
1473        return false;
1474    iterator_type __b = __cont->begin().base();
1475    return __b < __i && __i <= __b + __cont->size();
1476}
1477
1478template <class _Container, class _Iter>
1479bool
1480__debug_iter<_Container, _Iter>::__can_decrement(integral_constant<int, 2>) const
1481{
1482    if (__cont == 0)
1483        return false;
1484    iterator_type __b = __cont->begin().base();
1485    return __b < __i && __i <= __b + __cont->size();
1486}
1487
1488template <class _Container, class _Iter>
1489bool
1490__debug_iter<_Container, _Iter>::__can_increment(integral_constant<int, 0>) const
1491{
1492    if (__cont == 0)
1493        return false;
1494    return __i != __cont->end().base();
1495}
1496
1497template <class _Container, class _Iter>
1498bool
1499__debug_iter<_Container, _Iter>::__can_increment(integral_constant<int, 1>) const
1500{
1501    if (__cont == 0)
1502        return false;
1503    iterator_type __b = __cont->begin().base();
1504    return __b <= __i && __i < __b + __cont->size();
1505}
1506
1507template <class _Container, class _Iter>
1508bool
1509__debug_iter<_Container, _Iter>::__can_increment(integral_constant<int, 2>) const
1510{
1511    if (__cont == 0)
1512        return false;
1513    iterator_type __b = __cont->begin().base();
1514    return __b <= __i && __i < __b + __cont->size();
1515}
1516
1517template <class _Container, class _Iter>
1518bool
1519__debug_iter<_Container, _Iter>::__can_add(difference_type __n, false_type) const
1520{
1521    if (__cont == 0)
1522        return false;
1523    iterator_type __b = __cont->begin().base();
1524    iterator_type __j = __i + __n;
1525    return __b <= __j && __j <= __b + __cont->size();
1526}
1527
1528template <class _Container, class _Iter>
1529bool
1530__debug_iter<_Container, _Iter>::__can_add(difference_type __n, true_type) const
1531{
1532    if (__cont == 0)
1533        return false;
1534    iterator_type __b = __cont->begin().base();
1535    iterator_type __j = __i + __n;
1536    return __b <= __j && __j <= __b + __cont->size();
1537}
1538
1539template <class _Container, class _Iter>
1540__debug_iter<_Container, _Iter>&
1541__debug_iter<_Container, _Iter>::operator+=(difference_type __n)
1542{
1543    assert(__can_add(__n));
1544    __i += __n;
1545    return *this;
1546}
1547
1548template <class _Container, class _Iter1, class _Iter2>
1549inline _LIBCPP_INLINE_VISIBILITY
1550bool
1551operator==(const __debug_iter<_Container, _Iter1>& __x, const __debug_iter<_Container, _Iter2>& __y)
1552{
1553    assert(__x.__cont && __x.__cont == __y.__cont);
1554    return __x.base() == __y.base();
1555}
1556
1557template <class _Container, class _Iter1, class _Iter2>
1558inline _LIBCPP_INLINE_VISIBILITY
1559bool
1560operator!=(const __debug_iter<_Container, _Iter1>& __x, const __debug_iter<_Container, _Iter2>& __y)
1561{
1562    return !(__x == __y);
1563}
1564
1565template <class _Container, class _Iter1, class _Iter2>
1566inline _LIBCPP_INLINE_VISIBILITY
1567bool
1568operator<(const __debug_iter<_Container, _Iter1>& __x, const __debug_iter<_Container, _Iter2>& __y)
1569{
1570    assert(__x.__cont && __x.__cont == __y.__cont);
1571    return __x.base() < __y.base();
1572}
1573
1574template <class _Container, class _Iter1, class _Iter2>
1575inline _LIBCPP_INLINE_VISIBILITY
1576bool
1577operator>(const __debug_iter<_Container, _Iter1>& __x, const __debug_iter<_Container, _Iter2>& __y)
1578{
1579    return __y < __x;
1580}
1581
1582template <class _Container, class _Iter1, class _Iter2>
1583inline _LIBCPP_INLINE_VISIBILITY
1584bool
1585operator>=(const __debug_iter<_Container, _Iter1>& __x, const __debug_iter<_Container, _Iter2>& __y)
1586{
1587    return !(__x < __y);
1588}
1589
1590template <class _Container, class _Iter1, class _Iter2>
1591inline _LIBCPP_INLINE_VISIBILITY
1592bool
1593operator<=(const __debug_iter<_Container, _Iter1>& __x, const __debug_iter<_Container, _Iter2>& __y)
1594{
1595    return !(__y < __x);
1596}
1597
1598template <class _Container, class _Iter1, class _Iter2>
1599inline _LIBCPP_INLINE_VISIBILITY
1600typename __debug_iter<_Container, _Iter1>::difference_type
1601operator-(const __debug_iter<_Container, _Iter1>& __x, const __debug_iter<_Container, _Iter2>& __y)
1602{
1603    assert(__x.__cont && __x.__cont == __y.__cont);
1604    return __x.base() - __y.base();
1605}
1606
1607template <class _Container, class _Iter>
1608inline _LIBCPP_INLINE_VISIBILITY
1609__debug_iter<_Container, _Iter>
1610operator+(typename __debug_iter<_Container, _Iter>::difference_type __n,
1611          const __debug_iter<_Container, _Iter>& __x)
1612{
1613    return __x + __n;
1614}
1615
1616#endif  // _LIBCPP_DEBUG
1617
1618#ifdef _LIBCPP_MOVE
1619
1620template <class _C>
1621inline
1622auto
1623begin(_C& __c) -> decltype(__c.begin())
1624{
1625    return __c.begin();
1626}
1627
1628template <class _C>
1629inline
1630auto
1631begin(const _C& __c) -> decltype(__c.begin())
1632{
1633    return __c.begin();
1634}
1635
1636template <class _C>
1637inline
1638auto
1639end(_C& __c) -> decltype(__c.end())
1640{
1641    return __c.end();
1642}
1643
1644template <class _C>
1645inline
1646auto
1647end(const _C& __c) -> decltype(__c.end())
1648{
1649    return __c.end();
1650}
1651
1652#else
1653
1654template <class _C>
1655inline
1656typename _C::iterator
1657begin(_C& __c)
1658{
1659    return __c.begin();
1660}
1661
1662template <class _C>
1663inline
1664typename _C::const_iterator
1665begin(const _C& __c)
1666{
1667    return __c.begin();
1668}
1669
1670template <class _C>
1671inline
1672typename _C::iterator
1673end(_C& __c)
1674{
1675    return __c.end();
1676}
1677
1678template <class _C>
1679inline
1680typename _C::const_iterator
1681end(const _C& __c)
1682{
1683    return __c.end();
1684}
1685
1686#endif
1687
1688template <class _T, size_t _N>
1689inline
1690_T*
1691begin(_T (&__array)[_N])
1692{
1693    return __array;
1694}
1695
1696template <class _T, size_t _N>
1697inline
1698_T*
1699end(_T (&__array)[_N])
1700{
1701    return __array + _N;
1702}
1703
1704_LIBCPP_END_NAMESPACE_STD
1705
1706#endif  // _LIBCPP_ITERATOR
1707