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 that @p last
00106    *  must be reachable from @p first.  Note that @c n may be negative.
00107    *
00108    *  For random access iterators, this uses their @c + and @c - operations
00109    *  and are constant time.  For other %iterator classes they are linear time.
00110   */
00111   template<typename _InputIterator>
00112     inline typename iterator_traits<_InputIterator>::difference_type
00113     distance(_InputIterator __first, _InputIterator __last)
00114     {
00115       // concept requirements -- taken care of in __distance
00116       return std::__distance(__first, __last,
00117                  std::__iterator_category(__first));
00118     }
00119 
00120   template<typename _InputIterator, typename _Distance>
00121     inline void
00122     __advance(_InputIterator& __i, _Distance __n, input_iterator_tag)
00123     {
00124       // concept requirements
00125       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00126       while (__n--)
00127     ++__i;
00128     }
00129 
00130   template<typename _BidirectionalIterator, typename _Distance>
00131     inline void
00132     __advance(_BidirectionalIterator& __i, _Distance __n,
00133           bidirectional_iterator_tag)
00134     {
00135       // concept requirements
00136       __glibcxx_function_requires(_BidirectionalIteratorConcept<
00137                   _BidirectionalIterator>)
00138       if (__n > 0)
00139         while (__n--)
00140       ++__i;
00141       else
00142         while (__n++)
00143       --__i;
00144     }
00145 
00146   template<typename _RandomAccessIterator, typename _Distance>
00147     inline void
00148     __advance(_RandomAccessIterator& __i, _Distance __n,
00149               random_access_iterator_tag)
00150     {
00151       // concept requirements
00152       __glibcxx_function_requires(_RandomAccessIteratorConcept<
00153                   _RandomAccessIterator>)
00154       __i += __n;
00155     }
00156 
00157   /**
00158    *  @brief A generalization of pointer arithmetic.
00159    *  @param  i  An input iterator.
00160    *  @param  n  The @a delta by which to change @p i.
00161    *  @return  Nothing.
00162    *
00163    *  This increments @p i by @p n.  For bidirectional and random access
00164    *  iterators, @p n may be negative, in which case @p i is decremented.
00165    *
00166    *  For random access iterators, this uses their @c + and @c - operations
00167    *  and are constant time.  For other %iterator classes they are linear time.
00168   */
00169   template<typename _InputIterator, typename _Distance>
00170     inline void
00171     advance(_InputIterator& __i, _Distance __n)
00172     {
00173       // concept requirements -- taken care of in __advance
00174       typename iterator_traits<_InputIterator>::difference_type __d = __n;
00175       std::__advance(__i, __d, std::__iterator_category(__i));
00176     }
00177 
00178 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00179 
00180   template<typename _ForwardIterator>
00181     inline _ForwardIterator
00182     next(_ForwardIterator __x, typename
00183      iterator_traits<_ForwardIterator>::difference_type __n = 1)
00184     {
00185       std::advance(__x, __n);
00186       return __x;
00187     }
00188 
00189   template<typename _BidirectionalIterator>
00190     inline _BidirectionalIterator
00191     prev(_BidirectionalIterator __x, typename
00192      iterator_traits<_BidirectionalIterator>::difference_type __n = 1) 
00193     {
00194       std::advance(__x, -__n);
00195       return __x;
00196     }
00197 
00198 #endif // __GXX_EXPERIMENTAL_CXX0X__
00199 
00200 _GLIBCXX_END_NAMESPACE_VERSION
00201 } // namespace
00202 
00203 #endif /* _STL_ITERATOR_BASE_FUNCS_H */