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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
45 * Copyright (c) 1996-1998
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
57 /** @file stl_algobase.h
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
62 #ifndef _STL_ALGOBASE_H
63 #define _STL_ALGOBASE_H 1
65 #include <bits/c++config.h>
67 #include <bits/functexcept.h>
68 #include <bits/cpp_type_traits.h>
69 #include <ext/type_traits.h>
70 #include <ext/numeric_traits.h>
71 #include <bits/stl_pair.h>
72 #include <bits/stl_iterator_base_types.h>
73 #include <bits/stl_iterator_base_funcs.h>
74 #include <bits/stl_iterator.h>
75 #include <bits/concept_check.h>
76 #include <debug/debug.h>
77 #include <bits/move.h> // For std::swap and _GLIBCXX_MOVE
79 _GLIBCXX_BEGIN_NAMESPACE(std
)
81 // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
82 // nutshell, we are partially implementing the resolution of DR 187,
83 // when it's safe, i.e., the value_types are equal.
84 template<bool _BoolType
>
87 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
89 iter_swap(_ForwardIterator1 __a
, _ForwardIterator2 __b
)
91 typedef typename iterator_traits
<_ForwardIterator1
>::value_type
93 _ValueType1 __tmp
= _GLIBCXX_MOVE(*__a
);
94 *__a
= _GLIBCXX_MOVE(*__b
);
95 *__b
= _GLIBCXX_MOVE(__tmp
);
100 struct __iter_swap
<true>
102 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
104 iter_swap(_ForwardIterator1 __a
, _ForwardIterator2 __b
)
111 * @brief Swaps the contents of two iterators.
112 * @ingroup mutating_algorithms
113 * @param a An iterator.
114 * @param b Another iterator.
117 * This function swaps the values pointed to by two iterators, not the
118 * iterators themselves.
120 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
122 iter_swap(_ForwardIterator1 __a
, _ForwardIterator2 __b
)
124 typedef typename iterator_traits
<_ForwardIterator1
>::value_type
126 typedef typename iterator_traits
<_ForwardIterator2
>::value_type
129 // concept requirements
130 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
132 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
134 __glibcxx_function_requires(_ConvertibleConcept
<_ValueType1
,
136 __glibcxx_function_requires(_ConvertibleConcept
<_ValueType2
,
139 typedef typename iterator_traits
<_ForwardIterator1
>::reference
141 typedef typename iterator_traits
<_ForwardIterator2
>::reference
143 std::__iter_swap
<__are_same
<_ValueType1
, _ValueType2
>::__value
144 && __are_same
<_ValueType1
&, _ReferenceType1
>::__value
145 && __are_same
<_ValueType2
&, _ReferenceType2
>::__value
>::
150 * @brief Swap the elements of two sequences.
151 * @ingroup mutating_algorithms
152 * @param first1 A forward iterator.
153 * @param last1 A forward iterator.
154 * @param first2 A forward iterator.
155 * @return An iterator equal to @p first2+(last1-first1).
157 * Swaps each element in the range @p [first1,last1) with the
158 * corresponding element in the range @p [first2,(last1-first1)).
159 * The ranges must not overlap.
161 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
163 swap_ranges(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
164 _ForwardIterator2 __first2
)
166 // concept requirements
167 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
169 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
171 __glibcxx_requires_valid_range(__first1
, __last1
);
173 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
174 std::iter_swap(__first1
, __first2
);
179 * @brief This does what you think it does.
180 * @ingroup sorting_algorithms
181 * @param a A thing of arbitrary type.
182 * @param b Another thing of arbitrary type.
183 * @return The lesser of the parameters.
185 * This is the simple classic generic implementation. It will work on
186 * temporary expressions, since they are only evaluated once, unlike a
187 * preprocessor macro.
189 template<typename _Tp
>
191 min(const _Tp
& __a
, const _Tp
& __b
)
193 // concept requirements
194 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
195 //return __b < __a ? __b : __a;
202 * @brief This does what you think it does.
203 * @ingroup sorting_algorithms
204 * @param a A thing of arbitrary type.
205 * @param b Another thing of arbitrary type.
206 * @return The greater of the parameters.
208 * This is the simple classic generic implementation. It will work on
209 * temporary expressions, since they are only evaluated once, unlike a
210 * preprocessor macro.
212 template<typename _Tp
>
214 max(const _Tp
& __a
, const _Tp
& __b
)
216 // concept requirements
217 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
218 //return __a < __b ? __b : __a;
225 * @brief This does what you think it does.
226 * @ingroup sorting_algorithms
227 * @param a A thing of arbitrary type.
228 * @param b Another thing of arbitrary type.
229 * @param comp A @link comparison_functors comparison functor@endlink.
230 * @return The lesser of the parameters.
232 * This will work on temporary expressions, since they are only evaluated
233 * once, unlike a preprocessor macro.
235 template<typename _Tp
, typename _Compare
>
237 min(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
239 //return __comp(__b, __a) ? __b : __a;
240 if (__comp(__b
, __a
))
246 * @brief This does what you think it does.
247 * @ingroup sorting_algorithms
248 * @param a A thing of arbitrary type.
249 * @param b Another thing of arbitrary type.
250 * @param comp A @link comparison_functors comparison functor@endlink.
251 * @return The greater of the parameters.
253 * This will work on temporary expressions, since they are only evaluated
254 * once, unlike a preprocessor macro.
256 template<typename _Tp
, typename _Compare
>
258 max(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
260 //return __comp(__a, __b) ? __b : __a;
261 if (__comp(__a
, __b
))
267 // If _Iterator is a __normal_iterator return its base (a plain pointer,
268 // normally) otherwise return it untouched. See copy, fill, ...
269 template<typename _Iterator
,
270 bool _IsNormal
= __is_normal_iterator
<_Iterator
>::__value
>
278 template<typename _Iterator
>
279 struct __niter_base
<_Iterator
, true>
281 static typename
_Iterator::iterator_type
283 { return __it
.base(); }
286 // Likewise, for move_iterator.
287 template<typename _Iterator
,
288 bool _IsMove
= __is_move_iterator
<_Iterator
>::__value
>
296 template<typename _Iterator
>
297 struct __miter_base
<_Iterator
, true>
299 static typename
_Iterator::iterator_type
301 { return __it
.base(); }
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
>
313 template<typename _II
, typename _OI
>
315 __copy_m(_II __first
, _II __last
, _OI __result
)
317 for (; __first
!= __last
; ++__result
, ++__first
)
318 *__result
= *__first
;
323 #ifdef __GXX_EXPERIMENTAL_CXX0X__
324 template<typename _Category
>
325 struct __copy_move
<true, false, _Category
>
327 template<typename _II
, typename _OI
>
329 __copy_m(_II __first
, _II __last
, _OI __result
)
331 for (; __first
!= __last
; ++__result
, ++__first
)
332 *__result
= std::move(*__first
);
339 struct __copy_move
<false, false, random_access_iterator_tag
>
341 template<typename _II
, typename _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
;
356 #ifdef __GXX_EXPERIMENTAL_CXX0X__
358 struct __copy_move
<true, false, random_access_iterator_tag
>
360 template<typename _II
, typename _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
);
376 template<bool _IsMove
>
377 struct __copy_move
<_IsMove
, true, random_access_iterator_tag
>
379 template<typename _Tp
>
381 __copy_m(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
383 __builtin_memmove(__result
, __first
,
384 sizeof(_Tp
) * (__last
- __first
));
385 return __result
+ (__last
- __first
);
389 template<bool _IsMove
, typename _II
, typename _OI
>
391 __copy_move_a(_II __first
, _II __last
, _OI __result
)
393 typedef typename iterator_traits
<_II
>::value_type _ValueTypeI
;
394 typedef typename iterator_traits
<_OI
>::value_type _ValueTypeO
;
395 typedef typename iterator_traits
<_II
>::iterator_category _Category
;
396 const bool __simple
= (__is_pod(_ValueTypeI
)
397 && __is_pointer
<_II
>::__value
398 && __is_pointer
<_OI
>::__value
399 && __are_same
<_ValueTypeI
, _ValueTypeO
>::__value
);
401 return std::__copy_move
<_IsMove
, __simple
,
402 _Category
>::__copy_m(__first
, __last
, __result
);
405 // Helpers for streambuf iterators (either istream or ostream).
406 // NB: avoid including <iosfwd>, relatively large.
407 template<typename _CharT
>
410 template<typename _CharT
, typename _Traits
>
411 class istreambuf_iterator
;
413 template<typename _CharT
, typename _Traits
>
414 class ostreambuf_iterator
;
416 template<bool _IsMove
, typename _CharT
>
417 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
418 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> > >::__type
419 __copy_move_a2(_CharT
*, _CharT
*,
420 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> >);
422 template<bool _IsMove
, typename _CharT
>
423 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
424 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> > >::__type
425 __copy_move_a2(const _CharT
*, const _CharT
*,
426 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> >);
428 template<bool _IsMove
, typename _CharT
>
429 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
431 __copy_move_a2(istreambuf_iterator
<_CharT
, char_traits
<_CharT
> >,
432 istreambuf_iterator
<_CharT
, char_traits
<_CharT
> >, _CharT
*);
434 template<bool _IsMove
, typename _II
, typename _OI
>
436 __copy_move_a2(_II __first
, _II __last
, _OI __result
)
438 return _OI(std::__copy_move_a
<_IsMove
>
439 (std::__niter_base
<_II
>::__b(__first
),
440 std::__niter_base
<_II
>::__b(__last
),
441 std::__niter_base
<_OI
>::__b(__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
>
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
<_II
>::__b(__first
),
473 std::__miter_base
<_II
>::__b(__last
), __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
>
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>
505 (std::__miter_base
<_II
>::__b(__first
),
506 std::__miter_base
<_II
>::__b(__last
), __result
));
509 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
511 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
514 template<bool, bool, typename
>
515 struct __copy_move_backward
517 template<typename _BI1
, typename _BI2
>
519 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
521 while (__first
!= __last
)
522 *--__result
= *--__last
;
527 #ifdef __GXX_EXPERIMENTAL_CXX0X__
528 template<typename _Category
>
529 struct __copy_move_backward
<true, false, _Category
>
531 template<typename _BI1
, typename _BI2
>
533 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
535 while (__first
!= __last
)
536 *--__result
= std::move(*--__last
);
543 struct __copy_move_backward
<false, false, random_access_iterator_tag
>
545 template<typename _BI1
, typename _BI2
>
547 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
549 typename iterator_traits
<_BI1
>::difference_type __n
;
550 for (__n
= __last
- __first
; __n
> 0; --__n
)
551 *--__result
= *--__last
;
556 #ifdef __GXX_EXPERIMENTAL_CXX0X__
558 struct __copy_move_backward
<true, false, random_access_iterator_tag
>
560 template<typename _BI1
, typename _BI2
>
562 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
564 typename iterator_traits
<_BI1
>::difference_type __n
;
565 for (__n
= __last
- __first
; __n
> 0; --__n
)
566 *--__result
= std::move(*--__last
);
572 template<bool _IsMove
>
573 struct __copy_move_backward
<_IsMove
, true, random_access_iterator_tag
>
575 template<typename _Tp
>
577 __copy_move_b(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
579 const ptrdiff_t _Num
= __last
- __first
;
580 __builtin_memmove(__result
- _Num
, __first
, sizeof(_Tp
) * _Num
);
581 return __result
- _Num
;
585 template<bool _IsMove
, typename _BI1
, typename _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_pod(_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
,
603 template<bool _IsMove
, typename _BI1
, typename _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
<_BI1
>::__b(__first
),
609 std::__niter_base
<_BI1
>::__b(__last
),
610 std::__niter_base
<_BI2
>::__b(__result
)));
614 * @brief Copies the range [first,last) into result.
615 * @ingroup mutating_algorithms
616 * @param first A bidirectional iterator.
617 * @param last A bidirectional iterator.
618 * @param result A bidirectional iterator.
619 * @return result - (first - last)
621 * The function has the same effect as copy, but starts at the end of the
622 * range and works its way to the start, returning the start of the result.
623 * This inline function will boil down to a call to @c memmove whenever
624 * possible. Failing that, if random access iterators are passed, then the
625 * loop count will be known (and therefore a candidate for compiler
626 * optimizations such as unrolling).
628 * Result may not be in the range [first,last). Use copy instead. Note
629 * that the start of the output range may overlap [first,last).
631 template<typename _BI1
, typename _BI2
>
633 copy_backward(_BI1 __first
, _BI1 __last
, _BI2 __result
)
635 // concept requirements
636 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BI1
>)
637 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<_BI2
>)
638 __glibcxx_function_requires(_ConvertibleConcept
<
639 typename iterator_traits
<_BI1
>::value_type
,
640 typename iterator_traits
<_BI2
>::value_type
>)
641 __glibcxx_requires_valid_range(__first
, __last
);
643 return (std::__copy_move_backward_a2
<__is_move_iterator
<_BI1
>::__value
>
644 (std::__miter_base
<_BI1
>::__b(__first
),
645 std::__miter_base
<_BI1
>::__b(__last
), __result
));
648 #ifdef __GXX_EXPERIMENTAL_CXX0X__
650 * @brief Moves the range [first,last) into result.
651 * @ingroup mutating_algorithms
652 * @param first A bidirectional iterator.
653 * @param last A bidirectional iterator.
654 * @param result A bidirectional iterator.
655 * @return result - (first - last)
657 * The function has the same effect as move, but starts at the end of the
658 * range and works its way to the start, returning the start of the result.
659 * This inline function will boil down to a call to @c memmove whenever
660 * possible. Failing that, if random access iterators are passed, then the
661 * loop count will be known (and therefore a candidate for compiler
662 * optimizations such as unrolling).
664 * Result may not be in the range [first,last). Use move instead. Note
665 * that the start of the output range may overlap [first,last).
667 template<typename _BI1
, typename _BI2
>
669 move_backward(_BI1 __first
, _BI1 __last
, _BI2 __result
)
671 // concept requirements
672 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BI1
>)
673 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<_BI2
>)
674 __glibcxx_function_requires(_ConvertibleConcept
<
675 typename iterator_traits
<_BI1
>::value_type
,
676 typename iterator_traits
<_BI2
>::value_type
>)
677 __glibcxx_requires_valid_range(__first
, __last
);
679 return (std::__copy_move_backward_a2
<true>
680 (std::__miter_base
<_BI1
>::__b(__first
),
681 std::__miter_base
<_BI1
>::__b(__last
), __result
));
684 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
686 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
689 template<typename _ForwardIterator
, typename _Tp
>
691 __gnu_cxx::__enable_if
<!__is_scalar
<_Tp
>::__value
, void>::__type
692 __fill_a(_ForwardIterator __first
, _ForwardIterator __last
,
695 for (; __first
!= __last
; ++__first
)
699 template<typename _ForwardIterator
, typename _Tp
>
701 __gnu_cxx::__enable_if
<__is_scalar
<_Tp
>::__value
, void>::__type
702 __fill_a(_ForwardIterator __first
, _ForwardIterator __last
,
705 const _Tp __tmp
= __value
;
706 for (; __first
!= __last
; ++__first
)
710 // Specialization: for char types we can use memset.
711 template<typename _Tp
>
713 __gnu_cxx::__enable_if
<__is_byte
<_Tp
>::__value
, void>::__type
714 __fill_a(_Tp
* __first
, _Tp
* __last
, const _Tp
& __c
)
716 const _Tp __tmp
= __c
;
717 __builtin_memset(__first
, static_cast<unsigned char>(__tmp
),
722 * @brief Fills the range [first,last) with copies of value.
723 * @ingroup mutating_algorithms
724 * @param first A forward iterator.
725 * @param last A forward iterator.
726 * @param value A reference-to-const of arbitrary type.
729 * This function fills a range with copies of the same value. For char
730 * types filling contiguous areas of memory, this becomes an inline call
731 * to @c memset or @c wmemset.
733 template<typename _ForwardIterator
, typename _Tp
>
735 fill(_ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __value
)
737 // concept requirements
738 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
740 __glibcxx_requires_valid_range(__first
, __last
);
742 std::__fill_a(std::__niter_base
<_ForwardIterator
>::__b(__first
),
743 std::__niter_base
<_ForwardIterator
>::__b(__last
), __value
);
746 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
748 __gnu_cxx::__enable_if
<!__is_scalar
<_Tp
>::__value
, _OutputIterator
>::__type
749 __fill_n_a(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
751 for (; __n
> 0; --__n
, ++__first
)
756 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
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 (; __n
> 0; --__n
, ++__first
)
767 template<typename _Size
, typename _Tp
>
769 __gnu_cxx::__enable_if
<__is_byte
<_Tp
>::__value
, _Tp
*>::__type
770 __fill_n_a(_Tp
* __first
, _Size __n
, const _Tp
& __c
)
772 std::__fill_a(__first
, __first
+ __n
, __c
);
773 return __first
+ __n
;
777 * @brief Fills the range [first,first+n) with copies of value.
778 * @ingroup mutating_algorithms
779 * @param first An output iterator.
780 * @param n The count of copies to perform.
781 * @param value A reference-to-const of arbitrary type.
782 * @return The iterator at first+n.
784 * This function fills a range with copies of the same value. For char
785 * types filling contiguous areas of memory, this becomes an inline call
786 * to @c memset or @ wmemset.
788 template<typename _OI
, typename _Size
, typename _Tp
>
790 fill_n(_OI __first
, _Size __n
, const _Tp
& __value
)
792 // concept requirements
793 __glibcxx_function_requires(_OutputIteratorConcept
<_OI
, _Tp
>)
795 return _OI(std::__fill_n_a(std::__niter_base
<_OI
>::__b(__first
),
799 template<bool _BoolType
>
802 template<typename _II1
, typename _II2
>
804 equal(_II1 __first1
, _II1 __last1
, _II2 __first2
)
806 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
807 if (!(*__first1
== *__first2
))
816 template<typename _Tp
>
818 equal(const _Tp
* __first1
, const _Tp
* __last1
, const _Tp
* __first2
)
820 return !__builtin_memcmp(__first1
, __first2
, sizeof(_Tp
)
821 * (__last1
- __first1
));
825 template<typename _II1
, typename _II2
>
827 __equal_aux(_II1 __first1
, _II1 __last1
, _II2 __first2
)
829 typedef typename iterator_traits
<_II1
>::value_type _ValueType1
;
830 typedef typename iterator_traits
<_II2
>::value_type _ValueType2
;
831 const bool __simple
= (__is_integer
<_ValueType1
>::__value
832 && __is_pointer
<_II1
>::__value
833 && __is_pointer
<_II2
>::__value
834 && __are_same
<_ValueType1
, _ValueType2
>::__value
);
836 return std::__equal
<__simple
>::equal(__first1
, __last1
, __first2
);
840 template<typename
, typename
>
843 template<typename _II1
, typename _II2
>
845 __newlast1(_II1
, _II1 __last1
, _II2
, _II2
)
848 template<typename _II
>
850 __cnd2(_II __first
, _II __last
)
851 { return __first
!= __last
; }
855 struct __lc_rai
<random_access_iterator_tag
, random_access_iterator_tag
>
857 template<typename _RAI1
, typename _RAI2
>
859 __newlast1(_RAI1 __first1
, _RAI1 __last1
,
860 _RAI2 __first2
, _RAI2 __last2
)
862 const typename iterator_traits
<_RAI1
>::difference_type
863 __diff1
= __last1
- __first1
;
864 const typename iterator_traits
<_RAI2
>::difference_type
865 __diff2
= __last2
- __first2
;
866 return __diff2
< __diff1
? __first1
+ __diff2
: __last1
;
869 template<typename _RAI
>
875 template<bool _BoolType
>
876 struct __lexicographical_compare
878 template<typename _II1
, typename _II2
>
879 static bool __lc(_II1
, _II1
, _II2
, _II2
);
882 template<bool _BoolType
>
883 template<typename _II1
, typename _II2
>
885 __lexicographical_compare
<_BoolType
>::
886 __lc(_II1 __first1
, _II1 __last1
, _II2 __first2
, _II2 __last2
)
888 typedef typename iterator_traits
<_II1
>::iterator_category _Category1
;
889 typedef typename iterator_traits
<_II2
>::iterator_category _Category2
;
890 typedef std::__lc_rai
<_Category1
, _Category2
> __rai_type
;
892 __last1
= __rai_type::__newlast1(__first1
, __last1
,
894 for (; __first1
!= __last1
&& __rai_type::__cnd2(__first2
, __last2
);
895 ++__first1
, ++__first2
)
897 if (*__first1
< *__first2
)
899 if (*__first2
< *__first1
)
902 return __first1
== __last1
&& __first2
!= __last2
;
906 struct __lexicographical_compare
<true>
908 template<typename _Tp
, typename _Up
>
910 __lc(const _Tp
* __first1
, const _Tp
* __last1
,
911 const _Up
* __first2
, const _Up
* __last2
)
913 const size_t __len1
= __last1
- __first1
;
914 const size_t __len2
= __last2
- __first2
;
915 const int __result
= __builtin_memcmp(__first1
, __first2
,
916 std::min(__len1
, __len2
));
917 return __result
!= 0 ? __result
< 0 : __len1
< __len2
;
921 template<typename _II1
, typename _II2
>
923 __lexicographical_compare_aux(_II1 __first1
, _II1 __last1
,
924 _II2 __first2
, _II2 __last2
)
926 typedef typename iterator_traits
<_II1
>::value_type _ValueType1
;
927 typedef typename iterator_traits
<_II2
>::value_type _ValueType2
;
928 const bool __simple
=
929 (__is_byte
<_ValueType1
>::__value
&& __is_byte
<_ValueType2
>::__value
930 && !__gnu_cxx::__numeric_traits
<_ValueType1
>::__is_signed
931 && !__gnu_cxx::__numeric_traits
<_ValueType2
>::__is_signed
932 && __is_pointer
<_II1
>::__value
933 && __is_pointer
<_II2
>::__value
);
935 return std::__lexicographical_compare
<__simple
>::__lc(__first1
, __last1
,
939 _GLIBCXX_END_NAMESPACE
941 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_P
)
944 * @brief Tests a range for element-wise equality.
945 * @ingroup non_mutating_algorithms
946 * @param first1 An input iterator.
947 * @param last1 An input iterator.
948 * @param first2 An input iterator.
949 * @return A boolean true or false.
951 * This compares the elements of two ranges using @c == and returns true or
952 * false depending on whether all of the corresponding elements of the
955 template<typename _II1
, typename _II2
>
957 equal(_II1 __first1
, _II1 __last1
, _II2 __first2
)
959 // concept requirements
960 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
961 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
962 __glibcxx_function_requires(_EqualOpConcept
<
963 typename iterator_traits
<_II1
>::value_type
,
964 typename iterator_traits
<_II2
>::value_type
>)
965 __glibcxx_requires_valid_range(__first1
, __last1
);
967 return std::__equal_aux(std::__niter_base
<_II1
>::__b(__first1
),
968 std::__niter_base
<_II1
>::__b(__last1
),
969 std::__niter_base
<_II2
>::__b(__first2
));
973 * @brief Tests a range for element-wise equality.
974 * @ingroup non_mutating_algorithms
975 * @param first1 An input iterator.
976 * @param last1 An input iterator.
977 * @param first2 An input iterator.
978 * @param binary_pred A binary predicate @link functors
980 * @return A boolean true or false.
982 * This compares the elements of two ranges using the binary_pred
983 * parameter, and returns true or
984 * false depending on whether all of the corresponding elements of the
987 template<typename _IIter1
, typename _IIter2
, typename _BinaryPredicate
>
989 equal(_IIter1 __first1
, _IIter1 __last1
,
990 _IIter2 __first2
, _BinaryPredicate __binary_pred
)
992 // concept requirements
993 __glibcxx_function_requires(_InputIteratorConcept
<_IIter1
>)
994 __glibcxx_function_requires(_InputIteratorConcept
<_IIter2
>)
995 __glibcxx_requires_valid_range(__first1
, __last1
);
997 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
998 if (!bool(__binary_pred(*__first1
, *__first2
)))
1004 * @brief Performs "dictionary" comparison on ranges.
1005 * @ingroup sorting_algorithms
1006 * @param first1 An input iterator.
1007 * @param last1 An input iterator.
1008 * @param first2 An input iterator.
1009 * @param last2 An input iterator.
1010 * @return A boolean true or false.
1012 * "Returns true if the sequence of elements defined by the range
1013 * [first1,last1) is lexicographically less than the sequence of elements
1014 * defined by the range [first2,last2). Returns false otherwise."
1015 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
1016 * then this is an inline call to @c memcmp.
1018 template<typename _II1
, typename _II2
>
1020 lexicographical_compare(_II1 __first1
, _II1 __last1
,
1021 _II2 __first2
, _II2 __last2
)
1023 // concept requirements
1024 typedef typename iterator_traits
<_II1
>::value_type _ValueType1
;
1025 typedef typename iterator_traits
<_II2
>::value_type _ValueType2
;
1026 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
1027 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
1028 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
1029 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
1030 __glibcxx_requires_valid_range(__first1
, __last1
);
1031 __glibcxx_requires_valid_range(__first2
, __last2
);
1033 return std::__lexicographical_compare_aux
1034 (std::__niter_base
<_II1
>::__b(__first1
),
1035 std::__niter_base
<_II1
>::__b(__last1
),
1036 std::__niter_base
<_II2
>::__b(__first2
),
1037 std::__niter_base
<_II2
>::__b(__last2
));
1041 * @brief Performs "dictionary" comparison on ranges.
1042 * @ingroup sorting_algorithms
1043 * @param first1 An input iterator.
1044 * @param last1 An input iterator.
1045 * @param first2 An input iterator.
1046 * @param last2 An input iterator.
1047 * @param comp A @link comparison_functors comparison functor@endlink.
1048 * @return A boolean true or false.
1050 * The same as the four-parameter @c lexicographical_compare, but uses the
1051 * comp parameter instead of @c <.
1053 template<typename _II1
, typename _II2
, typename _Compare
>
1055 lexicographical_compare(_II1 __first1
, _II1 __last1
,
1056 _II2 __first2
, _II2 __last2
, _Compare __comp
)
1058 typedef typename iterator_traits
<_II1
>::iterator_category _Category1
;
1059 typedef typename iterator_traits
<_II2
>::iterator_category _Category2
;
1060 typedef std::__lc_rai
<_Category1
, _Category2
> __rai_type
;
1062 // concept requirements
1063 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
1064 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
1065 __glibcxx_requires_valid_range(__first1
, __last1
);
1066 __glibcxx_requires_valid_range(__first2
, __last2
);
1068 __last1
= __rai_type::__newlast1(__first1
, __last1
, __first2
, __last2
);
1069 for (; __first1
!= __last1
&& __rai_type::__cnd2(__first2
, __last2
);
1070 ++__first1
, ++__first2
)
1072 if (__comp(*__first1
, *__first2
))
1074 if (__comp(*__first2
, *__first1
))
1077 return __first1
== __last1
&& __first2
!= __last2
;
1081 * @brief Finds the places in ranges which don't match.
1082 * @ingroup non_mutating_algorithms
1083 * @param first1 An input iterator.
1084 * @param last1 An input iterator.
1085 * @param first2 An input iterator.
1086 * @return A pair of iterators pointing to the first mismatch.
1088 * This compares the elements of two ranges using @c == and returns a pair
1089 * of iterators. The first iterator points into the first range, the
1090 * second iterator points into the second range, and the elements pointed
1091 * to by the iterators are not equal.
1093 template<typename _InputIterator1
, typename _InputIterator2
>
1094 pair
<_InputIterator1
, _InputIterator2
>
1095 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1096 _InputIterator2 __first2
)
1098 // concept requirements
1099 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
1100 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
1101 __glibcxx_function_requires(_EqualOpConcept
<
1102 typename iterator_traits
<_InputIterator1
>::value_type
,
1103 typename iterator_traits
<_InputIterator2
>::value_type
>)
1104 __glibcxx_requires_valid_range(__first1
, __last1
);
1106 while (__first1
!= __last1
&& *__first1
== *__first2
)
1111 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
1115 * @brief Finds the places in ranges which don't match.
1116 * @ingroup non_mutating_algorithms
1117 * @param first1 An input iterator.
1118 * @param last1 An input iterator.
1119 * @param first2 An input iterator.
1120 * @param binary_pred A binary predicate @link functors
1122 * @return A pair of iterators pointing to the first mismatch.
1124 * This compares the elements of two ranges using the binary_pred
1125 * parameter, and returns a pair
1126 * of iterators. The first iterator points into the first range, the
1127 * second iterator points into the second range, and the elements pointed
1128 * to by the iterators are not equal.
1130 template<typename _InputIterator1
, typename _InputIterator2
,
1131 typename _BinaryPredicate
>
1132 pair
<_InputIterator1
, _InputIterator2
>
1133 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1134 _InputIterator2 __first2
, _BinaryPredicate __binary_pred
)
1136 // concept requirements
1137 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
1138 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
1139 __glibcxx_requires_valid_range(__first1
, __last1
);
1141 while (__first1
!= __last1
&& bool(__binary_pred(*__first1
, *__first2
)))
1146 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
1149 _GLIBCXX_END_NESTED_NAMESPACE
1151 // NB: This file is included within many other C++ includes, as a way
1152 // of getting the base algorithms. So, make sure that parallel bits
1153 // come in too if requested.
1154 #ifdef _GLIBCXX_PARALLEL
1155 # include <parallel/algobase.h>