[multiple changes]
[gcc.git] / libstdc++-v3 / include / bits / stl_algobase.h
1 // Bits and pieces used in algorithms -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003 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.
10
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.
15
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,
19 // USA.
20
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.
29
30 /*
31 *
32 * Copyright (c) 1994
33 * Hewlett-Packard Company
34 *
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.
42 *
43 *
44 * Copyright (c) 1996-1998
45 * Silicon Graphics Computer Systems, Inc.
46 *
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.
54 */
55
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.
59 */
60
61 #ifndef _ALGOBASE_H
62 #define _ALGOBASE_H 1
63
64 #include <bits/c++config.h>
65 #include <cstring>
66 #include <climits>
67 #include <cstdlib>
68 #include <cstddef>
69 #include <new>
70 #include <iosfwd>
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>
78
79 namespace std
80 {
81 /**
82 * @brief Swaps the contents of two iterators.
83 * @param a An iterator.
84 * @param b Another iterator.
85 * @return Nothing.
86 *
87 * This function swaps the values pointed to by two iterators, not the
88 * iterators themselves.
89 */
90 template<typename _ForwardIterator1, typename _ForwardIterator2>
91 inline void
92 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
93 {
94 typedef typename iterator_traits<_ForwardIterator1>::value_type _ValueType1;
95 typedef typename iterator_traits<_ForwardIterator2>::value_type _ValueType2;
96
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>)
102
103 _ValueType1 __tmp = *__a;
104 *__a = *__b;
105 *__b = __tmp;
106 }
107
108 /**
109 * @brief Swaps two values.
110 * @param a A thing of arbitrary type.
111 * @param b Another thing of arbitrary type.
112 * @return Nothing.
113 *
114 * This is the simple classic generic implementation. It will work on
115 * any type which has a copy constructor and an assignment operator.
116 */
117 template<typename _Tp>
118 inline void
119 swap(_Tp& __a, _Tp& __b)
120 {
121 // concept requirements
122 __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
123
124 _Tp __tmp = __a;
125 __a = __b;
126 __b = __tmp;
127 }
128
129 #undef min
130 #undef max
131
132 /**
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.
137 *
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.
141 */
142 template<typename _Tp>
143 inline const _Tp&
144 min(const _Tp& __a, const _Tp& __b)
145 {
146 // concept requirements
147 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
148 //return __b < __a ? __b : __a;
149 if (__b < __a) return __b; return __a;
150 }
151
152 /**
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.
157 *
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.
161 */
162 template<typename _Tp>
163 inline const _Tp&
164 max(const _Tp& __a, const _Tp& __b)
165 {
166 // concept requirements
167 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
168 //return __a < __b ? __b : __a;
169 if (__a < __b) return __b; return __a;
170 }
171
172 /**
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.
178 *
179 * This will work on temporary expressions, since they are only evaluated
180 * once, unlike a preprocessor macro.
181 */
182 template<typename _Tp, typename _Compare>
183 inline const _Tp&
184 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
185 {
186 //return __comp(__b, __a) ? __b : __a;
187 if (__comp(__b, __a)) return __b; return __a;
188 }
189
190 /**
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.
196 *
197 * This will work on temporary expressions, since they are only evaluated
198 * once, unlike a preprocessor macro.
199 */
200 template<typename _Tp, typename _Compare>
201 inline const _Tp&
202 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
203 {
204 //return __comp(__a, __b) ? __b : __a;
205 if (__comp(__a, __b)) return __b; return __a;
206 }
207
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.
213
214 template<typename _InputIterator, typename _OutputIterator>
215 inline _OutputIterator
216 __copy(_InputIterator __first, _InputIterator __last,
217 _OutputIterator __result, input_iterator_tag)
218 {
219 for (; __first != __last; ++__result, ++__first)
220 *__result = *__first;
221 return __result;
222 }
223
224 template<typename _RandomAccessIterator, typename _OutputIterator>
225 inline _OutputIterator
226 __copy(_RandomAccessIterator __first, _RandomAccessIterator __last,
227 _OutputIterator __result, random_access_iterator_tag)
228 {
229 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
230 _Distance;
231 for (_Distance __n = __last - __first; __n > 0; --__n)
232 {
233 *__result = *__first;
234 ++__first;
235 ++__result;
236 }
237 return __result;
238 }
239
240 template<typename _Tp>
241 inline _Tp*
242 __copy_trivial(const _Tp* __first, const _Tp* __last, _Tp* __result)
243 {
244 std::memmove(__result, __first, sizeof(_Tp) * (__last - __first));
245 return __result + (__last - __first);
246 }
247
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)); }
253
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)); }
259
260 template<typename _Tp>
261 inline _Tp*
262 __copy_aux2(_Tp* __first, _Tp* __last, _Tp* __result, __true_type)
263 { return std::__copy_trivial(__first, __last, __result); }
264
265 template<typename _Tp>
266 inline _Tp*
267 __copy_aux2(const _Tp* __first, const _Tp* __last, _Tp* __result,
268 __true_type)
269 { return std::__copy_trivial(__first, __last, __result); }
270
271 template<typename _InputIterator, typename _OutputIterator>
272 inline _OutputIterator
273 __copy_ni2(_InputIterator __first, _InputIterator __last,
274 _OutputIterator __result, __true_type)
275 {
276 typedef typename iterator_traits<_InputIterator>::value_type
277 _ValueType;
278 typedef typename __type_traits<_ValueType>::has_trivial_assignment_operator
279 _Trivial;
280 return _OutputIterator(std::__copy_aux2(__first, __last, __result.base(),
281 _Trivial()));
282 }
283
284 template<typename _InputIterator, typename _OutputIterator>
285 inline _OutputIterator
286 __copy_ni2(_InputIterator __first, _InputIterator __last,
287 _OutputIterator __result, __false_type)
288 {
289 typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
290 typedef typename __type_traits<_ValueType>::has_trivial_assignment_operator
291 _Trivial;
292 return std::__copy_aux2(__first, __last, __result, _Trivial());
293 }
294
295 template<typename _InputIterator, typename _OutputIterator>
296 inline _OutputIterator
297 __copy_ni1(_InputIterator __first, _InputIterator __last,
298 _OutputIterator __result, __true_type)
299 {
300 typedef typename _Is_normal_iterator<_OutputIterator>::_Normal __Normal;
301 return std::__copy_ni2(__first.base(), __last.base(), __result, __Normal());
302 }
303
304 template<typename _InputIterator, typename _OutputIterator>
305 inline _OutputIterator
306 __copy_ni1(_InputIterator __first, _InputIterator __last,
307 _OutputIterator __result, __false_type)
308 {
309 typedef typename _Is_normal_iterator<_OutputIterator>::_Normal __Normal;
310 return std::__copy_ni2(__first, __last, __result, __Normal());
311 }
312
313 /**
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)
319 *
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.
325 *
326 * Note that the end of the output range is permitted to be contained
327 * within [first,last).
328 */
329 template<typename _InputIterator, typename _OutputIterator>
330 inline _OutputIterator
331 copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
332 {
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);
338
339 typedef typename _Is_normal_iterator<_InputIterator>::_Normal __Normal;
340 return std::__copy_ni1(__first, __last, __result, __Normal());
341 }
342
343 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2>
344 inline _BidirectionalIterator2
345 __copy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
346 _BidirectionalIterator2 __result, bidirectional_iterator_tag)
347 {
348 while (__first != __last)
349 *--__result = *--__last;
350 return __result;
351 }
352
353 template<typename _RandomAccessIterator, typename _BidirectionalIterator>
354 inline _BidirectionalIterator
355 __copy_backward(_RandomAccessIterator __first, _RandomAccessIterator __last,
356 _BidirectionalIterator __result, random_access_iterator_tag)
357 {
358 typename iterator_traits<_RandomAccessIterator>::difference_type __n;
359 for (__n = __last - __first; __n > 0; --__n)
360 *--__result = *--__last;
361 return __result;
362 }
363
364
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,
370 typename _BoolType>
371 struct __copy_backward_dispatch
372 {
373 static _BidirectionalIterator2
374 copy(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
375 _BidirectionalIterator2 __result)
376 {
377 return std::__copy_backward(__first, __last, __result,
378 std::__iterator_category(__first));
379 }
380 };
381
382 template<typename _Tp>
383 struct __copy_backward_dispatch<_Tp*, _Tp*, __true_type>
384 {
385 static _Tp*
386 copy(const _Tp* __first, const _Tp* __last, _Tp* __result)
387 {
388 const ptrdiff_t _Num = __last - __first;
389 std::memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
390 return __result - _Num;
391 }
392 };
393
394 template<typename _Tp>
395 struct __copy_backward_dispatch<const _Tp*, _Tp*, __true_type>
396 {
397 static _Tp*
398 copy(const _Tp* __first, const _Tp* __last, _Tp* __result)
399 {
400 return std::__copy_backward_dispatch<_Tp*, _Tp*, __true_type>
401 ::copy(__first, __last, __result);
402 }
403 };
404
405 template<typename _BI1, typename _BI2>
406 inline _BI2
407 __copy_backward_aux(_BI1 __first, _BI1 __last, _BI2 __result)
408 {
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,
412 __last,
413 __result);
414 }
415
416 template <typename _BI1, typename _BI2>
417 inline _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())); }
421
422 template <typename _BI1, typename _BI2>
423 inline _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); }
427
428 template <typename _BI1, typename _BI2>
429 inline _BI2
430 __copy_backward_input_normal_iterator(_BI1 __first, _BI1 __last,
431 _BI2 __result, __true_type)
432 {
433 typedef typename _Is_normal_iterator<_BI2>::_Normal __Normal;
434 return std::__copy_backward_output_normal_iterator(__first.base(),
435 __last.base(), __result,
436 __Normal());
437 }
438
439 template <typename _BI1, typename _BI2>
440 inline _BI2
441 __copy_backward_input_normal_iterator(_BI1 __first, _BI1 __last,
442 _BI2 __result, __false_type)
443 {
444 typedef typename _Is_normal_iterator<_BI2>::_Normal __Normal;
445 return std::__copy_backward_output_normal_iterator(__first, __last, __result,
446 __Normal());
447 }
448
449 /**
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)
455 *
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).
462 *
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).
465 */
466 template <typename _BI1, typename _BI2>
467 inline _BI2
468 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
469 {
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);
477
478 typedef typename _Is_normal_iterator<_BI1>::_Normal __Normal;
479 return std::__copy_backward_input_normal_iterator(__first, __last, __result,
480 __Normal());
481 }
482
483
484 /**
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.
489 * @return Nothing.
490 *
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
493 * @c memset.
494 */
495 template<typename _ForwardIterator, typename _Tp>
496 void
497 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
498 {
499 // concept requirements
500 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator>)
501 __glibcxx_requires_valid_range(__first, __last);
502
503 for ( ; __first != __last; ++__first)
504 *__first = __value;
505 }
506
507 /**
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.
513 *
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
516 * @c memset.
517 */
518 template<typename _OutputIterator, typename _Size, typename _Tp>
519 _OutputIterator
520 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
521 {
522 // concept requirements
523 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,_Tp>)
524
525 for ( ; __n > 0; --__n, ++__first)
526 *__first = __value;
527 return __first;
528 }
529
530 // Specialization: for one-byte types we can use memset.
531 inline void
532 fill(unsigned char* __first, unsigned char* __last, const unsigned char& __c)
533 {
534 __glibcxx_requires_valid_range(__first, __last);
535 unsigned char __tmp = __c;
536 std::memset(__first, __tmp, __last - __first);
537 }
538
539 inline void
540 fill(signed char* __first, signed char* __last, const signed char& __c)
541 {
542 __glibcxx_requires_valid_range(__first, __last);
543 signed char __tmp = __c;
544 std::memset(__first, static_cast<unsigned char>(__tmp), __last - __first);
545 }
546
547 inline void
548 fill(char* __first, char* __last, const char& __c)
549 {
550 __glibcxx_requires_valid_range(__first, __last);
551 char __tmp = __c;
552 std::memset(__first, static_cast<unsigned char>(__tmp), __last - __first);
553 }
554
555 template<typename _Size>
556 inline unsigned char*
557 fill_n(unsigned char* __first, _Size __n, const unsigned char& __c)
558 {
559 std::fill(__first, __first + __n, __c);
560 return __first + __n;
561 }
562
563 template<typename _Size>
564 inline signed char*
565 fill_n(char* __first, _Size __n, const signed char& __c)
566 {
567 std::fill(__first, __first + __n, __c);
568 return __first + __n;
569 }
570
571 template<typename _Size>
572 inline char*
573 fill_n(char* __first, _Size __n, const char& __c)
574 {
575 std::fill(__first, __first + __n, __c);
576 return __first + __n;
577 }
578
579
580 /**
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.
586 *
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.
591 */
592 template<typename _InputIterator1, typename _InputIterator2>
593 pair<_InputIterator1, _InputIterator2>
594 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
595 _InputIterator2 __first2)
596 {
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);
605
606 while (__first1 != __last1 && *__first1 == *__first2)
607 {
608 ++__first1;
609 ++__first2;
610 }
611 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
612 }
613
614 /**
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.
621 *
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.
627 */
628 template<typename _InputIterator1, typename _InputIterator2, typename _BinaryPredicate>
629 pair<_InputIterator1, _InputIterator2>
630 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
631 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
632 {
633 // concept requirements
634 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
635 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
636 __glibcxx_requires_valid_range(__first1, __last1);
637
638 while (__first1 != __last1 && __binary_pred(*__first1, *__first2))
639 {
640 ++__first1;
641 ++__first2;
642 }
643 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
644 }
645
646 /**
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.
652 *
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
655 * ranges are equal.
656 */
657 template<typename _InputIterator1, typename _InputIterator2>
658 inline bool
659 equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
660 {
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);
668
669 for ( ; __first1 != __last1; ++__first1, ++__first2)
670 if (!(*__first1 == *__first2))
671 return false;
672 return true;
673 }
674
675 /**
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.
682 *
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
686 * ranges are equal.
687 */
688 template<typename _InputIterator1, typename _InputIterator2, typename _BinaryPredicate>
689 inline bool
690 equal(_InputIterator1 __first1, _InputIterator1 __last1,
691 _InputIterator2 __first2,
692 _BinaryPredicate __binary_pred)
693 {
694 // concept requirements
695 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
696 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
697 __glibcxx_requires_valid_range(__first1, __last1);
698
699 for ( ; __first1 != __last1; ++__first1, ++__first2)
700 if (!__binary_pred(*__first1, *__first2))
701 return false;
702 return true;
703 }
704
705 /**
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.
712 *
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.
718 */
719 template<typename _InputIterator1, typename _InputIterator2>
720 bool
721 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
722 _InputIterator2 __first2, _InputIterator2 __last2)
723 {
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);
733
734 for (;__first1 != __last1 && __first2 != __last2; ++__first1, ++__first2)
735 {
736 if (*__first1 < *__first2)
737 return true;
738 if (*__first2 < *__first1)
739 return false;
740 }
741 return __first1 == __last1 && __first2 != __last2;
742 }
743
744 /**
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.
752 *
753 * The same as the four-parameter @c lexigraphical_compare, but uses the
754 * comp parameter instead of @c <.
755 */
756 template<typename _InputIterator1, typename _InputIterator2, typename _Compare>
757 bool
758 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
759 _InputIterator2 __first2, _InputIterator2 __last2,
760 _Compare __comp)
761 {
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);
767
768 for ( ; __first1 != __last1 && __first2 != __last2
769 ; ++__first1, ++__first2)
770 {
771 if (__comp(*__first1, *__first2))
772 return true;
773 if (__comp(*__first2, *__first1))
774 return false;
775 }
776 return __first1 == __last1 && __first2 != __last2;
777 }
778
779 inline bool
780 lexicographical_compare(const unsigned char* __first1,
781 const unsigned char* __last1,
782 const unsigned char* __first2,
783 const unsigned char* __last2)
784 {
785 __glibcxx_requires_valid_range(__first1, __last1);
786 __glibcxx_requires_valid_range(__first2, __last2);
787
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;
792 }
793
794 inline bool
795 lexicographical_compare(const char* __first1, const char* __last1,
796 const char* __first2, const char* __last2)
797 {
798 __glibcxx_requires_valid_range(__first1, __last1);
799 __glibcxx_requires_valid_range(__first2, __last2);
800
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 */
812 }
813
814 } // namespace std
815
816 #endif