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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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
>)
97 const _Tp __tmp
= __a
;
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 const _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
, __last
,
447 struct __copy_backward_normal
449 template<typename _BI1
, typename _BI2
>
451 copy_b_n(_BI1 __first
, _BI1 __last
, _BI2 __result
)
452 { return std::__copy_backward_aux(__first
, __last
, __result
); }
456 struct __copy_backward_normal
<true, false>
458 template<typename _BI1
, typename _BI2
>
460 copy_b_n(_BI1 __first
, _BI1 __last
, _BI2 __result
)
461 { return std::__copy_backward_aux(__first
.base(), __last
.base(),
466 struct __copy_backward_normal
<false, true>
468 template<typename _BI1
, typename _BI2
>
470 copy_b_n(_BI1 __first
, _BI1 __last
, _BI2 __result
)
471 { return _BI2(std::__copy_backward_aux(__first
, __last
,
476 struct __copy_backward_normal
<true, true>
478 template<typename _BI1
, typename _BI2
>
480 copy_b_n(_BI1 __first
, _BI1 __last
, _BI2 __result
)
481 { return _BI2(std::__copy_backward_aux(__first
.base(), __last
.base(),
486 * @brief Copies the range [first,last) into result.
487 * @param first A bidirectional iterator.
488 * @param last A bidirectional iterator.
489 * @param result A bidirectional iterator.
490 * @return result - (first - last)
492 * The function has the same effect as copy, but starts at the end of the
493 * range and works its way to the start, returning the start of the result.
494 * This inline function will boil down to a call to @c memmove whenever
495 * possible. Failing that, if random access iterators are passed, then the
496 * loop count will be known (and therefore a candidate for compiler
497 * optimizations such as unrolling).
499 * Result may not be in the range [first,last). Use copy instead. Note
500 * that the start of the output range may overlap [first,last).
502 template <typename _BI1
, typename _BI2
>
504 copy_backward(_BI1 __first
, _BI1 __last
, _BI2 __result
)
506 // concept requirements
507 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BI1
>)
508 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<_BI2
>)
509 __glibcxx_function_requires(_ConvertibleConcept
<
510 typename iterator_traits
<_BI1
>::value_type
,
511 typename iterator_traits
<_BI2
>::value_type
>)
512 __glibcxx_requires_valid_range(__first
, __last
);
514 const bool __bi1
= __is_normal_iterator
<_BI1
>::__value
;
515 const bool __bi2
= __is_normal_iterator
<_BI2
>::__value
;
516 return std::__copy_backward_normal
<__bi1
, __bi2
>::copy_b_n(__first
, __last
,
523 template<typename _ForwardIterator
, typename _Tp
>
525 fill(_ForwardIterator __first
, _ForwardIterator __last
,
528 for (; __first
!= __last
; ++__first
)
536 template<typename _ForwardIterator
, typename _Tp
>
538 fill(_ForwardIterator __first
, _ForwardIterator __last
,
541 const _Tp __tmp
= __value
;
542 for (; __first
!= __last
; ++__first
)
548 * @brief Fills the range [first,last) with copies of value.
549 * @param first A forward iterator.
550 * @param last A forward iterator.
551 * @param value A reference-to-const of arbitrary type.
554 * This function fills a range with copies of the same value. For one-byte
555 * types filling contiguous areas of memory, this becomes an inline call to
558 template<typename _ForwardIterator
, typename _Tp
>
560 fill(_ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __value
)
562 // concept requirements
563 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
565 __glibcxx_requires_valid_range(__first
, __last
);
567 const bool __scalar
= __is_scalar
<_Tp
>::__value
;
568 std::__fill
<__scalar
>::fill(__first
, __last
, __value
);
571 // Specialization: for one-byte types we can use memset.
573 fill(unsigned char* __first
, unsigned char* __last
, const unsigned char& __c
)
575 __glibcxx_requires_valid_range(__first
, __last
);
576 const unsigned char __tmp
= __c
;
577 std::memset(__first
, __tmp
, __last
- __first
);
581 fill(signed char* __first
, signed char* __last
, const signed char& __c
)
583 __glibcxx_requires_valid_range(__first
, __last
);
584 const signed char __tmp
= __c
;
585 std::memset(__first
, static_cast<unsigned char>(__tmp
), __last
- __first
);
589 fill(char* __first
, char* __last
, const char& __c
)
591 __glibcxx_requires_valid_range(__first
, __last
);
592 const char __tmp
= __c
;
593 std::memset(__first
, static_cast<unsigned char>(__tmp
), __last
- __first
);
599 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
600 static _OutputIterator
601 fill_n(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
603 for (; __n
> 0; --__n
, ++__first
)
610 struct __fill_n
<true>
612 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
613 static _OutputIterator
614 fill_n(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
616 const _Tp __tmp
= __value
;
617 for (; __n
> 0; --__n
, ++__first
)
624 * @brief Fills the range [first,first+n) with copies of value.
625 * @param first An output iterator.
626 * @param n The count of copies to perform.
627 * @param value A reference-to-const of arbitrary type.
628 * @return The iterator at first+n.
630 * This function fills a range with copies of the same value. For one-byte
631 * types filling contiguous areas of memory, this becomes an inline call to
634 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
636 fill_n(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
638 // concept requirements
639 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
, _Tp
>)
641 const bool __scalar
= __is_scalar
<_Tp
>::__value
;
642 return std::__fill_n
<__scalar
>::fill_n(__first
, __n
, __value
);
645 template<typename _Size
>
646 inline unsigned char*
647 fill_n(unsigned char* __first
, _Size __n
, const unsigned char& __c
)
649 std::fill(__first
, __first
+ __n
, __c
);
650 return __first
+ __n
;
653 template<typename _Size
>
655 fill_n(char* __first
, _Size __n
, const signed char& __c
)
657 std::fill(__first
, __first
+ __n
, __c
);
658 return __first
+ __n
;
661 template<typename _Size
>
663 fill_n(char* __first
, _Size __n
, const char& __c
)
665 std::fill(__first
, __first
+ __n
, __c
);
666 return __first
+ __n
;
670 * @brief Finds the places in ranges which don't match.
671 * @param first1 An input iterator.
672 * @param last1 An input iterator.
673 * @param first2 An input iterator.
674 * @return A pair of iterators pointing to the first mismatch.
676 * This compares the elements of two ranges using @c == and returns a pair
677 * of iterators. The first iterator points into the first range, the
678 * second iterator points into the second range, and the elements pointed
679 * to by the iterators are not equal.
681 template<typename _InputIterator1
, typename _InputIterator2
>
682 pair
<_InputIterator1
, _InputIterator2
>
683 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
684 _InputIterator2 __first2
)
686 // concept requirements
687 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
688 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
689 __glibcxx_function_requires(_EqualOpConcept
<
690 typename iterator_traits
<_InputIterator1
>::value_type
,
691 typename iterator_traits
<_InputIterator2
>::value_type
>)
692 __glibcxx_requires_valid_range(__first1
, __last1
);
694 while (__first1
!= __last1
&& *__first1
== *__first2
)
699 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
703 * @brief Finds the places in ranges which don't match.
704 * @param first1 An input iterator.
705 * @param last1 An input iterator.
706 * @param first2 An input iterator.
707 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
708 * @return A pair of iterators pointing to the first mismatch.
710 * This compares the elements of two ranges using the binary_pred
711 * parameter, and returns a pair
712 * of iterators. The first iterator points into the first range, the
713 * second iterator points into the second range, and the elements pointed
714 * to by the iterators are not equal.
716 template<typename _InputIterator1
, typename _InputIterator2
,
717 typename _BinaryPredicate
>
718 pair
<_InputIterator1
, _InputIterator2
>
719 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
720 _InputIterator2 __first2
, _BinaryPredicate __binary_pred
)
722 // concept requirements
723 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
724 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
725 __glibcxx_requires_valid_range(__first1
, __last1
);
727 while (__first1
!= __last1
&& __binary_pred(*__first1
, *__first2
))
732 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
736 * @brief Tests a range for element-wise equality.
737 * @param first1 An input iterator.
738 * @param last1 An input iterator.
739 * @param first2 An input iterator.
740 * @return A boolean true or false.
742 * This compares the elements of two ranges using @c == and returns true or
743 * false depending on whether all of the corresponding elements of the
746 template<typename _InputIterator1
, typename _InputIterator2
>
748 equal(_InputIterator1 __first1
, _InputIterator1 __last1
,
749 _InputIterator2 __first2
)
751 // concept requirements
752 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
753 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
754 __glibcxx_function_requires(_EqualOpConcept
<
755 typename iterator_traits
<_InputIterator1
>::value_type
,
756 typename iterator_traits
<_InputIterator2
>::value_type
>)
757 __glibcxx_requires_valid_range(__first1
, __last1
);
759 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
760 if (!(*__first1
== *__first2
))
766 * @brief Tests a range for element-wise equality.
767 * @param first1 An input iterator.
768 * @param last1 An input iterator.
769 * @param first2 An input iterator.
770 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
771 * @return A boolean true or false.
773 * This compares the elements of two ranges using the binary_pred
774 * parameter, and returns true or
775 * false depending on whether all of the corresponding elements of the
778 template<typename _InputIterator1
, typename _InputIterator2
,
779 typename _BinaryPredicate
>
781 equal(_InputIterator1 __first1
, _InputIterator1 __last1
,
782 _InputIterator2 __first2
,
783 _BinaryPredicate __binary_pred
)
785 // concept requirements
786 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
787 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
788 __glibcxx_requires_valid_range(__first1
, __last1
);
790 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
791 if (!__binary_pred(*__first1
, *__first2
))
797 * @brief Performs "dictionary" comparison on ranges.
798 * @param first1 An input iterator.
799 * @param last1 An input iterator.
800 * @param first2 An input iterator.
801 * @param last2 An input iterator.
802 * @return A boolean true or false.
804 * "Returns true if the sequence of elements defined by the range
805 * [first1,last1) is lexicographically less than the sequence of elements
806 * defined by the range [first2,last2). Returns false otherwise."
807 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
808 * then this is an inline call to @c memcmp.
810 template<typename _InputIterator1
, typename _InputIterator2
>
812 lexicographical_compare(_InputIterator1 __first1
, _InputIterator1 __last1
,
813 _InputIterator2 __first2
, _InputIterator2 __last2
)
815 // concept requirements
816 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
817 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
818 __glibcxx_function_requires(_LessThanOpConcept
<
819 typename iterator_traits
<_InputIterator1
>::value_type
,
820 typename iterator_traits
<_InputIterator2
>::value_type
>)
821 __glibcxx_function_requires(_LessThanOpConcept
<
822 typename iterator_traits
<_InputIterator2
>::value_type
,
823 typename iterator_traits
<_InputIterator1
>::value_type
>)
824 __glibcxx_requires_valid_range(__first1
, __last1
);
825 __glibcxx_requires_valid_range(__first2
, __last2
);
827 for (; __first1
!= __last1
&& __first2
!= __last2
;
828 ++__first1
, ++__first2
)
830 if (*__first1
< *__first2
)
832 if (*__first2
< *__first1
)
835 return __first1
== __last1
&& __first2
!= __last2
;
839 * @brief Performs "dictionary" comparison on ranges.
840 * @param first1 An input iterator.
841 * @param last1 An input iterator.
842 * @param first2 An input iterator.
843 * @param last2 An input iterator.
844 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
845 * @return A boolean true or false.
847 * The same as the four-parameter @c lexigraphical_compare, but uses the
848 * comp parameter instead of @c <.
850 template<typename _InputIterator1
, typename _InputIterator2
,
853 lexicographical_compare(_InputIterator1 __first1
, _InputIterator1 __last1
,
854 _InputIterator2 __first2
, _InputIterator2 __last2
,
857 // concept requirements
858 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
859 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
860 __glibcxx_requires_valid_range(__first1
, __last1
);
861 __glibcxx_requires_valid_range(__first2
, __last2
);
863 for (; __first1
!= __last1
&& __first2
!= __last2
;
864 ++__first1
, ++__first2
)
866 if (__comp(*__first1
, *__first2
))
868 if (__comp(*__first2
, *__first1
))
871 return __first1
== __last1
&& __first2
!= __last2
;
875 lexicographical_compare(const unsigned char* __first1
,
876 const unsigned char* __last1
,
877 const unsigned char* __first2
,
878 const unsigned char* __last2
)
880 __glibcxx_requires_valid_range(__first1
, __last1
);
881 __glibcxx_requires_valid_range(__first2
, __last2
);
883 const size_t __len1
= __last1
- __first1
;
884 const size_t __len2
= __last2
- __first2
;
885 const int __result
= std::memcmp(__first1
, __first2
,
886 std::min(__len1
, __len2
));
887 return __result
!= 0 ? __result
< 0 : __len1
< __len2
;
891 lexicographical_compare(const char* __first1
, const char* __last1
,
892 const char* __first2
, const char* __last2
)
894 __glibcxx_requires_valid_range(__first1
, __last1
);
895 __glibcxx_requires_valid_range(__first2
, __last2
);
897 #if CHAR_MAX == SCHAR_MAX
898 return std::lexicographical_compare((const signed char*) __first1
,
899 (const signed char*) __last1
,
900 (const signed char*) __first2
,
901 (const signed char*) __last2
);
902 #else /* CHAR_MAX == SCHAR_MAX */
903 return std::lexicographical_compare((const unsigned char*) __first1
,
904 (const unsigned char*) __last1
,
905 (const unsigned char*) __first2
,
906 (const unsigned char*) __last2
);
907 #endif /* CHAR_MAX == SCHAR_MAX */