]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/stl_algobase.h
Add parallel mode.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_algobase.h
1 // Bits and pieces used in algorithms -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 /*
32 *
33 * Copyright (c) 1994
34 * Hewlett-Packard Company
35 *
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
43 *
44 *
45 * Copyright (c) 1996-1998
46 * Silicon Graphics Computer Systems, Inc.
47 *
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
55 */
56
57 /** @file stl_algobase.h
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
60 */
61
62 #ifndef _STL_ALGOBASE_H
63 #define _STL_ALGOBASE_H 1
64
65 #include <bits/c++config.h>
66 #include <cstddef>
67 #include <bits/functexcept.h>
68 #include <bits/cpp_type_traits.h>
69 #include <ext/type_traits.h>
70 #include <ext/numeric_traits.h>
71 #include <bits/algorithmfwd.h>
72 #include <bits/stl_iterator_base_funcs.h>
73 #include <bits/stl_iterator.h>
74 #include <bits/concept_check.h>
75 #include <debug/debug.h>
76
77 _GLIBCXX_BEGIN_NAMESPACE(std)
78
79 /**
80 * @brief Swaps two values.
81 * @param a A thing of arbitrary type.
82 * @param b Another thing of arbitrary type.
83 * @return Nothing.
84 *
85 * This is the simple classic generic implementation. It will work on
86 * any type which has a copy constructor and an assignment operator.
87 */
88 template<typename _Tp>
89 inline void
90 swap(_Tp& __a, _Tp& __b)
91 {
92 // concept requirements
93 __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
94
95 _Tp __tmp = __a;
96 __a = __b;
97 __b = __tmp;
98 }
99
100 // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
101 // nutshell, we are partially implementing the resolution of DR 187,
102 // when it's safe, i.e., the value_types are equal.
103 template<bool _BoolType>
104 struct __iter_swap
105 {
106 template<typename _ForwardIterator1, typename _ForwardIterator2>
107 static void
108 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
109 {
110 typedef typename iterator_traits<_ForwardIterator1>::value_type
111 _ValueType1;
112 _ValueType1 __tmp = *__a;
113 *__a = *__b;
114 *__b = __tmp;
115 }
116 };
117
118 template<>
119 struct __iter_swap<true>
120 {
121 template<typename _ForwardIterator1, typename _ForwardIterator2>
122 static void
123 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
124 {
125 swap(*__a, *__b);
126 }
127 };
128
129 /**
130 * @brief Swaps the contents of two iterators.
131 * @param a An iterator.
132 * @param b Another iterator.
133 * @return Nothing.
134 *
135 * This function swaps the values pointed to by two iterators, not the
136 * iterators themselves.
137 */
138 template<typename _ForwardIterator1, typename _ForwardIterator2>
139 inline void
140 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
141 {
142 typedef typename iterator_traits<_ForwardIterator1>::value_type
143 _ValueType1;
144 typedef typename iterator_traits<_ForwardIterator2>::value_type
145 _ValueType2;
146
147 // concept requirements
148 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
149 _ForwardIterator1>)
150 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
151 _ForwardIterator2>)
152 __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
153 _ValueType2>)
154 __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
155 _ValueType1>)
156
157 typedef typename iterator_traits<_ForwardIterator1>::reference
158 _ReferenceType1;
159 typedef typename iterator_traits<_ForwardIterator2>::reference
160 _ReferenceType2;
161 std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value
162 && __are_same<_ValueType1&, _ReferenceType1>::__value
163 && __are_same<_ValueType2&, _ReferenceType2>::__value>::
164 iter_swap(__a, __b);
165 }
166
167 /**
168 * @brief Swap the elements of two sequences.
169 * @param first1 A forward iterator.
170 * @param last1 A forward iterator.
171 * @param first2 A forward iterator.
172 * @return An iterator equal to @p first2+(last1-first1).
173 *
174 * Swaps each element in the range @p [first1,last1) with the
175 * corresponding element in the range @p [first2,(last1-first1)).
176 * The ranges must not overlap.
177 */
178 template<typename _ForwardIterator1, typename _ForwardIterator2>
179 _ForwardIterator2
180 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
181 _ForwardIterator2 __first2)
182 {
183 // concept requirements
184 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
185 _ForwardIterator1>)
186 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
187 _ForwardIterator2>)
188 __glibcxx_function_requires(_ConvertibleConcept<
189 typename iterator_traits<_ForwardIterator1>::value_type,
190 typename iterator_traits<_ForwardIterator2>::value_type>)
191 __glibcxx_function_requires(_ConvertibleConcept<
192 typename iterator_traits<_ForwardIterator2>::value_type,
193 typename iterator_traits<_ForwardIterator1>::value_type>)
194 __glibcxx_requires_valid_range(__first1, __last1);
195
196 for (; __first1 != __last1; ++__first1, ++__first2)
197 std::iter_swap(__first1, __first2);
198 return __first2;
199 }
200
201 /**
202 * @brief This does what you think it does.
203 * @param a A thing of arbitrary type.
204 * @param b Another thing of arbitrary type.
205 * @return The lesser of the parameters.
206 *
207 * This is the simple classic generic implementation. It will work on
208 * temporary expressions, since they are only evaluated once, unlike a
209 * preprocessor macro.
210 */
211 template<typename _Tp>
212 inline const _Tp&
213 min(const _Tp& __a, const _Tp& __b)
214 {
215 // concept requirements
216 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
217 //return __b < __a ? __b : __a;
218 if (__b < __a)
219 return __b;
220 return __a;
221 }
222
223 /**
224 * @brief This does what you think it does.
225 * @param a A thing of arbitrary type.
226 * @param b Another thing of arbitrary type.
227 * @return The greater of the parameters.
228 *
229 * This is the simple classic generic implementation. It will work on
230 * temporary expressions, since they are only evaluated once, unlike a
231 * preprocessor macro.
232 */
233 template<typename _Tp>
234 inline const _Tp&
235 max(const _Tp& __a, const _Tp& __b)
236 {
237 // concept requirements
238 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
239 //return __a < __b ? __b : __a;
240 if (__a < __b)
241 return __b;
242 return __a;
243 }
244
245 /**
246 * @brief This does what you think it does.
247 * @param a A thing of arbitrary type.
248 * @param b Another thing of arbitrary type.
249 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
250 * @return The lesser of the parameters.
251 *
252 * This will work on temporary expressions, since they are only evaluated
253 * once, unlike a preprocessor macro.
254 */
255 template<typename _Tp, typename _Compare>
256 inline const _Tp&
257 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
258 {
259 //return __comp(__b, __a) ? __b : __a;
260 if (__comp(__b, __a))
261 return __b;
262 return __a;
263 }
264
265 /**
266 * @brief This does what you think it does.
267 * @param a A thing of arbitrary type.
268 * @param b Another thing of arbitrary type.
269 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
270 * @return The greater of the parameters.
271 *
272 * This will work on temporary expressions, since they are only evaluated
273 * once, unlike a preprocessor macro.
274 */
275 template<typename _Tp, typename _Compare>
276 inline const _Tp&
277 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
278 {
279 //return __comp(__a, __b) ? __b : __a;
280 if (__comp(__a, __b))
281 return __b;
282 return __a;
283 }
284
285
286 // If _Iterator is a __normal_iterator return its base (a plain pointer,
287 // normally) otherwise return it untouched. See copy, fill, ...
288 template<typename _Iterator,
289 bool _BoolType = __is_normal_iterator<_Iterator>::__value>
290 struct __niter_base
291 {
292 static const _Iterator&
293 __b(const _Iterator& __it)
294 { return __it; }
295 };
296
297 template<typename _Iterator>
298 struct __niter_base<_Iterator, true>
299 {
300 static const typename _Iterator::_Iterator_type&
301 __b(const _Iterator& __it)
302 { return __it.base(); }
303 };
304
305 // All of these auxiliary structs serve two purposes. (1) Replace
306 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
307 // because the input and output ranges are permitted to overlap.)
308 // (2) If we're using random access iterators, then write the loop as
309 // a for loop with an explicit count.
310
311 template<bool, typename>
312 struct __copy
313 {
314 template<typename _II, typename _OI>
315 static _OI
316 copy(_II __first, _II __last, _OI __result)
317 {
318 for (; __first != __last; ++__result, ++__first)
319 *__result = *__first;
320 return __result;
321 }
322 };
323
324 template<bool _BoolType>
325 struct __copy<_BoolType, random_access_iterator_tag>
326 {
327 template<typename _II, typename _OI>
328 static _OI
329 copy(_II __first, _II __last, _OI __result)
330 {
331 typedef typename iterator_traits<_II>::difference_type _Distance;
332 for(_Distance __n = __last - __first; __n > 0; --__n)
333 {
334 *__result = *__first;
335 ++__first;
336 ++__result;
337 }
338 return __result;
339 }
340 };
341
342 template<>
343 struct __copy<true, random_access_iterator_tag>
344 {
345 template<typename _Tp>
346 static _Tp*
347 copy(const _Tp* __first, const _Tp* __last, _Tp* __result)
348 {
349 __builtin_memmove(__result, __first,
350 sizeof(_Tp) * (__last - __first));
351 return __result + (__last - __first);
352 }
353 };
354
355 template<typename _II, typename _OI>
356 inline _OI
357 __copy_aux(_II __first, _II __last, _OI __result)
358 {
359 typedef typename iterator_traits<_II>::value_type _ValueTypeI;
360 typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
361 typedef typename iterator_traits<_II>::iterator_category _Category;
362 const bool __simple = (__is_pod(_ValueTypeI)
363 && __is_pointer<_II>::__value
364 && __is_pointer<_OI>::__value
365 && __are_same<_ValueTypeI, _ValueTypeO>::__value);
366
367 return std::__copy<__simple, _Category>::copy(__first, __last, __result);
368 }
369
370 // Helpers for streambuf iterators (either istream or ostream).
371 // NB: avoid including <iosfwd>, relatively large.
372 template<typename _CharT>
373 struct char_traits;
374
375 template<typename _CharT, typename _Traits>
376 class istreambuf_iterator;
377
378 template<typename _CharT, typename _Traits>
379 class ostreambuf_iterator;
380
381 template<typename _CharT>
382 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
383 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
384 __copy_aux(_CharT*, _CharT*,
385 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
386
387 template<typename _CharT>
388 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
389 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
390 __copy_aux(const _CharT*, const _CharT*,
391 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
392
393 template<typename _CharT>
394 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
395 _CharT*>::__type
396 __copy_aux(istreambuf_iterator<_CharT, char_traits<_CharT> >,
397 istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
398
399 /**
400 * @brief Copies the range [first,last) into result.
401 * @param first An input iterator.
402 * @param last An input iterator.
403 * @param result An output iterator.
404 * @return result + (first - last)
405 *
406 * This inline function will boil down to a call to @c memmove whenever
407 * possible. Failing that, if random access iterators are passed, then the
408 * loop count will be known (and therefore a candidate for compiler
409 * optimizations such as unrolling). Result may not be contained within
410 * [first,last); the copy_backward function should be used instead.
411 *
412 * Note that the end of the output range is permitted to be contained
413 * within [first,last).
414 */
415 template<typename _II, typename _OI>
416 inline _OI
417 copy(_II __first, _II __last, _OI __result)
418 {
419 // concept requirements
420 __glibcxx_function_requires(_InputIteratorConcept<_II>)
421 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
422 typename iterator_traits<_II>::value_type>)
423 __glibcxx_requires_valid_range(__first, __last);
424
425 return _OI(std::__copy_aux(__niter_base<_II>::__b(__first),
426 __niter_base<_II>::__b(__last),
427 __niter_base<_OI>::__b(__result)));
428 }
429
430
431 template<bool, typename>
432 struct __copy_backward
433 {
434 template<typename _BI1, typename _BI2>
435 static _BI2
436 __copy_b(_BI1 __first, _BI1 __last, _BI2 __result)
437 {
438 while (__first != __last)
439 *--__result = *--__last;
440 return __result;
441 }
442 };
443
444 template<bool _BoolType>
445 struct __copy_backward<_BoolType, random_access_iterator_tag>
446 {
447 template<typename _BI1, typename _BI2>
448 static _BI2
449 __copy_b(_BI1 __first, _BI1 __last, _BI2 __result)
450 {
451 typename iterator_traits<_BI1>::difference_type __n;
452 for (__n = __last - __first; __n > 0; --__n)
453 *--__result = *--__last;
454 return __result;
455 }
456 };
457
458 template<>
459 struct __copy_backward<true, random_access_iterator_tag>
460 {
461 template<typename _Tp>
462 static _Tp*
463 __copy_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
464 {
465 const ptrdiff_t _Num = __last - __first;
466 __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
467 return __result - _Num;
468 }
469 };
470
471 template<typename _BI1, typename _BI2>
472 inline _BI2
473 __copy_backward_aux(_BI1 __first, _BI1 __last, _BI2 __result)
474 {
475 typedef typename iterator_traits<_BI1>::value_type _ValueType1;
476 typedef typename iterator_traits<_BI2>::value_type _ValueType2;
477 typedef typename iterator_traits<_BI1>::iterator_category _Category;
478 const bool __simple = (__is_pod(_ValueType1)
479 && __is_pointer<_BI1>::__value
480 && __is_pointer<_BI2>::__value
481 && __are_same<_ValueType1, _ValueType2>::__value);
482
483 return std::__copy_backward<__simple, _Category>::__copy_b(__first,
484 __last,
485 __result);
486 }
487
488 /**
489 * @brief Copies the range [first,last) into result.
490 * @param first A bidirectional iterator.
491 * @param last A bidirectional iterator.
492 * @param result A bidirectional iterator.
493 * @return result - (first - last)
494 *
495 * The function has the same effect as copy, but starts at the end of the
496 * range and works its way to the start, returning the start of the result.
497 * This inline function will boil down to a call to @c memmove whenever
498 * possible. Failing that, if random access iterators are passed, then the
499 * loop count will be known (and therefore a candidate for compiler
500 * optimizations such as unrolling).
501 *
502 * Result may not be in the range [first,last). Use copy instead. Note
503 * that the start of the output range may overlap [first,last).
504 */
505 template <typename _BI1, typename _BI2>
506 inline _BI2
507 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
508 {
509 // concept requirements
510 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
511 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
512 __glibcxx_function_requires(_ConvertibleConcept<
513 typename iterator_traits<_BI1>::value_type,
514 typename iterator_traits<_BI2>::value_type>)
515 __glibcxx_requires_valid_range(__first, __last);
516
517 return _BI2(std::__copy_backward_aux(__niter_base<_BI1>::__b(__first),
518 __niter_base<_BI1>::__b(__last),
519 __niter_base<_BI2>::__b(__result)));
520 }
521
522
523 template<bool>
524 struct __fill
525 {
526 template<typename _ForwardIterator, typename _Tp>
527 static void
528 fill(_ForwardIterator __first, _ForwardIterator __last,
529 const _Tp& __value)
530 {
531 for (; __first != __last; ++__first)
532 *__first = __value;
533 }
534 };
535
536 template<>
537 struct __fill<true>
538 {
539 template<typename _ForwardIterator, typename _Tp>
540 static void
541 fill(_ForwardIterator __first, _ForwardIterator __last,
542 const _Tp& __value)
543 {
544 const _Tp __tmp = __value;
545 for (; __first != __last; ++__first)
546 *__first = __tmp;
547 }
548 };
549
550 template<typename _ForwardIterator, typename _Tp>
551 inline void
552 __fill_aux(_ForwardIterator __first, _ForwardIterator __last,
553 const _Tp& __value)
554 {
555 const bool __scalar = __is_scalar<_Tp>::__value;
556 std::__fill<__scalar>::fill(__first, __last, __value);
557 }
558
559 // Specialization: for char types we can use memset.
560 inline void
561 __fill_aux(unsigned char* __first, unsigned char* __last, unsigned char __c)
562 { __builtin_memset(__first, __c, __last - __first); }
563
564 inline void
565 __fill_aux(signed char* __first, signed char* __last, signed char __c)
566 { __builtin_memset(__first, static_cast<unsigned char>(__c),
567 __last - __first); }
568
569 inline void
570 __fill_aux(char* __first, char* __last, char __c)
571 { __builtin_memset(__first, static_cast<unsigned char>(__c),
572 __last - __first); }
573
574 /**
575 * @brief Fills the range [first,last) with copies of value.
576 * @param first A forward iterator.
577 * @param last A forward iterator.
578 * @param value A reference-to-const of arbitrary type.
579 * @return Nothing.
580 *
581 * This function fills a range with copies of the same value. For char
582 * types filling contiguous areas of memory, this becomes an inline call
583 * to @c memset or @c wmemset.
584 */
585 template<typename _ForwardIterator, typename _Tp>
586 inline void
587 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
588 {
589 // concept requirements
590 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
591 _ForwardIterator>)
592 __glibcxx_requires_valid_range(__first, __last);
593
594 std::__fill_aux(__niter_base<_ForwardIterator>::__b(__first),
595 __niter_base<_ForwardIterator>::__b(__last), __value);
596 }
597
598 template<bool>
599 struct __fill_n
600 {
601 template<typename _OutputIterator, typename _Size, typename _Tp>
602 static _OutputIterator
603 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
604 {
605 for (; __n > 0; --__n, ++__first)
606 *__first = __value;
607 return __first;
608 }
609 };
610
611 template<>
612 struct __fill_n<true>
613 {
614 template<typename _OutputIterator, typename _Size, typename _Tp>
615 static _OutputIterator
616 fill_n(_OutputIterator __first, _Size __n, const _Tp& __value)
617 {
618 const _Tp __tmp = __value;
619 for (; __n > 0; --__n, ++__first)
620 *__first = __tmp;
621 return __first;
622 }
623 };
624
625 template<typename _OutputIterator, typename _Size, typename _Tp>
626 inline _OutputIterator
627 __fill_n_aux(_OutputIterator __first, _Size __n, const _Tp& __value)
628 {
629 const bool __scalar = __is_scalar<_Tp>::__value;
630 return std::__fill_n<__scalar>::fill_n(__first, __n, __value);
631 }
632
633 template<typename _Size>
634 inline unsigned char*
635 __fill_n_aux(unsigned char* __first, _Size __n, unsigned char __c)
636 {
637 std::__fill_aux(__first, __first + __n, __c);
638 return __first + __n;
639 }
640
641 template<typename _Size>
642 inline signed char*
643 __fill_n_aux(signed char* __first, _Size __n, signed char __c)
644 {
645 std::__fill_aux(__first, __first + __n, __c);
646 return __first + __n;
647 }
648
649 template<typename _Size>
650 inline char*
651 __fill_n_aux(char* __first, _Size __n, char __c)
652 {
653 std::__fill_aux(__first, __first + __n, __c);
654 return __first + __n;
655 }
656
657 /**
658 * @brief Fills the range [first,first+n) with copies of value.
659 * @param first An output iterator.
660 * @param n The count of copies to perform.
661 * @param value A reference-to-const of arbitrary type.
662 * @return The iterator at first+n.
663 *
664 * This function fills a range with copies of the same value. For char
665 * types filling contiguous areas of memory, this becomes an inline call
666 * to @c memset or @ wmemset.
667 */
668 template<typename _OI, typename _Size, typename _Tp>
669 inline _OI
670 fill_n(_OI __first, _Size __n, const _Tp& __value)
671 {
672 // concept requirements
673 __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>)
674
675 return _OI(std::__fill_n_aux(__niter_base<_OI>::__b(__first), __n,
676 __value));
677 }
678
679 template<bool _BoolType>
680 struct __equal
681 {
682 template<typename _II1, typename _II2>
683 static bool
684 equal(_II1 __first1, _II1 __last1, _II2 __first2)
685 {
686 for (; __first1 != __last1; ++__first1, ++__first2)
687 if (!(*__first1 == *__first2))
688 return false;
689 return true;
690 }
691 };
692
693 template<>
694 struct __equal<true>
695 {
696 template<typename _Tp>
697 static bool
698 equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
699 {
700 return !__builtin_memcmp(__first1, __first2, sizeof(_Tp)
701 * (__last1 - __first1));
702 }
703 };
704
705 template<typename _II1, typename _II2>
706 inline bool
707 __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
708 {
709 typedef typename iterator_traits<_II1>::value_type _ValueType1;
710 typedef typename iterator_traits<_II2>::value_type _ValueType2;
711 const bool __simple = (__is_integer<_ValueType1>::__value
712 && __is_pointer<_II1>::__value
713 && __is_pointer<_II2>::__value
714 && __are_same<_ValueType1, _ValueType2>::__value);
715
716 return std::__equal<__simple>::equal(__first1, __last1, __first2);
717 }
718
719
720 template<typename, typename>
721 struct __lc_rai
722 {
723 template<typename _II1, typename _II2>
724 static _II1
725 __newlast1(_II1, _II1 __last1, _II2, _II2)
726 { return __last1; }
727
728 template<typename _II>
729 static bool
730 __cnd2(_II __first, _II __last)
731 { return __first != __last; }
732 };
733
734 template<>
735 struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag>
736 {
737 template<typename _RAI1, typename _RAI2>
738 static _RAI1
739 __newlast1(_RAI1 __first1, _RAI1 __last1,
740 _RAI2 __first2, _RAI2 __last2)
741 {
742 const typename iterator_traits<_RAI1>::difference_type
743 __diff1 = __last1 - __first1;
744 const typename iterator_traits<_RAI2>::difference_type
745 __diff2 = __last2 - __first2;
746 return __diff2 < __diff1 ? __first1 + __diff2 : __last1;
747 }
748
749 template<typename _RAI>
750 static bool
751 __cnd2(_RAI, _RAI)
752 { return true; }
753 };
754
755 // XXX should these be enabled-if'd for signed/unsigned types instead?
756 inline bool
757 lexicographical_compare(const unsigned char* __first1,
758 const unsigned char* __last1,
759 const unsigned char* __first2,
760 const unsigned char* __last2)
761 {
762 __glibcxx_requires_valid_range(__first1, __last1);
763 __glibcxx_requires_valid_range(__first2, __last2);
764
765 const size_t __len1 = __last1 - __first1;
766 const size_t __len2 = __last2 - __first2;
767 const int __result = __builtin_memcmp(__first1, __first2,
768 std::min(__len1, __len2));
769 return __result != 0 ? __result < 0 : __len1 < __len2;
770 }
771
772 inline bool
773 lexicographical_compare(const char* __first1, const char* __last1,
774 const char* __first2, const char* __last2)
775 {
776 __glibcxx_requires_valid_range(__first1, __last1);
777 __glibcxx_requires_valid_range(__first2, __last2);
778
779 if (__gnu_cxx::__numeric_traits<char>::__is_signed)
780 {
781 typedef const signed char* value_type;
782 value_type __f1 = reinterpret_cast<value_type>(__first1);
783 value_type __l1 = reinterpret_cast<value_type>(__last1);
784 value_type __f2 = reinterpret_cast<value_type>(__first2);
785 value_type __l2 = reinterpret_cast<value_type>(__last2);
786 return _GLIBCXX_STD_P::lexicographical_compare(__f1, __l1, __f2, __l2);
787 }
788 else
789 {
790 typedef const unsigned char* value_type;
791 value_type __f1 = reinterpret_cast<value_type>(__first1);
792 value_type __l1 = reinterpret_cast<value_type>(__last1);
793 value_type __f2 = reinterpret_cast<value_type>(__first2);
794 value_type __l2 = reinterpret_cast<value_type>(__last2);
795 return _GLIBCXX_STD_P::lexicographical_compare(__f1, __l1, __f2, __l2);
796 }
797 }
798
799 _GLIBCXX_END_NAMESPACE
800
801 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
802
803 /**
804 * @brief Tests a range for element-wise equality.
805 * @param first1 An input iterator.
806 * @param last1 An input iterator.
807 * @param first2 An input iterator.
808 * @return A boolean true or false.
809 *
810 * This compares the elements of two ranges using @c == and returns true or
811 * false depending on whether all of the corresponding elements of the
812 * ranges are equal.
813 */
814 template<typename _II1, typename _II2>
815 inline bool
816 equal(_II1 __first1, _II1 __last1, _II2 __first2)
817 {
818 // concept requirements
819 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
820 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
821 __glibcxx_function_requires(_EqualOpConcept<
822 typename iterator_traits<_II1>::value_type,
823 typename iterator_traits<_II2>::value_type>)
824 __glibcxx_requires_valid_range(__first1, __last1);
825
826 return std::__equal_aux(__niter_base<_II1>::__b(__first1),
827 __niter_base<_II1>::__b(__last1),
828 __niter_base<_II2>::__b(__first2));
829 }
830
831 /**
832 * @brief Tests a range for element-wise equality.
833 * @param first1 An input iterator.
834 * @param last1 An input iterator.
835 * @param first2 An input iterator.
836 * @param binary_pred A binary predicate @link s20_3_1_base
837 * functor@endlink.
838 * @return A boolean true or false.
839 *
840 * This compares the elements of two ranges using the binary_pred
841 * parameter, and returns true or
842 * false depending on whether all of the corresponding elements of the
843 * ranges are equal.
844 */
845 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
846 inline bool
847 equal(_IIter1 __first1, _IIter1 __last1,
848 _IIter2 __first2, _BinaryPredicate __binary_pred)
849 {
850 // concept requirements
851 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
852 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
853 __glibcxx_requires_valid_range(__first1, __last1);
854
855 for (; __first1 != __last1; ++__first1, ++__first2)
856 if (!bool(__binary_pred(*__first1, *__first2)))
857 return false;
858 return true;
859 }
860
861 /**
862 * @brief Performs "dictionary" comparison on ranges.
863 * @param first1 An input iterator.
864 * @param last1 An input iterator.
865 * @param first2 An input iterator.
866 * @param last2 An input iterator.
867 * @return A boolean true or false.
868 *
869 * "Returns true if the sequence of elements defined by the range
870 * [first1,last1) is lexicographically less than the sequence of elements
871 * defined by the range [first2,last2). Returns false otherwise."
872 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
873 * then this is an inline call to @c memcmp.
874 */
875 template<typename _II1, typename _II2>
876 bool
877 lexicographical_compare(_II1 __first1, _II1 __last1,
878 _II2 __first2, _II2 __last2)
879 {
880 typedef typename iterator_traits<_II1>::iterator_category _Category1;
881 typedef typename iterator_traits<_II2>::iterator_category _Category2;
882
883 // concept requirements
884 typedef typename iterator_traits<_II1>::value_type _ValueType1;
885 typedef typename iterator_traits<_II2>::value_type _ValueType2;
886 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
887 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
888 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
889 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
890 __glibcxx_requires_valid_range(__first1, __last1);
891 __glibcxx_requires_valid_range(__first2, __last2);
892
893 __last1 = __lc_rai<_Category1, _Category2>::__newlast1(__first1,
894 __last1,
895 __first2,
896 __last2);
897 for (; __first1 != __last1
898 && __lc_rai<_Category1, _Category2>::__cnd2(__first2, __last2);
899 ++__first1, ++__first2)
900 {
901 if (*__first1 < *__first2)
902 return true;
903 if (*__first2 < *__first1)
904 return false;
905 }
906 return __first1 == __last1 && __first2 != __last2;
907 }
908
909 /**
910 * @brief Performs "dictionary" comparison on ranges.
911 * @param first1 An input iterator.
912 * @param last1 An input iterator.
913 * @param first2 An input iterator.
914 * @param last2 An input iterator.
915 * @param comp A @link s20_3_3_comparisons comparison functor@endlink.
916 * @return A boolean true or false.
917 *
918 * The same as the four-parameter @c lexicographical_compare, but uses the
919 * comp parameter instead of @c <.
920 */
921 template<typename _II1, typename _II2, typename _Compare>
922 bool
923 lexicographical_compare(_II1 __first1, _II1 __last1,
924 _II2 __first2, _II2 __last2, _Compare __comp)
925 {
926 typedef typename iterator_traits<_II1>::iterator_category _Category1;
927 typedef typename iterator_traits<_II2>::iterator_category _Category2;
928 typedef __lc_rai<_Category1, _Category2> __rai_type;
929
930 // concept requirements
931 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
932 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
933 __glibcxx_requires_valid_range(__first1, __last1);
934 __glibcxx_requires_valid_range(__first2, __last2);
935
936 __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
937 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
938 ++__first1, ++__first2)
939 {
940 if (__comp(*__first1, *__first2))
941 return true;
942 if (__comp(*__first2, *__first1))
943 return false;
944 }
945 return __first1 == __last1 && __first2 != __last2;
946 }
947
948
949 /**
950 * @brief Finds the places in ranges which don't match.
951 * @param first1 An input iterator.
952 * @param last1 An input iterator.
953 * @param first2 An input iterator.
954 * @return A pair of iterators pointing to the first mismatch.
955 *
956 * This compares the elements of two ranges using @c == and returns a pair
957 * of iterators. The first iterator points into the first range, the
958 * second iterator points into the second range, and the elements pointed
959 * to by the iterators are not equal.
960 */
961 template<typename _InputIterator1, typename _InputIterator2>
962 pair<_InputIterator1, _InputIterator2>
963 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
964 _InputIterator2 __first2)
965 {
966 // concept requirements
967 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
968 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
969 __glibcxx_function_requires(_EqualOpConcept<
970 typename iterator_traits<_InputIterator1>::value_type,
971 typename iterator_traits<_InputIterator2>::value_type>)
972 __glibcxx_requires_valid_range(__first1, __last1);
973
974 while (__first1 != __last1 && *__first1 == *__first2)
975 {
976 ++__first1;
977 ++__first2;
978 }
979 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
980 }
981
982 /**
983 * @brief Finds the places in ranges which don't match.
984 * @param first1 An input iterator.
985 * @param last1 An input iterator.
986 * @param first2 An input iterator.
987 * @param binary_pred A binary predicate @link s20_3_1_base
988 * functor@endlink.
989 * @return A pair of iterators pointing to the first mismatch.
990 *
991 * This compares the elements of two ranges using the binary_pred
992 * parameter, and returns a pair
993 * of iterators. The first iterator points into the first range, the
994 * second iterator points into the second range, and the elements pointed
995 * to by the iterators are not equal.
996 */
997 template<typename _InputIterator1, typename _InputIterator2,
998 typename _BinaryPredicate>
999 pair<_InputIterator1, _InputIterator2>
1000 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1001 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1002 {
1003 // concept requirements
1004 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1005 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1006 __glibcxx_requires_valid_range(__first1, __last1);
1007
1008 while (__first1 != __last1 && bool(__binary_pred(*__first1, *__first2)))
1009 {
1010 ++__first1;
1011 ++__first2;
1012 }
1013 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1014 }
1015
1016 _GLIBCXX_END_NESTED_NAMESPACE
1017
1018 // NB: This file is included within many other C++ includes, as a way
1019 // of getting the base algorithms. So, make sure that parallel bits
1020 // come in too if requested.
1021 #ifdef _GLIBCXX_PARALLEL
1022 //# include <parallel/algorithm>
1023 # include <parallel/algobase.h>
1024 #endif
1025
1026 #endif