libstdc++
stl_iterator_base_funcs.h
Go to the documentation of this file.
00001 // Functions used by iterators -*- C++ -*-
00002 
00003 // Copyright (C) 2001-2019 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_VERSION
00070 
00071 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
00072   // Forward declaration for the overloads of __distance.
00073   template <typename> struct _List_iterator;
00074   template <typename> struct _List_const_iterator;
00075 _GLIBCXX_END_NAMESPACE_CONTAINER
00076 
00077   template<typename _InputIterator>
00078     inline _GLIBCXX14_CONSTEXPR
00079     typename iterator_traits<_InputIterator>::difference_type
00080     __distance(_InputIterator __first, _InputIterator __last,
00081                input_iterator_tag)
00082     {
00083       // concept requirements
00084       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00085 
00086       typename iterator_traits<_InputIterator>::difference_type __n = 0;
00087       while (__first != __last)
00088         {
00089           ++__first;
00090           ++__n;
00091         }
00092       return __n;
00093     }
00094 
00095   template<typename _RandomAccessIterator>
00096     inline _GLIBCXX14_CONSTEXPR
00097     typename iterator_traits<_RandomAccessIterator>::difference_type
00098     __distance(_RandomAccessIterator __first, _RandomAccessIterator __last,
00099                random_access_iterator_tag)
00100     {
00101       // concept requirements
00102       __glibcxx_function_requires(_RandomAccessIteratorConcept<
00103                                   _RandomAccessIterator>)
00104       return __last - __first;
00105     }
00106 
00107 #if _GLIBCXX_USE_CXX11_ABI
00108   // Forward declaration because of the qualified call in distance.
00109   template<typename _Tp>
00110     ptrdiff_t
00111     __distance(_GLIBCXX_STD_C::_List_iterator<_Tp>,
00112                _GLIBCXX_STD_C::_List_iterator<_Tp>,
00113                input_iterator_tag);
00114 
00115   template<typename _Tp>
00116     ptrdiff_t
00117     __distance(_GLIBCXX_STD_C::_List_const_iterator<_Tp>,
00118                _GLIBCXX_STD_C::_List_const_iterator<_Tp>,
00119                input_iterator_tag);
00120 #endif
00121 
00122   /**
00123    *  @brief A generalization of pointer arithmetic.
00124    *  @param  __first  An input iterator.
00125    *  @param  __last  An input iterator.
00126    *  @return  The distance between them.
00127    *
00128    *  Returns @c n such that __first + n == __last.  This requires
00129    *  that @p __last must be reachable from @p __first.  Note that @c
00130    *  n may be negative.
00131    *
00132    *  For random access iterators, this uses their @c + and @c - operations
00133    *  and are constant time.  For other %iterator classes they are linear time.
00134   */
00135   template<typename _InputIterator>
00136     inline _GLIBCXX17_CONSTEXPR
00137     typename iterator_traits<_InputIterator>::difference_type
00138     distance(_InputIterator __first, _InputIterator __last)
00139     {
00140       // concept requirements -- taken care of in __distance
00141       return std::__distance(__first, __last,
00142                              std::__iterator_category(__first));
00143     }
00144 
00145   template<typename _InputIterator, typename _Distance>
00146     inline _GLIBCXX14_CONSTEXPR void
00147     __advance(_InputIterator& __i, _Distance __n, input_iterator_tag)
00148     {
00149       // concept requirements
00150       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00151       __glibcxx_assert(__n >= 0);
00152       while (__n--)
00153         ++__i;
00154     }
00155 
00156   template<typename _BidirectionalIterator, typename _Distance>
00157     inline _GLIBCXX14_CONSTEXPR void
00158     __advance(_BidirectionalIterator& __i, _Distance __n,
00159               bidirectional_iterator_tag)
00160     {
00161       // concept requirements
00162       __glibcxx_function_requires(_BidirectionalIteratorConcept<
00163                                   _BidirectionalIterator>)
00164       if (__n > 0)
00165         while (__n--)
00166           ++__i;
00167       else
00168         while (__n++)
00169           --__i;
00170     }
00171 
00172   template<typename _RandomAccessIterator, typename _Distance>
00173     inline _GLIBCXX14_CONSTEXPR void
00174     __advance(_RandomAccessIterator& __i, _Distance __n,
00175               random_access_iterator_tag)
00176     {
00177       // concept requirements
00178       __glibcxx_function_requires(_RandomAccessIteratorConcept<
00179                                   _RandomAccessIterator>)
00180       if (__builtin_constant_p(__n) && __n == 1)
00181         ++__i;
00182       else if (__builtin_constant_p(__n) && __n == -1)
00183         --__i;
00184       else
00185         __i += __n;
00186     }
00187 
00188   /**
00189    *  @brief A generalization of pointer arithmetic.
00190    *  @param  __i  An input iterator.
00191    *  @param  __n  The @a delta by which to change @p __i.
00192    *  @return  Nothing.
00193    *
00194    *  This increments @p i by @p n.  For bidirectional and random access
00195    *  iterators, @p __n may be negative, in which case @p __i is decremented.
00196    *
00197    *  For random access iterators, this uses their @c + and @c - operations
00198    *  and are constant time.  For other %iterator classes they are linear time.
00199   */
00200   template<typename _InputIterator, typename _Distance>
00201     inline _GLIBCXX17_CONSTEXPR void
00202     advance(_InputIterator& __i, _Distance __n)
00203     {
00204       // concept requirements -- taken care of in __advance
00205       typename iterator_traits<_InputIterator>::difference_type __d = __n;
00206       std::__advance(__i, __d, std::__iterator_category(__i));
00207     }
00208 
00209 #if __cplusplus >= 201103L
00210 
00211   template<typename _InputIterator>
00212     inline _GLIBCXX17_CONSTEXPR _InputIterator
00213     next(_InputIterator __x, typename
00214          iterator_traits<_InputIterator>::difference_type __n = 1)
00215     {
00216       // concept requirements
00217       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00218       std::advance(__x, __n);
00219       return __x;
00220     }
00221 
00222   template<typename _BidirectionalIterator>
00223     inline _GLIBCXX17_CONSTEXPR _BidirectionalIterator
00224     prev(_BidirectionalIterator __x, typename
00225          iterator_traits<_BidirectionalIterator>::difference_type __n = 1) 
00226     {
00227       // concept requirements
00228       __glibcxx_function_requires(_BidirectionalIteratorConcept<
00229                                   _BidirectionalIterator>)
00230       std::advance(__x, -__n);
00231       return __x;
00232     }
00233 
00234 #endif // C++11
00235 
00236 _GLIBCXX_END_NAMESPACE_VERSION
00237 } // namespace
00238 
00239 #endif /* _STL_ITERATOR_BASE_FUNCS_H */