stl_iterator.h (make_move_iterator): Implement DR2061.
[gcc.git] / libstdc++-v3 / include / bits / stl_iterator.h
1 // Iterators -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4 // 2010, 2011
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
11 // any later version.
12
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
21
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
26
27 /*
28 *
29 * Copyright (c) 1994
30 * Hewlett-Packard Company
31 *
32 * Permission to use, copy, modify, distribute and sell this software
33 * and its documentation for any purpose is hereby granted without fee,
34 * provided that the above copyright notice appear in all copies and
35 * that both that copyright notice and this permission notice appear
36 * in supporting documentation. Hewlett-Packard Company makes no
37 * representations about the suitability of this software for any
38 * purpose. It is provided "as is" without express or implied warranty.
39 *
40 *
41 * Copyright (c) 1996-1998
42 * Silicon Graphics Computer Systems, Inc.
43 *
44 * Permission to use, copy, modify, distribute and sell this software
45 * and its documentation for any purpose is hereby granted without fee,
46 * provided that the above copyright notice appear in all copies and
47 * that both that copyright notice and this permission notice appear
48 * in supporting documentation. Silicon Graphics makes no
49 * representations about the suitability of this software for any
50 * purpose. It is provided "as is" without express or implied warranty.
51 */
52
53 /** @file bits/stl_iterator.h
54 * This is an internal header file, included by other library headers.
55 * Do not attempt to use it directly. @headername{iterator}
56 *
57 * This file implements reverse_iterator, back_insert_iterator,
58 * front_insert_iterator, insert_iterator, __normal_iterator, and their
59 * supporting functions and overloaded operators.
60 */
61
62 #ifndef _STL_ITERATOR_H
63 #define _STL_ITERATOR_H 1
64
65 #include <bits/cpp_type_traits.h>
66 #include <ext/type_traits.h>
67 #include <bits/move.h>
68
69 namespace std _GLIBCXX_VISIBILITY(default)
70 {
71 _GLIBCXX_BEGIN_NAMESPACE_VERSION
72
73 /**
74 * @addtogroup iterators
75 * @{
76 */
77
78 // 24.4.1 Reverse iterators
79 /**
80 * Bidirectional and random access iterators have corresponding reverse
81 * %iterator adaptors that iterate through the data structure in the
82 * opposite direction. They have the same signatures as the corresponding
83 * iterators. The fundamental relation between a reverse %iterator and its
84 * corresponding %iterator @c i is established by the identity:
85 * @code
86 * &*(reverse_iterator(i)) == &*(i - 1)
87 * @endcode
88 *
89 * <em>This mapping is dictated by the fact that while there is always a
90 * pointer past the end of an array, there might not be a valid pointer
91 * before the beginning of an array.</em> [24.4.1]/1,2
92 *
93 * Reverse iterators can be tricky and surprising at first. Their
94 * semantics make sense, however, and the trickiness is a side effect of
95 * the requirement that the iterators must be safe.
96 */
97 template<typename _Iterator>
98 class reverse_iterator
99 : public iterator<typename iterator_traits<_Iterator>::iterator_category,
100 typename iterator_traits<_Iterator>::value_type,
101 typename iterator_traits<_Iterator>::difference_type,
102 typename iterator_traits<_Iterator>::pointer,
103 typename iterator_traits<_Iterator>::reference>
104 {
105 protected:
106 _Iterator current;
107
108 typedef iterator_traits<_Iterator> __traits_type;
109
110 public:
111 typedef _Iterator iterator_type;
112 typedef typename __traits_type::difference_type difference_type;
113 typedef typename __traits_type::pointer pointer;
114 typedef typename __traits_type::reference reference;
115
116 /**
117 * The default constructor default-initializes member @p current.
118 * If it is a pointer, that means it is zero-initialized.
119 */
120 // _GLIBCXX_RESOLVE_LIB_DEFECTS
121 // 235 No specification of default ctor for reverse_iterator
122 reverse_iterator() : current() { }
123
124 /**
125 * This %iterator will move in the opposite direction that @p x does.
126 */
127 explicit
128 reverse_iterator(iterator_type __x) : current(__x) { }
129
130 /**
131 * The copy constructor is normal.
132 */
133 reverse_iterator(const reverse_iterator& __x)
134 : current(__x.current) { }
135
136 /**
137 * A reverse_iterator across other types can be copied in the normal
138 * fashion.
139 */
140 template<typename _Iter>
141 reverse_iterator(const reverse_iterator<_Iter>& __x)
142 : current(__x.base()) { }
143
144 /**
145 * @return @c current, the %iterator used for underlying work.
146 */
147 iterator_type
148 base() const
149 { return current; }
150
151 /**
152 * @return TODO
153 *
154 * @doctodo
155 */
156 reference
157 operator*() const
158 {
159 _Iterator __tmp = current;
160 return *--__tmp;
161 }
162
163 /**
164 * @return TODO
165 *
166 * @doctodo
167 */
168 pointer
169 operator->() const
170 { return &(operator*()); }
171
172 /**
173 * @return TODO
174 *
175 * @doctodo
176 */
177 reverse_iterator&
178 operator++()
179 {
180 --current;
181 return *this;
182 }
183
184 /**
185 * @return TODO
186 *
187 * @doctodo
188 */
189 reverse_iterator
190 operator++(int)
191 {
192 reverse_iterator __tmp = *this;
193 --current;
194 return __tmp;
195 }
196
197 /**
198 * @return TODO
199 *
200 * @doctodo
201 */
202 reverse_iterator&
203 operator--()
204 {
205 ++current;
206 return *this;
207 }
208
209 /**
210 * @return TODO
211 *
212 * @doctodo
213 */
214 reverse_iterator
215 operator--(int)
216 {
217 reverse_iterator __tmp = *this;
218 ++current;
219 return __tmp;
220 }
221
222 /**
223 * @return TODO
224 *
225 * @doctodo
226 */
227 reverse_iterator
228 operator+(difference_type __n) const
229 { return reverse_iterator(current - __n); }
230
231 /**
232 * @return TODO
233 *
234 * @doctodo
235 */
236 reverse_iterator&
237 operator+=(difference_type __n)
238 {
239 current -= __n;
240 return *this;
241 }
242
243 /**
244 * @return TODO
245 *
246 * @doctodo
247 */
248 reverse_iterator
249 operator-(difference_type __n) const
250 { return reverse_iterator(current + __n); }
251
252 /**
253 * @return TODO
254 *
255 * @doctodo
256 */
257 reverse_iterator&
258 operator-=(difference_type __n)
259 {
260 current += __n;
261 return *this;
262 }
263
264 /**
265 * @return TODO
266 *
267 * @doctodo
268 */
269 reference
270 operator[](difference_type __n) const
271 { return *(*this + __n); }
272 };
273
274 //@{
275 /**
276 * @param __x A %reverse_iterator.
277 * @param __y A %reverse_iterator.
278 * @return A simple bool.
279 *
280 * Reverse iterators forward many operations to their underlying base()
281 * iterators. Others are implemented in terms of one another.
282 *
283 */
284 template<typename _Iterator>
285 inline bool
286 operator==(const reverse_iterator<_Iterator>& __x,
287 const reverse_iterator<_Iterator>& __y)
288 { return __x.base() == __y.base(); }
289
290 template<typename _Iterator>
291 inline bool
292 operator<(const reverse_iterator<_Iterator>& __x,
293 const reverse_iterator<_Iterator>& __y)
294 { return __y.base() < __x.base(); }
295
296 template<typename _Iterator>
297 inline bool
298 operator!=(const reverse_iterator<_Iterator>& __x,
299 const reverse_iterator<_Iterator>& __y)
300 { return !(__x == __y); }
301
302 template<typename _Iterator>
303 inline bool
304 operator>(const reverse_iterator<_Iterator>& __x,
305 const reverse_iterator<_Iterator>& __y)
306 { return __y < __x; }
307
308 template<typename _Iterator>
309 inline bool
310 operator<=(const reverse_iterator<_Iterator>& __x,
311 const reverse_iterator<_Iterator>& __y)
312 { return !(__y < __x); }
313
314 template<typename _Iterator>
315 inline bool
316 operator>=(const reverse_iterator<_Iterator>& __x,
317 const reverse_iterator<_Iterator>& __y)
318 { return !(__x < __y); }
319
320 template<typename _Iterator>
321 inline typename reverse_iterator<_Iterator>::difference_type
322 operator-(const reverse_iterator<_Iterator>& __x,
323 const reverse_iterator<_Iterator>& __y)
324 { return __y.base() - __x.base(); }
325
326 template<typename _Iterator>
327 inline reverse_iterator<_Iterator>
328 operator+(typename reverse_iterator<_Iterator>::difference_type __n,
329 const reverse_iterator<_Iterator>& __x)
330 { return reverse_iterator<_Iterator>(__x.base() - __n); }
331
332 // _GLIBCXX_RESOLVE_LIB_DEFECTS
333 // DR 280. Comparison of reverse_iterator to const reverse_iterator.
334 template<typename _IteratorL, typename _IteratorR>
335 inline bool
336 operator==(const reverse_iterator<_IteratorL>& __x,
337 const reverse_iterator<_IteratorR>& __y)
338 { return __x.base() == __y.base(); }
339
340 template<typename _IteratorL, typename _IteratorR>
341 inline bool
342 operator<(const reverse_iterator<_IteratorL>& __x,
343 const reverse_iterator<_IteratorR>& __y)
344 { return __y.base() < __x.base(); }
345
346 template<typename _IteratorL, typename _IteratorR>
347 inline bool
348 operator!=(const reverse_iterator<_IteratorL>& __x,
349 const reverse_iterator<_IteratorR>& __y)
350 { return !(__x == __y); }
351
352 template<typename _IteratorL, typename _IteratorR>
353 inline bool
354 operator>(const reverse_iterator<_IteratorL>& __x,
355 const reverse_iterator<_IteratorR>& __y)
356 { return __y < __x; }
357
358 template<typename _IteratorL, typename _IteratorR>
359 inline bool
360 operator<=(const reverse_iterator<_IteratorL>& __x,
361 const reverse_iterator<_IteratorR>& __y)
362 { return !(__y < __x); }
363
364 template<typename _IteratorL, typename _IteratorR>
365 inline bool
366 operator>=(const reverse_iterator<_IteratorL>& __x,
367 const reverse_iterator<_IteratorR>& __y)
368 { return !(__x < __y); }
369
370 template<typename _IteratorL, typename _IteratorR>
371 #ifdef __GXX_EXPERIMENTAL_CXX0X__
372 // DR 685.
373 inline auto
374 operator-(const reverse_iterator<_IteratorL>& __x,
375 const reverse_iterator<_IteratorR>& __y)
376 -> decltype(__y.base() - __x.base())
377 #else
378 inline typename reverse_iterator<_IteratorL>::difference_type
379 operator-(const reverse_iterator<_IteratorL>& __x,
380 const reverse_iterator<_IteratorR>& __y)
381 #endif
382 { return __y.base() - __x.base(); }
383 //@}
384
385 // 24.4.2.2.1 back_insert_iterator
386 /**
387 * @brief Turns assignment into insertion.
388 *
389 * These are output iterators, constructed from a container-of-T.
390 * Assigning a T to the iterator appends it to the container using
391 * push_back.
392 *
393 * Tip: Using the back_inserter function to create these iterators can
394 * save typing.
395 */
396 template<typename _Container>
397 class back_insert_iterator
398 : public iterator<output_iterator_tag, void, void, void, void>
399 {
400 protected:
401 _Container* container;
402
403 public:
404 /// A nested typedef for the type of whatever container you used.
405 typedef _Container container_type;
406
407 /// The only way to create this %iterator is with a container.
408 explicit
409 back_insert_iterator(_Container& __x) : container(&__x) { }
410
411 /**
412 * @param __value An instance of whatever type
413 * container_type::const_reference is; presumably a
414 * reference-to-const T for container<T>.
415 * @return This %iterator, for chained operations.
416 *
417 * This kind of %iterator doesn't really have a @a position in the
418 * container (you can think of the position as being permanently at
419 * the end, if you like). Assigning a value to the %iterator will
420 * always append the value to the end of the container.
421 */
422 #ifndef __GXX_EXPERIMENTAL_CXX0X__
423 back_insert_iterator&
424 operator=(typename _Container::const_reference __value)
425 {
426 container->push_back(__value);
427 return *this;
428 }
429 #else
430 back_insert_iterator&
431 operator=(const typename _Container::value_type& __value)
432 {
433 container->push_back(__value);
434 return *this;
435 }
436
437 back_insert_iterator&
438 operator=(typename _Container::value_type&& __value)
439 {
440 container->push_back(std::move(__value));
441 return *this;
442 }
443 #endif
444
445 /// Simply returns *this.
446 back_insert_iterator&
447 operator*()
448 { return *this; }
449
450 /// Simply returns *this. (This %iterator does not @a move.)
451 back_insert_iterator&
452 operator++()
453 { return *this; }
454
455 /// Simply returns *this. (This %iterator does not @a move.)
456 back_insert_iterator
457 operator++(int)
458 { return *this; }
459 };
460
461 /**
462 * @param __x A container of arbitrary type.
463 * @return An instance of back_insert_iterator working on @p __x.
464 *
465 * This wrapper function helps in creating back_insert_iterator instances.
466 * Typing the name of the %iterator requires knowing the precise full
467 * type of the container, which can be tedious and impedes generic
468 * programming. Using this function lets you take advantage of automatic
469 * template parameter deduction, making the compiler match the correct
470 * types for you.
471 */
472 template<typename _Container>
473 inline back_insert_iterator<_Container>
474 back_inserter(_Container& __x)
475 { return back_insert_iterator<_Container>(__x); }
476
477 /**
478 * @brief Turns assignment into insertion.
479 *
480 * These are output iterators, constructed from a container-of-T.
481 * Assigning a T to the iterator prepends it to the container using
482 * push_front.
483 *
484 * Tip: Using the front_inserter function to create these iterators can
485 * save typing.
486 */
487 template<typename _Container>
488 class front_insert_iterator
489 : public iterator<output_iterator_tag, void, void, void, void>
490 {
491 protected:
492 _Container* container;
493
494 public:
495 /// A nested typedef for the type of whatever container you used.
496 typedef _Container container_type;
497
498 /// The only way to create this %iterator is with a container.
499 explicit front_insert_iterator(_Container& __x) : container(&__x) { }
500
501 /**
502 * @param __value An instance of whatever type
503 * container_type::const_reference is; presumably a
504 * reference-to-const T for container<T>.
505 * @return This %iterator, for chained operations.
506 *
507 * This kind of %iterator doesn't really have a @a position in the
508 * container (you can think of the position as being permanently at
509 * the front, if you like). Assigning a value to the %iterator will
510 * always prepend the value to the front of the container.
511 */
512 #ifndef __GXX_EXPERIMENTAL_CXX0X__
513 front_insert_iterator&
514 operator=(typename _Container::const_reference __value)
515 {
516 container->push_front(__value);
517 return *this;
518 }
519 #else
520 front_insert_iterator&
521 operator=(const typename _Container::value_type& __value)
522 {
523 container->push_front(__value);
524 return *this;
525 }
526
527 front_insert_iterator&
528 operator=(typename _Container::value_type&& __value)
529 {
530 container->push_front(std::move(__value));
531 return *this;
532 }
533 #endif
534
535 /// Simply returns *this.
536 front_insert_iterator&
537 operator*()
538 { return *this; }
539
540 /// Simply returns *this. (This %iterator does not @a move.)
541 front_insert_iterator&
542 operator++()
543 { return *this; }
544
545 /// Simply returns *this. (This %iterator does not @a move.)
546 front_insert_iterator
547 operator++(int)
548 { return *this; }
549 };
550
551 /**
552 * @param __x A container of arbitrary type.
553 * @return An instance of front_insert_iterator working on @p x.
554 *
555 * This wrapper function helps in creating front_insert_iterator instances.
556 * Typing the name of the %iterator requires knowing the precise full
557 * type of the container, which can be tedious and impedes generic
558 * programming. Using this function lets you take advantage of automatic
559 * template parameter deduction, making the compiler match the correct
560 * types for you.
561 */
562 template<typename _Container>
563 inline front_insert_iterator<_Container>
564 front_inserter(_Container& __x)
565 { return front_insert_iterator<_Container>(__x); }
566
567 /**
568 * @brief Turns assignment into insertion.
569 *
570 * These are output iterators, constructed from a container-of-T.
571 * Assigning a T to the iterator inserts it in the container at the
572 * %iterator's position, rather than overwriting the value at that
573 * position.
574 *
575 * (Sequences will actually insert a @e copy of the value before the
576 * %iterator's position.)
577 *
578 * Tip: Using the inserter function to create these iterators can
579 * save typing.
580 */
581 template<typename _Container>
582 class insert_iterator
583 : public iterator<output_iterator_tag, void, void, void, void>
584 {
585 protected:
586 _Container* container;
587 typename _Container::iterator iter;
588
589 public:
590 /// A nested typedef for the type of whatever container you used.
591 typedef _Container container_type;
592
593 /**
594 * The only way to create this %iterator is with a container and an
595 * initial position (a normal %iterator into the container).
596 */
597 insert_iterator(_Container& __x, typename _Container::iterator __i)
598 : container(&__x), iter(__i) {}
599
600 /**
601 * @param __value An instance of whatever type
602 * container_type::const_reference is; presumably a
603 * reference-to-const T for container<T>.
604 * @return This %iterator, for chained operations.
605 *
606 * This kind of %iterator maintains its own position in the
607 * container. Assigning a value to the %iterator will insert the
608 * value into the container at the place before the %iterator.
609 *
610 * The position is maintained such that subsequent assignments will
611 * insert values immediately after one another. For example,
612 * @code
613 * // vector v contains A and Z
614 *
615 * insert_iterator i (v, ++v.begin());
616 * i = 1;
617 * i = 2;
618 * i = 3;
619 *
620 * // vector v contains A, 1, 2, 3, and Z
621 * @endcode
622 */
623 #ifndef __GXX_EXPERIMENTAL_CXX0X__
624 insert_iterator&
625 operator=(typename _Container::const_reference __value)
626 {
627 iter = container->insert(iter, __value);
628 ++iter;
629 return *this;
630 }
631 #else
632 insert_iterator&
633 operator=(const typename _Container::value_type& __value)
634 {
635 iter = container->insert(iter, __value);
636 ++iter;
637 return *this;
638 }
639
640 insert_iterator&
641 operator=(typename _Container::value_type&& __value)
642 {
643 iter = container->insert(iter, std::move(__value));
644 ++iter;
645 return *this;
646 }
647 #endif
648
649 /// Simply returns *this.
650 insert_iterator&
651 operator*()
652 { return *this; }
653
654 /// Simply returns *this. (This %iterator does not @a move.)
655 insert_iterator&
656 operator++()
657 { return *this; }
658
659 /// Simply returns *this. (This %iterator does not @a move.)
660 insert_iterator&
661 operator++(int)
662 { return *this; }
663 };
664
665 /**
666 * @param __x A container of arbitrary type.
667 * @return An instance of insert_iterator working on @p __x.
668 *
669 * This wrapper function helps in creating insert_iterator instances.
670 * Typing the name of the %iterator requires knowing the precise full
671 * type of the container, which can be tedious and impedes generic
672 * programming. Using this function lets you take advantage of automatic
673 * template parameter deduction, making the compiler match the correct
674 * types for you.
675 */
676 template<typename _Container, typename _Iterator>
677 inline insert_iterator<_Container>
678 inserter(_Container& __x, _Iterator __i)
679 {
680 return insert_iterator<_Container>(__x,
681 typename _Container::iterator(__i));
682 }
683
684 // @} group iterators
685
686 _GLIBCXX_END_NAMESPACE_VERSION
687 } // namespace
688
689 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
690 {
691 _GLIBCXX_BEGIN_NAMESPACE_VERSION
692
693 // This iterator adapter is @a normal in the sense that it does not
694 // change the semantics of any of the operators of its iterator
695 // parameter. Its primary purpose is to convert an iterator that is
696 // not a class, e.g. a pointer, into an iterator that is a class.
697 // The _Container parameter exists solely so that different containers
698 // using this template can instantiate different types, even if the
699 // _Iterator parameter is the same.
700 using std::iterator_traits;
701 using std::iterator;
702 template<typename _Iterator, typename _Container>
703 class __normal_iterator
704 {
705 protected:
706 _Iterator _M_current;
707
708 typedef iterator_traits<_Iterator> __traits_type;
709
710 public:
711 typedef _Iterator iterator_type;
712 typedef typename __traits_type::iterator_category iterator_category;
713 typedef typename __traits_type::value_type value_type;
714 typedef typename __traits_type::difference_type difference_type;
715 typedef typename __traits_type::reference reference;
716 typedef typename __traits_type::pointer pointer;
717
718 _GLIBCXX_CONSTEXPR __normal_iterator() : _M_current(_Iterator()) { }
719
720 explicit
721 __normal_iterator(const _Iterator& __i) : _M_current(__i) { }
722
723 // Allow iterator to const_iterator conversion
724 template<typename _Iter>
725 __normal_iterator(const __normal_iterator<_Iter,
726 typename __enable_if<
727 (std::__are_same<_Iter, typename _Container::pointer>::__value),
728 _Container>::__type>& __i)
729 : _M_current(__i.base()) { }
730
731 // Forward iterator requirements
732 reference
733 operator*() const
734 { return *_M_current; }
735
736 pointer
737 operator->() const
738 { return _M_current; }
739
740 __normal_iterator&
741 operator++()
742 {
743 ++_M_current;
744 return *this;
745 }
746
747 __normal_iterator
748 operator++(int)
749 { return __normal_iterator(_M_current++); }
750
751 // Bidirectional iterator requirements
752 __normal_iterator&
753 operator--()
754 {
755 --_M_current;
756 return *this;
757 }
758
759 __normal_iterator
760 operator--(int)
761 { return __normal_iterator(_M_current--); }
762
763 // Random access iterator requirements
764 reference
765 operator[](const difference_type& __n) const
766 { return _M_current[__n]; }
767
768 __normal_iterator&
769 operator+=(const difference_type& __n)
770 { _M_current += __n; return *this; }
771
772 __normal_iterator
773 operator+(const difference_type& __n) const
774 { return __normal_iterator(_M_current + __n); }
775
776 __normal_iterator&
777 operator-=(const difference_type& __n)
778 { _M_current -= __n; return *this; }
779
780 __normal_iterator
781 operator-(const difference_type& __n) const
782 { return __normal_iterator(_M_current - __n); }
783
784 const _Iterator&
785 base() const
786 { return _M_current; }
787 };
788
789 // Note: In what follows, the left- and right-hand-side iterators are
790 // allowed to vary in types (conceptually in cv-qualification) so that
791 // comparison between cv-qualified and non-cv-qualified iterators be
792 // valid. However, the greedy and unfriendly operators in std::rel_ops
793 // will make overload resolution ambiguous (when in scope) if we don't
794 // provide overloads whose operands are of the same type. Can someone
795 // remind me what generic programming is about? -- Gaby
796
797 // Forward iterator requirements
798 template<typename _IteratorL, typename _IteratorR, typename _Container>
799 inline bool
800 operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
801 const __normal_iterator<_IteratorR, _Container>& __rhs)
802 { return __lhs.base() == __rhs.base(); }
803
804 template<typename _Iterator, typename _Container>
805 inline bool
806 operator==(const __normal_iterator<_Iterator, _Container>& __lhs,
807 const __normal_iterator<_Iterator, _Container>& __rhs)
808 { return __lhs.base() == __rhs.base(); }
809
810 template<typename _IteratorL, typename _IteratorR, typename _Container>
811 inline bool
812 operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs,
813 const __normal_iterator<_IteratorR, _Container>& __rhs)
814 { return __lhs.base() != __rhs.base(); }
815
816 template<typename _Iterator, typename _Container>
817 inline bool
818 operator!=(const __normal_iterator<_Iterator, _Container>& __lhs,
819 const __normal_iterator<_Iterator, _Container>& __rhs)
820 { return __lhs.base() != __rhs.base(); }
821
822 // Random access iterator requirements
823 template<typename _IteratorL, typename _IteratorR, typename _Container>
824 inline bool
825 operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
826 const __normal_iterator<_IteratorR, _Container>& __rhs)
827 { return __lhs.base() < __rhs.base(); }
828
829 template<typename _Iterator, typename _Container>
830 inline bool
831 operator<(const __normal_iterator<_Iterator, _Container>& __lhs,
832 const __normal_iterator<_Iterator, _Container>& __rhs)
833 { return __lhs.base() < __rhs.base(); }
834
835 template<typename _IteratorL, typename _IteratorR, typename _Container>
836 inline bool
837 operator>(const __normal_iterator<_IteratorL, _Container>& __lhs,
838 const __normal_iterator<_IteratorR, _Container>& __rhs)
839 { return __lhs.base() > __rhs.base(); }
840
841 template<typename _Iterator, typename _Container>
842 inline bool
843 operator>(const __normal_iterator<_Iterator, _Container>& __lhs,
844 const __normal_iterator<_Iterator, _Container>& __rhs)
845 { return __lhs.base() > __rhs.base(); }
846
847 template<typename _IteratorL, typename _IteratorR, typename _Container>
848 inline bool
849 operator<=(const __normal_iterator<_IteratorL, _Container>& __lhs,
850 const __normal_iterator<_IteratorR, _Container>& __rhs)
851 { return __lhs.base() <= __rhs.base(); }
852
853 template<typename _Iterator, typename _Container>
854 inline bool
855 operator<=(const __normal_iterator<_Iterator, _Container>& __lhs,
856 const __normal_iterator<_Iterator, _Container>& __rhs)
857 { return __lhs.base() <= __rhs.base(); }
858
859 template<typename _IteratorL, typename _IteratorR, typename _Container>
860 inline bool
861 operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs,
862 const __normal_iterator<_IteratorR, _Container>& __rhs)
863 { return __lhs.base() >= __rhs.base(); }
864
865 template<typename _Iterator, typename _Container>
866 inline bool
867 operator>=(const __normal_iterator<_Iterator, _Container>& __lhs,
868 const __normal_iterator<_Iterator, _Container>& __rhs)
869 { return __lhs.base() >= __rhs.base(); }
870
871 // _GLIBCXX_RESOLVE_LIB_DEFECTS
872 // According to the resolution of DR179 not only the various comparison
873 // operators but also operator- must accept mixed iterator/const_iterator
874 // parameters.
875 template<typename _IteratorL, typename _IteratorR, typename _Container>
876 #ifdef __GXX_EXPERIMENTAL_CXX0X__
877 // DR 685.
878 inline auto
879 operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
880 const __normal_iterator<_IteratorR, _Container>& __rhs)
881 -> decltype(__lhs.base() - __rhs.base())
882 #else
883 inline typename __normal_iterator<_IteratorL, _Container>::difference_type
884 operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
885 const __normal_iterator<_IteratorR, _Container>& __rhs)
886 #endif
887 { return __lhs.base() - __rhs.base(); }
888
889 template<typename _Iterator, typename _Container>
890 inline typename __normal_iterator<_Iterator, _Container>::difference_type
891 operator-(const __normal_iterator<_Iterator, _Container>& __lhs,
892 const __normal_iterator<_Iterator, _Container>& __rhs)
893 { return __lhs.base() - __rhs.base(); }
894
895 template<typename _Iterator, typename _Container>
896 inline __normal_iterator<_Iterator, _Container>
897 operator+(typename __normal_iterator<_Iterator, _Container>::difference_type
898 __n, const __normal_iterator<_Iterator, _Container>& __i)
899 { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); }
900
901 _GLIBCXX_END_NAMESPACE_VERSION
902 } // namespace
903
904 #ifdef __GXX_EXPERIMENTAL_CXX0X__
905
906 namespace std _GLIBCXX_VISIBILITY(default)
907 {
908 _GLIBCXX_BEGIN_NAMESPACE_VERSION
909
910 /**
911 * @addtogroup iterators
912 * @{
913 */
914
915 // 24.4.3 Move iterators
916 /**
917 * Class template move_iterator is an iterator adapter with the same
918 * behavior as the underlying iterator except that its dereference
919 * operator implicitly converts the value returned by the underlying
920 * iterator's dereference operator to an rvalue reference. Some
921 * generic algorithms can be called with move iterators to replace
922 * copying with moving.
923 */
924 template<typename _Iterator>
925 class move_iterator
926 {
927 protected:
928 _Iterator _M_current;
929
930 typedef iterator_traits<_Iterator> __traits_type;
931
932 public:
933 typedef _Iterator iterator_type;
934 typedef typename __traits_type::iterator_category iterator_category;
935 typedef typename __traits_type::value_type value_type;
936 typedef typename __traits_type::difference_type difference_type;
937 // NB: DR 680.
938 typedef _Iterator pointer;
939 typedef value_type&& reference;
940
941 move_iterator()
942 : _M_current() { }
943
944 explicit
945 move_iterator(iterator_type __i)
946 : _M_current(__i) { }
947
948 template<typename _Iter>
949 move_iterator(const move_iterator<_Iter>& __i)
950 : _M_current(__i.base()) { }
951
952 iterator_type
953 base() const
954 { return _M_current; }
955
956 reference
957 operator*() const
958 { return std::move(*_M_current); }
959
960 pointer
961 operator->() const
962 { return _M_current; }
963
964 move_iterator&
965 operator++()
966 {
967 ++_M_current;
968 return *this;
969 }
970
971 move_iterator
972 operator++(int)
973 {
974 move_iterator __tmp = *this;
975 ++_M_current;
976 return __tmp;
977 }
978
979 move_iterator&
980 operator--()
981 {
982 --_M_current;
983 return *this;
984 }
985
986 move_iterator
987 operator--(int)
988 {
989 move_iterator __tmp = *this;
990 --_M_current;
991 return __tmp;
992 }
993
994 move_iterator
995 operator+(difference_type __n) const
996 { return move_iterator(_M_current + __n); }
997
998 move_iterator&
999 operator+=(difference_type __n)
1000 {
1001 _M_current += __n;
1002 return *this;
1003 }
1004
1005 move_iterator
1006 operator-(difference_type __n) const
1007 { return move_iterator(_M_current - __n); }
1008
1009 move_iterator&
1010 operator-=(difference_type __n)
1011 {
1012 _M_current -= __n;
1013 return *this;
1014 }
1015
1016 reference
1017 operator[](difference_type __n) const
1018 { return std::move(_M_current[__n]); }
1019 };
1020
1021 // Note: See __normal_iterator operators note from Gaby to understand
1022 // why there are always 2 versions for most of the move_iterator
1023 // operators.
1024 template<typename _IteratorL, typename _IteratorR>
1025 inline bool
1026 operator==(const move_iterator<_IteratorL>& __x,
1027 const move_iterator<_IteratorR>& __y)
1028 { return __x.base() == __y.base(); }
1029
1030 template<typename _Iterator>
1031 inline bool
1032 operator==(const move_iterator<_Iterator>& __x,
1033 const move_iterator<_Iterator>& __y)
1034 { return __x.base() == __y.base(); }
1035
1036 template<typename _IteratorL, typename _IteratorR>
1037 inline bool
1038 operator!=(const move_iterator<_IteratorL>& __x,
1039 const move_iterator<_IteratorR>& __y)
1040 { return !(__x == __y); }
1041
1042 template<typename _Iterator>
1043 inline bool
1044 operator!=(const move_iterator<_Iterator>& __x,
1045 const move_iterator<_Iterator>& __y)
1046 { return !(__x == __y); }
1047
1048 template<typename _IteratorL, typename _IteratorR>
1049 inline bool
1050 operator<(const move_iterator<_IteratorL>& __x,
1051 const move_iterator<_IteratorR>& __y)
1052 { return __x.base() < __y.base(); }
1053
1054 template<typename _Iterator>
1055 inline bool
1056 operator<(const move_iterator<_Iterator>& __x,
1057 const move_iterator<_Iterator>& __y)
1058 { return __x.base() < __y.base(); }
1059
1060 template<typename _IteratorL, typename _IteratorR>
1061 inline bool
1062 operator<=(const move_iterator<_IteratorL>& __x,
1063 const move_iterator<_IteratorR>& __y)
1064 { return !(__y < __x); }
1065
1066 template<typename _Iterator>
1067 inline bool
1068 operator<=(const move_iterator<_Iterator>& __x,
1069 const move_iterator<_Iterator>& __y)
1070 { return !(__y < __x); }
1071
1072 template<typename _IteratorL, typename _IteratorR>
1073 inline bool
1074 operator>(const move_iterator<_IteratorL>& __x,
1075 const move_iterator<_IteratorR>& __y)
1076 { return __y < __x; }
1077
1078 template<typename _Iterator>
1079 inline bool
1080 operator>(const move_iterator<_Iterator>& __x,
1081 const move_iterator<_Iterator>& __y)
1082 { return __y < __x; }
1083
1084 template<typename _IteratorL, typename _IteratorR>
1085 inline bool
1086 operator>=(const move_iterator<_IteratorL>& __x,
1087 const move_iterator<_IteratorR>& __y)
1088 { return !(__x < __y); }
1089
1090 template<typename _Iterator>
1091 inline bool
1092 operator>=(const move_iterator<_Iterator>& __x,
1093 const move_iterator<_Iterator>& __y)
1094 { return !(__x < __y); }
1095
1096 // DR 685.
1097 template<typename _IteratorL, typename _IteratorR>
1098 inline auto
1099 operator-(const move_iterator<_IteratorL>& __x,
1100 const move_iterator<_IteratorR>& __y)
1101 -> decltype(__x.base() - __y.base())
1102 { return __x.base() - __y.base(); }
1103
1104 template<typename _Iterator>
1105 inline auto
1106 operator-(const move_iterator<_Iterator>& __x,
1107 const move_iterator<_Iterator>& __y)
1108 -> decltype(__x.base() - __y.base())
1109 { return __x.base() - __y.base(); }
1110
1111 template<typename _Iterator>
1112 inline move_iterator<_Iterator>
1113 operator+(typename move_iterator<_Iterator>::difference_type __n,
1114 const move_iterator<_Iterator>& __x)
1115 { return __x + __n; }
1116
1117 template<typename _Iterator>
1118 inline move_iterator<_Iterator>
1119 make_move_iterator(_Iterator __i)
1120 { return move_iterator<_Iterator>(__i); }
1121
1122 template<typename _Iterator, typename _ReturnType
1123 = typename conditional<__move_if_noexcept_cond
1124 <typename iterator_traits<_Iterator>::value_type>::value,
1125 _Iterator, move_iterator<_Iterator>>::type>
1126 inline _ReturnType
1127 __make_move_if_noexcept_iterator(_Iterator __i)
1128 { return _ReturnType(__i); }
1129
1130 // @} group iterators
1131
1132 _GLIBCXX_END_NAMESPACE_VERSION
1133 } // namespace
1134
1135 #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) std::make_move_iterator(_Iter)
1136 #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) \
1137 std::__make_move_if_noexcept_iterator(_Iter)
1138 #else
1139 #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) (_Iter)
1140 #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) (_Iter)
1141 #endif // __GXX_EXPERIMENTAL_CXX0X__
1142
1143 #endif