libstdc++
|
00001 // Core algorithmic facilities -*- C++ -*- 00002 00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 00004 // 2011 Free Software Foundation, Inc. 00005 // 00006 // This file is part of the GNU ISO C++ Library. This library is free 00007 // software; you can redistribute it and/or modify it under the 00008 // terms of the GNU General Public License as published by the 00009 // Free Software Foundation; either version 3, or (at your option) 00010 // any later version. 00011 00012 // This library is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 00017 // Under Section 7 of GPL version 3, you are granted additional 00018 // permissions described in the GCC Runtime Library Exception, version 00019 // 3.1, as published by the Free Software Foundation. 00020 00021 // You should have received a copy of the GNU General Public License and 00022 // a copy of the GCC Runtime Library Exception along with this program; 00023 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00024 // <http://www.gnu.org/licenses/>. 00025 00026 /* 00027 * 00028 * Copyright (c) 1994 00029 * Hewlett-Packard Company 00030 * 00031 * Permission to use, copy, modify, distribute and sell this software 00032 * and its documentation for any purpose is hereby granted without fee, 00033 * provided that the above copyright notice appear in all copies and 00034 * that both that copyright notice and this permission notice appear 00035 * in supporting documentation. Hewlett-Packard Company makes no 00036 * representations about the suitability of this software for any 00037 * purpose. It is provided "as is" without express or implied warranty. 00038 * 00039 * 00040 * Copyright (c) 1996-1998 00041 * Silicon Graphics Computer Systems, Inc. 00042 * 00043 * Permission to use, copy, modify, distribute and sell this software 00044 * and its documentation for any purpose is hereby granted without fee, 00045 * provided that the above copyright notice appear in all copies and 00046 * that both that copyright notice and this permission notice appear 00047 * in supporting documentation. Silicon Graphics makes no 00048 * representations about the suitability of this software for any 00049 * purpose. It is provided "as is" without express or implied warranty. 00050 */ 00051 00052 /** @file bits/stl_algobase.h 00053 * This is an internal header file, included by other library headers. 00054 * Do not attempt to use it directly. @headername{algorithm} 00055 */ 00056 00057 #ifndef _STL_ALGOBASE_H 00058 #define _STL_ALGOBASE_H 1 00059 00060 #include <bits/c++config.h> 00061 #include <bits/functexcept.h> 00062 #include <bits/cpp_type_traits.h> 00063 #include <ext/type_traits.h> 00064 #include <ext/numeric_traits.h> 00065 #include <bits/stl_pair.h> 00066 #include <bits/stl_iterator_base_types.h> 00067 #include <bits/stl_iterator_base_funcs.h> 00068 #include <bits/stl_iterator.h> 00069 #include <bits/concept_check.h> 00070 #include <debug/debug.h> 00071 #include <bits/move.h> // For std::swap and _GLIBCXX_MOVE 00072 00073 namespace std _GLIBCXX_VISIBILITY(default) 00074 { 00075 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00076 00077 // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a 00078 // nutshell, we are partially implementing the resolution of DR 187, 00079 // when it's safe, i.e., the value_types are equal. 00080 template<bool _BoolType> 00081 struct __iter_swap 00082 { 00083 template<typename _ForwardIterator1, typename _ForwardIterator2> 00084 static void 00085 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b) 00086 { 00087 typedef typename iterator_traits<_ForwardIterator1>::value_type 00088 _ValueType1; 00089 _ValueType1 __tmp = _GLIBCXX_MOVE(*__a); 00090 *__a = _GLIBCXX_MOVE(*__b); 00091 *__b = _GLIBCXX_MOVE(__tmp); 00092 } 00093 }; 00094 00095 template<> 00096 struct __iter_swap<true> 00097 { 00098 template<typename _ForwardIterator1, typename _ForwardIterator2> 00099 static void 00100 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b) 00101 { 00102 swap(*__a, *__b); 00103 } 00104 }; 00105 00106 /** 00107 * @brief Swaps the contents of two iterators. 00108 * @ingroup mutating_algorithms 00109 * @param __a An iterator. 00110 * @param __b Another iterator. 00111 * @return Nothing. 00112 * 00113 * This function swaps the values pointed to by two iterators, not the 00114 * iterators themselves. 00115 */ 00116 template<typename _ForwardIterator1, typename _ForwardIterator2> 00117 inline void 00118 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b) 00119 { 00120 typedef typename iterator_traits<_ForwardIterator1>::value_type 00121 _ValueType1; 00122 typedef typename iterator_traits<_ForwardIterator2>::value_type 00123 _ValueType2; 00124 00125 // concept requirements 00126 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 00127 _ForwardIterator1>) 00128 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 00129 _ForwardIterator2>) 00130 __glibcxx_function_requires(_ConvertibleConcept<_ValueType1, 00131 _ValueType2>) 00132 __glibcxx_function_requires(_ConvertibleConcept<_ValueType2, 00133 _ValueType1>) 00134 00135 typedef typename iterator_traits<_ForwardIterator1>::reference 00136 _ReferenceType1; 00137 typedef typename iterator_traits<_ForwardIterator2>::reference 00138 _ReferenceType2; 00139 std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value 00140 && __are_same<_ValueType1&, _ReferenceType1>::__value 00141 && __are_same<_ValueType2&, _ReferenceType2>::__value>:: 00142 iter_swap(__a, __b); 00143 } 00144 00145 /** 00146 * @brief Swap the elements of two sequences. 00147 * @ingroup mutating_algorithms 00148 * @param __first1 A forward iterator. 00149 * @param __last1 A forward iterator. 00150 * @param __first2 A forward iterator. 00151 * @return An iterator equal to @p first2+(last1-first1). 00152 * 00153 * Swaps each element in the range @p [first1,last1) with the 00154 * corresponding element in the range @p [first2,(last1-first1)). 00155 * The ranges must not overlap. 00156 */ 00157 template<typename _ForwardIterator1, typename _ForwardIterator2> 00158 _ForwardIterator2 00159 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 00160 _ForwardIterator2 __first2) 00161 { 00162 // concept requirements 00163 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 00164 _ForwardIterator1>) 00165 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 00166 _ForwardIterator2>) 00167 __glibcxx_requires_valid_range(__first1, __last1); 00168 00169 for (; __first1 != __last1; ++__first1, ++__first2) 00170 std::iter_swap(__first1, __first2); 00171 return __first2; 00172 } 00173 00174 /** 00175 * @brief This does what you think it does. 00176 * @ingroup sorting_algorithms 00177 * @param __a A thing of arbitrary type. 00178 * @param __b Another thing of arbitrary type. 00179 * @return The lesser of the parameters. 00180 * 00181 * This is the simple classic generic implementation. It will work on 00182 * temporary expressions, since they are only evaluated once, unlike a 00183 * preprocessor macro. 00184 */ 00185 template<typename _Tp> 00186 inline const _Tp& 00187 min(const _Tp& __a, const _Tp& __b) 00188 { 00189 // concept requirements 00190 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>) 00191 //return __b < __a ? __b : __a; 00192 if (__b < __a) 00193 return __b; 00194 return __a; 00195 } 00196 00197 /** 00198 * @brief This does what you think it does. 00199 * @ingroup sorting_algorithms 00200 * @param __a A thing of arbitrary type. 00201 * @param __b Another thing of arbitrary type. 00202 * @return The greater of the parameters. 00203 * 00204 * This is the simple classic generic implementation. It will work on 00205 * temporary expressions, since they are only evaluated once, unlike a 00206 * preprocessor macro. 00207 */ 00208 template<typename _Tp> 00209 inline const _Tp& 00210 max(const _Tp& __a, const _Tp& __b) 00211 { 00212 // concept requirements 00213 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>) 00214 //return __a < __b ? __b : __a; 00215 if (__a < __b) 00216 return __b; 00217 return __a; 00218 } 00219 00220 /** 00221 * @brief This does what you think it does. 00222 * @ingroup sorting_algorithms 00223 * @param __a A thing of arbitrary type. 00224 * @param __b Another thing of arbitrary type. 00225 * @param __comp A @link comparison_functors comparison functor@endlink. 00226 * @return The lesser of the parameters. 00227 * 00228 * This will work on temporary expressions, since they are only evaluated 00229 * once, unlike a preprocessor macro. 00230 */ 00231 template<typename _Tp, typename _Compare> 00232 inline const _Tp& 00233 min(const _Tp& __a, const _Tp& __b, _Compare __comp) 00234 { 00235 //return __comp(__b, __a) ? __b : __a; 00236 if (__comp(__b, __a)) 00237 return __b; 00238 return __a; 00239 } 00240 00241 /** 00242 * @brief This does what you think it does. 00243 * @ingroup sorting_algorithms 00244 * @param __a A thing of arbitrary type. 00245 * @param __b Another thing of arbitrary type. 00246 * @param __comp A @link comparison_functors comparison functor@endlink. 00247 * @return The greater of the parameters. 00248 * 00249 * This will work on temporary expressions, since they are only evaluated 00250 * once, unlike a preprocessor macro. 00251 */ 00252 template<typename _Tp, typename _Compare> 00253 inline const _Tp& 00254 max(const _Tp& __a, const _Tp& __b, _Compare __comp) 00255 { 00256 //return __comp(__a, __b) ? __b : __a; 00257 if (__comp(__a, __b)) 00258 return __b; 00259 return __a; 00260 } 00261 00262 // If _Iterator is a __normal_iterator return its base (a plain pointer, 00263 // normally) otherwise return it untouched. See copy, fill, ... 00264 template<typename _Iterator> 00265 struct _Niter_base 00266 : _Iter_base<_Iterator, __is_normal_iterator<_Iterator>::__value> 00267 { }; 00268 00269 template<typename _Iterator> 00270 inline typename _Niter_base<_Iterator>::iterator_type 00271 __niter_base(_Iterator __it) 00272 { return std::_Niter_base<_Iterator>::_S_base(__it); } 00273 00274 // Likewise, for move_iterator. 00275 template<typename _Iterator> 00276 struct _Miter_base 00277 : _Iter_base<_Iterator, __is_move_iterator<_Iterator>::__value> 00278 { }; 00279 00280 template<typename _Iterator> 00281 inline typename _Miter_base<_Iterator>::iterator_type 00282 __miter_base(_Iterator __it) 00283 { return std::_Miter_base<_Iterator>::_S_base(__it); } 00284 00285 // All of these auxiliary structs serve two purposes. (1) Replace 00286 // calls to copy with memmove whenever possible. (Memmove, not memcpy, 00287 // because the input and output ranges are permitted to overlap.) 00288 // (2) If we're using random access iterators, then write the loop as 00289 // a for loop with an explicit count. 00290 00291 template<bool, bool, typename> 00292 struct __copy_move 00293 { 00294 template<typename _II, typename _OI> 00295 static _OI 00296 __copy_m(_II __first, _II __last, _OI __result) 00297 { 00298 for (; __first != __last; ++__result, ++__first) 00299 *__result = *__first; 00300 return __result; 00301 } 00302 }; 00303 00304 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00305 template<typename _Category> 00306 struct __copy_move<true, false, _Category> 00307 { 00308 template<typename _II, typename _OI> 00309 static _OI 00310 __copy_m(_II __first, _II __last, _OI __result) 00311 { 00312 for (; __first != __last; ++__result, ++__first) 00313 *__result = std::move(*__first); 00314 return __result; 00315 } 00316 }; 00317 #endif 00318 00319 template<> 00320 struct __copy_move<false, false, random_access_iterator_tag> 00321 { 00322 template<typename _II, typename _OI> 00323 static _OI 00324 __copy_m(_II __first, _II __last, _OI __result) 00325 { 00326 typedef typename iterator_traits<_II>::difference_type _Distance; 00327 for(_Distance __n = __last - __first; __n > 0; --__n) 00328 { 00329 *__result = *__first; 00330 ++__first; 00331 ++__result; 00332 } 00333 return __result; 00334 } 00335 }; 00336 00337 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00338 template<> 00339 struct __copy_move<true, false, random_access_iterator_tag> 00340 { 00341 template<typename _II, typename _OI> 00342 static _OI 00343 __copy_m(_II __first, _II __last, _OI __result) 00344 { 00345 typedef typename iterator_traits<_II>::difference_type _Distance; 00346 for(_Distance __n = __last - __first; __n > 0; --__n) 00347 { 00348 *__result = std::move(*__first); 00349 ++__first; 00350 ++__result; 00351 } 00352 return __result; 00353 } 00354 }; 00355 #endif 00356 00357 template<bool _IsMove> 00358 struct __copy_move<_IsMove, true, random_access_iterator_tag> 00359 { 00360 template<typename _Tp> 00361 static _Tp* 00362 __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result) 00363 { 00364 const ptrdiff_t _Num = __last - __first; 00365 if (_Num) 00366 __builtin_memmove(__result, __first, sizeof(_Tp) * _Num); 00367 return __result + _Num; 00368 } 00369 }; 00370 00371 template<bool _IsMove, typename _II, typename _OI> 00372 inline _OI 00373 __copy_move_a(_II __first, _II __last, _OI __result) 00374 { 00375 typedef typename iterator_traits<_II>::value_type _ValueTypeI; 00376 typedef typename iterator_traits<_OI>::value_type _ValueTypeO; 00377 typedef typename iterator_traits<_II>::iterator_category _Category; 00378 const bool __simple = (__is_trivial(_ValueTypeI) 00379 && __is_pointer<_II>::__value 00380 && __is_pointer<_OI>::__value 00381 && __are_same<_ValueTypeI, _ValueTypeO>::__value); 00382 00383 return std::__copy_move<_IsMove, __simple, 00384 _Category>::__copy_m(__first, __last, __result); 00385 } 00386 00387 // Helpers for streambuf iterators (either istream or ostream). 00388 // NB: avoid including <iosfwd>, relatively large. 00389 template<typename _CharT> 00390 struct char_traits; 00391 00392 template<typename _CharT, typename _Traits> 00393 class istreambuf_iterator; 00394 00395 template<typename _CharT, typename _Traits> 00396 class ostreambuf_iterator; 00397 00398 template<bool _IsMove, typename _CharT> 00399 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, 00400 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type 00401 __copy_move_a2(_CharT*, _CharT*, 00402 ostreambuf_iterator<_CharT, char_traits<_CharT> >); 00403 00404 template<bool _IsMove, typename _CharT> 00405 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, 00406 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type 00407 __copy_move_a2(const _CharT*, const _CharT*, 00408 ostreambuf_iterator<_CharT, char_traits<_CharT> >); 00409 00410 template<bool _IsMove, typename _CharT> 00411 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, 00412 _CharT*>::__type 00413 __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >, 00414 istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*); 00415 00416 template<bool _IsMove, typename _II, typename _OI> 00417 inline _OI 00418 __copy_move_a2(_II __first, _II __last, _OI __result) 00419 { 00420 return _OI(std::__copy_move_a<_IsMove>(std::__niter_base(__first), 00421 std::__niter_base(__last), 00422 std::__niter_base(__result))); 00423 } 00424 00425 /** 00426 * @brief Copies the range [first,last) into result. 00427 * @ingroup mutating_algorithms 00428 * @param __first An input iterator. 00429 * @param __last An input iterator. 00430 * @param __result An output iterator. 00431 * @return result + (first - last) 00432 * 00433 * This inline function will boil down to a call to @c memmove whenever 00434 * possible. Failing that, if random access iterators are passed, then the 00435 * loop count will be known (and therefore a candidate for compiler 00436 * optimizations such as unrolling). Result may not be contained within 00437 * [first,last); the copy_backward function should be used instead. 00438 * 00439 * Note that the end of the output range is permitted to be contained 00440 * within [first,last). 00441 */ 00442 template<typename _II, typename _OI> 00443 inline _OI 00444 copy(_II __first, _II __last, _OI __result) 00445 { 00446 // concept requirements 00447 __glibcxx_function_requires(_InputIteratorConcept<_II>) 00448 __glibcxx_function_requires(_OutputIteratorConcept<_OI, 00449 typename iterator_traits<_II>::value_type>) 00450 __glibcxx_requires_valid_range(__first, __last); 00451 00452 return (std::__copy_move_a2<__is_move_iterator<_II>::__value> 00453 (std::__miter_base(__first), std::__miter_base(__last), 00454 __result)); 00455 } 00456 00457 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00458 /** 00459 * @brief Moves the range [first,last) into result. 00460 * @ingroup mutating_algorithms 00461 * @param __first An input iterator. 00462 * @param __last An input iterator. 00463 * @param __result An output iterator. 00464 * @return result + (first - last) 00465 * 00466 * This inline function will boil down to a call to @c memmove whenever 00467 * possible. Failing that, if random access iterators are passed, then the 00468 * loop count will be known (and therefore a candidate for compiler 00469 * optimizations such as unrolling). Result may not be contained within 00470 * [first,last); the move_backward function should be used instead. 00471 * 00472 * Note that the end of the output range is permitted to be contained 00473 * within [first,last). 00474 */ 00475 template<typename _II, typename _OI> 00476 inline _OI 00477 move(_II __first, _II __last, _OI __result) 00478 { 00479 // concept requirements 00480 __glibcxx_function_requires(_InputIteratorConcept<_II>) 00481 __glibcxx_function_requires(_OutputIteratorConcept<_OI, 00482 typename iterator_traits<_II>::value_type>) 00483 __glibcxx_requires_valid_range(__first, __last); 00484 00485 return std::__copy_move_a2<true>(std::__miter_base(__first), 00486 std::__miter_base(__last), __result); 00487 } 00488 00489 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp) 00490 #else 00491 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp) 00492 #endif 00493 00494 template<bool, bool, typename> 00495 struct __copy_move_backward 00496 { 00497 template<typename _BI1, typename _BI2> 00498 static _BI2 00499 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result) 00500 { 00501 while (__first != __last) 00502 *--__result = *--__last; 00503 return __result; 00504 } 00505 }; 00506 00507 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00508 template<typename _Category> 00509 struct __copy_move_backward<true, false, _Category> 00510 { 00511 template<typename _BI1, typename _BI2> 00512 static _BI2 00513 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result) 00514 { 00515 while (__first != __last) 00516 *--__result = std::move(*--__last); 00517 return __result; 00518 } 00519 }; 00520 #endif 00521 00522 template<> 00523 struct __copy_move_backward<false, false, random_access_iterator_tag> 00524 { 00525 template<typename _BI1, typename _BI2> 00526 static _BI2 00527 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result) 00528 { 00529 typename iterator_traits<_BI1>::difference_type __n; 00530 for (__n = __last - __first; __n > 0; --__n) 00531 *--__result = *--__last; 00532 return __result; 00533 } 00534 }; 00535 00536 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00537 template<> 00538 struct __copy_move_backward<true, false, random_access_iterator_tag> 00539 { 00540 template<typename _BI1, typename _BI2> 00541 static _BI2 00542 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result) 00543 { 00544 typename iterator_traits<_BI1>::difference_type __n; 00545 for (__n = __last - __first; __n > 0; --__n) 00546 *--__result = std::move(*--__last); 00547 return __result; 00548 } 00549 }; 00550 #endif 00551 00552 template<bool _IsMove> 00553 struct __copy_move_backward<_IsMove, true, random_access_iterator_tag> 00554 { 00555 template<typename _Tp> 00556 static _Tp* 00557 __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result) 00558 { 00559 const ptrdiff_t _Num = __last - __first; 00560 if (_Num) 00561 __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num); 00562 return __result - _Num; 00563 } 00564 }; 00565 00566 template<bool _IsMove, typename _BI1, typename _BI2> 00567 inline _BI2 00568 __copy_move_backward_a(_BI1 __first, _BI1 __last, _BI2 __result) 00569 { 00570 typedef typename iterator_traits<_BI1>::value_type _ValueType1; 00571 typedef typename iterator_traits<_BI2>::value_type _ValueType2; 00572 typedef typename iterator_traits<_BI1>::iterator_category _Category; 00573 const bool __simple = (__is_trivial(_ValueType1) 00574 && __is_pointer<_BI1>::__value 00575 && __is_pointer<_BI2>::__value 00576 && __are_same<_ValueType1, _ValueType2>::__value); 00577 00578 return std::__copy_move_backward<_IsMove, __simple, 00579 _Category>::__copy_move_b(__first, 00580 __last, 00581 __result); 00582 } 00583 00584 template<bool _IsMove, typename _BI1, typename _BI2> 00585 inline _BI2 00586 __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result) 00587 { 00588 return _BI2(std::__copy_move_backward_a<_IsMove> 00589 (std::__niter_base(__first), std::__niter_base(__last), 00590 std::__niter_base(__result))); 00591 } 00592 00593 /** 00594 * @brief Copies the range [first,last) into result. 00595 * @ingroup mutating_algorithms 00596 * @param __first A bidirectional iterator. 00597 * @param __last A bidirectional iterator. 00598 * @param __result A bidirectional iterator. 00599 * @return result - (first - last) 00600 * 00601 * The function has the same effect as copy, but starts at the end of the 00602 * range and works its way to the start, returning the start of the result. 00603 * This inline function will boil down to a call to @c memmove whenever 00604 * possible. Failing that, if random access iterators are passed, then the 00605 * loop count will be known (and therefore a candidate for compiler 00606 * optimizations such as unrolling). 00607 * 00608 * Result may not be in the range [first,last). Use copy instead. Note 00609 * that the start of the output range may overlap [first,last). 00610 */ 00611 template<typename _BI1, typename _BI2> 00612 inline _BI2 00613 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result) 00614 { 00615 // concept requirements 00616 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>) 00617 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>) 00618 __glibcxx_function_requires(_ConvertibleConcept< 00619 typename iterator_traits<_BI1>::value_type, 00620 typename iterator_traits<_BI2>::value_type>) 00621 __glibcxx_requires_valid_range(__first, __last); 00622 00623 return (std::__copy_move_backward_a2<__is_move_iterator<_BI1>::__value> 00624 (std::__miter_base(__first), std::__miter_base(__last), 00625 __result)); 00626 } 00627 00628 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00629 /** 00630 * @brief Moves the range [first,last) into result. 00631 * @ingroup mutating_algorithms 00632 * @param __first A bidirectional iterator. 00633 * @param __last A bidirectional iterator. 00634 * @param __result A bidirectional iterator. 00635 * @return result - (first - last) 00636 * 00637 * The function has the same effect as move, but starts at the end of the 00638 * range and works its way to the start, returning the start of the result. 00639 * This inline function will boil down to a call to @c memmove whenever 00640 * possible. Failing that, if random access iterators are passed, then the 00641 * loop count will be known (and therefore a candidate for compiler 00642 * optimizations such as unrolling). 00643 * 00644 * Result may not be in the range (first,last]. Use move instead. Note 00645 * that the start of the output range may overlap [first,last). 00646 */ 00647 template<typename _BI1, typename _BI2> 00648 inline _BI2 00649 move_backward(_BI1 __first, _BI1 __last, _BI2 __result) 00650 { 00651 // concept requirements 00652 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>) 00653 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>) 00654 __glibcxx_function_requires(_ConvertibleConcept< 00655 typename iterator_traits<_BI1>::value_type, 00656 typename iterator_traits<_BI2>::value_type>) 00657 __glibcxx_requires_valid_range(__first, __last); 00658 00659 return std::__copy_move_backward_a2<true>(std::__miter_base(__first), 00660 std::__miter_base(__last), 00661 __result); 00662 } 00663 00664 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp) 00665 #else 00666 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp) 00667 #endif 00668 00669 template<typename _ForwardIterator, typename _Tp> 00670 inline typename 00671 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type 00672 __fill_a(_ForwardIterator __first, _ForwardIterator __last, 00673 const _Tp& __value) 00674 { 00675 for (; __first != __last; ++__first) 00676 *__first = __value; 00677 } 00678 00679 template<typename _ForwardIterator, typename _Tp> 00680 inline typename 00681 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type 00682 __fill_a(_ForwardIterator __first, _ForwardIterator __last, 00683 const _Tp& __value) 00684 { 00685 const _Tp __tmp = __value; 00686 for (; __first != __last; ++__first) 00687 *__first = __tmp; 00688 } 00689 00690 // Specialization: for char types we can use memset. 00691 template<typename _Tp> 00692 inline typename 00693 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type 00694 __fill_a(_Tp* __first, _Tp* __last, const _Tp& __c) 00695 { 00696 const _Tp __tmp = __c; 00697 __builtin_memset(__first, static_cast<unsigned char>(__tmp), 00698 __last - __first); 00699 } 00700 00701 /** 00702 * @brief Fills the range [first,last) with copies of value. 00703 * @ingroup mutating_algorithms 00704 * @param __first A forward iterator. 00705 * @param __last A forward iterator. 00706 * @param __value A reference-to-const of arbitrary type. 00707 * @return Nothing. 00708 * 00709 * This function fills a range with copies of the same value. For char 00710 * types filling contiguous areas of memory, this becomes an inline call 00711 * to @c memset or @c wmemset. 00712 */ 00713 template<typename _ForwardIterator, typename _Tp> 00714 inline void 00715 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) 00716 { 00717 // concept requirements 00718 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 00719 _ForwardIterator>) 00720 __glibcxx_requires_valid_range(__first, __last); 00721 00722 std::__fill_a(std::__niter_base(__first), std::__niter_base(__last), 00723 __value); 00724 } 00725 00726 template<typename _OutputIterator, typename _Size, typename _Tp> 00727 inline typename 00728 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type 00729 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value) 00730 { 00731 for (__decltype(__n + 0) __niter = __n; 00732 __niter > 0; --__niter, ++__first) 00733 *__first = __value; 00734 return __first; 00735 } 00736 00737 template<typename _OutputIterator, typename _Size, typename _Tp> 00738 inline typename 00739 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type 00740 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value) 00741 { 00742 const _Tp __tmp = __value; 00743 for (__decltype(__n + 0) __niter = __n; 00744 __niter > 0; --__niter, ++__first) 00745 *__first = __tmp; 00746 return __first; 00747 } 00748 00749 template<typename _Size, typename _Tp> 00750 inline typename 00751 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, _Tp*>::__type 00752 __fill_n_a(_Tp* __first, _Size __n, const _Tp& __c) 00753 { 00754 std::__fill_a(__first, __first + __n, __c); 00755 return __first + __n; 00756 } 00757 00758 /** 00759 * @brief Fills the range [first,first+n) with copies of value. 00760 * @ingroup mutating_algorithms 00761 * @param __first An output iterator. 00762 * @param __n The count of copies to perform. 00763 * @param __value A reference-to-const of arbitrary type. 00764 * @return The iterator at first+n. 00765 * 00766 * This function fills a range with copies of the same value. For char 00767 * types filling contiguous areas of memory, this becomes an inline call 00768 * to @c memset or @ wmemset. 00769 * 00770 * _GLIBCXX_RESOLVE_LIB_DEFECTS 00771 * DR 865. More algorithms that throw away information 00772 */ 00773 template<typename _OI, typename _Size, typename _Tp> 00774 inline _OI 00775 fill_n(_OI __first, _Size __n, const _Tp& __value) 00776 { 00777 // concept requirements 00778 __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>) 00779 00780 return _OI(std::__fill_n_a(std::__niter_base(__first), __n, __value)); 00781 } 00782 00783 template<bool _BoolType> 00784 struct __equal 00785 { 00786 template<typename _II1, typename _II2> 00787 static bool 00788 equal(_II1 __first1, _II1 __last1, _II2 __first2) 00789 { 00790 for (; __first1 != __last1; ++__first1, ++__first2) 00791 if (!(*__first1 == *__first2)) 00792 return false; 00793 return true; 00794 } 00795 }; 00796 00797 template<> 00798 struct __equal<true> 00799 { 00800 template<typename _Tp> 00801 static bool 00802 equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2) 00803 { 00804 return !__builtin_memcmp(__first1, __first2, sizeof(_Tp) 00805 * (__last1 - __first1)); 00806 } 00807 }; 00808 00809 template<typename _II1, typename _II2> 00810 inline bool 00811 __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2) 00812 { 00813 typedef typename iterator_traits<_II1>::value_type _ValueType1; 00814 typedef typename iterator_traits<_II2>::value_type _ValueType2; 00815 const bool __simple = ((__is_integer<_ValueType1>::__value 00816 || __is_pointer<_ValueType1>::__value) 00817 && __is_pointer<_II1>::__value 00818 && __is_pointer<_II2>::__value 00819 && __are_same<_ValueType1, _ValueType2>::__value); 00820 00821 return std::__equal<__simple>::equal(__first1, __last1, __first2); 00822 } 00823 00824 00825 template<typename, typename> 00826 struct __lc_rai 00827 { 00828 template<typename _II1, typename _II2> 00829 static _II1 00830 __newlast1(_II1, _II1 __last1, _II2, _II2) 00831 { return __last1; } 00832 00833 template<typename _II> 00834 static bool 00835 __cnd2(_II __first, _II __last) 00836 { return __first != __last; } 00837 }; 00838 00839 template<> 00840 struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag> 00841 { 00842 template<typename _RAI1, typename _RAI2> 00843 static _RAI1 00844 __newlast1(_RAI1 __first1, _RAI1 __last1, 00845 _RAI2 __first2, _RAI2 __last2) 00846 { 00847 const typename iterator_traits<_RAI1>::difference_type 00848 __diff1 = __last1 - __first1; 00849 const typename iterator_traits<_RAI2>::difference_type 00850 __diff2 = __last2 - __first2; 00851 return __diff2 < __diff1 ? __first1 + __diff2 : __last1; 00852 } 00853 00854 template<typename _RAI> 00855 static bool 00856 __cnd2(_RAI, _RAI) 00857 { return true; } 00858 }; 00859 00860 template<bool _BoolType> 00861 struct __lexicographical_compare 00862 { 00863 template<typename _II1, typename _II2> 00864 static bool __lc(_II1, _II1, _II2, _II2); 00865 }; 00866 00867 template<bool _BoolType> 00868 template<typename _II1, typename _II2> 00869 bool 00870 __lexicographical_compare<_BoolType>:: 00871 __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2) 00872 { 00873 typedef typename iterator_traits<_II1>::iterator_category _Category1; 00874 typedef typename iterator_traits<_II2>::iterator_category _Category2; 00875 typedef std::__lc_rai<_Category1, _Category2> __rai_type; 00876 00877 __last1 = __rai_type::__newlast1(__first1, __last1, 00878 __first2, __last2); 00879 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2); 00880 ++__first1, ++__first2) 00881 { 00882 if (*__first1 < *__first2) 00883 return true; 00884 if (*__first2 < *__first1) 00885 return false; 00886 } 00887 return __first1 == __last1 && __first2 != __last2; 00888 } 00889 00890 template<> 00891 struct __lexicographical_compare<true> 00892 { 00893 template<typename _Tp, typename _Up> 00894 static bool 00895 __lc(const _Tp* __first1, const _Tp* __last1, 00896 const _Up* __first2, const _Up* __last2) 00897 { 00898 const size_t __len1 = __last1 - __first1; 00899 const size_t __len2 = __last2 - __first2; 00900 const int __result = __builtin_memcmp(__first1, __first2, 00901 std::min(__len1, __len2)); 00902 return __result != 0 ? __result < 0 : __len1 < __len2; 00903 } 00904 }; 00905 00906 template<typename _II1, typename _II2> 00907 inline bool 00908 __lexicographical_compare_aux(_II1 __first1, _II1 __last1, 00909 _II2 __first2, _II2 __last2) 00910 { 00911 typedef typename iterator_traits<_II1>::value_type _ValueType1; 00912 typedef typename iterator_traits<_II2>::value_type _ValueType2; 00913 const bool __simple = 00914 (__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value 00915 && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed 00916 && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed 00917 && __is_pointer<_II1>::__value 00918 && __is_pointer<_II2>::__value); 00919 00920 return std::__lexicographical_compare<__simple>::__lc(__first1, __last1, 00921 __first2, __last2); 00922 } 00923 00924 /** 00925 * @brief Finds the first position in which @a val could be inserted 00926 * without changing the ordering. 00927 * @param __first An iterator. 00928 * @param __last Another iterator. 00929 * @param __val The search term. 00930 * @return An iterator pointing to the first element <em>not less 00931 * than</em> @a val, or end() if every element is less than 00932 * @a val. 00933 * @ingroup binary_search_algorithms 00934 */ 00935 template<typename _ForwardIterator, typename _Tp> 00936 _ForwardIterator 00937 lower_bound(_ForwardIterator __first, _ForwardIterator __last, 00938 const _Tp& __val) 00939 { 00940 typedef typename iterator_traits<_ForwardIterator>::value_type 00941 _ValueType; 00942 typedef typename iterator_traits<_ForwardIterator>::difference_type 00943 _DistanceType; 00944 00945 // concept requirements 00946 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 00947 __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>) 00948 __glibcxx_requires_partitioned_lower(__first, __last, __val); 00949 00950 _DistanceType __len = std::distance(__first, __last); 00951 00952 while (__len > 0) 00953 { 00954 _DistanceType __half = __len >> 1; 00955 _ForwardIterator __middle = __first; 00956 std::advance(__middle, __half); 00957 if (*__middle < __val) 00958 { 00959 __first = __middle; 00960 ++__first; 00961 __len = __len - __half - 1; 00962 } 00963 else 00964 __len = __half; 00965 } 00966 return __first; 00967 } 00968 00969 /// This is a helper function for the sort routines and for random.tcc. 00970 // Precondition: __n > 0. 00971 template<typename _Size> 00972 inline _Size 00973 __lg(_Size __n) 00974 { 00975 _Size __k; 00976 for (__k = 0; __n != 0; __n >>= 1) 00977 ++__k; 00978 return __k - 1; 00979 } 00980 00981 inline int 00982 __lg(int __n) 00983 { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); } 00984 00985 inline long 00986 __lg(long __n) 00987 { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); } 00988 00989 inline long long 00990 __lg(long long __n) 00991 { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); } 00992 00993 _GLIBCXX_END_NAMESPACE_VERSION 00994 00995 _GLIBCXX_BEGIN_NAMESPACE_ALGO 00996 00997 /** 00998 * @brief Tests a range for element-wise equality. 00999 * @ingroup non_mutating_algorithms 01000 * @param __first1 An input iterator. 01001 * @param __last1 An input iterator. 01002 * @param __first2 An input iterator. 01003 * @return A boolean true or false. 01004 * 01005 * This compares the elements of two ranges using @c == and returns true or 01006 * false depending on whether all of the corresponding elements of the 01007 * ranges are equal. 01008 */ 01009 template<typename _II1, typename _II2> 01010 inline bool 01011 equal(_II1 __first1, _II1 __last1, _II2 __first2) 01012 { 01013 // concept requirements 01014 __glibcxx_function_requires(_InputIteratorConcept<_II1>) 01015 __glibcxx_function_requires(_InputIteratorConcept<_II2>) 01016 __glibcxx_function_requires(_EqualOpConcept< 01017 typename iterator_traits<_II1>::value_type, 01018 typename iterator_traits<_II2>::value_type>) 01019 __glibcxx_requires_valid_range(__first1, __last1); 01020 01021 return std::__equal_aux(std::__niter_base(__first1), 01022 std::__niter_base(__last1), 01023 std::__niter_base(__first2)); 01024 } 01025 01026 /** 01027 * @brief Tests a range for element-wise equality. 01028 * @ingroup non_mutating_algorithms 01029 * @param __first1 An input iterator. 01030 * @param __last1 An input iterator. 01031 * @param __first2 An input iterator. 01032 * @param __binary_pred A binary predicate @link functors 01033 * functor@endlink. 01034 * @return A boolean true or false. 01035 * 01036 * This compares the elements of two ranges using the binary_pred 01037 * parameter, and returns true or 01038 * false depending on whether all of the corresponding elements of the 01039 * ranges are equal. 01040 */ 01041 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate> 01042 inline bool 01043 equal(_IIter1 __first1, _IIter1 __last1, 01044 _IIter2 __first2, _BinaryPredicate __binary_pred) 01045 { 01046 // concept requirements 01047 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>) 01048 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>) 01049 __glibcxx_requires_valid_range(__first1, __last1); 01050 01051 for (; __first1 != __last1; ++__first1, ++__first2) 01052 if (!bool(__binary_pred(*__first1, *__first2))) 01053 return false; 01054 return true; 01055 } 01056 01057 /** 01058 * @brief Performs @b dictionary comparison on ranges. 01059 * @ingroup sorting_algorithms 01060 * @param __first1 An input iterator. 01061 * @param __last1 An input iterator. 01062 * @param __first2 An input iterator. 01063 * @param __last2 An input iterator. 01064 * @return A boolean true or false. 01065 * 01066 * <em>Returns true if the sequence of elements defined by the range 01067 * [first1,last1) is lexicographically less than the sequence of elements 01068 * defined by the range [first2,last2). Returns false otherwise.</em> 01069 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers, 01070 * then this is an inline call to @c memcmp. 01071 */ 01072 template<typename _II1, typename _II2> 01073 inline bool 01074 lexicographical_compare(_II1 __first1, _II1 __last1, 01075 _II2 __first2, _II2 __last2) 01076 { 01077 // concept requirements 01078 typedef typename iterator_traits<_II1>::value_type _ValueType1; 01079 typedef typename iterator_traits<_II2>::value_type _ValueType2; 01080 __glibcxx_function_requires(_InputIteratorConcept<_II1>) 01081 __glibcxx_function_requires(_InputIteratorConcept<_II2>) 01082 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>) 01083 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>) 01084 __glibcxx_requires_valid_range(__first1, __last1); 01085 __glibcxx_requires_valid_range(__first2, __last2); 01086 01087 return std::__lexicographical_compare_aux(std::__niter_base(__first1), 01088 std::__niter_base(__last1), 01089 std::__niter_base(__first2), 01090 std::__niter_base(__last2)); 01091 } 01092 01093 /** 01094 * @brief Performs @b dictionary comparison on ranges. 01095 * @ingroup sorting_algorithms 01096 * @param __first1 An input iterator. 01097 * @param __last1 An input iterator. 01098 * @param __first2 An input iterator. 01099 * @param __last2 An input iterator. 01100 * @param __comp A @link comparison_functors comparison functor@endlink. 01101 * @return A boolean true or false. 01102 * 01103 * The same as the four-parameter @c lexicographical_compare, but uses the 01104 * comp parameter instead of @c <. 01105 */ 01106 template<typename _II1, typename _II2, typename _Compare> 01107 bool 01108 lexicographical_compare(_II1 __first1, _II1 __last1, 01109 _II2 __first2, _II2 __last2, _Compare __comp) 01110 { 01111 typedef typename iterator_traits<_II1>::iterator_category _Category1; 01112 typedef typename iterator_traits<_II2>::iterator_category _Category2; 01113 typedef std::__lc_rai<_Category1, _Category2> __rai_type; 01114 01115 // concept requirements 01116 __glibcxx_function_requires(_InputIteratorConcept<_II1>) 01117 __glibcxx_function_requires(_InputIteratorConcept<_II2>) 01118 __glibcxx_requires_valid_range(__first1, __last1); 01119 __glibcxx_requires_valid_range(__first2, __last2); 01120 01121 __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2); 01122 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2); 01123 ++__first1, ++__first2) 01124 { 01125 if (__comp(*__first1, *__first2)) 01126 return true; 01127 if (__comp(*__first2, *__first1)) 01128 return false; 01129 } 01130 return __first1 == __last1 && __first2 != __last2; 01131 } 01132 01133 /** 01134 * @brief Finds the places in ranges which don't match. 01135 * @ingroup non_mutating_algorithms 01136 * @param __first1 An input iterator. 01137 * @param __last1 An input iterator. 01138 * @param __first2 An input iterator. 01139 * @return A pair of iterators pointing to the first mismatch. 01140 * 01141 * This compares the elements of two ranges using @c == and returns a pair 01142 * of iterators. The first iterator points into the first range, the 01143 * second iterator points into the second range, and the elements pointed 01144 * to by the iterators are not equal. 01145 */ 01146 template<typename _InputIterator1, typename _InputIterator2> 01147 pair<_InputIterator1, _InputIterator2> 01148 mismatch(_InputIterator1 __first1, _InputIterator1 __last1, 01149 _InputIterator2 __first2) 01150 { 01151 // concept requirements 01152 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 01153 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 01154 __glibcxx_function_requires(_EqualOpConcept< 01155 typename iterator_traits<_InputIterator1>::value_type, 01156 typename iterator_traits<_InputIterator2>::value_type>) 01157 __glibcxx_requires_valid_range(__first1, __last1); 01158 01159 while (__first1 != __last1 && *__first1 == *__first2) 01160 { 01161 ++__first1; 01162 ++__first2; 01163 } 01164 return pair<_InputIterator1, _InputIterator2>(__first1, __first2); 01165 } 01166 01167 /** 01168 * @brief Finds the places in ranges which don't match. 01169 * @ingroup non_mutating_algorithms 01170 * @param __first1 An input iterator. 01171 * @param __last1 An input iterator. 01172 * @param __first2 An input iterator. 01173 * @param __binary_pred A binary predicate @link functors 01174 * functor@endlink. 01175 * @return A pair of iterators pointing to the first mismatch. 01176 * 01177 * This compares the elements of two ranges using the binary_pred 01178 * parameter, and returns a pair 01179 * of iterators. The first iterator points into the first range, the 01180 * second iterator points into the second range, and the elements pointed 01181 * to by the iterators are not equal. 01182 */ 01183 template<typename _InputIterator1, typename _InputIterator2, 01184 typename _BinaryPredicate> 01185 pair<_InputIterator1, _InputIterator2> 01186 mismatch(_InputIterator1 __first1, _InputIterator1 __last1, 01187 _InputIterator2 __first2, _BinaryPredicate __binary_pred) 01188 { 01189 // concept requirements 01190 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 01191 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 01192 __glibcxx_requires_valid_range(__first1, __last1); 01193 01194 while (__first1 != __last1 && bool(__binary_pred(*__first1, *__first2))) 01195 { 01196 ++__first1; 01197 ++__first2; 01198 } 01199 return pair<_InputIterator1, _InputIterator2>(__first1, __first2); 01200 } 01201 01202 _GLIBCXX_END_NAMESPACE_ALGO 01203 } // namespace std 01204 01205 // NB: This file is included within many other C++ includes, as a way 01206 // of getting the base algorithms. So, make sure that parallel bits 01207 // come in too if requested. 01208 #ifdef _GLIBCXX_PARALLEL 01209 # include <parallel/algobase.h> 01210 #endif 01211 01212 #endif