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>
70 #include <bits/functexcept.h>
71 #include <bits/stl_pair.h>
72 #include <bits/cpp_type_traits.h>
73 #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 // NB: avoid including <iosfwd>, relatively large.
321 template<typename _CharT>
324 template<typename _CharT, typename _Traits>
325 class istreambuf_iterator;
327 template<typename _CharT, typename _Traits>
328 class ostreambuf_iterator;
330 template<typename _CharT>
331 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
332 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
333 __copy_aux(_CharT*, _CharT*,
334 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
336 template<typename _CharT>
337 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
338 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
339 __copy_aux(const _CharT*, const _CharT*,
340 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
342 template<typename _CharT>
343 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
345 __copy_aux(istreambuf_iterator<_CharT, char_traits<_CharT> >,
346 istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
351 template<typename _II, typename _OI>
353 __copy_n(_II __first, _II __last, _OI __result)
354 { return std::__copy_aux(__first, __last, __result); }
358 struct __copy_normal<true, false>
360 template<typename _II, typename _OI>
362 __copy_n(_II __first, _II __last, _OI __result)
363 { return std::__copy_aux(__first.base(), __last.base(), __result); }
367 struct __copy_normal<false, true>
369 template<typename _II, typename _OI>
371 __copy_n(_II __first, _II __last, _OI __result)
372 { return _OI(std::__copy_aux(__first, __last, __result.base())); }
376 struct __copy_normal<true, true>
378 template<typename _II, typename _OI>
380 __copy_n(_II __first, _II __last, _OI __result)
381 { return _OI(std::__copy_aux(__first.base(), __last.base(),
386 * @brief Copies the range [first,last) into result.
387 * @param first An input iterator.
388 * @param last An input iterator.
389 * @param result An output iterator.
390 * @return result + (first - last)
392 * This inline function will boil down to a call to @c memmove whenever
393 * possible. Failing that, if random access iterators are passed, then the
394 * loop count will be known (and therefore a candidate for compiler
395 * optimizations such as unrolling). Result may not be contained within
396 * [first,last); the copy_backward function should be used instead.
398 * Note that the end of the output range is permitted to be contained
399 * within [first,last).
401 template<typename _InputIterator, typename _OutputIterator>
402 inline _OutputIterator
403 copy(_InputIterator __first, _InputIterator __last,
404 _OutputIterator __result)
406 // concept requirements
407 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
408 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
409 typename iterator_traits<_InputIterator>::value_type>)
410 __glibcxx_requires_valid_range(__first, __last);
412 const bool __in = __is_normal_iterator<_InputIterator>::__value;
413 const bool __out = __is_normal_iterator<_OutputIterator>::__value;
414 return std::__copy_normal<__in, __out>::__copy_n(__first, __last,
418 template<bool, typename>
419 struct __copy_backward
421 template<typename _BI1, typename _BI2>
423 __copy_b(_BI1 __first, _BI1 __last, _BI2 __result)
425 while (__first != __last)
426 *--__result = *--__last;
431 template<bool _BoolType>
432 struct __copy_backward<_BoolType, random_access_iterator_tag>
434 template<typename _BI1, typename _BI2>
436 __copy_b(_BI1 __first, _BI1 __last, _BI2 __result)
438 typename iterator_traits<_BI1>::difference_type __n;
439 for (__n = __last - __first; __n > 0; --__n)
440 *--__result = *--__last;
446 struct __copy_backward<true, random_access_iterator_tag>
448 template<typename _Tp>
450 __copy_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
452 const ptrdiff_t _Num = __last - __first;
453 std::memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
454 return __result - _Num;
458 template<typename _BI1, typename _BI2>
460 __copy_backward_aux(_BI1 __first, _BI1 __last, _BI2 __result)
462 typedef typename iterator_traits<_BI1>::value_type _ValueType1;
463 typedef typename iterator_traits<_BI2>::value_type _ValueType2;
464 typedef typename iterator_traits<_BI1>::iterator_category _Category;
465 const bool __simple = (__is_scalar<_ValueType1>::__value
466 && __is_pointer<_BI1>::__value
467 && __is_pointer<_BI2>::__value
468 && __are_same<_ValueType1, _ValueType2>::__value);
470 return std::__copy_backward<__simple, _Category>::__copy_b(__first,
476 struct __copy_backward_normal
478 template<typename _BI1, typename _BI2>
480 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
481 { return std::__copy_backward_aux(__first, __last, __result); }
485 struct __copy_backward_normal<true, false>
487 template<typename _BI1, typename _BI2>
489 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
490 { return std::__copy_backward_aux(__first.base(), __last.base(),
495 struct __copy_backward_normal<false, true>
497 template<typename _BI1, typename _BI2>
499 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
500 { return _BI2(std::__copy_backward_aux(__first, __last,
505 struct __copy_backward_normal<true, true>
507 template<typename _BI1, typename _BI2>
509 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
510 { return _BI2(std::__copy_backward_aux(__first.base(), __last.base(),
515 * @brief Copies the range [first,last) into result.
516 * @param first A bidirectional iterator.
517 * @param last A bidirectional iterator.
518 * @param result A bidirectional iterator.
519 * @return result - (first - last)
521 * The function has the same effect as copy, but starts at the end of the
522 * range and works its way to the start, returning the start of the result.
523 * This inline function will boil down to a call to @c memmove whenever
524 * possible. Failing that, if random access iterators are passed, then the
525 * loop count will be known (and therefore a candidate for compiler
526 * optimizations such as unrolling).
528 * Result may not be in the range [first,last). Use copy instead. Note
529 * that the start of the output range may overlap [first,last).
531 template <typename _BI1, typename _BI2>
533 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
535 // concept requirements
536 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
537 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
538 __glibcxx_function_requires(_ConvertibleConcept<
539 typename iterator_traits<_BI1>::value_type,
540 typename iterator_traits<_BI2>::value_type>)
541 __glibcxx_requires_valid_range(__first, __last);
543 const bool __bi1 = __is_normal_iterator<_BI1>::__value;
544 const bool __bi2 = __is_normal_iterator<_BI2>::__value;
545 return std::__copy_backward_normal<__bi1, __bi2>::__copy_b_n(__first,
554 template<typename _ForwardIterator, typename _Tp>
556 fill(_ForwardIterator __first, _ForwardIterator __last,
559 for (; __first != __last; ++__first)
567 template<typename _ForwardIterator, typename _Tp>
569 fill(_ForwardIterator __first, _ForwardIterator __last,
572 const _Tp __tmp = __value;
573 for (; __first != __last; ++__first)
578 template<typename _ForwardIterator, typename _Tp>
580 __fill_aux(_ForwardIterator __first, _ForwardIterator __last,
583 const bool __scalar = __is_scalar<_Tp>::__value;
584 std::__fill<__scalar>::fill(__first, __last, __value);
587 // Specialization: for char types we can use memset (wmemset).
589 __fill_aux(unsigned char* __first, unsigned char* __last, unsigned char __c)
590 { std::memset(__first, __c, __last - __first); }
593 __fill_aux(signed char* __first, signed char* __last, signed char __c)
594 { std::memset(__first, static_cast<unsigned char>(__c), __last - __first); }
597 __fill_aux(char* __first, char* __last, char __c)
598 { std::memset(__first, static_cast<unsigned char>(__c), __last - __first); }
600 #ifdef _GLIBCXX_USE_WCHAR_T
602 __fill_aux(wchar_t* __first, wchar_t* __last, wchar_t __c)
603 { std::wmemset(__first, __c, __last - __first); }
609 template<typename _ForwardIterator, typename _Tp>
611 __fill_n(_ForwardIterator __first, _ForwardIterator __last,
613 { std::__fill_aux(__first, __last, __value); }
617 struct __fill_normal<true>
619 template<typename _ForwardIterator, typename _Tp>
621 __fill_n(_ForwardIterator __first, _ForwardIterator __last,
623 { std::__fill_aux(__first.base(), __last.base(), __value); }
627 * @brief Fills the range [first,last) with copies of value.
628 * @param first A forward iterator.
629 * @param last A forward iterator.
630 * @param value A reference-to-const of arbitrary type.
633 * This function fills a range with copies of the same value. For char
634 * types filling contiguous areas of memory, this becomes an inline call
635 * to @c memset or @c wmemset.
637 template<typename _ForwardIterator, typename _Tp>
639 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
641 // concept requirements
642 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
644 __glibcxx_requires_valid_range(__first, __last);
646 const bool __fi = __is_normal_iterator<_ForwardIterator>::__value;
647 std::__fill_normal<__fi>::__fill_n(__first, __last, __value);
654 template<typename _OutputIterator, typename _Size, typename _Tp>
655 static _OutputIterator
656 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
658 for (; __n > 0; --__n, ++__first)
665 struct __fill_n<true>
667 template<typename _OutputIterator, typename _Size, typename _Tp>
668 static _OutputIterator
669 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
671 const _Tp __tmp = __value;
672 for (; __n > 0; --__n, ++__first)
678 template<typename _OutputIterator, typename _Size, typename _Tp>
679 inline _OutputIterator
680 __fill_n_aux(_OutputIterator __first, _Size __n, const _Tp& __value)
682 const bool __scalar = __is_scalar<_Tp>::__value;
683 return std::__fill_n<__scalar>::fill_n(__first, __n, __value);
686 template<typename _Size>
687 inline unsigned char*
688 __fill_n_aux(unsigned char* __first, _Size __n, unsigned char __c)
690 std::__fill_aux(__first, __first + __n, __c);
691 return __first + __n;
694 template<typename _Size>
696 __fill_n_aux(signed char* __first, _Size __n, signed char __c)
698 std::__fill_aux(__first, __first + __n, __c);
699 return __first + __n;
702 template<typename _Size>
704 __fill_n_aux(char* __first, _Size __n, char __c)
706 std::__fill_aux(__first, __first + __n, __c);
707 return __first + __n;
710 #ifdef _GLIBCXX_USE_WCHAR_T
711 template<typename _Size>
713 __fill_n_aux(wchar_t* __first, _Size __n, wchar_t __c)
715 std::__fill_aux(__first, __first + __n, __c);
716 return __first + __n;
721 struct __fill_n_normal
723 template<typename _OI, typename _Size, typename _Tp>
725 __fill_n_n(_OI __first, _Size __n, const _Tp& __value)
726 { return std::__fill_n_aux(__first, __n, __value); }
730 struct __fill_n_normal<true>
732 template<typename _OI, typename _Size, typename _Tp>
734 __fill_n_n(_OI __first, _Size __n, const _Tp& __value)
735 { return _OI(std::__fill_n_aux(__first.base(), __n, __value)); }
739 * @brief Fills the range [first,first+n) with copies of value.
740 * @param first An output iterator.
741 * @param n The count of copies to perform.
742 * @param value A reference-to-const of arbitrary type.
743 * @return The iterator at first+n.
745 * This function fills a range with copies of the same value. For char
746 * types filling contiguous areas of memory, this becomes an inline call
747 * to @c memset or @ wmemset.
749 template<typename _OutputIterator, typename _Size, typename _Tp>
750 inline _OutputIterator
751 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
753 // concept requirements
754 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, _Tp>)
756 const bool __oi = __is_normal_iterator<_OutputIterator>::__value;
757 return std::__fill_n_normal<__oi>::__fill_n_n(__first, __n, __value);
761 * @brief Finds the places in ranges which don't match.
762 * @param first1 An input iterator.
763 * @param last1 An input iterator.
764 * @param first2 An input iterator.
765 * @return A pair of iterators pointing to the first mismatch.
767 * This compares the elements of two ranges using @c == and returns a pair
768 * of iterators. The first iterator points into the first range, the
769 * second iterator points into the second range, and the elements pointed
770 * to by the iterators are not equal.
772 template<typename _InputIterator1, typename _InputIterator2>
773 pair<_InputIterator1, _InputIterator2>
774 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
775 _InputIterator2 __first2)
777 // concept requirements
778 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
779 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
780 __glibcxx_function_requires(_EqualOpConcept<
781 typename iterator_traits<_InputIterator1>::value_type,
782 typename iterator_traits<_InputIterator2>::value_type>)
783 __glibcxx_requires_valid_range(__first1, __last1);
785 while (__first1 != __last1 && *__first1 == *__first2)
790 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
794 * @brief Finds the places in ranges which don't match.
795 * @param first1 An input iterator.
796 * @param last1 An input iterator.
797 * @param first2 An input iterator.
798 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
799 * @return A pair of iterators pointing to the first mismatch.
801 * This compares the elements of two ranges using the binary_pred
802 * parameter, and returns a pair
803 * of iterators. The first iterator points into the first range, the
804 * second iterator points into the second range, and the elements pointed
805 * to by the iterators are not equal.
807 template<typename _InputIterator1, typename _InputIterator2,
808 typename _BinaryPredicate>
809 pair<_InputIterator1, _InputIterator2>
810 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
811 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
813 // concept requirements
814 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
815 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
816 __glibcxx_requires_valid_range(__first1, __last1);
818 while (__first1 != __last1 && __binary_pred(*__first1, *__first2))
823 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
827 * @brief Tests a range for element-wise equality.
828 * @param first1 An input iterator.
829 * @param last1 An input iterator.
830 * @param first2 An input iterator.
831 * @return A boolean true or false.
833 * This compares the elements of two ranges using @c == and returns true or
834 * false depending on whether all of the corresponding elements of the
837 template<typename _InputIterator1, typename _InputIterator2>
839 equal(_InputIterator1 __first1, _InputIterator1 __last1,
840 _InputIterator2 __first2)
842 // concept requirements
843 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
844 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
845 __glibcxx_function_requires(_EqualOpConcept<
846 typename iterator_traits<_InputIterator1>::value_type,
847 typename iterator_traits<_InputIterator2>::value_type>)
848 __glibcxx_requires_valid_range(__first1, __last1);
850 for (; __first1 != __last1; ++__first1, ++__first2)
851 if (!(*__first1 == *__first2))
857 * @brief Tests a range for element-wise equality.
858 * @param first1 An input iterator.
859 * @param last1 An input iterator.
860 * @param first2 An input iterator.
861 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
862 * @return A boolean true or false.
864 * This compares the elements of two ranges using the binary_pred
865 * parameter, and returns true or
866 * false depending on whether all of the corresponding elements of the
869 template<typename _InputIterator1, typename _InputIterator2,
870 typename _BinaryPredicate>
872 equal(_InputIterator1 __first1, _InputIterator1 __last1,
873 _InputIterator2 __first2,
874 _BinaryPredicate __binary_pred)
876 // concept requirements
877 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
878 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
879 __glibcxx_requires_valid_range(__first1, __last1);
881 for (; __first1 != __last1; ++__first1, ++__first2)
882 if (!__binary_pred(*__first1, *__first2))
888 * @brief Performs "dictionary" comparison on ranges.
889 * @param first1 An input iterator.
890 * @param last1 An input iterator.
891 * @param first2 An input iterator.
892 * @param last2 An input iterator.
893 * @return A boolean true or false.
895 * "Returns true if the sequence of elements defined by the range
896 * [first1,last1) is lexicographically less than the sequence of elements
897 * defined by the range [first2,last2). Returns false otherwise."
898 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
899 * then this is an inline call to @c memcmp.
901 template<typename _InputIterator1, typename _InputIterator2>
903 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
904 _InputIterator2 __first2, _InputIterator2 __last2)
906 // concept requirements
907 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
908 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
909 __glibcxx_function_requires(_LessThanOpConcept<
910 typename iterator_traits<_InputIterator1>::value_type,
911 typename iterator_traits<_InputIterator2>::value_type>)
912 __glibcxx_function_requires(_LessThanOpConcept<
913 typename iterator_traits<_InputIterator2>::value_type,
914 typename iterator_traits<_InputIterator1>::value_type>)
915 __glibcxx_requires_valid_range(__first1, __last1);
916 __glibcxx_requires_valid_range(__first2, __last2);
918 for (; __first1 != __last1 && __first2 != __last2;
919 ++__first1, ++__first2)
921 if (*__first1 < *__first2)
923 if (*__first2 < *__first1)
926 return __first1 == __last1 && __first2 != __last2;
930 * @brief Performs "dictionary" comparison on ranges.
931 * @param first1 An input iterator.
932 * @param last1 An input iterator.
933 * @param first2 An input iterator.
934 * @param last2 An input iterator.
935 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
936 * @return A boolean true or false.
938 * The same as the four-parameter @c lexigraphical_compare, but uses the
939 * comp parameter instead of @c <.
941 template<typename _InputIterator1, typename _InputIterator2,
944 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
945 _InputIterator2 __first2, _InputIterator2 __last2,
948 // concept requirements
949 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
950 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
951 __glibcxx_requires_valid_range(__first1, __last1);
952 __glibcxx_requires_valid_range(__first2, __last2);
954 for (; __first1 != __last1 && __first2 != __last2;
955 ++__first1, ++__first2)
957 if (__comp(*__first1, *__first2))
959 if (__comp(*__first2, *__first1))
962 return __first1 == __last1 && __first2 != __last2;
966 lexicographical_compare(const unsigned char* __first1,
967 const unsigned char* __last1,
968 const unsigned char* __first2,
969 const unsigned char* __last2)
971 __glibcxx_requires_valid_range(__first1, __last1);
972 __glibcxx_requires_valid_range(__first2, __last2);
974 const size_t __len1 = __last1 - __first1;
975 const size_t __len2 = __last2 - __first2;
976 const int __result = std::memcmp(__first1, __first2,
977 std::min(__len1, __len2));
978 return __result != 0 ? __result < 0 : __len1 < __len2;
982 lexicographical_compare(const char* __first1, const char* __last1,
983 const char* __first2, const char* __last2)
985 __glibcxx_requires_valid_range(__first1, __last1);
986 __glibcxx_requires_valid_range(__first2, __last2);
988 if (std::numeric_limits<char>::is_signed)
989 return std::lexicographical_compare((const signed char*) __first1,
990 (const signed char*) __last1,
991 (const signed char*) __first2,
992 (const signed char*) __last2);
994 return std::lexicographical_compare((const unsigned char*) __first1,
995 (const unsigned char*) __last1,
996 (const unsigned char*) __first2,
997 (const unsigned char*) __last2);
1000 _GLIBCXX_END_NAMESPACE