1 // Bits and pieces used in algorithms -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
45 * Copyright (c) 1996-1998
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
57 /** @file stl_algobase.h
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
65 #include <bits/c++config.h>
69 #include <bits/functexcept.h>
70 #include <bits/stl_pair.h>
71 #include <bits/cpp_type_traits.h>
72 #include <ext/type_traits.h>
74 #include <bits/stl_iterator_base_types.h>
75 #include <bits/stl_iterator_base_funcs.h>
76 #include <bits/stl_iterator.h>
77 #include <bits/concept_check.h>
78 #include <debug/debug.h>
80 _GLIBCXX_BEGIN_NAMESPACE(std)
83 * @brief Swaps two values.
84 * @param a A thing of arbitrary type.
85 * @param b Another thing of arbitrary type.
88 * This is the simple classic generic implementation. It will work on
89 * any type which has a copy constructor and an assignment operator.
91 template<typename _Tp>
93 swap(_Tp& __a, _Tp& __b)
95 // concept requirements
96 __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
103 // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
104 // nutshell, we are partially implementing the resolution of DR 187,
105 // when it's safe, i.e., the value_types are equal.
106 template<bool _BoolType>
109 template<typename _ForwardIterator1, typename _ForwardIterator2>
111 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
113 typedef typename iterator_traits<_ForwardIterator1>::value_type
115 _ValueType1 __tmp = *__a;
122 struct __iter_swap<true>
124 template<typename _ForwardIterator1, typename _ForwardIterator2>
126 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
133 * @brief Swaps the contents of two iterators.
134 * @param a An iterator.
135 * @param b Another iterator.
138 * This function swaps the values pointed to by two iterators, not the
139 * iterators themselves.
141 template<typename _ForwardIterator1, typename _ForwardIterator2>
143 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
145 typedef typename iterator_traits<_ForwardIterator1>::value_type
147 typedef typename iterator_traits<_ForwardIterator2>::value_type
150 // concept requirements
151 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
153 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
155 __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
157 __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
160 typedef typename iterator_traits<_ForwardIterator1>::reference
162 typedef typename iterator_traits<_ForwardIterator2>::reference
164 std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value &&
165 __are_same<_ValueType1 &, _ReferenceType1>::__value &&
166 __are_same<_ValueType2 &, _ReferenceType2>::__value>::
171 * @brief This does what you think it does.
172 * @param a A thing of arbitrary type.
173 * @param b Another thing of arbitrary type.
174 * @return The lesser of the parameters.
176 * This is the simple classic generic implementation. It will work on
177 * temporary expressions, since they are only evaluated once, unlike a
178 * preprocessor macro.
180 template<typename _Tp>
182 min(const _Tp& __a, const _Tp& __b)
184 // concept requirements
185 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
186 //return __b < __a ? __b : __a;
193 * @brief This does what you think it does.
194 * @param a A thing of arbitrary type.
195 * @param b Another thing of arbitrary type.
196 * @return The greater of the parameters.
198 * This is the simple classic generic implementation. It will work on
199 * temporary expressions, since they are only evaluated once, unlike a
200 * preprocessor macro.
202 template<typename _Tp>
204 max(const _Tp& __a, const _Tp& __b)
206 // concept requirements
207 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
208 //return __a < __b ? __b : __a;
215 * @brief This does what you think it does.
216 * @param a A thing of arbitrary type.
217 * @param b Another thing of arbitrary type.
218 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
219 * @return The lesser of the parameters.
221 * This will work on temporary expressions, since they are only evaluated
222 * once, unlike a preprocessor macro.
224 template<typename _Tp, typename _Compare>
226 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
228 //return __comp(__b, __a) ? __b : __a;
229 if (__comp(__b, __a))
235 * @brief This does what you think it does.
236 * @param a A thing of arbitrary type.
237 * @param b Another thing of arbitrary type.
238 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
239 * @return The greater of the parameters.
241 * This will work on temporary expressions, since they are only evaluated
242 * once, unlike a preprocessor macro.
244 template<typename _Tp, typename _Compare>
246 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
248 //return __comp(__a, __b) ? __b : __a;
249 if (__comp(__a, __b))
254 // All of these auxiliary structs serve two purposes. (1) Replace
255 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
256 // because the input and output ranges are permitted to overlap.)
257 // (2) If we're using random access iterators, then write the loop as
258 // a for loop with an explicit count.
260 template<bool, typename>
263 template<typename _II, typename _OI>
265 copy(_II __first, _II __last, _OI __result)
267 for (; __first != __last; ++__result, ++__first)
268 *__result = *__first;
273 template<bool _BoolType>
274 struct __copy<_BoolType, random_access_iterator_tag>
276 template<typename _II, typename _OI>
278 copy(_II __first, _II __last, _OI __result)
280 typedef typename iterator_traits<_II>::difference_type _Distance;
281 for(_Distance __n = __last - __first; __n > 0; --__n)
283 *__result = *__first;
292 struct __copy<true, random_access_iterator_tag>
294 template<typename _Tp>
296 copy(const _Tp* __first, const _Tp* __last, _Tp* __result)
298 std::memmove(__result, __first, sizeof(_Tp) * (__last - __first));
299 return __result + (__last - __first);
303 template<typename _II, typename _OI>
305 __copy_aux(_II __first, _II __last, _OI __result)
307 typedef typename iterator_traits<_II>::value_type _ValueTypeI;
308 typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
309 typedef typename iterator_traits<_II>::iterator_category _Category;
310 const bool __simple = (__is_scalar<_ValueTypeI>::__value
311 && __is_pointer<_II>::__value
312 && __is_pointer<_OI>::__value
313 && __are_same<_ValueTypeI, _ValueTypeO>::__value);
315 return std::__copy<__simple, _Category>::copy(__first, __last, __result);
318 // Helpers for streambuf iterators (either istream or ostream).
319 // NB: avoid including <iosfwd>, relatively large.
320 template<typename _CharT>
323 template<typename _CharT, typename _Traits>
324 class istreambuf_iterator;
326 template<typename _CharT, typename _Traits>
327 class ostreambuf_iterator;
329 template<typename _CharT>
330 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
331 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
332 __copy_aux(_CharT*, _CharT*,
333 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
335 template<typename _CharT>
336 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
337 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
338 __copy_aux(const _CharT*, const _CharT*,
339 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
341 template<typename _CharT>
342 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
344 __copy_aux(istreambuf_iterator<_CharT, char_traits<_CharT> >,
345 istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
350 template<typename _II, typename _OI>
352 __copy_n(_II __first, _II __last, _OI __result)
353 { return std::__copy_aux(__first, __last, __result); }
357 struct __copy_normal<true, false>
359 template<typename _II, typename _OI>
361 __copy_n(_II __first, _II __last, _OI __result)
362 { return std::__copy_aux(__first.base(), __last.base(), __result); }
366 struct __copy_normal<false, true>
368 template<typename _II, typename _OI>
370 __copy_n(_II __first, _II __last, _OI __result)
371 { return _OI(std::__copy_aux(__first, __last, __result.base())); }
375 struct __copy_normal<true, true>
377 template<typename _II, typename _OI>
379 __copy_n(_II __first, _II __last, _OI __result)
380 { return _OI(std::__copy_aux(__first.base(), __last.base(),
385 * @brief Copies the range [first,last) into result.
386 * @param first An input iterator.
387 * @param last An input iterator.
388 * @param result An output iterator.
389 * @return result + (first - last)
391 * This inline function will boil down to a call to @c memmove whenever
392 * possible. Failing that, if random access iterators are passed, then the
393 * loop count will be known (and therefore a candidate for compiler
394 * optimizations such as unrolling). Result may not be contained within
395 * [first,last); the copy_backward function should be used instead.
397 * Note that the end of the output range is permitted to be contained
398 * within [first,last).
400 template<typename _InputIterator, typename _OutputIterator>
401 inline _OutputIterator
402 copy(_InputIterator __first, _InputIterator __last,
403 _OutputIterator __result)
405 // concept requirements
406 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
407 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
408 typename iterator_traits<_InputIterator>::value_type>)
409 __glibcxx_requires_valid_range(__first, __last);
411 const bool __in = __is_normal_iterator<_InputIterator>::__value;
412 const bool __out = __is_normal_iterator<_OutputIterator>::__value;
413 return std::__copy_normal<__in, __out>::__copy_n(__first, __last,
417 template<bool, typename>
418 struct __copy_backward
420 template<typename _BI1, typename _BI2>
422 __copy_b(_BI1 __first, _BI1 __last, _BI2 __result)
424 while (__first != __last)
425 *--__result = *--__last;
430 template<bool _BoolType>
431 struct __copy_backward<_BoolType, random_access_iterator_tag>
433 template<typename _BI1, typename _BI2>
435 __copy_b(_BI1 __first, _BI1 __last, _BI2 __result)
437 typename iterator_traits<_BI1>::difference_type __n;
438 for (__n = __last - __first; __n > 0; --__n)
439 *--__result = *--__last;
445 struct __copy_backward<true, random_access_iterator_tag>
447 template<typename _Tp>
449 __copy_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
451 const ptrdiff_t _Num = __last - __first;
452 std::memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
453 return __result - _Num;
457 template<typename _BI1, typename _BI2>
459 __copy_backward_aux(_BI1 __first, _BI1 __last, _BI2 __result)
461 typedef typename iterator_traits<_BI1>::value_type _ValueType1;
462 typedef typename iterator_traits<_BI2>::value_type _ValueType2;
463 typedef typename iterator_traits<_BI1>::iterator_category _Category;
464 const bool __simple = (__is_scalar<_ValueType1>::__value
465 && __is_pointer<_BI1>::__value
466 && __is_pointer<_BI2>::__value
467 && __are_same<_ValueType1, _ValueType2>::__value);
469 return std::__copy_backward<__simple, _Category>::__copy_b(__first,
475 struct __copy_backward_normal
477 template<typename _BI1, typename _BI2>
479 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
480 { return std::__copy_backward_aux(__first, __last, __result); }
484 struct __copy_backward_normal<true, false>
486 template<typename _BI1, typename _BI2>
488 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
489 { return std::__copy_backward_aux(__first.base(), __last.base(),
494 struct __copy_backward_normal<false, true>
496 template<typename _BI1, typename _BI2>
498 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
499 { return _BI2(std::__copy_backward_aux(__first, __last,
504 struct __copy_backward_normal<true, true>
506 template<typename _BI1, typename _BI2>
508 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
509 { return _BI2(std::__copy_backward_aux(__first.base(), __last.base(),
514 * @brief Copies the range [first,last) into result.
515 * @param first A bidirectional iterator.
516 * @param last A bidirectional iterator.
517 * @param result A bidirectional iterator.
518 * @return result - (first - last)
520 * The function has the same effect as copy, but starts at the end of the
521 * range and works its way to the start, returning the start of the result.
522 * This inline function will boil down to a call to @c memmove whenever
523 * possible. Failing that, if random access iterators are passed, then the
524 * loop count will be known (and therefore a candidate for compiler
525 * optimizations such as unrolling).
527 * Result may not be in the range [first,last). Use copy instead. Note
528 * that the start of the output range may overlap [first,last).
530 template <typename _BI1, typename _BI2>
532 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
534 // concept requirements
535 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
536 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
537 __glibcxx_function_requires(_ConvertibleConcept<
538 typename iterator_traits<_BI1>::value_type,
539 typename iterator_traits<_BI2>::value_type>)
540 __glibcxx_requires_valid_range(__first, __last);
542 const bool __bi1 = __is_normal_iterator<_BI1>::__value;
543 const bool __bi2 = __is_normal_iterator<_BI2>::__value;
544 return std::__copy_backward_normal<__bi1, __bi2>::__copy_b_n(__first,
553 template<typename _ForwardIterator, typename _Tp>
555 fill(_ForwardIterator __first, _ForwardIterator __last,
558 for (; __first != __last; ++__first)
566 template<typename _ForwardIterator, typename _Tp>
568 fill(_ForwardIterator __first, _ForwardIterator __last,
571 const _Tp __tmp = __value;
572 for (; __first != __last; ++__first)
577 template<typename _ForwardIterator, typename _Tp>
579 __fill_aux(_ForwardIterator __first, _ForwardIterator __last,
582 const bool __scalar = __is_scalar<_Tp>::__value;
583 std::__fill<__scalar>::fill(__first, __last, __value);
586 // Specialization: for char types we can use memset (wmemset).
588 __fill_aux(unsigned char* __first, unsigned char* __last, unsigned char __c)
589 { std::memset(__first, __c, __last - __first); }
592 __fill_aux(signed char* __first, signed char* __last, signed char __c)
593 { std::memset(__first, static_cast<unsigned char>(__c), __last - __first); }
596 __fill_aux(char* __first, char* __last, char __c)
597 { std::memset(__first, static_cast<unsigned char>(__c), __last - __first); }
599 #ifdef _GLIBCXX_USE_WCHAR_T
601 __fill_aux(wchar_t* __first, wchar_t* __last, wchar_t __c)
602 { std::wmemset(__first, __c, __last - __first); }
608 template<typename _ForwardIterator, typename _Tp>
610 __fill_n(_ForwardIterator __first, _ForwardIterator __last,
612 { std::__fill_aux(__first, __last, __value); }
616 struct __fill_normal<true>
618 template<typename _ForwardIterator, typename _Tp>
620 __fill_n(_ForwardIterator __first, _ForwardIterator __last,
622 { std::__fill_aux(__first.base(), __last.base(), __value); }
626 * @brief Fills the range [first,last) with copies of value.
627 * @param first A forward iterator.
628 * @param last A forward iterator.
629 * @param value A reference-to-const of arbitrary type.
632 * This function fills a range with copies of the same value. For char
633 * types filling contiguous areas of memory, this becomes an inline call
634 * to @c memset or @c wmemset.
636 template<typename _ForwardIterator, typename _Tp>
638 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
640 // concept requirements
641 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
643 __glibcxx_requires_valid_range(__first, __last);
645 const bool __fi = __is_normal_iterator<_ForwardIterator>::__value;
646 std::__fill_normal<__fi>::__fill_n(__first, __last, __value);
653 template<typename _OutputIterator, typename _Size, typename _Tp>
654 static _OutputIterator
655 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
657 for (; __n > 0; --__n, ++__first)
664 struct __fill_n<true>
666 template<typename _OutputIterator, typename _Size, typename _Tp>
667 static _OutputIterator
668 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
670 const _Tp __tmp = __value;
671 for (; __n > 0; --__n, ++__first)
677 template<typename _OutputIterator, typename _Size, typename _Tp>
678 inline _OutputIterator
679 __fill_n_aux(_OutputIterator __first, _Size __n, const _Tp& __value)
681 const bool __scalar = __is_scalar<_Tp>::__value;
682 return std::__fill_n<__scalar>::fill_n(__first, __n, __value);
685 template<typename _Size>
686 inline unsigned char*
687 __fill_n_aux(unsigned char* __first, _Size __n, unsigned char __c)
689 std::__fill_aux(__first, __first + __n, __c);
690 return __first + __n;
693 template<typename _Size>
695 __fill_n_aux(signed char* __first, _Size __n, signed char __c)
697 std::__fill_aux(__first, __first + __n, __c);
698 return __first + __n;
701 template<typename _Size>
703 __fill_n_aux(char* __first, _Size __n, char __c)
705 std::__fill_aux(__first, __first + __n, __c);
706 return __first + __n;
709 #ifdef _GLIBCXX_USE_WCHAR_T
710 template<typename _Size>
712 __fill_n_aux(wchar_t* __first, _Size __n, wchar_t __c)
714 std::__fill_aux(__first, __first + __n, __c);
715 return __first + __n;
720 struct __fill_n_normal
722 template<typename _OI, typename _Size, typename _Tp>
724 __fill_n_n(_OI __first, _Size __n, const _Tp& __value)
725 { return std::__fill_n_aux(__first, __n, __value); }
729 struct __fill_n_normal<true>
731 template<typename _OI, typename _Size, typename _Tp>
733 __fill_n_n(_OI __first, _Size __n, const _Tp& __value)
734 { return _OI(std::__fill_n_aux(__first.base(), __n, __value)); }
738 * @brief Fills the range [first,first+n) with copies of value.
739 * @param first An output iterator.
740 * @param n The count of copies to perform.
741 * @param value A reference-to-const of arbitrary type.
742 * @return The iterator at first+n.
744 * This function fills a range with copies of the same value. For char
745 * types filling contiguous areas of memory, this becomes an inline call
746 * to @c memset or @ wmemset.
748 template<typename _OutputIterator, typename _Size, typename _Tp>
749 inline _OutputIterator
750 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
752 // concept requirements
753 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, _Tp>)
755 const bool __oi = __is_normal_iterator<_OutputIterator>::__value;
756 return std::__fill_n_normal<__oi>::__fill_n_n(__first, __n, __value);
760 * @brief Finds the places in ranges which don't match.
761 * @param first1 An input iterator.
762 * @param last1 An input iterator.
763 * @param first2 An input iterator.
764 * @return A pair of iterators pointing to the first mismatch.
766 * This compares the elements of two ranges using @c == and returns a pair
767 * of iterators. The first iterator points into the first range, the
768 * second iterator points into the second range, and the elements pointed
769 * to by the iterators are not equal.
771 template<typename _InputIterator1, typename _InputIterator2>
772 pair<_InputIterator1, _InputIterator2>
773 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
774 _InputIterator2 __first2)
776 // concept requirements
777 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
778 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
779 __glibcxx_function_requires(_EqualOpConcept<
780 typename iterator_traits<_InputIterator1>::value_type,
781 typename iterator_traits<_InputIterator2>::value_type>)
782 __glibcxx_requires_valid_range(__first1, __last1);
784 while (__first1 != __last1 && *__first1 == *__first2)
789 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
793 * @brief Finds the places in ranges which don't match.
794 * @param first1 An input iterator.
795 * @param last1 An input iterator.
796 * @param first2 An input iterator.
797 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
798 * @return A pair of iterators pointing to the first mismatch.
800 * This compares the elements of two ranges using the binary_pred
801 * parameter, and returns a pair
802 * of iterators. The first iterator points into the first range, the
803 * second iterator points into the second range, and the elements pointed
804 * to by the iterators are not equal.
806 template<typename _InputIterator1, typename _InputIterator2,
807 typename _BinaryPredicate>
808 pair<_InputIterator1, _InputIterator2>
809 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
810 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
812 // concept requirements
813 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
814 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
815 __glibcxx_requires_valid_range(__first1, __last1);
817 while (__first1 != __last1 && __binary_pred(*__first1, *__first2))
822 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
826 * @brief Tests a range for element-wise equality.
827 * @param first1 An input iterator.
828 * @param last1 An input iterator.
829 * @param first2 An input iterator.
830 * @return A boolean true or false.
832 * This compares the elements of two ranges using @c == and returns true or
833 * false depending on whether all of the corresponding elements of the
836 template<typename _InputIterator1, typename _InputIterator2>
838 equal(_InputIterator1 __first1, _InputIterator1 __last1,
839 _InputIterator2 __first2)
841 // concept requirements
842 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
843 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
844 __glibcxx_function_requires(_EqualOpConcept<
845 typename iterator_traits<_InputIterator1>::value_type,
846 typename iterator_traits<_InputIterator2>::value_type>)
847 __glibcxx_requires_valid_range(__first1, __last1);
849 for (; __first1 != __last1; ++__first1, ++__first2)
850 if (!(*__first1 == *__first2))
856 * @brief Tests a range for element-wise equality.
857 * @param first1 An input iterator.
858 * @param last1 An input iterator.
859 * @param first2 An input iterator.
860 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
861 * @return A boolean true or false.
863 * This compares the elements of two ranges using the binary_pred
864 * parameter, and returns true or
865 * false depending on whether all of the corresponding elements of the
868 template<typename _InputIterator1, typename _InputIterator2,
869 typename _BinaryPredicate>
871 equal(_InputIterator1 __first1, _InputIterator1 __last1,
872 _InputIterator2 __first2,
873 _BinaryPredicate __binary_pred)
875 // concept requirements
876 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
877 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
878 __glibcxx_requires_valid_range(__first1, __last1);
880 for (; __first1 != __last1; ++__first1, ++__first2)
881 if (!__binary_pred(*__first1, *__first2))
887 * @brief Performs "dictionary" comparison on ranges.
888 * @param first1 An input iterator.
889 * @param last1 An input iterator.
890 * @param first2 An input iterator.
891 * @param last2 An input iterator.
892 * @return A boolean true or false.
894 * "Returns true if the sequence of elements defined by the range
895 * [first1,last1) is lexicographically less than the sequence of elements
896 * defined by the range [first2,last2). Returns false otherwise."
897 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
898 * then this is an inline call to @c memcmp.
900 template<typename _InputIterator1, typename _InputIterator2>
902 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
903 _InputIterator2 __first2, _InputIterator2 __last2)
905 // concept requirements
906 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
907 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
908 __glibcxx_function_requires(_LessThanOpConcept<
909 typename iterator_traits<_InputIterator1>::value_type,
910 typename iterator_traits<_InputIterator2>::value_type>)
911 __glibcxx_function_requires(_LessThanOpConcept<
912 typename iterator_traits<_InputIterator2>::value_type,
913 typename iterator_traits<_InputIterator1>::value_type>)
914 __glibcxx_requires_valid_range(__first1, __last1);
915 __glibcxx_requires_valid_range(__first2, __last2);
917 for (; __first1 != __last1 && __first2 != __last2;
918 ++__first1, ++__first2)
920 if (*__first1 < *__first2)
922 if (*__first2 < *__first1)
925 return __first1 == __last1 && __first2 != __last2;
929 * @brief Performs "dictionary" comparison on ranges.
930 * @param first1 An input iterator.
931 * @param last1 An input iterator.
932 * @param first2 An input iterator.
933 * @param last2 An input iterator.
934 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
935 * @return A boolean true or false.
937 * The same as the four-parameter @c lexigraphical_compare, but uses the
938 * comp parameter instead of @c <.
940 template<typename _InputIterator1, typename _InputIterator2,
943 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
944 _InputIterator2 __first2, _InputIterator2 __last2,
947 // concept requirements
948 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
949 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
950 __glibcxx_requires_valid_range(__first1, __last1);
951 __glibcxx_requires_valid_range(__first2, __last2);
953 for (; __first1 != __last1 && __first2 != __last2;
954 ++__first1, ++__first2)
956 if (__comp(*__first1, *__first2))
958 if (__comp(*__first2, *__first1))
961 return __first1 == __last1 && __first2 != __last2;
965 lexicographical_compare(const unsigned char* __first1,
966 const unsigned char* __last1,
967 const unsigned char* __first2,
968 const unsigned char* __last2)
970 __glibcxx_requires_valid_range(__first1, __last1);
971 __glibcxx_requires_valid_range(__first2, __last2);
973 const size_t __len1 = __last1 - __first1;
974 const size_t __len2 = __last2 - __first2;
975 const int __result = std::memcmp(__first1, __first2,
976 std::min(__len1, __len2));
977 return __result != 0 ? __result < 0 : __len1 < __len2;
981 lexicographical_compare(const char* __first1, const char* __last1,
982 const char* __first2, const char* __last2)
984 __glibcxx_requires_valid_range(__first1, __last1);
985 __glibcxx_requires_valid_range(__first2, __last2);
987 if (std::numeric_limits<char>::is_signed)
988 return std::lexicographical_compare((const signed char*) __first1,
989 (const signed char*) __last1,
990 (const signed char*) __first2,
991 (const signed char*) __last2);
993 return std::lexicographical_compare((const unsigned char*) __first1,
994 (const unsigned char*) __last1,
995 (const unsigned char*) __first2,
996 (const unsigned char*) __last2);
999 _GLIBCXX_END_NAMESPACE