libstdc++
stl_iterator_base_funcs.h
Go to the documentation of this file.
00001 // Functions used by iterators -*- C++ -*-
00002 
00003 // Copyright (C) 2001-2016 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /*
00026  *
00027  * Copyright (c) 1994
00028  * Hewlett-Packard Company
00029  *
00030  * Permission to use, copy, modify, distribute and sell this software
00031  * and its documentation for any purpose is hereby granted without fee,
00032  * provided that the above copyright notice appear in all copies and
00033  * that both that copyright notice and this permission notice appear
00034  * in supporting documentation.  Hewlett-Packard Company makes no
00035  * representations about the suitability of this software for any
00036  * purpose.  It is provided "as is" without express or implied warranty.
00037  *
00038  *
00039  * Copyright (c) 1996-1998
00040  * Silicon Graphics Computer Systems, Inc.
00041  *
00042  * Permission to use, copy, modify, distribute and sell this software
00043  * and its documentation for any purpose is hereby granted without fee,
00044  * provided that the above copyright notice appear in all copies and
00045  * that both that copyright notice and this permission notice appear
00046  * in supporting documentation.  Silicon Graphics makes no
00047  * representations about the suitability of this software for any
00048  * purpose.  It is provided "as is" without express or implied warranty.
00049  */
00050 
00051 /** @file bits/stl_iterator_base_funcs.h
00052  *  This is an internal header file, included by other library headers.
00053  *  Do not attempt to use it directly. @headername{iterator}
00054  *
00055  *  This file contains all of the general iterator-related utility
00056  *  functions, such as distance() and advance().
00057  */
00058 
00059 #ifndef _STL_ITERATOR_BASE_FUNCS_H
00060 #define _STL_ITERATOR_BASE_FUNCS_H 1
00061 
00062 #pragma GCC system_header
00063 
00064 #include <bits/concept_check.h>
00065 #include <debug/assertions.h>
00066 
00067 namespace std _GLIBCXX_VISIBILITY(default)
00068 {
00069 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
00070   // Forward declaration for the overloads of __distance.
00071   template <typename> struct _List_iterator;
00072   template <typename> struct _List_const_iterator;
00073 _GLIBCXX_END_NAMESPACE_CONTAINER
00074 
00075 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00076 
00077   template<typename _InputIterator>
00078     inline typename iterator_traits<_InputIterator>::difference_type
00079     __distance(_InputIterator __first, _InputIterator __last,
00080                input_iterator_tag)
00081     {
00082       // concept requirements
00083       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00084 
00085       typename iterator_traits<_InputIterator>::difference_type __n = 0;
00086       while (__first != __last)
00087         {
00088           ++__first;
00089           ++__n;
00090         }
00091       return __n;
00092     }
00093 
00094   template<typename _RandomAccessIterator>
00095     inline typename iterator_traits<_RandomAccessIterator>::difference_type
00096     __distance(_RandomAccessIterator __first, _RandomAccessIterator __last,
00097                random_access_iterator_tag)
00098     {
00099       // concept requirements
00100       __glibcxx_function_requires(_RandomAccessIteratorConcept<
00101                                   _RandomAccessIterator>)
00102       return __last - __first;
00103     }
00104 
00105 #if _GLIBCXX_USE_CXX11_ABI
00106   // Forward declaration because of the qualified call in distance.
00107   template<typename _Tp>
00108     ptrdiff_t
00109     __distance(_GLIBCXX_STD_C::_List_iterator<_Tp>,
00110                _GLIBCXX_STD_C::_List_iterator<_Tp>,
00111                input_iterator_tag);
00112 
00113   template<typename _Tp>
00114     ptrdiff_t
00115     __distance(_GLIBCXX_STD_C::_List_const_iterator<_Tp>,
00116                _GLIBCXX_STD_C::_List_const_iterator<_Tp>,
00117                input_iterator_tag);
00118 #endif
00119 
00120   /**
00121    *  @brief A generalization of pointer arithmetic.
00122    *  @param  __first  An input iterator.
00123    *  @param  __last  An input iterator.
00124    *  @return  The distance between them.
00125    *
00126    *  Returns @c n such that __first + n == __last.  This requires
00127    *  that @p __last must be reachable from @p __first.  Note that @c
00128    *  n may be negative.
00129    *
00130    *  For random access iterators, this uses their @c + and @c - operations
00131    *  and are constant time.  For other %iterator classes they are linear time.
00132   */
00133   template<typename _InputIterator>
00134     inline typename iterator_traits<_InputIterator>::difference_type
00135     distance(_InputIterator __first, _InputIterator __last)
00136     {
00137       // concept requirements -- taken care of in __distance
00138       return std::__distance(__first, __last,
00139                              std::__iterator_category(__first));
00140     }
00141 
00142   template<typename _InputIterator, typename _Distance>
00143     inline void
00144     __advance(_InputIterator& __i, _Distance __n, input_iterator_tag)
00145     {
00146       // concept requirements
00147       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00148       __glibcxx_assert(__n >= 0);
00149       while (__n--)
00150         ++__i;
00151     }
00152 
00153   template<typename _BidirectionalIterator, typename _Distance>
00154     inline void
00155     __advance(_BidirectionalIterator& __i, _Distance __n,
00156               bidirectional_iterator_tag)
00157     {
00158       // concept requirements
00159       __glibcxx_function_requires(_BidirectionalIteratorConcept<
00160                                   _BidirectionalIterator>)
00161       if (__n > 0)
00162         while (__n--)
00163           ++__i;
00164       else
00165         while (__n++)
00166           --__i;
00167     }
00168 
00169   template<typename _RandomAccessIterator, typename _Distance>
00170     inline void
00171     __advance(_RandomAccessIterator& __i, _Distance __n,
00172               random_access_iterator_tag)
00173     {
00174       // concept requirements
00175       __glibcxx_function_requires(_RandomAccessIteratorConcept<
00176                                   _RandomAccessIterator>)
00177       __i += __n;
00178     }
00179 
00180   /**
00181    *  @brief A generalization of pointer arithmetic.
00182    *  @param  __i  An input iterator.
00183    *  @param  __n  The @a delta by which to change @p __i.
00184    *  @return  Nothing.
00185    *
00186    *  This increments @p i by @p n.  For bidirectional and random access
00187    *  iterators, @p __n may be negative, in which case @p __i is decremented.
00188    *
00189    *  For random access iterators, this uses their @c + and @c - operations
00190    *  and are constant time.  For other %iterator classes they are linear time.
00191   */
00192   template<typename _InputIterator, typename _Distance>
00193     inline void
00194     advance(_InputIterator& __i, _Distance __n)
00195     {
00196       // concept requirements -- taken care of in __advance
00197       typename iterator_traits<_InputIterator>::difference_type __d = __n;
00198       std::__advance(__i, __d, std::__iterator_category(__i));
00199     }
00200 
00201 #if __cplusplus >= 201103L
00202 
00203   template<typename _ForwardIterator>
00204     inline _ForwardIterator
00205     next(_ForwardIterator __x, typename
00206          iterator_traits<_ForwardIterator>::difference_type __n = 1)
00207     {
00208       // concept requirements
00209       __glibcxx_function_requires(_ForwardIteratorConcept<
00210                                   _ForwardIterator>)
00211       std::advance(__x, __n);
00212       return __x;
00213     }
00214 
00215   template<typename _BidirectionalIterator>
00216     inline _BidirectionalIterator
00217     prev(_BidirectionalIterator __x, typename
00218          iterator_traits<_BidirectionalIterator>::difference_type __n = 1) 
00219     {
00220       // concept requirements
00221       __glibcxx_function_requires(_BidirectionalIteratorConcept<
00222                                   _BidirectionalIterator>)
00223       std::advance(__x, -__n);
00224       return __x;
00225     }
00226 
00227 #endif // C++11
00228 
00229 _GLIBCXX_END_NAMESPACE_VERSION
00230 } // namespace
00231 
00232 #endif /* _STL_ITERATOR_BASE_FUNCS_H */