libstdc++
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 bits/stl_iterator_base_funcs.h
00053  *  This is an internal header file, included by other library headers.
00054  *  Do not attempt to use it directly. @headername{iterator}
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 namespace std _GLIBCXX_VISIBILITY(default)
00068 {
00069 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00070 
00071   template<typename _InputIterator>
00072     inline typename iterator_traits<_InputIterator>::difference_type
00073     __distance(_InputIterator __first, _InputIterator __last,
00074                input_iterator_tag)
00075     {
00076       // concept requirements
00077       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00078 
00079       typename iterator_traits<_InputIterator>::difference_type __n = 0;
00080       while (__first != __last)
00081     {
00082       ++__first;
00083       ++__n;
00084     }
00085       return __n;
00086     }
00087 
00088   template<typename _RandomAccessIterator>
00089     inline typename iterator_traits<_RandomAccessIterator>::difference_type
00090     __distance(_RandomAccessIterator __first, _RandomAccessIterator __last,
00091                random_access_iterator_tag)
00092     {
00093       // concept requirements
00094       __glibcxx_function_requires(_RandomAccessIteratorConcept<
00095                   _RandomAccessIterator>)
00096       return __last - __first;
00097     }
00098 
00099   /**
00100    *  @brief A generalization of pointer arithmetic.
00101    *  @param  __first  An input iterator.
00102    *  @param  __last  An input iterator.
00103    *  @return  The distance between them.
00104    *
00105    *  Returns @c n such that __first + n == __last.  This requires
00106    *  that @p __last must be reachable from @p __first.  Note that @c
00107    *  n may be negative.
00108    *
00109    *  For random access iterators, this uses their @c + and @c - operations
00110    *  and are constant time.  For other %iterator classes they are linear time.
00111   */
00112   template<typename _InputIterator>
00113     inline typename iterator_traits<_InputIterator>::difference_type
00114     distance(_InputIterator __first, _InputIterator __last)
00115     {
00116       // concept requirements -- taken care of in __distance
00117       return std::__distance(__first, __last,
00118                  std::__iterator_category(__first));
00119     }
00120 
00121   template<typename _InputIterator, typename _Distance>
00122     inline void
00123     __advance(_InputIterator& __i, _Distance __n, input_iterator_tag)
00124     {
00125       // concept requirements
00126       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00127       while (__n--)
00128     ++__i;
00129     }
00130 
00131   template<typename _BidirectionalIterator, typename _Distance>
00132     inline void
00133     __advance(_BidirectionalIterator& __i, _Distance __n,
00134           bidirectional_iterator_tag)
00135     {
00136       // concept requirements
00137       __glibcxx_function_requires(_BidirectionalIteratorConcept<
00138                   _BidirectionalIterator>)
00139       if (__n > 0)
00140         while (__n--)
00141       ++__i;
00142       else
00143         while (__n++)
00144       --__i;
00145     }
00146 
00147   template<typename _RandomAccessIterator, typename _Distance>
00148     inline void
00149     __advance(_RandomAccessIterator& __i, _Distance __n,
00150               random_access_iterator_tag)
00151     {
00152       // concept requirements
00153       __glibcxx_function_requires(_RandomAccessIteratorConcept<
00154                   _RandomAccessIterator>)
00155       __i += __n;
00156     }
00157 
00158   /**
00159    *  @brief A generalization of pointer arithmetic.
00160    *  @param  __i  An input iterator.
00161    *  @param  __n  The @a delta by which to change @p __i.
00162    *  @return  Nothing.
00163    *
00164    *  This increments @p i by @p n.  For bidirectional and random access
00165    *  iterators, @p __n may be negative, in which case @p __i is decremented.
00166    *
00167    *  For random access iterators, this uses their @c + and @c - operations
00168    *  and are constant time.  For other %iterator classes they are linear time.
00169   */
00170   template<typename _InputIterator, typename _Distance>
00171     inline void
00172     advance(_InputIterator& __i, _Distance __n)
00173     {
00174       // concept requirements -- taken care of in __advance
00175       typename iterator_traits<_InputIterator>::difference_type __d = __n;
00176       std::__advance(__i, __d, std::__iterator_category(__i));
00177     }
00178 
00179 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00180 
00181   template<typename _ForwardIterator>
00182     inline _ForwardIterator
00183     next(_ForwardIterator __x, typename
00184      iterator_traits<_ForwardIterator>::difference_type __n = 1)
00185     {
00186       std::advance(__x, __n);
00187       return __x;
00188     }
00189 
00190   template<typename _BidirectionalIterator>
00191     inline _BidirectionalIterator
00192     prev(_BidirectionalIterator __x, typename
00193      iterator_traits<_BidirectionalIterator>::difference_type __n = 1) 
00194     {
00195       std::advance(__x, -__n);
00196       return __x;
00197     }
00198 
00199 #endif // __GXX_EXPERIMENTAL_CXX0X__
00200 
00201 _GLIBCXX_END_NAMESPACE_VERSION
00202 } // namespace
00203 
00204 #endif /* _STL_ITERATOR_BASE_FUNCS_H */