blob: ed2d7a713b7ccee6b5384aeabc8f64d0436bc0b9 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- iterator ----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
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 Hinnant324bb032010-08-22 00:02:43 +000091
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000092 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 Hinnant45f57172010-09-14 20:26:27 +0000154 back_insert_iterator& operator=(const typename Container::value_type& value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000155 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 Hinnant45f57172010-09-14 20:26:27 +0000175 front_insert_iterator& operator=(const typename Container::value_type& value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000176 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 Hinnant45f57172010-09-14 20:26:27 +0000197 insert_iterator& operator=(const typename Container::value_type& value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000198 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
Howard Hinnant82894812010-09-22 16:48:34 +0000328struct _LIBCPP_VISIBLE input_iterator_tag {};
329struct _LIBCPP_VISIBLE output_iterator_tag {};
330struct _LIBCPP_VISIBLE forward_iterator_tag : public input_iterator_tag {};
331struct _LIBCPP_VISIBLE bidirectional_iterator_tag : public forward_iterator_tag {};
332struct _LIBCPP_VISIBLE random_access_iterator_tag : public bidirectional_iterator_tag {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000333
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>
Howard Hinnant82894812010-09-22 16:48:34 +0000375struct _LIBCPP_VISIBLE iterator_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000376 : __iterator_traits<_Iter, __has_iterator_category<_Iter>::value> {};
377
378template<class _Tp>
Howard Hinnant82894812010-09-22 16:48:34 +0000379struct _LIBCPP_VISIBLE iterator_traits<_Tp*>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000380{
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&>
Howard Hinnant82894812010-09-22 16:48:34 +0000410struct _LIBCPP_VISIBLE iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000411{
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>
Howard Hinnant82894812010-09-22 16:48:34 +0000485inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000486_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{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000491 _VSTD::advance(__x, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000492 return __x;
493}
494
495template <class _BidiretionalIter>
Howard Hinnant82894812010-09-22 16:48:34 +0000496inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000497_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{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000502 _VSTD::advance(__x, -__n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000503 return __x;
504}
505
506template <class _Iter>
Howard Hinnant82894812010-09-22 16:48:34 +0000507class _LIBCPP_VISIBLE reverse_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000508 : 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;
Howard Hinnant324bb032010-08-22 00:02:43 +0000523
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000524 _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>
Howard Hinnant82894812010-09-22 16:48:34 +0000614class _LIBCPP_VISIBLE back_insert_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000615 : 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) {}
Howard Hinnantd2a92512010-09-13 01:43:27 +0000627 _LIBCPP_INLINE_VISIBILITY back_insert_iterator& operator=(const typename _Container::value_type& __value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000628 {container->push_back(__value); return *this;}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000629#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000630 _LIBCPP_INLINE_VISIBILITY back_insert_iterator& operator=(typename _Container::value_type&& __value)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000631 {container->push_back(_VSTD::move(__value)); return *this;}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000632#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000633 _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>
Howard Hinnant82894812010-09-22 16:48:34 +0000647class _LIBCPP_VISIBLE front_insert_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000648 : 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) {}
Howard Hinnantd2a92512010-09-13 01:43:27 +0000660 _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator=(const typename _Container::value_type& __value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000661 {container->push_front(__value); return *this;}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000662#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000663 _LIBCPP_INLINE_VISIBILITY front_insert_iterator& operator=(typename _Container::value_type&& __value)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000664 {container->push_front(_VSTD::move(__value)); return *this;}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000665#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000666 _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>
Howard Hinnant82894812010-09-22 16:48:34 +0000680class _LIBCPP_VISIBLE insert_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000681 : 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) {}
Howard Hinnantd2a92512010-09-13 01:43:27 +0000695 _LIBCPP_INLINE_VISIBILITY insert_iterator& operator=(const typename _Container::value_type& __value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000696 {iter = container->insert(iter, __value); ++iter; return *this;}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000697#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000698 _LIBCPP_INLINE_VISIBILITY insert_iterator& operator=(typename _Container::value_type&& __value)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000699 {iter = container->insert(iter, _VSTD::move(__value)); ++iter; return *this;}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000700#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000701 _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>
Howard Hinnant82894812010-09-22 16:48:34 +0000716class _LIBCPP_VISIBLE istream_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000717 : 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> >
Howard Hinnant82894812010-09-22 16:48:34 +0000755class _LIBCPP_VISIBLE ostream_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000756 : 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>
Howard Hinnant82894812010-09-22 16:48:34 +0000784class _LIBCPP_VISIBLE istreambuf_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000785 : 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
Howard Hinnant82894812010-09-22 16:48:34 +0000809 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000810 void __test_for_eof()
811 {
812 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof()))
813 __sbuf_ = 0;
814 }
815public:
816 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator() throw() : __sbuf_(0) {}
817 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(istream_type& __s) throw()
818 : __sbuf_(__s.rdbuf()) {__test_for_eof();}
819 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(streambuf_type* __s) throw()
820 : __sbuf_(__s) {__test_for_eof();}
821 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(const __proxy& __p) throw()
822 : __sbuf_(__p.__sbuf_) {}
823
Howard Hinnant82894812010-09-22 16:48:34 +0000824 _LIBCPP_INLINE_VISIBILITY _CharT operator*() const {return __sbuf_->sgetc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000825 _LIBCPP_INLINE_VISIBILITY char_type* operator->() const {return nullptr;}
826 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator& operator++()
827 {
828 if (traits_type::eq_int_type(__sbuf_->snextc(), traits_type::eof()))
829 __sbuf_ = 0;
830 return *this;
831 }
832 _LIBCPP_INLINE_VISIBILITY __proxy operator++(int)
833 {
834 char_type __c = __sbuf_->sgetc();
835 ++(*this);
836 return __proxy(__c, __sbuf_);
837 }
838
839 _LIBCPP_INLINE_VISIBILITY bool equal(const istreambuf_iterator& __b) const
840 {return (__sbuf_ == 0) == (__b.__sbuf_ == 0);}
841};
842
843template <class _CharT, class _Traits>
844inline _LIBCPP_INLINE_VISIBILITY
845bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,
846 const istreambuf_iterator<_CharT,_Traits>& __b)
847 {return __a.equal(__b);}
848
849template <class _CharT, class _Traits>
850inline _LIBCPP_INLINE_VISIBILITY
851bool operator!=(const istreambuf_iterator<_CharT,_Traits>& __a,
852 const istreambuf_iterator<_CharT,_Traits>& __b)
853 {return !__a.equal(__b);}
854
855template <class _CharT, class _Traits>
Howard Hinnant82894812010-09-22 16:48:34 +0000856class _LIBCPP_VISIBLE ostreambuf_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000857 : public iterator<output_iterator_tag, void, void, void, void>
858{
859public:
860 typedef _CharT char_type;
861 typedef _Traits traits_type;
862 typedef basic_streambuf<_CharT,_Traits> streambuf_type;
863 typedef basic_ostream<_CharT,_Traits> ostream_type;
864private:
865 streambuf_type* __sbuf_;
866public:
867 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(ostream_type& __s) throw()
868 : __sbuf_(__s.rdbuf()) {}
869 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(streambuf_type* __s) throw()
870 : __sbuf_(__s) {}
871 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator=(_CharT __c)
872 {
873 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sputc(__c), traits_type::eof()))
874 __sbuf_ = 0;
875 return *this;
876 }
877 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator*() {return *this;}
878 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++() {return *this;}
879 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++(int) {return *this;}
880 _LIBCPP_INLINE_VISIBILITY bool failed() const throw() {return __sbuf_ == 0;}
881};
882
883template <class _Iter>
Howard Hinnant82894812010-09-22 16:48:34 +0000884class _LIBCPP_VISIBLE move_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000885{
886private:
887 _Iter __i;
888public:
889 typedef _Iter iterator_type;
890 typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
891 typedef typename iterator_traits<iterator_type>::value_type value_type;
892 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
893 typedef typename iterator_traits<iterator_type>::pointer pointer;
Howard Hinnant73d21a42010-09-04 23:28:19 +0000894#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000895 typedef value_type&& reference;
896#else
897 typedef typename iterator_traits<iterator_type>::reference reference;
898#endif
Howard Hinnant324bb032010-08-22 00:02:43 +0000899
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000900 _LIBCPP_INLINE_VISIBILITY move_iterator() : __i() {}
901 _LIBCPP_INLINE_VISIBILITY explicit move_iterator(_Iter __x) : __i(__x) {}
902 template <class _Up> _LIBCPP_INLINE_VISIBILITY move_iterator(const move_iterator<_Up>& __u)
903 : __i(__u.base()) {}
904 _LIBCPP_INLINE_VISIBILITY _Iter base() const {return __i;}
Douglas Gregoraab015a2011-01-26 00:12:48 +0000905 _LIBCPP_INLINE_VISIBILITY reference operator*() const {
906 return static_cast<reference>(*__i);
907 }
908 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {
909 typename iterator_traits<iterator_type>::reference __ref = *__i;
910 return &__ref;
911 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000912 _LIBCPP_INLINE_VISIBILITY move_iterator& operator++() {++__i; return *this;}
913 _LIBCPP_INLINE_VISIBILITY move_iterator operator++(int)
914 {move_iterator __tmp(*this); ++__i; return __tmp;}
915 _LIBCPP_INLINE_VISIBILITY move_iterator& operator--() {--__i; return *this;}
916 _LIBCPP_INLINE_VISIBILITY move_iterator operator--(int)
917 {move_iterator __tmp(*this); --__i; return __tmp;}
918 _LIBCPP_INLINE_VISIBILITY move_iterator operator+ (difference_type __n) const
919 {return move_iterator(__i + __n);}
920 _LIBCPP_INLINE_VISIBILITY move_iterator& operator+=(difference_type __n)
921 {__i += __n; return *this;}
922 _LIBCPP_INLINE_VISIBILITY move_iterator operator- (difference_type __n) const
923 {return move_iterator(__i - __n);}
924 _LIBCPP_INLINE_VISIBILITY move_iterator& operator-=(difference_type __n)
925 {__i -= __n; return *this;}
926 _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const
Douglas Gregoraab015a2011-01-26 00:12:48 +0000927 {
928 return static_cast<reference>(__i[__n]);
929 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000930};
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
974bool
975operator<=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
976{
977 return __x.base() <= __y.base();
978}
979
980template <class _Iter1, class _Iter2>
981inline _LIBCPP_INLINE_VISIBILITY
982typename move_iterator<_Iter1>::difference_type
983operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
984{
985 return __x.base() - __y.base();
986}
987
988template <class _Iter>
989inline _LIBCPP_INLINE_VISIBILITY
990move_iterator<_Iter>
991operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
992{
993 return move_iterator<_Iter>(__x.base() + __n);
994}
995
996template <class _Iter>
997inline _LIBCPP_INLINE_VISIBILITY
998move_iterator<_Iter>
999make_move_iterator(const _Iter& __i)
1000{
1001 return move_iterator<_Iter>(__i);
1002}
1003
1004// __wrap_iter
1005
1006template <class _Iter> class __wrap_iter;
1007
1008template <class _Iter1, class _Iter2>
1009bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00001010operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001011
1012template <class _Iter1, class _Iter2>
1013bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00001014operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001015
1016template <class _Iter1, class _Iter2>
1017bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00001018operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001019
1020template <class _Iter1, class _Iter2>
1021bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00001022operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001023
1024template <class _Iter1, class _Iter2>
1025bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00001026operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001027
1028template <class _Iter1, class _Iter2>
1029bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00001030operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001031
1032template <class _Iter1, class _Iter2>
1033typename __wrap_iter<_Iter1>::difference_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00001034operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001035
1036template <class _Iter>
1037__wrap_iter<_Iter>
Howard Hinnanta6119a82011-05-29 19:57:12 +00001038operator+(typename __wrap_iter<_Iter>::difference_type, const __wrap_iter<_Iter>&) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001039
1040template <class _I, class _O> _O copy(_I, _I, _O);
1041template <class _B1, class _B2> _B2 copy_backward(_B1, _B1, _B2);
1042template <class _I, class _O> _O move(_I, _I, _O);
1043template <class _B1, class _B2> _B2 move_backward(_B1, _B1, _B2);
1044
1045template <class _Tp>
1046typename enable_if
1047<
Howard Hinnant1468b662010-11-19 22:17:28 +00001048 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001049 _Tp*
1050>::type
1051__unwrap_iter(__wrap_iter<_Tp*>);
1052
1053template <class _Iter>
1054class __wrap_iter
1055{
1056public:
1057 typedef _Iter iterator_type;
1058 typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
1059 typedef typename iterator_traits<iterator_type>::value_type value_type;
1060 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
1061 typedef typename iterator_traits<iterator_type>::pointer pointer;
1062 typedef typename iterator_traits<iterator_type>::reference reference;
1063private:
1064 iterator_type __i;
1065public:
Howard Hinnanta6119a82011-05-29 19:57:12 +00001066 _LIBCPP_INLINE_VISIBILITY __wrap_iter() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001067 template <class _Up> _LIBCPP_INLINE_VISIBILITY __wrap_iter(const __wrap_iter<_Up>& __u,
Howard Hinnanta6119a82011-05-29 19:57:12 +00001068 typename enable_if<is_convertible<_Up, iterator_type>::value>::type* = 0) _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +00001069 : __i(__u.base())
1070 {
1071#ifdef _LIBCPP_DEBUG2
1072 __get_db()->__iterator_copy(this, &__u);
1073#endif
1074 }
1075#ifdef _LIBCPP_DEBUG2
1076 _LIBCPP_INLINE_VISIBILITY
1077 __wrap_iter(const __wrap_iter& __x)
1078 : __i(__x.base())
1079 {
1080 __get_db()->__iterator_copy(this, &__x);
1081 }
1082 _LIBCPP_INLINE_VISIBILITY
1083 __wrap_iter& operator=(const __wrap_iter& __x)
1084 {
1085 if (this != &__x)
1086 {
1087 __get_db()->__iterator_copy(this, &__x);
1088 __i = __x.__i;
1089 }
1090 return *this;
1091 }
1092 _LIBCPP_INLINE_VISIBILITY
1093 ~__wrap_iter()
1094 {
1095 __get_db()->__erase_i(this);
1096 }
1097#endif
1098 _LIBCPP_INLINE_VISIBILITY reference operator*() const _NOEXCEPT
1099 {
1100 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1101 "Attempted to dereference a non-dereferenceable iterator");
1102 return *__i;
1103 }
Howard Hinnanta6119a82011-05-29 19:57:12 +00001104 _LIBCPP_INLINE_VISIBILITY pointer operator->() const _NOEXCEPT {return &(operator*());}
Howard Hinnant7a563db2011-09-14 18:33:51 +00001105 _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator++() _NOEXCEPT
1106 {
1107 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1108 "Attempted to increment non-incrementable iterator");
1109 ++__i;
1110 return *this;
1111 }
Howard Hinnanta6119a82011-05-29 19:57:12 +00001112 _LIBCPP_INLINE_VISIBILITY __wrap_iter operator++(int) _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +00001113 {__wrap_iter __tmp(*this); ++(*this); return __tmp;}
1114 _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator--() _NOEXCEPT
1115 {
1116 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
1117 "Attempted to decrement non-decrementable iterator");
1118 --__i;
1119 return *this;
1120 }
Howard Hinnanta6119a82011-05-29 19:57:12 +00001121 _LIBCPP_INLINE_VISIBILITY __wrap_iter operator--(int) _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +00001122 {__wrap_iter __tmp(*this); --(*this); return __tmp;}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001123 _LIBCPP_INLINE_VISIBILITY __wrap_iter operator+ (difference_type __n) const _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +00001124 {__wrap_iter __w(*this); __w += __n; return __w;}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001125 _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator+=(difference_type __n) _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +00001126 {
1127 _LIBCPP_ASSERT(__get_const_db()->__addable(this, __n),
1128 "Attempted to add/subtract iterator outside of valid range");
1129 __i += __n;
1130 return *this;
1131 }
Howard Hinnanta6119a82011-05-29 19:57:12 +00001132 _LIBCPP_INLINE_VISIBILITY __wrap_iter operator- (difference_type __n) const _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +00001133 {return *this + (-__n);}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001134 _LIBCPP_INLINE_VISIBILITY __wrap_iter& operator-=(difference_type __n) _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +00001135 {*this += -__n; return *this;}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001136 _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const _NOEXCEPT
Howard Hinnant7a563db2011-09-14 18:33:51 +00001137 {
1138 _LIBCPP_ASSERT(__get_const_db()->__subscriptable(this, __n),
1139 "Attempted to subscript iterator outside of valid range");
1140 return __i[__n];
1141 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001142
Howard Hinnanta6119a82011-05-29 19:57:12 +00001143 _LIBCPP_INLINE_VISIBILITY iterator_type base() const _NOEXCEPT {return __i;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001144
1145private:
Howard Hinnanta6119a82011-05-29 19:57:12 +00001146 _LIBCPP_INLINE_VISIBILITY __wrap_iter(iterator_type __x) _NOEXCEPT : __i(__x) {}
Howard Hinnant7a563db2011-09-14 18:33:51 +00001147#ifdef _LIBCPP_DEBUG2
1148 _LIBCPP_INLINE_VISIBILITY __wrap_iter(const void* __p, iterator_type __x) : __i(__x)
1149 {
1150 __get_db()->__insert_ic(this, __p);
1151 }
1152#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001153
1154 template <class _Up> friend class __wrap_iter;
1155 template <class _CharT, class _Traits, class _Alloc> friend class basic_string;
1156 template <class _Tp, class _Alloc> friend class vector;
1157
1158 template <class _Iter1, class _Iter2>
1159 friend
1160 bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00001161 operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00001162
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001163 template <class _Iter1, class _Iter2>
1164 friend
1165 bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00001166 operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00001167
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001168 template <class _Iter1, class _Iter2>
1169 friend
1170 bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00001171 operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00001172
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001173 template <class _Iter1, class _Iter2>
1174 friend
1175 bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00001176 operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00001177
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001178 template <class _Iter1, class _Iter2>
1179 friend
1180 bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00001181 operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00001182
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001183 template <class _Iter1, class _Iter2>
1184 friend
1185 bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00001186 operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00001187
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001188 template <class _Iter1, class _Iter2>
1189 friend
1190 typename __wrap_iter<_Iter1>::difference_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00001191 operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant324bb032010-08-22 00:02:43 +00001192
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001193 template <class _Iter1>
1194 friend
1195 __wrap_iter<_Iter1>
Howard Hinnanta6119a82011-05-29 19:57:12 +00001196 operator+(typename __wrap_iter<_Iter1>::difference_type, const __wrap_iter<_Iter1>&) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001197
1198 template <class _I, class _O> friend _O copy(_I, _I, _O);
1199 template <class _B1, class _B2> friend _B2 copy_backward(_B1, _B1, _B2);
1200 template <class _I, class _O> friend _O move(_I, _I, _O);
1201 template <class _B1, class _B2> friend _B2 move_backward(_B1, _B1, _B2);
1202
1203 template <class _Tp>
1204 friend
1205 typename enable_if
1206 <
Howard Hinnant1468b662010-11-19 22:17:28 +00001207 is_trivially_copy_assignable<_Tp>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001208 _Tp*
1209 >::type
1210 __unwrap_iter(__wrap_iter<_Tp*>);
1211};
1212
1213template <class _Iter1, class _Iter2>
1214inline _LIBCPP_INLINE_VISIBILITY
1215bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00001216operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001217{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001218 _LIBCPP_ASSERT(__get_const_db()->__comparable(&__x, &__y),
1219 "Attempted to compare incomparable iterators");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001220 return __x.base() == __y.base();
1221}
1222
1223template <class _Iter1, class _Iter2>
1224inline _LIBCPP_INLINE_VISIBILITY
1225bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00001226operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001227{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001228 _LIBCPP_ASSERT(__get_const_db()->__comparable(&__x, &__y),
1229 "Attempted to compare incomparable iterators");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001230 return __x.base() < __y.base();
1231}
1232
1233template <class _Iter1, class _Iter2>
1234inline _LIBCPP_INLINE_VISIBILITY
1235bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00001236operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001237{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001238 return !(__x == __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001239}
1240
1241template <class _Iter1, class _Iter2>
1242inline _LIBCPP_INLINE_VISIBILITY
1243bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00001244operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001245{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001246 return __y < __x;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001247}
1248
1249template <class _Iter1, class _Iter2>
1250inline _LIBCPP_INLINE_VISIBILITY
1251bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00001252operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001253{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001254 return !(__x < __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001255}
1256
1257template <class _Iter1, class _Iter2>
1258inline _LIBCPP_INLINE_VISIBILITY
1259bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00001260operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001261{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001262 return !(__y < __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001263}
1264
1265template <class _Iter1, class _Iter2>
1266inline _LIBCPP_INLINE_VISIBILITY
1267typename __wrap_iter<_Iter1>::difference_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00001268operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001269{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001270 _LIBCPP_ASSERT(__get_const_db()->__comparable(&__x, &__y),
1271 "Attempted to subtract incompatible iterators");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001272 return __x.base() - __y.base();
1273}
1274
1275template <class _Iter>
1276inline _LIBCPP_INLINE_VISIBILITY
1277__wrap_iter<_Iter>
1278operator+(typename __wrap_iter<_Iter>::difference_type __n,
Howard Hinnant7a563db2011-09-14 18:33:51 +00001279 __wrap_iter<_Iter> __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001280{
Howard Hinnant7a563db2011-09-14 18:33:51 +00001281 __x += __n;
1282 return __x;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001283}
1284
1285#ifdef _LIBCPP_DEBUG
1286
1287// __debug_iter
1288
1289template <class _Container, class _Iter> class __debug_iter;
1290
1291template <class _Container, class _Iter1, class _Iter2>
1292bool
1293operator==(const __debug_iter<_Container, _Iter1>&, const __debug_iter<_Container, _Iter2>&);
1294
1295template <class _Container, class _Iter1, class _Iter2>
1296bool
1297operator<(const __debug_iter<_Container, _Iter1>&, const __debug_iter<_Container, _Iter2>&);
1298
1299template <class _Container, class _Iter1, class _Iter2>
1300bool
1301operator!=(const __debug_iter<_Container, _Iter1>&, const __debug_iter<_Container, _Iter2>&);
1302
1303template <class _Container, class _Iter1, class _Iter2>
1304bool
1305operator>(const __debug_iter<_Container, _Iter1>&, const __debug_iter<_Container, _Iter2>&);
1306
1307template <class _Container, class _Iter1, class _Iter2>
1308bool
1309operator>=(const __debug_iter<_Container, _Iter1>&, const __debug_iter<_Container, _Iter2>&);
1310
1311template <class _Container, class _Iter1, class _Iter2>
1312bool
1313operator<=(const __debug_iter<_Container, _Iter1>&, const __debug_iter<_Container, _Iter2>&);
1314
1315template <class _Container, class _Iter1, class _Iter2>
1316typename __debug_iter<_Container, _Iter1>::difference_type
1317operator-(const __debug_iter<_Container, _Iter1>&, const __debug_iter<_Container, _Iter2>&);
1318
1319template <class _Container, class _Iter>
1320__debug_iter<_Container, _Iter>
1321operator+(typename __debug_iter<_Container, _Iter>::difference_type, const __debug_iter<_Container, _Iter>&);
1322
1323template <class _Container, class _Iter>
1324class __debug_iter
1325{
1326public:
1327 typedef _Iter iterator_type;
1328 typedef _Container __container_type;
1329 typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
1330 typedef typename iterator_traits<iterator_type>::value_type value_type;
1331 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
1332 typedef typename iterator_traits<iterator_type>::pointer pointer;
1333 typedef typename iterator_traits<iterator_type>::reference reference;
1334private:
1335 iterator_type __i;
1336 __debug_iter* __next;
1337 __container_type* __cont;
Howard Hinnant324bb032010-08-22 00:02:43 +00001338
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001339public:
1340 _LIBCPP_INLINE_VISIBILITY __debug_iter() : __next(0), __cont(0) {}
1341 _LIBCPP_INLINE_VISIBILITY __debug_iter(const __debug_iter& __x)
1342 : __i(__x.base()), __next(0), __cont(0) {__set_owner(__x.__cont);}
1343 __debug_iter& operator=(const __debug_iter& __x);
1344 template <class _Up> _LIBCPP_INLINE_VISIBILITY __debug_iter(const __debug_iter<_Container, _Up>& __u,
1345 typename enable_if<is_convertible<_Up, iterator_type>::value>::type* = 0)
1346 : __i(__u.base()), __next(0), __cont(0) {__set_owner(__u.__cont);}
1347 _LIBCPP_INLINE_VISIBILITY ~__debug_iter() {__remove_owner();}
1348 _LIBCPP_INLINE_VISIBILITY reference operator*() const {assert(__is_deref()); return *__i;}
1349 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return &(operator*());}
1350 _LIBCPP_INLINE_VISIBILITY __debug_iter& operator++() {assert(__can_increment()); ++__i; return *this;}
1351 _LIBCPP_INLINE_VISIBILITY __debug_iter operator++(int)
1352 {__debug_iter __tmp(*this); operator++(); return __tmp;}
1353 _LIBCPP_INLINE_VISIBILITY __debug_iter& operator--() {assert(__can_decrement()); --__i; return *this;}
1354 _LIBCPP_INLINE_VISIBILITY __debug_iter operator--(int)
1355 {__debug_iter __tmp(*this); operator--(); return __tmp;}
1356 _LIBCPP_INLINE_VISIBILITY __debug_iter operator+ (difference_type __n) const
1357 {__debug_iter __t(*this); __t += __n; return __t;}
1358 __debug_iter& operator+=(difference_type __n);
1359 _LIBCPP_INLINE_VISIBILITY __debug_iter operator- (difference_type __n) const
1360 {__debug_iter __t(*this); __t -= __n; return __t;}
1361 _LIBCPP_INLINE_VISIBILITY __debug_iter& operator-=(difference_type __n)
1362 {*this += -__n; return *this;}
1363 _LIBCPP_INLINE_VISIBILITY reference operator[](difference_type __n) const
1364 {return *(*this + __n);}
1365
1366private:
1367 _LIBCPP_INLINE_VISIBILITY __debug_iter(const __container_type* __c, iterator_type __x)
1368 : __i(__x), __next(0), __cont(0) {__set_owner(__c);}
1369 _LIBCPP_INLINE_VISIBILITY iterator_type base() const {return __i;}
1370
1371 void __set_owner(const __container_type* __c);
1372 void __remove_owner();
1373 static void __remove_all(__container_type* __c);
1374 static void swap(__container_type* __x, __container_type* __y);
Howard Hinnant324bb032010-08-22 00:02:43 +00001375
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001376 _LIBCPP_INLINE_VISIBILITY bool __is_deref() const
1377 {return __is_deref(__is_random_access_iterator<iterator_type>());}
1378 bool __is_deref(false_type) const;
1379 bool __is_deref(true_type) const;
1380 _LIBCPP_INLINE_VISIBILITY bool __can_decrement() const
1381 {return __can_decrement(integral_constant<int, is_pointer<iterator_type>::value ? 2:
1382 __is_random_access_iterator<iterator_type>::value ? 1 : 0>());}
1383 bool __can_decrement(integral_constant<int, 0>) const;
1384 bool __can_decrement(integral_constant<int, 1>) const;
1385 bool __can_decrement(integral_constant<int, 2>) const;
1386 _LIBCPP_INLINE_VISIBILITY bool __can_increment() const
1387 {return __can_increment(integral_constant<int, is_pointer<iterator_type>::value ? 2:
1388 __is_random_access_iterator<iterator_type>::value ? 1 : 0>());}
1389 bool __can_increment(integral_constant<int, 0>) const;
1390 bool __can_increment(integral_constant<int, 1>) const;
1391 bool __can_increment(integral_constant<int, 2>) const;
1392
1393 _LIBCPP_INLINE_VISIBILITY bool __can_add(difference_type __n) const
1394 {return __can_add(__n, is_pointer<iterator_type>());}
1395 bool __can_add(difference_type __n, false_type) const;
1396 bool __can_add(difference_type __n, true_type) const;
1397
1398 template <class _Cp, class _Up> friend class __debug_iter;
1399 friend class _Container::__self;
1400
1401 template <class _Cp, class _Iter1, class _Iter2>
1402 friend
1403 bool
1404 operator==(const __debug_iter<_Cp, _Iter1>&, const __debug_iter<_Cp, _Iter2>&);
Howard Hinnant324bb032010-08-22 00:02:43 +00001405
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001406 template <class _Cp, class _Iter1, class _Iter2>
1407 friend
1408 bool
1409 operator<(const __debug_iter<_Cp, _Iter1>&, const __debug_iter<_Cp, _Iter2>&);
Howard Hinnant324bb032010-08-22 00:02:43 +00001410
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001411 template <class _Cp, class _Iter1, class _Iter2>
1412 friend
1413 bool
1414 operator!=(const __debug_iter<_Cp, _Iter1>&, const __debug_iter<_Cp, _Iter2>&);
Howard Hinnant324bb032010-08-22 00:02:43 +00001415
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001416 template <class _Cp, class _Iter1, class _Iter2>
1417 friend
1418 bool
1419 operator>(const __debug_iter<_Cp, _Iter1>&, const __debug_iter<_Cp, _Iter2>&);
Howard Hinnant324bb032010-08-22 00:02:43 +00001420
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001421 template <class _Cp, class _Iter1, class _Iter2>
1422 friend
1423 bool
1424 operator>=(const __debug_iter<_Cp, _Iter1>&, const __debug_iter<_Cp, _Iter2>&);
Howard Hinnant324bb032010-08-22 00:02:43 +00001425
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001426 template <class _Cp, class _Iter1, class _Iter2>
1427 friend
1428 bool
1429 operator<=(const __debug_iter<_Cp, _Iter1>&, const __debug_iter<_Cp, _Iter2>&);
Howard Hinnant324bb032010-08-22 00:02:43 +00001430
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001431 template <class _Cp, class _Iter1, class _Iter2>
1432 friend
1433 typename __debug_iter<_Cp, _Iter1>::difference_type
1434 operator-(const __debug_iter<_Cp, _Iter1>&, const __debug_iter<_Cp, _Iter2>&);
Howard Hinnant324bb032010-08-22 00:02:43 +00001435
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001436 template <class _Cp, class _Iter1>
1437 friend
1438 __debug_iter<_Cp, _Iter1>
1439 operator+(typename __debug_iter<_Cp, _Iter1>::difference_type, const __debug_iter<_Cp, _Iter1>&);
1440};
1441
1442template <class _Container, class _Iter>
1443__debug_iter<_Container, _Iter>&
1444__debug_iter<_Container, _Iter>::operator=(const __debug_iter& __x)
1445{
1446 if (this != &__x)
1447 {
1448 __remove_owner();
1449 __i = __x.__i;
1450 __set_owner(__x.__cont);
1451 }
1452 return *this;
1453}
Howard Hinnant324bb032010-08-22 00:02:43 +00001454
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001455template <class _Container, class _Iter>
1456void
1457__debug_iter<_Container, _Iter>::__set_owner(const __container_type* __c)
1458{
1459 __cont = const_cast<__container_type*>(__c);
1460 __debug_iter*& __head = __cont->__get_iterator_list(this);
1461 __next = __head;
1462 __head = this;
1463}
1464
1465template <class _Container, class _Iter>
1466void
1467__debug_iter<_Container, _Iter>::__remove_owner()
1468{
1469 if (__cont)
1470 {
1471 __debug_iter*& __head = __cont->__get_iterator_list(this);
1472 if (__head == this)
1473 __head = __next;
1474 else
1475 {
1476 __debug_iter* __prev = __head;
1477 for (__debug_iter* __p = __head->__next; __p != this; __p = __p->__next)
1478 __prev = __p;
1479 __prev->__next = __next;
1480 }
1481 __cont = 0;
1482 }
1483}
1484
1485template <class _Container, class _Iter>
1486void
1487__debug_iter<_Container, _Iter>::__remove_all(__container_type* __c)
1488{
1489 __debug_iter*& __head = __c->__get_iterator_list((__debug_iter*)0);
1490 __debug_iter* __p = __head;
1491 __head = 0;
1492 while (__p)
1493 {
1494 __p->__cont = 0;
1495 __debug_iter* __n = __p->__next;
1496 __p->__next = 0;
1497 __p = __n;
1498 }
1499}
1500
1501template <class _Container, class _Iter>
1502void
1503__debug_iter<_Container, _Iter>::swap(__container_type* __x, __container_type* __y)
1504{
1505 __debug_iter*& __head_x = __x->__get_iterator_list((__debug_iter*)0);
1506 __debug_iter*& __head_y = __y->__get_iterator_list((__debug_iter*)0);
1507 __debug_iter* __p = __head_x;
1508 __head_x = __head_y;
1509 __head_y = __p;
1510 for (__p = __head_x; __p; __p = __p->__next)
1511 __p->__cont = __x;
1512 for (__p = __head_y; __p; __p = __p->__next)
1513 __p->__cont = __y;
1514}
1515
1516template <class _Container, class _Iter>
1517bool
1518__debug_iter<_Container, _Iter>::__is_deref(false_type) const
1519{
1520 if (__cont == 0)
1521 return false;
1522 return __i != __cont->end().base();
1523}
1524
1525template <class _Container, class _Iter>
1526bool
1527__debug_iter<_Container, _Iter>::__is_deref(true_type) const
1528{
1529 if (__cont == 0)
1530 return false;
1531 return __i < __cont->end().base();
1532}
1533
1534template <class _Container, class _Iter>
1535bool
1536__debug_iter<_Container, _Iter>::__can_decrement(integral_constant<int, 0>) const
1537{
1538 if (__cont == 0)
1539 return false;
1540 return __i != __cont->begin().base();
1541}
1542
1543template <class _Container, class _Iter>
1544bool
1545__debug_iter<_Container, _Iter>::__can_decrement(integral_constant<int, 1>) const
1546{
1547 if (__cont == 0)
1548 return false;
1549 iterator_type __b = __cont->begin().base();
1550 return __b < __i && __i <= __b + __cont->size();
1551}
1552
1553template <class _Container, class _Iter>
1554bool
1555__debug_iter<_Container, _Iter>::__can_decrement(integral_constant<int, 2>) const
1556{
1557 if (__cont == 0)
1558 return false;
1559 iterator_type __b = __cont->begin().base();
1560 return __b < __i && __i <= __b + __cont->size();
1561}
1562
1563template <class _Container, class _Iter>
1564bool
1565__debug_iter<_Container, _Iter>::__can_increment(integral_constant<int, 0>) const
1566{
1567 if (__cont == 0)
1568 return false;
1569 return __i != __cont->end().base();
1570}
1571
1572template <class _Container, class _Iter>
1573bool
1574__debug_iter<_Container, _Iter>::__can_increment(integral_constant<int, 1>) const
1575{
1576 if (__cont == 0)
1577 return false;
1578 iterator_type __b = __cont->begin().base();
1579 return __b <= __i && __i < __b + __cont->size();
1580}
1581
1582template <class _Container, class _Iter>
1583bool
1584__debug_iter<_Container, _Iter>::__can_increment(integral_constant<int, 2>) const
1585{
1586 if (__cont == 0)
1587 return false;
1588 iterator_type __b = __cont->begin().base();
1589 return __b <= __i && __i < __b + __cont->size();
1590}
1591
1592template <class _Container, class _Iter>
1593bool
1594__debug_iter<_Container, _Iter>::__can_add(difference_type __n, false_type) const
1595{
1596 if (__cont == 0)
1597 return false;
1598 iterator_type __b = __cont->begin().base();
1599 iterator_type __j = __i + __n;
1600 return __b <= __j && __j <= __b + __cont->size();
1601}
1602
1603template <class _Container, class _Iter>
1604bool
1605__debug_iter<_Container, _Iter>::__can_add(difference_type __n, true_type) const
1606{
1607 if (__cont == 0)
1608 return false;
1609 iterator_type __b = __cont->begin().base();
1610 iterator_type __j = __i + __n;
1611 return __b <= __j && __j <= __b + __cont->size();
1612}
1613
1614template <class _Container, class _Iter>
1615__debug_iter<_Container, _Iter>&
1616__debug_iter<_Container, _Iter>::operator+=(difference_type __n)
1617{
1618 assert(__can_add(__n));
1619 __i += __n;
1620 return *this;
1621}
1622
1623template <class _Container, class _Iter1, class _Iter2>
1624inline _LIBCPP_INLINE_VISIBILITY
1625bool
1626operator==(const __debug_iter<_Container, _Iter1>& __x, const __debug_iter<_Container, _Iter2>& __y)
1627{
1628 assert(__x.__cont && __x.__cont == __y.__cont);
1629 return __x.base() == __y.base();
1630}
1631
1632template <class _Container, class _Iter1, class _Iter2>
1633inline _LIBCPP_INLINE_VISIBILITY
1634bool
1635operator!=(const __debug_iter<_Container, _Iter1>& __x, const __debug_iter<_Container, _Iter2>& __y)
1636{
1637 return !(__x == __y);
1638}
1639
1640template <class _Container, class _Iter1, class _Iter2>
1641inline _LIBCPP_INLINE_VISIBILITY
1642bool
1643operator<(const __debug_iter<_Container, _Iter1>& __x, const __debug_iter<_Container, _Iter2>& __y)
1644{
1645 assert(__x.__cont && __x.__cont == __y.__cont);
1646 return __x.base() < __y.base();
1647}
1648
1649template <class _Container, class _Iter1, class _Iter2>
1650inline _LIBCPP_INLINE_VISIBILITY
1651bool
1652operator>(const __debug_iter<_Container, _Iter1>& __x, const __debug_iter<_Container, _Iter2>& __y)
1653{
1654 return __y < __x;
1655}
1656
1657template <class _Container, class _Iter1, class _Iter2>
1658inline _LIBCPP_INLINE_VISIBILITY
1659bool
1660operator>=(const __debug_iter<_Container, _Iter1>& __x, const __debug_iter<_Container, _Iter2>& __y)
1661{
1662 return !(__x < __y);
1663}
1664
1665template <class _Container, class _Iter1, class _Iter2>
1666inline _LIBCPP_INLINE_VISIBILITY
1667bool
1668operator<=(const __debug_iter<_Container, _Iter1>& __x, const __debug_iter<_Container, _Iter2>& __y)
1669{
1670 return !(__y < __x);
1671}
1672
1673template <class _Container, class _Iter1, class _Iter2>
1674inline _LIBCPP_INLINE_VISIBILITY
1675typename __debug_iter<_Container, _Iter1>::difference_type
1676operator-(const __debug_iter<_Container, _Iter1>& __x, const __debug_iter<_Container, _Iter2>& __y)
1677{
1678 assert(__x.__cont && __x.__cont == __y.__cont);
1679 return __x.base() - __y.base();
1680}
1681
1682template <class _Container, class _Iter>
1683inline _LIBCPP_INLINE_VISIBILITY
1684__debug_iter<_Container, _Iter>
1685operator+(typename __debug_iter<_Container, _Iter>::difference_type __n,
1686 const __debug_iter<_Container, _Iter>& __x)
1687{
1688 return __x + __n;
1689}
1690
1691#endif // _LIBCPP_DEBUG
1692
Howard Hinnant726a76f2010-11-16 21:10:23 +00001693#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_TRAILING_RETURN)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001694
1695template <class _C>
Howard Hinnant82894812010-09-22 16:48:34 +00001696inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001697auto
1698begin(_C& __c) -> decltype(__c.begin())
1699{
1700 return __c.begin();
1701}
1702
1703template <class _C>
Howard Hinnant82894812010-09-22 16:48:34 +00001704inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001705auto
1706begin(const _C& __c) -> decltype(__c.begin())
1707{
1708 return __c.begin();
1709}
1710
1711template <class _C>
Howard Hinnant82894812010-09-22 16:48:34 +00001712inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001713auto
1714end(_C& __c) -> decltype(__c.end())
1715{
1716 return __c.end();
1717}
1718
1719template <class _C>
Howard Hinnant82894812010-09-22 16:48:34 +00001720inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001721auto
1722end(const _C& __c) -> decltype(__c.end())
1723{
1724 return __c.end();
1725}
1726
Howard Hinnant726a76f2010-11-16 21:10:23 +00001727#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_TRAILING_RETURN)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001728
1729template <class _C>
Howard Hinnant82894812010-09-22 16:48:34 +00001730inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001731typename _C::iterator
1732begin(_C& __c)
1733{
1734 return __c.begin();
1735}
1736
1737template <class _C>
Howard Hinnant82894812010-09-22 16:48:34 +00001738inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001739typename _C::const_iterator
1740begin(const _C& __c)
1741{
1742 return __c.begin();
1743}
1744
1745template <class _C>
Howard Hinnant82894812010-09-22 16:48:34 +00001746inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001747typename _C::iterator
1748end(_C& __c)
1749{
1750 return __c.end();
1751}
1752
1753template <class _C>
Howard Hinnant82894812010-09-22 16:48:34 +00001754inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001755typename _C::const_iterator
1756end(const _C& __c)
1757{
1758 return __c.end();
1759}
1760
Howard Hinnant726a76f2010-11-16 21:10:23 +00001761#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_TRAILING_RETURN)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001762
1763template <class _T, size_t _N>
Howard Hinnant82894812010-09-22 16:48:34 +00001764inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001765_T*
1766begin(_T (&__array)[_N])
1767{
1768 return __array;
1769}
1770
1771template <class _T, size_t _N>
Howard Hinnant82894812010-09-22 16:48:34 +00001772inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001773_T*
1774end(_T (&__array)[_N])
1775{
1776 return __array + _N;
1777}
1778
1779_LIBCPP_END_NAMESPACE_STD
1780
1781#endif // _LIBCPP_ITERATOR