1 // Algorithm implementation -*- C++ -*- 2 3 // Copyright (C) 2001-2014 Free Software Foundation, Inc. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the 7 // terms of the GNU General Public License as published by the 8 // Free Software Foundation; either version 3, or (at your option) 9 // any later version. 10 11 // This library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 16 // Under Section 7 of GPL version 3, you are granted additional 17 // permissions described in the GCC Runtime Library Exception, version 18 // 3.1, as published by the Free Software Foundation. 19 20 // You should have received a copy of the GNU General Public License and 21 // a copy of the GCC Runtime Library Exception along with this program; 22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 // <http://www.gnu.org/licenses/>. 24 25 /* 26 * 27 * Copyright (c) 1994 28 * Hewlett-Packard Company 29 * 30 * Permission to use, copy, modify, distribute and sell this software 31 * and its documentation for any purpose is hereby granted without fee, 32 * provided that the above copyright notice appear in all copies and 33 * that both that copyright notice and this permission notice appear 34 * in supporting documentation. Hewlett-Packard Company makes no 35 * representations about the suitability of this software for any 36 * purpose. It is provided "as is" without express or implied warranty. 37 * 38 * 39 * Copyright (c) 1996 40 * Silicon Graphics Computer Systems, Inc. 41 * 42 * Permission to use, copy, modify, distribute and sell this software 43 * and its documentation for any purpose is hereby granted without fee, 44 * provided that the above copyright notice appear in all copies and 45 * that both that copyright notice and this permission notice appear 46 * in supporting documentation. Silicon Graphics makes no 47 * representations about the suitability of this software for any 48 * purpose. It is provided "as is" without express or implied warranty. 49 */ 50 51 /** @file bits/stl_algo.h 52 * This is an internal header file, included by other library headers. 53 * Do not attempt to use it directly. @headername{algorithm} 54 */ 55 56 #ifndef _STL_ALGO_H 57 #define _STL_ALGO_H 1 58 59 #include <cstdlib> // for rand 60 #include <bits/algorithmfwd.h> 61 #include <bits/stl_heap.h> 62 #include <bits/stl_tempbuf.h> // for _Temporary_buffer 63 #include <bits/predefined_ops.h> 64 65 #if __cplusplus >= 201103L 66 #include <random> // for std::uniform_int_distribution 67 #endif 68 69 // See concept_check.h for the __glibcxx_*_requires macros. 70 71 namespace std _GLIBCXX_VISIBILITY(default) 72 { 73 _GLIBCXX_BEGIN_NAMESPACE_VERSION 74 75 /// Swaps the median value of *__a, *__b and *__c under __comp to *__result 76 template<typename _Iterator, typename _Compare> 77 void 78 __move_median_to_first(_Iterator __result,_Iterator __a, _Iterator __b, 79 _Iterator __c, _Compare __comp) 80 { 81 if (__comp(__a, __b)) 82 { 83 if (__comp(__b, __c)) 84 std::iter_swap(__result, __b); 85 else if (__comp(__a, __c)) 86 std::iter_swap(__result, __c); 87 else 88 std::iter_swap(__result, __a); 89 } 90 else if (__comp(__a, __c)) 91 std::iter_swap(__result, __a); 92 else if (__comp(__b, __c)) 93 std::iter_swap(__result, __c); 94 else 95 std::iter_swap(__result, __b); 96 } 97 98 /// This is an overload used by find algos for the Input Iterator case. 99 template<typename _InputIterator, typename _Predicate> 100 inline _InputIterator 101 __find_if(_InputIterator __first, _InputIterator __last, 102 _Predicate __pred, input_iterator_tag) 103 { 104 while (__first != __last && !__pred(__first)) 105 ++__first; 106 return __first; 107 } 108 109 /// This is an overload used by find algos for the RAI case. 110 template<typename _RandomAccessIterator, typename _Predicate> 111 _RandomAccessIterator 112 __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last, 113 _Predicate __pred, random_access_iterator_tag) 114 { 115 typename iterator_traits<_RandomAccessIterator>::difference_type 116 __trip_count = (__last - __first) >> 2; 117 118 for (; __trip_count > 0; --__trip_count) 119 { 120 if (__pred(__first)) 121 return __first; 122 ++__first; 123 124 if (__pred(__first)) 125 return __first; 126 ++__first; 127 128 if (__pred(__first)) 129 return __first; 130 ++__first; 131 132 if (__pred(__first)) 133 return __first; 134 ++__first; 135 } 136 137 switch (__last - __first) 138 { 139 case 3: 140 if (__pred(__first)) 141 return __first; 142 ++__first; 143 case 2: 144 if (__pred(__first)) 145 return __first; 146 ++__first; 147 case 1: 148 if (__pred(__first)) 149 return __first; 150 ++__first; 151 case 0: 152 default: 153 return __last; 154 } 155 } 156 157 template<typename _Iterator, typename _Predicate> 158 inline _Iterator 159 __find_if(_Iterator __first, _Iterator __last, _Predicate __pred) 160 { 161 return __find_if(__first, __last, __pred, 162 std::__iterator_category(__first)); 163 } 164 165 /// Provided for stable_partition to use. 166 template<typename _InputIterator, typename _Predicate> 167 inline _InputIterator 168 __find_if_not(_InputIterator __first, _InputIterator __last, 169 _Predicate __pred) 170 { 171 return std::__find_if(__first, __last, 172 __gnu_cxx::__ops::__negate(__pred), 173 std::__iterator_category(__first)); 174 } 175 176 /// Like find_if_not(), but uses and updates a count of the 177 /// remaining range length instead of comparing against an end 178 /// iterator. 179 template<typename _InputIterator, typename _Predicate, typename _Distance> 180 _InputIterator 181 __find_if_not_n(_InputIterator __first, _Distance& __len, _Predicate __pred) 182 { 183 for (; __len; --__len, ++__first) 184 if (!__pred(__first)) 185 break; 186 return __first; 187 } 188 189 // set_difference 190 // set_intersection 191 // set_symmetric_difference 192 // set_union 193 // for_each 194 // find 195 // find_if 196 // find_first_of 197 // adjacent_find 198 // count 199 // count_if 200 // search 201 202 // Local modification: if __google_stl_debug_compare is defined to 203 // non-zero value, check sort predicate for strict weak ordering. 204 // Google ref b/1731200. 205 #if __google_stl_debug_compare 206 template<typename _Compare> 207 struct _CheckedCompare { 208 _Compare _M_compare; 209 210 _CheckedCompare(const _Compare & __comp): _M_compare(__comp) { } 211 212 template <typename _Tp> 213 bool operator()(const _Tp& __x, const _Tp& __y) { 214 if (_M_compare(__x, __x)) 215 __throw_runtime_error("strict weak ordering: (__x LT __x) != false"); 216 if (_M_compare(__y, __y)) 217 __throw_runtime_error("strict weak ordering: (__y LT __y) != false"); 218 bool lt = _M_compare(__x, __y); 219 if (lt && _M_compare(__y, __x)) 220 __throw_runtime_error("strict weak ordering: ((__x LT __y) && (__y LT __x)) != false"); 221 return lt; 222 } 223 224 // Different types; can't perform any checks. 225 template <typename _Tp1, typename _Tp2> 226 bool operator()(const _Tp1& __x, const _Tp2& __y) { 227 return _M_compare(__x, __y); 228 } 229 }; 230 # define __CheckedCompare(__comp) _CheckedCompare<__typeof__(__comp)>(__comp) 231 #else 232 # define __CheckedCompare(__comp) __comp 233 #endif 234 235 236 template<typename _ForwardIterator1, typename _ForwardIterator2, 237 typename _BinaryPredicate> 238 _ForwardIterator1 239 __search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 240 _ForwardIterator2 __first2, _ForwardIterator2 __last2, 241 _BinaryPredicate __predicate) 242 { 243 // Test for empty ranges 244 if (__first1 == __last1 || __first2 == __last2) 245 return __first1; 246 247 // Test for a pattern of length 1. 248 _ForwardIterator2 __p1(__first2); 249 if (++__p1 == __last2) 250 return std::__find_if(__first1, __last1, 251 __gnu_cxx::__ops::__iter_comp_iter(__predicate, __first2)); 252 253 // General case. 254 _ForwardIterator2 __p; 255 _ForwardIterator1 __current = __first1; 256 257 for (;;) 258 { 259 __first1 = 260 std::__find_if(__first1, __last1, 261 __gnu_cxx::__ops::__iter_comp_iter(__predicate, __first2)); 262 263 if (__first1 == __last1) 264 return __last1; 265 266 __p = __p1; 267 __current = __first1; 268 if (++__current == __last1) 269 return __last1; 270 271 while (__predicate(__current, __p)) 272 { 273 if (++__p == __last2) 274 return __first1; 275 if (++__current == __last1) 276 return __last1; 277 } 278 ++__first1; 279 } 280 return __first1; 281 } 282 283 // search_n 284 285 /** 286 * This is an helper function for search_n overloaded for forward iterators. 287 */ 288 template<typename _ForwardIterator, typename _Integer, 289 typename _UnaryPredicate> 290 _ForwardIterator 291 __search_n_aux(_ForwardIterator __first, _ForwardIterator __last, 292 _Integer __count, _UnaryPredicate __unary_pred, 293 std::forward_iterator_tag) 294 { 295 __first = std::__find_if(__first, __last, __unary_pred); 296 while (__first != __last) 297 { 298 typename iterator_traits<_ForwardIterator>::difference_type 299 __n = __count; 300 _ForwardIterator __i = __first; 301 ++__i; 302 while (__i != __last && __n != 1 && __unary_pred(__i)) 303 { 304 ++__i; 305 --__n; 306 } 307 if (__n == 1) 308 return __first; 309 if (__i == __last) 310 return __last; 311 __first = std::__find_if(++__i, __last, __unary_pred); 312 } 313 return __last; 314 } 315 316 /** 317 * This is an helper function for search_n overloaded for random access 318 * iterators. 319 */ 320 template<typename _RandomAccessIter, typename _Integer, 321 typename _UnaryPredicate> 322 _RandomAccessIter 323 __search_n_aux(_RandomAccessIter __first, _RandomAccessIter __last, 324 _Integer __count, _UnaryPredicate __unary_pred, 325 std::random_access_iterator_tag) 326 { 327 typedef typename std::iterator_traits<_RandomAccessIter>::difference_type 328 _DistanceType; 329 330 _DistanceType __tailSize = __last - __first; 331 _DistanceType __remainder = __count; 332 333 while (__remainder <= __tailSize) // the main loop... 334 { 335 __first += __remainder; 336 __tailSize -= __remainder; 337 // __first here is always pointing to one past the last element of 338 // next possible match. 339 _RandomAccessIter __backTrack = __first; 340 while (__unary_pred(--__backTrack)) 341 { 342 if (--__remainder == 0) 343 return (__first - __count); // Success 344 } 345 __remainder = __count + 1 - (__first - __backTrack); 346 } 347 return __last; // Failure 348 } 349 350 template<typename _ForwardIterator, typename _Integer, 351 typename _UnaryPredicate> 352 _ForwardIterator 353 __search_n(_ForwardIterator __first, _ForwardIterator __last, 354 _Integer __count, 355 _UnaryPredicate __unary_pred) 356 { 357 if (__count <= 0) 358 return __first; 359 360 if (__count == 1) 361 return std::__find_if(__first, __last, __unary_pred); 362 363 return std::__search_n_aux(__first, __last, __count, __unary_pred, 364 std::__iterator_category(__first)); 365 } 366 367 // find_end for forward iterators. 368 template<typename _ForwardIterator1, typename _ForwardIterator2, 369 typename _BinaryPredicate> 370 _ForwardIterator1 371 __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 372 _ForwardIterator2 __first2, _ForwardIterator2 __last2, 373 forward_iterator_tag, forward_iterator_tag, 374 _BinaryPredicate __comp) 375 { 376 if (__first2 == __last2) 377 return __last1; 378 379 _ForwardIterator1 __result = __last1; 380 while (1) 381 { 382 _ForwardIterator1 __new_result 383 = std::__search(__first1, __last1, __first2, __last2, __comp); 384 if (__new_result == __last1) 385 return __result; 386 else 387 { 388 __result = __new_result; 389 __first1 = __new_result; 390 ++__first1; 391 } 392 } 393 } 394 395 // find_end for bidirectional iterators (much faster). 396 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2, 397 typename _BinaryPredicate> 398 _BidirectionalIterator1 399 __find_end(_BidirectionalIterator1 __first1, 400 _BidirectionalIterator1 __last1, 401 _BidirectionalIterator2 __first2, 402 _BidirectionalIterator2 __last2, 403 bidirectional_iterator_tag, bidirectional_iterator_tag, 404 _BinaryPredicate __comp) 405 { 406 // concept requirements 407 __glibcxx_function_requires(_BidirectionalIteratorConcept< 408 _BidirectionalIterator1>) 409 __glibcxx_function_requires(_BidirectionalIteratorConcept< 410 _BidirectionalIterator2>) 411 412 typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1; 413 typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2; 414 415 _RevIterator1 __rlast1(__first1); 416 _RevIterator2 __rlast2(__first2); 417 _RevIterator1 __rresult = std::__search(_RevIterator1(__last1), __rlast1, 418 _RevIterator2(__last2), __rlast2, 419 __comp); 420 421 if (__rresult == __rlast1) 422 return __last1; 423 else 424 { 425 _BidirectionalIterator1 __result = __rresult.base(); 426 std::advance(__result, -std::distance(__first2, __last2)); 427 return __result; 428 } 429 } 430 431 /** 432 * @brief Find last matching subsequence in a sequence. 433 * @ingroup non_mutating_algorithms 434 * @param __first1 Start of range to search. 435 * @param __last1 End of range to search. 436 * @param __first2 Start of sequence to match. 437 * @param __last2 End of sequence to match. 438 * @return The last iterator @c i in the range 439 * @p [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == 440 * @p *(__first2+N) for each @c N in the range @p 441 * [0,__last2-__first2), or @p __last1 if no such iterator exists. 442 * 443 * Searches the range @p [__first1,__last1) for a sub-sequence that 444 * compares equal value-by-value with the sequence given by @p 445 * [__first2,__last2) and returns an iterator to the __first 446 * element of the sub-sequence, or @p __last1 if the sub-sequence 447 * is not found. The sub-sequence will be the last such 448 * subsequence contained in [__first1,__last1). 449 * 450 * Because the sub-sequence must lie completely within the range @p 451 * [__first1,__last1) it must start at a position less than @p 452 * __last1-(__last2-__first2) where @p __last2-__first2 is the 453 * length of the sub-sequence. This means that the returned 454 * iterator @c i will be in the range @p 455 * [__first1,__last1-(__last2-__first2)) 456 */ 457 template<typename _ForwardIterator1, typename _ForwardIterator2> 458 inline _ForwardIterator1 459 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 460 _ForwardIterator2 __first2, _ForwardIterator2 __last2) 461 { 462 // concept requirements 463 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) 464 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) 465 __glibcxx_function_requires(_EqualOpConcept< 466 typename iterator_traits<_ForwardIterator1>::value_type, 467 typename iterator_traits<_ForwardIterator2>::value_type>) 468 __glibcxx_requires_valid_range(__first1, __last1); 469 __glibcxx_requires_valid_range(__first2, __last2); 470 471 return std::__find_end(__first1, __last1, __first2, __last2, 472 std::__iterator_category(__first1), 473 std::__iterator_category(__first2), 474 __gnu_cxx::__ops::__iter_equal_to_iter()); 475 } 476 477 /** 478 * @brief Find last matching subsequence in a sequence using a predicate. 479 * @ingroup non_mutating_algorithms 480 * @param __first1 Start of range to search. 481 * @param __last1 End of range to search. 482 * @param __first2 Start of sequence to match. 483 * @param __last2 End of sequence to match. 484 * @param __comp The predicate to use. 485 * @return The last iterator @c i in the range @p 486 * [__first1,__last1-(__last2-__first2)) such that @c 487 * predicate(*(i+N), @p (__first2+N)) is true for each @c N in the 488 * range @p [0,__last2-__first2), or @p __last1 if no such iterator 489 * exists. 490 * 491 * Searches the range @p [__first1,__last1) for a sub-sequence that 492 * compares equal value-by-value with the sequence given by @p 493 * [__first2,__last2) using comp as a predicate and returns an 494 * iterator to the first element of the sub-sequence, or @p __last1 495 * if the sub-sequence is not found. The sub-sequence will be the 496 * last such subsequence contained in [__first,__last1). 497 * 498 * Because the sub-sequence must lie completely within the range @p 499 * [__first1,__last1) it must start at a position less than @p 500 * __last1-(__last2-__first2) where @p __last2-__first2 is the 501 * length of the sub-sequence. This means that the returned 502 * iterator @c i will be in the range @p 503 * [__first1,__last1-(__last2-__first2)) 504 */ 505 template<typename _ForwardIterator1, typename _ForwardIterator2, 506 typename _BinaryPredicate> 507 inline _ForwardIterator1 508 find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 509 _ForwardIterator2 __first2, _ForwardIterator2 __last2, 510 _BinaryPredicate __comp) 511 { 512 // concept requirements 513 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) 514 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) 515 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, 516 typename iterator_traits<_ForwardIterator1>::value_type, 517 typename iterator_traits<_ForwardIterator2>::value_type>) 518 __glibcxx_requires_valid_range(__first1, __last1); 519 __glibcxx_requires_valid_range(__first2, __last2); 520 521 return std::__find_end(__first1, __last1, __first2, __last2, 522 std::__iterator_category(__first1), 523 std::__iterator_category(__first2), 524 __gnu_cxx::__ops::__iter_comp_iter(__comp)); 525 } 526 527 #if __cplusplus >= 201103L 528 /** 529 * @brief Checks that a predicate is true for all the elements 530 * of a sequence. 531 * @ingroup non_mutating_algorithms 532 * @param __first An input iterator. 533 * @param __last An input iterator. 534 * @param __pred A predicate. 535 * @return True if the check is true, false otherwise. 536 * 537 * Returns true if @p __pred is true for each element in the range 538 * @p [__first,__last), and false otherwise. 539 */ 540 template<typename _InputIterator, typename _Predicate> 541 inline bool 542 all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) 543 { return __last == std::find_if_not(__first, __last, __pred); } 544 545 /** 546 * @brief Checks that a predicate is false for all the elements 547 * of a sequence. 548 * @ingroup non_mutating_algorithms 549 * @param __first An input iterator. 550 * @param __last An input iterator. 551 * @param __pred A predicate. 552 * @return True if the check is true, false otherwise. 553 * 554 * Returns true if @p __pred is false for each element in the range 555 * @p [__first,__last), and false otherwise. 556 */ 557 template<typename _InputIterator, typename _Predicate> 558 inline bool 559 none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) 560 { return __last == _GLIBCXX_STD_A::find_if(__first, __last, __pred); } 561 562 /** 563 * @brief Checks that a predicate is false for at least an element 564 * of a sequence. 565 * @ingroup non_mutating_algorithms 566 * @param __first An input iterator. 567 * @param __last An input iterator. 568 * @param __pred A predicate. 569 * @return True if the check is true, false otherwise. 570 * 571 * Returns true if an element exists in the range @p 572 * [__first,__last) such that @p __pred is true, and false 573 * otherwise. 574 */ 575 template<typename _InputIterator, typename _Predicate> 576 inline bool 577 any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) 578 { return !std::none_of(__first, __last, __pred); } 579 580 /** 581 * @brief Find the first element in a sequence for which a 582 * predicate is false. 583 * @ingroup non_mutating_algorithms 584 * @param __first An input iterator. 585 * @param __last An input iterator. 586 * @param __pred A predicate. 587 * @return The first iterator @c i in the range @p [__first,__last) 588 * such that @p __pred(*i) is false, or @p __last if no such iterator exists. 589 */ 590 template<typename _InputIterator, typename _Predicate> 591 inline _InputIterator 592 find_if_not(_InputIterator __first, _InputIterator __last, 593 _Predicate __pred) 594 { 595 // concept requirements 596 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 597 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 598 typename iterator_traits<_InputIterator>::value_type>) 599 __glibcxx_requires_valid_range(__first, __last); 600 return std::__find_if_not(__first, __last, 601 __gnu_cxx::__ops::__pred_iter(__pred)); 602 } 603 604 /** 605 * @brief Checks whether the sequence is partitioned. 606 * @ingroup mutating_algorithms 607 * @param __first An input iterator. 608 * @param __last An input iterator. 609 * @param __pred A predicate. 610 * @return True if the range @p [__first,__last) is partioned by @p __pred, 611 * i.e. if all elements that satisfy @p __pred appear before those that 612 * do not. 613 */ 614 template<typename _InputIterator, typename _Predicate> 615 inline bool 616 is_partitioned(_InputIterator __first, _InputIterator __last, 617 _Predicate __pred) 618 { 619 __first = std::find_if_not(__first, __last, __pred); 620 return std::none_of(__first, __last, __pred); 621 } 622 623 /** 624 * @brief Find the partition point of a partitioned range. 625 * @ingroup mutating_algorithms 626 * @param __first An iterator. 627 * @param __last Another iterator. 628 * @param __pred A predicate. 629 * @return An iterator @p mid such that @p all_of(__first, mid, __pred) 630 * and @p none_of(mid, __last, __pred) are both true. 631 */ 632 template<typename _ForwardIterator, typename _Predicate> 633 _ForwardIterator 634 partition_point(_ForwardIterator __first, _ForwardIterator __last, 635 _Predicate __pred) 636 { 637 // concept requirements 638 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 639 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 640 typename iterator_traits<_ForwardIterator>::value_type>) 641 642 // A specific debug-mode test will be necessary... 643 __glibcxx_requires_valid_range(__first, __last); 644 645 typedef typename iterator_traits<_ForwardIterator>::difference_type 646 _DistanceType; 647 648 _DistanceType __len = std::distance(__first, __last); 649 _DistanceType __half; 650 _ForwardIterator __middle; 651 652 while (__len > 0) 653 { 654 __half = __len >> 1; 655 __middle = __first; 656 std::advance(__middle, __half); 657 if (__pred(*__middle)) 658 { 659 __first = __middle; 660 ++__first; 661 __len = __len - __half - 1; 662 } 663 else 664 __len = __half; 665 } 666 return __first; 667 } 668 #endif 669 670 template<typename _InputIterator, typename _OutputIterator, 671 typename _Predicate> 672 _OutputIterator 673 __remove_copy_if(_InputIterator __first, _InputIterator __last, 674 _OutputIterator __result, _Predicate __pred) 675 { 676 for (; __first != __last; ++__first) 677 if (!__pred(__first)) 678 { 679 *__result = *__first; 680 ++__result; 681 } 682 return __result; 683 } 684 685 /** 686 * @brief Copy a sequence, removing elements of a given value. 687 * @ingroup mutating_algorithms 688 * @param __first An input iterator. 689 * @param __last An input iterator. 690 * @param __result An output iterator. 691 * @param __value The value to be removed. 692 * @return An iterator designating the end of the resulting sequence. 693 * 694 * Copies each element in the range @p [__first,__last) not equal 695 * to @p __value to the range beginning at @p __result. 696 * remove_copy() is stable, so the relative order of elements that 697 * are copied is unchanged. 698 */ 699 template<typename _InputIterator, typename _OutputIterator, typename _Tp> 700 inline _OutputIterator 701 remove_copy(_InputIterator __first, _InputIterator __last, 702 _OutputIterator __result, const _Tp& __value) 703 { 704 // concept requirements 705 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 706 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 707 typename iterator_traits<_InputIterator>::value_type>) 708 __glibcxx_function_requires(_EqualOpConcept< 709 typename iterator_traits<_InputIterator>::value_type, _Tp>) 710 __glibcxx_requires_valid_range(__first, __last); 711 712 return std::__remove_copy_if(__first, __last, __result, 713 __gnu_cxx::__ops::__iter_equals_val(__value)); 714 } 715 716 /** 717 * @brief Copy a sequence, removing elements for which a predicate is true. 718 * @ingroup mutating_algorithms 719 * @param __first An input iterator. 720 * @param __last An input iterator. 721 * @param __result An output iterator. 722 * @param __pred A predicate. 723 * @return An iterator designating the end of the resulting sequence. 724 * 725 * Copies each element in the range @p [__first,__last) for which 726 * @p __pred returns false to the range beginning at @p __result. 727 * 728 * remove_copy_if() is stable, so the relative order of elements that are 729 * copied is unchanged. 730 */ 731 template<typename _InputIterator, typename _OutputIterator, 732 typename _Predicate> 733 inline _OutputIterator 734 remove_copy_if(_InputIterator __first, _InputIterator __last, 735 _OutputIterator __result, _Predicate __pred) 736 { 737 // concept requirements 738 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 739 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 740 typename iterator_traits<_InputIterator>::value_type>) 741 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 742 typename iterator_traits<_InputIterator>::value_type>) 743 __glibcxx_requires_valid_range(__first, __last); 744 745 return std::__remove_copy_if(__first, __last, __result, 746 __gnu_cxx::__ops::__pred_iter(__pred)); 747 } 748 749 #if __cplusplus >= 201103L 750 /** 751 * @brief Copy the elements of a sequence for which a predicate is true. 752 * @ingroup mutating_algorithms 753 * @param __first An input iterator. 754 * @param __last An input iterator. 755 * @param __result An output iterator. 756 * @param __pred A predicate. 757 * @return An iterator designating the end of the resulting sequence. 758 * 759 * Copies each element in the range @p [__first,__last) for which 760 * @p __pred returns true to the range beginning at @p __result. 761 * 762 * copy_if() is stable, so the relative order of elements that are 763 * copied is unchanged. 764 */ 765 template<typename _InputIterator, typename _OutputIterator, 766 typename _Predicate> 767 _OutputIterator 768 copy_if(_InputIterator __first, _InputIterator __last, 769 _OutputIterator __result, _Predicate __pred) 770 { 771 // concept requirements 772 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 773 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 774 typename iterator_traits<_InputIterator>::value_type>) 775 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 776 typename iterator_traits<_InputIterator>::value_type>) 777 __glibcxx_requires_valid_range(__first, __last); 778 779 for (; __first != __last; ++__first) 780 if (__pred(*__first)) 781 { 782 *__result = *__first; 783 ++__result; 784 } 785 return __result; 786 } 787 788 template<typename _InputIterator, typename _Size, typename _OutputIterator> 789 _OutputIterator 790 __copy_n(_InputIterator __first, _Size __n, 791 _OutputIterator __result, input_iterator_tag) 792 { 793 if (__n > 0) 794 { 795 while (true) 796 { 797 *__result = *__first; 798 ++__result; 799 if (--__n > 0) 800 ++__first; 801 else 802 break; 803 } 804 } 805 return __result; 806 } 807 808 template<typename _RandomAccessIterator, typename _Size, 809 typename _OutputIterator> 810 inline _OutputIterator 811 __copy_n(_RandomAccessIterator __first, _Size __n, 812 _OutputIterator __result, random_access_iterator_tag) 813 { return std::copy(__first, __first + __n, __result); } 814 815 /** 816 * @brief Copies the range [first,first+n) into [result,result+n). 817 * @ingroup mutating_algorithms 818 * @param __first An input iterator. 819 * @param __n The number of elements to copy. 820 * @param __result An output iterator. 821 * @return result+n. 822 * 823 * This inline function will boil down to a call to @c memmove whenever 824 * possible. Failing that, if random access iterators are passed, then the 825 * loop count will be known (and therefore a candidate for compiler 826 * optimizations such as unrolling). 827 */ 828 template<typename _InputIterator, typename _Size, typename _OutputIterator> 829 inline _OutputIterator 830 copy_n(_InputIterator __first, _Size __n, _OutputIterator __result) 831 { 832 // concept requirements 833 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 834 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 835 typename iterator_traits<_InputIterator>::value_type>) 836 837 return std::__copy_n(__first, __n, __result, 838 std::__iterator_category(__first)); 839 } 840 841 /** 842 * @brief Copy the elements of a sequence to separate output sequences 843 * depending on the truth value of a predicate. 844 * @ingroup mutating_algorithms 845 * @param __first An input iterator. 846 * @param __last An input iterator. 847 * @param __out_true An output iterator. 848 * @param __out_false An output iterator. 849 * @param __pred A predicate. 850 * @return A pair designating the ends of the resulting sequences. 851 * 852 * Copies each element in the range @p [__first,__last) for which 853 * @p __pred returns true to the range beginning at @p out_true 854 * and each element for which @p __pred returns false to @p __out_false. 855 */ 856 template<typename _InputIterator, typename _OutputIterator1, 857 typename _OutputIterator2, typename _Predicate> 858 pair<_OutputIterator1, _OutputIterator2> 859 partition_copy(_InputIterator __first, _InputIterator __last, 860 _OutputIterator1 __out_true, _OutputIterator2 __out_false, 861 _Predicate __pred) 862 { 863 // concept requirements 864 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 865 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator1, 866 typename iterator_traits<_InputIterator>::value_type>) 867 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator2, 868 typename iterator_traits<_InputIterator>::value_type>) 869 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 870 typename iterator_traits<_InputIterator>::value_type>) 871 __glibcxx_requires_valid_range(__first, __last); 872 873 for (; __first != __last; ++__first) 874 if (__pred(*__first)) 875 { 876 *__out_true = *__first; 877 ++__out_true; 878 } 879 else 880 { 881 *__out_false = *__first; 882 ++__out_false; 883 } 884 885 return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false); 886 } 887 #endif 888 889 template<typename _ForwardIterator, typename _Predicate> 890 _ForwardIterator 891 __remove_if(_ForwardIterator __first, _ForwardIterator __last, 892 _Predicate __pred) 893 { 894 __first = std::__find_if(__first, __last, __pred); 895 if (__first == __last) 896 return __first; 897 _ForwardIterator __result = __first; 898 ++__first; 899 for (; __first != __last; ++__first) 900 if (!__pred(__first)) 901 { 902 *__result = _GLIBCXX_MOVE(*__first); 903 ++__result; 904 } 905 return __result; 906 } 907 908 /** 909 * @brief Remove elements from a sequence. 910 * @ingroup mutating_algorithms 911 * @param __first An input iterator. 912 * @param __last An input iterator. 913 * @param __value The value to be removed. 914 * @return An iterator designating the end of the resulting sequence. 915 * 916 * All elements equal to @p __value are removed from the range 917 * @p [__first,__last). 918 * 919 * remove() is stable, so the relative order of elements that are 920 * not removed is unchanged. 921 * 922 * Elements between the end of the resulting sequence and @p __last 923 * are still present, but their value is unspecified. 924 */ 925 template<typename _ForwardIterator, typename _Tp> 926 inline _ForwardIterator 927 remove(_ForwardIterator __first, _ForwardIterator __last, 928 const _Tp& __value) 929 { 930 // concept requirements 931 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 932 _ForwardIterator>) 933 __glibcxx_function_requires(_EqualOpConcept< 934 typename iterator_traits<_ForwardIterator>::value_type, _Tp>) 935 __glibcxx_requires_valid_range(__first, __last); 936 937 return std::__remove_if(__first, __last, 938 __gnu_cxx::__ops::__iter_equals_val(__value)); 939 } 940 941 /** 942 * @brief Remove elements from a sequence using a predicate. 943 * @ingroup mutating_algorithms 944 * @param __first A forward iterator. 945 * @param __last A forward iterator. 946 * @param __pred A predicate. 947 * @return An iterator designating the end of the resulting sequence. 948 * 949 * All elements for which @p __pred returns true are removed from the range 950 * @p [__first,__last). 951 * 952 * remove_if() is stable, so the relative order of elements that are 953 * not removed is unchanged. 954 * 955 * Elements between the end of the resulting sequence and @p __last 956 * are still present, but their value is unspecified. 957 */ 958 template<typename _ForwardIterator, typename _Predicate> 959 inline _ForwardIterator 960 remove_if(_ForwardIterator __first, _ForwardIterator __last, 961 _Predicate __pred) 962 { 963 // concept requirements 964 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 965 _ForwardIterator>) 966 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 967 typename iterator_traits<_ForwardIterator>::value_type>) 968 __glibcxx_requires_valid_range(__first, __last); 969 970 return std::__remove_if(__first, __last, 971 __gnu_cxx::__ops::__pred_iter(__pred)); 972 } 973 974 template<typename _ForwardIterator, typename _BinaryPredicate> 975 _ForwardIterator 976 __adjacent_find(_ForwardIterator __first, _ForwardIterator __last, 977 _BinaryPredicate __binary_pred) 978 { 979 if (__first == __last) 980 return __last; 981 _ForwardIterator __next = __first; 982 while (++__next != __last) 983 { 984 if (__binary_pred(__first, __next)) 985 return __first; 986 __first = __next; 987 } 988 return __last; 989 } 990 991 template<typename _ForwardIterator, typename _BinaryPredicate> 992 _ForwardIterator 993 __unique(_ForwardIterator __first, _ForwardIterator __last, 994 _BinaryPredicate __binary_pred) 995 { 996 // Skip the beginning, if already unique. 997 __first = std::__adjacent_find(__first, __last, __binary_pred); 998 if (__first == __last) 999 return __last; 1000 1001 // Do the real copy work. 1002 _ForwardIterator __dest = __first; 1003 ++__first; 1004 while (++__first != __last) 1005 if (!__binary_pred(__dest, __first)) 1006 *++__dest = _GLIBCXX_MOVE(*__first); 1007 return ++__dest; 1008 } 1009 1010 /** 1011 * @brief Remove consecutive duplicate values from a sequence. 1012 * @ingroup mutating_algorithms 1013 * @param __first A forward iterator. 1014 * @param __last A forward iterator. 1015 * @return An iterator designating the end of the resulting sequence. 1016 * 1017 * Removes all but the first element from each group of consecutive 1018 * values that compare equal. 1019 * unique() is stable, so the relative order of elements that are 1020 * not removed is unchanged. 1021 * Elements between the end of the resulting sequence and @p __last 1022 * are still present, but their value is unspecified. 1023 */ 1024 template<typename _ForwardIterator> 1025 inline _ForwardIterator 1026 unique(_ForwardIterator __first, _ForwardIterator __last) 1027 { 1028 // concept requirements 1029 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 1030 _ForwardIterator>) 1031 __glibcxx_function_requires(_EqualityComparableConcept< 1032 typename iterator_traits<_ForwardIterator>::value_type>) 1033 __glibcxx_requires_valid_range(__first, __last); 1034 1035 return std::__unique(__first, __last, 1036 __gnu_cxx::__ops::__iter_equal_to_iter()); 1037 } 1038 1039 /** 1040 * @brief Remove consecutive values from a sequence using a predicate. 1041 * @ingroup mutating_algorithms 1042 * @param __first A forward iterator. 1043 * @param __last A forward iterator. 1044 * @param __binary_pred A binary predicate. 1045 * @return An iterator designating the end of the resulting sequence. 1046 * 1047 * Removes all but the first element from each group of consecutive 1048 * values for which @p __binary_pred returns true. 1049 * unique() is stable, so the relative order of elements that are 1050 * not removed is unchanged. 1051 * Elements between the end of the resulting sequence and @p __last 1052 * are still present, but their value is unspecified. 1053 */ 1054 template<typename _ForwardIterator, typename _BinaryPredicate> 1055 inline _ForwardIterator 1056 unique(_ForwardIterator __first, _ForwardIterator __last, 1057 _BinaryPredicate __binary_pred) 1058 { 1059 // concept requirements 1060 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 1061 _ForwardIterator>) 1062 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, 1063 typename iterator_traits<_ForwardIterator>::value_type, 1064 typename iterator_traits<_ForwardIterator>::value_type>) 1065 __glibcxx_requires_valid_range(__first, __last); 1066 1067 return std::__unique(__first, __last, 1068 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred)); 1069 } 1070 1071 /** 1072 * This is an uglified 1073 * unique_copy(_InputIterator, _InputIterator, _OutputIterator, 1074 * _BinaryPredicate) 1075 * overloaded for forward iterators and output iterator as result. 1076 */ 1077 template<typename _ForwardIterator, typename _OutputIterator, 1078 typename _BinaryPredicate> 1079 _OutputIterator 1080 __unique_copy(_ForwardIterator __first, _ForwardIterator __last, 1081 _OutputIterator __result, _BinaryPredicate __binary_pred, 1082 forward_iterator_tag, output_iterator_tag) 1083 { 1084 // concept requirements -- iterators already checked 1085 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, 1086 typename iterator_traits<_ForwardIterator>::value_type, 1087 typename iterator_traits<_ForwardIterator>::value_type>) 1088 1089 _ForwardIterator __next = __first; 1090 *__result = *__first; 1091 while (++__next != __last) 1092 if (!__binary_pred(__first, __next)) 1093 { 1094 __first = __next; 1095 *++__result = *__first; 1096 } 1097 return ++__result; 1098 } 1099 1100 /** 1101 * This is an uglified 1102 * unique_copy(_InputIterator, _InputIterator, _OutputIterator, 1103 * _BinaryPredicate) 1104 * overloaded for input iterators and output iterator as result. 1105 */ 1106 template<typename _InputIterator, typename _OutputIterator, 1107 typename _BinaryPredicate> 1108 _OutputIterator 1109 __unique_copy(_InputIterator __first, _InputIterator __last, 1110 _OutputIterator __result, _BinaryPredicate __binary_pred, 1111 input_iterator_tag, output_iterator_tag) 1112 { 1113 // concept requirements -- iterators already checked 1114 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, 1115 typename iterator_traits<_InputIterator>::value_type, 1116 typename iterator_traits<_InputIterator>::value_type>) 1117 1118 typename iterator_traits<_InputIterator>::value_type __value = *__first; 1119 __decltype(__gnu_cxx::__ops::__iter_comp_val(__binary_pred)) 1120 __rebound_pred 1121 = __gnu_cxx::__ops::__iter_comp_val(__binary_pred); 1122 *__result = __value; 1123 while (++__first != __last) 1124 if (!__rebound_pred(__first, __value)) 1125 { 1126 __value = *__first; 1127 *++__result = __value; 1128 } 1129 return ++__result; 1130 } 1131 1132 /** 1133 * This is an uglified 1134 * unique_copy(_InputIterator, _InputIterator, _OutputIterator, 1135 * _BinaryPredicate) 1136 * overloaded for input iterators and forward iterator as result. 1137 */ 1138 template<typename _InputIterator, typename _ForwardIterator, 1139 typename _BinaryPredicate> 1140 _ForwardIterator 1141 __unique_copy(_InputIterator __first, _InputIterator __last, 1142 _ForwardIterator __result, _BinaryPredicate __binary_pred, 1143 input_iterator_tag, forward_iterator_tag) 1144 { 1145 // concept requirements -- iterators already checked 1146 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, 1147 typename iterator_traits<_ForwardIterator>::value_type, 1148 typename iterator_traits<_InputIterator>::value_type>) 1149 *__result = *__first; 1150 while (++__first != __last) 1151 if (!__binary_pred(__result, __first)) 1152 *++__result = *__first; 1153 return ++__result; 1154 } 1155 1156 /** 1157 * This is an uglified reverse(_BidirectionalIterator, 1158 * _BidirectionalIterator) 1159 * overloaded for bidirectional iterators. 1160 */ 1161 template<typename _BidirectionalIterator> 1162 void 1163 __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, 1164 bidirectional_iterator_tag) 1165 { 1166 while (true) 1167 if (__first == __last || __first == --__last) 1168 return; 1169 else 1170 { 1171 std::iter_swap(__first, __last); 1172 ++__first; 1173 } 1174 } 1175 1176 /** 1177 * This is an uglified reverse(_BidirectionalIterator, 1178 * _BidirectionalIterator) 1179 * overloaded for random access iterators. 1180 */ 1181 template<typename _RandomAccessIterator> 1182 void 1183 __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, 1184 random_access_iterator_tag) 1185 { 1186 if (__first == __last) 1187 return; 1188 --__last; 1189 while (__first < __last) 1190 { 1191 std::iter_swap(__first, __last); 1192 ++__first; 1193 --__last; 1194 } 1195 } 1196 1197 /** 1198 * @brief Reverse a sequence. 1199 * @ingroup mutating_algorithms 1200 * @param __first A bidirectional iterator. 1201 * @param __last A bidirectional iterator. 1202 * @return reverse() returns no value. 1203 * 1204 * Reverses the order of the elements in the range @p [__first,__last), 1205 * so that the first element becomes the last etc. 1206 * For every @c i such that @p 0<=i<=(__last-__first)/2), @p reverse() 1207 * swaps @p *(__first+i) and @p *(__last-(i+1)) 1208 */ 1209 template<typename _BidirectionalIterator> 1210 inline void 1211 reverse(_BidirectionalIterator __first, _BidirectionalIterator __last) 1212 { 1213 // concept requirements 1214 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< 1215 _BidirectionalIterator>) 1216 __glibcxx_requires_valid_range(__first, __last); 1217 std::__reverse(__first, __last, std::__iterator_category(__first)); 1218 } 1219 1220 /** 1221 * @brief Copy a sequence, reversing its elements. 1222 * @ingroup mutating_algorithms 1223 * @param __first A bidirectional iterator. 1224 * @param __last A bidirectional iterator. 1225 * @param __result An output iterator. 1226 * @return An iterator designating the end of the resulting sequence. 1227 * 1228 * Copies the elements in the range @p [__first,__last) to the 1229 * range @p [__result,__result+(__last-__first)) such that the 1230 * order of the elements is reversed. For every @c i such that @p 1231 * 0<=i<=(__last-__first), @p reverse_copy() performs the 1232 * assignment @p *(__result+(__last-__first)-1-i) = *(__first+i). 1233 * The ranges @p [__first,__last) and @p 1234 * [__result,__result+(__last-__first)) must not overlap. 1235 */ 1236 template<typename _BidirectionalIterator, typename _OutputIterator> 1237 _OutputIterator 1238 reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, 1239 _OutputIterator __result) 1240 { 1241 // concept requirements 1242 __glibcxx_function_requires(_BidirectionalIteratorConcept< 1243 _BidirectionalIterator>) 1244 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 1245 typename iterator_traits<_BidirectionalIterator>::value_type>) 1246 __glibcxx_requires_valid_range(__first, __last); 1247 1248 while (__first != __last) 1249 { 1250 --__last; 1251 *__result = *__last; 1252 ++__result; 1253 } 1254 return __result; 1255 } 1256 1257 /** 1258 * This is a helper function for the rotate algorithm specialized on RAIs. 1259 * It returns the greatest common divisor of two integer values. 1260 */ 1261 template<typename _EuclideanRingElement> 1262 _EuclideanRingElement 1263 __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n) 1264 { 1265 while (__n != 0) 1266 { 1267 _EuclideanRingElement __t = __m % __n; 1268 __m = __n; 1269 __n = __t; 1270 } 1271 return __m; 1272 } 1273 1274 /// This is a helper function for the rotate algorithm. 1275 template<typename _ForwardIterator> 1276 void 1277 __rotate(_ForwardIterator __first, 1278 _ForwardIterator __middle, 1279 _ForwardIterator __last, 1280 forward_iterator_tag) 1281 { 1282 if (__first == __middle || __last == __middle) 1283 return; 1284 1285 _ForwardIterator __first2 = __middle; 1286 do 1287 { 1288 std::iter_swap(__first, __first2); 1289 ++__first; 1290 ++__first2; 1291 if (__first == __middle) 1292 __middle = __first2; 1293 } 1294 while (__first2 != __last); 1295 1296 __first2 = __middle; 1297 1298 while (__first2 != __last) 1299 { 1300 std::iter_swap(__first, __first2); 1301 ++__first; 1302 ++__first2; 1303 if (__first == __middle) 1304 __middle = __first2; 1305 else if (__first2 == __last) 1306 __first2 = __middle; 1307 } 1308 } 1309 1310 /// This is a helper function for the rotate algorithm. 1311 template<typename _BidirectionalIterator> 1312 void 1313 __rotate(_BidirectionalIterator __first, 1314 _BidirectionalIterator __middle, 1315 _BidirectionalIterator __last, 1316 bidirectional_iterator_tag) 1317 { 1318 // concept requirements 1319 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< 1320 _BidirectionalIterator>) 1321 1322 if (__first == __middle || __last == __middle) 1323 return; 1324 1325 std::__reverse(__first, __middle, bidirectional_iterator_tag()); 1326 std::__reverse(__middle, __last, bidirectional_iterator_tag()); 1327 1328 while (__first != __middle && __middle != __last) 1329 { 1330 std::iter_swap(__first, --__last); 1331 ++__first; 1332 } 1333 1334 if (__first == __middle) 1335 std::__reverse(__middle, __last, bidirectional_iterator_tag()); 1336 else 1337 std::__reverse(__first, __middle, bidirectional_iterator_tag()); 1338 } 1339 1340 /// This is a helper function for the rotate algorithm. 1341 template<typename _RandomAccessIterator> 1342 void 1343 __rotate(_RandomAccessIterator __first, 1344 _RandomAccessIterator __middle, 1345 _RandomAccessIterator __last, 1346 random_access_iterator_tag) 1347 { 1348 // concept requirements 1349 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 1350 _RandomAccessIterator>) 1351 1352 if (__first == __middle || __last == __middle) 1353 return; 1354 1355 typedef typename iterator_traits<_RandomAccessIterator>::difference_type 1356 _Distance; 1357 typedef typename iterator_traits<_RandomAccessIterator>::value_type 1358 _ValueType; 1359 1360 _Distance __n = __last - __first; 1361 _Distance __k = __middle - __first; 1362 1363 if (__k == __n - __k) 1364 { 1365 std::swap_ranges(__first, __middle, __middle); 1366 return; 1367 } 1368 1369 _RandomAccessIterator __p = __first; 1370 1371 for (;;) 1372 { 1373 if (__k < __n - __k) 1374 { 1375 if (__is_pod(_ValueType) && __k == 1) 1376 { 1377 _ValueType __t = _GLIBCXX_MOVE(*__p); 1378 _GLIBCXX_MOVE3(__p + 1, __p + __n, __p); 1379 *(__p + __n - 1) = _GLIBCXX_MOVE(__t); 1380 return; 1381 } 1382 _RandomAccessIterator __q = __p + __k; 1383 for (_Distance __i = 0; __i < __n - __k; ++ __i) 1384 { 1385 std::iter_swap(__p, __q); 1386 ++__p; 1387 ++__q; 1388 } 1389 __n %= __k; 1390 if (__n == 0) 1391 return; 1392 std::swap(__n, __k); 1393 __k = __n - __k; 1394 } 1395 else 1396 { 1397 __k = __n - __k; 1398 if (__is_pod(_ValueType) && __k == 1) 1399 { 1400 _ValueType __t = _GLIBCXX_MOVE(*(__p + __n - 1)); 1401 _GLIBCXX_MOVE_BACKWARD3(__p, __p + __n - 1, __p + __n); 1402 *__p = _GLIBCXX_MOVE(__t); 1403 return; 1404 } 1405 _RandomAccessIterator __q = __p + __n; 1406 __p = __q - __k; 1407 for (_Distance __i = 0; __i < __n - __k; ++ __i) 1408 { 1409 --__p; 1410 --__q; 1411 std::iter_swap(__p, __q); 1412 } 1413 __n %= __k; 1414 if (__n == 0) 1415 return; 1416 std::swap(__n, __k); 1417 } 1418 } 1419 } 1420 1421 /** 1422 * @brief Rotate the elements of a sequence. 1423 * @ingroup mutating_algorithms 1424 * @param __first A forward iterator. 1425 * @param __middle A forward iterator. 1426 * @param __last A forward iterator. 1427 * @return Nothing. 1428 * 1429 * Rotates the elements of the range @p [__first,__last) by 1430 * @p (__middle - __first) positions so that the element at @p __middle 1431 * is moved to @p __first, the element at @p __middle+1 is moved to 1432 * @p __first+1 and so on for each element in the range 1433 * @p [__first,__last). 1434 * 1435 * This effectively swaps the ranges @p [__first,__middle) and 1436 * @p [__middle,__last). 1437 * 1438 * Performs 1439 * @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n) 1440 * for each @p n in the range @p [0,__last-__first). 1441 */ 1442 template<typename _ForwardIterator> 1443 inline void 1444 rotate(_ForwardIterator __first, _ForwardIterator __middle, 1445 _ForwardIterator __last) 1446 { 1447 // concept requirements 1448 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 1449 _ForwardIterator>) 1450 __glibcxx_requires_valid_range(__first, __middle); 1451 __glibcxx_requires_valid_range(__middle, __last); 1452 1453 std::__rotate(__first, __middle, __last, 1454 std::__iterator_category(__first)); 1455 } 1456 1457 /** 1458 * @brief Copy a sequence, rotating its elements. 1459 * @ingroup mutating_algorithms 1460 * @param __first A forward iterator. 1461 * @param __middle A forward iterator. 1462 * @param __last A forward iterator. 1463 * @param __result An output iterator. 1464 * @return An iterator designating the end of the resulting sequence. 1465 * 1466 * Copies the elements of the range @p [__first,__last) to the 1467 * range beginning at @result, rotating the copied elements by 1468 * @p (__middle-__first) positions so that the element at @p __middle 1469 * is moved to @p __result, the element at @p __middle+1 is moved 1470 * to @p __result+1 and so on for each element in the range @p 1471 * [__first,__last). 1472 * 1473 * Performs 1474 * @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n) 1475 * for each @p n in the range @p [0,__last-__first). 1476 */ 1477 template<typename _ForwardIterator, typename _OutputIterator> 1478 inline _OutputIterator 1479 rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, 1480 _ForwardIterator __last, _OutputIterator __result) 1481 { 1482 // concept requirements 1483 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 1484 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 1485 typename iterator_traits<_ForwardIterator>::value_type>) 1486 __glibcxx_requires_valid_range(__first, __middle); 1487 __glibcxx_requires_valid_range(__middle, __last); 1488 1489 return std::copy(__first, __middle, 1490 std::copy(__middle, __last, __result)); 1491 } 1492 1493 /// This is a helper function... 1494 template<typename _ForwardIterator, typename _Predicate> 1495 _ForwardIterator 1496 __partition(_ForwardIterator __first, _ForwardIterator __last, 1497 _Predicate __pred, forward_iterator_tag) 1498 { 1499 if (__first == __last) 1500 return __first; 1501 1502 while (__pred(*__first)) 1503 if (++__first == __last) 1504 return __first; 1505 1506 _ForwardIterator __next = __first; 1507 1508 while (++__next != __last) 1509 if (__pred(*__next)) 1510 { 1511 std::iter_swap(__first, __next); 1512 ++__first; 1513 } 1514 1515 return __first; 1516 } 1517 1518 /// This is a helper function... 1519 template<typename _BidirectionalIterator, typename _Predicate> 1520 _BidirectionalIterator 1521 __partition(_BidirectionalIterator __first, _BidirectionalIterator __last, 1522 _Predicate __pred, bidirectional_iterator_tag) 1523 { 1524 while (true) 1525 { 1526 while (true) 1527 if (__first == __last) 1528 return __first; 1529 else if (__pred(*__first)) 1530 ++__first; 1531 else 1532 break; 1533 --__last; 1534 while (true) 1535 if (__first == __last) 1536 return __first; 1537 else if (!bool(__pred(*__last))) 1538 --__last; 1539 else 1540 break; 1541 std::iter_swap(__first, __last); 1542 ++__first; 1543 } 1544 } 1545 1546 // partition 1547 1548 /// This is a helper function... 1549 /// Requires __len != 0 and !__pred(*__first), 1550 /// same as __stable_partition_adaptive. 1551 template<typename _ForwardIterator, typename _Predicate, typename _Distance> 1552 _ForwardIterator 1553 __inplace_stable_partition(_ForwardIterator __first, 1554 _Predicate __pred, _Distance __len) 1555 { 1556 if (__len == 1) 1557 return __first; 1558 _ForwardIterator __middle = __first; 1559 std::advance(__middle, __len / 2); 1560 _ForwardIterator __left_split = 1561 std::__inplace_stable_partition(__first, __pred, __len / 2); 1562 // Advance past true-predicate values to satisfy this 1563 // function's preconditions. 1564 _Distance __right_len = __len - __len / 2; 1565 _ForwardIterator __right_split = 1566 std::__find_if_not_n(__middle, __right_len, __pred); 1567 if (__right_len) 1568 __right_split = std::__inplace_stable_partition(__middle, 1569 __pred, 1570 __right_len); 1571 std::rotate(__left_split, __middle, __right_split); 1572 std::advance(__left_split, std::distance(__middle, __right_split)); 1573 return __left_split; 1574 } 1575 1576 /// This is a helper function... 1577 /// Requires __first != __last and !__pred(__first) 1578 /// and __len == distance(__first, __last). 1579 /// 1580 /// !__pred(__first) allows us to guarantee that we don't 1581 /// move-assign an element onto itself. 1582 template<typename _ForwardIterator, typename _Pointer, typename _Predicate, 1583 typename _Distance> 1584 _ForwardIterator 1585 __stable_partition_adaptive(_ForwardIterator __first, 1586 _ForwardIterator __last, 1587 _Predicate __pred, _Distance __len, 1588 _Pointer __buffer, 1589 _Distance __buffer_size) 1590 { 1591 if (__len <= __buffer_size) 1592 { 1593 _ForwardIterator __result1 = __first; 1594 _Pointer __result2 = __buffer; 1595 // The precondition guarantees that !__pred(__first), so 1596 // move that element to the buffer before starting the loop. 1597 // This ensures that we only call __pred once per element. 1598 *__result2 = _GLIBCXX_MOVE(*__first); 1599 ++__result2; 1600 ++__first; 1601 for (; __first != __last; ++__first) 1602 if (__pred(__first)) 1603 { 1604 *__result1 = _GLIBCXX_MOVE(*__first); 1605 ++__result1; 1606 } 1607 else 1608 { 1609 *__result2 = _GLIBCXX_MOVE(*__first); 1610 ++__result2; 1611 } 1612 _GLIBCXX_MOVE3(__buffer, __result2, __result1); 1613 return __result1; 1614 } 1615 else 1616 { 1617 _ForwardIterator __middle = __first; 1618 std::advance(__middle, __len / 2); 1619 _ForwardIterator __left_split = 1620 std::__stable_partition_adaptive(__first, __middle, __pred, 1621 __len / 2, __buffer, 1622 __buffer_size); 1623 // Advance past true-predicate values to satisfy this 1624 // function's preconditions. 1625 _Distance __right_len = __len - __len / 2; 1626 _ForwardIterator __right_split = 1627 std::__find_if_not_n(__middle, __right_len, __pred); 1628 if (__right_len) 1629 __right_split = 1630 std::__stable_partition_adaptive(__right_split, __last, __pred, 1631 __right_len, 1632 __buffer, __buffer_size); 1633 std::rotate(__left_split, __middle, __right_split); 1634 std::advance(__left_split, std::distance(__middle, __right_split)); 1635 return __left_split; 1636 } 1637 } 1638 1639 template<typename _ForwardIterator, typename _Predicate> 1640 _ForwardIterator 1641 __stable_partition(_ForwardIterator __first, _ForwardIterator __last, 1642 _Predicate __pred) 1643 { 1644 __first = std::__find_if_not(__first, __last, __pred); 1645 1646 if (__first == __last) 1647 return __first; 1648 1649 typedef typename iterator_traits<_ForwardIterator>::value_type 1650 _ValueType; 1651 typedef typename iterator_traits<_ForwardIterator>::difference_type 1652 _DistanceType; 1653 1654 _Temporary_buffer<_ForwardIterator, _ValueType> __buf(__first, __last); 1655 if (__buf.size() > 0) 1656 return 1657 std::__stable_partition_adaptive(__first, __last, __pred, 1658 _DistanceType(__buf.requested_size()), 1659 __buf.begin(), 1660 _DistanceType(__buf.size())); 1661 else 1662 return 1663 std::__inplace_stable_partition(__first, __pred, 1664 _DistanceType(__buf.requested_size())); 1665 } 1666 1667 /** 1668 * @brief Move elements for which a predicate is true to the beginning 1669 * of a sequence, preserving relative ordering. 1670 * @ingroup mutating_algorithms 1671 * @param __first A forward iterator. 1672 * @param __last A forward iterator. 1673 * @param __pred A predicate functor. 1674 * @return An iterator @p middle such that @p __pred(i) is true for each 1675 * iterator @p i in the range @p [first,middle) and false for each @p i 1676 * in the range @p [middle,last). 1677 * 1678 * Performs the same function as @p partition() with the additional 1679 * guarantee that the relative ordering of elements in each group is 1680 * preserved, so any two elements @p x and @p y in the range 1681 * @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same 1682 * relative ordering after calling @p stable_partition(). 1683 */ 1684 template<typename _ForwardIterator, typename _Predicate> 1685 inline _ForwardIterator 1686 stable_partition(_ForwardIterator __first, _ForwardIterator __last, 1687 _Predicate __pred) 1688 { 1689 // concept requirements 1690 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 1691 _ForwardIterator>) 1692 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 1693 typename iterator_traits<_ForwardIterator>::value_type>) 1694 __glibcxx_requires_valid_range(__first, __last); 1695 1696 return std::__stable_partition(__first, __last, 1697 __gnu_cxx::__ops::__pred_iter(__pred)); 1698 } 1699 1700 /// This is a helper function for the sort routines. 1701 template<typename _RandomAccessIterator, typename _Compare> 1702 void 1703 __heap_select(_RandomAccessIterator __first, 1704 _RandomAccessIterator __middle, 1705 _RandomAccessIterator __last, _Compare __comp) 1706 { 1707 std::__make_heap(__first, __middle, __comp); 1708 for (_RandomAccessIterator __i = __middle; __i < __last; ++__i) 1709 if (__comp(__i, __first)) 1710 std::__pop_heap(__first, __middle, __i, __comp); 1711 } 1712 1713 // partial_sort 1714 1715 template<typename _InputIterator, typename _RandomAccessIterator, 1716 typename _Compare> 1717 _RandomAccessIterator 1718 __partial_sort_copy(_InputIterator __first, _InputIterator __last, 1719 _RandomAccessIterator __result_first, 1720 _RandomAccessIterator __result_last, 1721 _Compare __comp) 1722 { 1723 typedef typename iterator_traits<_InputIterator>::value_type 1724 _InputValueType; 1725 typedef iterator_traits<_RandomAccessIterator> _RItTraits; 1726 typedef typename _RItTraits::difference_type _DistanceType; 1727 1728 if (__result_first == __result_last) 1729 return __result_last; 1730 _RandomAccessIterator __result_real_last = __result_first; 1731 while (__first != __last && __result_real_last != __result_last) 1732 { 1733 *__result_real_last = *__first; 1734 ++__result_real_last; 1735 ++__first; 1736 } 1737 1738 std::__make_heap(__result_first, __result_real_last, __comp); 1739 while (__first != __last) 1740 { 1741 if (__comp(__first, __result_first)) 1742 std::__adjust_heap(__result_first, _DistanceType(0), 1743 _DistanceType(__result_real_last 1744 - __result_first), 1745 _InputValueType(*__first), __comp); 1746 ++__first; 1747 } 1748 std::__sort_heap(__result_first, __result_real_last, __comp); 1749 return __result_real_last; 1750 } 1751 1752 /** 1753 * @brief Copy the smallest elements of a sequence. 1754 * @ingroup sorting_algorithms 1755 * @param __first An iterator. 1756 * @param __last Another iterator. 1757 * @param __result_first A random-access iterator. 1758 * @param __result_last Another random-access iterator. 1759 * @return An iterator indicating the end of the resulting sequence. 1760 * 1761 * Copies and sorts the smallest N values from the range @p [__first,__last) 1762 * to the range beginning at @p __result_first, where the number of 1763 * elements to be copied, @p N, is the smaller of @p (__last-__first) and 1764 * @p (__result_last-__result_first). 1765 * After the sort if @e i and @e j are iterators in the range 1766 * @p [__result_first,__result_first+N) such that i precedes j then 1767 * *j<*i is false. 1768 * The value returned is @p __result_first+N. 1769 */ 1770 template<typename _InputIterator, typename _RandomAccessIterator> 1771 inline _RandomAccessIterator 1772 partial_sort_copy(_InputIterator __first, _InputIterator __last, 1773 _RandomAccessIterator __result_first, 1774 _RandomAccessIterator __result_last) 1775 { 1776 typedef typename iterator_traits<_InputIterator>::value_type 1777 _InputValueType; 1778 typedef typename iterator_traits<_RandomAccessIterator>::value_type 1779 _OutputValueType; 1780 typedef typename iterator_traits<_RandomAccessIterator>::difference_type 1781 _DistanceType; 1782 1783 // concept requirements 1784 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 1785 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType, 1786 _OutputValueType>) 1787 __glibcxx_function_requires(_LessThanOpConcept<_InputValueType, 1788 _OutputValueType>) 1789 __glibcxx_function_requires(_LessThanComparableConcept<_OutputValueType>) 1790 __glibcxx_requires_valid_range(__first, __last); 1791 __glibcxx_requires_valid_range(__result_first, __result_last); 1792 1793 return std::__partial_sort_copy(__first, __last, 1794 __result_first, __result_last, 1795 __gnu_cxx::__ops::__iter_less_iter()); 1796 } 1797 1798 /** 1799 * @brief Copy the smallest elements of a sequence using a predicate for 1800 * comparison. 1801 * @ingroup sorting_algorithms 1802 * @param __first An input iterator. 1803 * @param __last Another input iterator. 1804 * @param __result_first A random-access iterator. 1805 * @param __result_last Another random-access iterator. 1806 * @param __comp A comparison functor. 1807 * @return An iterator indicating the end of the resulting sequence. 1808 * 1809 * Copies and sorts the smallest N values from the range @p [__first,__last) 1810 * to the range beginning at @p result_first, where the number of 1811 * elements to be copied, @p N, is the smaller of @p (__last-__first) and 1812 * @p (__result_last-__result_first). 1813 * After the sort if @e i and @e j are iterators in the range 1814 * @p [__result_first,__result_first+N) such that i precedes j then 1815 * @p __comp(*j,*i) is false. 1816 * The value returned is @p __result_first+N. 1817 */ 1818 template<typename _InputIterator, typename _RandomAccessIterator, 1819 typename _Compare> 1820 inline _RandomAccessIterator 1821 partial_sort_copy(_InputIterator __first, _InputIterator __last, 1822 _RandomAccessIterator __result_first, 1823 _RandomAccessIterator __result_last, 1824 _Compare __comp) 1825 { 1826 typedef typename iterator_traits<_InputIterator>::value_type 1827 _InputValueType; 1828 typedef typename iterator_traits<_RandomAccessIterator>::value_type 1829 _OutputValueType; 1830 typedef typename iterator_traits<_RandomAccessIterator>::difference_type 1831 _DistanceType; 1832 1833 // concept requirements 1834 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 1835 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 1836 _RandomAccessIterator>) 1837 __glibcxx_function_requires(_ConvertibleConcept<_InputValueType, 1838 _OutputValueType>) 1839 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 1840 _InputValueType, _OutputValueType>) 1841 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 1842 _OutputValueType, _OutputValueType>) 1843 __glibcxx_requires_valid_range(__first, __last); 1844 __glibcxx_requires_valid_range(__result_first, __result_last); 1845 1846 return std::__partial_sort_copy(__first, __last, 1847 __result_first, __result_last, 1848 __gnu_cxx::__ops::__iter_comp_iter(__CheckedCompare(__comp))); 1849 } 1850 1851 /// This is a helper function for the sort routine. 1852 template<typename _RandomAccessIterator, typename _Compare> 1853 void 1854 __unguarded_linear_insert(_RandomAccessIterator __last, 1855 _Compare __comp) 1856 { 1857 typename iterator_traits<_RandomAccessIterator>::value_type 1858 __val = _GLIBCXX_MOVE(*__last); 1859 _RandomAccessIterator __next = __last; 1860 --__next; 1861 while (__comp(__val, __next)) 1862 { 1863 *__last = _GLIBCXX_MOVE(*__next); 1864 __last = __next; 1865 --__next; 1866 } 1867 *__last = _GLIBCXX_MOVE(__val); 1868 } 1869 1870 /// This is a helper function for the sort routine. 1871 template<typename _RandomAccessIterator, typename _Compare> 1872 void 1873 __insertion_sort(_RandomAccessIterator __first, 1874 _RandomAccessIterator __last, _Compare __comp) 1875 { 1876 if (__first == __last) return; 1877 1878 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) 1879 { 1880 if (__comp(__i, __first)) 1881 { 1882 typename iterator_traits<_RandomAccessIterator>::value_type 1883 __val = _GLIBCXX_MOVE(*__i); 1884 _GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + 1); 1885 *__first = _GLIBCXX_MOVE(__val); 1886 } 1887 else 1888 std::__unguarded_linear_insert(__i, 1889 __gnu_cxx::__ops::__val_comp_iter(__comp)); 1890 } 1891 } 1892 1893 /// This is a helper function for the sort routine. 1894 template<typename _RandomAccessIterator, typename _Compare> 1895 inline void 1896 __unguarded_insertion_sort(_RandomAccessIterator __first, 1897 _RandomAccessIterator __last, _Compare __comp) 1898 { 1899 for (_RandomAccessIterator __i = __first; __i != __last; ++__i) 1900 std::__unguarded_linear_insert(__i, 1901 __gnu_cxx::__ops::__val_comp_iter(__comp)); 1902 } 1903 1904 /** 1905 * @doctodo 1906 * This controls some aspect of the sort routines. 1907 */ 1908 enum { _S_threshold = 16 }; 1909 1910 /// This is a helper function for the sort routine. 1911 template<typename _RandomAccessIterator, typename _Compare> 1912 void 1913 __final_insertion_sort(_RandomAccessIterator __first, 1914 _RandomAccessIterator __last, _Compare __comp) 1915 { 1916 if (__last - __first > int(_S_threshold)) 1917 { 1918 std::__insertion_sort(__first, __first + int(_S_threshold), __comp); 1919 std::__unguarded_insertion_sort(__first + int(_S_threshold), __last, 1920 __comp); 1921 } 1922 else 1923 std::__insertion_sort(__first, __last, __comp); 1924 } 1925 1926 /// This is a helper function... 1927 template<typename _RandomAccessIterator, typename _Compare> 1928 _RandomAccessIterator 1929 __unguarded_partition(_RandomAccessIterator __first, 1930 _RandomAccessIterator __last, 1931 _RandomAccessIterator __pivot, _Compare __comp) 1932 { 1933 while (true) 1934 { 1935 while (__comp(__first, __pivot)) 1936 ++__first; 1937 --__last; 1938 while (__comp(__pivot, __last)) 1939 --__last; 1940 if (!(__first < __last)) 1941 return __first; 1942 std::iter_swap(__first, __last); 1943 ++__first; 1944 } 1945 } 1946 1947 /// This is a helper function... 1948 template<typename _RandomAccessIterator, typename _Compare> 1949 inline _RandomAccessIterator 1950 __unguarded_partition_pivot(_RandomAccessIterator __first, 1951 _RandomAccessIterator __last, _Compare __comp) 1952 { 1953 _RandomAccessIterator __mid = __first + (__last - __first) / 2; 1954 std::__move_median_to_first(__first, __first + 1, __mid, __last - 1, 1955 __comp); 1956 return std::__unguarded_partition(__first + 1, __last, __first, __comp); 1957 } 1958 1959 template<typename _RandomAccessIterator, typename _Compare> 1960 inline void 1961 __partial_sort(_RandomAccessIterator __first, 1962 _RandomAccessIterator __middle, 1963 _RandomAccessIterator __last, 1964 _Compare __comp) 1965 { 1966 std::__heap_select(__first, __middle, __last, __comp); 1967 std::__sort_heap(__first, __middle, __comp); 1968 } 1969 1970 /// This is a helper function for the sort routine. 1971 template<typename _RandomAccessIterator, typename _Size, typename _Compare> 1972 void 1973 __introsort_loop(_RandomAccessIterator __first, 1974 _RandomAccessIterator __last, 1975 _Size __depth_limit, _Compare __comp) 1976 { 1977 while (__last - __first > int(_S_threshold)) 1978 { 1979 if (__depth_limit == 0) 1980 { 1981 std::__partial_sort(__first, __last, __last, __comp); 1982 return; 1983 } 1984 --__depth_limit; 1985 _RandomAccessIterator __cut = 1986 std::__unguarded_partition_pivot(__first, __last, __comp); 1987 std::__introsort_loop(__cut, __last, __depth_limit, __comp); 1988 __last = __cut; 1989 } 1990 } 1991 1992 // sort 1993 1994 template<typename _RandomAccessIterator, typename _Compare> 1995 inline void 1996 __sort(_RandomAccessIterator __first, _RandomAccessIterator __last, 1997 _Compare __comp) 1998 { 1999 if (__first != __last) 2000 { 2001 std::__introsort_loop(__first, __last, 2002 std::__lg(__last - __first) * 2, 2003 __comp); 2004 std::__final_insertion_sort(__first, __last, __comp); 2005 } 2006 } 2007 2008 template<typename _RandomAccessIterator, typename _Size, typename _Compare> 2009 void 2010 __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth, 2011 _RandomAccessIterator __last, _Size __depth_limit, 2012 _Compare __comp) 2013 { 2014 while (__last - __first > 3) 2015 { 2016 if (__depth_limit == 0) 2017 { 2018 std::__heap_select(__first, __nth + 1, __last, __comp); 2019 // Place the nth largest element in its final position. 2020 std::iter_swap(__first, __nth); 2021 return; 2022 } 2023 --__depth_limit; 2024 _RandomAccessIterator __cut = 2025 std::__unguarded_partition_pivot(__first, __last, __comp); 2026 if (__cut <= __nth) 2027 __first = __cut; 2028 else 2029 __last = __cut; 2030 } 2031 std::__insertion_sort(__first, __last, __comp); 2032 } 2033 2034 // nth_element 2035 2036 // lower_bound moved to stl_algobase.h 2037 2038 /** 2039 * @brief Finds the first position in which @p __val could be inserted 2040 * without changing the ordering. 2041 * @ingroup binary_search_algorithms 2042 * @param __first An iterator. 2043 * @param __last Another iterator. 2044 * @param __val The search term. 2045 * @param __comp A functor to use for comparisons. 2046 * @return An iterator pointing to the first element <em>not less 2047 * than</em> @p __val, or end() if every element is less 2048 * than @p __val. 2049 * @ingroup binary_search_algorithms 2050 * 2051 * The comparison function should have the same effects on ordering as 2052 * the function used for the initial sort. 2053 */ 2054 template<typename _ForwardIterator, typename _Tp, typename _Compare> 2055 inline _ForwardIterator 2056 lower_bound(_ForwardIterator __first, _ForwardIterator __last, 2057 const _Tp& __val, _Compare __comp) 2058 { 2059 typedef typename iterator_traits<_ForwardIterator>::value_type 2060 _ValueType; 2061 2062 // concept requirements 2063 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 2064 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 2065 _ValueType, _Tp>) 2066 __glibcxx_requires_partitioned_lower_pred(__first, __last, 2067 __val, __comp); 2068 2069 return std::__lower_bound(__first, __last, __val, 2070 __gnu_cxx::__ops::__iter_comp_val(__CheckedCompare(__comp))); 2071 } 2072 2073 template<typename _ForwardIterator, typename _Tp, typename _Compare> 2074 _ForwardIterator 2075 __upper_bound(_ForwardIterator __first, _ForwardIterator __last, 2076 const _Tp& __val, _Compare __comp) 2077 { 2078 typedef typename iterator_traits<_ForwardIterator>::difference_type 2079 _DistanceType; 2080 2081 _DistanceType __len = std::distance(__first, __last); 2082 2083 while (__len > 0) 2084 { 2085 _DistanceType __half = __len >> 1; 2086 _ForwardIterator __middle = __first; 2087 std::advance(__middle, __half); 2088 if (__comp(__val, __middle)) 2089 __len = __half; 2090 else 2091 { 2092 __first = __middle; 2093 ++__first; 2094 __len = __len - __half - 1; 2095 } 2096 } 2097 return __first; 2098 } 2099 2100 /** 2101 * @brief Finds the last position in which @p __val could be inserted 2102 * without changing the ordering. 2103 * @ingroup binary_search_algorithms 2104 * @param __first An iterator. 2105 * @param __last Another iterator. 2106 * @param __val The search term. 2107 * @return An iterator pointing to the first element greater than @p __val, 2108 * or end() if no elements are greater than @p __val. 2109 * @ingroup binary_search_algorithms 2110 */ 2111 template<typename _ForwardIterator, typename _Tp> 2112 inline _ForwardIterator 2113 upper_bound(_ForwardIterator __first, _ForwardIterator __last, 2114 const _Tp& __val) 2115 { 2116 typedef typename iterator_traits<_ForwardIterator>::value_type 2117 _ValueType; 2118 2119 // concept requirements 2120 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 2121 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>) 2122 __glibcxx_requires_partitioned_upper(__first, __last, __val); 2123 2124 return std::__upper_bound(__first, __last, __val, 2125 __gnu_cxx::__ops::__val_less_iter()); 2126 } 2127 2128 /** 2129 * @brief Finds the last position in which @p __val could be inserted 2130 * without changing the ordering. 2131 * @ingroup binary_search_algorithms 2132 * @param __first An iterator. 2133 * @param __last Another iterator. 2134 * @param __val The search term. 2135 * @param __comp A functor to use for comparisons. 2136 * @return An iterator pointing to the first element greater than @p __val, 2137 * or end() if no elements are greater than @p __val. 2138 * @ingroup binary_search_algorithms 2139 * 2140 * The comparison function should have the same effects on ordering as 2141 * the function used for the initial sort. 2142 */ 2143 template<typename _ForwardIterator, typename _Tp, typename _Compare> 2144 inline _ForwardIterator 2145 upper_bound(_ForwardIterator __first, _ForwardIterator __last, 2146 const _Tp& __val, _Compare __comp) 2147 { 2148 typedef typename iterator_traits<_ForwardIterator>::value_type 2149 _ValueType; 2150 2151 // concept requirements 2152 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 2153 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 2154 _Tp, _ValueType>) 2155 __glibcxx_requires_partitioned_upper_pred(__first, __last, 2156 __val, __comp); 2157 2158 return std::__upper_bound(__first, __last, __val, 2159 __gnu_cxx::__ops::__val_comp_iter(__CheckedCompare(__comp))); 2160 } 2161 2162 template<typename _ForwardIterator, typename _Tp, 2163 typename _CompareItTp, typename _CompareTpIt> 2164 pair<_ForwardIterator, _ForwardIterator> 2165 __equal_range(_ForwardIterator __first, _ForwardIterator __last, 2166 const _Tp& __val, 2167 _CompareItTp __comp_it_val, _CompareTpIt __comp_val_it) 2168 { 2169 typedef typename iterator_traits<_ForwardIterator>::difference_type 2170 _DistanceType; 2171 2172 _DistanceType __len = std::distance(__first, __last); 2173 2174 while (__len > 0) 2175 { 2176 _DistanceType __half = __len >> 1; 2177 _ForwardIterator __middle = __first; 2178 std::advance(__middle, __half); 2179 if (__comp_it_val(__middle, __val)) 2180 { 2181 __first = __middle; 2182 ++__first; 2183 __len = __len - __half - 1; 2184 } 2185 else if (__comp_val_it(__val, __middle)) 2186 __len = __half; 2187 else 2188 { 2189 _ForwardIterator __left 2190 = std::__lower_bound(__first, __middle, __val, __comp_it_val); 2191 std::advance(__first, __len); 2192 _ForwardIterator __right 2193 = std::__upper_bound(++__middle, __first, __val, __comp_val_it); 2194 return pair<_ForwardIterator, _ForwardIterator>(__left, __right); 2195 } 2196 } 2197 return pair<_ForwardIterator, _ForwardIterator>(__first, __first); 2198 } 2199 2200 /** 2201 * @brief Finds the largest subrange in which @p __val could be inserted 2202 * at any place in it without changing the ordering. 2203 * @ingroup binary_search_algorithms 2204 * @param __first An iterator. 2205 * @param __last Another iterator. 2206 * @param __val The search term. 2207 * @return An pair of iterators defining the subrange. 2208 * @ingroup binary_search_algorithms 2209 * 2210 * This is equivalent to 2211 * @code 2212 * std::make_pair(lower_bound(__first, __last, __val), 2213 * upper_bound(__first, __last, __val)) 2214 * @endcode 2215 * but does not actually call those functions. 2216 */ 2217 template<typename _ForwardIterator, typename _Tp> 2218 inline pair<_ForwardIterator, _ForwardIterator> 2219 equal_range(_ForwardIterator __first, _ForwardIterator __last, 2220 const _Tp& __val) 2221 { 2222 typedef typename iterator_traits<_ForwardIterator>::value_type 2223 _ValueType; 2224 2225 // concept requirements 2226 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 2227 __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>) 2228 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>) 2229 __glibcxx_requires_partitioned_lower(__first, __last, __val); 2230 __glibcxx_requires_partitioned_upper(__first, __last, __val); 2231 2232 return std::__equal_range(__first, __last, __val, 2233 __gnu_cxx::__ops::__iter_less_val(), 2234 __gnu_cxx::__ops::__val_less_iter()); 2235 } 2236 2237 /** 2238 * @brief Finds the largest subrange in which @p __val could be inserted 2239 * at any place in it without changing the ordering. 2240 * @param __first An iterator. 2241 * @param __last Another iterator. 2242 * @param __val The search term. 2243 * @param __comp A functor to use for comparisons. 2244 * @return An pair of iterators defining the subrange. 2245 * @ingroup binary_search_algorithms 2246 * 2247 * This is equivalent to 2248 * @code 2249 * std::make_pair(lower_bound(__first, __last, __val, __comp), 2250 * upper_bound(__first, __last, __val, __comp)) 2251 * @endcode 2252 * but does not actually call those functions. 2253 */ 2254 template<typename _ForwardIterator, typename _Tp, typename _Compare> 2255 inline pair<_ForwardIterator, _ForwardIterator> 2256 equal_range(_ForwardIterator __first, _ForwardIterator __last, 2257 const _Tp& __val, _Compare __comp) 2258 { 2259 typedef typename iterator_traits<_ForwardIterator>::value_type 2260 _ValueType; 2261 2262 // concept requirements 2263 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 2264 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 2265 _ValueType, _Tp>) 2266 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 2267 _Tp, _ValueType>) 2268 __glibcxx_requires_partitioned_lower_pred(__first, __last, 2269 __val, __comp); 2270 __glibcxx_requires_partitioned_upper_pred(__first, __last, 2271 __val, __comp); 2272 2273 return std::__equal_range(__first, __last, __val, 2274 __gnu_cxx::__ops::__iter_comp_val(__CheckedCompare(__comp)), 2275 __gnu_cxx::__ops::__val_comp_iter(__CheckedCompare(__comp))); 2276 } 2277 2278 /** 2279 * @brief Determines whether an element exists in a range. 2280 * @ingroup binary_search_algorithms 2281 * @param __first An iterator. 2282 * @param __last Another iterator. 2283 * @param __val The search term. 2284 * @return True if @p __val (or its equivalent) is in [@p 2285 * __first,@p __last ]. 2286 * 2287 * Note that this does not actually return an iterator to @p __val. For 2288 * that, use std::find or a container's specialized find member functions. 2289 */ 2290 template<typename _ForwardIterator, typename _Tp> 2291 bool 2292 binary_search(_ForwardIterator __first, _ForwardIterator __last, 2293 const _Tp& __val) 2294 { 2295 typedef typename iterator_traits<_ForwardIterator>::value_type 2296 _ValueType; 2297 2298 // concept requirements 2299 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 2300 __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>) 2301 __glibcxx_requires_partitioned_lower(__first, __last, __val); 2302 __glibcxx_requires_partitioned_upper(__first, __last, __val); 2303 2304 _ForwardIterator __i 2305 = std::__lower_bound(__first, __last, __val, 2306 __gnu_cxx::__ops::__iter_less_val()); 2307 return __i != __last && !(__val < *__i); 2308 } 2309 2310 /** 2311 * @brief Determines whether an element exists in a range. 2312 * @ingroup binary_search_algorithms 2313 * @param __first An iterator. 2314 * @param __last Another iterator. 2315 * @param __val The search term. 2316 * @param __comp A functor to use for comparisons. 2317 * @return True if @p __val (or its equivalent) is in @p [__first,__last]. 2318 * 2319 * Note that this does not actually return an iterator to @p __val. For 2320 * that, use std::find or a container's specialized find member functions. 2321 * 2322 * The comparison function should have the same effects on ordering as 2323 * the function used for the initial sort. 2324 */ 2325 template<typename _ForwardIterator, typename _Tp, typename _Compare> 2326 bool 2327 binary_search(_ForwardIterator __first, _ForwardIterator __last, 2328 const _Tp& __val, _Compare __comp) 2329 { 2330 typedef typename iterator_traits<_ForwardIterator>::value_type 2331 _ValueType; 2332 2333 // concept requirements 2334 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 2335 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 2336 _Tp, _ValueType>) 2337 __glibcxx_requires_partitioned_lower_pred(__first, __last, 2338 __val, __comp); 2339 __glibcxx_requires_partitioned_upper_pred(__first, __last, 2340 __val, __comp); 2341 2342 _ForwardIterator __i 2343 = std::__lower_bound(__first, __last, __val, 2344 __gnu_cxx::__ops::__iter_comp_val(__CheckedCompare(__comp))); 2345 return __i != __last && !bool(__comp(__val, *__i)); 2346 } 2347 2348 // merge 2349 2350 /// This is a helper function for the __merge_adaptive routines. 2351 template<typename _InputIterator1, typename _InputIterator2, 2352 typename _OutputIterator, typename _Compare> 2353 void 2354 __move_merge_adaptive(_InputIterator1 __first1, _InputIterator1 __last1, 2355 _InputIterator2 __first2, _InputIterator2 __last2, 2356 _OutputIterator __result, _Compare __comp) 2357 { 2358 while (__first1 != __last1 && __first2 != __last2) 2359 { 2360 if (__comp(__first2, __first1)) 2361 { 2362 *__result = _GLIBCXX_MOVE(*__first2); 2363 ++__first2; 2364 } 2365 else 2366 { 2367 *__result = _GLIBCXX_MOVE(*__first1); 2368 ++__first1; 2369 } 2370 ++__result; 2371 } 2372 if (__first1 != __last1) 2373 _GLIBCXX_MOVE3(__first1, __last1, __result); 2374 } 2375 2376 /// This is a helper function for the __merge_adaptive routines. 2377 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2, 2378 typename _BidirectionalIterator3, typename _Compare> 2379 void 2380 __move_merge_adaptive_backward(_BidirectionalIterator1 __first1, 2381 _BidirectionalIterator1 __last1, 2382 _BidirectionalIterator2 __first2, 2383 _BidirectionalIterator2 __last2, 2384 _BidirectionalIterator3 __result, 2385 _Compare __comp) 2386 { 2387 if (__first1 == __last1) 2388 { 2389 _GLIBCXX_MOVE_BACKWARD3(__first2, __last2, __result); 2390 return; 2391 } 2392 else if (__first2 == __last2) 2393 return; 2394 2395 --__last1; 2396 --__last2; 2397 while (true) 2398 { 2399 if (__comp(__last2, __last1)) 2400 { 2401 *--__result = _GLIBCXX_MOVE(*__last1); 2402 if (__first1 == __last1) 2403 { 2404 _GLIBCXX_MOVE_BACKWARD3(__first2, ++__last2, __result); 2405 return; 2406 } 2407 --__last1; 2408 } 2409 else 2410 { 2411 *--__result = _GLIBCXX_MOVE(*__last2); 2412 if (__first2 == __last2) 2413 return; 2414 --__last2; 2415 } 2416 } 2417 } 2418 2419 /// This is a helper function for the merge routines. 2420 template<typename _BidirectionalIterator1, typename _BidirectionalIterator2, 2421 typename _Distance> 2422 _BidirectionalIterator1 2423 __rotate_adaptive(_BidirectionalIterator1 __first, 2424 _BidirectionalIterator1 __middle, 2425 _BidirectionalIterator1 __last, 2426 _Distance __len1, _Distance __len2, 2427 _BidirectionalIterator2 __buffer, 2428 _Distance __buffer_size) 2429 { 2430 _BidirectionalIterator2 __buffer_end; 2431 if (__len1 > __len2 && __len2 <= __buffer_size) 2432 { 2433 if (__len2) 2434 { 2435 __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer); 2436 _GLIBCXX_MOVE_BACKWARD3(__first, __middle, __last); 2437 return _GLIBCXX_MOVE3(__buffer, __buffer_end, __first); 2438 } 2439 else 2440 return __first; 2441 } 2442 else if (__len1 <= __buffer_size) 2443 { 2444 if (__len1) 2445 { 2446 __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer); 2447 _GLIBCXX_MOVE3(__middle, __last, __first); 2448 return _GLIBCXX_MOVE_BACKWARD3(__buffer, __buffer_end, __last); 2449 } 2450 else 2451 return __last; 2452 } 2453 else 2454 { 2455 std::rotate(__first, __middle, __last); 2456 std::advance(__first, std::distance(__middle, __last)); 2457 return __first; 2458 } 2459 } 2460 2461 /// This is a helper function for the merge routines. 2462 template<typename _BidirectionalIterator, typename _Distance, 2463 typename _Pointer, typename _Compare> 2464 void 2465 __merge_adaptive(_BidirectionalIterator __first, 2466 _BidirectionalIterator __middle, 2467 _BidirectionalIterator __last, 2468 _Distance __len1, _Distance __len2, 2469 _Pointer __buffer, _Distance __buffer_size, 2470 _Compare __comp) 2471 { 2472 if (__len1 <= __len2 && __len1 <= __buffer_size) 2473 { 2474 _Pointer __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer); 2475 std::__move_merge_adaptive(__buffer, __buffer_end, __middle, __last, 2476 __first, __comp); 2477 } 2478 else if (__len2 <= __buffer_size) 2479 { 2480 _Pointer __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer); 2481 std::__move_merge_adaptive_backward(__first, __middle, __buffer, 2482 __buffer_end, __last, __comp); 2483 } 2484 else 2485 { 2486 _BidirectionalIterator __first_cut = __first; 2487 _BidirectionalIterator __second_cut = __middle; 2488 _Distance __len11 = 0; 2489 _Distance __len22 = 0; 2490 if (__len1 > __len2) 2491 { 2492 __len11 = __len1 / 2; 2493 std::advance(__first_cut, __len11); 2494 __second_cut 2495 = std::__lower_bound(__middle, __last, *__first_cut, 2496 __gnu_cxx::__ops::__iter_comp_val(__comp)); 2497 __len22 = std::distance(__middle, __second_cut); 2498 } 2499 else 2500 { 2501 __len22 = __len2 / 2; 2502 std::advance(__second_cut, __len22); 2503 __first_cut 2504 = std::__upper_bound(__first, __middle, *__second_cut, 2505 __gnu_cxx::__ops::__val_comp_iter(__comp)); 2506 __len11 = std::distance(__first, __first_cut); 2507 } 2508 _BidirectionalIterator __new_middle 2509 = std::__rotate_adaptive(__first_cut, __middle, __second_cut, 2510 __len1 - __len11, __len22, __buffer, 2511 __buffer_size); 2512 std::__merge_adaptive(__first, __first_cut, __new_middle, __len11, 2513 __len22, __buffer, __buffer_size, __comp); 2514 std::__merge_adaptive(__new_middle, __second_cut, __last, 2515 __len1 - __len11, 2516 __len2 - __len22, __buffer, 2517 __buffer_size, __comp); 2518 } 2519 } 2520 2521 /// This is a helper function for the merge routines. 2522 template<typename _BidirectionalIterator, typename _Distance, 2523 typename _Compare> 2524 void 2525 __merge_without_buffer(_BidirectionalIterator __first, 2526 _BidirectionalIterator __middle, 2527 _BidirectionalIterator __last, 2528 _Distance __len1, _Distance __len2, 2529 _Compare __comp) 2530 { 2531 if (__len1 == 0 || __len2 == 0) 2532 return; 2533 if (__len1 + __len2 == 2) 2534 { 2535 if (__comp(__middle, __first)) 2536 std::iter_swap(__first, __middle); 2537 return; 2538 } 2539 _BidirectionalIterator __first_cut = __first; 2540 _BidirectionalIterator __second_cut = __middle; 2541 _Distance __len11 = 0; 2542 _Distance __len22 = 0; 2543 if (__len1 > __len2) 2544 { 2545 __len11 = __len1 / 2; 2546 std::advance(__first_cut, __len11); 2547 __second_cut 2548 = std::__lower_bound(__middle, __last, *__first_cut, 2549 __gnu_cxx::__ops::__iter_comp_val(__comp)); 2550 __len22 = std::distance(__middle, __second_cut); 2551 } 2552 else 2553 { 2554 __len22 = __len2 / 2; 2555 std::advance(__second_cut, __len22); 2556 __first_cut 2557 = std::__upper_bound(__first, __middle, *__second_cut, 2558 __gnu_cxx::__ops::__val_comp_iter(__comp)); 2559 __len11 = std::distance(__first, __first_cut); 2560 } 2561 std::rotate(__first_cut, __middle, __second_cut); 2562 _BidirectionalIterator __new_middle = __first_cut; 2563 std::advance(__new_middle, std::distance(__middle, __second_cut)); 2564 std::__merge_without_buffer(__first, __first_cut, __new_middle, 2565 __len11, __len22, __comp); 2566 std::__merge_without_buffer(__new_middle, __second_cut, __last, 2567 __len1 - __len11, __len2 - __len22, __comp); 2568 } 2569 2570 template<typename _BidirectionalIterator, typename _Compare> 2571 void 2572 __inplace_merge(_BidirectionalIterator __first, 2573 _BidirectionalIterator __middle, 2574 _BidirectionalIterator __last, 2575 _Compare __comp) 2576 { 2577 typedef typename iterator_traits<_BidirectionalIterator>::value_type 2578 _ValueType; 2579 typedef typename iterator_traits<_BidirectionalIterator>::difference_type 2580 _DistanceType; 2581 2582 if (__first == __middle || __middle == __last) 2583 return; 2584 2585 const _DistanceType __len1 = std::distance(__first, __middle); 2586 const _DistanceType __len2 = std::distance(__middle, __last); 2587 2588 typedef _Temporary_buffer<_BidirectionalIterator, _ValueType> _TmpBuf; 2589 _TmpBuf __buf(__first, __last); 2590 2591 if (__buf.begin() == 0) 2592 std::__merge_without_buffer 2593 (__first, __middle, __last, __len1, __len2, __comp); 2594 else 2595 std::__merge_adaptive 2596 (__first, __middle, __last, __len1, __len2, __buf.begin(), 2597 _DistanceType(__buf.size()), __comp); 2598 } 2599 2600 /** 2601 * @brief Merges two sorted ranges in place. 2602 * @ingroup sorting_algorithms 2603 * @param __first An iterator. 2604 * @param __middle Another iterator. 2605 * @param __last Another iterator. 2606 * @return Nothing. 2607 * 2608 * Merges two sorted and consecutive ranges, [__first,__middle) and 2609 * [__middle,__last), and puts the result in [__first,__last). The 2610 * output will be sorted. The sort is @e stable, that is, for 2611 * equivalent elements in the two ranges, elements from the first 2612 * range will always come before elements from the second. 2613 * 2614 * If enough additional memory is available, this takes (__last-__first)-1 2615 * comparisons. Otherwise an NlogN algorithm is used, where N is 2616 * distance(__first,__last). 2617 */ 2618 template<typename _BidirectionalIterator> 2619 inline void 2620 inplace_merge(_BidirectionalIterator __first, 2621 _BidirectionalIterator __middle, 2622 _BidirectionalIterator __last) 2623 { 2624 // concept requirements 2625 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< 2626 _BidirectionalIterator>) 2627 __glibcxx_function_requires(_LessThanComparableConcept< 2628 typename iterator_traits<_BidirectionalIterator>::value_type>) 2629 __glibcxx_requires_sorted(__first, __middle); 2630 __glibcxx_requires_sorted(__middle, __last); 2631 2632 std::__inplace_merge(__first, __middle, __last, 2633 __gnu_cxx::__ops::__iter_less_iter()); 2634 } 2635 2636 /** 2637 * @brief Merges two sorted ranges in place. 2638 * @ingroup sorting_algorithms 2639 * @param __first An iterator. 2640 * @param __middle Another iterator. 2641 * @param __last Another iterator. 2642 * @param __comp A functor to use for comparisons. 2643 * @return Nothing. 2644 * 2645 * Merges two sorted and consecutive ranges, [__first,__middle) and 2646 * [middle,last), and puts the result in [__first,__last). The output will 2647 * be sorted. The sort is @e stable, that is, for equivalent 2648 * elements in the two ranges, elements from the first range will always 2649 * come before elements from the second. 2650 * 2651 * If enough additional memory is available, this takes (__last-__first)-1 2652 * comparisons. Otherwise an NlogN algorithm is used, where N is 2653 * distance(__first,__last). 2654 * 2655 * The comparison function should have the same effects on ordering as 2656 * the function used for the initial sort. 2657 */ 2658 template<typename _BidirectionalIterator, typename _Compare> 2659 inline void 2660 inplace_merge(_BidirectionalIterator __first, 2661 _BidirectionalIterator __middle, 2662 _BidirectionalIterator __last, 2663 _Compare __comp) 2664 { 2665 // concept requirements 2666 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< 2667 _BidirectionalIterator>) 2668 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 2669 typename iterator_traits<_BidirectionalIterator>::value_type, 2670 typename iterator_traits<_BidirectionalIterator>::value_type>) 2671 __glibcxx_requires_sorted_pred(__first, __middle, __comp); 2672 __glibcxx_requires_sorted_pred(__middle, __last, __comp); 2673 2674 std::__inplace_merge(__first, __middle, __last, 2675 __gnu_cxx::__ops::__iter_comp_iter(__CheckedCompare(__comp))); 2676 } 2677 2678 2679 /// This is a helper function for the __merge_sort_loop routines. 2680 template<typename _InputIterator, typename _OutputIterator, 2681 typename _Compare> 2682 _OutputIterator 2683 __move_merge(_InputIterator __first1, _InputIterator __last1, 2684 _InputIterator __first2, _InputIterator __last2, 2685 _OutputIterator __result, _Compare __comp) 2686 { 2687 while (__first1 != __last1 && __first2 != __last2) 2688 { 2689 if (__comp(__first2, __first1)) 2690 { 2691 *__result = _GLIBCXX_MOVE(*__first2); 2692 ++__first2; 2693 } 2694 else 2695 { 2696 *__result = _GLIBCXX_MOVE(*__first1); 2697 ++__first1; 2698 } 2699 ++__result; 2700 } 2701 return _GLIBCXX_MOVE3(__first2, __last2, 2702 _GLIBCXX_MOVE3(__first1, __last1, 2703 __result)); 2704 } 2705 2706 template<typename _RandomAccessIterator1, typename _RandomAccessIterator2, 2707 typename _Distance, typename _Compare> 2708 void 2709 __merge_sort_loop(_RandomAccessIterator1 __first, 2710 _RandomAccessIterator1 __last, 2711 _RandomAccessIterator2 __result, _Distance __step_size, 2712 _Compare __comp) 2713 { 2714 const _Distance __two_step = 2 * __step_size; 2715 2716 while (__last - __first >= __two_step) 2717 { 2718 __result = std::__move_merge(__first, __first + __step_size, 2719 __first + __step_size, 2720 __first + __two_step, 2721 __result, __comp); 2722 __first += __two_step; 2723 } 2724 __step_size = std::min(_Distance(__last - __first), __step_size); 2725 2726 std::__move_merge(__first, __first + __step_size, 2727 __first + __step_size, __last, __result, __comp); 2728 } 2729 2730 template<typename _RandomAccessIterator, typename _Distance, 2731 typename _Compare> 2732 void 2733 __chunk_insertion_sort(_RandomAccessIterator __first, 2734 _RandomAccessIterator __last, 2735 _Distance __chunk_size, _Compare __comp) 2736 { 2737 while (__last - __first >= __chunk_size) 2738 { 2739 std::__insertion_sort(__first, __first + __chunk_size, __comp); 2740 __first += __chunk_size; 2741 } 2742 std::__insertion_sort(__first, __last, __comp); 2743 } 2744 2745 enum { _S_chunk_size = 7 }; 2746 2747 template<typename _RandomAccessIterator, typename _Pointer, typename _Compare> 2748 void 2749 __merge_sort_with_buffer(_RandomAccessIterator __first, 2750 _RandomAccessIterator __last, 2751 _Pointer __buffer, _Compare __comp) 2752 { 2753 typedef typename iterator_traits<_RandomAccessIterator>::difference_type 2754 _Distance; 2755 2756 const _Distance __len = __last - __first; 2757 const _Pointer __buffer_last = __buffer + __len; 2758 2759 _Distance __step_size = _S_chunk_size; 2760 std::__chunk_insertion_sort(__first, __last, __step_size, __comp); 2761 2762 while (__step_size < __len) 2763 { 2764 std::__merge_sort_loop(__first, __last, __buffer, 2765 __step_size, __comp); 2766 __step_size *= 2; 2767 std::__merge_sort_loop(__buffer, __buffer_last, __first, 2768 __step_size, __comp); 2769 __step_size *= 2; 2770 } 2771 } 2772 2773 template<typename _RandomAccessIterator, typename _Pointer, 2774 typename _Distance, typename _Compare> 2775 void 2776 __stable_sort_adaptive(_RandomAccessIterator __first, 2777 _RandomAccessIterator __last, 2778 _Pointer __buffer, _Distance __buffer_size, 2779 _Compare __comp) 2780 { 2781 const _Distance __len = (__last - __first + 1) / 2; 2782 const _RandomAccessIterator __middle = __first + __len; 2783 if (__len > __buffer_size) 2784 { 2785 std::__stable_sort_adaptive(__first, __middle, __buffer, 2786 __buffer_size, __comp); 2787 std::__stable_sort_adaptive(__middle, __last, __buffer, 2788 __buffer_size, __comp); 2789 } 2790 else 2791 { 2792 std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp); 2793 std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp); 2794 } 2795 std::__merge_adaptive(__first, __middle, __last, 2796 _Distance(__middle - __first), 2797 _Distance(__last - __middle), 2798 __buffer, __buffer_size, 2799 __comp); 2800 } 2801 2802 /// This is a helper function for the stable sorting routines. 2803 template<typename _RandomAccessIterator, typename _Compare> 2804 void 2805 __inplace_stable_sort(_RandomAccessIterator __first, 2806 _RandomAccessIterator __last, _Compare __comp) 2807 { 2808 if (__last - __first < 15) 2809 { 2810 std::__insertion_sort(__first, __last, __comp); 2811 return; 2812 } 2813 _RandomAccessIterator __middle = __first + (__last - __first) / 2; 2814 std::__inplace_stable_sort(__first, __middle, __comp); 2815 std::__inplace_stable_sort(__middle, __last, __comp); 2816 std::__merge_without_buffer(__first, __middle, __last, 2817 __middle - __first, 2818 __last - __middle, 2819 __comp); 2820 } 2821 2822 // stable_sort 2823 2824 // Set algorithms: includes, set_union, set_intersection, set_difference, 2825 // set_symmetric_difference. All of these algorithms have the precondition 2826 // that their input ranges are sorted and the postcondition that their output 2827 // ranges are sorted. 2828 2829 template<typename _InputIterator1, typename _InputIterator2, 2830 typename _Compare> 2831 bool 2832 __includes(_InputIterator1 __first1, _InputIterator1 __last1, 2833 _InputIterator2 __first2, _InputIterator2 __last2, 2834 _Compare __comp) 2835 { 2836 while (__first1 != __last1 && __first2 != __last2) 2837 if (__comp(__first2, __first1)) 2838 return false; 2839 else if (__comp(__first1, __first2)) 2840 ++__first1; 2841 else 2842 ++__first1, ++__first2; 2843 2844 return __first2 == __last2; 2845 } 2846 2847 /** 2848 * @brief Determines whether all elements of a sequence exists in a range. 2849 * @param __first1 Start of search range. 2850 * @param __last1 End of search range. 2851 * @param __first2 Start of sequence 2852 * @param __last2 End of sequence. 2853 * @return True if each element in [__first2,__last2) is contained in order 2854 * within [__first1,__last1). False otherwise. 2855 * @ingroup set_algorithms 2856 * 2857 * This operation expects both [__first1,__last1) and 2858 * [__first2,__last2) to be sorted. Searches for the presence of 2859 * each element in [__first2,__last2) within [__first1,__last1). 2860 * The iterators over each range only move forward, so this is a 2861 * linear algorithm. If an element in [__first2,__last2) is not 2862 * found before the search iterator reaches @p __last2, false is 2863 * returned. 2864 */ 2865 template<typename _InputIterator1, typename _InputIterator2> 2866 inline bool 2867 includes(_InputIterator1 __first1, _InputIterator1 __last1, 2868 _InputIterator2 __first2, _InputIterator2 __last2) 2869 { 2870 // concept requirements 2871 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 2872 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 2873 __glibcxx_function_requires(_LessThanOpConcept< 2874 typename iterator_traits<_InputIterator1>::value_type, 2875 typename iterator_traits<_InputIterator2>::value_type>) 2876 __glibcxx_function_requires(_LessThanOpConcept< 2877 typename iterator_traits<_InputIterator2>::value_type, 2878 typename iterator_traits<_InputIterator1>::value_type>) 2879 __glibcxx_requires_sorted_set(__first1, __last1, __first2); 2880 __glibcxx_requires_sorted_set(__first2, __last2, __first1); 2881 2882 return std::__includes(__first1, __last1, __first2, __last2, 2883 __gnu_cxx::__ops::__iter_less_iter()); 2884 } 2885 2886 /** 2887 * @brief Determines whether all elements of a sequence exists in a range 2888 * using comparison. 2889 * @ingroup set_algorithms 2890 * @param __first1 Start of search range. 2891 * @param __last1 End of search range. 2892 * @param __first2 Start of sequence 2893 * @param __last2 End of sequence. 2894 * @param __comp Comparison function to use. 2895 * @return True if each element in [__first2,__last2) is contained 2896 * in order within [__first1,__last1) according to comp. False 2897 * otherwise. @ingroup set_algorithms 2898 * 2899 * This operation expects both [__first1,__last1) and 2900 * [__first2,__last2) to be sorted. Searches for the presence of 2901 * each element in [__first2,__last2) within [__first1,__last1), 2902 * using comp to decide. The iterators over each range only move 2903 * forward, so this is a linear algorithm. If an element in 2904 * [__first2,__last2) is not found before the search iterator 2905 * reaches @p __last2, false is returned. 2906 */ 2907 template<typename _InputIterator1, typename _InputIterator2, 2908 typename _Compare> 2909 inline bool 2910 includes(_InputIterator1 __first1, _InputIterator1 __last1, 2911 _InputIterator2 __first2, _InputIterator2 __last2, 2912 _Compare __comp) 2913 { 2914 // concept requirements 2915 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 2916 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 2917 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 2918 typename iterator_traits<_InputIterator1>::value_type, 2919 typename iterator_traits<_InputIterator2>::value_type>) 2920 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 2921 typename iterator_traits<_InputIterator2>::value_type, 2922 typename iterator_traits<_InputIterator1>::value_type>) 2923 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); 2924 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); 2925 2926 return std::__includes(__first1, __last1, __first2, __last2, 2927 __gnu_cxx::__ops::__iter_comp_iter(__CheckedCompare(__comp))); 2928 } 2929 2930 // nth_element 2931 // merge 2932 // set_difference 2933 // set_intersection 2934 // set_union 2935 // stable_sort 2936 // set_symmetric_difference 2937 // min_element 2938 // max_element 2939 2940 template<typename _BidirectionalIterator, typename _Compare> 2941 bool 2942 __next_permutation(_BidirectionalIterator __first, 2943 _BidirectionalIterator __last, _Compare __comp) 2944 { 2945 if (__first == __last) 2946 return false; 2947 _BidirectionalIterator __i = __first; 2948 ++__i; 2949 if (__i == __last) 2950 return false; 2951 __i = __last; 2952 --__i; 2953 2954 for(;;) 2955 { 2956 _BidirectionalIterator __ii = __i; 2957 --__i; 2958 if (__comp(__i, __ii)) 2959 { 2960 _BidirectionalIterator __j = __last; 2961 while (!__comp(__i, --__j)) 2962 {} 2963 std::iter_swap(__i, __j); 2964 std::__reverse(__ii, __last, 2965 std::__iterator_category(__first)); 2966 return true; 2967 } 2968 if (__i == __first) 2969 { 2970 std::__reverse(__first, __last, 2971 std::__iterator_category(__first)); 2972 return false; 2973 } 2974 } 2975 } 2976 2977 /** 2978 * @brief Permute range into the next @e dictionary ordering. 2979 * @ingroup sorting_algorithms 2980 * @param __first Start of range. 2981 * @param __last End of range. 2982 * @return False if wrapped to first permutation, true otherwise. 2983 * 2984 * Treats all permutations of the range as a set of @e dictionary sorted 2985 * sequences. Permutes the current sequence into the next one of this set. 2986 * Returns true if there are more sequences to generate. If the sequence 2987 * is the largest of the set, the smallest is generated and false returned. 2988 */ 2989 template<typename _BidirectionalIterator> 2990 inline bool 2991 next_permutation(_BidirectionalIterator __first, 2992 _BidirectionalIterator __last) 2993 { 2994 // concept requirements 2995 __glibcxx_function_requires(_BidirectionalIteratorConcept< 2996 _BidirectionalIterator>) 2997 __glibcxx_function_requires(_LessThanComparableConcept< 2998 typename iterator_traits<_BidirectionalIterator>::value_type>) 2999 __glibcxx_requires_valid_range(__first, __last); 3000 3001 return std::__next_permutation 3002 (__first, __last, __gnu_cxx::__ops::__iter_less_iter()); 3003 } 3004 3005 /** 3006 * @brief Permute range into the next @e dictionary ordering using 3007 * comparison functor. 3008 * @ingroup sorting_algorithms 3009 * @param __first Start of range. 3010 * @param __last End of range. 3011 * @param __comp A comparison functor. 3012 * @return False if wrapped to first permutation, true otherwise. 3013 * 3014 * Treats all permutations of the range [__first,__last) as a set of 3015 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current 3016 * sequence into the next one of this set. Returns true if there are more 3017 * sequences to generate. If the sequence is the largest of the set, the 3018 * smallest is generated and false returned. 3019 */ 3020 template<typename _BidirectionalIterator, typename _Compare> 3021 inline bool 3022 next_permutation(_BidirectionalIterator __first, 3023 _BidirectionalIterator __last, _Compare __comp) 3024 { 3025 // concept requirements 3026 __glibcxx_function_requires(_BidirectionalIteratorConcept< 3027 _BidirectionalIterator>) 3028 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 3029 typename iterator_traits<_BidirectionalIterator>::value_type, 3030 typename iterator_traits<_BidirectionalIterator>::value_type>) 3031 __glibcxx_requires_valid_range(__first, __last); 3032 3033 return std::__next_permutation 3034 (__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__CheckedCompare(__comp))); 3035 } 3036 3037 template<typename _BidirectionalIterator, typename _Compare> 3038 bool 3039 __prev_permutation(_BidirectionalIterator __first, 3040 _BidirectionalIterator __last, _Compare __comp) 3041 { 3042 if (__first == __last) 3043 return false; 3044 _BidirectionalIterator __i = __first; 3045 ++__i; 3046 if (__i == __last) 3047 return false; 3048 __i = __last; 3049 --__i; 3050 3051 for(;;) 3052 { 3053 _BidirectionalIterator __ii = __i; 3054 --__i; 3055 if (__comp(__ii, __i)) 3056 { 3057 _BidirectionalIterator __j = __last; 3058 while (!__comp(--__j, __i)) 3059 {} 3060 std::iter_swap(__i, __j); 3061 std::__reverse(__ii, __last, 3062 std::__iterator_category(__first)); 3063 return true; 3064 } 3065 if (__i == __first) 3066 { 3067 std::__reverse(__first, __last, 3068 std::__iterator_category(__first)); 3069 return false; 3070 } 3071 } 3072 } 3073 3074 /** 3075 * @brief Permute range into the previous @e dictionary ordering. 3076 * @ingroup sorting_algorithms 3077 * @param __first Start of range. 3078 * @param __last End of range. 3079 * @return False if wrapped to last permutation, true otherwise. 3080 * 3081 * Treats all permutations of the range as a set of @e dictionary sorted 3082 * sequences. Permutes the current sequence into the previous one of this 3083 * set. Returns true if there are more sequences to generate. If the 3084 * sequence is the smallest of the set, the largest is generated and false 3085 * returned. 3086 */ 3087 template<typename _BidirectionalIterator> 3088 inline bool 3089 prev_permutation(_BidirectionalIterator __first, 3090 _BidirectionalIterator __last) 3091 { 3092 // concept requirements 3093 __glibcxx_function_requires(_BidirectionalIteratorConcept< 3094 _BidirectionalIterator>) 3095 __glibcxx_function_requires(_LessThanComparableConcept< 3096 typename iterator_traits<_BidirectionalIterator>::value_type>) 3097 __glibcxx_requires_valid_range(__first, __last); 3098 3099 return std::__prev_permutation(__first, __last, 3100 __gnu_cxx::__ops::__iter_less_iter()); 3101 } 3102 3103 /** 3104 * @brief Permute range into the previous @e dictionary ordering using 3105 * comparison functor. 3106 * @ingroup sorting_algorithms 3107 * @param __first Start of range. 3108 * @param __last End of range. 3109 * @param __comp A comparison functor. 3110 * @return False if wrapped to last permutation, true otherwise. 3111 * 3112 * Treats all permutations of the range [__first,__last) as a set of 3113 * @e dictionary sorted sequences ordered by @p __comp. Permutes the current 3114 * sequence into the previous one of this set. Returns true if there are 3115 * more sequences to generate. If the sequence is the smallest of the set, 3116 * the largest is generated and false returned. 3117 */ 3118 template<typename _BidirectionalIterator, typename _Compare> 3119 inline bool 3120 prev_permutation(_BidirectionalIterator __first, 3121 _BidirectionalIterator __last, _Compare __comp) 3122 { 3123 // concept requirements 3124 __glibcxx_function_requires(_BidirectionalIteratorConcept< 3125 _BidirectionalIterator>) 3126 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 3127 typename iterator_traits<_BidirectionalIterator>::value_type, 3128 typename iterator_traits<_BidirectionalIterator>::value_type>) 3129 __glibcxx_requires_valid_range(__first, __last); 3130 3131 return std::__prev_permutation(__first, __last, 3132 __gnu_cxx::__ops::__iter_comp_iter(__CheckedCompare(__comp))); 3133 } 3134 3135 // replace 3136 // replace_if 3137 3138 template<typename _InputIterator, typename _OutputIterator, 3139 typename _Predicate, typename _Tp> 3140 _OutputIterator 3141 __replace_copy_if(_InputIterator __first, _InputIterator __last, 3142 _OutputIterator __result, 3143 _Predicate __pred, const _Tp& __new_value) 3144 { 3145 for (; __first != __last; ++__first, ++__result) 3146 if (__pred(__first)) 3147 *__result = __new_value; 3148 else 3149 *__result = *__first; 3150 return __result; 3151 } 3152 3153 /** 3154 * @brief Copy a sequence, replacing each element of one value with another 3155 * value. 3156 * @param __first An input iterator. 3157 * @param __last An input iterator. 3158 * @param __result An output iterator. 3159 * @param __old_value The value to be replaced. 3160 * @param __new_value The replacement value. 3161 * @return The end of the output sequence, @p result+(last-first). 3162 * 3163 * Copies each element in the input range @p [__first,__last) to the 3164 * output range @p [__result,__result+(__last-__first)) replacing elements 3165 * equal to @p __old_value with @p __new_value. 3166 */ 3167 template<typename _InputIterator, typename _OutputIterator, typename _Tp> 3168 inline _OutputIterator 3169 replace_copy(_InputIterator __first, _InputIterator __last, 3170 _OutputIterator __result, 3171 const _Tp& __old_value, const _Tp& __new_value) 3172 { 3173 // concept requirements 3174 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 3175 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 3176 typename iterator_traits<_InputIterator>::value_type>) 3177 __glibcxx_function_requires(_EqualOpConcept< 3178 typename iterator_traits<_InputIterator>::value_type, _Tp>) 3179 __glibcxx_requires_valid_range(__first, __last); 3180 3181 return std::__replace_copy_if(__first, __last, __result, 3182 __gnu_cxx::__ops::__iter_equals_val(__old_value), 3183 __new_value); 3184 } 3185 3186 /** 3187 * @brief Copy a sequence, replacing each value for which a predicate 3188 * returns true with another value. 3189 * @ingroup mutating_algorithms 3190 * @param __first An input iterator. 3191 * @param __last An input iterator. 3192 * @param __result An output iterator. 3193 * @param __pred A predicate. 3194 * @param __new_value The replacement value. 3195 * @return The end of the output sequence, @p __result+(__last-__first). 3196 * 3197 * Copies each element in the range @p [__first,__last) to the range 3198 * @p [__result,__result+(__last-__first)) replacing elements for which 3199 * @p __pred returns true with @p __new_value. 3200 */ 3201 template<typename _InputIterator, typename _OutputIterator, 3202 typename _Predicate, typename _Tp> 3203 inline _OutputIterator 3204 replace_copy_if(_InputIterator __first, _InputIterator __last, 3205 _OutputIterator __result, 3206 _Predicate __pred, const _Tp& __new_value) 3207 { 3208 // concept requirements 3209 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 3210 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 3211 typename iterator_traits<_InputIterator>::value_type>) 3212 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 3213 typename iterator_traits<_InputIterator>::value_type>) 3214 __glibcxx_requires_valid_range(__first, __last); 3215 3216 return std::__replace_copy_if(__first, __last, __result, 3217 __gnu_cxx::__ops::__pred_iter(__pred), 3218 __new_value); 3219 } 3220 3221 template<typename _InputIterator, typename _Predicate> 3222 typename iterator_traits<_InputIterator>::difference_type 3223 __count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) 3224 { 3225 typename iterator_traits<_InputIterator>::difference_type __n = 0; 3226 for (; __first != __last; ++__first) 3227 if (__pred(__first)) 3228 ++__n; 3229 return __n; 3230 } 3231 3232 #if __cplusplus >= 201103L 3233 /** 3234 * @brief Determines whether the elements of a sequence are sorted. 3235 * @ingroup sorting_algorithms 3236 * @param __first An iterator. 3237 * @param __last Another iterator. 3238 * @return True if the elements are sorted, false otherwise. 3239 */ 3240 template<typename _ForwardIterator> 3241 inline bool 3242 is_sorted(_ForwardIterator __first, _ForwardIterator __last) 3243 { return std::is_sorted_until(__first, __last) == __last; } 3244 3245 /** 3246 * @brief Determines whether the elements of a sequence are sorted 3247 * according to a comparison functor. 3248 * @ingroup sorting_algorithms 3249 * @param __first An iterator. 3250 * @param __last Another iterator. 3251 * @param __comp A comparison functor. 3252 * @return True if the elements are sorted, false otherwise. 3253 */ 3254 template<typename _ForwardIterator, typename _Compare> 3255 inline bool 3256 is_sorted(_ForwardIterator __first, _ForwardIterator __last, 3257 _Compare __comp) 3258 { return std::is_sorted_until(__first, __last, __comp) == __last; } 3259 3260 template<typename _ForwardIterator, typename _Compare> 3261 _ForwardIterator 3262 __is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, 3263 _Compare __comp) 3264 { 3265 if (__first == __last) 3266 return __last; 3267 3268 _ForwardIterator __next = __first; 3269 for (++__next; __next != __last; __first = __next, ++__next) 3270 if (__comp(__next, __first)) 3271 return __next; 3272 return __next; 3273 } 3274 3275 /** 3276 * @brief Determines the end of a sorted sequence. 3277 * @ingroup sorting_algorithms 3278 * @param __first An iterator. 3279 * @param __last Another iterator. 3280 * @return An iterator pointing to the last iterator i in [__first, __last) 3281 * for which the range [__first, i) is sorted. 3282 */ 3283 template<typename _ForwardIterator> 3284 inline _ForwardIterator 3285 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last) 3286 { 3287 // concept requirements 3288 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 3289 __glibcxx_function_requires(_LessThanComparableConcept< 3290 typename iterator_traits<_ForwardIterator>::value_type>) 3291 __glibcxx_requires_valid_range(__first, __last); 3292 3293 return std::__is_sorted_until(__first, __last, 3294 __gnu_cxx::__ops::__iter_less_iter()); 3295 } 3296 3297 /** 3298 * @brief Determines the end of a sorted sequence using comparison functor. 3299 * @ingroup sorting_algorithms 3300 * @param __first An iterator. 3301 * @param __last Another iterator. 3302 * @param __comp A comparison functor. 3303 * @return An iterator pointing to the last iterator i in [__first, __last) 3304 * for which the range [__first, i) is sorted. 3305 */ 3306 template<typename _ForwardIterator, typename _Compare> 3307 inline _ForwardIterator 3308 is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, 3309 _Compare __comp) 3310 { 3311 // concept requirements 3312 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 3313 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 3314 typename iterator_traits<_ForwardIterator>::value_type, 3315 typename iterator_traits<_ForwardIterator>::value_type>) 3316 __glibcxx_requires_valid_range(__first, __last); 3317 3318 return std::__is_sorted_until(__first, __last, 3319 __gnu_cxx::__ops::__iter_comp_iter(__CheckedCompare(__comp))); 3320 } 3321 3322 /** 3323 * @brief Determines min and max at once as an ordered pair. 3324 * @ingroup sorting_algorithms 3325 * @param __a A thing of arbitrary type. 3326 * @param __b Another thing of arbitrary type. 3327 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a, 3328 * __b) otherwise. 3329 */ 3330 template<typename _Tp> 3331 inline pair<const _Tp&, const _Tp&> 3332 minmax(const _Tp& __a, const _Tp& __b) 3333 { 3334 // concept requirements 3335 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>) 3336 3337 return __b < __a ? pair<const _Tp&, const _Tp&>(__b, __a) 3338 : pair<const _Tp&, const _Tp&>(__a, __b); 3339 } 3340 3341 /** 3342 * @brief Determines min and max at once as an ordered pair. 3343 * @ingroup sorting_algorithms 3344 * @param __a A thing of arbitrary type. 3345 * @param __b Another thing of arbitrary type. 3346 * @param __comp A @link comparison_functors comparison functor @endlink. 3347 * @return A pair(__b, __a) if __b is smaller than __a, pair(__a, 3348 * __b) otherwise. 3349 */ 3350 template<typename _Tp, typename _Compare> 3351 inline pair<const _Tp&, const _Tp&> 3352 minmax(const _Tp& __a, const _Tp& __b, _Compare __comp) 3353 { 3354 return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) 3355 : pair<const _Tp&, const _Tp&>(__a, __b); 3356 } 3357 3358 template<typename _ForwardIterator, typename _Compare> 3359 pair<_ForwardIterator, _ForwardIterator> 3360 __minmax_element(_ForwardIterator __first, _ForwardIterator __last, 3361 _Compare __comp) 3362 { 3363 _ForwardIterator __next = __first; 3364 if (__first == __last 3365 || ++__next == __last) 3366 return std::make_pair(__first, __first); 3367 3368 _ForwardIterator __min, __max; 3369 if (__comp(__next, __first)) 3370 { 3371 __min = __next; 3372 __max = __first; 3373 } 3374 else 3375 { 3376 __min = __first; 3377 __max = __next; 3378 } 3379 3380 __first = __next; 3381 ++__first; 3382 3383 while (__first != __last) 3384 { 3385 __next = __first; 3386 if (++__next == __last) 3387 { 3388 if (__comp(__first, __min)) 3389 __min = __first; 3390 else if (!__comp(__first, __max)) 3391 __max = __first; 3392 break; 3393 } 3394 3395 if (__comp(__next, __first)) 3396 { 3397 if (__comp(__next, __min)) 3398 __min = __next; 3399 if (!__comp(__first, __max)) 3400 __max = __first; 3401 } 3402 else 3403 { 3404 if (__comp(__first, __min)) 3405 __min = __first; 3406 if (!__comp(__next, __max)) 3407 __max = __next; 3408 } 3409 3410 __first = __next; 3411 ++__first; 3412 } 3413 3414 return std::make_pair(__min, __max); 3415 } 3416 3417 /** 3418 * @brief Return a pair of iterators pointing to the minimum and maximum 3419 * elements in a range. 3420 * @ingroup sorting_algorithms 3421 * @param __first Start of range. 3422 * @param __last End of range. 3423 * @return make_pair(m, M), where m is the first iterator i in 3424 * [__first, __last) such that no other element in the range is 3425 * smaller, and where M is the last iterator i in [__first, __last) 3426 * such that no other element in the range is larger. 3427 */ 3428 template<typename _ForwardIterator> 3429 inline pair<_ForwardIterator, _ForwardIterator> 3430 minmax_element(_ForwardIterator __first, _ForwardIterator __last) 3431 { 3432 // concept requirements 3433 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 3434 __glibcxx_function_requires(_LessThanComparableConcept< 3435 typename iterator_traits<_ForwardIterator>::value_type>) 3436 __glibcxx_requires_valid_range(__first, __last); 3437 3438 return std::__minmax_element(__first, __last, 3439 __gnu_cxx::__ops::__iter_less_iter()); 3440 } 3441 3442 /** 3443 * @brief Return a pair of iterators pointing to the minimum and maximum 3444 * elements in a range. 3445 * @ingroup sorting_algorithms 3446 * @param __first Start of range. 3447 * @param __last End of range. 3448 * @param __comp Comparison functor. 3449 * @return make_pair(m, M), where m is the first iterator i in 3450 * [__first, __last) such that no other element in the range is 3451 * smaller, and where M is the last iterator i in [__first, __last) 3452 * such that no other element in the range is larger. 3453 */ 3454 template<typename _ForwardIterator, typename _Compare> 3455 inline pair<_ForwardIterator, _ForwardIterator> 3456 minmax_element(_ForwardIterator __first, _ForwardIterator __last, 3457 _Compare __comp) 3458 { 3459 // concept requirements 3460 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 3461 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 3462 typename iterator_traits<_ForwardIterator>::value_type, 3463 typename iterator_traits<_ForwardIterator>::value_type>) 3464 __glibcxx_requires_valid_range(__first, __last); 3465 3466 return std::__minmax_element(__first, __last, 3467 __gnu_cxx::__ops::__iter_comp_iter(__comp)); 3468 } 3469 3470 // N2722 + DR 915. 3471 template<typename _Tp> 3472 inline _Tp 3473 min(initializer_list<_Tp> __l) 3474 { return *std::min_element(__l.begin(), __l.end()); } 3475 3476 template<typename _Tp, typename _Compare> 3477 inline _Tp 3478 min(initializer_list<_Tp> __l, _Compare __comp) 3479 { return *std::min_element(__l.begin(), __l.end(), __comp); } 3480 3481 template<typename _Tp> 3482 inline _Tp 3483 max(initializer_list<_Tp> __l) 3484 { return *std::max_element(__l.begin(), __l.end()); } 3485 3486 template<typename _Tp, typename _Compare> 3487 inline _Tp 3488 max(initializer_list<_Tp> __l, _Compare __comp) 3489 { return *std::max_element(__l.begin(), __l.end(), __comp); } 3490 3491 template<typename _Tp> 3492 inline pair<_Tp, _Tp> 3493 minmax(initializer_list<_Tp> __l) 3494 { 3495 pair<const _Tp*, const _Tp*> __p = 3496 std::minmax_element(__l.begin(), __l.end()); 3497 return std::make_pair(*__p.first, *__p.second); 3498 } 3499 3500 template<typename _Tp, typename _Compare> 3501 inline pair<_Tp, _Tp> 3502 minmax(initializer_list<_Tp> __l, _Compare __comp) 3503 { 3504 pair<const _Tp*, const _Tp*> __p = 3505 std::minmax_element(__l.begin(), __l.end(), __comp); 3506 return std::make_pair(*__p.first, *__p.second); 3507 } 3508 3509 template<typename _ForwardIterator1, typename _ForwardIterator2, 3510 typename _BinaryPredicate> 3511 bool 3512 __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 3513 _ForwardIterator2 __first2, _BinaryPredicate __pred) 3514 { 3515 // Efficiently compare identical prefixes: O(N) if sequences 3516 // have the same elements in the same order. 3517 for (; __first1 != __last1; ++__first1, ++__first2) 3518 if (!__pred(__first1, __first2)) 3519 break; 3520 3521 if (__first1 == __last1) 3522 return true; 3523 3524 // Establish __last2 assuming equal ranges by iterating over the 3525 // rest of the list. 3526 _ForwardIterator2 __last2 = __first2; 3527 std::advance(__last2, std::distance(__first1, __last1)); 3528 for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan) 3529 { 3530 if (__scan != std::__find_if(__first1, __scan, 3531 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))) 3532 continue; // We've seen this one before. 3533 3534 auto __matches 3535 = std::__count_if(__first2, __last2, 3536 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)); 3537 if (0 == __matches || 3538 std::__count_if(__scan, __last1, 3539 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)) 3540 != __matches) 3541 return false; 3542 } 3543 return true; 3544 } 3545 3546 /** 3547 * @brief Checks whether a permutation of the second sequence is equal 3548 * to the first sequence. 3549 * @ingroup non_mutating_algorithms 3550 * @param __first1 Start of first range. 3551 * @param __last1 End of first range. 3552 * @param __first2 Start of second range. 3553 * @return true if there exists a permutation of the elements in the range 3554 * [__first2, __first2 + (__last1 - __first1)), beginning with 3555 * ForwardIterator2 begin, such that equal(__first1, __last1, begin) 3556 * returns true; otherwise, returns false. 3557 */ 3558 template<typename _ForwardIterator1, typename _ForwardIterator2> 3559 inline bool 3560 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 3561 _ForwardIterator2 __first2) 3562 { 3563 // concept requirements 3564 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) 3565 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) 3566 __glibcxx_function_requires(_EqualOpConcept< 3567 typename iterator_traits<_ForwardIterator1>::value_type, 3568 typename iterator_traits<_ForwardIterator2>::value_type>) 3569 __glibcxx_requires_valid_range(__first1, __last1); 3570 3571 return std::__is_permutation(__first1, __last1, __first2, 3572 __gnu_cxx::__ops::__iter_equal_to_iter()); 3573 } 3574 3575 /** 3576 * @brief Checks whether a permutation of the second sequence is equal 3577 * to the first sequence. 3578 * @ingroup non_mutating_algorithms 3579 * @param __first1 Start of first range. 3580 * @param __last1 End of first range. 3581 * @param __first2 Start of second range. 3582 * @param __pred A binary predicate. 3583 * @return true if there exists a permutation of the elements in 3584 * the range [__first2, __first2 + (__last1 - __first1)), 3585 * beginning with ForwardIterator2 begin, such that 3586 * equal(__first1, __last1, __begin, __pred) returns true; 3587 * otherwise, returns false. 3588 */ 3589 template<typename _ForwardIterator1, typename _ForwardIterator2, 3590 typename _BinaryPredicate> 3591 inline bool 3592 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 3593 _ForwardIterator2 __first2, _BinaryPredicate __pred) 3594 { 3595 // concept requirements 3596 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) 3597 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) 3598 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, 3599 typename iterator_traits<_ForwardIterator1>::value_type, 3600 typename iterator_traits<_ForwardIterator2>::value_type>) 3601 __glibcxx_requires_valid_range(__first1, __last1); 3602 3603 return std::__is_permutation(__first1, __last1, __first2, 3604 __gnu_cxx::__ops::__iter_comp_iter(__pred)); 3605 } 3606 3607 #if __cplusplus > 201103L 3608 template<typename _ForwardIterator1, typename _ForwardIterator2, 3609 typename _BinaryPredicate> 3610 bool 3611 __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 3612 _ForwardIterator2 __first2, _ForwardIterator2 __last2, 3613 _BinaryPredicate __pred) 3614 { 3615 using _Cat1 3616 = typename iterator_traits<_ForwardIterator1>::iterator_category; 3617 using _Cat2 3618 = typename iterator_traits<_ForwardIterator2>::iterator_category; 3619 using _It1_is_RA = is_same<_Cat1, random_access_iterator_tag>; 3620 using _It2_is_RA = is_same<_Cat2, random_access_iterator_tag>; 3621 constexpr bool __ra_iters = _It1_is_RA() && _It2_is_RA(); 3622 if (__ra_iters) 3623 { 3624 auto __d1 = std::distance(__first1, __last1); 3625 auto __d2 = std::distance(__first2, __last2); 3626 if (__d1 != __d2) 3627 return false; 3628 } 3629 3630 // Efficiently compare identical prefixes: O(N) if sequences 3631 // have the same elements in the same order. 3632 for (; __first1 != __last1; ++__first1, ++__first2) 3633 if (!__pred(__first1, __first2)) 3634 break; 3635 3636 if (__ra_iters) 3637 { 3638 if (__first1 == __last1) 3639 return true; 3640 } 3641 else 3642 { 3643 auto __d1 = std::distance(__first1, __last1); 3644 auto __d2 = std::distance(__first2, __last2); 3645 if (__d1 == 0 && __d2 == 0) 3646 return true; 3647 if (__d1 != __d2) 3648 return false; 3649 } 3650 3651 for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan) 3652 { 3653 if (__scan != std::__find_if(__first1, __scan, 3654 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))) 3655 continue; // We've seen this one before. 3656 3657 auto __matches = std::__count_if(__first2, __last2, 3658 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)); 3659 if (0 == __matches 3660 || std::__count_if(__scan, __last1, 3661 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)) 3662 != __matches) 3663 return false; 3664 } 3665 return true; 3666 } 3667 3668 /** 3669 * @brief Checks whether a permutaion of the second sequence is equal 3670 * to the first sequence. 3671 * @ingroup non_mutating_algorithms 3672 * @param __first1 Start of first range. 3673 * @param __last1 End of first range. 3674 * @param __first2 Start of second range. 3675 * @param __last2 End of first range. 3676 * @return true if there exists a permutation of the elements in the range 3677 * [__first2, __last2), beginning with ForwardIterator2 begin, 3678 * such that equal(__first1, __last1, begin) returns true; 3679 * otherwise, returns false. 3680 */ 3681 template<typename _ForwardIterator1, typename _ForwardIterator2> 3682 inline bool 3683 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 3684 _ForwardIterator2 __first2, _ForwardIterator2 __last2) 3685 { 3686 __glibcxx_requires_valid_range(__first1, __last1); 3687 __glibcxx_requires_valid_range(__first2, __last2); 3688 3689 return 3690 std::__is_permutation(__first1, __last1, __first2, __last2, 3691 __gnu_cxx::__ops::__iter_equal_to_iter()); 3692 } 3693 3694 /** 3695 * @brief Checks whether a permutation of the second sequence is equal 3696 * to the first sequence. 3697 * @ingroup non_mutating_algorithms 3698 * @param __first1 Start of first range. 3699 * @param __last1 End of first range. 3700 * @param __first2 Start of second range. 3701 * @param __last2 End of first range. 3702 * @param __pred A binary predicate. 3703 * @return true if there exists a permutation of the elements in the range 3704 * [__first2, __last2), beginning with ForwardIterator2 begin, 3705 * such that equal(__first1, __last1, __begin, __pred) returns true; 3706 * otherwise, returns false. 3707 */ 3708 template<typename _ForwardIterator1, typename _ForwardIterator2, 3709 typename _BinaryPredicate> 3710 inline bool 3711 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 3712 _ForwardIterator2 __first2, _ForwardIterator2 __last2, 3713 _BinaryPredicate __pred) 3714 { 3715 __glibcxx_requires_valid_range(__first1, __last1); 3716 __glibcxx_requires_valid_range(__first2, __last2); 3717 3718 return std::__is_permutation(__first1, __last1, __first2, __last2, 3719 __gnu_cxx::__ops::__iter_comp_iter(__pred)); 3720 } 3721 #endif 3722 3723 #ifdef _GLIBCXX_USE_C99_STDINT_TR1 3724 /** 3725 * @brief Shuffle the elements of a sequence using a uniform random 3726 * number generator. 3727 * @ingroup mutating_algorithms 3728 * @param __first A forward iterator. 3729 * @param __last A forward iterator. 3730 * @param __g A UniformRandomNumberGenerator (26.5.1.3). 3731 * @return Nothing. 3732 * 3733 * Reorders the elements in the range @p [__first,__last) using @p __g to 3734 * provide random numbers. 3735 */ 3736 template<typename _RandomAccessIterator, 3737 typename _UniformRandomNumberGenerator> 3738 void 3739 shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last, 3740 _UniformRandomNumberGenerator&& __g) 3741 { 3742 // concept requirements 3743 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 3744 _RandomAccessIterator>) 3745 __glibcxx_requires_valid_range(__first, __last); 3746 3747 if (__first == __last) 3748 return; 3749 3750 typedef typename iterator_traits<_RandomAccessIterator>::difference_type 3751 _DistanceType; 3752 3753 typedef typename std::make_unsigned<_DistanceType>::type __ud_type; 3754 typedef typename std::uniform_int_distribution<__ud_type> __distr_type; 3755 typedef typename __distr_type::param_type __p_type; 3756 __distr_type __d; 3757 3758 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) 3759 std::iter_swap(__i, __first + __d(__g, __p_type(0, __i - __first))); 3760 } 3761 #endif 3762 3763 #endif // C++11 3764 3765 _GLIBCXX_END_NAMESPACE_VERSION 3766 3767 _GLIBCXX_BEGIN_NAMESPACE_ALGO 3768 3769 /** 3770 * @brief Apply a function to every element of a sequence. 3771 * @ingroup non_mutating_algorithms 3772 * @param __first An input iterator. 3773 * @param __last An input iterator. 3774 * @param __f A unary function object. 3775 * @return @p __f (std::move(@p __f) in C++0x). 3776 * 3777 * Applies the function object @p __f to each element in the range 3778 * @p [first,last). @p __f must not modify the order of the sequence. 3779 * If @p __f has a return value it is ignored. 3780 */ 3781 template<typename _InputIterator, typename _Function> 3782 _Function 3783 for_each(_InputIterator __first, _InputIterator __last, _Function __f) 3784 { 3785 // concept requirements 3786 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 3787 __glibcxx_requires_valid_range(__first, __last); 3788 for (; __first != __last; ++__first) 3789 __f(*__first); 3790 return _GLIBCXX_MOVE(__f); 3791 } 3792 3793 /** 3794 * @brief Find the first occurrence of a value in a sequence. 3795 * @ingroup non_mutating_algorithms 3796 * @param __first An input iterator. 3797 * @param __last An input iterator. 3798 * @param __val The value to find. 3799 * @return The first iterator @c i in the range @p [__first,__last) 3800 * such that @c *i == @p __val, or @p __last if no such iterator exists. 3801 */ 3802 template<typename _InputIterator, typename _Tp> 3803 inline _InputIterator 3804 find(_InputIterator __first, _InputIterator __last, 3805 const _Tp& __val) 3806 { 3807 // concept requirements 3808 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 3809 __glibcxx_function_requires(_EqualOpConcept< 3810 typename iterator_traits<_InputIterator>::value_type, _Tp>) 3811 __glibcxx_requires_valid_range(__first, __last); 3812 return std::__find_if(__first, __last, 3813 __gnu_cxx::__ops::__iter_equals_val(__val)); 3814 } 3815 3816 /** 3817 * @brief Find the first element in a sequence for which a 3818 * predicate is true. 3819 * @ingroup non_mutating_algorithms 3820 * @param __first An input iterator. 3821 * @param __last An input iterator. 3822 * @param __pred A predicate. 3823 * @return The first iterator @c i in the range @p [__first,__last) 3824 * such that @p __pred(*i) is true, or @p __last if no such iterator exists. 3825 */ 3826 template<typename _InputIterator, typename _Predicate> 3827 inline _InputIterator 3828 find_if(_InputIterator __first, _InputIterator __last, 3829 _Predicate __pred) 3830 { 3831 // concept requirements 3832 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 3833 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 3834 typename iterator_traits<_InputIterator>::value_type>) 3835 __glibcxx_requires_valid_range(__first, __last); 3836 3837 return std::__find_if(__first, __last, 3838 __gnu_cxx::__ops::__pred_iter(__pred)); 3839 } 3840 3841 /** 3842 * @brief Find element from a set in a sequence. 3843 * @ingroup non_mutating_algorithms 3844 * @param __first1 Start of range to search. 3845 * @param __last1 End of range to search. 3846 * @param __first2 Start of match candidates. 3847 * @param __last2 End of match candidates. 3848 * @return The first iterator @c i in the range 3849 * @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an 3850 * iterator in [__first2,__last2), or @p __last1 if no such iterator exists. 3851 * 3852 * Searches the range @p [__first1,__last1) for an element that is 3853 * equal to some element in the range [__first2,__last2). If 3854 * found, returns an iterator in the range [__first1,__last1), 3855 * otherwise returns @p __last1. 3856 */ 3857 template<typename _InputIterator, typename _ForwardIterator> 3858 _InputIterator 3859 find_first_of(_InputIterator __first1, _InputIterator __last1, 3860 _ForwardIterator __first2, _ForwardIterator __last2) 3861 { 3862 // concept requirements 3863 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 3864 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 3865 __glibcxx_function_requires(_EqualOpConcept< 3866 typename iterator_traits<_InputIterator>::value_type, 3867 typename iterator_traits<_ForwardIterator>::value_type>) 3868 __glibcxx_requires_valid_range(__first1, __last1); 3869 __glibcxx_requires_valid_range(__first2, __last2); 3870 3871 for (; __first1 != __last1; ++__first1) 3872 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter) 3873 if (*__first1 == *__iter) 3874 return __first1; 3875 return __last1; 3876 } 3877 3878 /** 3879 * @brief Find element from a set in a sequence using a predicate. 3880 * @ingroup non_mutating_algorithms 3881 * @param __first1 Start of range to search. 3882 * @param __last1 End of range to search. 3883 * @param __first2 Start of match candidates. 3884 * @param __last2 End of match candidates. 3885 * @param __comp Predicate to use. 3886 * @return The first iterator @c i in the range 3887 * @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true 3888 * and i2 is an iterator in [__first2,__last2), or @p __last1 if no 3889 * such iterator exists. 3890 * 3891 3892 * Searches the range @p [__first1,__last1) for an element that is 3893 * equal to some element in the range [__first2,__last2). If 3894 * found, returns an iterator in the range [__first1,__last1), 3895 * otherwise returns @p __last1. 3896 */ 3897 template<typename _InputIterator, typename _ForwardIterator, 3898 typename _BinaryPredicate> 3899 _InputIterator 3900 find_first_of(_InputIterator __first1, _InputIterator __last1, 3901 _ForwardIterator __first2, _ForwardIterator __last2, 3902 _BinaryPredicate __comp) 3903 { 3904 // concept requirements 3905 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 3906 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 3907 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, 3908 typename iterator_traits<_InputIterator>::value_type, 3909 typename iterator_traits<_ForwardIterator>::value_type>) 3910 __glibcxx_requires_valid_range(__first1, __last1); 3911 __glibcxx_requires_valid_range(__first2, __last2); 3912 3913 for (; __first1 != __last1; ++__first1) 3914 for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter) 3915 if (__comp(*__first1, *__iter)) 3916 return __first1; 3917 return __last1; 3918 } 3919 3920 /** 3921 * @brief Find two adjacent values in a sequence that are equal. 3922 * @ingroup non_mutating_algorithms 3923 * @param __first A forward iterator. 3924 * @param __last A forward iterator. 3925 * @return The first iterator @c i such that @c i and @c i+1 are both 3926 * valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1), 3927 * or @p __last if no such iterator exists. 3928 */ 3929 template<typename _ForwardIterator> 3930 inline _ForwardIterator 3931 adjacent_find(_ForwardIterator __first, _ForwardIterator __last) 3932 { 3933 // concept requirements 3934 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 3935 __glibcxx_function_requires(_EqualityComparableConcept< 3936 typename iterator_traits<_ForwardIterator>::value_type>) 3937 __glibcxx_requires_valid_range(__first, __last); 3938 3939 return std::__adjacent_find(__first, __last, 3940 __gnu_cxx::__ops::__iter_equal_to_iter()); 3941 } 3942 3943 /** 3944 * @brief Find two adjacent values in a sequence using a predicate. 3945 * @ingroup non_mutating_algorithms 3946 * @param __first A forward iterator. 3947 * @param __last A forward iterator. 3948 * @param __binary_pred A binary predicate. 3949 * @return The first iterator @c i such that @c i and @c i+1 are both 3950 * valid iterators in @p [__first,__last) and such that 3951 * @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator 3952 * exists. 3953 */ 3954 template<typename _ForwardIterator, typename _BinaryPredicate> 3955 inline _ForwardIterator 3956 adjacent_find(_ForwardIterator __first, _ForwardIterator __last, 3957 _BinaryPredicate __binary_pred) 3958 { 3959 // concept requirements 3960 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 3961 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, 3962 typename iterator_traits<_ForwardIterator>::value_type, 3963 typename iterator_traits<_ForwardIterator>::value_type>) 3964 __glibcxx_requires_valid_range(__first, __last); 3965 3966 return std::__adjacent_find(__first, __last, 3967 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred)); 3968 } 3969 3970 /** 3971 * @brief Count the number of copies of a value in a sequence. 3972 * @ingroup non_mutating_algorithms 3973 * @param __first An input iterator. 3974 * @param __last An input iterator. 3975 * @param __value The value to be counted. 3976 * @return The number of iterators @c i in the range @p [__first,__last) 3977 * for which @c *i == @p __value 3978 */ 3979 template<typename _InputIterator, typename _Tp> 3980 inline typename iterator_traits<_InputIterator>::difference_type 3981 count(_InputIterator __first, _InputIterator __last, const _Tp& __value) 3982 { 3983 // concept requirements 3984 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 3985 __glibcxx_function_requires(_EqualOpConcept< 3986 typename iterator_traits<_InputIterator>::value_type, _Tp>) 3987 __glibcxx_requires_valid_range(__first, __last); 3988 3989 return std::__count_if(__first, __last, 3990 __gnu_cxx::__ops::__iter_equals_val(__value)); 3991 } 3992 3993 /** 3994 * @brief Count the elements of a sequence for which a predicate is true. 3995 * @ingroup non_mutating_algorithms 3996 * @param __first An input iterator. 3997 * @param __last An input iterator. 3998 * @param __pred A predicate. 3999 * @return The number of iterators @c i in the range @p [__first,__last) 4000 * for which @p __pred(*i) is true. 4001 */ 4002 template<typename _InputIterator, typename _Predicate> 4003 inline typename iterator_traits<_InputIterator>::difference_type 4004 count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) 4005 { 4006 // concept requirements 4007 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 4008 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 4009 typename iterator_traits<_InputIterator>::value_type>) 4010 __glibcxx_requires_valid_range(__first, __last); 4011 4012 return std::__count_if(__first, __last, 4013 __gnu_cxx::__ops::__pred_iter(__pred)); 4014 } 4015 4016 /** 4017 * @brief Search a sequence for a matching sub-sequence. 4018 * @ingroup non_mutating_algorithms 4019 * @param __first1 A forward iterator. 4020 * @param __last1 A forward iterator. 4021 * @param __first2 A forward iterator. 4022 * @param __last2 A forward iterator. 4023 * @return The first iterator @c i in the range @p 4024 * [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p 4025 * *(__first2+N) for each @c N in the range @p 4026 * [0,__last2-__first2), or @p __last1 if no such iterator exists. 4027 * 4028 * Searches the range @p [__first1,__last1) for a sub-sequence that 4029 * compares equal value-by-value with the sequence given by @p 4030 * [__first2,__last2) and returns an iterator to the first element 4031 * of the sub-sequence, or @p __last1 if the sub-sequence is not 4032 * found. 4033 * 4034 * Because the sub-sequence must lie completely within the range @p 4035 * [__first1,__last1) it must start at a position less than @p 4036 * __last1-(__last2-__first2) where @p __last2-__first2 is the 4037 * length of the sub-sequence. 4038 * 4039 * This means that the returned iterator @c i will be in the range 4040 * @p [__first1,__last1-(__last2-__first2)) 4041 */ 4042 template<typename _ForwardIterator1, typename _ForwardIterator2> 4043 inline _ForwardIterator1 4044 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 4045 _ForwardIterator2 __first2, _ForwardIterator2 __last2) 4046 { 4047 // concept requirements 4048 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) 4049 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) 4050 __glibcxx_function_requires(_EqualOpConcept< 4051 typename iterator_traits<_ForwardIterator1>::value_type, 4052 typename iterator_traits<_ForwardIterator2>::value_type>) 4053 __glibcxx_requires_valid_range(__first1, __last1); 4054 __glibcxx_requires_valid_range(__first2, __last2); 4055 4056 return std::__search(__first1, __last1, __first2, __last2, 4057 __gnu_cxx::__ops::__iter_equal_to_iter()); 4058 } 4059 4060 /** 4061 * @brief Search a sequence for a matching sub-sequence using a predicate. 4062 * @ingroup non_mutating_algorithms 4063 * @param __first1 A forward iterator. 4064 * @param __last1 A forward iterator. 4065 * @param __first2 A forward iterator. 4066 * @param __last2 A forward iterator. 4067 * @param __predicate A binary predicate. 4068 * @return The first iterator @c i in the range 4069 * @p [__first1,__last1-(__last2-__first2)) such that 4070 * @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range 4071 * @p [0,__last2-__first2), or @p __last1 if no such iterator exists. 4072 * 4073 * Searches the range @p [__first1,__last1) for a sub-sequence that 4074 * compares equal value-by-value with the sequence given by @p 4075 * [__first2,__last2), using @p __predicate to determine equality, 4076 * and returns an iterator to the first element of the 4077 * sub-sequence, or @p __last1 if no such iterator exists. 4078 * 4079 * @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2) 4080 */ 4081 template<typename _ForwardIterator1, typename _ForwardIterator2, 4082 typename _BinaryPredicate> 4083 inline _ForwardIterator1 4084 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 4085 _ForwardIterator2 __first2, _ForwardIterator2 __last2, 4086 _BinaryPredicate __predicate) 4087 { 4088 // concept requirements 4089 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) 4090 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) 4091 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, 4092 typename iterator_traits<_ForwardIterator1>::value_type, 4093 typename iterator_traits<_ForwardIterator2>::value_type>) 4094 __glibcxx_requires_valid_range(__first1, __last1); 4095 __glibcxx_requires_valid_range(__first2, __last2); 4096 4097 return std::__search(__first1, __last1, __first2, __last2, 4098 __gnu_cxx::__ops::__iter_comp_iter(__predicate)); 4099 } 4100 4101 /** 4102 * @brief Search a sequence for a number of consecutive values. 4103 * @ingroup non_mutating_algorithms 4104 * @param __first A forward iterator. 4105 * @param __last A forward iterator. 4106 * @param __count The number of consecutive values. 4107 * @param __val The value to find. 4108 * @return The first iterator @c i in the range @p 4109 * [__first,__last-__count) such that @c *(i+N) == @p __val for 4110 * each @c N in the range @p [0,__count), or @p __last if no such 4111 * iterator exists. 4112 * 4113 * Searches the range @p [__first,__last) for @p count consecutive elements 4114 * equal to @p __val. 4115 */ 4116 template<typename _ForwardIterator, typename _Integer, typename _Tp> 4117 inline _ForwardIterator 4118 search_n(_ForwardIterator __first, _ForwardIterator __last, 4119 _Integer __count, const _Tp& __val) 4120 { 4121 // concept requirements 4122 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 4123 __glibcxx_function_requires(_EqualOpConcept< 4124 typename iterator_traits<_ForwardIterator>::value_type, _Tp>) 4125 __glibcxx_requires_valid_range(__first, __last); 4126 4127 return std::__search_n(__first, __last, __count, 4128 __gnu_cxx::__ops::__iter_equals_val(__val)); 4129 } 4130 4131 4132 /** 4133 * @brief Search a sequence for a number of consecutive values using a 4134 * predicate. 4135 * @ingroup non_mutating_algorithms 4136 * @param __first A forward iterator. 4137 * @param __last A forward iterator. 4138 * @param __count The number of consecutive values. 4139 * @param __val The value to find. 4140 * @param __binary_pred A binary predicate. 4141 * @return The first iterator @c i in the range @p 4142 * [__first,__last-__count) such that @p 4143 * __binary_pred(*(i+N),__val) is true for each @c N in the range 4144 * @p [0,__count), or @p __last if no such iterator exists. 4145 * 4146 * Searches the range @p [__first,__last) for @p __count 4147 * consecutive elements for which the predicate returns true. 4148 */ 4149 template<typename _ForwardIterator, typename _Integer, typename _Tp, 4150 typename _BinaryPredicate> 4151 inline _ForwardIterator 4152 search_n(_ForwardIterator __first, _ForwardIterator __last, 4153 _Integer __count, const _Tp& __val, 4154 _BinaryPredicate __binary_pred) 4155 { 4156 // concept requirements 4157 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 4158 __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, 4159 typename iterator_traits<_ForwardIterator>::value_type, _Tp>) 4160 __glibcxx_requires_valid_range(__first, __last); 4161 4162 return std::__search_n(__first, __last, __count, 4163 __gnu_cxx::__ops::__iter_comp_val(__binary_pred, __val)); 4164 } 4165 4166 4167 /** 4168 * @brief Perform an operation on a sequence. 4169 * @ingroup mutating_algorithms 4170 * @param __first An input iterator. 4171 * @param __last An input iterator. 4172 * @param __result An output iterator. 4173 * @param __unary_op A unary operator. 4174 * @return An output iterator equal to @p __result+(__last-__first). 4175 * 4176 * Applies the operator to each element in the input range and assigns 4177 * the results to successive elements of the output sequence. 4178 * Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the 4179 * range @p [0,__last-__first). 4180 * 4181 * @p unary_op must not alter its argument. 4182 */ 4183 template<typename _InputIterator, typename _OutputIterator, 4184 typename _UnaryOperation> 4185 _OutputIterator 4186 transform(_InputIterator __first, _InputIterator __last, 4187 _OutputIterator __result, _UnaryOperation __unary_op) 4188 { 4189 // concept requirements 4190 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 4191 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 4192 // "the type returned by a _UnaryOperation" 4193 __typeof__(__unary_op(*__first))>) 4194 __glibcxx_requires_valid_range(__first, __last); 4195 4196 for (; __first != __last; ++__first, ++__result) 4197 *__result = __unary_op(*__first); 4198 return __result; 4199 } 4200 4201 /** 4202 * @brief Perform an operation on corresponding elements of two sequences. 4203 * @ingroup mutating_algorithms 4204 * @param __first1 An input iterator. 4205 * @param __last1 An input iterator. 4206 * @param __first2 An input iterator. 4207 * @param __result An output iterator. 4208 * @param __binary_op A binary operator. 4209 * @return An output iterator equal to @p result+(last-first). 4210 * 4211 * Applies the operator to the corresponding elements in the two 4212 * input ranges and assigns the results to successive elements of the 4213 * output sequence. 4214 * Evaluates @p 4215 * *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each 4216 * @c N in the range @p [0,__last1-__first1). 4217 * 4218 * @p binary_op must not alter either of its arguments. 4219 */ 4220 template<typename _InputIterator1, typename _InputIterator2, 4221 typename _OutputIterator, typename _BinaryOperation> 4222 _OutputIterator 4223 transform(_InputIterator1 __first1, _InputIterator1 __last1, 4224 _InputIterator2 __first2, _OutputIterator __result, 4225 _BinaryOperation __binary_op) 4226 { 4227 // concept requirements 4228 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 4229 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 4230 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 4231 // "the type returned by a _BinaryOperation" 4232 __typeof__(__binary_op(*__first1,*__first2))>) 4233 __glibcxx_requires_valid_range(__first1, __last1); 4234 4235 for (; __first1 != __last1; ++__first1, ++__first2, ++__result) 4236 *__result = __binary_op(*__first1, *__first2); 4237 return __result; 4238 } 4239 4240 /** 4241 * @brief Replace each occurrence of one value in a sequence with another 4242 * value. 4243 * @ingroup mutating_algorithms 4244 * @param __first A forward iterator. 4245 * @param __last A forward iterator. 4246 * @param __old_value The value to be replaced. 4247 * @param __new_value The replacement value. 4248 * @return replace() returns no value. 4249 * 4250 * For each iterator @c i in the range @p [__first,__last) if @c *i == 4251 * @p __old_value then the assignment @c *i = @p __new_value is performed. 4252 */ 4253 template<typename _ForwardIterator, typename _Tp> 4254 void 4255 replace(_ForwardIterator __first, _ForwardIterator __last, 4256 const _Tp& __old_value, const _Tp& __new_value) 4257 { 4258 // concept requirements 4259 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 4260 _ForwardIterator>) 4261 __glibcxx_function_requires(_EqualOpConcept< 4262 typename iterator_traits<_ForwardIterator>::value_type, _Tp>) 4263 __glibcxx_function_requires(_ConvertibleConcept<_Tp, 4264 typename iterator_traits<_ForwardIterator>::value_type>) 4265 __glibcxx_requires_valid_range(__first, __last); 4266 4267 for (; __first != __last; ++__first) 4268 if (*__first == __old_value) 4269 *__first = __new_value; 4270 } 4271 4272 /** 4273 * @brief Replace each value in a sequence for which a predicate returns 4274 * true with another value. 4275 * @ingroup mutating_algorithms 4276 * @param __first A forward iterator. 4277 * @param __last A forward iterator. 4278 * @param __pred A predicate. 4279 * @param __new_value The replacement value. 4280 * @return replace_if() returns no value. 4281 * 4282 * For each iterator @c i in the range @p [__first,__last) if @p __pred(*i) 4283 * is true then the assignment @c *i = @p __new_value is performed. 4284 */ 4285 template<typename _ForwardIterator, typename _Predicate, typename _Tp> 4286 void 4287 replace_if(_ForwardIterator __first, _ForwardIterator __last, 4288 _Predicate __pred, const _Tp& __new_value) 4289 { 4290 // concept requirements 4291 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 4292 _ForwardIterator>) 4293 __glibcxx_function_requires(_ConvertibleConcept<_Tp, 4294 typename iterator_traits<_ForwardIterator>::value_type>) 4295 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 4296 typename iterator_traits<_ForwardIterator>::value_type>) 4297 __glibcxx_requires_valid_range(__first, __last); 4298 4299 for (; __first != __last; ++__first) 4300 if (__pred(*__first)) 4301 *__first = __new_value; 4302 } 4303 4304 /** 4305 * @brief Assign the result of a function object to each value in a 4306 * sequence. 4307 * @ingroup mutating_algorithms 4308 * @param __first A forward iterator. 4309 * @param __last A forward iterator. 4310 * @param __gen A function object taking no arguments and returning 4311 * std::iterator_traits<_ForwardIterator>::value_type 4312 * @return generate() returns no value. 4313 * 4314 * Performs the assignment @c *i = @p __gen() for each @c i in the range 4315 * @p [__first,__last). 4316 */ 4317 template<typename _ForwardIterator, typename _Generator> 4318 void 4319 generate(_ForwardIterator __first, _ForwardIterator __last, 4320 _Generator __gen) 4321 { 4322 // concept requirements 4323 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 4324 __glibcxx_function_requires(_GeneratorConcept<_Generator, 4325 typename iterator_traits<_ForwardIterator>::value_type>) 4326 __glibcxx_requires_valid_range(__first, __last); 4327 4328 for (; __first != __last; ++__first) 4329 *__first = __gen(); 4330 } 4331 4332 /** 4333 * @brief Assign the result of a function object to each value in a 4334 * sequence. 4335 * @ingroup mutating_algorithms 4336 * @param __first A forward iterator. 4337 * @param __n The length of the sequence. 4338 * @param __gen A function object taking no arguments and returning 4339 * std::iterator_traits<_ForwardIterator>::value_type 4340 * @return The end of the sequence, @p __first+__n 4341 * 4342 * Performs the assignment @c *i = @p __gen() for each @c i in the range 4343 * @p [__first,__first+__n). 4344 * 4345 * _GLIBCXX_RESOLVE_LIB_DEFECTS 4346 * DR 865. More algorithms that throw away information 4347 */ 4348 template<typename _OutputIterator, typename _Size, typename _Generator> 4349 _OutputIterator 4350 generate_n(_OutputIterator __first, _Size __n, _Generator __gen) 4351 { 4352 // concept requirements 4353 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 4354 // "the type returned by a _Generator" 4355 __typeof__(__gen())>) 4356 4357 for (__decltype(__n + 0) __niter = __n; 4358 __niter > 0; --__niter, ++__first) 4359 *__first = __gen(); 4360 return __first; 4361 } 4362 4363 /** 4364 * @brief Copy a sequence, removing consecutive duplicate values. 4365 * @ingroup mutating_algorithms 4366 * @param __first An input iterator. 4367 * @param __last An input iterator. 4368 * @param __result An output iterator. 4369 * @return An iterator designating the end of the resulting sequence. 4370 * 4371 * Copies each element in the range @p [__first,__last) to the range 4372 * beginning at @p __result, except that only the first element is copied 4373 * from groups of consecutive elements that compare equal. 4374 * unique_copy() is stable, so the relative order of elements that are 4375 * copied is unchanged. 4376 * 4377 * _GLIBCXX_RESOLVE_LIB_DEFECTS 4378 * DR 241. Does unique_copy() require CopyConstructible and Assignable? 4379 * 4380 * _GLIBCXX_RESOLVE_LIB_DEFECTS 4381 * DR 538. 241 again: Does unique_copy() require CopyConstructible and 4382 * Assignable? 4383 */ 4384 template<typename _InputIterator, typename _OutputIterator> 4385 inline _OutputIterator 4386 unique_copy(_InputIterator __first, _InputIterator __last, 4387 _OutputIterator __result) 4388 { 4389 // concept requirements 4390 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 4391 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 4392 typename iterator_traits<_InputIterator>::value_type>) 4393 __glibcxx_function_requires(_EqualityComparableConcept< 4394 typename iterator_traits<_InputIterator>::value_type>) 4395 __glibcxx_requires_valid_range(__first, __last); 4396 4397 if (__first == __last) 4398 return __result; 4399 return std::__unique_copy(__first, __last, __result, 4400 __gnu_cxx::__ops::__iter_equal_to_iter(), 4401 std::__iterator_category(__first), 4402 std::__iterator_category(__result)); 4403 } 4404 4405 /** 4406 * @brief Copy a sequence, removing consecutive values using a predicate. 4407 * @ingroup mutating_algorithms 4408 * @param __first An input iterator. 4409 * @param __last An input iterator. 4410 * @param __result An output iterator. 4411 * @param __binary_pred A binary predicate. 4412 * @return An iterator designating the end of the resulting sequence. 4413 * 4414 * Copies each element in the range @p [__first,__last) to the range 4415 * beginning at @p __result, except that only the first element is copied 4416 * from groups of consecutive elements for which @p __binary_pred returns 4417 * true. 4418 * unique_copy() is stable, so the relative order of elements that are 4419 * copied is unchanged. 4420 * 4421 * _GLIBCXX_RESOLVE_LIB_DEFECTS 4422 * DR 241. Does unique_copy() require CopyConstructible and Assignable? 4423 */ 4424 template<typename _InputIterator, typename _OutputIterator, 4425 typename _BinaryPredicate> 4426 inline _OutputIterator 4427 unique_copy(_InputIterator __first, _InputIterator __last, 4428 _OutputIterator __result, 4429 _BinaryPredicate __binary_pred) 4430 { 4431 // concept requirements -- predicates checked later 4432 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 4433 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 4434 typename iterator_traits<_InputIterator>::value_type>) 4435 __glibcxx_requires_valid_range(__first, __last); 4436 4437 if (__first == __last) 4438 return __result; 4439 return std::__unique_copy(__first, __last, __result, 4440 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred), 4441 std::__iterator_category(__first), 4442 std::__iterator_category(__result)); 4443 } 4444 4445 /** 4446 * @brief Randomly shuffle the elements of a sequence. 4447 * @ingroup mutating_algorithms 4448 * @param __first A forward iterator. 4449 * @param __last A forward iterator. 4450 * @return Nothing. 4451 * 4452 * Reorder the elements in the range @p [__first,__last) using a random 4453 * distribution, so that every possible ordering of the sequence is 4454 * equally likely. 4455 */ 4456 template<typename _RandomAccessIterator> 4457 inline void 4458 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last) 4459 { 4460 // concept requirements 4461 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 4462 _RandomAccessIterator>) 4463 __glibcxx_requires_valid_range(__first, __last); 4464 4465 if (__first != __last) 4466 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) 4467 std::iter_swap(__i, __first + (std::rand() % ((__i - __first) + 1))); 4468 } 4469 4470 /** 4471 * @brief Shuffle the elements of a sequence using a random number 4472 * generator. 4473 * @ingroup mutating_algorithms 4474 * @param __first A forward iterator. 4475 * @param __last A forward iterator. 4476 * @param __rand The RNG functor or function. 4477 * @return Nothing. 4478 * 4479 * Reorders the elements in the range @p [__first,__last) using @p __rand to 4480 * provide a random distribution. Calling @p __rand(N) for a positive 4481 * integer @p N should return a randomly chosen integer from the 4482 * range [0,N). 4483 */ 4484 template<typename _RandomAccessIterator, typename _RandomNumberGenerator> 4485 void 4486 random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last, 4487 #if __cplusplus >= 201103L 4488 _RandomNumberGenerator&& __rand) 4489 #else 4490 _RandomNumberGenerator& __rand) 4491 #endif 4492 { 4493 // concept requirements 4494 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 4495 _RandomAccessIterator>) 4496 __glibcxx_requires_valid_range(__first, __last); 4497 4498 if (__first == __last) 4499 return; 4500 for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) 4501 std::iter_swap(__i, __first + __rand((__i - __first) + 1)); 4502 } 4503 4504 4505 /** 4506 * @brief Move elements for which a predicate is true to the beginning 4507 * of a sequence. 4508 * @ingroup mutating_algorithms 4509 * @param __first A forward iterator. 4510 * @param __last A forward iterator. 4511 * @param __pred A predicate functor. 4512 * @return An iterator @p middle such that @p __pred(i) is true for each 4513 * iterator @p i in the range @p [__first,middle) and false for each @p i 4514 * in the range @p [middle,__last). 4515 * 4516 * @p __pred must not modify its operand. @p partition() does not preserve 4517 * the relative ordering of elements in each group, use 4518 * @p stable_partition() if this is needed. 4519 */ 4520 template<typename _ForwardIterator, typename _Predicate> 4521 inline _ForwardIterator 4522 partition(_ForwardIterator __first, _ForwardIterator __last, 4523 _Predicate __pred) 4524 { 4525 // concept requirements 4526 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< 4527 _ForwardIterator>) 4528 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, 4529 typename iterator_traits<_ForwardIterator>::value_type>) 4530 __glibcxx_requires_valid_range(__first, __last); 4531 4532 return std::__partition(__first, __last, __pred, 4533 std::__iterator_category(__first)); 4534 } 4535 4536 4537 /** 4538 * @brief Sort the smallest elements of a sequence. 4539 * @ingroup sorting_algorithms 4540 * @param __first An iterator. 4541 * @param __middle Another iterator. 4542 * @param __last Another iterator. 4543 * @return Nothing. 4544 * 4545 * Sorts the smallest @p (__middle-__first) elements in the range 4546 * @p [first,last) and moves them to the range @p [__first,__middle). The 4547 * order of the remaining elements in the range @p [__middle,__last) is 4548 * undefined. 4549 * After the sort if @e i and @e j are iterators in the range 4550 * @p [__first,__middle) such that i precedes j and @e k is an iterator in 4551 * the range @p [__middle,__last) then *j<*i and *k<*i are both false. 4552 */ 4553 template<typename _RandomAccessIterator> 4554 inline void 4555 partial_sort(_RandomAccessIterator __first, 4556 _RandomAccessIterator __middle, 4557 _RandomAccessIterator __last) 4558 { 4559 // concept requirements 4560 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 4561 _RandomAccessIterator>) 4562 __glibcxx_function_requires(_LessThanComparableConcept< 4563 typename iterator_traits<_RandomAccessIterator>::value_type>) 4564 __glibcxx_requires_valid_range(__first, __middle); 4565 __glibcxx_requires_valid_range(__middle, __last); 4566 4567 std::__partial_sort(__first, __middle, __last, 4568 __gnu_cxx::__ops::__iter_less_iter()); 4569 } 4570 4571 /** 4572 * @brief Sort the smallest elements of a sequence using a predicate 4573 * for comparison. 4574 * @ingroup sorting_algorithms 4575 * @param __first An iterator. 4576 * @param __middle Another iterator. 4577 * @param __last Another iterator. 4578 * @param __comp A comparison functor. 4579 * @return Nothing. 4580 * 4581 * Sorts the smallest @p (__middle-__first) elements in the range 4582 * @p [__first,__last) and moves them to the range @p [__first,__middle). The 4583 * order of the remaining elements in the range @p [__middle,__last) is 4584 * undefined. 4585 * After the sort if @e i and @e j are iterators in the range 4586 * @p [__first,__middle) such that i precedes j and @e k is an iterator in 4587 * the range @p [__middle,__last) then @p *__comp(j,*i) and @p __comp(*k,*i) 4588 * are both false. 4589 */ 4590 template<typename _RandomAccessIterator, typename _Compare> 4591 inline void 4592 partial_sort(_RandomAccessIterator __first, 4593 _RandomAccessIterator __middle, 4594 _RandomAccessIterator __last, 4595 _Compare __comp) 4596 { 4597 // concept requirements 4598 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 4599 _RandomAccessIterator>) 4600 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 4601 typename iterator_traits<_RandomAccessIterator>::value_type, 4602 typename iterator_traits<_RandomAccessIterator>::value_type>) 4603 __glibcxx_requires_valid_range(__first, __middle); 4604 __glibcxx_requires_valid_range(__middle, __last); 4605 4606 std::__partial_sort(__first, __middle, __last, 4607 __gnu_cxx::__ops::__iter_comp_iter(__CheckedCompare(__comp))); 4608 } 4609 4610 /** 4611 * @brief Sort a sequence just enough to find a particular position. 4612 * @ingroup sorting_algorithms 4613 * @param __first An iterator. 4614 * @param __nth Another iterator. 4615 * @param __last Another iterator. 4616 * @return Nothing. 4617 * 4618 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth 4619 * is the same element that would have been in that position had the 4620 * whole sequence been sorted. The elements either side of @p *__nth are 4621 * not completely sorted, but for any iterator @e i in the range 4622 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it 4623 * holds that *j < *i is false. 4624 */ 4625 template<typename _RandomAccessIterator> 4626 inline void 4627 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, 4628 _RandomAccessIterator __last) 4629 { 4630 // concept requirements 4631 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 4632 _RandomAccessIterator>) 4633 __glibcxx_function_requires(_LessThanComparableConcept< 4634 typename iterator_traits<_RandomAccessIterator>::value_type>) 4635 __glibcxx_requires_valid_range(__first, __nth); 4636 __glibcxx_requires_valid_range(__nth, __last); 4637 4638 if (__first == __last || __nth == __last) 4639 return; 4640 4641 std::__introselect(__first, __nth, __last, 4642 std::__lg(__last - __first) * 2, 4643 __gnu_cxx::__ops::__iter_less_iter()); 4644 } 4645 4646 /** 4647 * @brief Sort a sequence just enough to find a particular position 4648 * using a predicate for comparison. 4649 * @ingroup sorting_algorithms 4650 * @param __first An iterator. 4651 * @param __nth Another iterator. 4652 * @param __last Another iterator. 4653 * @param __comp A comparison functor. 4654 * @return Nothing. 4655 * 4656 * Rearranges the elements in the range @p [__first,__last) so that @p *__nth 4657 * is the same element that would have been in that position had the 4658 * whole sequence been sorted. The elements either side of @p *__nth are 4659 * not completely sorted, but for any iterator @e i in the range 4660 * @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it 4661 * holds that @p __comp(*j,*i) is false. 4662 */ 4663 template<typename _RandomAccessIterator, typename _Compare> 4664 inline void 4665 nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, 4666 _RandomAccessIterator __last, _Compare __comp) 4667 { 4668 // concept requirements 4669 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 4670 _RandomAccessIterator>) 4671 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 4672 typename iterator_traits<_RandomAccessIterator>::value_type, 4673 typename iterator_traits<_RandomAccessIterator>::value_type>) 4674 __glibcxx_requires_valid_range(__first, __nth); 4675 __glibcxx_requires_valid_range(__nth, __last); 4676 4677 if (__first == __last || __nth == __last) 4678 return; 4679 4680 std::__introselect(__first, __nth, __last, 4681 std::__lg(__last - __first) * 2, 4682 __gnu_cxx::__ops::__iter_comp_iter(__CheckedCompare(__comp))); 4683 } 4684 4685 /** 4686 * @brief Sort the elements of a sequence. 4687 * @ingroup sorting_algorithms 4688 * @param __first An iterator. 4689 * @param __last Another iterator. 4690 * @return Nothing. 4691 * 4692 * Sorts the elements in the range @p [__first,__last) in ascending order, 4693 * such that for each iterator @e i in the range @p [__first,__last-1), 4694 * *(i+1)<*i is false. 4695 * 4696 * The relative ordering of equivalent elements is not preserved, use 4697 * @p stable_sort() if this is needed. 4698 */ 4699 template<typename _RandomAccessIterator> 4700 inline void 4701 sort(_RandomAccessIterator __first, _RandomAccessIterator __last) 4702 { 4703 // concept requirements 4704 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 4705 _RandomAccessIterator>) 4706 __glibcxx_function_requires(_LessThanComparableConcept< 4707 typename iterator_traits<_RandomAccessIterator>::value_type>) 4708 __glibcxx_requires_valid_range(__first, __last); 4709 4710 std::__sort(__first, __last, __gnu_cxx::__ops::__iter_less_iter()); 4711 } 4712 4713 /** 4714 * @brief Sort the elements of a sequence using a predicate for comparison. 4715 * @ingroup sorting_algorithms 4716 * @param __first An iterator. 4717 * @param __last Another iterator. 4718 * @param __comp A comparison functor. 4719 * @return Nothing. 4720 * 4721 * Sorts the elements in the range @p [__first,__last) in ascending order, 4722 * such that @p __comp(*(i+1),*i) is false for every iterator @e i in the 4723 * range @p [__first,__last-1). 4724 * 4725 * The relative ordering of equivalent elements is not preserved, use 4726 * @p stable_sort() if this is needed. 4727 */ 4728 template<typename _RandomAccessIterator, typename _Compare> 4729 inline void 4730 sort(_RandomAccessIterator __first, _RandomAccessIterator __last, 4731 _Compare __comp) 4732 { 4733 // concept requirements 4734 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 4735 _RandomAccessIterator>) 4736 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 4737 typename iterator_traits<_RandomAccessIterator>::value_type, 4738 typename iterator_traits<_RandomAccessIterator>::value_type>) 4739 __glibcxx_requires_valid_range(__first, __last); 4740 4741 std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__CheckedCompare(__comp))); 4742 } 4743 4744 template<typename _InputIterator1, typename _InputIterator2, 4745 typename _OutputIterator, typename _Compare> 4746 _OutputIterator 4747 __merge(_InputIterator1 __first1, _InputIterator1 __last1, 4748 _InputIterator2 __first2, _InputIterator2 __last2, 4749 _OutputIterator __result, _Compare __comp) 4750 { 4751 while (__first1 != __last1 && __first2 != __last2) 4752 { 4753 if (__comp(__first2, __first1)) 4754 { 4755 *__result = *__first2; 4756 ++__first2; 4757 } 4758 else 4759 { 4760 *__result = *__first1; 4761 ++__first1; 4762 } 4763 ++__result; 4764 } 4765 return std::copy(__first2, __last2, 4766 std::copy(__first1, __last1, __result)); 4767 } 4768 4769 /** 4770 * @brief Merges two sorted ranges. 4771 * @ingroup sorting_algorithms 4772 * @param __first1 An iterator. 4773 * @param __first2 Another iterator. 4774 * @param __last1 Another iterator. 4775 * @param __last2 Another iterator. 4776 * @param __result An iterator pointing to the end of the merged range. 4777 * @return An iterator pointing to the first element <em>not less 4778 * than</em> @e val. 4779 * 4780 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into 4781 * the sorted range @p [__result, __result + (__last1-__first1) + 4782 * (__last2-__first2)). Both input ranges must be sorted, and the 4783 * output range must not overlap with either of the input ranges. 4784 * The sort is @e stable, that is, for equivalent elements in the 4785 * two ranges, elements from the first range will always come 4786 * before elements from the second. 4787 */ 4788 template<typename _InputIterator1, typename _InputIterator2, 4789 typename _OutputIterator> 4790 inline _OutputIterator 4791 merge(_InputIterator1 __first1, _InputIterator1 __last1, 4792 _InputIterator2 __first2, _InputIterator2 __last2, 4793 _OutputIterator __result) 4794 { 4795 // concept requirements 4796 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 4797 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 4798 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 4799 typename iterator_traits<_InputIterator1>::value_type>) 4800 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 4801 typename iterator_traits<_InputIterator2>::value_type>) 4802 __glibcxx_function_requires(_LessThanOpConcept< 4803 typename iterator_traits<_InputIterator2>::value_type, 4804 typename iterator_traits<_InputIterator1>::value_type>) 4805 __glibcxx_requires_sorted_set(__first1, __last1, __first2); 4806 __glibcxx_requires_sorted_set(__first2, __last2, __first1); 4807 4808 return _GLIBCXX_STD_A::__merge(__first1, __last1, 4809 __first2, __last2, __result, 4810 __gnu_cxx::__ops::__iter_less_iter()); 4811 } 4812 4813 /** 4814 * @brief Merges two sorted ranges. 4815 * @ingroup sorting_algorithms 4816 * @param __first1 An iterator. 4817 * @param __first2 Another iterator. 4818 * @param __last1 Another iterator. 4819 * @param __last2 Another iterator. 4820 * @param __result An iterator pointing to the end of the merged range. 4821 * @param __comp A functor to use for comparisons. 4822 * @return An iterator pointing to the first element "not less 4823 * than" @e val. 4824 * 4825 * Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into 4826 * the sorted range @p [__result, __result + (__last1-__first1) + 4827 * (__last2-__first2)). Both input ranges must be sorted, and the 4828 * output range must not overlap with either of the input ranges. 4829 * The sort is @e stable, that is, for equivalent elements in the 4830 * two ranges, elements from the first range will always come 4831 * before elements from the second. 4832 * 4833 * The comparison function should have the same effects on ordering as 4834 * the function used for the initial sort. 4835 */ 4836 template<typename _InputIterator1, typename _InputIterator2, 4837 typename _OutputIterator, typename _Compare> 4838 inline _OutputIterator 4839 merge(_InputIterator1 __first1, _InputIterator1 __last1, 4840 _InputIterator2 __first2, _InputIterator2 __last2, 4841 _OutputIterator __result, _Compare __comp) 4842 { 4843 // concept requirements 4844 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 4845 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 4846 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 4847 typename iterator_traits<_InputIterator1>::value_type>) 4848 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 4849 typename iterator_traits<_InputIterator2>::value_type>) 4850 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 4851 typename iterator_traits<_InputIterator2>::value_type, 4852 typename iterator_traits<_InputIterator1>::value_type>) 4853 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); 4854 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); 4855 4856 return _GLIBCXX_STD_A::__merge(__first1, __last1, 4857 __first2, __last2, __result, 4858 __gnu_cxx::__ops::__iter_comp_iter(__CheckedCompare(__comp))); 4859 } 4860 4861 template<typename _RandomAccessIterator, typename _Compare> 4862 inline void 4863 __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, 4864 _Compare __comp) 4865 { 4866 typedef typename iterator_traits<_RandomAccessIterator>::value_type 4867 _ValueType; 4868 typedef typename iterator_traits<_RandomAccessIterator>::difference_type 4869 _DistanceType; 4870 4871 typedef _Temporary_buffer<_RandomAccessIterator, _ValueType> _TmpBuf; 4872 _TmpBuf __buf(__first, __last); 4873 4874 if (__buf.begin() == 0) 4875 std::__inplace_stable_sort(__first, __last, __comp); 4876 else 4877 std::__stable_sort_adaptive(__first, __last, __buf.begin(), 4878 _DistanceType(__buf.size()), __comp); 4879 } 4880 4881 /** 4882 * @brief Sort the elements of a sequence, preserving the relative order 4883 * of equivalent elements. 4884 * @ingroup sorting_algorithms 4885 * @param __first An iterator. 4886 * @param __last Another iterator. 4887 * @return Nothing. 4888 * 4889 * Sorts the elements in the range @p [__first,__last) in ascending order, 4890 * such that for each iterator @p i in the range @p [__first,__last-1), 4891 * @p *(i+1)<*i is false. 4892 * 4893 * The relative ordering of equivalent elements is preserved, so any two 4894 * elements @p x and @p y in the range @p [__first,__last) such that 4895 * @p x<y is false and @p y<x is false will have the same relative 4896 * ordering after calling @p stable_sort(). 4897 */ 4898 template<typename _RandomAccessIterator> 4899 inline void 4900 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last) 4901 { 4902 // concept requirements 4903 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 4904 _RandomAccessIterator>) 4905 __glibcxx_function_requires(_LessThanComparableConcept< 4906 typename iterator_traits<_RandomAccessIterator>::value_type>) 4907 __glibcxx_requires_valid_range(__first, __last); 4908 4909 _GLIBCXX_STD_A::__stable_sort(__first, __last, 4910 __gnu_cxx::__ops::__iter_less_iter()); 4911 } 4912 4913 /** 4914 * @brief Sort the elements of a sequence using a predicate for comparison, 4915 * preserving the relative order of equivalent elements. 4916 * @ingroup sorting_algorithms 4917 * @param __first An iterator. 4918 * @param __last Another iterator. 4919 * @param __comp A comparison functor. 4920 * @return Nothing. 4921 * 4922 * Sorts the elements in the range @p [__first,__last) in ascending order, 4923 * such that for each iterator @p i in the range @p [__first,__last-1), 4924 * @p __comp(*(i+1),*i) is false. 4925 * 4926 * The relative ordering of equivalent elements is preserved, so any two 4927 * elements @p x and @p y in the range @p [__first,__last) such that 4928 * @p __comp(x,y) is false and @p __comp(y,x) is false will have the same 4929 * relative ordering after calling @p stable_sort(). 4930 */ 4931 template<typename _RandomAccessIterator, typename _Compare> 4932 inline void 4933 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, 4934 _Compare __comp) 4935 { 4936 // concept requirements 4937 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< 4938 _RandomAccessIterator>) 4939 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 4940 typename iterator_traits<_RandomAccessIterator>::value_type, 4941 typename iterator_traits<_RandomAccessIterator>::value_type>) 4942 __glibcxx_requires_valid_range(__first, __last); 4943 4944 _GLIBCXX_STD_A::__stable_sort(__first, __last, 4945 __gnu_cxx::__ops::__iter_comp_iter(__CheckedCompare(__comp))); 4946 } 4947 4948 template<typename _InputIterator1, typename _InputIterator2, 4949 typename _OutputIterator, 4950 typename _Compare> 4951 _OutputIterator 4952 __set_union(_InputIterator1 __first1, _InputIterator1 __last1, 4953 _InputIterator2 __first2, _InputIterator2 __last2, 4954 _OutputIterator __result, _Compare __comp) 4955 { 4956 while (__first1 != __last1 && __first2 != __last2) 4957 { 4958 if (__comp(__first1, __first2)) 4959 { 4960 *__result = *__first1; 4961 ++__first1; 4962 } 4963 else if (__comp(__first2, __first1)) 4964 { 4965 *__result = *__first2; 4966 ++__first2; 4967 } 4968 else 4969 { 4970 *__result = *__first1; 4971 ++__first1; 4972 ++__first2; 4973 } 4974 ++__result; 4975 } 4976 return std::copy(__first2, __last2, 4977 std::copy(__first1, __last1, __result)); 4978 } 4979 4980 /** 4981 * @brief Return the union of two sorted ranges. 4982 * @ingroup set_algorithms 4983 * @param __first1 Start of first range. 4984 * @param __last1 End of first range. 4985 * @param __first2 Start of second range. 4986 * @param __last2 End of second range. 4987 * @return End of the output range. 4988 * @ingroup set_algorithms 4989 * 4990 * This operation iterates over both ranges, copying elements present in 4991 * each range in order to the output range. Iterators increment for each 4992 * range. When the current element of one range is less than the other, 4993 * that element is copied and the iterator advanced. If an element is 4994 * contained in both ranges, the element from the first range is copied and 4995 * both ranges advance. The output range may not overlap either input 4996 * range. 4997 */ 4998 template<typename _InputIterator1, typename _InputIterator2, 4999 typename _OutputIterator> 5000 inline _OutputIterator 5001 set_union(_InputIterator1 __first1, _InputIterator1 __last1, 5002 _InputIterator2 __first2, _InputIterator2 __last2, 5003 _OutputIterator __result) 5004 { 5005 // concept requirements 5006 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 5007 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 5008 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 5009 typename iterator_traits<_InputIterator1>::value_type>) 5010 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 5011 typename iterator_traits<_InputIterator2>::value_type>) 5012 __glibcxx_function_requires(_LessThanOpConcept< 5013 typename iterator_traits<_InputIterator1>::value_type, 5014 typename iterator_traits<_InputIterator2>::value_type>) 5015 __glibcxx_function_requires(_LessThanOpConcept< 5016 typename iterator_traits<_InputIterator2>::value_type, 5017 typename iterator_traits<_InputIterator1>::value_type>) 5018 __glibcxx_requires_sorted_set(__first1, __last1, __first2); 5019 __glibcxx_requires_sorted_set(__first2, __last2, __first1); 5020 5021 return _GLIBCXX_STD_A::__set_union(__first1, __last1, 5022 __first2, __last2, __result, 5023 __gnu_cxx::__ops::__iter_less_iter()); 5024 } 5025 5026 /** 5027 * @brief Return the union of two sorted ranges using a comparison functor. 5028 * @ingroup set_algorithms 5029 * @param __first1 Start of first range. 5030 * @param __last1 End of first range. 5031 * @param __first2 Start of second range. 5032 * @param __last2 End of second range. 5033 * @param __comp The comparison functor. 5034 * @return End of the output range. 5035 * @ingroup set_algorithms 5036 * 5037 * This operation iterates over both ranges, copying elements present in 5038 * each range in order to the output range. Iterators increment for each 5039 * range. When the current element of one range is less than the other 5040 * according to @p __comp, that element is copied and the iterator advanced. 5041 * If an equivalent element according to @p __comp is contained in both 5042 * ranges, the element from the first range is copied and both ranges 5043 * advance. The output range may not overlap either input range. 5044 */ 5045 template<typename _InputIterator1, typename _InputIterator2, 5046 typename _OutputIterator, typename _Compare> 5047 inline _OutputIterator 5048 set_union(_InputIterator1 __first1, _InputIterator1 __last1, 5049 _InputIterator2 __first2, _InputIterator2 __last2, 5050 _OutputIterator __result, _Compare __comp) 5051 { 5052 // concept requirements 5053 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 5054 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 5055 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 5056 typename iterator_traits<_InputIterator1>::value_type>) 5057 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 5058 typename iterator_traits<_InputIterator2>::value_type>) 5059 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 5060 typename iterator_traits<_InputIterator1>::value_type, 5061 typename iterator_traits<_InputIterator2>::value_type>) 5062 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 5063 typename iterator_traits<_InputIterator2>::value_type, 5064 typename iterator_traits<_InputIterator1>::value_type>) 5065 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); 5066 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); 5067 5068 return _GLIBCXX_STD_A::__set_union(__first1, __last1, 5069 __first2, __last2, __result, 5070 __gnu_cxx::__ops::__iter_comp_iter(__CheckedCompare(__comp))); 5071 } 5072 5073 template<typename _InputIterator1, typename _InputIterator2, 5074 typename _OutputIterator, 5075 typename _Compare> 5076 _OutputIterator 5077 __set_intersection(_InputIterator1 __first1, _InputIterator1 __last1, 5078 _InputIterator2 __first2, _InputIterator2 __last2, 5079 _OutputIterator __result, _Compare __comp) 5080 { 5081 while (__first1 != __last1 && __first2 != __last2) 5082 if (__comp(__first1, __first2)) 5083 ++__first1; 5084 else if (__comp(__first2, __first1)) 5085 ++__first2; 5086 else 5087 { 5088 *__result = *__first1; 5089 ++__first1; 5090 ++__first2; 5091 ++__result; 5092 } 5093 return __result; 5094 } 5095 5096 /** 5097 * @brief Return the intersection of two sorted ranges. 5098 * @ingroup set_algorithms 5099 * @param __first1 Start of first range. 5100 * @param __last1 End of first range. 5101 * @param __first2 Start of second range. 5102 * @param __last2 End of second range. 5103 * @return End of the output range. 5104 * @ingroup set_algorithms 5105 * 5106 * This operation iterates over both ranges, copying elements present in 5107 * both ranges in order to the output range. Iterators increment for each 5108 * range. When the current element of one range is less than the other, 5109 * that iterator advances. If an element is contained in both ranges, the 5110 * element from the first range is copied and both ranges advance. The 5111 * output range may not overlap either input range. 5112 */ 5113 template<typename _InputIterator1, typename _InputIterator2, 5114 typename _OutputIterator> 5115 inline _OutputIterator 5116 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1, 5117 _InputIterator2 __first2, _InputIterator2 __last2, 5118 _OutputIterator __result) 5119 { 5120 // concept requirements 5121 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 5122 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 5123 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 5124 typename iterator_traits<_InputIterator1>::value_type>) 5125 __glibcxx_function_requires(_LessThanOpConcept< 5126 typename iterator_traits<_InputIterator1>::value_type, 5127 typename iterator_traits<_InputIterator2>::value_type>) 5128 __glibcxx_function_requires(_LessThanOpConcept< 5129 typename iterator_traits<_InputIterator2>::value_type, 5130 typename iterator_traits<_InputIterator1>::value_type>) 5131 __glibcxx_requires_sorted_set(__first1, __last1, __first2); 5132 __glibcxx_requires_sorted_set(__first2, __last2, __first1); 5133 5134 return _GLIBCXX_STD_A::__set_intersection(__first1, __last1, 5135 __first2, __last2, __result, 5136 __gnu_cxx::__ops::__iter_less_iter()); 5137 } 5138 5139 /** 5140 * @brief Return the intersection of two sorted ranges using comparison 5141 * functor. 5142 * @ingroup set_algorithms 5143 * @param __first1 Start of first range. 5144 * @param __last1 End of first range. 5145 * @param __first2 Start of second range. 5146 * @param __last2 End of second range. 5147 * @param __comp The comparison functor. 5148 * @return End of the output range. 5149 * @ingroup set_algorithms 5150 * 5151 * This operation iterates over both ranges, copying elements present in 5152 * both ranges in order to the output range. Iterators increment for each 5153 * range. When the current element of one range is less than the other 5154 * according to @p __comp, that iterator advances. If an element is 5155 * contained in both ranges according to @p __comp, the element from the 5156 * first range is copied and both ranges advance. The output range may not 5157 * overlap either input range. 5158 */ 5159 template<typename _InputIterator1, typename _InputIterator2, 5160 typename _OutputIterator, typename _Compare> 5161 inline _OutputIterator 5162 set_intersection(_InputIterator1 __first1, _InputIterator1 __last1, 5163 _InputIterator2 __first2, _InputIterator2 __last2, 5164 _OutputIterator __result, _Compare __comp) 5165 { 5166 // concept requirements 5167 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 5168 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 5169 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 5170 typename iterator_traits<_InputIterator1>::value_type>) 5171 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 5172 typename iterator_traits<_InputIterator1>::value_type, 5173 typename iterator_traits<_InputIterator2>::value_type>) 5174 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 5175 typename iterator_traits<_InputIterator2>::value_type, 5176 typename iterator_traits<_InputIterator1>::value_type>) 5177 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); 5178 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); 5179 5180 return _GLIBCXX_STD_A::__set_intersection(__first1, __last1, 5181 __first2, __last2, __result, 5182 __gnu_cxx::__ops::__iter_comp_iter(__CheckedCompare(__comp))); 5183 } 5184 5185 template<typename _InputIterator1, typename _InputIterator2, 5186 typename _OutputIterator, 5187 typename _Compare> 5188 _OutputIterator 5189 __set_difference(_InputIterator1 __first1, _InputIterator1 __last1, 5190 _InputIterator2 __first2, _InputIterator2 __last2, 5191 _OutputIterator __result, _Compare __comp) 5192 { 5193 while (__first1 != __last1 && __first2 != __last2) 5194 if (__comp(__first1, __first2)) 5195 { 5196 *__result = *__first1; 5197 ++__first1; 5198 ++__result; 5199 } 5200 else if (__comp(__first2, __first1)) 5201 ++__first2; 5202 else 5203 { 5204 ++__first1; 5205 ++__first2; 5206 } 5207 return std::copy(__first1, __last1, __result); 5208 } 5209 5210 /** 5211 * @brief Return the difference of two sorted ranges. 5212 * @ingroup set_algorithms 5213 * @param __first1 Start of first range. 5214 * @param __last1 End of first range. 5215 * @param __first2 Start of second range. 5216 * @param __last2 End of second range. 5217 * @return End of the output range. 5218 * @ingroup set_algorithms 5219 * 5220 * This operation iterates over both ranges, copying elements present in 5221 * the first range but not the second in order to the output range. 5222 * Iterators increment for each range. When the current element of the 5223 * first range is less than the second, that element is copied and the 5224 * iterator advances. If the current element of the second range is less, 5225 * the iterator advances, but no element is copied. If an element is 5226 * contained in both ranges, no elements are copied and both ranges 5227 * advance. The output range may not overlap either input range. 5228 */ 5229 template<typename _InputIterator1, typename _InputIterator2, 5230 typename _OutputIterator> 5231 inline _OutputIterator 5232 set_difference(_InputIterator1 __first1, _InputIterator1 __last1, 5233 _InputIterator2 __first2, _InputIterator2 __last2, 5234 _OutputIterator __result) 5235 { 5236 // concept requirements 5237 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 5238 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 5239 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 5240 typename iterator_traits<_InputIterator1>::value_type>) 5241 __glibcxx_function_requires(_LessThanOpConcept< 5242 typename iterator_traits<_InputIterator1>::value_type, 5243 typename iterator_traits<_InputIterator2>::value_type>) 5244 __glibcxx_function_requires(_LessThanOpConcept< 5245 typename iterator_traits<_InputIterator2>::value_type, 5246 typename iterator_traits<_InputIterator1>::value_type>) 5247 __glibcxx_requires_sorted_set(__first1, __last1, __first2); 5248 __glibcxx_requires_sorted_set(__first2, __last2, __first1); 5249 5250 return _GLIBCXX_STD_A::__set_difference(__first1, __last1, 5251 __first2, __last2, __result, 5252 __gnu_cxx::__ops::__iter_less_iter()); 5253 } 5254 5255 /** 5256 * @brief Return the difference of two sorted ranges using comparison 5257 * functor. 5258 * @ingroup set_algorithms 5259 * @param __first1 Start of first range. 5260 * @param __last1 End of first range. 5261 * @param __first2 Start of second range. 5262 * @param __last2 End of second range. 5263 * @param __comp The comparison functor. 5264 * @return End of the output range. 5265 * @ingroup set_algorithms 5266 * 5267 * This operation iterates over both ranges, copying elements present in 5268 * the first range but not the second in order to the output range. 5269 * Iterators increment for each range. When the current element of the 5270 * first range is less than the second according to @p __comp, that element 5271 * is copied and the iterator advances. If the current element of the 5272 * second range is less, no element is copied and the iterator advances. 5273 * If an element is contained in both ranges according to @p __comp, no 5274 * elements are copied and both ranges advance. The output range may not 5275 * overlap either input range. 5276 */ 5277 template<typename _InputIterator1, typename _InputIterator2, 5278 typename _OutputIterator, typename _Compare> 5279 inline _OutputIterator 5280 set_difference(_InputIterator1 __first1, _InputIterator1 __last1, 5281 _InputIterator2 __first2, _InputIterator2 __last2, 5282 _OutputIterator __result, _Compare __comp) 5283 { 5284 // concept requirements 5285 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 5286 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 5287 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 5288 typename iterator_traits<_InputIterator1>::value_type>) 5289 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 5290 typename iterator_traits<_InputIterator1>::value_type, 5291 typename iterator_traits<_InputIterator2>::value_type>) 5292 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 5293 typename iterator_traits<_InputIterator2>::value_type, 5294 typename iterator_traits<_InputIterator1>::value_type>) 5295 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); 5296 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); 5297 5298 return _GLIBCXX_STD_A::__set_difference(__first1, __last1, 5299 __first2, __last2, __result, 5300 __gnu_cxx::__ops::__iter_comp_iter(__CheckedCompare(__comp))); 5301 } 5302 5303 template<typename _InputIterator1, typename _InputIterator2, 5304 typename _OutputIterator, 5305 typename _Compare> 5306 _OutputIterator 5307 __set_symmetric_difference(_InputIterator1 __first1, 5308 _InputIterator1 __last1, 5309 _InputIterator2 __first2, 5310 _InputIterator2 __last2, 5311 _OutputIterator __result, 5312 _Compare __comp) 5313 { 5314 while (__first1 != __last1 && __first2 != __last2) 5315 if (__comp(__first1, __first2)) 5316 { 5317 *__result = *__first1; 5318 ++__first1; 5319 ++__result; 5320 } 5321 else if (__comp(__first2, __first1)) 5322 { 5323 *__result = *__first2; 5324 ++__first2; 5325 ++__result; 5326 } 5327 else 5328 { 5329 ++__first1; 5330 ++__first2; 5331 } 5332 return std::copy(__first2, __last2, 5333 std::copy(__first1, __last1, __result)); 5334 } 5335 5336 /** 5337 * @brief Return the symmetric difference of two sorted ranges. 5338 * @ingroup set_algorithms 5339 * @param __first1 Start of first range. 5340 * @param __last1 End of first range. 5341 * @param __first2 Start of second range. 5342 * @param __last2 End of second range. 5343 * @return End of the output range. 5344 * @ingroup set_algorithms 5345 * 5346 * This operation iterates over both ranges, copying elements present in 5347 * one range but not the other in order to the output range. Iterators 5348 * increment for each range. When the current element of one range is less 5349 * than the other, that element is copied and the iterator advances. If an 5350 * element is contained in both ranges, no elements are copied and both 5351 * ranges advance. The output range may not overlap either input range. 5352 */ 5353 template<typename _InputIterator1, typename _InputIterator2, 5354 typename _OutputIterator> 5355 inline _OutputIterator 5356 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1, 5357 _InputIterator2 __first2, _InputIterator2 __last2, 5358 _OutputIterator __result) 5359 { 5360 // concept requirements 5361 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 5362 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 5363 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 5364 typename iterator_traits<_InputIterator1>::value_type>) 5365 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 5366 typename iterator_traits<_InputIterator2>::value_type>) 5367 __glibcxx_function_requires(_LessThanOpConcept< 5368 typename iterator_traits<_InputIterator1>::value_type, 5369 typename iterator_traits<_InputIterator2>::value_type>) 5370 __glibcxx_function_requires(_LessThanOpConcept< 5371 typename iterator_traits<_InputIterator2>::value_type, 5372 typename iterator_traits<_InputIterator1>::value_type>) 5373 __glibcxx_requires_sorted_set(__first1, __last1, __first2); 5374 __glibcxx_requires_sorted_set(__first2, __last2, __first1); 5375 5376 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1, __last1, 5377 __first2, __last2, __result, 5378 __gnu_cxx::__ops::__iter_less_iter()); 5379 } 5380 5381 /** 5382 * @brief Return the symmetric difference of two sorted ranges using 5383 * comparison functor. 5384 * @ingroup set_algorithms 5385 * @param __first1 Start of first range. 5386 * @param __last1 End of first range. 5387 * @param __first2 Start of second range. 5388 * @param __last2 End of second range. 5389 * @param __comp The comparison functor. 5390 * @return End of the output range. 5391 * @ingroup set_algorithms 5392 * 5393 * This operation iterates over both ranges, copying elements present in 5394 * one range but not the other in order to the output range. Iterators 5395 * increment for each range. When the current element of one range is less 5396 * than the other according to @p comp, that element is copied and the 5397 * iterator advances. If an element is contained in both ranges according 5398 * to @p __comp, no elements are copied and both ranges advance. The output 5399 * range may not overlap either input range. 5400 */ 5401 template<typename _InputIterator1, typename _InputIterator2, 5402 typename _OutputIterator, typename _Compare> 5403 inline _OutputIterator 5404 set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1, 5405 _InputIterator2 __first2, _InputIterator2 __last2, 5406 _OutputIterator __result, 5407 _Compare __comp) 5408 { 5409 // concept requirements 5410 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 5411 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 5412 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 5413 typename iterator_traits<_InputIterator1>::value_type>) 5414 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 5415 typename iterator_traits<_InputIterator2>::value_type>) 5416 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 5417 typename iterator_traits<_InputIterator1>::value_type, 5418 typename iterator_traits<_InputIterator2>::value_type>) 5419 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 5420 typename iterator_traits<_InputIterator2>::value_type, 5421 typename iterator_traits<_InputIterator1>::value_type>) 5422 __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); 5423 __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); 5424 5425 return _GLIBCXX_STD_A::__set_symmetric_difference(__first1, __last1, 5426 __first2, __last2, __result, 5427 __gnu_cxx::__ops::__iter_comp_iter(__CheckedCompare(__comp))); 5428 } 5429 5430 template<typename _ForwardIterator, typename _Compare> 5431 _ForwardIterator 5432 __min_element(_ForwardIterator __first, _ForwardIterator __last, 5433 _Compare __comp) 5434 { 5435 if (__first == __last) 5436 return __first; 5437 _ForwardIterator __result = __first; 5438 while (++__first != __last) 5439 if (__comp(__first, __result)) 5440 __result = __first; 5441 return __result; 5442 } 5443 5444 /** 5445 * @brief Return the minimum element in a range. 5446 * @ingroup sorting_algorithms 5447 * @param __first Start of range. 5448 * @param __last End of range. 5449 * @return Iterator referencing the first instance of the smallest value. 5450 */ 5451 template<typename _ForwardIterator> 5452 _ForwardIterator 5453 inline min_element(_ForwardIterator __first, _ForwardIterator __last) 5454 { 5455 // concept requirements 5456 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 5457 __glibcxx_function_requires(_LessThanComparableConcept< 5458 typename iterator_traits<_ForwardIterator>::value_type>) 5459 __glibcxx_requires_valid_range(__first, __last); 5460 5461 return _GLIBCXX_STD_A::__min_element(__first, __last, 5462 __gnu_cxx::__ops::__iter_less_iter()); 5463 } 5464 5465 /** 5466 * @brief Return the minimum element in a range using comparison functor. 5467 * @ingroup sorting_algorithms 5468 * @param __first Start of range. 5469 * @param __last End of range. 5470 * @param __comp Comparison functor. 5471 * @return Iterator referencing the first instance of the smallest value 5472 * according to __comp. 5473 */ 5474 template<typename _ForwardIterator, typename _Compare> 5475 inline _ForwardIterator 5476 min_element(_ForwardIterator __first, _ForwardIterator __last, 5477 _Compare __comp) 5478 { 5479 // concept requirements 5480 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 5481 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 5482 typename iterator_traits<_ForwardIterator>::value_type, 5483 typename iterator_traits<_ForwardIterator>::value_type>) 5484 __glibcxx_requires_valid_range(__first, __last); 5485 5486 return _GLIBCXX_STD_A::__min_element(__first, __last, 5487 __gnu_cxx::__ops::__iter_comp_iter(__comp)); 5488 } 5489 5490 template<typename _ForwardIterator, typename _Compare> 5491 _ForwardIterator 5492 __max_element(_ForwardIterator __first, _ForwardIterator __last, 5493 _Compare __comp) 5494 { 5495 if (__first == __last) return __first; 5496 _ForwardIterator __result = __first; 5497 while (++__first != __last) 5498 if (__comp(__result, __first)) 5499 __result = __first; 5500 return __result; 5501 } 5502 5503 /** 5504 * @brief Return the maximum element in a range. 5505 * @ingroup sorting_algorithms 5506 * @param __first Start of range. 5507 * @param __last End of range. 5508 * @return Iterator referencing the first instance of the largest value. 5509 */ 5510 template<typename _ForwardIterator> 5511 inline _ForwardIterator 5512 max_element(_ForwardIterator __first, _ForwardIterator __last) 5513 { 5514 // concept requirements 5515 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 5516 __glibcxx_function_requires(_LessThanComparableConcept< 5517 typename iterator_traits<_ForwardIterator>::value_type>) 5518 __glibcxx_requires_valid_range(__first, __last); 5519 5520 return _GLIBCXX_STD_A::__max_element(__first, __last, 5521 __gnu_cxx::__ops::__iter_less_iter()); 5522 } 5523 5524 /** 5525 * @brief Return the maximum element in a range using comparison functor. 5526 * @ingroup sorting_algorithms 5527 * @param __first Start of range. 5528 * @param __last End of range. 5529 * @param __comp Comparison functor. 5530 * @return Iterator referencing the first instance of the largest value 5531 * according to __comp. 5532 */ 5533 template<typename _ForwardIterator, typename _Compare> 5534 inline _ForwardIterator 5535 max_element(_ForwardIterator __first, _ForwardIterator __last, 5536 _Compare __comp) 5537 { 5538 // concept requirements 5539 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) 5540 __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, 5541 typename iterator_traits<_ForwardIterator>::value_type, 5542 typename iterator_traits<_ForwardIterator>::value_type>) 5543 __glibcxx_requires_valid_range(__first, __last); 5544 5545 return _GLIBCXX_STD_A::__max_element(__first, __last, 5546 __gnu_cxx::__ops::__iter_comp_iter(__comp)); 5547 } 5548 5549 _GLIBCXX_END_NAMESPACE_ALGO 5550 } // namespace std 5551 5552 #endif /* _STL_ALGO_H */ 5553