stl_iterator_base_funcs.h revision 1.1.1.11
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#include <bits/stl_iterator_base_types.h> 67 68namespace std _GLIBCXX_VISIBILITY(default) 69{ 70_GLIBCXX_BEGIN_NAMESPACE_VERSION 71 72_GLIBCXX_BEGIN_NAMESPACE_CONTAINER 73 // Forward declaration for the overloads of __distance. 74 template <typename> struct _List_iterator; 75 template <typename> struct _List_const_iterator; 76_GLIBCXX_END_NAMESPACE_CONTAINER 77 78 template<typename _InputIterator> 79 inline _GLIBCXX14_CONSTEXPR 80 typename iterator_traits<_InputIterator>::difference_type 81 __distance(_InputIterator __first, _InputIterator __last, 82 input_iterator_tag) 83 { 84 // concept requirements 85 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 86 87 typename iterator_traits<_InputIterator>::difference_type __n = 0; 88 while (__first != __last) 89 { 90 ++__first; 91 ++__n; 92 } 93 return __n; 94 } 95 96 template<typename _RandomAccessIterator> 97 inline _GLIBCXX14_CONSTEXPR 98 typename iterator_traits<_RandomAccessIterator>::difference_type 99 __distance(_RandomAccessIterator __first, _RandomAccessIterator __last, 100 random_access_iterator_tag) 101 { 102 // concept requirements 103 __glibcxx_function_requires(_RandomAccessIteratorConcept< 104 _RandomAccessIterator>) 105 return __last - __first; 106 } 107 108#if _GLIBCXX_USE_CXX11_ABI 109 // Forward declaration because of the qualified call in distance. 110 template<typename _Tp> 111 ptrdiff_t 112 __distance(_GLIBCXX_STD_C::_List_iterator<_Tp>, 113 _GLIBCXX_STD_C::_List_iterator<_Tp>, 114 input_iterator_tag); 115 116 template<typename _Tp> 117 ptrdiff_t 118 __distance(_GLIBCXX_STD_C::_List_const_iterator<_Tp>, 119 _GLIBCXX_STD_C::_List_const_iterator<_Tp>, 120 input_iterator_tag); 121#endif 122 123#if __cplusplus >= 201103L 124 // Give better error if std::distance called with a non-Cpp17InputIterator. 125 template<typename _OutputIterator> 126 void 127 __distance(_OutputIterator, _OutputIterator, output_iterator_tag) = delete; 128#endif 129 130 /** 131 * @brief A generalization of pointer arithmetic. 132 * @param __first An input iterator. 133 * @param __last An input iterator. 134 * @return The distance between them. 135 * 136 * Returns @c n such that __first + n == __last. This requires 137 * that @p __last must be reachable from @p __first. Note that @c 138 * n may be negative. 139 * 140 * For random access iterators, this uses their @c + and @c - operations 141 * and are constant time. For other %iterator classes they are linear time. 142 */ 143 template<typename _InputIterator> 144 _GLIBCXX_NODISCARD 145 inline _GLIBCXX17_CONSTEXPR 146 typename iterator_traits<_InputIterator>::difference_type 147 distance(_InputIterator __first, _InputIterator __last) 148 { 149 // concept requirements -- taken care of in __distance 150 return std::__distance(__first, __last, 151 std::__iterator_category(__first)); 152 } 153 154 template<typename _InputIterator, typename _Distance> 155 inline _GLIBCXX14_CONSTEXPR void 156 __advance(_InputIterator& __i, _Distance __n, input_iterator_tag) 157 { 158 // concept requirements 159 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 160 __glibcxx_assert(__n >= 0); 161 while (__n--) 162 ++__i; 163 } 164 165 template<typename _BidirectionalIterator, typename _Distance> 166 inline _GLIBCXX14_CONSTEXPR void 167 __advance(_BidirectionalIterator& __i, _Distance __n, 168 bidirectional_iterator_tag) 169 { 170 // concept requirements 171 __glibcxx_function_requires(_BidirectionalIteratorConcept< 172 _BidirectionalIterator>) 173 if (__n > 0) 174 while (__n--) 175 ++__i; 176 else 177 while (__n++) 178 --__i; 179 } 180 181 template<typename _RandomAccessIterator, typename _Distance> 182 inline _GLIBCXX14_CONSTEXPR void 183 __advance(_RandomAccessIterator& __i, _Distance __n, 184 random_access_iterator_tag) 185 { 186 // concept requirements 187 __glibcxx_function_requires(_RandomAccessIteratorConcept< 188 _RandomAccessIterator>) 189 if (__builtin_constant_p(__n) && __n == 1) 190 ++__i; 191 else if (__builtin_constant_p(__n) && __n == -1) 192 --__i; 193 else 194 __i += __n; 195 } 196 197#if __cplusplus >= 201103L 198 // Give better error if std::advance called with a non-Cpp17InputIterator. 199 template<typename _OutputIterator, typename _Distance> 200 void 201 __advance(_OutputIterator&, _Distance, output_iterator_tag) = delete; 202#endif 203 204 /** 205 * @brief A generalization of pointer arithmetic. 206 * @param __i An input iterator. 207 * @param __n The @a delta by which to change @p __i. 208 * @return Nothing. 209 * 210 * This increments @p i by @p n. For bidirectional and random access 211 * iterators, @p __n may be negative, in which case @p __i is decremented. 212 * 213 * For random access iterators, this uses their @c + and @c - operations 214 * and are constant time. For other %iterator classes they are linear time. 215 */ 216 template<typename _InputIterator, typename _Distance> 217 inline _GLIBCXX17_CONSTEXPR void 218 advance(_InputIterator& __i, _Distance __n) 219 { 220 // concept requirements -- taken care of in __advance 221 typename iterator_traits<_InputIterator>::difference_type __d = __n; 222 std::__advance(__i, __d, std::__iterator_category(__i)); 223 } 224 225#if __cplusplus >= 201103L 226 227 template<typename _InputIterator> 228 _GLIBCXX_NODISCARD 229 inline _GLIBCXX17_CONSTEXPR _InputIterator 230 next(_InputIterator __x, typename 231 iterator_traits<_InputIterator>::difference_type __n = 1) 232 { 233 // concept requirements 234 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 235 std::advance(__x, __n); 236 return __x; 237 } 238 239 template<typename _BidirectionalIterator> 240 _GLIBCXX_NODISCARD 241 inline _GLIBCXX17_CONSTEXPR _BidirectionalIterator 242 prev(_BidirectionalIterator __x, typename 243 iterator_traits<_BidirectionalIterator>::difference_type __n = 1) 244 { 245 // concept requirements 246 __glibcxx_function_requires(_BidirectionalIteratorConcept< 247 _BidirectionalIterator>) 248 std::advance(__x, -__n); 249 return __x; 250 } 251 252#endif // C++11 253 254_GLIBCXX_END_NAMESPACE_VERSION 255} // namespace 256 257#endif /* _STL_ITERATOR_BASE_FUNCS_H */ 258