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>
73 #include <ext/numeric_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 Swap the elements of two sequences.
172 * @param first1 A forward iterator.
173 * @param last1 A forward iterator.
174 * @param first2 A forward iterator.
175 * @return An iterator equal to @p first2+(last1-first1).
177 * Swaps each element in the range @p [first1,last1) with the
178 * corresponding element in the range @p [first2,(last1-first1)).
179 * The ranges must not overlap.
181 template<typename _ForwardIterator1, typename _ForwardIterator2>
183 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
184 _ForwardIterator2 __first2)
186 // concept requirements
187 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
189 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
191 __glibcxx_function_requires(_ConvertibleConcept<
192 typename iterator_traits<_ForwardIterator1>::value_type,
193 typename iterator_traits<_ForwardIterator2>::value_type>)
194 __glibcxx_function_requires(_ConvertibleConcept<
195 typename iterator_traits<_ForwardIterator2>::value_type,
196 typename iterator_traits<_ForwardIterator1>::value_type>)
197 __glibcxx_requires_valid_range(__first1, __last1);
199 for (; __first1 != __last1; ++__first1, ++__first2)
200 std::iter_swap(__first1, __first2);
205 * @brief This does what you think it does.
206 * @param a A thing of arbitrary type.
207 * @param b Another thing of arbitrary type.
208 * @return The lesser of the parameters.
210 * This is the simple classic generic implementation. It will work on
211 * temporary expressions, since they are only evaluated once, unlike a
212 * preprocessor macro.
214 template<typename _Tp>
216 min(const _Tp& __a, const _Tp& __b)
218 // concept requirements
219 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
220 //return __b < __a ? __b : __a;
227 * @brief This does what you think it does.
228 * @param a A thing of arbitrary type.
229 * @param b Another thing of arbitrary type.
230 * @return The greater of the parameters.
232 * This is the simple classic generic implementation. It will work on
233 * temporary expressions, since they are only evaluated once, unlike a
234 * preprocessor macro.
236 template<typename _Tp>
238 max(const _Tp& __a, const _Tp& __b)
240 // concept requirements
241 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
242 //return __a < __b ? __b : __a;
249 * @brief This does what you think it does.
250 * @param a A thing of arbitrary type.
251 * @param b Another thing of arbitrary type.
252 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
253 * @return The lesser of the parameters.
255 * This will work on temporary expressions, since they are only evaluated
256 * once, unlike a preprocessor macro.
258 template<typename _Tp, typename _Compare>
260 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
262 //return __comp(__b, __a) ? __b : __a;
263 if (__comp(__b, __a))
269 * @brief This does what you think it does.
270 * @param a A thing of arbitrary type.
271 * @param b Another thing of arbitrary type.
272 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
273 * @return The greater of the parameters.
275 * This will work on temporary expressions, since they are only evaluated
276 * once, unlike a preprocessor macro.
278 template<typename _Tp, typename _Compare>
280 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
282 //return __comp(__a, __b) ? __b : __a;
283 if (__comp(__a, __b))
288 // All of these auxiliary structs serve two purposes. (1) Replace
289 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
290 // because the input and output ranges are permitted to overlap.)
291 // (2) If we're using random access iterators, then write the loop as
292 // a for loop with an explicit count.
294 template<bool, typename>
297 template<typename _II, typename _OI>
299 copy(_II __first, _II __last, _OI __result)
301 for (; __first != __last; ++__result, ++__first)
302 *__result = *__first;
307 template<bool _BoolType>
308 struct __copy<_BoolType, random_access_iterator_tag>
310 template<typename _II, typename _OI>
312 copy(_II __first, _II __last, _OI __result)
314 typedef typename iterator_traits<_II>::difference_type _Distance;
315 for(_Distance __n = __last - __first; __n > 0; --__n)
317 *__result = *__first;
326 struct __copy<true, random_access_iterator_tag>
328 template<typename _Tp>
330 copy(const _Tp* __first, const _Tp* __last, _Tp* __result)
332 std::memmove(__result, __first, sizeof(_Tp) * (__last - __first));
333 return __result + (__last - __first);
337 template<typename _II, typename _OI>
339 __copy_aux(_II __first, _II __last, _OI __result)
341 typedef typename iterator_traits<_II>::value_type _ValueTypeI;
342 typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
343 typedef typename iterator_traits<_II>::iterator_category _Category;
344 const bool __simple = (__is_pod(_ValueTypeI)
345 && __is_pointer<_II>::__value
346 && __is_pointer<_OI>::__value
347 && __are_same<_ValueTypeI, _ValueTypeO>::__value);
349 return std::__copy<__simple, _Category>::copy(__first, __last, __result);
352 // Helpers for streambuf iterators (either istream or ostream).
353 // NB: avoid including <iosfwd>, relatively large.
354 template<typename _CharT>
357 template<typename _CharT, typename _Traits>
358 class istreambuf_iterator;
360 template<typename _CharT, typename _Traits>
361 class ostreambuf_iterator;
363 template<typename _CharT>
364 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
365 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
366 __copy_aux(_CharT*, _CharT*,
367 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
369 template<typename _CharT>
370 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
371 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
372 __copy_aux(const _CharT*, const _CharT*,
373 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
375 template<typename _CharT>
376 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
378 __copy_aux(istreambuf_iterator<_CharT, char_traits<_CharT> >,
379 istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
384 template<typename _II, typename _OI>
386 __copy_n(_II __first, _II __last, _OI __result)
387 { return std::__copy_aux(__first, __last, __result); }
391 struct __copy_normal<true, false>
393 template<typename _II, typename _OI>
395 __copy_n(_II __first, _II __last, _OI __result)
396 { return std::__copy_aux(__first.base(), __last.base(), __result); }
400 struct __copy_normal<false, true>
402 template<typename _II, typename _OI>
404 __copy_n(_II __first, _II __last, _OI __result)
405 { return _OI(std::__copy_aux(__first, __last, __result.base())); }
409 struct __copy_normal<true, true>
411 template<typename _II, typename _OI>
413 __copy_n(_II __first, _II __last, _OI __result)
414 { return _OI(std::__copy_aux(__first.base(), __last.base(),
419 * @brief Copies the range [first,last) into result.
420 * @param first An input iterator.
421 * @param last An input iterator.
422 * @param result An output iterator.
423 * @return result + (first - last)
425 * This inline function will boil down to a call to @c memmove whenever
426 * possible. Failing that, if random access iterators are passed, then the
427 * loop count will be known (and therefore a candidate for compiler
428 * optimizations such as unrolling). Result may not be contained within
429 * [first,last); the copy_backward function should be used instead.
431 * Note that the end of the output range is permitted to be contained
432 * within [first,last).
434 template<typename _InputIterator, typename _OutputIterator>
435 inline _OutputIterator
436 copy(_InputIterator __first, _InputIterator __last,
437 _OutputIterator __result)
439 // concept requirements
440 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
441 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
442 typename iterator_traits<_InputIterator>::value_type>)
443 __glibcxx_requires_valid_range(__first, __last);
445 const bool __in = __is_normal_iterator<_InputIterator>::__value;
446 const bool __out = __is_normal_iterator<_OutputIterator>::__value;
447 return std::__copy_normal<__in, __out>::__copy_n(__first, __last,
451 template<bool, typename>
452 struct __copy_backward
454 template<typename _BI1, typename _BI2>
456 __copy_b(_BI1 __first, _BI1 __last, _BI2 __result)
458 while (__first != __last)
459 *--__result = *--__last;
464 template<bool _BoolType>
465 struct __copy_backward<_BoolType, random_access_iterator_tag>
467 template<typename _BI1, typename _BI2>
469 __copy_b(_BI1 __first, _BI1 __last, _BI2 __result)
471 typename iterator_traits<_BI1>::difference_type __n;
472 for (__n = __last - __first; __n > 0; --__n)
473 *--__result = *--__last;
479 struct __copy_backward<true, random_access_iterator_tag>
481 template<typename _Tp>
483 __copy_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
485 const ptrdiff_t _Num = __last - __first;
486 std::memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
487 return __result - _Num;
491 template<typename _BI1, typename _BI2>
493 __copy_backward_aux(_BI1 __first, _BI1 __last, _BI2 __result)
495 typedef typename iterator_traits<_BI1>::value_type _ValueType1;
496 typedef typename iterator_traits<_BI2>::value_type _ValueType2;
497 typedef typename iterator_traits<_BI1>::iterator_category _Category;
498 const bool __simple = (__is_pod(_ValueType1)
499 && __is_pointer<_BI1>::__value
500 && __is_pointer<_BI2>::__value
501 && __are_same<_ValueType1, _ValueType2>::__value);
503 return std::__copy_backward<__simple, _Category>::__copy_b(__first,
509 struct __copy_backward_normal
511 template<typename _BI1, typename _BI2>
513 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
514 { return std::__copy_backward_aux(__first, __last, __result); }
518 struct __copy_backward_normal<true, false>
520 template<typename _BI1, typename _BI2>
522 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
523 { return std::__copy_backward_aux(__first.base(), __last.base(),
528 struct __copy_backward_normal<false, true>
530 template<typename _BI1, typename _BI2>
532 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
533 { return _BI2(std::__copy_backward_aux(__first, __last,
538 struct __copy_backward_normal<true, true>
540 template<typename _BI1, typename _BI2>
542 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
543 { return _BI2(std::__copy_backward_aux(__first.base(), __last.base(),
548 * @brief Copies the range [first,last) into result.
549 * @param first A bidirectional iterator.
550 * @param last A bidirectional iterator.
551 * @param result A bidirectional iterator.
552 * @return result - (first - last)
554 * The function has the same effect as copy, but starts at the end of the
555 * range and works its way to the start, returning the start of the result.
556 * This inline function will boil down to a call to @c memmove whenever
557 * possible. Failing that, if random access iterators are passed, then the
558 * loop count will be known (and therefore a candidate for compiler
559 * optimizations such as unrolling).
561 * Result may not be in the range [first,last). Use copy instead. Note
562 * that the start of the output range may overlap [first,last).
564 template <typename _BI1, typename _BI2>
566 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
568 // concept requirements
569 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
570 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
571 __glibcxx_function_requires(_ConvertibleConcept<
572 typename iterator_traits<_BI1>::value_type,
573 typename iterator_traits<_BI2>::value_type>)
574 __glibcxx_requires_valid_range(__first, __last);
576 const bool __bi1 = __is_normal_iterator<_BI1>::__value;
577 const bool __bi2 = __is_normal_iterator<_BI2>::__value;
578 return std::__copy_backward_normal<__bi1, __bi2>::__copy_b_n(__first,
587 template<typename _ForwardIterator, typename _Tp>
589 fill(_ForwardIterator __first, _ForwardIterator __last,
592 for (; __first != __last; ++__first)
600 template<typename _ForwardIterator, typename _Tp>
602 fill(_ForwardIterator __first, _ForwardIterator __last,
605 const _Tp __tmp = __value;
606 for (; __first != __last; ++__first)
611 template<typename _ForwardIterator, typename _Tp>
613 __fill_aux(_ForwardIterator __first, _ForwardIterator __last,
616 const bool __scalar = __is_scalar<_Tp>::__value;
617 std::__fill<__scalar>::fill(__first, __last, __value);
620 // Specialization: for char types we can use memset (wmemset).
622 __fill_aux(unsigned char* __first, unsigned char* __last, unsigned char __c)
623 { std::memset(__first, __c, __last - __first); }
626 __fill_aux(signed char* __first, signed char* __last, signed char __c)
627 { std::memset(__first, static_cast<unsigned char>(__c), __last - __first); }
630 __fill_aux(char* __first, char* __last, char __c)
631 { std::memset(__first, static_cast<unsigned char>(__c), __last - __first); }
633 #ifdef _GLIBCXX_USE_WCHAR_T
635 __fill_aux(wchar_t* __first, wchar_t* __last, wchar_t __c)
636 { std::wmemset(__first, __c, __last - __first); }
642 template<typename _ForwardIterator, typename _Tp>
644 __fill_n(_ForwardIterator __first, _ForwardIterator __last,
646 { std::__fill_aux(__first, __last, __value); }
650 struct __fill_normal<true>
652 template<typename _ForwardIterator, typename _Tp>
654 __fill_n(_ForwardIterator __first, _ForwardIterator __last,
656 { std::__fill_aux(__first.base(), __last.base(), __value); }
660 * @brief Fills the range [first,last) with copies of value.
661 * @param first A forward iterator.
662 * @param last A forward iterator.
663 * @param value A reference-to-const of arbitrary type.
666 * This function fills a range with copies of the same value. For char
667 * types filling contiguous areas of memory, this becomes an inline call
668 * to @c memset or @c wmemset.
670 template<typename _ForwardIterator, typename _Tp>
672 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
674 // concept requirements
675 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
677 __glibcxx_requires_valid_range(__first, __last);
679 const bool __fi = __is_normal_iterator<_ForwardIterator>::__value;
680 std::__fill_normal<__fi>::__fill_n(__first, __last, __value);
687 template<typename _OutputIterator, typename _Size, typename _Tp>
688 static _OutputIterator
689 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
691 for (; __n > 0; --__n, ++__first)
698 struct __fill_n<true>
700 template<typename _OutputIterator, typename _Size, typename _Tp>
701 static _OutputIterator
702 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
704 const _Tp __tmp = __value;
705 for (; __n > 0; --__n, ++__first)
711 template<typename _OutputIterator, typename _Size, typename _Tp>
712 inline _OutputIterator
713 __fill_n_aux(_OutputIterator __first, _Size __n, const _Tp& __value)
715 const bool __scalar = __is_scalar<_Tp>::__value;
716 return std::__fill_n<__scalar>::fill_n(__first, __n, __value);
719 template<typename _Size>
720 inline unsigned char*
721 __fill_n_aux(unsigned char* __first, _Size __n, unsigned char __c)
723 std::__fill_aux(__first, __first + __n, __c);
724 return __first + __n;
727 template<typename _Size>
729 __fill_n_aux(signed char* __first, _Size __n, signed char __c)
731 std::__fill_aux(__first, __first + __n, __c);
732 return __first + __n;
735 template<typename _Size>
737 __fill_n_aux(char* __first, _Size __n, char __c)
739 std::__fill_aux(__first, __first + __n, __c);
740 return __first + __n;
743 #ifdef _GLIBCXX_USE_WCHAR_T
744 template<typename _Size>
746 __fill_n_aux(wchar_t* __first, _Size __n, wchar_t __c)
748 std::__fill_aux(__first, __first + __n, __c);
749 return __first + __n;
754 struct __fill_n_normal
756 template<typename _OI, typename _Size, typename _Tp>
758 __fill_n_n(_OI __first, _Size __n, const _Tp& __value)
759 { return std::__fill_n_aux(__first, __n, __value); }
763 struct __fill_n_normal<true>
765 template<typename _OI, typename _Size, typename _Tp>
767 __fill_n_n(_OI __first, _Size __n, const _Tp& __value)
768 { return _OI(std::__fill_n_aux(__first.base(), __n, __value)); }
772 * @brief Fills the range [first,first+n) with copies of value.
773 * @param first An output iterator.
774 * @param n The count of copies to perform.
775 * @param value A reference-to-const of arbitrary type.
776 * @return The iterator at first+n.
778 * This function fills a range with copies of the same value. For char
779 * types filling contiguous areas of memory, this becomes an inline call
780 * to @c memset or @ wmemset.
782 template<typename _OutputIterator, typename _Size, typename _Tp>
783 inline _OutputIterator
784 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
786 // concept requirements
787 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, _Tp>)
789 const bool __oi = __is_normal_iterator<_OutputIterator>::__value;
790 return std::__fill_n_normal<__oi>::__fill_n_n(__first, __n, __value);
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 * @return A pair of iterators pointing to the first mismatch.
800 * This compares the elements of two ranges using @c == and returns a pair
801 * of iterators. The first iterator points into the first range, the
802 * second iterator points into the second range, and the elements pointed
803 * to by the iterators are not equal.
805 template<typename _InputIterator1, typename _InputIterator2>
806 pair<_InputIterator1, _InputIterator2>
807 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
808 _InputIterator2 __first2)
810 // concept requirements
811 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
812 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
813 __glibcxx_function_requires(_EqualOpConcept<
814 typename iterator_traits<_InputIterator1>::value_type,
815 typename iterator_traits<_InputIterator2>::value_type>)
816 __glibcxx_requires_valid_range(__first1, __last1);
818 while (__first1 != __last1 && *__first1 == *__first2)
823 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
827 * @brief Finds the places in ranges which don't match.
828 * @param first1 An input iterator.
829 * @param last1 An input iterator.
830 * @param first2 An input iterator.
831 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
832 * @return A pair of iterators pointing to the first mismatch.
834 * This compares the elements of two ranges using the binary_pred
835 * parameter, and returns a pair
836 * of iterators. The first iterator points into the first range, the
837 * second iterator points into the second range, and the elements pointed
838 * to by the iterators are not equal.
840 template<typename _InputIterator1, typename _InputIterator2,
841 typename _BinaryPredicate>
842 pair<_InputIterator1, _InputIterator2>
843 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
844 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
846 // concept requirements
847 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
848 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
849 __glibcxx_requires_valid_range(__first1, __last1);
851 while (__first1 != __last1 && bool(__binary_pred(*__first1, *__first2)))
856 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
860 * @brief Tests a range for element-wise equality.
861 * @param first1 An input iterator.
862 * @param last1 An input iterator.
863 * @param first2 An input iterator.
864 * @return A boolean true or false.
866 * This compares the elements of two ranges using @c == and returns true or
867 * false depending on whether all of the corresponding elements of the
870 template<typename _InputIterator1, typename _InputIterator2>
872 equal(_InputIterator1 __first1, _InputIterator1 __last1,
873 _InputIterator2 __first2)
875 // concept requirements
876 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
877 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
878 __glibcxx_function_requires(_EqualOpConcept<
879 typename iterator_traits<_InputIterator1>::value_type,
880 typename iterator_traits<_InputIterator2>::value_type>)
881 __glibcxx_requires_valid_range(__first1, __last1);
883 for (; __first1 != __last1; ++__first1, ++__first2)
884 if (!(*__first1 == *__first2))
890 * @brief Tests a range for element-wise equality.
891 * @param first1 An input iterator.
892 * @param last1 An input iterator.
893 * @param first2 An input iterator.
894 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
895 * @return A boolean true or false.
897 * This compares the elements of two ranges using the binary_pred
898 * parameter, and returns true or
899 * false depending on whether all of the corresponding elements of the
902 template<typename _InputIterator1, typename _InputIterator2,
903 typename _BinaryPredicate>
905 equal(_InputIterator1 __first1, _InputIterator1 __last1,
906 _InputIterator2 __first2,
907 _BinaryPredicate __binary_pred)
909 // concept requirements
910 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
911 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
912 __glibcxx_requires_valid_range(__first1, __last1);
914 for (; __first1 != __last1; ++__first1, ++__first2)
915 if (!bool(__binary_pred(*__first1, *__first2)))
921 * @brief Performs "dictionary" comparison on ranges.
922 * @param first1 An input iterator.
923 * @param last1 An input iterator.
924 * @param first2 An input iterator.
925 * @param last2 An input iterator.
926 * @return A boolean true or false.
928 * "Returns true if the sequence of elements defined by the range
929 * [first1,last1) is lexicographically less than the sequence of elements
930 * defined by the range [first2,last2). Returns false otherwise."
931 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
932 * then this is an inline call to @c memcmp.
934 template<typename _InputIterator1, typename _InputIterator2>
936 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
937 _InputIterator2 __first2, _InputIterator2 __last2)
939 // concept requirements
940 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
941 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
942 __glibcxx_function_requires(_LessThanOpConcept<
943 typename iterator_traits<_InputIterator1>::value_type,
944 typename iterator_traits<_InputIterator2>::value_type>)
945 __glibcxx_function_requires(_LessThanOpConcept<
946 typename iterator_traits<_InputIterator2>::value_type,
947 typename iterator_traits<_InputIterator1>::value_type>)
948 __glibcxx_requires_valid_range(__first1, __last1);
949 __glibcxx_requires_valid_range(__first2, __last2);
951 for (; __first1 != __last1 && __first2 != __last2;
952 ++__first1, ++__first2)
954 if (*__first1 < *__first2)
956 if (*__first2 < *__first1)
959 return __first1 == __last1 && __first2 != __last2;
963 * @brief Performs "dictionary" comparison on ranges.
964 * @param first1 An input iterator.
965 * @param last1 An input iterator.
966 * @param first2 An input iterator.
967 * @param last2 An input iterator.
968 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
969 * @return A boolean true or false.
971 * The same as the four-parameter @c lexigraphical_compare, but uses the
972 * comp parameter instead of @c <.
974 template<typename _InputIterator1, typename _InputIterator2,
977 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
978 _InputIterator2 __first2, _InputIterator2 __last2,
981 // concept requirements
982 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
983 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
984 __glibcxx_requires_valid_range(__first1, __last1);
985 __glibcxx_requires_valid_range(__first2, __last2);
987 for (; __first1 != __last1 && __first2 != __last2;
988 ++__first1, ++__first2)
990 if (__comp(*__first1, *__first2))
992 if (__comp(*__first2, *__first1))
995 return __first1 == __last1 && __first2 != __last2;
999 lexicographical_compare(const unsigned char* __first1,
1000 const unsigned char* __last1,
1001 const unsigned char* __first2,
1002 const unsigned char* __last2)
1004 __glibcxx_requires_valid_range(__first1, __last1);
1005 __glibcxx_requires_valid_range(__first2, __last2);
1007 const size_t __len1 = __last1 - __first1;
1008 const size_t __len2 = __last2 - __first2;
1009 const int __result = std::memcmp(__first1, __first2,
1010 std::min(__len1, __len2));
1011 return __result != 0 ? __result < 0 : __len1 < __len2;
1015 lexicographical_compare(const char* __first1, const char* __last1,
1016 const char* __first2, const char* __last2)
1018 __glibcxx_requires_valid_range(__first1, __last1);
1019 __glibcxx_requires_valid_range(__first2, __last2);
1021 if (__gnu_cxx::__numeric_traits<char>::__is_signed)
1022 return std::lexicographical_compare((const signed char*) __first1,
1023 (const signed char*) __last1,
1024 (const signed char*) __first2,
1025 (const signed char*) __last2);
1027 return std::lexicographical_compare((const unsigned char*) __first1,
1028 (const unsigned char*) __last1,
1029 (const unsigned char*) __first2,
1030 (const unsigned char*) __last2);
1033 _GLIBCXX_END_NAMESPACE