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>
78 _GLIBCXX_BEGIN_NAMESPACE(std
)
81 * @brief Swaps two values.
82 * @param a A thing of arbitrary type.
83 * @param b Another thing of arbitrary type.
86 * This is the simple classic generic implementation. It will work on
87 * any type which has a copy constructor and an assignment operator.
89 template<typename _Tp
>
91 swap(_Tp
& __a
, _Tp
& __b
)
93 // concept requirements
94 __glibcxx_function_requires(_SGIAssignableConcept
<_Tp
>)
101 // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
102 // nutshell, we are partially implementing the resolution of DR 187,
103 // when it's safe, i.e., the value_types are equal.
104 template<bool _BoolType
>
107 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
109 iter_swap(_ForwardIterator1 __a
, _ForwardIterator2 __b
)
111 typedef typename iterator_traits
<_ForwardIterator1
>::value_type
113 _ValueType1 __tmp
= *__a
;
120 struct __iter_swap
<true>
122 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
124 iter_swap(_ForwardIterator1 __a
, _ForwardIterator2 __b
)
131 * @brief Swaps the contents of two iterators.
132 * @param a An iterator.
133 * @param b Another iterator.
136 * This function swaps the values pointed to by two iterators, not the
137 * iterators themselves.
139 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
141 iter_swap(_ForwardIterator1 __a
, _ForwardIterator2 __b
)
143 typedef typename iterator_traits
<_ForwardIterator1
>::value_type
145 typedef typename iterator_traits
<_ForwardIterator2
>::value_type
148 // concept requirements
149 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
151 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
153 __glibcxx_function_requires(_ConvertibleConcept
<_ValueType1
,
155 __glibcxx_function_requires(_ConvertibleConcept
<_ValueType2
,
158 typedef typename iterator_traits
<_ForwardIterator1
>::reference
160 typedef typename iterator_traits
<_ForwardIterator2
>::reference
162 std::__iter_swap
<__are_same
<_ValueType1
, _ValueType2
>::__value
&&
163 __are_same
<_ValueType1
&, _ReferenceType1
>::__value
&&
164 __are_same
<_ValueType2
&, _ReferenceType2
>::__value
>::
172 * @brief This does what you think it does.
173 * @param a A thing of arbitrary type.
174 * @param b Another thing of arbitrary type.
175 * @return The lesser of the parameters.
177 * This is the simple classic generic implementation. It will work on
178 * temporary expressions, since they are only evaluated once, unlike a
179 * preprocessor macro.
181 template<typename _Tp
>
183 min(const _Tp
& __a
, const _Tp
& __b
)
185 // concept requirements
186 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
187 //return __b < __a ? __b : __a;
194 * @brief This does what you think it does.
195 * @param a A thing of arbitrary type.
196 * @param b Another thing of arbitrary type.
197 * @return The greater of the parameters.
199 * This is the simple classic generic implementation. It will work on
200 * temporary expressions, since they are only evaluated once, unlike a
201 * preprocessor macro.
203 template<typename _Tp
>
205 max(const _Tp
& __a
, const _Tp
& __b
)
207 // concept requirements
208 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
209 //return __a < __b ? __b : __a;
216 * @brief This does what you think it does.
217 * @param a A thing of arbitrary type.
218 * @param b Another thing of arbitrary type.
219 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
220 * @return The lesser of the parameters.
222 * This will work on temporary expressions, since they are only evaluated
223 * once, unlike a preprocessor macro.
225 template<typename _Tp
, typename _Compare
>
227 min(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
229 //return __comp(__b, __a) ? __b : __a;
230 if (__comp(__b
, __a
))
236 * @brief This does what you think it does.
237 * @param a A thing of arbitrary type.
238 * @param b Another thing of arbitrary type.
239 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
240 * @return The greater of the parameters.
242 * This will work on temporary expressions, since they are only evaluated
243 * once, unlike a preprocessor macro.
245 template<typename _Tp
, typename _Compare
>
247 max(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
249 //return __comp(__a, __b) ? __b : __a;
250 if (__comp(__a
, __b
))
255 // All of these auxiliary structs serve two purposes. (1) Replace
256 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
257 // because the input and output ranges are permitted to overlap.)
258 // (2) If we're using random access iterators, then write the loop as
259 // a for loop with an explicit count.
261 template<bool, typename
>
264 template<typename _II
, typename _OI
>
266 copy(_II __first
, _II __last
, _OI __result
)
268 for (; __first
!= __last
; ++__result
, ++__first
)
269 *__result
= *__first
;
274 template<bool _BoolType
>
275 struct __copy
<_BoolType
, random_access_iterator_tag
>
277 template<typename _II
, typename _OI
>
279 copy(_II __first
, _II __last
, _OI __result
)
281 typedef typename iterator_traits
<_II
>::difference_type _Distance
;
282 for(_Distance __n
= __last
- __first
; __n
> 0; --__n
)
284 *__result
= *__first
;
293 struct __copy
<true, random_access_iterator_tag
>
295 template<typename _Tp
>
297 copy(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
299 std::memmove(__result
, __first
, sizeof(_Tp
) * (__last
- __first
));
300 return __result
+ (__last
- __first
);
304 template<typename _II
, typename _OI
>
306 __copy_aux(_II __first
, _II __last
, _OI __result
)
308 typedef typename iterator_traits
<_II
>::value_type _ValueTypeI
;
309 typedef typename iterator_traits
<_OI
>::value_type _ValueTypeO
;
310 typedef typename iterator_traits
<_II
>::iterator_category _Category
;
311 const bool __simple
= (__is_scalar
<_ValueTypeI
>::__value
312 && __is_pointer
<_II
>::__value
313 && __is_pointer
<_OI
>::__value
314 && __are_same
<_ValueTypeI
, _ValueTypeO
>::__value
);
316 return std::__copy
<__simple
, _Category
>::copy(__first
, __last
, __result
);
322 template<typename _II
, typename _OI
>
324 __copy_n(_II __first
, _II __last
, _OI __result
)
325 { return std::__copy_aux(__first
, __last
, __result
); }
329 struct __copy_normal
<true, false>
331 template<typename _II
, typename _OI
>
333 __copy_n(_II __first
, _II __last
, _OI __result
)
334 { return std::__copy_aux(__first
.base(), __last
.base(), __result
); }
338 struct __copy_normal
<false, true>
340 template<typename _II
, typename _OI
>
342 __copy_n(_II __first
, _II __last
, _OI __result
)
343 { return _OI(std::__copy_aux(__first
, __last
, __result
.base())); }
347 struct __copy_normal
<true, true>
349 template<typename _II
, typename _OI
>
351 __copy_n(_II __first
, _II __last
, _OI __result
)
352 { return _OI(std::__copy_aux(__first
.base(), __last
.base(),
357 * @brief Copies the range [first,last) into result.
358 * @param first An input iterator.
359 * @param last An input iterator.
360 * @param result An output iterator.
361 * @return result + (first - last)
363 * This inline function will boil down to a call to @c memmove whenever
364 * possible. Failing that, if random access iterators are passed, then the
365 * loop count will be known (and therefore a candidate for compiler
366 * optimizations such as unrolling). Result may not be contained within
367 * [first,last); the copy_backward function should be used instead.
369 * Note that the end of the output range is permitted to be contained
370 * within [first,last).
372 template<typename _InputIterator
, typename _OutputIterator
>
373 inline _OutputIterator
374 copy(_InputIterator __first
, _InputIterator __last
,
375 _OutputIterator __result
)
377 // concept requirements
378 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
379 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
380 typename iterator_traits
<_InputIterator
>::value_type
>)
381 __glibcxx_requires_valid_range(__first
, __last
);
383 const bool __in
= __is_normal_iterator
<_InputIterator
>::__value
;
384 const bool __out
= __is_normal_iterator
<_OutputIterator
>::__value
;
385 return std::__copy_normal
<__in
, __out
>::__copy_n(__first
, __last
,
389 template<bool, typename
>
390 struct __copy_backward
392 template<typename _BI1
, typename _BI2
>
394 __copy_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
396 while (__first
!= __last
)
397 *--__result
= *--__last
;
402 template<bool _BoolType
>
403 struct __copy_backward
<_BoolType
, random_access_iterator_tag
>
405 template<typename _BI1
, typename _BI2
>
407 __copy_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
409 typename iterator_traits
<_BI1
>::difference_type __n
;
410 for (__n
= __last
- __first
; __n
> 0; --__n
)
411 *--__result
= *--__last
;
417 struct __copy_backward
<true, random_access_iterator_tag
>
419 template<typename _Tp
>
421 __copy_b(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
423 const ptrdiff_t _Num
= __last
- __first
;
424 std::memmove(__result
- _Num
, __first
, sizeof(_Tp
) * _Num
);
425 return __result
- _Num
;
429 template<typename _BI1
, typename _BI2
>
431 __copy_backward_aux(_BI1 __first
, _BI1 __last
, _BI2 __result
)
433 typedef typename iterator_traits
<_BI1
>::value_type _ValueType1
;
434 typedef typename iterator_traits
<_BI2
>::value_type _ValueType2
;
435 typedef typename iterator_traits
<_BI1
>::iterator_category _Category
;
436 const bool __simple
= (__is_scalar
<_ValueType1
>::__value
437 && __is_pointer
<_BI1
>::__value
438 && __is_pointer
<_BI2
>::__value
439 && __are_same
<_ValueType1
, _ValueType2
>::__value
);
441 return std::__copy_backward
<__simple
, _Category
>::__copy_b(__first
,
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
,
524 template<typename _ForwardIterator
, typename _Tp
>
526 fill(_ForwardIterator __first
, _ForwardIterator __last
,
529 for (; __first
!= __last
; ++__first
)
537 template<typename _ForwardIterator
, typename _Tp
>
539 fill(_ForwardIterator __first
, _ForwardIterator __last
,
542 const _Tp __tmp
= __value
;
543 for (; __first
!= __last
; ++__first
)
549 * @brief Fills the range [first,last) with copies of value.
550 * @param first A forward iterator.
551 * @param last A forward iterator.
552 * @param value A reference-to-const of arbitrary type.
555 * This function fills a range with copies of the same value. For one-byte
556 * types filling contiguous areas of memory, this becomes an inline call to
559 template<typename _ForwardIterator
, typename _Tp
>
561 fill(_ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __value
)
563 // concept requirements
564 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
566 __glibcxx_requires_valid_range(__first
, __last
);
568 const bool __scalar
= __is_scalar
<_Tp
>::__value
;
569 std::__fill
<__scalar
>::fill(__first
, __last
, __value
);
572 // Specialization: for one-byte types we can use memset.
574 fill(unsigned char* __first
, unsigned char* __last
, const unsigned char& __c
)
576 __glibcxx_requires_valid_range(__first
, __last
);
577 const unsigned char __tmp
= __c
;
578 std::memset(__first
, __tmp
, __last
- __first
);
582 fill(signed char* __first
, signed char* __last
, const signed char& __c
)
584 __glibcxx_requires_valid_range(__first
, __last
);
585 const signed char __tmp
= __c
;
586 std::memset(__first
, static_cast<unsigned char>(__tmp
), __last
- __first
);
590 fill(char* __first
, char* __last
, const char& __c
)
592 __glibcxx_requires_valid_range(__first
, __last
);
593 const char __tmp
= __c
;
594 std::memset(__first
, static_cast<unsigned char>(__tmp
), __last
- __first
);
597 template<typename _Tp
, typename _Ref
, typename _Ptr
>
598 struct _Deque_iterator
;
600 // Overload for deque::iterators, exploiting the "segmented-iterator
601 // optimization". NB: leave const_iterators alone!
602 template<typename _Tp
>
604 fill(const _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>& __first
,
605 const _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>& __last
, const _Tp
& __value
)
607 typedef typename _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>::_Self _Self
;
609 for (typename
_Self::_Map_pointer __node
= __first
._M_node
+ 1;
610 __node
< __last
._M_node
; ++__node
)
611 std::fill(*__node
, *__node
+ _Self::_S_buffer_size(), __value
);
613 if (__first
._M_node
!= __last
._M_node
)
615 std::fill(__first
._M_cur
, __first
._M_last
, __value
);
616 std::fill(__last
._M_first
, __last
._M_cur
, __value
);
619 std::fill(__first
._M_cur
, __last
._M_cur
, __value
);
626 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
627 static _OutputIterator
628 fill_n(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
630 for (; __n
> 0; --__n
, ++__first
)
637 struct __fill_n
<true>
639 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
640 static _OutputIterator
641 fill_n(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
643 const _Tp __tmp
= __value
;
644 for (; __n
> 0; --__n
, ++__first
)
651 * @brief Fills the range [first,first+n) with copies of value.
652 * @param first An output iterator.
653 * @param n The count of copies to perform.
654 * @param value A reference-to-const of arbitrary type.
655 * @return The iterator at first+n.
657 * This function fills a range with copies of the same value. For one-byte
658 * types filling contiguous areas of memory, this becomes an inline call to
661 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
663 fill_n(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
665 // concept requirements
666 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
, _Tp
>)
668 const bool __scalar
= __is_scalar
<_Tp
>::__value
;
669 return std::__fill_n
<__scalar
>::fill_n(__first
, __n
, __value
);
672 template<typename _Size
>
673 inline unsigned char*
674 fill_n(unsigned char* __first
, _Size __n
, const unsigned char& __c
)
676 std::fill(__first
, __first
+ __n
, __c
);
677 return __first
+ __n
;
680 template<typename _Size
>
682 fill_n(char* __first
, _Size __n
, const signed char& __c
)
684 std::fill(__first
, __first
+ __n
, __c
);
685 return __first
+ __n
;
688 template<typename _Size
>
690 fill_n(char* __first
, _Size __n
, const char& __c
)
692 std::fill(__first
, __first
+ __n
, __c
);
693 return __first
+ __n
;
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 * @return A pair of iterators pointing to the first mismatch.
703 * This compares the elements of two ranges using @c == and returns a pair
704 * of iterators. The first iterator points into the first range, the
705 * second iterator points into the second range, and the elements pointed
706 * to by the iterators are not equal.
708 template<typename _InputIterator1
, typename _InputIterator2
>
709 pair
<_InputIterator1
, _InputIterator2
>
710 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
711 _InputIterator2 __first2
)
713 // concept requirements
714 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
715 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
716 __glibcxx_function_requires(_EqualOpConcept
<
717 typename iterator_traits
<_InputIterator1
>::value_type
,
718 typename iterator_traits
<_InputIterator2
>::value_type
>)
719 __glibcxx_requires_valid_range(__first1
, __last1
);
721 while (__first1
!= __last1
&& *__first1
== *__first2
)
726 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
730 * @brief Finds the places in ranges which don't match.
731 * @param first1 An input iterator.
732 * @param last1 An input iterator.
733 * @param first2 An input iterator.
734 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
735 * @return A pair of iterators pointing to the first mismatch.
737 * This compares the elements of two ranges using the binary_pred
738 * parameter, and returns a pair
739 * of iterators. The first iterator points into the first range, the
740 * second iterator points into the second range, and the elements pointed
741 * to by the iterators are not equal.
743 template<typename _InputIterator1
, typename _InputIterator2
,
744 typename _BinaryPredicate
>
745 pair
<_InputIterator1
, _InputIterator2
>
746 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
747 _InputIterator2 __first2
, _BinaryPredicate __binary_pred
)
749 // concept requirements
750 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
751 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
752 __glibcxx_requires_valid_range(__first1
, __last1
);
754 while (__first1
!= __last1
&& __binary_pred(*__first1
, *__first2
))
759 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
763 * @brief Tests a range for element-wise equality.
764 * @param first1 An input iterator.
765 * @param last1 An input iterator.
766 * @param first2 An input iterator.
767 * @return A boolean true or false.
769 * This compares the elements of two ranges using @c == and returns true or
770 * false depending on whether all of the corresponding elements of the
773 template<typename _InputIterator1
, typename _InputIterator2
>
775 equal(_InputIterator1 __first1
, _InputIterator1 __last1
,
776 _InputIterator2 __first2
)
778 // concept requirements
779 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
780 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
781 __glibcxx_function_requires(_EqualOpConcept
<
782 typename iterator_traits
<_InputIterator1
>::value_type
,
783 typename iterator_traits
<_InputIterator2
>::value_type
>)
784 __glibcxx_requires_valid_range(__first1
, __last1
);
786 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
787 if (!(*__first1
== *__first2
))
793 * @brief Tests a range for element-wise equality.
794 * @param first1 An input iterator.
795 * @param last1 An input iterator.
796 * @param first2 An input iterator.
797 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
798 * @return A boolean true or false.
800 * This compares the elements of two ranges using the binary_pred
801 * parameter, and returns true or
802 * false depending on whether all of the corresponding elements of the
805 template<typename _InputIterator1
, typename _InputIterator2
,
806 typename _BinaryPredicate
>
808 equal(_InputIterator1 __first1
, _InputIterator1 __last1
,
809 _InputIterator2 __first2
,
810 _BinaryPredicate __binary_pred
)
812 // concept requirements
813 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
814 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
815 __glibcxx_requires_valid_range(__first1
, __last1
);
817 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
818 if (!__binary_pred(*__first1
, *__first2
))
824 * @brief Performs "dictionary" comparison on ranges.
825 * @param first1 An input iterator.
826 * @param last1 An input iterator.
827 * @param first2 An input iterator.
828 * @param last2 An input iterator.
829 * @return A boolean true or false.
831 * "Returns true if the sequence of elements defined by the range
832 * [first1,last1) is lexicographically less than the sequence of elements
833 * defined by the range [first2,last2). Returns false otherwise."
834 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
835 * then this is an inline call to @c memcmp.
837 template<typename _InputIterator1
, typename _InputIterator2
>
839 lexicographical_compare(_InputIterator1 __first1
, _InputIterator1 __last1
,
840 _InputIterator2 __first2
, _InputIterator2 __last2
)
842 // concept requirements
843 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
844 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
845 __glibcxx_function_requires(_LessThanOpConcept
<
846 typename iterator_traits
<_InputIterator1
>::value_type
,
847 typename iterator_traits
<_InputIterator2
>::value_type
>)
848 __glibcxx_function_requires(_LessThanOpConcept
<
849 typename iterator_traits
<_InputIterator2
>::value_type
,
850 typename iterator_traits
<_InputIterator1
>::value_type
>)
851 __glibcxx_requires_valid_range(__first1
, __last1
);
852 __glibcxx_requires_valid_range(__first2
, __last2
);
854 for (; __first1
!= __last1
&& __first2
!= __last2
;
855 ++__first1
, ++__first2
)
857 if (*__first1
< *__first2
)
859 if (*__first2
< *__first1
)
862 return __first1
== __last1
&& __first2
!= __last2
;
866 * @brief Performs "dictionary" comparison on ranges.
867 * @param first1 An input iterator.
868 * @param last1 An input iterator.
869 * @param first2 An input iterator.
870 * @param last2 An input iterator.
871 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
872 * @return A boolean true or false.
874 * The same as the four-parameter @c lexigraphical_compare, but uses the
875 * comp parameter instead of @c <.
877 template<typename _InputIterator1
, typename _InputIterator2
,
880 lexicographical_compare(_InputIterator1 __first1
, _InputIterator1 __last1
,
881 _InputIterator2 __first2
, _InputIterator2 __last2
,
884 // concept requirements
885 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
886 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
887 __glibcxx_requires_valid_range(__first1
, __last1
);
888 __glibcxx_requires_valid_range(__first2
, __last2
);
890 for (; __first1
!= __last1
&& __first2
!= __last2
;
891 ++__first1
, ++__first2
)
893 if (__comp(*__first1
, *__first2
))
895 if (__comp(*__first2
, *__first1
))
898 return __first1
== __last1
&& __first2
!= __last2
;
902 lexicographical_compare(const unsigned char* __first1
,
903 const unsigned char* __last1
,
904 const unsigned char* __first2
,
905 const unsigned char* __last2
)
907 __glibcxx_requires_valid_range(__first1
, __last1
);
908 __glibcxx_requires_valid_range(__first2
, __last2
);
910 const size_t __len1
= __last1
- __first1
;
911 const size_t __len2
= __last2
- __first2
;
912 const int __result
= std::memcmp(__first1
, __first2
,
913 std::min(__len1
, __len2
));
914 return __result
!= 0 ? __result
< 0 : __len1
< __len2
;
918 lexicographical_compare(const char* __first1
, const char* __last1
,
919 const char* __first2
, const char* __last2
)
921 __glibcxx_requires_valid_range(__first1
, __last1
);
922 __glibcxx_requires_valid_range(__first2
, __last2
);
924 #if CHAR_MAX == SCHAR_MAX
925 return std::lexicographical_compare((const signed char*) __first1
,
926 (const signed char*) __last1
,
927 (const signed char*) __first2
,
928 (const signed char*) __last2
);
929 #else /* CHAR_MAX == SCHAR_MAX */
930 return std::lexicographical_compare((const unsigned char*) __first1
,
931 (const unsigned char*) __last1
,
932 (const unsigned char*) __first2
,
933 (const unsigned char*) __last2
);
934 #endif /* CHAR_MAX == SCHAR_MAX */
937 _GLIBCXX_END_NAMESPACE