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