blob: 0be7a8b34978e0002e97332c36f2746799fe841d [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:161// -*- C++ -*-
2//===-------------------------- iterator ----------------------------------===//
3//
Howard Hinnant5b08a8a2010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:165//
Howard Hinnant412dbeb2010-11-16 22:09:026// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant3e519522010-05-11 19:42:168//
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;
Howard Hinnantb3371f62010-08-22 00:02:4391
Howard Hinnant3e519522010-05-11 19:42:1692 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);
Howard Hinnant03976c12010-09-14 20:26:27154 back_insert_iterator& operator=(const typename Container::value_type& value);
Howard Hinnant3e519522010-05-11 19:42:16155 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);
Howard Hinnant03976c12010-09-14 20:26:27175 front_insert_iterator& operator=(const typename Container::value_type& value);
Howard Hinnant3e519522010-05-11 19:42:16176 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);
Howard Hinnant03976c12010-09-14 20:26:27197 insert_iterator& operator=(const typename Container::value_type& value);
Howard Hinnant3e519522010-05-11 19:42:16198 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
Howard Hinnant8e882dc2012-07-20 19:36:34266 istreambuf_iterator() noexcept;
267 istreambuf_iterator(istream_type& s) noexcept;
268 istreambuf_iterator(streambuf_type* s) noexcept;
269 istreambuf_iterator(a-private-type) noexcept;
Howard Hinnant3e519522010-05-11 19:42:16270
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
Howard Hinnant8e882dc2012-07-20 19:36:34296 ostreambuf_iterator(ostream_type& s) noexcept;
297 ostreambuf_iterator(streambuf_type* s) noexcept;
Howard Hinnant3e519522010-05-11 19:42:16298 ostreambuf_iterator& operator=(charT c);
299 ostreambuf_iterator& operator*();
300 ostreambuf_iterator& operator++();
301 ostreambuf_iterator& operator++(int);
Howard Hinnant8e882dc2012-07-20 19:36:34302 bool failed() const noexcept;
Howard Hinnant3e519522010-05-11 19:42:16303};
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>
Howard Hinnantb5c63a22012-11-14 21:17:15320#if __APPLE__
321#include <Availability.h>
322#endif
323
Howard Hinnant3e519522010-05-11 19:42:16324#ifdef _LIBCPP_DEBUG
325#include <cassert>
326#endif
327
Howard Hinnant073458b2011-10-17 20:05:10328#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16329#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10330#endif
Howard Hinnant3e519522010-05-11 19:42:16331
332_LIBCPP_BEGIN_NAMESPACE_STD
333
Howard Hinnant848a5372010-09-22 16:48:34334struct _LIBCPP_VISIBLE input_iterator_tag {};
335struct _LIBCPP_VISIBLE output_iterator_tag {};
336struct _LIBCPP_VISIBLE forward_iterator_tag : public input_iterator_tag {};
337struct _LIBCPP_VISIBLE bidirectional_iterator_tag : public forward_iterator_tag {};
338struct _LIBCPP_VISIBLE random_access_iterator_tag : public bidirectional_iterator_tag {};
Howard Hinnant3e519522010-05-11 19:42:16339
340template <class _Tp>
341struct __has_iterator_category
342{
343private:
Howard Hinnant54d333a2012-10-30 19:06:59344 struct __two {char __lx; char __lxx;};
Howard Hinnant3e519522010-05-11 19:42:16345 template <class _Up> static __two __test(...);
346 template <class _Up> static char __test(typename _Up::iterator_category* = 0);
347public:
348 static const bool value = sizeof(__test<_Tp>(0)) == 1;
349};
350
351template <class _Iter, bool> struct ____iterator_traits {};
352
353template <class _Iter>
354struct ____iterator_traits<_Iter, true>
355{
356 typedef typename _Iter::difference_type difference_type;
357 typedef typename _Iter::value_type value_type;
358 typedef typename _Iter::pointer pointer;
359 typedef typename _Iter::reference reference;
360 typedef typename _Iter::iterator_category iterator_category;
361};
362
363template <class _Iter, bool> struct __iterator_traits {};
364
365template <class _Iter>
366struct __iterator_traits<_Iter, true>
367 : ____iterator_traits
368 <
369 _Iter,
370 is_convertible<typename _Iter::iterator_category, input_iterator_tag>::value ||
371 is_convertible<typename _Iter::iterator_category, output_iterator_tag>::value
372 >
373{};
374
375// iterator_traits<Iterator> will only have the nested types if Iterator::iterator_category
376// exists. Else iterator_traits<Iterator> will be an empty class. This is a
377// conforming extension which allows some programs to compile and behave as
378// the client expects instead of failing at compile time.
379
380template <class _Iter>
Howard Hinnant848a5372010-09-22 16:48:34381struct _LIBCPP_VISIBLE iterator_traits
Howard Hinnant3e519522010-05-11 19:42:16382 : __iterator_traits<_Iter, __has_iterator_category<_Iter>::value> {};
383
384template<class _Tp>
Howard Hinnant848a5372010-09-22 16:48:34385struct _LIBCPP_VISIBLE iterator_traits<_Tp*>
Howard Hinnant3e519522010-05-11 19:42:16386{
387 typedef ptrdiff_t difference_type;
388 typedef typename remove_const<_Tp>::type value_type;
389 typedef _Tp* pointer;
390 typedef _Tp& reference;
391 typedef random_access_iterator_tag iterator_category;
392};
393
394template <class _Tp, class _Up, bool = __has_iterator_category<iterator_traits<_Tp> >::value>
395struct __has_iterator_category_convertible_to
396 : public integral_constant<bool, is_convertible<typename iterator_traits<_Tp>::iterator_category, _Up>::value>
397{};
398
399template <class _Tp, class _Up>
400struct __has_iterator_category_convertible_to<_Tp, _Up, false> : public false_type {};
401
402template <class _Tp>
403struct __is_input_iterator : public __has_iterator_category_convertible_to<_Tp, input_iterator_tag> {};
404
405template <class _Tp>
406struct __is_forward_iterator : public __has_iterator_category_convertible_to<_Tp, forward_iterator_tag> {};
407
408template <class _Tp>
409struct __is_bidirectional_iterator : public __has_iterator_category_convertible_to<_Tp, bidirectional_iterator_tag> {};
410
411template <class _Tp>
412struct __is_random_access_iterator : public __has_iterator_category_convertible_to<_Tp, random_access_iterator_tag> {};
413
414template<class _Category, class _Tp, class _Distance = ptrdiff_t,
415 class _Pointer = _Tp*, class _Reference = _Tp&>
Howard Hinnant848a5372010-09-22 16:48:34416struct _LIBCPP_VISIBLE iterator
Howard Hinnant3e519522010-05-11 19:42:16417{
418 typedef _Tp value_type;
419 typedef _Distance difference_type;
420 typedef _Pointer pointer;
421 typedef _Reference reference;
422 typedef _Category iterator_category;
423};
424
425template <class _InputIter>
426inline _LIBCPP_INLINE_VISIBILITY
427void __advance(_InputIter& __i,
428 typename iterator_traits<_InputIter>::difference_type __n, input_iterator_tag)
429{
430 for (; __n > 0; --__n)
431 ++__i;
432}
433
434template <class _BiDirIter>
435inline _LIBCPP_INLINE_VISIBILITY
436void __advance(_BiDirIter& __i,
437 typename iterator_traits<_BiDirIter>::difference_type __n, bidirectional_iterator_tag)
438{
439 if (__n >= 0)
440 for (; __n > 0; --__n)
441 ++__i;
442 else
443 for (; __n < 0; ++__n)
444 --__i;
445}
446
447template <class _RandIter>
448inline _LIBCPP_INLINE_VISIBILITY
449void __advance(_RandIter& __i,
450 typename iterator_traits<_RandIter>::difference_type __n, random_access_iterator_tag)
451{
452 __i += __n;
453}
454
455template <class _InputIter>
456inline _LIBCPP_INLINE_VISIBILITY
457void advance(_InputIter& __i,
458 typename iterator_traits<_InputIter>::difference_type __n)
459{
460 __advance(__i, __n, typename iterator_traits<_InputIter>::iterator_category());
461}
462
463template <class _InputIter>
464inline _LIBCPP_INLINE_VISIBILITY
465typename iterator_traits<_InputIter>::difference_type
466__distance(_InputIter __first, _InputIter __last, input_iterator_tag)
467{
468 typename iterator_traits<_InputIter>::difference_type __r(0);
469 for (; __first != __last; ++__first)
470 ++__r;
471 return __r;
472}
473
474template <class _RandIter>
475inline _LIBCPP_INLINE_VISIBILITY
476typename iterator_traits<_RandIter>::difference_type
477__distance(_RandIter __first, _RandIter __last, random_access_iterator_tag)
478{
479 return __last - __first;
480}
481
482template <class _InputIter>
483inline _LIBCPP_INLINE_VISIBILITY
484typename iterator_traits<_InputIter>::difference_type
485distance(_InputIter __first, _InputIter __last)
486{
487 return __distance(__first, __last, typename iterator_traits<_InputIter>::iterator_category());
488}
489
490template <class _ForwardIter>
Howard Hinnant848a5372010-09-22 16:48:34491inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16492_ForwardIter
493next(_ForwardIter __x,
494 typename iterator_traits<_ForwardIter>::difference_type __n = 1,
495 typename enable_if<__is_forward_iterator<_ForwardIter>::value>::type* = 0)
496{
Howard Hinnantce48a112011-06-30 21:18:19497 _VSTD::advance(__x, __n);
Howard Hinnant3e519522010-05-11 19:42:16498 return __x;
499}
500
501template <class _BidiretionalIter>
Howard Hinnant848a5372010-09-22 16:48:34502inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16503_BidiretionalIter
504prev(_BidiretionalIter __x,
505 typename iterator_traits<_BidiretionalIter>::difference_type __n = 1,
506 typename enable_if<__is_bidirectional_iterator<_BidiretionalIter>::value>::type* = 0)
507{
Howard Hinnantce48a112011-06-30 21:18:19508 _VSTD::advance(__x, -__n);
Howard Hinnant3e519522010-05-11 19:42:16509 return __x;
510}
511
512template <class _Iter>
Howard Hinnant848a5372010-09-22 16:48:34513class _LIBCPP_VISIBLE reverse_iterator
Howard Hinnant3e519522010-05-11 19:42:16514 : public iterator<typename iterator_traits<_Iter>::iterator_category,
515 typename iterator_traits<_Iter>::value_type,
516 typename iterator_traits<_Iter>::difference_type,
517 typename iterator_traits<_Iter>::pointer,
518 typename iterator_traits<_Iter>::reference>
519{
520private:
521 mutable _Iter __t;
522protected:
523 _Iter current;
524public:
525 typedef _Iter iterator_type;
526 typedef typename iterator_traits<_Iter>::difference_type difference_type;
527 typedef typename iterator_traits<_Iter>::reference reference;
528 typedef typename iterator_traits<_Iter>::pointer pointer;
Howard Hinnantb3371f62010-08-22 00:02:43529
Howard Hinnant3e519522010-05-11 19:42:16530 _LIBCPP_INLINE_VISIBILITY reverse_iterator() : current() {}
531 _LIBCPP_INLINE_VISIBILITY explicit reverse_iterator(_Iter __x) : __t(__x), current(__x) {}
532 template <class _Up> _LIBCPP_INLINE_VISIBILITY reverse_iterator(const reverse_iterator<_Up>& __u)
533 : __t(__u.base()), current(__u.base()) {}
534 _LIBCPP_INLINE_VISIBILITY _Iter base() const {return current;}
535 _LIBCPP_INLINE_VISIBILITY reference operator*() const {__t = current; return *--__t;}
536 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return &(operator*());}
537 _LIBCPP_INLINE_VISIBILITY reverse_iterator& operator++() {--current; return *this;}
538 _LIBCPP_INLINE_VISIBILITY reverse_iterator operator++(int)
539 {reverse_iterator __tmp(*this); --current; return __tmp;}
540 _LIBCPP_INLINE_VISIBILITY reverse_iterator& operator--() {++current; return *this;}
541 _LIBCPP_INLINE_VISIBILITY reverse_iterator operator--(int)
542 {reverse_iterator __tmp(*this); ++current; return __tmp;}
543 _LIBCPP_INLINE_VISIBILITY reverse_iterator operator+ (difference_type __n) const
544 {return reverse_iterator(current - __n);}
545 _LIBCPP_INLINE_VISIBILITY reverse_iterator& operator+=(difference_type __n)
546 {current -= __n; return *this;}
547 _LIBCPP_INLINE_VISIBILITY reverse_iterator operator- (difference_type __n) const
548 {return reverse_iterator(current + __n);}
549 _LIBCPP_INLINE_VISIBILITY reverse_iterator& operator-=(difference_type __n)
550 {current += __n; return *this;}
551 _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const
552 {return current[-__n-1];}
553};
554
555template <class _Iter1, class _Iter2>
556inline _LIBCPP_INLINE_VISIBILITY
557bool
558operator==(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
559{
560 return __x.base() == __y.base();
561}
562
563template <class _Iter1, class _Iter2>
564inline _LIBCPP_INLINE_VISIBILITY
565bool
566operator<(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
567{
568 return __x.base() > __y.base();
569}
570
571template <class _Iter1, class _Iter2>
572inline _LIBCPP_INLINE_VISIBILITY
573bool
574operator!=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
575{
576 return __x.base() != __y.base();
577}
578
579template <class _Iter1, class _Iter2>
580inline _LIBCPP_INLINE_VISIBILITY
581bool
582operator>(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
583{
584 return __x.base() < __y.base();
585}
586
587template <class _Iter1, class _Iter2>
588inline _LIBCPP_INLINE_VISIBILITY
589bool
590operator>=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
591{
592 return __x.base() <= __y.base();
593}
594
595template <class _Iter1, class _Iter2>
596inline _LIBCPP_INLINE_VISIBILITY
597bool
598operator<=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
599{
600 return __x.base() >= __y.base();
601}
602
603template <class _Iter1, class _Iter2>
604inline _LIBCPP_INLINE_VISIBILITY
605typename reverse_iterator<_Iter1>::difference_type
606operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
607{
608 return __y.base() - __x.base();
609}
610
611template <class _Iter>
612inline _LIBCPP_INLINE_VISIBILITY
613reverse_iterator<_Iter>
614operator+(typename reverse_iterator<_Iter>::difference_type __n, const reverse_iterator<_Iter>& __x)
615{
616 return reverse_iterator<_Iter>(__x.base() - __n);
617}
618
619template <class _Container>
Howard Hinnant848a5372010-09-22 16:48:34620class _LIBCPP_VISIBLE back_insert_iterator
Howard Hinnant3e519522010-05-11 19:42:16621 : public iterator<output_iterator_tag,
622 void,
623 void,
624 void,
625 back_insert_iterator<_Container>&>
626{
627protected:
628 _Container* container;
629public:
630 typedef _Container container_type;
631
632 _LIBCPP_INLINE_VISIBILITY explicit back_insert_iterator(_Container& __x) : container(&__x) {}
Howard Hinnante4383372011-10-22 20:59:45633 _LIBCPP_INLINE_VISIBILITY back_insert_iterator& operator=(const typename _Container::value_type& __value_)
634 {container->push_back(__value_); return *this;}
Howard Hinnant7609c9b2010-09-04 23:28:19635#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante4383372011-10-22 20:59:45636 _LIBCPP_INLINE_VISIBILITY back_insert_iterator& operator=(typename _Container::value_type&& __value_)
637 {container->push_back(_VSTD::move(__value_)); return *this;}
Howard Hinnant7609c9b2010-09-04 23:28:19638#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16639 _LIBCPP_INLINE_VISIBILITY back_insert_iterator& operator*() {return *this;}
640 _LIBCPP_INLINE_VISIBILITY back_insert_iterator& operator++() {return *this;}
641 _LIBCPP_INLINE_VISIBILITY back_insert_iterator operator++(int) {return *this;}
642};
643
644template <class _Container>
645inline _LIBCPP_INLINE_VISIBILITY
646back_insert_iterator<_Container>
647back_inserter(_Container& __x)
648{
649 return back_insert_iterator<_Container>(__x);
650}
651
652template <class _Container>
Howard Hinnant848a5372010-09-22 16:48:34653class _LIBCPP_VISIBLE front_insert_iterator
Howard Hinnant3e519522010-05-11 19:42:16654 : public iterator<output_iterator_tag,
655 void,
656 void,
657 void,
658 front_insert_iterator<_Container>&>
659{
660protected:
661 _Container* container;
662public:
663 typedef _Container container_type;
664
665 _LIBCPP_INLINE_VISIBILITY explicit front_insert_iterator(_Container& __x) : container(&__x) {}
Howard Hinnante4383372011-10-22 20:59:45666 _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator=(const typename _Container::value_type& __value_)
667 {container->push_front(__value_); return *this;}
Howard Hinnant7609c9b2010-09-04 23:28:19668#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante4383372011-10-22 20:59:45669 _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator=(typename _Container::value_type&& __value_)
670 {container->push_front(_VSTD::move(__value_)); return *this;}
Howard Hinnant7609c9b2010-09-04 23:28:19671#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16672 _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator*() {return *this;}
673 _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator++() {return *this;}
674 _LIBCPP_INLINE_VISIBILITY front_insert_iterator operator++(int) {return *this;}
675};
676
677template <class _Container>
678inline _LIBCPP_INLINE_VISIBILITY
679front_insert_iterator<_Container>
680front_inserter(_Container& __x)
681{
682 return front_insert_iterator<_Container>(__x);
683}
684
685template <class _Container>
Howard Hinnant848a5372010-09-22 16:48:34686class _LIBCPP_VISIBLE insert_iterator
Howard Hinnant3e519522010-05-11 19:42:16687 : public iterator<output_iterator_tag,
688 void,
689 void,
690 void,
691 insert_iterator<_Container>&>
692{
693protected:
694 _Container* container;
695 typename _Container::iterator iter;
696public:
697 typedef _Container container_type;
698
699 _LIBCPP_INLINE_VISIBILITY insert_iterator(_Container& __x, typename _Container::iterator __i)
700 : container(&__x), iter(__i) {}
Howard Hinnante4383372011-10-22 20:59:45701 _LIBCPP_INLINE_VISIBILITY insert_iterator& operator=(const typename _Container::value_type& __value_)
702 {iter = container->insert(iter, __value_); ++iter; return *this;}
Howard Hinnant7609c9b2010-09-04 23:28:19703#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante4383372011-10-22 20:59:45704 _LIBCPP_INLINE_VISIBILITY insert_iterator& operator=(typename _Container::value_type&& __value_)
705 {iter = container->insert(iter, _VSTD::move(__value_)); ++iter; return *this;}
Howard Hinnant7609c9b2010-09-04 23:28:19706#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16707 _LIBCPP_INLINE_VISIBILITY insert_iterator& operator*() {return *this;}
708 _LIBCPP_INLINE_VISIBILITY insert_iterator& operator++() {return *this;}
709 _LIBCPP_INLINE_VISIBILITY insert_iterator& operator++(int) {return *this;}
710};
711
712template <class _Container>
713inline _LIBCPP_INLINE_VISIBILITY
714insert_iterator<_Container>
715inserter(_Container& __x, typename _Container::iterator __i)
716{
717 return insert_iterator<_Container>(__x, __i);
718}
719
720template <class _Tp, class _CharT = char,
721 class _Traits = char_traits<_CharT>, class _Distance = ptrdiff_t>
Howard Hinnant848a5372010-09-22 16:48:34722class _LIBCPP_VISIBLE istream_iterator
Howard Hinnant3e519522010-05-11 19:42:16723 : public iterator<input_iterator_tag, _Tp, _Distance, const _Tp*, const _Tp&>
724{
725public:
726 typedef _CharT char_type;
727 typedef _Traits traits_type;
728 typedef basic_istream<_CharT,_Traits> istream_type;
729private:
730 istream_type* __in_stream_;
731 _Tp __value_;
732public:
733 _LIBCPP_INLINE_VISIBILITY istream_iterator() : __in_stream_(0) {}
734 _LIBCPP_INLINE_VISIBILITY istream_iterator(istream_type& __s) : __in_stream_(&__s)
735 {
736 if (!(*__in_stream_ >> __value_))
737 __in_stream_ = 0;
738 }
739
740 _LIBCPP_INLINE_VISIBILITY const _Tp& operator*() const {return __value_;}
741 _LIBCPP_INLINE_VISIBILITY const _Tp* operator->() const {return &(operator*());}
742 _LIBCPP_INLINE_VISIBILITY istream_iterator& operator++()
743 {
744 if (!(*__in_stream_ >> __value_))
745 __in_stream_ = 0;
746 return *this;
747 }
748 _LIBCPP_INLINE_VISIBILITY istream_iterator operator++(int)
749 {istream_iterator __t(*this); ++(*this); return __t;}
750
751 friend _LIBCPP_INLINE_VISIBILITY
752 bool operator==(const istream_iterator& __x, const istream_iterator& __y)
753 {return __x.__in_stream_ == __y.__in_stream_;}
754
755 friend _LIBCPP_INLINE_VISIBILITY
756 bool operator!=(const istream_iterator& __x, const istream_iterator& __y)
757 {return !(__x == __y);}
758};
759
760template <class _Tp, class _CharT = char, class _Traits = char_traits<_CharT> >
Howard Hinnant848a5372010-09-22 16:48:34761class _LIBCPP_VISIBLE ostream_iterator
Howard Hinnant3e519522010-05-11 19:42:16762 : public iterator<output_iterator_tag, void, void, void, void>
763{
764public:
765 typedef _CharT char_type;
766 typedef _Traits traits_type;
767 typedef basic_ostream<_CharT,_Traits> ostream_type;
768private:
769 ostream_type* __out_stream_;
770 const char_type* __delim_;
771public:
772 _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s)
773 : __out_stream_(&__s), __delim_(0) {}
774 _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s, const _CharT* __delimiter)
775 : __out_stream_(&__s), __delim_(__delimiter) {}
Howard Hinnante4383372011-10-22 20:59:45776 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator=(const _Tp& __value_)
Howard Hinnant3e519522010-05-11 19:42:16777 {
Howard Hinnante4383372011-10-22 20:59:45778 *__out_stream_ << __value_;
Howard Hinnant3e519522010-05-11 19:42:16779 if (__delim_)
780 *__out_stream_ << __delim_;
781 return *this;
782 }
783
784 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator*() {return *this;}
785 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++() {return *this;}
786 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++(int) {return *this;}
787};
788
789template<class _CharT, class _Traits>
Howard Hinnant848a5372010-09-22 16:48:34790class _LIBCPP_VISIBLE istreambuf_iterator
Howard Hinnant3e519522010-05-11 19:42:16791 : public iterator<input_iterator_tag, _CharT,
792 typename _Traits::off_type, _CharT*,
793 _CharT>
794{
795public:
796 typedef _CharT char_type;
797 typedef _Traits traits_type;
798 typedef typename _Traits::int_type int_type;
799 typedef basic_streambuf<_CharT,_Traits> streambuf_type;
800 typedef basic_istream<_CharT,_Traits> istream_type;
801private:
802 streambuf_type* __sbuf_;
803
804 class __proxy
805 {
806 char_type __keep_;
807 streambuf_type* __sbuf_;
808 _LIBCPP_INLINE_VISIBILITY __proxy(char_type __c, streambuf_type* __s)
809 : __keep_(__c), __sbuf_(__s) {}
810 friend class istreambuf_iterator;
811 public:
812 _LIBCPP_INLINE_VISIBILITY char_type operator*() const {return __keep_;}
813 };
814
Howard Hinnant848a5372010-09-22 16:48:34815 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16816 void __test_for_eof()
817 {
818 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof()))
819 __sbuf_ = 0;
820 }
821public:
Howard Hinnant8e882dc2012-07-20 19:36:34822 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator() _NOEXCEPT : __sbuf_(0) {}
823 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(istream_type& __s) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16824 : __sbuf_(__s.rdbuf()) {__test_for_eof();}
Howard Hinnant8e882dc2012-07-20 19:36:34825 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(streambuf_type* __s) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16826 : __sbuf_(__s) {__test_for_eof();}
Howard Hinnant8e882dc2012-07-20 19:36:34827 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(const __proxy& __p) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16828 : __sbuf_(__p.__sbuf_) {}
829
Howard Hinnantc2063662011-12-01 20:21:04830 _LIBCPP_INLINE_VISIBILITY char_type operator*() const
831 {return static_cast<char_type>(__sbuf_->sgetc());}
Howard Hinnant3e519522010-05-11 19:42:16832 _LIBCPP_INLINE_VISIBILITY char_type* operator->() const {return nullptr;}
833 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator& operator++()
834 {
835 if (traits_type::eq_int_type(__sbuf_->snextc(), traits_type::eof()))
836 __sbuf_ = 0;
837 return *this;
838 }
839 _LIBCPP_INLINE_VISIBILITY __proxy operator++(int)
840 {
841 char_type __c = __sbuf_->sgetc();
842 ++(*this);
843 return __proxy(__c, __sbuf_);
844 }
845
846 _LIBCPP_INLINE_VISIBILITY bool equal(const istreambuf_iterator& __b) const
847 {return (__sbuf_ == 0) == (__b.__sbuf_ == 0);}
848};
849
850template <class _CharT, class _Traits>
851inline _LIBCPP_INLINE_VISIBILITY
852bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,
853 const istreambuf_iterator<_CharT,_Traits>& __b)
854 {return __a.equal(__b);}
855
856template <class _CharT, class _Traits>
857inline _LIBCPP_INLINE_VISIBILITY
858bool operator!=(const istreambuf_iterator<_CharT,_Traits>& __a,
859 const istreambuf_iterator<_CharT,_Traits>& __b)
860 {return !__a.equal(__b);}
861
862template <class _CharT, class _Traits>
Howard Hinnant848a5372010-09-22 16:48:34863class _LIBCPP_VISIBLE ostreambuf_iterator
Howard Hinnant3e519522010-05-11 19:42:16864 : public iterator<output_iterator_tag, void, void, void, void>
865{
866public:
867 typedef _CharT char_type;
868 typedef _Traits traits_type;
869 typedef basic_streambuf<_CharT,_Traits> streambuf_type;
870 typedef basic_ostream<_CharT,_Traits> ostream_type;
871private:
872 streambuf_type* __sbuf_;
873public:
Howard Hinnant8e882dc2012-07-20 19:36:34874 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(ostream_type& __s) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16875 : __sbuf_(__s.rdbuf()) {}
Howard Hinnant8e882dc2012-07-20 19:36:34876 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(streambuf_type* __s) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16877 : __sbuf_(__s) {}
878 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator=(_CharT __c)
879 {
880 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sputc(__c), traits_type::eof()))
881 __sbuf_ = 0;
882 return *this;
883 }
884 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator*() {return *this;}
885 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++() {return *this;}
886 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++(int) {return *this;}
Howard Hinnant8e882dc2012-07-20 19:36:34887 _LIBCPP_INLINE_VISIBILITY bool failed() const _NOEXCEPT {return __sbuf_ == 0;}
Howard Hinnant92b59402012-09-19 19:14:15888
Howard Hinnantb5c63a22012-11-14 21:17:15889#if !defined(__APPLE__) || \
890 (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED > __MAC_10_8) || \
891 (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_6_0)
892
Howard Hinnant92b59402012-09-19 19:14:15893 template <class _Ch, class _Tr>
894 friend
895 _LIBCPP_HIDDEN
896 ostreambuf_iterator<_Ch, _Tr>
897 __pad_and_output(ostreambuf_iterator<_Ch, _Tr> __s,
898 const _Ch* __ob, const _Ch* __op, const _Ch* __oe,
899 ios_base& __iob, _Ch __fl);
Howard Hinnantb5c63a22012-11-14 21:17:15900#endif
Howard Hinnant3e519522010-05-11 19:42:16901};
902
903template <class _Iter>
Howard Hinnant848a5372010-09-22 16:48:34904class _LIBCPP_VISIBLE move_iterator
Howard Hinnant3e519522010-05-11 19:42:16905{
906private:
907 _Iter __i;
908public:
909 typedef _Iter iterator_type;
910 typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
911 typedef typename iterator_traits<iterator_type>::value_type value_type;
912 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
913 typedef typename iterator_traits<iterator_type>::pointer pointer;
Howard Hinnant7609c9b2010-09-04 23:28:19914#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16915 typedef value_type&& reference;
916#else
917 typedef typename iterator_traits<iterator_type>::reference reference;
918#endif
Howard Hinnantb3371f62010-08-22 00:02:43919
Howard Hinnant3e519522010-05-11 19:42:16920 _LIBCPP_INLINE_VISIBILITY move_iterator() : __i() {}
921 _LIBCPP_INLINE_VISIBILITY explicit move_iterator(_Iter __x) : __i(__x) {}
922 template <class _Up> _LIBCPP_INLINE_VISIBILITY move_iterator(const move_iterator<_Up>& __u)
923 : __i(__u.base()) {}
924 _LIBCPP_INLINE_VISIBILITY _Iter base() const {return __i;}
Douglas Gregord18302f2011-01-26 00:12:48925 _LIBCPP_INLINE_VISIBILITY reference operator*() const {
926 return static_cast<reference>(*__i);
927 }
928 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {
929 typename iterator_traits<iterator_type>::reference __ref = *__i;
930 return &__ref;
931 }
Howard Hinnant3e519522010-05-11 19:42:16932 _LIBCPP_INLINE_VISIBILITY move_iterator& operator++() {++__i; return *this;}
933 _LIBCPP_INLINE_VISIBILITY move_iterator operator++(int)
934 {move_iterator __tmp(*this); ++__i; return __tmp;}
935 _LIBCPP_INLINE_VISIBILITY move_iterator& operator--() {--__i; return *this;}
936 _LIBCPP_INLINE_VISIBILITY move_iterator operator--(int)
937 {move_iterator __tmp(*this); --__i; return __tmp;}
938 _LIBCPP_INLINE_VISIBILITY move_iterator operator+ (difference_type __n) const
939 {return move_iterator(__i + __n);}
940 _LIBCPP_INLINE_VISIBILITY move_iterator& operator+=(difference_type __n)
941 {__i += __n; return *this;}
942 _LIBCPP_INLINE_VISIBILITY move_iterator operator- (difference_type __n) const
943 {return move_iterator(__i - __n);}
944 _LIBCPP_INLINE_VISIBILITY move_iterator& operator-=(difference_type __n)
945 {__i -= __n; return *this;}
946 _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const
Douglas Gregord18302f2011-01-26 00:12:48947 {
948 return static_cast<reference>(__i[__n]);
949 }
Howard Hinnant3e519522010-05-11 19:42:16950};
951
952template <class _Iter1, class _Iter2>
953inline _LIBCPP_INLINE_VISIBILITY
954bool
955operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
956{
957 return __x.base() == __y.base();
958}
959
960template <class _Iter1, class _Iter2>
961inline _LIBCPP_INLINE_VISIBILITY
962bool
963operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
964{
965 return __x.base() < __y.base();
966}
967
968template <class _Iter1, class _Iter2>
969inline _LIBCPP_INLINE_VISIBILITY
970bool
971operator!=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
972{
973 return __x.base() != __y.base();
974}
975
976template <class _Iter1, class _Iter2>
977inline _LIBCPP_INLINE_VISIBILITY
978bool
979operator>(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
980{
981 return __x.base() > __y.base();
982}
983
984template <class _Iter1, class _Iter2>
985inline _LIBCPP_INLINE_VISIBILITY
986bool
987operator>=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
988{
989 return __x.base() >= __y.base();
990}
991
992template <class _Iter1, class _Iter2>
993inline _LIBCPP_INLINE_VISIBILITY
994bool
995operator<=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
996{
997 return __x.base() <= __y.base();
998}
999
1000template <class _Iter1, class _Iter2>
1001inline _LIBCPP_INLINE_VISIBILITY
1002typename move_iterator<_Iter1>::difference_type
1003operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1004{
1005 return __x.base() - __y.base();
1006}
1007
1008template <class _Iter>
1009inline _LIBCPP_INLINE_VISIBILITY
1010move_iterator<_Iter>
1011operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
1012{
1013 return move_iterator<_Iter>(__x.base() + __n);
1014}
1015
1016template <class _Iter>
1017inline _LIBCPP_INLINE_VISIBILITY
1018move_iterator<_Iter>
1019make_move_iterator(const _Iter& __i)
1020{
1021 return move_iterator<_Iter>(__i);
1022}
1023
1024// __wrap_iter
1025
1026template <class _Iter> class __wrap_iter;
1027
1028template <class _Iter1, class _Iter2>
Howard Hinnantaeb85682012-09-14 00:39:161029_LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161030bool
Howard Hinnant76c7cd02011-05-29 19:57:121031operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:161032
1033template <class _Iter1, class _Iter2>
Howard Hinnantaeb85682012-09-14 00:39:161034_LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161035bool
Howard Hinnant76c7cd02011-05-29 19:57:121036operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:161037
1038template <class _Iter1, class _Iter2>
Howard Hinnantaeb85682012-09-14 00:39:161039_LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161040bool
Howard Hinnant76c7cd02011-05-29 19:57:121041operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:161042
1043template <class _Iter1, class _Iter2>
Howard Hinnantaeb85682012-09-14 00:39:161044_LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161045bool
Howard Hinnant76c7cd02011-05-29 19:57:121046operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:161047
1048template <class _Iter1, class _Iter2>
Howard Hinnantaeb85682012-09-14 00:39:161049_LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161050bool
Howard Hinnant76c7cd02011-05-29 19:57:121051operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:161052
1053template <class _Iter1, class _Iter2>
Howard Hinnantaeb85682012-09-14 00:39:161054_LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161055bool
Howard Hinnant76c7cd02011-05-29 19:57:121056operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:161057
1058template <class _Iter1, class _Iter2>
Howard Hinnantaeb85682012-09-14 00:39:161059_LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161060typename __wrap_iter<_Iter1>::difference_type
Howard Hinnant76c7cd02011-05-29 19:57:121061operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:161062
1063template <class _Iter>
Howard Hinnantaeb85682012-09-14 00:39:161064_LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161065__wrap_iter<_Iter>
Howard Hinnant7ba930b2011-10-11 21:28:381066operator+(typename __wrap_iter<_Iter>::difference_type, __wrap_iter<_Iter>) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:161067
Howard Hinnantaeb85682012-09-14 00:39:161068template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY copy(_Ip, _Ip, _Op);
1069template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY copy_backward(_B1, _B1, _B2);
1070template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY move(_Ip, _Ip, _Op);
1071template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY move_backward(_B1, _B1, _B2);
Howard Hinnant3e519522010-05-11 19:42:161072
1073template <class _Tp>
Howard Hinnantaeb85682012-09-14 00:39:161074_LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161075typename enable_if
1076<
Howard Hinnantca740482010-11-19 22:17:281077 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnant3e519522010-05-11 19:42:161078 _Tp*
1079>::type
1080__unwrap_iter(__wrap_iter<_Tp*>);
1081
1082template <class _Iter>
1083class __wrap_iter
1084{
1085public:
1086 typedef _Iter iterator_type;
1087 typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
1088 typedef typename iterator_traits<iterator_type>::value_type value_type;
1089 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
1090 typedef typename iterator_traits<iterator_type>::pointer pointer;
1091 typedef typename iterator_traits<iterator_type>::reference reference;
1092private:
1093 iterator_type __i;
1094public:
Howard Hinnantc36bfc42011-09-16 19:52:231095 _LIBCPP_INLINE_VISIBILITY __wrap_iter() _NOEXCEPT
1096 {
1097#if _LIBCPP_DEBUG_LEVEL >= 2
1098 __get_db()->__insert_i(this);
1099#endif
1100 }
Howard Hinnant3e519522010-05-11 19:42:161101 template <class _Up> _LIBCPP_INLINE_VISIBILITY __wrap_iter(const __wrap_iter<_Up>& __u,
Howard Hinnant76c7cd02011-05-29 19:57:121102 typename enable_if<is_convertible<_Up, iterator_type>::value>::type* = 0) _NOEXCEPT
Howard Hinnantf554add2011-09-14 18:33:511103 : __i(__u.base())
1104 {
Howard Hinnantcec9af92011-09-16 17:29:171105#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantf554add2011-09-14 18:33:511106 __get_db()->__iterator_copy(this, &__u);
1107#endif
1108 }
Howard Hinnantcec9af92011-09-16 17:29:171109#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantf554add2011-09-14 18:33:511110 _LIBCPP_INLINE_VISIBILITY
1111 __wrap_iter(const __wrap_iter& __x)
1112 : __i(__x.base())
1113 {
1114 __get_db()->__iterator_copy(this, &__x);
1115 }
1116 _LIBCPP_INLINE_VISIBILITY
1117 __wrap_iter& operator=(const __wrap_iter& __x)
1118 {
1119 if (this != &__x)
1120 {
1121 __get_db()->__iterator_copy(this, &__x);
1122 __i = __x.__i;
1123 }
1124 return *this;
1125 }
1126 _LIBCPP_INLINE_VISIBILITY
1127 ~__wrap_iter()
1128 {
1129 __get_db()->__erase_i(this);
1130 }
1131#endif
1132 _LIBCPP_INLINE_VISIBILITY reference operator*() const _NOEXCEPT
1133 {
Howard Hinnantcec9af92011-09-16 17:29:171134#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantf554add2011-09-14 18:33:511135 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1136 "Attempted to dereference a non-dereferenceable iterator");
Howard Hinnantcec9af92011-09-16 17:29:171137#endif
Howard Hinnantf554add2011-09-14 18:33:511138 return *__i;
1139 }
Howard Hinnant76c7cd02011-05-29 19:57:121140 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return &(operator*());}
Howard Hinnantf554add2011-09-14 18:33:511141 _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator++() _NOEXCEPT
1142 {
Howard Hinnantcec9af92011-09-16 17:29:171143#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantf554add2011-09-14 18:33:511144 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1145 "Attempted to increment non-incrementable iterator");
Howard Hinnantcec9af92011-09-16 17:29:171146#endif
Howard Hinnantf554add2011-09-14 18:33:511147 ++__i;
1148 return *this;
1149 }
Howard Hinnant76c7cd02011-05-29 19:57:121150 _LIBCPP_INLINE_VISIBILITY __wrap_iter operator++(int) _NOEXCEPT
Howard Hinnantf554add2011-09-14 18:33:511151 {__wrap_iter __tmp(*this); ++(*this); return __tmp;}
1152 _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator--() _NOEXCEPT
1153 {
Howard Hinnantcec9af92011-09-16 17:29:171154#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantf554add2011-09-14 18:33:511155 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
1156 "Attempted to decrement non-decrementable iterator");
Howard Hinnantcec9af92011-09-16 17:29:171157#endif
Howard Hinnantf554add2011-09-14 18:33:511158 --__i;
1159 return *this;
1160 }
Howard Hinnant76c7cd02011-05-29 19:57:121161 _LIBCPP_INLINE_VISIBILITY __wrap_iter operator--(int) _NOEXCEPT
Howard Hinnantf554add2011-09-14 18:33:511162 {__wrap_iter __tmp(*this); --(*this); return __tmp;}
Howard Hinnant76c7cd02011-05-29 19:57:121163 _LIBCPP_INLINE_VISIBILITY __wrap_iter operator+ (difference_type __n) const _NOEXCEPT
Howard Hinnantf554add2011-09-14 18:33:511164 {__wrap_iter __w(*this); __w += __n; return __w;}
Howard Hinnant76c7cd02011-05-29 19:57:121165 _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator+=(difference_type __n) _NOEXCEPT
Howard Hinnantf554add2011-09-14 18:33:511166 {
Howard Hinnantcec9af92011-09-16 17:29:171167#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantf554add2011-09-14 18:33:511168 _LIBCPP_ASSERT(__get_const_db()->__addable(this, __n),
1169 "Attempted to add/subtract iterator outside of valid range");
Howard Hinnantcec9af92011-09-16 17:29:171170#endif
Howard Hinnantf554add2011-09-14 18:33:511171 __i += __n;
1172 return *this;
1173 }
Howard Hinnant76c7cd02011-05-29 19:57:121174 _LIBCPP_INLINE_VISIBILITY __wrap_iter operator- (difference_type __n) const _NOEXCEPT
Howard Hinnantf554add2011-09-14 18:33:511175 {return *this + (-__n);}
Howard Hinnant76c7cd02011-05-29 19:57:121176 _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator-=(difference_type __n) _NOEXCEPT
Howard Hinnantf554add2011-09-14 18:33:511177 {*this += -__n; return *this;}
Howard Hinnant76c7cd02011-05-29 19:57:121178 _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const _NOEXCEPT
Howard Hinnantf554add2011-09-14 18:33:511179 {
Howard Hinnantcec9af92011-09-16 17:29:171180#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantf554add2011-09-14 18:33:511181 _LIBCPP_ASSERT(__get_const_db()->__subscriptable(this, __n),
1182 "Attempted to subscript iterator outside of valid range");
Howard Hinnantcec9af92011-09-16 17:29:171183#endif
Howard Hinnantf554add2011-09-14 18:33:511184 return __i[__n];
1185 }
Howard Hinnant3e519522010-05-11 19:42:161186
Howard Hinnant76c7cd02011-05-29 19:57:121187 _LIBCPP_INLINE_VISIBILITY iterator_type base() const _NOEXCEPT {return __i;}
Howard Hinnant3e519522010-05-11 19:42:161188
1189private:
Howard Hinnant76c7cd02011-05-29 19:57:121190 _LIBCPP_INLINE_VISIBILITY __wrap_iter(iterator_type __x) _NOEXCEPT : __i(__x) {}
Howard Hinnantcec9af92011-09-16 17:29:171191#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantf554add2011-09-14 18:33:511192 _LIBCPP_INLINE_VISIBILITY __wrap_iter(const void* __p, iterator_type __x) : __i(__x)
1193 {
1194 __get_db()->__insert_ic(this, __p);
1195 }
1196#endif
Howard Hinnant3e519522010-05-11 19:42:161197
1198 template <class _Up> friend class __wrap_iter;
1199 template <class _CharT, class _Traits, class _Alloc> friend class basic_string;
1200 template <class _Tp, class _Alloc> friend class vector;
1201
1202 template <class _Iter1, class _Iter2>
1203 friend
1204 bool
Howard Hinnant76c7cd02011-05-29 19:57:121205 operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantb3371f62010-08-22 00:02:431206
Howard Hinnant3e519522010-05-11 19:42:161207 template <class _Iter1, class _Iter2>
1208 friend
1209 bool
Howard Hinnant76c7cd02011-05-29 19:57:121210 operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantb3371f62010-08-22 00:02:431211
Howard Hinnant3e519522010-05-11 19:42:161212 template <class _Iter1, class _Iter2>
1213 friend
1214 bool
Howard Hinnant76c7cd02011-05-29 19:57:121215 operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantb3371f62010-08-22 00:02:431216
Howard Hinnant3e519522010-05-11 19:42:161217 template <class _Iter1, class _Iter2>
1218 friend
1219 bool
Howard Hinnant76c7cd02011-05-29 19:57:121220 operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantb3371f62010-08-22 00:02:431221
Howard Hinnant3e519522010-05-11 19:42:161222 template <class _Iter1, class _Iter2>
1223 friend
1224 bool
Howard Hinnant76c7cd02011-05-29 19:57:121225 operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantb3371f62010-08-22 00:02:431226
Howard Hinnant3e519522010-05-11 19:42:161227 template <class _Iter1, class _Iter2>
1228 friend
1229 bool
Howard Hinnant76c7cd02011-05-29 19:57:121230 operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantb3371f62010-08-22 00:02:431231
Howard Hinnant3e519522010-05-11 19:42:161232 template <class _Iter1, class _Iter2>
1233 friend
1234 typename __wrap_iter<_Iter1>::difference_type
Howard Hinnant76c7cd02011-05-29 19:57:121235 operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantb3371f62010-08-22 00:02:431236
Howard Hinnant3e519522010-05-11 19:42:161237 template <class _Iter1>
1238 friend
1239 __wrap_iter<_Iter1>
Howard Hinnant7ba930b2011-10-11 21:28:381240 operator+(typename __wrap_iter<_Iter1>::difference_type, __wrap_iter<_Iter1>) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:161241
Howard Hinnantc003db12011-11-29 18:15:501242 template <class _Ip, class _Op> friend _Op copy(_Ip, _Ip, _Op);
Howard Hinnant3e519522010-05-11 19:42:161243 template <class _B1, class _B2> friend _B2 copy_backward(_B1, _B1, _B2);
Howard Hinnantc003db12011-11-29 18:15:501244 template <class _Ip, class _Op> friend _Op move(_Ip, _Ip, _Op);
Howard Hinnant3e519522010-05-11 19:42:161245 template <class _B1, class _B2> friend _B2 move_backward(_B1, _B1, _B2);
1246
1247 template <class _Tp>
1248 friend
1249 typename enable_if
1250 <
Howard Hinnantca740482010-11-19 22:17:281251 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnant3e519522010-05-11 19:42:161252 _Tp*
1253 >::type
1254 __unwrap_iter(__wrap_iter<_Tp*>);
1255};
1256
1257template <class _Iter1, class _Iter2>
1258inline _LIBCPP_INLINE_VISIBILITY
1259bool
Howard Hinnant76c7cd02011-05-29 19:57:121260operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161261{
Howard Hinnantcec9af92011-09-16 17:29:171262#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantf554add2011-09-14 18:33:511263 _LIBCPP_ASSERT(__get_const_db()->__comparable(&__x, &__y),
1264 "Attempted to compare incomparable iterators");
Howard Hinnantcec9af92011-09-16 17:29:171265#endif
Howard Hinnant3e519522010-05-11 19:42:161266 return __x.base() == __y.base();
1267}
1268
1269template <class _Iter1, class _Iter2>
1270inline _LIBCPP_INLINE_VISIBILITY
1271bool
Howard Hinnant76c7cd02011-05-29 19:57:121272operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161273{
Howard Hinnantcec9af92011-09-16 17:29:171274#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantf554add2011-09-14 18:33:511275 _LIBCPP_ASSERT(__get_const_db()->__comparable(&__x, &__y),
1276 "Attempted to compare incomparable iterators");
Howard Hinnantcec9af92011-09-16 17:29:171277#endif
Howard Hinnant3e519522010-05-11 19:42:161278 return __x.base() < __y.base();
1279}
1280
1281template <class _Iter1, class _Iter2>
1282inline _LIBCPP_INLINE_VISIBILITY
1283bool
Howard Hinnant76c7cd02011-05-29 19:57:121284operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161285{
Howard Hinnantf554add2011-09-14 18:33:511286 return !(__x == __y);
Howard Hinnant3e519522010-05-11 19:42:161287}
1288
1289template <class _Iter1, class _Iter2>
1290inline _LIBCPP_INLINE_VISIBILITY
1291bool
Howard Hinnant76c7cd02011-05-29 19:57:121292operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161293{
Howard Hinnantf554add2011-09-14 18:33:511294 return __y < __x;
Howard Hinnant3e519522010-05-11 19:42:161295}
1296
1297template <class _Iter1, class _Iter2>
1298inline _LIBCPP_INLINE_VISIBILITY
1299bool
Howard Hinnant76c7cd02011-05-29 19:57:121300operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161301{
Howard Hinnantf554add2011-09-14 18:33:511302 return !(__x < __y);
Howard Hinnant3e519522010-05-11 19:42:161303}
1304
1305template <class _Iter1, class _Iter2>
1306inline _LIBCPP_INLINE_VISIBILITY
1307bool
Howard Hinnant76c7cd02011-05-29 19:57:121308operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161309{
Howard Hinnantf554add2011-09-14 18:33:511310 return !(__y < __x);
Howard Hinnant3e519522010-05-11 19:42:161311}
1312
Howard Hinnant6e551ae2012-10-02 19:45:421313template <class _Iter1>
1314inline _LIBCPP_INLINE_VISIBILITY
1315bool
1316operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
1317{
1318 return !(__x == __y);
1319}
1320
1321template <class _Iter1>
1322inline _LIBCPP_INLINE_VISIBILITY
1323bool
1324operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
1325{
1326 return __y < __x;
1327}
1328
1329template <class _Iter1>
1330inline _LIBCPP_INLINE_VISIBILITY
1331bool
1332operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
1333{
1334 return !(__x < __y);
1335}
1336
1337template <class _Iter1>
1338inline _LIBCPP_INLINE_VISIBILITY
1339bool
1340operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
1341{
1342 return !(__y < __x);
1343}
1344
Howard Hinnant3e519522010-05-11 19:42:161345template <class _Iter1, class _Iter2>
1346inline _LIBCPP_INLINE_VISIBILITY
1347typename __wrap_iter<_Iter1>::difference_type
Howard Hinnant76c7cd02011-05-29 19:57:121348operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161349{
Howard Hinnantcec9af92011-09-16 17:29:171350#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantf554add2011-09-14 18:33:511351 _LIBCPP_ASSERT(__get_const_db()->__comparable(&__x, &__y),
1352 "Attempted to subtract incompatible iterators");
Howard Hinnantcec9af92011-09-16 17:29:171353#endif
Howard Hinnant3e519522010-05-11 19:42:161354 return __x.base() - __y.base();
1355}
1356
1357template <class _Iter>
1358inline _LIBCPP_INLINE_VISIBILITY
1359__wrap_iter<_Iter>
1360operator+(typename __wrap_iter<_Iter>::difference_type __n,
Howard Hinnantf554add2011-09-14 18:33:511361 __wrap_iter<_Iter> __x) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:161362{
Howard Hinnantf554add2011-09-14 18:33:511363 __x += __n;
1364 return __x;
Howard Hinnant3e519522010-05-11 19:42:161365}
1366
1367#ifdef _LIBCPP_DEBUG
1368
1369// __debug_iter
1370
1371template <class _Container, class _Iter> class __debug_iter;
1372
1373template <class _Container, class _Iter1, class _Iter2>
Howard Hinnantaeb85682012-09-14 00:39:161374_LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161375bool
1376operator==(const __debug_iter<_Container, _Iter1>&, const __debug_iter<_Container, _Iter2>&);
1377
1378template <class _Container, class _Iter1, class _Iter2>
Howard Hinnantaeb85682012-09-14 00:39:161379_LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161380bool
1381operator<(const __debug_iter<_Container, _Iter1>&, const __debug_iter<_Container, _Iter2>&);
1382
1383template <class _Container, class _Iter1, class _Iter2>
Howard Hinnantaeb85682012-09-14 00:39:161384_LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161385bool
1386operator!=(const __debug_iter<_Container, _Iter1>&, const __debug_iter<_Container, _Iter2>&);
1387
1388template <class _Container, class _Iter1, class _Iter2>
Howard Hinnantaeb85682012-09-14 00:39:161389_LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161390bool
1391operator>(const __debug_iter<_Container, _Iter1>&, const __debug_iter<_Container, _Iter2>&);
1392
1393template <class _Container, class _Iter1, class _Iter2>
Howard Hinnantaeb85682012-09-14 00:39:161394_LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161395bool
1396operator>=(const __debug_iter<_Container, _Iter1>&, const __debug_iter<_Container, _Iter2>&);
1397
1398template <class _Container, class _Iter1, class _Iter2>
Howard Hinnantaeb85682012-09-14 00:39:161399_LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161400bool
1401operator<=(const __debug_iter<_Container, _Iter1>&, const __debug_iter<_Container, _Iter2>&);
1402
1403template <class _Container, class _Iter1, class _Iter2>
Howard Hinnantaeb85682012-09-14 00:39:161404_LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161405typename __debug_iter<_Container, _Iter1>::difference_type
1406operator-(const __debug_iter<_Container, _Iter1>&, const __debug_iter<_Container, _Iter2>&);
1407
1408template <class _Container, class _Iter>
Howard Hinnantaeb85682012-09-14 00:39:161409_LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161410__debug_iter<_Container, _Iter>
1411operator+(typename __debug_iter<_Container, _Iter>::difference_type, const __debug_iter<_Container, _Iter>&);
1412
1413template <class _Container, class _Iter>
1414class __debug_iter
1415{
1416public:
1417 typedef _Iter iterator_type;
1418 typedef _Container __container_type;
1419 typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
1420 typedef typename iterator_traits<iterator_type>::value_type value_type;
1421 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
1422 typedef typename iterator_traits<iterator_type>::pointer pointer;
1423 typedef typename iterator_traits<iterator_type>::reference reference;
1424private:
1425 iterator_type __i;
1426 __debug_iter* __next;
1427 __container_type* __cont;
Howard Hinnantb3371f62010-08-22 00:02:431428
Howard Hinnant3e519522010-05-11 19:42:161429public:
1430 _LIBCPP_INLINE_VISIBILITY __debug_iter() : __next(0), __cont(0) {}
1431 _LIBCPP_INLINE_VISIBILITY __debug_iter(const __debug_iter& __x)
1432 : __i(__x.base()), __next(0), __cont(0) {__set_owner(__x.__cont);}
1433 __debug_iter& operator=(const __debug_iter& __x);
1434 template <class _Up> _LIBCPP_INLINE_VISIBILITY __debug_iter(const __debug_iter<_Container, _Up>& __u,
1435 typename enable_if<is_convertible<_Up, iterator_type>::value>::type* = 0)
1436 : __i(__u.base()), __next(0), __cont(0) {__set_owner(__u.__cont);}
1437 _LIBCPP_INLINE_VISIBILITY ~__debug_iter() {__remove_owner();}
1438 _LIBCPP_INLINE_VISIBILITY reference operator*() const {assert(__is_deref()); return *__i;}
1439 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return &(operator*());}
1440 _LIBCPP_INLINE_VISIBILITY __debug_iter& operator++() {assert(__can_increment()); ++__i; return *this;}
1441 _LIBCPP_INLINE_VISIBILITY __debug_iter operator++(int)
1442 {__debug_iter __tmp(*this); operator++(); return __tmp;}
1443 _LIBCPP_INLINE_VISIBILITY __debug_iter& operator--() {assert(__can_decrement()); --__i; return *this;}
1444 _LIBCPP_INLINE_VISIBILITY __debug_iter operator--(int)
1445 {__debug_iter __tmp(*this); operator--(); return __tmp;}
1446 _LIBCPP_INLINE_VISIBILITY __debug_iter operator+ (difference_type __n) const
1447 {__debug_iter __t(*this); __t += __n; return __t;}
1448 __debug_iter& operator+=(difference_type __n);
1449 _LIBCPP_INLINE_VISIBILITY __debug_iter operator- (difference_type __n) const
1450 {__debug_iter __t(*this); __t -= __n; return __t;}
1451 _LIBCPP_INLINE_VISIBILITY __debug_iter& operator-=(difference_type __n)
1452 {*this += -__n; return *this;}
1453 _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const
1454 {return *(*this + __n);}
1455
1456private:
1457 _LIBCPP_INLINE_VISIBILITY __debug_iter(const __container_type* __c, iterator_type __x)
1458 : __i(__x), __next(0), __cont(0) {__set_owner(__c);}
1459 _LIBCPP_INLINE_VISIBILITY iterator_type base() const {return __i;}
1460
1461 void __set_owner(const __container_type* __c);
1462 void __remove_owner();
1463 static void __remove_all(__container_type* __c);
1464 static void swap(__container_type* __x, __container_type* __y);
Howard Hinnantb3371f62010-08-22 00:02:431465
Howard Hinnant3e519522010-05-11 19:42:161466 _LIBCPP_INLINE_VISIBILITY bool __is_deref() const
1467 {return __is_deref(__is_random_access_iterator<iterator_type>());}
1468 bool __is_deref(false_type) const;
1469 bool __is_deref(true_type) const;
1470 _LIBCPP_INLINE_VISIBILITY bool __can_decrement() const
1471 {return __can_decrement(integral_constant<int, is_pointer<iterator_type>::value ? 2:
1472 __is_random_access_iterator<iterator_type>::value ? 1 : 0>());}
1473 bool __can_decrement(integral_constant<int, 0>) const;
1474 bool __can_decrement(integral_constant<int, 1>) const;
1475 bool __can_decrement(integral_constant<int, 2>) const;
1476 _LIBCPP_INLINE_VISIBILITY bool __can_increment() const
1477 {return __can_increment(integral_constant<int, is_pointer<iterator_type>::value ? 2:
1478 __is_random_access_iterator<iterator_type>::value ? 1 : 0>());}
1479 bool __can_increment(integral_constant<int, 0>) const;
1480 bool __can_increment(integral_constant<int, 1>) const;
1481 bool __can_increment(integral_constant<int, 2>) const;
1482
1483 _LIBCPP_INLINE_VISIBILITY bool __can_add(difference_type __n) const
1484 {return __can_add(__n, is_pointer<iterator_type>());}
1485 bool __can_add(difference_type __n, false_type) const;
1486 bool __can_add(difference_type __n, true_type) const;
1487
1488 template <class _Cp, class _Up> friend class __debug_iter;
1489 friend class _Container::__self;
1490
1491 template <class _Cp, class _Iter1, class _Iter2>
1492 friend
1493 bool
1494 operator==(const __debug_iter<_Cp, _Iter1>&, const __debug_iter<_Cp, _Iter2>&);
Howard Hinnantb3371f62010-08-22 00:02:431495
Howard Hinnant3e519522010-05-11 19:42:161496 template <class _Cp, class _Iter1, class _Iter2>
1497 friend
1498 bool
1499 operator<(const __debug_iter<_Cp, _Iter1>&, const __debug_iter<_Cp, _Iter2>&);
Howard Hinnantb3371f62010-08-22 00:02:431500
Howard Hinnant3e519522010-05-11 19:42:161501 template <class _Cp, class _Iter1, class _Iter2>
1502 friend
1503 bool
1504 operator!=(const __debug_iter<_Cp, _Iter1>&, const __debug_iter<_Cp, _Iter2>&);
Howard Hinnantb3371f62010-08-22 00:02:431505
Howard Hinnant3e519522010-05-11 19:42:161506 template <class _Cp, class _Iter1, class _Iter2>
1507 friend
1508 bool
1509 operator>(const __debug_iter<_Cp, _Iter1>&, const __debug_iter<_Cp, _Iter2>&);
Howard Hinnantb3371f62010-08-22 00:02:431510
Howard Hinnant3e519522010-05-11 19:42:161511 template <class _Cp, class _Iter1, class _Iter2>
1512 friend
1513 bool
1514 operator>=(const __debug_iter<_Cp, _Iter1>&, const __debug_iter<_Cp, _Iter2>&);
Howard Hinnantb3371f62010-08-22 00:02:431515
Howard Hinnant3e519522010-05-11 19:42:161516 template <class _Cp, class _Iter1, class _Iter2>
1517 friend
1518 bool
1519 operator<=(const __debug_iter<_Cp, _Iter1>&, const __debug_iter<_Cp, _Iter2>&);
Howard Hinnantb3371f62010-08-22 00:02:431520
Howard Hinnant3e519522010-05-11 19:42:161521 template <class _Cp, class _Iter1, class _Iter2>
1522 friend
1523 typename __debug_iter<_Cp, _Iter1>::difference_type
1524 operator-(const __debug_iter<_Cp, _Iter1>&, const __debug_iter<_Cp, _Iter2>&);
Howard Hinnantb3371f62010-08-22 00:02:431525
Howard Hinnant3e519522010-05-11 19:42:161526 template <class _Cp, class _Iter1>
1527 friend
1528 __debug_iter<_Cp, _Iter1>
1529 operator+(typename __debug_iter<_Cp, _Iter1>::difference_type, const __debug_iter<_Cp, _Iter1>&);
1530};
1531
1532template <class _Container, class _Iter>
1533__debug_iter<_Container, _Iter>&
1534__debug_iter<_Container, _Iter>::operator=(const __debug_iter& __x)
1535{
1536 if (this != &__x)
1537 {
1538 __remove_owner();
1539 __i = __x.__i;
1540 __set_owner(__x.__cont);
1541 }
1542 return *this;
1543}
Howard Hinnantb3371f62010-08-22 00:02:431544
Howard Hinnant3e519522010-05-11 19:42:161545template <class _Container, class _Iter>
1546void
1547__debug_iter<_Container, _Iter>::__set_owner(const __container_type* __c)
1548{
1549 __cont = const_cast<__container_type*>(__c);
1550 __debug_iter*& __head = __cont->__get_iterator_list(this);
1551 __next = __head;
1552 __head = this;
1553}
1554
1555template <class _Container, class _Iter>
1556void
1557__debug_iter<_Container, _Iter>::__remove_owner()
1558{
1559 if (__cont)
1560 {
1561 __debug_iter*& __head = __cont->__get_iterator_list(this);
1562 if (__head == this)
1563 __head = __next;
1564 else
1565 {
1566 __debug_iter* __prev = __head;
1567 for (__debug_iter* __p = __head->__next; __p != this; __p = __p->__next)
1568 __prev = __p;
1569 __prev->__next = __next;
1570 }
1571 __cont = 0;
1572 }
1573}
1574
1575template <class _Container, class _Iter>
1576void
1577__debug_iter<_Container, _Iter>::__remove_all(__container_type* __c)
1578{
1579 __debug_iter*& __head = __c->__get_iterator_list((__debug_iter*)0);
1580 __debug_iter* __p = __head;
1581 __head = 0;
1582 while (__p)
1583 {
1584 __p->__cont = 0;
1585 __debug_iter* __n = __p->__next;
1586 __p->__next = 0;
1587 __p = __n;
1588 }
1589}
1590
1591template <class _Container, class _Iter>
1592void
1593__debug_iter<_Container, _Iter>::swap(__container_type* __x, __container_type* __y)
1594{
1595 __debug_iter*& __head_x = __x->__get_iterator_list((__debug_iter*)0);
1596 __debug_iter*& __head_y = __y->__get_iterator_list((__debug_iter*)0);
1597 __debug_iter* __p = __head_x;
1598 __head_x = __head_y;
1599 __head_y = __p;
1600 for (__p = __head_x; __p; __p = __p->__next)
1601 __p->__cont = __x;
1602 for (__p = __head_y; __p; __p = __p->__next)
1603 __p->__cont = __y;
1604}
1605
1606template <class _Container, class _Iter>
1607bool
1608__debug_iter<_Container, _Iter>::__is_deref(false_type) const
1609{
1610 if (__cont == 0)
1611 return false;
1612 return __i != __cont->end().base();
1613}
1614
1615template <class _Container, class _Iter>
1616bool
1617__debug_iter<_Container, _Iter>::__is_deref(true_type) const
1618{
1619 if (__cont == 0)
1620 return false;
1621 return __i < __cont->end().base();
1622}
1623
1624template <class _Container, class _Iter>
1625bool
1626__debug_iter<_Container, _Iter>::__can_decrement(integral_constant<int, 0>) const
1627{
1628 if (__cont == 0)
1629 return false;
1630 return __i != __cont->begin().base();
1631}
1632
1633template <class _Container, class _Iter>
1634bool
1635__debug_iter<_Container, _Iter>::__can_decrement(integral_constant<int, 1>) const
1636{
1637 if (__cont == 0)
1638 return false;
1639 iterator_type __b = __cont->begin().base();
1640 return __b < __i && __i <= __b + __cont->size();
1641}
1642
1643template <class _Container, class _Iter>
1644bool
1645__debug_iter<_Container, _Iter>::__can_decrement(integral_constant<int, 2>) const
1646{
1647 if (__cont == 0)
1648 return false;
1649 iterator_type __b = __cont->begin().base();
1650 return __b < __i && __i <= __b + __cont->size();
1651}
1652
1653template <class _Container, class _Iter>
1654bool
1655__debug_iter<_Container, _Iter>::__can_increment(integral_constant<int, 0>) const
1656{
1657 if (__cont == 0)
1658 return false;
1659 return __i != __cont->end().base();
1660}
1661
1662template <class _Container, class _Iter>
1663bool
1664__debug_iter<_Container, _Iter>::__can_increment(integral_constant<int, 1>) const
1665{
1666 if (__cont == 0)
1667 return false;
1668 iterator_type __b = __cont->begin().base();
1669 return __b <= __i && __i < __b + __cont->size();
1670}
1671
1672template <class _Container, class _Iter>
1673bool
1674__debug_iter<_Container, _Iter>::__can_increment(integral_constant<int, 2>) const
1675{
1676 if (__cont == 0)
1677 return false;
1678 iterator_type __b = __cont->begin().base();
1679 return __b <= __i && __i < __b + __cont->size();
1680}
1681
1682template <class _Container, class _Iter>
1683bool
1684__debug_iter<_Container, _Iter>::__can_add(difference_type __n, false_type) const
1685{
1686 if (__cont == 0)
1687 return false;
1688 iterator_type __b = __cont->begin().base();
1689 iterator_type __j = __i + __n;
1690 return __b <= __j && __j <= __b + __cont->size();
1691}
1692
1693template <class _Container, class _Iter>
1694bool
1695__debug_iter<_Container, _Iter>::__can_add(difference_type __n, true_type) const
1696{
1697 if (__cont == 0)
1698 return false;
1699 iterator_type __b = __cont->begin().base();
1700 iterator_type __j = __i + __n;
1701 return __b <= __j && __j <= __b + __cont->size();
1702}
1703
1704template <class _Container, class _Iter>
1705__debug_iter<_Container, _Iter>&
1706__debug_iter<_Container, _Iter>::operator+=(difference_type __n)
1707{
1708 assert(__can_add(__n));
1709 __i += __n;
1710 return *this;
1711}
1712
1713template <class _Container, class _Iter1, class _Iter2>
1714inline _LIBCPP_INLINE_VISIBILITY
1715bool
1716operator==(const __debug_iter<_Container, _Iter1>& __x, const __debug_iter<_Container, _Iter2>& __y)
1717{
1718 assert(__x.__cont && __x.__cont == __y.__cont);
1719 return __x.base() == __y.base();
1720}
1721
1722template <class _Container, class _Iter1, class _Iter2>
1723inline _LIBCPP_INLINE_VISIBILITY
1724bool
1725operator!=(const __debug_iter<_Container, _Iter1>& __x, const __debug_iter<_Container, _Iter2>& __y)
1726{
1727 return !(__x == __y);
1728}
1729
1730template <class _Container, class _Iter1, class _Iter2>
1731inline _LIBCPP_INLINE_VISIBILITY
1732bool
1733operator<(const __debug_iter<_Container, _Iter1>& __x, const __debug_iter<_Container, _Iter2>& __y)
1734{
1735 assert(__x.__cont && __x.__cont == __y.__cont);
1736 return __x.base() < __y.base();
1737}
1738
1739template <class _Container, class _Iter1, class _Iter2>
1740inline _LIBCPP_INLINE_VISIBILITY
1741bool
1742operator>(const __debug_iter<_Container, _Iter1>& __x, const __debug_iter<_Container, _Iter2>& __y)
1743{
1744 return __y < __x;
1745}
1746
1747template <class _Container, class _Iter1, class _Iter2>
1748inline _LIBCPP_INLINE_VISIBILITY
1749bool
1750operator>=(const __debug_iter<_Container, _Iter1>& __x, const __debug_iter<_Container, _Iter2>& __y)
1751{
1752 return !(__x < __y);
1753}
1754
1755template <class _Container, class _Iter1, class _Iter2>
1756inline _LIBCPP_INLINE_VISIBILITY
1757bool
1758operator<=(const __debug_iter<_Container, _Iter1>& __x, const __debug_iter<_Container, _Iter2>& __y)
1759{
1760 return !(__y < __x);
1761}
1762
1763template <class _Container, class _Iter1, class _Iter2>
1764inline _LIBCPP_INLINE_VISIBILITY
1765typename __debug_iter<_Container, _Iter1>::difference_type
1766operator-(const __debug_iter<_Container, _Iter1>& __x, const __debug_iter<_Container, _Iter2>& __y)
1767{
1768 assert(__x.__cont && __x.__cont == __y.__cont);
1769 return __x.base() - __y.base();
1770}
1771
1772template <class _Container, class _Iter>
1773inline _LIBCPP_INLINE_VISIBILITY
1774__debug_iter<_Container, _Iter>
1775operator+(typename __debug_iter<_Container, _Iter>::difference_type __n,
1776 const __debug_iter<_Container, _Iter>& __x)
1777{
1778 return __x + __n;
1779}
1780
1781#endif // _LIBCPP_DEBUG
1782
Howard Hinnantc4931c42010-11-16 21:10:231783#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_TRAILING_RETURN)
Howard Hinnant3e519522010-05-11 19:42:161784
Howard Hinnantc003db12011-11-29 18:15:501785template <class _Cp>
Howard Hinnant848a5372010-09-22 16:48:341786inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161787auto
Howard Hinnantc003db12011-11-29 18:15:501788begin(_Cp& __c) -> decltype(__c.begin())
Howard Hinnant3e519522010-05-11 19:42:161789{
1790 return __c.begin();
1791}
1792
Howard Hinnantc003db12011-11-29 18:15:501793template <class _Cp>
Howard Hinnant848a5372010-09-22 16:48:341794inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161795auto
Howard Hinnantc003db12011-11-29 18:15:501796begin(const _Cp& __c) -> decltype(__c.begin())
Howard Hinnant3e519522010-05-11 19:42:161797{
1798 return __c.begin();
1799}
1800
Howard Hinnantc003db12011-11-29 18:15:501801template <class _Cp>
Howard Hinnant848a5372010-09-22 16:48:341802inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161803auto
Howard Hinnantc003db12011-11-29 18:15:501804end(_Cp& __c) -> decltype(__c.end())
Howard Hinnant3e519522010-05-11 19:42:161805{
1806 return __c.end();
1807}
1808
Howard Hinnantc003db12011-11-29 18:15:501809template <class _Cp>
Howard Hinnant848a5372010-09-22 16:48:341810inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:161811auto
Howard Hinnantc003db12011-11-29 18:15:501812end(const _Cp& __c) -> decltype(__c.end())
Howard Hinnant3e519522010-05-11 19:42:161813{
1814 return __c.end();
1815}
1816
Howard Hinnantc4931c42010-11-16 21:10:231817#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_TRAILING_RETURN)
Howard Hinnant3e519522010-05-11 19:42:161818
Howard Hinnantc003db12011-11-29 18:15:501819template <class _Cp>
Howard Hinnant848a5372010-09-22 16:48:341820inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc003db12011-11-29 18:15:501821typename _Cp::iterator
1822begin(_Cp& __c)
Howard Hinnant3e519522010-05-11 19:42:161823{
1824 return __c.begin();
1825}
1826
Howard Hinnantc003db12011-11-29 18:15:501827template <class _Cp>
Howard Hinnant848a5372010-09-22 16:48:341828inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc003db12011-11-29 18:15:501829typename _Cp::const_iterator
1830begin(const _Cp& __c)
Howard Hinnant3e519522010-05-11 19:42:161831{
1832 return __c.begin();
1833}
1834
Howard Hinnantc003db12011-11-29 18:15:501835template <class _Cp>
Howard Hinnant848a5372010-09-22 16:48:341836inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc003db12011-11-29 18:15:501837typename _Cp::iterator
1838end(_Cp& __c)
Howard Hinnant3e519522010-05-11 19:42:161839{
1840 return __c.end();
1841}
1842
Howard Hinnantc003db12011-11-29 18:15:501843template <class _Cp>
Howard Hinnant848a5372010-09-22 16:48:341844inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc003db12011-11-29 18:15:501845typename _Cp::const_iterator
1846end(const _Cp& __c)
Howard Hinnant3e519522010-05-11 19:42:161847{
1848 return __c.end();
1849}
1850
Howard Hinnantc4931c42010-11-16 21:10:231851#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_TRAILING_RETURN)
Howard Hinnant3e519522010-05-11 19:42:161852
Howard Hinnantc003db12011-11-29 18:15:501853template <class _Tp, size_t _Np>
Howard Hinnant848a5372010-09-22 16:48:341854inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc003db12011-11-29 18:15:501855_Tp*
1856begin(_Tp (&__array)[_Np])
Howard Hinnant3e519522010-05-11 19:42:161857{
1858 return __array;
1859}
1860
Howard Hinnantc003db12011-11-29 18:15:501861template <class _Tp, size_t _Np>
Howard Hinnant848a5372010-09-22 16:48:341862inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc003db12011-11-29 18:15:501863_Tp*
1864end(_Tp (&__array)[_Np])
Howard Hinnant3e519522010-05-11 19:42:161865{
Howard Hinnantc003db12011-11-29 18:15:501866 return __array + _Np;
Howard Hinnant3e519522010-05-11 19:42:161867}
1868
1869_LIBCPP_END_NAMESPACE_STD
1870
1871#endif // _LIBCPP_ITERATOR