OSDN Git Service

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