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>
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
96 typedef typename iterator_traits
<_ForwardIterator2
>::value_type
99 // concept requirements
100 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
102 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
104 __glibcxx_function_requires(_ConvertibleConcept
<_ValueType1
,
106 __glibcxx_function_requires(_ConvertibleConcept
<_ValueType2
,
109 const _ValueType1 __tmp
= *__a
;
115 * @brief Swaps two values.
116 * @param a A thing of arbitrary type.
117 * @param b Another thing of arbitrary type.
120 * This is the simple classic generic implementation. It will work on
121 * any type which has a copy constructor and an assignment operator.
123 template<typename _Tp
>
125 swap(_Tp
& __a
, _Tp
& __b
)
127 // concept requirements
128 __glibcxx_function_requires(_SGIAssignableConcept
<_Tp
>)
130 const _Tp __tmp
= __a
;
139 * @brief This does what you think it does.
140 * @param a A thing of arbitrary type.
141 * @param b Another thing of arbitrary type.
142 * @return The lesser of the parameters.
144 * This is the simple classic generic implementation. It will work on
145 * temporary expressions, since they are only evaluated once, unlike a
146 * preprocessor macro.
148 template<typename _Tp
>
150 min(const _Tp
& __a
, const _Tp
& __b
)
152 // concept requirements
153 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
154 //return __b < __a ? __b : __a;
161 * @brief This does what you think it does.
162 * @param a A thing of arbitrary type.
163 * @param b Another thing of arbitrary type.
164 * @return The greater of the parameters.
166 * This is the simple classic generic implementation. It will work on
167 * temporary expressions, since they are only evaluated once, unlike a
168 * preprocessor macro.
170 template<typename _Tp
>
172 max(const _Tp
& __a
, const _Tp
& __b
)
174 // concept requirements
175 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
176 //return __a < __b ? __b : __a;
183 * @brief This does what you think it does.
184 * @param a A thing of arbitrary type.
185 * @param b Another thing of arbitrary type.
186 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
187 * @return The lesser of the parameters.
189 * This will work on temporary expressions, since they are only evaluated
190 * once, unlike a preprocessor macro.
192 template<typename _Tp
, typename _Compare
>
194 min(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
196 //return __comp(__b, __a) ? __b : __a;
197 if (__comp(__b
, __a
))
203 * @brief This does what you think it does.
204 * @param a A thing of arbitrary type.
205 * @param b Another thing of arbitrary type.
206 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
207 * @return The greater of the parameters.
209 * This will work on temporary expressions, since they are only evaluated
210 * once, unlike a preprocessor macro.
212 template<typename _Tp
, typename _Compare
>
214 max(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
216 //return __comp(__a, __b) ? __b : __a;
217 if (__comp(__a
, __b
))
222 // All of these auxiliary functions serve two purposes. (1) Replace
223 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
224 // because the input and output ranges are permitted to overlap.)
225 // (2) If we're using random access iterators, then write the loop as
226 // a for loop with an explicit count.
228 template<typename _InputIterator
, typename _OutputIterator
>
229 inline _OutputIterator
230 __copy(_InputIterator __first
, _InputIterator __last
,
231 _OutputIterator __result
, input_iterator_tag
)
233 for (; __first
!= __last
; ++__result
, ++__first
)
234 *__result
= *__first
;
238 template<typename _RandomAccessIterator
, typename _OutputIterator
>
239 inline _OutputIterator
240 __copy(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
241 _OutputIterator __result
, random_access_iterator_tag
)
243 typedef typename iterator_traits
<_RandomAccessIterator
>::difference_type
245 for (_Distance __n
= __last
- __first
; __n
> 0; --__n
)
247 *__result
= *__first
;
254 template<typename _Tp
>
256 __copy_trivial(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
258 std::memmove(__result
, __first
, sizeof(_Tp
) * (__last
- __first
));
259 return __result
+ (__last
- __first
);
262 template<typename _InputIterator
, typename _OutputIterator
>
263 inline _OutputIterator
264 __copy_aux2(_InputIterator __first
, _InputIterator __last
,
265 _OutputIterator __result
, __false_type
)
266 { return std::__copy(__first
, __last
, __result
,
267 std::__iterator_category(__first
)); }
269 template<typename _InputIterator
, typename _OutputIterator
>
270 inline _OutputIterator
271 __copy_aux2(_InputIterator __first
, _InputIterator __last
,
272 _OutputIterator __result
, __true_type
)
273 { return std::__copy(__first
, __last
, __result
,
274 std::__iterator_category(__first
)); }
276 template<typename _Tp
>
278 __copy_aux2(_Tp
* __first
, _Tp
* __last
, _Tp
* __result
, __true_type
)
279 { return std::__copy_trivial(__first
, __last
, __result
); }
281 template<typename _Tp
>
283 __copy_aux2(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
,
285 { return std::__copy_trivial(__first
, __last
, __result
); }
287 template<typename _InputIterator
, typename _OutputIterator
>
288 inline _OutputIterator
289 __copy_ni2(_InputIterator __first
, _InputIterator __last
,
290 _OutputIterator __result
, __true_type
)
292 typedef typename iterator_traits
<_InputIterator
>::value_type
294 typedef typename __type_traits
<
295 _ValueType
>::has_trivial_assignment_operator _Trivial
;
296 return _OutputIterator(std::__copy_aux2(__first
, __last
, __result
.base(),
300 template<typename _InputIterator
, typename _OutputIterator
>
301 inline _OutputIterator
302 __copy_ni2(_InputIterator __first
, _InputIterator __last
,
303 _OutputIterator __result
, __false_type
)
305 typedef typename iterator_traits
<_InputIterator
>::value_type _ValueType
;
306 typedef typename __type_traits
<
307 _ValueType
>::has_trivial_assignment_operator _Trivial
;
308 return std::__copy_aux2(__first
, __last
, __result
, _Trivial());
311 template<typename _InputIterator
, typename _OutputIterator
>
312 inline _OutputIterator
313 __copy_ni1(_InputIterator __first
, _InputIterator __last
,
314 _OutputIterator __result
, __true_type
)
316 typedef typename _Is_normal_iterator
<_OutputIterator
>::_Normal __Normal
;
317 return std::__copy_ni2(__first
.base(), __last
.base(),
318 __result
, __Normal());
321 template<typename _InputIterator
, typename _OutputIterator
>
322 inline _OutputIterator
323 __copy_ni1(_InputIterator __first
, _InputIterator __last
,
324 _OutputIterator __result
, __false_type
)
326 typedef typename _Is_normal_iterator
<_OutputIterator
>::_Normal __Normal
;
327 return std::__copy_ni2(__first
, __last
, __result
, __Normal());
331 * @brief Copies the range [first,last) into result.
332 * @param first An input iterator.
333 * @param last An input iterator.
334 * @param result An output iterator.
335 * @return result + (first - last)
337 * This inline function will boil down to a call to @c memmove whenever
338 * possible. Failing that, if random access iterators are passed, then the
339 * loop count will be known (and therefore a candidate for compiler
340 * optimizations such as unrolling). Result may not be contained within
341 * [first,last); the copy_backward function should be used instead.
343 * Note that the end of the output range is permitted to be contained
344 * within [first,last).
346 template<typename _InputIterator
, typename _OutputIterator
>
347 inline _OutputIterator
348 copy(_InputIterator __first
, _InputIterator __last
,
349 _OutputIterator __result
)
351 // concept requirements
352 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
353 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
,
354 typename iterator_traits
<_InputIterator
>::value_type
>)
355 __glibcxx_requires_valid_range(__first
, __last
);
357 typedef typename _Is_normal_iterator
<_InputIterator
>::_Normal __Normal
;
358 return std::__copy_ni1(__first
, __last
, __result
, __Normal());
361 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
>
362 inline _BidirectionalIterator2
363 __copy_backward(_BidirectionalIterator1 __first
,
364 _BidirectionalIterator1 __last
,
365 _BidirectionalIterator2 __result
,
366 bidirectional_iterator_tag
)
368 while (__first
!= __last
)
369 *--__result
= *--__last
;
373 template<typename _RandomAccessIterator
, typename _BidirectionalIterator
>
374 inline _BidirectionalIterator
375 __copy_backward(_RandomAccessIterator __first
, _RandomAccessIterator __last
,
376 _BidirectionalIterator __result
, random_access_iterator_tag
)
378 typename iterator_traits
<_RandomAccessIterator
>::difference_type __n
;
379 for (__n
= __last
- __first
; __n
> 0; --__n
)
380 *--__result
= *--__last
;
385 // This dispatch class is a workaround for compilers that do not
386 // have partial ordering of function templates. All we're doing is
387 // creating a specialization so that we can turn a call to copy_backward
388 // into a memmove whenever possible.
389 template<typename _BidirectionalIterator1
, typename _BidirectionalIterator2
,
391 struct __copy_backward_dispatch
393 static _BidirectionalIterator2
394 copy(_BidirectionalIterator1 __first
, _BidirectionalIterator1 __last
,
395 _BidirectionalIterator2 __result
)
396 { return std::__copy_backward(__first
, __last
, __result
,
397 std::__iterator_category(__first
)); }
400 template<typename _Tp
>
401 struct __copy_backward_dispatch
<_Tp
*, _Tp
*, __true_type
>
404 copy(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
406 const ptrdiff_t _Num
= __last
- __first
;
407 std::memmove(__result
- _Num
, __first
, sizeof(_Tp
) * _Num
);
408 return __result
- _Num
;
412 template<typename _Tp
>
413 struct __copy_backward_dispatch
<const _Tp
*, _Tp
*, __true_type
>
416 copy(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
418 return std::__copy_backward_dispatch
<_Tp
*, _Tp
*, __true_type
>
419 ::copy(__first
, __last
, __result
);
423 template<typename _BI1
, typename _BI2
>
425 __copy_backward_aux(_BI1 __first
, _BI1 __last
, _BI2 __result
)
427 typedef typename __type_traits
<typename iterator_traits
<_BI2
>::value_type
>
428 ::has_trivial_assignment_operator _Trivial
;
430 std::__copy_backward_dispatch
<_BI1
, _BI2
, _Trivial
>::copy(__first
,
435 template <typename _BI1
, typename _BI2
>
437 __copy_backward_output_normal_iterator(_BI1 __first
, _BI1 __last
,
438 _BI2 __result
, __true_type
)
439 { return _BI2(std::__copy_backward_aux(__first
, __last
, __result
.base())); }
441 template <typename _BI1
, typename _BI2
>
443 __copy_backward_output_normal_iterator(_BI1 __first
, _BI1 __last
,
444 _BI2 __result
, __false_type
)
445 { return std::__copy_backward_aux(__first
, __last
, __result
); }
447 template <typename _BI1
, typename _BI2
>
449 __copy_backward_input_normal_iterator(_BI1 __first
, _BI1 __last
,
450 _BI2 __result
, __true_type
)
452 typedef typename _Is_normal_iterator
<_BI2
>::_Normal __Normal
;
453 return std::__copy_backward_output_normal_iterator(__first
.base(),
455 __result
, __Normal());
458 template <typename _BI1
, typename _BI2
>
460 __copy_backward_input_normal_iterator(_BI1 __first
, _BI1 __last
,
461 _BI2 __result
, __false_type
)
463 typedef typename _Is_normal_iterator
<_BI2
>::_Normal __Normal
;
464 return std::__copy_backward_output_normal_iterator(__first
, __last
,
465 __result
, __Normal());
469 * @brief Copies the range [first,last) into result.
470 * @param first A bidirectional iterator.
471 * @param last A bidirectional iterator.
472 * @param result A bidirectional iterator.
473 * @return result - (first - last)
475 * The function has the same effect as copy, but starts at the end of the
476 * range and works its way to the start, returning the start of the result.
477 * This inline function will boil down to a call to @c memmove whenever
478 * possible. Failing that, if random access iterators are passed, then the
479 * loop count will be known (and therefore a candidate for compiler
480 * optimizations such as unrolling).
482 * Result may not be in the range [first,last). Use copy instead. Note
483 * that the start of the output range may overlap [first,last).
485 template <typename _BI1
, typename _BI2
>
487 copy_backward(_BI1 __first
, _BI1 __last
, _BI2 __result
)
489 // concept requirements
490 __glibcxx_function_requires(_BidirectionalIteratorConcept
<_BI1
>)
491 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept
<_BI2
>)
492 __glibcxx_function_requires(_ConvertibleConcept
<
493 typename iterator_traits
<_BI1
>::value_type
,
494 typename iterator_traits
<_BI2
>::value_type
>)
495 __glibcxx_requires_valid_range(__first
, __last
);
497 typedef typename _Is_normal_iterator
<_BI1
>::_Normal __Normal
;
498 return std::__copy_backward_input_normal_iterator(__first
, __last
,
499 __result
, __Normal());
505 template<typename _ForwardIterator
, typename _Tp
>
507 fill(_ForwardIterator __first
, _ForwardIterator __last
,
510 for (; __first
!= __last
; ++__first
)
516 struct __fill
<__true_type
>
518 template<typename _ForwardIterator
, typename _Tp
>
520 fill(_ForwardIterator __first
, _ForwardIterator __last
,
523 const _Tp __tmp
= __value
;
524 for (; __first
!= __last
; ++__first
)
530 * @brief Fills the range [first,last) with copies of value.
531 * @param first A forward iterator.
532 * @param last A forward iterator.
533 * @param value A reference-to-const of arbitrary type.
536 * This function fills a range with copies of the same value. For one-byte
537 * types filling contiguous areas of memory, this becomes an inline call to
540 template<typename _ForwardIterator
, typename _Tp
>
542 fill(_ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __value
)
544 // concept requirements
545 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
547 __glibcxx_requires_valid_range(__first
, __last
);
549 typedef typename __type_traits
<_Tp
>::has_trivial_copy_constructor
551 std::__fill
<_Trivial
>::fill(__first
, __last
, __value
);
554 // Specialization: for one-byte types we can use memset.
556 fill(unsigned char* __first
, unsigned char* __last
, const unsigned char& __c
)
558 __glibcxx_requires_valid_range(__first
, __last
);
559 const unsigned char __tmp
= __c
;
560 std::memset(__first
, __tmp
, __last
- __first
);
564 fill(signed char* __first
, signed char* __last
, const signed char& __c
)
566 __glibcxx_requires_valid_range(__first
, __last
);
567 const signed char __tmp
= __c
;
568 std::memset(__first
, static_cast<unsigned char>(__tmp
), __last
- __first
);
572 fill(char* __first
, char* __last
, const char& __c
)
574 __glibcxx_requires_valid_range(__first
, __last
);
575 const char __tmp
= __c
;
576 std::memset(__first
, static_cast<unsigned char>(__tmp
), __last
- __first
);
582 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
583 static _OutputIterator
584 fill_n(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
586 for (; __n
> 0; --__n
, ++__first
)
593 struct __fill_n
<__true_type
>
595 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
596 static _OutputIterator
597 fill_n(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
599 const _Tp __tmp
= __value
;
600 for (; __n
> 0; --__n
, ++__first
)
607 * @brief Fills the range [first,first+n) with copies of value.
608 * @param first An output iterator.
609 * @param n The count of copies to perform.
610 * @param value A reference-to-const of arbitrary type.
611 * @return The iterator at first+n.
613 * This function fills a range with copies of the same value. For one-byte
614 * types filling contiguous areas of memory, this becomes an inline call to
617 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
619 fill_n(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
621 // concept requirements
622 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
, _Tp
>)
624 typedef typename __type_traits
<_Tp
>::has_trivial_copy_constructor
626 return std::__fill_n
<_Trivial
>::fill_n(__first
, __n
, __value
);
629 template<typename _Size
>
630 inline unsigned char*
631 fill_n(unsigned char* __first
, _Size __n
, const unsigned char& __c
)
633 std::fill(__first
, __first
+ __n
, __c
);
634 return __first
+ __n
;
637 template<typename _Size
>
639 fill_n(char* __first
, _Size __n
, const signed char& __c
)
641 std::fill(__first
, __first
+ __n
, __c
);
642 return __first
+ __n
;
645 template<typename _Size
>
647 fill_n(char* __first
, _Size __n
, const char& __c
)
649 std::fill(__first
, __first
+ __n
, __c
);
650 return __first
+ __n
;
654 * @brief Finds the places in ranges which don't match.
655 * @param first1 An input iterator.
656 * @param last1 An input iterator.
657 * @param first2 An input iterator.
658 * @return A pair of iterators pointing to the first mismatch.
660 * This compares the elements of two ranges using @c == and returns a pair
661 * of iterators. The first iterator points into the first range, the
662 * second iterator points into the second range, and the elements pointed
663 * to by the iterators are not equal.
665 template<typename _InputIterator1
, typename _InputIterator2
>
666 pair
<_InputIterator1
, _InputIterator2
>
667 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
668 _InputIterator2 __first2
)
670 // concept requirements
671 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
672 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
673 __glibcxx_function_requires(_EqualityComparableConcept
<
674 typename iterator_traits
<_InputIterator1
>::value_type
>)
675 __glibcxx_function_requires(_EqualityComparableConcept
<
676 typename iterator_traits
<_InputIterator2
>::value_type
>)
677 __glibcxx_requires_valid_range(__first1
, __last1
);
679 while (__first1
!= __last1
&& *__first1
== *__first2
)
684 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
688 * @brief Finds the places in ranges which don't match.
689 * @param first1 An input iterator.
690 * @param last1 An input iterator.
691 * @param first2 An input iterator.
692 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
693 * @return A pair of iterators pointing to the first mismatch.
695 * This compares the elements of two ranges using the binary_pred
696 * parameter, and returns a pair
697 * of iterators. The first iterator points into the first range, the
698 * second iterator points into the second range, and the elements pointed
699 * to by the iterators are not equal.
701 template<typename _InputIterator1
, typename _InputIterator2
,
702 typename _BinaryPredicate
>
703 pair
<_InputIterator1
, _InputIterator2
>
704 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
705 _InputIterator2 __first2
, _BinaryPredicate __binary_pred
)
707 // concept requirements
708 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
709 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
710 __glibcxx_requires_valid_range(__first1
, __last1
);
712 while (__first1
!= __last1
&& __binary_pred(*__first1
, *__first2
))
717 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
721 * @brief Tests a range for element-wise equality.
722 * @param first1 An input iterator.
723 * @param last1 An input iterator.
724 * @param first2 An input iterator.
725 * @return A boolean true or false.
727 * This compares the elements of two ranges using @c == and returns true or
728 * false depending on whether all of the corresponding elements of the
731 template<typename _InputIterator1
, typename _InputIterator2
>
733 equal(_InputIterator1 __first1
, _InputIterator1 __last1
,
734 _InputIterator2 __first2
)
736 // concept requirements
737 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
738 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
739 __glibcxx_function_requires(_EqualOpConcept
<
740 typename iterator_traits
<_InputIterator1
>::value_type
,
741 typename iterator_traits
<_InputIterator2
>::value_type
>)
742 __glibcxx_requires_valid_range(__first1
, __last1
);
744 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
)
745 if (!(*__first1
== *__first2
))
751 * @brief Tests a range for element-wise equality.
752 * @param first1 An input iterator.
753 * @param last1 An input iterator.
754 * @param first2 An input iterator.
755 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
756 * @return A boolean true or false.
758 * This compares the elements of two ranges using the binary_pred
759 * parameter, and returns true or
760 * false depending on whether all of the corresponding elements of the
763 template<typename _InputIterator1
, typename _InputIterator2
,
764 typename _BinaryPredicate
>
766 equal(_InputIterator1 __first1
, _InputIterator1 __last1
,
767 _InputIterator2 __first2
,
768 _BinaryPredicate __binary_pred
)
770 // concept requirements
771 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
772 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
773 __glibcxx_requires_valid_range(__first1
, __last1
);
775 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
)
776 if (!__binary_pred(*__first1
, *__first2
))
782 * @brief Performs "dictionary" comparison on ranges.
783 * @param first1 An input iterator.
784 * @param last1 An input iterator.
785 * @param first2 An input iterator.
786 * @param last2 An input iterator.
787 * @return A boolean true or false.
789 * "Returns true if the sequence of elements defined by the range
790 * [first1,last1) is lexicographically less than the sequence of elements
791 * defined by the range [first2,last2). Returns false otherwise."
792 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
793 * then this is an inline call to @c memcmp.
795 template<typename _InputIterator1
, typename _InputIterator2
>
797 lexicographical_compare(_InputIterator1 __first1
, _InputIterator1 __last1
,
798 _InputIterator2 __first2
, _InputIterator2 __last2
)
800 // concept requirements
801 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
802 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
803 __glibcxx_function_requires(_LessThanComparableConcept
<
804 typename iterator_traits
<_InputIterator1
>::value_type
>)
805 __glibcxx_function_requires(_LessThanComparableConcept
<
806 typename iterator_traits
<_InputIterator2
>::value_type
>)
807 __glibcxx_requires_valid_range(__first1
, __last1
);
808 __glibcxx_requires_valid_range(__first2
, __last2
);
810 for (;__first1
!= __last1
&& __first2
!= __last2
; ++__first1
, ++__first2
)
812 if (*__first1
< *__first2
)
814 if (*__first2
< *__first1
)
817 return __first1
== __last1
&& __first2
!= __last2
;
821 * @brief Performs "dictionary" comparison on ranges.
822 * @param first1 An input iterator.
823 * @param last1 An input iterator.
824 * @param first2 An input iterator.
825 * @param last2 An input iterator.
826 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
827 * @return A boolean true or false.
829 * The same as the four-parameter @c lexigraphical_compare, but uses the
830 * comp parameter instead of @c <.
832 template<typename _InputIterator1
, typename _InputIterator2
,
835 lexicographical_compare(_InputIterator1 __first1
, _InputIterator1 __last1
,
836 _InputIterator2 __first2
, _InputIterator2 __last2
,
839 // concept requirements
840 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
841 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
842 __glibcxx_requires_valid_range(__first1
, __last1
);
843 __glibcxx_requires_valid_range(__first2
, __last2
);
845 for ( ; __first1
!= __last1
&& __first2
!= __last2
846 ; ++__first1
, ++__first2
)
848 if (__comp(*__first1
, *__first2
))
850 if (__comp(*__first2
, *__first1
))
853 return __first1
== __last1
&& __first2
!= __last2
;
857 lexicographical_compare(const unsigned char* __first1
,
858 const unsigned char* __last1
,
859 const unsigned char* __first2
,
860 const unsigned char* __last2
)
862 __glibcxx_requires_valid_range(__first1
, __last1
);
863 __glibcxx_requires_valid_range(__first2
, __last2
);
865 const size_t __len1
= __last1
- __first1
;
866 const size_t __len2
= __last2
- __first2
;
867 const int __result
= std::memcmp(__first1
, __first2
,
868 std::min(__len1
, __len2
));
869 return __result
!= 0 ? __result
< 0 : __len1
< __len2
;
873 lexicographical_compare(const char* __first1
, const char* __last1
,
874 const char* __first2
, const char* __last2
)
876 __glibcxx_requires_valid_range(__first1
, __last1
);
877 __glibcxx_requires_valid_range(__first2
, __last2
);
879 #if CHAR_MAX == SCHAR_MAX
880 return std::lexicographical_compare((const signed char*) __first1
,
881 (const signed char*) __last1
,
882 (const signed char*) __first2
,
883 (const signed char*) __last2
);
884 #else /* CHAR_MAX == SCHAR_MAX */
885 return std::lexicographical_compare((const unsigned char*) __first1
,
886 (const unsigned char*) __last1
,
887 (const unsigned char*) __first2
,
888 (const unsigned char*) __last2
);
889 #endif /* CHAR_MAX == SCHAR_MAX */