|
libstdc++
|
00001 // Algorithm implementation -*- C++ -*- 00002 00003 // Copyright (C) 2001-2019 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 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_algo.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_ALGO_H 00057 #define _STL_ALGO_H 1 00058 00059 #include <cstdlib> // for rand 00060 #include <bits/algorithmfwd.h> 00061 #include <bits/stl_heap.h> 00062 #include <bits/stl_tempbuf.h> // for _Temporary_buffer 00063 #include <bits/predefined_ops.h> 00064 00065 #if __cplusplus >= 201103L 00066 #include <bits/uniform_int_dist.h> 00067 #endif 00068 00069 // See concept_check.h for the __glibcxx_*_requires macros. 00070 00071 namespace std _GLIBCXX_VISIBILITY(default) 00072 { 00073 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00074 00075 /// Swaps the median value of *__a, *__b and *__c under __comp to *__result 00076 template<typename _Iterator, typename _Compare> 00077 void 00078 __move_median_to_first(_Iterator __result,_Iterator __a, _Iterator __b, 00079 _Iterator __c, _Compare __comp) 00080 { 00081 if (__comp(__a, __b)) 00082 { 00083 if (__comp(__b, __c)) 00084 std::iter_swap(__result, __b); 00085 else if (__comp(__a, __c)) 00086 std::iter_swap(__result, __c); 00087 else 00088 std::iter_swap(__result, __a); 00089 } 00090 else if (__comp(__a, __c)) 00091 std::iter_swap(__result, __a); 00092 else if (__comp(__b, __c)) 00093 std::iter_swap(__result, __c); 00094 else 00095 std::iter_swap(__result, __b); 00096 } 00097 00098 /// This is an overload used by find algos for the Input Iterator case. 00099 template<typename _InputIterator, typename _Predicate> 00100 inline _InputIterator 00101 __find_if(_InputIterator __first, _InputIterator __last, 00102 _Predicate __pred, input_iterator_tag) 00103 { 00104 while (__first != __last && !__pred(__first)) 00105 ++__first; 00106 return __first; 00107 } 00108 00109 /// This is an overload used by find algos for the RAI case. 00110 template<typename _RandomAccessIterator, typename _Predicate> 00111 _RandomAccessIterator 00112 __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last, 00113 _Predicate __pred, random_access_iterator_tag) 00114 { 00115 typename iterator_traits<_RandomAccessIterator>::difference_type 00116 __trip_count = (__last - __first) >> 2; 00117 00118 for (; __trip_count > 0; --__trip_count) 00119 { 00120 if (__pred(__first)) 00121 return __first; 00122 ++__first; 00123 00124 if (__pred(__first)) 00125 return __first; 00126 ++__first; 00127 00128 if (__pred(__first)) 00129 return __first; 00130 ++__first; 00131 00132 if (__pred(__first)) 00133 return __first; 00134 ++__first; 00135 } 00136 00137 switch (__last - __first) 00138 { 00139 case 3: 00140 if (__pred(__first)) 00141 return __first; 00142 ++__first; 00143 case 2: 00144 if (__pred(__first)) 00145 return __first; 00146 ++__first; 00147 case 1: 00148 if (__pred(__first)) 00149 return __first; 00150 ++__first; 00151 case 0: 00152 default: 00153 return __last; 00154 } 00155 } 00156 00157 template<typename _Iterator, typename _Predicate> 00158 inline _Iterator 00159 __find_if(_Iterator __first, _Iterator __last, _Predicate __pred) 00160 { 00161 return __find_if(__first, __last, __pred, 00162 std::__iterator_category(__first)); 00163 } 00164 00165 /// Provided for stable_partition to use. 00166 template<typename _InputIterator, typename _Predicate> 00167 inline _InputIterator 00168 __find_if_not(_InputIterator __first, _InputIterator __last, 00169 _Predicate __pred) 00170 { 00171 return std::__find_if(__first, __last, 00172 __gnu_cxx::__ops::__negate(__pred), 00173 std::__iterator_category(__first)); 00174 } 00175 00176 /// Like find_if_not(), but uses and updates a count of the 00177 /// remaining range length instead of comparing against an end 00178 /// iterator. 00179 template<typename _InputIterator, typename _Predicate, typename _Distance> 00180 _InputIterator 00181 __find_if_not_n(_InputIterator __first, _Distance& __len, _Predicate __pred) 00182 { 00183 for (; __len; --__len, (void) ++__first) 00184 if (!__pred(__first)) 00185 break; 00186 return __first; 00187 } 00188 00189 // set_difference 00190 // set_intersection 00191 // set_symmetric_difference 00192 // set_union 00193 // for_each 00194 // find 00195 // find_if 00196 // find_first_of 00197 // adjacent_find 00198 // count 00199 // count_if 00200 // search 00201 00202 template<typename _ForwardIterator1, typename _ForwardIterator2, 00203 typename _BinaryPredicate> 00204 _ForwardIterator1 00205 __search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 00206 _ForwardIterator2 __first2, _ForwardIterator2 __last2, 00207 _BinaryPredicate __predicate) 00208 { 00209 // Test for empty ranges 00210 if (__first1 == __last1 || __first2 == __last2) 00211 return __first1; 00212 00213 // Test for a pattern of length 1. 00214 _ForwardIterator2 __p1(__first2); 00215 if (++__p1 == __last2) 00216 return std::__find_if(__first1, __last1, 00217 __gnu_cxx::__ops::__iter_comp_iter(__predicate, __first2)); 00218 00219 // General case. 00220 _ForwardIterator2 __p; 00221 _ForwardIterator1 __current = __first1; 00222 00223 for (;;) 00224 { 00225 __first1 = 00226 std::__find_if(__first1, __last1, 00227 __gnu_cxx::__ops::__iter_comp_iter(__predicate, __first2)); 00228 00229 if (__first1 == __last1) 00230 return __last1; 00231 00232 __p = __p1; 00233 __current = __first1; 00234 if (++__current == __last1) 00235 return __last1; 00236 00237 while (__predicate(__current, __p)) 00238 { 00239 if (++__p == __last2) 00240 return __first1; 00241 if (++__current == __last1) 00242 return __last1; 00243 } 00244 ++__first1; 00245 } 00246 return __first1; 00247 } 00248 00249 // search_n 00250 00251 /** 00252 * This is an helper function for search_n overloaded for forward iterators. 00253 */ 00254 template<typename _ForwardIterator, typename _Integer, 00255 typename _UnaryPredicate> 00256 _ForwardIterator 00257 __search_n_aux(_ForwardIterator __first, _ForwardIterator __last, 00258 _Integer __count, _UnaryPredicate __unary_pred, 00259 std::forward_iterator_tag) 00260 { 00261 __first = std::__find_if(__first, __last, __unary_pred); 00262 while (__first != __last) 00263 { 00264 typename iterator_traits<_ForwardIterator>::difference_type 00265 __n = __count; 00266 _ForwardIterator __i = __first; 00267 ++__i; 00268 while (__i != __last && __n != 1 && __unary_pred(__i)) 00269 { 00270 ++__i; 00271 --__n; 00272 } 00273 if (__n == 1) 00274 return __first; 00275 if (__i == __last) 00276 return __last; 00277 __first = std::__find_if(++__i, __last, __unary_pred); 00278 } 00279 return __last; 00280 } 00281 00282 /** 00283 * This is an helper function for search_n overloaded for random access 00284 * iterators. 00285 */ 00286 template<typename _RandomAccessIter, typename _Integer, 00287 typename _UnaryPredicate> 00288 _RandomAccessIter 00289 __search_n_aux(_RandomAccessIter __first, _RandomAccessIter __last, 00290 _Integer __count, _UnaryPredicate __unary_pred, 00291 std::random_access_iterator_tag) 00292 { 00293 typedef typename std::iterator_traits<_RandomAccessIter>::difference_type 00294 _DistanceType; 00295 00296 _DistanceType __tailSize = __last - __first; 00297 _DistanceType __remainder = __count; 00298 00299 while (__remainder <= __tailSize) // the main loop... 00300 { 00301 __first += __remainder; 00302 __tailSize -= __remainder; 00303 // __first here is always pointing to one past the last element of 00304 // next possible match. 00305 _RandomAccessIter __backTrack = __first; 00306 while (__unary_pred(--__backTrack)) 00307 { 00308 if (--__remainder == 0) 00309 return (__first - __count); // Success 00310 } 00311 __remainder = __count + 1 - (__first - __backTrack); 00312 } 00313 return __last; // Failure 00314 } 00315 00316 template<typename _ForwardIterator, typename _Integer, 00317 typename _UnaryPredicate> 00318 _ForwardIterator 00319 __search_n(_ForwardIterator __first, _ForwardIterator __last, 00320 _Integer __count, 00321 _UnaryPredicate __unary_pred) 00322 { 00323 if (__count <= 0) 00324 return __first; 00325 00326 if (__count == 1) 00327 return std::__find_if(__first, __last, __unary_pred); 00328 00329 return std::__search_n_aux(__first, __last, __count, __unary_pred, 00330 std::__iterator_category(__first)); 00331 } 00332 00333 // find_end for forward iterators. 00334 template<typename _ForwardIterator1, typename _ForwardIterator2, 00335 typename _BinaryPredicate> 00336 _ForwardIterator1 00337 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 00338 _ForwardIterator2 __first2, _ForwardIterator2 __last2, 00339 forward_iterator_tag, forward_iterator_tag, 00340 _BinaryPredicate __comp) 00341 { 00342 if (__first2 == __last2) 00343 return __last1; 00344 00345 _ForwardIterator1 __result = __last1; 00346 while (1) 00347 { 00348 _ForwardIterator1 __new_result 00349 = std::__search(__first1, __last1, __first2, __last2, __comp); 00350 if (__new_result == __last1) 00351 return __result; 00352 else 00353 { 00354 __result = __new_result; 00355 __first1 = __new_result; 00356 ++__first1; 00357 } 00358 } 00359 } 00360 00361 // find_end for bidirectional iterators (much faster). 00362 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2, 00363 typename _BinaryPredicate> 00364 _BidirectionalIterator1 00365 __find_end(_BidirectionalIterator1 __first1, 00366 _BidirectionalIterator1 __last1, 00367 _BidirectionalIterator2 __first2, 00368 _BidirectionalIterator2 __last2, 00369 bidirectional_iterator_tag, bidirectional_iterator_tag, 00370 _BinaryPredicate __comp) 00371 { 00372 // concept requirements 00373 __glibcxx_function_requires(_BidirectionalIteratorConcept< 00374 _BidirectionalIterator1>) 00375 __glibcxx_function_requires(_BidirectionalIteratorConcept< 00376 _BidirectionalIterator2>) 00377 00378 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1; 00379 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2; 00380 00381 _RevIterator1 __rlast1(__first1); 00382 _RevIterator2 __rlast2(__first2); 00383 _RevIterator1 __rresult = std::__search(_RevIterator1(__last1), __rlast1, 00384 _RevIterator2(__last2), __rlast2, 00385 __comp); 00386 00387 if (__rresult == __rlast1) 00388 return __last1; 00389 else 00390 { 00391 _BidirectionalIterator1 __result = __rresult.base(); 00392 std::advance(__result, -std::distance(__first2, __last2)); 00393 return __result; 00394 } 00395 } 00396 00397 /** 00398 * @brief Find last matching subsequence in a sequence. 00399 * @ingroup non_mutating_algorithms 00400 * @param __first1 Start of range to search. 00401 * @param __last1 End of range to search. 00402 * @param __first2 Start of sequence to match. 00403 * @param __last2 End of sequence to match. 00404 * @return The last iterator @c i in the range 00405 * @p [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == 00406 * @p *(__first2+N) for each @c N in the range @p 00407 * [0,__last2-__first2), or @p __last1 if no such iterator exists. 00408 * 00409 * Searches the range @p [__first1,__last1) for a sub-sequence that 00410 * compares equal value-by-value with the sequence given by @p 00411 * [__first2,__last2) and returns an iterator to the __first 00412 * element of the sub-sequence, or @p __last1 if the sub-sequence 00413 * is not found. The sub-sequence will be the last such 00414 * subsequence contained in [__first1,__last1). 00415 * 00416 * Because the sub-sequence must lie completely within the range @p 00417 * [__first1,__last1) it must start at a position less than @p 00418 * __last1-(__last2-__first2) where @p __last2-__first2 is the 00419 * length of the sub-sequence. This means that the returned 00420 * iterator @c i will be in the range @p 00421 * [__first1,__last1-(__last2-__first2)) 00422 */ 00423 template<typename _ForwardIterator1, typename _ForwardIterator2> 00424 inline _ForwardIterator1 00425 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 00426 _ForwardIterator2 __first2, _ForwardIterator2 __last2) 00427 { 00428 // concept requirements 00429 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) 00430 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) 00431 __glibcxx_function_requires(_EqualOpConcept< 00432 typename iterator_traits<_ForwardIterator1>::value_type, 00433 typename iterator_traits<_ForwardIterator2>::value_type>) 00434 __glibcxx_requires_valid_range(__first1, __last1); 00435 __glibcxx_requires_valid_range(__first2, __last2); 00436 00437 return std::__find_end(__first1, __last1, __first2, __last2, 00438 std::__iterator_category(__first1), 00439 std::__iterator_category(__first2), 00440 __gnu_cxx::__ops::__iter_equal_to_iter()); 00441 } 00442 00443 /** 00444 * @brief Find last matching subsequence in a sequence using a predicate. 00445 * @ingroup non_mutating_algorithms 00446 * @param __first1 Start of range to search. 00447 * @param __last1 End of range to search. 00448 * @param __first2 Start of sequence to match. 00449 * @param __last2 End of sequence to match. 00450 * @param __comp The predicate to use. 00451 * @return The last iterator @c i in the range @p 00452 * [__first1,__last1-(__last2-__first2)) such that @c 00453 * predicate(*(i+N), @p (__first2+N)) is true for each @c N in the 00454 * range @p [0,__last2-__first2), or @p __last1 if no such iterator 00455 * exists. 00456 * 00457 * Searches the range @p [__first1,__last1) for a sub-sequence that 00458 * compares equal value-by-value with the sequence given by @p 00459 * [__first2,__last2) using comp as a predicate and returns an 00460 * iterator to the first element of the sub-sequence, or @p __last1 00461 * if the sub-sequence is not found. The sub-sequence will be the 00462 * last such subsequence contained in [__first,__last1). 00463 * 00464 * Because the sub-sequence must lie completely within the range @p 00465 * [__first1,__last1) it must start at a position less than @p 00466 * __last1-(__last2-__first2) where @p __last2-__first2 is the 00467 * length of the sub-sequence. This means that the returned 00468 * iterator @c i will be in the range @p 00469 * [__first1,__last1-(__last2-__first2)) 00470 */ 00471 template<typename _ForwardIterator1, typename _ForwardIterator2, 00472 typename _BinaryPredicate> 00473 inline _ForwardIterator1 00474 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 00475 _ForwardIterator2 __first2, _ForwardIterator2 __last2, 00476 _BinaryPredicate __comp) 00477 { 00478 // concept requirements 00479 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) 00480 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) 00481 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, 00482 typename iterator_traits<_ForwardIterator1>::value_type, 00483 typename iterator_traits<_ForwardIterator2>::value_type>) 00484 __glibcxx_requires_valid_range(__first1, __last1); 00485 __glibcxx_requires_valid_range(__first2, __last2); 00486 00487 return std::__find_end(__first1, __last1, __first2, __last2, 00488 std::__iterator_category(__first1), 00489 std::__iterator_category(__first2), 00490 __gnu_cxx::__ops::__iter_comp_iter(__comp)); 00491 } 00492 00493 #if __cplusplus >= 201103L 00494 /** 00495 * @brief Checks that a predicate is true for all the elements 00496 * of a sequence. 00497 * @ingroup non_mutating_algorithms 00498 * @param __first An input iterator. 00499 * @param __last An input iterator. 00500 * @param __pred A predicate. 00501 * @return True if the check is true, false otherwise. 00502 * 00503 * Returns true if @p __pred is true for each element in the range 00504 * @p [__first,__last), and false otherwise. 00505 */ 00506 template<typename _InputIterator, typename _Predicate> 00507 inline bool 00508 all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) 00509 { return __last == std::find_if_not(__first, __last, __pred); } 00510 00511 /** 00512 * @brief Checks that a predicate is false for all the elements 00513 * of a sequence. 00514 * @ingroup non_mutating_algorithms 00515 * @param __first An input iterator. 00516 * @param __last An input iterator. 00517 * @param __pred A predicate. 00518 * @return True if the check is true, false otherwise. 00519 * 00520 * Returns true if @p __pred is false for each element in the range 00521 * @p [__first,__last), and false otherwise. 00522 */ 00523 template<typename _InputIterator, typename _Predicate> 00524 inline bool 00525 none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) 00526 { return __last == _GLIBCXX_STD_A::find_if(__first, __last, __pred); } 00527 00528 /** 00529 * @brief Checks that a predicate is false for at least an element 00530 * of a sequence. 00531 * @ingroup non_mutating_algorithms 00532 * @param __first An input iterator. 00533 * @param __last An input iterator. 00534 * @param __pred A predicate. 00535 * @return True if the check is true, false otherwise. 00536 * 00537 * Returns true if an element exists in the range @p 00538 * [__first,__last) such that @p __pred is true, and false 00539 * otherwise. 00540 */ 00541 template<typename _InputIterator, typename _Predicate> 00542 inline bool 00543 any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) 00544 { return !std::none_of(__first, __last, __pred); } 00545 00546 /** 00547 * @brief Find the first element in a sequence for which a 00548 * predicate is false. 00549 * @ingroup non_mutating_algorithms 00550 * @param __first An input iterator. 00551 * @param __last An input iterator. 00552 * @param __pred A predicate. 00553 * @return The first iterator @c i in the range @p [__first,__last) 00554 * such that @p __pred(*i) is false, or @p __last if no such iterator exists. 00555 */ 00556 template<typename _InputIterator, typename _Predicate> 00557 inline _InputIterator 00558 find_if_not(_InputIterator __first, _InputIterator __last, 00559 _Predicate __pred) 00560 { 00561 // concept requirements 00562 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 00563 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 00564 typename iterator_traits<_InputIterator>::value_type>) 00565 __glibcxx_requires_valid_range(__first, __last); 00566 return std::__find_if_not(__first, __last, 00567 __gnu_cxx::__ops::__pred_iter(__pred)); 00568 } 00569 00570 /** 00571 * @brief Checks whether the sequence is partitioned. 00572 * @ingroup mutating_algorithms 00573 * @param __first An input iterator. 00574 * @param __last An input iterator. 00575 * @param __pred A predicate. 00576 * @return True if the range @p [__first,__last) is partioned by @p __pred, 00577 * i.e. if all elements that satisfy @p __pred appear before those that 00578 * do not. 00579 */ 00580 template<typename _InputIterator, typename _Predicate> 00581 inline bool 00582 is_partitioned(_InputIterator __first, _InputIterator __last, 00583 _Predicate __pred) 00584 { 00585 __first = std::find_if_not(__first, __last, __pred); 00586 if (__first == __last) 00587 return true; 00588 ++__first; 00589 return std::none_of(__first, __last, __pred); 00590 } 00591 00592 /** 00593 * @brief Find the partition point of a partitioned range. 00594 * @ingroup mutating_algorithms 00595 * @param __first An iterator. 00596 * @param __last Another iterator. 00597 * @param __pred A predicate. 00598 * @return An iterator @p mid such that @p all_of(__first, mid, __pred) 00599 * and @p none_of(mid, __last, __pred) are both true. 00600 */ 00601 template<typename _ForwardIterator, typename _Predicate> 00602 _ForwardIterator 00603 partition_point(_ForwardIterator __first, _ForwardIterator __last, 00604 _Predicate __pred) 00605 { 00606 // concept requirements 00607 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 00608 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 00609 typename iterator_traits<_ForwardIterator>::value_type>) 00610 00611 // A specific debug-mode test will be necessary... 00612 __glibcxx_requires_valid_range(__first, __last); 00613 00614 typedef typename iterator_traits<_ForwardIterator>::difference_type 00615 _DistanceType; 00616 00617 _DistanceType __len = std::distance(__first, __last); 00618 _DistanceType __half; 00619 _ForwardIterator __middle; 00620 00621 while (__len > 0) 00622 { 00623 __half = __len >> 1; 00624 __middle = __first; 00625 std::advance(__middle, __half); 00626 if (__pred(*__middle)) 00627 { 00628 __first = __middle; 00629 ++__first; 00630 __len = __len - __half - 1; 00631 } 00632 else 00633 __len = __half; 00634 } 00635 return __first; 00636 } 00637 #endif 00638 00639 template<typename _InputIterator, typename _OutputIterator, 00640 typename _Predicate> 00641 _OutputIterator 00642 __remove_copy_if(_InputIterator __first, _InputIterator __last, 00643 _OutputIterator __result, _Predicate __pred) 00644 { 00645 for (; __first != __last; ++__first) 00646 if (!__pred(__first)) 00647 { 00648 *__result = *__first; 00649 ++__result; 00650 } 00651 return __result; 00652 } 00653 00654 /** 00655 * @brief Copy a sequence, removing elements of a given value. 00656 * @ingroup mutating_algorithms 00657 * @param __first An input iterator. 00658 * @param __last An input iterator. 00659 * @param __result An output iterator. 00660 * @param __value The value to be removed. 00661 * @return An iterator designating the end of the resulting sequence. 00662 * 00663 * Copies each element in the range @p [__first,__last) not equal 00664 * to @p __value to the range beginning at @p __result. 00665 * remove_copy() is stable, so the relative order of elements that 00666 * are copied is unchanged. 00667 */ 00668 template<typename _InputIterator, typename _OutputIterator, typename _Tp> 00669 inline _OutputIterator 00670 remove_copy(_InputIterator __first, _InputIterator __last, 00671 _OutputIterator __result, const _Tp& __value) 00672 { 00673 // concept requirements 00674 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 00675 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 00676 typename iterator_traits<_InputIterator>::value_type>) 00677 __glibcxx_function_requires(_EqualOpConcept< 00678 typename iterator_traits<_InputIterator>::value_type, _Tp>) 00679 __glibcxx_requires_valid_range(__first, __last); 00680 00681 return std::__remove_copy_if(__first, __last, __result, 00682 __gnu_cxx::__ops::__iter_equals_val(__value)); 00683 } 00684 00685 /** 00686 * @brief Copy a sequence, removing elements for which a predicate is true. 00687 * @ingroup mutating_algorithms 00688 * @param __first An input iterator. 00689 * @param __last An input iterator. 00690 * @param __result An output iterator. 00691 * @param __pred A predicate. 00692 * @return An iterator designating the end of the resulting sequence. 00693 * 00694 * Copies each element in the range @p [__first,__last) for which 00695 * @p __pred returns false to the range beginning at @p __result. 00696 * 00697 * remove_copy_if() is stable, so the relative order of elements that are 00698 * copied is unchanged. 00699 */ 00700 template<typename _InputIterator, typename _OutputIterator, 00701 typename _Predicate> 00702 inline _OutputIterator 00703 remove_copy_if(_InputIterator __first, _InputIterator __last, 00704 _OutputIterator __result, _Predicate __pred) 00705 { 00706 // concept requirements 00707 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 00708 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 00709 typename iterator_traits<_InputIterator>::value_type>) 00710 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 00711 typename iterator_traits<_InputIterator>::value_type>) 00712 __glibcxx_requires_valid_range(__first, __last); 00713 00714 return std::__remove_copy_if(__first, __last, __result, 00715 __gnu_cxx::__ops::__pred_iter(__pred)); 00716 } 00717 00718 #if __cplusplus >= 201103L 00719 /** 00720 * @brief Copy the elements of a sequence for which a predicate is true. 00721 * @ingroup mutating_algorithms 00722 * @param __first An input iterator. 00723 * @param __last An input iterator. 00724 * @param __result An output iterator. 00725 * @param __pred A predicate. 00726 * @return An iterator designating the end of the resulting sequence. 00727 * 00728 * Copies each element in the range @p [__first,__last) for which 00729 * @p __pred returns true to the range beginning at @p __result. 00730 * 00731 * copy_if() is stable, so the relative order of elements that are 00732 * copied is unchanged. 00733 */ 00734 template<typename _InputIterator, typename _OutputIterator, 00735 typename _Predicate> 00736 _OutputIterator 00737 copy_if(_InputIterator __first, _InputIterator __last, 00738 _OutputIterator __result, _Predicate __pred) 00739 { 00740 // concept requirements 00741 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 00742 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 00743 typename iterator_traits<_InputIterator>::value_type>) 00744 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 00745 typename iterator_traits<_InputIterator>::value_type>) 00746 __glibcxx_requires_valid_range(__first, __last); 00747 00748 for (; __first != __last; ++__first) 00749 if (__pred(*__first)) 00750 { 00751 *__result = *__first; 00752 ++__result; 00753 } 00754 return __result; 00755 } 00756 00757 template<typename _InputIterator, typename _Size, typename _OutputIterator> 00758 _OutputIterator 00759 __copy_n(_InputIterator __first, _Size __n, 00760 _OutputIterator __result, input_iterator_tag) 00761 { 00762 if (__n > 0) 00763 { 00764 while (true) 00765 { 00766 *__result = *__first; 00767 ++__result; 00768 if (--__n > 0) 00769 ++__first; 00770 else 00771 break; 00772 } 00773 } 00774 return __result; 00775 } 00776 00777 template<typename _RandomAccessIterator, typename _Size, 00778 typename _OutputIterator> 00779 inline _OutputIterator 00780 __copy_n(_RandomAccessIterator __first, _Size __n, 00781 _OutputIterator __result, random_access_iterator_tag) 00782 { return std::copy(__first, __first + __n, __result); } 00783 00784 /** 00785 * @brief Copies the range [first,first+n) into [result,result+n). 00786 * @ingroup mutating_algorithms 00787 * @param __first An input iterator. 00788 * @param __n The number of elements to copy. 00789 * @param __result An output iterator. 00790 * @return result+n. 00791 * 00792 * This inline function will boil down to a call to @c memmove whenever 00793 * possible. Failing that, if random access iterators are passed, then the 00794 * loop count will be known (and therefore a candidate for compiler 00795 * optimizations such as unrolling). 00796 */ 00797 template<typename _InputIterator, typename _Size, typename _OutputIterator> 00798 inline _OutputIterator 00799 copy_n(_InputIterator __first, _Size __n, _OutputIterator __result) 00800 { 00801 // concept requirements 00802 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 00803 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 00804 typename iterator_traits<_InputIterator>::value_type>) 00805 00806 return std::__copy_n(__first, __n, __result, 00807 std::__iterator_category(__first)); 00808 } 00809 00810 /** 00811 * @brief Copy the elements of a sequence to separate output sequences 00812 * depending on the truth value of a predicate. 00813 * @ingroup mutating_algorithms 00814 * @param __first An input iterator. 00815 * @param __last An input iterator. 00816 * @param __out_true An output iterator. 00817 * @param __out_false An output iterator. 00818 * @param __pred A predicate. 00819 * @return A pair designating the ends of the resulting sequences. 00820 * 00821 * Copies each element in the range @p [__first,__last) for which 00822 * @p __pred returns true to the range beginning at @p out_true 00823 * and each element for which @p __pred returns false to @p __out_false. 00824 */ 00825 template<typename _InputIterator, typename _OutputIterator1, 00826 typename _OutputIterator2, typename _Predicate> 00827 pair<_OutputIterator1, _OutputIterator2> 00828 partition_copy(_InputIterator __first, _InputIterator __last, 00829 _OutputIterator1 __out_true, _OutputIterator2 __out_false, 00830 _Predicate __pred) 00831 { 00832 // concept requirements 00833 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 00834 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator1, 00835 typename iterator_traits<_InputIterator>::value_type>) 00836 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator2, 00837 typename iterator_traits<_InputIterator>::value_type>) 00838 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 00839 typename iterator_traits<_InputIterator>::value_type>) 00840 __glibcxx_requires_valid_range(__first, __last); 00841 00842 for (; __first != __last; ++__first) 00843 if (__pred(*__first)) 00844 { 00845 *__out_true = *__first; 00846 ++__out_true; 00847 } 00848 else 00849 { 00850 *__out_false = *__first; 00851 ++__out_false; 00852 } 00853 00854 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false); 00855 } 00856 #endif 00857 00858 template<typename _ForwardIterator, typename _Predicate> 00859 _ForwardIterator 00860 __remove_if(_ForwardIterator __first, _ForwardIterator __last, 00861 _Predicate __pred) 00862 { 00863 __first = std::__find_if(__first, __last, __pred); 00864 if (__first == __last) 00865 return __first; 00866 _ForwardIterator __result = __first; 00867 ++__first; 00868 for (; __first != __last; ++__first) 00869 if (!__pred(__first)) 00870 { 00871 *__result = _GLIBCXX_MOVE(*__first); 00872 ++__result; 00873 } 00874 return __result; 00875 } 00876 00877 /** 00878 * @brief Remove elements from a sequence. 00879 * @ingroup mutating_algorithms 00880 * @param __first An input iterator. 00881 * @param __last An input iterator. 00882 * @param __value The value to be removed. 00883 * @return An iterator designating the end of the resulting sequence. 00884 * 00885 * All elements equal to @p __value are removed from the range 00886 * @p [__first,__last). 00887 * 00888 * remove() is stable, so the relative order of elements that are 00889 * not removed is unchanged. 00890 * 00891 * Elements between the end of the resulting sequence and @p __last 00892 * are still present, but their value is unspecified. 00893 */ 00894 template<typename _ForwardIterator, typename _Tp> 00895 inline _ForwardIterator 00896 remove(_ForwardIterator __first, _ForwardIterator __last, 00897 const _Tp& __value) 00898 { 00899 // concept requirements 00900 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 00901 _ForwardIterator>) 00902 __glibcxx_function_requires(_EqualOpConcept< 00903 typename iterator_traits<_ForwardIterator>::value_type, _Tp>) 00904 __glibcxx_requires_valid_range(__first, __last); 00905 00906 return std::__remove_if(__first, __last, 00907 __gnu_cxx::__ops::__iter_equals_val(__value)); 00908 } 00909 00910 /** 00911 * @brief Remove elements from a sequence using a predicate. 00912 * @ingroup mutating_algorithms 00913 * @param __first A forward iterator. 00914 * @param __last A forward iterator. 00915 * @param __pred A predicate. 00916 * @return An iterator designating the end of the resulting sequence. 00917 * 00918 * All elements for which @p __pred returns true are removed from the range 00919 * @p [__first,__last). 00920 * 00921 * remove_if() is stable, so the relative order of elements that are 00922 * not removed is unchanged. 00923 * 00924 * Elements between the end of the resulting sequence and @p __last 00925 * are still present, but their value is unspecified. 00926 */ 00927 template<typename _ForwardIterator, typename _Predicate> 00928 inline _ForwardIterator 00929 remove_if(_ForwardIterator __first, _ForwardIterator __last, 00930 _Predicate __pred) 00931 { 00932 // concept requirements 00933 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 00934 _ForwardIterator>) 00935 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 00936 typename iterator_traits<_ForwardIterator>::value_type>) 00937 __glibcxx_requires_valid_range(__first, __last); 00938 00939 return std::__remove_if(__first, __last, 00940 __gnu_cxx::__ops::__pred_iter(__pred)); 00941 } 00942 00943 template<typename _ForwardIterator, typename _BinaryPredicate> 00944 _ForwardIterator 00945 __adjacent_find(_ForwardIterator __first, _ForwardIterator __last, 00946 _BinaryPredicate __binary_pred) 00947 { 00948 if (__first == __last) 00949 return __last; 00950 _ForwardIterator __next = __first; 00951 while (++__next != __last) 00952 { 00953 if (__binary_pred(__first, __next)) 00954 return __first; 00955 __first = __next; 00956 } 00957 return __last; 00958 } 00959 00960 template<typename _ForwardIterator, typename _BinaryPredicate> 00961 _ForwardIterator 00962 __unique(_ForwardIterator __first, _ForwardIterator __last, 00963 _BinaryPredicate __binary_pred) 00964 { 00965 // Skip the beginning, if already unique. 00966 __first = std::__adjacent_find(__first, __last, __binary_pred); 00967 if (__first == __last) 00968 return __last; 00969 00970 // Do the real copy work. 00971 _ForwardIterator __dest = __first; 00972 ++__first; 00973 while (++__first != __last) 00974 if (!__binary_pred(__dest, __first)) 00975 *++__dest = _GLIBCXX_MOVE(*__first); 00976 return ++__dest; 00977 } 00978 00979 /** 00980 * @brief Remove consecutive duplicate values from a sequence. 00981 * @ingroup mutating_algorithms 00982 * @param __first A forward iterator. 00983 * @param __last A forward iterator. 00984 * @return An iterator designating the end of the resulting sequence. 00985 * 00986 * Removes all but the first element from each group of consecutive 00987 * values that compare equal. 00988 * unique() is stable, so the relative order of elements that are 00989 * not removed is unchanged. 00990 * Elements between the end of the resulting sequence and @p __last 00991 * are still present, but their value is unspecified. 00992 */ 00993 template<typename _ForwardIterator> 00994 inline _ForwardIterator 00995 unique(_ForwardIterator __first, _ForwardIterator __last) 00996 { 00997 // concept requirements 00998 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 00999 _ForwardIterator>) 01000 __glibcxx_function_requires(_EqualityComparableConcept< 01001 typename iterator_traits<_ForwardIterator>::value_type>) 01002 __glibcxx_requires_valid_range(__first, __last); 01003 01004 return std::__unique(__first, __last, 01005 __gnu_cxx::__ops::__iter_equal_to_iter()); 01006 } 01007 01008 /** 01009 * @brief Remove consecutive values from a sequence using a predicate. 01010 * @ingroup mutating_algorithms 01011 * @param __first A forward iterator. 01012 * @param __last A forward iterator. 01013 * @param __binary_pred A binary predicate. 01014 * @return An iterator designating the end of the resulting sequence. 01015 * 01016 * Removes all but the first element from each group of consecutive 01017 * values for which @p __binary_pred returns true. 01018 * unique() is stable, so the relative order of elements that are 01019 * not removed is unchanged. 01020 * Elements between the end of the resulting sequence and @p __last 01021 * are still present, but their value is unspecified. 01022 */ 01023 template<typename _ForwardIterator, typename _BinaryPredicate> 01024 inline _ForwardIterator 01025 unique(_ForwardIterator __first, _ForwardIterator __last, 01026 _BinaryPredicate __binary_pred) 01027 { 01028 // concept requirements 01029 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 01030 _ForwardIterator>) 01031 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, 01032 typename iterator_traits<_ForwardIterator>::value_type, 01033 typename iterator_traits<_ForwardIterator>::value_type>) 01034 __glibcxx_requires_valid_range(__first, __last); 01035 01036 return std::__unique(__first, __last, 01037 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred)); 01038 } 01039 01040 /** 01041 * This is an uglified 01042 * unique_copy(_InputIterator, _InputIterator, _OutputIterator, 01043 * _BinaryPredicate) 01044 * overloaded for forward iterators and output iterator as result. 01045 */ 01046 template<typename _ForwardIterator, typename _OutputIterator, 01047 typename _BinaryPredicate> 01048 _OutputIterator 01049 __unique_copy(_ForwardIterator __first, _ForwardIterator __last, 01050 _OutputIterator __result, _BinaryPredicate __binary_pred, 01051 forward_iterator_tag, output_iterator_tag) 01052 { 01053 // concept requirements -- iterators already checked 01054 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, 01055 typename iterator_traits<_ForwardIterator>::value_type, 01056 typename iterator_traits<_ForwardIterator>::value_type>) 01057 01058 _ForwardIterator __next = __first; 01059 *__result = *__first; 01060 while (++__next != __last) 01061 if (!__binary_pred(__first, __next)) 01062 { 01063 __first = __next; 01064 *++__result = *__first; 01065 } 01066 return ++__result; 01067 } 01068 01069 /** 01070 * This is an uglified 01071 * unique_copy(_InputIterator, _InputIterator, _OutputIterator, 01072 * _BinaryPredicate) 01073 * overloaded for input iterators and output iterator as result. 01074 */ 01075 template<typename _InputIterator, typename _OutputIterator, 01076 typename _BinaryPredicate> 01077 _OutputIterator 01078 __unique_copy(_InputIterator __first, _InputIterator __last, 01079 _OutputIterator __result, _BinaryPredicate __binary_pred, 01080 input_iterator_tag, output_iterator_tag) 01081 { 01082 // concept requirements -- iterators already checked 01083 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, 01084 typename iterator_traits<_InputIterator>::value_type, 01085 typename iterator_traits<_InputIterator>::value_type>) 01086 01087 typename iterator_traits<_InputIterator>::value_type __value = *__first; 01088 __decltype(__gnu_cxx::__ops::__iter_comp_val(__binary_pred)) 01089 __rebound_pred 01090 = __gnu_cxx::__ops::__iter_comp_val(__binary_pred); 01091 *__result = __value; 01092 while (++__first != __last) 01093 if (!__rebound_pred(__first, __value)) 01094 { 01095 __value = *__first; 01096 *++__result = __value; 01097 } 01098 return ++__result; 01099 } 01100 01101 /** 01102 * This is an uglified 01103 * unique_copy(_InputIterator, _InputIterator, _OutputIterator, 01104 * _BinaryPredicate) 01105 * overloaded for input iterators and forward iterator as result. 01106 */ 01107 template<typename _InputIterator, typename _ForwardIterator, 01108 typename _BinaryPredicate> 01109 _ForwardIterator 01110 __unique_copy(_InputIterator __first, _InputIterator __last, 01111 _ForwardIterator __result, _BinaryPredicate __binary_pred, 01112 input_iterator_tag, forward_iterator_tag) 01113 { 01114 // concept requirements -- iterators already checked 01115 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, 01116 typename iterator_traits<_ForwardIterator>::value_type, 01117 typename iterator_traits<_InputIterator>::value_type>) 01118 *__result = *__first; 01119 while (++__first != __last) 01120 if (!__binary_pred(__result, __first)) 01121 *++__result = *__first; 01122 return ++__result; 01123 } 01124 01125 /** 01126 * This is an uglified reverse(_BidirectionalIterator, 01127 * _BidirectionalIterator) 01128 * overloaded for bidirectional iterators. 01129 */ 01130 template<typename _BidirectionalIterator> 01131 void 01132 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, 01133 bidirectional_iterator_tag) 01134 { 01135 while (true) 01136 if (__first == __last || __first == --__last) 01137 return; 01138 else 01139 { 01140 std::iter_swap(__first, __last); 01141 ++__first; 01142 } 01143 } 01144 01145 /** 01146 * This is an uglified reverse(_BidirectionalIterator, 01147 * _BidirectionalIterator) 01148 * overloaded for random access iterators. 01149 */ 01150 template<typename _RandomAccessIterator> 01151 void 01152 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, 01153 random_access_iterator_tag) 01154 { 01155 if (__first == __last) 01156 return; 01157 --__last; 01158 while (__first < __last) 01159 { 01160 std::iter_swap(__first, __last); 01161 ++__first; 01162 --__last; 01163 } 01164 } 01165 01166 /** 01167 * @brief Reverse a sequence. 01168 * @ingroup mutating_algorithms 01169 * @param __first A bidirectional iterator. 01170 * @param __last A bidirectional iterator. 01171 * @return reverse() returns no value. 01172 * 01173 * Reverses the order of the elements in the range @p [__first,__last), 01174 * so that the first element becomes the last etc. 01175 * For every @c i such that @p 0<=i<=(__last-__first)/2), @p reverse() 01176 * swaps @p *(__first+i) and @p *(__last-(i+1)) 01177 */ 01178 template<typename _BidirectionalIterator> 01179 inline void 01180 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last) 01181 { 01182 // concept requirements 01183 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< 01184 _BidirectionalIterator>) 01185 __glibcxx_requires_valid_range(__first, __last); 01186 std::__reverse(__first, __last, std::__iterator_category(__first)); 01187 } 01188 01189 /** 01190 * @brief Copy a sequence, reversing its elements. 01191 * @ingroup mutating_algorithms 01192 * @param __first A bidirectional iterator. 01193 * @param __last A bidirectional iterator. 01194 * @param __result An output iterator. 01195 * @return An iterator designating the end of the resulting sequence. 01196 * 01197 * Copies the elements in the range @p [__first,__last) to the 01198 * range @p [__result,__result+(__last-__first)) such that the 01199 * order of the elements is reversed. For every @c i such that @p 01200 * 0<=i<=(__last-__first), @p reverse_copy() performs the 01201 * assignment @p *(__result+(__last-__first)-1-i) = *(__first+i). 01202 * The ranges @p [__first,__last) and @p 01203 * [__result,__result+(__last-__first)) must not overlap. 01204 */ 01205 template<typename _BidirectionalIterator, typename _OutputIterator> 01206 _OutputIterator 01207 reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, 01208 _OutputIterator __result) 01209 { 01210 // concept requirements 01211 __glibcxx_function_requires(_BidirectionalIteratorConcept< 01212 _BidirectionalIterator>) 01213 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 01214 typename iterator_traits<_BidirectionalIterator>::value_type>) 01215 __glibcxx_requires_valid_range(__first, __last); 01216 01217 while (__first != __last) 01218 { 01219 --__last; 01220 *__result = *__last; 01221 ++__result; 01222 } 01223 return __result; 01224 } 01225 01226 /** 01227 * This is a helper function for the rotate algorithm specialized on RAIs. 01228 * It returns the greatest common divisor of two integer values. 01229 */ 01230 template<typename _EuclideanRingElement> 01231 _EuclideanRingElement 01232 __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n) 01233 { 01234 while (__n != 0) 01235 { 01236 _EuclideanRingElement __t = __m % __n; 01237 __m = __n; 01238 __n = __t; 01239 } 01240 return __m; 01241 } 01242 01243 inline namespace _V2 01244 { 01245 01246 /// This is a helper function for the rotate algorithm. 01247 template<typename _ForwardIterator> 01248 _ForwardIterator 01249 __rotate(_ForwardIterator __first, 01250 _ForwardIterator __middle, 01251 _ForwardIterator __last, 01252 forward_iterator_tag) 01253 { 01254 if (__first == __middle) 01255 return __last; 01256 else if (__last == __middle) 01257 return __first; 01258 01259 _ForwardIterator __first2 = __middle; 01260 do 01261 { 01262 std::iter_swap(__first, __first2); 01263 ++__first; 01264 ++__first2; 01265 if (__first == __middle) 01266 __middle = __first2; 01267 } 01268 while (__first2 != __last); 01269 01270 _ForwardIterator __ret = __first; 01271 01272 __first2 = __middle; 01273 01274 while (__first2 != __last) 01275 { 01276 std::iter_swap(__first, __first2); 01277 ++__first; 01278 ++__first2; 01279 if (__first == __middle) 01280 __middle = __first2; 01281 else if (__first2 == __last) 01282 __first2 = __middle; 01283 } 01284 return __ret; 01285 } 01286 01287 /// This is a helper function for the rotate algorithm. 01288 template<typename _BidirectionalIterator> 01289 _BidirectionalIterator 01290 __rotate(_BidirectionalIterator __first, 01291 _BidirectionalIterator __middle, 01292 _BidirectionalIterator __last, 01293 bidirectional_iterator_tag) 01294 { 01295 // concept requirements 01296 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< 01297 _BidirectionalIterator>) 01298 01299 if (__first == __middle) 01300 return __last; 01301 else if (__last == __middle) 01302 return __first; 01303 01304 std::__reverse(__first, __middle, bidirectional_iterator_tag()); 01305 std::__reverse(__middle, __last, bidirectional_iterator_tag()); 01306 01307 while (__first != __middle && __middle != __last) 01308 { 01309 std::iter_swap(__first, --__last); 01310 ++__first; 01311 } 01312 01313 if (__first == __middle) 01314 { 01315 std::__reverse(__middle, __last, bidirectional_iterator_tag()); 01316 return __last; 01317 } 01318 else 01319 { 01320 std::__reverse(__first, __middle, bidirectional_iterator_tag()); 01321 return __first; 01322 } 01323 } 01324 01325 /// This is a helper function for the rotate algorithm. 01326 template<typename _RandomAccessIterator> 01327 _RandomAccessIterator 01328 __rotate(_RandomAccessIterator __first, 01329 _RandomAccessIterator __middle, 01330 _RandomAccessIterator __last, 01331 random_access_iterator_tag) 01332 { 01333 // concept requirements 01334 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 01335 _RandomAccessIterator>) 01336 01337 if (__first == __middle) 01338 return __last; 01339 else if (__last == __middle) 01340 return __first; 01341 01342 typedef typename iterator_traits<_RandomAccessIterator>::difference_type 01343 _Distance; 01344 typedef typename iterator_traits<_RandomAccessIterator>::value_type 01345 _ValueType; 01346 01347 _Distance __n = __last - __first; 01348 _Distance __k = __middle - __first; 01349 01350 if (__k == __n - __k) 01351 { 01352 std::swap_ranges(__first, __middle, __middle); 01353 return __middle; 01354 } 01355 01356 _RandomAccessIterator __p = __first; 01357 _RandomAccessIterator __ret = __first + (__last - __middle); 01358 01359 for (;;) 01360 { 01361 if (__k < __n - __k) 01362 { 01363 if (__is_pod(_ValueType) && __k == 1) 01364 { 01365 _ValueType __t = _GLIBCXX_MOVE(*__p); 01366 _GLIBCXX_MOVE3(__p + 1, __p + __n, __p); 01367 *(__p + __n - 1) = _GLIBCXX_MOVE(__t); 01368 return __ret; 01369 } 01370 _RandomAccessIterator __q = __p + __k; 01371 for (_Distance __i = 0; __i < __n - __k; ++ __i) 01372 { 01373 std::iter_swap(__p, __q); 01374 ++__p; 01375 ++__q; 01376 } 01377 __n %= __k; 01378 if (__n == 0) 01379 return __ret; 01380 std::swap(__n, __k); 01381 __k = __n - __k; 01382 } 01383 else 01384 { 01385 __k = __n - __k; 01386 if (__is_pod(_ValueType) && __k == 1) 01387 { 01388 _ValueType __t = _GLIBCXX_MOVE(*(__p + __n - 1)); 01389 _GLIBCXX_MOVE_BACKWARD3(__p, __p + __n - 1, __p + __n); 01390 *__p = _GLIBCXX_MOVE(__t); 01391 return __ret; 01392 } 01393 _RandomAccessIterator __q = __p + __n; 01394 __p = __q - __k; 01395 for (_Distance __i = 0; __i < __n - __k; ++ __i) 01396 { 01397 --__p; 01398 --__q; 01399 std::iter_swap(__p, __q); 01400 } 01401 __n %= __k; 01402 if (__n == 0) 01403 return __ret; 01404 std::swap(__n, __k); 01405 } 01406 } 01407 } 01408 01409 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01410 // DR 488. rotate throws away useful information 01411 /** 01412 * @brief Rotate the elements of a sequence. 01413 * @ingroup mutating_algorithms 01414 * @param __first A forward iterator. 01415 * @param __middle A forward iterator. 01416 * @param __last A forward iterator. 01417 * @return first + (last - middle). 01418 * 01419 * Rotates the elements of the range @p [__first,__last) by 01420 * @p (__middle - __first) positions so that the element at @p __middle 01421 * is moved to @p __first, the element at @p __middle+1 is moved to 01422 * @p __first+1 and so on for each element in the range 01423 * @p [__first,__last). 01424 * 01425 * This effectively swaps the ranges @p [__first,__middle) and 01426 * @p [__middle,__last). 01427 * 01428 * Performs 01429 * @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n) 01430 * for each @p n in the range @p [0,__last-__first). 01431 */ 01432 template<typename _ForwardIterator> 01433 inline _ForwardIterator 01434 rotate(_ForwardIterator __first, _ForwardIterator __middle, 01435 _ForwardIterator __last) 01436 { 01437 // concept requirements 01438 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 01439 _ForwardIterator>) 01440 __glibcxx_requires_valid_range(__first, __middle); 01441 __glibcxx_requires_valid_range(__middle, __last); 01442 01443 return std::__rotate(__first, __middle, __last, 01444 std::__iterator_category(__first)); 01445 } 01446 01447 } // namespace _V2 01448 01449 /** 01450 * @brief Copy a sequence, rotating its elements. 01451 * @ingroup mutating_algorithms 01452 * @param __first A forward iterator. 01453 * @param __middle A forward iterator. 01454 * @param __last A forward iterator. 01455 * @param __result An output iterator. 01456 * @return An iterator designating the end of the resulting sequence. 01457 * 01458 * Copies the elements of the range @p [__first,__last) to the 01459 * range beginning at @result, rotating the copied elements by 01460 * @p (__middle-__first) positions so that the element at @p __middle 01461 * is moved to @p __result, the element at @p __middle+1 is moved 01462 * to @p __result+1 and so on for each element in the range @p 01463 * [__first,__last). 01464 * 01465 * Performs 01466 * @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n) 01467 * for each @p n in the range @p [0,__last-__first). 01468 */ 01469 template<typename _ForwardIterator, typename _OutputIterator> 01470 inline _OutputIterator 01471 rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, 01472 _ForwardIterator __last, _OutputIterator __result) 01473 { 01474 // concept requirements 01475 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 01476 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 01477 typename iterator_traits<_ForwardIterator>::value_type>) 01478 __glibcxx_requires_valid_range(__first, __middle); 01479 __glibcxx_requires_valid_range(__middle, __last); 01480 01481 return std::copy(__first, __middle, 01482 std::copy(__middle, __last, __result)); 01483 } 01484 01485 /// This is a helper function... 01486 template<typename _ForwardIterator, typename _Predicate> 01487 _ForwardIterator 01488 __partition(_ForwardIterator __first, _ForwardIterator __last, 01489 _Predicate __pred, forward_iterator_tag) 01490 { 01491 if (__first == __last) 01492 return __first; 01493 01494 while (__pred(*__first)) 01495 if (++__first == __last) 01496 return __first; 01497 01498 _ForwardIterator __next = __first; 01499 01500 while (++__next != __last) 01501 if (__pred(*__next)) 01502 { 01503 std::iter_swap(__first, __next); 01504 ++__first; 01505 } 01506 01507 return __first; 01508 } 01509 01510 /// This is a helper function... 01511 template<typename _BidirectionalIterator, typename _Predicate> 01512 _BidirectionalIterator 01513 __partition(_BidirectionalIterator __first, _BidirectionalIterator __last, 01514 _Predicate __pred, bidirectional_iterator_tag) 01515 { 01516 while (true) 01517 { 01518 while (true) 01519 if (__first == __last) 01520 return __first; 01521 else if (__pred(*__first)) 01522 ++__first; 01523 else 01524 break; 01525 --__last; 01526 while (true) 01527 if (__first == __last) 01528 return __first; 01529 else if (!bool(__pred(*__last))) 01530 --__last; 01531 else 01532 break; 01533 std::iter_swap(__first, __last); 01534 ++__first; 01535 } 01536 } 01537 01538 // partition 01539 01540 /// This is a helper function... 01541 /// Requires __first != __last and !__pred(__first) 01542 /// and __len == distance(__first, __last). 01543 /// 01544 /// !__pred(__first) allows us to guarantee that we don't 01545 /// move-assign an element onto itself. 01546 template<typename _ForwardIterator, typename _Pointer, typename _Predicate, 01547 typename _Distance> 01548 _ForwardIterator 01549 __stable_partition_adaptive(_ForwardIterator __first, 01550 _ForwardIterator __last, 01551 _Predicate __pred, _Distance __len, 01552 _Pointer __buffer, 01553 _Distance __buffer_size) 01554 { 01555 if (__len == 1) 01556 return __first; 01557 01558 if (__len <= __buffer_size) 01559 { 01560 _ForwardIterator __result1 = __first; 01561 _Pointer __result2 = __buffer; 01562 01563 // The precondition guarantees that !__pred(__first), so 01564 // move that element to the buffer before starting the loop. 01565 // This ensures that we only call __pred once per element. 01566 *__result2 = _GLIBCXX_MOVE(*__first); 01567 ++__result2; 01568 ++__first; 01569 for (; __first != __last; ++__first) 01570 if (__pred(__first)) 01571 { 01572 *__result1 = _GLIBCXX_MOVE(*__first); 01573 ++__result1; 01574 } 01575 else 01576 { 01577 *__result2 = _GLIBCXX_MOVE(*__first); 01578 ++__result2; 01579 } 01580 01581 _GLIBCXX_MOVE3(__buffer, __result2, __result1); 01582 return __result1; 01583 } 01584 01585 _ForwardIterator __middle = __first; 01586 std::advance(__middle, __len / 2); 01587 _ForwardIterator __left_split = 01588 std::__stable_partition_adaptive(__first, __middle, __pred, 01589 __len / 2, __buffer, 01590 __buffer_size); 01591 01592 // Advance past true-predicate values to satisfy this 01593 // function's preconditions. 01594 _Distance __right_len = __len - __len / 2; 01595 _ForwardIterator __right_split = 01596 std::__find_if_not_n(__middle, __right_len, __pred); 01597 01598 if (__right_len) 01599 __right_split = 01600 std::__stable_partition_adaptive(__right_split, __last, __pred, 01601 __right_len, 01602 __buffer, __buffer_size); 01603 01604 return std::rotate(__left_split, __middle, __right_split); 01605 } 01606 01607 template<typename _ForwardIterator, typename _Predicate> 01608 _ForwardIterator 01609 __stable_partition(_ForwardIterator __first, _ForwardIterator __last, 01610 _Predicate __pred) 01611 { 01612 __first = std::__find_if_not(__first, __last, __pred); 01613 01614 if (__first == __last) 01615 return __first; 01616 01617 typedef typename iterator_traits<_ForwardIterator>::value_type 01618 _ValueType; 01619 typedef typename iterator_traits<_ForwardIterator>::difference_type 01620 _DistanceType; 01621 01622 _Temporary_buffer<_ForwardIterator, _ValueType> 01623 __buf(__first, std::distance(__first, __last)); 01624 return 01625 std::__stable_partition_adaptive(__first, __last, __pred, 01626 _DistanceType(__buf.requested_size()), 01627 __buf.begin(), 01628 _DistanceType(__buf.size())); 01629 } 01630 01631 /** 01632 * @brief Move elements for which a predicate is true to the beginning 01633 * of a sequence, preserving relative ordering. 01634 * @ingroup mutating_algorithms 01635 * @param __first A forward iterator. 01636 * @param __last A forward iterator. 01637 * @param __pred A predicate functor. 01638 * @return An iterator @p middle such that @p __pred(i) is true for each 01639 * iterator @p i in the range @p [first,middle) and false for each @p i 01640 * in the range @p [middle,last). 01641 * 01642 * Performs the same function as @p partition() with the additional 01643 * guarantee that the relative ordering of elements in each group is 01644 * preserved, so any two elements @p x and @p y in the range 01645 * @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same 01646 * relative ordering after calling @p stable_partition(). 01647 */ 01648 template<typename _ForwardIterator, typename _Predicate> 01649 inline _ForwardIterator 01650 stable_partition(_ForwardIterator __first, _ForwardIterator __last, 01651 _Predicate __pred) 01652 { 01653 // concept requirements 01654 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 01655 _ForwardIterator>) 01656 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 01657 typename iterator_traits<_ForwardIterator>::value_type>) 01658 __glibcxx_requires_valid_range(__first, __last); 01659 01660 return std::__stable_partition(__first, __last, 01661 __gnu_cxx::__ops::__pred_iter(__pred)); 01662 } 01663 01664 /// This is a helper function for the sort routines. 01665 template<typename _RandomAccessIterator, typename _Compare> 01666 void 01667 __heap_select(_RandomAccessIterator __first, 01668 _RandomAccessIterator __middle, 01669 _RandomAccessIterator __last, _Compare __comp) 01670 { 01671 std::__make_heap(__first, __middle, __comp); 01672 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i) 01673 if (__comp(__i, __first)) 01674 std::__pop_heap(__first, __middle, __i, __comp); 01675 } 01676 01677 // partial_sort 01678 01679 template<typename _InputIterator, typename _RandomAccessIterator, 01680 typename _Compare> 01681 _RandomAccessIterator 01682 __partial_sort_copy(_InputIterator __first, _InputIterator __last, 01683 _RandomAccessIterator __result_first, 01684 _RandomAccessIterator __result_last, 01685 _Compare __comp) 01686 { 01687 typedef typename iterator_traits<_InputIterator>::value_type 01688 _InputValueType; 01689 typedef iterator_traits<_RandomAccessIterator> _RItTraits; 01690 typedef typename _RItTraits::difference_type _DistanceType; 01691 01692 if (__result_first == __result_last) 01693 return __result_last; 01694 _RandomAccessIterator __result_real_last = __result_first; 01695 while (__first != __last && __result_real_last != __result_last) 01696 { 01697 *__result_real_last = *__first; 01698 ++__result_real_last; 01699 ++__first; 01700 } 01701 01702 std::__make_heap(__result_first, __result_real_last, __comp); 01703 while (__first != __last) 01704 { 01705 if (__comp(__first, __result_first)) 01706 std::__adjust_heap(__result_first, _DistanceType(0), 01707 _DistanceType(__result_real_last 01708 - __result_first), 01709 _InputValueType(*__first), __comp); 01710 ++__first; 01711 } 01712 std::__sort_heap(__result_first, __result_real_last, __comp); 01713 return __result_real_last; 01714 } 01715 01716 /** 01717 * @brief Copy the smallest elements of a sequence. 01718 * @ingroup sorting_algorithms 01719 * @param __first An iterator. 01720 * @param __last Another iterator. 01721 * @param __result_first A random-access iterator. 01722 * @param __result_last Another random-access iterator. 01723 * @return An iterator indicating the end of the resulting sequence. 01724 * 01725 * Copies and sorts the smallest N values from the range @p [__first,__last) 01726 * to the range beginning at @p __result_first, where the number of 01727 * elements to be copied, @p N, is the smaller of @p (__last-__first) and 01728 * @p (__result_last-__result_first). 01729 * After the sort if @e i and @e j are iterators in the range 01730 * @p [__result_first,__result_first+N) such that i precedes j then 01731 * *j<*i is false. 01732 * The value returned is @p __result_first+N. 01733 */ 01734 template<typename _InputIterator, typename _RandomAccessIterator> 01735 inline _RandomAccessIterator 01736 partial_sort_copy(_InputIterator __first, _InputIterator __last, 01737 _RandomAccessIterator __result_first, 01738 _RandomAccessIterator __result_last) 01739 { 01740 #ifdef _GLIBCXX_CONCEPT_CHECKS 01741 typedef typename iterator_traits<_InputIterator>::value_type 01742 _InputValueType; 01743 typedef typename iterator_traits<_RandomAccessIterator>::value_type 01744 _OutputValueType; 01745 #endif 01746 01747 // concept requirements 01748 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 01749 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType, 01750 _OutputValueType>) 01751 __glibcxx_function_requires(_LessThanOpConcept<_InputValueType, 01752 _OutputValueType>) 01753 __glibcxx_function_requires(_LessThanComparableConcept<_OutputValueType>) 01754 __glibcxx_requires_valid_range(__first, __last); 01755 __glibcxx_requires_irreflexive(__first, __last); 01756 __glibcxx_requires_valid_range(__result_first, __result_last); 01757 01758 return std::__partial_sort_copy(__first, __last, 01759 __result_first, __result_last, 01760 __gnu_cxx::__ops::__iter_less_iter()); 01761 } 01762 01763 /** 01764 * @brief Copy the smallest elements of a sequence using a predicate for 01765 * comparison. 01766 * @ingroup sorting_algorithms 01767 * @param __first An input iterator. 01768 * @param __last Another input iterator. 01769 * @param __result_first A random-access iterator. 01770 * @param __result_last Another random-access iterator. 01771 * @param __comp A comparison functor. 01772 * @return An iterator indicating the end of the resulting sequence. 01773 * 01774 * Copies and sorts the smallest N values from the range @p [__first,__last) 01775 * to the range beginning at @p result_first, where the number of 01776 * elements to be copied, @p N, is the smaller of @p (__last-__first) and 01777 * @p (__result_last-__result_first). 01778 * After the sort if @e i and @e j are iterators in the range 01779 * @p [__result_first,__result_first+N) such that i precedes j then 01780 * @p __comp(*j,*i) is false. 01781 * The value returned is @p __result_first+N. 01782 */ 01783 template<typename _InputIterator, typename _RandomAccessIterator, 01784 typename _Compare> 01785 inline _RandomAccessIterator 01786 partial_sort_copy(_InputIterator __first, _InputIterator __last, 01787 _RandomAccessIterator __result_first, 01788 _RandomAccessIterator __result_last, 01789 _Compare __comp) 01790 { 01791 #ifdef _GLIBCXX_CONCEPT_CHECKS 01792 typedef typename iterator_traits<_InputIterator>::value_type 01793 _InputValueType; 01794 typedef typename iterator_traits<_RandomAccessIterator>::value_type 01795 _OutputValueType; 01796 #endif 01797 01798 // concept requirements 01799 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 01800 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 01801 _RandomAccessIterator>) 01802 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType, 01803 _OutputValueType>) 01804 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 01805 _InputValueType, _OutputValueType>) 01806 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 01807 _OutputValueType, _OutputValueType>) 01808 __glibcxx_requires_valid_range(__first, __last); 01809 __glibcxx_requires_irreflexive_pred(__first, __last, __comp); 01810 __glibcxx_requires_valid_range(__result_first, __result_last); 01811 01812 return std::__partial_sort_copy(__first, __last, 01813 __result_first, __result_last, 01814 __gnu_cxx::__ops::__iter_comp_iter(__comp)); 01815 } 01816 01817 /// This is a helper function for the sort routine. 01818 template<typename _RandomAccessIterator, typename _Compare> 01819 void 01820 __unguarded_linear_insert(_RandomAccessIterator __last, 01821 _Compare __comp) 01822 { 01823 typename iterator_traits<_RandomAccessIterator>::value_type 01824 __val = _GLIBCXX_MOVE(*__last); 01825 _RandomAccessIterator __next = __last; 01826 --__next; 01827 while (__comp(__val, __next)) 01828 { 01829 *__last = _GLIBCXX_MOVE(*__next); 01830 __last = __next; 01831 --__next; 01832 } 01833 *__last = _GLIBCXX_MOVE(__val); 01834 } 01835 01836 /// This is a helper function for the sort routine. 01837 template<typename _RandomAccessIterator, typename _Compare> 01838 void 01839 __insertion_sort(_RandomAccessIterator __first, 01840 _RandomAccessIterator __last, _Compare __comp) 01841 { 01842 if (__first == __last) return; 01843 01844 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) 01845 { 01846 if (__comp(__i, __first)) 01847 { 01848 typename iterator_traits<_RandomAccessIterator>::value_type 01849 __val = _GLIBCXX_MOVE(*__i); 01850 _GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + 1); 01851 *__first = _GLIBCXX_MOVE(__val); 01852 } 01853 else 01854 std::__unguarded_linear_insert(__i, 01855 __gnu_cxx::__ops::__val_comp_iter(__comp)); 01856 } 01857 } 01858 01859 /// This is a helper function for the sort routine. 01860 template<typename _RandomAccessIterator, typename _Compare> 01861 inline void 01862 __unguarded_insertion_sort(_RandomAccessIterator __first, 01863 _RandomAccessIterator __last, _Compare __comp) 01864 { 01865 for (_RandomAccessIterator __i = __first; __i != __last; ++__i) 01866 std::__unguarded_linear_insert(__i, 01867 __gnu_cxx::__ops::__val_comp_iter(__comp)); 01868 } 01869 01870 /** 01871 * @doctodo 01872 * This controls some aspect of the sort routines. 01873 */ 01874 enum { _S_threshold = 16 }; 01875 01876 /// This is a helper function for the sort routine. 01877 template<typename _RandomAccessIterator, typename _Compare> 01878 void 01879 __final_insertion_sort(_RandomAccessIterator __first, 01880 _RandomAccessIterator __last, _Compare __comp) 01881 { 01882 if (__last - __first > int(_S_threshold)) 01883 { 01884 std::__insertion_sort(__first, __first + int(_S_threshold), __comp); 01885 std::__unguarded_insertion_sort(__first + int(_S_threshold), __last, 01886 __comp); 01887 } 01888 else 01889 std::__insertion_sort(__first, __last, __comp); 01890 } 01891 01892 /// This is a helper function... 01893 template<typename _RandomAccessIterator, typename _Compare> 01894 _RandomAccessIterator 01895 __unguarded_partition(_RandomAccessIterator __first, 01896 _RandomAccessIterator __last, 01897 _RandomAccessIterator __pivot, _Compare __comp) 01898 { 01899 while (true) 01900 { 01901 while (__comp(__first, __pivot)) 01902 ++__first; 01903 --__last; 01904 while (__comp(__pivot, __last)) 01905 --__last; 01906 if (!(__first < __last)) 01907 return __first; 01908 std::iter_swap(__first, __last); 01909 ++__first; 01910 } 01911 } 01912 01913 /// This is a helper function... 01914 template<typename _RandomAccessIterator, typename _Compare> 01915 inline _RandomAccessIterator 01916 __unguarded_partition_pivot(_RandomAccessIterator __first, 01917 _RandomAccessIterator __last, _Compare __comp) 01918 { 01919 _RandomAccessIterator __mid = __first + (__last - __first) / 2; 01920 std::__move_median_to_first(__first, __first + 1, __mid, __last - 1, 01921 __comp); 01922 return std::__unguarded_partition(__first + 1, __last, __first, __comp); 01923 } 01924 01925 template<typename _RandomAccessIterator, typename _Compare> 01926 inline void 01927 __partial_sort(_RandomAccessIterator __first, 01928 _RandomAccessIterator __middle, 01929 _RandomAccessIterator __last, 01930 _Compare __comp) 01931 { 01932 std::__heap_select(__first, __middle, __last, __comp); 01933 std::__sort_heap(__first, __middle, __comp); 01934 } 01935 01936 /// This is a helper function for the sort routine. 01937 template<typename _RandomAccessIterator, typename _Size, typename _Compare> 01938 void 01939 __introsort_loop(_RandomAccessIterator __first, 01940 _RandomAccessIterator __last, 01941 _Size __depth_limit, _Compare __comp) 01942 { 01943 while (__last - __first > int(_S_threshold)) 01944 { 01945 if (__depth_limit == 0) 01946 { 01947 std::__partial_sort(__first, __last, __last, __comp); 01948 return; 01949 } 01950 --__depth_limit; 01951 _RandomAccessIterator __cut = 01952 std::__unguarded_partition_pivot(__first, __last, __comp); 01953 std::__introsort_loop(__cut, __last, __depth_limit, __comp); 01954 __last = __cut; 01955 } 01956 } 01957 01958 // sort 01959 01960 template<typename _RandomAccessIterator, typename _Compare> 01961 inline void 01962 __sort(_RandomAccessIterator __first, _RandomAccessIterator __last, 01963 _Compare __comp) 01964 { 01965 if (__first != __last) 01966 { 01967 std::__introsort_loop(__first, __last, 01968 std::__lg(__last - __first) * 2, 01969 __comp); 01970 std::__final_insertion_sort(__first, __last, __comp); 01971 } 01972 } 01973 01974 template<typename _RandomAccessIterator, typename _Size, typename _Compare> 01975 void 01976 __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth, 01977 _RandomAccessIterator __last, _Size __depth_limit, 01978 _Compare __comp) 01979 { 01980 while (__last - __first > 3) 01981 { 01982 if (__depth_limit == 0) 01983 { 01984 std::__heap_select(__first, __nth + 1, __last, __comp); 01985 // Place the nth largest element in its final position. 01986 std::iter_swap(__first, __nth); 01987 return; 01988 } 01989 --__depth_limit; 01990 _RandomAccessIterator __cut = 01991 std::__unguarded_partition_pivot(__first, __last, __comp); 01992 if (__cut <= __nth) 01993 __first = __cut; 01994 else 01995 __last = __cut; 01996 } 01997 std::__insertion_sort(__first, __last, __comp); 01998 } 01999 02000 // nth_element 02001 02002 // lower_bound moved to stl_algobase.h 02003 02004 /** 02005 * @brief Finds the first position in which @p __val could be inserted 02006 * without changing the ordering. 02007 * @ingroup binary_search_algorithms 02008 * @param __first An iterator. 02009 * @param __last Another iterator. 02010 * @param __val The search term. 02011 * @param __comp A functor to use for comparisons. 02012 * @return An iterator pointing to the first element <em>not less 02013 * than</em> @p __val, or end() if every element is less 02014 * than @p __val. 02015 * @ingroup binary_search_algorithms 02016 * 02017 * The comparison function should have the same effects on ordering as 02018 * the function used for the initial sort. 02019 */ 02020 template<typename _ForwardIterator, typename _Tp, typename _Compare> 02021 inline _ForwardIterator 02022 lower_bound(_ForwardIterator __first, _ForwardIterator __last, 02023 const _Tp& __val, _Compare __comp) 02024 { 02025 // concept requirements 02026 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 02027 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 02028 typename iterator_traits<_ForwardIterator>::value_type, _Tp>) 02029 __glibcxx_requires_partitioned_lower_pred(__first, __last, 02030 __val, __comp); 02031 02032 return std::__lower_bound(__first, __last, __val, 02033 __gnu_cxx::__ops::__iter_comp_val(__comp)); 02034 } 02035 02036 template<typename _ForwardIterator, typename _Tp, typename _Compare> 02037 _ForwardIterator 02038 __upper_bound(_ForwardIterator __first, _ForwardIterator __last, 02039 const _Tp& __val, _Compare __comp) 02040 { 02041 typedef typename iterator_traits<_ForwardIterator>::difference_type 02042 _DistanceType; 02043 02044 _DistanceType __len = std::distance(__first, __last); 02045 02046 while (__len > 0) 02047 { 02048 _DistanceType __half = __len >> 1; 02049 _ForwardIterator __middle = __first; 02050 std::advance(__middle, __half); 02051 if (__comp(__val, __middle)) 02052 __len = __half; 02053 else 02054 { 02055 __first = __middle; 02056 ++__first; 02057 __len = __len - __half - 1; 02058 } 02059 } 02060 return __first; 02061 } 02062 02063 /** 02064 * @brief Finds the last position in which @p __val could be inserted 02065 * without changing the ordering. 02066 * @ingroup binary_search_algorithms 02067 * @param __first An iterator. 02068 * @param __last Another iterator. 02069 * @param __val The search term. 02070 * @return An iterator pointing to the first element greater than @p __val, 02071 * or end() if no elements are greater than @p __val. 02072 * @ingroup binary_search_algorithms 02073 */ 02074 template<typename _ForwardIterator, typename _Tp> 02075 inline _ForwardIterator 02076 upper_bound(_ForwardIterator __first, _ForwardIterator __last, 02077 const _Tp& __val) 02078 { 02079 // concept requirements 02080 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 02081 __glibcxx_function_requires(_LessThanOpConcept< 02082 _Tp, typename iterator_traits<_ForwardIterator>::value_type>) 02083 __glibcxx_requires_partitioned_upper(__first, __last, __val); 02084 02085 return std::__upper_bound(__first, __last, __val, 02086 __gnu_cxx::__ops::__val_less_iter()); 02087 } 02088 02089 /** 02090 * @brief Finds the last position in which @p __val could be inserted 02091 * without changing the ordering. 02092 * @ingroup binary_search_algorithms 02093 * @param __first An iterator. 02094 * @param __last Another iterator. 02095 * @param __val The search term. 02096 * @param __comp A functor to use for comparisons. 02097 * @return An iterator pointing to the first element greater than @p __val, 02098 * or end() if no elements are greater than @p __val. 02099 * @ingroup binary_search_algorithms 02100 * 02101 * The comparison function should have the same effects on ordering as 02102 * the function used for the initial sort. 02103 */ 02104 template<typename _ForwardIterator, typename _Tp, typename _Compare> 02105 inline _ForwardIterator 02106 upper_bound(_ForwardIterator __first, _ForwardIterator __last, 02107 const _Tp& __val, _Compare __comp) 02108 { 02109 // concept requirements 02110 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 02111 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 02112 _Tp, typename iterator_traits<_ForwardIterator>::value_type>) 02113 __glibcxx_requires_partitioned_upper_pred(__first, __last, 02114 __val, __comp); 02115 02116 return std::__upper_bound(__first, __last, __val, 02117 __gnu_cxx::__ops::__val_comp_iter(__comp)); 02118 } 02119 02120 template<typename _ForwardIterator, typename _Tp, 02121 typename _CompareItTp, typename _CompareTpIt> 02122 pair<_ForwardIterator, _ForwardIterator> 02123 __equal_range(_ForwardIterator __first, _ForwardIterator __last, 02124 const _Tp& __val, 02125 _CompareItTp __comp_it_val, _CompareTpIt __comp_val_it) 02126 { 02127 typedef typename iterator_traits<_ForwardIterator>::difference_type 02128 _DistanceType; 02129 02130 _DistanceType __len = std::distance(__first, __last); 02131 02132 while (__len > 0) 02133 { 02134 _DistanceType __half = __len >> 1; 02135 _ForwardIterator __middle = __first; 02136 std::advance(__middle, __half); 02137 if (__comp_it_val(__middle, __val)) 02138 { 02139 __first = __middle; 02140 ++__first; 02141 __len = __len - __half - 1; 02142 } 02143 else if (__comp_val_it(__val, __middle)) 02144 __len = __half; 02145 else 02146 { 02147 _ForwardIterator __left 02148 = std::__lower_bound(__first, __middle, __val, __comp_it_val); 02149 std::advance(__first, __len); 02150 _ForwardIterator __right 02151 = std::__upper_bound(++__middle, __first, __val, __comp_val_it); 02152 return pair<_ForwardIterator, _ForwardIterator>(__left, __right); 02153 } 02154 } 02155 return pair<_ForwardIterator, _ForwardIterator>(__first, __first); 02156 } 02157 02158 /** 02159 * @brief Finds the largest subrange in which @p __val could be inserted 02160 * at any place in it without changing the ordering. 02161 * @ingroup binary_search_algorithms 02162 * @param __first An iterator. 02163 * @param __last Another iterator. 02164 * @param __val The search term. 02165 * @return An pair of iterators defining the subrange. 02166 * @ingroup binary_search_algorithms 02167 * 02168 * This is equivalent to 02169 * @code 02170 * std::make_pair(lower_bound(__first, __last, __val), 02171 * upper_bound(__first, __last, __val)) 02172 * @endcode 02173 * but does not actually call those functions. 02174 */ 02175 template<typename _ForwardIterator, typename _Tp> 02176 inline pair<_ForwardIterator, _ForwardIterator> 02177 equal_range(_ForwardIterator __first, _ForwardIterator __last, 02178 const _Tp& __val) 02179 { 02180 // concept requirements 02181 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 02182 __glibcxx_function_requires(_LessThanOpConcept< 02183 typename iterator_traits<_ForwardIterator>::value_type, _Tp>) 02184 __glibcxx_function_requires(_LessThanOpConcept< 02185 _Tp, typename iterator_traits<_ForwardIterator>::value_type>) 02186 __glibcxx_requires_partitioned_lower(__first, __last, __val); 02187 __glibcxx_requires_partitioned_upper(__first, __last, __val); 02188 02189 return std::__equal_range(__first, __last, __val, 02190 __gnu_cxx::__ops::__iter_less_val(), 02191 __gnu_cxx::__ops::__val_less_iter()); 02192 } 02193 02194 /** 02195 * @brief Finds the largest subrange in which @p __val could be inserted 02196 * at any place in it without changing the ordering. 02197 * @param __first An iterator. 02198 * @param __last Another iterator. 02199 * @param __val The search term. 02200 * @param __comp A functor to use for comparisons. 02201 * @return An pair of iterators defining the subrange. 02202 * @ingroup binary_search_algorithms 02203 * 02204 * This is equivalent to 02205 * @code 02206 * std::make_pair(lower_bound(__first, __last, __val, __comp), 02207 * upper_bound(__first, __last, __val, __comp)) 02208 * @endcode 02209 * but does not actually call those functions. 02210 */ 02211 template<typename _ForwardIterator, typename _Tp, typename _Compare> 02212 inline pair<_ForwardIterator, _ForwardIterator> 02213 equal_range(_ForwardIterator __first, _ForwardIterator __last, 02214 const _Tp& __val, _Compare __comp) 02215 { 02216 // concept requirements 02217 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 02218 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 02219 typename iterator_traits<_ForwardIterator>::value_type, _Tp>) 02220 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 02221 _Tp, typename iterator_traits<_ForwardIterator>::value_type>) 02222 __glibcxx_requires_partitioned_lower_pred(__first, __last, 02223 __val, __comp); 02224 __glibcxx_requires_partitioned_upper_pred(__first, __last, 02225 __val, __comp); 02226 02227 return std::__equal_range(__first, __last, __val, 02228 __gnu_cxx::__ops::__iter_comp_val(__comp), 02229 __gnu_cxx::__ops::__val_comp_iter(__comp)); 02230 } 02231 02232 /** 02233 * @brief Determines whether an element exists in a range. 02234 * @ingroup binary_search_algorithms 02235 * @param __first An iterator. 02236 * @param __last Another iterator. 02237 * @param __val The search term. 02238 * @return True if @p __val (or its equivalent) is in [@p 02239 * __first,@p __last ]. 02240 * 02241 * Note that this does not actually return an iterator to @p __val. For 02242 * that, use std::find or a container's specialized find member functions. 02243 */ 02244 template<typename _ForwardIterator, typename _Tp> 02245 bool 02246 binary_search(_ForwardIterator __first, _ForwardIterator __last, 02247 const _Tp& __val) 02248 { 02249 // concept requirements 02250 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 02251 __glibcxx_function_requires(_LessThanOpConcept< 02252 _Tp, typename iterator_traits<_ForwardIterator>::value_type>) 02253 __glibcxx_requires_partitioned_lower(__first, __last, __val); 02254 __glibcxx_requires_partitioned_upper(__first, __last, __val); 02255 02256 _ForwardIterator __i 02257 = std::__lower_bound(__first, __last, __val, 02258 __gnu_cxx::__ops::__iter_less_val()); 02259 return __i != __last && !(__val < *__i); 02260 } 02261 02262 /** 02263 * @brief Determines whether an element exists in a range. 02264 * @ingroup binary_search_algorithms 02265 * @param __first An iterator. 02266 * @param __last Another iterator. 02267 * @param __val The search term. 02268 * @param __comp A functor to use for comparisons. 02269 * @return True if @p __val (or its equivalent) is in @p [__first,__last]. 02270 * 02271 * Note that this does not actually return an iterator to @p __val. For 02272 * that, use std::find or a container's specialized find member functions. 02273 * 02274 * The comparison function should have the same effects on ordering as 02275 * the function used for the initial sort. 02276 */ 02277 template<typename _ForwardIterator, typename _Tp, typename _Compare> 02278 bool 02279 binary_search(_ForwardIterator __first, _ForwardIterator __last, 02280 const _Tp& __val, _Compare __comp) 02281 { 02282 // concept requirements 02283 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 02284 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 02285 _Tp, typename iterator_traits<_ForwardIterator>::value_type>) 02286 __glibcxx_requires_partitioned_lower_pred(__first, __last, 02287 __val, __comp); 02288 __glibcxx_requires_partitioned_upper_pred(__first, __last, 02289 __val, __comp); 02290 02291 _ForwardIterator __i 02292 = std::__lower_bound(__first, __last, __val, 02293 __gnu_cxx::__ops::__iter_comp_val(__comp)); 02294 return __i != __last && !bool(__comp(__val, *__i)); 02295 } 02296 02297 // merge 02298 02299 /// This is a helper function for the __merge_adaptive routines. 02300 template<typename _InputIterator1, typename _InputIterator2, 02301 typename _OutputIterator, typename _Compare> 02302 void 02303 __move_merge_adaptive(_InputIterator1 __first1, _InputIterator1 __last1, 02304 _InputIterator2 __first2, _InputIterator2 __last2, 02305 _OutputIterator __result, _Compare __comp) 02306 { 02307 while (__first1 != __last1 && __first2 != __last2) 02308 { 02309 if (__comp(__first2, __first1)) 02310 { 02311 *__result = _GLIBCXX_MOVE(*__first2); 02312 ++__first2; 02313 } 02314 else 02315 { 02316 *__result = _GLIBCXX_MOVE(*__first1); 02317 ++__first1; 02318 } 02319 ++__result; 02320 } 02321 if (__first1 != __last1) 02322 _GLIBCXX_MOVE3(__first1, __last1, __result); 02323 } 02324 02325 /// This is a helper function for the __merge_adaptive routines. 02326 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2, 02327 typename _BidirectionalIterator3, typename _Compare> 02328 void 02329 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1, 02330 _BidirectionalIterator1 __last1, 02331 _BidirectionalIterator2 __first2, 02332 _BidirectionalIterator2 __last2, 02333 _BidirectionalIterator3 __result, 02334 _Compare __comp) 02335 { 02336 if (__first1 == __last1) 02337 { 02338 _GLIBCXX_MOVE_BACKWARD3(__first2, __last2, __result); 02339 return; 02340 } 02341 else if (__first2 == __last2) 02342 return; 02343 02344 --__last1; 02345 --__last2; 02346 while (true) 02347 { 02348 if (__comp(__last2, __last1)) 02349 { 02350 *--__result = _GLIBCXX_MOVE(*__last1); 02351 if (__first1 == __last1) 02352 { 02353 _GLIBCXX_MOVE_BACKWARD3(__first2, ++__last2, __result); 02354 return; 02355 } 02356 --__last1; 02357 } 02358 else 02359 { 02360 *--__result = _GLIBCXX_MOVE(*__last2); 02361 if (__first2 == __last2) 02362 return; 02363 --__last2; 02364 } 02365 } 02366 } 02367 02368 /// This is a helper function for the merge routines. 02369 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2, 02370 typename _Distance> 02371 _BidirectionalIterator1 02372 __rotate_adaptive(_BidirectionalIterator1 __first, 02373 _BidirectionalIterator1 __middle, 02374 _BidirectionalIterator1 __last, 02375 _Distance __len1, _Distance __len2, 02376 _BidirectionalIterator2 __buffer, 02377 _Distance __buffer_size) 02378 { 02379 _BidirectionalIterator2 __buffer_end; 02380 if (__len1 > __len2 && __len2 <= __buffer_size) 02381 { 02382 if (__len2) 02383 { 02384 __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer); 02385 _GLIBCXX_MOVE_BACKWARD3(__first, __middle, __last); 02386 return _GLIBCXX_MOVE3(__buffer, __buffer_end, __first); 02387 } 02388 else 02389 return __first; 02390 } 02391 else if (__len1 <= __buffer_size) 02392 { 02393 if (__len1) 02394 { 02395 __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer); 02396 _GLIBCXX_MOVE3(__middle, __last, __first); 02397 return _GLIBCXX_MOVE_BACKWARD3(__buffer, __buffer_end, __last); 02398 } 02399 else 02400 return __last; 02401 } 02402 else 02403 return std::rotate(__first, __middle, __last); 02404 } 02405 02406 /// This is a helper function for the merge routines. 02407 template<typename _BidirectionalIterator, typename _Distance, 02408 typename _Pointer, typename _Compare> 02409 void 02410 __merge_adaptive(_BidirectionalIterator __first, 02411 _BidirectionalIterator __middle, 02412 _BidirectionalIterator __last, 02413 _Distance __len1, _Distance __len2, 02414 _Pointer __buffer, _Distance __buffer_size, 02415 _Compare __comp) 02416 { 02417 if (__len1 <= __len2 && __len1 <= __buffer_size) 02418 { 02419 _Pointer __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer); 02420 std::__move_merge_adaptive(__buffer, __buffer_end, __middle, __last, 02421 __first, __comp); 02422 } 02423 else if (__len2 <= __buffer_size) 02424 { 02425 _Pointer __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer); 02426 std::__move_merge_adaptive_backward(__first, __middle, __buffer, 02427 __buffer_end, __last, __comp); 02428 } 02429 else 02430 { 02431 _BidirectionalIterator __first_cut = __first; 02432 _BidirectionalIterator __second_cut = __middle; 02433 _Distance __len11 = 0; 02434 _Distance __len22 = 0; 02435 if (__len1 > __len2) 02436 { 02437 __len11 = __len1 / 2; 02438 std::advance(__first_cut, __len11); 02439 __second_cut 02440 = std::__lower_bound(__middle, __last, *__first_cut, 02441 __gnu_cxx::__ops::__iter_comp_val(__comp)); 02442 __len22 = std::distance(__middle, __second_cut); 02443 } 02444 else 02445 { 02446 __len22 = __len2 / 2; 02447 std::advance(__second_cut, __len22); 02448 __first_cut 02449 = std::__upper_bound(__first, __middle, *__second_cut, 02450 __gnu_cxx::__ops::__val_comp_iter(__comp)); 02451 __len11 = std::distance(__first, __first_cut); 02452 } 02453 02454 _BidirectionalIterator __new_middle 02455 = std::__rotate_adaptive(__first_cut, __middle, __second_cut, 02456 __len1 - __len11, __len22, __buffer, 02457 __buffer_size); 02458 std::__merge_adaptive(__first, __first_cut, __new_middle, __len11, 02459 __len22, __buffer, __buffer_size, __comp); 02460 std::__merge_adaptive(__new_middle, __second_cut, __last, 02461 __len1 - __len11, 02462 __len2 - __len22, __buffer, 02463 __buffer_size, __comp); 02464 } 02465 } 02466 02467 /// This is a helper function for the merge routines. 02468 template<typename _BidirectionalIterator, typename _Distance, 02469 typename _Compare> 02470 void 02471 __merge_without_buffer(_BidirectionalIterator __first, 02472 _BidirectionalIterator __middle, 02473 _BidirectionalIterator __last, 02474 _Distance __len1, _Distance __len2, 02475 _Compare __comp) 02476 { 02477 if (__len1 == 0 || __len2 == 0) 02478 return; 02479 02480 if (__len1 + __len2 == 2) 02481 { 02482 if (__comp(__middle, __first)) 02483 std::iter_swap(__first, __middle); 02484 return; 02485 } 02486 02487 _BidirectionalIterator __first_cut = __first; 02488 _BidirectionalIterator __second_cut = __middle; 02489 _Distance __len11 = 0; 02490 _Distance __len22 = 0; 02491 if (__len1 > __len2) 02492 { 02493 __len11 = __len1 / 2; 02494 std::advance(__first_cut, __len11); 02495 __second_cut 02496 = std::__lower_bound(__middle, __last, *__first_cut, 02497 __gnu_cxx::__ops::__iter_comp_val(__comp)); 02498 __len22 = std::distance(__middle, __second_cut); 02499 } 02500 else 02501 { 02502 __len22 = __len2 / 2; 02503 std::advance(__second_cut, __len22); 02504 __first_cut 02505 = std::__upper_bound(__first, __middle, *__second_cut, 02506 __gnu_cxx::__ops::__val_comp_iter(__comp)); 02507 __len11 = std::distance(__first, __first_cut); 02508 } 02509 02510 _BidirectionalIterator __new_middle 02511 = std::rotate(__first_cut, __middle, __second_cut); 02512 std::__merge_without_buffer(__first, __first_cut, __new_middle, 02513 __len11, __len22, __comp); 02514 std::__merge_without_buffer(__new_middle, __second_cut, __last, 02515 __len1 - __len11, __len2 - __len22, __comp); 02516 } 02517 02518 template<typename _BidirectionalIterator, typename _Compare> 02519 void 02520 __inplace_merge(_BidirectionalIterator __first, 02521 _BidirectionalIterator __middle, 02522 _BidirectionalIterator __last, 02523 _Compare __comp) 02524 { 02525 typedef typename iterator_traits<_BidirectionalIterator>::value_type 02526 _ValueType; 02527 typedef typename iterator_traits<_BidirectionalIterator>::difference_type 02528 _DistanceType; 02529 02530 if (__first == __middle || __middle == __last) 02531 return; 02532 02533 const _DistanceType __len1 = std::distance(__first, __middle); 02534 const _DistanceType __len2 = std::distance(__middle, __last); 02535 02536 typedef _Temporary_buffer<_BidirectionalIterator, _ValueType> _TmpBuf; 02537 _TmpBuf __buf(__first, __len1 + __len2); 02538 02539 if (__buf.begin() == 0) 02540 std::__merge_without_buffer 02541 (__first, __middle, __last, __len1, __len2, __comp); 02542 else 02543 std::__merge_adaptive 02544 (__first, __middle, __last, __len1, __len2, __buf.begin(), 02545 _DistanceType(__buf.size()), __comp); 02546 } 02547 02548 /** 02549 * @brief Merges two sorted ranges in place. 02550 * @ingroup sorting_algorithms 02551 * @param __first An iterator. 02552 * @param __middle Another iterator. 02553 * @param __last Another iterator. 02554 * @return Nothing. 02555 * 02556 * Merges two sorted and consecutive ranges, [__first,__middle) and 02557 * [__middle,__last), and puts the result in [__first,__last). The 02558 * output will be sorted. The sort is @e stable, that is, for 02559 * equivalent elements in the two ranges, elements from the first 02560 * range will always come before elements from the second. 02561 * 02562 * If enough additional memory is available, this takes (__last-__first)-1 02563 * comparisons. Otherwise an NlogN algorithm is used, where N is 02564 * distance(__first,__last). 02565 */ 02566 template<typename _BidirectionalIterator> 02567 inline void 02568 inplace_merge(_BidirectionalIterator __first, 02569 _BidirectionalIterator __middle, 02570 _BidirectionalIterator __last) 02571 { 02572 // concept requirements 02573 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< 02574 _BidirectionalIterator>) 02575 __glibcxx_function_requires(_LessThanComparableConcept< 02576 typename iterator_traits<_BidirectionalIterator>::value_type>) 02577 __glibcxx_requires_sorted(__first, __middle); 02578 __glibcxx_requires_sorted(__middle, __last); 02579 __glibcxx_requires_irreflexive(__first, __last); 02580 02581 std::__inplace_merge(__first, __middle, __last, 02582 __gnu_cxx::__ops::__iter_less_iter()); 02583 } 02584 02585 /** 02586 * @brief Merges two sorted ranges in place. 02587 * @ingroup sorting_algorithms 02588 * @param __first An iterator. 02589 * @param __middle Another iterator. 02590 * @param __last Another iterator. 02591 * @param __comp A functor to use for comparisons. 02592 * @return Nothing. 02593 * 02594 * Merges two sorted and consecutive ranges, [__first,__middle) and 02595 * [middle,last), and puts the result in [__first,__last). The output will 02596 * be sorted. The sort is @e stable, that is, for equivalent 02597 * elements in the two ranges, elements from the first range will always 02598 * come before elements from the second. 02599 * 02600 * If enough additional memory is available, this takes (__last-__first)-1 02601 * comparisons. Otherwise an NlogN algorithm is used, where N is 02602 * distance(__first,__last). 02603 * 02604 * The comparison function should have the same effects on ordering as 02605 * the function used for the initial sort. 02606 */ 02607 template<typename _BidirectionalIterator, typename _Compare> 02608 inline void 02609 inplace_merge(_BidirectionalIterator __first, 02610 _BidirectionalIterator __middle, 02611 _BidirectionalIterator __last, 02612 _Compare __comp) 02613 { 02614 // concept requirements 02615 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< 02616 _BidirectionalIterator>) 02617 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 02618 typename iterator_traits<_BidirectionalIterator>::value_type, 02619 typename iterator_traits<_BidirectionalIterator>::value_type>) 02620 __glibcxx_requires_sorted_pred(__first, __middle, __comp); 02621 __glibcxx_requires_sorted_pred(__middle, __last, __comp); 02622 __glibcxx_requires_irreflexive_pred(__first, __last, __comp); 02623 02624 std::__inplace_merge(__first, __middle, __last, 02625 __gnu_cxx::__ops::__iter_comp_iter(__comp)); 02626 } 02627 02628 02629 /// This is a helper function for the __merge_sort_loop routines. 02630 template<typename _InputIterator, typename _OutputIterator, 02631 typename _Compare> 02632 _OutputIterator 02633 __move_merge(_InputIterator __first1, _InputIterator __last1, 02634 _InputIterator __first2, _InputIterator __last2, 02635 _OutputIterator __result, _Compare __comp) 02636 { 02637 while (__first1 != __last1 && __first2 != __last2) 02638 { 02639 if (__comp(__first2, __first1)) 02640 { 02641 *__result = _GLIBCXX_MOVE(*__first2); 02642 ++__first2; 02643 } 02644 else 02645 { 02646 *__result = _GLIBCXX_MOVE(*__first1); 02647 ++__first1; 02648 } 02649 ++__result; 02650 } 02651 return _GLIBCXX_MOVE3(__first2, __last2, 02652 _GLIBCXX_MOVE3(__first1, __last1, 02653 __result)); 02654 } 02655 02656 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2, 02657 typename _Distance, typename _Compare> 02658 void 02659 __merge_sort_loop(_RandomAccessIterator1 __first, 02660 _RandomAccessIterator1 __last, 02661 _RandomAccessIterator2 __result, _Distance __step_size, 02662 _Compare __comp) 02663 { 02664 const _Distance __two_step = 2 * __step_size; 02665 02666 while (__last - __first >= __two_step) 02667 { 02668 __result = std::__move_merge(__first, __first + __step_size, 02669 __first + __step_size, 02670 __first + __two_step, 02671 __result, __comp); 02672 __first += __two_step; 02673 } 02674 __step_size = std::min(_Distance(__last - __first), __step_size); 02675 02676 std::__move_merge(__first, __first + __step_size, 02677 __first + __step_size, __last, __result, __comp); 02678 } 02679 02680 template<typename _RandomAccessIterator, typename _Distance, 02681 typename _Compare> 02682 void 02683 __chunk_insertion_sort(_RandomAccessIterator __first, 02684 _RandomAccessIterator __last, 02685 _Distance __chunk_size, _Compare __comp) 02686 { 02687 while (__last - __first >= __chunk_size) 02688 { 02689 std::__insertion_sort(__first, __first + __chunk_size, __comp); 02690 __first += __chunk_size; 02691 } 02692 std::__insertion_sort(__first, __last, __comp); 02693 } 02694 02695 enum { _S_chunk_size = 7 }; 02696 02697 template<typename _RandomAccessIterator, typename _Pointer, typename _Compare> 02698 void 02699 __merge_sort_with_buffer(_RandomAccessIterator __first, 02700 _RandomAccessIterator __last, 02701 _Pointer __buffer, _Compare __comp) 02702 { 02703 typedef typename iterator_traits<_RandomAccessIterator>::difference_type 02704 _Distance; 02705 02706 const _Distance __len = __last - __first; 02707 const _Pointer __buffer_last = __buffer + __len; 02708 02709 _Distance __step_size = _S_chunk_size; 02710 std::__chunk_insertion_sort(__first, __last, __step_size, __comp); 02711 02712 while (__step_size < __len) 02713 { 02714 std::__merge_sort_loop(__first, __last, __buffer, 02715 __step_size, __comp); 02716 __step_size *= 2; 02717 std::__merge_sort_loop(__buffer, __buffer_last, __first, 02718 __step_size, __comp); 02719 __step_size *= 2; 02720 } 02721 } 02722 02723 template<typename _RandomAccessIterator, typename _Pointer, 02724 typename _Distance, typename _Compare> 02725 void 02726 __stable_sort_adaptive(_RandomAccessIterator __first, 02727 _RandomAccessIterator __last, 02728 _Pointer __buffer, _Distance __buffer_size, 02729 _Compare __comp) 02730 { 02731 const _Distance __len = (__last - __first + 1) / 2; 02732 const _RandomAccessIterator __middle = __first + __len; 02733 if (__len > __buffer_size) 02734 { 02735 std::__stable_sort_adaptive(__first, __middle, __buffer, 02736 __buffer_size, __comp); 02737 std::__stable_sort_adaptive(__middle, __last, __buffer, 02738 __buffer_size, __comp); 02739 } 02740 else 02741 { 02742 std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp); 02743 std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp); 02744 } 02745 std::__merge_adaptive(__first, __middle, __last, 02746 _Distance(__middle - __first), 02747 _Distance(__last - __middle), 02748 __buffer, __buffer_size, 02749 __comp); 02750 } 02751 02752 /// This is a helper function for the stable sorting routines. 02753 template<typename _RandomAccessIterator, typename _Compare> 02754 void 02755 __inplace_stable_sort(_RandomAccessIterator __first, 02756 _RandomAccessIterator __last, _Compare __comp) 02757 { 02758 if (__last - __first < 15) 02759 { 02760 std::__insertion_sort(__first, __last, __comp); 02761 return; 02762 } 02763 _RandomAccessIterator __middle = __first + (__last - __first) / 2; 02764 std::__inplace_stable_sort(__first, __middle, __comp); 02765 std::__inplace_stable_sort(__middle, __last, __comp); 02766 std::__merge_without_buffer(__first, __middle, __last, 02767 __middle - __first, 02768 __last - __middle, 02769 __comp); 02770 } 02771 02772 // stable_sort 02773 02774 // Set algorithms: includes, set_union, set_intersection, set_difference, 02775 // set_symmetric_difference. All of these algorithms have the precondition 02776 // that their input ranges are sorted and the postcondition that their output 02777 // ranges are sorted. 02778 02779 template<typename _InputIterator1, typename _InputIterator2, 02780 typename _Compare> 02781 bool 02782 __includes(_InputIterator1 __first1, _InputIterator1 __last1, 02783 _InputIterator2 __first2, _InputIterator2 __last2, 02784 _Compare __comp) 02785 { 02786 while (__first1 != __last1 && __first2 != __last2) 02787 if (__comp(__first2, __first1)) 02788 return false; 02789 else if (__comp(__first1, __first2)) 02790 ++__first1; 02791 else 02792 { 02793 ++__first1; 02794 ++__first2; 02795 } 02796 02797 return __first2 == __last2; 02798 } 02799 02800 /** 02801 * @brief Determines whether all elements of a sequence exists in a range. 02802 * @param __first1 Start of search range. 02803 * @param __last1 End of search range. 02804 * @param __first2 Start of sequence 02805 * @param __last2 End of sequence. 02806 * @return True if each element in [__first2,__last2) is contained in order 02807 * within [__first1,__last1). False otherwise. 02808 * @ingroup set_algorithms 02809 * 02810 * This operation expects both [__first1,__last1) and 02811 * [__first2,__last2) to be sorted. Searches for the presence of 02812 * each element in [__first2,__last2) within [__first1,__last1). 02813 * The iterators over each range only move forward, so this is a 02814 * linear algorithm. If an element in [__first2,__last2) is not 02815 * found before the search iterator reaches @p __last2, false is 02816 * returned. 02817 */ 02818 template<typename _InputIterator1, typename _InputIterator2> 02819 inline bool 02820 includes(_InputIterator1 __first1, _InputIterator1 __last1, 02821 _InputIterator2 __first2, _InputIterator2 __last2) 02822 { 02823 // concept requirements 02824 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 02825 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 02826 __glibcxx_function_requires(_LessThanOpConcept< 02827 typename iterator_traits<_InputIterator1>::value_type, 02828 typename iterator_traits<_InputIterator2>::value_type>) 02829 __glibcxx_function_requires(_LessThanOpConcept< 02830 typename iterator_traits<_InputIterator2>::value_type, 02831 typename iterator_traits<_InputIterator1>::value_type>) 02832 __glibcxx_requires_sorted_set(__first1, __last1, __first2); 02833 __glibcxx_requires_sorted_set(__first2, __last2, __first1); 02834 __glibcxx_requires_irreflexive2(__first1, __last1); 02835 __glibcxx_requires_irreflexive2(__first2, __last2); 02836 02837 return std::__includes(__first1, __last1, __first2, __last2, 02838 __gnu_cxx::__ops::__iter_less_iter()); 02839 } 02840 02841 /** 02842 * @brief Determines whether all elements of a sequence exists in a range 02843 * using comparison. 02844 * @ingroup set_algorithms 02845 * @param __first1 Start of search range. 02846 * @param __last1 End of search range. 02847 * @param __first2 Start of sequence 02848 * @param __last2 End of sequence. 02849 * @param __comp Comparison function to use. 02850 * @return True if each element in [__first2,__last2) is contained 02851 * in order within [__first1,__last1) according to comp. False 02852 * otherwise. @ingroup set_algorithms 02853 * 02854 * This operation expects both [__first1,__last1) and 02855 * [__first2,__last2) to be sorted. Searches for the presence of 02856 * each element in [__first2,__last2) within [__first1,__last1), 02857 * using comp to decide. The iterators over each range only move 02858 * forward, so this is a linear algorithm. If an element in 02859 * [__first2,__last2) is not found before the search iterator 02860 * reaches @p __last2, false is returned. 02861 */ 02862 template<typename _InputIterator1, typename _InputIterator2, 02863 typename _Compare> 02864 inline bool 02865 includes(_InputIterator1 __first1, _InputIterator1 __last1, 02866 _InputIterator2 __first2, _InputIterator2 __last2, 02867 _Compare __comp) 02868 { 02869 // concept requirements 02870 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 02871 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 02872 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 02873 typename iterator_traits<_InputIterator1>::value_type, 02874 typename iterator_traits<_InputIterator2>::value_type>) 02875 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 02876 typename iterator_traits<_InputIterator2>::value_type, 02877 typename iterator_traits<_InputIterator1>::value_type>) 02878 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); 02879 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); 02880 __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp); 02881 __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp); 02882 02883 return std::__includes(__first1, __last1, __first2, __last2, 02884 __gnu_cxx::__ops::__iter_comp_iter(__comp)); 02885 } 02886 02887 // nth_element 02888 // merge 02889 // set_difference 02890 // set_intersection 02891 // set_union 02892 // stable_sort 02893 // set_symmetric_difference 02894 // min_element 02895 // max_element 02896 02897 template<typename _BidirectionalIterator, typename _Compare> 02898 bool 02899 __next_permutation(_BidirectionalIterator __first, 02900 _BidirectionalIterator __last, _Compare __comp) 02901 { 02902 if (__first == __last) 02903 return false; 02904 _BidirectionalIterator __i = __first; 02905 ++__i; 02906 if (__i == __last) 02907 return false; 02908 __i = __last; 02909 --__i; 02910 02911 for(;;) 02912 { 02913 _BidirectionalIterator __ii = __i; 02914 --__i; 02915 if (__comp(__i, __ii)) 02916 { 02917 _BidirectionalIterator __j = __last; 02918 while (!__comp(__i, --__j)) 02919 {} 02920 std::iter_swap(__i, __j); 02921 std::__reverse(__ii, __last, 02922 std::__iterator_category(__first)); 02923 return true; 02924 } 02925 if (__i == __first) 02926 { 02927 std::__reverse(__first, __last, 02928 std::__iterator_category(__first)); 02929 return false; 02930 } 02931 } 02932 } 02933 02934 /** 02935 * @brief Permute range into the next @e dictionary ordering. 02936 * @ingroup sorting_algorithms 02937 * @param __first Start of range. 02938 * @param __last End of range. 02939 * @return False if wrapped to first permutation, true otherwise. 02940 * 02941 * Treats all permutations of the range as a set of @e dictionary sorted 02942 * sequences. Permutes the current sequence into the next one of this set. 02943 * Returns true if there are more sequences to generate. If the sequence 02944 * is the largest of the set, the smallest is generated and false returned. 02945 */ 02946 template<typename _BidirectionalIterator> 02947 inline bool 02948 next_permutation(_BidirectionalIterator __first, 02949 _BidirectionalIterator __last) 02950 { 02951 // concept requirements 02952 __glibcxx_function_requires(_BidirectionalIteratorConcept< 02953 _BidirectionalIterator>) 02954 __glibcxx_function_requires(_LessThanComparableConcept< 02955 typename iterator_traits<_BidirectionalIterator>::value_type>) 02956 __glibcxx_requires_valid_range(__first, __last); 02957 __glibcxx_requires_irreflexive(__first, __last); 02958 02959 return std::__next_permutation 02960 (__first, __last, __gnu_cxx::__ops::__iter_less_iter()); 02961 } 02962 02963 /** 02964 * @brief Permute range into the next @e dictionary ordering using 02965 * comparison functor. 02966 * @ingroup sorting_algorithms 02967 * @param __first Start of range. 02968 * @param __last End of range. 02969 * @param __comp A comparison functor. 02970 * @return False if wrapped to first permutation, true otherwise. 02971 * 02972 * Treats all permutations of the range [__first,__last) as a set of 02973 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current 02974 * sequence into the next one of this set. Returns true if there are more 02975 * sequences to generate. If the sequence is the largest of the set, the 02976 * smallest is generated and false returned. 02977 */ 02978 template<typename _BidirectionalIterator, typename _Compare> 02979 inline bool 02980 next_permutation(_BidirectionalIterator __first, 02981 _BidirectionalIterator __last, _Compare __comp) 02982 { 02983 // concept requirements 02984 __glibcxx_function_requires(_BidirectionalIteratorConcept< 02985 _BidirectionalIterator>) 02986 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 02987 typename iterator_traits<_BidirectionalIterator>::value_type, 02988 typename iterator_traits<_BidirectionalIterator>::value_type>) 02989 __glibcxx_requires_valid_range(__first, __last); 02990 __glibcxx_requires_irreflexive_pred(__first, __last, __comp); 02991 02992 return std::__next_permutation 02993 (__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp)); 02994 } 02995 02996 template<typename _BidirectionalIterator, typename _Compare> 02997 bool 02998 __prev_permutation(_BidirectionalIterator __first, 02999 _BidirectionalIterator __last, _Compare __comp) 03000 { 03001 if (__first == __last) 03002 return false; 03003 _BidirectionalIterator __i = __first; 03004 ++__i; 03005 if (__i == __last) 03006 return false; 03007 __i = __last; 03008 --__i; 03009 03010 for(;;) 03011 { 03012 _BidirectionalIterator __ii = __i; 03013 --__i; 03014 if (__comp(__ii, __i)) 03015 { 03016 _BidirectionalIterator __j = __last; 03017 while (!__comp(--__j, __i)) 03018 {} 03019 std::iter_swap(__i, __j); 03020 std::__reverse(__ii, __last, 03021 std::__iterator_category(__first)); 03022 return true; 03023 } 03024 if (__i == __first) 03025 { 03026 std::__reverse(__first, __last, 03027 std::__iterator_category(__first)); 03028 return false; 03029 } 03030 } 03031 } 03032 03033 /** 03034 * @brief Permute range into the previous @e dictionary ordering. 03035 * @ingroup sorting_algorithms 03036 * @param __first Start of range. 03037 * @param __last End of range. 03038 * @return False if wrapped to last permutation, true otherwise. 03039 * 03040 * Treats all permutations of the range as a set of @e dictionary sorted 03041 * sequences. Permutes the current sequence into the previous one of this 03042 * set. Returns true if there are more sequences to generate. If the 03043 * sequence is the smallest of the set, the largest is generated and false 03044 * returned. 03045 */ 03046 template<typename _BidirectionalIterator> 03047 inline bool 03048 prev_permutation(_BidirectionalIterator __first, 03049 _BidirectionalIterator __last) 03050 { 03051 // concept requirements 03052 __glibcxx_function_requires(_BidirectionalIteratorConcept< 03053 _BidirectionalIterator>) 03054 __glibcxx_function_requires(_LessThanComparableConcept< 03055 typename iterator_traits<_BidirectionalIterator>::value_type>) 03056 __glibcxx_requires_valid_range(__first, __last); 03057 __glibcxx_requires_irreflexive(__first, __last); 03058 03059 return std::__prev_permutation(__first, __last, 03060 __gnu_cxx::__ops::__iter_less_iter()); 03061 } 03062 03063 /** 03064 * @brief Permute range into the previous @e dictionary ordering using 03065 * comparison functor. 03066 * @ingroup sorting_algorithms 03067 * @param __first Start of range. 03068 * @param __last End of range. 03069 * @param __comp A comparison functor. 03070 * @return False if wrapped to last permutation, true otherwise. 03071 * 03072 * Treats all permutations of the range [__first,__last) as a set of 03073 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current 03074 * sequence into the previous one of this set. Returns true if there are 03075 * more sequences to generate. If the sequence is the smallest of the set, 03076 * the largest is generated and false returned. 03077 */ 03078 template<typename _BidirectionalIterator, typename _Compare> 03079 inline bool 03080 prev_permutation(_BidirectionalIterator __first, 03081 _BidirectionalIterator __last, _Compare __comp) 03082 { 03083 // concept requirements 03084 __glibcxx_function_requires(_BidirectionalIteratorConcept< 03085 _BidirectionalIterator>) 03086 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 03087 typename iterator_traits<_BidirectionalIterator>::value_type, 03088 typename iterator_traits<_BidirectionalIterator>::value_type>) 03089 __glibcxx_requires_valid_range(__first, __last); 03090 __glibcxx_requires_irreflexive_pred(__first, __last, __comp); 03091 03092 return std::__prev_permutation(__first, __last, 03093 __gnu_cxx::__ops::__iter_comp_iter(__comp)); 03094 } 03095 03096 // replace 03097 // replace_if 03098 03099 template<typename _InputIterator, typename _OutputIterator, 03100 typename _Predicate, typename _Tp> 03101 _OutputIterator 03102 __replace_copy_if(_InputIterator __first, _InputIterator __last, 03103 _OutputIterator __result, 03104 _Predicate __pred, const _Tp& __new_value) 03105 { 03106 for (; __first != __last; ++__first, (void)++__result) 03107 if (__pred(__first)) 03108 *__result = __new_value; 03109 else 03110 *__result = *__first; 03111 return __result; 03112 } 03113 03114 /** 03115 * @brief Copy a sequence, replacing each element of one value with another 03116 * value. 03117 * @param __first An input iterator. 03118 * @param __last An input iterator. 03119 * @param __result An output iterator. 03120 * @param __old_value The value to be replaced. 03121 * @param __new_value The replacement value. 03122 * @return The end of the output sequence, @p result+(last-first). 03123 * 03124 * Copies each element in the input range @p [__first,__last) to the 03125 * output range @p [__result,__result+(__last-__first)) replacing elements 03126 * equal to @p __old_value with @p __new_value. 03127 */ 03128 template<typename _InputIterator, typename _OutputIterator, typename _Tp> 03129 inline _OutputIterator 03130 replace_copy(_InputIterator __first, _InputIterator __last, 03131 _OutputIterator __result, 03132 const _Tp& __old_value, const _Tp& __new_value) 03133 { 03134 // concept requirements 03135 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 03136 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 03137 typename iterator_traits<_InputIterator>::value_type>) 03138 __glibcxx_function_requires(_EqualOpConcept< 03139 typename iterator_traits<_InputIterator>::value_type, _Tp>) 03140 __glibcxx_requires_valid_range(__first, __last); 03141 03142 return std::__replace_copy_if(__first, __last, __result, 03143 __gnu_cxx::__ops::__iter_equals_val(__old_value), 03144 __new_value); 03145 } 03146 03147 /** 03148 * @brief Copy a sequence, replacing each value for which a predicate 03149 * returns true with another value. 03150 * @ingroup mutating_algorithms 03151 * @param __first An input iterator. 03152 * @param __last An input iterator. 03153 * @param __result An output iterator. 03154 * @param __pred A predicate. 03155 * @param __new_value The replacement value. 03156 * @return The end of the output sequence, @p __result+(__last-__first). 03157 * 03158 * Copies each element in the range @p [__first,__last) to the range 03159 * @p [__result,__result+(__last-__first)) replacing elements for which 03160 * @p __pred returns true with @p __new_value. 03161 */ 03162 template<typename _InputIterator, typename _OutputIterator, 03163 typename _Predicate, typename _Tp> 03164 inline _OutputIterator 03165 replace_copy_if(_InputIterator __first, _InputIterator __last, 03166 _OutputIterator __result, 03167 _Predicate __pred, const _Tp& __new_value) 03168 { 03169 // concept requirements 03170 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 03171 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 03172 typename iterator_traits<_InputIterator>::value_type>) 03173 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 03174 typename iterator_traits<_InputIterator>::value_type>) 03175 __glibcxx_requires_valid_range(__first, __last); 03176 03177 return std::__replace_copy_if(__first, __last, __result, 03178 __gnu_cxx::__ops::__pred_iter(__pred), 03179 __new_value); 03180 } 03181 03182 template<typename _InputIterator, typename _Predicate> 03183 typename iterator_traits<_InputIterator>::difference_type 03184 __count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) 03185 { 03186 typename iterator_traits<_InputIterator>::difference_type __n = 0; 03187 for (; __first != __last; ++__first) 03188 if (__pred(__first)) 03189 ++__n; 03190 return __n; 03191 } 03192 03193 #if __cplusplus >= 201103L 03194 /** 03195 * @brief Determines whether the elements of a sequence are sorted. 03196 * @ingroup sorting_algorithms 03197 * @param __first An iterator. 03198 * @param __last Another iterator. 03199 * @return True if the elements are sorted, false otherwise. 03200 */ 03201 template<typename _ForwardIterator> 03202 inline bool 03203 is_sorted(_ForwardIterator __first, _ForwardIterator __last) 03204 { return std::is_sorted_until(__first, __last) == __last; } 03205 03206 /** 03207 * @brief Determines whether the elements of a sequence are sorted 03208 * according to a comparison functor. 03209 * @ingroup sorting_algorithms 03210 * @param __first An iterator. 03211 * @param __last Another iterator. 03212 * @param __comp A comparison functor. 03213 * @return True if the elements are sorted, false otherwise. 03214 */ 03215 template<typename _ForwardIterator, typename _Compare> 03216 inline bool 03217 is_sorted(_ForwardIterator __first, _ForwardIterator __last, 03218 _Compare __comp) 03219 { return std::is_sorted_until(__first, __last, __comp) == __last; } 03220 03221 template<typename _ForwardIterator, typename _Compare> 03222 _ForwardIterator 03223 __is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, 03224 _Compare __comp) 03225 { 03226 if (__first == __last) 03227 return __last; 03228 03229 _ForwardIterator __next = __first; 03230 for (++__next; __next != __last; __first = __next, (void)++__next) 03231 if (__comp(__next, __first)) 03232 return __next; 03233 return __next; 03234 } 03235 03236 /** 03237 * @brief Determines the end of a sorted sequence. 03238 * @ingroup sorting_algorithms 03239 * @param __first An iterator. 03240 * @param __last Another iterator. 03241 * @return An iterator pointing to the last iterator i in [__first, __last) 03242 * for which the range [__first, i) is sorted. 03243 */ 03244 template<typename _ForwardIterator> 03245 inline _ForwardIterator 03246 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last) 03247 { 03248 // concept requirements 03249 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 03250 __glibcxx_function_requires(_LessThanComparableConcept< 03251 typename iterator_traits<_ForwardIterator>::value_type>) 03252 __glibcxx_requires_valid_range(__first, __last); 03253 __glibcxx_requires_irreflexive(__first, __last); 03254 03255 return std::__is_sorted_until(__first, __last, 03256 __gnu_cxx::__ops::__iter_less_iter()); 03257 } 03258 03259 /** 03260 * @brief Determines the end of a sorted sequence using comparison functor. 03261 * @ingroup sorting_algorithms 03262 * @param __first An iterator. 03263 * @param __last Another iterator. 03264 * @param __comp A comparison functor. 03265 * @return An iterator pointing to the last iterator i in [__first, __last) 03266 * for which the range [__first, i) is sorted. 03267 */ 03268 template<typename _ForwardIterator, typename _Compare> 03269 inline _ForwardIterator 03270 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, 03271 _Compare __comp) 03272 { 03273 // concept requirements 03274 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 03275 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 03276 typename iterator_traits<_ForwardIterator>::value_type, 03277 typename iterator_traits<_ForwardIterator>::value_type>) 03278 __glibcxx_requires_valid_range(__first, __last); 03279 __glibcxx_requires_irreflexive_pred(__first, __last, __comp); 03280 03281 return std::__is_sorted_until(__first, __last, 03282 __gnu_cxx::__ops::__iter_comp_iter(__comp)); 03283 } 03284 03285 /** 03286 * @brief Determines min and max at once as an ordered pair. 03287 * @ingroup sorting_algorithms 03288 * @param __a A thing of arbitrary type. 03289 * @param __b Another thing of arbitrary type. 03290 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a, 03291 * __b) otherwise. 03292 */ 03293 template<typename _Tp> 03294 _GLIBCXX14_CONSTEXPR 03295 inline pair<const _Tp&, const _Tp&> 03296 minmax(const _Tp& __a, const _Tp& __b) 03297 { 03298 // concept requirements 03299 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>) 03300 03301 return __b < __a ? pair<const _Tp&, const _Tp&>(__b, __a) 03302 : pair<const _Tp&, const _Tp&>(__a, __b); 03303 } 03304 03305 /** 03306 * @brief Determines min and max at once as an ordered pair. 03307 * @ingroup sorting_algorithms 03308 * @param __a A thing of arbitrary type. 03309 * @param __b Another thing of arbitrary type. 03310 * @param __comp A @link comparison_functors comparison functor @endlink. 03311 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a, 03312 * __b) otherwise. 03313 */ 03314 template<typename _Tp, typename _Compare> 03315 _GLIBCXX14_CONSTEXPR 03316 inline pair<const _Tp&, const _Tp&> 03317 minmax(const _Tp& __a, const _Tp& __b, _Compare __comp) 03318 { 03319 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) 03320 : pair<const _Tp&, const _Tp&>(__a, __b); 03321 } 03322 03323 template<typename _ForwardIterator, typename _Compare> 03324 _GLIBCXX14_CONSTEXPR 03325 pair<_ForwardIterator, _ForwardIterator> 03326 __minmax_element(_ForwardIterator __first, _ForwardIterator __last, 03327 _Compare __comp) 03328 { 03329 _ForwardIterator __next = __first; 03330 if (__first == __last 03331 || ++__next == __last) 03332 return std::make_pair(__first, __first); 03333 03334 _ForwardIterator __min{}, __max{}; 03335 if (__comp(__next, __first)) 03336 { 03337 __min = __next; 03338 __max = __first; 03339 } 03340 else 03341 { 03342 __min = __first; 03343 __max = __next; 03344 } 03345 03346 __first = __next; 03347 ++__first; 03348 03349 while (__first != __last) 03350 { 03351 __next = __first; 03352 if (++__next == __last) 03353 { 03354 if (__comp(__first, __min)) 03355 __min = __first; 03356 else if (!__comp(__first, __max)) 03357 __max = __first; 03358 break; 03359 } 03360 03361 if (__comp(__next, __first)) 03362 { 03363 if (__comp(__next, __min)) 03364 __min = __next; 03365 if (!__comp(__first, __max)) 03366 __max = __first; 03367 } 03368 else 03369 { 03370 if (__comp(__first, __min)) 03371 __min = __first; 03372 if (!__comp(__next, __max)) 03373 __max = __next; 03374 } 03375 03376 __first = __next; 03377 ++__first; 03378 } 03379 03380 return std::make_pair(__min, __max); 03381 } 03382 03383 /** 03384 * @brief Return a pair of iterators pointing to the minimum and maximum 03385 * elements in a range. 03386 * @ingroup sorting_algorithms 03387 * @param __first Start of range. 03388 * @param __last End of range. 03389 * @return make_pair(m, M), where m is the first iterator i in 03390 * [__first, __last) such that no other element in the range is 03391 * smaller, and where M is the last iterator i in [__first, __last) 03392 * such that no other element in the range is larger. 03393 */ 03394 template<typename _ForwardIterator> 03395 _GLIBCXX14_CONSTEXPR 03396 inline pair<_ForwardIterator, _ForwardIterator> 03397 minmax_element(_ForwardIterator __first, _ForwardIterator __last) 03398 { 03399 // concept requirements 03400 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 03401 __glibcxx_function_requires(_LessThanComparableConcept< 03402 typename iterator_traits<_ForwardIterator>::value_type>) 03403 __glibcxx_requires_valid_range(__first, __last); 03404 __glibcxx_requires_irreflexive(__first, __last); 03405 03406 return std::__minmax_element(__first, __last, 03407 __gnu_cxx::__ops::__iter_less_iter()); 03408 } 03409 03410 /** 03411 * @brief Return a pair of iterators pointing to the minimum and maximum 03412 * elements in a range. 03413 * @ingroup sorting_algorithms 03414 * @param __first Start of range. 03415 * @param __last End of range. 03416 * @param __comp Comparison functor. 03417 * @return make_pair(m, M), where m is the first iterator i in 03418 * [__first, __last) such that no other element in the range is 03419 * smaller, and where M is the last iterator i in [__first, __last) 03420 * such that no other element in the range is larger. 03421 */ 03422 template<typename _ForwardIterator, typename _Compare> 03423 _GLIBCXX14_CONSTEXPR 03424 inline pair<_ForwardIterator, _ForwardIterator> 03425 minmax_element(_ForwardIterator __first, _ForwardIterator __last, 03426 _Compare __comp) 03427 { 03428 // concept requirements 03429 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 03430 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 03431 typename iterator_traits<_ForwardIterator>::value_type, 03432 typename iterator_traits<_ForwardIterator>::value_type>) 03433 __glibcxx_requires_valid_range(__first, __last); 03434 __glibcxx_requires_irreflexive_pred(__first, __last, __comp); 03435 03436 return std::__minmax_element(__first, __last, 03437 __gnu_cxx::__ops::__iter_comp_iter(__comp)); 03438 } 03439 03440 // N2722 + DR 915. 03441 template<typename _Tp> 03442 _GLIBCXX14_CONSTEXPR 03443 inline _Tp 03444 min(initializer_list<_Tp> __l) 03445 { return *std::min_element(__l.begin(), __l.end()); } 03446 03447 template<typename _Tp, typename _Compare> 03448 _GLIBCXX14_CONSTEXPR 03449 inline _Tp 03450 min(initializer_list<_Tp> __l, _Compare __comp) 03451 { return *std::min_element(__l.begin(), __l.end(), __comp); } 03452 03453 template<typename _Tp> 03454 _GLIBCXX14_CONSTEXPR 03455 inline _Tp 03456 max(initializer_list<_Tp> __l) 03457 { return *std::max_element(__l.begin(), __l.end()); } 03458 03459 template<typename _Tp, typename _Compare> 03460 _GLIBCXX14_CONSTEXPR 03461 inline _Tp 03462 max(initializer_list<_Tp> __l, _Compare __comp) 03463 { return *std::max_element(__l.begin(), __l.end(), __comp); } 03464 03465 template<typename _Tp> 03466 _GLIBCXX14_CONSTEXPR 03467 inline pair<_Tp, _Tp> 03468 minmax(initializer_list<_Tp> __l) 03469 { 03470 pair<const _Tp*, const _Tp*> __p = 03471 std::minmax_element(__l.begin(), __l.end()); 03472 return std::make_pair(*__p.first, *__p.second); 03473 } 03474 03475 template<typename _Tp, typename _Compare> 03476 _GLIBCXX14_CONSTEXPR 03477 inline pair<_Tp, _Tp> 03478 minmax(initializer_list<_Tp> __l, _Compare __comp) 03479 { 03480 pair<const _Tp*, const _Tp*> __p = 03481 std::minmax_element(__l.begin(), __l.end(), __comp); 03482 return std::make_pair(*__p.first, *__p.second); 03483 } 03484 03485 template<typename _ForwardIterator1, typename _ForwardIterator2, 03486 typename _BinaryPredicate> 03487 bool 03488 __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 03489 _ForwardIterator2 __first2, _BinaryPredicate __pred) 03490 { 03491 // Efficiently compare identical prefixes: O(N) if sequences 03492 // have the same elements in the same order. 03493 for (; __first1 != __last1; ++__first1, (void)++__first2) 03494 if (!__pred(__first1, __first2)) 03495 break; 03496 03497 if (__first1 == __last1) 03498 return true; 03499 03500 // Establish __last2 assuming equal ranges by iterating over the 03501 // rest of the list. 03502 _ForwardIterator2 __last2 = __first2; 03503 std::advance(__last2, std::distance(__first1, __last1)); 03504 for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan) 03505 { 03506 if (__scan != std::__find_if(__first1, __scan, 03507 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))) 03508 continue; // We've seen this one before. 03509 03510 auto __matches 03511 = std::__count_if(__first2, __last2, 03512 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)); 03513 if (0 == __matches || 03514 std::__count_if(__scan, __last1, 03515 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)) 03516 != __matches) 03517 return false; 03518 } 03519 return true; 03520 } 03521 03522 /** 03523 * @brief Checks whether a permutation of the second sequence is equal 03524 * to the first sequence. 03525 * @ingroup non_mutating_algorithms 03526 * @param __first1 Start of first range. 03527 * @param __last1 End of first range. 03528 * @param __first2 Start of second range. 03529 * @return true if there exists a permutation of the elements in the range 03530 * [__first2, __first2 + (__last1 - __first1)), beginning with 03531 * ForwardIterator2 begin, such that equal(__first1, __last1, begin) 03532 * returns true; otherwise, returns false. 03533 */ 03534 template<typename _ForwardIterator1, typename _ForwardIterator2> 03535 inline bool 03536 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 03537 _ForwardIterator2 __first2) 03538 { 03539 // concept requirements 03540 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) 03541 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) 03542 __glibcxx_function_requires(_EqualOpConcept< 03543 typename iterator_traits<_ForwardIterator1>::value_type, 03544 typename iterator_traits<_ForwardIterator2>::value_type>) 03545 __glibcxx_requires_valid_range(__first1, __last1); 03546 03547 return std::__is_permutation(__first1, __last1, __first2, 03548 __gnu_cxx::__ops::__iter_equal_to_iter()); 03549 } 03550 03551 /** 03552 * @brief Checks whether a permutation of the second sequence is equal 03553 * to the first sequence. 03554 * @ingroup non_mutating_algorithms 03555 * @param __first1 Start of first range. 03556 * @param __last1 End of first range. 03557 * @param __first2 Start of second range. 03558 * @param __pred A binary predicate. 03559 * @return true if there exists a permutation of the elements in 03560 * the range [__first2, __first2 + (__last1 - __first1)), 03561 * beginning with ForwardIterator2 begin, such that 03562 * equal(__first1, __last1, __begin, __pred) returns true; 03563 * otherwise, returns false. 03564 */ 03565 template<typename _ForwardIterator1, typename _ForwardIterator2, 03566 typename _BinaryPredicate> 03567 inline bool 03568 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 03569 _ForwardIterator2 __first2, _BinaryPredicate __pred) 03570 { 03571 // concept requirements 03572 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) 03573 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) 03574 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, 03575 typename iterator_traits<_ForwardIterator1>::value_type, 03576 typename iterator_traits<_ForwardIterator2>::value_type>) 03577 __glibcxx_requires_valid_range(__first1, __last1); 03578 03579 return std::__is_permutation(__first1, __last1, __first2, 03580 __gnu_cxx::__ops::__iter_comp_iter(__pred)); 03581 } 03582 03583 #if __cplusplus > 201103L 03584 template<typename _ForwardIterator1, typename _ForwardIterator2, 03585 typename _BinaryPredicate> 03586 bool 03587 __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 03588 _ForwardIterator2 __first2, _ForwardIterator2 __last2, 03589 _BinaryPredicate __pred) 03590 { 03591 using _Cat1 03592 = typename iterator_traits<_ForwardIterator1>::iterator_category; 03593 using _Cat2 03594 = typename iterator_traits<_ForwardIterator2>::iterator_category; 03595 using _It1_is_RA = is_same<_Cat1, random_access_iterator_tag>; 03596 using _It2_is_RA = is_same<_Cat2, random_access_iterator_tag>; 03597 constexpr bool __ra_iters = _It1_is_RA() && _It2_is_RA(); 03598 if (__ra_iters) 03599 { 03600 auto __d1 = std::distance(__first1, __last1); 03601 auto __d2 = std::distance(__first2, __last2); 03602 if (__d1 != __d2) 03603 return false; 03604 } 03605 03606 // Efficiently compare identical prefixes: O(N) if sequences 03607 // have the same elements in the same order. 03608 for (; __first1 != __last1 && __first2 != __last2; 03609 ++__first1, (void)++__first2) 03610 if (!__pred(__first1, __first2)) 03611 break; 03612 03613 if (__ra_iters) 03614 { 03615 if (__first1 == __last1) 03616 return true; 03617 } 03618 else 03619 { 03620 auto __d1 = std::distance(__first1, __last1); 03621 auto __d2 = std::distance(__first2, __last2); 03622 if (__d1 == 0 && __d2 == 0) 03623 return true; 03624 if (__d1 != __d2) 03625 return false; 03626 } 03627 03628 for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan) 03629 { 03630 if (__scan != std::__find_if(__first1, __scan, 03631 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))) 03632 continue; // We've seen this one before. 03633 03634 auto __matches = std::__count_if(__first2, __last2, 03635 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)); 03636 if (0 == __matches 03637 || std::__count_if(__scan, __last1, 03638 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)) 03639 != __matches) 03640 return false; 03641 } 03642 return true; 03643 } 03644 03645 /** 03646 * @brief Checks whether a permutaion of the second sequence is equal 03647 * to the first sequence. 03648 * @ingroup non_mutating_algorithms 03649 * @param __first1 Start of first range. 03650 * @param __last1 End of first range. 03651 * @param __first2 Start of second range. 03652 * @param __last2 End of first range. 03653 * @return true if there exists a permutation of the elements in the range 03654 * [__first2, __last2), beginning with ForwardIterator2 begin, 03655 * such that equal(__first1, __last1, begin) returns true; 03656 * otherwise, returns false. 03657 */ 03658 template<typename _ForwardIterator1, typename _ForwardIterator2> 03659 inline bool 03660 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 03661 _ForwardIterator2 __first2, _ForwardIterator2 __last2) 03662 { 03663 __glibcxx_requires_valid_range(__first1, __last1); 03664 __glibcxx_requires_valid_range(__first2, __last2); 03665 03666 return 03667 std::__is_permutation(__first1, __last1, __first2, __last2, 03668 __gnu_cxx::__ops::__iter_equal_to_iter()); 03669 } 03670 03671 /** 03672 * @brief Checks whether a permutation of the second sequence is equal 03673 * to the first sequence. 03674 * @ingroup non_mutating_algorithms 03675 * @param __first1 Start of first range. 03676 * @param __last1 End of first range. 03677 * @param __first2 Start of second range. 03678 * @param __last2 End of first range. 03679 * @param __pred A binary predicate. 03680 * @return true if there exists a permutation of the elements in the range 03681 * [__first2, __last2), beginning with ForwardIterator2 begin, 03682 * such that equal(__first1, __last1, __begin, __pred) returns true; 03683 * otherwise, returns false. 03684 */ 03685 template<typename _ForwardIterator1, typename _ForwardIterator2, 03686 typename _BinaryPredicate> 03687 inline bool 03688 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 03689 _ForwardIterator2 __first2, _ForwardIterator2 __last2, 03690 _BinaryPredicate __pred) 03691 { 03692 __glibcxx_requires_valid_range(__first1, __last1); 03693 __glibcxx_requires_valid_range(__first2, __last2); 03694 03695 return std::__is_permutation(__first1, __last1, __first2, __last2, 03696 __gnu_cxx::__ops::__iter_comp_iter(__pred)); 03697 } 03698 03699 #if __cplusplus > 201402L 03700 03701 #define __cpp_lib_clamp 201603 03702 03703 /** 03704 * @brief Returns the value clamped between lo and hi. 03705 * @ingroup sorting_algorithms 03706 * @param __val A value of arbitrary type. 03707 * @param __lo A lower limit of arbitrary type. 03708 * @param __hi An upper limit of arbitrary type. 03709 * @return max(__val, __lo) if __val < __hi or min(__val, __hi) otherwise. 03710 */ 03711 template<typename _Tp> 03712 constexpr const _Tp& 03713 clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi) 03714 { 03715 __glibcxx_assert(!(__hi < __lo)); 03716 return (__val < __lo) ? __lo : (__hi < __val) ? __hi : __val; 03717 } 03718 03719 /** 03720 * @brief Returns the value clamped between lo and hi. 03721 * @ingroup sorting_algorithms 03722 * @param __val A value of arbitrary type. 03723 * @param __lo A lower limit of arbitrary type. 03724 * @param __hi An upper limit of arbitrary type. 03725 * @param __comp A comparison functor. 03726 * @return max(__val, __lo, __comp) if __comp(__val, __hi) 03727 * or min(__val, __hi, __comp) otherwise. 03728 */ 03729 template<typename _Tp, typename _Compare> 03730 constexpr const _Tp& 03731 clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi, _Compare __comp) 03732 { 03733 __glibcxx_assert(!__comp(__hi, __lo)); 03734 return __comp(__val, __lo) ? __lo : __comp(__hi, __val) ? __hi : __val; 03735 } 03736 #endif // C++17 03737 #endif // C++14 03738 03739 #ifdef _GLIBCXX_USE_C99_STDINT_TR1 03740 /** 03741 * @brief Generate two uniformly distributed integers using a 03742 * single distribution invocation. 03743 * @param __b0 The upper bound for the first integer. 03744 * @param __b1 The upper bound for the second integer. 03745 * @param __g A UniformRandomBitGenerator. 03746 * @return A pair (i, j) with i and j uniformly distributed 03747 * over [0, __b0) and [0, __b1), respectively. 03748 * 03749 * Requires: __b0 * __b1 <= __g.max() - __g.min(). 03750 * 03751 * Using uniform_int_distribution with a range that is very 03752 * small relative to the range of the generator ends up wasting 03753 * potentially expensively generated randomness, since 03754 * uniform_int_distribution does not store leftover randomness 03755 * between invocations. 03756 * 03757 * If we know we want two integers in ranges that are sufficiently 03758 * small, we can compose the ranges, use a single distribution 03759 * invocation, and significantly reduce the waste. 03760 */ 03761 template<typename _IntType, typename _UniformRandomBitGenerator> 03762 pair<_IntType, _IntType> 03763 __gen_two_uniform_ints(_IntType __b0, _IntType __b1, 03764 _UniformRandomBitGenerator&& __g) 03765 { 03766 _IntType __x 03767 = uniform_int_distribution<_IntType>{0, (__b0 * __b1) - 1}(__g); 03768 return std::make_pair(__x / __b1, __x % __b1); 03769 } 03770 03771 /** 03772 * @brief Shuffle the elements of a sequence using a uniform random 03773 * number generator. 03774 * @ingroup mutating_algorithms 03775 * @param __first A forward iterator. 03776 * @param __last A forward iterator. 03777 * @param __g A UniformRandomNumberGenerator (26.5.1.3). 03778 * @return Nothing. 03779 * 03780 * Reorders the elements in the range @p [__first,__last) using @p __g to 03781 * provide random numbers. 03782 */ 03783 template<typename _RandomAccessIterator, 03784 typename _UniformRandomNumberGenerator> 03785 void 03786 shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last, 03787 _UniformRandomNumberGenerator&& __g) 03788 { 03789 // concept requirements 03790 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 03791 _RandomAccessIterator>) 03792 __glibcxx_requires_valid_range(__first, __last); 03793 03794 if (__first == __last) 03795 return; 03796 03797 typedef typename iterator_traits<_RandomAccessIterator>::difference_type 03798 _DistanceType; 03799 03800 typedef typename std::make_unsigned<_DistanceType>::type __ud_type; 03801 typedef typename std::uniform_int_distribution<__ud_type> __distr_type; 03802 typedef typename __distr_type::param_type __p_type; 03803 03804 typedef typename remove_reference<_UniformRandomNumberGenerator>::type 03805 _Gen; 03806 typedef typename common_type<typename _Gen::result_type, __ud_type>::type 03807 __uc_type; 03808 03809 const __uc_type __urngrange = __g.max() - __g.min(); 03810 const __uc_type __urange = __uc_type(__last - __first); 03811 03812 if (__urngrange / __urange >= __urange) 03813 // I.e. (__urngrange >= __urange * __urange) but without wrap issues. 03814 { 03815 _RandomAccessIterator __i = __first + 1; 03816 03817 // Since we know the range isn't empty, an even number of elements 03818 // means an uneven number of elements /to swap/, in which case we 03819 // do the first one up front: 03820 03821 if ((__urange % 2) == 0) 03822 { 03823 __distr_type __d{0, 1}; 03824 std::iter_swap(__i++, __first + __d(__g)); 03825 } 03826 03827 // Now we know that __last - __i is even, so we do the rest in pairs, 03828 // using a single distribution invocation to produce swap positions 03829 // for two successive elements at a time: 03830 03831 while (__i != __last) 03832 { 03833 const __uc_type __swap_range = __uc_type(__i - __first) + 1; 03834 03835 const pair<__uc_type, __uc_type> __pospos = 03836 __gen_two_uniform_ints(__swap_range, __swap_range + 1, __g); 03837 03838 std::iter_swap(__i++, __first + __pospos.first); 03839 std::iter_swap(__i++, __first + __pospos.second); 03840 } 03841 03842 return; 03843 } 03844 03845 __distr_type __d; 03846 03847 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) 03848 std::iter_swap(__i, __first + __d(__g, __p_type(0, __i - __first))); 03849 } 03850 #endif 03851 03852 #endif // C++11 03853 03854 _GLIBCXX_BEGIN_NAMESPACE_ALGO 03855 03856 /** 03857 * @brief Apply a function to every element of a sequence. 03858 * @ingroup non_mutating_algorithms 03859 * @param __first An input iterator. 03860 * @param __last An input iterator. 03861 * @param __f A unary function object. 03862 * @return @p __f 03863 * 03864 * Applies the function object @p __f to each element in the range 03865 * @p [first,last). @p __f must not modify the order of the sequence. 03866 * If @p __f has a return value it is ignored. 03867 */ 03868 template<typename _InputIterator, typename _Function> 03869 _Function 03870 for_each(_InputIterator __first, _InputIterator __last, _Function __f) 03871 { 03872 // concept requirements 03873 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 03874 __glibcxx_requires_valid_range(__first, __last); 03875 for (; __first != __last; ++__first) 03876 __f(*__first); 03877 return __f; // N.B. [alg.foreach] says std::move(f) but it's redundant. 03878 } 03879 03880 #if __cplusplus >= 201703L 03881 /** 03882 * @brief Apply a function to every element of a sequence. 03883 * @ingroup non_mutating_algorithms 03884 * @param __first An input iterator. 03885 * @param __n A value convertible to an integer. 03886 * @param __f A unary function object. 03887 * @return `__first+__n` 03888 * 03889 * Applies the function object `__f` to each element in the range 03890 * `[first, first+n)`. `__f` must not modify the order of the sequence. 03891 * If `__f` has a return value it is ignored. 03892 */ 03893 template<typename _InputIterator, typename _Size, typename _Function> 03894 _InputIterator 03895 for_each_n(_InputIterator __first, _Size __n, _Function __f) 03896 { 03897 typename iterator_traits<_InputIterator>::difference_type __n2 = __n; 03898 using _Cat = typename iterator_traits<_InputIterator>::iterator_category; 03899 if constexpr (is_base_of_v<random_access_iterator_tag, _Cat>) 03900 { 03901 if (__n2 <= 0) 03902 return __first; 03903 auto __last = __first + __n2; 03904 std::for_each(__first, __last, std::move(__f)); 03905 return __last; 03906 } 03907 else 03908 { 03909 while (__n2-->0) 03910 { 03911 __f(*__first); 03912 ++__first; 03913 } 03914 return __first; 03915 } 03916 } 03917 #endif // C++17 03918 03919 /** 03920 * @brief Find the first occurrence of a value in a sequence. 03921 * @ingroup non_mutating_algorithms 03922 * @param __first An input iterator. 03923 * @param __last An input iterator. 03924 * @param __val The value to find. 03925 * @return The first iterator @c i in the range @p [__first,__last) 03926 * such that @c *i == @p __val, or @p __last if no such iterator exists. 03927 */ 03928 template<typename _InputIterator, typename _Tp> 03929 inline _InputIterator 03930 find(_InputIterator __first, _InputIterator __last, 03931 const _Tp& __val) 03932 { 03933 // concept requirements 03934 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 03935 __glibcxx_function_requires(_EqualOpConcept< 03936 typename iterator_traits<_InputIterator>::value_type, _Tp>) 03937 __glibcxx_requires_valid_range(__first, __last); 03938 return std::__find_if(__first, __last, 03939 __gnu_cxx::__ops::__iter_equals_val(__val)); 03940 } 03941 03942 /** 03943 * @brief Find the first element in a sequence for which a 03944 * predicate is true. 03945 * @ingroup non_mutating_algorithms 03946 * @param __first An input iterator. 03947 * @param __last An input iterator. 03948 * @param __pred A predicate. 03949 * @return The first iterator @c i in the range @p [__first,__last) 03950 * such that @p __pred(*i) is true, or @p __last if no such iterator exists. 03951 */ 03952 template<typename _InputIterator, typename _Predicate> 03953 inline _InputIterator 03954 find_if(_InputIterator __first, _InputIterator __last, 03955 _Predicate __pred) 03956 { 03957 // concept requirements 03958 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 03959 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 03960 typename iterator_traits<_InputIterator>::value_type>) 03961 __glibcxx_requires_valid_range(__first, __last); 03962 03963 return std::__find_if(__first, __last, 03964 __gnu_cxx::__ops::__pred_iter(__pred)); 03965 } 03966 03967 /** 03968 * @brief Find element from a set in a sequence. 03969 * @ingroup non_mutating_algorithms 03970 * @param __first1 Start of range to search. 03971 * @param __last1 End of range to search. 03972 * @param __first2 Start of match candidates. 03973 * @param __last2 End of match candidates. 03974 * @return The first iterator @c i in the range 03975 * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an 03976 * iterator in [__first2,__last2), or @p __last1 if no such iterator exists. 03977 * 03978 * Searches the range @p [__first1,__last1) for an element that is 03979 * equal to some element in the range [__first2,__last2). If 03980 * found, returns an iterator in the range [__first1,__last1), 03981 * otherwise returns @p __last1. 03982 */ 03983 template<typename _InputIterator, typename _ForwardIterator> 03984 _InputIterator 03985 find_first_of(_InputIterator __first1, _InputIterator __last1, 03986 _ForwardIterator __first2, _ForwardIterator __last2) 03987 { 03988 // concept requirements 03989 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 03990 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 03991 __glibcxx_function_requires(_EqualOpConcept< 03992 typename iterator_traits<_InputIterator>::value_type, 03993 typename iterator_traits<_ForwardIterator>::value_type>) 03994 __glibcxx_requires_valid_range(__first1, __last1); 03995 __glibcxx_requires_valid_range(__first2, __last2); 03996 03997 for (; __first1 != __last1; ++__first1) 03998 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter) 03999 if (*__first1 == *__iter) 04000 return __first1; 04001 return __last1; 04002 } 04003 04004 /** 04005 * @brief Find element from a set in a sequence using a predicate. 04006 * @ingroup non_mutating_algorithms 04007 * @param __first1 Start of range to search. 04008 * @param __last1 End of range to search. 04009 * @param __first2 Start of match candidates. 04010 * @param __last2 End of match candidates. 04011 * @param __comp Predicate to use. 04012 * @return The first iterator @c i in the range 04013 * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true 04014 * and i2 is an iterator in [__first2,__last2), or @p __last1 if no 04015 * such iterator exists. 04016 * 04017 04018 * Searches the range @p [__first1,__last1) for an element that is 04019 * equal to some element in the range [__first2,__last2). If 04020 * found, returns an iterator in the range [__first1,__last1), 04021 * otherwise returns @p __last1. 04022 */ 04023 template<typename _InputIterator, typename _ForwardIterator, 04024 typename _BinaryPredicate> 04025 _InputIterator 04026 find_first_of(_InputIterator __first1, _InputIterator __last1, 04027 _ForwardIterator __first2, _ForwardIterator __last2, 04028 _BinaryPredicate __comp) 04029 { 04030 // concept requirements 04031 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 04032 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 04033 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, 04034 typename iterator_traits<_InputIterator>::value_type, 04035 typename iterator_traits<_ForwardIterator>::value_type>) 04036 __glibcxx_requires_valid_range(__first1, __last1); 04037 __glibcxx_requires_valid_range(__first2, __last2); 04038 04039 for (; __first1 != __last1; ++__first1) 04040 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter) 04041 if (__comp(*__first1, *__iter)) 04042 return __first1; 04043 return __last1; 04044 } 04045 04046 /** 04047 * @brief Find two adjacent values in a sequence that are equal. 04048 * @ingroup non_mutating_algorithms 04049 * @param __first A forward iterator. 04050 * @param __last A forward iterator. 04051 * @return The first iterator @c i such that @c i and @c i+1 are both 04052 * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1), 04053 * or @p __last if no such iterator exists. 04054 */ 04055 template<typename _ForwardIterator> 04056 inline _ForwardIterator 04057 adjacent_find(_ForwardIterator __first, _ForwardIterator __last) 04058 { 04059 // concept requirements 04060 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 04061 __glibcxx_function_requires(_EqualityComparableConcept< 04062 typename iterator_traits<_ForwardIterator>::value_type>) 04063 __glibcxx_requires_valid_range(__first, __last); 04064 04065 return std::__adjacent_find(__first, __last, 04066 __gnu_cxx::__ops::__iter_equal_to_iter()); 04067 } 04068 04069 /** 04070 * @brief Find two adjacent values in a sequence using a predicate. 04071 * @ingroup non_mutating_algorithms 04072 * @param __first A forward iterator. 04073 * @param __last A forward iterator. 04074 * @param __binary_pred A binary predicate. 04075 * @return The first iterator @c i such that @c i and @c i+1 are both 04076 * valid iterators in @p [__first,__last) and such that 04077 * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator 04078 * exists. 04079 */ 04080 template<typename _ForwardIterator, typename _BinaryPredicate> 04081 inline _ForwardIterator 04082 adjacent_find(_ForwardIterator __first, _ForwardIterator __last, 04083 _BinaryPredicate __binary_pred) 04084 { 04085 // concept requirements 04086 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 04087 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, 04088 typename iterator_traits<_ForwardIterator>::value_type, 04089 typename iterator_traits<_ForwardIterator>::value_type>) 04090 __glibcxx_requires_valid_range(__first, __last); 04091 04092 return std::__adjacent_find(__first, __last, 04093 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred)); 04094 } 04095 04096 /** 04097 * @brief Count the number of copies of a value in a sequence. 04098 * @ingroup non_mutating_algorithms 04099 * @param __first An input iterator. 04100 * @param __last An input iterator. 04101 * @param __value The value to be counted. 04102 * @return The number of iterators @c i in the range @p [__first,__last) 04103 * for which @c *i == @p __value 04104 */ 04105 template<typename _InputIterator, typename _Tp> 04106 inline typename iterator_traits<_InputIterator>::difference_type 04107 count(_InputIterator __first, _InputIterator __last, const _Tp& __value) 04108 { 04109 // concept requirements 04110 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 04111 __glibcxx_function_requires(_EqualOpConcept< 04112 typename iterator_traits<_InputIterator>::value_type, _Tp>) 04113 __glibcxx_requires_valid_range(__first, __last); 04114 04115 return std::__count_if(__first, __last, 04116 __gnu_cxx::__ops::__iter_equals_val(__value)); 04117 } 04118 04119 /** 04120 * @brief Count the elements of a sequence for which a predicate is true. 04121 * @ingroup non_mutating_algorithms 04122 * @param __first An input iterator. 04123 * @param __last An input iterator. 04124 * @param __pred A predicate. 04125 * @return The number of iterators @c i in the range @p [__first,__last) 04126 * for which @p __pred(*i) is true. 04127 */ 04128 template<typename _InputIterator, typename _Predicate> 04129 inline typename iterator_traits<_InputIterator>::difference_type 04130 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) 04131 { 04132 // concept requirements 04133 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 04134 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 04135 typename iterator_traits<_InputIterator>::value_type>) 04136 __glibcxx_requires_valid_range(__first, __last); 04137 04138 return std::__count_if(__first, __last, 04139 __gnu_cxx::__ops::__pred_iter(__pred)); 04140 } 04141 04142 /** 04143 * @brief Search a sequence for a matching sub-sequence. 04144 * @ingroup non_mutating_algorithms 04145 * @param __first1 A forward iterator. 04146 * @param __last1 A forward iterator. 04147 * @param __first2 A forward iterator. 04148 * @param __last2 A forward iterator. 04149 * @return The first iterator @c i in the range @p 04150 * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p 04151 * *(__first2+N) for each @c N in the range @p 04152 * [0,__last2-__first2), or @p __last1 if no such iterator exists. 04153 * 04154 * Searches the range @p [__first1,__last1) for a sub-sequence that 04155 * compares equal value-by-value with the sequence given by @p 04156 * [__first2,__last2) and returns an iterator to the first element 04157 * of the sub-sequence, or @p __last1 if the sub-sequence is not 04158 * found. 04159 * 04160 * Because the sub-sequence must lie completely within the range @p 04161 * [__first1,__last1) it must start at a position less than @p 04162 * __last1-(__last2-__first2) where @p __last2-__first2 is the 04163 * length of the sub-sequence. 04164 * 04165 * This means that the returned iterator @c i will be in the range 04166 * @p [__first1,__last1-(__last2-__first2)) 04167 */ 04168 template<typename _ForwardIterator1, typename _ForwardIterator2> 04169 inline _ForwardIterator1 04170 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 04171 _ForwardIterator2 __first2, _ForwardIterator2 __last2) 04172 { 04173 // concept requirements 04174 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) 04175 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) 04176 __glibcxx_function_requires(_EqualOpConcept< 04177 typename iterator_traits<_ForwardIterator1>::value_type, 04178 typename iterator_traits<_ForwardIterator2>::value_type>) 04179 __glibcxx_requires_valid_range(__first1, __last1); 04180 __glibcxx_requires_valid_range(__first2, __last2); 04181 04182 return std::__search(__first1, __last1, __first2, __last2, 04183 __gnu_cxx::__ops::__iter_equal_to_iter()); 04184 } 04185 04186 /** 04187 * @brief Search a sequence for a matching sub-sequence using a predicate. 04188 * @ingroup non_mutating_algorithms 04189 * @param __first1 A forward iterator. 04190 * @param __last1 A forward iterator. 04191 * @param __first2 A forward iterator. 04192 * @param __last2 A forward iterator. 04193 * @param __predicate A binary predicate. 04194 * @return The first iterator @c i in the range 04195 * @p [__first1,__last1-(__last2-__first2)) such that 04196 * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range 04197 * @p [0,__last2-__first2), or @p __last1 if no such iterator exists. 04198 * 04199 * Searches the range @p [__first1,__last1) for a sub-sequence that 04200 * compares equal value-by-value with the sequence given by @p 04201 * [__first2,__last2), using @p __predicate to determine equality, 04202 * and returns an iterator to the first element of the 04203 * sub-sequence, or @p __last1 if no such iterator exists. 04204 * 04205 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2) 04206 */ 04207 template<typename _ForwardIterator1, typename _ForwardIterator2, 04208 typename _BinaryPredicate> 04209 inline _ForwardIterator1 04210 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 04211 _ForwardIterator2 __first2, _ForwardIterator2 __last2, 04212 _BinaryPredicate __predicate) 04213 { 04214 // concept requirements 04215 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) 04216 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) 04217 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, 04218 typename iterator_traits<_ForwardIterator1>::value_type, 04219 typename iterator_traits<_ForwardIterator2>::value_type>) 04220 __glibcxx_requires_valid_range(__first1, __last1); 04221 __glibcxx_requires_valid_range(__first2, __last2); 04222 04223 return std::__search(__first1, __last1, __first2, __last2, 04224 __gnu_cxx::__ops::__iter_comp_iter(__predicate)); 04225 } 04226 04227 /** 04228 * @brief Search a sequence for a number of consecutive values. 04229 * @ingroup non_mutating_algorithms 04230 * @param __first A forward iterator. 04231 * @param __last A forward iterator. 04232 * @param __count The number of consecutive values. 04233 * @param __val The value to find. 04234 * @return The first iterator @c i in the range @p 04235 * [__first,__last-__count) such that @c *(i+N) == @p __val for 04236 * each @c N in the range @p [0,__count), or @p __last if no such 04237 * iterator exists. 04238 * 04239 * Searches the range @p [__first,__last) for @p count consecutive elements 04240 * equal to @p __val. 04241 */ 04242 template<typename _ForwardIterator, typename _Integer, typename _Tp> 04243 inline _ForwardIterator 04244 search_n(_ForwardIterator __first, _ForwardIterator __last, 04245 _Integer __count, const _Tp& __val) 04246 { 04247 // concept requirements 04248 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 04249 __glibcxx_function_requires(_EqualOpConcept< 04250 typename iterator_traits<_ForwardIterator>::value_type, _Tp>) 04251 __glibcxx_requires_valid_range(__first, __last); 04252 04253 return std::__search_n(__first, __last, __count, 04254 __gnu_cxx::__ops::__iter_equals_val(__val)); 04255 } 04256 04257 04258 /** 04259 * @brief Search a sequence for a number of consecutive values using a 04260 * predicate. 04261 * @ingroup non_mutating_algorithms 04262 * @param __first A forward iterator. 04263 * @param __last A forward iterator. 04264 * @param __count The number of consecutive values. 04265 * @param __val The value to find. 04266 * @param __binary_pred A binary predicate. 04267 * @return The first iterator @c i in the range @p 04268 * [__first,__last-__count) such that @p 04269 * __binary_pred(*(i+N),__val) is true for each @c N in the range 04270 * @p [0,__count), or @p __last if no such iterator exists. 04271 * 04272 * Searches the range @p [__first,__last) for @p __count 04273 * consecutive elements for which the predicate returns true. 04274 */ 04275 template<typename _ForwardIterator, typename _Integer, typename _Tp, 04276 typename _BinaryPredicate> 04277 inline _ForwardIterator 04278 search_n(_ForwardIterator __first, _ForwardIterator __last, 04279 _Integer __count, const _Tp& __val, 04280 _BinaryPredicate __binary_pred) 04281 { 04282 // concept requirements 04283 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 04284 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, 04285 typename iterator_traits<_ForwardIterator>::value_type, _Tp>) 04286 __glibcxx_requires_valid_range(__first, __last); 04287 04288 return std::__search_n(__first, __last, __count, 04289 __gnu_cxx::__ops::__iter_comp_val(__binary_pred, __val)); 04290 } 04291 04292 #if __cplusplus > 201402L 04293 /** @brief Search a sequence using a Searcher object. 04294 * 04295 * @param __first A forward iterator. 04296 * @param __last A forward iterator. 04297 * @param __searcher A callable object. 04298 * @return @p __searcher(__first,__last).first 04299 */ 04300 template<typename _ForwardIterator, typename _Searcher> 04301 inline _ForwardIterator 04302 search(_ForwardIterator __first, _ForwardIterator __last, 04303 const _Searcher& __searcher) 04304 { return __searcher(__first, __last).first; } 04305 #endif 04306 04307 /** 04308 * @brief Perform an operation on a sequence. 04309 * @ingroup mutating_algorithms 04310 * @param __first An input iterator. 04311 * @param __last An input iterator. 04312 * @param __result An output iterator. 04313 * @param __unary_op A unary operator. 04314 * @return An output iterator equal to @p __result+(__last-__first). 04315 * 04316 * Applies the operator to each element in the input range and assigns 04317 * the results to successive elements of the output sequence. 04318 * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the 04319 * range @p [0,__last-__first). 04320 * 04321 * @p unary_op must not alter its argument. 04322 */ 04323 template<typename _InputIterator, typename _OutputIterator, 04324 typename _UnaryOperation> 04325 _OutputIterator 04326 transform(_InputIterator __first, _InputIterator __last, 04327 _OutputIterator __result, _UnaryOperation __unary_op) 04328 { 04329 // concept requirements 04330 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 04331 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 04332 // "the type returned by a _UnaryOperation" 04333 __typeof__(__unary_op(*__first))>) 04334 __glibcxx_requires_valid_range(__first, __last); 04335 04336 for (; __first != __last; ++__first, (void)++__result) 04337 *__result = __unary_op(*__first); 04338 return __result; 04339 } 04340 04341 /** 04342 * @brief Perform an operation on corresponding elements of two sequences. 04343 * @ingroup mutating_algorithms 04344 * @param __first1 An input iterator. 04345 * @param __last1 An input iterator. 04346 * @param __first2 An input iterator. 04347 * @param __result An output iterator. 04348 * @param __binary_op A binary operator. 04349 * @return An output iterator equal to @p result+(last-first). 04350 * 04351 * Applies the operator to the corresponding elements in the two 04352 * input ranges and assigns the results to successive elements of the 04353 * output sequence. 04354 * Evaluates @p 04355 * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each 04356 * @c N in the range @p [0,__last1-__first1). 04357 * 04358 * @p binary_op must not alter either of its arguments. 04359 */ 04360 template<typename _InputIterator1, typename _InputIterator2, 04361 typename _OutputIterator, typename _BinaryOperation> 04362 _OutputIterator 04363 transform(_InputIterator1 __first1, _InputIterator1 __last1, 04364 _InputIterator2 __first2, _OutputIterator __result, 04365 _BinaryOperation __binary_op) 04366 { 04367 // concept requirements 04368 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 04369 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 04370 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 04371 // "the type returned by a _BinaryOperation" 04372 __typeof__(__binary_op(*__first1,*__first2))>) 04373 __glibcxx_requires_valid_range(__first1, __last1); 04374 04375 for (; __first1 != __last1; ++__first1, (void)++__first2, ++__result) 04376 *__result = __binary_op(*__first1, *__first2); 04377 return __result; 04378 } 04379 04380 /** 04381 * @brief Replace each occurrence of one value in a sequence with another 04382 * value. 04383 * @ingroup mutating_algorithms 04384 * @param __first A forward iterator. 04385 * @param __last A forward iterator. 04386 * @param __old_value The value to be replaced. 04387 * @param __new_value The replacement value. 04388 * @return replace() returns no value. 04389 * 04390 * For each iterator @c i in the range @p [__first,__last) if @c *i == 04391 * @p __old_value then the assignment @c *i = @p __new_value is performed. 04392 */ 04393 template<typename _ForwardIterator, typename _Tp> 04394 void 04395 replace(_ForwardIterator __first, _ForwardIterator __last, 04396 const _Tp& __old_value, const _Tp& __new_value) 04397 { 04398 // concept requirements 04399 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 04400 _ForwardIterator>) 04401 __glibcxx_function_requires(_EqualOpConcept< 04402 typename iterator_traits<_ForwardIterator>::value_type, _Tp>) 04403 __glibcxx_function_requires(_ConvertibleConcept<_Tp, 04404 typename iterator_traits<_ForwardIterator>::value_type>) 04405 __glibcxx_requires_valid_range(__first, __last); 04406 04407 for (; __first != __last; ++__first) 04408 if (*__first == __old_value) 04409 *__first = __new_value; 04410 } 04411 04412 /** 04413 * @brief Replace each value in a sequence for which a predicate returns 04414 * true with another value. 04415 * @ingroup mutating_algorithms 04416 * @param __first A forward iterator. 04417 * @param __last A forward iterator. 04418 * @param __pred A predicate. 04419 * @param __new_value The replacement value. 04420 * @return replace_if() returns no value. 04421 * 04422 * For each iterator @c i in the range @p [__first,__last) if @p __pred(*i) 04423 * is true then the assignment @c *i = @p __new_value is performed. 04424 */ 04425 template<typename _ForwardIterator, typename _Predicate, typename _Tp> 04426 void 04427 replace_if(_ForwardIterator __first, _ForwardIterator __last, 04428 _Predicate __pred, const _Tp& __new_value) 04429 { 04430 // concept requirements 04431 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 04432 _ForwardIterator>) 04433 __glibcxx_function_requires(_ConvertibleConcept<_Tp, 04434 typename iterator_traits<_ForwardIterator>::value_type>) 04435 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 04436 typename iterator_traits<_ForwardIterator>::value_type>) 04437 __glibcxx_requires_valid_range(__first, __last); 04438 04439 for (; __first != __last; ++__first) 04440 if (__pred(*__first)) 04441 *__first = __new_value; 04442 } 04443 04444 /** 04445 * @brief Assign the result of a function object to each value in a 04446 * sequence. 04447 * @ingroup mutating_algorithms 04448 * @param __first A forward iterator. 04449 * @param __last A forward iterator. 04450 * @param __gen A function object taking no arguments and returning 04451 * std::iterator_traits<_ForwardIterator>::value_type 04452 * @return generate() returns no value. 04453 * 04454 * Performs the assignment @c *i = @p __gen() for each @c i in the range 04455 * @p [__first,__last). 04456 */ 04457 template<typename _ForwardIterator, typename _Generator> 04458 void 04459 generate(_ForwardIterator __first, _ForwardIterator __last, 04460 _Generator __gen) 04461 { 04462 // concept requirements 04463 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 04464 __glibcxx_function_requires(_GeneratorConcept<_Generator, 04465 typename iterator_traits<_ForwardIterator>::value_type>) 04466 __glibcxx_requires_valid_range(__first, __last); 04467 04468 for (; __first != __last; ++__first) 04469 *__first = __gen(); 04470 } 04471 04472 /** 04473 * @brief Assign the result of a function object to each value in a 04474 * sequence. 04475 * @ingroup mutating_algorithms 04476 * @param __first A forward iterator. 04477 * @param __n The length of the sequence. 04478 * @param __gen A function object taking no arguments and returning 04479 * std::iterator_traits<_ForwardIterator>::value_type 04480 * @return The end of the sequence, @p __first+__n 04481 * 04482 * Performs the assignment @c *i = @p __gen() for each @c i in the range 04483 * @p [__first,__first+__n). 04484 * 04485 * _GLIBCXX_RESOLVE_LIB_DEFECTS 04486 * DR 865. More algorithms that throw away information 04487 */ 04488 template<typename _OutputIterator, typename _Size, typename _Generator> 04489 _OutputIterator 04490 generate_n(_OutputIterator __first, _Size __n, _Generator __gen) 04491 { 04492 // concept requirements 04493 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 04494 // "the type returned by a _Generator" 04495 __typeof__(__gen())>) 04496 04497 for (__decltype(__n + 0) __niter = __n; 04498 __niter > 0; --__niter, (void) ++__first) 04499 *__first = __gen(); 04500 return __first; 04501 } 04502 04503 /** 04504 * @brief Copy a sequence, removing consecutive duplicate values. 04505 * @ingroup mutating_algorithms 04506 * @param __first An input iterator. 04507 * @param __last An input iterator. 04508 * @param __result An output iterator. 04509 * @return An iterator designating the end of the resulting sequence. 04510 * 04511 * Copies each element in the range @p [__first,__last) to the range 04512 * beginning at @p __result, except that only the first element is copied 04513 * from groups of consecutive elements that compare equal. 04514 * unique_copy() is stable, so the relative order of elements that are 04515 * copied is unchanged. 04516 * 04517 * _GLIBCXX_RESOLVE_LIB_DEFECTS 04518 * DR 241. Does unique_copy() require CopyConstructible and Assignable? 04519 * 04520 * _GLIBCXX_RESOLVE_LIB_DEFECTS 04521 * DR 538. 241 again: Does unique_copy() require CopyConstructible and 04522 * Assignable? 04523 */ 04524 template<typename _InputIterator, typename _OutputIterator> 04525 inline _OutputIterator 04526 unique_copy(_InputIterator __first, _InputIterator __last, 04527 _OutputIterator __result) 04528 { 04529 // concept requirements 04530 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 04531 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 04532 typename iterator_traits<_InputIterator>::value_type>) 04533 __glibcxx_function_requires(_EqualityComparableConcept< 04534 typename iterator_traits<_InputIterator>::value_type>) 04535 __glibcxx_requires_valid_range(__first, __last); 04536 04537 if (__first == __last) 04538 return __result; 04539 return std::__unique_copy(__first, __last, __result, 04540 __gnu_cxx::__ops::__iter_equal_to_iter(), 04541 std::__iterator_category(__first), 04542 std::__iterator_category(__result)); 04543 } 04544 04545 /** 04546 * @brief Copy a sequence, removing consecutive values using a predicate. 04547 * @ingroup mutating_algorithms 04548 * @param __first An input iterator. 04549 * @param __last An input iterator. 04550 * @param __result An output iterator. 04551 * @param __binary_pred A binary predicate. 04552 * @return An iterator designating the end of the resulting sequence. 04553 * 04554 * Copies each element in the range @p [__first,__last) to the range 04555 * beginning at @p __result, except that only the first element is copied 04556 * from groups of consecutive elements for which @p __binary_pred returns 04557 * true. 04558 * unique_copy() is stable, so the relative order of elements that are 04559 * copied is unchanged. 04560 * 04561 * _GLIBCXX_RESOLVE_LIB_DEFECTS 04562 * DR 241. Does unique_copy() require CopyConstructible and Assignable? 04563 */ 04564 template<typename _InputIterator, typename _OutputIterator, 04565 typename _BinaryPredicate> 04566 inline _OutputIterator 04567 unique_copy(_InputIterator __first, _InputIterator __last, 04568 _OutputIterator __result, 04569 _BinaryPredicate __binary_pred) 04570 { 04571 // concept requirements -- predicates checked later 04572 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 04573 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 04574 typename iterator_traits<_InputIterator>::value_type>) 04575 __glibcxx_requires_valid_range(__first, __last); 04576 04577 if (__first == __last) 04578 return __result; 04579 return std::__unique_copy(__first, __last, __result, 04580 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred), 04581 std::__iterator_category(__first), 04582 std::__iterator_category(__result)); 04583 } 04584 04585 #if _GLIBCXX_HOSTED 04586 /** 04587 * @brief Randomly shuffle the elements of a sequence. 04588 * @ingroup mutating_algorithms 04589 * @param __first A forward iterator. 04590 * @param __last A forward iterator. 04591 * @return Nothing. 04592 * 04593 * Reorder the elements in the range @p [__first,__last) using a random 04594 * distribution, so that every possible ordering of the sequence is 04595 * equally likely. 04596 */ 04597 template<typename _RandomAccessIterator> 04598 inline void 04599 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last) 04600 { 04601 // concept requirements 04602 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 04603 _RandomAccessIterator>) 04604 __glibcxx_requires_valid_range(__first, __last); 04605 04606 if (__first != __last) 04607 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) 04608 { 04609 // XXX rand() % N is not uniformly distributed 04610 _RandomAccessIterator __j = __first 04611 + std::rand() % ((__i - __first) + 1); 04612 if (__i != __j) 04613 std::iter_swap(__i, __j); 04614 } 04615 } 04616 #endif 04617 04618 /** 04619 * @brief Shuffle the elements of a sequence using a random number 04620 * generator. 04621 * @ingroup mutating_algorithms 04622 * @param __first A forward iterator. 04623 * @param __last A forward iterator. 04624 * @param __rand The RNG functor or function. 04625 * @return Nothing. 04626 * 04627 * Reorders the elements in the range @p [__first,__last) using @p __rand to 04628 * provide a random distribution. Calling @p __rand(N) for a positive 04629 * integer @p N should return a randomly chosen integer from the 04630 * range [0,N). 04631 */ 04632 template<typename _RandomAccessIterator, typename _RandomNumberGenerator> 04633 void 04634 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last, 04635 #if __cplusplus >= 201103L 04636 _RandomNumberGenerator&& __rand) 04637 #else 04638 _RandomNumberGenerator& __rand) 04639 #endif 04640 { 04641 // concept requirements 04642 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 04643 _RandomAccessIterator>) 04644 __glibcxx_requires_valid_range(__first, __last); 04645 04646 if (__first == __last) 04647 return; 04648 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) 04649 { 04650 _RandomAccessIterator __j = __first + __rand((__i - __first) + 1); 04651 if (__i != __j) 04652 std::iter_swap(__i, __j); 04653 } 04654 } 04655 04656 04657 /** 04658 * @brief Move elements for which a predicate is true to the beginning 04659 * of a sequence. 04660 * @ingroup mutating_algorithms 04661 * @param __first A forward iterator. 04662 * @param __last A forward iterator. 04663 * @param __pred A predicate functor. 04664 * @return An iterator @p middle such that @p __pred(i) is true for each 04665 * iterator @p i in the range @p [__first,middle) and false for each @p i 04666 * in the range @p [middle,__last). 04667 * 04668 * @p __pred must not modify its operand. @p partition() does not preserve 04669 * the relative ordering of elements in each group, use 04670 * @p stable_partition() if this is needed. 04671 */ 04672 template<typename _ForwardIterator, typename _Predicate> 04673 inline _ForwardIterator 04674 partition(_ForwardIterator __first, _ForwardIterator __last, 04675 _Predicate __pred) 04676 { 04677 // concept requirements 04678 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 04679 _ForwardIterator>) 04680 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 04681 typename iterator_traits<_ForwardIterator>::value_type>) 04682 __glibcxx_requires_valid_range(__first, __last); 04683 04684 return std::__partition(__first, __last, __pred, 04685 std::__iterator_category(__first)); 04686 } 04687 04688 04689 /** 04690 * @brief Sort the smallest elements of a sequence. 04691 * @ingroup sorting_algorithms 04692 * @param __first An iterator. 04693 * @param __middle Another iterator. 04694 * @param __last Another iterator. 04695 * @return Nothing. 04696 * 04697 * Sorts the smallest @p (__middle-__first) elements in the range 04698 * @p [first,last) and moves them to the range @p [__first,__middle). The 04699 * order of the remaining elements in the range @p [__middle,__last) is 04700 * undefined. 04701 * After the sort if @e i and @e j are iterators in the range 04702 * @p [__first,__middle) such that i precedes j and @e k is an iterator in 04703 * the range @p [__middle,__last) then *j<*i and *k<*i are both false. 04704 */ 04705 template<typename _RandomAccessIterator> 04706 inline void 04707 partial_sort(_RandomAccessIterator __first, 04708 _RandomAccessIterator __middle, 04709 _RandomAccessIterator __last) 04710 { 04711 // concept requirements 04712 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 04713 _RandomAccessIterator>) 04714 __glibcxx_function_requires(_LessThanComparableConcept< 04715 typename iterator_traits<_RandomAccessIterator>::value_type>) 04716 __glibcxx_requires_valid_range(__first, __middle); 04717 __glibcxx_requires_valid_range(__middle, __last); 04718 __glibcxx_requires_irreflexive(__first, __last); 04719 04720 std::__partial_sort(__first, __middle, __last, 04721 __gnu_cxx::__ops::__iter_less_iter()); 04722 } 04723 04724 /** 04725 * @brief Sort the smallest elements of a sequence using a predicate 04726 * for comparison. 04727 * @ingroup sorting_algorithms 04728 * @param __first An iterator. 04729 * @param __middle Another iterator. 04730 * @param __last Another iterator. 04731 * @param __comp A comparison functor. 04732 * @return Nothing. 04733 * 04734 * Sorts the smallest @p (__middle-__first) elements in the range 04735 * @p [__first,__last) and moves them to the range @p [__first,__middle). The 04736 * order of the remaining elements in the range @p [__middle,__last) is 04737 * undefined. 04738 * After the sort if @e i and @e j are iterators in the range 04739 * @p [__first,__middle) such that i precedes j and @e k is an iterator in 04740 * the range @p [__middle,__last) then @p *__comp(j,*i) and @p __comp(*k,*i) 04741 * are both false. 04742 */ 04743 template<typename _RandomAccessIterator, typename _Compare> 04744 inline void 04745 partial_sort(_RandomAccessIterator __first, 04746 _RandomAccessIterator __middle, 04747 _RandomAccessIterator __last, 04748 _Compare __comp) 04749 { 04750 // concept requirements 04751 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 04752 _RandomAccessIterator>) 04753 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 04754 typename iterator_traits<_RandomAccessIterator>::value_type, 04755 typename iterator_traits<_RandomAccessIterator>::value_type>) 04756 __glibcxx_requires_valid_range(__first, __middle); 04757 __glibcxx_requires_valid_range(__middle, __last); 04758 __glibcxx_requires_irreflexive_pred(__first, __last, __comp); 04759 04760 std::__partial_sort(__first, __middle, __last, 04761 __gnu_cxx::__ops::__iter_comp_iter(__comp)); 04762 } 04763 04764 /** 04765 * @brief Sort a sequence just enough to find a particular position. 04766 * @ingroup sorting_algorithms 04767 * @param __first An iterator. 04768 * @param __nth Another iterator. 04769 * @param __last Another iterator. 04770 * @return Nothing. 04771 * 04772 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth 04773 * is the same element that would have been in that position had the 04774 * whole sequence been sorted. The elements either side of @p *__nth are 04775 * not completely sorted, but for any iterator @e i in the range 04776 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it 04777 * holds that *j < *i is false. 04778 */ 04779 template<typename _RandomAccessIterator> 04780 inline void 04781 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, 04782 _RandomAccessIterator __last) 04783 { 04784 // concept requirements 04785 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 04786 _RandomAccessIterator>) 04787 __glibcxx_function_requires(_LessThanComparableConcept< 04788 typename iterator_traits<_RandomAccessIterator>::value_type>) 04789 __glibcxx_requires_valid_range(__first, __nth); 04790 __glibcxx_requires_valid_range(__nth, __last); 04791 __glibcxx_requires_irreflexive(__first, __last); 04792 04793 if (__first == __last || __nth == __last) 04794 return; 04795 04796 std::__introselect(__first, __nth, __last, 04797 std::__lg(__last - __first) * 2, 04798 __gnu_cxx::__ops::__iter_less_iter()); 04799 } 04800 04801 /** 04802 * @brief Sort a sequence just enough to find a particular position 04803 * using a predicate for comparison. 04804 * @ingroup sorting_algorithms 04805 * @param __first An iterator. 04806 * @param __nth Another iterator. 04807 * @param __last Another iterator. 04808 * @param __comp A comparison functor. 04809 * @return Nothing. 04810 * 04811 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth 04812 * is the same element that would have been in that position had the 04813 * whole sequence been sorted. The elements either side of @p *__nth are 04814 * not completely sorted, but for any iterator @e i in the range 04815 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it 04816 * holds that @p __comp(*j,*i) is false. 04817 */ 04818 template<typename _RandomAccessIterator, typename _Compare> 04819 inline void 04820 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, 04821 _RandomAccessIterator __last, _Compare __comp) 04822 { 04823 // concept requirements 04824 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 04825 _RandomAccessIterator>) 04826 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 04827 typename iterator_traits<_RandomAccessIterator>::value_type, 04828 typename iterator_traits<_RandomAccessIterator>::value_type>) 04829 __glibcxx_requires_valid_range(__first, __nth); 04830 __glibcxx_requires_valid_range(__nth, __last); 04831 __glibcxx_requires_irreflexive_pred(__first, __last, __comp); 04832 04833 if (__first == __last || __nth == __last) 04834 return; 04835 04836 std::__introselect(__first, __nth, __last, 04837 std::__lg(__last - __first) * 2, 04838 __gnu_cxx::__ops::__iter_comp_iter(__comp)); 04839 } 04840 04841 /** 04842 * @brief Sort the elements of a sequence. 04843 * @ingroup sorting_algorithms 04844 * @param __first An iterator. 04845 * @param __last Another iterator. 04846 * @return Nothing. 04847 * 04848 * Sorts the elements in the range @p [__first,__last) in ascending order, 04849 * such that for each iterator @e i in the range @p [__first,__last-1), 04850 * *(i+1)<*i is false. 04851 * 04852 * The relative ordering of equivalent elements is not preserved, use 04853 * @p stable_sort() if this is needed. 04854 */ 04855 template<typename _RandomAccessIterator> 04856 inline void 04857 sort(_RandomAccessIterator __first, _RandomAccessIterator __last) 04858 { 04859 // concept requirements 04860 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 04861 _RandomAccessIterator>) 04862 __glibcxx_function_requires(_LessThanComparableConcept< 04863 typename iterator_traits<_RandomAccessIterator>::value_type>) 04864 __glibcxx_requires_valid_range(__first, __last); 04865 __glibcxx_requires_irreflexive(__first, __last); 04866 04867 std::__sort(__first, __last, __gnu_cxx::__ops::__iter_less_iter()); 04868 } 04869 04870 /** 04871 * @brief Sort the elements of a sequence using a predicate for comparison. 04872 * @ingroup sorting_algorithms 04873 * @param __first An iterator. 04874 * @param __last Another iterator. 04875 * @param __comp A comparison functor. 04876 * @return Nothing. 04877 * 04878 * Sorts the elements in the range @p [__first,__last) in ascending order, 04879 * such that @p __comp(*(i+1),*i) is false for every iterator @e i in the 04880 * range @p [__first,__last-1). 04881 * 04882 * The relative ordering of equivalent elements is not preserved, use 04883 * @p stable_sort() if this is needed. 04884 */ 04885 template<typename _RandomAccessIterator, typename _Compare> 04886 inline void 04887 sort(_RandomAccessIterator __first, _RandomAccessIterator __last, 04888 _Compare __comp) 04889 { 04890 // concept requirements 04891 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 04892 _RandomAccessIterator>) 04893 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 04894 typename iterator_traits<_RandomAccessIterator>::value_type, 04895 typename iterator_traits<_RandomAccessIterator>::value_type>) 04896 __glibcxx_requires_valid_range(__first, __last); 04897 __glibcxx_requires_irreflexive_pred(__first, __last, __comp); 04898 04899 std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp)); 04900 } 04901 04902 template<typename _InputIterator1, typename _InputIterator2, 04903 typename _OutputIterator, typename _Compare> 04904 _OutputIterator 04905 __merge(_InputIterator1 __first1, _InputIterator1 __last1, 04906 _InputIterator2 __first2, _InputIterator2 __last2, 04907 _OutputIterator __result, _Compare __comp) 04908 { 04909 while (__first1 != __last1 && __first2 != __last2) 04910 { 04911 if (__comp(__first2, __first1)) 04912 { 04913 *__result = *__first2; 04914 ++__first2; 04915 } 04916 else 04917 { 04918 *__result = *__first1; 04919 ++__first1; 04920 } 04921 ++__result; 04922 } 04923 return std::copy(__first2, __last2, 04924 std::copy(__first1, __last1, __result)); 04925 } 04926 04927 /** 04928 * @brief Merges two sorted ranges. 04929 * @ingroup sorting_algorithms 04930 * @param __first1 An iterator. 04931 * @param __first2 Another iterator. 04932 * @param __last1 Another iterator. 04933 * @param __last2 Another iterator. 04934 * @param __result An iterator pointing to the end of the merged range. 04935 * @return An iterator pointing to the first element <em>not less 04936 * than</em> @e val. 04937 * 04938 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into 04939 * the sorted range @p [__result, __result + (__last1-__first1) + 04940 * (__last2-__first2)). Both input ranges must be sorted, and the 04941 * output range must not overlap with either of the input ranges. 04942 * The sort is @e stable, that is, for equivalent elements in the 04943 * two ranges, elements from the first range will always come 04944 * before elements from the second. 04945 */ 04946 template<typename _InputIterator1, typename _InputIterator2, 04947 typename _OutputIterator> 04948 inline _OutputIterator 04949 merge(_InputIterator1 __first1, _InputIterator1 __last1, 04950 _InputIterator2 __first2, _InputIterator2 __last2, 04951 _OutputIterator __result) 04952 { 04953 // concept requirements 04954 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 04955 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 04956 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 04957 typename iterator_traits<_InputIterator1>::value_type>) 04958 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 04959 typename iterator_traits<_InputIterator2>::value_type>) 04960 __glibcxx_function_requires(_LessThanOpConcept< 04961 typename iterator_traits<_InputIterator2>::value_type, 04962 typename iterator_traits<_InputIterator1>::value_type>) 04963 __glibcxx_requires_sorted_set(__first1, __last1, __first2); 04964 __glibcxx_requires_sorted_set(__first2, __last2, __first1); 04965 __glibcxx_requires_irreflexive2(__first1, __last1); 04966 __glibcxx_requires_irreflexive2(__first2, __last2); 04967 04968 return _GLIBCXX_STD_A::__merge(__first1, __last1, 04969 __first2, __last2, __result, 04970 __gnu_cxx::__ops::__iter_less_iter()); 04971 } 04972 04973 /** 04974 * @brief Merges two sorted ranges. 04975 * @ingroup sorting_algorithms 04976 * @param __first1 An iterator. 04977 * @param __first2 Another iterator. 04978 * @param __last1 Another iterator. 04979 * @param __last2 Another iterator. 04980 * @param __result An iterator pointing to the end of the merged range. 04981 * @param __comp A functor to use for comparisons. 04982 * @return An iterator pointing to the first element "not less 04983 * than" @e val. 04984 * 04985 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into 04986 * the sorted range @p [__result, __result + (__last1-__first1) + 04987 * (__last2-__first2)). Both input ranges must be sorted, and the 04988 * output range must not overlap with either of the input ranges. 04989 * The sort is @e stable, that is, for equivalent elements in the 04990 * two ranges, elements from the first range will always come 04991 * before elements from the second. 04992 * 04993 * The comparison function should have the same effects on ordering as 04994 * the function used for the initial sort. 04995 */ 04996 template<typename _InputIterator1, typename _InputIterator2, 04997 typename _OutputIterator, typename _Compare> 04998 inline _OutputIterator 04999 merge(_InputIterator1 __first1, _InputIterator1 __last1, 05000 _InputIterator2 __first2, _InputIterator2 __last2, 05001 _OutputIterator __result, _Compare __comp) 05002 { 05003 // concept requirements 05004 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 05005 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 05006 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 05007 typename iterator_traits<_InputIterator1>::value_type>) 05008 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 05009 typename iterator_traits<_InputIterator2>::value_type>) 05010 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 05011 typename iterator_traits<_InputIterator2>::value_type, 05012 typename iterator_traits<_InputIterator1>::value_type>) 05013 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); 05014 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); 05015 __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp); 05016 __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp); 05017 05018 return _GLIBCXX_STD_A::__merge(__first1, __last1, 05019 __first2, __last2, __result, 05020 __gnu_cxx::__ops::__iter_comp_iter(__comp)); 05021 } 05022 05023 template<typename _RandomAccessIterator, typename _Compare> 05024 inline void 05025 __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, 05026 _Compare __comp) 05027 { 05028 typedef typename iterator_traits<_RandomAccessIterator>::value_type 05029 _ValueType; 05030 typedef typename iterator_traits<_RandomAccessIterator>::difference_type 05031 _DistanceType; 05032 05033 typedef _Temporary_buffer<_RandomAccessIterator, _ValueType> _TmpBuf; 05034 _TmpBuf __buf(__first, std::distance(__first, __last)); 05035 05036 if (__buf.begin() == 0) 05037 std::__inplace_stable_sort(__first, __last, __comp); 05038 else 05039 std::__stable_sort_adaptive(__first, __last, __buf.begin(), 05040 _DistanceType(__buf.size()), __comp); 05041 } 05042 05043 /** 05044 * @brief Sort the elements of a sequence, preserving the relative order 05045 * of equivalent elements. 05046 * @ingroup sorting_algorithms 05047 * @param __first An iterator. 05048 * @param __last Another iterator. 05049 * @return Nothing. 05050 * 05051 * Sorts the elements in the range @p [__first,__last) in ascending order, 05052 * such that for each iterator @p i in the range @p [__first,__last-1), 05053 * @p *(i+1)<*i is false. 05054 * 05055 * The relative ordering of equivalent elements is preserved, so any two 05056 * elements @p x and @p y in the range @p [__first,__last) such that 05057 * @p x<y is false and @p y<x is false will have the same relative 05058 * ordering after calling @p stable_sort(). 05059 */ 05060 template<typename _RandomAccessIterator> 05061 inline void 05062 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last) 05063 { 05064 // concept requirements 05065 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 05066 _RandomAccessIterator>) 05067 __glibcxx_function_requires(_LessThanComparableConcept< 05068 typename iterator_traits<_RandomAccessIterator>::value_type>) 05069 __glibcxx_requires_valid_range(__first, __last); 05070 __glibcxx_requires_irreflexive(__first, __last); 05071 05072 _GLIBCXX_STD_A::__stable_sort(__first, __last, 05073 __gnu_cxx::__ops::__iter_less_iter()); 05074 } 05075 05076 /** 05077 * @brief Sort the elements of a sequence using a predicate for comparison, 05078 * preserving the relative order of equivalent elements. 05079 * @ingroup sorting_algorithms 05080 * @param __first An iterator. 05081 * @param __last Another iterator. 05082 * @param __comp A comparison functor. 05083 * @return Nothing. 05084 * 05085 * Sorts the elements in the range @p [__first,__last) in ascending order, 05086 * such that for each iterator @p i in the range @p [__first,__last-1), 05087 * @p __comp(*(i+1),*i) is false. 05088 * 05089 * The relative ordering of equivalent elements is preserved, so any two 05090 * elements @p x and @p y in the range @p [__first,__last) such that 05091 * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same 05092 * relative ordering after calling @p stable_sort(). 05093 */ 05094 template<typename _RandomAccessIterator, typename _Compare> 05095 inline void 05096 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, 05097 _Compare __comp) 05098 { 05099 // concept requirements 05100 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 05101 _RandomAccessIterator>) 05102 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 05103 typename iterator_traits<_RandomAccessIterator>::value_type, 05104 typename iterator_traits<_RandomAccessIterator>::value_type>) 05105 __glibcxx_requires_valid_range(__first, __last); 05106 __glibcxx_requires_irreflexive_pred(__first, __last, __comp); 05107 05108 _GLIBCXX_STD_A::__stable_sort(__first, __last, 05109 __gnu_cxx::__ops::__iter_comp_iter(__comp)); 05110 } 05111 05112 template<typename _InputIterator1, typename _InputIterator2, 05113 typename _OutputIterator, 05114 typename _Compare> 05115 _OutputIterator 05116 __set_union(_InputIterator1 __first1, _InputIterator1 __last1, 05117 _InputIterator2 __first2, _InputIterator2 __last2, 05118 _OutputIterator __result, _Compare __comp) 05119 { 05120 while (__first1 != __last1 && __first2 != __last2) 05121 { 05122 if (__comp(__first1, __first2)) 05123 { 05124 *__result = *__first1; 05125 ++__first1; 05126 } 05127 else if (__comp(__first2, __first1)) 05128 { 05129 *__result = *__first2; 05130 ++__first2; 05131 } 05132 else 05133 { 05134 *__result = *__first1; 05135 ++__first1; 05136 ++__first2; 05137 } 05138 ++__result; 05139 } 05140 return std::copy(__first2, __last2, 05141 std::copy(__first1, __last1, __result)); 05142 } 05143 05144 /** 05145 * @brief Return the union of two sorted ranges. 05146 * @ingroup set_algorithms 05147 * @param __first1 Start of first range. 05148 * @param __last1 End of first range. 05149 * @param __first2 Start of second range. 05150 * @param __last2 End of second range. 05151 * @param __result Start of output range. 05152 * @return End of the output range. 05153 * @ingroup set_algorithms 05154 * 05155 * This operation iterates over both ranges, copying elements present in 05156 * each range in order to the output range. Iterators increment for each 05157 * range. When the current element of one range is less than the other, 05158 * that element is copied and the iterator advanced. If an element is 05159 * contained in both ranges, the element from the first range is copied and 05160 * both ranges advance. The output range may not overlap either input 05161 * range. 05162 */ 05163 template<typename _InputIterator1, typename _InputIterator2, 05164 typename _OutputIterator> 05165 inline _OutputIterator 05166 set_union(_InputIterator1 __first1, _InputIterator1 __last1, 05167 _InputIterator2 __first2, _InputIterator2 __last2, 05168 _OutputIterator __result) 05169 { 05170 // concept requirements 05171 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 05172 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 05173 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 05174 typename iterator_traits<_InputIterator1>::value_type>) 05175 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 05176 typename iterator_traits<_InputIterator2>::value_type>) 05177 __glibcxx_function_requires(_LessThanOpConcept< 05178 typename iterator_traits<_InputIterator1>::value_type, 05179 typename iterator_traits<_InputIterator2>::value_type>) 05180 __glibcxx_function_requires(_LessThanOpConcept< 05181 typename iterator_traits<_InputIterator2>::value_type, 05182 typename iterator_traits<_InputIterator1>::value_type>) 05183 __glibcxx_requires_sorted_set(__first1, __last1, __first2); 05184 __glibcxx_requires_sorted_set(__first2, __last2, __first1); 05185 __glibcxx_requires_irreflexive2(__first1, __last1); 05186 __glibcxx_requires_irreflexive2(__first2, __last2); 05187 05188 return _GLIBCXX_STD_A::__set_union(__first1, __last1, 05189 __first2, __last2, __result, 05190 __gnu_cxx::__ops::__iter_less_iter()); 05191 } 05192 05193 /** 05194 * @brief Return the union of two sorted ranges using a comparison functor. 05195 * @ingroup set_algorithms 05196 * @param __first1 Start of first range. 05197 * @param __last1 End of first range. 05198 * @param __first2 Start of second range. 05199 * @param __last2 End of second range. 05200 * @param __result Start of output range. 05201 * @param __comp The comparison functor. 05202 * @return End of the output range. 05203 * @ingroup set_algorithms 05204 * 05205 * This operation iterates over both ranges, copying elements present in 05206 * each range in order to the output range. Iterators increment for each 05207 * range. When the current element of one range is less than the other 05208 * according to @p __comp, that element is copied and the iterator advanced. 05209 * If an equivalent element according to @p __comp is contained in both 05210 * ranges, the element from the first range is copied and both ranges 05211 * advance. The output range may not overlap either input range. 05212 */ 05213 template<typename _InputIterator1, typename _InputIterator2, 05214 typename _OutputIterator, typename _Compare> 05215 inline _OutputIterator 05216 set_union(_InputIterator1 __first1, _InputIterator1 __last1, 05217 _InputIterator2 __first2, _InputIterator2 __last2, 05218 _OutputIterator __result, _Compare __comp) 05219 { 05220 // concept requirements 05221 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 05222 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 05223 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 05224 typename iterator_traits<_InputIterator1>::value_type>) 05225 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 05226 typename iterator_traits<_InputIterator2>::value_type>) 05227 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 05228 typename iterator_traits<_InputIterator1>::value_type, 05229 typename iterator_traits<_InputIterator2>::value_type>) 05230 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 05231 typename iterator_traits<_InputIterator2>::value_type, 05232 typename iterator_traits<_InputIterator1>::value_type>) 05233 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); 05234 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); 05235 __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp); 05236 __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp); 05237 05238 return _GLIBCXX_STD_A::__set_union(__first1, __last1, 05239 __first2, __last2, __result, 05240 __gnu_cxx::__ops::__iter_comp_iter(__comp)); 05241 } 05242 05243 template<typename _InputIterator1, typename _InputIterator2, 05244 typename _OutputIterator, 05245 typename _Compare> 05246 _OutputIterator 05247 __set_intersection(_InputIterator1 __first1, _InputIterator1 __last1, 05248 _InputIterator2 __first2, _InputIterator2 __last2, 05249 _OutputIterator __result, _Compare __comp) 05250 { 05251 while (__first1 != __last1 && __first2 != __last2) 05252 if (__comp(__first1, __first2)) 05253 ++__first1; 05254 else if (__comp(__first2, __first1)) 05255 ++__first2; 05256 else 05257 { 05258 *__result = *__first1; 05259 ++__first1; 05260 ++__first2; 05261 ++__result; 05262 } 05263 return __result; 05264 } 05265 05266 /** 05267 * @brief Return the intersection of two sorted ranges. 05268 * @ingroup set_algorithms 05269 * @param __first1 Start of first range. 05270 * @param __last1 End of first range. 05271 * @param __first2 Start of second range. 05272 * @param __last2 End of second range. 05273 * @param __result Start of output range. 05274 * @return End of the output range. 05275 * @ingroup set_algorithms 05276 * 05277 * This operation iterates over both ranges, copying elements present in 05278 * both ranges in order to the output range. Iterators increment for each 05279 * range. When the current element of one range is less than the other, 05280 * that iterator advances. If an element is contained in both ranges, the 05281 * element from the first range is copied and both ranges advance. The 05282 * output range may not overlap either input range. 05283 */ 05284 template<typename _InputIterator1, typename _InputIterator2, 05285 typename _OutputIterator> 05286 inline _OutputIterator 05287 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1, 05288 _InputIterator2 __first2, _InputIterator2 __last2, 05289 _OutputIterator __result) 05290 { 05291 // concept requirements 05292 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 05293 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 05294 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 05295 typename iterator_traits<_InputIterator1>::value_type>) 05296 __glibcxx_function_requires(_LessThanOpConcept< 05297 typename iterator_traits<_InputIterator1>::value_type, 05298 typename iterator_traits<_InputIterator2>::value_type>) 05299 __glibcxx_function_requires(_LessThanOpConcept< 05300 typename iterator_traits<_InputIterator2>::value_type, 05301 typename iterator_traits<_InputIterator1>::value_type>) 05302 __glibcxx_requires_sorted_set(__first1, __last1, __first2); 05303 __glibcxx_requires_sorted_set(__first2, __last2, __first1); 05304 __glibcxx_requires_irreflexive2(__first1, __last1); 05305 __glibcxx_requires_irreflexive2(__first2, __last2); 05306 05307 return _GLIBCXX_STD_A::__set_intersection(__first1, __last1, 05308 __first2, __last2, __result, 05309 __gnu_cxx::__ops::__iter_less_iter()); 05310 } 05311 05312 /** 05313 * @brief Return the intersection of two sorted ranges using comparison 05314 * functor. 05315 * @ingroup set_algorithms 05316 * @param __first1 Start of first range. 05317 * @param __last1 End of first range. 05318 * @param __first2 Start of second range. 05319 * @param __last2 End of second range. 05320 * @param __result Start of output range. 05321 * @param __comp The comparison functor. 05322 * @return End of the output range. 05323 * @ingroup set_algorithms 05324 * 05325 * This operation iterates over both ranges, copying elements present in 05326 * both ranges in order to the output range. Iterators increment for each 05327 * range. When the current element of one range is less than the other 05328 * according to @p __comp, that iterator advances. If an element is 05329 * contained in both ranges according to @p __comp, the element from the 05330 * first range is copied and both ranges advance. The output range may not 05331 * overlap either input range. 05332 */ 05333 template<typename _InputIterator1, typename _InputIterator2, 05334 typename _OutputIterator, typename _Compare> 05335 inline _OutputIterator 05336 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1, 05337 _InputIterator2 __first2, _InputIterator2 __last2, 05338 _OutputIterator __result, _Compare __comp) 05339 { 05340 // concept requirements 05341 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 05342 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 05343 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 05344 typename iterator_traits<_InputIterator1>::value_type>) 05345 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 05346 typename iterator_traits<_InputIterator1>::value_type, 05347 typename iterator_traits<_InputIterator2>::value_type>) 05348 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 05349 typename iterator_traits<_InputIterator2>::value_type, 05350 typename iterator_traits<_InputIterator1>::value_type>) 05351 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); 05352 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); 05353 __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp); 05354 __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp); 05355 05356 return _GLIBCXX_STD_A::__set_intersection(__first1, __last1, 05357 __first2, __last2, __result, 05358 __gnu_cxx::__ops::__iter_comp_iter(__comp)); 05359 } 05360 05361 template<typename _InputIterator1, typename _InputIterator2, 05362 typename _OutputIterator, 05363 typename _Compare> 05364 _OutputIterator 05365 __set_difference(_InputIterator1 __first1, _InputIterator1 __last1, 05366 _InputIterator2 __first2, _InputIterator2 __last2, 05367 _OutputIterator __result, _Compare __comp) 05368 { 05369 while (__first1 != __last1 && __first2 != __last2) 05370 if (__comp(__first1, __first2)) 05371 { 05372 *__result = *__first1; 05373 ++__first1; 05374 ++__result; 05375 } 05376 else if (__comp(__first2, __first1)) 05377 ++__first2; 05378 else 05379 { 05380 ++__first1; 05381 ++__first2; 05382 } 05383 return std::copy(__first1, __last1, __result); 05384 } 05385 05386 /** 05387 * @brief Return the difference of two sorted ranges. 05388 * @ingroup set_algorithms 05389 * @param __first1 Start of first range. 05390 * @param __last1 End of first range. 05391 * @param __first2 Start of second range. 05392 * @param __last2 End of second range. 05393 * @param __result Start of output range. 05394 * @return End of the output range. 05395 * @ingroup set_algorithms 05396 * 05397 * This operation iterates over both ranges, copying elements present in 05398 * the first range but not the second in order to the output range. 05399 * Iterators increment for each range. When the current element of the 05400 * first range is less than the second, that element is copied and the 05401 * iterator advances. If the current element of the second range is less, 05402 * the iterator advances, but no element is copied. If an element is 05403 * contained in both ranges, no elements are copied and both ranges 05404 * advance. The output range may not overlap either input range. 05405 */ 05406 template<typename _InputIterator1, typename _InputIterator2, 05407 typename _OutputIterator> 05408 inline _OutputIterator 05409 set_difference(_InputIterator1 __first1, _InputIterator1 __last1, 05410 _InputIterator2 __first2, _InputIterator2 __last2, 05411 _OutputIterator __result) 05412 { 05413 // concept requirements 05414 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 05415 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 05416 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 05417 typename iterator_traits<_InputIterator1>::value_type>) 05418 __glibcxx_function_requires(_LessThanOpConcept< 05419 typename iterator_traits<_InputIterator1>::value_type, 05420 typename iterator_traits<_InputIterator2>::value_type>) 05421 __glibcxx_function_requires(_LessThanOpConcept< 05422 typename iterator_traits<_InputIterator2>::value_type, 05423 typename iterator_traits<_InputIterator1>::value_type>) 05424 __glibcxx_requires_sorted_set(__first1, __last1, __first2); 05425 __glibcxx_requires_sorted_set(__first2, __last2, __first1); 05426 __glibcxx_requires_irreflexive2(__first1, __last1); 05427 __glibcxx_requires_irreflexive2(__first2, __last2); 05428 05429 return _GLIBCXX_STD_A::__set_difference(__first1, __last1, 05430 __first2, __last2, __result, 05431 __gnu_cxx::__ops::__iter_less_iter()); 05432 } 05433 05434 /** 05435 * @brief Return the difference of two sorted ranges using comparison 05436 * functor. 05437 * @ingroup set_algorithms 05438 * @param __first1 Start of first range. 05439 * @param __last1 End of first range. 05440 * @param __first2 Start of second range. 05441 * @param __last2 End of second range. 05442 * @param __result Start of output range. 05443 * @param __comp The comparison functor. 05444 * @return End of the output range. 05445 * @ingroup set_algorithms 05446 * 05447 * This operation iterates over both ranges, copying elements present in 05448 * the first range but not the second in order to the output range. 05449 * Iterators increment for each range. When the current element of the 05450 * first range is less than the second according to @p __comp, that element 05451 * is copied and the iterator advances. If the current element of the 05452 * second range is less, no element is copied and the iterator advances. 05453 * If an element is contained in both ranges according to @p __comp, no 05454 * elements are copied and both ranges advance. The output range may not 05455 * overlap either input range. 05456 */ 05457 template<typename _InputIterator1, typename _InputIterator2, 05458 typename _OutputIterator, typename _Compare> 05459 inline _OutputIterator 05460 set_difference(_InputIterator1 __first1, _InputIterator1 __last1, 05461 _InputIterator2 __first2, _InputIterator2 __last2, 05462 _OutputIterator __result, _Compare __comp) 05463 { 05464 // concept requirements 05465 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 05466 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 05467 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 05468 typename iterator_traits<_InputIterator1>::value_type>) 05469 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 05470 typename iterator_traits<_InputIterator1>::value_type, 05471 typename iterator_traits<_InputIterator2>::value_type>) 05472 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 05473 typename iterator_traits<_InputIterator2>::value_type, 05474 typename iterator_traits<_InputIterator1>::value_type>) 05475 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); 05476 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); 05477 __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp); 05478 __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp); 05479 05480 return _GLIBCXX_STD_A::__set_difference(__first1, __last1, 05481 __first2, __last2, __result, 05482 __gnu_cxx::__ops::__iter_comp_iter(__comp)); 05483 } 05484 05485 template<typename _InputIterator1, typename _InputIterator2, 05486 typename _OutputIterator, 05487 typename _Compare> 05488 _OutputIterator 05489 __set_symmetric_difference(_InputIterator1 __first1, 05490 _InputIterator1 __last1, 05491 _InputIterator2 __first2, 05492 _InputIterator2 __last2, 05493 _OutputIterator __result, 05494 _Compare __comp) 05495 { 05496 while (__first1 != __last1 && __first2 != __last2) 05497 if (__comp(__first1, __first2)) 05498 { 05499 *__result = *__first1; 05500 ++__first1; 05501 ++__result; 05502 } 05503 else if (__comp(__first2, __first1)) 05504 { 05505 *__result = *__first2; 05506 ++__first2; 05507 ++__result; 05508 } 05509 else 05510 { 05511 ++__first1; 05512 ++__first2; 05513 } 05514 return std::copy(__first2, __last2, 05515 std::copy(__first1, __last1, __result)); 05516 } 05517 05518 /** 05519 * @brief Return the symmetric difference of two sorted ranges. 05520 * @ingroup set_algorithms 05521 * @param __first1 Start of first range. 05522 * @param __last1 End of first range. 05523 * @param __first2 Start of second range. 05524 * @param __last2 End of second range. 05525 * @param __result Start of output range. 05526 * @return End of the output range. 05527 * @ingroup set_algorithms 05528 * 05529 * This operation iterates over both ranges, copying elements present in 05530 * one range but not the other in order to the output range. Iterators 05531 * increment for each range. When the current element of one range is less 05532 * than the other, that element is copied and the iterator advances. If an 05533 * element is contained in both ranges, no elements are copied and both 05534 * ranges advance. The output range may not overlap either input range. 05535 */ 05536 template<typename _InputIterator1, typename _InputIterator2, 05537 typename _OutputIterator> 05538 inline _OutputIterator 05539 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1, 05540 _InputIterator2 __first2, _InputIterator2 __last2, 05541 _OutputIterator __result) 05542 { 05543 // concept requirements 05544 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 05545 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 05546 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 05547 typename iterator_traits<_InputIterator1>::value_type>) 05548 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 05549 typename iterator_traits<_InputIterator2>::value_type>) 05550 __glibcxx_function_requires(_LessThanOpConcept< 05551 typename iterator_traits<_InputIterator1>::value_type, 05552 typename iterator_traits<_InputIterator2>::value_type>) 05553 __glibcxx_function_requires(_LessThanOpConcept< 05554 typename iterator_traits<_InputIterator2>::value_type, 05555 typename iterator_traits<_InputIterator1>::value_type>) 05556 __glibcxx_requires_sorted_set(__first1, __last1, __first2); 05557 __glibcxx_requires_sorted_set(__first2, __last2, __first1); 05558 __glibcxx_requires_irreflexive2(__first1, __last1); 05559 __glibcxx_requires_irreflexive2(__first2, __last2); 05560 05561 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1, __last1, 05562 __first2, __last2, __result, 05563 __gnu_cxx::__ops::__iter_less_iter()); 05564 } 05565 05566 /** 05567 * @brief Return the symmetric difference of two sorted ranges using 05568 * comparison functor. 05569 * @ingroup set_algorithms 05570 * @param __first1 Start of first range. 05571 * @param __last1 End of first range. 05572 * @param __first2 Start of second range. 05573 * @param __last2 End of second range. 05574 * @param __result Start of output range. 05575 * @param __comp The comparison functor. 05576 * @return End of the output range. 05577 * @ingroup set_algorithms 05578 * 05579 * This operation iterates over both ranges, copying elements present in 05580 * one range but not the other in order to the output range. Iterators 05581 * increment for each range. When the current element of one range is less 05582 * than the other according to @p comp, that element is copied and the 05583 * iterator advances. If an element is contained in both ranges according 05584 * to @p __comp, no elements are copied and both ranges advance. The output 05585 * range may not overlap either input range. 05586 */ 05587 template<typename _InputIterator1, typename _InputIterator2, 05588 typename _OutputIterator, typename _Compare> 05589 inline _OutputIterator 05590 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1, 05591 _InputIterator2 __first2, _InputIterator2 __last2, 05592 _OutputIterator __result, 05593 _Compare __comp) 05594 { 05595 // concept requirements 05596 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 05597 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 05598 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 05599 typename iterator_traits<_InputIterator1>::value_type>) 05600 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 05601 typename iterator_traits<_InputIterator2>::value_type>) 05602 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 05603 typename iterator_traits<_InputIterator1>::value_type, 05604 typename iterator_traits<_InputIterator2>::value_type>) 05605 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 05606 typename iterator_traits<_InputIterator2>::value_type, 05607 typename iterator_traits<_InputIterator1>::value_type>) 05608 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); 05609 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); 05610 __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp); 05611 __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp); 05612 05613 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1, __last1, 05614 __first2, __last2, __result, 05615 __gnu_cxx::__ops::__iter_comp_iter(__comp)); 05616 } 05617 05618 template<typename _ForwardIterator, typename _Compare> 05619 _GLIBCXX14_CONSTEXPR 05620 _ForwardIterator 05621 __min_element(_ForwardIterator __first, _ForwardIterator __last, 05622 _Compare __comp) 05623 { 05624 if (__first == __last) 05625 return __first; 05626 _ForwardIterator __result = __first; 05627 while (++__first != __last) 05628 if (__comp(__first, __result)) 05629 __result = __first; 05630 return __result; 05631 } 05632 05633 /** 05634 * @brief Return the minimum element in a range. 05635 * @ingroup sorting_algorithms 05636 * @param __first Start of range. 05637 * @param __last End of range. 05638 * @return Iterator referencing the first instance of the smallest value. 05639 */ 05640 template<typename _ForwardIterator> 05641 _GLIBCXX14_CONSTEXPR 05642 _ForwardIterator 05643 inline min_element(_ForwardIterator __first, _ForwardIterator __last) 05644 { 05645 // concept requirements 05646 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 05647 __glibcxx_function_requires(_LessThanComparableConcept< 05648 typename iterator_traits<_ForwardIterator>::value_type>) 05649 __glibcxx_requires_valid_range(__first, __last); 05650 __glibcxx_requires_irreflexive(__first, __last); 05651 05652 return _GLIBCXX_STD_A::__min_element(__first, __last, 05653 __gnu_cxx::__ops::__iter_less_iter()); 05654 } 05655 05656 /** 05657 * @brief Return the minimum element in a range using comparison functor. 05658 * @ingroup sorting_algorithms 05659 * @param __first Start of range. 05660 * @param __last End of range. 05661 * @param __comp Comparison functor. 05662 * @return Iterator referencing the first instance of the smallest value 05663 * according to __comp. 05664 */ 05665 template<typename _ForwardIterator, typename _Compare> 05666 _GLIBCXX14_CONSTEXPR 05667 inline _ForwardIterator 05668 min_element(_ForwardIterator __first, _ForwardIterator __last, 05669 _Compare __comp) 05670 { 05671 // concept requirements 05672 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 05673 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 05674 typename iterator_traits<_ForwardIterator>::value_type, 05675 typename iterator_traits<_ForwardIterator>::value_type>) 05676 __glibcxx_requires_valid_range(__first, __last); 05677 __glibcxx_requires_irreflexive_pred(__first, __last, __comp); 05678 05679 return _GLIBCXX_STD_A::__min_element(__first, __last, 05680 __gnu_cxx::__ops::__iter_comp_iter(__comp)); 05681 } 05682 05683 template<typename _ForwardIterator, typename _Compare> 05684 _GLIBCXX14_CONSTEXPR 05685 _ForwardIterator 05686 __max_element(_ForwardIterator __first, _ForwardIterator __last, 05687 _Compare __comp) 05688 { 05689 if (__first == __last) return __first; 05690 _ForwardIterator __result = __first; 05691 while (++__first != __last) 05692 if (__comp(__result, __first)) 05693 __result = __first; 05694 return __result; 05695 } 05696 05697 /** 05698 * @brief Return the maximum element in a range. 05699 * @ingroup sorting_algorithms 05700 * @param __first Start of range. 05701 * @param __last End of range. 05702 * @return Iterator referencing the first instance of the largest value. 05703 */ 05704 template<typename _ForwardIterator> 05705 _GLIBCXX14_CONSTEXPR 05706 inline _ForwardIterator 05707 max_element(_ForwardIterator __first, _ForwardIterator __last) 05708 { 05709 // concept requirements 05710 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 05711 __glibcxx_function_requires(_LessThanComparableConcept< 05712 typename iterator_traits<_ForwardIterator>::value_type>) 05713 __glibcxx_requires_valid_range(__first, __last); 05714 __glibcxx_requires_irreflexive(__first, __last); 05715 05716 return _GLIBCXX_STD_A::__max_element(__first, __last, 05717 __gnu_cxx::__ops::__iter_less_iter()); 05718 } 05719 05720 /** 05721 * @brief Return the maximum element in a range using comparison functor. 05722 * @ingroup sorting_algorithms 05723 * @param __first Start of range. 05724 * @param __last End of range. 05725 * @param __comp Comparison functor. 05726 * @return Iterator referencing the first instance of the largest value 05727 * according to __comp. 05728 */ 05729 template<typename _ForwardIterator, typename _Compare> 05730 _GLIBCXX14_CONSTEXPR 05731 inline _ForwardIterator 05732 max_element(_ForwardIterator __first, _ForwardIterator __last, 05733 _Compare __comp) 05734 { 05735 // concept requirements 05736 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 05737 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 05738 typename iterator_traits<_ForwardIterator>::value_type, 05739 typename iterator_traits<_ForwardIterator>::value_type>) 05740 __glibcxx_requires_valid_range(__first, __last); 05741 __glibcxx_requires_irreflexive_pred(__first, __last, __comp); 05742 05743 return _GLIBCXX_STD_A::__max_element(__first, __last, 05744 __gnu_cxx::__ops::__iter_comp_iter(__comp)); 05745 } 05746 05747 #if __cplusplus >= 201402L 05748 /// Reservoir sampling algorithm. 05749 template<typename _InputIterator, typename _RandomAccessIterator, 05750 typename _Size, typename _UniformRandomBitGenerator> 05751 _RandomAccessIterator 05752 __sample(_InputIterator __first, _InputIterator __last, input_iterator_tag, 05753 _RandomAccessIterator __out, random_access_iterator_tag, 05754 _Size __n, _UniformRandomBitGenerator&& __g) 05755 { 05756 using __distrib_type = uniform_int_distribution<_Size>; 05757 using __param_type = typename __distrib_type::param_type; 05758 __distrib_type __d{}; 05759 _Size __sample_sz = 0; 05760 while (__first != __last && __sample_sz != __n) 05761 { 05762 __out[__sample_sz++] = *__first; 05763 ++__first; 05764 } 05765 for (auto __pop_sz = __sample_sz; __first != __last; 05766 ++__first, (void) ++__pop_sz) 05767 { 05768 const auto __k = __d(__g, __param_type{0, __pop_sz}); 05769 if (__k < __n) 05770 __out[__k] = *__first; 05771 } 05772 return __out + __sample_sz; 05773 } 05774 05775 /// Selection sampling algorithm. 05776 template<typename _ForwardIterator, typename _OutputIterator, typename _Cat, 05777 typename _Size, typename _UniformRandomBitGenerator> 05778 _OutputIterator 05779 __sample(_ForwardIterator __first, _ForwardIterator __last, 05780 forward_iterator_tag, 05781 _OutputIterator __out, _Cat, 05782 _Size __n, _UniformRandomBitGenerator&& __g) 05783 { 05784 using __distrib_type = uniform_int_distribution<_Size>; 05785 using __param_type = typename __distrib_type::param_type; 05786 using _USize = make_unsigned_t<_Size>; 05787 using _Gen = remove_reference_t<_UniformRandomBitGenerator>; 05788 using __uc_type = common_type_t<typename _Gen::result_type, _USize>; 05789 05790 __distrib_type __d{}; 05791 _Size __unsampled_sz = std::distance(__first, __last); 05792 __n = std::min(__n, __unsampled_sz); 05793 05794 // If possible, we use __gen_two_uniform_ints to efficiently produce 05795 // two random numbers using a single distribution invocation: 05796 05797 const __uc_type __urngrange = __g.max() - __g.min(); 05798 if (__urngrange / __uc_type(__unsampled_sz) >= __uc_type(__unsampled_sz)) 05799 // I.e. (__urngrange >= __unsampled_sz * __unsampled_sz) but without 05800 // wrapping issues. 05801 { 05802 while (__n != 0 && __unsampled_sz >= 2) 05803 { 05804 const pair<_Size, _Size> __p = 05805 __gen_two_uniform_ints(__unsampled_sz, __unsampled_sz - 1, __g); 05806 05807 --__unsampled_sz; 05808 if (__p.first < __n) 05809 { 05810 *__out++ = *__first; 05811 --__n; 05812 } 05813 05814 ++__first; 05815 05816 if (__n == 0) break; 05817 05818 --__unsampled_sz; 05819 if (__p.second < __n) 05820 { 05821 *__out++ = *__first; 05822 --__n; 05823 } 05824 05825 ++__first; 05826 } 05827 } 05828 05829 // The loop above is otherwise equivalent to this one-at-a-time version: 05830 05831 for (; __n != 0; ++__first) 05832 if (__d(__g, __param_type{0, --__unsampled_sz}) < __n) 05833 { 05834 *__out++ = *__first; 05835 --__n; 05836 } 05837 return __out; 05838 } 05839 05840 #if __cplusplus > 201402L 05841 #define __cpp_lib_sample 201603 05842 /// Take a random sample from a population. 05843 template<typename _PopulationIterator, typename _SampleIterator, 05844 typename _Distance, typename _UniformRandomBitGenerator> 05845 _SampleIterator 05846 sample(_PopulationIterator __first, _PopulationIterator __last, 05847 _SampleIterator __out, _Distance __n, 05848 _UniformRandomBitGenerator&& __g) 05849 { 05850 using __pop_cat = typename 05851 std::iterator_traits<_PopulationIterator>::iterator_category; 05852 using __samp_cat = typename 05853 std::iterator_traits<_SampleIterator>::iterator_category; 05854 05855 static_assert( 05856 __or_<is_convertible<__pop_cat, forward_iterator_tag>, 05857 is_convertible<__samp_cat, random_access_iterator_tag>>::value, 05858 "output range must use a RandomAccessIterator when input range" 05859 " does not meet the ForwardIterator requirements"); 05860 05861 static_assert(is_integral<_Distance>::value, 05862 "sample size must be an integer type"); 05863 05864 typename iterator_traits<_PopulationIterator>::difference_type __d = __n; 05865 return _GLIBCXX_STD_A:: 05866 __sample(__first, __last, __pop_cat{}, __out, __samp_cat{}, __d, 05867 std::forward<_UniformRandomBitGenerator>(__g)); 05868 } 05869 #endif // C++17 05870 #endif // C++14 05871 05872 _GLIBCXX_END_NAMESPACE_ALGO 05873 _GLIBCXX_END_NAMESPACE_VERSION 05874 } // namespace std 05875 05876 #endif /* _STL_ALGO_H */