Merge from mainline (163495:164578).
[official-gcc/graphite-test-results.git] / libstdc++-v3 / include / bits / stl_algobase.h
blob1d951aa7999696d6c0c60b5459dcc546208f87ba
1 // Core algorithmic facilities -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
5 //
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)
10 // any later version.
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/>.
28 * Copyright (c) 1994
29 * Hewlett-Packard Company
31 * Permission to use, copy, modify, distribute and sell this software
32 * and its documentation for any purpose is hereby granted without fee,
33 * provided that the above copyright notice appear in all copies and
34 * that both that copyright notice and this permission notice appear
35 * in supporting documentation. Hewlett-Packard Company makes no
36 * representations about the suitability of this software for any
37 * purpose. It is provided "as is" without express or implied warranty.
40 * Copyright (c) 1996-1998
41 * Silicon Graphics Computer Systems, Inc.
43 * Permission to use, copy, modify, distribute and sell this software
44 * and its documentation for any purpose is hereby granted without fee,
45 * provided that the above copyright notice appear in all copies and
46 * that both that copyright notice and this permission notice appear
47 * in supporting documentation. Silicon Graphics makes no
48 * representations about the suitability of this software for any
49 * purpose. It is provided "as is" without express or implied warranty.
52 /** @file stl_algobase.h
53 * This is an internal header file, included by other library headers.
54 * You should not attempt to use it directly.
57 #ifndef _STL_ALGOBASE_H
58 #define _STL_ALGOBASE_H 1
60 #include <bits/c++config.h>
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 _GLIBCXX_BEGIN_NAMESPACE(std)
75 // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
76 // nutshell, we are partially implementing the resolution of DR 187,
77 // when it's safe, i.e., the value_types are equal.
78 template<bool _BoolType>
79 struct __iter_swap
81 template<typename _ForwardIterator1, typename _ForwardIterator2>
82 static void
83 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
85 typedef typename iterator_traits<_ForwardIterator1>::value_type
86 _ValueType1;
87 _ValueType1 __tmp = _GLIBCXX_MOVE(*__a);
88 *__a = _GLIBCXX_MOVE(*__b);
89 *__b = _GLIBCXX_MOVE(__tmp);
93 template<>
94 struct __iter_swap<true>
96 template<typename _ForwardIterator1, typename _ForwardIterator2>
97 static void
98 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
100 swap(*__a, *__b);
105 * @brief Swaps the contents of two iterators.
106 * @ingroup mutating_algorithms
107 * @param a An iterator.
108 * @param b Another iterator.
109 * @return Nothing.
111 * This function swaps the values pointed to by two iterators, not the
112 * iterators themselves.
114 template<typename _ForwardIterator1, typename _ForwardIterator2>
115 inline void
116 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
118 typedef typename iterator_traits<_ForwardIterator1>::value_type
119 _ValueType1;
120 typedef typename iterator_traits<_ForwardIterator2>::value_type
121 _ValueType2;
123 // concept requirements
124 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
125 _ForwardIterator1>)
126 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
127 _ForwardIterator2>)
128 __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
129 _ValueType2>)
130 __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
131 _ValueType1>)
133 typedef typename iterator_traits<_ForwardIterator1>::reference
134 _ReferenceType1;
135 typedef typename iterator_traits<_ForwardIterator2>::reference
136 _ReferenceType2;
137 std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value
138 && __are_same<_ValueType1&, _ReferenceType1>::__value
139 && __are_same<_ValueType2&, _ReferenceType2>::__value>::
140 iter_swap(__a, __b);
144 * @brief Swap the elements of two sequences.
145 * @ingroup mutating_algorithms
146 * @param first1 A forward iterator.
147 * @param last1 A forward iterator.
148 * @param first2 A forward iterator.
149 * @return An iterator equal to @p first2+(last1-first1).
151 * Swaps each element in the range @p [first1,last1) with the
152 * corresponding element in the range @p [first2,(last1-first1)).
153 * The ranges must not overlap.
155 template<typename _ForwardIterator1, typename _ForwardIterator2>
156 _ForwardIterator2
157 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
158 _ForwardIterator2 __first2)
160 // concept requirements
161 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
162 _ForwardIterator1>)
163 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
164 _ForwardIterator2>)
165 __glibcxx_requires_valid_range(__first1, __last1);
167 for (; __first1 != __last1; ++__first1, ++__first2)
168 std::iter_swap(__first1, __first2);
169 return __first2;
173 * @brief This does what you think it does.
174 * @ingroup sorting_algorithms
175 * @param a A thing of arbitrary type.
176 * @param b Another thing of arbitrary type.
177 * @return The lesser of the parameters.
179 * This is the simple classic generic implementation. It will work on
180 * temporary expressions, since they are only evaluated once, unlike a
181 * preprocessor macro.
183 template<typename _Tp>
184 inline const _Tp&
185 min(const _Tp& __a, const _Tp& __b)
187 // concept requirements
188 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
189 //return __b < __a ? __b : __a;
190 if (__b < __a)
191 return __b;
192 return __a;
196 * @brief This does what you think it does.
197 * @ingroup sorting_algorithms
198 * @param a A thing of arbitrary type.
199 * @param b Another thing of arbitrary type.
200 * @return The greater of the parameters.
202 * This is the simple classic generic implementation. It will work on
203 * temporary expressions, since they are only evaluated once, unlike a
204 * preprocessor macro.
206 template<typename _Tp>
207 inline const _Tp&
208 max(const _Tp& __a, const _Tp& __b)
210 // concept requirements
211 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
212 //return __a < __b ? __b : __a;
213 if (__a < __b)
214 return __b;
215 return __a;
219 * @brief This does what you think it does.
220 * @ingroup sorting_algorithms
221 * @param a A thing of arbitrary type.
222 * @param b Another thing of arbitrary type.
223 * @param comp A @link comparison_functors comparison functor@endlink.
224 * @return The lesser of the parameters.
226 * This will work on temporary expressions, since they are only evaluated
227 * once, unlike a preprocessor macro.
229 template<typename _Tp, typename _Compare>
230 inline const _Tp&
231 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
233 //return __comp(__b, __a) ? __b : __a;
234 if (__comp(__b, __a))
235 return __b;
236 return __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>
251 inline const _Tp&
252 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
254 //return __comp(__a, __b) ? __b : __a;
255 if (__comp(__a, __b))
256 return __b;
257 return __a;
260 // If _Iterator is a __normal_iterator return its base (a plain pointer,
261 // normally) otherwise return it untouched. See copy, fill, ...
262 template<typename _Iterator>
263 struct _Niter_base
264 : _Iter_base<_Iterator, __is_normal_iterator<_Iterator>::__value>
265 { };
267 template<typename _Iterator>
268 inline typename _Niter_base<_Iterator>::iterator_type
269 __niter_base(_Iterator __it)
270 { return std::_Niter_base<_Iterator>::_S_base(__it); }
272 // Likewise, for move_iterator.
273 template<typename _Iterator>
274 struct _Miter_base
275 : _Iter_base<_Iterator, __is_move_iterator<_Iterator>::__value>
276 { };
278 template<typename _Iterator>
279 inline typename _Miter_base<_Iterator>::iterator_type
280 __miter_base(_Iterator __it)
281 { return std::_Miter_base<_Iterator>::_S_base(__it); }
283 // All of these auxiliary structs serve two purposes. (1) Replace
284 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
285 // because the input and output ranges are permitted to overlap.)
286 // (2) If we're using random access iterators, then write the loop as
287 // a for loop with an explicit count.
289 template<bool, bool, typename>
290 struct __copy_move
292 template<typename _II, typename _OI>
293 static _OI
294 __copy_m(_II __first, _II __last, _OI __result)
296 for (; __first != __last; ++__result, ++__first)
297 *__result = *__first;
298 return __result;
302 #ifdef __GXX_EXPERIMENTAL_CXX0X__
303 template<typename _Category>
304 struct __copy_move<true, false, _Category>
306 template<typename _II, typename _OI>
307 static _OI
308 __copy_m(_II __first, _II __last, _OI __result)
310 for (; __first != __last; ++__result, ++__first)
311 *__result = std::move(*__first);
312 return __result;
315 #endif
317 template<>
318 struct __copy_move<false, false, random_access_iterator_tag>
320 template<typename _II, typename _OI>
321 static _OI
322 __copy_m(_II __first, _II __last, _OI __result)
324 typedef typename iterator_traits<_II>::difference_type _Distance;
325 for(_Distance __n = __last - __first; __n > 0; --__n)
327 *__result = *__first;
328 ++__first;
329 ++__result;
331 return __result;
335 #ifdef __GXX_EXPERIMENTAL_CXX0X__
336 template<>
337 struct __copy_move<true, false, random_access_iterator_tag>
339 template<typename _II, typename _OI>
340 static _OI
341 __copy_m(_II __first, _II __last, _OI __result)
343 typedef typename iterator_traits<_II>::difference_type _Distance;
344 for(_Distance __n = __last - __first; __n > 0; --__n)
346 *__result = std::move(*__first);
347 ++__first;
348 ++__result;
350 return __result;
353 #endif
355 template<bool _IsMove>
356 struct __copy_move<_IsMove, true, random_access_iterator_tag>
358 template<typename _Tp>
359 static _Tp*
360 __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
362 const ptrdiff_t _Num = __last - __first;
363 if (_Num)
364 __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
365 return __result + _Num;
369 template<bool _IsMove, typename _II, typename _OI>
370 inline _OI
371 __copy_move_a(_II __first, _II __last, _OI __result)
373 typedef typename iterator_traits<_II>::value_type _ValueTypeI;
374 typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
375 typedef typename iterator_traits<_II>::iterator_category _Category;
376 const bool __simple = (__is_trivial(_ValueTypeI)
377 && __is_pointer<_II>::__value
378 && __is_pointer<_OI>::__value
379 && __are_same<_ValueTypeI, _ValueTypeO>::__value);
381 return std::__copy_move<_IsMove, __simple,
382 _Category>::__copy_m(__first, __last, __result);
385 // Helpers for streambuf iterators (either istream or ostream).
386 // NB: avoid including <iosfwd>, relatively large.
387 template<typename _CharT>
388 struct char_traits;
390 template<typename _CharT, typename _Traits>
391 class istreambuf_iterator;
393 template<typename _CharT, typename _Traits>
394 class ostreambuf_iterator;
396 template<bool _IsMove, typename _CharT>
397 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
398 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
399 __copy_move_a2(_CharT*, _CharT*,
400 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
402 template<bool _IsMove, typename _CharT>
403 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
404 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
405 __copy_move_a2(const _CharT*, const _CharT*,
406 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
408 template<bool _IsMove, typename _CharT>
409 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
410 _CharT*>::__type
411 __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >,
412 istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
414 template<bool _IsMove, typename _II, typename _OI>
415 inline _OI
416 __copy_move_a2(_II __first, _II __last, _OI __result)
418 return _OI(std::__copy_move_a<_IsMove>(std::__niter_base(__first),
419 std::__niter_base(__last),
420 std::__niter_base(__result)));
424 * @brief Copies the range [first,last) into result.
425 * @ingroup mutating_algorithms
426 * @param first An input iterator.
427 * @param last An input iterator.
428 * @param result An output iterator.
429 * @return result + (first - last)
431 * This inline function will boil down to a call to @c memmove whenever
432 * possible. Failing that, if random access iterators are passed, then the
433 * loop count will be known (and therefore a candidate for compiler
434 * optimizations such as unrolling). Result may not be contained within
435 * [first,last); the copy_backward function should be used instead.
437 * Note that the end of the output range is permitted to be contained
438 * within [first,last).
440 template<typename _II, typename _OI>
441 inline _OI
442 copy(_II __first, _II __last, _OI __result)
444 // concept requirements
445 __glibcxx_function_requires(_InputIteratorConcept<_II>)
446 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
447 typename iterator_traits<_II>::value_type>)
448 __glibcxx_requires_valid_range(__first, __last);
450 return (std::__copy_move_a2<__is_move_iterator<_II>::__value>
451 (std::__miter_base(__first), std::__miter_base(__last),
452 __result));
455 #ifdef __GXX_EXPERIMENTAL_CXX0X__
457 * @brief Moves the range [first,last) into result.
458 * @ingroup mutating_algorithms
459 * @param first An input iterator.
460 * @param last An input iterator.
461 * @param result An output iterator.
462 * @return result + (first - last)
464 * This inline function will boil down to a call to @c memmove whenever
465 * possible. Failing that, if random access iterators are passed, then the
466 * loop count will be known (and therefore a candidate for compiler
467 * optimizations such as unrolling). Result may not be contained within
468 * [first,last); the move_backward function should be used instead.
470 * Note that the end of the output range is permitted to be contained
471 * within [first,last).
473 template<typename _II, typename _OI>
474 inline _OI
475 move(_II __first, _II __last, _OI __result)
477 // concept requirements
478 __glibcxx_function_requires(_InputIteratorConcept<_II>)
479 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
480 typename iterator_traits<_II>::value_type>)
481 __glibcxx_requires_valid_range(__first, __last);
483 return std::__copy_move_a2<true>(std::__miter_base(__first),
484 std::__miter_base(__last), __result);
487 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
488 #else
489 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
490 #endif
492 template<bool, bool, typename>
493 struct __copy_move_backward
495 template<typename _BI1, typename _BI2>
496 static _BI2
497 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
499 while (__first != __last)
500 *--__result = *--__last;
501 return __result;
505 #ifdef __GXX_EXPERIMENTAL_CXX0X__
506 template<typename _Category>
507 struct __copy_move_backward<true, false, _Category>
509 template<typename _BI1, typename _BI2>
510 static _BI2
511 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
513 while (__first != __last)
514 *--__result = std::move(*--__last);
515 return __result;
518 #endif
520 template<>
521 struct __copy_move_backward<false, false, random_access_iterator_tag>
523 template<typename _BI1, typename _BI2>
524 static _BI2
525 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
527 typename iterator_traits<_BI1>::difference_type __n;
528 for (__n = __last - __first; __n > 0; --__n)
529 *--__result = *--__last;
530 return __result;
534 #ifdef __GXX_EXPERIMENTAL_CXX0X__
535 template<>
536 struct __copy_move_backward<true, false, random_access_iterator_tag>
538 template<typename _BI1, typename _BI2>
539 static _BI2
540 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
542 typename iterator_traits<_BI1>::difference_type __n;
543 for (__n = __last - __first; __n > 0; --__n)
544 *--__result = std::move(*--__last);
545 return __result;
548 #endif
550 template<bool _IsMove>
551 struct __copy_move_backward<_IsMove, true, random_access_iterator_tag>
553 template<typename _Tp>
554 static _Tp*
555 __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
557 const ptrdiff_t _Num = __last - __first;
558 if (_Num)
559 __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
560 return __result - _Num;
564 template<bool _IsMove, typename _BI1, typename _BI2>
565 inline _BI2
566 __copy_move_backward_a(_BI1 __first, _BI1 __last, _BI2 __result)
568 typedef typename iterator_traits<_BI1>::value_type _ValueType1;
569 typedef typename iterator_traits<_BI2>::value_type _ValueType2;
570 typedef typename iterator_traits<_BI1>::iterator_category _Category;
571 const bool __simple = (__is_trivial(_ValueType1)
572 && __is_pointer<_BI1>::__value
573 && __is_pointer<_BI2>::__value
574 && __are_same<_ValueType1, _ValueType2>::__value);
576 return std::__copy_move_backward<_IsMove, __simple,
577 _Category>::__copy_move_b(__first,
578 __last,
579 __result);
582 template<bool _IsMove, typename _BI1, typename _BI2>
583 inline _BI2
584 __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result)
586 return _BI2(std::__copy_move_backward_a<_IsMove>
587 (std::__niter_base(__first), std::__niter_base(__last),
588 std::__niter_base(__result)));
592 * @brief Copies the range [first,last) into result.
593 * @ingroup mutating_algorithms
594 * @param first A bidirectional iterator.
595 * @param last A bidirectional iterator.
596 * @param result A bidirectional iterator.
597 * @return result - (first - last)
599 * The function has the same effect as copy, but starts at the end of the
600 * range and works its way to the start, returning the start of the result.
601 * This inline function will boil down to a call to @c memmove whenever
602 * possible. Failing that, if random access iterators are passed, then the
603 * loop count will be known (and therefore a candidate for compiler
604 * optimizations such as unrolling).
606 * Result may not be in the range [first,last). Use copy instead. Note
607 * that the start of the output range may overlap [first,last).
609 template<typename _BI1, typename _BI2>
610 inline _BI2
611 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
613 // concept requirements
614 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
615 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
616 __glibcxx_function_requires(_ConvertibleConcept<
617 typename iterator_traits<_BI1>::value_type,
618 typename iterator_traits<_BI2>::value_type>)
619 __glibcxx_requires_valid_range(__first, __last);
621 return (std::__copy_move_backward_a2<__is_move_iterator<_BI1>::__value>
622 (std::__miter_base(__first), std::__miter_base(__last),
623 __result));
626 #ifdef __GXX_EXPERIMENTAL_CXX0X__
628 * @brief Moves the range [first,last) into result.
629 * @ingroup mutating_algorithms
630 * @param first A bidirectional iterator.
631 * @param last A bidirectional iterator.
632 * @param result A bidirectional iterator.
633 * @return result - (first - last)
635 * The function has the same effect as move, but starts at the end of the
636 * range and works its way to the start, returning the start of the result.
637 * This inline function will boil down to a call to @c memmove whenever
638 * possible. Failing that, if random access iterators are passed, then the
639 * loop count will be known (and therefore a candidate for compiler
640 * optimizations such as unrolling).
642 * Result may not be in the range [first,last). Use move instead. Note
643 * that the start of the output range may overlap [first,last).
645 template<typename _BI1, typename _BI2>
646 inline _BI2
647 move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
649 // concept requirements
650 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
651 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
652 __glibcxx_function_requires(_ConvertibleConcept<
653 typename iterator_traits<_BI1>::value_type,
654 typename iterator_traits<_BI2>::value_type>)
655 __glibcxx_requires_valid_range(__first, __last);
657 return std::__copy_move_backward_a2<true>(std::__miter_base(__first),
658 std::__miter_base(__last),
659 __result);
662 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
663 #else
664 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
665 #endif
667 template<typename _ForwardIterator, typename _Tp>
668 inline typename
669 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
670 __fill_a(_ForwardIterator __first, _ForwardIterator __last,
671 const _Tp& __value)
673 for (; __first != __last; ++__first)
674 *__first = __value;
677 template<typename _ForwardIterator, typename _Tp>
678 inline typename
679 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
680 __fill_a(_ForwardIterator __first, _ForwardIterator __last,
681 const _Tp& __value)
683 const _Tp __tmp = __value;
684 for (; __first != __last; ++__first)
685 *__first = __tmp;
688 // Specialization: for char types we can use memset.
689 template<typename _Tp>
690 inline typename
691 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type
692 __fill_a(_Tp* __first, _Tp* __last, const _Tp& __c)
694 const _Tp __tmp = __c;
695 __builtin_memset(__first, static_cast<unsigned char>(__tmp),
696 __last - __first);
700 * @brief Fills the range [first,last) with copies of value.
701 * @ingroup mutating_algorithms
702 * @param first A forward iterator.
703 * @param last A forward iterator.
704 * @param value A reference-to-const of arbitrary type.
705 * @return Nothing.
707 * This function fills a range with copies of the same value. For char
708 * types filling contiguous areas of memory, this becomes an inline call
709 * to @c memset or @c wmemset.
711 template<typename _ForwardIterator, typename _Tp>
712 inline void
713 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
715 // concept requirements
716 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
717 _ForwardIterator>)
718 __glibcxx_requires_valid_range(__first, __last);
720 std::__fill_a(std::__niter_base(__first), std::__niter_base(__last),
721 __value);
724 template<typename _OutputIterator, typename _Size, typename _Tp>
725 inline typename
726 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
727 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
729 for (__decltype(__n + 0) __niter = __n;
730 __niter > 0; --__niter, ++__first)
731 *__first = __value;
732 return __first;
735 template<typename _OutputIterator, typename _Size, typename _Tp>
736 inline typename
737 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
738 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
740 const _Tp __tmp = __value;
741 for (__decltype(__n + 0) __niter = __n;
742 __niter > 0; --__niter, ++__first)
743 *__first = __tmp;
744 return __first;
747 template<typename _Size, typename _Tp>
748 inline typename
749 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, _Tp*>::__type
750 __fill_n_a(_Tp* __first, _Size __n, const _Tp& __c)
752 std::__fill_a(__first, __first + __n, __c);
753 return __first + __n;
757 * @brief Fills the range [first,first+n) with copies of value.
758 * @ingroup mutating_algorithms
759 * @param first An output iterator.
760 * @param n The count of copies to perform.
761 * @param value A reference-to-const of arbitrary type.
762 * @return The iterator at first+n.
764 * This function fills a range with copies of the same value. For char
765 * types filling contiguous areas of memory, this becomes an inline call
766 * to @c memset or @ wmemset.
768 * _GLIBCXX_RESOLVE_LIB_DEFECTS
769 * DR 865. More algorithms that throw away information
771 template<typename _OI, typename _Size, typename _Tp>
772 inline _OI
773 fill_n(_OI __first, _Size __n, const _Tp& __value)
775 // concept requirements
776 __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>)
778 return _OI(std::__fill_n_a(std::__niter_base(__first), __n, __value));
781 template<bool _BoolType>
782 struct __equal
784 template<typename _II1, typename _II2>
785 static bool
786 equal(_II1 __first1, _II1 __last1, _II2 __first2)
788 for (; __first1 != __last1; ++__first1, ++__first2)
789 if (!(*__first1 == *__first2))
790 return false;
791 return true;
795 template<>
796 struct __equal<true>
798 template<typename _Tp>
799 static bool
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>
808 inline bool
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>
823 struct __lc_rai
825 template<typename _II1, typename _II2>
826 static _II1
827 __newlast1(_II1, _II1 __last1, _II2, _II2)
828 { return __last1; }
830 template<typename _II>
831 static bool
832 __cnd2(_II __first, _II __last)
833 { return __first != __last; }
836 template<>
837 struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag>
839 template<typename _RAI1, typename _RAI2>
840 static _RAI1
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>
852 static bool
853 __cnd2(_RAI, _RAI)
854 { return true; }
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>
866 bool
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,
875 __first2, __last2);
876 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
877 ++__first1, ++__first2)
879 if (*__first1 < *__first2)
880 return true;
881 if (*__first2 < *__first1)
882 return false;
884 return __first1 == __last1 && __first2 != __last2;
887 template<>
888 struct __lexicographical_compare<true>
890 template<typename _Tp, typename _Up>
891 static bool
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>
904 inline bool
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,
918 __first2, __last2);
922 * @brief Finds the first position in which @a val could be inserted
923 * without changing the ordering.
924 * @param first An iterator.
925 * @param last Another iterator.
926 * @param val The search term.
927 * @return An iterator pointing to the first element <em>not less
928 * than</em> @a val, or end() if every element is less than
929 * @a val.
930 * @ingroup binary_search_algorithms
932 template<typename _ForwardIterator, typename _Tp>
933 _ForwardIterator
934 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
935 const _Tp& __val)
937 typedef typename iterator_traits<_ForwardIterator>::value_type
938 _ValueType;
939 typedef typename iterator_traits<_ForwardIterator>::difference_type
940 _DistanceType;
942 // concept requirements
943 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
944 __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
945 __glibcxx_requires_partitioned_lower(__first, __last, __val);
947 _DistanceType __len = std::distance(__first, __last);
949 while (__len > 0)
951 _DistanceType __half = __len >> 1;
952 _ForwardIterator __middle = __first;
953 std::advance(__middle, __half);
954 if (*__middle < __val)
956 __first = __middle;
957 ++__first;
958 __len = __len - __half - 1;
960 else
961 __len = __half;
963 return __first;
966 /// This is a helper function for the sort routines and for random.tcc.
967 // Precondition: __n > 0.
968 template<typename _Size>
969 inline _Size
970 __lg(_Size __n)
972 _Size __k;
973 for (__k = 0; __n != 0; __n >>= 1)
974 ++__k;
975 return __k - 1;
978 inline int
979 __lg(int __n)
980 { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
982 inline long
983 __lg(long __n)
984 { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
986 inline long long
987 __lg(long long __n)
988 { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
990 _GLIBCXX_END_NAMESPACE
992 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
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
1004 * ranges are equal.
1006 template<typename _II1, typename _II2>
1007 inline bool
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
1030 * functor@endlink.
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
1036 * ranges are equal.
1038 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1039 inline bool
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)))
1050 return false;
1051 return true;
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>
1070 inline bool
1071 lexicographical_compare(_II1 __first1, _II1 __last1,
1072 _II2 __first2, _II2 __last2)
1074 // concept requirements
1075 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1076 typedef typename iterator_traits<_II2>::value_type _ValueType2;
1077 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1078 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1079 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
1080 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
1081 __glibcxx_requires_valid_range(__first1, __last1);
1082 __glibcxx_requires_valid_range(__first2, __last2);
1084 return std::__lexicographical_compare_aux(std::__niter_base(__first1),
1085 std::__niter_base(__last1),
1086 std::__niter_base(__first2),
1087 std::__niter_base(__last2));
1091 * @brief Performs @b dictionary comparison on ranges.
1092 * @ingroup sorting_algorithms
1093 * @param first1 An input iterator.
1094 * @param last1 An input iterator.
1095 * @param first2 An input iterator.
1096 * @param last2 An input iterator.
1097 * @param comp A @link comparison_functors comparison functor@endlink.
1098 * @return A boolean true or false.
1100 * The same as the four-parameter @c lexicographical_compare, but uses the
1101 * comp parameter instead of @c <.
1103 template<typename _II1, typename _II2, typename _Compare>
1104 bool
1105 lexicographical_compare(_II1 __first1, _II1 __last1,
1106 _II2 __first2, _II2 __last2, _Compare __comp)
1108 typedef typename iterator_traits<_II1>::iterator_category _Category1;
1109 typedef typename iterator_traits<_II2>::iterator_category _Category2;
1110 typedef std::__lc_rai<_Category1, _Category2> __rai_type;
1112 // concept requirements
1113 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1114 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1115 __glibcxx_requires_valid_range(__first1, __last1);
1116 __glibcxx_requires_valid_range(__first2, __last2);
1118 __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
1119 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
1120 ++__first1, ++__first2)
1122 if (__comp(*__first1, *__first2))
1123 return true;
1124 if (__comp(*__first2, *__first1))
1125 return false;
1127 return __first1 == __last1 && __first2 != __last2;
1131 * @brief Finds the places in ranges which don't match.
1132 * @ingroup non_mutating_algorithms
1133 * @param first1 An input iterator.
1134 * @param last1 An input iterator.
1135 * @param first2 An input iterator.
1136 * @return A pair of iterators pointing to the first mismatch.
1138 * This compares the elements of two ranges using @c == and returns a pair
1139 * of iterators. The first iterator points into the first range, the
1140 * second iterator points into the second range, and the elements pointed
1141 * to by the iterators are not equal.
1143 template<typename _InputIterator1, typename _InputIterator2>
1144 pair<_InputIterator1, _InputIterator2>
1145 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1146 _InputIterator2 __first2)
1148 // concept requirements
1149 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1150 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1151 __glibcxx_function_requires(_EqualOpConcept<
1152 typename iterator_traits<_InputIterator1>::value_type,
1153 typename iterator_traits<_InputIterator2>::value_type>)
1154 __glibcxx_requires_valid_range(__first1, __last1);
1156 while (__first1 != __last1 && *__first1 == *__first2)
1158 ++__first1;
1159 ++__first2;
1161 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1165 * @brief Finds the places in ranges which don't match.
1166 * @ingroup non_mutating_algorithms
1167 * @param first1 An input iterator.
1168 * @param last1 An input iterator.
1169 * @param first2 An input iterator.
1170 * @param binary_pred A binary predicate @link functors
1171 * functor@endlink.
1172 * @return A pair of iterators pointing to the first mismatch.
1174 * This compares the elements of two ranges using the binary_pred
1175 * parameter, and returns a pair
1176 * of iterators. The first iterator points into the first range, the
1177 * second iterator points into the second range, and the elements pointed
1178 * to by the iterators are not equal.
1180 template<typename _InputIterator1, typename _InputIterator2,
1181 typename _BinaryPredicate>
1182 pair<_InputIterator1, _InputIterator2>
1183 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1184 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1186 // concept requirements
1187 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1188 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1189 __glibcxx_requires_valid_range(__first1, __last1);
1191 while (__first1 != __last1 && bool(__binary_pred(*__first1, *__first2)))
1193 ++__first1;
1194 ++__first2;
1196 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1199 _GLIBCXX_END_NESTED_NAMESPACE
1201 // NB: This file is included within many other C++ includes, as a way
1202 // of getting the base algorithms. So, make sure that parallel bits
1203 // come in too if requested.
1204 #ifdef _GLIBCXX_PARALLEL
1205 # include <parallel/algobase.h>
1206 #endif
1208 #endif