stl_iterator.h

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

Generated on 9 Feb 2010 for libstdc++ by  doxygen 1.6.1