1 // Functions used by iterators -*- C++ -*-
3 // Copyright (C) 2001-2016 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
28 * Hewlett-Packard Company
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
39 * Copyright (c) 1996-1998
40 * Silicon Graphics Computer Systems, Inc.
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
51 /** @file bits/stl_iterator_base_funcs.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{iterator}
55 * This file contains all of the general iterator-related utility
56 * functions, such as distance() and advance().
59 #ifndef _STL_ITERATOR_BASE_FUNCS_H
60 #define _STL_ITERATOR_BASE_FUNCS_H 1
62 #pragma GCC system_header
64 #include <bits/concept_check.h>
65 #include <debug/assertions.h>
67 namespace std
_GLIBCXX_VISIBILITY(default)
69 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
70 // Forward declaration for the overloads of __distance.
71 template <typename
> struct _List_iterator
;
72 template <typename
> struct _List_const_iterator
;
73 _GLIBCXX_END_NAMESPACE_CONTAINER
75 _GLIBCXX_BEGIN_NAMESPACE_VERSION
77 template<typename _InputIterator
>
78 inline _GLIBCXX14_CONSTEXPR
79 typename iterator_traits
<_InputIterator
>::difference_type
80 __distance(_InputIterator __first
, _InputIterator __last
,
83 // concept requirements
84 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
86 typename iterator_traits
<_InputIterator
>::difference_type __n
= 0;
87 while (__first
!= __last
)
95 template<typename _RandomAccessIterator
>
96 inline _GLIBCXX14_CONSTEXPR
97 typename iterator_traits
<_RandomAccessIterator
>::difference_type
98 __distance(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
99 random_access_iterator_tag
)
101 // concept requirements
102 __glibcxx_function_requires(_RandomAccessIteratorConcept
<
103 _RandomAccessIterator
>)
104 return __last
- __first
;
107 #if _GLIBCXX_USE_CXX11_ABI
108 // Forward declaration because of the qualified call in distance.
109 template<typename _Tp
>
111 __distance(_GLIBCXX_STD_C::_List_iterator
<_Tp
>,
112 _GLIBCXX_STD_C::_List_iterator
<_Tp
>,
115 template<typename _Tp
>
117 __distance(_GLIBCXX_STD_C::_List_const_iterator
<_Tp
>,
118 _GLIBCXX_STD_C::_List_const_iterator
<_Tp
>,
123 * @brief A generalization of pointer arithmetic.
124 * @param __first An input iterator.
125 * @param __last An input iterator.
126 * @return The distance between them.
128 * Returns @c n such that __first + n == __last. This requires
129 * that @p __last must be reachable from @p __first. Note that @c
132 * For random access iterators, this uses their @c + and @c - operations
133 * and are constant time. For other %iterator classes they are linear time.
135 template<typename _InputIterator
>
136 inline _GLIBCXX17_CONSTEXPR
137 typename iterator_traits
<_InputIterator
>::difference_type
138 distance(_InputIterator __first
, _InputIterator __last
)
140 // concept requirements -- taken care of in __distance
141 return std::__distance(__first
, __last
,
142 std::__iterator_category(__first
));
145 template<typename _InputIterator
, typename _Distance
>
146 inline _GLIBCXX14_CONSTEXPR
void
147 __advance(_InputIterator
& __i
, _Distance __n
, input_iterator_tag
)
149 // concept requirements
150 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
151 __glibcxx_assert(__n
>= 0);
156 template<typename _BidirectionalIterator
, typename _Distance
>
157 inline _GLIBCXX14_CONSTEXPR
void
158 __advance(_BidirectionalIterator
& __i
, _Distance __n
,
159 bidirectional_iterator_tag
)
161 // concept requirements
162 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
163 _BidirectionalIterator
>)
172 template<typename _RandomAccessIterator
, typename _Distance
>
173 inline _GLIBCXX14_CONSTEXPR
void
174 __advance(_RandomAccessIterator
& __i
, _Distance __n
,
175 random_access_iterator_tag
)
177 // concept requirements
178 __glibcxx_function_requires(_RandomAccessIteratorConcept
<
179 _RandomAccessIterator
>)
184 * @brief A generalization of pointer arithmetic.
185 * @param __i An input iterator.
186 * @param __n The @a delta by which to change @p __i.
189 * This increments @p i by @p n. For bidirectional and random access
190 * iterators, @p __n may be negative, in which case @p __i is decremented.
192 * For random access iterators, this uses their @c + and @c - operations
193 * and are constant time. For other %iterator classes they are linear time.
195 template<typename _InputIterator
, typename _Distance
>
196 inline _GLIBCXX17_CONSTEXPR
void
197 advance(_InputIterator
& __i
, _Distance __n
)
199 // concept requirements -- taken care of in __advance
200 typename iterator_traits
<_InputIterator
>::difference_type __d
= __n
;
201 std::__advance(__i
, __d
, std::__iterator_category(__i
));
204 #if __cplusplus >= 201103L
206 template<typename _ForwardIterator
>
207 inline _GLIBCXX17_CONSTEXPR _ForwardIterator
208 next(_ForwardIterator __x
, typename
209 iterator_traits
<_ForwardIterator
>::difference_type __n
= 1)
211 // concept requirements
212 __glibcxx_function_requires(_ForwardIteratorConcept
<
214 std::advance(__x
, __n
);
218 template<typename _BidirectionalIterator
>
219 inline _GLIBCXX17_CONSTEXPR _BidirectionalIterator
220 prev(_BidirectionalIterator __x
, typename
221 iterator_traits
<_BidirectionalIterator
>::difference_type __n
= 1)
223 // concept requirements
224 __glibcxx_function_requires(_BidirectionalIteratorConcept
<
225 _BidirectionalIterator
>)
226 std::advance(__x
, -__n
);
232 _GLIBCXX_END_NAMESPACE_VERSION
235 #endif /* _STL_ITERATOR_BASE_FUNCS_H */