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