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