|
libstdc++
|
00001 // Core algorithmic facilities -*- C++ -*- 00002 00003 // Copyright (C) 2001-2015 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 #include <bits/predefined_ops.h> 00072 00073 namespace std _GLIBCXX_VISIBILITY(default) 00074 { 00075 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00076 00077 #if __cplusplus < 201103L 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 #if __cplusplus < 201103L 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 _GLIBCXX14_CONSTEXPR 00194 inline const _Tp& 00195 min(const _Tp& __a, const _Tp& __b) 00196 { 00197 // concept requirements 00198 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>) 00199 //return __b < __a ? __b : __a; 00200 if (__b < __a) 00201 return __b; 00202 return __a; 00203 } 00204 00205 /** 00206 * @brief This does what you think it does. 00207 * @ingroup sorting_algorithms 00208 * @param __a A thing of arbitrary type. 00209 * @param __b Another thing of arbitrary type. 00210 * @return The greater of the parameters. 00211 * 00212 * This is the simple classic generic implementation. It will work on 00213 * temporary expressions, since they are only evaluated once, unlike a 00214 * preprocessor macro. 00215 */ 00216 template<typename _Tp> 00217 _GLIBCXX14_CONSTEXPR 00218 inline const _Tp& 00219 max(const _Tp& __a, const _Tp& __b) 00220 { 00221 // concept requirements 00222 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>) 00223 //return __a < __b ? __b : __a; 00224 if (__a < __b) 00225 return __b; 00226 return __a; 00227 } 00228 00229 /** 00230 * @brief This does what you think it does. 00231 * @ingroup sorting_algorithms 00232 * @param __a A thing of arbitrary type. 00233 * @param __b Another thing of arbitrary type. 00234 * @param __comp A @link comparison_functors comparison functor@endlink. 00235 * @return The lesser of the parameters. 00236 * 00237 * This will work on temporary expressions, since they are only evaluated 00238 * once, unlike a preprocessor macro. 00239 */ 00240 template<typename _Tp, typename _Compare> 00241 _GLIBCXX14_CONSTEXPR 00242 inline const _Tp& 00243 min(const _Tp& __a, const _Tp& __b, _Compare __comp) 00244 { 00245 //return __comp(__b, __a) ? __b : __a; 00246 if (__comp(__b, __a)) 00247 return __b; 00248 return __a; 00249 } 00250 00251 /** 00252 * @brief This does what you think it does. 00253 * @ingroup sorting_algorithms 00254 * @param __a A thing of arbitrary type. 00255 * @param __b Another thing of arbitrary type. 00256 * @param __comp A @link comparison_functors comparison functor@endlink. 00257 * @return The greater of the parameters. 00258 * 00259 * This will work on temporary expressions, since they are only evaluated 00260 * once, unlike a preprocessor macro. 00261 */ 00262 template<typename _Tp, typename _Compare> 00263 _GLIBCXX14_CONSTEXPR 00264 inline const _Tp& 00265 max(const _Tp& __a, const _Tp& __b, _Compare __comp) 00266 { 00267 //return __comp(__a, __b) ? __b : __a; 00268 if (__comp(__a, __b)) 00269 return __b; 00270 return __a; 00271 } 00272 00273 // If _Iterator is a __normal_iterator return its base (a plain pointer, 00274 // normally) otherwise return it untouched. See copy, fill, ... 00275 template<typename _Iterator> 00276 struct _Niter_base 00277 : _Iter_base<_Iterator, __is_normal_iterator<_Iterator>::__value> 00278 { }; 00279 00280 template<typename _Iterator> 00281 inline typename _Niter_base<_Iterator>::iterator_type 00282 __niter_base(_Iterator __it) 00283 { return std::_Niter_base<_Iterator>::_S_base(__it); } 00284 00285 // Likewise, for move_iterator. 00286 template<typename _Iterator> 00287 struct _Miter_base 00288 : _Iter_base<_Iterator, __is_move_iterator<_Iterator>::__value> 00289 { }; 00290 00291 template<typename _Iterator> 00292 inline typename _Miter_base<_Iterator>::iterator_type 00293 __miter_base(_Iterator __it) 00294 { return std::_Miter_base<_Iterator>::_S_base(__it); } 00295 00296 // All of these auxiliary structs serve two purposes. (1) Replace 00297 // calls to copy with memmove whenever possible. (Memmove, not memcpy, 00298 // because the input and output ranges are permitted to overlap.) 00299 // (2) If we're using random access iterators, then write the loop as 00300 // a for loop with an explicit count. 00301 00302 template<bool, bool, typename> 00303 struct __copy_move 00304 { 00305 template<typename _II, typename _OI> 00306 static _OI 00307 __copy_m(_II __first, _II __last, _OI __result) 00308 { 00309 for (; __first != __last; ++__result, ++__first) 00310 *__result = *__first; 00311 return __result; 00312 } 00313 }; 00314 00315 #if __cplusplus >= 201103L 00316 template<typename _Category> 00317 struct __copy_move<true, false, _Category> 00318 { 00319 template<typename _II, typename _OI> 00320 static _OI 00321 __copy_m(_II __first, _II __last, _OI __result) 00322 { 00323 for (; __first != __last; ++__result, ++__first) 00324 *__result = std::move(*__first); 00325 return __result; 00326 } 00327 }; 00328 #endif 00329 00330 template<> 00331 struct __copy_move<false, false, random_access_iterator_tag> 00332 { 00333 template<typename _II, typename _OI> 00334 static _OI 00335 __copy_m(_II __first, _II __last, _OI __result) 00336 { 00337 typedef typename iterator_traits<_II>::difference_type _Distance; 00338 for(_Distance __n = __last - __first; __n > 0; --__n) 00339 { 00340 *__result = *__first; 00341 ++__first; 00342 ++__result; 00343 } 00344 return __result; 00345 } 00346 }; 00347 00348 #if __cplusplus >= 201103L 00349 template<> 00350 struct __copy_move<true, false, random_access_iterator_tag> 00351 { 00352 template<typename _II, typename _OI> 00353 static _OI 00354 __copy_m(_II __first, _II __last, _OI __result) 00355 { 00356 typedef typename iterator_traits<_II>::difference_type _Distance; 00357 for(_Distance __n = __last - __first; __n > 0; --__n) 00358 { 00359 *__result = std::move(*__first); 00360 ++__first; 00361 ++__result; 00362 } 00363 return __result; 00364 } 00365 }; 00366 #endif 00367 00368 template<bool _IsMove> 00369 struct __copy_move<_IsMove, true, random_access_iterator_tag> 00370 { 00371 template<typename _Tp> 00372 static _Tp* 00373 __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result) 00374 { 00375 #if __cplusplus >= 201103L 00376 // trivial types can have deleted assignment 00377 static_assert( is_copy_assignable<_Tp>::value, 00378 "type is not assignable" ); 00379 #endif 00380 const ptrdiff_t _Num = __last - __first; 00381 if (_Num) 00382 __builtin_memmove(__result, __first, sizeof(_Tp) * _Num); 00383 return __result + _Num; 00384 } 00385 }; 00386 00387 template<bool _IsMove, typename _II, typename _OI> 00388 inline _OI 00389 __copy_move_a(_II __first, _II __last, _OI __result) 00390 { 00391 typedef typename iterator_traits<_II>::value_type _ValueTypeI; 00392 typedef typename iterator_traits<_OI>::value_type _ValueTypeO; 00393 typedef typename iterator_traits<_II>::iterator_category _Category; 00394 const bool __simple = (__is_trivial(_ValueTypeI) 00395 && __is_pointer<_II>::__value 00396 && __is_pointer<_OI>::__value 00397 && __are_same<_ValueTypeI, _ValueTypeO>::__value); 00398 00399 return std::__copy_move<_IsMove, __simple, 00400 _Category>::__copy_m(__first, __last, __result); 00401 } 00402 00403 // Helpers for streambuf iterators (either istream or ostream). 00404 // NB: avoid including <iosfwd>, relatively large. 00405 template<typename _CharT> 00406 struct char_traits; 00407 00408 template<typename _CharT, typename _Traits> 00409 class istreambuf_iterator; 00410 00411 template<typename _CharT, typename _Traits> 00412 class ostreambuf_iterator; 00413 00414 template<bool _IsMove, typename _CharT> 00415 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, 00416 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type 00417 __copy_move_a2(_CharT*, _CharT*, 00418 ostreambuf_iterator<_CharT, char_traits<_CharT> >); 00419 00420 template<bool _IsMove, typename _CharT> 00421 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, 00422 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type 00423 __copy_move_a2(const _CharT*, const _CharT*, 00424 ostreambuf_iterator<_CharT, char_traits<_CharT> >); 00425 00426 template<bool _IsMove, typename _CharT> 00427 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, 00428 _CharT*>::__type 00429 __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >, 00430 istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*); 00431 00432 template<bool _IsMove, typename _II, typename _OI> 00433 inline _OI 00434 __copy_move_a2(_II __first, _II __last, _OI __result) 00435 { 00436 return _OI(std::__copy_move_a<_IsMove>(std::__niter_base(__first), 00437 std::__niter_base(__last), 00438 std::__niter_base(__result))); 00439 } 00440 00441 /** 00442 * @brief Copies the range [first,last) into result. 00443 * @ingroup mutating_algorithms 00444 * @param __first An input iterator. 00445 * @param __last An input iterator. 00446 * @param __result An output iterator. 00447 * @return result + (first - last) 00448 * 00449 * This inline function will boil down to a call to @c memmove whenever 00450 * possible. Failing that, if random access iterators are passed, then the 00451 * loop count will be known (and therefore a candidate for compiler 00452 * optimizations such as unrolling). Result may not be contained within 00453 * [first,last); the copy_backward function should be used instead. 00454 * 00455 * Note that the end of the output range is permitted to be contained 00456 * within [first,last). 00457 */ 00458 template<typename _II, typename _OI> 00459 inline _OI 00460 copy(_II __first, _II __last, _OI __result) 00461 { 00462 // concept requirements 00463 __glibcxx_function_requires(_InputIteratorConcept<_II>) 00464 __glibcxx_function_requires(_OutputIteratorConcept<_OI, 00465 typename iterator_traits<_II>::value_type>) 00466 __glibcxx_requires_valid_range(__first, __last); 00467 00468 return (std::__copy_move_a2<__is_move_iterator<_II>::__value> 00469 (std::__miter_base(__first), std::__miter_base(__last), 00470 __result)); 00471 } 00472 00473 #if __cplusplus >= 201103L 00474 /** 00475 * @brief Moves the range [first,last) into result. 00476 * @ingroup mutating_algorithms 00477 * @param __first An input iterator. 00478 * @param __last An input iterator. 00479 * @param __result An output iterator. 00480 * @return result + (first - last) 00481 * 00482 * This inline function will boil down to a call to @c memmove whenever 00483 * possible. Failing that, if random access iterators are passed, then the 00484 * loop count will be known (and therefore a candidate for compiler 00485 * optimizations such as unrolling). Result may not be contained within 00486 * [first,last); the move_backward function should be used instead. 00487 * 00488 * Note that the end of the output range is permitted to be contained 00489 * within [first,last). 00490 */ 00491 template<typename _II, typename _OI> 00492 inline _OI 00493 move(_II __first, _II __last, _OI __result) 00494 { 00495 // concept requirements 00496 __glibcxx_function_requires(_InputIteratorConcept<_II>) 00497 __glibcxx_function_requires(_OutputIteratorConcept<_OI, 00498 typename iterator_traits<_II>::value_type>) 00499 __glibcxx_requires_valid_range(__first, __last); 00500 00501 return std::__copy_move_a2<true>(std::__miter_base(__first), 00502 std::__miter_base(__last), __result); 00503 } 00504 00505 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp) 00506 #else 00507 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp) 00508 #endif 00509 00510 template<bool, bool, typename> 00511 struct __copy_move_backward 00512 { 00513 template<typename _BI1, typename _BI2> 00514 static _BI2 00515 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result) 00516 { 00517 while (__first != __last) 00518 *--__result = *--__last; 00519 return __result; 00520 } 00521 }; 00522 00523 #if __cplusplus >= 201103L 00524 template<typename _Category> 00525 struct __copy_move_backward<true, false, _Category> 00526 { 00527 template<typename _BI1, typename _BI2> 00528 static _BI2 00529 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result) 00530 { 00531 while (__first != __last) 00532 *--__result = std::move(*--__last); 00533 return __result; 00534 } 00535 }; 00536 #endif 00537 00538 template<> 00539 struct __copy_move_backward<false, false, random_access_iterator_tag> 00540 { 00541 template<typename _BI1, typename _BI2> 00542 static _BI2 00543 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result) 00544 { 00545 typename iterator_traits<_BI1>::difference_type __n; 00546 for (__n = __last - __first; __n > 0; --__n) 00547 *--__result = *--__last; 00548 return __result; 00549 } 00550 }; 00551 00552 #if __cplusplus >= 201103L 00553 template<> 00554 struct __copy_move_backward<true, false, random_access_iterator_tag> 00555 { 00556 template<typename _BI1, typename _BI2> 00557 static _BI2 00558 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result) 00559 { 00560 typename iterator_traits<_BI1>::difference_type __n; 00561 for (__n = __last - __first; __n > 0; --__n) 00562 *--__result = std::move(*--__last); 00563 return __result; 00564 } 00565 }; 00566 #endif 00567 00568 template<bool _IsMove> 00569 struct __copy_move_backward<_IsMove, true, random_access_iterator_tag> 00570 { 00571 template<typename _Tp> 00572 static _Tp* 00573 __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result) 00574 { 00575 #if __cplusplus >= 201103L 00576 // trivial types can have deleted assignment 00577 static_assert( is_copy_assignable<_Tp>::value, 00578 "type is not assignable" ); 00579 #endif 00580 const ptrdiff_t _Num = __last - __first; 00581 if (_Num) 00582 __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num); 00583 return __result - _Num; 00584 } 00585 }; 00586 00587 template<bool _IsMove, typename _BI1, typename _BI2> 00588 inline _BI2 00589 __copy_move_backward_a(_BI1 __first, _BI1 __last, _BI2 __result) 00590 { 00591 typedef typename iterator_traits<_BI1>::value_type _ValueType1; 00592 typedef typename iterator_traits<_BI2>::value_type _ValueType2; 00593 typedef typename iterator_traits<_BI1>::iterator_category _Category; 00594 const bool __simple = (__is_trivial(_ValueType1) 00595 && __is_pointer<_BI1>::__value 00596 && __is_pointer<_BI2>::__value 00597 && __are_same<_ValueType1, _ValueType2>::__value); 00598 00599 return std::__copy_move_backward<_IsMove, __simple, 00600 _Category>::__copy_move_b(__first, 00601 __last, 00602 __result); 00603 } 00604 00605 template<bool _IsMove, typename _BI1, typename _BI2> 00606 inline _BI2 00607 __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result) 00608 { 00609 return _BI2(std::__copy_move_backward_a<_IsMove> 00610 (std::__niter_base(__first), std::__niter_base(__last), 00611 std::__niter_base(__result))); 00612 } 00613 00614 /** 00615 * @brief Copies the range [first,last) into result. 00616 * @ingroup mutating_algorithms 00617 * @param __first A bidirectional iterator. 00618 * @param __last A bidirectional iterator. 00619 * @param __result A bidirectional iterator. 00620 * @return result - (first - last) 00621 * 00622 * The function has the same effect as copy, but starts at the end of the 00623 * range and works its way to the start, returning the start of the result. 00624 * This inline function will boil down to a call to @c memmove whenever 00625 * possible. Failing that, if random access iterators are passed, then the 00626 * loop count will be known (and therefore a candidate for compiler 00627 * optimizations such as unrolling). 00628 * 00629 * Result may not be in the range (first,last]. Use copy instead. Note 00630 * that the start of the output range may overlap [first,last). 00631 */ 00632 template<typename _BI1, typename _BI2> 00633 inline _BI2 00634 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result) 00635 { 00636 // concept requirements 00637 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>) 00638 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>) 00639 __glibcxx_function_requires(_ConvertibleConcept< 00640 typename iterator_traits<_BI1>::value_type, 00641 typename iterator_traits<_BI2>::value_type>) 00642 __glibcxx_requires_valid_range(__first, __last); 00643 00644 return (std::__copy_move_backward_a2<__is_move_iterator<_BI1>::__value> 00645 (std::__miter_base(__first), std::__miter_base(__last), 00646 __result)); 00647 } 00648 00649 #if __cplusplus >= 201103L 00650 /** 00651 * @brief Moves the range [first,last) into result. 00652 * @ingroup mutating_algorithms 00653 * @param __first A bidirectional iterator. 00654 * @param __last A bidirectional iterator. 00655 * @param __result A bidirectional iterator. 00656 * @return result - (first - last) 00657 * 00658 * The function has the same effect as move, but starts at the end of the 00659 * range and works its way to the start, returning the start of the result. 00660 * This inline function will boil down to a call to @c memmove whenever 00661 * possible. Failing that, if random access iterators are passed, then the 00662 * loop count will be known (and therefore a candidate for compiler 00663 * optimizations such as unrolling). 00664 * 00665 * Result may not be in the range (first,last]. Use move instead. Note 00666 * that the start of the output range may overlap [first,last). 00667 */ 00668 template<typename _BI1, typename _BI2> 00669 inline _BI2 00670 move_backward(_BI1 __first, _BI1 __last, _BI2 __result) 00671 { 00672 // concept requirements 00673 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>) 00674 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>) 00675 __glibcxx_function_requires(_ConvertibleConcept< 00676 typename iterator_traits<_BI1>::value_type, 00677 typename iterator_traits<_BI2>::value_type>) 00678 __glibcxx_requires_valid_range(__first, __last); 00679 00680 return std::__copy_move_backward_a2<true>(std::__miter_base(__first), 00681 std::__miter_base(__last), 00682 __result); 00683 } 00684 00685 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp) 00686 #else 00687 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp) 00688 #endif 00689 00690 template<typename _ForwardIterator, typename _Tp> 00691 inline typename 00692 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type 00693 __fill_a(_ForwardIterator __first, _ForwardIterator __last, 00694 const _Tp& __value) 00695 { 00696 for (; __first != __last; ++__first) 00697 *__first = __value; 00698 } 00699 00700 template<typename _ForwardIterator, typename _Tp> 00701 inline typename 00702 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type 00703 __fill_a(_ForwardIterator __first, _ForwardIterator __last, 00704 const _Tp& __value) 00705 { 00706 const _Tp __tmp = __value; 00707 for (; __first != __last; ++__first) 00708 *__first = __tmp; 00709 } 00710 00711 // Specialization: for char types we can use memset. 00712 template<typename _Tp> 00713 inline typename 00714 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type 00715 __fill_a(_Tp* __first, _Tp* __last, const _Tp& __c) 00716 { 00717 const _Tp __tmp = __c; 00718 __builtin_memset(__first, static_cast<unsigned char>(__tmp), 00719 __last - __first); 00720 } 00721 00722 /** 00723 * @brief Fills the range [first,last) with copies of value. 00724 * @ingroup mutating_algorithms 00725 * @param __first A forward iterator. 00726 * @param __last A forward iterator. 00727 * @param __value A reference-to-const of arbitrary type. 00728 * @return Nothing. 00729 * 00730 * This function fills a range with copies of the same value. For char 00731 * types filling contiguous areas of memory, this becomes an inline call 00732 * to @c memset or @c wmemset. 00733 */ 00734 template<typename _ForwardIterator, typename _Tp> 00735 inline void 00736 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) 00737 { 00738 // concept requirements 00739 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 00740 _ForwardIterator>) 00741 __glibcxx_requires_valid_range(__first, __last); 00742 00743 std::__fill_a(std::__niter_base(__first), std::__niter_base(__last), 00744 __value); 00745 } 00746 00747 template<typename _OutputIterator, typename _Size, typename _Tp> 00748 inline typename 00749 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type 00750 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value) 00751 { 00752 for (__decltype(__n + 0) __niter = __n; 00753 __niter > 0; --__niter, ++__first) 00754 *__first = __value; 00755 return __first; 00756 } 00757 00758 template<typename _OutputIterator, typename _Size, typename _Tp> 00759 inline typename 00760 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type 00761 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value) 00762 { 00763 const _Tp __tmp = __value; 00764 for (__decltype(__n + 0) __niter = __n; 00765 __niter > 0; --__niter, ++__first) 00766 *__first = __tmp; 00767 return __first; 00768 } 00769 00770 template<typename _Size, typename _Tp> 00771 inline typename 00772 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, _Tp*>::__type 00773 __fill_n_a(_Tp* __first, _Size __n, const _Tp& __c) 00774 { 00775 std::__fill_a(__first, __first + __n, __c); 00776 return __first + __n; 00777 } 00778 00779 /** 00780 * @brief Fills the range [first,first+n) with copies of value. 00781 * @ingroup mutating_algorithms 00782 * @param __first An output iterator. 00783 * @param __n The count of copies to perform. 00784 * @param __value A reference-to-const of arbitrary type. 00785 * @return The iterator at first+n. 00786 * 00787 * This function fills a range with copies of the same value. For char 00788 * types filling contiguous areas of memory, this becomes an inline call 00789 * to @c memset or @ wmemset. 00790 * 00791 * _GLIBCXX_RESOLVE_LIB_DEFECTS 00792 * DR 865. More algorithms that throw away information 00793 */ 00794 template<typename _OI, typename _Size, typename _Tp> 00795 inline _OI 00796 fill_n(_OI __first, _Size __n, const _Tp& __value) 00797 { 00798 // concept requirements 00799 __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>) 00800 00801 return _OI(std::__fill_n_a(std::__niter_base(__first), __n, __value)); 00802 } 00803 00804 template<bool _BoolType> 00805 struct __equal 00806 { 00807 template<typename _II1, typename _II2> 00808 static bool 00809 equal(_II1 __first1, _II1 __last1, _II2 __first2) 00810 { 00811 for (; __first1 != __last1; ++__first1, ++__first2) 00812 if (!(*__first1 == *__first2)) 00813 return false; 00814 return true; 00815 } 00816 }; 00817 00818 template<> 00819 struct __equal<true> 00820 { 00821 template<typename _Tp> 00822 static bool 00823 equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2) 00824 { 00825 return !__builtin_memcmp(__first1, __first2, sizeof(_Tp) 00826 * (__last1 - __first1)); 00827 } 00828 }; 00829 00830 template<typename _II1, typename _II2> 00831 inline bool 00832 __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2) 00833 { 00834 typedef typename iterator_traits<_II1>::value_type _ValueType1; 00835 typedef typename iterator_traits<_II2>::value_type _ValueType2; 00836 const bool __simple = ((__is_integer<_ValueType1>::__value 00837 || __is_pointer<_ValueType1>::__value) 00838 && __is_pointer<_II1>::__value 00839 && __is_pointer<_II2>::__value 00840 && __are_same<_ValueType1, _ValueType2>::__value); 00841 00842 return std::__equal<__simple>::equal(__first1, __last1, __first2); 00843 } 00844 00845 template<typename, typename> 00846 struct __lc_rai 00847 { 00848 template<typename _II1, typename _II2> 00849 static _II1 00850 __newlast1(_II1, _II1 __last1, _II2, _II2) 00851 { return __last1; } 00852 00853 template<typename _II> 00854 static bool 00855 __cnd2(_II __first, _II __last) 00856 { return __first != __last; } 00857 }; 00858 00859 template<> 00860 struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag> 00861 { 00862 template<typename _RAI1, typename _RAI2> 00863 static _RAI1 00864 __newlast1(_RAI1 __first1, _RAI1 __last1, 00865 _RAI2 __first2, _RAI2 __last2) 00866 { 00867 const typename iterator_traits<_RAI1>::difference_type 00868 __diff1 = __last1 - __first1; 00869 const typename iterator_traits<_RAI2>::difference_type 00870 __diff2 = __last2 - __first2; 00871 return __diff2 < __diff1 ? __first1 + __diff2 : __last1; 00872 } 00873 00874 template<typename _RAI> 00875 static bool 00876 __cnd2(_RAI, _RAI) 00877 { return true; } 00878 }; 00879 00880 template<typename _II1, typename _II2, typename _Compare> 00881 bool 00882 __lexicographical_compare_impl(_II1 __first1, _II1 __last1, 00883 _II2 __first2, _II2 __last2, 00884 _Compare __comp) 00885 { 00886 typedef typename iterator_traits<_II1>::iterator_category _Category1; 00887 typedef typename iterator_traits<_II2>::iterator_category _Category2; 00888 typedef std::__lc_rai<_Category1, _Category2> __rai_type; 00889 00890 __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2); 00891 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2); 00892 ++__first1, ++__first2) 00893 { 00894 if (__comp(__first1, __first2)) 00895 return true; 00896 if (__comp(__first2, __first1)) 00897 return false; 00898 } 00899 return __first1 == __last1 && __first2 != __last2; 00900 } 00901 00902 template<bool _BoolType> 00903 struct __lexicographical_compare 00904 { 00905 template<typename _II1, typename _II2> 00906 static bool __lc(_II1, _II1, _II2, _II2); 00907 }; 00908 00909 template<bool _BoolType> 00910 template<typename _II1, typename _II2> 00911 bool 00912 __lexicographical_compare<_BoolType>:: 00913 __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2) 00914 { 00915 return std::__lexicographical_compare_impl(__first1, __last1, 00916 __first2, __last2, 00917 __gnu_cxx::__ops::__iter_less_iter()); 00918 } 00919 00920 template<> 00921 struct __lexicographical_compare<true> 00922 { 00923 template<typename _Tp, typename _Up> 00924 static bool 00925 __lc(const _Tp* __first1, const _Tp* __last1, 00926 const _Up* __first2, const _Up* __last2) 00927 { 00928 const size_t __len1 = __last1 - __first1; 00929 const size_t __len2 = __last2 - __first2; 00930 const int __result = __builtin_memcmp(__first1, __first2, 00931 std::min(__len1, __len2)); 00932 return __result != 0 ? __result < 0 : __len1 < __len2; 00933 } 00934 }; 00935 00936 template<typename _II1, typename _II2> 00937 inline bool 00938 __lexicographical_compare_aux(_II1 __first1, _II1 __last1, 00939 _II2 __first2, _II2 __last2) 00940 { 00941 typedef typename iterator_traits<_II1>::value_type _ValueType1; 00942 typedef typename iterator_traits<_II2>::value_type _ValueType2; 00943 const bool __simple = 00944 (__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value 00945 && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed 00946 && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed 00947 && __is_pointer<_II1>::__value 00948 && __is_pointer<_II2>::__value); 00949 00950 return std::__lexicographical_compare<__simple>::__lc(__first1, __last1, 00951 __first2, __last2); 00952 } 00953 00954 template<typename _ForwardIterator, typename _Tp, typename _Compare> 00955 _ForwardIterator 00956 __lower_bound(_ForwardIterator __first, _ForwardIterator __last, 00957 const _Tp& __val, _Compare __comp) 00958 { 00959 typedef typename iterator_traits<_ForwardIterator>::difference_type 00960 _DistanceType; 00961 00962 _DistanceType __len = std::distance(__first, __last); 00963 00964 while (__len > 0) 00965 { 00966 _DistanceType __half = __len >> 1; 00967 _ForwardIterator __middle = __first; 00968 std::advance(__middle, __half); 00969 if (__comp(__middle, __val)) 00970 { 00971 __first = __middle; 00972 ++__first; 00973 __len = __len - __half - 1; 00974 } 00975 else 00976 __len = __half; 00977 } 00978 return __first; 00979 } 00980 00981 /** 00982 * @brief Finds the first position in which @a val could be inserted 00983 * without changing the ordering. 00984 * @param __first An iterator. 00985 * @param __last Another iterator. 00986 * @param __val The search term. 00987 * @return An iterator pointing to the first element <em>not less 00988 * than</em> @a val, or end() if every element is less than 00989 * @a val. 00990 * @ingroup binary_search_algorithms 00991 */ 00992 template<typename _ForwardIterator, typename _Tp> 00993 inline _ForwardIterator 00994 lower_bound(_ForwardIterator __first, _ForwardIterator __last, 00995 const _Tp& __val) 00996 { 00997 // concept requirements 00998 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 00999 __glibcxx_function_requires(_LessThanOpConcept< 01000 typename iterator_traits<_ForwardIterator>::value_type, _Tp>) 01001 __glibcxx_requires_partitioned_lower(__first, __last, __val); 01002 01003 return std::__lower_bound(__first, __last, __val, 01004 __gnu_cxx::__ops::__iter_less_val()); 01005 } 01006 01007 /// This is a helper function for the sort routines and for random.tcc. 01008 // Precondition: __n > 0. 01009 inline _GLIBCXX_CONSTEXPR int 01010 __lg(int __n) 01011 { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); } 01012 01013 inline _GLIBCXX_CONSTEXPR unsigned 01014 __lg(unsigned __n) 01015 { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); } 01016 01017 inline _GLIBCXX_CONSTEXPR long 01018 __lg(long __n) 01019 { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); } 01020 01021 inline _GLIBCXX_CONSTEXPR unsigned long 01022 __lg(unsigned long __n) 01023 { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); } 01024 01025 inline _GLIBCXX_CONSTEXPR long long 01026 __lg(long long __n) 01027 { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); } 01028 01029 inline _GLIBCXX_CONSTEXPR unsigned long long 01030 __lg(unsigned long long __n) 01031 { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); } 01032 01033 _GLIBCXX_END_NAMESPACE_VERSION 01034 01035 _GLIBCXX_BEGIN_NAMESPACE_ALGO 01036 01037 /** 01038 * @brief Tests a range for element-wise equality. 01039 * @ingroup non_mutating_algorithms 01040 * @param __first1 An input iterator. 01041 * @param __last1 An input iterator. 01042 * @param __first2 An input iterator. 01043 * @return A boolean true or false. 01044 * 01045 * This compares the elements of two ranges using @c == and returns true or 01046 * false depending on whether all of the corresponding elements of the 01047 * ranges are equal. 01048 */ 01049 template<typename _II1, typename _II2> 01050 inline bool 01051 equal(_II1 __first1, _II1 __last1, _II2 __first2) 01052 { 01053 // concept requirements 01054 __glibcxx_function_requires(_InputIteratorConcept<_II1>) 01055 __glibcxx_function_requires(_InputIteratorConcept<_II2>) 01056 __glibcxx_function_requires(_EqualOpConcept< 01057 typename iterator_traits<_II1>::value_type, 01058 typename iterator_traits<_II2>::value_type>) 01059 __glibcxx_requires_valid_range(__first1, __last1); 01060 01061 return std::__equal_aux(std::__niter_base(__first1), 01062 std::__niter_base(__last1), 01063 std::__niter_base(__first2)); 01064 } 01065 01066 /** 01067 * @brief Tests a range for element-wise equality. 01068 * @ingroup non_mutating_algorithms 01069 * @param __first1 An input iterator. 01070 * @param __last1 An input iterator. 01071 * @param __first2 An input iterator. 01072 * @param __binary_pred A binary predicate @link functors 01073 * functor@endlink. 01074 * @return A boolean true or false. 01075 * 01076 * This compares the elements of two ranges using the binary_pred 01077 * parameter, and returns true or 01078 * false depending on whether all of the corresponding elements of the 01079 * ranges are equal. 01080 */ 01081 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate> 01082 inline bool 01083 equal(_IIter1 __first1, _IIter1 __last1, 01084 _IIter2 __first2, _BinaryPredicate __binary_pred) 01085 { 01086 // concept requirements 01087 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>) 01088 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>) 01089 __glibcxx_requires_valid_range(__first1, __last1); 01090 01091 for (; __first1 != __last1; ++__first1, ++__first2) 01092 if (!bool(__binary_pred(*__first1, *__first2))) 01093 return false; 01094 return true; 01095 } 01096 01097 #if __cplusplus > 201103L 01098 01099 #define __cpp_lib_robust_nonmodifying_seq_ops 201304 01100 01101 /** 01102 * @brief Tests a range for element-wise equality. 01103 * @ingroup non_mutating_algorithms 01104 * @param __first1 An input iterator. 01105 * @param __last1 An input iterator. 01106 * @param __first2 An input iterator. 01107 * @param __last2 An input iterator. 01108 * @return A boolean true or false. 01109 * 01110 * This compares the elements of two ranges using @c == and returns true or 01111 * false depending on whether all of the corresponding elements of the 01112 * ranges are equal. 01113 */ 01114 template<typename _II1, typename _II2> 01115 inline bool 01116 equal(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2) 01117 { 01118 // concept requirements 01119 __glibcxx_function_requires(_InputIteratorConcept<_II1>) 01120 __glibcxx_function_requires(_InputIteratorConcept<_II2>) 01121 __glibcxx_function_requires(_EqualOpConcept< 01122 typename iterator_traits<_II1>::value_type, 01123 typename iterator_traits<_II2>::value_type>) 01124 __glibcxx_requires_valid_range(__first1, __last1); 01125 __glibcxx_requires_valid_range(__first2, __last2); 01126 01127 using _RATag = random_access_iterator_tag; 01128 using _Cat1 = typename iterator_traits<_II1>::iterator_category; 01129 using _Cat2 = typename iterator_traits<_II2>::iterator_category; 01130 using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>; 01131 if (_RAIters()) 01132 { 01133 auto __d1 = std::distance(__first1, __last1); 01134 auto __d2 = std::distance(__first2, __last2); 01135 if (__d1 != __d2) 01136 return false; 01137 return _GLIBCXX_STD_A::equal(__first1, __last1, __first2); 01138 } 01139 01140 for (; __first1 != __last1 && __first2 != __last2; ++__first1, ++__first2) 01141 if (!(*__first1 == *__first2)) 01142 return false; 01143 return __first1 == __last1 && __first2 == __last2; 01144 } 01145 01146 /** 01147 * @brief Tests a range for element-wise equality. 01148 * @ingroup non_mutating_algorithms 01149 * @param __first1 An input iterator. 01150 * @param __last1 An input iterator. 01151 * @param __first2 An input iterator. 01152 * @param __last2 An input iterator. 01153 * @param __binary_pred A binary predicate @link functors 01154 * functor@endlink. 01155 * @return A boolean true or false. 01156 * 01157 * This compares the elements of two ranges using the binary_pred 01158 * parameter, and returns true or 01159 * false depending on whether all of the corresponding elements of the 01160 * ranges are equal. 01161 */ 01162 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate> 01163 inline bool 01164 equal(_IIter1 __first1, _IIter1 __last1, 01165 _IIter2 __first2, _IIter2 __last2, _BinaryPredicate __binary_pred) 01166 { 01167 // concept requirements 01168 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>) 01169 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>) 01170 __glibcxx_requires_valid_range(__first1, __last1); 01171 __glibcxx_requires_valid_range(__first2, __last2); 01172 01173 using _RATag = random_access_iterator_tag; 01174 using _Cat1 = typename iterator_traits<_IIter1>::iterator_category; 01175 using _Cat2 = typename iterator_traits<_IIter2>::iterator_category; 01176 using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>; 01177 if (_RAIters()) 01178 { 01179 auto __d1 = std::distance(__first1, __last1); 01180 auto __d2 = std::distance(__first2, __last2); 01181 if (__d1 != __d2) 01182 return false; 01183 return _GLIBCXX_STD_A::equal(__first1, __last1, __first2, 01184 __binary_pred); 01185 } 01186 01187 for (; __first1 != __last1 && __first2 != __last2; ++__first1, ++__first2) 01188 if (!bool(__binary_pred(*__first1, *__first2))) 01189 return false; 01190 return __first1 == __last1 && __first2 == __last2; 01191 } 01192 #endif 01193 01194 /** 01195 * @brief Performs @b dictionary comparison on ranges. 01196 * @ingroup sorting_algorithms 01197 * @param __first1 An input iterator. 01198 * @param __last1 An input iterator. 01199 * @param __first2 An input iterator. 01200 * @param __last2 An input iterator. 01201 * @return A boolean true or false. 01202 * 01203 * <em>Returns true if the sequence of elements defined by the range 01204 * [first1,last1) is lexicographically less than the sequence of elements 01205 * defined by the range [first2,last2). Returns false otherwise.</em> 01206 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers, 01207 * then this is an inline call to @c memcmp. 01208 */ 01209 template<typename _II1, typename _II2> 01210 inline bool 01211 lexicographical_compare(_II1 __first1, _II1 __last1, 01212 _II2 __first2, _II2 __last2) 01213 { 01214 #ifdef _GLIBCXX_CONCEPT_CHECKS 01215 // concept requirements 01216 typedef typename iterator_traits<_II1>::value_type _ValueType1; 01217 typedef typename iterator_traits<_II2>::value_type _ValueType2; 01218 #endif 01219 __glibcxx_function_requires(_InputIteratorConcept<_II1>) 01220 __glibcxx_function_requires(_InputIteratorConcept<_II2>) 01221 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>) 01222 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>) 01223 __glibcxx_requires_valid_range(__first1, __last1); 01224 __glibcxx_requires_valid_range(__first2, __last2); 01225 01226 return std::__lexicographical_compare_aux(std::__niter_base(__first1), 01227 std::__niter_base(__last1), 01228 std::__niter_base(__first2), 01229 std::__niter_base(__last2)); 01230 } 01231 01232 /** 01233 * @brief Performs @b dictionary comparison on ranges. 01234 * @ingroup sorting_algorithms 01235 * @param __first1 An input iterator. 01236 * @param __last1 An input iterator. 01237 * @param __first2 An input iterator. 01238 * @param __last2 An input iterator. 01239 * @param __comp A @link comparison_functors comparison functor@endlink. 01240 * @return A boolean true or false. 01241 * 01242 * The same as the four-parameter @c lexicographical_compare, but uses the 01243 * comp parameter instead of @c <. 01244 */ 01245 template<typename _II1, typename _II2, typename _Compare> 01246 inline bool 01247 lexicographical_compare(_II1 __first1, _II1 __last1, 01248 _II2 __first2, _II2 __last2, _Compare __comp) 01249 { 01250 // concept requirements 01251 __glibcxx_function_requires(_InputIteratorConcept<_II1>) 01252 __glibcxx_function_requires(_InputIteratorConcept<_II2>) 01253 __glibcxx_requires_valid_range(__first1, __last1); 01254 __glibcxx_requires_valid_range(__first2, __last2); 01255 01256 return std::__lexicographical_compare_impl 01257 (__first1, __last1, __first2, __last2, 01258 __gnu_cxx::__ops::__iter_comp_iter(__comp)); 01259 } 01260 01261 template<typename _InputIterator1, typename _InputIterator2, 01262 typename _BinaryPredicate> 01263 pair<_InputIterator1, _InputIterator2> 01264 __mismatch(_InputIterator1 __first1, _InputIterator1 __last1, 01265 _InputIterator2 __first2, _BinaryPredicate __binary_pred) 01266 { 01267 while (__first1 != __last1 && __binary_pred(__first1, __first2)) 01268 { 01269 ++__first1; 01270 ++__first2; 01271 } 01272 return pair<_InputIterator1, _InputIterator2>(__first1, __first2); 01273 } 01274 01275 /** 01276 * @brief Finds the places in ranges which don't match. 01277 * @ingroup non_mutating_algorithms 01278 * @param __first1 An input iterator. 01279 * @param __last1 An input iterator. 01280 * @param __first2 An input iterator. 01281 * @return A pair of iterators pointing to the first mismatch. 01282 * 01283 * This compares the elements of two ranges using @c == and returns a pair 01284 * of iterators. The first iterator points into the first range, the 01285 * second iterator points into the second range, and the elements pointed 01286 * to by the iterators are not equal. 01287 */ 01288 template<typename _InputIterator1, typename _InputIterator2> 01289 inline pair<_InputIterator1, _InputIterator2> 01290 mismatch(_InputIterator1 __first1, _InputIterator1 __last1, 01291 _InputIterator2 __first2) 01292 { 01293 // concept requirements 01294 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 01295 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 01296 __glibcxx_function_requires(_EqualOpConcept< 01297 typename iterator_traits<_InputIterator1>::value_type, 01298 typename iterator_traits<_InputIterator2>::value_type>) 01299 __glibcxx_requires_valid_range(__first1, __last1); 01300 01301 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, 01302 __gnu_cxx::__ops::__iter_equal_to_iter()); 01303 } 01304 01305 /** 01306 * @brief Finds the places in ranges which don't match. 01307 * @ingroup non_mutating_algorithms 01308 * @param __first1 An input iterator. 01309 * @param __last1 An input iterator. 01310 * @param __first2 An input iterator. 01311 * @param __binary_pred A binary predicate @link functors 01312 * functor@endlink. 01313 * @return A pair of iterators pointing to the first mismatch. 01314 * 01315 * This compares the elements of two ranges using the binary_pred 01316 * parameter, and returns a pair 01317 * of iterators. The first iterator points into the first range, the 01318 * second iterator points into the second range, and the elements pointed 01319 * to by the iterators are not equal. 01320 */ 01321 template<typename _InputIterator1, typename _InputIterator2, 01322 typename _BinaryPredicate> 01323 inline pair<_InputIterator1, _InputIterator2> 01324 mismatch(_InputIterator1 __first1, _InputIterator1 __last1, 01325 _InputIterator2 __first2, _BinaryPredicate __binary_pred) 01326 { 01327 // concept requirements 01328 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 01329 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 01330 __glibcxx_requires_valid_range(__first1, __last1); 01331 01332 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, 01333 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred)); 01334 } 01335 01336 #if __cplusplus > 201103L 01337 01338 template<typename _InputIterator1, typename _InputIterator2, 01339 typename _BinaryPredicate> 01340 pair<_InputIterator1, _InputIterator2> 01341 __mismatch(_InputIterator1 __first1, _InputIterator1 __last1, 01342 _InputIterator2 __first2, _InputIterator2 __last2, 01343 _BinaryPredicate __binary_pred) 01344 { 01345 while (__first1 != __last1 && __first2 != __last2 01346 && __binary_pred(__first1, __first2)) 01347 { 01348 ++__first1; 01349 ++__first2; 01350 } 01351 return pair<_InputIterator1, _InputIterator2>(__first1, __first2); 01352 } 01353 01354 /** 01355 * @brief Finds the places in ranges which don't match. 01356 * @ingroup non_mutating_algorithms 01357 * @param __first1 An input iterator. 01358 * @param __last1 An input iterator. 01359 * @param __first2 An input iterator. 01360 * @param __last2 An input iterator. 01361 * @return A pair of iterators pointing to the first mismatch. 01362 * 01363 * This compares the elements of two ranges using @c == and returns a pair 01364 * of iterators. The first iterator points into the first range, the 01365 * second iterator points into the second range, and the elements pointed 01366 * to by the iterators are not equal. 01367 */ 01368 template<typename _InputIterator1, typename _InputIterator2> 01369 inline pair<_InputIterator1, _InputIterator2> 01370 mismatch(_InputIterator1 __first1, _InputIterator1 __last1, 01371 _InputIterator2 __first2, _InputIterator2 __last2) 01372 { 01373 // concept requirements 01374 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 01375 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 01376 __glibcxx_function_requires(_EqualOpConcept< 01377 typename iterator_traits<_InputIterator1>::value_type, 01378 typename iterator_traits<_InputIterator2>::value_type>) 01379 __glibcxx_requires_valid_range(__first1, __last1); 01380 __glibcxx_requires_valid_range(__first2, __last2); 01381 01382 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2, 01383 __gnu_cxx::__ops::__iter_equal_to_iter()); 01384 } 01385 01386 /** 01387 * @brief Finds the places in ranges which don't match. 01388 * @ingroup non_mutating_algorithms 01389 * @param __first1 An input iterator. 01390 * @param __last1 An input iterator. 01391 * @param __first2 An input iterator. 01392 * @param __last2 An input iterator. 01393 * @param __binary_pred A binary predicate @link functors 01394 * functor@endlink. 01395 * @return A pair of iterators pointing to the first mismatch. 01396 * 01397 * This compares the elements of two ranges using the binary_pred 01398 * parameter, and returns a pair 01399 * of iterators. The first iterator points into the first range, the 01400 * second iterator points into the second range, and the elements pointed 01401 * to by the iterators are not equal. 01402 */ 01403 template<typename _InputIterator1, typename _InputIterator2, 01404 typename _BinaryPredicate> 01405 inline pair<_InputIterator1, _InputIterator2> 01406 mismatch(_InputIterator1 __first1, _InputIterator1 __last1, 01407 _InputIterator2 __first2, _InputIterator2 __last2, 01408 _BinaryPredicate __binary_pred) 01409 { 01410 // concept requirements 01411 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 01412 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 01413 __glibcxx_requires_valid_range(__first1, __last1); 01414 __glibcxx_requires_valid_range(__first2, __last2); 01415 01416 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2, 01417 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred)); 01418 } 01419 #endif 01420 01421 _GLIBCXX_END_NAMESPACE_ALGO 01422 } // namespace std 01423 01424 // NB: This file is included within many other C++ includes, as a way 01425 // of getting the base algorithms. So, make sure that parallel bits 01426 // come in too if requested. 01427 #ifdef _GLIBCXX_PARALLEL 01428 # include <parallel/algobase.h> 01429 #endif 01430 01431 #endif