re PR libstdc++/23767 (std::vector iterator implementation wrong)
[gcc.git] / libstdc++-v3 / include / bits / stl_iterator.h
1 // Iterators -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 /*
31 *
32 * Copyright (c) 1994
33 * Hewlett-Packard Company
34 *
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
42 *
43 *
44 * Copyright (c) 1996-1998
45 * Silicon Graphics Computer Systems, Inc.
46 *
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
54 */
55
56 /** @file stl_iterator.h
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
59 *
60 * This file implements reverse_iterator, back_insert_iterator,
61 * front_insert_iterator, insert_iterator, __normal_iterator, and their
62 * supporting functions and overloaded operators.
63 */
64
65 #ifndef _ITERATOR_H
66 #define _ITERATOR_H 1
67
68 #include <bits/cpp_type_traits.h>
69
70 namespace std
71 {
72 // 24.4.1 Reverse iterators
73 /**
74 * "Bidirectional and random access iterators have corresponding reverse
75 * %iterator adaptors that iterate through the data structure in the
76 * opposite direction. They have the same signatures as the corresponding
77 * iterators. The fundamental relation between a reverse %iterator and its
78 * corresponding %iterator @c i is established by the identity:
79 * @code
80 * &*(reverse_iterator(i)) == &*(i - 1)
81 * @endcode
82 *
83 * This mapping is dictated by the fact that while there is always a
84 * pointer past the end of an array, there might not be a valid pointer
85 * before the beginning of an array." [24.4.1]/1,2
86 *
87 * Reverse iterators can be tricky and surprising at first. Their
88 * semantics make sense, however, and the trickiness is a side effect of
89 * the requirement that the iterators must be safe.
90 */
91 template<typename _Iterator>
92 class reverse_iterator
93 : public iterator<typename iterator_traits<_Iterator>::iterator_category,
94 typename iterator_traits<_Iterator>::value_type,
95 typename iterator_traits<_Iterator>::difference_type,
96 typename iterator_traits<_Iterator>::pointer,
97 typename iterator_traits<_Iterator>::reference>
98 {
99 protected:
100 _Iterator current;
101
102 public:
103 typedef _Iterator iterator_type;
104 typedef typename iterator_traits<_Iterator>::difference_type
105 difference_type;
106 typedef typename iterator_traits<_Iterator>::reference reference;
107 typedef typename iterator_traits<_Iterator>::pointer pointer;
108
109 public:
110 /**
111 * The default constructor default-initializes member @p current.
112 * If it is a pointer, that means it is zero-initialized.
113 */
114 // _GLIBCXX_RESOLVE_LIB_DEFECTS
115 // 235 No specification of default ctor for reverse_iterator
116 reverse_iterator() : current() { }
117
118 /**
119 * This %iterator will move in the opposite direction that @p x does.
120 */
121 explicit
122 reverse_iterator(iterator_type __x) : current(__x) { }
123
124 /**
125 * The copy constructor is normal.
126 */
127 reverse_iterator(const reverse_iterator& __x)
128 : current(__x.current) { }
129
130 /**
131 * A reverse_iterator across other types can be copied in the normal
132 * fashion.
133 */
134 template<typename _Iter>
135 reverse_iterator(const reverse_iterator<_Iter>& __x)
136 : current(__x.base()) { }
137
138 /**
139 * @return @c current, the %iterator used for underlying work.
140 */
141 iterator_type
142 base() const
143 { return current; }
144
145 /**
146 * @return TODO
147 *
148 * @doctodo
149 */
150 reference
151 operator*() const
152 {
153 _Iterator __tmp = current;
154 return *--__tmp;
155 }
156
157 /**
158 * @return TODO
159 *
160 * @doctodo
161 */
162 pointer
163 operator->() const
164 { return &(operator*()); }
165
166 /**
167 * @return TODO
168 *
169 * @doctodo
170 */
171 reverse_iterator&
172 operator++()
173 {
174 --current;
175 return *this;
176 }
177
178 /**
179 * @return TODO
180 *
181 * @doctodo
182 */
183 reverse_iterator
184 operator++(int)
185 {
186 reverse_iterator __tmp = *this;
187 --current;
188 return __tmp;
189 }
190
191 /**
192 * @return TODO
193 *
194 * @doctodo
195 */
196 reverse_iterator&
197 operator--()
198 {
199 ++current;
200 return *this;
201 }
202
203 /**
204 * @return TODO
205 *
206 * @doctodo
207 */
208 reverse_iterator operator--(int)
209 {
210 reverse_iterator __tmp = *this;
211 ++current;
212 return __tmp;
213 }
214
215 /**
216 * @return TODO
217 *
218 * @doctodo
219 */
220 reverse_iterator
221 operator+(difference_type __n) const
222 { return reverse_iterator(current - __n); }
223
224 /**
225 * @return TODO
226 *
227 * @doctodo
228 */
229 reverse_iterator&
230 operator+=(difference_type __n)
231 {
232 current -= __n;
233 return *this;
234 }
235
236 /**
237 * @return TODO
238 *
239 * @doctodo
240 */
241 reverse_iterator
242 operator-(difference_type __n) const
243 { return reverse_iterator(current + __n); }
244
245 /**
246 * @return TODO
247 *
248 * @doctodo
249 */
250 reverse_iterator&
251 operator-=(difference_type __n)
252 {
253 current += __n;
254 return *this;
255 }
256
257 /**
258 * @return TODO
259 *
260 * @doctodo
261 */
262 reference
263 operator[](difference_type __n) const
264 { return *(*this + __n); }
265 };
266
267 //@{
268 /**
269 * @param x A %reverse_iterator.
270 * @param y A %reverse_iterator.
271 * @return A simple bool.
272 *
273 * Reverse iterators forward many operations to their underlying base()
274 * iterators. Others are implemented in terms of one another.
275 *
276 */
277 template<typename _Iterator>
278 inline bool
279 operator==(const reverse_iterator<_Iterator>& __x,
280 const reverse_iterator<_Iterator>& __y)
281 { return __x.base() == __y.base(); }
282
283 template<typename _Iterator>
284 inline bool
285 operator<(const reverse_iterator<_Iterator>& __x,
286 const reverse_iterator<_Iterator>& __y)
287 { return __y.base() < __x.base(); }
288
289 template<typename _Iterator>
290 inline bool
291 operator!=(const reverse_iterator<_Iterator>& __x,
292 const reverse_iterator<_Iterator>& __y)
293 { return !(__x == __y); }
294
295 template<typename _Iterator>
296 inline bool
297 operator>(const reverse_iterator<_Iterator>& __x,
298 const reverse_iterator<_Iterator>& __y)
299 { return __y < __x; }
300
301 template<typename _Iterator>
302 inline bool
303 operator<=(const reverse_iterator<_Iterator>& __x,
304 const reverse_iterator<_Iterator>& __y)
305 { return !(__y < __x); }
306
307 template<typename _Iterator>
308 inline bool
309 operator>=(const reverse_iterator<_Iterator>& __x,
310 const reverse_iterator<_Iterator>& __y)
311 { return !(__x < __y); }
312
313 template<typename _Iterator>
314 inline typename reverse_iterator<_Iterator>::difference_type
315 operator-(const reverse_iterator<_Iterator>& __x,
316 const reverse_iterator<_Iterator>& __y)
317 { return __y.base() - __x.base(); }
318
319 template<typename _Iterator>
320 inline reverse_iterator<_Iterator>
321 operator+(typename reverse_iterator<_Iterator>::difference_type __n,
322 const reverse_iterator<_Iterator>& __x)
323 { return reverse_iterator<_Iterator>(__x.base() - __n); }
324 //@}
325
326 // 24.4.2.2.1 back_insert_iterator
327 /**
328 * @brief Turns assignment into insertion.
329 *
330 * These are output iterators, constructed from a container-of-T.
331 * Assigning a T to the iterator appends it to the container using
332 * push_back.
333 *
334 * Tip: Using the back_inserter function to create these iterators can
335 * save typing.
336 */
337 template<typename _Container>
338 class back_insert_iterator
339 : public iterator<output_iterator_tag, void, void, void, void>
340 {
341 protected:
342 _Container* container;
343
344 public:
345 /// A nested typedef for the type of whatever container you used.
346 typedef _Container container_type;
347
348 /// The only way to create this %iterator is with a container.
349 explicit
350 back_insert_iterator(_Container& __x) : container(&__x) { }
351
352 /**
353 * @param value An instance of whatever type
354 * container_type::const_reference is; presumably a
355 * reference-to-const T for container<T>.
356 * @return This %iterator, for chained operations.
357 *
358 * This kind of %iterator doesn't really have a "position" in the
359 * container (you can think of the position as being permanently at
360 * the end, if you like). Assigning a value to the %iterator will
361 * always append the value to the end of the container.
362 */
363 back_insert_iterator&
364 operator=(typename _Container::const_reference __value)
365 {
366 container->push_back(__value);
367 return *this;
368 }
369
370 /// Simply returns *this.
371 back_insert_iterator&
372 operator*()
373 { return *this; }
374
375 /// Simply returns *this. (This %iterator does not "move".)
376 back_insert_iterator&
377 operator++()
378 { return *this; }
379
380 /// Simply returns *this. (This %iterator does not "move".)
381 back_insert_iterator
382 operator++(int)
383 { return *this; }
384 };
385
386 /**
387 * @param x A container of arbitrary type.
388 * @return An instance of back_insert_iterator working on @p x.
389 *
390 * This wrapper function helps in creating back_insert_iterator instances.
391 * Typing the name of the %iterator requires knowing the precise full
392 * type of the container, which can be tedious and impedes generic
393 * programming. Using this function lets you take advantage of automatic
394 * template parameter deduction, making the compiler match the correct
395 * types for you.
396 */
397 template<typename _Container>
398 inline back_insert_iterator<_Container>
399 back_inserter(_Container& __x)
400 { return back_insert_iterator<_Container>(__x); }
401
402 /**
403 * @brief Turns assignment into insertion.
404 *
405 * These are output iterators, constructed from a container-of-T.
406 * Assigning a T to the iterator prepends it to the container using
407 * push_front.
408 *
409 * Tip: Using the front_inserter function to create these iterators can
410 * save typing.
411 */
412 template<typename _Container>
413 class front_insert_iterator
414 : public iterator<output_iterator_tag, void, void, void, void>
415 {
416 protected:
417 _Container* container;
418
419 public:
420 /// A nested typedef for the type of whatever container you used.
421 typedef _Container container_type;
422
423 /// The only way to create this %iterator is with a container.
424 explicit front_insert_iterator(_Container& __x) : container(&__x) { }
425
426 /**
427 * @param value An instance of whatever type
428 * container_type::const_reference is; presumably a
429 * reference-to-const T for container<T>.
430 * @return This %iterator, for chained operations.
431 *
432 * This kind of %iterator doesn't really have a "position" in the
433 * container (you can think of the position as being permanently at
434 * the front, if you like). Assigning a value to the %iterator will
435 * always prepend the value to the front of the container.
436 */
437 front_insert_iterator&
438 operator=(typename _Container::const_reference __value)
439 {
440 container->push_front(__value);
441 return *this;
442 }
443
444 /// Simply returns *this.
445 front_insert_iterator&
446 operator*()
447 { return *this; }
448
449 /// Simply returns *this. (This %iterator does not "move".)
450 front_insert_iterator&
451 operator++()
452 { return *this; }
453
454 /// Simply returns *this. (This %iterator does not "move".)
455 front_insert_iterator
456 operator++(int)
457 { return *this; }
458 };
459
460 /**
461 * @param x A container of arbitrary type.
462 * @return An instance of front_insert_iterator working on @p x.
463 *
464 * This wrapper function helps in creating front_insert_iterator instances.
465 * Typing the name of the %iterator requires knowing the precise full
466 * type of the container, which can be tedious and impedes generic
467 * programming. Using this function lets you take advantage of automatic
468 * template parameter deduction, making the compiler match the correct
469 * types for you.
470 */
471 template<typename _Container>
472 inline front_insert_iterator<_Container>
473 front_inserter(_Container& __x)
474 { return front_insert_iterator<_Container>(__x); }
475
476 /**
477 * @brief Turns assignment into insertion.
478 *
479 * These are output iterators, constructed from a container-of-T.
480 * Assigning a T to the iterator inserts it in the container at the
481 * %iterator's position, rather than overwriting the value at that
482 * position.
483 *
484 * (Sequences will actually insert a @e copy of the value before the
485 * %iterator's position.)
486 *
487 * Tip: Using the inserter function to create these iterators can
488 * save typing.
489 */
490 template<typename _Container>
491 class insert_iterator
492 : public iterator<output_iterator_tag, void, void, void, void>
493 {
494 protected:
495 _Container* container;
496 typename _Container::iterator iter;
497
498 public:
499 /// A nested typedef for the type of whatever container you used.
500 typedef _Container container_type;
501
502 /**
503 * The only way to create this %iterator is with a container and an
504 * initial position (a normal %iterator into the container).
505 */
506 insert_iterator(_Container& __x, typename _Container::iterator __i)
507 : container(&__x), iter(__i) {}
508
509 /**
510 * @param value An instance of whatever type
511 * container_type::const_reference is; presumably a
512 * reference-to-const T for container<T>.
513 * @return This %iterator, for chained operations.
514 *
515 * This kind of %iterator maintains its own position in the
516 * container. Assigning a value to the %iterator will insert the
517 * value into the container at the place before the %iterator.
518 *
519 * The position is maintained such that subsequent assignments will
520 * insert values immediately after one another. For example,
521 * @code
522 * // vector v contains A and Z
523 *
524 * insert_iterator i (v, ++v.begin());
525 * i = 1;
526 * i = 2;
527 * i = 3;
528 *
529 * // vector v contains A, 1, 2, 3, and Z
530 * @endcode
531 */
532 insert_iterator&
533 operator=(const typename _Container::const_reference __value)
534 {
535 iter = container->insert(iter, __value);
536 ++iter;
537 return *this;
538 }
539
540 /// Simply returns *this.
541 insert_iterator&
542 operator*()
543 { return *this; }
544
545 /// Simply returns *this. (This %iterator does not "move".)
546 insert_iterator&
547 operator++()
548 { return *this; }
549
550 /// Simply returns *this. (This %iterator does not "move".)
551 insert_iterator&
552 operator++(int)
553 { return *this; }
554 };
555
556 /**
557 * @param x A container of arbitrary type.
558 * @return An instance of insert_iterator working on @p x.
559 *
560 * This wrapper function helps in creating insert_iterator instances.
561 * Typing the name of the %iterator requires knowing the precise full
562 * type of the container, which can be tedious and impedes generic
563 * programming. Using this function lets you take advantage of automatic
564 * template parameter deduction, making the compiler match the correct
565 * types for you.
566 */
567 template<typename _Container, typename _Iterator>
568 inline insert_iterator<_Container>
569 inserter(_Container& __x, _Iterator __i)
570 {
571 return insert_iterator<_Container>(__x,
572 typename _Container::iterator(__i));
573 }
574 } // namespace std
575
576 namespace __gnu_cxx
577 {
578 // This iterator adapter is 'normal' in the sense that it does not
579 // change the semantics of any of the operators of its iterator
580 // parameter. Its primary purpose is to convert an iterator that is
581 // not a class, e.g. a pointer, into an iterator that is a class.
582 // The _Container parameter exists solely so that different containers
583 // using this template can instantiate different types, even if the
584 // _Iterator parameter is the same.
585 using std::iterator_traits;
586 using std::iterator;
587 template<typename _Iterator, typename _Container>
588 class __normal_iterator
589 {
590 protected:
591 _Iterator _M_current;
592
593 public:
594 typedef typename iterator_traits<_Iterator>::iterator_category
595 iterator_category;
596 typedef typename iterator_traits<_Iterator>::value_type value_type;
597 typedef typename iterator_traits<_Iterator>::difference_type
598 difference_type;
599 typedef typename iterator_traits<_Iterator>::reference reference;
600 typedef typename iterator_traits<_Iterator>::pointer pointer;
601
602 __normal_iterator() : _M_current(_Iterator()) { }
603
604 explicit
605 __normal_iterator(const _Iterator& __i) : _M_current(__i) { }
606
607 // Allow iterator to const_iterator conversion
608 template<typename _Iter>
609 __normal_iterator(const __normal_iterator<_Iter,
610 typename std::__enable_if<_Container,
611 (std::__are_same<_Iter,
612 typename _Container::pointer>::__value)
613 >::__type>& __i)
614 : _M_current(__i.base()) { }
615
616 // Forward iterator requirements
617 reference
618 operator*() const
619 { return *_M_current; }
620
621 pointer
622 operator->() const
623 { return _M_current; }
624
625 __normal_iterator&
626 operator++()
627 {
628 ++_M_current;
629 return *this;
630 }
631
632 __normal_iterator
633 operator++(int)
634 { return __normal_iterator(_M_current++); }
635
636 // Bidirectional iterator requirements
637 __normal_iterator&
638 operator--()
639 {
640 --_M_current;
641 return *this;
642 }
643
644 __normal_iterator
645 operator--(int)
646 { return __normal_iterator(_M_current--); }
647
648 // Random access iterator requirements
649 reference
650 operator[](const difference_type& __n) const
651 { return _M_current[__n]; }
652
653 __normal_iterator&
654 operator+=(const difference_type& __n)
655 { _M_current += __n; return *this; }
656
657 __normal_iterator
658 operator+(const difference_type& __n) const
659 { return __normal_iterator(_M_current + __n); }
660
661 __normal_iterator&
662 operator-=(const difference_type& __n)
663 { _M_current -= __n; return *this; }
664
665 __normal_iterator
666 operator-(const difference_type& __n) const
667 { return __normal_iterator(_M_current - __n); }
668
669 const _Iterator&
670 base() const
671 { return _M_current; }
672 };
673
674 // Note: In what follows, the left- and right-hand-side iterators are
675 // allowed to vary in types (conceptually in cv-qualification) so that
676 // comparaison between cv-qualified and non-cv-qualified iterators be
677 // valid. However, the greedy and unfriendly operators in std::rel_ops
678 // will make overload resolution ambiguous (when in scope) if we don't
679 // provide overloads whose operands are of the same type. Can someone
680 // remind me what generic programming is about? -- Gaby
681
682 // Forward iterator requirements
683 template<typename _IteratorL, typename _IteratorR, typename _Container>
684 inline bool
685 operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
686 const __normal_iterator<_IteratorR, _Container>& __rhs)
687 { return __lhs.base() == __rhs.base(); }
688
689 template<typename _Iterator, typename _Container>
690 inline bool
691 operator==(const __normal_iterator<_Iterator, _Container>& __lhs,
692 const __normal_iterator<_Iterator, _Container>& __rhs)
693 { return __lhs.base() == __rhs.base(); }
694
695 template<typename _IteratorL, typename _IteratorR, typename _Container>
696 inline bool
697 operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs,
698 const __normal_iterator<_IteratorR, _Container>& __rhs)
699 { return __lhs.base() != __rhs.base(); }
700
701 template<typename _Iterator, typename _Container>
702 inline bool
703 operator!=(const __normal_iterator<_Iterator, _Container>& __lhs,
704 const __normal_iterator<_Iterator, _Container>& __rhs)
705 { return __lhs.base() != __rhs.base(); }
706
707 // Random access iterator requirements
708 template<typename _IteratorL, typename _IteratorR, typename _Container>
709 inline bool
710 operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
711 const __normal_iterator<_IteratorR, _Container>& __rhs)
712 { return __lhs.base() < __rhs.base(); }
713
714 template<typename _Iterator, typename _Container>
715 inline bool
716 operator<(const __normal_iterator<_Iterator, _Container>& __lhs,
717 const __normal_iterator<_Iterator, _Container>& __rhs)
718 { return __lhs.base() < __rhs.base(); }
719
720 template<typename _IteratorL, typename _IteratorR, typename _Container>
721 inline bool
722 operator>(const __normal_iterator<_IteratorL, _Container>& __lhs,
723 const __normal_iterator<_IteratorR, _Container>& __rhs)
724 { return __lhs.base() > __rhs.base(); }
725
726 template<typename _Iterator, typename _Container>
727 inline bool
728 operator>(const __normal_iterator<_Iterator, _Container>& __lhs,
729 const __normal_iterator<_Iterator, _Container>& __rhs)
730 { return __lhs.base() > __rhs.base(); }
731
732 template<typename _IteratorL, typename _IteratorR, typename _Container>
733 inline bool
734 operator<=(const __normal_iterator<_IteratorL, _Container>& __lhs,
735 const __normal_iterator<_IteratorR, _Container>& __rhs)
736 { return __lhs.base() <= __rhs.base(); }
737
738 template<typename _Iterator, typename _Container>
739 inline bool
740 operator<=(const __normal_iterator<_Iterator, _Container>& __lhs,
741 const __normal_iterator<_Iterator, _Container>& __rhs)
742 { return __lhs.base() <= __rhs.base(); }
743
744 template<typename _IteratorL, typename _IteratorR, typename _Container>
745 inline bool
746 operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs,
747 const __normal_iterator<_IteratorR, _Container>& __rhs)
748 { return __lhs.base() >= __rhs.base(); }
749
750 template<typename _Iterator, typename _Container>
751 inline bool
752 operator>=(const __normal_iterator<_Iterator, _Container>& __lhs,
753 const __normal_iterator<_Iterator, _Container>& __rhs)
754 { return __lhs.base() >= __rhs.base(); }
755
756 // _GLIBCXX_RESOLVE_LIB_DEFECTS
757 // According to the resolution of DR179 not only the various comparison
758 // operators but also operator- must accept mixed iterator/const_iterator
759 // parameters.
760 template<typename _IteratorL, typename _IteratorR, typename _Container>
761 inline typename __normal_iterator<_IteratorL, _Container>::difference_type
762 operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
763 const __normal_iterator<_IteratorR, _Container>& __rhs)
764 { return __lhs.base() - __rhs.base(); }
765
766 template<typename _Iterator, typename _Container>
767 inline __normal_iterator<_Iterator, _Container>
768 operator+(typename __normal_iterator<_Iterator, _Container>::difference_type
769 __n, const __normal_iterator<_Iterator, _Container>& __i)
770 { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); }
771 } // namespace __gnu_cxx
772
773 #endif
774
775 // Local Variables:
776 // mode:C++
777 // End: