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 | #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_CONTAINER |
70 | |
71 | template <typename> struct _List_iterator; |
72 | template <typename> struct _List_const_iterator; |
73 | _GLIBCXX_END_NAMESPACE_CONTAINER |
74 | |
75 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
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 | |
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 | |
102 | __glibcxx_function_requires(_RandomAccessIteratorConcept< |
103 | _RandomAccessIterator>) |
104 | return __last - __first; |
105 | } |
106 | |
107 | #if _GLIBCXX_USE_CXX11_ABI |
108 | |
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 | |
123 | |
124 | |
125 | |
126 | |
127 | |
128 | |
129 | |
130 | |
131 | |
132 | |
133 | |
134 | |
135 | template<typename _InputIterator> |
136 | inline _GLIBCXX17_CONSTEXPR |
137 | typename iterator_traits<_InputIterator>::difference_type |
138 | distance(_InputIterator __first, _InputIterator __last) |
139 | { |
140 | |
141 | return std::__distance(__first, __last, |
142 | std::__iterator_category(__first)); |
143 | } |
144 | |
145 | template<typename _InputIterator, typename _Distance> |
146 | inline _GLIBCXX14_CONSTEXPR void |
147 | __advance(_InputIterator& __i, _Distance __n, input_iterator_tag) |
148 | { |
149 | |
150 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
151 | __glibcxx_assert(__n >= 0); |
152 | while (__n--) |
153 | ++__i; |
154 | } |
155 | |
156 | template<typename _BidirectionalIterator, typename _Distance> |
157 | inline _GLIBCXX14_CONSTEXPR void |
158 | __advance(_BidirectionalIterator& __i, _Distance __n, |
159 | bidirectional_iterator_tag) |
160 | { |
161 | |
162 | __glibcxx_function_requires(_BidirectionalIteratorConcept< |
163 | _BidirectionalIterator>) |
164 | if (__n > 0) |
165 | while (__n--) |
166 | ++__i; |
167 | else |
168 | while (__n++) |
169 | --__i; |
170 | } |
171 | |
172 | template<typename _RandomAccessIterator, typename _Distance> |
173 | inline _GLIBCXX14_CONSTEXPR void |
174 | __advance(_RandomAccessIterator& __i, _Distance __n, |
175 | random_access_iterator_tag) |
176 | { |
177 | |
178 | __glibcxx_function_requires(_RandomAccessIteratorConcept< |
179 | _RandomAccessIterator>) |
180 | __i += __n; |
181 | } |
182 | |
183 | |
184 | |
185 | |
186 | |
187 | |
188 | |
189 | |
190 | |
191 | |
192 | |
193 | |
194 | |
195 | template<typename _InputIterator, typename _Distance> |
196 | inline _GLIBCXX17_CONSTEXPR void |
197 | advance(_InputIterator& __i, _Distance __n) |
198 | { |
199 | |
200 | typename iterator_traits<_InputIterator>::difference_type __d = __n; |
201 | std::__advance(__i, __d, std::__iterator_category(__i)); |
202 | } |
203 | |
204 | #if __cplusplus >= 201103L |
205 | |
206 | template<typename _ForwardIterator> |
207 | inline _GLIBCXX17_CONSTEXPR _ForwardIterator |
208 | next(_ForwardIterator __x, typename |
209 | iterator_traits<_ForwardIterator>::difference_type __n = 1) |
210 | { |
211 | |
212 | __glibcxx_function_requires(_ForwardIteratorConcept< |
213 | _ForwardIterator>) |
214 | std::advance(__x, __n); |
215 | return __x; |
216 | } |
217 | |
218 | template<typename _BidirectionalIterator> |
219 | inline _GLIBCXX17_CONSTEXPR _BidirectionalIterator |
220 | prev(_BidirectionalIterator __x, typename |
221 | iterator_traits<_BidirectionalIterator>::difference_type __n = 1) |
222 | { |
223 | |
224 | __glibcxx_function_requires(_BidirectionalIteratorConcept< |
225 | _BidirectionalIterator>) |
226 | std::advance(__x, -__n); |
227 | return __x; |
228 | } |
229 | |
230 | #endif |
231 | |
232 | _GLIBCXX_END_NAMESPACE_VERSION |
233 | } |
234 | |
235 | #endif |
236 | |