1 // Bits and pieces used in algorithms -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
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>
71 #include <bits/stl_pair.h>
72 #include <bits/cpp_type_traits.h>
73 #include <bits/stl_iterator_base_types.h>
74 #include <bits/stl_iterator_base_funcs.h>
75 #include <bits/stl_iterator.h>
76 #include <bits/concept_check.h>
77 #include <debug/debug.h>
79 _GLIBCXX_BEGIN_NAMESPACE(std)
82 * @brief Swaps two values.
83 * @param a A thing of arbitrary type.
84 * @param b Another thing of arbitrary type.
87 * This is the simple classic generic implementation. It will work on
88 * any type which has a copy constructor and an assignment operator.
90 template<typename _Tp>
92 swap(_Tp& __a, _Tp& __b)
94 // concept requirements
95 __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
102 // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
103 // nutshell, we are partially implementing the resolution of DR 187,
104 // when it's safe, i.e., the value_types are equal.
105 template<bool _BoolType>
108 template<typename _ForwardIterator1, typename _ForwardIterator2>
110 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
112 typedef typename iterator_traits<_ForwardIterator1>::value_type
114 _ValueType1 __tmp = *__a;
121 struct __iter_swap<true>
123 template<typename _ForwardIterator1, typename _ForwardIterator2>
125 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
132 * @brief Swaps the contents of two iterators.
133 * @param a An iterator.
134 * @param b Another iterator.
137 * This function swaps the values pointed to by two iterators, not the
138 * iterators themselves.
140 template<typename _ForwardIterator1, typename _ForwardIterator2>
142 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
144 typedef typename iterator_traits<_ForwardIterator1>::value_type
146 typedef typename iterator_traits<_ForwardIterator2>::value_type
149 // concept requirements
150 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
152 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
154 __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
156 __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
159 typedef typename iterator_traits<_ForwardIterator1>::reference
161 typedef typename iterator_traits<_ForwardIterator2>::reference
163 std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value &&
164 __are_same<_ValueType1 &, _ReferenceType1>::__value &&
165 __are_same<_ValueType2 &, _ReferenceType2>::__value>::
173 * @brief This does what you think it does.
174 * @param a A thing of arbitrary type.
175 * @param b Another thing of arbitrary type.
176 * @return The lesser of the parameters.
178 * This is the simple classic generic implementation. It will work on
179 * temporary expressions, since they are only evaluated once, unlike a
180 * preprocessor macro.
182 template<typename _Tp>
184 min(const _Tp& __a, const _Tp& __b)
186 // concept requirements
187 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
188 //return __b < __a ? __b : __a;
195 * @brief This does what you think it does.
196 * @param a A thing of arbitrary type.
197 * @param b Another thing of arbitrary type.
198 * @return The greater of the parameters.
200 * This is the simple classic generic implementation. It will work on
201 * temporary expressions, since they are only evaluated once, unlike a
202 * preprocessor macro.
204 template<typename _Tp>
206 max(const _Tp& __a, const _Tp& __b)
208 // concept requirements
209 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
210 //return __a < __b ? __b : __a;
217 * @brief This does what you think it does.
218 * @param a A thing of arbitrary type.
219 * @param b Another thing of arbitrary type.
220 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
221 * @return The lesser of the parameters.
223 * This will work on temporary expressions, since they are only evaluated
224 * once, unlike a preprocessor macro.
226 template<typename _Tp, typename _Compare>
228 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
230 //return __comp(__b, __a) ? __b : __a;
231 if (__comp(__b, __a))
237 * @brief This does what you think it does.
238 * @param a A thing of arbitrary type.
239 * @param b Another thing of arbitrary type.
240 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
241 * @return The greater of the parameters.
243 * This will work on temporary expressions, since they are only evaluated
244 * once, unlike a preprocessor macro.
246 template<typename _Tp, typename _Compare>
248 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
250 //return __comp(__a, __b) ? __b : __a;
251 if (__comp(__a, __b))
256 // All of these auxiliary structs serve two purposes. (1) Replace
257 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
258 // because the input and output ranges are permitted to overlap.)
259 // (2) If we're using random access iterators, then write the loop as
260 // a for loop with an explicit count.
262 template<bool, typename>
265 template<typename _II, typename _OI>
267 copy(_II __first, _II __last, _OI __result)
269 for (; __first != __last; ++__result, ++__first)
270 *__result = *__first;
275 template<bool _BoolType>
276 struct __copy<_BoolType, random_access_iterator_tag>
278 template<typename _II, typename _OI>
280 copy(_II __first, _II __last, _OI __result)
282 typedef typename iterator_traits<_II>::difference_type _Distance;
283 for(_Distance __n = __last - __first; __n > 0; --__n)
285 *__result = *__first;
294 struct __copy<true, random_access_iterator_tag>
296 template<typename _Tp>
298 copy(const _Tp* __first, const _Tp* __last, _Tp* __result)
300 std::memmove(__result, __first, sizeof(_Tp) * (__last - __first));
301 return __result + (__last - __first);
305 template<typename _II, typename _OI>
307 __copy_aux(_II __first, _II __last, _OI __result)
309 typedef typename iterator_traits<_II>::value_type _ValueTypeI;
310 typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
311 typedef typename iterator_traits<_II>::iterator_category _Category;
312 const bool __simple = (__is_scalar<_ValueTypeI>::__value
313 && __is_pointer<_II>::__value
314 && __is_pointer<_OI>::__value
315 && __are_same<_ValueTypeI, _ValueTypeO>::__value);
317 return std::__copy<__simple, _Category>::copy(__first, __last, __result);
320 // Helpers for streambuf iterators (either istream or ostream).
321 template<typename _CharT>
322 typename __enable_if<ostreambuf_iterator<_CharT>,
323 __is_char<_CharT>::__value>::__type
324 __copy_aux(_CharT*, _CharT*, ostreambuf_iterator<_CharT>);
326 template<typename _CharT>
327 typename __enable_if<ostreambuf_iterator<_CharT>,
328 __is_char<_CharT>::__value>::__type
329 __copy_aux(const _CharT*, const _CharT*, ostreambuf_iterator<_CharT>);
331 template<typename _CharT>
332 typename __enable_if<_CharT*, __is_char<_CharT>::__value>::__type
333 __copy_aux(istreambuf_iterator<_CharT>, istreambuf_iterator<_CharT>,
339 template<typename _II, typename _OI>
341 __copy_n(_II __first, _II __last, _OI __result)
342 { return std::__copy_aux(__first, __last, __result); }
346 struct __copy_normal<true, false>
348 template<typename _II, typename _OI>
350 __copy_n(_II __first, _II __last, _OI __result)
351 { return std::__copy_aux(__first.base(), __last.base(), __result); }
355 struct __copy_normal<false, true>
357 template<typename _II, typename _OI>
359 __copy_n(_II __first, _II __last, _OI __result)
360 { return _OI(std::__copy_aux(__first, __last, __result.base())); }
364 struct __copy_normal<true, true>
366 template<typename _II, typename _OI>
368 __copy_n(_II __first, _II __last, _OI __result)
369 { return _OI(std::__copy_aux(__first.base(), __last.base(),
374 * @brief Copies the range [first,last) into result.
375 * @param first An input iterator.
376 * @param last An input iterator.
377 * @param result An output iterator.
378 * @return result + (first - last)
380 * This inline function will boil down to a call to @c memmove whenever
381 * possible. Failing that, if random access iterators are passed, then the
382 * loop count will be known (and therefore a candidate for compiler
383 * optimizations such as unrolling). Result may not be contained within
384 * [first,last); the copy_backward function should be used instead.
386 * Note that the end of the output range is permitted to be contained
387 * within [first,last).
389 template<typename _InputIterator, typename _OutputIterator>
390 inline _OutputIterator
391 copy(_InputIterator __first, _InputIterator __last,
392 _OutputIterator __result)
394 // concept requirements
395 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
396 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
397 typename iterator_traits<_InputIterator>::value_type>)
398 __glibcxx_requires_valid_range(__first, __last);
400 const bool __in = __is_normal_iterator<_InputIterator>::__value;
401 const bool __out = __is_normal_iterator<_OutputIterator>::__value;
402 return std::__copy_normal<__in, __out>::__copy_n(__first, __last,
406 // Overload for streambuf iterators.
407 template<typename _CharT>
408 typename __enable_if<ostreambuf_iterator<_CharT>,
409 __is_char<_CharT>::__value>::__type
410 copy(istreambuf_iterator<_CharT>, istreambuf_iterator<_CharT>,
411 ostreambuf_iterator<_CharT>);
413 template<bool, typename>
414 struct __copy_backward
416 template<typename _BI1, typename _BI2>
418 __copy_b(_BI1 __first, _BI1 __last, _BI2 __result)
420 while (__first != __last)
421 *--__result = *--__last;
426 template<bool _BoolType>
427 struct __copy_backward<_BoolType, random_access_iterator_tag>
429 template<typename _BI1, typename _BI2>
431 __copy_b(_BI1 __first, _BI1 __last, _BI2 __result)
433 typename iterator_traits<_BI1>::difference_type __n;
434 for (__n = __last - __first; __n > 0; --__n)
435 *--__result = *--__last;
441 struct __copy_backward<true, random_access_iterator_tag>
443 template<typename _Tp>
445 __copy_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
447 const ptrdiff_t _Num = __last - __first;
448 std::memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
449 return __result - _Num;
453 template<typename _BI1, typename _BI2>
455 __copy_backward_aux(_BI1 __first, _BI1 __last, _BI2 __result)
457 typedef typename iterator_traits<_BI1>::value_type _ValueType1;
458 typedef typename iterator_traits<_BI2>::value_type _ValueType2;
459 typedef typename iterator_traits<_BI1>::iterator_category _Category;
460 const bool __simple = (__is_scalar<_ValueType1>::__value
461 && __is_pointer<_BI1>::__value
462 && __is_pointer<_BI2>::__value
463 && __are_same<_ValueType1, _ValueType2>::__value);
465 return std::__copy_backward<__simple, _Category>::__copy_b(__first,
471 struct __copy_backward_normal
473 template<typename _BI1, typename _BI2>
475 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
476 { return std::__copy_backward_aux(__first, __last, __result); }
480 struct __copy_backward_normal<true, false>
482 template<typename _BI1, typename _BI2>
484 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
485 { return std::__copy_backward_aux(__first.base(), __last.base(),
490 struct __copy_backward_normal<false, true>
492 template<typename _BI1, typename _BI2>
494 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
495 { return _BI2(std::__copy_backward_aux(__first, __last,
500 struct __copy_backward_normal<true, true>
502 template<typename _BI1, typename _BI2>
504 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
505 { return _BI2(std::__copy_backward_aux(__first.base(), __last.base(),
510 * @brief Copies the range [first,last) into result.
511 * @param first A bidirectional iterator.
512 * @param last A bidirectional iterator.
513 * @param result A bidirectional iterator.
514 * @return result - (first - last)
516 * The function has the same effect as copy, but starts at the end of the
517 * range and works its way to the start, returning the start of the result.
518 * This inline function will boil down to a call to @c memmove whenever
519 * possible. Failing that, if random access iterators are passed, then the
520 * loop count will be known (and therefore a candidate for compiler
521 * optimizations such as unrolling).
523 * Result may not be in the range [first,last). Use copy instead. Note
524 * that the start of the output range may overlap [first,last).
526 template <typename _BI1, typename _BI2>
528 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
530 // concept requirements
531 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
532 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
533 __glibcxx_function_requires(_ConvertibleConcept<
534 typename iterator_traits<_BI1>::value_type,
535 typename iterator_traits<_BI2>::value_type>)
536 __glibcxx_requires_valid_range(__first, __last);
538 const bool __bi1 = __is_normal_iterator<_BI1>::__value;
539 const bool __bi2 = __is_normal_iterator<_BI2>::__value;
540 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)
573 * @brief Fills the range [first,last) with copies of value.
574 * @param first A forward iterator.
575 * @param last A forward iterator.
576 * @param value A reference-to-const of arbitrary type.
579 * This function fills a range with copies of the same value. For one-byte
580 * types filling contiguous areas of memory, this becomes an inline call to
583 template<typename _ForwardIterator, typename _Tp>
585 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
587 // concept requirements
588 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
590 __glibcxx_requires_valid_range(__first, __last);
592 const bool __scalar = __is_scalar<_Tp>::__value;
593 std::__fill<__scalar>::fill(__first, __last, __value);
596 // Specialization: for one-byte types we can use memset.
598 fill(unsigned char* __first, unsigned char* __last, const unsigned char& __c)
600 __glibcxx_requires_valid_range(__first, __last);
601 const unsigned char __tmp = __c;
602 std::memset(__first, __tmp, __last - __first);
606 fill(signed char* __first, signed char* __last, const signed char& __c)
608 __glibcxx_requires_valid_range(__first, __last);
609 const signed char __tmp = __c;
610 std::memset(__first, static_cast<unsigned char>(__tmp), __last - __first);
614 fill(char* __first, char* __last, const char& __c)
616 __glibcxx_requires_valid_range(__first, __last);
617 const char __tmp = __c;
618 std::memset(__first, static_cast<unsigned char>(__tmp), __last - __first);
621 template<typename _Tp, typename _Ref, typename _Ptr>
622 struct _Deque_iterator;
624 // Overload for deque::iterators, exploiting the "segmented-iterator
625 // optimization". NB: leave const_iterators alone!
626 template<typename _Tp>
628 fill(const _Deque_iterator<_Tp, _Tp&, _Tp*>& __first,
629 const _Deque_iterator<_Tp, _Tp&, _Tp*>& __last, const _Tp& __value)
631 typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
633 for (typename _Self::_Map_pointer __node = __first._M_node + 1;
634 __node < __last._M_node; ++__node)
635 std::fill(*__node, *__node + _Self::_S_buffer_size(), __value);
637 if (__first._M_node != __last._M_node)
639 std::fill(__first._M_cur, __first._M_last, __value);
640 std::fill(__last._M_first, __last._M_cur, __value);
643 std::fill(__first._M_cur, __last._M_cur, __value);
650 template<typename _OutputIterator, typename _Size, typename _Tp>
651 static _OutputIterator
652 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
654 for (; __n > 0; --__n, ++__first)
661 struct __fill_n<true>
663 template<typename _OutputIterator, typename _Size, typename _Tp>
664 static _OutputIterator
665 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
667 const _Tp __tmp = __value;
668 for (; __n > 0; --__n, ++__first)
675 * @brief Fills the range [first,first+n) with copies of value.
676 * @param first An output iterator.
677 * @param n The count of copies to perform.
678 * @param value A reference-to-const of arbitrary type.
679 * @return The iterator at first+n.
681 * This function fills a range with copies of the same value. For one-byte
682 * types filling contiguous areas of memory, this becomes an inline call to
685 template<typename _OutputIterator, typename _Size, typename _Tp>
687 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
689 // concept requirements
690 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, _Tp>)
692 const bool __scalar = __is_scalar<_Tp>::__value;
693 return std::__fill_n<__scalar>::fill_n(__first, __n, __value);
696 template<typename _Size>
697 inline unsigned char*
698 fill_n(unsigned char* __first, _Size __n, const unsigned char& __c)
700 std::fill(__first, __first + __n, __c);
701 return __first + __n;
704 template<typename _Size>
706 fill_n(char* __first, _Size __n, const signed char& __c)
708 std::fill(__first, __first + __n, __c);
709 return __first + __n;
712 template<typename _Size>
714 fill_n(char* __first, _Size __n, const char& __c)
716 std::fill(__first, __first + __n, __c);
717 return __first + __n;
721 * @brief Finds the places in ranges which don't match.
722 * @param first1 An input iterator.
723 * @param last1 An input iterator.
724 * @param first2 An input iterator.
725 * @return A pair of iterators pointing to the first mismatch.
727 * This compares the elements of two ranges using @c == and returns a pair
728 * of iterators. The first iterator points into the first range, the
729 * second iterator points into the second range, and the elements pointed
730 * to by the iterators are not equal.
732 template<typename _InputIterator1, typename _InputIterator2>
733 pair<_InputIterator1, _InputIterator2>
734 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
735 _InputIterator2 __first2)
737 // concept requirements
738 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
739 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
740 __glibcxx_function_requires(_EqualOpConcept<
741 typename iterator_traits<_InputIterator1>::value_type,
742 typename iterator_traits<_InputIterator2>::value_type>)
743 __glibcxx_requires_valid_range(__first1, __last1);
745 while (__first1 != __last1 && *__first1 == *__first2)
750 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
754 * @brief Finds the places in ranges which don't match.
755 * @param first1 An input iterator.
756 * @param last1 An input iterator.
757 * @param first2 An input iterator.
758 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
759 * @return A pair of iterators pointing to the first mismatch.
761 * This compares the elements of two ranges using the binary_pred
762 * parameter, and returns a pair
763 * of iterators. The first iterator points into the first range, the
764 * second iterator points into the second range, and the elements pointed
765 * to by the iterators are not equal.
767 template<typename _InputIterator1, typename _InputIterator2,
768 typename _BinaryPredicate>
769 pair<_InputIterator1, _InputIterator2>
770 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
771 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
773 // concept requirements
774 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
775 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
776 __glibcxx_requires_valid_range(__first1, __last1);
778 while (__first1 != __last1 && __binary_pred(*__first1, *__first2))
783 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
787 * @brief Tests a range for element-wise equality.
788 * @param first1 An input iterator.
789 * @param last1 An input iterator.
790 * @param first2 An input iterator.
791 * @return A boolean true or false.
793 * This compares the elements of two ranges using @c == and returns true or
794 * false depending on whether all of the corresponding elements of the
797 template<typename _InputIterator1, typename _InputIterator2>
799 equal(_InputIterator1 __first1, _InputIterator1 __last1,
800 _InputIterator2 __first2)
802 // concept requirements
803 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
804 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
805 __glibcxx_function_requires(_EqualOpConcept<
806 typename iterator_traits<_InputIterator1>::value_type,
807 typename iterator_traits<_InputIterator2>::value_type>)
808 __glibcxx_requires_valid_range(__first1, __last1);
810 for (; __first1 != __last1; ++__first1, ++__first2)
811 if (!(*__first1 == *__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 (!__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 = std::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 CHAR_MAX == SCHAR_MAX
949 return std::lexicographical_compare((const signed char*) __first1,
950 (const signed char*) __last1,
951 (const signed char*) __first2,
952 (const signed char*) __last2);
953 #else /* CHAR_MAX == SCHAR_MAX */
954 return std::lexicographical_compare((const unsigned char*) __first1,
955 (const unsigned char*) __last1,
956 (const unsigned char*) __first2,
957 (const unsigned char*) __last2);
958 #endif /* CHAR_MAX == SCHAR_MAX */
961 _GLIBCXX_END_NAMESPACE