Merged trunk at revision 161680 into branch.
[official-gcc.git] / libstdc++-v3 / include / bits / stl_algobase.h
blobcfaeef8f645ab6ee31fc404fbc745ab391a37b70
1 // Core algorithmic facilities -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // 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.
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.
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.
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/>.
28 * Copyright (c) 1994
29 * Hewlett-Packard Company
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.
40 * Copyright (c) 1996-1998
41 * Silicon Graphics Computer Systems, Inc.
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.
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.
57 #ifndef _STL_ALGOBASE_H
58 #define _STL_ALGOBASE_H 1
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
73 _GLIBCXX_BEGIN_NAMESPACE(std)
75 // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
76 // nutshell, we are partially implementing the resolution of DR 187,
77 // when it's safe, i.e., the value_types are equal.
78 template<bool _BoolType>
79 struct __iter_swap
81 template<typename _ForwardIterator1, typename _ForwardIterator2>
82 static void
83 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
85 typedef typename iterator_traits<_ForwardIterator1>::value_type
86 _ValueType1;
87 _ValueType1 __tmp = _GLIBCXX_MOVE(*__a);
88 *__a = _GLIBCXX_MOVE(*__b);
89 *__b = _GLIBCXX_MOVE(__tmp);
93 template<>
94 struct __iter_swap<true>
96 template<typename _ForwardIterator1, typename _ForwardIterator2>
97 static void
98 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
100 swap(*__a, *__b);
105 * @brief Swaps the contents of two iterators.
106 * @ingroup mutating_algorithms
107 * @param a An iterator.
108 * @param b Another iterator.
109 * @return Nothing.
111 * This function swaps the values pointed to by two iterators, not the
112 * iterators themselves.
114 template<typename _ForwardIterator1, typename _ForwardIterator2>
115 inline void
116 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
118 typedef typename iterator_traits<_ForwardIterator1>::value_type
119 _ValueType1;
120 typedef typename iterator_traits<_ForwardIterator2>::value_type
121 _ValueType2;
123 // concept requirements
124 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
125 _ForwardIterator1>)
126 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
127 _ForwardIterator2>)
128 __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
129 _ValueType2>)
130 __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
131 _ValueType1>)
133 typedef typename iterator_traits<_ForwardIterator1>::reference
134 _ReferenceType1;
135 typedef typename iterator_traits<_ForwardIterator2>::reference
136 _ReferenceType2;
137 std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value
138 && __are_same<_ValueType1&, _ReferenceType1>::__value
139 && __are_same<_ValueType2&, _ReferenceType2>::__value>::
140 iter_swap(__a, __b);
144 * @brief Swap the elements of two sequences.
145 * @ingroup mutating_algorithms
146 * @param first1 A forward iterator.
147 * @param last1 A forward iterator.
148 * @param first2 A forward iterator.
149 * @return An iterator equal to @p first2+(last1-first1).
151 * Swaps each element in the range @p [first1,last1) with the
152 * corresponding element in the range @p [first2,(last1-first1)).
153 * The ranges must not overlap.
155 template<typename _ForwardIterator1, typename _ForwardIterator2>
156 _ForwardIterator2
157 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
158 _ForwardIterator2 __first2)
160 // concept requirements
161 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
162 _ForwardIterator1>)
163 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
164 _ForwardIterator2>)
165 __glibcxx_requires_valid_range(__first1, __last1);
167 for (; __first1 != __last1; ++__first1, ++__first2)
168 std::iter_swap(__first1, __first2);
169 return __first2;
173 * @brief This does what you think it does.
174 * @ingroup sorting_algorithms
175 * @param a A thing of arbitrary type.
176 * @param b Another thing of arbitrary type.
177 * @return The lesser of the parameters.
179 * This is the simple classic generic implementation. It will work on
180 * temporary expressions, since they are only evaluated once, unlike a
181 * preprocessor macro.
183 template<typename _Tp>
184 inline const _Tp&
185 min(const _Tp& __a, const _Tp& __b)
187 // concept requirements
188 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
189 //return __b < __a ? __b : __a;
190 if (__b < __a)
191 return __b;
192 return __a;
196 * @brief This does what you think it does.
197 * @ingroup sorting_algorithms
198 * @param a A thing of arbitrary type.
199 * @param b Another thing of arbitrary type.
200 * @return The greater of the parameters.
202 * This is the simple classic generic implementation. It will work on
203 * temporary expressions, since they are only evaluated once, unlike a
204 * preprocessor macro.
206 template<typename _Tp>
207 inline const _Tp&
208 max(const _Tp& __a, const _Tp& __b)
210 // concept requirements
211 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
212 //return __a < __b ? __b : __a;
213 if (__a < __b)
214 return __b;
215 return __a;
219 * @brief This does what you think it does.
220 * @ingroup sorting_algorithms
221 * @param a A thing of arbitrary type.
222 * @param b Another thing of arbitrary type.
223 * @param comp A @link comparison_functors comparison functor@endlink.
224 * @return The lesser of the parameters.
226 * This will work on temporary expressions, since they are only evaluated
227 * once, unlike a preprocessor macro.
229 template<typename _Tp, typename _Compare>
230 inline const _Tp&
231 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
233 //return __comp(__b, __a) ? __b : __a;
234 if (__comp(__b, __a))
235 return __b;
236 return __a;
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.
247 * This will work on temporary expressions, since they are only evaluated
248 * once, unlike a preprocessor macro.
250 template<typename _Tp, typename _Compare>
251 inline const _Tp&
252 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
254 //return __comp(__a, __b) ? __b : __a;
255 if (__comp(__a, __b))
256 return __b;
257 return __a;
261 // If _Iterator has a base returns it otherwise _Iterator is returned
262 // untouched
263 template<typename _Iterator, bool _HasBase>
264 struct _Iter_base
266 typedef _Iterator iterator_type;
267 static iterator_type
268 _S_base(_Iterator __it)
269 { return __it; }
272 template<typename _Iterator>
273 struct _Iter_base<_Iterator, true>
275 typedef typename _Iterator::iterator_type iterator_type;
276 static iterator_type
277 _S_base(_Iterator __it)
278 { return __it.base(); }
281 // If _Iterator is a __normal_iterator return its base (a plain pointer,
282 // normally) otherwise return it untouched. See copy, fill, ...
283 template<typename _Iterator>
284 struct _Niter_base
285 : _Iter_base<_Iterator, __is_normal_iterator<_Iterator>::__value>
286 { };
288 template<typename _Iterator>
289 inline typename _Niter_base<_Iterator>::iterator_type
290 __niter_base(_Iterator __it)
291 { return std::_Niter_base<_Iterator>::_S_base(__it); }
293 // Likewise, for move_iterator.
294 template<typename _Iterator>
295 struct _Miter_base
296 : _Iter_base<_Iterator, __is_move_iterator<_Iterator>::__value>
297 { };
299 template<typename _Iterator>
300 inline typename _Miter_base<_Iterator>::iterator_type
301 __miter_base(_Iterator __it)
302 { return std::_Miter_base<_Iterator>::_S_base(__it); }
304 // All of these auxiliary structs serve two purposes. (1) Replace
305 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
306 // because the input and output ranges are permitted to overlap.)
307 // (2) If we're using random access iterators, then write the loop as
308 // a for loop with an explicit count.
310 template<bool, bool, typename>
311 struct __copy_move
313 template<typename _II, typename _OI>
314 static _OI
315 __copy_m(_II __first, _II __last, _OI __result)
317 for (; __first != __last; ++__result, ++__first)
318 *__result = *__first;
319 return __result;
323 #ifdef __GXX_EXPERIMENTAL_CXX0X__
324 template<typename _Category>
325 struct __copy_move<true, false, _Category>
327 template<typename _II, typename _OI>
328 static _OI
329 __copy_m(_II __first, _II __last, _OI __result)
331 for (; __first != __last; ++__result, ++__first)
332 *__result = std::move(*__first);
333 return __result;
336 #endif
338 template<>
339 struct __copy_move<false, false, random_access_iterator_tag>
341 template<typename _II, typename _OI>
342 static _OI
343 __copy_m(_II __first, _II __last, _OI __result)
345 typedef typename iterator_traits<_II>::difference_type _Distance;
346 for(_Distance __n = __last - __first; __n > 0; --__n)
348 *__result = *__first;
349 ++__first;
350 ++__result;
352 return __result;
356 #ifdef __GXX_EXPERIMENTAL_CXX0X__
357 template<>
358 struct __copy_move<true, false, random_access_iterator_tag>
360 template<typename _II, typename _OI>
361 static _OI
362 __copy_m(_II __first, _II __last, _OI __result)
364 typedef typename iterator_traits<_II>::difference_type _Distance;
365 for(_Distance __n = __last - __first; __n > 0; --__n)
367 *__result = std::move(*__first);
368 ++__first;
369 ++__result;
371 return __result;
374 #endif
376 template<bool _IsMove>
377 struct __copy_move<_IsMove, true, random_access_iterator_tag>
379 template<typename _Tp>
380 static _Tp*
381 __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
383 const ptrdiff_t _Num = __last - __first;
384 if (_Num)
385 __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
386 return __result + _Num;
390 template<bool _IsMove, typename _II, typename _OI>
391 inline _OI
392 __copy_move_a(_II __first, _II __last, _OI __result)
394 typedef typename iterator_traits<_II>::value_type _ValueTypeI;
395 typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
396 typedef typename iterator_traits<_II>::iterator_category _Category;
397 const bool __simple = (__is_trivial(_ValueTypeI)
398 && __is_pointer<_II>::__value
399 && __is_pointer<_OI>::__value
400 && __are_same<_ValueTypeI, _ValueTypeO>::__value);
402 return std::__copy_move<_IsMove, __simple,
403 _Category>::__copy_m(__first, __last, __result);
406 // Helpers for streambuf iterators (either istream or ostream).
407 // NB: avoid including <iosfwd>, relatively large.
408 template<typename _CharT>
409 struct char_traits;
411 template<typename _CharT, typename _Traits>
412 class istreambuf_iterator;
414 template<typename _CharT, typename _Traits>
415 class ostreambuf_iterator;
417 template<bool _IsMove, typename _CharT>
418 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
419 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
420 __copy_move_a2(_CharT*, _CharT*,
421 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
423 template<bool _IsMove, typename _CharT>
424 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
425 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
426 __copy_move_a2(const _CharT*, const _CharT*,
427 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
429 template<bool _IsMove, typename _CharT>
430 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
431 _CharT*>::__type
432 __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >,
433 istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
435 template<bool _IsMove, typename _II, typename _OI>
436 inline _OI
437 __copy_move_a2(_II __first, _II __last, _OI __result)
439 return _OI(std::__copy_move_a<_IsMove>(std::__niter_base(__first),
440 std::__niter_base(__last),
441 std::__niter_base(__result)));
445 * @brief Copies the range [first,last) into result.
446 * @ingroup mutating_algorithms
447 * @param first An input iterator.
448 * @param last An input iterator.
449 * @param result An output iterator.
450 * @return result + (first - last)
452 * This inline function will boil down to a call to @c memmove whenever
453 * possible. Failing that, if random access iterators are passed, then the
454 * loop count will be known (and therefore a candidate for compiler
455 * optimizations such as unrolling). Result may not be contained within
456 * [first,last); the copy_backward function should be used instead.
458 * Note that the end of the output range is permitted to be contained
459 * within [first,last).
461 template<typename _II, typename _OI>
462 inline _OI
463 copy(_II __first, _II __last, _OI __result)
465 // concept requirements
466 __glibcxx_function_requires(_InputIteratorConcept<_II>)
467 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
468 typename iterator_traits<_II>::value_type>)
469 __glibcxx_requires_valid_range(__first, __last);
471 return (std::__copy_move_a2<__is_move_iterator<_II>::__value>
472 (std::__miter_base(__first), std::__miter_base(__last),
473 __result));
476 #ifdef __GXX_EXPERIMENTAL_CXX0X__
478 * @brief Moves the range [first,last) into result.
479 * @ingroup mutating_algorithms
480 * @param first An input iterator.
481 * @param last An input iterator.
482 * @param result An output iterator.
483 * @return result + (first - last)
485 * This inline function will boil down to a call to @c memmove whenever
486 * possible. Failing that, if random access iterators are passed, then the
487 * loop count will be known (and therefore a candidate for compiler
488 * optimizations such as unrolling). Result may not be contained within
489 * [first,last); the move_backward function should be used instead.
491 * Note that the end of the output range is permitted to be contained
492 * within [first,last).
494 template<typename _II, typename _OI>
495 inline _OI
496 move(_II __first, _II __last, _OI __result)
498 // concept requirements
499 __glibcxx_function_requires(_InputIteratorConcept<_II>)
500 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
501 typename iterator_traits<_II>::value_type>)
502 __glibcxx_requires_valid_range(__first, __last);
504 return std::__copy_move_a2<true>(std::__miter_base(__first),
505 std::__miter_base(__last), __result);
508 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
509 #else
510 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
511 #endif
513 template<bool, bool, typename>
514 struct __copy_move_backward
516 template<typename _BI1, typename _BI2>
517 static _BI2
518 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
520 while (__first != __last)
521 *--__result = *--__last;
522 return __result;
526 #ifdef __GXX_EXPERIMENTAL_CXX0X__
527 template<typename _Category>
528 struct __copy_move_backward<true, false, _Category>
530 template<typename _BI1, typename _BI2>
531 static _BI2
532 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
534 while (__first != __last)
535 *--__result = std::move(*--__last);
536 return __result;
539 #endif
541 template<>
542 struct __copy_move_backward<false, false, random_access_iterator_tag>
544 template<typename _BI1, typename _BI2>
545 static _BI2
546 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
548 typename iterator_traits<_BI1>::difference_type __n;
549 for (__n = __last - __first; __n > 0; --__n)
550 *--__result = *--__last;
551 return __result;
555 #ifdef __GXX_EXPERIMENTAL_CXX0X__
556 template<>
557 struct __copy_move_backward<true, false, random_access_iterator_tag>
559 template<typename _BI1, typename _BI2>
560 static _BI2
561 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
563 typename iterator_traits<_BI1>::difference_type __n;
564 for (__n = __last - __first; __n > 0; --__n)
565 *--__result = std::move(*--__last);
566 return __result;
569 #endif
571 template<bool _IsMove>
572 struct __copy_move_backward<_IsMove, true, random_access_iterator_tag>
574 template<typename _Tp>
575 static _Tp*
576 __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
578 const ptrdiff_t _Num = __last - __first;
579 if (_Num)
580 __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
581 return __result - _Num;
585 template<bool _IsMove, typename _BI1, typename _BI2>
586 inline _BI2
587 __copy_move_backward_a(_BI1 __first, _BI1 __last, _BI2 __result)
589 typedef typename iterator_traits<_BI1>::value_type _ValueType1;
590 typedef typename iterator_traits<_BI2>::value_type _ValueType2;
591 typedef typename iterator_traits<_BI1>::iterator_category _Category;
592 const bool __simple = (__is_trivial(_ValueType1)
593 && __is_pointer<_BI1>::__value
594 && __is_pointer<_BI2>::__value
595 && __are_same<_ValueType1, _ValueType2>::__value);
597 return std::__copy_move_backward<_IsMove, __simple,
598 _Category>::__copy_move_b(__first,
599 __last,
600 __result);
603 template<bool _IsMove, typename _BI1, typename _BI2>
604 inline _BI2
605 __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result)
607 return _BI2(std::__copy_move_backward_a<_IsMove>
608 (std::__niter_base(__first), std::__niter_base(__last),
609 std::__niter_base(__result)));
613 * @brief Copies the range [first,last) into result.
614 * @ingroup mutating_algorithms
615 * @param first A bidirectional iterator.
616 * @param last A bidirectional iterator.
617 * @param result A bidirectional iterator.
618 * @return result - (first - last)
620 * The function has the same effect as copy, but starts at the end of the
621 * range and works its way to the start, returning the start of the result.
622 * This inline function will boil down to a call to @c memmove whenever
623 * possible. Failing that, if random access iterators are passed, then the
624 * loop count will be known (and therefore a candidate for compiler
625 * optimizations such as unrolling).
627 * Result may not be in the range [first,last). Use copy instead. Note
628 * that the start of the output range may overlap [first,last).
630 template<typename _BI1, typename _BI2>
631 inline _BI2
632 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
634 // concept requirements
635 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
636 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
637 __glibcxx_function_requires(_ConvertibleConcept<
638 typename iterator_traits<_BI1>::value_type,
639 typename iterator_traits<_BI2>::value_type>)
640 __glibcxx_requires_valid_range(__first, __last);
642 return (std::__copy_move_backward_a2<__is_move_iterator<_BI1>::__value>
643 (std::__miter_base(__first), std::__miter_base(__last),
644 __result));
647 #ifdef __GXX_EXPERIMENTAL_CXX0X__
649 * @brief Moves the range [first,last) into result.
650 * @ingroup mutating_algorithms
651 * @param first A bidirectional iterator.
652 * @param last A bidirectional iterator.
653 * @param result A bidirectional iterator.
654 * @return result - (first - last)
656 * The function has the same effect as move, but starts at the end of the
657 * range and works its way to the start, returning the start of the result.
658 * This inline function will boil down to a call to @c memmove whenever
659 * possible. Failing that, if random access iterators are passed, then the
660 * loop count will be known (and therefore a candidate for compiler
661 * optimizations such as unrolling).
663 * Result may not be in the range [first,last). Use move instead. Note
664 * that the start of the output range may overlap [first,last).
666 template<typename _BI1, typename _BI2>
667 inline _BI2
668 move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
670 // concept requirements
671 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
672 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
673 __glibcxx_function_requires(_ConvertibleConcept<
674 typename iterator_traits<_BI1>::value_type,
675 typename iterator_traits<_BI2>::value_type>)
676 __glibcxx_requires_valid_range(__first, __last);
678 return std::__copy_move_backward_a2<true>(std::__miter_base(__first),
679 std::__miter_base(__last),
680 __result);
683 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
684 #else
685 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
686 #endif
688 template<typename _ForwardIterator, typename _Tp>
689 inline typename
690 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
691 __fill_a(_ForwardIterator __first, _ForwardIterator __last,
692 const _Tp& __value)
694 for (; __first != __last; ++__first)
695 *__first = __value;
698 template<typename _ForwardIterator, typename _Tp>
699 inline typename
700 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
701 __fill_a(_ForwardIterator __first, _ForwardIterator __last,
702 const _Tp& __value)
704 const _Tp __tmp = __value;
705 for (; __first != __last; ++__first)
706 *__first = __tmp;
709 // Specialization: for char types we can use memset.
710 template<typename _Tp>
711 inline typename
712 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type
713 __fill_a(_Tp* __first, _Tp* __last, const _Tp& __c)
715 const _Tp __tmp = __c;
716 __builtin_memset(__first, static_cast<unsigned char>(__tmp),
717 __last - __first);
721 * @brief Fills the range [first,last) with copies of value.
722 * @ingroup mutating_algorithms
723 * @param first A forward iterator.
724 * @param last A forward iterator.
725 * @param value A reference-to-const of arbitrary type.
726 * @return Nothing.
728 * This function fills a range with copies of the same value. For char
729 * types filling contiguous areas of memory, this becomes an inline call
730 * to @c memset or @c wmemset.
732 template<typename _ForwardIterator, typename _Tp>
733 inline void
734 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
736 // concept requirements
737 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
738 _ForwardIterator>)
739 __glibcxx_requires_valid_range(__first, __last);
741 std::__fill_a(std::__niter_base(__first), std::__niter_base(__last),
742 __value);
745 template<typename _OutputIterator, typename _Size, typename _Tp>
746 inline typename
747 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
748 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
750 for (__decltype(__n + 0) __niter = __n;
751 __niter > 0; --__niter, ++__first)
752 *__first = __value;
753 return __first;
756 template<typename _OutputIterator, typename _Size, typename _Tp>
757 inline typename
758 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
759 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
761 const _Tp __tmp = __value;
762 for (__decltype(__n + 0) __niter = __n;
763 __niter > 0; --__niter, ++__first)
764 *__first = __tmp;
765 return __first;
768 template<typename _Size, typename _Tp>
769 inline typename
770 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, _Tp*>::__type
771 __fill_n_a(_Tp* __first, _Size __n, const _Tp& __c)
773 std::__fill_a(__first, __first + __n, __c);
774 return __first + __n;
778 * @brief Fills the range [first,first+n) with copies of value.
779 * @ingroup mutating_algorithms
780 * @param first An output iterator.
781 * @param n The count of copies to perform.
782 * @param value A reference-to-const of arbitrary type.
783 * @return The iterator at first+n.
785 * This function fills a range with copies of the same value. For char
786 * types filling contiguous areas of memory, this becomes an inline call
787 * to @c memset or @ wmemset.
789 * _GLIBCXX_RESOLVE_LIB_DEFECTS
790 * DR 865. More algorithms that throw away information
792 template<typename _OI, typename _Size, typename _Tp>
793 inline _OI
794 fill_n(_OI __first, _Size __n, const _Tp& __value)
796 // concept requirements
797 __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>)
799 return _OI(std::__fill_n_a(std::__niter_base(__first), __n, __value));
802 template<bool _BoolType>
803 struct __equal
805 template<typename _II1, typename _II2>
806 static bool
807 equal(_II1 __first1, _II1 __last1, _II2 __first2)
809 for (; __first1 != __last1; ++__first1, ++__first2)
810 if (!(*__first1 == *__first2))
811 return false;
812 return true;
816 template<>
817 struct __equal<true>
819 template<typename _Tp>
820 static bool
821 equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
823 return !__builtin_memcmp(__first1, __first2, sizeof(_Tp)
824 * (__last1 - __first1));
828 template<typename _II1, typename _II2>
829 inline bool
830 __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
832 typedef typename iterator_traits<_II1>::value_type _ValueType1;
833 typedef typename iterator_traits<_II2>::value_type _ValueType2;
834 const bool __simple = (__is_integer<_ValueType1>::__value
835 && __is_pointer<_II1>::__value
836 && __is_pointer<_II2>::__value
837 && __are_same<_ValueType1, _ValueType2>::__value);
839 return std::__equal<__simple>::equal(__first1, __last1, __first2);
843 template<typename, typename>
844 struct __lc_rai
846 template<typename _II1, typename _II2>
847 static _II1
848 __newlast1(_II1, _II1 __last1, _II2, _II2)
849 { return __last1; }
851 template<typename _II>
852 static bool
853 __cnd2(_II __first, _II __last)
854 { return __first != __last; }
857 template<>
858 struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag>
860 template<typename _RAI1, typename _RAI2>
861 static _RAI1
862 __newlast1(_RAI1 __first1, _RAI1 __last1,
863 _RAI2 __first2, _RAI2 __last2)
865 const typename iterator_traits<_RAI1>::difference_type
866 __diff1 = __last1 - __first1;
867 const typename iterator_traits<_RAI2>::difference_type
868 __diff2 = __last2 - __first2;
869 return __diff2 < __diff1 ? __first1 + __diff2 : __last1;
872 template<typename _RAI>
873 static bool
874 __cnd2(_RAI, _RAI)
875 { return true; }
878 template<bool _BoolType>
879 struct __lexicographical_compare
881 template<typename _II1, typename _II2>
882 static bool __lc(_II1, _II1, _II2, _II2);
885 template<bool _BoolType>
886 template<typename _II1, typename _II2>
887 bool
888 __lexicographical_compare<_BoolType>::
889 __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
891 typedef typename iterator_traits<_II1>::iterator_category _Category1;
892 typedef typename iterator_traits<_II2>::iterator_category _Category2;
893 typedef std::__lc_rai<_Category1, _Category2> __rai_type;
895 __last1 = __rai_type::__newlast1(__first1, __last1,
896 __first2, __last2);
897 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
898 ++__first1, ++__first2)
900 if (*__first1 < *__first2)
901 return true;
902 if (*__first2 < *__first1)
903 return false;
905 return __first1 == __last1 && __first2 != __last2;
908 template<>
909 struct __lexicographical_compare<true>
911 template<typename _Tp, typename _Up>
912 static bool
913 __lc(const _Tp* __first1, const _Tp* __last1,
914 const _Up* __first2, const _Up* __last2)
916 const size_t __len1 = __last1 - __first1;
917 const size_t __len2 = __last2 - __first2;
918 const int __result = __builtin_memcmp(__first1, __first2,
919 std::min(__len1, __len2));
920 return __result != 0 ? __result < 0 : __len1 < __len2;
924 template<typename _II1, typename _II2>
925 inline bool
926 __lexicographical_compare_aux(_II1 __first1, _II1 __last1,
927 _II2 __first2, _II2 __last2)
929 typedef typename iterator_traits<_II1>::value_type _ValueType1;
930 typedef typename iterator_traits<_II2>::value_type _ValueType2;
931 const bool __simple =
932 (__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
933 && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
934 && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
935 && __is_pointer<_II1>::__value
936 && __is_pointer<_II2>::__value);
938 return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
939 __first2, __last2);
943 * @brief Finds the first position in which @a val could be inserted
944 * without changing the ordering.
945 * @param first An iterator.
946 * @param last Another iterator.
947 * @param val The search term.
948 * @return An iterator pointing to the first element <em>not less
949 * than</em> @a val, or end() if every element is less than
950 * @a val.
951 * @ingroup binary_search_algorithms
953 template<typename _ForwardIterator, typename _Tp>
954 _ForwardIterator
955 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
956 const _Tp& __val)
958 typedef typename iterator_traits<_ForwardIterator>::value_type
959 _ValueType;
960 typedef typename iterator_traits<_ForwardIterator>::difference_type
961 _DistanceType;
963 // concept requirements
964 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
965 __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
966 __glibcxx_requires_partitioned_lower(__first, __last, __val);
968 _DistanceType __len = std::distance(__first, __last);
969 _DistanceType __half;
970 _ForwardIterator __middle;
972 while (__len > 0)
974 __half = __len >> 1;
975 __middle = __first;
976 std::advance(__middle, __half);
977 if (*__middle < __val)
979 __first = __middle;
980 ++__first;
981 __len = __len - __half - 1;
983 else
984 __len = __half;
986 return __first;
989 /// This is a helper function for the sort routines and for random.tcc.
990 // Precondition: __n > 0.
991 template<typename _Size>
992 inline _Size
993 __lg(_Size __n)
995 _Size __k;
996 for (__k = 0; __n != 0; __n >>= 1)
997 ++__k;
998 return __k - 1;
1001 inline int
1002 __lg(int __n)
1003 { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
1005 inline long
1006 __lg(long __n)
1007 { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
1009 inline long long
1010 __lg(long long __n)
1011 { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
1013 _GLIBCXX_END_NAMESPACE
1015 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
1018 * @brief Tests a range for element-wise equality.
1019 * @ingroup non_mutating_algorithms
1020 * @param first1 An input iterator.
1021 * @param last1 An input iterator.
1022 * @param first2 An input iterator.
1023 * @return A boolean true or false.
1025 * This compares the elements of two ranges using @c == and returns true or
1026 * false depending on whether all of the corresponding elements of the
1027 * ranges are equal.
1029 template<typename _II1, typename _II2>
1030 inline bool
1031 equal(_II1 __first1, _II1 __last1, _II2 __first2)
1033 // concept requirements
1034 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1035 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1036 __glibcxx_function_requires(_EqualOpConcept<
1037 typename iterator_traits<_II1>::value_type,
1038 typename iterator_traits<_II2>::value_type>)
1039 __glibcxx_requires_valid_range(__first1, __last1);
1041 return std::__equal_aux(std::__niter_base(__first1),
1042 std::__niter_base(__last1),
1043 std::__niter_base(__first2));
1047 * @brief Tests a range for element-wise equality.
1048 * @ingroup non_mutating_algorithms
1049 * @param first1 An input iterator.
1050 * @param last1 An input iterator.
1051 * @param first2 An input iterator.
1052 * @param binary_pred A binary predicate @link functors
1053 * functor@endlink.
1054 * @return A boolean true or false.
1056 * This compares the elements of two ranges using the binary_pred
1057 * parameter, and returns true or
1058 * false depending on whether all of the corresponding elements of the
1059 * ranges are equal.
1061 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1062 inline bool
1063 equal(_IIter1 __first1, _IIter1 __last1,
1064 _IIter2 __first2, _BinaryPredicate __binary_pred)
1066 // concept requirements
1067 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1068 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1069 __glibcxx_requires_valid_range(__first1, __last1);
1071 for (; __first1 != __last1; ++__first1, ++__first2)
1072 if (!bool(__binary_pred(*__first1, *__first2)))
1073 return false;
1074 return true;
1078 * @brief Performs @b dictionary comparison on ranges.
1079 * @ingroup sorting_algorithms
1080 * @param first1 An input iterator.
1081 * @param last1 An input iterator.
1082 * @param first2 An input iterator.
1083 * @param last2 An input iterator.
1084 * @return A boolean true or false.
1086 * <em>Returns true if the sequence of elements defined by the range
1087 * [first1,last1) is lexicographically less than the sequence of elements
1088 * defined by the range [first2,last2). Returns false otherwise.</em>
1089 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
1090 * then this is an inline call to @c memcmp.
1092 template<typename _II1, typename _II2>
1093 inline bool
1094 lexicographical_compare(_II1 __first1, _II1 __last1,
1095 _II2 __first2, _II2 __last2)
1097 // concept requirements
1098 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1099 typedef typename iterator_traits<_II2>::value_type _ValueType2;
1100 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1101 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1102 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
1103 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
1104 __glibcxx_requires_valid_range(__first1, __last1);
1105 __glibcxx_requires_valid_range(__first2, __last2);
1107 return std::__lexicographical_compare_aux(std::__niter_base(__first1),
1108 std::__niter_base(__last1),
1109 std::__niter_base(__first2),
1110 std::__niter_base(__last2));
1114 * @brief Performs @b dictionary comparison on ranges.
1115 * @ingroup sorting_algorithms
1116 * @param first1 An input iterator.
1117 * @param last1 An input iterator.
1118 * @param first2 An input iterator.
1119 * @param last2 An input iterator.
1120 * @param comp A @link comparison_functors comparison functor@endlink.
1121 * @return A boolean true or false.
1123 * The same as the four-parameter @c lexicographical_compare, but uses the
1124 * comp parameter instead of @c <.
1126 template<typename _II1, typename _II2, typename _Compare>
1127 bool
1128 lexicographical_compare(_II1 __first1, _II1 __last1,
1129 _II2 __first2, _II2 __last2, _Compare __comp)
1131 typedef typename iterator_traits<_II1>::iterator_category _Category1;
1132 typedef typename iterator_traits<_II2>::iterator_category _Category2;
1133 typedef std::__lc_rai<_Category1, _Category2> __rai_type;
1135 // concept requirements
1136 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1137 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1138 __glibcxx_requires_valid_range(__first1, __last1);
1139 __glibcxx_requires_valid_range(__first2, __last2);
1141 __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
1142 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
1143 ++__first1, ++__first2)
1145 if (__comp(*__first1, *__first2))
1146 return true;
1147 if (__comp(*__first2, *__first1))
1148 return false;
1150 return __first1 == __last1 && __first2 != __last2;
1154 * @brief Finds the places in ranges which don't match.
1155 * @ingroup non_mutating_algorithms
1156 * @param first1 An input iterator.
1157 * @param last1 An input iterator.
1158 * @param first2 An input iterator.
1159 * @return A pair of iterators pointing to the first mismatch.
1161 * This compares the elements of two ranges using @c == and returns a pair
1162 * of iterators. The first iterator points into the first range, the
1163 * second iterator points into the second range, and the elements pointed
1164 * to by the iterators are not equal.
1166 template<typename _InputIterator1, typename _InputIterator2>
1167 pair<_InputIterator1, _InputIterator2>
1168 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1169 _InputIterator2 __first2)
1171 // concept requirements
1172 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1173 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1174 __glibcxx_function_requires(_EqualOpConcept<
1175 typename iterator_traits<_InputIterator1>::value_type,
1176 typename iterator_traits<_InputIterator2>::value_type>)
1177 __glibcxx_requires_valid_range(__first1, __last1);
1179 while (__first1 != __last1 && *__first1 == *__first2)
1181 ++__first1;
1182 ++__first2;
1184 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1188 * @brief Finds the places in ranges which don't match.
1189 * @ingroup non_mutating_algorithms
1190 * @param first1 An input iterator.
1191 * @param last1 An input iterator.
1192 * @param first2 An input iterator.
1193 * @param binary_pred A binary predicate @link functors
1194 * functor@endlink.
1195 * @return A pair of iterators pointing to the first mismatch.
1197 * This compares the elements of two ranges using the binary_pred
1198 * parameter, and returns a pair
1199 * of iterators. The first iterator points into the first range, the
1200 * second iterator points into the second range, and the elements pointed
1201 * to by the iterators are not equal.
1203 template<typename _InputIterator1, typename _InputIterator2,
1204 typename _BinaryPredicate>
1205 pair<_InputIterator1, _InputIterator2>
1206 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1207 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1209 // concept requirements
1210 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1211 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1212 __glibcxx_requires_valid_range(__first1, __last1);
1214 while (__first1 != __last1 && bool(__binary_pred(*__first1, *__first2)))
1216 ++__first1;
1217 ++__first2;
1219 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1222 _GLIBCXX_END_NESTED_NAMESPACE
1224 // NB: This file is included within many other C++ includes, as a way
1225 // of getting the base algorithms. So, make sure that parallel bits
1226 // come in too if requested.
1227 #ifdef _GLIBCXX_PARALLEL
1228 # include <parallel/algobase.h>
1229 #endif
1231 #endif