libstdc++
|
00001 // Iterators -*- C++ -*- 00002 00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 00004 // 2010, 2011, 2012 00005 // Free Software Foundation, Inc. 00006 // 00007 // This file is part of the GNU ISO C++ Library. This library is free 00008 // software; you can redistribute it and/or modify it under the 00009 // terms of the GNU General Public License as published by the 00010 // Free Software Foundation; either version 3, or (at your option) 00011 // any later version. 00012 00013 // This library is distributed in the hope that it will be useful, 00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 // GNU General Public License for more details. 00017 00018 // Under Section 7 of GPL version 3, you are granted additional 00019 // permissions described in the GCC Runtime Library Exception, version 00020 // 3.1, as published by the Free Software Foundation. 00021 00022 // You should have received a copy of the GNU General Public License and 00023 // a copy of the GCC Runtime Library Exception along with this program; 00024 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00025 // <http://www.gnu.org/licenses/>. 00026 00027 /* 00028 * 00029 * Copyright (c) 1994 00030 * Hewlett-Packard Company 00031 * 00032 * Permission to use, copy, modify, distribute and sell this software 00033 * and its documentation for any purpose is hereby granted without fee, 00034 * provided that the above copyright notice appear in all copies and 00035 * that both that copyright notice and this permission notice appear 00036 * in supporting documentation. Hewlett-Packard Company makes no 00037 * representations about the suitability of this software for any 00038 * purpose. It is provided "as is" without express or implied warranty. 00039 * 00040 * 00041 * Copyright (c) 1996-1998 00042 * Silicon Graphics Computer Systems, Inc. 00043 * 00044 * Permission to use, copy, modify, distribute and sell this software 00045 * and its documentation for any purpose is hereby granted without fee, 00046 * provided that the above copyright notice appear in all copies and 00047 * that both that copyright notice and this permission notice appear 00048 * in supporting documentation. Silicon Graphics makes no 00049 * representations about the suitability of this software for any 00050 * purpose. It is provided "as is" without express or implied warranty. 00051 */ 00052 00053 /** @file bits/stl_iterator.h 00054 * This is an internal header file, included by other library headers. 00055 * Do not attempt to use it directly. @headername{iterator} 00056 * 00057 * This file implements reverse_iterator, back_insert_iterator, 00058 * front_insert_iterator, insert_iterator, __normal_iterator, and their 00059 * supporting functions and overloaded operators. 00060 */ 00061 00062 #ifndef _STL_ITERATOR_H 00063 #define _STL_ITERATOR_H 1 00064 00065 #include <bits/cpp_type_traits.h> 00066 #include <ext/type_traits.h> 00067 #include <bits/move.h> 00068 00069 namespace std _GLIBCXX_VISIBILITY(default) 00070 { 00071 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00072 00073 /** 00074 * @addtogroup iterators 00075 * @{ 00076 */ 00077 00078 // 24.4.1 Reverse iterators 00079 /** 00080 * Bidirectional and random access iterators have corresponding reverse 00081 * %iterator adaptors that iterate through the data structure in the 00082 * opposite direction. They have the same signatures as the corresponding 00083 * iterators. The fundamental relation between a reverse %iterator and its 00084 * corresponding %iterator @c i is established by the identity: 00085 * @code 00086 * &*(reverse_iterator(i)) == &*(i - 1) 00087 * @endcode 00088 * 00089 * <em>This mapping is dictated by the fact that while there is always a 00090 * pointer past the end of an array, there might not be a valid pointer 00091 * before the beginning of an array.</em> [24.4.1]/1,2 00092 * 00093 * Reverse iterators can be tricky and surprising at first. Their 00094 * semantics make sense, however, and the trickiness is a side effect of 00095 * the requirement that the iterators must be safe. 00096 */ 00097 template<typename _Iterator> 00098 class reverse_iterator 00099 : public iterator<typename iterator_traits<_Iterator>::iterator_category, 00100 typename iterator_traits<_Iterator>::value_type, 00101 typename iterator_traits<_Iterator>::difference_type, 00102 typename iterator_traits<_Iterator>::pointer, 00103 typename iterator_traits<_Iterator>::reference> 00104 { 00105 protected: 00106 _Iterator current; 00107 00108 typedef iterator_traits<_Iterator> __traits_type; 00109 00110 public: 00111 typedef _Iterator iterator_type; 00112 typedef typename __traits_type::difference_type difference_type; 00113 typedef typename __traits_type::pointer pointer; 00114 typedef typename __traits_type::reference reference; 00115 00116 /** 00117 * The default constructor value-initializes member @p current. 00118 * If it is a pointer, that means it is zero-initialized. 00119 */ 00120 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00121 // 235 No specification of default ctor for reverse_iterator 00122 reverse_iterator() : current() { } 00123 00124 /** 00125 * This %iterator will move in the opposite direction that @p x does. 00126 */ 00127 explicit 00128 reverse_iterator(iterator_type __x) : current(__x) { } 00129 00130 /** 00131 * The copy constructor is normal. 00132 */ 00133 reverse_iterator(const reverse_iterator& __x) 00134 : current(__x.current) { } 00135 00136 /** 00137 * A %reverse_iterator across other types can be copied if the 00138 * underlying %iterator can be converted to the type of @c current. 00139 */ 00140 template<typename _Iter> 00141 reverse_iterator(const reverse_iterator<_Iter>& __x) 00142 : current(__x.base()) { } 00143 00144 /** 00145 * @return @c current, the %iterator used for underlying work. 00146 */ 00147 iterator_type 00148 base() const 00149 { return current; } 00150 00151 /** 00152 * @return A reference to the value at @c --current 00153 * 00154 * This requires that @c --current is dereferenceable. 00155 * 00156 * @warning This implementation requires that for an iterator of the 00157 * underlying iterator type, @c x, a reference obtained by 00158 * @c *x remains valid after @c x has been modified or 00159 * destroyed. This is a bug: http://gcc.gnu.org/PR51823 00160 */ 00161 reference 00162 operator*() const 00163 { 00164 _Iterator __tmp = current; 00165 return *--__tmp; 00166 } 00167 00168 /** 00169 * @return A pointer to the value at @c --current 00170 * 00171 * This requires that @c --current is dereferenceable. 00172 */ 00173 pointer 00174 operator->() const 00175 { return &(operator*()); } 00176 00177 /** 00178 * @return @c *this 00179 * 00180 * Decrements the underlying iterator. 00181 */ 00182 reverse_iterator& 00183 operator++() 00184 { 00185 --current; 00186 return *this; 00187 } 00188 00189 /** 00190 * @return The original value of @c *this 00191 * 00192 * Decrements the underlying iterator. 00193 */ 00194 reverse_iterator 00195 operator++(int) 00196 { 00197 reverse_iterator __tmp = *this; 00198 --current; 00199 return __tmp; 00200 } 00201 00202 /** 00203 * @return @c *this 00204 * 00205 * Increments the underlying iterator. 00206 */ 00207 reverse_iterator& 00208 operator--() 00209 { 00210 ++current; 00211 return *this; 00212 } 00213 00214 /** 00215 * @return A reverse_iterator with the previous value of @c *this 00216 * 00217 * Increments the underlying iterator. 00218 */ 00219 reverse_iterator 00220 operator--(int) 00221 { 00222 reverse_iterator __tmp = *this; 00223 ++current; 00224 return __tmp; 00225 } 00226 00227 /** 00228 * @return A reverse_iterator that refers to @c current - @a __n 00229 * 00230 * The underlying iterator must be a Random Access Iterator. 00231 */ 00232 reverse_iterator 00233 operator+(difference_type __n) const 00234 { return reverse_iterator(current - __n); } 00235 00236 /** 00237 * @return *this 00238 * 00239 * Moves the underlying iterator backwards @a __n steps. 00240 * The underlying iterator must be a Random Access Iterator. 00241 */ 00242 reverse_iterator& 00243 operator+=(difference_type __n) 00244 { 00245 current -= __n; 00246 return *this; 00247 } 00248 00249 /** 00250 * @return A reverse_iterator that refers to @c current - @a __n 00251 * 00252 * The underlying iterator must be a Random Access Iterator. 00253 */ 00254 reverse_iterator 00255 operator-(difference_type __n) const 00256 { return reverse_iterator(current + __n); } 00257 00258 /** 00259 * @return *this 00260 * 00261 * Moves the underlying iterator forwards @a __n steps. 00262 * The underlying iterator must be a Random Access Iterator. 00263 */ 00264 reverse_iterator& 00265 operator-=(difference_type __n) 00266 { 00267 current += __n; 00268 return *this; 00269 } 00270 00271 /** 00272 * @return The value at @c current - @a __n - 1 00273 * 00274 * The underlying iterator must be a Random Access Iterator. 00275 */ 00276 reference 00277 operator[](difference_type __n) const 00278 { return *(*this + __n); } 00279 }; 00280 00281 //@{ 00282 /** 00283 * @param __x A %reverse_iterator. 00284 * @param __y A %reverse_iterator. 00285 * @return A simple bool. 00286 * 00287 * Reverse iterators forward many operations to their underlying base() 00288 * iterators. Others are implemented in terms of one another. 00289 * 00290 */ 00291 template<typename _Iterator> 00292 inline bool 00293 operator==(const reverse_iterator<_Iterator>& __x, 00294 const reverse_iterator<_Iterator>& __y) 00295 { return __x.base() == __y.base(); } 00296 00297 template<typename _Iterator> 00298 inline bool 00299 operator<(const reverse_iterator<_Iterator>& __x, 00300 const reverse_iterator<_Iterator>& __y) 00301 { return __y.base() < __x.base(); } 00302 00303 template<typename _Iterator> 00304 inline bool 00305 operator!=(const reverse_iterator<_Iterator>& __x, 00306 const reverse_iterator<_Iterator>& __y) 00307 { return !(__x == __y); } 00308 00309 template<typename _Iterator> 00310 inline bool 00311 operator>(const reverse_iterator<_Iterator>& __x, 00312 const reverse_iterator<_Iterator>& __y) 00313 { return __y < __x; } 00314 00315 template<typename _Iterator> 00316 inline bool 00317 operator<=(const reverse_iterator<_Iterator>& __x, 00318 const reverse_iterator<_Iterator>& __y) 00319 { return !(__y < __x); } 00320 00321 template<typename _Iterator> 00322 inline bool 00323 operator>=(const reverse_iterator<_Iterator>& __x, 00324 const reverse_iterator<_Iterator>& __y) 00325 { return !(__x < __y); } 00326 00327 template<typename _Iterator> 00328 inline typename reverse_iterator<_Iterator>::difference_type 00329 operator-(const reverse_iterator<_Iterator>& __x, 00330 const reverse_iterator<_Iterator>& __y) 00331 { return __y.base() - __x.base(); } 00332 00333 template<typename _Iterator> 00334 inline reverse_iterator<_Iterator> 00335 operator+(typename reverse_iterator<_Iterator>::difference_type __n, 00336 const reverse_iterator<_Iterator>& __x) 00337 { return reverse_iterator<_Iterator>(__x.base() - __n); } 00338 00339 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00340 // DR 280. Comparison of reverse_iterator to const reverse_iterator. 00341 template<typename _IteratorL, typename _IteratorR> 00342 inline bool 00343 operator==(const reverse_iterator<_IteratorL>& __x, 00344 const reverse_iterator<_IteratorR>& __y) 00345 { return __x.base() == __y.base(); } 00346 00347 template<typename _IteratorL, typename _IteratorR> 00348 inline bool 00349 operator<(const reverse_iterator<_IteratorL>& __x, 00350 const reverse_iterator<_IteratorR>& __y) 00351 { return __y.base() < __x.base(); } 00352 00353 template<typename _IteratorL, typename _IteratorR> 00354 inline bool 00355 operator!=(const reverse_iterator<_IteratorL>& __x, 00356 const reverse_iterator<_IteratorR>& __y) 00357 { return !(__x == __y); } 00358 00359 template<typename _IteratorL, typename _IteratorR> 00360 inline bool 00361 operator>(const reverse_iterator<_IteratorL>& __x, 00362 const reverse_iterator<_IteratorR>& __y) 00363 { return __y < __x; } 00364 00365 template<typename _IteratorL, typename _IteratorR> 00366 inline bool 00367 operator<=(const reverse_iterator<_IteratorL>& __x, 00368 const reverse_iterator<_IteratorR>& __y) 00369 { return !(__y < __x); } 00370 00371 template<typename _IteratorL, typename _IteratorR> 00372 inline bool 00373 operator>=(const reverse_iterator<_IteratorL>& __x, 00374 const reverse_iterator<_IteratorR>& __y) 00375 { return !(__x < __y); } 00376 00377 template<typename _IteratorL, typename _IteratorR> 00378 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00379 // DR 685. 00380 inline auto 00381 operator-(const reverse_iterator<_IteratorL>& __x, 00382 const reverse_iterator<_IteratorR>& __y) 00383 -> decltype(__y.base() - __x.base()) 00384 #else 00385 inline typename reverse_iterator<_IteratorL>::difference_type 00386 operator-(const reverse_iterator<_IteratorL>& __x, 00387 const reverse_iterator<_IteratorR>& __y) 00388 #endif 00389 { return __y.base() - __x.base(); } 00390 //@} 00391 00392 // 24.4.2.2.1 back_insert_iterator 00393 /** 00394 * @brief Turns assignment into insertion. 00395 * 00396 * These are output iterators, constructed from a container-of-T. 00397 * Assigning a T to the iterator appends it to the container using 00398 * push_back. 00399 * 00400 * Tip: Using the back_inserter function to create these iterators can 00401 * save typing. 00402 */ 00403 template<typename _Container> 00404 class back_insert_iterator 00405 : public iterator<output_iterator_tag, void, void, void, void> 00406 { 00407 protected: 00408 _Container* container; 00409 00410 public: 00411 /// A nested typedef for the type of whatever container you used. 00412 typedef _Container container_type; 00413 00414 /// The only way to create this %iterator is with a container. 00415 explicit 00416 back_insert_iterator(_Container& __x) : container(&__x) { } 00417 00418 /** 00419 * @param __value An instance of whatever type 00420 * container_type::const_reference is; presumably a 00421 * reference-to-const T for container<T>. 00422 * @return This %iterator, for chained operations. 00423 * 00424 * This kind of %iterator doesn't really have a @a position in the 00425 * container (you can think of the position as being permanently at 00426 * the end, if you like). Assigning a value to the %iterator will 00427 * always append the value to the end of the container. 00428 */ 00429 #ifndef __GXX_EXPERIMENTAL_CXX0X__ 00430 back_insert_iterator& 00431 operator=(typename _Container::const_reference __value) 00432 { 00433 container->push_back(__value); 00434 return *this; 00435 } 00436 #else 00437 back_insert_iterator& 00438 operator=(const typename _Container::value_type& __value) 00439 { 00440 container->push_back(__value); 00441 return *this; 00442 } 00443 00444 back_insert_iterator& 00445 operator=(typename _Container::value_type&& __value) 00446 { 00447 container->push_back(std::move(__value)); 00448 return *this; 00449 } 00450 #endif 00451 00452 /// Simply returns *this. 00453 back_insert_iterator& 00454 operator*() 00455 { return *this; } 00456 00457 /// Simply returns *this. (This %iterator does not @a move.) 00458 back_insert_iterator& 00459 operator++() 00460 { return *this; } 00461 00462 /// Simply returns *this. (This %iterator does not @a move.) 00463 back_insert_iterator 00464 operator++(int) 00465 { return *this; } 00466 }; 00467 00468 /** 00469 * @param __x A container of arbitrary type. 00470 * @return An instance of back_insert_iterator working on @p __x. 00471 * 00472 * This wrapper function helps in creating back_insert_iterator instances. 00473 * Typing the name of the %iterator requires knowing the precise full 00474 * type of the container, which can be tedious and impedes generic 00475 * programming. Using this function lets you take advantage of automatic 00476 * template parameter deduction, making the compiler match the correct 00477 * types for you. 00478 */ 00479 template<typename _Container> 00480 inline back_insert_iterator<_Container> 00481 back_inserter(_Container& __x) 00482 { return back_insert_iterator<_Container>(__x); } 00483 00484 /** 00485 * @brief Turns assignment into insertion. 00486 * 00487 * These are output iterators, constructed from a container-of-T. 00488 * Assigning a T to the iterator prepends it to the container using 00489 * push_front. 00490 * 00491 * Tip: Using the front_inserter function to create these iterators can 00492 * save typing. 00493 */ 00494 template<typename _Container> 00495 class front_insert_iterator 00496 : public iterator<output_iterator_tag, void, void, void, void> 00497 { 00498 protected: 00499 _Container* container; 00500 00501 public: 00502 /// A nested typedef for the type of whatever container you used. 00503 typedef _Container container_type; 00504 00505 /// The only way to create this %iterator is with a container. 00506 explicit front_insert_iterator(_Container& __x) : container(&__x) { } 00507 00508 /** 00509 * @param __value An instance of whatever type 00510 * container_type::const_reference is; presumably a 00511 * reference-to-const T for container<T>. 00512 * @return This %iterator, for chained operations. 00513 * 00514 * This kind of %iterator doesn't really have a @a position in the 00515 * container (you can think of the position as being permanently at 00516 * the front, if you like). Assigning a value to the %iterator will 00517 * always prepend the value to the front of the container. 00518 */ 00519 #ifndef __GXX_EXPERIMENTAL_CXX0X__ 00520 front_insert_iterator& 00521 operator=(typename _Container::const_reference __value) 00522 { 00523 container->push_front(__value); 00524 return *this; 00525 } 00526 #else 00527 front_insert_iterator& 00528 operator=(const typename _Container::value_type& __value) 00529 { 00530 container->push_front(__value); 00531 return *this; 00532 } 00533 00534 front_insert_iterator& 00535 operator=(typename _Container::value_type&& __value) 00536 { 00537 container->push_front(std::move(__value)); 00538 return *this; 00539 } 00540 #endif 00541 00542 /// Simply returns *this. 00543 front_insert_iterator& 00544 operator*() 00545 { return *this; } 00546 00547 /// Simply returns *this. (This %iterator does not @a move.) 00548 front_insert_iterator& 00549 operator++() 00550 { return *this; } 00551 00552 /// Simply returns *this. (This %iterator does not @a move.) 00553 front_insert_iterator 00554 operator++(int) 00555 { return *this; } 00556 }; 00557 00558 /** 00559 * @param __x A container of arbitrary type. 00560 * @return An instance of front_insert_iterator working on @p x. 00561 * 00562 * This wrapper function helps in creating front_insert_iterator instances. 00563 * Typing the name of the %iterator requires knowing the precise full 00564 * type of the container, which can be tedious and impedes generic 00565 * programming. Using this function lets you take advantage of automatic 00566 * template parameter deduction, making the compiler match the correct 00567 * types for you. 00568 */ 00569 template<typename _Container> 00570 inline front_insert_iterator<_Container> 00571 front_inserter(_Container& __x) 00572 { return front_insert_iterator<_Container>(__x); } 00573 00574 /** 00575 * @brief Turns assignment into insertion. 00576 * 00577 * These are output iterators, constructed from a container-of-T. 00578 * Assigning a T to the iterator inserts it in the container at the 00579 * %iterator's position, rather than overwriting the value at that 00580 * position. 00581 * 00582 * (Sequences will actually insert a @e copy of the value before the 00583 * %iterator's position.) 00584 * 00585 * Tip: Using the inserter function to create these iterators can 00586 * save typing. 00587 */ 00588 template<typename _Container> 00589 class insert_iterator 00590 : public iterator<output_iterator_tag, void, void, void, void> 00591 { 00592 protected: 00593 _Container* container; 00594 typename _Container::iterator iter; 00595 00596 public: 00597 /// A nested typedef for the type of whatever container you used. 00598 typedef _Container container_type; 00599 00600 /** 00601 * The only way to create this %iterator is with a container and an 00602 * initial position (a normal %iterator into the container). 00603 */ 00604 insert_iterator(_Container& __x, typename _Container::iterator __i) 00605 : container(&__x), iter(__i) {} 00606 00607 /** 00608 * @param __value An instance of whatever type 00609 * container_type::const_reference is; presumably a 00610 * reference-to-const T for container<T>. 00611 * @return This %iterator, for chained operations. 00612 * 00613 * This kind of %iterator maintains its own position in the 00614 * container. Assigning a value to the %iterator will insert the 00615 * value into the container at the place before the %iterator. 00616 * 00617 * The position is maintained such that subsequent assignments will 00618 * insert values immediately after one another. For example, 00619 * @code 00620 * // vector v contains A and Z 00621 * 00622 * insert_iterator i (v, ++v.begin()); 00623 * i = 1; 00624 * i = 2; 00625 * i = 3; 00626 * 00627 * // vector v contains A, 1, 2, 3, and Z 00628 * @endcode 00629 */ 00630 #ifndef __GXX_EXPERIMENTAL_CXX0X__ 00631 insert_iterator& 00632 operator=(typename _Container::const_reference __value) 00633 { 00634 iter = container->insert(iter, __value); 00635 ++iter; 00636 return *this; 00637 } 00638 #else 00639 insert_iterator& 00640 operator=(const typename _Container::value_type& __value) 00641 { 00642 iter = container->insert(iter, __value); 00643 ++iter; 00644 return *this; 00645 } 00646 00647 insert_iterator& 00648 operator=(typename _Container::value_type&& __value) 00649 { 00650 iter = container->insert(iter, std::move(__value)); 00651 ++iter; 00652 return *this; 00653 } 00654 #endif 00655 00656 /// Simply returns *this. 00657 insert_iterator& 00658 operator*() 00659 { return *this; } 00660 00661 /// Simply returns *this. (This %iterator does not @a move.) 00662 insert_iterator& 00663 operator++() 00664 { return *this; } 00665 00666 /// Simply returns *this. (This %iterator does not @a move.) 00667 insert_iterator& 00668 operator++(int) 00669 { return *this; } 00670 }; 00671 00672 /** 00673 * @param __x A container of arbitrary type. 00674 * @return An instance of insert_iterator working on @p __x. 00675 * 00676 * This wrapper function helps in creating insert_iterator instances. 00677 * Typing the name of the %iterator requires knowing the precise full 00678 * type of the container, which can be tedious and impedes generic 00679 * programming. Using this function lets you take advantage of automatic 00680 * template parameter deduction, making the compiler match the correct 00681 * types for you. 00682 */ 00683 template<typename _Container, typename _Iterator> 00684 inline insert_iterator<_Container> 00685 inserter(_Container& __x, _Iterator __i) 00686 { 00687 return insert_iterator<_Container>(__x, 00688 typename _Container::iterator(__i)); 00689 } 00690 00691 // @} group iterators 00692 00693 _GLIBCXX_END_NAMESPACE_VERSION 00694 } // namespace 00695 00696 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 00697 { 00698 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00699 00700 // This iterator adapter is @a normal in the sense that it does not 00701 // change the semantics of any of the operators of its iterator 00702 // parameter. Its primary purpose is to convert an iterator that is 00703 // not a class, e.g. a pointer, into an iterator that is a class. 00704 // The _Container parameter exists solely so that different containers 00705 // using this template can instantiate different types, even if the 00706 // _Iterator parameter is the same. 00707 using std::iterator_traits; 00708 using std::iterator; 00709 template<typename _Iterator, typename _Container> 00710 class __normal_iterator 00711 { 00712 protected: 00713 _Iterator _M_current; 00714 00715 typedef iterator_traits<_Iterator> __traits_type; 00716 00717 public: 00718 typedef _Iterator iterator_type; 00719 typedef typename __traits_type::iterator_category iterator_category; 00720 typedef typename __traits_type::value_type value_type; 00721 typedef typename __traits_type::difference_type difference_type; 00722 typedef typename __traits_type::reference reference; 00723 typedef typename __traits_type::pointer pointer; 00724 00725 _GLIBCXX_CONSTEXPR __normal_iterator() : _M_current(_Iterator()) { } 00726 00727 explicit 00728 __normal_iterator(const _Iterator& __i) : _M_current(__i) { } 00729 00730 // Allow iterator to const_iterator conversion 00731 template<typename _Iter> 00732 __normal_iterator(const __normal_iterator<_Iter, 00733 typename __enable_if< 00734 (std::__are_same<_Iter, typename _Container::pointer>::__value), 00735 _Container>::__type>& __i) 00736 : _M_current(__i.base()) { } 00737 00738 // Forward iterator requirements 00739 reference 00740 operator*() const 00741 { return *_M_current; } 00742 00743 pointer 00744 operator->() const 00745 { return _M_current; } 00746 00747 __normal_iterator& 00748 operator++() 00749 { 00750 ++_M_current; 00751 return *this; 00752 } 00753 00754 __normal_iterator 00755 operator++(int) 00756 { return __normal_iterator(_M_current++); } 00757 00758 // Bidirectional iterator requirements 00759 __normal_iterator& 00760 operator--() 00761 { 00762 --_M_current; 00763 return *this; 00764 } 00765 00766 __normal_iterator 00767 operator--(int) 00768 { return __normal_iterator(_M_current--); } 00769 00770 // Random access iterator requirements 00771 reference 00772 operator[](const difference_type& __n) const 00773 { return _M_current[__n]; } 00774 00775 __normal_iterator& 00776 operator+=(const difference_type& __n) 00777 { _M_current += __n; return *this; } 00778 00779 __normal_iterator 00780 operator+(const difference_type& __n) const 00781 { return __normal_iterator(_M_current + __n); } 00782 00783 __normal_iterator& 00784 operator-=(const difference_type& __n) 00785 { _M_current -= __n; return *this; } 00786 00787 __normal_iterator 00788 operator-(const difference_type& __n) const 00789 { return __normal_iterator(_M_current - __n); } 00790 00791 const _Iterator& 00792 base() const 00793 { return _M_current; } 00794 }; 00795 00796 // Note: In what follows, the left- and right-hand-side iterators are 00797 // allowed to vary in types (conceptually in cv-qualification) so that 00798 // comparison between cv-qualified and non-cv-qualified iterators be 00799 // valid. However, the greedy and unfriendly operators in std::rel_ops 00800 // will make overload resolution ambiguous (when in scope) if we don't 00801 // provide overloads whose operands are of the same type. Can someone 00802 // remind me what generic programming is about? -- Gaby 00803 00804 // Forward iterator requirements 00805 template<typename _IteratorL, typename _IteratorR, typename _Container> 00806 inline bool 00807 operator==(const __normal_iterator<_IteratorL, _Container>& __lhs, 00808 const __normal_iterator<_IteratorR, _Container>& __rhs) 00809 { return __lhs.base() == __rhs.base(); } 00810 00811 template<typename _Iterator, typename _Container> 00812 inline bool 00813 operator==(const __normal_iterator<_Iterator, _Container>& __lhs, 00814 const __normal_iterator<_Iterator, _Container>& __rhs) 00815 { return __lhs.base() == __rhs.base(); } 00816 00817 template<typename _IteratorL, typename _IteratorR, typename _Container> 00818 inline bool 00819 operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs, 00820 const __normal_iterator<_IteratorR, _Container>& __rhs) 00821 { return __lhs.base() != __rhs.base(); } 00822 00823 template<typename _Iterator, typename _Container> 00824 inline bool 00825 operator!=(const __normal_iterator<_Iterator, _Container>& __lhs, 00826 const __normal_iterator<_Iterator, _Container>& __rhs) 00827 { return __lhs.base() != __rhs.base(); } 00828 00829 // Random access iterator requirements 00830 template<typename _IteratorL, typename _IteratorR, typename _Container> 00831 inline bool 00832 operator<(const __normal_iterator<_IteratorL, _Container>& __lhs, 00833 const __normal_iterator<_IteratorR, _Container>& __rhs) 00834 { return __lhs.base() < __rhs.base(); } 00835 00836 template<typename _Iterator, typename _Container> 00837 inline bool 00838 operator<(const __normal_iterator<_Iterator, _Container>& __lhs, 00839 const __normal_iterator<_Iterator, _Container>& __rhs) 00840 { return __lhs.base() < __rhs.base(); } 00841 00842 template<typename _IteratorL, typename _IteratorR, typename _Container> 00843 inline bool 00844 operator>(const __normal_iterator<_IteratorL, _Container>& __lhs, 00845 const __normal_iterator<_IteratorR, _Container>& __rhs) 00846 { return __lhs.base() > __rhs.base(); } 00847 00848 template<typename _Iterator, typename _Container> 00849 inline bool 00850 operator>(const __normal_iterator<_Iterator, _Container>& __lhs, 00851 const __normal_iterator<_Iterator, _Container>& __rhs) 00852 { return __lhs.base() > __rhs.base(); } 00853 00854 template<typename _IteratorL, typename _IteratorR, typename _Container> 00855 inline bool 00856 operator<=(const __normal_iterator<_IteratorL, _Container>& __lhs, 00857 const __normal_iterator<_IteratorR, _Container>& __rhs) 00858 { return __lhs.base() <= __rhs.base(); } 00859 00860 template<typename _Iterator, typename _Container> 00861 inline bool 00862 operator<=(const __normal_iterator<_Iterator, _Container>& __lhs, 00863 const __normal_iterator<_Iterator, _Container>& __rhs) 00864 { return __lhs.base() <= __rhs.base(); } 00865 00866 template<typename _IteratorL, typename _IteratorR, typename _Container> 00867 inline bool 00868 operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs, 00869 const __normal_iterator<_IteratorR, _Container>& __rhs) 00870 { return __lhs.base() >= __rhs.base(); } 00871 00872 template<typename _Iterator, typename _Container> 00873 inline bool 00874 operator>=(const __normal_iterator<_Iterator, _Container>& __lhs, 00875 const __normal_iterator<_Iterator, _Container>& __rhs) 00876 { return __lhs.base() >= __rhs.base(); } 00877 00878 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00879 // According to the resolution of DR179 not only the various comparison 00880 // operators but also operator- must accept mixed iterator/const_iterator 00881 // parameters. 00882 template<typename _IteratorL, typename _IteratorR, typename _Container> 00883 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00884 // DR 685. 00885 inline auto 00886 operator-(const __normal_iterator<_IteratorL, _Container>& __lhs, 00887 const __normal_iterator<_IteratorR, _Container>& __rhs) 00888 -> decltype(__lhs.base() - __rhs.base()) 00889 #else 00890 inline typename __normal_iterator<_IteratorL, _Container>::difference_type 00891 operator-(const __normal_iterator<_IteratorL, _Container>& __lhs, 00892 const __normal_iterator<_IteratorR, _Container>& __rhs) 00893 #endif 00894 { return __lhs.base() - __rhs.base(); } 00895 00896 template<typename _Iterator, typename _Container> 00897 inline typename __normal_iterator<_Iterator, _Container>::difference_type 00898 operator-(const __normal_iterator<_Iterator, _Container>& __lhs, 00899 const __normal_iterator<_Iterator, _Container>& __rhs) 00900 { return __lhs.base() - __rhs.base(); } 00901 00902 template<typename _Iterator, typename _Container> 00903 inline __normal_iterator<_Iterator, _Container> 00904 operator+(typename __normal_iterator<_Iterator, _Container>::difference_type 00905 __n, const __normal_iterator<_Iterator, _Container>& __i) 00906 { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); } 00907 00908 _GLIBCXX_END_NAMESPACE_VERSION 00909 } // namespace 00910 00911 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00912 00913 namespace std _GLIBCXX_VISIBILITY(default) 00914 { 00915 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00916 00917 /** 00918 * @addtogroup iterators 00919 * @{ 00920 */ 00921 00922 // 24.4.3 Move iterators 00923 /** 00924 * Class template move_iterator is an iterator adapter with the same 00925 * behavior as the underlying iterator except that its dereference 00926 * operator implicitly converts the value returned by the underlying 00927 * iterator's dereference operator to an rvalue reference. Some 00928 * generic algorithms can be called with move iterators to replace 00929 * copying with moving. 00930 */ 00931 template<typename _Iterator> 00932 class move_iterator 00933 { 00934 protected: 00935 _Iterator _M_current; 00936 00937 typedef iterator_traits<_Iterator> __traits_type; 00938 00939 public: 00940 typedef _Iterator iterator_type; 00941 typedef typename __traits_type::iterator_category iterator_category; 00942 typedef typename __traits_type::value_type value_type; 00943 typedef typename __traits_type::difference_type difference_type; 00944 // NB: DR 680. 00945 typedef _Iterator pointer; 00946 typedef value_type&& reference; 00947 00948 move_iterator() 00949 : _M_current() { } 00950 00951 explicit 00952 move_iterator(iterator_type __i) 00953 : _M_current(__i) { } 00954 00955 template<typename _Iter> 00956 move_iterator(const move_iterator<_Iter>& __i) 00957 : _M_current(__i.base()) { } 00958 00959 iterator_type 00960 base() const 00961 { return _M_current; } 00962 00963 reference 00964 operator*() const 00965 { return std::move(*_M_current); } 00966 00967 pointer 00968 operator->() const 00969 { return _M_current; } 00970 00971 move_iterator& 00972 operator++() 00973 { 00974 ++_M_current; 00975 return *this; 00976 } 00977 00978 move_iterator 00979 operator++(int) 00980 { 00981 move_iterator __tmp = *this; 00982 ++_M_current; 00983 return __tmp; 00984 } 00985 00986 move_iterator& 00987 operator--() 00988 { 00989 --_M_current; 00990 return *this; 00991 } 00992 00993 move_iterator 00994 operator--(int) 00995 { 00996 move_iterator __tmp = *this; 00997 --_M_current; 00998 return __tmp; 00999 } 01000 01001 move_iterator 01002 operator+(difference_type __n) const 01003 { return move_iterator(_M_current + __n); } 01004 01005 move_iterator& 01006 operator+=(difference_type __n) 01007 { 01008 _M_current += __n; 01009 return *this; 01010 } 01011 01012 move_iterator 01013 operator-(difference_type __n) const 01014 { return move_iterator(_M_current - __n); } 01015 01016 move_iterator& 01017 operator-=(difference_type __n) 01018 { 01019 _M_current -= __n; 01020 return *this; 01021 } 01022 01023 reference 01024 operator[](difference_type __n) const 01025 { return std::move(_M_current[__n]); } 01026 }; 01027 01028 // Note: See __normal_iterator operators note from Gaby to understand 01029 // why there are always 2 versions for most of the move_iterator 01030 // operators. 01031 template<typename _IteratorL, typename _IteratorR> 01032 inline bool 01033 operator==(const move_iterator<_IteratorL>& __x, 01034 const move_iterator<_IteratorR>& __y) 01035 { return __x.base() == __y.base(); } 01036 01037 template<typename _Iterator> 01038 inline bool 01039 operator==(const move_iterator<_Iterator>& __x, 01040 const move_iterator<_Iterator>& __y) 01041 { return __x.base() == __y.base(); } 01042 01043 template<typename _IteratorL, typename _IteratorR> 01044 inline bool 01045 operator!=(const move_iterator<_IteratorL>& __x, 01046 const move_iterator<_IteratorR>& __y) 01047 { return !(__x == __y); } 01048 01049 template<typename _Iterator> 01050 inline bool 01051 operator!=(const move_iterator<_Iterator>& __x, 01052 const move_iterator<_Iterator>& __y) 01053 { return !(__x == __y); } 01054 01055 template<typename _IteratorL, typename _IteratorR> 01056 inline bool 01057 operator<(const move_iterator<_IteratorL>& __x, 01058 const move_iterator<_IteratorR>& __y) 01059 { return __x.base() < __y.base(); } 01060 01061 template<typename _Iterator> 01062 inline bool 01063 operator<(const move_iterator<_Iterator>& __x, 01064 const move_iterator<_Iterator>& __y) 01065 { return __x.base() < __y.base(); } 01066 01067 template<typename _IteratorL, typename _IteratorR> 01068 inline bool 01069 operator<=(const move_iterator<_IteratorL>& __x, 01070 const move_iterator<_IteratorR>& __y) 01071 { return !(__y < __x); } 01072 01073 template<typename _Iterator> 01074 inline bool 01075 operator<=(const move_iterator<_Iterator>& __x, 01076 const move_iterator<_Iterator>& __y) 01077 { return !(__y < __x); } 01078 01079 template<typename _IteratorL, typename _IteratorR> 01080 inline bool 01081 operator>(const move_iterator<_IteratorL>& __x, 01082 const move_iterator<_IteratorR>& __y) 01083 { return __y < __x; } 01084 01085 template<typename _Iterator> 01086 inline bool 01087 operator>(const move_iterator<_Iterator>& __x, 01088 const move_iterator<_Iterator>& __y) 01089 { return __y < __x; } 01090 01091 template<typename _IteratorL, typename _IteratorR> 01092 inline bool 01093 operator>=(const move_iterator<_IteratorL>& __x, 01094 const move_iterator<_IteratorR>& __y) 01095 { return !(__x < __y); } 01096 01097 template<typename _Iterator> 01098 inline bool 01099 operator>=(const move_iterator<_Iterator>& __x, 01100 const move_iterator<_Iterator>& __y) 01101 { return !(__x < __y); } 01102 01103 // DR 685. 01104 template<typename _IteratorL, typename _IteratorR> 01105 inline auto 01106 operator-(const move_iterator<_IteratorL>& __x, 01107 const move_iterator<_IteratorR>& __y) 01108 -> decltype(__x.base() - __y.base()) 01109 { return __x.base() - __y.base(); } 01110 01111 template<typename _Iterator> 01112 inline auto 01113 operator-(const move_iterator<_Iterator>& __x, 01114 const move_iterator<_Iterator>& __y) 01115 -> decltype(__x.base() - __y.base()) 01116 { return __x.base() - __y.base(); } 01117 01118 template<typename _Iterator> 01119 inline move_iterator<_Iterator> 01120 operator+(typename move_iterator<_Iterator>::difference_type __n, 01121 const move_iterator<_Iterator>& __x) 01122 { return __x + __n; } 01123 01124 template<typename _Iterator> 01125 inline move_iterator<_Iterator> 01126 make_move_iterator(_Iterator __i) 01127 { return move_iterator<_Iterator>(__i); } 01128 01129 template<typename _Iterator, typename _ReturnType 01130 = typename conditional<__move_if_noexcept_cond 01131 <typename iterator_traits<_Iterator>::value_type>::value, 01132 _Iterator, move_iterator<_Iterator>>::type> 01133 inline _ReturnType 01134 __make_move_if_noexcept_iterator(_Iterator __i) 01135 { return _ReturnType(__i); } 01136 01137 // @} group iterators 01138 01139 _GLIBCXX_END_NAMESPACE_VERSION 01140 } // namespace 01141 01142 #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) std::make_move_iterator(_Iter) 01143 #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) \ 01144 std::__make_move_if_noexcept_iterator(_Iter) 01145 #else 01146 #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) (_Iter) 01147 #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) (_Iter) 01148 #endif // __GXX_EXPERIMENTAL_CXX0X__ 01149 01150 #endif