2005-12-18 Benjamin Kosnik <bkoz@redhat.com>
[official-gcc.git] / libstdc++-v3 / include / bits / stl_algobase.h
blob730699648e1802f38a9fda549e18d818d5ad3013
1 // Bits and pieces used in algorithms -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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,
19 // USA.
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.
32 * Copyright (c) 1994
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.
61 #ifndef _ALGOBASE_H
62 #define _ALGOBASE_H 1
64 #include <bits/c++config.h>
65 #include <cstring>
66 #include <climits>
67 #include <cstdlib>
68 #include <cstddef>
69 #include <iosfwd>
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)
80 /**
81 * @brief Swaps two values.
82 * @param a A thing of arbitrary type.
83 * @param b Another thing of arbitrary type.
84 * @return Nothing.
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>
90 inline void
91 swap(_Tp& __a, _Tp& __b)
93 // concept requirements
94 __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
96 _Tp __tmp = __a;
97 __a = __b;
98 __b = __tmp;
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>
105 struct __iter_swap
107 template<typename _ForwardIterator1, typename _ForwardIterator2>
108 static void
109 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
111 typedef typename iterator_traits<_ForwardIterator1>::value_type
112 _ValueType1;
113 _ValueType1 __tmp = *__a;
114 *__a = *__b;
115 *__b = __tmp;
119 template<>
120 struct __iter_swap<true>
122 template<typename _ForwardIterator1, typename _ForwardIterator2>
123 static void
124 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
126 swap(*__a, *__b);
131 * @brief Swaps the contents of two iterators.
132 * @param a An iterator.
133 * @param b Another iterator.
134 * @return Nothing.
136 * This function swaps the values pointed to by two iterators, not the
137 * iterators themselves.
139 template<typename _ForwardIterator1, typename _ForwardIterator2>
140 inline void
141 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
143 typedef typename iterator_traits<_ForwardIterator1>::value_type
144 _ValueType1;
145 typedef typename iterator_traits<_ForwardIterator2>::value_type
146 _ValueType2;
148 // concept requirements
149 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
150 _ForwardIterator1>)
151 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
152 _ForwardIterator2>)
153 __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
154 _ValueType2>)
155 __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
156 _ValueType1>)
158 typedef typename iterator_traits<_ForwardIterator1>::reference
159 _ReferenceType1;
160 typedef typename iterator_traits<_ForwardIterator2>::reference
161 _ReferenceType2;
162 std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value &&
163 __are_same<_ValueType1 &, _ReferenceType1>::__value &&
164 __are_same<_ValueType2 &, _ReferenceType2>::__value>::
165 iter_swap(__a, __b);
168 #undef min
169 #undef max
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>
182 inline const _Tp&
183 min(const _Tp& __a, const _Tp& __b)
185 // concept requirements
186 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
187 //return __b < __a ? __b : __a;
188 if (__b < __a)
189 return __b;
190 return __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>
204 inline const _Tp&
205 max(const _Tp& __a, const _Tp& __b)
207 // concept requirements
208 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
209 //return __a < __b ? __b : __a;
210 if (__a < __b)
211 return __b;
212 return __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>
226 inline const _Tp&
227 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
229 //return __comp(__b, __a) ? __b : __a;
230 if (__comp(__b, __a))
231 return __b;
232 return __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>
246 inline const _Tp&
247 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
249 //return __comp(__a, __b) ? __b : __a;
250 if (__comp(__a, __b))
251 return __b;
252 return __a;
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>
262 struct __copy
264 template<typename _II, typename _OI>
265 static _OI
266 copy(_II __first, _II __last, _OI __result)
268 for (; __first != __last; ++__result, ++__first)
269 *__result = *__first;
270 return __result;
274 template<bool _BoolType>
275 struct __copy<_BoolType, random_access_iterator_tag>
277 template<typename _II, typename _OI>
278 static _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;
285 ++__first;
286 ++__result;
288 return __result;
292 template<>
293 struct __copy<true, random_access_iterator_tag>
295 template<typename _Tp>
296 static _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>
305 inline _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);
319 template<bool, bool>
320 struct __copy_normal
322 template<typename _II, typename _OI>
323 static _OI
324 __copy_n(_II __first, _II __last, _OI __result)
325 { return std::__copy_aux(__first, __last, __result); }
328 template<>
329 struct __copy_normal<true, false>
331 template<typename _II, typename _OI>
332 static _OI
333 __copy_n(_II __first, _II __last, _OI __result)
334 { return std::__copy_aux(__first.base(), __last.base(), __result); }
337 template<>
338 struct __copy_normal<false, true>
340 template<typename _II, typename _OI>
341 static _OI
342 __copy_n(_II __first, _II __last, _OI __result)
343 { return _OI(std::__copy_aux(__first, __last, __result.base())); }
346 template<>
347 struct __copy_normal<true, true>
349 template<typename _II, typename _OI>
350 static _OI
351 __copy_n(_II __first, _II __last, _OI __result)
352 { return _OI(std::__copy_aux(__first.base(), __last.base(),
353 __result.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,
386 __result);
389 template<bool, typename>
390 struct __copy_backward
392 template<typename _BI1, typename _BI2>
393 static _BI2
394 __copy_b(_BI1 __first, _BI1 __last, _BI2 __result)
396 while (__first != __last)
397 *--__result = *--__last;
398 return __result;
402 template<bool _BoolType>
403 struct __copy_backward<_BoolType, random_access_iterator_tag>
405 template<typename _BI1, typename _BI2>
406 static _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;
412 return __result;
416 template<>
417 struct __copy_backward<true, random_access_iterator_tag>
419 template<typename _Tp>
420 static _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>
430 inline _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,
442 __last,
443 __result);
446 template<bool, bool>
447 struct __copy_backward_normal
449 template<typename _BI1, typename _BI2>
450 static _BI2
451 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
452 { return std::__copy_backward_aux(__first, __last, __result); }
455 template<>
456 struct __copy_backward_normal<true, false>
458 template<typename _BI1, typename _BI2>
459 static _BI2
460 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
461 { return std::__copy_backward_aux(__first.base(), __last.base(),
462 __result); }
465 template<>
466 struct __copy_backward_normal<false, true>
468 template<typename _BI1, typename _BI2>
469 static _BI2
470 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
471 { return _BI2(std::__copy_backward_aux(__first, __last,
472 __result.base())); }
475 template<>
476 struct __copy_backward_normal<true, true>
478 template<typename _BI1, typename _BI2>
479 static _BI2
480 __copy_b_n(_BI1 __first, _BI1 __last, _BI2 __result)
481 { return _BI2(std::__copy_backward_aux(__first.base(), __last.base(),
482 __result.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>
503 inline _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,
517 __last,
518 __result);
521 template<bool>
522 struct __fill
524 template<typename _ForwardIterator, typename _Tp>
525 static void
526 fill(_ForwardIterator __first, _ForwardIterator __last,
527 const _Tp& __value)
529 for (; __first != __last; ++__first)
530 *__first = __value;
534 template<>
535 struct __fill<true>
537 template<typename _ForwardIterator, typename _Tp>
538 static void
539 fill(_ForwardIterator __first, _ForwardIterator __last,
540 const _Tp& __value)
542 const _Tp __tmp = __value;
543 for (; __first != __last; ++__first)
544 *__first = __tmp;
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.
553 * @return Nothing.
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
557 * @c memset.
559 template<typename _ForwardIterator, typename _Tp>
560 void
561 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
563 // concept requirements
564 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
565 _ForwardIterator>)
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.
573 inline void
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);
581 inline void
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);
589 inline void
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<bool>
598 struct __fill_n
600 template<typename _OutputIterator, typename _Size, typename _Tp>
601 static _OutputIterator
602 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
604 for (; __n > 0; --__n, ++__first)
605 *__first = __value;
606 return __first;
610 template<>
611 struct __fill_n<true>
613 template<typename _OutputIterator, typename _Size, typename _Tp>
614 static _OutputIterator
615 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
617 const _Tp __tmp = __value;
618 for (; __n > 0; --__n, ++__first)
619 *__first = __tmp;
620 return __first;
625 * @brief Fills the range [first,first+n) with copies of value.
626 * @param first An output iterator.
627 * @param n The count of copies to perform.
628 * @param value A reference-to-const of arbitrary type.
629 * @return The iterator at first+n.
631 * This function fills a range with copies of the same value. For one-byte
632 * types filling contiguous areas of memory, this becomes an inline call to
633 * @c memset.
635 template<typename _OutputIterator, typename _Size, typename _Tp>
636 _OutputIterator
637 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
639 // concept requirements
640 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, _Tp>)
642 const bool __scalar = __is_scalar<_Tp>::__value;
643 return std::__fill_n<__scalar>::fill_n(__first, __n, __value);
646 template<typename _Size>
647 inline unsigned char*
648 fill_n(unsigned char* __first, _Size __n, const unsigned char& __c)
650 std::fill(__first, __first + __n, __c);
651 return __first + __n;
654 template<typename _Size>
655 inline signed char*
656 fill_n(char* __first, _Size __n, const signed char& __c)
658 std::fill(__first, __first + __n, __c);
659 return __first + __n;
662 template<typename _Size>
663 inline char*
664 fill_n(char* __first, _Size __n, const char& __c)
666 std::fill(__first, __first + __n, __c);
667 return __first + __n;
671 * @brief Finds the places in ranges which don't match.
672 * @param first1 An input iterator.
673 * @param last1 An input iterator.
674 * @param first2 An input iterator.
675 * @return A pair of iterators pointing to the first mismatch.
677 * This compares the elements of two ranges using @c == and returns a pair
678 * of iterators. The first iterator points into the first range, the
679 * second iterator points into the second range, and the elements pointed
680 * to by the iterators are not equal.
682 template<typename _InputIterator1, typename _InputIterator2>
683 pair<_InputIterator1, _InputIterator2>
684 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
685 _InputIterator2 __first2)
687 // concept requirements
688 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
689 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
690 __glibcxx_function_requires(_EqualOpConcept<
691 typename iterator_traits<_InputIterator1>::value_type,
692 typename iterator_traits<_InputIterator2>::value_type>)
693 __glibcxx_requires_valid_range(__first1, __last1);
695 while (__first1 != __last1 && *__first1 == *__first2)
697 ++__first1;
698 ++__first2;
700 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
704 * @brief Finds the places in ranges which don't match.
705 * @param first1 An input iterator.
706 * @param last1 An input iterator.
707 * @param first2 An input iterator.
708 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
709 * @return A pair of iterators pointing to the first mismatch.
711 * This compares the elements of two ranges using the binary_pred
712 * parameter, and returns a pair
713 * of iterators. The first iterator points into the first range, the
714 * second iterator points into the second range, and the elements pointed
715 * to by the iterators are not equal.
717 template<typename _InputIterator1, typename _InputIterator2,
718 typename _BinaryPredicate>
719 pair<_InputIterator1, _InputIterator2>
720 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
721 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
723 // concept requirements
724 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
725 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
726 __glibcxx_requires_valid_range(__first1, __last1);
728 while (__first1 != __last1 && __binary_pred(*__first1, *__first2))
730 ++__first1;
731 ++__first2;
733 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
737 * @brief Tests a range for element-wise equality.
738 * @param first1 An input iterator.
739 * @param last1 An input iterator.
740 * @param first2 An input iterator.
741 * @return A boolean true or false.
743 * This compares the elements of two ranges using @c == and returns true or
744 * false depending on whether all of the corresponding elements of the
745 * ranges are equal.
747 template<typename _InputIterator1, typename _InputIterator2>
748 inline bool
749 equal(_InputIterator1 __first1, _InputIterator1 __last1,
750 _InputIterator2 __first2)
752 // concept requirements
753 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
754 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
755 __glibcxx_function_requires(_EqualOpConcept<
756 typename iterator_traits<_InputIterator1>::value_type,
757 typename iterator_traits<_InputIterator2>::value_type>)
758 __glibcxx_requires_valid_range(__first1, __last1);
760 for (; __first1 != __last1; ++__first1, ++__first2)
761 if (!(*__first1 == *__first2))
762 return false;
763 return true;
767 * @brief Tests a range for element-wise equality.
768 * @param first1 An input iterator.
769 * @param last1 An input iterator.
770 * @param first2 An input iterator.
771 * @param binary_pred A binary predicate @link s20_3_1_base functor@endlink.
772 * @return A boolean true or false.
774 * This compares the elements of two ranges using the binary_pred
775 * parameter, and returns true or
776 * false depending on whether all of the corresponding elements of the
777 * ranges are equal.
779 template<typename _InputIterator1, typename _InputIterator2,
780 typename _BinaryPredicate>
781 inline bool
782 equal(_InputIterator1 __first1, _InputIterator1 __last1,
783 _InputIterator2 __first2,
784 _BinaryPredicate __binary_pred)
786 // concept requirements
787 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
788 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
789 __glibcxx_requires_valid_range(__first1, __last1);
791 for (; __first1 != __last1; ++__first1, ++__first2)
792 if (!__binary_pred(*__first1, *__first2))
793 return false;
794 return true;
798 * @brief Performs "dictionary" comparison on ranges.
799 * @param first1 An input iterator.
800 * @param last1 An input iterator.
801 * @param first2 An input iterator.
802 * @param last2 An input iterator.
803 * @return A boolean true or false.
805 * "Returns true if the sequence of elements defined by the range
806 * [first1,last1) is lexicographically less than the sequence of elements
807 * defined by the range [first2,last2). Returns false otherwise."
808 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
809 * then this is an inline call to @c memcmp.
811 template<typename _InputIterator1, typename _InputIterator2>
812 bool
813 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
814 _InputIterator2 __first2, _InputIterator2 __last2)
816 // concept requirements
817 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
818 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
819 __glibcxx_function_requires(_LessThanOpConcept<
820 typename iterator_traits<_InputIterator1>::value_type,
821 typename iterator_traits<_InputIterator2>::value_type>)
822 __glibcxx_function_requires(_LessThanOpConcept<
823 typename iterator_traits<_InputIterator2>::value_type,
824 typename iterator_traits<_InputIterator1>::value_type>)
825 __glibcxx_requires_valid_range(__first1, __last1);
826 __glibcxx_requires_valid_range(__first2, __last2);
828 for (; __first1 != __last1 && __first2 != __last2;
829 ++__first1, ++__first2)
831 if (*__first1 < *__first2)
832 return true;
833 if (*__first2 < *__first1)
834 return false;
836 return __first1 == __last1 && __first2 != __last2;
840 * @brief Performs "dictionary" comparison on ranges.
841 * @param first1 An input iterator.
842 * @param last1 An input iterator.
843 * @param first2 An input iterator.
844 * @param last2 An input iterator.
845 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
846 * @return A boolean true or false.
848 * The same as the four-parameter @c lexigraphical_compare, but uses the
849 * comp parameter instead of @c <.
851 template<typename _InputIterator1, typename _InputIterator2,
852 typename _Compare>
853 bool
854 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
855 _InputIterator2 __first2, _InputIterator2 __last2,
856 _Compare __comp)
858 // concept requirements
859 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
860 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
861 __glibcxx_requires_valid_range(__first1, __last1);
862 __glibcxx_requires_valid_range(__first2, __last2);
864 for (; __first1 != __last1 && __first2 != __last2;
865 ++__first1, ++__first2)
867 if (__comp(*__first1, *__first2))
868 return true;
869 if (__comp(*__first2, *__first1))
870 return false;
872 return __first1 == __last1 && __first2 != __last2;
875 inline bool
876 lexicographical_compare(const unsigned char* __first1,
877 const unsigned char* __last1,
878 const unsigned char* __first2,
879 const unsigned char* __last2)
881 __glibcxx_requires_valid_range(__first1, __last1);
882 __glibcxx_requires_valid_range(__first2, __last2);
884 const size_t __len1 = __last1 - __first1;
885 const size_t __len2 = __last2 - __first2;
886 const int __result = std::memcmp(__first1, __first2,
887 std::min(__len1, __len2));
888 return __result != 0 ? __result < 0 : __len1 < __len2;
891 inline bool
892 lexicographical_compare(const char* __first1, const char* __last1,
893 const char* __first2, const char* __last2)
895 __glibcxx_requires_valid_range(__first1, __last1);
896 __glibcxx_requires_valid_range(__first2, __last2);
898 #if CHAR_MAX == SCHAR_MAX
899 return std::lexicographical_compare((const signed char*) __first1,
900 (const signed char*) __last1,
901 (const signed char*) __first2,
902 (const signed char*) __last2);
903 #else /* CHAR_MAX == SCHAR_MAX */
904 return std::lexicographical_compare((const unsigned char*) __first1,
905 (const unsigned char*) __last1,
906 (const unsigned char*) __first2,
907 (const unsigned char*) __last2);
908 #endif /* CHAR_MAX == SCHAR_MAX */
911 _GLIBCXX_END_NAMESPACE
913 #endif