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