OSDN Git Service

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