Merged revisions 143552,143554,143557,143560,143562,143564-143567,143570-143573,14357...
[official-gcc.git] / libstdc++-v3 / include / bits / stl_algobase.h
blobf370677da1c6a2901a248350fc0e790ce5b56aba
1 // Core algorithmic facilities -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 2, 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 // 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,
20 // USA.
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.
33 * Copyright (c) 1994
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>
66 #include <cstddef>
67 #include <bits/functexcept.h>
68 #include <bits/cpp_type_traits.h>
69 #include <ext/type_traits.h>
70 #include <ext/numeric_traits.h>
71 #include <bits/stl_pair.h>
72 #include <bits/stl_iterator_base_types.h>
73 #include <bits/stl_iterator_base_funcs.h>
74 #include <bits/stl_iterator.h>
75 #include <bits/concept_check.h>
76 #include <debug/debug.h>
77 #include <bits/move.h> // For std::swap and _GLIBCXX_MOVE
79 _GLIBCXX_BEGIN_NAMESPACE(std)
81 // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
82 // nutshell, we are partially implementing the resolution of DR 187,
83 // when it's safe, i.e., the value_types are equal.
84 template<bool _BoolType>
85 struct __iter_swap
87 template<typename _ForwardIterator1, typename _ForwardIterator2>
88 static void
89 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
91 typedef typename iterator_traits<_ForwardIterator1>::value_type
92 _ValueType1;
93 _ValueType1 __tmp = _GLIBCXX_MOVE(*__a);
94 *__a = _GLIBCXX_MOVE(*__b);
95 *__b = _GLIBCXX_MOVE(__tmp);
99 template<>
100 struct __iter_swap<true>
102 template<typename _ForwardIterator1, typename _ForwardIterator2>
103 static void
104 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
106 swap(*__a, *__b);
111 * @brief Swaps the contents of two iterators.
112 * @ingroup mutating_algorithms
113 * @param a An iterator.
114 * @param b Another iterator.
115 * @return Nothing.
117 * This function swaps the values pointed to by two iterators, not the
118 * iterators themselves.
120 template<typename _ForwardIterator1, typename _ForwardIterator2>
121 inline void
122 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
124 typedef typename iterator_traits<_ForwardIterator1>::value_type
125 _ValueType1;
126 typedef typename iterator_traits<_ForwardIterator2>::value_type
127 _ValueType2;
129 // concept requirements
130 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
131 _ForwardIterator1>)
132 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
133 _ForwardIterator2>)
134 __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
135 _ValueType2>)
136 __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
137 _ValueType1>)
139 typedef typename iterator_traits<_ForwardIterator1>::reference
140 _ReferenceType1;
141 typedef typename iterator_traits<_ForwardIterator2>::reference
142 _ReferenceType2;
143 std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value
144 && __are_same<_ValueType1&, _ReferenceType1>::__value
145 && __are_same<_ValueType2&, _ReferenceType2>::__value>::
146 iter_swap(__a, __b);
150 * @brief Swap the elements of two sequences.
151 * @ingroup mutating_algorithms
152 * @param first1 A forward iterator.
153 * @param last1 A forward iterator.
154 * @param first2 A forward iterator.
155 * @return An iterator equal to @p first2+(last1-first1).
157 * Swaps each element in the range @p [first1,last1) with the
158 * corresponding element in the range @p [first2,(last1-first1)).
159 * The ranges must not overlap.
161 template<typename _ForwardIterator1, typename _ForwardIterator2>
162 _ForwardIterator2
163 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
164 _ForwardIterator2 __first2)
166 // concept requirements
167 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
168 _ForwardIterator1>)
169 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
170 _ForwardIterator2>)
171 __glibcxx_requires_valid_range(__first1, __last1);
173 for (; __first1 != __last1; ++__first1, ++__first2)
174 std::iter_swap(__first1, __first2);
175 return __first2;
179 * @brief This does what you think it does.
180 * @ingroup sorting_algorithms
181 * @param a A thing of arbitrary type.
182 * @param b Another thing of arbitrary type.
183 * @return The lesser of the parameters.
185 * This is the simple classic generic implementation. It will work on
186 * temporary expressions, since they are only evaluated once, unlike a
187 * preprocessor macro.
189 template<typename _Tp>
190 inline const _Tp&
191 min(const _Tp& __a, const _Tp& __b)
193 // concept requirements
194 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
195 //return __b < __a ? __b : __a;
196 if (__b < __a)
197 return __b;
198 return __a;
202 * @brief This does what you think it does.
203 * @ingroup sorting_algorithms
204 * @param a A thing of arbitrary type.
205 * @param b Another thing of arbitrary type.
206 * @return The greater of the parameters.
208 * This is the simple classic generic implementation. It will work on
209 * temporary expressions, since they are only evaluated once, unlike a
210 * preprocessor macro.
212 template<typename _Tp>
213 inline const _Tp&
214 max(const _Tp& __a, const _Tp& __b)
216 // concept requirements
217 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
218 //return __a < __b ? __b : __a;
219 if (__a < __b)
220 return __b;
221 return __a;
225 * @brief This does what you think it does.
226 * @ingroup sorting_algorithms
227 * @param a A thing of arbitrary type.
228 * @param b Another thing of arbitrary type.
229 * @param comp A @link comparison_functors comparison functor@endlink.
230 * @return The lesser of the parameters.
232 * This will work on temporary expressions, since they are only evaluated
233 * once, unlike a preprocessor macro.
235 template<typename _Tp, typename _Compare>
236 inline const _Tp&
237 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
239 //return __comp(__b, __a) ? __b : __a;
240 if (__comp(__b, __a))
241 return __b;
242 return __a;
246 * @brief This does what you think it does.
247 * @ingroup sorting_algorithms
248 * @param a A thing of arbitrary type.
249 * @param b Another thing of arbitrary type.
250 * @param comp A @link comparison_functors comparison functor@endlink.
251 * @return The greater of the parameters.
253 * This will work on temporary expressions, since they are only evaluated
254 * once, unlike a preprocessor macro.
256 template<typename _Tp, typename _Compare>
257 inline const _Tp&
258 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
260 //return __comp(__a, __b) ? __b : __a;
261 if (__comp(__a, __b))
262 return __b;
263 return __a;
267 // If _Iterator is a __normal_iterator return its base (a plain pointer,
268 // normally) otherwise return it untouched. See copy, fill, ...
269 template<typename _Iterator,
270 bool _IsNormal = __is_normal_iterator<_Iterator>::__value>
271 struct __niter_base
273 static _Iterator
274 __b(_Iterator __it)
275 { return __it; }
278 template<typename _Iterator>
279 struct __niter_base<_Iterator, true>
281 static typename _Iterator::iterator_type
282 __b(_Iterator __it)
283 { return __it.base(); }
286 // Likewise, for move_iterator.
287 template<typename _Iterator,
288 bool _IsMove = __is_move_iterator<_Iterator>::__value>
289 struct __miter_base
291 static _Iterator
292 __b(_Iterator __it)
293 { return __it; }
296 template<typename _Iterator>
297 struct __miter_base<_Iterator, true>
299 static typename _Iterator::iterator_type
300 __b(_Iterator __it)
301 { return __it.base(); }
304 // All of these auxiliary structs serve two purposes. (1) Replace
305 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
306 // because the input and output ranges are permitted to overlap.)
307 // (2) If we're using random access iterators, then write the loop as
308 // a for loop with an explicit count.
310 template<bool, bool, typename>
311 struct __copy_move
313 template<typename _II, typename _OI>
314 static _OI
315 __copy_m(_II __first, _II __last, _OI __result)
317 for (; __first != __last; ++__result, ++__first)
318 *__result = *__first;
319 return __result;
323 #ifdef __GXX_EXPERIMENTAL_CXX0X__
324 template<typename _Category>
325 struct __copy_move<true, false, _Category>
327 template<typename _II, typename _OI>
328 static _OI
329 __copy_m(_II __first, _II __last, _OI __result)
331 for (; __first != __last; ++__result, ++__first)
332 *__result = std::move(*__first);
333 return __result;
336 #endif
338 template<>
339 struct __copy_move<false, false, random_access_iterator_tag>
341 template<typename _II, typename _OI>
342 static _OI
343 __copy_m(_II __first, _II __last, _OI __result)
345 typedef typename iterator_traits<_II>::difference_type _Distance;
346 for(_Distance __n = __last - __first; __n > 0; --__n)
348 *__result = *__first;
349 ++__first;
350 ++__result;
352 return __result;
356 #ifdef __GXX_EXPERIMENTAL_CXX0X__
357 template<>
358 struct __copy_move<true, false, random_access_iterator_tag>
360 template<typename _II, typename _OI>
361 static _OI
362 __copy_m(_II __first, _II __last, _OI __result)
364 typedef typename iterator_traits<_II>::difference_type _Distance;
365 for(_Distance __n = __last - __first; __n > 0; --__n)
367 *__result = std::move(*__first);
368 ++__first;
369 ++__result;
371 return __result;
374 #endif
376 template<bool _IsMove>
377 struct __copy_move<_IsMove, true, random_access_iterator_tag>
379 template<typename _Tp>
380 static _Tp*
381 __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
383 __builtin_memmove(__result, __first,
384 sizeof(_Tp) * (__last - __first));
385 return __result + (__last - __first);
389 template<bool _IsMove, typename _II, typename _OI>
390 inline _OI
391 __copy_move_a(_II __first, _II __last, _OI __result)
393 typedef typename iterator_traits<_II>::value_type _ValueTypeI;
394 typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
395 typedef typename iterator_traits<_II>::iterator_category _Category;
396 const bool __simple = (__is_pod(_ValueTypeI)
397 && __is_pointer<_II>::__value
398 && __is_pointer<_OI>::__value
399 && __are_same<_ValueTypeI, _ValueTypeO>::__value);
401 return std::__copy_move<_IsMove, __simple,
402 _Category>::__copy_m(__first, __last, __result);
405 // Helpers for streambuf iterators (either istream or ostream).
406 // NB: avoid including <iosfwd>, relatively large.
407 template<typename _CharT>
408 struct char_traits;
410 template<typename _CharT, typename _Traits>
411 class istreambuf_iterator;
413 template<typename _CharT, typename _Traits>
414 class ostreambuf_iterator;
416 template<bool _IsMove, typename _CharT>
417 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
418 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
419 __copy_move_a2(_CharT*, _CharT*,
420 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
422 template<bool _IsMove, typename _CharT>
423 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
424 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
425 __copy_move_a2(const _CharT*, const _CharT*,
426 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
428 template<bool _IsMove, typename _CharT>
429 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
430 _CharT*>::__type
431 __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >,
432 istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
434 template<bool _IsMove, typename _II, typename _OI>
435 inline _OI
436 __copy_move_a2(_II __first, _II __last, _OI __result)
438 return _OI(std::__copy_move_a<_IsMove>
439 (std::__niter_base<_II>::__b(__first),
440 std::__niter_base<_II>::__b(__last),
441 std::__niter_base<_OI>::__b(__result)));
445 * @brief Copies the range [first,last) into result.
446 * @ingroup mutating_algorithms
447 * @param first An input iterator.
448 * @param last An input iterator.
449 * @param result An output iterator.
450 * @return result + (first - last)
452 * This inline function will boil down to a call to @c memmove whenever
453 * possible. Failing that, if random access iterators are passed, then the
454 * loop count will be known (and therefore a candidate for compiler
455 * optimizations such as unrolling). Result may not be contained within
456 * [first,last); the copy_backward function should be used instead.
458 * Note that the end of the output range is permitted to be contained
459 * within [first,last).
461 template<typename _II, typename _OI>
462 inline _OI
463 copy(_II __first, _II __last, _OI __result)
465 // concept requirements
466 __glibcxx_function_requires(_InputIteratorConcept<_II>)
467 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
468 typename iterator_traits<_II>::value_type>)
469 __glibcxx_requires_valid_range(__first, __last);
471 return (std::__copy_move_a2<__is_move_iterator<_II>::__value>
472 (std::__miter_base<_II>::__b(__first),
473 std::__miter_base<_II>::__b(__last), __result));
476 #ifdef __GXX_EXPERIMENTAL_CXX0X__
478 * @brief Moves the range [first,last) into result.
479 * @ingroup mutating_algorithms
480 * @param first An input iterator.
481 * @param last An input iterator.
482 * @param result An output iterator.
483 * @return result + (first - last)
485 * This inline function will boil down to a call to @c memmove whenever
486 * possible. Failing that, if random access iterators are passed, then the
487 * loop count will be known (and therefore a candidate for compiler
488 * optimizations such as unrolling). Result may not be contained within
489 * [first,last); the move_backward function should be used instead.
491 * Note that the end of the output range is permitted to be contained
492 * within [first,last).
494 template<typename _II, typename _OI>
495 inline _OI
496 move(_II __first, _II __last, _OI __result)
498 // concept requirements
499 __glibcxx_function_requires(_InputIteratorConcept<_II>)
500 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
501 typename iterator_traits<_II>::value_type>)
502 __glibcxx_requires_valid_range(__first, __last);
504 return (std::__copy_move_a2<true>
505 (std::__miter_base<_II>::__b(__first),
506 std::__miter_base<_II>::__b(__last), __result));
509 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
510 #else
511 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
512 #endif
514 template<bool, bool, typename>
515 struct __copy_move_backward
517 template<typename _BI1, typename _BI2>
518 static _BI2
519 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
521 while (__first != __last)
522 *--__result = *--__last;
523 return __result;
527 #ifdef __GXX_EXPERIMENTAL_CXX0X__
528 template<typename _Category>
529 struct __copy_move_backward<true, false, _Category>
531 template<typename _BI1, typename _BI2>
532 static _BI2
533 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
535 while (__first != __last)
536 *--__result = std::move(*--__last);
537 return __result;
540 #endif
542 template<>
543 struct __copy_move_backward<false, false, random_access_iterator_tag>
545 template<typename _BI1, typename _BI2>
546 static _BI2
547 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
549 typename iterator_traits<_BI1>::difference_type __n;
550 for (__n = __last - __first; __n > 0; --__n)
551 *--__result = *--__last;
552 return __result;
556 #ifdef __GXX_EXPERIMENTAL_CXX0X__
557 template<>
558 struct __copy_move_backward<true, false, random_access_iterator_tag>
560 template<typename _BI1, typename _BI2>
561 static _BI2
562 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
564 typename iterator_traits<_BI1>::difference_type __n;
565 for (__n = __last - __first; __n > 0; --__n)
566 *--__result = std::move(*--__last);
567 return __result;
570 #endif
572 template<bool _IsMove>
573 struct __copy_move_backward<_IsMove, true, random_access_iterator_tag>
575 template<typename _Tp>
576 static _Tp*
577 __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
579 const ptrdiff_t _Num = __last - __first;
580 __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
581 return __result - _Num;
585 template<bool _IsMove, typename _BI1, typename _BI2>
586 inline _BI2
587 __copy_move_backward_a(_BI1 __first, _BI1 __last, _BI2 __result)
589 typedef typename iterator_traits<_BI1>::value_type _ValueType1;
590 typedef typename iterator_traits<_BI2>::value_type _ValueType2;
591 typedef typename iterator_traits<_BI1>::iterator_category _Category;
592 const bool __simple = (__is_pod(_ValueType1)
593 && __is_pointer<_BI1>::__value
594 && __is_pointer<_BI2>::__value
595 && __are_same<_ValueType1, _ValueType2>::__value);
597 return std::__copy_move_backward<_IsMove, __simple,
598 _Category>::__copy_move_b(__first,
599 __last,
600 __result);
603 template<bool _IsMove, typename _BI1, typename _BI2>
604 inline _BI2
605 __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result)
607 return _BI2(std::__copy_move_backward_a<_IsMove>
608 (std::__niter_base<_BI1>::__b(__first),
609 std::__niter_base<_BI1>::__b(__last),
610 std::__niter_base<_BI2>::__b(__result)));
614 * @brief Copies the range [first,last) into result.
615 * @ingroup mutating_algorithms
616 * @param first A bidirectional iterator.
617 * @param last A bidirectional iterator.
618 * @param result A bidirectional iterator.
619 * @return result - (first - last)
621 * The function has the same effect as copy, but starts at the end of the
622 * range and works its way to the start, returning the start of the result.
623 * This inline function will boil down to a call to @c memmove whenever
624 * possible. Failing that, if random access iterators are passed, then the
625 * loop count will be known (and therefore a candidate for compiler
626 * optimizations such as unrolling).
628 * Result may not be in the range [first,last). Use copy instead. Note
629 * that the start of the output range may overlap [first,last).
631 template<typename _BI1, typename _BI2>
632 inline _BI2
633 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
635 // concept requirements
636 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
637 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
638 __glibcxx_function_requires(_ConvertibleConcept<
639 typename iterator_traits<_BI1>::value_type,
640 typename iterator_traits<_BI2>::value_type>)
641 __glibcxx_requires_valid_range(__first, __last);
643 return (std::__copy_move_backward_a2<__is_move_iterator<_BI1>::__value>
644 (std::__miter_base<_BI1>::__b(__first),
645 std::__miter_base<_BI1>::__b(__last), __result));
648 #ifdef __GXX_EXPERIMENTAL_CXX0X__
650 * @brief Moves the range [first,last) into result.
651 * @ingroup mutating_algorithms
652 * @param first A bidirectional iterator.
653 * @param last A bidirectional iterator.
654 * @param result A bidirectional iterator.
655 * @return result - (first - last)
657 * The function has the same effect as move, but starts at the end of the
658 * range and works its way to the start, returning the start of the result.
659 * This inline function will boil down to a call to @c memmove whenever
660 * possible. Failing that, if random access iterators are passed, then the
661 * loop count will be known (and therefore a candidate for compiler
662 * optimizations such as unrolling).
664 * Result may not be in the range [first,last). Use move instead. Note
665 * that the start of the output range may overlap [first,last).
667 template<typename _BI1, typename _BI2>
668 inline _BI2
669 move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
671 // concept requirements
672 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
673 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
674 __glibcxx_function_requires(_ConvertibleConcept<
675 typename iterator_traits<_BI1>::value_type,
676 typename iterator_traits<_BI2>::value_type>)
677 __glibcxx_requires_valid_range(__first, __last);
679 return (std::__copy_move_backward_a2<true>
680 (std::__miter_base<_BI1>::__b(__first),
681 std::__miter_base<_BI1>::__b(__last), __result));
684 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
685 #else
686 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
687 #endif
689 template<typename _ForwardIterator, typename _Tp>
690 inline typename
691 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
692 __fill_a(_ForwardIterator __first, _ForwardIterator __last,
693 const _Tp& __value)
695 for (; __first != __last; ++__first)
696 *__first = __value;
699 template<typename _ForwardIterator, typename _Tp>
700 inline typename
701 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
702 __fill_a(_ForwardIterator __first, _ForwardIterator __last,
703 const _Tp& __value)
705 const _Tp __tmp = __value;
706 for (; __first != __last; ++__first)
707 *__first = __tmp;
710 // Specialization: for char types we can use memset.
711 template<typename _Tp>
712 inline typename
713 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type
714 __fill_a(_Tp* __first, _Tp* __last, const _Tp& __c)
716 const _Tp __tmp = __c;
717 __builtin_memset(__first, static_cast<unsigned char>(__tmp),
718 __last - __first);
722 * @brief Fills the range [first,last) with copies of value.
723 * @ingroup mutating_algorithms
724 * @param first A forward iterator.
725 * @param last A forward iterator.
726 * @param value A reference-to-const of arbitrary type.
727 * @return Nothing.
729 * This function fills a range with copies of the same value. For char
730 * types filling contiguous areas of memory, this becomes an inline call
731 * to @c memset or @c wmemset.
733 template<typename _ForwardIterator, typename _Tp>
734 inline void
735 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
737 // concept requirements
738 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
739 _ForwardIterator>)
740 __glibcxx_requires_valid_range(__first, __last);
742 std::__fill_a(std::__niter_base<_ForwardIterator>::__b(__first),
743 std::__niter_base<_ForwardIterator>::__b(__last), __value);
746 template<typename _OutputIterator, typename _Size, typename _Tp>
747 inline typename
748 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
749 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
751 for (; __n > 0; --__n, ++__first)
752 *__first = __value;
753 return __first;
756 template<typename _OutputIterator, typename _Size, typename _Tp>
757 inline typename
758 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
759 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
761 const _Tp __tmp = __value;
762 for (; __n > 0; --__n, ++__first)
763 *__first = __tmp;
764 return __first;
767 template<typename _Size, typename _Tp>
768 inline typename
769 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, _Tp*>::__type
770 __fill_n_a(_Tp* __first, _Size __n, const _Tp& __c)
772 std::__fill_a(__first, __first + __n, __c);
773 return __first + __n;
777 * @brief Fills the range [first,first+n) with copies of value.
778 * @ingroup mutating_algorithms
779 * @param first An output iterator.
780 * @param n The count of copies to perform.
781 * @param value A reference-to-const of arbitrary type.
782 * @return The iterator at first+n.
784 * This function fills a range with copies of the same value. For char
785 * types filling contiguous areas of memory, this becomes an inline call
786 * to @c memset or @ wmemset.
788 template<typename _OI, typename _Size, typename _Tp>
789 inline _OI
790 fill_n(_OI __first, _Size __n, const _Tp& __value)
792 // concept requirements
793 __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>)
795 return _OI(std::__fill_n_a(std::__niter_base<_OI>::__b(__first),
796 __n, __value));
799 template<bool _BoolType>
800 struct __equal
802 template<typename _II1, typename _II2>
803 static bool
804 equal(_II1 __first1, _II1 __last1, _II2 __first2)
806 for (; __first1 != __last1; ++__first1, ++__first2)
807 if (!(*__first1 == *__first2))
808 return false;
809 return true;
813 template<>
814 struct __equal<true>
816 template<typename _Tp>
817 static bool
818 equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
820 return !__builtin_memcmp(__first1, __first2, sizeof(_Tp)
821 * (__last1 - __first1));
825 template<typename _II1, typename _II2>
826 inline bool
827 __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
829 typedef typename iterator_traits<_II1>::value_type _ValueType1;
830 typedef typename iterator_traits<_II2>::value_type _ValueType2;
831 const bool __simple = (__is_integer<_ValueType1>::__value
832 && __is_pointer<_II1>::__value
833 && __is_pointer<_II2>::__value
834 && __are_same<_ValueType1, _ValueType2>::__value);
836 return std::__equal<__simple>::equal(__first1, __last1, __first2);
840 template<typename, typename>
841 struct __lc_rai
843 template<typename _II1, typename _II2>
844 static _II1
845 __newlast1(_II1, _II1 __last1, _II2, _II2)
846 { return __last1; }
848 template<typename _II>
849 static bool
850 __cnd2(_II __first, _II __last)
851 { return __first != __last; }
854 template<>
855 struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag>
857 template<typename _RAI1, typename _RAI2>
858 static _RAI1
859 __newlast1(_RAI1 __first1, _RAI1 __last1,
860 _RAI2 __first2, _RAI2 __last2)
862 const typename iterator_traits<_RAI1>::difference_type
863 __diff1 = __last1 - __first1;
864 const typename iterator_traits<_RAI2>::difference_type
865 __diff2 = __last2 - __first2;
866 return __diff2 < __diff1 ? __first1 + __diff2 : __last1;
869 template<typename _RAI>
870 static bool
871 __cnd2(_RAI, _RAI)
872 { return true; }
875 template<bool _BoolType>
876 struct __lexicographical_compare
878 template<typename _II1, typename _II2>
879 static bool __lc(_II1, _II1, _II2, _II2);
882 template<bool _BoolType>
883 template<typename _II1, typename _II2>
884 bool
885 __lexicographical_compare<_BoolType>::
886 __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
888 typedef typename iterator_traits<_II1>::iterator_category _Category1;
889 typedef typename iterator_traits<_II2>::iterator_category _Category2;
890 typedef std::__lc_rai<_Category1, _Category2> __rai_type;
892 __last1 = __rai_type::__newlast1(__first1, __last1,
893 __first2, __last2);
894 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
895 ++__first1, ++__first2)
897 if (*__first1 < *__first2)
898 return true;
899 if (*__first2 < *__first1)
900 return false;
902 return __first1 == __last1 && __first2 != __last2;
905 template<>
906 struct __lexicographical_compare<true>
908 template<typename _Tp, typename _Up>
909 static bool
910 __lc(const _Tp* __first1, const _Tp* __last1,
911 const _Up* __first2, const _Up* __last2)
913 const size_t __len1 = __last1 - __first1;
914 const size_t __len2 = __last2 - __first2;
915 const int __result = __builtin_memcmp(__first1, __first2,
916 std::min(__len1, __len2));
917 return __result != 0 ? __result < 0 : __len1 < __len2;
921 template<typename _II1, typename _II2>
922 inline bool
923 __lexicographical_compare_aux(_II1 __first1, _II1 __last1,
924 _II2 __first2, _II2 __last2)
926 typedef typename iterator_traits<_II1>::value_type _ValueType1;
927 typedef typename iterator_traits<_II2>::value_type _ValueType2;
928 const bool __simple =
929 (__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
930 && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
931 && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
932 && __is_pointer<_II1>::__value
933 && __is_pointer<_II2>::__value);
935 return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
936 __first2, __last2);
939 _GLIBCXX_END_NAMESPACE
941 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
944 * @brief Tests a range for element-wise equality.
945 * @ingroup non_mutating_algorithms
946 * @param first1 An input iterator.
947 * @param last1 An input iterator.
948 * @param first2 An input iterator.
949 * @return A boolean true or false.
951 * This compares the elements of two ranges using @c == and returns true or
952 * false depending on whether all of the corresponding elements of the
953 * ranges are equal.
955 template<typename _II1, typename _II2>
956 inline bool
957 equal(_II1 __first1, _II1 __last1, _II2 __first2)
959 // concept requirements
960 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
961 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
962 __glibcxx_function_requires(_EqualOpConcept<
963 typename iterator_traits<_II1>::value_type,
964 typename iterator_traits<_II2>::value_type>)
965 __glibcxx_requires_valid_range(__first1, __last1);
967 return std::__equal_aux(std::__niter_base<_II1>::__b(__first1),
968 std::__niter_base<_II1>::__b(__last1),
969 std::__niter_base<_II2>::__b(__first2));
973 * @brief Tests a range for element-wise equality.
974 * @ingroup non_mutating_algorithms
975 * @param first1 An input iterator.
976 * @param last1 An input iterator.
977 * @param first2 An input iterator.
978 * @param binary_pred A binary predicate @link functors
979 * functor@endlink.
980 * @return A boolean true or false.
982 * This compares the elements of two ranges using the binary_pred
983 * parameter, and returns true or
984 * false depending on whether all of the corresponding elements of the
985 * ranges are equal.
987 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
988 inline bool
989 equal(_IIter1 __first1, _IIter1 __last1,
990 _IIter2 __first2, _BinaryPredicate __binary_pred)
992 // concept requirements
993 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
994 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
995 __glibcxx_requires_valid_range(__first1, __last1);
997 for (; __first1 != __last1; ++__first1, ++__first2)
998 if (!bool(__binary_pred(*__first1, *__first2)))
999 return false;
1000 return true;
1004 * @brief Performs "dictionary" comparison on ranges.
1005 * @ingroup sorting_algorithms
1006 * @param first1 An input iterator.
1007 * @param last1 An input iterator.
1008 * @param first2 An input iterator.
1009 * @param last2 An input iterator.
1010 * @return A boolean true or false.
1012 * "Returns true if the sequence of elements defined by the range
1013 * [first1,last1) is lexicographically less than the sequence of elements
1014 * defined by the range [first2,last2). Returns false otherwise."
1015 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
1016 * then this is an inline call to @c memcmp.
1018 template<typename _II1, typename _II2>
1019 inline bool
1020 lexicographical_compare(_II1 __first1, _II1 __last1,
1021 _II2 __first2, _II2 __last2)
1023 // concept requirements
1024 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1025 typedef typename iterator_traits<_II2>::value_type _ValueType2;
1026 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1027 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1028 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
1029 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
1030 __glibcxx_requires_valid_range(__first1, __last1);
1031 __glibcxx_requires_valid_range(__first2, __last2);
1033 return std::__lexicographical_compare_aux
1034 (std::__niter_base<_II1>::__b(__first1),
1035 std::__niter_base<_II1>::__b(__last1),
1036 std::__niter_base<_II2>::__b(__first2),
1037 std::__niter_base<_II2>::__b(__last2));
1041 * @brief Performs "dictionary" comparison on ranges.
1042 * @ingroup sorting_algorithms
1043 * @param first1 An input iterator.
1044 * @param last1 An input iterator.
1045 * @param first2 An input iterator.
1046 * @param last2 An input iterator.
1047 * @param comp A @link comparison_functors comparison functor@endlink.
1048 * @return A boolean true or false.
1050 * The same as the four-parameter @c lexicographical_compare, but uses the
1051 * comp parameter instead of @c <.
1053 template<typename _II1, typename _II2, typename _Compare>
1054 bool
1055 lexicographical_compare(_II1 __first1, _II1 __last1,
1056 _II2 __first2, _II2 __last2, _Compare __comp)
1058 typedef typename iterator_traits<_II1>::iterator_category _Category1;
1059 typedef typename iterator_traits<_II2>::iterator_category _Category2;
1060 typedef std::__lc_rai<_Category1, _Category2> __rai_type;
1062 // concept requirements
1063 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1064 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1065 __glibcxx_requires_valid_range(__first1, __last1);
1066 __glibcxx_requires_valid_range(__first2, __last2);
1068 __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
1069 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
1070 ++__first1, ++__first2)
1072 if (__comp(*__first1, *__first2))
1073 return true;
1074 if (__comp(*__first2, *__first1))
1075 return false;
1077 return __first1 == __last1 && __first2 != __last2;
1081 * @brief Finds the places in ranges which don't match.
1082 * @ingroup non_mutating_algorithms
1083 * @param first1 An input iterator.
1084 * @param last1 An input iterator.
1085 * @param first2 An input iterator.
1086 * @return A pair of iterators pointing to the first mismatch.
1088 * This compares the elements of two ranges using @c == and returns a pair
1089 * of iterators. The first iterator points into the first range, the
1090 * second iterator points into the second range, and the elements pointed
1091 * to by the iterators are not equal.
1093 template<typename _InputIterator1, typename _InputIterator2>
1094 pair<_InputIterator1, _InputIterator2>
1095 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1096 _InputIterator2 __first2)
1098 // concept requirements
1099 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1100 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1101 __glibcxx_function_requires(_EqualOpConcept<
1102 typename iterator_traits<_InputIterator1>::value_type,
1103 typename iterator_traits<_InputIterator2>::value_type>)
1104 __glibcxx_requires_valid_range(__first1, __last1);
1106 while (__first1 != __last1 && *__first1 == *__first2)
1108 ++__first1;
1109 ++__first2;
1111 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1115 * @brief Finds the places in ranges which don't match.
1116 * @ingroup non_mutating_algorithms
1117 * @param first1 An input iterator.
1118 * @param last1 An input iterator.
1119 * @param first2 An input iterator.
1120 * @param binary_pred A binary predicate @link functors
1121 * functor@endlink.
1122 * @return A pair of iterators pointing to the first mismatch.
1124 * This compares the elements of two ranges using the binary_pred
1125 * parameter, and returns a pair
1126 * of iterators. The first iterator points into the first range, the
1127 * second iterator points into the second range, and the elements pointed
1128 * to by the iterators are not equal.
1130 template<typename _InputIterator1, typename _InputIterator2,
1131 typename _BinaryPredicate>
1132 pair<_InputIterator1, _InputIterator2>
1133 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1134 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1136 // concept requirements
1137 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1138 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1139 __glibcxx_requires_valid_range(__first1, __last1);
1141 while (__first1 != __last1 && bool(__binary_pred(*__first1, *__first2)))
1143 ++__first1;
1144 ++__first2;
1146 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1149 _GLIBCXX_END_NESTED_NAMESPACE
1151 // NB: This file is included within many other C++ includes, as a way
1152 // of getting the base algorithms. So, make sure that parallel bits
1153 // come in too if requested.
1154 #ifdef _GLIBCXX_PARALLEL
1155 # include <parallel/algobase.h>
1156 #endif
1158 #endif