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>
72 #include <bits/stl_pair.h>
73 #include <bits/cpp_type_traits.h>
74 #include <ext/type_traits.h>
75 #include <bits/stl_iterator_base_types.h>
76 #include <bits/stl_iterator_base_funcs.h>
77 #include <bits/stl_iterator.h>
78 #include <bits/concept_check.h>
79 #include <debug/debug.h>
81 _GLIBCXX_BEGIN_NAMESPACE(std)
84 * @brief Swaps two values.
85 * @param a A thing of arbitrary type.
86 * @param b Another thing of arbitrary type.
89 * This is the simple classic generic implementation. It will work on
90 * any type which has a copy constructor and an assignment operator.
92 template<typename _Tp>
94 swap(_Tp& __a, _Tp& __b)
96 // concept requirements
97 __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
104 // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
105 // nutshell, we are partially implementing the resolution of DR 187,
106 // when it's safe, i.e., the value_types are equal.
107 template<bool _BoolType>
110 template<typename _ForwardIterator1, typename _ForwardIterator2>
112 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
114 typedef typename iterator_traits<_ForwardIterator1>::value_type
116 _ValueType1 __tmp = *__a;
123 struct __iter_swap<true>
125 template<typename _ForwardIterator1, typename _ForwardIterator2>
127 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
134 * @brief Swaps the contents of two iterators.
135 * @param a An iterator.
136 * @param b Another iterator.
139 * This function swaps the values pointed to by two iterators, not the
140 * iterators themselves.
142 template<typename _ForwardIterator1, typename _ForwardIterator2>
144 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
146 typedef typename iterator_traits<_ForwardIterator1>::value_type
148 typedef typename iterator_traits<_ForwardIterator2>::value_type
151 // concept requirements
152 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
154 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
156 __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
158 __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
161 typedef typename iterator_traits<_ForwardIterator1>::reference
163 typedef typename iterator_traits<_ForwardIterator2>::reference
165 std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value &&
166 __are_same<_ValueType1 &, _ReferenceType1>::__value &&
167 __are_same<_ValueType2 &, _ReferenceType2>::__value>::
172 * @brief This does what you think it does.
173 * @param a A thing of arbitrary type.
174 * @param b Another thing of arbitrary type.
175 * @return The lesser of the parameters.
177 * This is the simple classic generic implementation. It will work on
178 * temporary expressions, since they are only evaluated once, unlike a
179 * preprocessor macro.
181 template<typename _Tp>
183 min(const _Tp& __a, const _Tp& __b)
185 // concept requirements
186 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
187 //return __b < __a ? __b : __a;
194 * @brief This does what you think it does.
195 * @param a A thing of arbitrary type.
196 * @param b Another thing of arbitrary type.
197 * @return The greater of the parameters.
199 * This is the simple classic generic implementation. It will work on
200 * temporary expressions, since they are only evaluated once, unlike a
201 * preprocessor macro.
203 template<typename _Tp>
205 max(const _Tp& __a, const _Tp& __b)
207 // concept requirements
208 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
209 //return __a < __b ? __b : __a;
216 * @brief This does what you think it does.
217 * @param a A thing of arbitrary type.
218 * @param b Another thing of arbitrary type.
219 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
220 * @return The lesser of the parameters.
222 * This will work on temporary expressions, since they are only evaluated
223 * once, unlike a preprocessor macro.
225 template<typename _Tp, typename _Compare>
227 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
229 //return __comp(__b, __a) ? __b : __a;
230 if (__comp(__b, __a))
236 * @brief This does what you think it does.
237 * @param a A thing of arbitrary type.
238 * @param b Another thing of arbitrary type.
239 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
240 * @return The greater of the parameters.
242 * This will work on temporary expressions, since they are only evaluated
243 * once, unlike a preprocessor macro.
245 template<typename _Tp, typename _Compare>
247 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
249 //return __comp(__a, __b) ? __b : __a;
250 if (__comp(__a, __b))
255 // All of these auxiliary structs serve two purposes. (1) Replace
256 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
257 // because the input and output ranges are permitted to overlap.)
258 // (2) If we're using random access iterators, then write the loop as
259 // a for loop with an explicit count.
261 template<bool, typename>
264 template<typename _II, typename _OI>
266 copy(_II __first, _II __last, _OI __result)
268 for (; __first != __last; ++__result, ++__first)
269 *__result = *__first;
274 template<bool _BoolType>
275 struct __copy<_BoolType, random_access_iterator_tag>
277 template<typename _II, typename _OI>
279 copy(_II __first, _II __last, _OI __result)
281 typedef typename iterator_traits<_II>::difference_type _Distance;
282 for(_Distance __n = __last - __first; __n > 0; --__n)
284 *__result = *__first;
293 struct __copy<true, random_access_iterator_tag>
295 template<typename _Tp>
297 copy(const _Tp* __first, const _Tp* __last, _Tp* __result)
299 std::memmove(__result, __first, sizeof(_Tp) * (__last - __first));
300 return __result + (__last - __first);
304 template<typename _II, typename _OI>
306 __copy_aux(_II __first, _II __last, _OI __result)
308 typedef typename iterator_traits<_II>::value_type _ValueTypeI;
309 typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
310 typedef typename iterator_traits<_II>::iterator_category _Category;
311 const bool __simple = (__is_scalar<_ValueTypeI>::__value
312 && __is_pointer<_II>::__value
313 && __is_pointer<_OI>::__value
314 && __are_same<_ValueTypeI, _ValueTypeO>::__value);
316 return std::__copy<__simple, _Category>::copy(__first, __last, __result);
319 // Helpers for streambuf iterators (either istream or ostream).
320 template<typename _CharT>
321 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
322 ostreambuf_iterator<_CharT> >::__type
323 __copy_aux(_CharT*, _CharT*, ostreambuf_iterator<_CharT>);
325 template<typename _CharT>
326 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
327 ostreambuf_iterator<_CharT> >::__type
328 __copy_aux(const _CharT*, const _CharT*, ostreambuf_iterator<_CharT>);
330 template<typename _CharT>
331 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, _CharT*>::__type
332 __copy_aux(istreambuf_iterator<_CharT>, istreambuf_iterator<_CharT>,
338 template<typename _II, typename _OI>
340 __copy_n(_II __first, _II __last, _OI __result)
341 { return std::__copy_aux(__first, __last, __result); }
345 struct __copy_normal<true, false>
347 template<typename _II, typename _OI>
349 __copy_n(_II __first, _II __last, _OI __result)
350 { return std::__copy_aux(__first.base(), __last.base(), __result); }
354 struct __copy_normal<false, true>
356 template<typename _II, typename _OI>
358 __copy_n(_II __first, _II __last, _OI __result)
359 { return _OI(std::__copy_aux(__first, __last, __result.base())); }
363 struct __copy_normal<true, true>
365 template<typename _II, typename _OI>
367 __copy_n(_II __first, _II __last, _OI __result)
368 { return _OI(std::__copy_aux(__first.base(), __last.base(),
373 * @brief Copies the range [first,last) into result.
374 * @param first An input iterator.
375 * @param last An input iterator.
376 * @param result An output iterator.
377 * @return result + (first - last)
379 * This inline function will boil down to a call to @c memmove whenever
380 * possible. Failing that, if random access iterators are passed, then the
381 * loop count will be known (and therefore a candidate for compiler
382 * optimizations such as unrolling). Result may not be contained within
383 * [first,last); the copy_backward function should be used instead.
385 * Note that the end of the output range is permitted to be contained
386 * within [first,last).
388 template<typename _InputIterator, typename _OutputIterator>
389 inline _OutputIterator
390 copy(_InputIterator __first, _InputIterator __last,
391 _OutputIterator __result)
393 // concept requirements
394 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
395 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
396 typename iterator_traits<_InputIterator>::value_type>)
397 __glibcxx_requires_valid_range(__first, __last);
399 const bool __in = __is_normal_iterator<_InputIterator>::__value;
400 const bool __out = __is_normal_iterator<_OutputIterator>::__value;
401 return std::__copy_normal<__in, __out>::__copy_n(__first, __last,
405 // Overload for streambuf iterators.
406 template<typename _CharT>
407 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
408 ostreambuf_iterator<_CharT> >::__type
409 copy(istreambuf_iterator<_CharT>, istreambuf_iterator<_CharT>,
410 ostreambuf_iterator<_CharT>);
412 template<bool, typename>
413 struct __copy_backward
415 template<typename _BI1, typename _BI2>
417 __copy_b(_BI1 __first, _BI1 __last, _BI2 __result)
419 while (__first != __last)
420 *--__result = *--__last;
425 template<bool _BoolType>
426 struct __copy_backward<_BoolType, random_access_iterator_tag>
428 template<typename _BI1, typename _BI2>
430 __copy_b(_BI1 __first, _BI1 __last, _BI2 __result)
432 typename iterator_traits<_BI1>::difference_type __n;
433 for (__n = __last - __first; __n > 0; --__n)
434 *--__result = *--__last;
440 struct __copy_backward<true, random_access_iterator_tag>
442 template<typename _Tp>
444 __copy_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
446 const ptrdiff_t _Num = __last - __first;
447 std::memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
448 return __result - _Num;
452 template<typename _BI1, typename _BI2>
454 __copy_backward_aux(_BI1 __first, _BI1 __last, _BI2 __result)
456 typedef typename iterator_traits<_BI1>::value_type _ValueType1;
457 typedef typename iterator_traits<_BI2>::value_type _ValueType2;
458 typedef typename iterator_traits<_BI1>::iterator_category _Category;
459 const bool __simple = (__is_scalar<_ValueType1>::__value
460 && __is_pointer<_BI1>::__value
461 && __is_pointer<_BI2>::__value
462 && __are_same<_ValueType1, _ValueType2>::__value);
464 return std::__copy_backward<__simple, _Category>::__copy_b(__first,
470 struct __copy_backward_normal
472 template<typename _BI1, typename _BI2>
474 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
475 { return std::__copy_backward_aux(__first, __last, __result); }
479 struct __copy_backward_normal<true, false>
481 template<typename _BI1, typename _BI2>
483 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
484 { return std::__copy_backward_aux(__first.base(), __last.base(),
489 struct __copy_backward_normal<false, true>
491 template<typename _BI1, typename _BI2>
493 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
494 { return _BI2(std::__copy_backward_aux(__first, __last,
499 struct __copy_backward_normal<true, true>
501 template<typename _BI1, typename _BI2>
503 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
504 { return _BI2(std::__copy_backward_aux(__first.base(), __last.base(),
509 * @brief Copies the range [first,last) into result.
510 * @param first A bidirectional iterator.
511 * @param last A bidirectional iterator.
512 * @param result A bidirectional iterator.
513 * @return result - (first - last)
515 * The function has the same effect as copy, but starts at the end of the
516 * range and works its way to the start, returning the start of the result.
517 * This inline function will boil down to a call to @c memmove whenever
518 * possible. Failing that, if random access iterators are passed, then the
519 * loop count will be known (and therefore a candidate for compiler
520 * optimizations such as unrolling).
522 * Result may not be in the range [first,last). Use copy instead. Note
523 * that the start of the output range may overlap [first,last).
525 template <typename _BI1, typename _BI2>
527 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
529 // concept requirements
530 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
531 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
532 __glibcxx_function_requires(_ConvertibleConcept<
533 typename iterator_traits<_BI1>::value_type,
534 typename iterator_traits<_BI2>::value_type>)
535 __glibcxx_requires_valid_range(__first, __last);
537 const bool __bi1 = __is_normal_iterator<_BI1>::__value;
538 const bool __bi2 = __is_normal_iterator<_BI2>::__value;
539 return std::__copy_backward_normal<__bi1, __bi2>::__copy_b_n(__first,
548 template<typename _ForwardIterator, typename _Tp>
550 fill(_ForwardIterator __first, _ForwardIterator __last,
553 for (; __first != __last; ++__first)
561 template<typename _ForwardIterator, typename _Tp>
563 fill(_ForwardIterator __first, _ForwardIterator __last,
566 const _Tp __tmp = __value;
567 for (; __first != __last; ++__first)
572 template<typename _ForwardIterator, typename _Tp>
574 __fill_aux(_ForwardIterator __first, _ForwardIterator __last,
577 const bool __scalar = __is_scalar<_Tp>::__value;
578 std::__fill<__scalar>::fill(__first, __last, __value);
581 // Specialization: for char types we can use memset (wmemset).
583 __fill_aux(unsigned char* __first, unsigned char* __last,
584 const unsigned char& __c)
586 const unsigned char __tmp = __c;
587 std::memset(__first, __tmp, __last - __first);
591 __fill_aux(signed char* __first, signed char* __last,
592 const signed char& __c)
594 const signed char __tmp = __c;
595 std::memset(__first, static_cast<unsigned char>(__tmp), __last - __first);
599 __fill_aux(char* __first, char* __last, const char& __c)
601 const char __tmp = __c;
602 std::memset(__first, static_cast<unsigned char>(__tmp), __last - __first);
605 #ifdef _GLIBCXX_USE_WCHAR_T
607 __fill_aux(wchar_t* __first, wchar_t* __last, const wchar_t& __c)
609 const wchar_t __tmp = __c;
610 std::wmemset(__first, __tmp, __last - __first);
617 template<typename _ForwardIterator, typename _Tp>
619 __fill_n(_ForwardIterator __first, _ForwardIterator __last,
621 { std::__fill_aux(__first, __last, __value); }
625 struct __fill_normal<true>
627 template<typename _ForwardIterator, typename _Tp>
629 __fill_n(_ForwardIterator __first, _ForwardIterator __last,
631 { std::__fill_aux(__first.base(), __last.base(), __value); }
635 * @brief Fills the range [first,last) with copies of value.
636 * @param first A forward iterator.
637 * @param last A forward iterator.
638 * @param value A reference-to-const of arbitrary type.
641 * This function fills a range with copies of the same value. For char
642 * types filling contiguous areas of memory, this becomes an inline call
643 * to @c memset or @c wmemset.
645 template<typename _ForwardIterator, typename _Tp>
647 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
649 // concept requirements
650 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
652 __glibcxx_requires_valid_range(__first, __last);
654 const bool __fi = __is_normal_iterator<_ForwardIterator>::__value;
655 std::__fill_normal<__fi>::__fill_n(__first, __last, __value);
662 template<typename _OutputIterator, typename _Size, typename _Tp>
663 static _OutputIterator
664 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
666 for (; __n > 0; --__n, ++__first)
673 struct __fill_n<true>
675 template<typename _OutputIterator, typename _Size, typename _Tp>
676 static _OutputIterator
677 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
679 const _Tp __tmp = __value;
680 for (; __n > 0; --__n, ++__first)
686 template<typename _OutputIterator, typename _Size, typename _Tp>
687 inline _OutputIterator
688 __fill_n_aux(_OutputIterator __first, _Size __n, const _Tp& __value)
690 const bool __scalar = __is_scalar<_Tp>::__value;
691 return std::__fill_n<__scalar>::fill_n(__first, __n, __value);
694 template<typename _Size>
695 inline unsigned char*
696 __fill_n_aux(unsigned char* __first, _Size __n, const unsigned char& __c)
698 std::__fill_aux(__first, __first + __n, __c);
699 return __first + __n;
702 template<typename _Size>
704 __fill_n_aux(signed char* __first, _Size __n, const signed char& __c)
706 std::__fill_aux(__first, __first + __n, __c);
707 return __first + __n;
710 template<typename _Size>
712 __fill_n_aux(char* __first, _Size __n, const char& __c)
714 std::__fill_aux(__first, __first + __n, __c);
715 return __first + __n;
718 #ifdef _GLIBCXX_USE_WCHAR_T
719 template<typename _Size>
721 __fill_n_aux(wchar_t* __first, _Size __n, const wchar_t& __c)
723 std::__fill_aux(__first, __first + __n, __c);
724 return __first + __n;
729 struct __fill_n_normal
731 template<typename _OI, typename _Size, typename _Tp>
733 __fill_n_n(_OI __first, _Size __n, const _Tp& __value)
734 { return std::__fill_n_aux(__first, __n, __value); }
738 struct __fill_n_normal<true>
740 template<typename _OI, typename _Size, typename _Tp>
742 __fill_n_n(_OI __first, _Size __n, const _Tp& __value)
743 { return _OI(std::__fill_n_aux(__first.base(), __n, __value)); }
747 * @brief Fills the range [first,first+n) with copies of value.
748 * @param first An output iterator.
749 * @param n The count of copies to perform.
750 * @param value A reference-to-const of arbitrary type.
751 * @return The iterator at first+n.
753 * This function fills a range with copies of the same value. For char
754 * types filling contiguous areas of memory, this becomes an inline call
755 * to @c memset or @ wmemset.
757 template<typename _OutputIterator, typename _Size, typename _Tp>
758 inline _OutputIterator
759 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
761 // concept requirements
762 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, _Tp>)
764 const bool __oi = __is_normal_iterator<_OutputIterator>::__value;
765 return std::__fill_n_normal<__oi>::__fill_n_n(__first, __n, __value);
769 * @brief Finds the places in ranges which don't match.
770 * @param first1 An input iterator.
771 * @param last1 An input iterator.
772 * @param first2 An input iterator.
773 * @return A pair of iterators pointing to the first mismatch.
775 * This compares the elements of two ranges using @c == and returns a pair
776 * of iterators. The first iterator points into the first range, the
777 * second iterator points into the second range, and the elements pointed
778 * to by the iterators are not equal.
780 template<typename _InputIterator1, typename _InputIterator2>
781 pair<_InputIterator1, _InputIterator2>
782 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
783 _InputIterator2 __first2)
785 // concept requirements
786 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
787 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
788 __glibcxx_function_requires(_EqualOpConcept<
789 typename iterator_traits<_InputIterator1>::value_type,
790 typename iterator_traits<_InputIterator2>::value_type>)
791 __glibcxx_requires_valid_range(__first1, __last1);
793 while (__first1 != __last1 && *__first1 == *__first2)
798 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
802 * @brief Finds the places in ranges which don't match.
803 * @param first1 An input iterator.
804 * @param last1 An input iterator.
805 * @param first2 An input iterator.
806 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
807 * @return A pair of iterators pointing to the first mismatch.
809 * This compares the elements of two ranges using the binary_pred
810 * parameter, and returns a pair
811 * of iterators. The first iterator points into the first range, the
812 * second iterator points into the second range, and the elements pointed
813 * to by the iterators are not equal.
815 template<typename _InputIterator1, typename _InputIterator2,
816 typename _BinaryPredicate>
817 pair<_InputIterator1, _InputIterator2>
818 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
819 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
821 // concept requirements
822 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
823 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
824 __glibcxx_requires_valid_range(__first1, __last1);
826 while (__first1 != __last1 && __binary_pred(*__first1, *__first2))
831 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
835 * @brief Tests a range for element-wise equality.
836 * @param first1 An input iterator.
837 * @param last1 An input iterator.
838 * @param first2 An input iterator.
839 * @return A boolean true or false.
841 * This compares the elements of two ranges using @c == and returns true or
842 * false depending on whether all of the corresponding elements of the
845 template<typename _InputIterator1, typename _InputIterator2>
847 equal(_InputIterator1 __first1, _InputIterator1 __last1,
848 _InputIterator2 __first2)
850 // concept requirements
851 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
852 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
853 __glibcxx_function_requires(_EqualOpConcept<
854 typename iterator_traits<_InputIterator1>::value_type,
855 typename iterator_traits<_InputIterator2>::value_type>)
856 __glibcxx_requires_valid_range(__first1, __last1);
858 for (; __first1 != __last1; ++__first1, ++__first2)
859 if (!(*__first1 == *__first2))
865 * @brief Tests a range for element-wise equality.
866 * @param first1 An input iterator.
867 * @param last1 An input iterator.
868 * @param first2 An input iterator.
869 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
870 * @return A boolean true or false.
872 * This compares the elements of two ranges using the binary_pred
873 * parameter, and returns true or
874 * false depending on whether all of the corresponding elements of the
877 template<typename _InputIterator1, typename _InputIterator2,
878 typename _BinaryPredicate>
880 equal(_InputIterator1 __first1, _InputIterator1 __last1,
881 _InputIterator2 __first2,
882 _BinaryPredicate __binary_pred)
884 // concept requirements
885 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
886 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
887 __glibcxx_requires_valid_range(__first1, __last1);
889 for (; __first1 != __last1; ++__first1, ++__first2)
890 if (!__binary_pred(*__first1, *__first2))
896 * @brief Performs "dictionary" comparison on ranges.
897 * @param first1 An input iterator.
898 * @param last1 An input iterator.
899 * @param first2 An input iterator.
900 * @param last2 An input iterator.
901 * @return A boolean true or false.
903 * "Returns true if the sequence of elements defined by the range
904 * [first1,last1) is lexicographically less than the sequence of elements
905 * defined by the range [first2,last2). Returns false otherwise."
906 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
907 * then this is an inline call to @c memcmp.
909 template<typename _InputIterator1, typename _InputIterator2>
911 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
912 _InputIterator2 __first2, _InputIterator2 __last2)
914 // concept requirements
915 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
916 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
917 __glibcxx_function_requires(_LessThanOpConcept<
918 typename iterator_traits<_InputIterator1>::value_type,
919 typename iterator_traits<_InputIterator2>::value_type>)
920 __glibcxx_function_requires(_LessThanOpConcept<
921 typename iterator_traits<_InputIterator2>::value_type,
922 typename iterator_traits<_InputIterator1>::value_type>)
923 __glibcxx_requires_valid_range(__first1, __last1);
924 __glibcxx_requires_valid_range(__first2, __last2);
926 for (; __first1 != __last1 && __first2 != __last2;
927 ++__first1, ++__first2)
929 if (*__first1 < *__first2)
931 if (*__first2 < *__first1)
934 return __first1 == __last1 && __first2 != __last2;
938 * @brief Performs "dictionary" comparison on ranges.
939 * @param first1 An input iterator.
940 * @param last1 An input iterator.
941 * @param first2 An input iterator.
942 * @param last2 An input iterator.
943 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
944 * @return A boolean true or false.
946 * The same as the four-parameter @c lexigraphical_compare, but uses the
947 * comp parameter instead of @c <.
949 template<typename _InputIterator1, typename _InputIterator2,
952 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
953 _InputIterator2 __first2, _InputIterator2 __last2,
956 // concept requirements
957 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
958 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
959 __glibcxx_requires_valid_range(__first1, __last1);
960 __glibcxx_requires_valid_range(__first2, __last2);
962 for (; __first1 != __last1 && __first2 != __last2;
963 ++__first1, ++__first2)
965 if (__comp(*__first1, *__first2))
967 if (__comp(*__first2, *__first1))
970 return __first1 == __last1 && __first2 != __last2;
974 lexicographical_compare(const unsigned char* __first1,
975 const unsigned char* __last1,
976 const unsigned char* __first2,
977 const unsigned char* __last2)
979 __glibcxx_requires_valid_range(__first1, __last1);
980 __glibcxx_requires_valid_range(__first2, __last2);
982 const size_t __len1 = __last1 - __first1;
983 const size_t __len2 = __last2 - __first2;
984 const int __result = std::memcmp(__first1, __first2,
985 std::min(__len1, __len2));
986 return __result != 0 ? __result < 0 : __len1 < __len2;
990 lexicographical_compare(const char* __first1, const char* __last1,
991 const char* __first2, const char* __last2)
993 __glibcxx_requires_valid_range(__first1, __last1);
994 __glibcxx_requires_valid_range(__first2, __last2);
996 #if CHAR_MAX == SCHAR_MAX
997 return std::lexicographical_compare((const signed char*) __first1,
998 (const signed char*) __last1,
999 (const signed char*) __first2,
1000 (const signed char*) __last2);
1001 #else /* CHAR_MAX == SCHAR_MAX */
1002 return std::lexicographical_compare((const unsigned char*) __first1,
1003 (const unsigned char*) __last1,
1004 (const unsigned char*) __first2,
1005 (const unsigned char*) __last2);
1006 #endif /* CHAR_MAX == SCHAR_MAX */
1009 _GLIBCXX_END_NAMESPACE