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