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