1 // Bits and pieces used in algorithms -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
44 * Copyright (c) 1996-1998
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
56 /** @file stl_algobase.h
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
64 #include <bits/c++config.h>
70 #include <bits/stl_pair.h>
71 #include <bits/cpp_type_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>
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
);
323 template<typename _II
, typename _OI
>
325 __copy_n(_II __first
, _II __last
, _OI __result
)
326 { return std::__copy_aux(__first
, __last
, __result
); }
330 struct __copy_normal
<true, false>
332 template<typename _II
, typename _OI
>
334 __copy_n(_II __first
, _II __last
, _OI __result
)
335 { return std::__copy_aux(__first
.base(), __last
.base(), __result
); }
339 struct __copy_normal
<false, true>
341 template<typename _II
, typename _OI
>
343 __copy_n(_II __first
, _II __last
, _OI __result
)
344 { return _OI(std::__copy_aux(__first
, __last
, __result
.base())); }
348 struct __copy_normal
<true, true>
350 template<typename _II
, typename _OI
>
352 __copy_n(_II __first
, _II __last
, _OI __result
)
353 { return _OI(std::__copy_aux(__first
.base(), __last
.base(),
358 * @brief Copies the range [first,last) into result.
359 * @param first An input iterator.
360 * @param last An input iterator.
361 * @param result An output iterator.
362 * @return result + (first - last)
364 * This inline function will boil down to a call to @c memmove whenever
365 * possible. Failing that, if random access iterators are passed, then the
366 * loop count will be known (and therefore a candidate for compiler
367 * optimizations such as unrolling). Result may not be contained within
368 * [first,last); the copy_backward function should be used instead.
370 * Note that the end of the output range is permitted to be contained
371 * within [first,last).
373 template<typename _InputIterator
, typename _OutputIterator
>
374 inline _OutputIterator
375 copy(_InputIterator __first
, _InputIterator __last
,
376 _OutputIterator __result
)
378 // concept requirements
379 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
380 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
381 typename iterator_traits
<_InputIterator
>::value_type
>)
382 __glibcxx_requires_valid_range(__first
, __last
);
384 const bool __in
= __is_normal_iterator
<_InputIterator
>::__value
;
385 const bool __out
= __is_normal_iterator
<_OutputIterator
>::__value
;
386 return std::__copy_normal
<__in
, __out
>::__copy_n(__first
, __last
,
390 template<bool, typename
>
391 struct __copy_backward
393 template<typename _BI1
, typename _BI2
>
395 __copy_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
397 while (__first
!= __last
)
398 *--__result
= *--__last
;
403 template<bool _BoolType
>
404 struct __copy_backward
<_BoolType
, random_access_iterator_tag
>
406 template<typename _BI1
, typename _BI2
>
408 __copy_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
410 typename iterator_traits
<_BI1
>::difference_type __n
;
411 for (__n
= __last
- __first
; __n
> 0; --__n
)
412 *--__result
= *--__last
;
418 struct __copy_backward
<true, random_access_iterator_tag
>
420 template<typename _Tp
>
422 __copy_b(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
424 const ptrdiff_t _Num
= __last
- __first
;
425 std::memmove(__result
- _Num
, __first
, sizeof(_Tp
) * _Num
);
426 return __result
- _Num
;
430 template<typename _BI1
, typename _BI2
>
432 __copy_backward_aux(_BI1 __first
, _BI1 __last
, _BI2 __result
)
434 typedef typename iterator_traits
<_BI1
>::value_type _ValueType1
;
435 typedef typename iterator_traits
<_BI2
>::value_type _ValueType2
;
436 typedef typename iterator_traits
<_BI1
>::iterator_category _Category
;
437 const bool __simple
= (__is_scalar
<_ValueType1
>::__value
438 && __is_pointer
<_BI1
>::__value
439 && __is_pointer
<_BI2
>::__value
440 && __are_same
<_ValueType1
, _ValueType2
>::__value
);
442 return std::__copy_backward
<__simple
, _Category
>::__copy_b(__first
,
448 struct __copy_backward_normal
450 template<typename _BI1
, typename _BI2
>
452 __copy_b_n(_BI1 __first
, _BI1 __last
, _BI2 __result
)
453 { return std::__copy_backward_aux(__first
, __last
, __result
); }
457 struct __copy_backward_normal
<true, false>
459 template<typename _BI1
, typename _BI2
>
461 __copy_b_n(_BI1 __first
, _BI1 __last
, _BI2 __result
)
462 { return std::__copy_backward_aux(__first
.base(), __last
.base(),
467 struct __copy_backward_normal
<false, true>
469 template<typename _BI1
, typename _BI2
>
471 __copy_b_n(_BI1 __first
, _BI1 __last
, _BI2 __result
)
472 { return _BI2(std::__copy_backward_aux(__first
, __last
,
477 struct __copy_backward_normal
<true, true>
479 template<typename _BI1
, typename _BI2
>
481 __copy_b_n(_BI1 __first
, _BI1 __last
, _BI2 __result
)
482 { return _BI2(std::__copy_backward_aux(__first
.base(), __last
.base(),
487 * @brief Copies the range [first,last) into result.
488 * @param first A bidirectional iterator.
489 * @param last A bidirectional iterator.
490 * @param result A bidirectional iterator.
491 * @return result - (first - last)
493 * The function has the same effect as copy, but starts at the end of the
494 * range and works its way to the start, returning the start of the result.
495 * This inline function will boil down to a call to @c memmove whenever
496 * possible. Failing that, if random access iterators are passed, then the
497 * loop count will be known (and therefore a candidate for compiler
498 * optimizations such as unrolling).
500 * Result may not be in the range [first,last). Use copy instead. Note
501 * that the start of the output range may overlap [first,last).
503 template <typename _BI1
, typename _BI2
>
505 copy_backward(_BI1 __first
, _BI1 __last
, _BI2 __result
)
507 // concept requirements
508 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BI1
>)
509 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<_BI2
>)
510 __glibcxx_function_requires(_ConvertibleConcept
<
511 typename iterator_traits
<_BI1
>::value_type
,
512 typename iterator_traits
<_BI2
>::value_type
>)
513 __glibcxx_requires_valid_range(__first
, __last
);
515 const bool __bi1
= __is_normal_iterator
<_BI1
>::__value
;
516 const bool __bi2
= __is_normal_iterator
<_BI2
>::__value
;
517 return std::__copy_backward_normal
<__bi1
, __bi2
>::__copy_b_n(__first
,
525 template<typename _ForwardIterator
, typename _Tp
>
527 fill(_ForwardIterator __first
, _ForwardIterator __last
,
530 for (; __first
!= __last
; ++__first
)
538 template<typename _ForwardIterator
, typename _Tp
>
540 fill(_ForwardIterator __first
, _ForwardIterator __last
,
543 const _Tp __tmp
= __value
;
544 for (; __first
!= __last
; ++__first
)
550 * @brief Fills the range [first,last) with copies of value.
551 * @param first A forward iterator.
552 * @param last A forward iterator.
553 * @param value A reference-to-const of arbitrary type.
556 * This function fills a range with copies of the same value. For one-byte
557 * types filling contiguous areas of memory, this becomes an inline call to
560 template<typename _ForwardIterator
, typename _Tp
>
562 fill(_ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __value
)
564 // concept requirements
565 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
567 __glibcxx_requires_valid_range(__first
, __last
);
569 const bool __scalar
= __is_scalar
<_Tp
>::__value
;
570 std::__fill
<__scalar
>::fill(__first
, __last
, __value
);
573 // Specialization: for one-byte types we can use memset.
575 fill(unsigned char* __first
, unsigned char* __last
, const unsigned char& __c
)
577 __glibcxx_requires_valid_range(__first
, __last
);
578 const unsigned char __tmp
= __c
;
579 std::memset(__first
, __tmp
, __last
- __first
);
583 fill(signed char* __first
, signed char* __last
, const signed char& __c
)
585 __glibcxx_requires_valid_range(__first
, __last
);
586 const signed char __tmp
= __c
;
587 std::memset(__first
, static_cast<unsigned char>(__tmp
), __last
- __first
);
591 fill(char* __first
, char* __last
, const char& __c
)
593 __glibcxx_requires_valid_range(__first
, __last
);
594 const char __tmp
= __c
;
595 std::memset(__first
, static_cast<unsigned char>(__tmp
), __last
- __first
);
601 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
602 static _OutputIterator
603 fill_n(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
605 for (; __n
> 0; --__n
, ++__first
)
612 struct __fill_n
<true>
614 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
615 static _OutputIterator
616 fill_n(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
618 const _Tp __tmp
= __value
;
619 for (; __n
> 0; --__n
, ++__first
)
626 * @brief Fills the range [first,first+n) with copies of value.
627 * @param first An output iterator.
628 * @param n The count of copies to perform.
629 * @param value A reference-to-const of arbitrary type.
630 * @return The iterator at first+n.
632 * This function fills a range with copies of the same value. For one-byte
633 * types filling contiguous areas of memory, this becomes an inline call to
636 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
638 fill_n(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
640 // concept requirements
641 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
, _Tp
>)
643 const bool __scalar
= __is_scalar
<_Tp
>::__value
;
644 return std::__fill_n
<__scalar
>::fill_n(__first
, __n
, __value
);
647 template<typename _Size
>
648 inline unsigned char*
649 fill_n(unsigned char* __first
, _Size __n
, const unsigned char& __c
)
651 std::fill(__first
, __first
+ __n
, __c
);
652 return __first
+ __n
;
655 template<typename _Size
>
657 fill_n(char* __first
, _Size __n
, const signed char& __c
)
659 std::fill(__first
, __first
+ __n
, __c
);
660 return __first
+ __n
;
663 template<typename _Size
>
665 fill_n(char* __first
, _Size __n
, const char& __c
)
667 std::fill(__first
, __first
+ __n
, __c
);
668 return __first
+ __n
;
672 * @brief Finds the places in ranges which don't match.
673 * @param first1 An input iterator.
674 * @param last1 An input iterator.
675 * @param first2 An input iterator.
676 * @return A pair of iterators pointing to the first mismatch.
678 * This compares the elements of two ranges using @c == and returns a pair
679 * of iterators. The first iterator points into the first range, the
680 * second iterator points into the second range, and the elements pointed
681 * to by the iterators are not equal.
683 template<typename _InputIterator1
, typename _InputIterator2
>
684 pair
<_InputIterator1
, _InputIterator2
>
685 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
686 _InputIterator2 __first2
)
688 // concept requirements
689 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
690 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
691 __glibcxx_function_requires(_EqualOpConcept
<
692 typename iterator_traits
<_InputIterator1
>::value_type
,
693 typename iterator_traits
<_InputIterator2
>::value_type
>)
694 __glibcxx_requires_valid_range(__first1
, __last1
);
696 while (__first1
!= __last1
&& *__first1
== *__first2
)
701 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
705 * @brief Finds the places in ranges which don't match.
706 * @param first1 An input iterator.
707 * @param last1 An input iterator.
708 * @param first2 An input iterator.
709 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
710 * @return A pair of iterators pointing to the first mismatch.
712 * This compares the elements of two ranges using the binary_pred
713 * parameter, and returns a pair
714 * of iterators. The first iterator points into the first range, the
715 * second iterator points into the second range, and the elements pointed
716 * to by the iterators are not equal.
718 template<typename _InputIterator1
, typename _InputIterator2
,
719 typename _BinaryPredicate
>
720 pair
<_InputIterator1
, _InputIterator2
>
721 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
722 _InputIterator2 __first2
, _BinaryPredicate __binary_pred
)
724 // concept requirements
725 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
726 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
727 __glibcxx_requires_valid_range(__first1
, __last1
);
729 while (__first1
!= __last1
&& __binary_pred(*__first1
, *__first2
))
734 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
738 * @brief Tests a range for element-wise equality.
739 * @param first1 An input iterator.
740 * @param last1 An input iterator.
741 * @param first2 An input iterator.
742 * @return A boolean true or false.
744 * This compares the elements of two ranges using @c == and returns true or
745 * false depending on whether all of the corresponding elements of the
748 template<typename _InputIterator1
, typename _InputIterator2
>
750 equal(_InputIterator1 __first1
, _InputIterator1 __last1
,
751 _InputIterator2 __first2
)
753 // concept requirements
754 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
755 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
756 __glibcxx_function_requires(_EqualOpConcept
<
757 typename iterator_traits
<_InputIterator1
>::value_type
,
758 typename iterator_traits
<_InputIterator2
>::value_type
>)
759 __glibcxx_requires_valid_range(__first1
, __last1
);
761 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
762 if (!(*__first1
== *__first2
))
768 * @brief Tests a range for element-wise equality.
769 * @param first1 An input iterator.
770 * @param last1 An input iterator.
771 * @param first2 An input iterator.
772 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
773 * @return A boolean true or false.
775 * This compares the elements of two ranges using the binary_pred
776 * parameter, and returns true or
777 * false depending on whether all of the corresponding elements of the
780 template<typename _InputIterator1
, typename _InputIterator2
,
781 typename _BinaryPredicate
>
783 equal(_InputIterator1 __first1
, _InputIterator1 __last1
,
784 _InputIterator2 __first2
,
785 _BinaryPredicate __binary_pred
)
787 // concept requirements
788 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
789 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
790 __glibcxx_requires_valid_range(__first1
, __last1
);
792 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
793 if (!__binary_pred(*__first1
, *__first2
))
799 * @brief Performs "dictionary" comparison on ranges.
800 * @param first1 An input iterator.
801 * @param last1 An input iterator.
802 * @param first2 An input iterator.
803 * @param last2 An input iterator.
804 * @return A boolean true or false.
806 * "Returns true if the sequence of elements defined by the range
807 * [first1,last1) is lexicographically less than the sequence of elements
808 * defined by the range [first2,last2). Returns false otherwise."
809 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
810 * then this is an inline call to @c memcmp.
812 template<typename _InputIterator1
, typename _InputIterator2
>
814 lexicographical_compare(_InputIterator1 __first1
, _InputIterator1 __last1
,
815 _InputIterator2 __first2
, _InputIterator2 __last2
)
817 // concept requirements
818 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
819 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
820 __glibcxx_function_requires(_LessThanOpConcept
<
821 typename iterator_traits
<_InputIterator1
>::value_type
,
822 typename iterator_traits
<_InputIterator2
>::value_type
>)
823 __glibcxx_function_requires(_LessThanOpConcept
<
824 typename iterator_traits
<_InputIterator2
>::value_type
,
825 typename iterator_traits
<_InputIterator1
>::value_type
>)
826 __glibcxx_requires_valid_range(__first1
, __last1
);
827 __glibcxx_requires_valid_range(__first2
, __last2
);
829 for (; __first1
!= __last1
&& __first2
!= __last2
;
830 ++__first1
, ++__first2
)
832 if (*__first1
< *__first2
)
834 if (*__first2
< *__first1
)
837 return __first1
== __last1
&& __first2
!= __last2
;
841 * @brief Performs "dictionary" comparison on ranges.
842 * @param first1 An input iterator.
843 * @param last1 An input iterator.
844 * @param first2 An input iterator.
845 * @param last2 An input iterator.
846 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
847 * @return A boolean true or false.
849 * The same as the four-parameter @c lexigraphical_compare, but uses the
850 * comp parameter instead of @c <.
852 template<typename _InputIterator1
, typename _InputIterator2
,
855 lexicographical_compare(_InputIterator1 __first1
, _InputIterator1 __last1
,
856 _InputIterator2 __first2
, _InputIterator2 __last2
,
859 // concept requirements
860 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
861 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
862 __glibcxx_requires_valid_range(__first1
, __last1
);
863 __glibcxx_requires_valid_range(__first2
, __last2
);
865 for (; __first1
!= __last1
&& __first2
!= __last2
;
866 ++__first1
, ++__first2
)
868 if (__comp(*__first1
, *__first2
))
870 if (__comp(*__first2
, *__first1
))
873 return __first1
== __last1
&& __first2
!= __last2
;
877 lexicographical_compare(const unsigned char* __first1
,
878 const unsigned char* __last1
,
879 const unsigned char* __first2
,
880 const unsigned char* __last2
)
882 __glibcxx_requires_valid_range(__first1
, __last1
);
883 __glibcxx_requires_valid_range(__first2
, __last2
);
885 const size_t __len1
= __last1
- __first1
;
886 const size_t __len2
= __last2
- __first2
;
887 const int __result
= std::memcmp(__first1
, __first2
,
888 std::min(__len1
, __len2
));
889 return __result
!= 0 ? __result
< 0 : __len1
< __len2
;
893 lexicographical_compare(const char* __first1
, const char* __last1
,
894 const char* __first2
, const char* __last2
)
896 __glibcxx_requires_valid_range(__first1
, __last1
);
897 __glibcxx_requires_valid_range(__first2
, __last2
);
899 #if CHAR_MAX == SCHAR_MAX
900 return std::lexicographical_compare((const signed char*) __first1
,
901 (const signed char*) __last1
,
902 (const signed char*) __first2
,
903 (const signed char*) __last2
);
904 #else /* CHAR_MAX == SCHAR_MAX */
905 return std::lexicographical_compare((const unsigned char*) __first1
,
906 (const unsigned char*) __last1
,
907 (const unsigned char*) __first2
,
908 (const unsigned char*) __last2
);
909 #endif /* CHAR_MAX == SCHAR_MAX */