1 // Bits and pieces used in algorithms -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004 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
,
158 std::__iter_swap
<__are_same
<_ValueType1
, _ValueType2
>::_M_type
>::
166 * @brief This does what you think it does.
167 * @param a A thing of arbitrary type.
168 * @param b Another thing of arbitrary type.
169 * @return The lesser of the parameters.
171 * This is the simple classic generic implementation. It will work on
172 * temporary expressions, since they are only evaluated once, unlike a
173 * preprocessor macro.
175 template<typename _Tp
>
177 min(const _Tp
& __a
, const _Tp
& __b
)
179 // concept requirements
180 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
181 //return __b < __a ? __b : __a;
188 * @brief This does what you think it does.
189 * @param a A thing of arbitrary type.
190 * @param b Another thing of arbitrary type.
191 * @return The greater of the parameters.
193 * This is the simple classic generic implementation. It will work on
194 * temporary expressions, since they are only evaluated once, unlike a
195 * preprocessor macro.
197 template<typename _Tp
>
199 max(const _Tp
& __a
, const _Tp
& __b
)
201 // concept requirements
202 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
203 //return __a < __b ? __b : __a;
210 * @brief This does what you think it does.
211 * @param a A thing of arbitrary type.
212 * @param b Another thing of arbitrary type.
213 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
214 * @return The lesser of the parameters.
216 * This will work on temporary expressions, since they are only evaluated
217 * once, unlike a preprocessor macro.
219 template<typename _Tp
, typename _Compare
>
221 min(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
223 //return __comp(__b, __a) ? __b : __a;
224 if (__comp(__b
, __a
))
230 * @brief This does what you think it does.
231 * @param a A thing of arbitrary type.
232 * @param b Another thing of arbitrary type.
233 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
234 * @return The greater of the parameters.
236 * This will work on temporary expressions, since they are only evaluated
237 * once, unlike a preprocessor macro.
239 template<typename _Tp
, typename _Compare
>
241 max(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
243 //return __comp(__a, __b) ? __b : __a;
244 if (__comp(__a
, __b
))
249 // All of these auxiliary structs serve two purposes. (1) Replace
250 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
251 // because the input and output ranges are permitted to overlap.)
252 // (2) If we're using random access iterators, then write the loop as
253 // a for loop with an explicit count.
255 template<bool, typename
>
258 template<typename _II
, typename _OI
>
260 copy(_II __first
, _II __last
, _OI __result
)
262 for (; __first
!= __last
; ++__result
, ++__first
)
263 *__result
= *__first
;
268 template<bool _BoolType
>
269 struct __copy
<_BoolType
, random_access_iterator_tag
>
271 template<typename _II
, typename _OI
>
273 copy(_II __first
, _II __last
, _OI __result
)
275 typedef typename iterator_traits
<_II
>::difference_type _Distance
;
276 for(_Distance __n
= __last
- __first
; __n
> 0; --__n
)
278 *__result
= *__first
;
287 struct __copy
<true, random_access_iterator_tag
>
289 template<typename _Tp
>
291 copy(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
293 std::memmove(__result
, __first
, sizeof(_Tp
) * (__last
- __first
));
294 return __result
+ (__last
- __first
);
298 template<typename _II
, typename _OI
>
300 __copy_aux(_II __first
, _II __last
, _OI __result
)
302 typedef typename iterator_traits
<_II
>::value_type _ValueTypeI
;
303 typedef typename iterator_traits
<_OI
>::value_type _ValueTypeO
;
304 typedef typename iterator_traits
<_II
>::iterator_category _Category
;
305 const bool __simple
= (__is_scalar
<_ValueTypeI
>::_M_type
306 && __is_pointer
<_II
>::_M_type
307 && __is_pointer
<_OI
>::_M_type
308 && __are_same
<_ValueTypeI
, _ValueTypeO
>::_M_type
);
310 return std::__copy
<__simple
, _Category
>::copy(__first
, __last
, __result
);
316 template<typename _II
, typename _OI
>
318 copy_n(_II __first
, _II __last
, _OI __result
)
319 { return std::__copy_aux(__first
, __last
, __result
); }
323 struct __copy_normal
<true, false>
325 template<typename _II
, typename _OI
>
327 copy_n(_II __first
, _II __last
, _OI __result
)
328 { return std::__copy_aux(__first
.base(), __last
.base(), __result
); }
332 struct __copy_normal
<false, true>
334 template<typename _II
, typename _OI
>
336 copy_n(_II __first
, _II __last
, _OI __result
)
337 { return _OI(std::__copy_aux(__first
, __last
, __result
.base())); }
341 struct __copy_normal
<true, true>
343 template<typename _II
, typename _OI
>
345 copy_n(_II __first
, _II __last
, _OI __result
)
346 { return _OI(std::__copy_aux(__first
.base(), __last
.base(),
351 * @brief Copies the range [first,last) into result.
352 * @param first An input iterator.
353 * @param last An input iterator.
354 * @param result An output iterator.
355 * @return result + (first - last)
357 * This inline function will boil down to a call to @c memmove whenever
358 * possible. Failing that, if random access iterators are passed, then the
359 * loop count will be known (and therefore a candidate for compiler
360 * optimizations such as unrolling). Result may not be contained within
361 * [first,last); the copy_backward function should be used instead.
363 * Note that the end of the output range is permitted to be contained
364 * within [first,last).
366 template<typename _InputIterator
, typename _OutputIterator
>
367 inline _OutputIterator
368 copy(_InputIterator __first
, _InputIterator __last
,
369 _OutputIterator __result
)
371 // concept requirements
372 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
373 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
374 typename iterator_traits
<_InputIterator
>::value_type
>)
375 __glibcxx_requires_valid_range(__first
, __last
);
377 const bool __in
= __is_normal_iterator
<_InputIterator
>::_M_type
;
378 const bool __out
= __is_normal_iterator
<_OutputIterator
>::_M_type
;
379 return std::__copy_normal
<__in
, __out
>::copy_n(__first
, __last
,
383 template<bool, typename
>
384 struct __copy_backward
386 template<typename _BI1
, typename _BI2
>
388 copy_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
390 while (__first
!= __last
)
391 *--__result
= *--__last
;
396 template<bool _BoolType
>
397 struct __copy_backward
<_BoolType
, random_access_iterator_tag
>
399 template<typename _BI1
, typename _BI2
>
401 copy_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
403 typename iterator_traits
<_BI1
>::difference_type __n
;
404 for (__n
= __last
- __first
; __n
> 0; --__n
)
405 *--__result
= *--__last
;
411 struct __copy_backward
<true, random_access_iterator_tag
>
413 template<typename _Tp
>
415 copy_b(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
417 const ptrdiff_t _Num
= __last
- __first
;
418 std::memmove(__result
- _Num
, __first
, sizeof(_Tp
) * _Num
);
419 return __result
- _Num
;
423 template<typename _BI1
, typename _BI2
>
425 __copy_backward_aux(_BI1 __first
, _BI1 __last
, _BI2 __result
)
427 typedef typename iterator_traits
<_BI1
>::value_type _ValueType1
;
428 typedef typename iterator_traits
<_BI2
>::value_type _ValueType2
;
429 typedef typename iterator_traits
<_BI1
>::iterator_category _Category
;
430 const bool __simple
= (__is_scalar
<_ValueType1
>::_M_type
431 && __is_pointer
<_BI1
>::_M_type
432 && __is_pointer
<_BI2
>::_M_type
433 && __are_same
<_ValueType1
, _ValueType2
>::_M_type
);
435 return std::__copy_backward
<__simple
, _Category
>::copy_b(__first
, __last
,
440 struct __copy_backward_normal
442 template<typename _BI1
, typename _BI2
>
444 copy_b_n(_BI1 __first
, _BI1 __last
, _BI2 __result
)
445 { return std::__copy_backward_aux(__first
, __last
, __result
); }
449 struct __copy_backward_normal
<true, false>
451 template<typename _BI1
, typename _BI2
>
453 copy_b_n(_BI1 __first
, _BI1 __last
, _BI2 __result
)
454 { return std::__copy_backward_aux(__first
.base(), __last
.base(),
459 struct __copy_backward_normal
<false, true>
461 template<typename _BI1
, typename _BI2
>
463 copy_b_n(_BI1 __first
, _BI1 __last
, _BI2 __result
)
464 { return _BI2(std::__copy_backward_aux(__first
, __last
,
469 struct __copy_backward_normal
<true, true>
471 template<typename _BI1
, typename _BI2
>
473 copy_b_n(_BI1 __first
, _BI1 __last
, _BI2 __result
)
474 { return _BI2(std::__copy_backward_aux(__first
.base(), __last
.base(),
479 * @brief Copies the range [first,last) into result.
480 * @param first A bidirectional iterator.
481 * @param last A bidirectional iterator.
482 * @param result A bidirectional iterator.
483 * @return result - (first - last)
485 * The function has the same effect as copy, but starts at the end of the
486 * range and works its way to the start, returning the start of the result.
487 * This inline function will boil down to a call to @c memmove whenever
488 * possible. Failing that, if random access iterators are passed, then the
489 * loop count will be known (and therefore a candidate for compiler
490 * optimizations such as unrolling).
492 * Result may not be in the range [first,last). Use copy instead. Note
493 * that the start of the output range may overlap [first,last).
495 template <typename _BI1
, typename _BI2
>
497 copy_backward(_BI1 __first
, _BI1 __last
, _BI2 __result
)
499 // concept requirements
500 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BI1
>)
501 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<_BI2
>)
502 __glibcxx_function_requires(_ConvertibleConcept
<
503 typename iterator_traits
<_BI1
>::value_type
,
504 typename iterator_traits
<_BI2
>::value_type
>)
505 __glibcxx_requires_valid_range(__first
, __last
);
507 const bool __bi1
= __is_normal_iterator
<_BI1
>::_M_type
;
508 const bool __bi2
= __is_normal_iterator
<_BI2
>::_M_type
;
509 return std::__copy_backward_normal
<__bi1
, __bi2
>::copy_b_n(__first
, __last
,
516 template<typename _ForwardIterator
, typename _Tp
>
518 fill(_ForwardIterator __first
, _ForwardIterator __last
,
521 for (; __first
!= __last
; ++__first
)
529 template<typename _ForwardIterator
, typename _Tp
>
531 fill(_ForwardIterator __first
, _ForwardIterator __last
,
534 const _Tp __tmp
= __value
;
535 for (; __first
!= __last
; ++__first
)
541 * @brief Fills the range [first,last) with copies of value.
542 * @param first A forward iterator.
543 * @param last A forward iterator.
544 * @param value A reference-to-const of arbitrary type.
547 * This function fills a range with copies of the same value. For one-byte
548 * types filling contiguous areas of memory, this becomes an inline call to
551 template<typename _ForwardIterator
, typename _Tp
>
553 fill(_ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __value
)
555 // concept requirements
556 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
558 __glibcxx_requires_valid_range(__first
, __last
);
560 const bool __scalar
= __is_scalar
<_Tp
>::_M_type
;
561 std::__fill
<__scalar
>::fill(__first
, __last
, __value
);
564 // Specialization: for one-byte types we can use memset.
566 fill(unsigned char* __first
, unsigned char* __last
, const unsigned char& __c
)
568 __glibcxx_requires_valid_range(__first
, __last
);
569 const unsigned char __tmp
= __c
;
570 std::memset(__first
, __tmp
, __last
- __first
);
574 fill(signed char* __first
, signed char* __last
, const signed char& __c
)
576 __glibcxx_requires_valid_range(__first
, __last
);
577 const signed char __tmp
= __c
;
578 std::memset(__first
, static_cast<unsigned char>(__tmp
), __last
- __first
);
582 fill(char* __first
, char* __last
, const char& __c
)
584 __glibcxx_requires_valid_range(__first
, __last
);
585 const char __tmp
= __c
;
586 std::memset(__first
, static_cast<unsigned char>(__tmp
), __last
- __first
);
592 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
593 static _OutputIterator
594 fill_n(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
596 for (; __n
> 0; --__n
, ++__first
)
603 struct __fill_n
<true>
605 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
606 static _OutputIterator
607 fill_n(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
609 const _Tp __tmp
= __value
;
610 for (; __n
> 0; --__n
, ++__first
)
617 * @brief Fills the range [first,first+n) with copies of value.
618 * @param first An output iterator.
619 * @param n The count of copies to perform.
620 * @param value A reference-to-const of arbitrary type.
621 * @return The iterator at first+n.
623 * This function fills a range with copies of the same value. For one-byte
624 * types filling contiguous areas of memory, this becomes an inline call to
627 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
629 fill_n(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
631 // concept requirements
632 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
, _Tp
>)
634 const bool __scalar
= __is_scalar
<_Tp
>::_M_type
;
635 return std::__fill_n
<__scalar
>::fill_n(__first
, __n
, __value
);
638 template<typename _Size
>
639 inline unsigned char*
640 fill_n(unsigned char* __first
, _Size __n
, const unsigned char& __c
)
642 std::fill(__first
, __first
+ __n
, __c
);
643 return __first
+ __n
;
646 template<typename _Size
>
648 fill_n(char* __first
, _Size __n
, const signed char& __c
)
650 std::fill(__first
, __first
+ __n
, __c
);
651 return __first
+ __n
;
654 template<typename _Size
>
656 fill_n(char* __first
, _Size __n
, const char& __c
)
658 std::fill(__first
, __first
+ __n
, __c
);
659 return __first
+ __n
;
663 * @brief Finds the places in ranges which don't match.
664 * @param first1 An input iterator.
665 * @param last1 An input iterator.
666 * @param first2 An input iterator.
667 * @return A pair of iterators pointing to the first mismatch.
669 * This compares the elements of two ranges using @c == and returns a pair
670 * of iterators. The first iterator points into the first range, the
671 * second iterator points into the second range, and the elements pointed
672 * to by the iterators are not equal.
674 template<typename _InputIterator1
, typename _InputIterator2
>
675 pair
<_InputIterator1
, _InputIterator2
>
676 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
677 _InputIterator2 __first2
)
679 // concept requirements
680 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
681 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
682 __glibcxx_function_requires(_EqualityComparableConcept
<
683 typename iterator_traits
<_InputIterator1
>::value_type
>)
684 __glibcxx_function_requires(_EqualityComparableConcept
<
685 typename iterator_traits
<_InputIterator2
>::value_type
>)
686 __glibcxx_requires_valid_range(__first1
, __last1
);
688 while (__first1
!= __last1
&& *__first1
== *__first2
)
693 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
697 * @brief Finds the places in ranges which don't match.
698 * @param first1 An input iterator.
699 * @param last1 An input iterator.
700 * @param first2 An input iterator.
701 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
702 * @return A pair of iterators pointing to the first mismatch.
704 * This compares the elements of two ranges using the binary_pred
705 * parameter, and returns a pair
706 * of iterators. The first iterator points into the first range, the
707 * second iterator points into the second range, and the elements pointed
708 * to by the iterators are not equal.
710 template<typename _InputIterator1
, typename _InputIterator2
,
711 typename _BinaryPredicate
>
712 pair
<_InputIterator1
, _InputIterator2
>
713 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
714 _InputIterator2 __first2
, _BinaryPredicate __binary_pred
)
716 // concept requirements
717 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
718 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
719 __glibcxx_requires_valid_range(__first1
, __last1
);
721 while (__first1
!= __last1
&& __binary_pred(*__first1
, *__first2
))
726 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
730 * @brief Tests a range for element-wise equality.
731 * @param first1 An input iterator.
732 * @param last1 An input iterator.
733 * @param first2 An input iterator.
734 * @return A boolean true or false.
736 * This compares the elements of two ranges using @c == and returns true or
737 * false depending on whether all of the corresponding elements of the
740 template<typename _InputIterator1
, typename _InputIterator2
>
742 equal(_InputIterator1 __first1
, _InputIterator1 __last1
,
743 _InputIterator2 __first2
)
745 // concept requirements
746 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
747 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
748 __glibcxx_function_requires(_EqualOpConcept
<
749 typename iterator_traits
<_InputIterator1
>::value_type
,
750 typename iterator_traits
<_InputIterator2
>::value_type
>)
751 __glibcxx_requires_valid_range(__first1
, __last1
);
753 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
754 if (!(*__first1
== *__first2
))
760 * @brief Tests a range for element-wise equality.
761 * @param first1 An input iterator.
762 * @param last1 An input iterator.
763 * @param first2 An input iterator.
764 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
765 * @return A boolean true or false.
767 * This compares the elements of two ranges using the binary_pred
768 * parameter, and returns true or
769 * false depending on whether all of the corresponding elements of the
772 template<typename _InputIterator1
, typename _InputIterator2
,
773 typename _BinaryPredicate
>
775 equal(_InputIterator1 __first1
, _InputIterator1 __last1
,
776 _InputIterator2 __first2
,
777 _BinaryPredicate __binary_pred
)
779 // concept requirements
780 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
781 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
782 __glibcxx_requires_valid_range(__first1
, __last1
);
784 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
785 if (!__binary_pred(*__first1
, *__first2
))
791 * @brief Performs "dictionary" comparison on ranges.
792 * @param first1 An input iterator.
793 * @param last1 An input iterator.
794 * @param first2 An input iterator.
795 * @param last2 An input iterator.
796 * @return A boolean true or false.
798 * "Returns true if the sequence of elements defined by the range
799 * [first1,last1) is lexicographically less than the sequence of elements
800 * defined by the range [first2,last2). Returns false otherwise."
801 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
802 * then this is an inline call to @c memcmp.
804 template<typename _InputIterator1
, typename _InputIterator2
>
806 lexicographical_compare(_InputIterator1 __first1
, _InputIterator1 __last1
,
807 _InputIterator2 __first2
, _InputIterator2 __last2
)
809 // concept requirements
810 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
811 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
812 __glibcxx_function_requires(_LessThanComparableConcept
<
813 typename iterator_traits
<_InputIterator1
>::value_type
>)
814 __glibcxx_function_requires(_LessThanComparableConcept
<
815 typename iterator_traits
<_InputIterator2
>::value_type
>)
816 __glibcxx_requires_valid_range(__first1
, __last1
);
817 __glibcxx_requires_valid_range(__first2
, __last2
);
819 for (; __first1
!= __last1
&& __first2
!= __last2
;
820 ++__first1
, ++__first2
)
822 if (*__first1
< *__first2
)
824 if (*__first2
< *__first1
)
827 return __first1
== __last1
&& __first2
!= __last2
;
831 * @brief Performs "dictionary" comparison on ranges.
832 * @param first1 An input iterator.
833 * @param last1 An input iterator.
834 * @param first2 An input iterator.
835 * @param last2 An input iterator.
836 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
837 * @return A boolean true or false.
839 * The same as the four-parameter @c lexigraphical_compare, but uses the
840 * comp parameter instead of @c <.
842 template<typename _InputIterator1
, typename _InputIterator2
,
845 lexicographical_compare(_InputIterator1 __first1
, _InputIterator1 __last1
,
846 _InputIterator2 __first2
, _InputIterator2 __last2
,
849 // concept requirements
850 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
851 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
852 __glibcxx_requires_valid_range(__first1
, __last1
);
853 __glibcxx_requires_valid_range(__first2
, __last2
);
855 for (; __first1
!= __last1
&& __first2
!= __last2
;
856 ++__first1
, ++__first2
)
858 if (__comp(*__first1
, *__first2
))
860 if (__comp(*__first2
, *__first1
))
863 return __first1
== __last1
&& __first2
!= __last2
;
867 lexicographical_compare(const unsigned char* __first1
,
868 const unsigned char* __last1
,
869 const unsigned char* __first2
,
870 const unsigned char* __last2
)
872 __glibcxx_requires_valid_range(__first1
, __last1
);
873 __glibcxx_requires_valid_range(__first2
, __last2
);
875 const size_t __len1
= __last1
- __first1
;
876 const size_t __len2
= __last2
- __first2
;
877 const int __result
= std::memcmp(__first1
, __first2
,
878 std::min(__len1
, __len2
));
879 return __result
!= 0 ? __result
< 0 : __len1
< __len2
;
883 lexicographical_compare(const char* __first1
, const char* __last1
,
884 const char* __first2
, const char* __last2
)
886 __glibcxx_requires_valid_range(__first1
, __last1
);
887 __glibcxx_requires_valid_range(__first2
, __last2
);
889 #if CHAR_MAX == SCHAR_MAX
890 return std::lexicographical_compare((const signed char*) __first1
,
891 (const signed char*) __last1
,
892 (const signed char*) __first2
,
893 (const signed char*) __last2
);
894 #else /* CHAR_MAX == SCHAR_MAX */
895 return std::lexicographical_compare((const unsigned char*) __first1
,
896 (const unsigned char*) __last1
,
897 (const unsigned char*) __first2
,
898 (const unsigned char*) __last2
);
899 #endif /* CHAR_MAX == SCHAR_MAX */