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