1 // Core algorithmic facilities -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 // 2011, 2012 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 bits/stl_algobase.h
53 * This is an internal header file, included by other library headers.
54 * Do not attempt to use it directly. @headername{algorithm}
57 #ifndef _STL_ALGOBASE_H
58 #define _STL_ALGOBASE_H 1
60 #include <bits/c++config.h>
61 #include <bits/functexcept.h>
62 #include <bits/cpp_type_traits.h>
63 #include <ext/type_traits.h>
64 #include <ext/numeric_traits.h>
65 #include <bits/stl_pair.h>
66 #include <bits/stl_iterator_base_types.h>
67 #include <bits/stl_iterator_base_funcs.h>
68 #include <bits/stl_iterator.h>
69 #include <bits/concept_check.h>
70 #include <debug/debug.h>
71 #include <bits/move.h> // For std::swap and _GLIBCXX_MOVE
73 namespace std
_GLIBCXX_VISIBILITY(default)
75 _GLIBCXX_BEGIN_NAMESPACE_VERSION
77 #ifndef __GXX_EXPERIMENTAL_CXX0X__
78 // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
79 // nutshell, we are partially implementing the resolution of DR 187,
80 // when it's safe, i.e., the value_types are equal.
81 template<bool _BoolType
>
84 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
86 iter_swap(_ForwardIterator1 __a
, _ForwardIterator2 __b
)
88 typedef typename iterator_traits
<_ForwardIterator1
>::value_type
90 _ValueType1 __tmp
= _GLIBCXX_MOVE(*__a
);
91 *__a
= _GLIBCXX_MOVE(*__b
);
92 *__b
= _GLIBCXX_MOVE(__tmp
);
97 struct __iter_swap
<true>
99 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
101 iter_swap(_ForwardIterator1 __a
, _ForwardIterator2 __b
)
109 * @brief Swaps the contents of two iterators.
110 * @ingroup mutating_algorithms
111 * @param __a An iterator.
112 * @param __b Another iterator.
115 * This function swaps the values pointed to by two iterators, not the
116 * iterators themselves.
118 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
120 iter_swap(_ForwardIterator1 __a
, _ForwardIterator2 __b
)
122 // concept requirements
123 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
125 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
128 #ifndef __GXX_EXPERIMENTAL_CXX0X__
129 typedef typename iterator_traits
<_ForwardIterator1
>::value_type
131 typedef typename iterator_traits
<_ForwardIterator2
>::value_type
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
>::
153 * @brief Swap the elements of two sequences.
154 * @ingroup mutating_algorithms
155 * @param __first1 A forward iterator.
156 * @param __last1 A forward iterator.
157 * @param __first2 A forward iterator.
158 * @return An iterator equal to @p first2+(last1-first1).
160 * Swaps each element in the range @p [first1,last1) with the
161 * corresponding element in the range @p [first2,(last1-first1)).
162 * The ranges must not overlap.
164 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
166 swap_ranges(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
167 _ForwardIterator2 __first2
)
169 // concept requirements
170 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
172 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
174 __glibcxx_requires_valid_range(__first1
, __last1
);
176 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
177 std::iter_swap(__first1
, __first2
);
182 * @brief This does what you think it does.
183 * @ingroup sorting_algorithms
184 * @param __a A thing of arbitrary type.
185 * @param __b Another thing of arbitrary type.
186 * @return The lesser of the parameters.
188 * This is the simple classic generic implementation. It will work on
189 * temporary expressions, since they are only evaluated once, unlike a
190 * preprocessor macro.
192 template<typename _Tp
>
194 min(const _Tp
& __a
, const _Tp
& __b
)
196 // concept requirements
197 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
199 return __b
< __a
? __b
: __a
;
203 * @brief This does what you think it does.
204 * @ingroup sorting_algorithms
205 * @param __a A thing of arbitrary type.
206 * @param __b Another thing of arbitrary type.
207 * @return The greater of the parameters.
209 * This is the simple classic generic implementation. It will work on
210 * temporary expressions, since they are only evaluated once, unlike a
211 * preprocessor macro.
213 template<typename _Tp
>
215 max(const _Tp
& __a
, const _Tp
& __b
)
217 // concept requirements
218 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
220 return __a
< __b
? __b
: __a
;
224 * @brief This does what you think it does.
225 * @ingroup sorting_algorithms
226 * @param __a A thing of arbitrary type.
227 * @param __b Another thing of arbitrary type.
228 * @param __comp A @link comparison_functors comparison functor@endlink.
229 * @return The lesser of the parameters.
231 * This will work on temporary expressions, since they are only evaluated
232 * once, unlike a preprocessor macro.
234 template<typename _Tp
, typename _Compare
>
236 min(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
237 { return __comp(__b
, __a
) ? __b
: __a
; }
240 * @brief This does what you think it does.
241 * @ingroup sorting_algorithms
242 * @param __a A thing of arbitrary type.
243 * @param __b Another thing of arbitrary type.
244 * @param __comp A @link comparison_functors comparison functor@endlink.
245 * @return The greater of the parameters.
247 * This will work on temporary expressions, since they are only evaluated
248 * once, unlike a preprocessor macro.
250 template<typename _Tp
, typename _Compare
>
252 max(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
253 { return __comp(__a
, __b
) ? __b
: __a
; }
255 // If _Iterator is a __normal_iterator return its base (a plain pointer,
256 // normally) otherwise return it untouched. See copy, fill, ...
257 template<typename _Iterator
>
259 : _Iter_base
<_Iterator
, __is_normal_iterator
<_Iterator
>::__value
>
262 template<typename _Iterator
>
263 inline typename _Niter_base
<_Iterator
>::iterator_type
264 __niter_base(_Iterator __it
)
265 { return std::_Niter_base
<_Iterator
>::_S_base(__it
); }
267 // Likewise, for move_iterator.
268 template<typename _Iterator
>
270 : _Iter_base
<_Iterator
, __is_move_iterator
<_Iterator
>::__value
>
273 template<typename _Iterator
>
274 inline typename _Miter_base
<_Iterator
>::iterator_type
275 __miter_base(_Iterator __it
)
276 { return std::_Miter_base
<_Iterator
>::_S_base(__it
); }
278 // All of these auxiliary structs serve two purposes. (1) Replace
279 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
280 // because the input and output ranges are permitted to overlap.)
281 // (2) If we're using random access iterators, then write the loop as
282 // a for loop with an explicit count.
284 template<bool, bool, typename
>
287 template<typename _II
, typename _OI
>
289 __copy_m(_II __first
, _II __last
, _OI __result
)
291 for (; __first
!= __last
; ++__result
, ++__first
)
292 *__result
= *__first
;
297 #ifdef __GXX_EXPERIMENTAL_CXX0X__
298 template<typename _Category
>
299 struct __copy_move
<true, false, _Category
>
301 template<typename _II
, typename _OI
>
303 __copy_m(_II __first
, _II __last
, _OI __result
)
305 for (; __first
!= __last
; ++__result
, ++__first
)
306 *__result
= std::move(*__first
);
313 struct __copy_move
<false, false, random_access_iterator_tag
>
315 template<typename _II
, typename _OI
>
317 __copy_m(_II __first
, _II __last
, _OI __result
)
319 typedef typename iterator_traits
<_II
>::difference_type _Distance
;
320 for(_Distance __n
= __last
- __first
; __n
> 0; --__n
)
322 *__result
= *__first
;
330 #ifdef __GXX_EXPERIMENTAL_CXX0X__
332 struct __copy_move
<true, false, random_access_iterator_tag
>
334 template<typename _II
, typename _OI
>
336 __copy_m(_II __first
, _II __last
, _OI __result
)
338 typedef typename iterator_traits
<_II
>::difference_type _Distance
;
339 for(_Distance __n
= __last
- __first
; __n
> 0; --__n
)
341 *__result
= std::move(*__first
);
350 template<bool _IsMove
>
351 struct __copy_move
<_IsMove
, true, random_access_iterator_tag
>
353 template<typename _Tp
>
355 __copy_m(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
357 const ptrdiff_t _Num
= __last
- __first
;
359 __builtin_memmove(__result
, __first
, sizeof(_Tp
) * _Num
);
360 return __result
+ _Num
;
364 template<bool _IsMove
, typename _II
, typename _OI
>
366 __copy_move_a(_II __first
, _II __last
, _OI __result
)
368 typedef typename iterator_traits
<_II
>::value_type _ValueTypeI
;
369 typedef typename iterator_traits
<_OI
>::value_type _ValueTypeO
;
370 typedef typename iterator_traits
<_II
>::iterator_category _Category
;
371 const bool __simple
= (__is_trivial(_ValueTypeI
)
372 && __is_pointer
<_II
>::__value
373 && __is_pointer
<_OI
>::__value
374 && __are_same
<_ValueTypeI
, _ValueTypeO
>::__value
);
376 return std::__copy_move
<_IsMove
, __simple
,
377 _Category
>::__copy_m(__first
, __last
, __result
);
380 // Helpers for streambuf iterators (either istream or ostream).
381 // NB: avoid including <iosfwd>, relatively large.
382 template<typename _CharT
>
385 template<typename _CharT
, typename _Traits
>
386 class istreambuf_iterator
;
388 template<typename _CharT
, typename _Traits
>
389 class ostreambuf_iterator
;
391 template<bool _IsMove
, typename _CharT
>
392 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
393 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> > >::__type
394 __copy_move_a2(_CharT
*, _CharT
*,
395 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> >);
397 template<bool _IsMove
, typename _CharT
>
398 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
399 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> > >::__type
400 __copy_move_a2(const _CharT
*, const _CharT
*,
401 ostreambuf_iterator
<_CharT
, char_traits
<_CharT
> >);
403 template<bool _IsMove
, typename _CharT
>
404 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
406 __copy_move_a2(istreambuf_iterator
<_CharT
, char_traits
<_CharT
> >,
407 istreambuf_iterator
<_CharT
, char_traits
<_CharT
> >, _CharT
*);
409 template<bool _IsMove
, typename _II
, typename _OI
>
411 __copy_move_a2(_II __first
, _II __last
, _OI __result
)
413 return _OI(std::__copy_move_a
<_IsMove
>(std::__niter_base(__first
),
414 std::__niter_base(__last
),
415 std::__niter_base(__result
)));
419 * @brief Copies the range [first,last) into result.
420 * @ingroup mutating_algorithms
421 * @param __first An input iterator.
422 * @param __last An input iterator.
423 * @param __result An output iterator.
424 * @return result + (first - last)
426 * This inline function will boil down to a call to @c memmove whenever
427 * possible. Failing that, if random access iterators are passed, then the
428 * loop count will be known (and therefore a candidate for compiler
429 * optimizations such as unrolling). Result may not be contained within
430 * [first,last); the copy_backward function should be used instead.
432 * Note that the end of the output range is permitted to be contained
433 * within [first,last).
435 template<typename _II
, typename _OI
>
437 copy(_II __first
, _II __last
, _OI __result
)
439 // concept requirements
440 __glibcxx_function_requires(_InputIteratorConcept
<_II
>)
441 __glibcxx_function_requires(_OutputIteratorConcept
<_OI
,
442 typename iterator_traits
<_II
>::value_type
>)
443 __glibcxx_requires_valid_range(__first
, __last
);
445 return (std::__copy_move_a2
<__is_move_iterator
<_II
>::__value
>
446 (std::__miter_base(__first
), std::__miter_base(__last
),
450 #ifdef __GXX_EXPERIMENTAL_CXX0X__
452 * @brief Moves the range [first,last) into result.
453 * @ingroup mutating_algorithms
454 * @param __first An input iterator.
455 * @param __last An input iterator.
456 * @param __result An output iterator.
457 * @return result + (first - last)
459 * This inline function will boil down to a call to @c memmove whenever
460 * possible. Failing that, if random access iterators are passed, then the
461 * loop count will be known (and therefore a candidate for compiler
462 * optimizations such as unrolling). Result may not be contained within
463 * [first,last); the move_backward function should be used instead.
465 * Note that the end of the output range is permitted to be contained
466 * within [first,last).
468 template<typename _II
, typename _OI
>
470 move(_II __first
, _II __last
, _OI __result
)
472 // concept requirements
473 __glibcxx_function_requires(_InputIteratorConcept
<_II
>)
474 __glibcxx_function_requires(_OutputIteratorConcept
<_OI
,
475 typename iterator_traits
<_II
>::value_type
>)
476 __glibcxx_requires_valid_range(__first
, __last
);
478 return std::__copy_move_a2
<true>(std::__miter_base(__first
),
479 std::__miter_base(__last
), __result
);
482 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
484 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
487 template<bool, bool, typename
>
488 struct __copy_move_backward
490 template<typename _BI1
, typename _BI2
>
492 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
494 while (__first
!= __last
)
495 *--__result
= *--__last
;
500 #ifdef __GXX_EXPERIMENTAL_CXX0X__
501 template<typename _Category
>
502 struct __copy_move_backward
<true, false, _Category
>
504 template<typename _BI1
, typename _BI2
>
506 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
508 while (__first
!= __last
)
509 *--__result
= std::move(*--__last
);
516 struct __copy_move_backward
<false, false, random_access_iterator_tag
>
518 template<typename _BI1
, typename _BI2
>
520 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
522 typename iterator_traits
<_BI1
>::difference_type __n
;
523 for (__n
= __last
- __first
; __n
> 0; --__n
)
524 *--__result
= *--__last
;
529 #ifdef __GXX_EXPERIMENTAL_CXX0X__
531 struct __copy_move_backward
<true, false, random_access_iterator_tag
>
533 template<typename _BI1
, typename _BI2
>
535 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
537 typename iterator_traits
<_BI1
>::difference_type __n
;
538 for (__n
= __last
- __first
; __n
> 0; --__n
)
539 *--__result
= std::move(*--__last
);
545 template<bool _IsMove
>
546 struct __copy_move_backward
<_IsMove
, true, random_access_iterator_tag
>
548 template<typename _Tp
>
550 __copy_move_b(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
552 const ptrdiff_t _Num
= __last
- __first
;
554 __builtin_memmove(__result
- _Num
, __first
, sizeof(_Tp
) * _Num
);
555 return __result
- _Num
;
559 template<bool _IsMove
, typename _BI1
, typename _BI2
>
561 __copy_move_backward_a(_BI1 __first
, _BI1 __last
, _BI2 __result
)
563 typedef typename iterator_traits
<_BI1
>::value_type _ValueType1
;
564 typedef typename iterator_traits
<_BI2
>::value_type _ValueType2
;
565 typedef typename iterator_traits
<_BI1
>::iterator_category _Category
;
566 const bool __simple
= (__is_trivial(_ValueType1
)
567 && __is_pointer
<_BI1
>::__value
568 && __is_pointer
<_BI2
>::__value
569 && __are_same
<_ValueType1
, _ValueType2
>::__value
);
571 return std::__copy_move_backward
<_IsMove
, __simple
,
572 _Category
>::__copy_move_b(__first
,
577 template<bool _IsMove
, typename _BI1
, typename _BI2
>
579 __copy_move_backward_a2(_BI1 __first
, _BI1 __last
, _BI2 __result
)
581 return _BI2(std::__copy_move_backward_a
<_IsMove
>
582 (std::__niter_base(__first
), std::__niter_base(__last
),
583 std::__niter_base(__result
)));
587 * @brief Copies the range [first,last) into result.
588 * @ingroup mutating_algorithms
589 * @param __first A bidirectional iterator.
590 * @param __last A bidirectional iterator.
591 * @param __result A bidirectional iterator.
592 * @return result - (first - last)
594 * The function has the same effect as copy, but starts at the end of the
595 * range and works its way to the start, returning the start of the result.
596 * This inline function will boil down to a call to @c memmove whenever
597 * possible. Failing that, if random access iterators are passed, then the
598 * loop count will be known (and therefore a candidate for compiler
599 * optimizations such as unrolling).
601 * Result may not be in the range [first,last). Use copy instead. Note
602 * that the start of the output range may overlap [first,last).
604 template<typename _BI1
, typename _BI2
>
606 copy_backward(_BI1 __first
, _BI1 __last
, _BI2 __result
)
608 // concept requirements
609 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BI1
>)
610 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<_BI2
>)
611 __glibcxx_function_requires(_ConvertibleConcept
<
612 typename iterator_traits
<_BI1
>::value_type
,
613 typename iterator_traits
<_BI2
>::value_type
>)
614 __glibcxx_requires_valid_range(__first
, __last
);
616 return (std::__copy_move_backward_a2
<__is_move_iterator
<_BI1
>::__value
>
617 (std::__miter_base(__first
), std::__miter_base(__last
),
621 #ifdef __GXX_EXPERIMENTAL_CXX0X__
623 * @brief Moves the range [first,last) into result.
624 * @ingroup mutating_algorithms
625 * @param __first A bidirectional iterator.
626 * @param __last A bidirectional iterator.
627 * @param __result A bidirectional iterator.
628 * @return result - (first - last)
630 * The function has the same effect as move, but starts at the end of the
631 * range and works its way to the start, returning the start of the result.
632 * This inline function will boil down to a call to @c memmove whenever
633 * possible. Failing that, if random access iterators are passed, then the
634 * loop count will be known (and therefore a candidate for compiler
635 * optimizations such as unrolling).
637 * Result may not be in the range (first,last]. Use move instead. Note
638 * that the start of the output range may overlap [first,last).
640 template<typename _BI1
, typename _BI2
>
642 move_backward(_BI1 __first
, _BI1 __last
, _BI2 __result
)
644 // concept requirements
645 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BI1
>)
646 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<_BI2
>)
647 __glibcxx_function_requires(_ConvertibleConcept
<
648 typename iterator_traits
<_BI1
>::value_type
,
649 typename iterator_traits
<_BI2
>::value_type
>)
650 __glibcxx_requires_valid_range(__first
, __last
);
652 return std::__copy_move_backward_a2
<true>(std::__miter_base(__first
),
653 std::__miter_base(__last
),
657 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
659 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
662 template<typename _ForwardIterator
, typename _Tp
>
664 __gnu_cxx::__enable_if
<!__is_scalar
<_Tp
>::__value
, void>::__type
665 __fill_a(_ForwardIterator __first
, _ForwardIterator __last
,
668 for (; __first
!= __last
; ++__first
)
672 template<typename _ForwardIterator
, typename _Tp
>
674 __gnu_cxx::__enable_if
<__is_scalar
<_Tp
>::__value
, void>::__type
675 __fill_a(_ForwardIterator __first
, _ForwardIterator __last
,
678 const _Tp __tmp
= __value
;
679 for (; __first
!= __last
; ++__first
)
683 // Specialization: for char types we can use memset.
684 template<typename _Tp
>
686 __gnu_cxx::__enable_if
<__is_byte
<_Tp
>::__value
, void>::__type
687 __fill_a(_Tp
* __first
, _Tp
* __last
, const _Tp
& __c
)
689 const _Tp __tmp
= __c
;
690 __builtin_memset(__first
, static_cast<unsigned char>(__tmp
),
695 * @brief Fills the range [first,last) with copies of value.
696 * @ingroup mutating_algorithms
697 * @param __first A forward iterator.
698 * @param __last A forward iterator.
699 * @param __value A reference-to-const of arbitrary type.
702 * This function fills a range with copies of the same value. For char
703 * types filling contiguous areas of memory, this becomes an inline call
704 * to @c memset or @c wmemset.
706 template<typename _ForwardIterator
, typename _Tp
>
708 fill(_ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __value
)
710 // concept requirements
711 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
713 __glibcxx_requires_valid_range(__first
, __last
);
715 std::__fill_a(std::__niter_base(__first
), std::__niter_base(__last
),
719 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
721 __gnu_cxx::__enable_if
<!__is_scalar
<_Tp
>::__value
, _OutputIterator
>::__type
722 __fill_n_a(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
724 for (__decltype(__n
+ 0) __niter
= __n
;
725 __niter
> 0; --__niter
, ++__first
)
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 const _Tp __tmp
= __value
;
736 for (__decltype(__n
+ 0) __niter
= __n
;
737 __niter
> 0; --__niter
, ++__first
)
742 template<typename _Size
, typename _Tp
>
744 __gnu_cxx::__enable_if
<__is_byte
<_Tp
>::__value
, _Tp
*>::__type
745 __fill_n_a(_Tp
* __first
, _Size __n
, const _Tp
& __c
)
747 std::__fill_a(__first
, __first
+ __n
, __c
);
748 return __first
+ __n
;
752 * @brief Fills the range [first,first+n) with copies of value.
753 * @ingroup mutating_algorithms
754 * @param __first An output iterator.
755 * @param __n The count of copies to perform.
756 * @param __value A reference-to-const of arbitrary type.
757 * @return The iterator at first+n.
759 * This function fills a range with copies of the same value. For char
760 * types filling contiguous areas of memory, this becomes an inline call
761 * to @c memset or @ wmemset.
763 * _GLIBCXX_RESOLVE_LIB_DEFECTS
764 * DR 865. More algorithms that throw away information
766 template<typename _OI
, typename _Size
, typename _Tp
>
768 fill_n(_OI __first
, _Size __n
, const _Tp
& __value
)
770 // concept requirements
771 __glibcxx_function_requires(_OutputIteratorConcept
<_OI
, _Tp
>)
773 return _OI(std::__fill_n_a(std::__niter_base(__first
), __n
, __value
));
776 template<bool _BoolType
>
779 template<typename _II1
, typename _II2
>
781 equal(_II1 __first1
, _II1 __last1
, _II2 __first2
)
783 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
784 if (!(*__first1
== *__first2
))
793 template<typename _Tp
>
795 equal(const _Tp
* __first1
, const _Tp
* __last1
, const _Tp
* __first2
)
797 return !__builtin_memcmp(__first1
, __first2
, sizeof(_Tp
)
798 * (__last1
- __first1
));
802 template<typename _II1
, typename _II2
>
804 __equal_aux(_II1 __first1
, _II1 __last1
, _II2 __first2
)
806 typedef typename iterator_traits
<_II1
>::value_type _ValueType1
;
807 typedef typename iterator_traits
<_II2
>::value_type _ValueType2
;
808 const bool __simple
= ((__is_integer
<_ValueType1
>::__value
809 || __is_pointer
<_ValueType1
>::__value
)
810 && __is_pointer
<_II1
>::__value
811 && __is_pointer
<_II2
>::__value
812 && __are_same
<_ValueType1
, _ValueType2
>::__value
);
814 return std::__equal
<__simple
>::equal(__first1
, __last1
, __first2
);
818 template<typename
, typename
>
821 template<typename _II1
, typename _II2
>
823 __newlast1(_II1
, _II1 __last1
, _II2
, _II2
)
826 template<typename _II
>
828 __cnd2(_II __first
, _II __last
)
829 { return __first
!= __last
; }
833 struct __lc_rai
<random_access_iterator_tag
, random_access_iterator_tag
>
835 template<typename _RAI1
, typename _RAI2
>
837 __newlast1(_RAI1 __first1
, _RAI1 __last1
,
838 _RAI2 __first2
, _RAI2 __last2
)
840 const typename iterator_traits
<_RAI1
>::difference_type
841 __diff1
= __last1
- __first1
;
842 const typename iterator_traits
<_RAI2
>::difference_type
843 __diff2
= __last2
- __first2
;
844 return __diff2
< __diff1
? __first1
+ __diff2
: __last1
;
847 template<typename _RAI
>
853 template<bool _BoolType
>
854 struct __lexicographical_compare
856 template<typename _II1
, typename _II2
>
857 static bool __lc(_II1
, _II1
, _II2
, _II2
);
860 template<bool _BoolType
>
861 template<typename _II1
, typename _II2
>
863 __lexicographical_compare
<_BoolType
>::
864 __lc(_II1 __first1
, _II1 __last1
, _II2 __first2
, _II2 __last2
)
866 typedef typename iterator_traits
<_II1
>::iterator_category _Category1
;
867 typedef typename iterator_traits
<_II2
>::iterator_category _Category2
;
868 typedef std::__lc_rai
<_Category1
, _Category2
> __rai_type
;
870 __last1
= __rai_type::__newlast1(__first1
, __last1
,
872 for (; __first1
!= __last1
&& __rai_type::__cnd2(__first2
, __last2
);
873 ++__first1
, ++__first2
)
875 if (*__first1
< *__first2
)
877 if (*__first2
< *__first1
)
880 return __first1
== __last1
&& __first2
!= __last2
;
884 struct __lexicographical_compare
<true>
886 template<typename _Tp
, typename _Up
>
888 __lc(const _Tp
* __first1
, const _Tp
* __last1
,
889 const _Up
* __first2
, const _Up
* __last2
)
891 const size_t __len1
= __last1
- __first1
;
892 const size_t __len2
= __last2
- __first2
;
893 const int __result
= __builtin_memcmp(__first1
, __first2
,
894 std::min(__len1
, __len2
));
895 return __result
!= 0 ? __result
< 0 : __len1
< __len2
;
899 template<typename _II1
, typename _II2
>
901 __lexicographical_compare_aux(_II1 __first1
, _II1 __last1
,
902 _II2 __first2
, _II2 __last2
)
904 typedef typename iterator_traits
<_II1
>::value_type _ValueType1
;
905 typedef typename iterator_traits
<_II2
>::value_type _ValueType2
;
906 const bool __simple
=
907 (__is_byte
<_ValueType1
>::__value
&& __is_byte
<_ValueType2
>::__value
908 && !__gnu_cxx::__numeric_traits
<_ValueType1
>::__is_signed
909 && !__gnu_cxx::__numeric_traits
<_ValueType2
>::__is_signed
910 && __is_pointer
<_II1
>::__value
911 && __is_pointer
<_II2
>::__value
);
913 return std::__lexicographical_compare
<__simple
>::__lc(__first1
, __last1
,
918 * @brief Finds the first position in which @a val could be inserted
919 * without changing the ordering.
920 * @param __first An iterator.
921 * @param __last Another iterator.
922 * @param __val The search term.
923 * @return An iterator pointing to the first element <em>not less
924 * than</em> @a val, or end() if every element is less than
926 * @ingroup binary_search_algorithms
928 template<typename _ForwardIterator
, typename _Tp
>
930 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
933 #ifdef _GLIBCXX_CONCEPT_CHECKS
934 typedef typename iterator_traits
<_ForwardIterator
>::value_type
937 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
940 // concept requirements
941 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
942 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
943 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
945 _DistanceType __len
= std::distance(__first
, __last
);
949 _DistanceType __half
= __len
>> 1;
950 _ForwardIterator __middle
= __first
;
951 std::advance(__middle
, __half
);
952 if (*__middle
< __val
)
956 __len
= __len
- __half
- 1;
964 /// This is a helper function for the sort routines and for random.tcc.
965 // Precondition: __n > 0.
966 inline _GLIBCXX_CONSTEXPR
int
968 { return sizeof(int) * __CHAR_BIT__
- 1 - __builtin_clz(__n
); }
970 inline _GLIBCXX_CONSTEXPR
unsigned
972 { return sizeof(int) * __CHAR_BIT__
- 1 - __builtin_clz(__n
); }
974 inline _GLIBCXX_CONSTEXPR
long
976 { return sizeof(long) * __CHAR_BIT__
- 1 - __builtin_clzl(__n
); }
978 inline _GLIBCXX_CONSTEXPR
unsigned long
979 __lg(unsigned long __n
)
980 { return sizeof(long) * __CHAR_BIT__
- 1 - __builtin_clzl(__n
); }
982 inline _GLIBCXX_CONSTEXPR
long long
984 { return sizeof(long long) * __CHAR_BIT__
- 1 - __builtin_clzll(__n
); }
986 inline _GLIBCXX_CONSTEXPR
unsigned long long
987 __lg(unsigned long long __n
)
988 { return sizeof(long long) * __CHAR_BIT__
- 1 - __builtin_clzll(__n
); }
990 _GLIBCXX_END_NAMESPACE_VERSION
992 _GLIBCXX_BEGIN_NAMESPACE_ALGO
995 * @brief Tests a range for element-wise equality.
996 * @ingroup non_mutating_algorithms
997 * @param __first1 An input iterator.
998 * @param __last1 An input iterator.
999 * @param __first2 An input iterator.
1000 * @return A boolean true or false.
1002 * This compares the elements of two ranges using @c == and returns true or
1003 * false depending on whether all of the corresponding elements of the
1006 template<typename _II1
, typename _II2
>
1008 equal(_II1 __first1
, _II1 __last1
, _II2 __first2
)
1010 // concept requirements
1011 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
1012 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
1013 __glibcxx_function_requires(_EqualOpConcept
<
1014 typename iterator_traits
<_II1
>::value_type
,
1015 typename iterator_traits
<_II2
>::value_type
>)
1016 __glibcxx_requires_valid_range(__first1
, __last1
);
1018 return std::__equal_aux(std::__niter_base(__first1
),
1019 std::__niter_base(__last1
),
1020 std::__niter_base(__first2
));
1024 * @brief Tests a range for element-wise equality.
1025 * @ingroup non_mutating_algorithms
1026 * @param __first1 An input iterator.
1027 * @param __last1 An input iterator.
1028 * @param __first2 An input iterator.
1029 * @param __binary_pred A binary predicate @link functors
1031 * @return A boolean true or false.
1033 * This compares the elements of two ranges using the binary_pred
1034 * parameter, and returns true or
1035 * false depending on whether all of the corresponding elements of the
1038 template<typename _IIter1
, typename _IIter2
, typename _BinaryPredicate
>
1040 equal(_IIter1 __first1
, _IIter1 __last1
,
1041 _IIter2 __first2
, _BinaryPredicate __binary_pred
)
1043 // concept requirements
1044 __glibcxx_function_requires(_InputIteratorConcept
<_IIter1
>)
1045 __glibcxx_function_requires(_InputIteratorConcept
<_IIter2
>)
1046 __glibcxx_requires_valid_range(__first1
, __last1
);
1048 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
1049 if (!bool(__binary_pred(*__first1
, *__first2
)))
1055 * @brief Performs @b dictionary comparison on ranges.
1056 * @ingroup sorting_algorithms
1057 * @param __first1 An input iterator.
1058 * @param __last1 An input iterator.
1059 * @param __first2 An input iterator.
1060 * @param __last2 An input iterator.
1061 * @return A boolean true or false.
1063 * <em>Returns true if the sequence of elements defined by the range
1064 * [first1,last1) is lexicographically less than the sequence of elements
1065 * defined by the range [first2,last2). Returns false otherwise.</em>
1066 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
1067 * then this is an inline call to @c memcmp.
1069 template<typename _II1
, typename _II2
>
1071 lexicographical_compare(_II1 __first1
, _II1 __last1
,
1072 _II2 __first2
, _II2 __last2
)
1074 #ifdef _GLIBCXX_CONCEPT_CHECKS
1075 // concept requirements
1076 typedef typename iterator_traits
<_II1
>::value_type _ValueType1
;
1077 typedef typename iterator_traits
<_II2
>::value_type _ValueType2
;
1079 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
1080 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
1081 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType1
, _ValueType2
>)
1082 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType2
, _ValueType1
>)
1083 __glibcxx_requires_valid_range(__first1
, __last1
);
1084 __glibcxx_requires_valid_range(__first2
, __last2
);
1086 return std::__lexicographical_compare_aux(std::__niter_base(__first1
),
1087 std::__niter_base(__last1
),
1088 std::__niter_base(__first2
),
1089 std::__niter_base(__last2
));
1093 * @brief Performs @b dictionary comparison on ranges.
1094 * @ingroup sorting_algorithms
1095 * @param __first1 An input iterator.
1096 * @param __last1 An input iterator.
1097 * @param __first2 An input iterator.
1098 * @param __last2 An input iterator.
1099 * @param __comp A @link comparison_functors comparison functor@endlink.
1100 * @return A boolean true or false.
1102 * The same as the four-parameter @c lexicographical_compare, but uses the
1103 * comp parameter instead of @c <.
1105 template<typename _II1
, typename _II2
, typename _Compare
>
1107 lexicographical_compare(_II1 __first1
, _II1 __last1
,
1108 _II2 __first2
, _II2 __last2
, _Compare __comp
)
1110 typedef typename iterator_traits
<_II1
>::iterator_category _Category1
;
1111 typedef typename iterator_traits
<_II2
>::iterator_category _Category2
;
1112 typedef std::__lc_rai
<_Category1
, _Category2
> __rai_type
;
1114 // concept requirements
1115 __glibcxx_function_requires(_InputIteratorConcept
<_II1
>)
1116 __glibcxx_function_requires(_InputIteratorConcept
<_II2
>)
1117 __glibcxx_requires_valid_range(__first1
, __last1
);
1118 __glibcxx_requires_valid_range(__first2
, __last2
);
1120 __last1
= __rai_type::__newlast1(__first1
, __last1
, __first2
, __last2
);
1121 for (; __first1
!= __last1
&& __rai_type::__cnd2(__first2
, __last2
);
1122 ++__first1
, ++__first2
)
1124 if (__comp(*__first1
, *__first2
))
1126 if (__comp(*__first2
, *__first1
))
1129 return __first1
== __last1
&& __first2
!= __last2
;
1133 * @brief Finds the places in ranges which don't match.
1134 * @ingroup non_mutating_algorithms
1135 * @param __first1 An input iterator.
1136 * @param __last1 An input iterator.
1137 * @param __first2 An input iterator.
1138 * @return A pair of iterators pointing to the first mismatch.
1140 * This compares the elements of two ranges using @c == and returns a pair
1141 * of iterators. The first iterator points into the first range, the
1142 * second iterator points into the second range, and the elements pointed
1143 * to by the iterators are not equal.
1145 template<typename _InputIterator1
, typename _InputIterator2
>
1146 pair
<_InputIterator1
, _InputIterator2
>
1147 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1148 _InputIterator2 __first2
)
1150 // concept requirements
1151 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
1152 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
1153 __glibcxx_function_requires(_EqualOpConcept
<
1154 typename iterator_traits
<_InputIterator1
>::value_type
,
1155 typename iterator_traits
<_InputIterator2
>::value_type
>)
1156 __glibcxx_requires_valid_range(__first1
, __last1
);
1158 while (__first1
!= __last1
&& *__first1
== *__first2
)
1163 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
1167 * @brief Finds the places in ranges which don't match.
1168 * @ingroup non_mutating_algorithms
1169 * @param __first1 An input iterator.
1170 * @param __last1 An input iterator.
1171 * @param __first2 An input iterator.
1172 * @param __binary_pred A binary predicate @link functors
1174 * @return A pair of iterators pointing to the first mismatch.
1176 * This compares the elements of two ranges using the binary_pred
1177 * parameter, and returns a pair
1178 * of iterators. The first iterator points into the first range, the
1179 * second iterator points into the second range, and the elements pointed
1180 * to by the iterators are not equal.
1182 template<typename _InputIterator1
, typename _InputIterator2
,
1183 typename _BinaryPredicate
>
1184 pair
<_InputIterator1
, _InputIterator2
>
1185 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1186 _InputIterator2 __first2
, _BinaryPredicate __binary_pred
)
1188 // concept requirements
1189 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
1190 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
1191 __glibcxx_requires_valid_range(__first1
, __last1
);
1193 while (__first1
!= __last1
&& bool(__binary_pred(*__first1
, *__first2
)))
1198 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
1201 _GLIBCXX_END_NAMESPACE_ALGO
1204 // NB: This file is included within many other C++ includes, as a way
1205 // of getting the base algorithms. So, make sure that parallel bits
1206 // come in too if requested.
1207 #ifdef _GLIBCXX_PARALLEL
1208 # include <parallel/algobase.h>