]>
Commit | Line | Data |
---|---|---|
c60cd1dc | 1 | // Core algorithmic facilities -*- C++ -*- |
42526146 | 2 | |
a2fe9203 | 3 | // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 |
0002d5d2 | 4 | // Free Software Foundation, Inc. |
42526146 PE |
5 | // |
6 | // This file is part of the GNU ISO C++ Library. This library is free | |
7 | // software; you can redistribute it and/or modify it under the | |
8 | // terms of the GNU General Public License as published by the | |
748086b7 | 9 | // Free Software Foundation; either version 3, or (at your option) |
42526146 PE |
10 | // any later version. |
11 | ||
12 | // This library is distributed in the hope that it will be useful, | |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU General Public License for more details. | |
16 | ||
748086b7 JJ |
17 | // Under Section 7 of GPL version 3, you are granted additional |
18 | // permissions described in the GCC Runtime Library Exception, version | |
19 | // 3.1, as published by the Free Software Foundation. | |
42526146 | 20 | |
748086b7 JJ |
21 | // You should have received a copy of the GNU General Public License and |
22 | // a copy of the GCC Runtime Library Exception along with this program; | |
23 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
24 | // <http://www.gnu.org/licenses/>. | |
42526146 | 25 | |
725dc051 BK |
26 | /* |
27 | * | |
28 | * Copyright (c) 1994 | |
29 | * Hewlett-Packard Company | |
30 | * | |
31 | * Permission to use, copy, modify, distribute and sell this software | |
32 | * and its documentation for any purpose is hereby granted without fee, | |
33 | * provided that the above copyright notice appear in all copies and | |
34 | * that both that copyright notice and this permission notice appear | |
35 | * in supporting documentation. Hewlett-Packard Company makes no | |
36 | * representations about the suitability of this software for any | |
37 | * purpose. It is provided "as is" without express or implied warranty. | |
38 | * | |
39 | * | |
40 | * Copyright (c) 1996-1998 | |
41 | * Silicon Graphics Computer Systems, Inc. | |
42 | * | |
43 | * Permission to use, copy, modify, distribute and sell this software | |
44 | * and its documentation for any purpose is hereby granted without fee, | |
45 | * provided that the above copyright notice appear in all copies and | |
46 | * that both that copyright notice and this permission notice appear | |
47 | * in supporting documentation. Silicon Graphics makes no | |
48 | * representations about the suitability of this software for any | |
49 | * purpose. It is provided "as is" without express or implied warranty. | |
50 | */ | |
51 | ||
729e3d3f PE |
52 | /** @file stl_algobase.h |
53 | * This is an internal header file, included by other library headers. | |
54 | * You should not attempt to use it directly. | |
725dc051 BK |
55 | */ |
56 | ||
046d30f4 PC |
57 | #ifndef _STL_ALGOBASE_H |
58 | #define _STL_ALGOBASE_H 1 | |
725dc051 | 59 | |
9cfeea6e | 60 | #include <bits/c++config.h> |
54c1bf78 | 61 | #include <cstddef> |
39b8cd70 | 62 | #include <bits/functexcept.h> |
badd64ad | 63 | #include <bits/cpp_type_traits.h> |
105c6331 | 64 | #include <ext/type_traits.h> |
6725add5 | 65 | #include <ext/numeric_traits.h> |
8ad7097c | 66 | #include <bits/stl_pair.h> |
4f39bf5c | 67 | #include <bits/stl_iterator_base_types.h> |
30a20a1e | 68 | #include <bits/stl_iterator_base_funcs.h> |
725dc051 | 69 | #include <bits/stl_iterator.h> |
30a20a1e | 70 | #include <bits/concept_check.h> |
285b36d6 | 71 | #include <debug/debug.h> |
8ad7097c | 72 | #include <bits/move.h> // For std::swap and _GLIBCXX_MOVE |
3a6b0f54 | 73 | |
3cbc7af0 | 74 | _GLIBCXX_BEGIN_NAMESPACE(std) |
575665ff | 75 | |
575665ff CJ |
76 | // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a |
77 | // nutshell, we are partially implementing the resolution of DR 187, | |
78 | // when it's safe, i.e., the value_types are equal. | |
79 | template<bool _BoolType> | |
80 | struct __iter_swap | |
81 | { | |
82 | template<typename _ForwardIterator1, typename _ForwardIterator2> | |
83 | static void | |
84 | iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b) | |
85 | { | |
86 | typedef typename iterator_traits<_ForwardIterator1>::value_type | |
87 | _ValueType1; | |
3a6b0f54 PC |
88 | _ValueType1 __tmp = _GLIBCXX_MOVE(*__a); |
89 | *__a = _GLIBCXX_MOVE(*__b); | |
90 | *__b = _GLIBCXX_MOVE(__tmp); | |
575665ff CJ |
91 | } |
92 | }; | |
93 | ||
94 | template<> | |
95 | struct __iter_swap<true> | |
96 | { | |
97 | template<typename _ForwardIterator1, typename _ForwardIterator2> | |
98 | static void | |
99 | iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b) | |
100 | { | |
101 | swap(*__a, *__b); | |
102 | } | |
103 | }; | |
104 | ||
729e3d3f PE |
105 | /** |
106 | * @brief Swaps the contents of two iterators. | |
5b9daa7e | 107 | * @ingroup mutating_algorithms |
729e3d3f PE |
108 | * @param a An iterator. |
109 | * @param b Another iterator. | |
110 | * @return Nothing. | |
111 | * | |
112 | * This function swaps the values pointed to by two iterators, not the | |
113 | * iterators themselves. | |
114 | */ | |
08addde6 | 115 | template<typename _ForwardIterator1, typename _ForwardIterator2> |
02d92e3b | 116 | inline void |
08addde6 | 117 | iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b) |
02d92e3b | 118 | { |
ffa67767 PC |
119 | typedef typename iterator_traits<_ForwardIterator1>::value_type |
120 | _ValueType1; | |
121 | typedef typename iterator_traits<_ForwardIterator2>::value_type | |
122 | _ValueType2; | |
02d92e3b SW |
123 | |
124 | // concept requirements | |
ffa67767 PC |
125 | __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< |
126 | _ForwardIterator1>) | |
127 | __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< | |
128 | _ForwardIterator2>) | |
129 | __glibcxx_function_requires(_ConvertibleConcept<_ValueType1, | |
130 | _ValueType2>) | |
131 | __glibcxx_function_requires(_ConvertibleConcept<_ValueType2, | |
132 | _ValueType1>) | |
aed63147 CJ |
133 | |
134 | typedef typename iterator_traits<_ForwardIterator1>::reference | |
135 | _ReferenceType1; | |
136 | typedef typename iterator_traits<_ForwardIterator2>::reference | |
137 | _ReferenceType2; | |
d22a3166 PC |
138 | std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value |
139 | && __are_same<_ValueType1&, _ReferenceType1>::__value | |
140 | && __are_same<_ValueType2&, _ReferenceType2>::__value>:: | |
575665ff | 141 | iter_swap(__a, __b); |
02d92e3b SW |
142 | } |
143 | ||
91b0b94a PC |
144 | /** |
145 | * @brief Swap the elements of two sequences. | |
5b9daa7e | 146 | * @ingroup mutating_algorithms |
91b0b94a PC |
147 | * @param first1 A forward iterator. |
148 | * @param last1 A forward iterator. | |
149 | * @param first2 A forward iterator. | |
150 | * @return An iterator equal to @p first2+(last1-first1). | |
151 | * | |
152 | * Swaps each element in the range @p [first1,last1) with the | |
153 | * corresponding element in the range @p [first2,(last1-first1)). | |
154 | * The ranges must not overlap. | |
155 | */ | |
156 | template<typename _ForwardIterator1, typename _ForwardIterator2> | |
157 | _ForwardIterator2 | |
158 | swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1, | |
159 | _ForwardIterator2 __first2) | |
160 | { | |
161 | // concept requirements | |
162 | __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< | |
163 | _ForwardIterator1>) | |
164 | __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< | |
165 | _ForwardIterator2>) | |
91b0b94a PC |
166 | __glibcxx_requires_valid_range(__first1, __last1); |
167 | ||
168 | for (; __first1 != __last1; ++__first1, ++__first2) | |
169 | std::iter_swap(__first1, __first2); | |
170 | return __first2; | |
171 | } | |
172 | ||
729e3d3f PE |
173 | /** |
174 | * @brief This does what you think it does. | |
5b9daa7e | 175 | * @ingroup sorting_algorithms |
729e3d3f PE |
176 | * @param a A thing of arbitrary type. |
177 | * @param b Another thing of arbitrary type. | |
178 | * @return The lesser of the parameters. | |
179 | * | |
180 | * This is the simple classic generic implementation. It will work on | |
181 | * temporary expressions, since they are only evaluated once, unlike a | |
182 | * preprocessor macro. | |
183 | */ | |
02d92e3b SW |
184 | template<typename _Tp> |
185 | inline const _Tp& | |
186 | min(const _Tp& __a, const _Tp& __b) | |
187 | { | |
188 | // concept requirements | |
3d7c150e | 189 | __glibcxx_function_requires(_LessThanComparableConcept<_Tp>) |
02d92e3b | 190 | //return __b < __a ? __b : __a; |
ffa67767 PC |
191 | if (__b < __a) |
192 | return __b; | |
193 | return __a; | |
02d92e3b SW |
194 | } |
195 | ||
1b4a6975 PE |
196 | /** |
197 | * @brief This does what you think it does. | |
5b9daa7e | 198 | * @ingroup sorting_algorithms |
1b4a6975 PE |
199 | * @param a A thing of arbitrary type. |
200 | * @param b Another thing of arbitrary type. | |
201 | * @return The greater of the parameters. | |
202 | * | |
203 | * This is the simple classic generic implementation. It will work on | |
204 | * temporary expressions, since they are only evaluated once, unlike a | |
205 | * preprocessor macro. | |
206 | */ | |
02d92e3b SW |
207 | template<typename _Tp> |
208 | inline const _Tp& | |
ed6814f7 | 209 | max(const _Tp& __a, const _Tp& __b) |
02d92e3b SW |
210 | { |
211 | // concept requirements | |
3d7c150e | 212 | __glibcxx_function_requires(_LessThanComparableConcept<_Tp>) |
02d92e3b | 213 | //return __a < __b ? __b : __a; |
ffa67767 PC |
214 | if (__a < __b) |
215 | return __b; | |
216 | return __a; | |
02d92e3b SW |
217 | } |
218 | ||
1b4a6975 PE |
219 | /** |
220 | * @brief This does what you think it does. | |
5b9daa7e | 221 | * @ingroup sorting_algorithms |
1b4a6975 PE |
222 | * @param a A thing of arbitrary type. |
223 | * @param b Another thing of arbitrary type. | |
aac2878e | 224 | * @param comp A @link comparison_functors comparison functor@endlink. |
1b4a6975 PE |
225 | * @return The lesser of the parameters. |
226 | * | |
227 | * This will work on temporary expressions, since they are only evaluated | |
228 | * once, unlike a preprocessor macro. | |
229 | */ | |
02d92e3b SW |
230 | template<typename _Tp, typename _Compare> |
231 | inline const _Tp& | |
232 | min(const _Tp& __a, const _Tp& __b, _Compare __comp) | |
233 | { | |
234 | //return __comp(__b, __a) ? __b : __a; | |
ffa67767 PC |
235 | if (__comp(__b, __a)) |
236 | return __b; | |
237 | return __a; | |
02d92e3b SW |
238 | } |
239 | ||
1b4a6975 PE |
240 | /** |
241 | * @brief This does what you think it does. | |
5b9daa7e | 242 | * @ingroup sorting_algorithms |
1b4a6975 PE |
243 | * @param a A thing of arbitrary type. |
244 | * @param b Another thing of arbitrary type. | |
aac2878e | 245 | * @param comp A @link comparison_functors comparison functor@endlink. |
1b4a6975 PE |
246 | * @return The greater of the parameters. |
247 | * | |
248 | * This will work on temporary expressions, since they are only evaluated | |
249 | * once, unlike a preprocessor macro. | |
250 | */ | |
02d92e3b SW |
251 | template<typename _Tp, typename _Compare> |
252 | inline const _Tp& | |
253 | max(const _Tp& __a, const _Tp& __b, _Compare __comp) | |
254 | { | |
255 | //return __comp(__a, __b) ? __b : __a; | |
ffa67767 PC |
256 | if (__comp(__a, __b)) |
257 | return __b; | |
258 | return __a; | |
02d92e3b SW |
259 | } |
260 | ||
d22a3166 | 261 | |
a2fe9203 FD |
262 | // If _Iterator has a base returns it otherwise _Iterator is returned |
263 | // untouched | |
264 | template<typename _Iterator, bool _HasBase> | |
6989b63f | 265 | struct _Iter_base |
d22a3166 | 266 | { |
6989b63f PC |
267 | typedef _Iterator iterator_type; |
268 | static iterator_type | |
269 | _S_base(_Iterator __it) | |
d22a3166 PC |
270 | { return __it; } |
271 | }; | |
272 | ||
273 | template<typename _Iterator> | |
6989b63f | 274 | struct _Iter_base<_Iterator, true> |
d22a3166 | 275 | { |
6989b63f PC |
276 | typedef typename _Iterator::iterator_type iterator_type; |
277 | static iterator_type | |
278 | _S_base(_Iterator __it) | |
f0112db9 PC |
279 | { return __it.base(); } |
280 | }; | |
281 | ||
a2fe9203 FD |
282 | // If _Iterator is a __normal_iterator return its base (a plain pointer, |
283 | // normally) otherwise return it untouched. See copy, fill, ... | |
284 | template<typename _Iterator> | |
6989b63f PC |
285 | struct _Niter_base |
286 | : _Iter_base<_Iterator, __is_normal_iterator<_Iterator>::__value> | |
a2fe9203 | 287 | { }; |
f0112db9 | 288 | |
6989b63f PC |
289 | template<typename _Iterator> |
290 | inline typename _Niter_base<_Iterator>::iterator_type | |
291 | __niter_base(_Iterator __it) | |
292 | { return std::_Niter_base<_Iterator>::_S_base(__it); } | |
293 | ||
a2fe9203 | 294 | // Likewise, for move_iterator. |
f0112db9 | 295 | template<typename _Iterator> |
6989b63f PC |
296 | struct _Miter_base |
297 | : _Iter_base<_Iterator, __is_move_iterator<_Iterator>::__value> | |
a2fe9203 | 298 | { }; |
d22a3166 | 299 | |
6989b63f PC |
300 | template<typename _Iterator> |
301 | inline typename _Miter_base<_Iterator>::iterator_type | |
302 | __miter_base(_Iterator __it) | |
303 | { return std::_Miter_base<_Iterator>::_S_base(__it); } | |
304 | ||
5e91e92e | 305 | // All of these auxiliary structs serve two purposes. (1) Replace |
02d92e3b SW |
306 | // calls to copy with memmove whenever possible. (Memmove, not memcpy, |
307 | // because the input and output ranges are permitted to overlap.) | |
308 | // (2) If we're using random access iterators, then write the loop as | |
309 | // a for loop with an explicit count. | |
310 | ||
5f6d5f0a | 311 | template<bool, bool, typename> |
3c167a8b | 312 | struct __copy_move |
02d92e3b | 313 | { |
695e0fbf PC |
314 | template<typename _II, typename _OI> |
315 | static _OI | |
3c167a8b | 316 | __copy_m(_II __first, _II __last, _OI __result) |
695e0fbf PC |
317 | { |
318 | for (; __first != __last; ++__result, ++__first) | |
5f6d5f0a PC |
319 | *__result = *__first; |
320 | return __result; | |
321 | } | |
322 | }; | |
323 | ||
324 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ | |
325 | template<typename _Category> | |
326 | struct __copy_move<true, false, _Category> | |
327 | { | |
328 | template<typename _II, typename _OI> | |
329 | static _OI | |
330 | __copy_m(_II __first, _II __last, _OI __result) | |
331 | { | |
332 | for (; __first != __last; ++__result, ++__first) | |
333 | *__result = std::move(*__first); | |
334 | return __result; | |
335 | } | |
336 | }; | |
337 | #endif | |
338 | ||
339 | template<> | |
340 | struct __copy_move<false, false, random_access_iterator_tag> | |
341 | { | |
342 | template<typename _II, typename _OI> | |
343 | static _OI | |
344 | __copy_m(_II __first, _II __last, _OI __result) | |
345 | { | |
346 | typedef typename iterator_traits<_II>::difference_type _Distance; | |
347 | for(_Distance __n = __last - __first; __n > 0; --__n) | |
348 | { | |
349 | *__result = *__first; | |
350 | ++__first; | |
351 | ++__result; | |
352 | } | |
695e0fbf PC |
353 | return __result; |
354 | } | |
355 | }; | |
02d92e3b | 356 | |
5f6d5f0a PC |
357 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
358 | template<> | |
359 | struct __copy_move<true, false, random_access_iterator_tag> | |
02d92e3b | 360 | { |
695e0fbf PC |
361 | template<typename _II, typename _OI> |
362 | static _OI | |
3c167a8b | 363 | __copy_m(_II __first, _II __last, _OI __result) |
695e0fbf PC |
364 | { |
365 | typedef typename iterator_traits<_II>::difference_type _Distance; | |
366 | for(_Distance __n = __last - __first; __n > 0; --__n) | |
367 | { | |
5f6d5f0a | 368 | *__result = std::move(*__first); |
695e0fbf PC |
369 | ++__first; |
370 | ++__result; | |
371 | } | |
372 | return __result; | |
46c4e5d6 | 373 | } |
695e0fbf | 374 | }; |
5f6d5f0a | 375 | #endif |
02d92e3b | 376 | |
f0112db9 PC |
377 | template<bool _IsMove> |
378 | struct __copy_move<_IsMove, true, random_access_iterator_tag> | |
02d92e3b | 379 | { |
695e0fbf PC |
380 | template<typename _Tp> |
381 | static _Tp* | |
3c167a8b PC |
382 | __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result) |
383 | { | |
f7d601a5 PC |
384 | const ptrdiff_t _Num = __last - __first; |
385 | if (_Num) | |
386 | __builtin_memmove(__result, __first, sizeof(_Tp) * _Num); | |
387 | return __result + _Num; | |
695e0fbf PC |
388 | } |
389 | }; | |
02d92e3b | 390 | |
f0112db9 | 391 | template<bool _IsMove, typename _II, typename _OI> |
695e0fbf | 392 | inline _OI |
3c167a8b | 393 | __copy_move_a(_II __first, _II __last, _OI __result) |
695e0fbf PC |
394 | { |
395 | typedef typename iterator_traits<_II>::value_type _ValueTypeI; | |
396 | typedef typename iterator_traits<_OI>::value_type _ValueTypeO; | |
397 | typedef typename iterator_traits<_II>::iterator_category _Category; | |
ff2ea587 | 398 | const bool __simple = (__is_pod(_ValueTypeI) |
4d73fac9 PC |
399 | && __is_pointer<_II>::__value |
400 | && __is_pointer<_OI>::__value | |
401 | && __are_same<_ValueTypeI, _ValueTypeO>::__value); | |
695e0fbf | 402 | |
f0112db9 | 403 | return std::__copy_move<_IsMove, __simple, |
3c167a8b | 404 | _Category>::__copy_m(__first, __last, __result); |
695e0fbf | 405 | } |
02d92e3b | 406 | |
0002d5d2 | 407 | // Helpers for streambuf iterators (either istream or ostream). |
39b8cd70 PC |
408 | // NB: avoid including <iosfwd>, relatively large. |
409 | template<typename _CharT> | |
410 | struct char_traits; | |
411 | ||
412 | template<typename _CharT, typename _Traits> | |
413 | class istreambuf_iterator; | |
414 | ||
415 | template<typename _CharT, typename _Traits> | |
416 | class ostreambuf_iterator; | |
417 | ||
f0112db9 | 418 | template<bool _IsMove, typename _CharT> |
f56fe8ff | 419 | typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, |
39b8cd70 | 420 | ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type |
f0112db9 PC |
421 | __copy_move_a2(_CharT*, _CharT*, |
422 | ostreambuf_iterator<_CharT, char_traits<_CharT> >); | |
0002d5d2 | 423 | |
f0112db9 | 424 | template<bool _IsMove, typename _CharT> |
105c6331 | 425 | typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, |
39b8cd70 | 426 | ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type |
f0112db9 PC |
427 | __copy_move_a2(const _CharT*, const _CharT*, |
428 | ostreambuf_iterator<_CharT, char_traits<_CharT> >); | |
0002d5d2 | 429 | |
f0112db9 | 430 | template<bool _IsMove, typename _CharT> |
f56fe8ff PC |
431 | typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, |
432 | _CharT*>::__type | |
f0112db9 PC |
433 | __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >, |
434 | istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*); | |
435 | ||
436 | template<bool _IsMove, typename _II, typename _OI> | |
437 | inline _OI | |
438 | __copy_move_a2(_II __first, _II __last, _OI __result) | |
439 | { | |
6989b63f PC |
440 | return _OI(std::__copy_move_a<_IsMove>(std::__niter_base(__first), |
441 | std::__niter_base(__last), | |
442 | std::__niter_base(__result))); | |
f0112db9 | 443 | } |
0002d5d2 | 444 | |
1b4a6975 PE |
445 | /** |
446 | * @brief Copies the range [first,last) into result. | |
5b9daa7e | 447 | * @ingroup mutating_algorithms |
1b4a6975 PE |
448 | * @param first An input iterator. |
449 | * @param last An input iterator. | |
450 | * @param result An output iterator. | |
451 | * @return result + (first - last) | |
452 | * | |
453 | * This inline function will boil down to a call to @c memmove whenever | |
454 | * possible. Failing that, if random access iterators are passed, then the | |
455 | * loop count will be known (and therefore a candidate for compiler | |
119dbb1f JQ |
456 | * optimizations such as unrolling). Result may not be contained within |
457 | * [first,last); the copy_backward function should be used instead. | |
458 | * | |
459 | * Note that the end of the output range is permitted to be contained | |
460 | * within [first,last). | |
1b4a6975 | 461 | */ |
d22a3166 PC |
462 | template<typename _II, typename _OI> |
463 | inline _OI | |
464 | copy(_II __first, _II __last, _OI __result) | |
02d92e3b SW |
465 | { |
466 | // concept requirements | |
d22a3166 PC |
467 | __glibcxx_function_requires(_InputIteratorConcept<_II>) |
468 | __glibcxx_function_requires(_OutputIteratorConcept<_OI, | |
469 | typename iterator_traits<_II>::value_type>) | |
285b36d6 | 470 | __glibcxx_requires_valid_range(__first, __last); |
02d92e3b | 471 | |
f0112db9 | 472 | return (std::__copy_move_a2<__is_move_iterator<_II>::__value> |
6989b63f PC |
473 | (std::__miter_base(__first), std::__miter_base(__last), |
474 | __result)); | |
02d92e3b | 475 | } |
0002d5d2 | 476 | |
3c167a8b PC |
477 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
478 | /** | |
479 | * @brief Moves the range [first,last) into result. | |
5b9daa7e | 480 | * @ingroup mutating_algorithms |
3c167a8b PC |
481 | * @param first An input iterator. |
482 | * @param last An input iterator. | |
483 | * @param result An output iterator. | |
484 | * @return result + (first - last) | |
485 | * | |
486 | * This inline function will boil down to a call to @c memmove whenever | |
487 | * possible. Failing that, if random access iterators are passed, then the | |
488 | * loop count will be known (and therefore a candidate for compiler | |
489 | * optimizations such as unrolling). Result may not be contained within | |
490 | * [first,last); the move_backward function should be used instead. | |
491 | * | |
492 | * Note that the end of the output range is permitted to be contained | |
493 | * within [first,last). | |
494 | */ | |
495 | template<typename _II, typename _OI> | |
496 | inline _OI | |
497 | move(_II __first, _II __last, _OI __result) | |
498 | { | |
499 | // concept requirements | |
500 | __glibcxx_function_requires(_InputIteratorConcept<_II>) | |
501 | __glibcxx_function_requires(_OutputIteratorConcept<_OI, | |
502 | typename iterator_traits<_II>::value_type>) | |
503 | __glibcxx_requires_valid_range(__first, __last); | |
504 | ||
6989b63f PC |
505 | return std::__copy_move_a2<true>(std::__miter_base(__first), |
506 | std::__miter_base(__last), __result); | |
3c167a8b | 507 | } |
245a5fe5 PC |
508 | |
509 | #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp) | |
510 | #else | |
511 | #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp) | |
3c167a8b | 512 | #endif |
d22a3166 | 513 | |
5f6d5f0a | 514 | template<bool, bool, typename> |
3c167a8b | 515 | struct __copy_move_backward |
badd64ad PC |
516 | { |
517 | template<typename _BI1, typename _BI2> | |
518 | static _BI2 | |
3c167a8b PC |
519 | __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result) |
520 | { | |
badd64ad | 521 | while (__first != __last) |
5f6d5f0a | 522 | *--__result = *--__last; |
badd64ad PC |
523 | return __result; |
524 | } | |
02d92e3b SW |
525 | }; |
526 | ||
5f6d5f0a PC |
527 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
528 | template<typename _Category> | |
529 | struct __copy_move_backward<true, false, _Category> | |
530 | { | |
531 | template<typename _BI1, typename _BI2> | |
532 | static _BI2 | |
533 | __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result) | |
534 | { | |
535 | while (__first != __last) | |
536 | *--__result = std::move(*--__last); | |
537 | return __result; | |
538 | } | |
539 | }; | |
540 | #endif | |
541 | ||
542 | template<> | |
543 | struct __copy_move_backward<false, false, random_access_iterator_tag> | |
badd64ad PC |
544 | { |
545 | template<typename _BI1, typename _BI2> | |
546 | static _BI2 | |
3c167a8b PC |
547 | __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result) |
548 | { | |
badd64ad PC |
549 | typename iterator_traits<_BI1>::difference_type __n; |
550 | for (__n = __last - __first; __n > 0; --__n) | |
5f6d5f0a | 551 | *--__result = *--__last; |
badd64ad PC |
552 | return __result; |
553 | } | |
02d92e3b SW |
554 | }; |
555 | ||
5f6d5f0a PC |
556 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
557 | template<> | |
558 | struct __copy_move_backward<true, false, random_access_iterator_tag> | |
559 | { | |
560 | template<typename _BI1, typename _BI2> | |
561 | static _BI2 | |
562 | __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result) | |
563 | { | |
564 | typename iterator_traits<_BI1>::difference_type __n; | |
565 | for (__n = __last - __first; __n > 0; --__n) | |
566 | *--__result = std::move(*--__last); | |
567 | return __result; | |
568 | } | |
569 | }; | |
570 | #endif | |
571 | ||
f0112db9 PC |
572 | template<bool _IsMove> |
573 | struct __copy_move_backward<_IsMove, true, random_access_iterator_tag> | |
badd64ad PC |
574 | { |
575 | template<typename _Tp> | |
576 | static _Tp* | |
3c167a8b PC |
577 | __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result) |
578 | { | |
badd64ad | 579 | const ptrdiff_t _Num = __last - __first; |
f7d601a5 PC |
580 | if (_Num) |
581 | __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num); | |
badd64ad PC |
582 | return __result - _Num; |
583 | } | |
02d92e3b SW |
584 | }; |
585 | ||
f0112db9 | 586 | template<bool _IsMove, typename _BI1, typename _BI2> |
02d92e3b | 587 | inline _BI2 |
3c167a8b | 588 | __copy_move_backward_a(_BI1 __first, _BI1 __last, _BI2 __result) |
02d92e3b | 589 | { |
695e0fbf PC |
590 | typedef typename iterator_traits<_BI1>::value_type _ValueType1; |
591 | typedef typename iterator_traits<_BI2>::value_type _ValueType2; | |
badd64ad | 592 | typedef typename iterator_traits<_BI1>::iterator_category _Category; |
ff2ea587 | 593 | const bool __simple = (__is_pod(_ValueType1) |
4d73fac9 PC |
594 | && __is_pointer<_BI1>::__value |
595 | && __is_pointer<_BI2>::__value | |
596 | && __are_same<_ValueType1, _ValueType2>::__value); | |
badd64ad | 597 | |
f0112db9 | 598 | return std::__copy_move_backward<_IsMove, __simple, |
3c167a8b | 599 | _Category>::__copy_move_b(__first, |
e75ea710 PC |
600 | __last, |
601 | __result); | |
02d92e3b SW |
602 | } |
603 | ||
f0112db9 PC |
604 | template<bool _IsMove, typename _BI1, typename _BI2> |
605 | inline _BI2 | |
606 | __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result) | |
607 | { | |
608 | return _BI2(std::__copy_move_backward_a<_IsMove> | |
6989b63f PC |
609 | (std::__niter_base(__first), std::__niter_base(__last), |
610 | std::__niter_base(__result))); | |
f0112db9 PC |
611 | } |
612 | ||
1b4a6975 PE |
613 | /** |
614 | * @brief Copies the range [first,last) into result. | |
5b9daa7e | 615 | * @ingroup mutating_algorithms |
119dbb1f JQ |
616 | * @param first A bidirectional iterator. |
617 | * @param last A bidirectional iterator. | |
618 | * @param result A bidirectional iterator. | |
1b4a6975 PE |
619 | * @return result - (first - last) |
620 | * | |
621 | * The function has the same effect as copy, but starts at the end of the | |
622 | * range and works its way to the start, returning the start of the result. | |
623 | * This inline function will boil down to a call to @c memmove whenever | |
624 | * possible. Failing that, if random access iterators are passed, then the | |
625 | * loop count will be known (and therefore a candidate for compiler | |
626 | * optimizations such as unrolling). | |
119dbb1f JQ |
627 | * |
628 | * Result may not be in the range [first,last). Use copy instead. Note | |
629 | * that the start of the output range may overlap [first,last). | |
1b4a6975 | 630 | */ |
f0112db9 | 631 | template<typename _BI1, typename _BI2> |
02d92e3b SW |
632 | inline _BI2 |
633 | copy_backward(_BI1 __first, _BI1 __last, _BI2 __result) | |
634 | { | |
635 | // concept requirements | |
3d7c150e BK |
636 | __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>) |
637 | __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>) | |
638 | __glibcxx_function_requires(_ConvertibleConcept< | |
02d92e3b | 639 | typename iterator_traits<_BI1>::value_type, |
4d16bdbb | 640 | typename iterator_traits<_BI2>::value_type>) |
285b36d6 | 641 | __glibcxx_requires_valid_range(__first, __last); |
02d92e3b | 642 | |
f0112db9 | 643 | return (std::__copy_move_backward_a2<__is_move_iterator<_BI1>::__value> |
6989b63f PC |
644 | (std::__miter_base(__first), std::__miter_base(__last), |
645 | __result)); | |
02d92e3b SW |
646 | } |
647 | ||
3c167a8b PC |
648 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
649 | /** | |
650 | * @brief Moves the range [first,last) into result. | |
5b9daa7e | 651 | * @ingroup mutating_algorithms |
3c167a8b PC |
652 | * @param first A bidirectional iterator. |
653 | * @param last A bidirectional iterator. | |
654 | * @param result A bidirectional iterator. | |
655 | * @return result - (first - last) | |
656 | * | |
657 | * The function has the same effect as move, but starts at the end of the | |
658 | * range and works its way to the start, returning the start of the result. | |
659 | * This inline function will boil down to a call to @c memmove whenever | |
660 | * possible. Failing that, if random access iterators are passed, then the | |
661 | * loop count will be known (and therefore a candidate for compiler | |
662 | * optimizations such as unrolling). | |
663 | * | |
664 | * Result may not be in the range [first,last). Use move instead. Note | |
665 | * that the start of the output range may overlap [first,last). | |
666 | */ | |
667 | template<typename _BI1, typename _BI2> | |
668 | inline _BI2 | |
669 | move_backward(_BI1 __first, _BI1 __last, _BI2 __result) | |
670 | { | |
671 | // concept requirements | |
672 | __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>) | |
673 | __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>) | |
674 | __glibcxx_function_requires(_ConvertibleConcept< | |
675 | typename iterator_traits<_BI1>::value_type, | |
676 | typename iterator_traits<_BI2>::value_type>) | |
677 | __glibcxx_requires_valid_range(__first, __last); | |
678 | ||
6989b63f PC |
679 | return std::__copy_move_backward_a2<true>(std::__miter_base(__first), |
680 | std::__miter_base(__last), | |
681 | __result); | |
3c167a8b | 682 | } |
245a5fe5 PC |
683 | |
684 | #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp) | |
685 | #else | |
686 | #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp) | |
3c167a8b | 687 | #endif |
e9e90c1f | 688 | |
394033f8 PC |
689 | template<typename _ForwardIterator, typename _Tp> |
690 | inline typename | |
691 | __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type | |
692 | __fill_a(_ForwardIterator __first, _ForwardIterator __last, | |
693 | const _Tp& __value) | |
6e539e23 | 694 | { |
394033f8 PC |
695 | for (; __first != __last; ++__first) |
696 | *__first = __value; | |
697 | } | |
698 | ||
08addde6 | 699 | template<typename _ForwardIterator, typename _Tp> |
394033f8 PC |
700 | inline typename |
701 | __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type | |
b14f95a8 PC |
702 | __fill_a(_ForwardIterator __first, _ForwardIterator __last, |
703 | const _Tp& __value) | |
02d92e3b | 704 | { |
b14f95a8 | 705 | const _Tp __tmp = __value; |
394033f8 | 706 | for (; __first != __last; ++__first) |
b14f95a8 | 707 | *__first = __tmp; |
02d92e3b SW |
708 | } |
709 | ||
d22a3166 | 710 | // Specialization: for char types we can use memset. |
394033f8 PC |
711 | template<typename _Tp> |
712 | inline typename | |
713 | __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type | |
b14f95a8 PC |
714 | __fill_a(_Tp* __first, _Tp* __last, const _Tp& __c) |
715 | { | |
716 | const _Tp __tmp = __c; | |
717 | __builtin_memset(__first, static_cast<unsigned char>(__tmp), | |
718 | __last - __first); | |
719 | } | |
725dc051 | 720 | |
e9e90c1f PC |
721 | /** |
722 | * @brief Fills the range [first,last) with copies of value. | |
5b9daa7e | 723 | * @ingroup mutating_algorithms |
e9e90c1f PC |
724 | * @param first A forward iterator. |
725 | * @param last A forward iterator. | |
726 | * @param value A reference-to-const of arbitrary type. | |
727 | * @return Nothing. | |
728 | * | |
729 | * This function fills a range with copies of the same value. For char | |
730 | * types filling contiguous areas of memory, this becomes an inline call | |
731 | * to @c memset or @c wmemset. | |
732 | */ | |
733 | template<typename _ForwardIterator, typename _Tp> | |
734 | inline void | |
735 | fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) | |
736 | { | |
737 | // concept requirements | |
738 | __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< | |
739 | _ForwardIterator>) | |
740 | __glibcxx_requires_valid_range(__first, __last); | |
741 | ||
6989b63f PC |
742 | std::__fill_a(std::__niter_base(__first), std::__niter_base(__last), |
743 | __value); | |
e9e90c1f PC |
744 | } |
745 | ||
6e539e23 | 746 | template<typename _OutputIterator, typename _Size, typename _Tp> |
394033f8 PC |
747 | inline typename |
748 | __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type | |
749 | __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value) | |
02d92e3b | 750 | { |
394033f8 PC |
751 | for (; __n > 0; --__n, ++__first) |
752 | *__first = __value; | |
753 | return __first; | |
02d92e3b SW |
754 | } |
755 | ||
394033f8 PC |
756 | template<typename _OutputIterator, typename _Size, typename _Tp> |
757 | inline typename | |
758 | __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type | |
b14f95a8 | 759 | __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value) |
02d92e3b | 760 | { |
b14f95a8 | 761 | const _Tp __tmp = __value; |
394033f8 | 762 | for (; __n > 0; --__n, ++__first) |
b14f95a8 | 763 | *__first = __tmp; |
394033f8 | 764 | return __first; |
02d92e3b SW |
765 | } |
766 | ||
394033f8 PC |
767 | template<typename _Size, typename _Tp> |
768 | inline typename | |
769 | __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, _Tp*>::__type | |
b14f95a8 | 770 | __fill_n_a(_Tp* __first, _Size __n, const _Tp& __c) |
e9e90c1f | 771 | { |
394033f8 | 772 | std::__fill_a(__first, __first + __n, __c); |
e9e90c1f PC |
773 | return __first + __n; |
774 | } | |
775 | ||
e9e90c1f PC |
776 | /** |
777 | * @brief Fills the range [first,first+n) with copies of value. | |
5b9daa7e | 778 | * @ingroup mutating_algorithms |
e9e90c1f PC |
779 | * @param first An output iterator. |
780 | * @param n The count of copies to perform. | |
781 | * @param value A reference-to-const of arbitrary type. | |
782 | * @return The iterator at first+n. | |
783 | * | |
784 | * This function fills a range with copies of the same value. For char | |
785 | * types filling contiguous areas of memory, this becomes an inline call | |
786 | * to @c memset or @ wmemset. | |
82ab4b64 PC |
787 | * |
788 | * _GLIBCXX_RESOLVE_LIB_DEFECTS | |
789 | * DR 865. More algorithms that throw away information | |
e9e90c1f | 790 | */ |
d22a3166 PC |
791 | template<typename _OI, typename _Size, typename _Tp> |
792 | inline _OI | |
793 | fill_n(_OI __first, _Size __n, const _Tp& __value) | |
e9e90c1f PC |
794 | { |
795 | // concept requirements | |
d22a3166 | 796 | __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>) |
e9e90c1f | 797 | |
6989b63f | 798 | return _OI(std::__fill_n_a(std::__niter_base(__first), __n, __value)); |
e9e90c1f | 799 | } |
02d92e3b | 800 | |
d22a3166 PC |
801 | template<bool _BoolType> |
802 | struct __equal | |
803 | { | |
804 | template<typename _II1, typename _II2> | |
805 | static bool | |
806 | equal(_II1 __first1, _II1 __last1, _II2 __first2) | |
807 | { | |
808 | for (; __first1 != __last1; ++__first1, ++__first2) | |
809 | if (!(*__first1 == *__first2)) | |
810 | return false; | |
811 | return true; | |
812 | } | |
813 | }; | |
814 | ||
815 | template<> | |
816 | struct __equal<true> | |
817 | { | |
818 | template<typename _Tp> | |
819 | static bool | |
820 | equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2) | |
821 | { | |
360721e3 PC |
822 | return !__builtin_memcmp(__first1, __first2, sizeof(_Tp) |
823 | * (__last1 - __first1)); | |
d22a3166 PC |
824 | } |
825 | }; | |
826 | ||
827 | template<typename _II1, typename _II2> | |
828 | inline bool | |
829 | __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2) | |
830 | { | |
831 | typedef typename iterator_traits<_II1>::value_type _ValueType1; | |
832 | typedef typename iterator_traits<_II2>::value_type _ValueType2; | |
833 | const bool __simple = (__is_integer<_ValueType1>::__value | |
834 | && __is_pointer<_II1>::__value | |
835 | && __is_pointer<_II2>::__value | |
836 | && __are_same<_ValueType1, _ValueType2>::__value); | |
837 | ||
838 | return std::__equal<__simple>::equal(__first1, __last1, __first2); | |
839 | } | |
840 | ||
c2ba9709 JS |
841 | |
842 | template<typename, typename> | |
843 | struct __lc_rai | |
844 | { | |
845 | template<typename _II1, typename _II2> | |
846 | static _II1 | |
847 | __newlast1(_II1, _II1 __last1, _II2, _II2) | |
848 | { return __last1; } | |
849 | ||
850 | template<typename _II> | |
851 | static bool | |
852 | __cnd2(_II __first, _II __last) | |
853 | { return __first != __last; } | |
854 | }; | |
855 | ||
856 | template<> | |
857 | struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag> | |
858 | { | |
859 | template<typename _RAI1, typename _RAI2> | |
860 | static _RAI1 | |
861 | __newlast1(_RAI1 __first1, _RAI1 __last1, | |
862 | _RAI2 __first2, _RAI2 __last2) | |
863 | { | |
864 | const typename iterator_traits<_RAI1>::difference_type | |
865 | __diff1 = __last1 - __first1; | |
866 | const typename iterator_traits<_RAI2>::difference_type | |
867 | __diff2 = __last2 - __first2; | |
868 | return __diff2 < __diff1 ? __first1 + __diff2 : __last1; | |
869 | } | |
870 | ||
871 | template<typename _RAI> | |
872 | static bool | |
873 | __cnd2(_RAI, _RAI) | |
874 | { return true; } | |
875 | }; | |
876 | ||
478b2b9c PC |
877 | template<bool _BoolType> |
878 | struct __lexicographical_compare | |
879 | { | |
880 | template<typename _II1, typename _II2> | |
ba940b7c | 881 | static bool __lc(_II1, _II1, _II2, _II2); |
478b2b9c PC |
882 | }; |
883 | ||
ba940b7c PC |
884 | template<bool _BoolType> |
885 | template<typename _II1, typename _II2> | |
886 | bool | |
887 | __lexicographical_compare<_BoolType>:: | |
888 | __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2) | |
889 | { | |
890 | typedef typename iterator_traits<_II1>::iterator_category _Category1; | |
891 | typedef typename iterator_traits<_II2>::iterator_category _Category2; | |
892 | typedef std::__lc_rai<_Category1, _Category2> __rai_type; | |
893 | ||
894 | __last1 = __rai_type::__newlast1(__first1, __last1, | |
895 | __first2, __last2); | |
896 | for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2); | |
897 | ++__first1, ++__first2) | |
898 | { | |
899 | if (*__first1 < *__first2) | |
900 | return true; | |
901 | if (*__first2 < *__first1) | |
902 | return false; | |
903 | } | |
904 | return __first1 == __last1 && __first2 != __last2; | |
905 | } | |
906 | ||
478b2b9c PC |
907 | template<> |
908 | struct __lexicographical_compare<true> | |
909 | { | |
910 | template<typename _Tp, typename _Up> | |
911 | static bool | |
912 | __lc(const _Tp* __first1, const _Tp* __last1, | |
913 | const _Up* __first2, const _Up* __last2) | |
914 | { | |
915 | const size_t __len1 = __last1 - __first1; | |
916 | const size_t __len2 = __last2 - __first2; | |
917 | const int __result = __builtin_memcmp(__first1, __first2, | |
918 | std::min(__len1, __len2)); | |
919 | return __result != 0 ? __result < 0 : __len1 < __len2; | |
920 | } | |
921 | }; | |
922 | ||
923 | template<typename _II1, typename _II2> | |
924 | inline bool | |
925 | __lexicographical_compare_aux(_II1 __first1, _II1 __last1, | |
926 | _II2 __first2, _II2 __last2) | |
927 | { | |
928 | typedef typename iterator_traits<_II1>::value_type _ValueType1; | |
929 | typedef typename iterator_traits<_II2>::value_type _ValueType2; | |
930 | const bool __simple = | |
931 | (__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value | |
932 | && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed | |
933 | && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed | |
934 | && __is_pointer<_II1>::__value | |
935 | && __is_pointer<_II2>::__value); | |
936 | ||
937 | return std::__lexicographical_compare<__simple>::__lc(__first1, __last1, | |
938 | __first2, __last2); | |
939 | } | |
940 | ||
247d8075 PC |
941 | /** |
942 | * @brief Finds the first position in which @a val could be inserted | |
943 | * without changing the ordering. | |
944 | * @param first An iterator. | |
945 | * @param last Another iterator. | |
946 | * @param val The search term. | |
947 | * @return An iterator pointing to the first element <em>not less | |
948 | * than</em> @a val, or end() if every element is less than | |
949 | * @a val. | |
950 | * @ingroup binary_search_algorithms | |
951 | */ | |
952 | template<typename _ForwardIterator, typename _Tp> | |
953 | _ForwardIterator | |
954 | lower_bound(_ForwardIterator __first, _ForwardIterator __last, | |
955 | const _Tp& __val) | |
956 | { | |
957 | typedef typename iterator_traits<_ForwardIterator>::value_type | |
958 | _ValueType; | |
959 | typedef typename iterator_traits<_ForwardIterator>::difference_type | |
960 | _DistanceType; | |
961 | ||
962 | // concept requirements | |
963 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
964 | __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>) | |
965 | __glibcxx_requires_partitioned_lower(__first, __last, __val); | |
966 | ||
967 | _DistanceType __len = std::distance(__first, __last); | |
968 | _DistanceType __half; | |
969 | _ForwardIterator __middle; | |
970 | ||
971 | while (__len > 0) | |
972 | { | |
973 | __half = __len >> 1; | |
974 | __middle = __first; | |
975 | std::advance(__middle, __half); | |
976 | if (*__middle < __val) | |
977 | { | |
978 | __first = __middle; | |
979 | ++__first; | |
980 | __len = __len - __half - 1; | |
981 | } | |
982 | else | |
983 | __len = __half; | |
984 | } | |
985 | return __first; | |
986 | } | |
987 | ||
988 | /** | |
989 | * @brief Finds the first position in which @a val could be inserted | |
990 | * without changing the ordering. | |
991 | * @ingroup binary_search_algorithms | |
992 | * @param first An iterator. | |
993 | * @param last Another iterator. | |
994 | * @param val The search term. | |
995 | * @param comp A functor to use for comparisons. | |
996 | * @return An iterator pointing to the first element <em>not less | |
997 | * than</em> @a val, or end() if every element is less | |
998 | * than @a val. | |
999 | * @ingroup binary_search_algorithms | |
1000 | * | |
1001 | * The comparison function should have the same effects on ordering as | |
1002 | * the function used for the initial sort. | |
1003 | */ | |
1004 | template<typename _ForwardIterator, typename _Tp, typename _Compare> | |
1005 | _ForwardIterator | |
1006 | lower_bound(_ForwardIterator __first, _ForwardIterator __last, | |
1007 | const _Tp& __val, _Compare __comp) | |
1008 | { | |
1009 | typedef typename iterator_traits<_ForwardIterator>::value_type | |
1010 | _ValueType; | |
1011 | typedef typename iterator_traits<_ForwardIterator>::difference_type | |
1012 | _DistanceType; | |
1013 | ||
1014 | // concept requirements | |
1015 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
1016 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, | |
1017 | _ValueType, _Tp>) | |
1018 | __glibcxx_requires_partitioned_lower_pred(__first, __last, | |
1019 | __val, __comp); | |
1020 | ||
1021 | _DistanceType __len = std::distance(__first, __last); | |
1022 | _DistanceType __half; | |
1023 | _ForwardIterator __middle; | |
1024 | ||
1025 | while (__len > 0) | |
1026 | { | |
1027 | __half = __len >> 1; | |
1028 | __middle = __first; | |
1029 | std::advance(__middle, __half); | |
1030 | if (__comp(*__middle, __val)) | |
1031 | { | |
1032 | __first = __middle; | |
1033 | ++__first; | |
1034 | __len = __len - __half - 1; | |
1035 | } | |
1036 | else | |
1037 | __len = __half; | |
1038 | } | |
1039 | return __first; | |
1040 | } | |
1041 | ||
1042 | /// This is a helper function for the sort routines and for random.tcc. | |
1043 | // Precondition: __n > 0. | |
1044 | template<typename _Size> | |
1045 | inline _Size | |
1046 | __lg(_Size __n) | |
1047 | { | |
1048 | _Size __k; | |
1049 | for (__k = 0; __n != 0; __n >>= 1) | |
1050 | ++__k; | |
1051 | return __k - 1; | |
1052 | } | |
1053 | ||
1054 | inline int | |
1055 | __lg(int __n) | |
1056 | { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); } | |
1057 | ||
1058 | inline long | |
1059 | __lg(long __n) | |
1060 | { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); } | |
1061 | ||
1062 | inline long long | |
1063 | __lg(long long __n) | |
1064 | { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); } | |
1065 | ||
c2ba9709 JS |
1066 | _GLIBCXX_END_NAMESPACE |
1067 | ||
1068 | _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P) | |
1069 | ||
1b4a6975 PE |
1070 | /** |
1071 | * @brief Tests a range for element-wise equality. | |
5b9daa7e | 1072 | * @ingroup non_mutating_algorithms |
1b4a6975 PE |
1073 | * @param first1 An input iterator. |
1074 | * @param last1 An input iterator. | |
1075 | * @param first2 An input iterator. | |
1076 | * @return A boolean true or false. | |
1077 | * | |
1078 | * This compares the elements of two ranges using @c == and returns true or | |
1079 | * false depending on whether all of the corresponding elements of the | |
1080 | * ranges are equal. | |
1081 | */ | |
d22a3166 | 1082 | template<typename _II1, typename _II2> |
02d92e3b | 1083 | inline bool |
d22a3166 | 1084 | equal(_II1 __first1, _II1 __last1, _II2 __first2) |
02d92e3b SW |
1085 | { |
1086 | // concept requirements | |
d22a3166 PC |
1087 | __glibcxx_function_requires(_InputIteratorConcept<_II1>) |
1088 | __glibcxx_function_requires(_InputIteratorConcept<_II2>) | |
3d7c150e | 1089 | __glibcxx_function_requires(_EqualOpConcept< |
d22a3166 PC |
1090 | typename iterator_traits<_II1>::value_type, |
1091 | typename iterator_traits<_II2>::value_type>) | |
285b36d6 | 1092 | __glibcxx_requires_valid_range(__first1, __last1); |
d22a3166 | 1093 | |
6989b63f PC |
1094 | return std::__equal_aux(std::__niter_base(__first1), |
1095 | std::__niter_base(__last1), | |
1096 | std::__niter_base(__first2)); | |
02d92e3b SW |
1097 | } |
1098 | ||
1b4a6975 PE |
1099 | /** |
1100 | * @brief Tests a range for element-wise equality. | |
5b9daa7e | 1101 | * @ingroup non_mutating_algorithms |
1b4a6975 PE |
1102 | * @param first1 An input iterator. |
1103 | * @param last1 An input iterator. | |
1104 | * @param first2 An input iterator. | |
aac2878e | 1105 | * @param binary_pred A binary predicate @link functors |
c2ba9709 JS |
1106 | * functor@endlink. |
1107 | * @return A boolean true or false. | |
1b4a6975 PE |
1108 | * |
1109 | * This compares the elements of two ranges using the binary_pred | |
1110 | * parameter, and returns true or | |
1111 | * false depending on whether all of the corresponding elements of the | |
1112 | * ranges are equal. | |
1113 | */ | |
c2ba9709 | 1114 | template<typename _IIter1, typename _IIter2, typename _BinaryPredicate> |
02d92e3b | 1115 | inline bool |
c2ba9709 JS |
1116 | equal(_IIter1 __first1, _IIter1 __last1, |
1117 | _IIter2 __first2, _BinaryPredicate __binary_pred) | |
02d92e3b SW |
1118 | { |
1119 | // concept requirements | |
c2ba9709 JS |
1120 | __glibcxx_function_requires(_InputIteratorConcept<_IIter1>) |
1121 | __glibcxx_function_requires(_InputIteratorConcept<_IIter2>) | |
285b36d6 | 1122 | __glibcxx_requires_valid_range(__first1, __last1); |
02d92e3b | 1123 | |
43da93a7 | 1124 | for (; __first1 != __last1; ++__first1, ++__first2) |
dded9d2c | 1125 | if (!bool(__binary_pred(*__first1, *__first2))) |
02d92e3b | 1126 | return false; |
725dc051 | 1127 | return true; |
02d92e3b SW |
1128 | } |
1129 | ||
1b4a6975 | 1130 | /** |
2a60a9f6 | 1131 | * @brief Performs @b dictionary comparison on ranges. |
5b9daa7e | 1132 | * @ingroup sorting_algorithms |
1b4a6975 PE |
1133 | * @param first1 An input iterator. |
1134 | * @param last1 An input iterator. | |
1135 | * @param first2 An input iterator. | |
1136 | * @param last2 An input iterator. | |
1137 | * @return A boolean true or false. | |
1138 | * | |
2a60a9f6 | 1139 | * <em>Returns true if the sequence of elements defined by the range |
1b4a6975 | 1140 | * [first1,last1) is lexicographically less than the sequence of elements |
2a60a9f6 | 1141 | * defined by the range [first2,last2). Returns false otherwise.</em> |
1b4a6975 PE |
1142 | * (Quoted from [25.3.8]/1.) If the iterators are all character pointers, |
1143 | * then this is an inline call to @c memcmp. | |
1144 | */ | |
c2fe93f7 | 1145 | template<typename _II1, typename _II2> |
de03de64 PC |
1146 | inline bool |
1147 | lexicographical_compare(_II1 __first1, _II1 __last1, | |
c2fe93f7 | 1148 | _II2 __first2, _II2 __last2) |
02d92e3b SW |
1149 | { |
1150 | // concept requirements | |
c2fe93f7 PC |
1151 | typedef typename iterator_traits<_II1>::value_type _ValueType1; |
1152 | typedef typename iterator_traits<_II2>::value_type _ValueType2; | |
1153 | __glibcxx_function_requires(_InputIteratorConcept<_II1>) | |
1154 | __glibcxx_function_requires(_InputIteratorConcept<_II2>) | |
1155 | __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>) | |
1156 | __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>) | |
285b36d6 BK |
1157 | __glibcxx_requires_valid_range(__first1, __last1); |
1158 | __glibcxx_requires_valid_range(__first2, __last2); | |
02d92e3b | 1159 | |
6989b63f PC |
1160 | return std::__lexicographical_compare_aux(std::__niter_base(__first1), |
1161 | std::__niter_base(__last1), | |
1162 | std::__niter_base(__first2), | |
1163 | std::__niter_base(__last2)); | |
de03de64 | 1164 | } |
4f39bf5c | 1165 | |
1b4a6975 | 1166 | /** |
2a60a9f6 | 1167 | * @brief Performs @b dictionary comparison on ranges. |
5b9daa7e | 1168 | * @ingroup sorting_algorithms |
1b4a6975 PE |
1169 | * @param first1 An input iterator. |
1170 | * @param last1 An input iterator. | |
1171 | * @param first2 An input iterator. | |
1172 | * @param last2 An input iterator. | |
aac2878e | 1173 | * @param comp A @link comparison_functors comparison functor@endlink. |
1b4a6975 PE |
1174 | * @return A boolean true or false. |
1175 | * | |
c2fe93f7 | 1176 | * The same as the four-parameter @c lexicographical_compare, but uses the |
1b4a6975 PE |
1177 | * comp parameter instead of @c <. |
1178 | */ | |
c2fe93f7 | 1179 | template<typename _II1, typename _II2, typename _Compare> |
02d92e3b | 1180 | bool |
c2fe93f7 PC |
1181 | lexicographical_compare(_II1 __first1, _II1 __last1, |
1182 | _II2 __first2, _II2 __last2, _Compare __comp) | |
02d92e3b | 1183 | { |
c2fe93f7 PC |
1184 | typedef typename iterator_traits<_II1>::iterator_category _Category1; |
1185 | typedef typename iterator_traits<_II2>::iterator_category _Category2; | |
4f39bf5c | 1186 | typedef std::__lc_rai<_Category1, _Category2> __rai_type; |
c2fe93f7 | 1187 | |
02d92e3b | 1188 | // concept requirements |
c2fe93f7 PC |
1189 | __glibcxx_function_requires(_InputIteratorConcept<_II1>) |
1190 | __glibcxx_function_requires(_InputIteratorConcept<_II2>) | |
285b36d6 BK |
1191 | __glibcxx_requires_valid_range(__first1, __last1); |
1192 | __glibcxx_requires_valid_range(__first2, __last2); | |
02d92e3b | 1193 | |
c2ba9709 JS |
1194 | __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2); |
1195 | for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2); | |
43da93a7 | 1196 | ++__first1, ++__first2) |
46c4e5d6 BK |
1197 | { |
1198 | if (__comp(*__first1, *__first2)) | |
1199 | return true; | |
1200 | if (__comp(*__first2, *__first1)) | |
1201 | return false; | |
1202 | } | |
02d92e3b SW |
1203 | return __first1 == __last1 && __first2 != __last2; |
1204 | } | |
1205 | ||
c2ba9709 JS |
1206 | /** |
1207 | * @brief Finds the places in ranges which don't match. | |
5b9daa7e | 1208 | * @ingroup non_mutating_algorithms |
c2ba9709 JS |
1209 | * @param first1 An input iterator. |
1210 | * @param last1 An input iterator. | |
1211 | * @param first2 An input iterator. | |
1212 | * @return A pair of iterators pointing to the first mismatch. | |
1213 | * | |
1214 | * This compares the elements of two ranges using @c == and returns a pair | |
1215 | * of iterators. The first iterator points into the first range, the | |
1216 | * second iterator points into the second range, and the elements pointed | |
1217 | * to by the iterators are not equal. | |
1218 | */ | |
1219 | template<typename _InputIterator1, typename _InputIterator2> | |
1220 | pair<_InputIterator1, _InputIterator2> | |
1221 | mismatch(_InputIterator1 __first1, _InputIterator1 __last1, | |
1222 | _InputIterator2 __first2) | |
1223 | { | |
1224 | // concept requirements | |
1225 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) | |
1226 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) | |
1227 | __glibcxx_function_requires(_EqualOpConcept< | |
1228 | typename iterator_traits<_InputIterator1>::value_type, | |
1229 | typename iterator_traits<_InputIterator2>::value_type>) | |
1230 | __glibcxx_requires_valid_range(__first1, __last1); | |
02d92e3b | 1231 | |
c2ba9709 JS |
1232 | while (__first1 != __last1 && *__first1 == *__first2) |
1233 | { | |
1234 | ++__first1; | |
1235 | ++__first2; | |
1236 | } | |
1237 | return pair<_InputIterator1, _InputIterator2>(__first1, __first2); | |
1238 | } | |
285b36d6 | 1239 | |
c2ba9709 JS |
1240 | /** |
1241 | * @brief Finds the places in ranges which don't match. | |
5b9daa7e | 1242 | * @ingroup non_mutating_algorithms |
c2ba9709 JS |
1243 | * @param first1 An input iterator. |
1244 | * @param last1 An input iterator. | |
1245 | * @param first2 An input iterator. | |
aac2878e | 1246 | * @param binary_pred A binary predicate @link functors |
c2ba9709 JS |
1247 | * functor@endlink. |
1248 | * @return A pair of iterators pointing to the first mismatch. | |
1249 | * | |
1250 | * This compares the elements of two ranges using the binary_pred | |
1251 | * parameter, and returns a pair | |
1252 | * of iterators. The first iterator points into the first range, the | |
1253 | * second iterator points into the second range, and the elements pointed | |
1254 | * to by the iterators are not equal. | |
1255 | */ | |
1256 | template<typename _InputIterator1, typename _InputIterator2, | |
1257 | typename _BinaryPredicate> | |
1258 | pair<_InputIterator1, _InputIterator2> | |
1259 | mismatch(_InputIterator1 __first1, _InputIterator1 __last1, | |
1260 | _InputIterator2 __first2, _BinaryPredicate __binary_pred) | |
1261 | { | |
1262 | // concept requirements | |
1263 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) | |
1264 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) | |
1265 | __glibcxx_requires_valid_range(__first1, __last1); | |
02d92e3b | 1266 | |
c2ba9709 JS |
1267 | while (__first1 != __last1 && bool(__binary_pred(*__first1, *__first2))) |
1268 | { | |
1269 | ++__first1; | |
1270 | ++__first2; | |
1271 | } | |
1272 | return pair<_InputIterator1, _InputIterator2>(__first1, __first2); | |
1273 | } | |
1274 | ||
1275 | _GLIBCXX_END_NESTED_NAMESPACE | |
1276 | ||
1277 | // NB: This file is included within many other C++ includes, as a way | |
1278 | // of getting the base algorithms. So, make sure that parallel bits | |
1279 | // come in too if requested. | |
1280 | #ifdef _GLIBCXX_PARALLEL | |
c2ba9709 JS |
1281 | # include <parallel/algobase.h> |
1282 | #endif | |
725dc051 | 1283 | |
ed6814f7 | 1284 | #endif |