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>
67 #include <bits/functexcept.h>
68 #include <bits/stl_pair.h>
69 #include <bits/cpp_type_traits.h>
70 #include <ext/type_traits.h>
71 #include <ext/numeric_traits.h>
72 #include <bits/stl_iterator_base_types.h>
73 #include <bits/stl_iterator_base_funcs.h>
74 #include <bits/stl_iterator.h>
75 #include <bits/concept_check.h>
76 #include <debug/debug.h>
78 _GLIBCXX_BEGIN_NAMESPACE(std)
81 * @brief Swaps two values.
82 * @param a A thing of arbitrary type.
83 * @param b Another thing of arbitrary type.
86 * This is the simple classic generic implementation. It will work on
87 * any type which has a copy constructor and an assignment operator.
89 template<typename _Tp>
91 swap(_Tp& __a, _Tp& __b)
93 // concept requirements
94 __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
101 // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
102 // nutshell, we are partially implementing the resolution of DR 187,
103 // when it's safe, i.e., the value_types are equal.
104 template<bool _BoolType>
107 template<typename _ForwardIterator1, typename _ForwardIterator2>
109 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
111 typedef typename iterator_traits<_ForwardIterator1>::value_type
113 _ValueType1 __tmp = *__a;
120 struct __iter_swap<true>
122 template<typename _ForwardIterator1, typename _ForwardIterator2>
124 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
131 * @brief Swaps the contents of two iterators.
132 * @param a An iterator.
133 * @param b Another iterator.
136 * This function swaps the values pointed to by two iterators, not the
137 * iterators themselves.
139 template<typename _ForwardIterator1, typename _ForwardIterator2>
141 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
143 typedef typename iterator_traits<_ForwardIterator1>::value_type
145 typedef typename iterator_traits<_ForwardIterator2>::value_type
148 // concept requirements
149 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
151 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
153 __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
155 __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
158 typedef typename iterator_traits<_ForwardIterator1>::reference
160 typedef typename iterator_traits<_ForwardIterator2>::reference
162 std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value
163 && __are_same<_ValueType1&, _ReferenceType1>::__value
164 && __are_same<_ValueType2&, _ReferenceType2>::__value>::
169 * @brief Swap the elements of two sequences.
170 * @param first1 A forward iterator.
171 * @param last1 A forward iterator.
172 * @param first2 A forward iterator.
173 * @return An iterator equal to @p first2+(last1-first1).
175 * Swaps each element in the range @p [first1,last1) with the
176 * corresponding element in the range @p [first2,(last1-first1)).
177 * The ranges must not overlap.
179 template<typename _ForwardIterator1, typename _ForwardIterator2>
181 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
182 _ForwardIterator2 __first2)
184 // concept requirements
185 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
187 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
189 __glibcxx_function_requires(_ConvertibleConcept<
190 typename iterator_traits<_ForwardIterator1>::value_type,
191 typename iterator_traits<_ForwardIterator2>::value_type>)
192 __glibcxx_function_requires(_ConvertibleConcept<
193 typename iterator_traits<_ForwardIterator2>::value_type,
194 typename iterator_traits<_ForwardIterator1>::value_type>)
195 __glibcxx_requires_valid_range(__first1, __last1);
197 for (; __first1 != __last1; ++__first1, ++__first2)
198 std::iter_swap(__first1, __first2);
203 * @brief This does what you think it does.
204 * @param a A thing of arbitrary type.
205 * @param b Another thing of arbitrary type.
206 * @return The lesser of the parameters.
208 * This is the simple classic generic implementation. It will work on
209 * temporary expressions, since they are only evaluated once, unlike a
210 * preprocessor macro.
212 template<typename _Tp>
214 min(const _Tp& __a, const _Tp& __b)
216 // concept requirements
217 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
218 //return __b < __a ? __b : __a;
225 * @brief This does what you think it does.
226 * @param a A thing of arbitrary type.
227 * @param b Another thing of arbitrary type.
228 * @return The greater of the parameters.
230 * This is the simple classic generic implementation. It will work on
231 * temporary expressions, since they are only evaluated once, unlike a
232 * preprocessor macro.
234 template<typename _Tp>
236 max(const _Tp& __a, const _Tp& __b)
238 // concept requirements
239 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
240 //return __a < __b ? __b : __a;
247 * @brief This does what you think it does.
248 * @param a A thing of arbitrary type.
249 * @param b Another thing of arbitrary type.
250 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
251 * @return The lesser of the parameters.
253 * This will work on temporary expressions, since they are only evaluated
254 * once, unlike a preprocessor macro.
256 template<typename _Tp, typename _Compare>
258 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
260 //return __comp(__b, __a) ? __b : __a;
261 if (__comp(__b, __a))
267 * @brief This does what you think it does.
268 * @param a A thing of arbitrary type.
269 * @param b Another thing of arbitrary type.
270 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
271 * @return The greater of the parameters.
273 * This will work on temporary expressions, since they are only evaluated
274 * once, unlike a preprocessor macro.
276 template<typename _Tp, typename _Compare>
278 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
280 //return __comp(__a, __b) ? __b : __a;
281 if (__comp(__a, __b))
287 // If _Iterator is a __normal_iterator return its base (a plain pointer,
288 // normally) otherwise return it untouched. See copy, fill, ...
289 template<typename _Iterator,
290 bool _BoolType = __is_normal_iterator<_Iterator>::__value>
293 static const _Iterator&
294 __b(const _Iterator& __it)
298 template<typename _Iterator>
299 struct __niter_base<_Iterator, true>
301 static const typename _Iterator::_Iterator_type&
302 __b(const _Iterator& __it)
303 { return __it.base(); }
306 // All of these auxiliary structs serve two purposes. (1) Replace
307 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
308 // because the input and output ranges are permitted to overlap.)
309 // (2) If we're using random access iterators, then write the loop as
310 // a for loop with an explicit count.
312 template<bool, typename>
315 template<typename _II, typename _OI>
317 copy(_II __first, _II __last, _OI __result)
319 for (; __first != __last; ++__result, ++__first)
320 *__result = *__first;
325 template<bool _BoolType>
326 struct __copy<_BoolType, random_access_iterator_tag>
328 template<typename _II, typename _OI>
330 copy(_II __first, _II __last, _OI __result)
332 typedef typename iterator_traits<_II>::difference_type _Distance;
333 for(_Distance __n = __last - __first; __n > 0; --__n)
335 *__result = *__first;
344 struct __copy<true, random_access_iterator_tag>
346 template<typename _Tp>
348 copy(const _Tp* __first, const _Tp* __last, _Tp* __result)
350 __builtin_memmove(__result, __first,
351 sizeof(_Tp) * (__last - __first));
352 return __result + (__last - __first);
356 template<typename _II, typename _OI>
358 __copy_aux(_II __first, _II __last, _OI __result)
360 typedef typename iterator_traits<_II>::value_type _ValueTypeI;
361 typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
362 typedef typename iterator_traits<_II>::iterator_category _Category;
363 const bool __simple = (__is_pod(_ValueTypeI)
364 && __is_pointer<_II>::__value
365 && __is_pointer<_OI>::__value
366 && __are_same<_ValueTypeI, _ValueTypeO>::__value);
368 return std::__copy<__simple, _Category>::copy(__first, __last, __result);
371 // Helpers for streambuf iterators (either istream or ostream).
372 // NB: avoid including <iosfwd>, relatively large.
373 template<typename _CharT>
376 template<typename _CharT, typename _Traits>
377 class istreambuf_iterator;
379 template<typename _CharT, typename _Traits>
380 class ostreambuf_iterator;
382 template<typename _CharT>
383 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
384 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
385 __copy_aux(_CharT*, _CharT*,
386 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
388 template<typename _CharT>
389 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
390 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
391 __copy_aux(const _CharT*, const _CharT*,
392 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
394 template<typename _CharT>
395 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
397 __copy_aux(istreambuf_iterator<_CharT, char_traits<_CharT> >,
398 istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
401 * @brief Copies the range [first,last) into result.
402 * @param first An input iterator.
403 * @param last An input iterator.
404 * @param result An output iterator.
405 * @return result + (first - last)
407 * This inline function will boil down to a call to @c memmove whenever
408 * possible. Failing that, if random access iterators are passed, then the
409 * loop count will be known (and therefore a candidate for compiler
410 * optimizations such as unrolling). Result may not be contained within
411 * [first,last); the copy_backward function should be used instead.
413 * Note that the end of the output range is permitted to be contained
414 * within [first,last).
416 template<typename _II, typename _OI>
418 copy(_II __first, _II __last, _OI __result)
420 // concept requirements
421 __glibcxx_function_requires(_InputIteratorConcept<_II>)
422 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
423 typename iterator_traits<_II>::value_type>)
424 __glibcxx_requires_valid_range(__first, __last);
426 return _OI(std::__copy_aux(__niter_base<_II>::__b(__first),
427 __niter_base<_II>::__b(__last),
428 __niter_base<_OI>::__b(__result)));
432 template<bool, typename>
433 struct __copy_backward
435 template<typename _BI1, typename _BI2>
437 __copy_b(_BI1 __first, _BI1 __last, _BI2 __result)
439 while (__first != __last)
440 *--__result = *--__last;
445 template<bool _BoolType>
446 struct __copy_backward<_BoolType, random_access_iterator_tag>
448 template<typename _BI1, typename _BI2>
450 __copy_b(_BI1 __first, _BI1 __last, _BI2 __result)
452 typename iterator_traits<_BI1>::difference_type __n;
453 for (__n = __last - __first; __n > 0; --__n)
454 *--__result = *--__last;
460 struct __copy_backward<true, random_access_iterator_tag>
462 template<typename _Tp>
464 __copy_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
466 const ptrdiff_t _Num = __last - __first;
467 __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
468 return __result - _Num;
472 template<typename _BI1, typename _BI2>
474 __copy_backward_aux(_BI1 __first, _BI1 __last, _BI2 __result)
476 typedef typename iterator_traits<_BI1>::value_type _ValueType1;
477 typedef typename iterator_traits<_BI2>::value_type _ValueType2;
478 typedef typename iterator_traits<_BI1>::iterator_category _Category;
479 const bool __simple = (__is_pod(_ValueType1)
480 && __is_pointer<_BI1>::__value
481 && __is_pointer<_BI2>::__value
482 && __are_same<_ValueType1, _ValueType2>::__value);
484 return std::__copy_backward<__simple, _Category>::__copy_b(__first,
490 * @brief Copies the range [first,last) into result.
491 * @param first A bidirectional iterator.
492 * @param last A bidirectional iterator.
493 * @param result A bidirectional iterator.
494 * @return result - (first - last)
496 * The function has the same effect as copy, but starts at the end of the
497 * range and works its way to the start, returning the start of the result.
498 * This inline function will boil down to a call to @c memmove whenever
499 * possible. Failing that, if random access iterators are passed, then the
500 * loop count will be known (and therefore a candidate for compiler
501 * optimizations such as unrolling).
503 * Result may not be in the range [first,last). Use copy instead. Note
504 * that the start of the output range may overlap [first,last).
506 template <typename _BI1, typename _BI2>
508 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
510 // concept requirements
511 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
512 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
513 __glibcxx_function_requires(_ConvertibleConcept<
514 typename iterator_traits<_BI1>::value_type,
515 typename iterator_traits<_BI2>::value_type>)
516 __glibcxx_requires_valid_range(__first, __last);
518 return _BI2(std::__copy_backward_aux(__niter_base<_BI1>::__b(__first),
519 __niter_base<_BI1>::__b(__last),
520 __niter_base<_BI2>::__b(__result)));
527 template<typename _ForwardIterator, typename _Tp>
529 fill(_ForwardIterator __first, _ForwardIterator __last,
532 for (; __first != __last; ++__first)
540 template<typename _ForwardIterator, typename _Tp>
542 fill(_ForwardIterator __first, _ForwardIterator __last,
545 const _Tp __tmp = __value;
546 for (; __first != __last; ++__first)
551 template<typename _ForwardIterator, typename _Tp>
553 __fill_aux(_ForwardIterator __first, _ForwardIterator __last,
556 const bool __scalar = __is_scalar<_Tp>::__value;
557 std::__fill<__scalar>::fill(__first, __last, __value);
560 // Specialization: for char types we can use memset.
562 __fill_aux(unsigned char* __first, unsigned char* __last, unsigned char __c)
563 { __builtin_memset(__first, __c, __last - __first); }
566 __fill_aux(signed char* __first, signed char* __last, signed char __c)
567 { __builtin_memset(__first, static_cast<unsigned char>(__c),
571 __fill_aux(char* __first, char* __last, char __c)
572 { __builtin_memset(__first, static_cast<unsigned char>(__c),
576 * @brief Fills the range [first,last) with copies of value.
577 * @param first A forward iterator.
578 * @param last A forward iterator.
579 * @param value A reference-to-const of arbitrary type.
582 * This function fills a range with copies of the same value. For char
583 * types filling contiguous areas of memory, this becomes an inline call
584 * to @c memset or @c wmemset.
586 template<typename _ForwardIterator, typename _Tp>
588 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
590 // concept requirements
591 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
593 __glibcxx_requires_valid_range(__first, __last);
595 std::__fill_aux(__niter_base<_ForwardIterator>::__b(__first),
596 __niter_base<_ForwardIterator>::__b(__last), __value);
603 template<typename _OutputIterator, typename _Size, typename _Tp>
604 static _OutputIterator
605 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
607 for (; __n > 0; --__n, ++__first)
614 struct __fill_n<true>
616 template<typename _OutputIterator, typename _Size, typename _Tp>
617 static _OutputIterator
618 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
620 const _Tp __tmp = __value;
621 for (; __n > 0; --__n, ++__first)
627 template<typename _OutputIterator, typename _Size, typename _Tp>
628 inline _OutputIterator
629 __fill_n_aux(_OutputIterator __first, _Size __n, const _Tp& __value)
631 const bool __scalar = __is_scalar<_Tp>::__value;
632 return std::__fill_n<__scalar>::fill_n(__first, __n, __value);
635 template<typename _Size>
636 inline unsigned char*
637 __fill_n_aux(unsigned char* __first, _Size __n, unsigned char __c)
639 std::__fill_aux(__first, __first + __n, __c);
640 return __first + __n;
643 template<typename _Size>
645 __fill_n_aux(signed char* __first, _Size __n, signed char __c)
647 std::__fill_aux(__first, __first + __n, __c);
648 return __first + __n;
651 template<typename _Size>
653 __fill_n_aux(char* __first, _Size __n, char __c)
655 std::__fill_aux(__first, __first + __n, __c);
656 return __first + __n;
660 * @brief Fills the range [first,first+n) with copies of value.
661 * @param first An output iterator.
662 * @param n The count of copies to perform.
663 * @param value A reference-to-const of arbitrary type.
664 * @return The iterator at first+n.
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 @ wmemset.
670 template<typename _OI, typename _Size, typename _Tp>
672 fill_n(_OI __first, _Size __n, const _Tp& __value)
674 // concept requirements
675 __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>)
677 return _OI(std::__fill_n_aux(__niter_base<_OI>::__b(__first), __n,
682 * @brief Finds the places in ranges which don't match.
683 * @param first1 An input iterator.
684 * @param last1 An input iterator.
685 * @param first2 An input iterator.
686 * @return A pair of iterators pointing to the first mismatch.
688 * This compares the elements of two ranges using @c == and returns a pair
689 * of iterators. The first iterator points into the first range, the
690 * second iterator points into the second range, and the elements pointed
691 * to by the iterators are not equal.
693 template<typename _InputIterator1, typename _InputIterator2>
694 pair<_InputIterator1, _InputIterator2>
695 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
696 _InputIterator2 __first2)
698 // concept requirements
699 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
700 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
701 __glibcxx_function_requires(_EqualOpConcept<
702 typename iterator_traits<_InputIterator1>::value_type,
703 typename iterator_traits<_InputIterator2>::value_type>)
704 __glibcxx_requires_valid_range(__first1, __last1);
706 while (__first1 != __last1 && *__first1 == *__first2)
711 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
715 * @brief Finds the places in ranges which don't match.
716 * @param first1 An input iterator.
717 * @param last1 An input iterator.
718 * @param first2 An input iterator.
719 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
720 * @return A pair of iterators pointing to the first mismatch.
722 * This compares the elements of two ranges using the binary_pred
723 * parameter, and returns a pair
724 * of iterators. The first iterator points into the first range, the
725 * second iterator points into the second range, and the elements pointed
726 * to by the iterators are not equal.
728 template<typename _InputIterator1, typename _InputIterator2,
729 typename _BinaryPredicate>
730 pair<_InputIterator1, _InputIterator2>
731 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
732 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
734 // concept requirements
735 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
736 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
737 __glibcxx_requires_valid_range(__first1, __last1);
739 while (__first1 != __last1 && bool(__binary_pred(*__first1, *__first2)))
744 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
748 template<bool _BoolType>
751 template<typename _II1, typename _II2>
753 equal(_II1 __first1, _II1 __last1, _II2 __first2)
755 for (; __first1 != __last1; ++__first1, ++__first2)
756 if (!(*__first1 == *__first2))
765 template<typename _Tp>
767 equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
769 return !__builtin_memcmp(__first1, __first2, sizeof(_Tp)
770 * (__last1 - __first1));
774 template<typename _II1, typename _II2>
776 __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
778 typedef typename iterator_traits<_II1>::value_type _ValueType1;
779 typedef typename iterator_traits<_II2>::value_type _ValueType2;
780 const bool __simple = (__is_integer<_ValueType1>::__value
781 && __is_pointer<_II1>::__value
782 && __is_pointer<_II2>::__value
783 && __are_same<_ValueType1, _ValueType2>::__value);
785 return std::__equal<__simple>::equal(__first1, __last1, __first2);
789 * @brief Tests a range for element-wise equality.
790 * @param first1 An input iterator.
791 * @param last1 An input iterator.
792 * @param first2 An input iterator.
793 * @return A boolean true or false.
795 * This compares the elements of two ranges using @c == and returns true or
796 * false depending on whether all of the corresponding elements of the
799 template<typename _II1, typename _II2>
801 equal(_II1 __first1, _II1 __last1, _II2 __first2)
803 // concept requirements
804 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
805 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
806 __glibcxx_function_requires(_EqualOpConcept<
807 typename iterator_traits<_II1>::value_type,
808 typename iterator_traits<_II2>::value_type>)
809 __glibcxx_requires_valid_range(__first1, __last1);
811 return std::__equal_aux(__niter_base<_II1>::__b(__first1),
812 __niter_base<_II1>::__b(__last1),
813 __niter_base<_II2>::__b(__first2));
817 * @brief Tests a range for element-wise equality.
818 * @param first1 An input iterator.
819 * @param last1 An input iterator.
820 * @param first2 An input iterator.
821 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
822 * @return A boolean true or false.
824 * This compares the elements of two ranges using the binary_pred
825 * parameter, and returns true or
826 * false depending on whether all of the corresponding elements of the
829 template<typename _InputIterator1, typename _InputIterator2,
830 typename _BinaryPredicate>
832 equal(_InputIterator1 __first1, _InputIterator1 __last1,
833 _InputIterator2 __first2,
834 _BinaryPredicate __binary_pred)
836 // concept requirements
837 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
838 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
839 __glibcxx_requires_valid_range(__first1, __last1);
841 for (; __first1 != __last1; ++__first1, ++__first2)
842 if (!bool(__binary_pred(*__first1, *__first2)))
848 * @brief Performs "dictionary" comparison on ranges.
849 * @param first1 An input iterator.
850 * @param last1 An input iterator.
851 * @param first2 An input iterator.
852 * @param last2 An input iterator.
853 * @return A boolean true or false.
855 * "Returns true if the sequence of elements defined by the range
856 * [first1,last1) is lexicographically less than the sequence of elements
857 * defined by the range [first2,last2). Returns false otherwise."
858 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
859 * then this is an inline call to @c memcmp.
861 template<typename _InputIterator1, typename _InputIterator2>
863 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
864 _InputIterator2 __first2, _InputIterator2 __last2)
866 // concept requirements
867 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
868 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
869 __glibcxx_function_requires(_LessThanOpConcept<
870 typename iterator_traits<_InputIterator1>::value_type,
871 typename iterator_traits<_InputIterator2>::value_type>)
872 __glibcxx_function_requires(_LessThanOpConcept<
873 typename iterator_traits<_InputIterator2>::value_type,
874 typename iterator_traits<_InputIterator1>::value_type>)
875 __glibcxx_requires_valid_range(__first1, __last1);
876 __glibcxx_requires_valid_range(__first2, __last2);
878 for (; __first1 != __last1 && __first2 != __last2;
879 ++__first1, ++__first2)
881 if (*__first1 < *__first2)
883 if (*__first2 < *__first1)
886 return __first1 == __last1 && __first2 != __last2;
890 * @brief Performs "dictionary" comparison on ranges.
891 * @param first1 An input iterator.
892 * @param last1 An input iterator.
893 * @param first2 An input iterator.
894 * @param last2 An input iterator.
895 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
896 * @return A boolean true or false.
898 * The same as the four-parameter @c lexigraphical_compare, but uses the
899 * comp parameter instead of @c <.
901 template<typename _InputIterator1, typename _InputIterator2,
904 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
905 _InputIterator2 __first2, _InputIterator2 __last2,
908 // concept requirements
909 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
910 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
911 __glibcxx_requires_valid_range(__first1, __last1);
912 __glibcxx_requires_valid_range(__first2, __last2);
914 for (; __first1 != __last1 && __first2 != __last2;
915 ++__first1, ++__first2)
917 if (__comp(*__first1, *__first2))
919 if (__comp(*__first2, *__first1))
922 return __first1 == __last1 && __first2 != __last2;
926 lexicographical_compare(const unsigned char* __first1,
927 const unsigned char* __last1,
928 const unsigned char* __first2,
929 const unsigned char* __last2)
931 __glibcxx_requires_valid_range(__first1, __last1);
932 __glibcxx_requires_valid_range(__first2, __last2);
934 const size_t __len1 = __last1 - __first1;
935 const size_t __len2 = __last2 - __first2;
936 const int __result = __builtin_memcmp(__first1, __first2,
937 std::min(__len1, __len2));
938 return __result != 0 ? __result < 0 : __len1 < __len2;
942 lexicographical_compare(const char* __first1, const char* __last1,
943 const char* __first2, const char* __last2)
945 __glibcxx_requires_valid_range(__first1, __last1);
946 __glibcxx_requires_valid_range(__first2, __last2);
948 if (__gnu_cxx::__numeric_traits<char>::__is_signed)
949 return std::lexicographical_compare((const signed char*) __first1,
950 (const signed char*) __last1,
951 (const signed char*) __first2,
952 (const signed char*) __last2);
954 return std::lexicographical_compare((const unsigned char*) __first1,
955 (const unsigned char*) __last1,
956 (const unsigned char*) __first2,
957 (const unsigned char*) __last2);
960 _GLIBCXX_END_NAMESPACE