1 // Bits and pieces used in algorithms -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003 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>
71 #include <bits/stl_pair.h>
72 #include <bits/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>
82 * @brief Swaps the contents of two iterators.
83 * @param a An iterator.
84 * @param b Another iterator.
87 * This function swaps the values pointed to by two iterators, not the
88 * iterators themselves.
90 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
92 iter_swap(_ForwardIterator1 __a
, _ForwardIterator2 __b
)
94 typedef typename iterator_traits
<_ForwardIterator1
>::value_type _ValueType1
;
95 typedef typename iterator_traits
<_ForwardIterator2
>::value_type _ValueType2
;
97 // concept requirements
98 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<_ForwardIterator1
>)
99 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<_ForwardIterator2
>)
100 __glibcxx_function_requires(_ConvertibleConcept
<_ValueType1
, _ValueType2
>)
101 __glibcxx_function_requires(_ConvertibleConcept
<_ValueType2
, _ValueType1
>)
103 _ValueType1 __tmp
= *__a
;
109 * @brief Swaps two values.
110 * @param a A thing of arbitrary type.
111 * @param b Another thing of arbitrary type.
114 * This is the simple classic generic implementation. It will work on
115 * any type which has a copy constructor and an assignment operator.
117 template<typename _Tp
>
119 swap(_Tp
& __a
, _Tp
& __b
)
121 // concept requirements
122 __glibcxx_function_requires(_SGIAssignableConcept
<_Tp
>)
133 * @brief This does what you think it does.
134 * @param a A thing of arbitrary type.
135 * @param b Another thing of arbitrary type.
136 * @return The lesser of the parameters.
138 * This is the simple classic generic implementation. It will work on
139 * temporary expressions, since they are only evaluated once, unlike a
140 * preprocessor macro.
142 template<typename _Tp
>
144 min(const _Tp
& __a
, const _Tp
& __b
)
146 // concept requirements
147 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
148 //return __b < __a ? __b : __a;
149 if (__b
< __a
) return __b
; return __a
;
153 * @brief This does what you think it does.
154 * @param a A thing of arbitrary type.
155 * @param b Another thing of arbitrary type.
156 * @return The greater of the parameters.
158 * This is the simple classic generic implementation. It will work on
159 * temporary expressions, since they are only evaluated once, unlike a
160 * preprocessor macro.
162 template<typename _Tp
>
164 max(const _Tp
& __a
, const _Tp
& __b
)
166 // concept requirements
167 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
168 //return __a < __b ? __b : __a;
169 if (__a
< __b
) return __b
; return __a
;
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 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
177 * @return The lesser of the parameters.
179 * This will work on temporary expressions, since they are only evaluated
180 * once, unlike a preprocessor macro.
182 template<typename _Tp
, typename _Compare
>
184 min(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
186 //return __comp(__b, __a) ? __b : __a;
187 if (__comp(__b
, __a
)) return __b
; return __a
;
191 * @brief This does what you think it does.
192 * @param a A thing of arbitrary type.
193 * @param b Another thing of arbitrary type.
194 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
195 * @return The greater of the parameters.
197 * This will work on temporary expressions, since they are only evaluated
198 * once, unlike a preprocessor macro.
200 template<typename _Tp
, typename _Compare
>
202 max(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
204 //return __comp(__a, __b) ? __b : __a;
205 if (__comp(__a
, __b
)) return __b
; return __a
;
208 // All of these auxiliary functions serve two purposes. (1) Replace
209 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
210 // because the input and output ranges are permitted to overlap.)
211 // (2) If we're using random access iterators, then write the loop as
212 // a for loop with an explicit count.
214 template<typename _InputIterator
, typename _OutputIterator
>
215 inline _OutputIterator
216 __copy(_InputIterator __first
, _InputIterator __last
,
217 _OutputIterator __result
, input_iterator_tag
)
219 for (; __first
!= __last
; ++__result
, ++__first
)
220 *__result
= *__first
;
224 template<typename _RandomAccessIterator
, typename _OutputIterator
>
225 inline _OutputIterator
226 __copy(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
227 _OutputIterator __result
, random_access_iterator_tag
)
229 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
231 for (_Distance __n
= __last
- __first
; __n
> 0; --__n
)
233 *__result
= *__first
;
240 template<typename _Tp
>
242 __copy_trivial(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
244 std::memmove(__result
, __first
, sizeof(_Tp
) * (__last
- __first
));
245 return __result
+ (__last
- __first
);
248 template<typename _InputIterator
, typename _OutputIterator
>
249 inline _OutputIterator
250 __copy_aux2(_InputIterator __first
, _InputIterator __last
,
251 _OutputIterator __result
, __false_type
)
252 { return std::__copy(__first
, __last
, __result
, std::__iterator_category(__first
)); }
254 template<typename _InputIterator
, typename _OutputIterator
>
255 inline _OutputIterator
256 __copy_aux2(_InputIterator __first
, _InputIterator __last
,
257 _OutputIterator __result
, __true_type
)
258 { return std::__copy(__first
, __last
, __result
, std::__iterator_category(__first
)); }
260 template<typename _Tp
>
262 __copy_aux2(_Tp
* __first
, _Tp
* __last
, _Tp
* __result
, __true_type
)
263 { return std::__copy_trivial(__first
, __last
, __result
); }
265 template<typename _Tp
>
267 __copy_aux2(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
,
269 { return std::__copy_trivial(__first
, __last
, __result
); }
271 template<typename _InputIterator
, typename _OutputIterator
>
272 inline _OutputIterator
273 __copy_ni2(_InputIterator __first
, _InputIterator __last
,
274 _OutputIterator __result
, __true_type
)
276 typedef typename iterator_traits
<_InputIterator
>::value_type
278 typedef typename __type_traits
<_ValueType
>::has_trivial_assignment_operator
280 return _OutputIterator(std::__copy_aux2(__first
, __last
, __result
.base(),
284 template<typename _InputIterator
, typename _OutputIterator
>
285 inline _OutputIterator
286 __copy_ni2(_InputIterator __first
, _InputIterator __last
,
287 _OutputIterator __result
, __false_type
)
289 typedef typename iterator_traits
<_InputIterator
>::value_type _ValueType
;
290 typedef typename __type_traits
<_ValueType
>::has_trivial_assignment_operator
292 return std::__copy_aux2(__first
, __last
, __result
, _Trivial());
295 template<typename _InputIterator
, typename _OutputIterator
>
296 inline _OutputIterator
297 __copy_ni1(_InputIterator __first
, _InputIterator __last
,
298 _OutputIterator __result
, __true_type
)
300 typedef typename _Is_normal_iterator
<_OutputIterator
>::_Normal __Normal
;
301 return std::__copy_ni2(__first
.base(), __last
.base(), __result
, __Normal());
304 template<typename _InputIterator
, typename _OutputIterator
>
305 inline _OutputIterator
306 __copy_ni1(_InputIterator __first
, _InputIterator __last
,
307 _OutputIterator __result
, __false_type
)
309 typedef typename _Is_normal_iterator
<_OutputIterator
>::_Normal __Normal
;
310 return std::__copy_ni2(__first
, __last
, __result
, __Normal());
314 * @brief Copies the range [first,last) into result.
315 * @param first An input iterator.
316 * @param last An input iterator.
317 * @param result An output iterator.
318 * @return result + (first - last)
320 * This inline function will boil down to a call to @c memmove whenever
321 * possible. Failing that, if random access iterators are passed, then the
322 * loop count will be known (and therefore a candidate for compiler
323 * optimizations such as unrolling). Result may not be contained within
324 * [first,last); the copy_backward function should be used instead.
326 * Note that the end of the output range is permitted to be contained
327 * within [first,last).
329 template<typename _InputIterator
, typename _OutputIterator
>
330 inline _OutputIterator
331 copy(_InputIterator __first
, _InputIterator __last
, _OutputIterator __result
)
333 // concept requirements
334 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
335 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
336 typename iterator_traits
<_InputIterator
>::value_type
>)
337 __glibcxx_requires_valid_range(__first
, __last
);
339 typedef typename _Is_normal_iterator
<_InputIterator
>::_Normal __Normal
;
340 return std::__copy_ni1(__first
, __last
, __result
, __Normal());
343 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
344 inline _BidirectionalIterator2
345 __copy_backward(_BidirectionalIterator1 __first
, _BidirectionalIterator1 __last
,
346 _BidirectionalIterator2 __result
, bidirectional_iterator_tag
)
348 while (__first
!= __last
)
349 *--__result
= *--__last
;
353 template<typename _RandomAccessIterator
, typename _BidirectionalIterator
>
354 inline _BidirectionalIterator
355 __copy_backward(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
356 _BidirectionalIterator __result
, random_access_iterator_tag
)
358 typename iterator_traits
<_RandomAccessIterator
>::difference_type __n
;
359 for (__n
= __last
- __first
; __n
> 0; --__n
)
360 *--__result
= *--__last
;
365 // This dispatch class is a workaround for compilers that do not
366 // have partial ordering of function templates. All we're doing is
367 // creating a specialization so that we can turn a call to copy_backward
368 // into a memmove whenever possible.
369 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
371 struct __copy_backward_dispatch
373 static _BidirectionalIterator2
374 copy(_BidirectionalIterator1 __first
, _BidirectionalIterator1 __last
,
375 _BidirectionalIterator2 __result
)
377 return std::__copy_backward(__first
, __last
, __result
,
378 std::__iterator_category(__first
));
382 template<typename _Tp
>
383 struct __copy_backward_dispatch
<_Tp
*, _Tp
*, __true_type
>
386 copy(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
388 const ptrdiff_t _Num
= __last
- __first
;
389 std::memmove(__result
- _Num
, __first
, sizeof(_Tp
) * _Num
);
390 return __result
- _Num
;
394 template<typename _Tp
>
395 struct __copy_backward_dispatch
<const _Tp
*, _Tp
*, __true_type
>
398 copy(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
400 return std::__copy_backward_dispatch
<_Tp
*, _Tp
*, __true_type
>
401 ::copy(__first
, __last
, __result
);
405 template<typename _BI1
, typename _BI2
>
407 __copy_backward_aux(_BI1 __first
, _BI1 __last
, _BI2 __result
)
409 typedef typename __type_traits
<typename iterator_traits
<_BI2
>::value_type
>
410 ::has_trivial_assignment_operator _Trivial
;
411 return std::__copy_backward_dispatch
<_BI1
, _BI2
, _Trivial
>::copy(__first
,
416 template <typename _BI1
, typename _BI2
>
418 __copy_backward_output_normal_iterator(_BI1 __first
, _BI1 __last
,
419 _BI2 __result
, __true_type
)
420 { return _BI2(std::__copy_backward_aux(__first
, __last
, __result
.base())); }
422 template <typename _BI1
, typename _BI2
>
424 __copy_backward_output_normal_iterator(_BI1 __first
, _BI1 __last
,
425 _BI2 __result
, __false_type
)
426 { return std::__copy_backward_aux(__first
, __last
, __result
); }
428 template <typename _BI1
, typename _BI2
>
430 __copy_backward_input_normal_iterator(_BI1 __first
, _BI1 __last
,
431 _BI2 __result
, __true_type
)
433 typedef typename _Is_normal_iterator
<_BI2
>::_Normal __Normal
;
434 return std::__copy_backward_output_normal_iterator(__first
.base(),
435 __last
.base(), __result
,
439 template <typename _BI1
, typename _BI2
>
441 __copy_backward_input_normal_iterator(_BI1 __first
, _BI1 __last
,
442 _BI2 __result
, __false_type
)
444 typedef typename _Is_normal_iterator
<_BI2
>::_Normal __Normal
;
445 return std::__copy_backward_output_normal_iterator(__first
, __last
, __result
,
450 * @brief Copies the range [first,last) into result.
451 * @param first A bidirectional iterator.
452 * @param last A bidirectional iterator.
453 * @param result A bidirectional iterator.
454 * @return result - (first - last)
456 * The function has the same effect as copy, but starts at the end of the
457 * range and works its way to the start, returning the start of the result.
458 * This inline function will boil down to a call to @c memmove whenever
459 * possible. Failing that, if random access iterators are passed, then the
460 * loop count will be known (and therefore a candidate for compiler
461 * optimizations such as unrolling).
463 * Result may not be in the range [first,last). Use copy instead. Note
464 * that the start of the output range may overlap [first,last).
466 template <typename _BI1
, typename _BI2
>
468 copy_backward(_BI1 __first
, _BI1 __last
, _BI2 __result
)
470 // concept requirements
471 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BI1
>)
472 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<_BI2
>)
473 __glibcxx_function_requires(_ConvertibleConcept
<
474 typename iterator_traits
<_BI1
>::value_type
,
475 typename iterator_traits
<_BI2
>::value_type
>)
476 __glibcxx_requires_valid_range(__first
, __last
);
478 typedef typename _Is_normal_iterator
<_BI1
>::_Normal __Normal
;
479 return std::__copy_backward_input_normal_iterator(__first
, __last
, __result
,
485 * @brief Fills the range [first,last) with copies of value.
486 * @param first A forward iterator.
487 * @param last A forward iterator.
488 * @param value A reference-to-const of arbitrary type.
491 * This function fills a range with copies of the same value. For one-byte
492 * types filling contiguous areas of memory, this becomes an inline call to
495 template<typename _ForwardIterator
, typename _Tp
>
497 fill(_ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __value
)
499 // concept requirements
500 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<_ForwardIterator
>)
501 __glibcxx_requires_valid_range(__first
, __last
);
503 for ( ; __first
!= __last
; ++__first
)
508 * @brief Fills the range [first,first+n) with copies of value.
509 * @param first An output iterator.
510 * @param n The count of copies to perform.
511 * @param value A reference-to-const of arbitrary type.
512 * @return The iterator at first+n.
514 * This function fills a range with copies of the same value. For one-byte
515 * types filling contiguous areas of memory, this becomes an inline call to
518 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
520 fill_n(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
522 // concept requirements
523 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,_Tp
>)
525 for ( ; __n
> 0; --__n
, ++__first
)
530 // Specialization: for one-byte types we can use memset.
532 fill(unsigned char* __first
, unsigned char* __last
, const unsigned char& __c
)
534 __glibcxx_requires_valid_range(__first
, __last
);
535 unsigned char __tmp
= __c
;
536 std::memset(__first
, __tmp
, __last
- __first
);
540 fill(signed char* __first
, signed char* __last
, const signed char& __c
)
542 __glibcxx_requires_valid_range(__first
, __last
);
543 signed char __tmp
= __c
;
544 std::memset(__first
, static_cast<unsigned char>(__tmp
), __last
- __first
);
548 fill(char* __first
, char* __last
, const char& __c
)
550 __glibcxx_requires_valid_range(__first
, __last
);
552 std::memset(__first
, static_cast<unsigned char>(__tmp
), __last
- __first
);
555 template<typename _Size
>
556 inline unsigned char*
557 fill_n(unsigned char* __first
, _Size __n
, const unsigned char& __c
)
559 std::fill(__first
, __first
+ __n
, __c
);
560 return __first
+ __n
;
563 template<typename _Size
>
565 fill_n(char* __first
, _Size __n
, const signed char& __c
)
567 std::fill(__first
, __first
+ __n
, __c
);
568 return __first
+ __n
;
571 template<typename _Size
>
573 fill_n(char* __first
, _Size __n
, const char& __c
)
575 std::fill(__first
, __first
+ __n
, __c
);
576 return __first
+ __n
;
581 * @brief Finds the places in ranges which don't match.
582 * @param first1 An input iterator.
583 * @param last1 An input iterator.
584 * @param first2 An input iterator.
585 * @return A pair of iterators pointing to the first mismatch.
587 * This compares the elements of two ranges using @c == and returns a pair
588 * of iterators. The first iterator points into the first range, the
589 * second iterator points into the second range, and the elements pointed
590 * to by the iterators are not equal.
592 template<typename _InputIterator1
, typename _InputIterator2
>
593 pair
<_InputIterator1
, _InputIterator2
>
594 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
595 _InputIterator2 __first2
)
597 // concept requirements
598 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
599 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
600 __glibcxx_function_requires(_EqualityComparableConcept
<
601 typename iterator_traits
<_InputIterator1
>::value_type
>)
602 __glibcxx_function_requires(_EqualityComparableConcept
<
603 typename iterator_traits
<_InputIterator2
>::value_type
>)
604 __glibcxx_requires_valid_range(__first1
, __last1
);
606 while (__first1
!= __last1
&& *__first1
== *__first2
)
611 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
615 * @brief Finds the places in ranges which don't match.
616 * @param first1 An input iterator.
617 * @param last1 An input iterator.
618 * @param first2 An input iterator.
619 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
620 * @return A pair of iterators pointing to the first mismatch.
622 * This compares the elements of two ranges using the binary_pred
623 * parameter, and returns a pair
624 * of iterators. The first iterator points into the first range, the
625 * second iterator points into the second range, and the elements pointed
626 * to by the iterators are not equal.
628 template<typename _InputIterator1
, typename _InputIterator2
, typename _BinaryPredicate
>
629 pair
<_InputIterator1
, _InputIterator2
>
630 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
631 _InputIterator2 __first2
, _BinaryPredicate __binary_pred
)
633 // concept requirements
634 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
635 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
636 __glibcxx_requires_valid_range(__first1
, __last1
);
638 while (__first1
!= __last1
&& __binary_pred(*__first1
, *__first2
))
643 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
647 * @brief Tests a range for element-wise equality.
648 * @param first1 An input iterator.
649 * @param last1 An input iterator.
650 * @param first2 An input iterator.
651 * @return A boolean true or false.
653 * This compares the elements of two ranges using @c == and returns true or
654 * false depending on whether all of the corresponding elements of the
657 template<typename _InputIterator1
, typename _InputIterator2
>
659 equal(_InputIterator1 __first1
, _InputIterator1 __last1
, _InputIterator2 __first2
)
661 // concept requirements
662 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
663 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
664 __glibcxx_function_requires(_EqualOpConcept
<
665 typename iterator_traits
<_InputIterator1
>::value_type
,
666 typename iterator_traits
<_InputIterator2
>::value_type
>)
667 __glibcxx_requires_valid_range(__first1
, __last1
);
669 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
)
670 if (!(*__first1
== *__first2
))
676 * @brief Tests a range for element-wise equality.
677 * @param first1 An input iterator.
678 * @param last1 An input iterator.
679 * @param first2 An input iterator.
680 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
681 * @return A boolean true or false.
683 * This compares the elements of two ranges using the binary_pred
684 * parameter, and returns true or
685 * false depending on whether all of the corresponding elements of the
688 template<typename _InputIterator1
, typename _InputIterator2
, typename _BinaryPredicate
>
690 equal(_InputIterator1 __first1
, _InputIterator1 __last1
,
691 _InputIterator2 __first2
,
692 _BinaryPredicate __binary_pred
)
694 // concept requirements
695 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
696 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
697 __glibcxx_requires_valid_range(__first1
, __last1
);
699 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
)
700 if (!__binary_pred(*__first1
, *__first2
))
706 * @brief Performs "dictionary" comparison on ranges.
707 * @param first1 An input iterator.
708 * @param last1 An input iterator.
709 * @param first2 An input iterator.
710 * @param last2 An input iterator.
711 * @return A boolean true or false.
713 * "Returns true if the sequence of elements defined by the range
714 * [first1,last1) is lexicographically less than the sequence of elements
715 * defined by the range [first2,last2). Returns false otherwise."
716 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
717 * then this is an inline call to @c memcmp.
719 template<typename _InputIterator1
, typename _InputIterator2
>
721 lexicographical_compare(_InputIterator1 __first1
, _InputIterator1 __last1
,
722 _InputIterator2 __first2
, _InputIterator2 __last2
)
724 // concept requirements
725 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
726 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
727 __glibcxx_function_requires(_LessThanComparableConcept
<
728 typename iterator_traits
<_InputIterator1
>::value_type
>)
729 __glibcxx_function_requires(_LessThanComparableConcept
<
730 typename iterator_traits
<_InputIterator2
>::value_type
>)
731 __glibcxx_requires_valid_range(__first1
, __last1
);
732 __glibcxx_requires_valid_range(__first2
, __last2
);
734 for (;__first1
!= __last1
&& __first2
!= __last2
; ++__first1
, ++__first2
)
736 if (*__first1
< *__first2
)
738 if (*__first2
< *__first1
)
741 return __first1
== __last1
&& __first2
!= __last2
;
745 * @brief Performs "dictionary" comparison on ranges.
746 * @param first1 An input iterator.
747 * @param last1 An input iterator.
748 * @param first2 An input iterator.
749 * @param last2 An input iterator.
750 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
751 * @return A boolean true or false.
753 * The same as the four-parameter @c lexigraphical_compare, but uses the
754 * comp parameter instead of @c <.
756 template<typename _InputIterator1
, typename _InputIterator2
, typename _Compare
>
758 lexicographical_compare(_InputIterator1 __first1
, _InputIterator1 __last1
,
759 _InputIterator2 __first2
, _InputIterator2 __last2
,
762 // concept requirements
763 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
764 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
765 __glibcxx_requires_valid_range(__first1
, __last1
);
766 __glibcxx_requires_valid_range(__first2
, __last2
);
768 for ( ; __first1
!= __last1
&& __first2
!= __last2
769 ; ++__first1
, ++__first2
)
771 if (__comp(*__first1
, *__first2
))
773 if (__comp(*__first2
, *__first1
))
776 return __first1
== __last1
&& __first2
!= __last2
;
780 lexicographical_compare(const unsigned char* __first1
,
781 const unsigned char* __last1
,
782 const unsigned char* __first2
,
783 const unsigned char* __last2
)
785 __glibcxx_requires_valid_range(__first1
, __last1
);
786 __glibcxx_requires_valid_range(__first2
, __last2
);
788 const size_t __len1
= __last1
- __first1
;
789 const size_t __len2
= __last2
- __first2
;
790 const int __result
= std::memcmp(__first1
, __first2
, std::min(__len1
, __len2
));
791 return __result
!= 0 ? __result
< 0 : __len1
< __len2
;
795 lexicographical_compare(const char* __first1
, const char* __last1
,
796 const char* __first2
, const char* __last2
)
798 __glibcxx_requires_valid_range(__first1
, __last1
);
799 __glibcxx_requires_valid_range(__first2
, __last2
);
801 #if CHAR_MAX == SCHAR_MAX
802 return std::lexicographical_compare((const signed char*) __first1
,
803 (const signed char*) __last1
,
804 (const signed char*) __first2
,
805 (const signed char*) __last2
);
806 #else /* CHAR_MAX == SCHAR_MAX */
807 return std::lexicographical_compare((const unsigned char*) __first1
,
808 (const unsigned char*) __last1
,
809 (const unsigned char*) __first2
,
810 (const unsigned char*) __last2
);
811 #endif /* CHAR_MAX == SCHAR_MAX */