1 // Core algorithmic facilities -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 // Free Software Foundation, Inc.
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)
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/>.
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>
62 #include <bits/functexcept.h>
63 #include <bits/cpp_type_traits.h>
64 #include <ext/type_traits.h>
65 #include <ext/numeric_traits.h>
66 #include <bits/stl_pair.h>
67 #include <bits/stl_iterator_base_types.h>
68 #include <bits/stl_iterator_base_funcs.h>
69 #include <bits/stl_iterator.h>
70 #include <bits/concept_check.h>
71 #include <debug/debug.h>
72 #include <bits/move.h> // For std::swap and _GLIBCXX_MOVE
74 _GLIBCXX_BEGIN_NAMESPACE(std)
76 // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
77 // nutshell, we are partially implementing the resolution of DR 187,
78 // when it's safe, i.e., the value_types are equal.
79 template<bool _BoolType>
82 template<typename _ForwardIterator1, typename _ForwardIterator2>
84 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
86 typedef typename iterator_traits<_ForwardIterator1>::value_type
88 _ValueType1 __tmp = _GLIBCXX_MOVE(*__a);
89 *__a = _GLIBCXX_MOVE(*__b);
90 *__b = _GLIBCXX_MOVE(__tmp);
95 struct __iter_swap<true>
97 template<typename _ForwardIterator1, typename _ForwardIterator2>
99 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
106 * @brief Swaps the contents of two iterators.
107 * @ingroup mutating_algorithms
108 * @param a An iterator.
109 * @param b Another iterator.
112 * This function swaps the values pointed to by two iterators, not the
113 * iterators themselves.
115 template<typename _ForwardIterator1, typename _ForwardIterator2>
117 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
119 typedef typename iterator_traits<_ForwardIterator1>::value_type
121 typedef typename iterator_traits<_ForwardIterator2>::value_type
124 // concept requirements
125 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
127 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
129 __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
131 __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
134 typedef typename iterator_traits<_ForwardIterator1>::reference
136 typedef typename iterator_traits<_ForwardIterator2>::reference
138 std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value
139 && __are_same<_ValueType1&, _ReferenceType1>::__value
140 && __are_same<_ValueType2&, _ReferenceType2>::__value>::
145 * @brief Swap the elements of two sequences.
146 * @ingroup mutating_algorithms
147 * @param first1 A forward iterator.
148 * @param last1 A forward iterator.
149 * @param first2 A forward iterator.
150 * @return An iterator equal to @p first2+(last1-first1).
152 * Swaps each element in the range @p [first1,last1) with the
153 * corresponding element in the range @p [first2,(last1-first1)).
154 * The ranges must not overlap.
156 template<typename _ForwardIterator1, typename _ForwardIterator2>
158 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
159 _ForwardIterator2 __first2)
161 // concept requirements
162 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
164 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
166 __glibcxx_requires_valid_range(__first1, __last1);
168 for (; __first1 != __last1; ++__first1, ++__first2)
169 std::iter_swap(__first1, __first2);
174 * @brief This does what you think it does.
175 * @ingroup sorting_algorithms
176 * @param a A thing of arbitrary type.
177 * @param b Another thing of arbitrary type.
178 * @return The lesser of the parameters.
180 * This is the simple classic generic implementation. It will work on
181 * temporary expressions, since they are only evaluated once, unlike a
182 * preprocessor macro.
184 template<typename _Tp>
186 min(const _Tp& __a, const _Tp& __b)
188 // concept requirements
189 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
190 //return __b < __a ? __b : __a;
197 * @brief This does what you think it does.
198 * @ingroup sorting_algorithms
199 * @param a A thing of arbitrary type.
200 * @param b Another thing of arbitrary type.
201 * @return The greater of the parameters.
203 * This is the simple classic generic implementation. It will work on
204 * temporary expressions, since they are only evaluated once, unlike a
205 * preprocessor macro.
207 template<typename _Tp>
209 max(const _Tp& __a, const _Tp& __b)
211 // concept requirements
212 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
213 //return __a < __b ? __b : __a;
220 * @brief This does what you think it does.
221 * @ingroup sorting_algorithms
222 * @param a A thing of arbitrary type.
223 * @param b Another thing of arbitrary type.
224 * @param comp A @link comparison_functors comparison functor@endlink.
225 * @return The lesser of the parameters.
227 * This will work on temporary expressions, since they are only evaluated
228 * once, unlike a preprocessor macro.
230 template<typename _Tp, typename _Compare>
232 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
234 //return __comp(__b, __a) ? __b : __a;
235 if (__comp(__b, __a))
241 * @brief This does what you think it does.
242 * @ingroup sorting_algorithms
243 * @param a A thing of arbitrary type.
244 * @param b Another thing of arbitrary type.
245 * @param comp A @link comparison_functors comparison functor@endlink.
246 * @return The greater of the parameters.
248 * This will work on temporary expressions, since they are only evaluated
249 * once, unlike a preprocessor macro.
251 template<typename _Tp, typename _Compare>
253 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
255 //return __comp(__a, __b) ? __b : __a;
256 if (__comp(__a, __b))
262 // If _Iterator is a __normal_iterator return its base (a plain pointer,
263 // normally) otherwise return it untouched. See copy, fill, ...
264 template<typename _Iterator,
265 bool _IsNormal = __is_normal_iterator<_Iterator>::__value>
273 template<typename _Iterator>
274 struct __niter_base<_Iterator, true>
276 static typename _Iterator::iterator_type
278 { return __it.base(); }
281 // Likewise, for move_iterator.
282 template<typename _Iterator,
283 bool _IsMove = __is_move_iterator<_Iterator>::__value>
291 template<typename _Iterator>
292 struct __miter_base<_Iterator, true>
294 static typename _Iterator::iterator_type
296 { return __it.base(); }
299 // All of these auxiliary structs serve two purposes. (1) Replace
300 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
301 // because the input and output ranges are permitted to overlap.)
302 // (2) If we're using random access iterators, then write the loop as
303 // a for loop with an explicit count.
305 template<bool, bool, typename>
308 template<typename _II, typename _OI>
310 __copy_m(_II __first, _II __last, _OI __result)
312 for (; __first != __last; ++__result, ++__first)
313 *__result = *__first;
318 #ifdef __GXX_EXPERIMENTAL_CXX0X__
319 template<typename _Category>
320 struct __copy_move<true, false, _Category>
322 template<typename _II, typename _OI>
324 __copy_m(_II __first, _II __last, _OI __result)
326 for (; __first != __last; ++__result, ++__first)
327 *__result = std::move(*__first);
334 struct __copy_move<false, false, random_access_iterator_tag>
336 template<typename _II, typename _OI>
338 __copy_m(_II __first, _II __last, _OI __result)
340 typedef typename iterator_traits<_II>::difference_type _Distance;
341 for(_Distance __n = __last - __first; __n > 0; --__n)
343 *__result = *__first;
351 #ifdef __GXX_EXPERIMENTAL_CXX0X__
353 struct __copy_move<true, false, random_access_iterator_tag>
355 template<typename _II, typename _OI>
357 __copy_m(_II __first, _II __last, _OI __result)
359 typedef typename iterator_traits<_II>::difference_type _Distance;
360 for(_Distance __n = __last - __first; __n > 0; --__n)
362 *__result = std::move(*__first);
371 template<bool _IsMove>
372 struct __copy_move<_IsMove, true, random_access_iterator_tag>
374 template<typename _Tp>
376 __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
378 const ptrdiff_t _Num = __last - __first;
380 __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
381 return __result + _Num;
385 template<bool _IsMove, typename _II, typename _OI>
387 __copy_move_a(_II __first, _II __last, _OI __result)
389 typedef typename iterator_traits<_II>::value_type _ValueTypeI;
390 typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
391 typedef typename iterator_traits<_II>::iterator_category _Category;
392 const bool __simple = (__is_pod(_ValueTypeI)
393 && __is_pointer<_II>::__value
394 && __is_pointer<_OI>::__value
395 && __are_same<_ValueTypeI, _ValueTypeO>::__value);
397 return std::__copy_move<_IsMove, __simple,
398 _Category>::__copy_m(__first, __last, __result);
401 // Helpers for streambuf iterators (either istream or ostream).
402 // NB: avoid including <iosfwd>, relatively large.
403 template<typename _CharT>
406 template<typename _CharT, typename _Traits>
407 class istreambuf_iterator;
409 template<typename _CharT, typename _Traits>
410 class ostreambuf_iterator;
412 template<bool _IsMove, typename _CharT>
413 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
414 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
415 __copy_move_a2(_CharT*, _CharT*,
416 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
418 template<bool _IsMove, typename _CharT>
419 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
420 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
421 __copy_move_a2(const _CharT*, const _CharT*,
422 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
424 template<bool _IsMove, typename _CharT>
425 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
427 __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >,
428 istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
430 template<bool _IsMove, typename _II, typename _OI>
432 __copy_move_a2(_II __first, _II __last, _OI __result)
434 return _OI(std::__copy_move_a<_IsMove>
435 (std::__niter_base<_II>::__b(__first),
436 std::__niter_base<_II>::__b(__last),
437 std::__niter_base<_OI>::__b(__result)));
441 * @brief Copies the range [first,last) into result.
442 * @ingroup mutating_algorithms
443 * @param first An input iterator.
444 * @param last An input iterator.
445 * @param result An output iterator.
446 * @return result + (first - last)
448 * This inline function will boil down to a call to @c memmove whenever
449 * possible. Failing that, if random access iterators are passed, then the
450 * loop count will be known (and therefore a candidate for compiler
451 * optimizations such as unrolling). Result may not be contained within
452 * [first,last); the copy_backward function should be used instead.
454 * Note that the end of the output range is permitted to be contained
455 * within [first,last).
457 template<typename _II, typename _OI>
459 copy(_II __first, _II __last, _OI __result)
461 // concept requirements
462 __glibcxx_function_requires(_InputIteratorConcept<_II>)
463 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
464 typename iterator_traits<_II>::value_type>)
465 __glibcxx_requires_valid_range(__first, __last);
467 return (std::__copy_move_a2<__is_move_iterator<_II>::__value>
468 (std::__miter_base<_II>::__b(__first),
469 std::__miter_base<_II>::__b(__last), __result));
472 #ifdef __GXX_EXPERIMENTAL_CXX0X__
474 * @brief Moves the range [first,last) into result.
475 * @ingroup mutating_algorithms
476 * @param first An input iterator.
477 * @param last An input iterator.
478 * @param result An output iterator.
479 * @return result + (first - last)
481 * This inline function will boil down to a call to @c memmove whenever
482 * possible. Failing that, if random access iterators are passed, then the
483 * loop count will be known (and therefore a candidate for compiler
484 * optimizations such as unrolling). Result may not be contained within
485 * [first,last); the move_backward function should be used instead.
487 * Note that the end of the output range is permitted to be contained
488 * within [first,last).
490 template<typename _II, typename _OI>
492 move(_II __first, _II __last, _OI __result)
494 // concept requirements
495 __glibcxx_function_requires(_InputIteratorConcept<_II>)
496 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
497 typename iterator_traits<_II>::value_type>)
498 __glibcxx_requires_valid_range(__first, __last);
500 return (std::__copy_move_a2<true>
501 (std::__miter_base<_II>::__b(__first),
502 std::__miter_base<_II>::__b(__last), __result));
505 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
507 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
510 template<bool, bool, typename>
511 struct __copy_move_backward
513 template<typename _BI1, typename _BI2>
515 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
517 while (__first != __last)
518 *--__result = *--__last;
523 #ifdef __GXX_EXPERIMENTAL_CXX0X__
524 template<typename _Category>
525 struct __copy_move_backward<true, false, _Category>
527 template<typename _BI1, typename _BI2>
529 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
531 while (__first != __last)
532 *--__result = std::move(*--__last);
539 struct __copy_move_backward<false, false, random_access_iterator_tag>
541 template<typename _BI1, typename _BI2>
543 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
545 typename iterator_traits<_BI1>::difference_type __n;
546 for (__n = __last - __first; __n > 0; --__n)
547 *--__result = *--__last;
552 #ifdef __GXX_EXPERIMENTAL_CXX0X__
554 struct __copy_move_backward<true, false, random_access_iterator_tag>
556 template<typename _BI1, typename _BI2>
558 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
560 typename iterator_traits<_BI1>::difference_type __n;
561 for (__n = __last - __first; __n > 0; --__n)
562 *--__result = std::move(*--__last);
568 template<bool _IsMove>
569 struct __copy_move_backward<_IsMove, true, random_access_iterator_tag>
571 template<typename _Tp>
573 __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
575 const ptrdiff_t _Num = __last - __first;
577 __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
578 return __result - _Num;
582 template<bool _IsMove, typename _BI1, typename _BI2>
584 __copy_move_backward_a(_BI1 __first, _BI1 __last, _BI2 __result)
586 typedef typename iterator_traits<_BI1>::value_type _ValueType1;
587 typedef typename iterator_traits<_BI2>::value_type _ValueType2;
588 typedef typename iterator_traits<_BI1>::iterator_category _Category;
589 const bool __simple = (__is_pod(_ValueType1)
590 && __is_pointer<_BI1>::__value
591 && __is_pointer<_BI2>::__value
592 && __are_same<_ValueType1, _ValueType2>::__value);
594 return std::__copy_move_backward<_IsMove, __simple,
595 _Category>::__copy_move_b(__first,
600 template<bool _IsMove, typename _BI1, typename _BI2>
602 __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result)
604 return _BI2(std::__copy_move_backward_a<_IsMove>
605 (std::__niter_base<_BI1>::__b(__first),
606 std::__niter_base<_BI1>::__b(__last),
607 std::__niter_base<_BI2>::__b(__result)));
611 * @brief Copies the range [first,last) into result.
612 * @ingroup mutating_algorithms
613 * @param first A bidirectional iterator.
614 * @param last A bidirectional iterator.
615 * @param result A bidirectional iterator.
616 * @return result - (first - last)
618 * The function has the same effect as copy, but starts at the end of the
619 * range and works its way to the start, returning the start of the result.
620 * This inline function will boil down to a call to @c memmove whenever
621 * possible. Failing that, if random access iterators are passed, then the
622 * loop count will be known (and therefore a candidate for compiler
623 * optimizations such as unrolling).
625 * Result may not be in the range [first,last). Use copy instead. Note
626 * that the start of the output range may overlap [first,last).
628 template<typename _BI1, typename _BI2>
630 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
632 // concept requirements
633 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
634 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
635 __glibcxx_function_requires(_ConvertibleConcept<
636 typename iterator_traits<_BI1>::value_type,
637 typename iterator_traits<_BI2>::value_type>)
638 __glibcxx_requires_valid_range(__first, __last);
640 return (std::__copy_move_backward_a2<__is_move_iterator<_BI1>::__value>
641 (std::__miter_base<_BI1>::__b(__first),
642 std::__miter_base<_BI1>::__b(__last), __result));
645 #ifdef __GXX_EXPERIMENTAL_CXX0X__
647 * @brief Moves the range [first,last) into result.
648 * @ingroup mutating_algorithms
649 * @param first A bidirectional iterator.
650 * @param last A bidirectional iterator.
651 * @param result A bidirectional iterator.
652 * @return result - (first - last)
654 * The function has the same effect as move, but starts at the end of the
655 * range and works its way to the start, returning the start of the result.
656 * This inline function will boil down to a call to @c memmove whenever
657 * possible. Failing that, if random access iterators are passed, then the
658 * loop count will be known (and therefore a candidate for compiler
659 * optimizations such as unrolling).
661 * Result may not be in the range [first,last). Use move instead. Note
662 * that the start of the output range may overlap [first,last).
664 template<typename _BI1, typename _BI2>
666 move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
668 // concept requirements
669 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
670 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
671 __glibcxx_function_requires(_ConvertibleConcept<
672 typename iterator_traits<_BI1>::value_type,
673 typename iterator_traits<_BI2>::value_type>)
674 __glibcxx_requires_valid_range(__first, __last);
676 return (std::__copy_move_backward_a2<true>
677 (std::__miter_base<_BI1>::__b(__first),
678 std::__miter_base<_BI1>::__b(__last), __result));
681 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
683 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
686 template<typename _ForwardIterator, typename _Tp>
688 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
689 __fill_a(_ForwardIterator __first, _ForwardIterator __last,
692 for (; __first != __last; ++__first)
696 template<typename _ForwardIterator, typename _Tp>
698 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
699 __fill_a(_ForwardIterator __first, _ForwardIterator __last,
702 const _Tp __tmp = __value;
703 for (; __first != __last; ++__first)
707 // Specialization: for char types we can use memset.
708 template<typename _Tp>
710 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type
711 __fill_a(_Tp* __first, _Tp* __last, const _Tp& __c)
713 const _Tp __tmp = __c;
714 __builtin_memset(__first, static_cast<unsigned char>(__tmp),
719 * @brief Fills the range [first,last) with copies of value.
720 * @ingroup mutating_algorithms
721 * @param first A forward iterator.
722 * @param last A forward iterator.
723 * @param value A reference-to-const of arbitrary type.
726 * This function fills a range with copies of the same value. For char
727 * types filling contiguous areas of memory, this becomes an inline call
728 * to @c memset or @c wmemset.
730 template<typename _ForwardIterator, typename _Tp>
732 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
734 // concept requirements
735 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
737 __glibcxx_requires_valid_range(__first, __last);
739 std::__fill_a(std::__niter_base<_ForwardIterator>::__b(__first),
740 std::__niter_base<_ForwardIterator>::__b(__last), __value);
743 template<typename _OutputIterator, typename _Size, typename _Tp>
745 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
746 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
748 for (; __n > 0; --__n, ++__first)
753 template<typename _OutputIterator, typename _Size, typename _Tp>
755 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
756 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
758 const _Tp __tmp = __value;
759 for (; __n > 0; --__n, ++__first)
764 template<typename _Size, typename _Tp>
766 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, _Tp*>::__type
767 __fill_n_a(_Tp* __first, _Size __n, const _Tp& __c)
769 std::__fill_a(__first, __first + __n, __c);
770 return __first + __n;
774 * @brief Fills the range [first,first+n) with copies of value.
775 * @ingroup mutating_algorithms
776 * @param first An output iterator.
777 * @param n The count of copies to perform.
778 * @param value A reference-to-const of arbitrary type.
779 * @return The iterator at first+n.
781 * This function fills a range with copies of the same value. For char
782 * types filling contiguous areas of memory, this becomes an inline call
783 * to @c memset or @ wmemset.
785 template<typename _OI, typename _Size, typename _Tp>
787 fill_n(_OI __first, _Size __n, const _Tp& __value)
789 // concept requirements
790 __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>)
792 return _OI(std::__fill_n_a(std::__niter_base<_OI>::__b(__first),
796 template<bool _BoolType>
799 template<typename _II1, typename _II2>
801 equal(_II1 __first1, _II1 __last1, _II2 __first2)
803 for (; __first1 != __last1; ++__first1, ++__first2)
804 if (!(*__first1 == *__first2))
813 template<typename _Tp>
815 equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
817 return !__builtin_memcmp(__first1, __first2, sizeof(_Tp)
818 * (__last1 - __first1));
822 template<typename _II1, typename _II2>
824 __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
826 typedef typename iterator_traits<_II1>::value_type _ValueType1;
827 typedef typename iterator_traits<_II2>::value_type _ValueType2;
828 const bool __simple = (__is_integer<_ValueType1>::__value
829 && __is_pointer<_II1>::__value
830 && __is_pointer<_II2>::__value
831 && __are_same<_ValueType1, _ValueType2>::__value);
833 return std::__equal<__simple>::equal(__first1, __last1, __first2);
837 template<typename, typename>
840 template<typename _II1, typename _II2>
842 __newlast1(_II1, _II1 __last1, _II2, _II2)
845 template<typename _II>
847 __cnd2(_II __first, _II __last)
848 { return __first != __last; }
852 struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag>
854 template<typename _RAI1, typename _RAI2>
856 __newlast1(_RAI1 __first1, _RAI1 __last1,
857 _RAI2 __first2, _RAI2 __last2)
859 const typename iterator_traits<_RAI1>::difference_type
860 __diff1 = __last1 - __first1;
861 const typename iterator_traits<_RAI2>::difference_type
862 __diff2 = __last2 - __first2;
863 return __diff2 < __diff1 ? __first1 + __diff2 : __last1;
866 template<typename _RAI>
872 template<bool _BoolType>
873 struct __lexicographical_compare
875 template<typename _II1, typename _II2>
876 static bool __lc(_II1, _II1, _II2, _II2);
879 template<bool _BoolType>
880 template<typename _II1, typename _II2>
882 __lexicographical_compare<_BoolType>::
883 __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
885 typedef typename iterator_traits<_II1>::iterator_category _Category1;
886 typedef typename iterator_traits<_II2>::iterator_category _Category2;
887 typedef std::__lc_rai<_Category1, _Category2> __rai_type;
889 __last1 = __rai_type::__newlast1(__first1, __last1,
891 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
892 ++__first1, ++__first2)
894 if (*__first1 < *__first2)
896 if (*__first2 < *__first1)
899 return __first1 == __last1 && __first2 != __last2;
903 struct __lexicographical_compare<true>
905 template<typename _Tp, typename _Up>
907 __lc(const _Tp* __first1, const _Tp* __last1,
908 const _Up* __first2, const _Up* __last2)
910 const size_t __len1 = __last1 - __first1;
911 const size_t __len2 = __last2 - __first2;
912 const int __result = __builtin_memcmp(__first1, __first2,
913 std::min(__len1, __len2));
914 return __result != 0 ? __result < 0 : __len1 < __len2;
918 template<typename _II1, typename _II2>
920 __lexicographical_compare_aux(_II1 __first1, _II1 __last1,
921 _II2 __first2, _II2 __last2)
923 typedef typename iterator_traits<_II1>::value_type _ValueType1;
924 typedef typename iterator_traits<_II2>::value_type _ValueType2;
925 const bool __simple =
926 (__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
927 && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
928 && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
929 && __is_pointer<_II1>::__value
930 && __is_pointer<_II2>::__value);
932 return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
936 _GLIBCXX_END_NAMESPACE
938 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
941 * @brief Tests a range for element-wise equality.
942 * @ingroup non_mutating_algorithms
943 * @param first1 An input iterator.
944 * @param last1 An input iterator.
945 * @param first2 An input iterator.
946 * @return A boolean true or false.
948 * This compares the elements of two ranges using @c == and returns true or
949 * false depending on whether all of the corresponding elements of the
952 template<typename _II1, typename _II2>
954 equal(_II1 __first1, _II1 __last1, _II2 __first2)
956 // concept requirements
957 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
958 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
959 __glibcxx_function_requires(_EqualOpConcept<
960 typename iterator_traits<_II1>::value_type,
961 typename iterator_traits<_II2>::value_type>)
962 __glibcxx_requires_valid_range(__first1, __last1);
964 return std::__equal_aux(std::__niter_base<_II1>::__b(__first1),
965 std::__niter_base<_II1>::__b(__last1),
966 std::__niter_base<_II2>::__b(__first2));
970 * @brief Tests a range for element-wise equality.
971 * @ingroup non_mutating_algorithms
972 * @param first1 An input iterator.
973 * @param last1 An input iterator.
974 * @param first2 An input iterator.
975 * @param binary_pred A binary predicate @link functors
977 * @return A boolean true or false.
979 * This compares the elements of two ranges using the binary_pred
980 * parameter, and returns true or
981 * false depending on whether all of the corresponding elements of the
984 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
986 equal(_IIter1 __first1, _IIter1 __last1,
987 _IIter2 __first2, _BinaryPredicate __binary_pred)
989 // concept requirements
990 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
991 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
992 __glibcxx_requires_valid_range(__first1, __last1);
994 for (; __first1 != __last1; ++__first1, ++__first2)
995 if (!bool(__binary_pred(*__first1, *__first2)))
1001 * @brief Performs 'dictionary' comparison on ranges.
1002 * @ingroup sorting_algorithms
1003 * @param first1 An input iterator.
1004 * @param last1 An input iterator.
1005 * @param first2 An input iterator.
1006 * @param last2 An input iterator.
1007 * @return A boolean true or false.
1009 * 'Returns true if the sequence of elements defined by the range
1010 * [first1,last1) is lexicographically less than the sequence of elements
1011 * defined by the range [first2,last2). Returns false otherwise.'
1012 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
1013 * then this is an inline call to @c memcmp.
1015 template<typename _II1, typename _II2>
1017 lexicographical_compare(_II1 __first1, _II1 __last1,
1018 _II2 __first2, _II2 __last2)
1020 // concept requirements
1021 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1022 typedef typename iterator_traits<_II2>::value_type _ValueType2;
1023 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1024 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1025 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
1026 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
1027 __glibcxx_requires_valid_range(__first1, __last1);
1028 __glibcxx_requires_valid_range(__first2, __last2);
1030 return std::__lexicographical_compare_aux
1031 (std::__niter_base<_II1>::__b(__first1),
1032 std::__niter_base<_II1>::__b(__last1),
1033 std::__niter_base<_II2>::__b(__first2),
1034 std::__niter_base<_II2>::__b(__last2));
1038 * @brief Performs "dictionary" comparison on ranges.
1039 * @ingroup sorting_algorithms
1040 * @param first1 An input iterator.
1041 * @param last1 An input iterator.
1042 * @param first2 An input iterator.
1043 * @param last2 An input iterator.
1044 * @param comp A @link comparison_functors comparison functor@endlink.
1045 * @return A boolean true or false.
1047 * The same as the four-parameter @c lexicographical_compare, but uses the
1048 * comp parameter instead of @c <.
1050 template<typename _II1, typename _II2, typename _Compare>
1052 lexicographical_compare(_II1 __first1, _II1 __last1,
1053 _II2 __first2, _II2 __last2, _Compare __comp)
1055 typedef typename iterator_traits<_II1>::iterator_category _Category1;
1056 typedef typename iterator_traits<_II2>::iterator_category _Category2;
1057 typedef std::__lc_rai<_Category1, _Category2> __rai_type;
1059 // concept requirements
1060 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1061 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1062 __glibcxx_requires_valid_range(__first1, __last1);
1063 __glibcxx_requires_valid_range(__first2, __last2);
1065 __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
1066 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
1067 ++__first1, ++__first2)
1069 if (__comp(*__first1, *__first2))
1071 if (__comp(*__first2, *__first1))
1074 return __first1 == __last1 && __first2 != __last2;
1078 * @brief Finds the places in ranges which don't match.
1079 * @ingroup non_mutating_algorithms
1080 * @param first1 An input iterator.
1081 * @param last1 An input iterator.
1082 * @param first2 An input iterator.
1083 * @return A pair of iterators pointing to the first mismatch.
1085 * This compares the elements of two ranges using @c == and returns a pair
1086 * of iterators. The first iterator points into the first range, the
1087 * second iterator points into the second range, and the elements pointed
1088 * to by the iterators are not equal.
1090 template<typename _InputIterator1, typename _InputIterator2>
1091 pair<_InputIterator1, _InputIterator2>
1092 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1093 _InputIterator2 __first2)
1095 // concept requirements
1096 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1097 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1098 __glibcxx_function_requires(_EqualOpConcept<
1099 typename iterator_traits<_InputIterator1>::value_type,
1100 typename iterator_traits<_InputIterator2>::value_type>)
1101 __glibcxx_requires_valid_range(__first1, __last1);
1103 while (__first1 != __last1 && *__first1 == *__first2)
1108 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1112 * @brief Finds the places in ranges which don't match.
1113 * @ingroup non_mutating_algorithms
1114 * @param first1 An input iterator.
1115 * @param last1 An input iterator.
1116 * @param first2 An input iterator.
1117 * @param binary_pred A binary predicate @link functors
1119 * @return A pair of iterators pointing to the first mismatch.
1121 * This compares the elements of two ranges using the binary_pred
1122 * parameter, and returns a pair
1123 * of iterators. The first iterator points into the first range, the
1124 * second iterator points into the second range, and the elements pointed
1125 * to by the iterators are not equal.
1127 template<typename _InputIterator1, typename _InputIterator2,
1128 typename _BinaryPredicate>
1129 pair<_InputIterator1, _InputIterator2>
1130 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1131 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1133 // concept requirements
1134 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1135 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1136 __glibcxx_requires_valid_range(__first1, __last1);
1138 while (__first1 != __last1 && bool(__binary_pred(*__first1, *__first2)))
1143 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1146 _GLIBCXX_END_NESTED_NAMESPACE
1148 // NB: This file is included within many other C++ includes, as a way
1149 // of getting the base algorithms. So, make sure that parallel bits
1150 // come in too if requested.
1151 #ifdef _GLIBCXX_PARALLEL
1152 # include <parallel/algobase.h>