1 // Core algorithmic facilities -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
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/stl_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 * @param a An iterator.
113 * @param b Another iterator.
116 * This function swaps the values pointed to by two iterators, not the
117 * iterators themselves.
119 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
121 iter_swap(_ForwardIterator1 __a
, _ForwardIterator2 __b
)
123 typedef typename iterator_traits
<_ForwardIterator1
>::value_type
125 typedef typename iterator_traits
<_ForwardIterator2
>::value_type
128 // concept requirements
129 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
131 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
133 __glibcxx_function_requires(_ConvertibleConcept
<_ValueType1
,
135 __glibcxx_function_requires(_ConvertibleConcept
<_ValueType2
,
138 typedef typename iterator_traits
<_ForwardIterator1
>::reference
140 typedef typename iterator_traits
<_ForwardIterator2
>::reference
142 std::__iter_swap
<__are_same
<_ValueType1
, _ValueType2
>::__value
143 && __are_same
<_ValueType1
&, _ReferenceType1
>::__value
144 && __are_same
<_ValueType2
&, _ReferenceType2
>::__value
>::
149 * @brief Swap the elements of two sequences.
150 * @param first1 A forward iterator.
151 * @param last1 A forward iterator.
152 * @param first2 A forward iterator.
153 * @return An iterator equal to @p first2+(last1-first1).
155 * Swaps each element in the range @p [first1,last1) with the
156 * corresponding element in the range @p [first2,(last1-first1)).
157 * The ranges must not overlap.
159 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
161 swap_ranges(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
162 _ForwardIterator2 __first2
)
164 // concept requirements
165 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
167 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
169 __glibcxx_requires_valid_range(__first1
, __last1
);
171 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
172 std::iter_swap(__first1
, __first2
);
177 * @brief This does what you think it does.
178 * @param a A thing of arbitrary type.
179 * @param b Another thing of arbitrary type.
180 * @return The lesser of the parameters.
182 * This is the simple classic generic implementation. It will work on
183 * temporary expressions, since they are only evaluated once, unlike a
184 * preprocessor macro.
186 template<typename _Tp
>
188 min(const _Tp
& __a
, const _Tp
& __b
)
190 // concept requirements
191 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
192 //return __b < __a ? __b : __a;
199 * @brief This does what you think it does.
200 * @param a A thing of arbitrary type.
201 * @param b Another thing of arbitrary type.
202 * @return The greater of the parameters.
204 * This is the simple classic generic implementation. It will work on
205 * temporary expressions, since they are only evaluated once, unlike a
206 * preprocessor macro.
208 template<typename _Tp
>
210 max(const _Tp
& __a
, const _Tp
& __b
)
212 // concept requirements
213 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
214 //return __a < __b ? __b : __a;
221 * @brief This does what you think it does.
222 * @param a A thing of arbitrary type.
223 * @param b Another thing of arbitrary type.
224 * @param comp A @link s20_3_3_comparisons 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 * @param a A thing of arbitrary type.
243 * @param b Another thing of arbitrary type.
244 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
245 * @return The greater of the parameters.
247 * This will work on temporary expressions, since they are only evaluated
248 * once, unlike a preprocessor macro.
250 template<typename _Tp
, typename _Compare
>
252 max(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
254 //return __comp(__a, __b) ? __b : __a;
255 if (__comp(__a
, __b
))
261 // If _Iterator is a __normal_iterator return its base (a plain pointer,
262 // normally) otherwise return it untouched. See copy, fill, ...
263 template<typename _Iterator
,
264 bool _IsNormal
= __is_normal_iterator
<_Iterator
>::__value
>
272 template<typename _Iterator
>
273 struct __niter_base
<_Iterator
, true>
275 static typename
_Iterator::iterator_type
277 { return __it
.base(); }
280 // Likewise, for move_iterator.
281 template<typename _Iterator
,
282 bool _IsMove
= __is_move_iterator
<_Iterator
>::__value
>
290 template<typename _Iterator
>
291 struct __miter_base
<_Iterator
, true>
293 static typename
_Iterator::iterator_type
295 { return __it
.base(); }
298 // All of these auxiliary structs serve two purposes. (1) Replace
299 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
300 // because the input and output ranges are permitted to overlap.)
301 // (2) If we're using random access iterators, then write the loop as
302 // a for loop with an explicit count.
304 template<bool, bool, typename
>
307 template<typename _II
, typename _OI
>
309 __copy_m(_II __first
, _II __last
, _OI __result
)
311 for (; __first
!= __last
; ++__result
, ++__first
)
312 *__result
= *__first
;
317 #ifdef __GXX_EXPERIMENTAL_CXX0X__
318 template<typename _Category
>
319 struct __copy_move
<true, false, _Category
>
321 template<typename _II
, typename _OI
>
323 __copy_m(_II __first
, _II __last
, _OI __result
)
325 for (; __first
!= __last
; ++__result
, ++__first
)
326 *__result
= std::move(*__first
);
333 struct __copy_move
<false, false, random_access_iterator_tag
>
335 template<typename _II
, typename _OI
>
337 __copy_m(_II __first
, _II __last
, _OI __result
)
339 typedef typename iterator_traits
<_II
>::difference_type _Distance
;
340 for(_Distance __n
= __last
- __first
; __n
> 0; --__n
)
342 *__result
= *__first
;
350 #ifdef __GXX_EXPERIMENTAL_CXX0X__
352 struct __copy_move
<true, false, random_access_iterator_tag
>
354 template<typename _II
, typename _OI
>
356 __copy_m(_II __first
, _II __last
, _OI __result
)
358 typedef typename iterator_traits
<_II
>::difference_type _Distance
;
359 for(_Distance __n
= __last
- __first
; __n
> 0; --__n
)
361 *__result
= std::move(*__first
);
370 template<bool _IsMove
>
371 struct __copy_move
<_IsMove
, true, random_access_iterator_tag
>
373 template<typename _Tp
>
375 __copy_m(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
377 __builtin_memmove(__result
, __first
,
378 sizeof(_Tp
) * (__last
- __first
));
379 return __result
+ (__last
- __first
);
383 template<bool _IsMove
, typename _II
, typename _OI
>
385 __copy_move_a(_II __first
, _II __last
, _OI __result
)
387 typedef typename iterator_traits
<_II
>::value_type _ValueTypeI
;
388 typedef typename iterator_traits
<_OI
>::value_type _ValueTypeO
;
389 typedef typename iterator_traits
<_II
>::iterator_category _Category
;
390 const bool __simple
= (__is_pod(_ValueTypeI
)
391 && __is_pointer
<_II
>::__value
392 && __is_pointer
<_OI
>::__value
393 && __are_same
<_ValueTypeI
, _ValueTypeO
>::__value
);
395 return std::__copy_move
<_IsMove
, __simple
,
396 _Category
>::__copy_m(__first
, __last
, __result
);
399 // Helpers for streambuf iterators (either istream or ostream).
400 // NB: avoid including <iosfwd>, relatively large.
401 template<typename _CharT
>
404 template<typename _CharT
, typename _Traits
>
405 class istreambuf_iterator
;
407 template<typename _CharT
, typename _Traits
>
408 class ostreambuf_iterator
;
410 template<bool _IsMove
, typename _CharT
>
411 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
412 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> > >::__type
413 __copy_move_a2(_CharT
*, _CharT
*,
414 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> >);
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(const _CharT
*, const _CharT
*,
420 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> >);
422 template<bool _IsMove
, typename _CharT
>
423 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
425 __copy_move_a2(istreambuf_iterator
<_CharT
, char_traits
<_CharT
> >,
426 istreambuf_iterator
<_CharT
, char_traits
<_CharT
> >, _CharT
*);
428 template<bool _IsMove
, typename _II
, typename _OI
>
430 __copy_move_a2(_II __first
, _II __last
, _OI __result
)
432 return _OI(std::__copy_move_a
<_IsMove
>
433 (std::__niter_base
<_II
>::__b(__first
),
434 std::__niter_base
<_II
>::__b(__last
),
435 std::__niter_base
<_OI
>::__b(__result
)));
439 * @brief Copies the range [first,last) into result.
440 * @param first An input iterator.
441 * @param last An input iterator.
442 * @param result An output iterator.
443 * @return result + (first - last)
445 * This inline function will boil down to a call to @c memmove whenever
446 * possible. Failing that, if random access iterators are passed, then the
447 * loop count will be known (and therefore a candidate for compiler
448 * optimizations such as unrolling). Result may not be contained within
449 * [first,last); the copy_backward function should be used instead.
451 * Note that the end of the output range is permitted to be contained
452 * within [first,last).
454 template<typename _II
, typename _OI
>
456 copy(_II __first
, _II __last
, _OI __result
)
458 // concept requirements
459 __glibcxx_function_requires(_InputIteratorConcept
<_II
>)
460 __glibcxx_function_requires(_OutputIteratorConcept
<_OI
,
461 typename iterator_traits
<_II
>::value_type
>)
462 __glibcxx_requires_valid_range(__first
, __last
);
464 return (std::__copy_move_a2
<__is_move_iterator
<_II
>::__value
>
465 (std::__miter_base
<_II
>::__b(__first
),
466 std::__miter_base
<_II
>::__b(__last
), __result
));
469 #ifdef __GXX_EXPERIMENTAL_CXX0X__
471 * @brief Moves the range [first,last) into result.
472 * @param first An input iterator.
473 * @param last An input iterator.
474 * @param result An output iterator.
475 * @return result + (first - last)
477 * This inline function will boil down to a call to @c memmove whenever
478 * possible. Failing that, if random access iterators are passed, then the
479 * loop count will be known (and therefore a candidate for compiler
480 * optimizations such as unrolling). Result may not be contained within
481 * [first,last); the move_backward function should be used instead.
483 * Note that the end of the output range is permitted to be contained
484 * within [first,last).
486 template<typename _II
, typename _OI
>
488 move(_II __first
, _II __last
, _OI __result
)
490 // concept requirements
491 __glibcxx_function_requires(_InputIteratorConcept
<_II
>)
492 __glibcxx_function_requires(_OutputIteratorConcept
<_OI
,
493 typename iterator_traits
<_II
>::value_type
>)
494 __glibcxx_requires_valid_range(__first
, __last
);
496 return (std::__copy_move_a2
<true>
497 (std::__miter_base
<_II
>::__b(__first
),
498 std::__miter_base
<_II
>::__b(__last
), __result
));
501 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
503 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
506 template<bool, bool, typename
>
507 struct __copy_move_backward
509 template<typename _BI1
, typename _BI2
>
511 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
513 while (__first
!= __last
)
514 *--__result
= *--__last
;
519 #ifdef __GXX_EXPERIMENTAL_CXX0X__
520 template<typename _Category
>
521 struct __copy_move_backward
<true, false, _Category
>
523 template<typename _BI1
, typename _BI2
>
525 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
527 while (__first
!= __last
)
528 *--__result
= std::move(*--__last
);
535 struct __copy_move_backward
<false, false, random_access_iterator_tag
>
537 template<typename _BI1
, typename _BI2
>
539 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
541 typename iterator_traits
<_BI1
>::difference_type __n
;
542 for (__n
= __last
- __first
; __n
> 0; --__n
)
543 *--__result
= *--__last
;
548 #ifdef __GXX_EXPERIMENTAL_CXX0X__
550 struct __copy_move_backward
<true, false, random_access_iterator_tag
>
552 template<typename _BI1
, typename _BI2
>
554 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
556 typename iterator_traits
<_BI1
>::difference_type __n
;
557 for (__n
= __last
- __first
; __n
> 0; --__n
)
558 *--__result
= std::move(*--__last
);
564 template<bool _IsMove
>
565 struct __copy_move_backward
<_IsMove
, true, random_access_iterator_tag
>
567 template<typename _Tp
>
569 __copy_move_b(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
571 const ptrdiff_t _Num
= __last
- __first
;
572 __builtin_memmove(__result
- _Num
, __first
, sizeof(_Tp
) * _Num
);
573 return __result
- _Num
;
577 template<bool _IsMove
, typename _BI1
, typename _BI2
>
579 __copy_move_backward_a(_BI1 __first
, _BI1 __last
, _BI2 __result
)
581 typedef typename iterator_traits
<_BI1
>::value_type _ValueType1
;
582 typedef typename iterator_traits
<_BI2
>::value_type _ValueType2
;
583 typedef typename iterator_traits
<_BI1
>::iterator_category _Category
;
584 const bool __simple
= (__is_pod(_ValueType1
)
585 && __is_pointer
<_BI1
>::__value
586 && __is_pointer
<_BI2
>::__value
587 && __are_same
<_ValueType1
, _ValueType2
>::__value
);
589 return std::__copy_move_backward
<_IsMove
, __simple
,
590 _Category
>::__copy_move_b(__first
,
595 template<bool _IsMove
, typename _BI1
, typename _BI2
>
597 __copy_move_backward_a2(_BI1 __first
, _BI1 __last
, _BI2 __result
)
599 return _BI2(std::__copy_move_backward_a
<_IsMove
>
600 (std::__niter_base
<_BI1
>::__b(__first
),
601 std::__niter_base
<_BI1
>::__b(__last
),
602 std::__niter_base
<_BI2
>::__b(__result
)));
606 * @brief Copies the range [first,last) into result.
607 * @param first A bidirectional iterator.
608 * @param last A bidirectional iterator.
609 * @param result A bidirectional iterator.
610 * @return result - (first - last)
612 * The function has the same effect as copy, but starts at the end of the
613 * range and works its way to the start, returning the start of the result.
614 * This inline function will boil down to a call to @c memmove whenever
615 * possible. Failing that, if random access iterators are passed, then the
616 * loop count will be known (and therefore a candidate for compiler
617 * optimizations such as unrolling).
619 * Result may not be in the range [first,last). Use copy instead. Note
620 * that the start of the output range may overlap [first,last).
622 template<typename _BI1
, typename _BI2
>
624 copy_backward(_BI1 __first
, _BI1 __last
, _BI2 __result
)
626 // concept requirements
627 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BI1
>)
628 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<_BI2
>)
629 __glibcxx_function_requires(_ConvertibleConcept
<
630 typename iterator_traits
<_BI1
>::value_type
,
631 typename iterator_traits
<_BI2
>::value_type
>)
632 __glibcxx_requires_valid_range(__first
, __last
);
634 return (std::__copy_move_backward_a2
<__is_move_iterator
<_BI1
>::__value
>
635 (std::__miter_base
<_BI1
>::__b(__first
),
636 std::__miter_base
<_BI1
>::__b(__last
), __result
));
639 #ifdef __GXX_EXPERIMENTAL_CXX0X__
641 * @brief Moves the range [first,last) into result.
642 * @param first A bidirectional iterator.
643 * @param last A bidirectional iterator.
644 * @param result A bidirectional iterator.
645 * @return result - (first - last)
647 * The function has the same effect as move, but starts at the end of the
648 * range and works its way to the start, returning the start of the result.
649 * This inline function will boil down to a call to @c memmove whenever
650 * possible. Failing that, if random access iterators are passed, then the
651 * loop count will be known (and therefore a candidate for compiler
652 * optimizations such as unrolling).
654 * Result may not be in the range [first,last). Use move instead. Note
655 * that the start of the output range may overlap [first,last).
657 template<typename _BI1
, typename _BI2
>
659 move_backward(_BI1 __first
, _BI1 __last
, _BI2 __result
)
661 // concept requirements
662 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BI1
>)
663 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<_BI2
>)
664 __glibcxx_function_requires(_ConvertibleConcept
<
665 typename iterator_traits
<_BI1
>::value_type
,
666 typename iterator_traits
<_BI2
>::value_type
>)
667 __glibcxx_requires_valid_range(__first
, __last
);
669 return (std::__copy_move_backward_a2
<true>
670 (std::__miter_base
<_BI1
>::__b(__first
),
671 std::__miter_base
<_BI1
>::__b(__last
), __result
));
674 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
676 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
679 template<typename _ForwardIterator
, typename _Tp
>
681 __gnu_cxx::__enable_if
<!__is_scalar
<_Tp
>::__value
, void>::__type
682 __fill_a(_ForwardIterator __first
, _ForwardIterator __last
,
685 for (; __first
!= __last
; ++__first
)
689 template<typename _ForwardIterator
, typename _Tp
>
691 __gnu_cxx::__enable_if
<__is_scalar
<_Tp
>::__value
, void>::__type
692 __fill_a(_ForwardIterator __first
, _ForwardIterator __last
, _Tp __value
)
694 for (; __first
!= __last
; ++__first
)
698 // Specialization: for char types we can use memset.
699 template<typename _Tp
>
701 __gnu_cxx::__enable_if
<__is_byte
<_Tp
>::__value
, void>::__type
702 __fill_a(_Tp
* __first
, _Tp
* __last
, _Tp __c
)
703 { __builtin_memset(__first
, static_cast<unsigned char>(__c
),
707 * @brief Fills the range [first,last) with copies of value.
708 * @param first A forward iterator.
709 * @param last A forward iterator.
710 * @param value A reference-to-const of arbitrary type.
713 * This function fills a range with copies of the same value. For char
714 * types filling contiguous areas of memory, this becomes an inline call
715 * to @c memset or @c wmemset.
717 template<typename _ForwardIterator
, typename _Tp
>
719 fill(_ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __value
)
721 // concept requirements
722 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
724 __glibcxx_requires_valid_range(__first
, __last
);
726 std::__fill_a(std::__niter_base
<_ForwardIterator
>::__b(__first
),
727 std::__niter_base
<_ForwardIterator
>::__b(__last
), __value
);
730 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
732 __gnu_cxx::__enable_if
<!__is_scalar
<_Tp
>::__value
, _OutputIterator
>::__type
733 __fill_n_a(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
735 for (; __n
> 0; --__n
, ++__first
)
740 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
742 __gnu_cxx::__enable_if
<__is_scalar
<_Tp
>::__value
, _OutputIterator
>::__type
743 __fill_n_a(_OutputIterator __first
, _Size __n
, _Tp __value
)
745 for (; __n
> 0; --__n
, ++__first
)
750 template<typename _Size
, typename _Tp
>
752 __gnu_cxx::__enable_if
<__is_byte
<_Tp
>::__value
, _Tp
*>::__type
753 __fill_n_a(_Tp
* __first
, _Size __n
, _Tp __c
)
755 std::__fill_a(__first
, __first
+ __n
, __c
);
756 return __first
+ __n
;
760 * @brief Fills the range [first,first+n) with copies of value.
761 * @param first An output iterator.
762 * @param n The count of copies to perform.
763 * @param value A reference-to-const of arbitrary type.
764 * @return The iterator at first+n.
766 * This function fills a range with copies of the same value. For char
767 * types filling contiguous areas of memory, this becomes an inline call
768 * to @c memset or @ wmemset.
770 template<typename _OI
, typename _Size
, typename _Tp
>
772 fill_n(_OI __first
, _Size __n
, const _Tp
& __value
)
774 // concept requirements
775 __glibcxx_function_requires(_OutputIteratorConcept
<_OI
, _Tp
>)
777 return _OI(std::__fill_n_a(std::__niter_base
<_OI
>::__b(__first
),
781 template<bool _BoolType
>
784 template<typename _II1
, typename _II2
>
786 equal(_II1 __first1
, _II1 __last1
, _II2 __first2
)
788 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
789 if (!(*__first1
== *__first2
))
798 template<typename _Tp
>
800 equal(const _Tp
* __first1
, const _Tp
* __last1
, const _Tp
* __first2
)
802 return !__builtin_memcmp(__first1
, __first2
, sizeof(_Tp
)
803 * (__last1
- __first1
));
807 template<typename _II1
, typename _II2
>
809 __equal_aux(_II1 __first1
, _II1 __last1
, _II2 __first2
)
811 typedef typename iterator_traits
<_II1
>::value_type _ValueType1
;
812 typedef typename iterator_traits
<_II2
>::value_type _ValueType2
;
813 const bool __simple
= (__is_integer
<_ValueType1
>::__value
814 && __is_pointer
<_II1
>::__value
815 && __is_pointer
<_II2
>::__value
816 && __are_same
<_ValueType1
, _ValueType2
>::__value
);
818 return std::__equal
<__simple
>::equal(__first1
, __last1
, __first2
);
822 template<typename
, typename
>
825 template<typename _II1
, typename _II2
>
827 __newlast1(_II1
, _II1 __last1
, _II2
, _II2
)
830 template<typename _II
>
832 __cnd2(_II __first
, _II __last
)
833 { return __first
!= __last
; }
837 struct __lc_rai
<random_access_iterator_tag
, random_access_iterator_tag
>
839 template<typename _RAI1
, typename _RAI2
>
841 __newlast1(_RAI1 __first1
, _RAI1 __last1
,
842 _RAI2 __first2
, _RAI2 __last2
)
844 const typename iterator_traits
<_RAI1
>::difference_type
845 __diff1
= __last1
- __first1
;
846 const typename iterator_traits
<_RAI2
>::difference_type
847 __diff2
= __last2
- __first2
;
848 return __diff2
< __diff1
? __first1
+ __diff2
: __last1
;
851 template<typename _RAI
>
857 template<bool _BoolType
>
858 struct __lexicographical_compare
860 template<typename _II1
, typename _II2
>
861 static bool __lc(_II1
, _II1
, _II2
, _II2
);
864 template<bool _BoolType
>
865 template<typename _II1
, typename _II2
>
867 __lexicographical_compare
<_BoolType
>::
868 __lc(_II1 __first1
, _II1 __last1
, _II2 __first2
, _II2 __last2
)
870 typedef typename iterator_traits
<_II1
>::iterator_category _Category1
;
871 typedef typename iterator_traits
<_II2
>::iterator_category _Category2
;
872 typedef std::__lc_rai
<_Category1
, _Category2
> __rai_type
;
874 __last1
= __rai_type::__newlast1(__first1
, __last1
,
876 for (; __first1
!= __last1
&& __rai_type::__cnd2(__first2
, __last2
);
877 ++__first1
, ++__first2
)
879 if (*__first1
< *__first2
)
881 if (*__first2
< *__first1
)
884 return __first1
== __last1
&& __first2
!= __last2
;
888 struct __lexicographical_compare
<true>
890 template<typename _Tp
, typename _Up
>
892 __lc(const _Tp
* __first1
, const _Tp
* __last1
,
893 const _Up
* __first2
, const _Up
* __last2
)
895 const size_t __len1
= __last1
- __first1
;
896 const size_t __len2
= __last2
- __first2
;
897 const int __result
= __builtin_memcmp(__first1
, __first2
,
898 std::min(__len1
, __len2
));
899 return __result
!= 0 ? __result
< 0 : __len1
< __len2
;
903 template<typename _II1
, typename _II2
>
905 __lexicographical_compare_aux(_II1 __first1
, _II1 __last1
,
906 _II2 __first2
, _II2 __last2
)
908 typedef typename iterator_traits
<_II1
>::value_type _ValueType1
;
909 typedef typename iterator_traits
<_II2
>::value_type _ValueType2
;
910 const bool __simple
=
911 (__is_byte
<_ValueType1
>::__value
&& __is_byte
<_ValueType2
>::__value
912 && !__gnu_cxx::__numeric_traits
<_ValueType1
>::__is_signed
913 && !__gnu_cxx::__numeric_traits
<_ValueType2
>::__is_signed
914 && __is_pointer
<_II1
>::__value
915 && __is_pointer
<_II2
>::__value
);
917 return std::__lexicographical_compare
<__simple
>::__lc(__first1
, __last1
,
921 _GLIBCXX_END_NAMESPACE
923 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_P
)
926 * @brief Tests a range for element-wise equality.
927 * @param first1 An input iterator.
928 * @param last1 An input iterator.
929 * @param first2 An input iterator.
930 * @return A boolean true or false.
932 * This compares the elements of two ranges using @c == and returns true or
933 * false depending on whether all of the corresponding elements of the
936 template<typename _II1
, typename _II2
>
938 equal(_II1 __first1
, _II1 __last1
, _II2 __first2
)
940 // concept requirements
941 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
942 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
943 __glibcxx_function_requires(_EqualOpConcept
<
944 typename iterator_traits
<_II1
>::value_type
,
945 typename iterator_traits
<_II2
>::value_type
>)
946 __glibcxx_requires_valid_range(__first1
, __last1
);
948 return std::__equal_aux(std::__niter_base
<_II1
>::__b(__first1
),
949 std::__niter_base
<_II1
>::__b(__last1
),
950 std::__niter_base
<_II2
>::__b(__first2
));
954 * @brief Tests a range for element-wise equality.
955 * @param first1 An input iterator.
956 * @param last1 An input iterator.
957 * @param first2 An input iterator.
958 * @param binary_pred A binary predicate @link s20_3_1_base
960 * @return A boolean true or false.
962 * This compares the elements of two ranges using the binary_pred
963 * parameter, and returns true or
964 * false depending on whether all of the corresponding elements of the
967 template<typename _IIter1
, typename _IIter2
, typename _BinaryPredicate
>
969 equal(_IIter1 __first1
, _IIter1 __last1
,
970 _IIter2 __first2
, _BinaryPredicate __binary_pred
)
972 // concept requirements
973 __glibcxx_function_requires(_InputIteratorConcept
<_IIter1
>)
974 __glibcxx_function_requires(_InputIteratorConcept
<_IIter2
>)
975 __glibcxx_requires_valid_range(__first1
, __last1
);
977 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
978 if (!bool(__binary_pred(*__first1
, *__first2
)))
984 * @brief Performs "dictionary" comparison on ranges.
985 * @param first1 An input iterator.
986 * @param last1 An input iterator.
987 * @param first2 An input iterator.
988 * @param last2 An input iterator.
989 * @return A boolean true or false.
991 * "Returns true if the sequence of elements defined by the range
992 * [first1,last1) is lexicographically less than the sequence of elements
993 * defined by the range [first2,last2). Returns false otherwise."
994 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
995 * then this is an inline call to @c memcmp.
997 template<typename _II1
, typename _II2
>
999 lexicographical_compare(_II1 __first1
, _II1 __last1
,
1000 _II2 __first2
, _II2 __last2
)
1002 // concept requirements
1003 typedef typename iterator_traits
<_II1
>::value_type _ValueType1
;
1004 typedef typename iterator_traits
<_II2
>::value_type _ValueType2
;
1005 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
1006 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
1007 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
1008 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
1009 __glibcxx_requires_valid_range(__first1
, __last1
);
1010 __glibcxx_requires_valid_range(__first2
, __last2
);
1012 return std::__lexicographical_compare_aux
1013 (std::__niter_base
<_II1
>::__b(__first1
),
1014 std::__niter_base
<_II1
>::__b(__last1
),
1015 std::__niter_base
<_II2
>::__b(__first2
),
1016 std::__niter_base
<_II2
>::__b(__last2
));
1020 * @brief Performs "dictionary" comparison on ranges.
1021 * @param first1 An input iterator.
1022 * @param last1 An input iterator.
1023 * @param first2 An input iterator.
1024 * @param last2 An input iterator.
1025 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
1026 * @return A boolean true or false.
1028 * The same as the four-parameter @c lexicographical_compare, but uses the
1029 * comp parameter instead of @c <.
1031 template<typename _II1
, typename _II2
, typename _Compare
>
1033 lexicographical_compare(_II1 __first1
, _II1 __last1
,
1034 _II2 __first2
, _II2 __last2
, _Compare __comp
)
1036 typedef typename iterator_traits
<_II1
>::iterator_category _Category1
;
1037 typedef typename iterator_traits
<_II2
>::iterator_category _Category2
;
1038 typedef std::__lc_rai
<_Category1
, _Category2
> __rai_type
;
1040 // concept requirements
1041 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
1042 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
1043 __glibcxx_requires_valid_range(__first1
, __last1
);
1044 __glibcxx_requires_valid_range(__first2
, __last2
);
1046 __last1
= __rai_type::__newlast1(__first1
, __last1
, __first2
, __last2
);
1047 for (; __first1
!= __last1
&& __rai_type::__cnd2(__first2
, __last2
);
1048 ++__first1
, ++__first2
)
1050 if (__comp(*__first1
, *__first2
))
1052 if (__comp(*__first2
, *__first1
))
1055 return __first1
== __last1
&& __first2
!= __last2
;
1059 * @brief Finds the places in ranges which don't match.
1060 * @param first1 An input iterator.
1061 * @param last1 An input iterator.
1062 * @param first2 An input iterator.
1063 * @return A pair of iterators pointing to the first mismatch.
1065 * This compares the elements of two ranges using @c == and returns a pair
1066 * of iterators. The first iterator points into the first range, the
1067 * second iterator points into the second range, and the elements pointed
1068 * to by the iterators are not equal.
1070 template<typename _InputIterator1
, typename _InputIterator2
>
1071 pair
<_InputIterator1
, _InputIterator2
>
1072 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1073 _InputIterator2 __first2
)
1075 // concept requirements
1076 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
1077 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
1078 __glibcxx_function_requires(_EqualOpConcept
<
1079 typename iterator_traits
<_InputIterator1
>::value_type
,
1080 typename iterator_traits
<_InputIterator2
>::value_type
>)
1081 __glibcxx_requires_valid_range(__first1
, __last1
);
1083 while (__first1
!= __last1
&& *__first1
== *__first2
)
1088 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
1092 * @brief Finds the places in ranges which don't match.
1093 * @param first1 An input iterator.
1094 * @param last1 An input iterator.
1095 * @param first2 An input iterator.
1096 * @param binary_pred A binary predicate @link s20_3_1_base
1098 * @return A pair of iterators pointing to the first mismatch.
1100 * This compares the elements of two ranges using the binary_pred
1101 * parameter, and returns a pair
1102 * of iterators. The first iterator points into the first range, the
1103 * second iterator points into the second range, and the elements pointed
1104 * to by the iterators are not equal.
1106 template<typename _InputIterator1
, typename _InputIterator2
,
1107 typename _BinaryPredicate
>
1108 pair
<_InputIterator1
, _InputIterator2
>
1109 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1110 _InputIterator2 __first2
, _BinaryPredicate __binary_pred
)
1112 // concept requirements
1113 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
1114 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
1115 __glibcxx_requires_valid_range(__first1
, __last1
);
1117 while (__first1
!= __last1
&& bool(__binary_pred(*__first1
, *__first2
)))
1122 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
1125 _GLIBCXX_END_NESTED_NAMESPACE
1127 // NB: This file is included within many other C++ includes, as a way
1128 // of getting the base algorithms. So, make sure that parallel bits
1129 // come in too if requested.
1130 #ifdef _GLIBCXX_PARALLEL
1131 # include <parallel/algobase.h>