]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/stl_iterator_base_funcs.h
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_iterator_base_funcs.h
1 // Functions used by iterators -*- C++ -*-
2
3 // Copyright (C) 2001-2022 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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.
19
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/>.
24
25 /*
26 *
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
29 *
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.
37 *
38 *
39 * Copyright (c) 1996-1998
40 * Silicon Graphics Computer Systems, Inc.
41 *
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.
49 */
50
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}
54 *
55 * This file contains all of the general iterator-related utility
56 * functions, such as distance() and advance().
57 */
58
59 #ifndef _STL_ITERATOR_BASE_FUNCS_H
60 #define _STL_ITERATOR_BASE_FUNCS_H 1
61
62 #pragma GCC system_header
63
64 #include <bits/concept_check.h>
65 #include <debug/assertions.h>
66
67 namespace std _GLIBCXX_VISIBILITY(default)
68 {
69 _GLIBCXX_BEGIN_NAMESPACE_VERSION
70
71 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
72 // Forward declaration for the overloads of __distance.
73 template <typename> struct _List_iterator;
74 template <typename> struct _List_const_iterator;
75 _GLIBCXX_END_NAMESPACE_CONTAINER
76
77 template<typename _InputIterator>
78 inline _GLIBCXX14_CONSTEXPR
79 typename iterator_traits<_InputIterator>::difference_type
80 __distance(_InputIterator __first, _InputIterator __last,
81 input_iterator_tag)
82 {
83 // concept requirements
84 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
85
86 typename iterator_traits<_InputIterator>::difference_type __n = 0;
87 while (__first != __last)
88 {
89 ++__first;
90 ++__n;
91 }
92 return __n;
93 }
94
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)
100 {
101 // concept requirements
102 __glibcxx_function_requires(_RandomAccessIteratorConcept<
103 _RandomAccessIterator>)
104 return __last - __first;
105 }
106
107 #if _GLIBCXX_USE_CXX11_ABI
108 // Forward declaration because of the qualified call in distance.
109 template<typename _Tp>
110 ptrdiff_t
111 __distance(_GLIBCXX_STD_C::_List_iterator<_Tp>,
112 _GLIBCXX_STD_C::_List_iterator<_Tp>,
113 input_iterator_tag);
114
115 template<typename _Tp>
116 ptrdiff_t
117 __distance(_GLIBCXX_STD_C::_List_const_iterator<_Tp>,
118 _GLIBCXX_STD_C::_List_const_iterator<_Tp>,
119 input_iterator_tag);
120 #endif
121
122 #if __cplusplus >= 201103L
123 // Give better error if std::distance called with a non-Cpp17InputIterator.
124 template<typename _OutputIterator>
125 void
126 __distance(_OutputIterator, _OutputIterator, output_iterator_tag) = delete;
127 #endif
128
129 /**
130 * @brief A generalization of pointer arithmetic.
131 * @param __first An input iterator.
132 * @param __last An input iterator.
133 * @return The distance between them.
134 *
135 * Returns @c n such that __first + n == __last. This requires
136 * that @p __last must be reachable from @p __first. Note that @c
137 * n may be negative.
138 *
139 * For random access iterators, this uses their @c + and @c - operations
140 * and are constant time. For other %iterator classes they are linear time.
141 */
142 template<typename _InputIterator>
143 _GLIBCXX_NODISCARD
144 inline _GLIBCXX17_CONSTEXPR
145 typename iterator_traits<_InputIterator>::difference_type
146 distance(_InputIterator __first, _InputIterator __last)
147 {
148 // concept requirements -- taken care of in __distance
149 return std::__distance(__first, __last,
150 std::__iterator_category(__first));
151 }
152
153 template<typename _InputIterator, typename _Distance>
154 inline _GLIBCXX14_CONSTEXPR void
155 __advance(_InputIterator& __i, _Distance __n, input_iterator_tag)
156 {
157 // concept requirements
158 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
159 __glibcxx_assert(__n >= 0);
160 while (__n--)
161 ++__i;
162 }
163
164 template<typename _BidirectionalIterator, typename _Distance>
165 inline _GLIBCXX14_CONSTEXPR void
166 __advance(_BidirectionalIterator& __i, _Distance __n,
167 bidirectional_iterator_tag)
168 {
169 // concept requirements
170 __glibcxx_function_requires(_BidirectionalIteratorConcept<
171 _BidirectionalIterator>)
172 if (__n > 0)
173 while (__n--)
174 ++__i;
175 else
176 while (__n++)
177 --__i;
178 }
179
180 template<typename _RandomAccessIterator, typename _Distance>
181 inline _GLIBCXX14_CONSTEXPR void
182 __advance(_RandomAccessIterator& __i, _Distance __n,
183 random_access_iterator_tag)
184 {
185 // concept requirements
186 __glibcxx_function_requires(_RandomAccessIteratorConcept<
187 _RandomAccessIterator>)
188 if (__builtin_constant_p(__n) && __n == 1)
189 ++__i;
190 else if (__builtin_constant_p(__n) && __n == -1)
191 --__i;
192 else
193 __i += __n;
194 }
195
196 #if __cplusplus >= 201103L
197 // Give better error if std::advance called with a non-Cpp17InputIterator.
198 template<typename _OutputIterator, typename _Distance>
199 void
200 __advance(_OutputIterator&, _Distance, output_iterator_tag) = delete;
201 #endif
202
203 /**
204 * @brief A generalization of pointer arithmetic.
205 * @param __i An input iterator.
206 * @param __n The @a delta by which to change @p __i.
207 * @return Nothing.
208 *
209 * This increments @p i by @p n. For bidirectional and random access
210 * iterators, @p __n may be negative, in which case @p __i is decremented.
211 *
212 * For random access iterators, this uses their @c + and @c - operations
213 * and are constant time. For other %iterator classes they are linear time.
214 */
215 template<typename _InputIterator, typename _Distance>
216 inline _GLIBCXX17_CONSTEXPR void
217 advance(_InputIterator& __i, _Distance __n)
218 {
219 // concept requirements -- taken care of in __advance
220 typename iterator_traits<_InputIterator>::difference_type __d = __n;
221 std::__advance(__i, __d, std::__iterator_category(__i));
222 }
223
224 #if __cplusplus >= 201103L
225
226 template<typename _InputIterator>
227 _GLIBCXX_NODISCARD
228 inline _GLIBCXX17_CONSTEXPR _InputIterator
229 next(_InputIterator __x, typename
230 iterator_traits<_InputIterator>::difference_type __n = 1)
231 {
232 // concept requirements
233 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
234 std::advance(__x, __n);
235 return __x;
236 }
237
238 template<typename _BidirectionalIterator>
239 _GLIBCXX_NODISCARD
240 inline _GLIBCXX17_CONSTEXPR _BidirectionalIterator
241 prev(_BidirectionalIterator __x, typename
242 iterator_traits<_BidirectionalIterator>::difference_type __n = 1)
243 {
244 // concept requirements
245 __glibcxx_function_requires(_BidirectionalIteratorConcept<
246 _BidirectionalIterator>)
247 std::advance(__x, -__n);
248 return __x;
249 }
250
251 #endif // C++11
252
253 _GLIBCXX_END_NAMESPACE_VERSION
254 } // namespace
255
256 #endif /* _STL_ITERATOR_BASE_FUNCS_H */