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