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 __builtin_memmove(__result
, __first
,
379 sizeof(_Tp
) * (__last
- __first
));
380 return __result
+ (__last
- __first
);
384 template<bool _IsMove
, typename _II
, typename _OI
>
386 __copy_move_a(_II __first
, _II __last
, _OI __result
)
388 typedef typename iterator_traits
<_II
>::value_type _ValueTypeI
;
389 typedef typename iterator_traits
<_OI
>::value_type _ValueTypeO
;
390 typedef typename iterator_traits
<_II
>::iterator_category _Category
;
391 const bool __simple
= (__is_pod(_ValueTypeI
)
392 && __is_pointer
<_II
>::__value
393 && __is_pointer
<_OI
>::__value
394 && __are_same
<_ValueTypeI
, _ValueTypeO
>::__value
);
396 return std::__copy_move
<_IsMove
, __simple
,
397 _Category
>::__copy_m(__first
, __last
, __result
);
400 // Helpers for streambuf iterators (either istream or ostream).
401 // NB: avoid including <iosfwd>, relatively large.
402 template<typename _CharT
>
405 template<typename _CharT
, typename _Traits
>
406 class istreambuf_iterator
;
408 template<typename _CharT
, typename _Traits
>
409 class ostreambuf_iterator
;
411 template<bool _IsMove
, typename _CharT
>
412 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
413 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> > >::__type
414 __copy_move_a2(_CharT
*, _CharT
*,
415 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> >);
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(const _CharT
*, const _CharT
*,
421 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> >);
423 template<bool _IsMove
, typename _CharT
>
424 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
426 __copy_move_a2(istreambuf_iterator
<_CharT
, char_traits
<_CharT
> >,
427 istreambuf_iterator
<_CharT
, char_traits
<_CharT
> >, _CharT
*);
429 template<bool _IsMove
, typename _II
, typename _OI
>
431 __copy_move_a2(_II __first
, _II __last
, _OI __result
)
433 return _OI(std::__copy_move_a
<_IsMove
>
434 (std::__niter_base
<_II
>::__b(__first
),
435 std::__niter_base
<_II
>::__b(__last
),
436 std::__niter_base
<_OI
>::__b(__result
)));
440 * @brief Copies the range [first,last) into result.
441 * @ingroup mutating_algorithms
442 * @param first An input iterator.
443 * @param last An input iterator.
444 * @param result An output iterator.
445 * @return result + (first - last)
447 * This inline function will boil down to a call to @c memmove whenever
448 * possible. Failing that, if random access iterators are passed, then the
449 * loop count will be known (and therefore a candidate for compiler
450 * optimizations such as unrolling). Result may not be contained within
451 * [first,last); the copy_backward function should be used instead.
453 * Note that the end of the output range is permitted to be contained
454 * within [first,last).
456 template<typename _II
, typename _OI
>
458 copy(_II __first
, _II __last
, _OI __result
)
460 // concept requirements
461 __glibcxx_function_requires(_InputIteratorConcept
<_II
>)
462 __glibcxx_function_requires(_OutputIteratorConcept
<_OI
,
463 typename iterator_traits
<_II
>::value_type
>)
464 __glibcxx_requires_valid_range(__first
, __last
);
466 return (std::__copy_move_a2
<__is_move_iterator
<_II
>::__value
>
467 (std::__miter_base
<_II
>::__b(__first
),
468 std::__miter_base
<_II
>::__b(__last
), __result
));
471 #ifdef __GXX_EXPERIMENTAL_CXX0X__
473 * @brief Moves the range [first,last) into result.
474 * @ingroup mutating_algorithms
475 * @param first An input iterator.
476 * @param last An input iterator.
477 * @param result An output iterator.
478 * @return result + (first - last)
480 * This inline function will boil down to a call to @c memmove whenever
481 * possible. Failing that, if random access iterators are passed, then the
482 * loop count will be known (and therefore a candidate for compiler
483 * optimizations such as unrolling). Result may not be contained within
484 * [first,last); the move_backward function should be used instead.
486 * Note that the end of the output range is permitted to be contained
487 * within [first,last).
489 template<typename _II
, typename _OI
>
491 move(_II __first
, _II __last
, _OI __result
)
493 // concept requirements
494 __glibcxx_function_requires(_InputIteratorConcept
<_II
>)
495 __glibcxx_function_requires(_OutputIteratorConcept
<_OI
,
496 typename iterator_traits
<_II
>::value_type
>)
497 __glibcxx_requires_valid_range(__first
, __last
);
499 return (std::__copy_move_a2
<true>
500 (std::__miter_base
<_II
>::__b(__first
),
501 std::__miter_base
<_II
>::__b(__last
), __result
));
504 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
506 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
509 template<bool, bool, typename
>
510 struct __copy_move_backward
512 template<typename _BI1
, typename _BI2
>
514 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
516 while (__first
!= __last
)
517 *--__result
= *--__last
;
522 #ifdef __GXX_EXPERIMENTAL_CXX0X__
523 template<typename _Category
>
524 struct __copy_move_backward
<true, false, _Category
>
526 template<typename _BI1
, typename _BI2
>
528 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
530 while (__first
!= __last
)
531 *--__result
= std::move(*--__last
);
538 struct __copy_move_backward
<false, false, random_access_iterator_tag
>
540 template<typename _BI1
, typename _BI2
>
542 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
544 typename iterator_traits
<_BI1
>::difference_type __n
;
545 for (__n
= __last
- __first
; __n
> 0; --__n
)
546 *--__result
= *--__last
;
551 #ifdef __GXX_EXPERIMENTAL_CXX0X__
553 struct __copy_move_backward
<true, false, random_access_iterator_tag
>
555 template<typename _BI1
, typename _BI2
>
557 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
559 typename iterator_traits
<_BI1
>::difference_type __n
;
560 for (__n
= __last
- __first
; __n
> 0; --__n
)
561 *--__result
= std::move(*--__last
);
567 template<bool _IsMove
>
568 struct __copy_move_backward
<_IsMove
, true, random_access_iterator_tag
>
570 template<typename _Tp
>
572 __copy_move_b(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
574 const ptrdiff_t _Num
= __last
- __first
;
575 __builtin_memmove(__result
- _Num
, __first
, sizeof(_Tp
) * _Num
);
576 return __result
- _Num
;
580 template<bool _IsMove
, typename _BI1
, typename _BI2
>
582 __copy_move_backward_a(_BI1 __first
, _BI1 __last
, _BI2 __result
)
584 typedef typename iterator_traits
<_BI1
>::value_type _ValueType1
;
585 typedef typename iterator_traits
<_BI2
>::value_type _ValueType2
;
586 typedef typename iterator_traits
<_BI1
>::iterator_category _Category
;
587 const bool __simple
= (__is_pod(_ValueType1
)
588 && __is_pointer
<_BI1
>::__value
589 && __is_pointer
<_BI2
>::__value
590 && __are_same
<_ValueType1
, _ValueType2
>::__value
);
592 return std::__copy_move_backward
<_IsMove
, __simple
,
593 _Category
>::__copy_move_b(__first
,
598 template<bool _IsMove
, typename _BI1
, typename _BI2
>
600 __copy_move_backward_a2(_BI1 __first
, _BI1 __last
, _BI2 __result
)
602 return _BI2(std::__copy_move_backward_a
<_IsMove
>
603 (std::__niter_base
<_BI1
>::__b(__first
),
604 std::__niter_base
<_BI1
>::__b(__last
),
605 std::__niter_base
<_BI2
>::__b(__result
)));
609 * @brief Copies the range [first,last) into result.
610 * @ingroup mutating_algorithms
611 * @param first A bidirectional iterator.
612 * @param last A bidirectional iterator.
613 * @param result A bidirectional iterator.
614 * @return result - (first - last)
616 * The function has the same effect as copy, but starts at the end of the
617 * range and works its way to the start, returning the start of the result.
618 * This inline function will boil down to a call to @c memmove whenever
619 * possible. Failing that, if random access iterators are passed, then the
620 * loop count will be known (and therefore a candidate for compiler
621 * optimizations such as unrolling).
623 * Result may not be in the range [first,last). Use copy instead. Note
624 * that the start of the output range may overlap [first,last).
626 template<typename _BI1
, typename _BI2
>
628 copy_backward(_BI1 __first
, _BI1 __last
, _BI2 __result
)
630 // concept requirements
631 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BI1
>)
632 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<_BI2
>)
633 __glibcxx_function_requires(_ConvertibleConcept
<
634 typename iterator_traits
<_BI1
>::value_type
,
635 typename iterator_traits
<_BI2
>::value_type
>)
636 __glibcxx_requires_valid_range(__first
, __last
);
638 return (std::__copy_move_backward_a2
<__is_move_iterator
<_BI1
>::__value
>
639 (std::__miter_base
<_BI1
>::__b(__first
),
640 std::__miter_base
<_BI1
>::__b(__last
), __result
));
643 #ifdef __GXX_EXPERIMENTAL_CXX0X__
645 * @brief Moves the range [first,last) into result.
646 * @ingroup mutating_algorithms
647 * @param first A bidirectional iterator.
648 * @param last A bidirectional iterator.
649 * @param result A bidirectional iterator.
650 * @return result - (first - last)
652 * The function has the same effect as move, but starts at the end of the
653 * range and works its way to the start, returning the start of the result.
654 * This inline function will boil down to a call to @c memmove whenever
655 * possible. Failing that, if random access iterators are passed, then the
656 * loop count will be known (and therefore a candidate for compiler
657 * optimizations such as unrolling).
659 * Result may not be in the range [first,last). Use move instead. Note
660 * that the start of the output range may overlap [first,last).
662 template<typename _BI1
, typename _BI2
>
664 move_backward(_BI1 __first
, _BI1 __last
, _BI2 __result
)
666 // concept requirements
667 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BI1
>)
668 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<_BI2
>)
669 __glibcxx_function_requires(_ConvertibleConcept
<
670 typename iterator_traits
<_BI1
>::value_type
,
671 typename iterator_traits
<_BI2
>::value_type
>)
672 __glibcxx_requires_valid_range(__first
, __last
);
674 return (std::__copy_move_backward_a2
<true>
675 (std::__miter_base
<_BI1
>::__b(__first
),
676 std::__miter_base
<_BI1
>::__b(__last
), __result
));
679 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
681 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
684 template<typename _ForwardIterator
, typename _Tp
>
686 __gnu_cxx::__enable_if
<!__is_scalar
<_Tp
>::__value
, void>::__type
687 __fill_a(_ForwardIterator __first
, _ForwardIterator __last
,
690 for (; __first
!= __last
; ++__first
)
694 template<typename _ForwardIterator
, typename _Tp
>
696 __gnu_cxx::__enable_if
<__is_scalar
<_Tp
>::__value
, void>::__type
697 __fill_a(_ForwardIterator __first
, _ForwardIterator __last
,
700 const _Tp __tmp
= __value
;
701 for (; __first
!= __last
; ++__first
)
705 // Specialization: for char types we can use memset.
706 template<typename _Tp
>
708 __gnu_cxx::__enable_if
<__is_byte
<_Tp
>::__value
, void>::__type
709 __fill_a(_Tp
* __first
, _Tp
* __last
, const _Tp
& __c
)
711 const _Tp __tmp
= __c
;
712 __builtin_memset(__first
, static_cast<unsigned char>(__tmp
),
717 * @brief Fills the range [first,last) with copies of value.
718 * @ingroup mutating_algorithms
719 * @param first A forward iterator.
720 * @param last A forward iterator.
721 * @param value A reference-to-const of arbitrary type.
724 * This function fills a range with copies of the same value. For char
725 * types filling contiguous areas of memory, this becomes an inline call
726 * to @c memset or @c wmemset.
728 template<typename _ForwardIterator
, typename _Tp
>
730 fill(_ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __value
)
732 // concept requirements
733 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
735 __glibcxx_requires_valid_range(__first
, __last
);
737 std::__fill_a(std::__niter_base
<_ForwardIterator
>::__b(__first
),
738 std::__niter_base
<_ForwardIterator
>::__b(__last
), __value
);
741 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
743 __gnu_cxx::__enable_if
<!__is_scalar
<_Tp
>::__value
, _OutputIterator
>::__type
744 __fill_n_a(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
746 for (; __n
> 0; --__n
, ++__first
)
751 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
753 __gnu_cxx::__enable_if
<__is_scalar
<_Tp
>::__value
, _OutputIterator
>::__type
754 __fill_n_a(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
756 const _Tp __tmp
= __value
;
757 for (; __n
> 0; --__n
, ++__first
)
762 template<typename _Size
, typename _Tp
>
764 __gnu_cxx::__enable_if
<__is_byte
<_Tp
>::__value
, _Tp
*>::__type
765 __fill_n_a(_Tp
* __first
, _Size __n
, const _Tp
& __c
)
767 std::__fill_a(__first
, __first
+ __n
, __c
);
768 return __first
+ __n
;
772 * @brief Fills the range [first,first+n) with copies of value.
773 * @ingroup mutating_algorithms
774 * @param first An output iterator.
775 * @param n The count of copies to perform.
776 * @param value A reference-to-const of arbitrary type.
777 * @return The iterator at first+n.
779 * This function fills a range with copies of the same value. For char
780 * types filling contiguous areas of memory, this becomes an inline call
781 * to @c memset or @ wmemset.
783 template<typename _OI
, typename _Size
, typename _Tp
>
785 fill_n(_OI __first
, _Size __n
, const _Tp
& __value
)
787 // concept requirements
788 __glibcxx_function_requires(_OutputIteratorConcept
<_OI
, _Tp
>)
790 return _OI(std::__fill_n_a(std::__niter_base
<_OI
>::__b(__first
),
794 template<bool _BoolType
>
797 template<typename _II1
, typename _II2
>
799 equal(_II1 __first1
, _II1 __last1
, _II2 __first2
)
801 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
802 if (!(*__first1
== *__first2
))
811 template<typename _Tp
>
813 equal(const _Tp
* __first1
, const _Tp
* __last1
, const _Tp
* __first2
)
815 return !__builtin_memcmp(__first1
, __first2
, sizeof(_Tp
)
816 * (__last1
- __first1
));
820 template<typename _II1
, typename _II2
>
822 __equal_aux(_II1 __first1
, _II1 __last1
, _II2 __first2
)
824 typedef typename iterator_traits
<_II1
>::value_type _ValueType1
;
825 typedef typename iterator_traits
<_II2
>::value_type _ValueType2
;
826 const bool __simple
= (__is_integer
<_ValueType1
>::__value
827 && __is_pointer
<_II1
>::__value
828 && __is_pointer
<_II2
>::__value
829 && __are_same
<_ValueType1
, _ValueType2
>::__value
);
831 return std::__equal
<__simple
>::equal(__first1
, __last1
, __first2
);
835 template<typename
, typename
>
838 template<typename _II1
, typename _II2
>
840 __newlast1(_II1
, _II1 __last1
, _II2
, _II2
)
843 template<typename _II
>
845 __cnd2(_II __first
, _II __last
)
846 { return __first
!= __last
; }
850 struct __lc_rai
<random_access_iterator_tag
, random_access_iterator_tag
>
852 template<typename _RAI1
, typename _RAI2
>
854 __newlast1(_RAI1 __first1
, _RAI1 __last1
,
855 _RAI2 __first2
, _RAI2 __last2
)
857 const typename iterator_traits
<_RAI1
>::difference_type
858 __diff1
= __last1
- __first1
;
859 const typename iterator_traits
<_RAI2
>::difference_type
860 __diff2
= __last2
- __first2
;
861 return __diff2
< __diff1
? __first1
+ __diff2
: __last1
;
864 template<typename _RAI
>
870 template<bool _BoolType
>
871 struct __lexicographical_compare
873 template<typename _II1
, typename _II2
>
874 static bool __lc(_II1
, _II1
, _II2
, _II2
);
877 template<bool _BoolType
>
878 template<typename _II1
, typename _II2
>
880 __lexicographical_compare
<_BoolType
>::
881 __lc(_II1 __first1
, _II1 __last1
, _II2 __first2
, _II2 __last2
)
883 typedef typename iterator_traits
<_II1
>::iterator_category _Category1
;
884 typedef typename iterator_traits
<_II2
>::iterator_category _Category2
;
885 typedef std::__lc_rai
<_Category1
, _Category2
> __rai_type
;
887 __last1
= __rai_type::__newlast1(__first1
, __last1
,
889 for (; __first1
!= __last1
&& __rai_type::__cnd2(__first2
, __last2
);
890 ++__first1
, ++__first2
)
892 if (*__first1
< *__first2
)
894 if (*__first2
< *__first1
)
897 return __first1
== __last1
&& __first2
!= __last2
;
901 struct __lexicographical_compare
<true>
903 template<typename _Tp
, typename _Up
>
905 __lc(const _Tp
* __first1
, const _Tp
* __last1
,
906 const _Up
* __first2
, const _Up
* __last2
)
908 const size_t __len1
= __last1
- __first1
;
909 const size_t __len2
= __last2
- __first2
;
910 const int __result
= __builtin_memcmp(__first1
, __first2
,
911 std::min(__len1
, __len2
));
912 return __result
!= 0 ? __result
< 0 : __len1
< __len2
;
916 template<typename _II1
, typename _II2
>
918 __lexicographical_compare_aux(_II1 __first1
, _II1 __last1
,
919 _II2 __first2
, _II2 __last2
)
921 typedef typename iterator_traits
<_II1
>::value_type _ValueType1
;
922 typedef typename iterator_traits
<_II2
>::value_type _ValueType2
;
923 const bool __simple
=
924 (__is_byte
<_ValueType1
>::__value
&& __is_byte
<_ValueType2
>::__value
925 && !__gnu_cxx::__numeric_traits
<_ValueType1
>::__is_signed
926 && !__gnu_cxx::__numeric_traits
<_ValueType2
>::__is_signed
927 && __is_pointer
<_II1
>::__value
928 && __is_pointer
<_II2
>::__value
);
930 return std::__lexicographical_compare
<__simple
>::__lc(__first1
, __last1
,
934 _GLIBCXX_END_NAMESPACE
936 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_P
)
939 * @brief Tests a range for element-wise equality.
940 * @ingroup non_mutating_algorithms
941 * @param first1 An input iterator.
942 * @param last1 An input iterator.
943 * @param first2 An input iterator.
944 * @return A boolean true or false.
946 * This compares the elements of two ranges using @c == and returns true or
947 * false depending on whether all of the corresponding elements of the
950 template<typename _II1
, typename _II2
>
952 equal(_II1 __first1
, _II1 __last1
, _II2 __first2
)
954 // concept requirements
955 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
956 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
957 __glibcxx_function_requires(_EqualOpConcept
<
958 typename iterator_traits
<_II1
>::value_type
,
959 typename iterator_traits
<_II2
>::value_type
>)
960 __glibcxx_requires_valid_range(__first1
, __last1
);
962 return std::__equal_aux(std::__niter_base
<_II1
>::__b(__first1
),
963 std::__niter_base
<_II1
>::__b(__last1
),
964 std::__niter_base
<_II2
>::__b(__first2
));
968 * @brief Tests a range for element-wise equality.
969 * @ingroup non_mutating_algorithms
970 * @param first1 An input iterator.
971 * @param last1 An input iterator.
972 * @param first2 An input iterator.
973 * @param binary_pred A binary predicate @link functors
975 * @return A boolean true or false.
977 * This compares the elements of two ranges using the binary_pred
978 * parameter, and returns true or
979 * false depending on whether all of the corresponding elements of the
982 template<typename _IIter1
, typename _IIter2
, typename _BinaryPredicate
>
984 equal(_IIter1 __first1
, _IIter1 __last1
,
985 _IIter2 __first2
, _BinaryPredicate __binary_pred
)
987 // concept requirements
988 __glibcxx_function_requires(_InputIteratorConcept
<_IIter1
>)
989 __glibcxx_function_requires(_InputIteratorConcept
<_IIter2
>)
990 __glibcxx_requires_valid_range(__first1
, __last1
);
992 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
993 if (!bool(__binary_pred(*__first1
, *__first2
)))
999 * @brief Performs "dictionary" comparison on ranges.
1000 * @ingroup sorting_algorithms
1001 * @param first1 An input iterator.
1002 * @param last1 An input iterator.
1003 * @param first2 An input iterator.
1004 * @param last2 An input iterator.
1005 * @return A boolean true or false.
1007 * "Returns true if the sequence of elements defined by the range
1008 * [first1,last1) is lexicographically less than the sequence of elements
1009 * defined by the range [first2,last2). Returns false otherwise."
1010 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
1011 * then this is an inline call to @c memcmp.
1013 template<typename _II1
, typename _II2
>
1015 lexicographical_compare(_II1 __first1
, _II1 __last1
,
1016 _II2 __first2
, _II2 __last2
)
1018 // concept requirements
1019 typedef typename iterator_traits
<_II1
>::value_type _ValueType1
;
1020 typedef typename iterator_traits
<_II2
>::value_type _ValueType2
;
1021 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
1022 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
1023 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
1024 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
1025 __glibcxx_requires_valid_range(__first1
, __last1
);
1026 __glibcxx_requires_valid_range(__first2
, __last2
);
1028 return std::__lexicographical_compare_aux
1029 (std::__niter_base
<_II1
>::__b(__first1
),
1030 std::__niter_base
<_II1
>::__b(__last1
),
1031 std::__niter_base
<_II2
>::__b(__first2
),
1032 std::__niter_base
<_II2
>::__b(__last2
));
1036 * @brief Performs "dictionary" comparison on ranges.
1037 * @ingroup sorting_algorithms
1038 * @param first1 An input iterator.
1039 * @param last1 An input iterator.
1040 * @param first2 An input iterator.
1041 * @param last2 An input iterator.
1042 * @param comp A @link comparison_functors comparison functor@endlink.
1043 * @return A boolean true or false.
1045 * The same as the four-parameter @c lexicographical_compare, but uses the
1046 * comp parameter instead of @c <.
1048 template<typename _II1
, typename _II2
, typename _Compare
>
1050 lexicographical_compare(_II1 __first1
, _II1 __last1
,
1051 _II2 __first2
, _II2 __last2
, _Compare __comp
)
1053 typedef typename iterator_traits
<_II1
>::iterator_category _Category1
;
1054 typedef typename iterator_traits
<_II2
>::iterator_category _Category2
;
1055 typedef std::__lc_rai
<_Category1
, _Category2
> __rai_type
;
1057 // concept requirements
1058 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
1059 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
1060 __glibcxx_requires_valid_range(__first1
, __last1
);
1061 __glibcxx_requires_valid_range(__first2
, __last2
);
1063 __last1
= __rai_type::__newlast1(__first1
, __last1
, __first2
, __last2
);
1064 for (; __first1
!= __last1
&& __rai_type::__cnd2(__first2
, __last2
);
1065 ++__first1
, ++__first2
)
1067 if (__comp(*__first1
, *__first2
))
1069 if (__comp(*__first2
, *__first1
))
1072 return __first1
== __last1
&& __first2
!= __last2
;
1076 * @brief Finds the places in ranges which don't match.
1077 * @ingroup non_mutating_algorithms
1078 * @param first1 An input iterator.
1079 * @param last1 An input iterator.
1080 * @param first2 An input iterator.
1081 * @return A pair of iterators pointing to the first mismatch.
1083 * This compares the elements of two ranges using @c == and returns a pair
1084 * of iterators. The first iterator points into the first range, the
1085 * second iterator points into the second range, and the elements pointed
1086 * to by the iterators are not equal.
1088 template<typename _InputIterator1
, typename _InputIterator2
>
1089 pair
<_InputIterator1
, _InputIterator2
>
1090 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1091 _InputIterator2 __first2
)
1093 // concept requirements
1094 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
1095 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
1096 __glibcxx_function_requires(_EqualOpConcept
<
1097 typename iterator_traits
<_InputIterator1
>::value_type
,
1098 typename iterator_traits
<_InputIterator2
>::value_type
>)
1099 __glibcxx_requires_valid_range(__first1
, __last1
);
1101 while (__first1
!= __last1
&& *__first1
== *__first2
)
1106 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
1110 * @brief Finds the places in ranges which don't match.
1111 * @ingroup non_mutating_algorithms
1112 * @param first1 An input iterator.
1113 * @param last1 An input iterator.
1114 * @param first2 An input iterator.
1115 * @param binary_pred A binary predicate @link functors
1117 * @return A pair of iterators pointing to the first mismatch.
1119 * This compares the elements of two ranges using the binary_pred
1120 * parameter, and returns a pair
1121 * of iterators. The first iterator points into the first range, the
1122 * second iterator points into the second range, and the elements pointed
1123 * to by the iterators are not equal.
1125 template<typename _InputIterator1
, typename _InputIterator2
,
1126 typename _BinaryPredicate
>
1127 pair
<_InputIterator1
, _InputIterator2
>
1128 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1129 _InputIterator2 __first2
, _BinaryPredicate __binary_pred
)
1131 // concept requirements
1132 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
1133 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
1134 __glibcxx_requires_valid_range(__first1
, __last1
);
1136 while (__first1
!= __last1
&& bool(__binary_pred(*__first1
, *__first2
)))
1141 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
1144 _GLIBCXX_END_NESTED_NAMESPACE
1146 // NB: This file is included within many other C++ includes, as a way
1147 // of getting the base algorithms. So, make sure that parallel bits
1148 // come in too if requested.
1149 #ifdef _GLIBCXX_PARALLEL
1150 # include <parallel/algobase.h>