# stl_iterator_base_funcs.h

Go to the documentation of this file.

    00001 // Functions used by 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_base_funcs.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 contains all of the general iterator-related utility
    00057  *  functions, such as distance() and advance().
    00058  */
    00059
    00060 #ifndef _STL_ITERATOR_BASE_FUNCS_H
    00061 #define _STL_ITERATOR_BASE_FUNCS_H 1
    00062
    00063 #pragma GCC system_header
    00064
    00065 #include <bits/concept_check.h>
    00066
    00067 _GLIBCXX_BEGIN_NAMESPACE(std)
    00068
    00069   template<typename _InputIterator>
    00070     inline typename iterator_traits<_InputIterator>::difference_type
    00071     __distance(_InputIterator __first, _InputIterator __last,
    00072                input_iterator_tag)
    00073     {
    00074       // concept requirements
    00075       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
    00076
    00077       typename iterator_traits<_InputIterator>::difference_type __n = 0;
    00078       while (__first != __last)
    00079     {
    00080       ++__first;
    00081       ++__n;
    00082     }
    00083       return __n;
    00084     }
    00085
    00086   template<typename _RandomAccessIterator>
    00087     inline typename iterator_traits<_RandomAccessIterator>::difference_type
    00088     __distance(_RandomAccessIterator __first, _RandomAccessIterator __last,
    00089                random_access_iterator_tag)
    00090     {
    00091       // concept requirements
    00092       __glibcxx_function_requires(_RandomAccessIteratorConcept<
    00093                   _RandomAccessIterator>)
    00094       return __last - __first;
    00095     }
    00096
    00097   /**
    00098    *  @brief A generalization of pointer arithmetic.
    00099    *  @param  first  An input iterator.
    00100    *  @param  last  An input iterator.
    00101    *  @return  The distance between them.
    00102    *
    00103    *  Returns @c n such that first + n == last.  This requires that @p last
    00104    *  must be reachable from @p first.  Note that @c n may be negative.
    00105    *
    00106    *  For random access iterators, this uses their @c + and @c - operations
    00107    *  and are constant time.  For other %iterator classes they are linear time.
    00108   */
    00109   template<typename _InputIterator>
    00110     inline typename iterator_traits<_InputIterator>::difference_type
    00111     distance(_InputIterator __first, _InputIterator __last)
    00112     {
    00113       // concept requirements -- taken care of in __distance
    00114       return std::__distance(__first, __last,
    00115                  std::__iterator_category(__first));
    00116     }
    00117
    00118   template<typename _InputIterator, typename _Distance>
    00119     inline void
    00120     __advance(_InputIterator& __i, _Distance __n, input_iterator_tag)
    00121     {
    00122       // concept requirements
    00123       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
    00124       while (__n--)
    00125     ++__i;
    00126     }
    00127
    00128   template<typename _BidirectionalIterator, typename _Distance>
    00129     inline void
    00130     __advance(_BidirectionalIterator& __i, _Distance __n,
    00131           bidirectional_iterator_tag)
    00132     {
    00133       // concept requirements
    00134       __glibcxx_function_requires(_BidirectionalIteratorConcept<
    00135                   _BidirectionalIterator>)
    00136       if (__n > 0)
    00137         while (__n--)
    00138       ++__i;
    00139       else
    00140         while (__n++)
    00141       --__i;
    00142     }
    00143
    00144   template<typename _RandomAccessIterator, typename _Distance>
    00145     inline void
    00146     __advance(_RandomAccessIterator& __i, _Distance __n,
    00147               random_access_iterator_tag)
    00148     {
    00149       // concept requirements
    00150       __glibcxx_function_requires(_RandomAccessIteratorConcept<
    00151                   _RandomAccessIterator>)
    00152       __i += __n;
    00153     }
    00154
    00155   /**
    00156    *  @brief A generalization of pointer arithmetic.
    00157    *  @param  i  An input iterator.
    00158    *  @param  n  The @a delta by which to change @p i.
    00159    *  @return  Nothing.
    00160    *
    00161    *  This increments @p i by @p n.  For bidirectional and random access
    00162    *  iterators, @p n may be negative, in which case @p i is decremented.
    00163    *
    00164    *  For random access iterators, this uses their @c + and @c - operations
    00165    *  and are constant time.  For other %iterator classes they are linear time.
    00166   */
    00167   template<typename _InputIterator, typename _Distance>
    00168     inline void
    00169     advance(_InputIterator& __i, _Distance __n)
    00170     {
    00171       // concept requirements -- taken care of in __advance
    00172       typename iterator_traits<_InputIterator>::difference_type __d = __n;
    00173       std::__advance(__i, __d, std::__iterator_category(__i));
    00174     }
    00175
    00176 _GLIBCXX_END_NAMESPACE
    00177
    00178 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00179
    00180 #include <ext/type_traits.h> // For __enable_if and __is_iterator
    00181
    00182 _GLIBCXX_BEGIN_NAMESPACE(std)
    00183
    00184   template<typename _ForwardIterator>
    00185     inline typename
    00186     __gnu_cxx::__enable_if<__is_iterator<_ForwardIterator>::__value,
    00187                _ForwardIterator>::__type
    00188     next(_ForwardIterator __x, typename
    00189      iterator_traits<_ForwardIterator>::difference_type __n = 1)
    00190     {
    00191       std::advance(__x, __n);
    00192       return __x;
    00193     }
    00194
    00195   template<typename _BidirectionalIterator>
    00196     inline typename
    00197     __gnu_cxx::__enable_if<__is_iterator<_BidirectionalIterator>::__value,
    00198                _BidirectionalIterator>::__type
    00199     prev(_BidirectionalIterator __x, typename
    00200      iterator_traits<_BidirectionalIterator>::difference_type __n = 1)
    00201     {
    00202       std::advance(__x, -__n);
    00203       return __x;
    00204     }
    00205
    00206 _GLIBCXX_END_NAMESPACE
    00207
    00208 #endif // __GXX_EXPERIMENTAL_CXX0X__
    00209
    00210 #endif /* _STL_ITERATOR_BASE_FUNCS_H */