| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
#ifndef _STL_ITERATOR_BASE_FUNCS_H |
| 60 |
#define _STL_ITERATOR_BASE_FUNCS_H 1 |
| 61 |
|
| 62 |
#ifdef _GLIBCXX_SYSHDR |
| 63 |
#pragma GCC system_header |
| 64 |
#endif |
| 65 |
|
| 66 |
#include <bits/concept_check.h> |
| 67 |
#include <debug/assertions.h> |
| 68 |
#include <bits/stl_iterator_base_types.h> |
| 69 |
|
| 70 |
namespace std _GLIBCXX_VISIBILITY(default) |
| 71 |
{ |
| 72 |
_GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 73 |
|
| 74 |
_GLIBCXX_BEGIN_NAMESPACE_CONTAINER |
| 75 |
|
| 76 |
template <typename> struct _List_iterator; |
| 77 |
template <typename> struct _List_const_iterator; |
| 78 |
_GLIBCXX_END_NAMESPACE_CONTAINER |
| 79 |
|
| 80 |
template<typename _InputIterator> |
| 81 |
inline _GLIBCXX14_CONSTEXPR |
| 82 |
typename iterator_traits<_InputIterator>::difference_type |
| 83 |
__distance(_InputIterator __first, _InputIterator __last, |
| 84 |
input_iterator_tag) |
| 85 |
{ |
| 86 |
|
| 87 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
| 88 |
|
| 89 |
typename iterator_traits<_InputIterator>::difference_type __n = 0; |
| 90 |
while (__first != __last) |
| 91 |
{ |
| 92 |
++__first; |
| 93 |
++__n; |
| 94 |
} |
| 95 |
return __n; |
| 96 |
} |
| 97 |
|
| 98 |
template<typename _RandomAccessIterator> |
| 99 |
__attribute__((__always_inline__)) |
| 100 |
inline _GLIBCXX14_CONSTEXPR |
| 101 |
typename iterator_traits<_RandomAccessIterator>::difference_type |
| 102 |
__distance(_RandomAccessIterator __first, _RandomAccessIterator __last, |
| 103 |
random_access_iterator_tag) |
| 104 |
{ |
| 105 |
|
| 106 |
__glibcxx_function_requires(_RandomAccessIteratorConcept< |
| 107 |
_RandomAccessIterator>) |
| 108 |
return __last - __first; |
| 109 |
} |
| 110 |
|
| 111 |
#if _GLIBCXX_USE_CXX11_ABI |
| 112 |
|
| 113 |
template<typename _Tp> |
| 114 |
ptrdiff_t |
| 115 |
__distance(_GLIBCXX_STD_C::_List_iterator<_Tp>, |
| 116 |
_GLIBCXX_STD_C::_List_iterator<_Tp>, |
| 117 |
input_iterator_tag); |
| 118 |
|
| 119 |
template<typename _Tp> |
| 120 |
ptrdiff_t |
| 121 |
__distance(_GLIBCXX_STD_C::_List_const_iterator<_Tp>, |
| 122 |
_GLIBCXX_STD_C::_List_const_iterator<_Tp>, |
| 123 |
input_iterator_tag); |
| 124 |
#endif |
| 125 |
|
| 126 |
#if __cplusplus >= 201103L |
| 127 |
|
| 128 |
template<typename _OutputIterator> |
| 129 |
void |
| 130 |
__distance(_OutputIterator, _OutputIterator, output_iterator_tag) = delete; |
| 131 |
#endif |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
template<typename _InputIterator> |
| 147 |
_GLIBCXX_NODISCARD __attribute__((__always_inline__)) |
| 148 |
inline _GLIBCXX17_CONSTEXPR |
| 149 |
typename iterator_traits<_InputIterator>::difference_type |
| 150 |
distance(_InputIterator __first, _InputIterator __last) |
| 151 |
{ |
| 152 |
|
| 153 |
return std::__distance(__first, __last, |
| 154 |
std::__iterator_category(__first)); |
| 155 |
} |
| 156 |
|
| 157 |
template<typename _InputIterator, typename _Distance> |
| 158 |
inline _GLIBCXX14_CONSTEXPR void |
| 159 |
__advance(_InputIterator& __i, _Distance __n, input_iterator_tag) |
| 160 |
{ |
| 161 |
|
| 162 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
| 163 |
__glibcxx_assert(__n >= 0); |
| 164 |
while (__n-- > 0) |
| 165 |
++__i; |
| 166 |
} |
| 167 |
|
| 168 |
template<typename _BidirectionalIterator, typename _Distance> |
| 169 |
inline _GLIBCXX14_CONSTEXPR void |
| 170 |
__advance(_BidirectionalIterator& __i, _Distance __n, |
| 171 |
bidirectional_iterator_tag) |
| 172 |
{ |
| 173 |
|
| 174 |
__glibcxx_function_requires(_BidirectionalIteratorConcept< |
| 175 |
_BidirectionalIterator>) |
| 176 |
if (__n > 0) |
| 177 |
while (__n--) |
| 178 |
++__i; |
| 179 |
else |
| 180 |
while (__n++) |
| 181 |
--__i; |
| 182 |
} |
| 183 |
|
| 184 |
template<typename _RandomAccessIterator, typename _Distance> |
| 185 |
inline _GLIBCXX14_CONSTEXPR void |
| 186 |
__advance(_RandomAccessIterator& __i, _Distance __n, |
| 187 |
random_access_iterator_tag) |
| 188 |
{ |
| 189 |
|
| 190 |
__glibcxx_function_requires(_RandomAccessIteratorConcept< |
| 191 |
_RandomAccessIterator>) |
| 192 |
if (__builtin_constant_p(__n) && __n == 1) |
| 193 |
++__i; |
| 194 |
else if (__builtin_constant_p(__n) && __n == -1) |
| 195 |
--__i; |
| 196 |
else |
| 197 |
__i += __n; |
| 198 |
} |
| 199 |
|
| 200 |
#if __cplusplus >= 201103L |
| 201 |
|
| 202 |
template<typename _OutputIterator, typename _Distance> |
| 203 |
void |
| 204 |
__advance(_OutputIterator&, _Distance, output_iterator_tag) = delete; |
| 205 |
#endif |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
template<typename _InputIterator, typename _Distance> |
| 220 |
__attribute__((__always_inline__)) |
| 221 |
inline _GLIBCXX17_CONSTEXPR void |
| 222 |
advance(_InputIterator& __i, _Distance __n) |
| 223 |
{ |
| 224 |
|
| 225 |
typename iterator_traits<_InputIterator>::difference_type __d = __n; |
| 226 |
std::__advance(__i, __d, std::__iterator_category(__i)); |
| 227 |
} |
| 228 |
|
| 229 |
#if __cplusplus >= 201103L |
| 230 |
|
| 231 |
template<typename _InputIterator> |
| 232 |
_GLIBCXX_NODISCARD [[__gnu__::__always_inline__]] |
| 233 |
inline _GLIBCXX17_CONSTEXPR _InputIterator |
| 234 |
next(_InputIterator __x, typename |
| 235 |
iterator_traits<_InputIterator>::difference_type __n = 1) |
| 236 |
{ |
| 237 |
|
| 238 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
| 239 |
std::advance(__x, __n); |
| 240 |
return __x; |
| 241 |
} |
| 242 |
|
| 243 |
template<typename _BidirectionalIterator> |
| 244 |
_GLIBCXX_NODISCARD [[__gnu__::__always_inline__]] |
| 245 |
inline _GLIBCXX17_CONSTEXPR _BidirectionalIterator |
| 246 |
prev(_BidirectionalIterator __x, typename |
| 247 |
iterator_traits<_BidirectionalIterator>::difference_type __n = 1) |
| 248 |
{ |
| 249 |
|
| 250 |
__glibcxx_function_requires(_BidirectionalIteratorConcept< |
| 251 |
_BidirectionalIterator>) |
| 252 |
std::advance(__x, -__n); |
| 253 |
return __x; |
| 254 |
} |
| 255 |
|
| 256 |
#endif |
| 257 |
|
| 258 |
_GLIBCXX_END_NAMESPACE_VERSION |
| 259 |
} |
| 260 |
|
| 261 |
#endif |