OSDN Git Service

2009-11-06 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
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 is a __normal_iterator return its base (a plain pointer,
263   // normally) otherwise return it untouched.  See copy, fill, ... 
264   template<typename _Iterator,
265            bool _IsNormal = __is_normal_iterator<_Iterator>::__value>
266     struct __niter_base
267     {
268       static _Iterator
269       __b(_Iterator __it)
270       { return __it; }
271     };
272
273   template<typename _Iterator>
274     struct __niter_base<_Iterator, true>
275     {
276       static typename _Iterator::iterator_type
277       __b(_Iterator __it)
278       { return __it.base(); }
279     };
280
281   // Likewise, for move_iterator.
282   template<typename _Iterator,
283            bool _IsMove = __is_move_iterator<_Iterator>::__value>
284     struct __miter_base
285     {
286       static _Iterator
287       __b(_Iterator __it)
288       { return __it; }
289     };
290
291   template<typename _Iterator>
292     struct __miter_base<_Iterator, true>
293     {
294       static typename _Iterator::iterator_type
295       __b(_Iterator __it)
296       { return __it.base(); }
297     };
298
299   // All of these auxiliary structs serve two purposes.  (1) Replace
300   // calls to copy with memmove whenever possible.  (Memmove, not memcpy,
301   // because the input and output ranges are permitted to overlap.)
302   // (2) If we're using random access iterators, then write the loop as
303   // a for loop with an explicit count.
304
305   template<bool, bool, typename>
306     struct __copy_move
307     {
308       template<typename _II, typename _OI>
309         static _OI
310         __copy_m(_II __first, _II __last, _OI __result)
311         {
312           for (; __first != __last; ++__result, ++__first)
313             *__result = *__first;
314           return __result;
315         }
316     };
317
318 #ifdef __GXX_EXPERIMENTAL_CXX0X__
319   template<typename _Category>
320     struct __copy_move<true, false, _Category>
321     {
322       template<typename _II, typename _OI>
323         static _OI
324         __copy_m(_II __first, _II __last, _OI __result)
325         {
326           for (; __first != __last; ++__result, ++__first)
327             *__result = std::move(*__first);
328           return __result;
329         }
330     };
331 #endif
332
333   template<>
334     struct __copy_move<false, false, random_access_iterator_tag>
335     {
336       template<typename _II, typename _OI>
337         static _OI
338         __copy_m(_II __first, _II __last, _OI __result)
339         { 
340           typedef typename iterator_traits<_II>::difference_type _Distance;
341           for(_Distance __n = __last - __first; __n > 0; --__n)
342             {
343               *__result = *__first;
344               ++__first;
345               ++__result;
346             }
347           return __result;
348         }
349     };
350
351 #ifdef __GXX_EXPERIMENTAL_CXX0X__
352   template<>
353     struct __copy_move<true, false, random_access_iterator_tag>
354     {
355       template<typename _II, typename _OI>
356         static _OI
357         __copy_m(_II __first, _II __last, _OI __result)
358         { 
359           typedef typename iterator_traits<_II>::difference_type _Distance;
360           for(_Distance __n = __last - __first; __n > 0; --__n)
361             {
362               *__result = std::move(*__first);
363               ++__first;
364               ++__result;
365             }
366           return __result;
367         }
368     };
369 #endif
370
371   template<bool _IsMove>
372     struct __copy_move<_IsMove, true, random_access_iterator_tag>
373     {
374       template<typename _Tp>
375         static _Tp*
376         __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
377         {
378           const ptrdiff_t _Num = __last - __first;
379           if (_Num)
380             __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
381           return __result + _Num;
382         }
383     };
384
385   template<bool _IsMove, typename _II, typename _OI>
386     inline _OI
387     __copy_move_a(_II __first, _II __last, _OI __result)
388     {
389       typedef typename iterator_traits<_II>::value_type _ValueTypeI;
390       typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
391       typedef typename iterator_traits<_II>::iterator_category _Category;
392       const bool __simple = (__is_pod(_ValueTypeI)
393                              && __is_pointer<_II>::__value
394                              && __is_pointer<_OI>::__value
395                              && __are_same<_ValueTypeI, _ValueTypeO>::__value);
396
397       return std::__copy_move<_IsMove, __simple,
398                               _Category>::__copy_m(__first, __last, __result);
399     }
400
401   // Helpers for streambuf iterators (either istream or ostream).
402   // NB: avoid including <iosfwd>, relatively large.
403   template<typename _CharT>
404     struct char_traits;
405
406   template<typename _CharT, typename _Traits>
407     class istreambuf_iterator;
408
409   template<typename _CharT, typename _Traits>
410     class ostreambuf_iterator;
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(_CharT*, _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              ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
421     __copy_move_a2(const _CharT*, const _CharT*,
422                    ostreambuf_iterator<_CharT, char_traits<_CharT> >);
423
424   template<bool _IsMove, typename _CharT>
425     typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
426                                     _CharT*>::__type
427     __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >,
428                    istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
429
430   template<bool _IsMove, typename _II, typename _OI>
431     inline _OI
432     __copy_move_a2(_II __first, _II __last, _OI __result)
433     {
434       return _OI(std::__copy_move_a<_IsMove>
435                  (std::__niter_base<_II>::__b(__first),
436                   std::__niter_base<_II>::__b(__last),
437                   std::__niter_base<_OI>::__b(__result)));
438     }
439
440   /**
441    *  @brief Copies the range [first,last) into result.
442    *  @ingroup mutating_algorithms
443    *  @param  first  An input iterator.
444    *  @param  last   An input iterator.
445    *  @param  result An output iterator.
446    *  @return   result + (first - last)
447    *
448    *  This inline function will boil down to a call to @c memmove whenever
449    *  possible.  Failing that, if random access iterators are passed, then the
450    *  loop count will be known (and therefore a candidate for compiler
451    *  optimizations such as unrolling).  Result may not be contained within
452    *  [first,last); the copy_backward function should be used instead.
453    *
454    *  Note that the end of the output range is permitted to be contained
455    *  within [first,last).
456   */
457   template<typename _II, typename _OI>
458     inline _OI
459     copy(_II __first, _II __last, _OI __result)
460     {
461       // concept requirements
462       __glibcxx_function_requires(_InputIteratorConcept<_II>)
463       __glibcxx_function_requires(_OutputIteratorConcept<_OI,
464             typename iterator_traits<_II>::value_type>)
465       __glibcxx_requires_valid_range(__first, __last);
466
467       return (std::__copy_move_a2<__is_move_iterator<_II>::__value>
468               (std::__miter_base<_II>::__b(__first),
469                std::__miter_base<_II>::__b(__last), __result));
470     }
471
472 #ifdef __GXX_EXPERIMENTAL_CXX0X__
473   /**
474    *  @brief Moves the range [first,last) into result.
475    *  @ingroup mutating_algorithms
476    *  @param  first  An input iterator.
477    *  @param  last   An input iterator.
478    *  @param  result An output iterator.
479    *  @return   result + (first - last)
480    *
481    *  This inline function will boil down to a call to @c memmove whenever
482    *  possible.  Failing that, if random access iterators are passed, then the
483    *  loop count will be known (and therefore a candidate for compiler
484    *  optimizations such as unrolling).  Result may not be contained within
485    *  [first,last); the move_backward function should be used instead.
486    *
487    *  Note that the end of the output range is permitted to be contained
488    *  within [first,last).
489   */
490   template<typename _II, typename _OI>
491     inline _OI
492     move(_II __first, _II __last, _OI __result)
493     {
494       // concept requirements
495       __glibcxx_function_requires(_InputIteratorConcept<_II>)
496       __glibcxx_function_requires(_OutputIteratorConcept<_OI,
497             typename iterator_traits<_II>::value_type>)
498       __glibcxx_requires_valid_range(__first, __last);
499
500       return (std::__copy_move_a2<true>
501               (std::__miter_base<_II>::__b(__first),
502                std::__miter_base<_II>::__b(__last), __result));
503     }
504
505 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
506 #else
507 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
508 #endif
509
510   template<bool, bool, typename>
511     struct __copy_move_backward
512     {
513       template<typename _BI1, typename _BI2>
514         static _BI2
515         __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
516         {
517           while (__first != __last)
518             *--__result = *--__last;
519           return __result;
520         }
521     };
522
523 #ifdef __GXX_EXPERIMENTAL_CXX0X__
524   template<typename _Category>
525     struct __copy_move_backward<true, false, _Category>
526     {
527       template<typename _BI1, typename _BI2>
528         static _BI2
529         __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
530         {
531           while (__first != __last)
532             *--__result = std::move(*--__last);
533           return __result;
534         }
535     };
536 #endif
537
538   template<>
539     struct __copy_move_backward<false, false, random_access_iterator_tag>
540     {
541       template<typename _BI1, typename _BI2>
542         static _BI2
543         __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
544         {
545           typename iterator_traits<_BI1>::difference_type __n;
546           for (__n = __last - __first; __n > 0; --__n)
547             *--__result = *--__last;
548           return __result;
549         }
550     };
551
552 #ifdef __GXX_EXPERIMENTAL_CXX0X__
553   template<>
554     struct __copy_move_backward<true, false, random_access_iterator_tag>
555     {
556       template<typename _BI1, typename _BI2>
557         static _BI2
558         __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
559         {
560           typename iterator_traits<_BI1>::difference_type __n;
561           for (__n = __last - __first; __n > 0; --__n)
562             *--__result = std::move(*--__last);
563           return __result;
564         }
565     };
566 #endif
567
568   template<bool _IsMove>
569     struct __copy_move_backward<_IsMove, true, random_access_iterator_tag>
570     {
571       template<typename _Tp>
572         static _Tp*
573         __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
574         {
575           const ptrdiff_t _Num = __last - __first;
576           if (_Num)
577             __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
578           return __result - _Num;
579         }
580     };
581
582   template<bool _IsMove, typename _BI1, typename _BI2>
583     inline _BI2
584     __copy_move_backward_a(_BI1 __first, _BI1 __last, _BI2 __result)
585     {
586       typedef typename iterator_traits<_BI1>::value_type _ValueType1;
587       typedef typename iterator_traits<_BI2>::value_type _ValueType2;
588       typedef typename iterator_traits<_BI1>::iterator_category _Category;
589       const bool __simple = (__is_pod(_ValueType1)
590                              && __is_pointer<_BI1>::__value
591                              && __is_pointer<_BI2>::__value
592                              && __are_same<_ValueType1, _ValueType2>::__value);
593
594       return std::__copy_move_backward<_IsMove, __simple,
595                                        _Category>::__copy_move_b(__first,
596                                                                  __last,
597                                                                  __result);
598     }
599
600   template<bool _IsMove, typename _BI1, typename _BI2>
601     inline _BI2
602     __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result)
603     {
604       return _BI2(std::__copy_move_backward_a<_IsMove>
605                   (std::__niter_base<_BI1>::__b(__first),
606                    std::__niter_base<_BI1>::__b(__last),
607                    std::__niter_base<_BI2>::__b(__result)));
608     }
609
610   /**
611    *  @brief Copies the range [first,last) into result.
612    *  @ingroup mutating_algorithms
613    *  @param  first  A bidirectional iterator.
614    *  @param  last   A bidirectional iterator.
615    *  @param  result A bidirectional iterator.
616    *  @return   result - (first - last)
617    *
618    *  The function has the same effect as copy, but starts at the end of the
619    *  range and works its way to the start, returning the start of the result.
620    *  This inline function will boil down to a call to @c memmove whenever
621    *  possible.  Failing that, if random access iterators are passed, then the
622    *  loop count will be known (and therefore a candidate for compiler
623    *  optimizations such as unrolling).
624    *
625    *  Result may not be in the range [first,last).  Use copy instead.  Note
626    *  that the start of the output range may overlap [first,last).
627   */
628   template<typename _BI1, typename _BI2>
629     inline _BI2
630     copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
631     {
632       // concept requirements
633       __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
634       __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
635       __glibcxx_function_requires(_ConvertibleConcept<
636             typename iterator_traits<_BI1>::value_type,
637             typename iterator_traits<_BI2>::value_type>)
638       __glibcxx_requires_valid_range(__first, __last);
639
640       return (std::__copy_move_backward_a2<__is_move_iterator<_BI1>::__value>
641               (std::__miter_base<_BI1>::__b(__first),
642                std::__miter_base<_BI1>::__b(__last), __result));
643     }
644
645 #ifdef __GXX_EXPERIMENTAL_CXX0X__
646   /**
647    *  @brief Moves the range [first,last) into result.
648    *  @ingroup mutating_algorithms
649    *  @param  first  A bidirectional iterator.
650    *  @param  last   A bidirectional iterator.
651    *  @param  result A bidirectional iterator.
652    *  @return   result - (first - last)
653    *
654    *  The function has the same effect as move, but starts at the end of the
655    *  range and works its way to the start, returning the start of the result.
656    *  This inline function will boil down to a call to @c memmove whenever
657    *  possible.  Failing that, if random access iterators are passed, then the
658    *  loop count will be known (and therefore a candidate for compiler
659    *  optimizations such as unrolling).
660    *
661    *  Result may not be in the range [first,last).  Use move instead.  Note
662    *  that the start of the output range may overlap [first,last).
663   */
664   template<typename _BI1, typename _BI2>
665     inline _BI2
666     move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
667     {
668       // concept requirements
669       __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
670       __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
671       __glibcxx_function_requires(_ConvertibleConcept<
672             typename iterator_traits<_BI1>::value_type,
673             typename iterator_traits<_BI2>::value_type>)
674       __glibcxx_requires_valid_range(__first, __last);
675
676       return (std::__copy_move_backward_a2<true>
677               (std::__miter_base<_BI1>::__b(__first),
678                std::__miter_base<_BI1>::__b(__last), __result));
679     }
680
681 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
682 #else
683 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
684 #endif
685
686   template<typename _ForwardIterator, typename _Tp>
687     inline typename
688     __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
689     __fill_a(_ForwardIterator __first, _ForwardIterator __last,
690              const _Tp& __value)
691     {
692       for (; __first != __last; ++__first)
693         *__first = __value;
694     }
695     
696   template<typename _ForwardIterator, typename _Tp>
697     inline typename
698     __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
699     __fill_a(_ForwardIterator __first, _ForwardIterator __last,
700              const _Tp& __value)
701     {
702       const _Tp __tmp = __value;
703       for (; __first != __last; ++__first)
704         *__first = __tmp;
705     }
706
707   // Specialization: for char types we can use memset.
708   template<typename _Tp>
709     inline typename
710     __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type
711     __fill_a(_Tp* __first, _Tp* __last, const _Tp& __c)
712     {
713       const _Tp __tmp = __c;
714       __builtin_memset(__first, static_cast<unsigned char>(__tmp),
715                        __last - __first);
716     }
717
718   /**
719    *  @brief Fills the range [first,last) with copies of value.
720    *  @ingroup mutating_algorithms
721    *  @param  first  A forward iterator.
722    *  @param  last   A forward iterator.
723    *  @param  value  A reference-to-const of arbitrary type.
724    *  @return   Nothing.
725    *
726    *  This function fills a range with copies of the same value.  For char
727    *  types filling contiguous areas of memory, this becomes an inline call
728    *  to @c memset or @c wmemset.
729   */
730   template<typename _ForwardIterator, typename _Tp>
731     inline void
732     fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
733     {
734       // concept requirements
735       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
736                                   _ForwardIterator>)
737       __glibcxx_requires_valid_range(__first, __last);
738
739       std::__fill_a(std::__niter_base<_ForwardIterator>::__b(__first),
740                     std::__niter_base<_ForwardIterator>::__b(__last), __value);
741     }
742
743   template<typename _OutputIterator, typename _Size, typename _Tp>
744     inline typename
745     __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
746     __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
747     {
748       for (; __n > 0; --__n, ++__first)
749         *__first = __value;
750       return __first;
751     }
752
753   template<typename _OutputIterator, typename _Size, typename _Tp>
754     inline typename
755     __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
756     __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
757     {
758       const _Tp __tmp = __value;
759       for (; __n > 0; --__n, ++__first)
760         *__first = __tmp;
761       return __first;
762     }
763
764   template<typename _Size, typename _Tp>
765     inline typename
766     __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, _Tp*>::__type
767     __fill_n_a(_Tp* __first, _Size __n, const _Tp& __c)
768     {
769       std::__fill_a(__first, __first + __n, __c);
770       return __first + __n;
771     }
772
773   /**
774    *  @brief Fills the range [first,first+n) with copies of value.
775    *  @ingroup mutating_algorithms
776    *  @param  first  An output iterator.
777    *  @param  n      The count of copies to perform.
778    *  @param  value  A reference-to-const of arbitrary type.
779    *  @return   The iterator at first+n.
780    *
781    *  This function fills a range with copies of the same value.  For char
782    *  types filling contiguous areas of memory, this becomes an inline call
783    *  to @c memset or @ wmemset.
784   */
785   template<typename _OI, typename _Size, typename _Tp>
786     inline _OI
787     fill_n(_OI __first, _Size __n, const _Tp& __value)
788     {
789       // concept requirements
790       __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>)
791
792       return _OI(std::__fill_n_a(std::__niter_base<_OI>::__b(__first),
793                                  __n, __value));
794     }
795
796   template<bool _BoolType>
797     struct __equal
798     {
799       template<typename _II1, typename _II2>
800         static bool
801         equal(_II1 __first1, _II1 __last1, _II2 __first2)
802         {
803           for (; __first1 != __last1; ++__first1, ++__first2)
804             if (!(*__first1 == *__first2))
805               return false;
806           return true;
807         }
808     };
809
810   template<>
811     struct __equal<true>
812     {
813       template<typename _Tp>
814         static bool
815         equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
816         {
817           return !__builtin_memcmp(__first1, __first2, sizeof(_Tp)
818                                    * (__last1 - __first1));
819         }
820     };
821
822   template<typename _II1, typename _II2>
823     inline bool
824     __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
825     {
826       typedef typename iterator_traits<_II1>::value_type _ValueType1;
827       typedef typename iterator_traits<_II2>::value_type _ValueType2;
828       const bool __simple = (__is_integer<_ValueType1>::__value
829                              && __is_pointer<_II1>::__value
830                              && __is_pointer<_II2>::__value
831                              && __are_same<_ValueType1, _ValueType2>::__value);
832
833       return std::__equal<__simple>::equal(__first1, __last1, __first2);
834     }
835
836
837   template<typename, typename>
838     struct __lc_rai
839     {
840       template<typename _II1, typename _II2>
841         static _II1
842         __newlast1(_II1, _II1 __last1, _II2, _II2)
843         { return __last1; }
844
845       template<typename _II>
846         static bool
847         __cnd2(_II __first, _II __last)
848         { return __first != __last; }
849     };
850
851   template<>
852     struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag>
853     {
854       template<typename _RAI1, typename _RAI2>
855         static _RAI1
856         __newlast1(_RAI1 __first1, _RAI1 __last1,
857                    _RAI2 __first2, _RAI2 __last2)
858         {
859           const typename iterator_traits<_RAI1>::difference_type
860             __diff1 = __last1 - __first1;
861           const typename iterator_traits<_RAI2>::difference_type
862             __diff2 = __last2 - __first2;
863           return __diff2 < __diff1 ? __first1 + __diff2 : __last1;
864         }
865
866       template<typename _RAI>
867         static bool
868         __cnd2(_RAI, _RAI)
869         { return true; }
870     };
871
872   template<bool _BoolType>
873     struct __lexicographical_compare
874     {
875       template<typename _II1, typename _II2>
876         static bool __lc(_II1, _II1, _II2, _II2);
877     };
878
879   template<bool _BoolType>
880     template<typename _II1, typename _II2>
881       bool
882       __lexicographical_compare<_BoolType>::
883       __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
884       {
885         typedef typename iterator_traits<_II1>::iterator_category _Category1;
886         typedef typename iterator_traits<_II2>::iterator_category _Category2;
887         typedef std::__lc_rai<_Category1, _Category2>   __rai_type;
888         
889         __last1 = __rai_type::__newlast1(__first1, __last1,
890                                          __first2, __last2);
891         for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
892              ++__first1, ++__first2)
893           {
894             if (*__first1 < *__first2)
895               return true;
896             if (*__first2 < *__first1)
897               return false;
898           }
899         return __first1 == __last1 && __first2 != __last2;
900       }
901
902   template<>
903     struct __lexicographical_compare<true>
904     {
905       template<typename _Tp, typename _Up>
906         static bool
907         __lc(const _Tp* __first1, const _Tp* __last1,
908              const _Up* __first2, const _Up* __last2)
909         {
910           const size_t __len1 = __last1 - __first1;
911           const size_t __len2 = __last2 - __first2;
912           const int __result = __builtin_memcmp(__first1, __first2,
913                                                 std::min(__len1, __len2));
914           return __result != 0 ? __result < 0 : __len1 < __len2;
915         }
916     };
917
918   template<typename _II1, typename _II2>
919     inline bool
920     __lexicographical_compare_aux(_II1 __first1, _II1 __last1,
921                                   _II2 __first2, _II2 __last2)
922     {
923       typedef typename iterator_traits<_II1>::value_type _ValueType1;
924       typedef typename iterator_traits<_II2>::value_type _ValueType2;
925       const bool __simple =
926         (__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
927          && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
928          && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
929          && __is_pointer<_II1>::__value
930          && __is_pointer<_II2>::__value);
931
932       return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
933                                                             __first2, __last2);
934     }
935
936 _GLIBCXX_END_NAMESPACE
937
938 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
939
940   /**
941    *  @brief Tests a range for element-wise equality.
942    *  @ingroup non_mutating_algorithms
943    *  @param  first1  An input iterator.
944    *  @param  last1   An input iterator.
945    *  @param  first2  An input iterator.
946    *  @return   A boolean true or false.
947    *
948    *  This compares the elements of two ranges using @c == and returns true or
949    *  false depending on whether all of the corresponding elements of the
950    *  ranges are equal.
951   */
952   template<typename _II1, typename _II2>
953     inline bool
954     equal(_II1 __first1, _II1 __last1, _II2 __first2)
955     {
956       // concept requirements
957       __glibcxx_function_requires(_InputIteratorConcept<_II1>)
958       __glibcxx_function_requires(_InputIteratorConcept<_II2>)
959       __glibcxx_function_requires(_EqualOpConcept<
960             typename iterator_traits<_II1>::value_type,
961             typename iterator_traits<_II2>::value_type>)
962       __glibcxx_requires_valid_range(__first1, __last1);
963
964       return std::__equal_aux(std::__niter_base<_II1>::__b(__first1),
965                               std::__niter_base<_II1>::__b(__last1),
966                               std::__niter_base<_II2>::__b(__first2));
967     }
968
969   /**
970    *  @brief Tests a range for element-wise equality.
971    *  @ingroup non_mutating_algorithms
972    *  @param  first1  An input iterator.
973    *  @param  last1   An input iterator.
974    *  @param  first2  An input iterator.
975    *  @param binary_pred A binary predicate @link functors
976    *                  functor@endlink.
977    *  @return         A boolean true or false.
978    *
979    *  This compares the elements of two ranges using the binary_pred
980    *  parameter, and returns true or
981    *  false depending on whether all of the corresponding elements of the
982    *  ranges are equal.
983   */
984   template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
985     inline bool
986     equal(_IIter1 __first1, _IIter1 __last1,
987           _IIter2 __first2, _BinaryPredicate __binary_pred)
988     {
989       // concept requirements
990       __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
991       __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
992       __glibcxx_requires_valid_range(__first1, __last1);
993
994       for (; __first1 != __last1; ++__first1, ++__first2)
995         if (!bool(__binary_pred(*__first1, *__first2)))
996           return false;
997       return true;
998     }
999
1000   /**
1001    *  @brief Performs 'dictionary' comparison on ranges.
1002    *  @ingroup sorting_algorithms
1003    *  @param  first1  An input iterator.
1004    *  @param  last1   An input iterator.
1005    *  @param  first2  An input iterator.
1006    *  @param  last2   An input iterator.
1007    *  @return   A boolean true or false.
1008    *
1009    *  'Returns true if the sequence of elements defined by the range
1010    *  [first1,last1) is lexicographically less than the sequence of elements
1011    *  defined by the range [first2,last2).  Returns false otherwise.'
1012    *  (Quoted from [25.3.8]/1.)  If the iterators are all character pointers,
1013    *  then this is an inline call to @c memcmp.
1014   */
1015   template<typename _II1, typename _II2>
1016     inline bool
1017     lexicographical_compare(_II1 __first1, _II1 __last1,
1018                             _II2 __first2, _II2 __last2)
1019     {
1020       // concept requirements
1021       typedef typename iterator_traits<_II1>::value_type _ValueType1;
1022       typedef typename iterator_traits<_II2>::value_type _ValueType2;
1023       __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1024       __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1025       __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
1026       __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
1027       __glibcxx_requires_valid_range(__first1, __last1);
1028       __glibcxx_requires_valid_range(__first2, __last2);
1029
1030       return std::__lexicographical_compare_aux
1031         (std::__niter_base<_II1>::__b(__first1),
1032          std::__niter_base<_II1>::__b(__last1),
1033          std::__niter_base<_II2>::__b(__first2),
1034          std::__niter_base<_II2>::__b(__last2));
1035     }
1036
1037   /**
1038    *  @brief Performs "dictionary" comparison on ranges.
1039    *  @ingroup sorting_algorithms
1040    *  @param  first1  An input iterator.
1041    *  @param  last1   An input iterator.
1042    *  @param  first2  An input iterator.
1043    *  @param  last2   An input iterator.
1044    *  @param  comp  A @link comparison_functors comparison functor@endlink.
1045    *  @return   A boolean true or false.
1046    *
1047    *  The same as the four-parameter @c lexicographical_compare, but uses the
1048    *  comp parameter instead of @c <.
1049   */
1050   template<typename _II1, typename _II2, typename _Compare>
1051     bool
1052     lexicographical_compare(_II1 __first1, _II1 __last1,
1053                             _II2 __first2, _II2 __last2, _Compare __comp)
1054     {
1055       typedef typename iterator_traits<_II1>::iterator_category _Category1;
1056       typedef typename iterator_traits<_II2>::iterator_category _Category2;
1057       typedef std::__lc_rai<_Category1, _Category2>     __rai_type;
1058
1059       // concept requirements
1060       __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1061       __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1062       __glibcxx_requires_valid_range(__first1, __last1);
1063       __glibcxx_requires_valid_range(__first2, __last2);
1064
1065       __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
1066       for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
1067            ++__first1, ++__first2)
1068         {
1069           if (__comp(*__first1, *__first2))
1070             return true;
1071           if (__comp(*__first2, *__first1))
1072             return false;
1073         }
1074       return __first1 == __last1 && __first2 != __last2;
1075     }
1076
1077   /**
1078    *  @brief Finds the places in ranges which don't match.
1079    *  @ingroup non_mutating_algorithms
1080    *  @param  first1  An input iterator.
1081    *  @param  last1   An input iterator.
1082    *  @param  first2  An input iterator.
1083    *  @return   A pair of iterators pointing to the first mismatch.
1084    *
1085    *  This compares the elements of two ranges using @c == and returns a pair
1086    *  of iterators.  The first iterator points into the first range, the
1087    *  second iterator points into the second range, and the elements pointed
1088    *  to by the iterators are not equal.
1089   */
1090   template<typename _InputIterator1, typename _InputIterator2>
1091     pair<_InputIterator1, _InputIterator2>
1092     mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1093              _InputIterator2 __first2)
1094     {
1095       // concept requirements
1096       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1097       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1098       __glibcxx_function_requires(_EqualOpConcept<
1099             typename iterator_traits<_InputIterator1>::value_type,
1100             typename iterator_traits<_InputIterator2>::value_type>)
1101       __glibcxx_requires_valid_range(__first1, __last1);
1102
1103       while (__first1 != __last1 && *__first1 == *__first2)
1104         {
1105           ++__first1;
1106           ++__first2;
1107         }
1108       return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1109     }
1110
1111   /**
1112    *  @brief Finds the places in ranges which don't match.
1113    *  @ingroup non_mutating_algorithms
1114    *  @param  first1  An input iterator.
1115    *  @param  last1   An input iterator.
1116    *  @param  first2  An input iterator.
1117    *  @param binary_pred A binary predicate @link functors
1118    *         functor@endlink.
1119    *  @return   A pair of iterators pointing to the first mismatch.
1120    *
1121    *  This compares the elements of two ranges using the binary_pred
1122    *  parameter, and returns a pair
1123    *  of iterators.  The first iterator points into the first range, the
1124    *  second iterator points into the second range, and the elements pointed
1125    *  to by the iterators are not equal.
1126   */
1127   template<typename _InputIterator1, typename _InputIterator2,
1128            typename _BinaryPredicate>
1129     pair<_InputIterator1, _InputIterator2>
1130     mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1131              _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1132     {
1133       // concept requirements
1134       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1135       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1136       __glibcxx_requires_valid_range(__first1, __last1);
1137
1138       while (__first1 != __last1 && bool(__binary_pred(*__first1, *__first2)))
1139         {
1140           ++__first1;
1141           ++__first2;
1142         }
1143       return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1144     }
1145
1146 _GLIBCXX_END_NESTED_NAMESPACE
1147
1148 // NB: This file is included within many other C++ includes, as a way
1149 // of getting the base algorithms. So, make sure that parallel bits
1150 // come in too if requested. 
1151 #ifdef _GLIBCXX_PARALLEL
1152 # include <parallel/algobase.h>
1153 #endif
1154
1155 #endif