algorithm revision 253159
1227825Stheraven// -*- C++ -*- 2227825Stheraven//===-------------------------- algorithm ---------------------------------===// 3227825Stheraven// 4227825Stheraven// The LLVM Compiler Infrastructure 5227825Stheraven// 6227825Stheraven// This file is dual licensed under the MIT and the University of Illinois Open 7227825Stheraven// Source Licenses. See LICENSE.TXT for details. 8227825Stheraven// 9227825Stheraven//===----------------------------------------------------------------------===// 10227825Stheraven 11227825Stheraven#ifndef _LIBCPP_ALGORITHM 12227825Stheraven#define _LIBCPP_ALGORITHM 13227825Stheraven 14227825Stheraven/* 15227825Stheraven algorithm synopsis 16227825Stheraven 17227825Stheraven#include <initializer_list> 18227825Stheraven 19227825Stheravennamespace std 20227825Stheraven{ 21227825Stheraven 22227825Stheraventemplate <class InputIterator, class Predicate> 23227825Stheraven bool 24227825Stheraven all_of(InputIterator first, InputIterator last, Predicate pred); 25227825Stheraven 26227825Stheraventemplate <class InputIterator, class Predicate> 27227825Stheraven bool 28227825Stheraven any_of(InputIterator first, InputIterator last, Predicate pred); 29227825Stheraven 30227825Stheraventemplate <class InputIterator, class Predicate> 31227825Stheraven bool 32227825Stheraven none_of(InputIterator first, InputIterator last, Predicate pred); 33227825Stheraven 34227825Stheraventemplate <class InputIterator, class Function> 35227825Stheraven Function 36227825Stheraven for_each(InputIterator first, InputIterator last, Function f); 37227825Stheraven 38227825Stheraventemplate <class InputIterator, class T> 39227825Stheraven InputIterator 40227825Stheraven find(InputIterator first, InputIterator last, const T& value); 41227825Stheraven 42227825Stheraventemplate <class InputIterator, class Predicate> 43227825Stheraven InputIterator 44227825Stheraven find_if(InputIterator first, InputIterator last, Predicate pred); 45227825Stheraven 46227825Stheraventemplate<class InputIterator, class Predicate> 47227825Stheraven InputIterator 48227825Stheraven find_if_not(InputIterator first, InputIterator last, Predicate pred); 49227825Stheraven 50227825Stheraventemplate <class ForwardIterator1, class ForwardIterator2> 51227825Stheraven ForwardIterator1 52227825Stheraven find_end(ForwardIterator1 first1, ForwardIterator1 last1, 53227825Stheraven ForwardIterator2 first2, ForwardIterator2 last2); 54227825Stheraven 55227825Stheraventemplate <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> 56227825Stheraven ForwardIterator1 57227825Stheraven find_end(ForwardIterator1 first1, ForwardIterator1 last1, 58227825Stheraven ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred); 59227825Stheraven 60227825Stheraventemplate <class ForwardIterator1, class ForwardIterator2> 61227825Stheraven ForwardIterator1 62227825Stheraven find_first_of(ForwardIterator1 first1, ForwardIterator1 last1, 63227825Stheraven ForwardIterator2 first2, ForwardIterator2 last2); 64227825Stheraven 65227825Stheraventemplate <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> 66227825Stheraven ForwardIterator1 67227825Stheraven find_first_of(ForwardIterator1 first1, ForwardIterator1 last1, 68227825Stheraven ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred); 69227825Stheraven 70227825Stheraventemplate <class ForwardIterator> 71227825Stheraven ForwardIterator 72227825Stheraven adjacent_find(ForwardIterator first, ForwardIterator last); 73227825Stheraven 74227825Stheraventemplate <class ForwardIterator, class BinaryPredicate> 75227825Stheraven ForwardIterator 76227825Stheraven adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred); 77227825Stheraven 78227825Stheraventemplate <class InputIterator, class T> 79227825Stheraven typename iterator_traits<InputIterator>::difference_type 80227825Stheraven count(InputIterator first, InputIterator last, const T& value); 81227825Stheraven 82227825Stheraventemplate <class InputIterator, class Predicate> 83227825Stheraven typename iterator_traits<InputIterator>::difference_type 84227825Stheraven count_if(InputIterator first, InputIterator last, Predicate pred); 85227825Stheraven 86227825Stheraventemplate <class InputIterator1, class InputIterator2> 87227825Stheraven pair<InputIterator1, InputIterator2> 88227825Stheraven mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2); 89227825Stheraven 90253159Stheraventemplate <class InputIterator1, class InputIterator2> 91253159Stheraven pair<InputIterator1, InputIterator2> 92253159Stheraven mismatch(InputIterator1 first1, InputIterator1 last1, 93253159Stheraven InputIterator2 first2, InputIterator2 last2); // **C++14** 94253159Stheraven 95227825Stheraventemplate <class InputIterator1, class InputIterator2, class BinaryPredicate> 96227825Stheraven pair<InputIterator1, InputIterator2> 97227825Stheraven mismatch(InputIterator1 first1, InputIterator1 last1, 98227825Stheraven InputIterator2 first2, BinaryPredicate pred); 99227825Stheraven 100253159Stheraventemplate <class InputIterator1, class InputIterator2, class BinaryPredicate> 101253159Stheraven pair<InputIterator1, InputIterator2> 102253159Stheraven mismatch(InputIterator1 first1, InputIterator1 last1, 103253159Stheraven InputIterator2 first2, InputIterator2 last2, 104253159Stheraven BinaryPredicate pred); // **C++14** 105253159Stheraven 106227825Stheraventemplate <class InputIterator1, class InputIterator2> 107227825Stheraven bool 108227825Stheraven equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2); 109227825Stheraven 110253159Stheraventemplate <class InputIterator1, class InputIterator2> 111253159Stheraven bool 112253159Stheraven equal(InputIterator1 first1, InputIterator1 last1, 113253159Stheraven InputIterator2 first2, InputIterator2 last2); // **C++14** 114253159Stheraven 115227825Stheraventemplate <class InputIterator1, class InputIterator2, class BinaryPredicate> 116227825Stheraven bool 117227825Stheraven equal(InputIterator1 first1, InputIterator1 last1, 118227825Stheraven InputIterator2 first2, BinaryPredicate pred); 119227825Stheraven 120253159Stheraventemplate <class InputIterator1, class InputIterator2, class BinaryPredicate> 121253159Stheraven bool 122253159Stheraven equal(InputIterator1 first1, InputIterator1 last1, 123253159Stheraven InputIterator2 first2, InputIterator2 last2, 124253159Stheraven BinaryPredicate pred); // **C++14** 125253159Stheraven 126227825Stheraventemplate<class ForwardIterator1, class ForwardIterator2> 127227825Stheraven bool 128227825Stheraven is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, 129227825Stheraven ForwardIterator2 first2); 130227825Stheraven 131253159Stheraventemplate<class ForwardIterator1, class ForwardIterator2> 132253159Stheraven bool 133253159Stheraven is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, 134253159Stheraven ForwardIterator2 first2, ForwardIterator2 last2); // **C++14** 135253159Stheraven 136227825Stheraventemplate<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> 137227825Stheraven bool 138227825Stheraven is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, 139227825Stheraven ForwardIterator2 first2, BinaryPredicate pred); 140227825Stheraven 141253159Stheraventemplate<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> 142253159Stheraven bool 143253159Stheraven is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, 144253159Stheraven ForwardIterator2 first2, ForwardIterator2 last2, 145253159Stheraven BinaryPredicate pred); // **C++14** 146253159Stheraven 147227825Stheraventemplate <class ForwardIterator1, class ForwardIterator2> 148227825Stheraven ForwardIterator1 149227825Stheraven search(ForwardIterator1 first1, ForwardIterator1 last1, 150227825Stheraven ForwardIterator2 first2, ForwardIterator2 last2); 151227825Stheraven 152227825Stheraventemplate <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> 153227825Stheraven ForwardIterator1 154227825Stheraven search(ForwardIterator1 first1, ForwardIterator1 last1, 155227825Stheraven ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred); 156227825Stheraven 157227825Stheraventemplate <class ForwardIterator, class Size, class T> 158227825Stheraven ForwardIterator 159227825Stheraven search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value); 160227825Stheraven 161227825Stheraventemplate <class ForwardIterator, class Size, class T, class BinaryPredicate> 162227825Stheraven ForwardIterator 163227825Stheraven search_n(ForwardIterator first, ForwardIterator last, 164227825Stheraven Size count, const T& value, BinaryPredicate pred); 165227825Stheraven 166227825Stheraventemplate <class InputIterator, class OutputIterator> 167227825Stheraven OutputIterator 168227825Stheraven copy(InputIterator first, InputIterator last, OutputIterator result); 169227825Stheraven 170227825Stheraventemplate<class InputIterator, class OutputIterator, class Predicate> 171227825Stheraven OutputIterator 172227825Stheraven copy_if(InputIterator first, InputIterator last, 173227825Stheraven OutputIterator result, Predicate pred); 174227825Stheraven 175227825Stheraventemplate<class InputIterator, class Size, class OutputIterator> 176227825Stheraven OutputIterator 177227825Stheraven copy_n(InputIterator first, Size n, OutputIterator result); 178227825Stheraven 179227825Stheraventemplate <class BidirectionalIterator1, class BidirectionalIterator2> 180227825Stheraven BidirectionalIterator2 181227825Stheraven copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last, 182227825Stheraven BidirectionalIterator2 result); 183227825Stheraven 184227825Stheraventemplate <class ForwardIterator1, class ForwardIterator2> 185227825Stheraven ForwardIterator2 186227825Stheraven swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2); 187227825Stheraven 188227825Stheraventemplate <class ForwardIterator1, class ForwardIterator2> 189227825Stheraven void 190227825Stheraven iter_swap(ForwardIterator1 a, ForwardIterator2 b); 191227825Stheraven 192227825Stheraventemplate <class InputIterator, class OutputIterator, class UnaryOperation> 193227825Stheraven OutputIterator 194227825Stheraven transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op); 195227825Stheraven 196227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation> 197227825Stheraven OutputIterator 198227825Stheraven transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, 199227825Stheraven OutputIterator result, BinaryOperation binary_op); 200227825Stheraven 201227825Stheraventemplate <class ForwardIterator, class T> 202227825Stheraven void 203227825Stheraven replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value); 204227825Stheraven 205227825Stheraventemplate <class ForwardIterator, class Predicate, class T> 206227825Stheraven void 207227825Stheraven replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value); 208227825Stheraven 209227825Stheraventemplate <class InputIterator, class OutputIterator, class T> 210227825Stheraven OutputIterator 211227825Stheraven replace_copy(InputIterator first, InputIterator last, OutputIterator result, 212227825Stheraven const T& old_value, const T& new_value); 213227825Stheraven 214227825Stheraventemplate <class InputIterator, class OutputIterator, class Predicate, class T> 215227825Stheraven OutputIterator 216227825Stheraven replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value); 217227825Stheraven 218227825Stheraventemplate <class ForwardIterator, class T> 219227825Stheraven void 220227825Stheraven fill(ForwardIterator first, ForwardIterator last, const T& value); 221227825Stheraven 222227825Stheraventemplate <class OutputIterator, class Size, class T> 223227825Stheraven OutputIterator 224227825Stheraven fill_n(OutputIterator first, Size n, const T& value); 225227825Stheraven 226227825Stheraventemplate <class ForwardIterator, class Generator> 227227825Stheraven void 228227825Stheraven generate(ForwardIterator first, ForwardIterator last, Generator gen); 229227825Stheraven 230227825Stheraventemplate <class OutputIterator, class Size, class Generator> 231227825Stheraven OutputIterator 232227825Stheraven generate_n(OutputIterator first, Size n, Generator gen); 233227825Stheraven 234227825Stheraventemplate <class ForwardIterator, class T> 235227825Stheraven ForwardIterator 236227825Stheraven remove(ForwardIterator first, ForwardIterator last, const T& value); 237227825Stheraven 238227825Stheraventemplate <class ForwardIterator, class Predicate> 239227825Stheraven ForwardIterator 240227825Stheraven remove_if(ForwardIterator first, ForwardIterator last, Predicate pred); 241227825Stheraven 242227825Stheraventemplate <class InputIterator, class OutputIterator, class T> 243227825Stheraven OutputIterator 244227825Stheraven remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value); 245227825Stheraven 246227825Stheraventemplate <class InputIterator, class OutputIterator, class Predicate> 247227825Stheraven OutputIterator 248227825Stheraven remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred); 249227825Stheraven 250227825Stheraventemplate <class ForwardIterator> 251227825Stheraven ForwardIterator 252227825Stheraven unique(ForwardIterator first, ForwardIterator last); 253227825Stheraven 254227825Stheraventemplate <class ForwardIterator, class BinaryPredicate> 255227825Stheraven ForwardIterator 256227825Stheraven unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred); 257227825Stheraven 258227825Stheraventemplate <class InputIterator, class OutputIterator> 259227825Stheraven OutputIterator 260227825Stheraven unique_copy(InputIterator first, InputIterator last, OutputIterator result); 261227825Stheraven 262227825Stheraventemplate <class InputIterator, class OutputIterator, class BinaryPredicate> 263227825Stheraven OutputIterator 264227825Stheraven unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred); 265227825Stheraven 266227825Stheraventemplate <class BidirectionalIterator> 267227825Stheraven void 268227825Stheraven reverse(BidirectionalIterator first, BidirectionalIterator last); 269227825Stheraven 270227825Stheraventemplate <class BidirectionalIterator, class OutputIterator> 271227825Stheraven OutputIterator 272227825Stheraven reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result); 273227825Stheraven 274227825Stheraventemplate <class ForwardIterator> 275227825Stheraven ForwardIterator 276227825Stheraven rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last); 277227825Stheraven 278227825Stheraventemplate <class ForwardIterator, class OutputIterator> 279227825Stheraven OutputIterator 280227825Stheraven rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result); 281227825Stheraven 282227825Stheraventemplate <class RandomAccessIterator> 283227825Stheraven void 284227825Stheraven random_shuffle(RandomAccessIterator first, RandomAccessIterator last); 285227825Stheraven 286227825Stheraventemplate <class RandomAccessIterator, class RandomNumberGenerator> 287227825Stheraven void 288227825Stheraven random_shuffle(RandomAccessIterator first, RandomAccessIterator last, RandomNumberGenerator& rand); 289227825Stheraven 290227825Stheraventemplate<class RandomAccessIterator, class UniformRandomNumberGenerator> 291227825Stheraven void shuffle(RandomAccessIterator first, RandomAccessIterator last, 292227825Stheraven UniformRandomNumberGenerator&& g); 293227825Stheraven 294227825Stheraventemplate <class InputIterator, class Predicate> 295227825Stheraven bool 296227825Stheraven is_partitioned(InputIterator first, InputIterator last, Predicate pred); 297227825Stheraven 298227825Stheraventemplate <class ForwardIterator, class Predicate> 299227825Stheraven ForwardIterator 300227825Stheraven partition(ForwardIterator first, ForwardIterator last, Predicate pred); 301227825Stheraven 302227825Stheraventemplate <class InputIterator, class OutputIterator1, 303227825Stheraven class OutputIterator2, class Predicate> 304227825Stheraven pair<OutputIterator1, OutputIterator2> 305227825Stheraven partition_copy(InputIterator first, InputIterator last, 306227825Stheraven OutputIterator1 out_true, OutputIterator2 out_false, 307227825Stheraven Predicate pred); 308227825Stheraven 309227825Stheraventemplate <class ForwardIterator, class Predicate> 310227825Stheraven ForwardIterator 311227825Stheraven stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred); 312227825Stheraven 313227825Stheraventemplate<class ForwardIterator, class Predicate> 314227825Stheraven ForwardIterator 315227825Stheraven partition_point(ForwardIterator first, ForwardIterator last, Predicate pred); 316227825Stheraven 317227825Stheraventemplate <class ForwardIterator> 318227825Stheraven bool 319227825Stheraven is_sorted(ForwardIterator first, ForwardIterator last); 320227825Stheraven 321227825Stheraventemplate <class ForwardIterator, class Compare> 322227825Stheraven bool 323227825Stheraven is_sorted(ForwardIterator first, ForwardIterator last, Compare comp); 324227825Stheraven 325227825Stheraventemplate<class ForwardIterator> 326227825Stheraven ForwardIterator 327227825Stheraven is_sorted_until(ForwardIterator first, ForwardIterator last); 328227825Stheraven 329227825Stheraventemplate <class ForwardIterator, class Compare> 330227825Stheraven ForwardIterator 331227825Stheraven is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp); 332227825Stheraven 333227825Stheraventemplate <class RandomAccessIterator> 334227825Stheraven void 335227825Stheraven sort(RandomAccessIterator first, RandomAccessIterator last); 336227825Stheraven 337227825Stheraventemplate <class RandomAccessIterator, class Compare> 338227825Stheraven void 339227825Stheraven sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp); 340227825Stheraven 341227825Stheraventemplate <class RandomAccessIterator> 342227825Stheraven void 343227825Stheraven stable_sort(RandomAccessIterator first, RandomAccessIterator last); 344227825Stheraven 345227825Stheraventemplate <class RandomAccessIterator, class Compare> 346227825Stheraven void 347227825Stheraven stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp); 348227825Stheraven 349227825Stheraventemplate <class RandomAccessIterator> 350227825Stheraven void 351227825Stheraven partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last); 352227825Stheraven 353227825Stheraventemplate <class RandomAccessIterator, class Compare> 354227825Stheraven void 355227825Stheraven partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp); 356227825Stheraven 357227825Stheraventemplate <class InputIterator, class RandomAccessIterator> 358227825Stheraven RandomAccessIterator 359227825Stheraven partial_sort_copy(InputIterator first, InputIterator last, 360227825Stheraven RandomAccessIterator result_first, RandomAccessIterator result_last); 361227825Stheraven 362227825Stheraventemplate <class InputIterator, class RandomAccessIterator, class Compare> 363227825Stheraven RandomAccessIterator 364227825Stheraven partial_sort_copy(InputIterator first, InputIterator last, 365227825Stheraven RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp); 366227825Stheraven 367227825Stheraventemplate <class RandomAccessIterator> 368227825Stheraven void 369227825Stheraven nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last); 370227825Stheraven 371227825Stheraventemplate <class RandomAccessIterator, class Compare> 372227825Stheraven void 373227825Stheraven nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp); 374227825Stheraven 375227825Stheraventemplate <class ForwardIterator, class T> 376227825Stheraven ForwardIterator 377227825Stheraven lower_bound(ForwardIterator first, ForwardIterator last, const T& value); 378227825Stheraven 379227825Stheraventemplate <class ForwardIterator, class T, class Compare> 380227825Stheraven ForwardIterator 381227825Stheraven lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp); 382227825Stheraven 383227825Stheraventemplate <class ForwardIterator, class T> 384227825Stheraven ForwardIterator 385227825Stheraven upper_bound(ForwardIterator first, ForwardIterator last, const T& value); 386227825Stheraven 387227825Stheraventemplate <class ForwardIterator, class T, class Compare> 388227825Stheraven ForwardIterator 389227825Stheraven upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp); 390227825Stheraven 391227825Stheraventemplate <class ForwardIterator, class T> 392227825Stheraven pair<ForwardIterator, ForwardIterator> 393227825Stheraven equal_range(ForwardIterator first, ForwardIterator last, const T& value); 394227825Stheraven 395227825Stheraventemplate <class ForwardIterator, class T, class Compare> 396227825Stheraven pair<ForwardIterator, ForwardIterator> 397227825Stheraven equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp); 398227825Stheraven 399227825Stheraventemplate <class ForwardIterator, class T> 400227825Stheraven bool 401227825Stheraven binary_search(ForwardIterator first, ForwardIterator last, const T& value); 402227825Stheraven 403227825Stheraventemplate <class ForwardIterator, class T, class Compare> 404227825Stheraven bool 405227825Stheraven binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp); 406227825Stheraven 407227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator> 408227825Stheraven OutputIterator 409227825Stheraven merge(InputIterator1 first1, InputIterator1 last1, 410227825Stheraven InputIterator2 first2, InputIterator2 last2, OutputIterator result); 411227825Stheraven 412227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> 413227825Stheraven OutputIterator 414227825Stheraven merge(InputIterator1 first1, InputIterator1 last1, 415227825Stheraven InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); 416227825Stheraven 417227825Stheraventemplate <class BidirectionalIterator> 418227825Stheraven void 419227825Stheraven inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last); 420227825Stheraven 421227825Stheraventemplate <class BidirectionalIterator, class Compare> 422227825Stheraven void 423227825Stheraven inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp); 424227825Stheraven 425227825Stheraventemplate <class InputIterator1, class InputIterator2> 426227825Stheraven bool 427227825Stheraven includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2); 428227825Stheraven 429227825Stheraventemplate <class InputIterator1, class InputIterator2, class Compare> 430227825Stheraven bool 431227825Stheraven includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp); 432227825Stheraven 433227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator> 434227825Stheraven OutputIterator 435227825Stheraven set_union(InputIterator1 first1, InputIterator1 last1, 436227825Stheraven InputIterator2 first2, InputIterator2 last2, OutputIterator result); 437227825Stheraven 438227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> 439227825Stheraven OutputIterator 440227825Stheraven set_union(InputIterator1 first1, InputIterator1 last1, 441227825Stheraven InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); 442227825Stheraven 443227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator> 444227825Stheraven OutputIterator 445227825Stheraven set_intersection(InputIterator1 first1, InputIterator1 last1, 446227825Stheraven InputIterator2 first2, InputIterator2 last2, OutputIterator result); 447227825Stheraven 448227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> 449227825Stheraven OutputIterator 450227825Stheraven set_intersection(InputIterator1 first1, InputIterator1 last1, 451227825Stheraven InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); 452227825Stheraven 453227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator> 454227825Stheraven OutputIterator 455227825Stheraven set_difference(InputIterator1 first1, InputIterator1 last1, 456227825Stheraven InputIterator2 first2, InputIterator2 last2, OutputIterator result); 457227825Stheraven 458227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> 459227825Stheraven OutputIterator 460227825Stheraven set_difference(InputIterator1 first1, InputIterator1 last1, 461227825Stheraven InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); 462227825Stheraven 463227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator> 464227825Stheraven OutputIterator 465227825Stheraven set_symmetric_difference(InputIterator1 first1, InputIterator1 last1, 466227825Stheraven InputIterator2 first2, InputIterator2 last2, OutputIterator result); 467227825Stheraven 468227825Stheraventemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> 469227825Stheraven OutputIterator 470227825Stheraven set_symmetric_difference(InputIterator1 first1, InputIterator1 last1, 471227825Stheraven InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); 472227825Stheraven 473227825Stheraventemplate <class RandomAccessIterator> 474227825Stheraven void 475227825Stheraven push_heap(RandomAccessIterator first, RandomAccessIterator last); 476227825Stheraven 477227825Stheraventemplate <class RandomAccessIterator, class Compare> 478227825Stheraven void 479227825Stheraven push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp); 480227825Stheraven 481227825Stheraventemplate <class RandomAccessIterator> 482227825Stheraven void 483227825Stheraven pop_heap(RandomAccessIterator first, RandomAccessIterator last); 484227825Stheraven 485227825Stheraventemplate <class RandomAccessIterator, class Compare> 486227825Stheraven void 487227825Stheraven pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp); 488227825Stheraven 489227825Stheraventemplate <class RandomAccessIterator> 490227825Stheraven void 491227825Stheraven make_heap(RandomAccessIterator first, RandomAccessIterator last); 492227825Stheraven 493227825Stheraventemplate <class RandomAccessIterator, class Compare> 494227825Stheraven void 495227825Stheraven make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp); 496227825Stheraven 497227825Stheraventemplate <class RandomAccessIterator> 498227825Stheraven void 499227825Stheraven sort_heap(RandomAccessIterator first, RandomAccessIterator last); 500227825Stheraven 501227825Stheraventemplate <class RandomAccessIterator, class Compare> 502227825Stheraven void 503227825Stheraven sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp); 504227825Stheraven 505227825Stheraventemplate <class RandomAccessIterator> 506227825Stheraven bool 507227825Stheraven is_heap(RandomAccessIterator first, RandomAccessiterator last); 508227825Stheraven 509227825Stheraventemplate <class RandomAccessIterator, class Compare> 510227825Stheraven bool 511227825Stheraven is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp); 512227825Stheraven 513227825Stheraventemplate <class RandomAccessIterator> 514227825Stheraven RandomAccessIterator 515227825Stheraven is_heap_until(RandomAccessIterator first, RandomAccessiterator last); 516227825Stheraven 517227825Stheraventemplate <class RandomAccessIterator, class Compare> 518227825Stheraven RandomAccessIterator 519227825Stheraven is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp); 520227825Stheraven 521227825Stheraventemplate <class ForwardIterator> 522227825Stheraven ForwardIterator 523227825Stheraven min_element(ForwardIterator first, ForwardIterator last); 524227825Stheraven 525227825Stheraventemplate <class ForwardIterator, class Compare> 526227825Stheraven ForwardIterator 527227825Stheraven min_element(ForwardIterator first, ForwardIterator last, Compare comp); 528227825Stheraven 529227825Stheraventemplate <class T> 530227825Stheraven const T& 531227825Stheraven min(const T& a, const T& b); 532227825Stheraven 533227825Stheraventemplate <class T, class Compare> 534227825Stheraven const T& 535227825Stheraven min(const T& a, const T& b, Compare comp); 536227825Stheraven 537227825Stheraventemplate<class T> 538227825Stheraven T 539227825Stheraven min(initializer_list<T> t); 540227825Stheraven 541227825Stheraventemplate<class T, class Compare> 542227825Stheraven T 543227825Stheraven min(initializer_list<T> t, Compare comp); 544227825Stheraven 545227825Stheraventemplate <class ForwardIterator> 546227825Stheraven ForwardIterator 547227825Stheraven max_element(ForwardIterator first, ForwardIterator last); 548227825Stheraven 549227825Stheraventemplate <class ForwardIterator, class Compare> 550227825Stheraven ForwardIterator 551227825Stheraven max_element(ForwardIterator first, ForwardIterator last, Compare comp); 552227825Stheraven 553227825Stheraventemplate <class T> 554227825Stheraven const T& 555227825Stheraven max(const T& a, const T& b); 556227825Stheraven 557227825Stheraventemplate <class T, class Compare> 558227825Stheraven const T& 559227825Stheraven max(const T& a, const T& b, Compare comp); 560227825Stheraven 561227825Stheraventemplate<class T> 562227825Stheraven T 563227825Stheraven max(initializer_list<T> t); 564227825Stheraven 565227825Stheraventemplate<class T, class Compare> 566227825Stheraven T 567227825Stheraven max(initializer_list<T> t, Compare comp); 568227825Stheraven 569227825Stheraventemplate<class ForwardIterator> 570227825Stheraven pair<ForwardIterator, ForwardIterator> 571227825Stheraven minmax_element(ForwardIterator first, ForwardIterator last); 572227825Stheraven 573227825Stheraventemplate<class ForwardIterator, class Compare> 574227825Stheraven pair<ForwardIterator, ForwardIterator> 575227825Stheraven minmax_element(ForwardIterator first, ForwardIterator last, Compare comp); 576227825Stheraven 577227825Stheraventemplate<class T> 578227825Stheraven pair<const T&, const T&> 579227825Stheraven minmax(const T& a, const T& b); 580227825Stheraven 581227825Stheraventemplate<class T, class Compare> 582227825Stheraven pair<const T&, const T&> 583227825Stheraven minmax(const T& a, const T& b, Compare comp); 584227825Stheraven 585227825Stheraventemplate<class T> 586227825Stheraven pair<T, T> 587227825Stheraven minmax(initializer_list<T> t); 588227825Stheraven 589227825Stheraventemplate<class T, class Compare> 590227825Stheraven pair<T, T> 591227825Stheraven minmax(initializer_list<T> t, Compare comp); 592227825Stheraven 593227825Stheraventemplate <class InputIterator1, class InputIterator2> 594227825Stheraven bool 595227825Stheraven lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2); 596227825Stheraven 597227825Stheraventemplate <class InputIterator1, class InputIterator2, class Compare> 598227825Stheraven bool 599227825Stheraven lexicographical_compare(InputIterator1 first1, InputIterator1 last1, 600227825Stheraven InputIterator2 first2, InputIterator2 last2, Compare comp); 601227825Stheraven 602227825Stheraventemplate <class BidirectionalIterator> 603227825Stheraven bool 604227825Stheraven next_permutation(BidirectionalIterator first, BidirectionalIterator last); 605227825Stheraven 606227825Stheraventemplate <class BidirectionalIterator, class Compare> 607227825Stheraven bool 608227825Stheraven next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp); 609227825Stheraven 610227825Stheraventemplate <class BidirectionalIterator> 611227825Stheraven bool 612227825Stheraven prev_permutation(BidirectionalIterator first, BidirectionalIterator last); 613227825Stheraven 614227825Stheraventemplate <class BidirectionalIterator, class Compare> 615227825Stheraven bool 616227825Stheraven prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp); 617227825Stheraven 618227825Stheraven} // std 619227825Stheraven 620227825Stheraven*/ 621227825Stheraven 622227825Stheraven#include <__config> 623227825Stheraven#include <initializer_list> 624227825Stheraven#include <type_traits> 625227825Stheraven#include <cstring> 626227825Stheraven#include <utility> 627227825Stheraven#include <memory> 628227825Stheraven#include <iterator> 629241903Sdim#include <cstddef> 630227825Stheraven 631232950Stheraven#include <__undef_min_max> 632232950Stheraven 633227825Stheraven#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 634227825Stheraven#pragma GCC system_header 635227825Stheraven#endif 636227825Stheraven 637227825Stheraven_LIBCPP_BEGIN_NAMESPACE_STD 638227825Stheraven 639227825Stheraventemplate <class _T1, class _T2 = _T1> 640227825Stheravenstruct __equal_to 641227825Stheraven{ 642227825Stheraven _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;} 643227825Stheraven _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;} 644227825Stheraven _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;} 645227825Stheraven _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;} 646227825Stheraven}; 647227825Stheraven 648227825Stheraventemplate <class _T1> 649227825Stheravenstruct __equal_to<_T1, _T1> 650227825Stheraven{ 651227825Stheraven _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;} 652227825Stheraven}; 653227825Stheraven 654227825Stheraventemplate <class _T1> 655227825Stheravenstruct __equal_to<const _T1, _T1> 656227825Stheraven{ 657227825Stheraven _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;} 658227825Stheraven}; 659227825Stheraven 660227825Stheraventemplate <class _T1> 661227825Stheravenstruct __equal_to<_T1, const _T1> 662227825Stheraven{ 663227825Stheraven _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;} 664227825Stheraven}; 665227825Stheraven 666227825Stheraventemplate <class _T1, class _T2 = _T1> 667227825Stheravenstruct __less 668227825Stheraven{ 669227825Stheraven _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;} 670227825Stheraven _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;} 671227825Stheraven _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;} 672227825Stheraven _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;} 673227825Stheraven}; 674227825Stheraven 675227825Stheraventemplate <class _T1> 676227825Stheravenstruct __less<_T1, _T1> 677227825Stheraven{ 678227825Stheraven _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;} 679227825Stheraven}; 680227825Stheraven 681227825Stheraventemplate <class _T1> 682227825Stheravenstruct __less<const _T1, _T1> 683227825Stheraven{ 684227825Stheraven _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;} 685227825Stheraven}; 686227825Stheraven 687227825Stheraventemplate <class _T1> 688227825Stheravenstruct __less<_T1, const _T1> 689227825Stheraven{ 690227825Stheraven _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;} 691227825Stheraven}; 692227825Stheraven 693227825Stheraventemplate <class _Predicate> 694227825Stheravenclass __negate 695227825Stheraven{ 696227825Stheravenprivate: 697227825Stheraven _Predicate __p_; 698227825Stheravenpublic: 699227825Stheraven _LIBCPP_INLINE_VISIBILITY __negate() {} 700227825Stheraven 701227825Stheraven _LIBCPP_INLINE_VISIBILITY 702227825Stheraven explicit __negate(_Predicate __p) : __p_(__p) {} 703227825Stheraven 704227825Stheraven template <class _T1> 705227825Stheraven _LIBCPP_INLINE_VISIBILITY 706227825Stheraven bool operator()(const _T1& __x) {return !__p_(__x);} 707227825Stheraven 708227825Stheraven template <class _T1, class _T2> 709227825Stheraven _LIBCPP_INLINE_VISIBILITY 710227825Stheraven bool operator()(const _T1& __x, const _T2& __y) {return !__p_(__x, __y);} 711227825Stheraven}; 712227825Stheraven 713227825Stheraven#ifdef _LIBCPP_DEBUG2 714227825Stheraven 715227825Stheraventemplate <class _Compare> 716227825Stheravenstruct __debug_less 717227825Stheraven{ 718227825Stheraven _Compare __comp_; 719227825Stheraven __debug_less(_Compare& __c) : __comp_(__c) {} 720227825Stheraven template <class _Tp, class _Up> 721227825Stheraven bool operator()(const _Tp& __x, const _Up& __y) 722227825Stheraven { 723227825Stheraven bool __r = __comp_(__x, __y); 724227825Stheraven if (__r) 725227825Stheraven _LIBCPP_ASSERT(!__comp_(__y, __x), "Comparator does not induce a strict weak ordering"); 726227825Stheraven return __r; 727227825Stheraven } 728227825Stheraven}; 729227825Stheraven 730227825Stheraven#endif // _LIBCPP_DEBUG2 731227825Stheraven 732227825Stheraven// Precondition: __x != 0 733232950Stheraveninline _LIBCPP_INLINE_VISIBILITY 734232950Stheravenunsigned 735232950Stheraven__ctz(unsigned __x) 736232950Stheraven{ 737232950Stheraven return static_cast<unsigned>(__builtin_ctz(__x)); 738232950Stheraven} 739227825Stheraven 740232950Stheraveninline _LIBCPP_INLINE_VISIBILITY 741232950Stheravenunsigned long 742232950Stheraven__ctz(unsigned long __x) 743232950Stheraven{ 744232950Stheraven return static_cast<unsigned long>(__builtin_ctzl(__x)); 745232950Stheraven} 746232950Stheraven 747232950Stheraveninline _LIBCPP_INLINE_VISIBILITY 748232950Stheravenunsigned long long 749232950Stheraven__ctz(unsigned long long __x) 750232950Stheraven{ 751232950Stheraven return static_cast<unsigned long long>(__builtin_ctzll(__x)); 752232950Stheraven} 753232950Stheraven 754227825Stheraven// Precondition: __x != 0 755232950Stheraveninline _LIBCPP_INLINE_VISIBILITY 756232950Stheravenunsigned 757232950Stheraven__clz(unsigned __x) 758232950Stheraven{ 759232950Stheraven return static_cast<unsigned>(__builtin_clz(__x)); 760232950Stheraven} 761227825Stheraven 762232950Stheraveninline _LIBCPP_INLINE_VISIBILITY 763232950Stheravenunsigned long 764232950Stheraven__clz(unsigned long __x) 765232950Stheraven{ 766232950Stheraven return static_cast<unsigned long>(__builtin_clzl (__x)); 767232950Stheraven} 768232950Stheraven 769232950Stheraveninline _LIBCPP_INLINE_VISIBILITY 770232950Stheravenunsigned long long 771232950Stheraven__clz(unsigned long long __x) 772232950Stheraven{ 773232950Stheraven return static_cast<unsigned long long>(__builtin_clzll(__x)); 774232950Stheraven} 775232950Stheraven 776227825Stheraveninline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned __x) {return __builtin_popcount (__x);} 777227825Stheraveninline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long __x) {return __builtin_popcountl (__x);} 778227825Stheraveninline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {return __builtin_popcountll(__x);} 779227825Stheraven 780227825Stheraven// all_of 781227825Stheraven 782227825Stheraventemplate <class _InputIterator, class _Predicate> 783227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 784227825Stheravenbool 785227825Stheravenall_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) 786227825Stheraven{ 787227825Stheraven for (; __first != __last; ++__first) 788227825Stheraven if (!__pred(*__first)) 789227825Stheraven return false; 790227825Stheraven return true; 791227825Stheraven} 792227825Stheraven 793227825Stheraven// any_of 794227825Stheraven 795227825Stheraventemplate <class _InputIterator, class _Predicate> 796227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 797227825Stheravenbool 798227825Stheravenany_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) 799227825Stheraven{ 800227825Stheraven for (; __first != __last; ++__first) 801227825Stheraven if (__pred(*__first)) 802227825Stheraven return true; 803227825Stheraven return false; 804227825Stheraven} 805227825Stheraven 806227825Stheraven// none_of 807227825Stheraven 808227825Stheraventemplate <class _InputIterator, class _Predicate> 809227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 810227825Stheravenbool 811227825Stheravennone_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) 812227825Stheraven{ 813227825Stheraven for (; __first != __last; ++__first) 814227825Stheraven if (__pred(*__first)) 815227825Stheraven return false; 816227825Stheraven return true; 817227825Stheraven} 818227825Stheraven 819227825Stheraven// for_each 820227825Stheraven 821227825Stheraventemplate <class _InputIterator, class _Function> 822227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 823227825Stheraven_Function 824227825Stheravenfor_each(_InputIterator __first, _InputIterator __last, _Function __f) 825227825Stheraven{ 826227825Stheraven for (; __first != __last; ++__first) 827227825Stheraven __f(*__first); 828227825Stheraven return _VSTD::move(__f); 829227825Stheraven} 830227825Stheraven 831227825Stheraven// find 832227825Stheraven 833227825Stheraventemplate <class _InputIterator, class _Tp> 834227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 835227825Stheraven_InputIterator 836227825Stheravenfind(_InputIterator __first, _InputIterator __last, const _Tp& __value_) 837227825Stheraven{ 838227825Stheraven for (; __first != __last; ++__first) 839227825Stheraven if (*__first == __value_) 840227825Stheraven break; 841227825Stheraven return __first; 842227825Stheraven} 843227825Stheraven 844227825Stheraven// find_if 845227825Stheraven 846227825Stheraventemplate <class _InputIterator, class _Predicate> 847227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 848227825Stheraven_InputIterator 849227825Stheravenfind_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) 850227825Stheraven{ 851227825Stheraven for (; __first != __last; ++__first) 852227825Stheraven if (__pred(*__first)) 853227825Stheraven break; 854227825Stheraven return __first; 855227825Stheraven} 856227825Stheraven 857227825Stheraven// find_if_not 858227825Stheraven 859227825Stheraventemplate<class _InputIterator, class _Predicate> 860227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 861227825Stheraven_InputIterator 862227825Stheravenfind_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred) 863227825Stheraven{ 864227825Stheraven for (; __first != __last; ++__first) 865227825Stheraven if (!__pred(*__first)) 866227825Stheraven break; 867227825Stheraven return __first; 868227825Stheraven} 869227825Stheraven 870227825Stheraven// find_end 871227825Stheraven 872227825Stheraventemplate <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2> 873227825Stheraven_ForwardIterator1 874227825Stheraven__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 875227825Stheraven _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred, 876227825Stheraven forward_iterator_tag, forward_iterator_tag) 877227825Stheraven{ 878227825Stheraven // modeled after search algorithm 879227825Stheraven _ForwardIterator1 __r = __last1; // __last1 is the "default" answer 880227825Stheraven if (__first2 == __last2) 881227825Stheraven return __r; 882227825Stheraven while (true) 883227825Stheraven { 884227825Stheraven while (true) 885227825Stheraven { 886227825Stheraven if (__first1 == __last1) // if source exhausted return last correct answer 887227825Stheraven return __r; // (or __last1 if never found) 888227825Stheraven if (__pred(*__first1, *__first2)) 889227825Stheraven break; 890227825Stheraven ++__first1; 891227825Stheraven } 892227825Stheraven // *__first1 matches *__first2, now match elements after here 893227825Stheraven _ForwardIterator1 __m1 = __first1; 894227825Stheraven _ForwardIterator2 __m2 = __first2; 895227825Stheraven while (true) 896227825Stheraven { 897227825Stheraven if (++__m2 == __last2) 898227825Stheraven { // Pattern exhaused, record answer and search for another one 899227825Stheraven __r = __first1; 900227825Stheraven ++__first1; 901227825Stheraven break; 902227825Stheraven } 903227825Stheraven if (++__m1 == __last1) // Source exhausted, return last answer 904227825Stheraven return __r; 905227825Stheraven if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first 906227825Stheraven { 907227825Stheraven ++__first1; 908227825Stheraven break; 909227825Stheraven } // else there is a match, check next elements 910227825Stheraven } 911227825Stheraven } 912227825Stheraven} 913227825Stheraven 914227825Stheraventemplate <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2> 915227825Stheraven_BidirectionalIterator1 916227825Stheraven__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1, 917227825Stheraven _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred, 918227825Stheraven bidirectional_iterator_tag, bidirectional_iterator_tag) 919227825Stheraven{ 920227825Stheraven // modeled after search algorithm (in reverse) 921227825Stheraven if (__first2 == __last2) 922227825Stheraven return __last1; // Everything matches an empty sequence 923227825Stheraven _BidirectionalIterator1 __l1 = __last1; 924227825Stheraven _BidirectionalIterator2 __l2 = __last2; 925227825Stheraven --__l2; 926227825Stheraven while (true) 927227825Stheraven { 928227825Stheraven // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks 929227825Stheraven while (true) 930227825Stheraven { 931227825Stheraven if (__first1 == __l1) // return __last1 if no element matches *__first2 932227825Stheraven return __last1; 933227825Stheraven if (__pred(*--__l1, *__l2)) 934227825Stheraven break; 935227825Stheraven } 936227825Stheraven // *__l1 matches *__l2, now match elements before here 937227825Stheraven _BidirectionalIterator1 __m1 = __l1; 938227825Stheraven _BidirectionalIterator2 __m2 = __l2; 939227825Stheraven while (true) 940227825Stheraven { 941227825Stheraven if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern) 942227825Stheraven return __m1; 943227825Stheraven if (__m1 == __first1) // Otherwise if source exhaused, pattern not found 944227825Stheraven return __last1; 945227825Stheraven if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1 946227825Stheraven { 947227825Stheraven break; 948227825Stheraven } // else there is a match, check next elements 949227825Stheraven } 950227825Stheraven } 951227825Stheraven} 952227825Stheraven 953227825Stheraventemplate <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2> 954227825Stheraven_RandomAccessIterator1 955227825Stheraven__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, 956227825Stheraven _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred, 957227825Stheraven random_access_iterator_tag, random_access_iterator_tag) 958227825Stheraven{ 959227825Stheraven // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern 960227825Stheraven typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2; 961227825Stheraven if (__len2 == 0) 962227825Stheraven return __last1; 963227825Stheraven typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1; 964227825Stheraven if (__len1 < __len2) 965227825Stheraven return __last1; 966227825Stheraven const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here 967227825Stheraven _RandomAccessIterator1 __l1 = __last1; 968227825Stheraven _RandomAccessIterator2 __l2 = __last2; 969227825Stheraven --__l2; 970227825Stheraven while (true) 971227825Stheraven { 972227825Stheraven while (true) 973227825Stheraven { 974227825Stheraven if (__s == __l1) 975227825Stheraven return __last1; 976227825Stheraven if (__pred(*--__l1, *__l2)) 977227825Stheraven break; 978227825Stheraven } 979227825Stheraven _RandomAccessIterator1 __m1 = __l1; 980227825Stheraven _RandomAccessIterator2 __m2 = __l2; 981227825Stheraven while (true) 982227825Stheraven { 983227825Stheraven if (__m2 == __first2) 984227825Stheraven return __m1; 985227825Stheraven // no need to check range on __m1 because __s guarantees we have enough source 986227825Stheraven if (!__pred(*--__m1, *--__m2)) 987227825Stheraven { 988227825Stheraven break; 989227825Stheraven } 990227825Stheraven } 991227825Stheraven } 992227825Stheraven} 993227825Stheraven 994227825Stheraventemplate <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> 995227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 996227825Stheraven_ForwardIterator1 997227825Stheravenfind_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 998227825Stheraven _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred) 999227825Stheraven{ 1000227825Stheraven return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type> 1001227825Stheraven (__first1, __last1, __first2, __last2, __pred, 1002227825Stheraven typename iterator_traits<_ForwardIterator1>::iterator_category(), 1003227825Stheraven typename iterator_traits<_ForwardIterator2>::iterator_category()); 1004227825Stheraven} 1005227825Stheraven 1006227825Stheraventemplate <class _ForwardIterator1, class _ForwardIterator2> 1007227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1008227825Stheraven_ForwardIterator1 1009227825Stheravenfind_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 1010227825Stheraven _ForwardIterator2 __first2, _ForwardIterator2 __last2) 1011227825Stheraven{ 1012227825Stheraven typedef typename iterator_traits<_ForwardIterator1>::value_type __v1; 1013227825Stheraven typedef typename iterator_traits<_ForwardIterator2>::value_type __v2; 1014227825Stheraven return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>()); 1015227825Stheraven} 1016227825Stheraven 1017227825Stheraven// find_first_of 1018227825Stheraven 1019227825Stheraventemplate <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> 1020227825Stheraven_ForwardIterator1 1021227825Stheravenfind_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 1022227825Stheraven _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred) 1023227825Stheraven{ 1024227825Stheraven for (; __first1 != __last1; ++__first1) 1025227825Stheraven for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j) 1026227825Stheraven if (__pred(*__first1, *__j)) 1027227825Stheraven return __first1; 1028227825Stheraven return __last1; 1029227825Stheraven} 1030227825Stheraven 1031227825Stheraventemplate <class _ForwardIterator1, class _ForwardIterator2> 1032227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1033227825Stheraven_ForwardIterator1 1034227825Stheravenfind_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 1035227825Stheraven _ForwardIterator2 __first2, _ForwardIterator2 __last2) 1036227825Stheraven{ 1037227825Stheraven typedef typename iterator_traits<_ForwardIterator1>::value_type __v1; 1038227825Stheraven typedef typename iterator_traits<_ForwardIterator2>::value_type __v2; 1039227825Stheraven return _VSTD::find_first_of(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>()); 1040227825Stheraven} 1041227825Stheraven 1042227825Stheraven// adjacent_find 1043227825Stheraven 1044227825Stheraventemplate <class _ForwardIterator, class _BinaryPredicate> 1045227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1046227825Stheraven_ForwardIterator 1047227825Stheravenadjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred) 1048227825Stheraven{ 1049227825Stheraven if (__first != __last) 1050227825Stheraven { 1051227825Stheraven _ForwardIterator __i = __first; 1052227825Stheraven while (++__i != __last) 1053227825Stheraven { 1054227825Stheraven if (__pred(*__first, *__i)) 1055227825Stheraven return __first; 1056227825Stheraven __first = __i; 1057227825Stheraven } 1058227825Stheraven } 1059227825Stheraven return __last; 1060227825Stheraven} 1061227825Stheraven 1062227825Stheraventemplate <class _ForwardIterator> 1063227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1064227825Stheraven_ForwardIterator 1065227825Stheravenadjacent_find(_ForwardIterator __first, _ForwardIterator __last) 1066227825Stheraven{ 1067227825Stheraven typedef typename iterator_traits<_ForwardIterator>::value_type __v; 1068227825Stheraven return _VSTD::adjacent_find(__first, __last, __equal_to<__v>()); 1069227825Stheraven} 1070227825Stheraven 1071227825Stheraven// count 1072227825Stheraven 1073227825Stheraventemplate <class _InputIterator, class _Tp> 1074227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1075227825Stheraventypename iterator_traits<_InputIterator>::difference_type 1076227825Stheravencount(_InputIterator __first, _InputIterator __last, const _Tp& __value_) 1077227825Stheraven{ 1078227825Stheraven typename iterator_traits<_InputIterator>::difference_type __r(0); 1079227825Stheraven for (; __first != __last; ++__first) 1080227825Stheraven if (*__first == __value_) 1081227825Stheraven ++__r; 1082227825Stheraven return __r; 1083227825Stheraven} 1084227825Stheraven 1085227825Stheraven// count_if 1086227825Stheraven 1087227825Stheraventemplate <class _InputIterator, class _Predicate> 1088227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1089227825Stheraventypename iterator_traits<_InputIterator>::difference_type 1090227825Stheravencount_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) 1091227825Stheraven{ 1092227825Stheraven typename iterator_traits<_InputIterator>::difference_type __r(0); 1093227825Stheraven for (; __first != __last; ++__first) 1094227825Stheraven if (__pred(*__first)) 1095227825Stheraven ++__r; 1096227825Stheraven return __r; 1097227825Stheraven} 1098227825Stheraven 1099227825Stheraven// mismatch 1100227825Stheraven 1101227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _BinaryPredicate> 1102227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1103227825Stheravenpair<_InputIterator1, _InputIterator2> 1104227825Stheravenmismatch(_InputIterator1 __first1, _InputIterator1 __last1, 1105227825Stheraven _InputIterator2 __first2, _BinaryPredicate __pred) 1106227825Stheraven{ 1107227825Stheraven for (; __first1 != __last1; ++__first1, ++__first2) 1108227825Stheraven if (!__pred(*__first1, *__first2)) 1109227825Stheraven break; 1110227825Stheraven return pair<_InputIterator1, _InputIterator2>(__first1, __first2); 1111227825Stheraven} 1112227825Stheraven 1113227825Stheraventemplate <class _InputIterator1, class _InputIterator2> 1114227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1115227825Stheravenpair<_InputIterator1, _InputIterator2> 1116227825Stheravenmismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2) 1117227825Stheraven{ 1118227825Stheraven typedef typename iterator_traits<_InputIterator1>::value_type __v1; 1119227825Stheraven typedef typename iterator_traits<_InputIterator2>::value_type __v2; 1120227825Stheraven return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>()); 1121227825Stheraven} 1122227825Stheraven 1123253159Stheraven#if _LIBCPP_STD_VER > 11 1124253159Stheraventemplate <class _InputIterator1, class _InputIterator2, class _BinaryPredicate> 1125253159Stheraveninline _LIBCPP_INLINE_VISIBILITY 1126253159Stheravenpair<_InputIterator1, _InputIterator2> 1127253159Stheravenmismatch(_InputIterator1 __first1, _InputIterator1 __last1, 1128253159Stheraven _InputIterator2 __first2, _InputIterator2 __last2, 1129253159Stheraven _BinaryPredicate __pred) 1130253159Stheraven{ 1131253159Stheraven for (; __first1 != __last1 && __first2 != __last2; ++__first1, ++__first2) 1132253159Stheraven if (!__pred(*__first1, *__first2)) 1133253159Stheraven break; 1134253159Stheraven return pair<_InputIterator1, _InputIterator2>(__first1, __first2); 1135253159Stheraven} 1136253159Stheraven 1137253159Stheraventemplate <class _InputIterator1, class _InputIterator2> 1138253159Stheraveninline _LIBCPP_INLINE_VISIBILITY 1139253159Stheravenpair<_InputIterator1, _InputIterator2> 1140253159Stheravenmismatch(_InputIterator1 __first1, _InputIterator1 __last1, 1141253159Stheraven _InputIterator2 __first2, _InputIterator2 __last2) 1142253159Stheraven{ 1143253159Stheraven typedef typename iterator_traits<_InputIterator1>::value_type __v1; 1144253159Stheraven typedef typename iterator_traits<_InputIterator2>::value_type __v2; 1145253159Stheraven return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>()); 1146253159Stheraven} 1147253159Stheraven#endif 1148253159Stheraven 1149227825Stheraven// equal 1150227825Stheraven 1151227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _BinaryPredicate> 1152227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1153227825Stheravenbool 1154227825Stheravenequal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred) 1155227825Stheraven{ 1156227825Stheraven for (; __first1 != __last1; ++__first1, ++__first2) 1157227825Stheraven if (!__pred(*__first1, *__first2)) 1158227825Stheraven return false; 1159227825Stheraven return true; 1160227825Stheraven} 1161227825Stheraven 1162227825Stheraventemplate <class _InputIterator1, class _InputIterator2> 1163227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1164227825Stheravenbool 1165227825Stheravenequal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2) 1166227825Stheraven{ 1167227825Stheraven typedef typename iterator_traits<_InputIterator1>::value_type __v1; 1168227825Stheraven typedef typename iterator_traits<_InputIterator2>::value_type __v2; 1169227825Stheraven return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>()); 1170227825Stheraven} 1171227825Stheraven 1172253159Stheraven#if _LIBCPP_STD_VER > 11 1173253159Stheraventemplate <class _BinaryPredicate, class _InputIterator1, class _InputIterator2> 1174253159Stheraveninline _LIBCPP_INLINE_VISIBILITY 1175253159Stheravenbool 1176253159Stheraven__equal(_InputIterator1 __first1, _InputIterator1 __last1, 1177253159Stheraven _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred, 1178253159Stheraven input_iterator_tag, input_iterator_tag ) 1179253159Stheraven{ 1180253159Stheraven for (; __first1 != __last1 && __first2 != __last2; ++__first1, ++__first2) 1181253159Stheraven if (!__pred(*__first1, *__first2)) 1182253159Stheraven return false; 1183253159Stheraven return __first1 == __last1 && __first2 == __last2; 1184253159Stheraven} 1185253159Stheraven 1186253159Stheraventemplate <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2> 1187253159Stheraveninline _LIBCPP_INLINE_VISIBILITY 1188253159Stheravenbool 1189253159Stheraven__equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, 1190253159Stheraven _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred, 1191253159Stheraven random_access_iterator_tag, random_access_iterator_tag ) 1192253159Stheraven{ 1193253159Stheraven if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2)) 1194253159Stheraven return false; 1195253159Stheraven return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2, 1196253159Stheraven typename add_lvalue_reference<_BinaryPredicate>::type> 1197253159Stheraven (__first1, __last1, __first2, __pred ); 1198253159Stheraven} 1199253159Stheraven 1200253159Stheraventemplate <class _InputIterator1, class _InputIterator2, class _BinaryPredicate> 1201253159Stheraveninline _LIBCPP_INLINE_VISIBILITY 1202253159Stheravenbool 1203253159Stheravenequal(_InputIterator1 __first1, _InputIterator1 __last1, 1204253159Stheraven _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred ) 1205253159Stheraven{ 1206253159Stheraven return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type> 1207253159Stheraven (__first1, __last1, __first2, __last2, __pred, 1208253159Stheraven typename iterator_traits<_InputIterator1>::iterator_category(), 1209253159Stheraven typename iterator_traits<_InputIterator2>::iterator_category()); 1210253159Stheraven} 1211253159Stheraven 1212253159Stheraventemplate <class _InputIterator1, class _InputIterator2> 1213253159Stheraveninline _LIBCPP_INLINE_VISIBILITY 1214253159Stheravenbool 1215253159Stheravenequal(_InputIterator1 __first1, _InputIterator1 __last1, 1216253159Stheraven _InputIterator2 __first2, _InputIterator2 __last2) 1217253159Stheraven{ 1218253159Stheraven typedef typename iterator_traits<_InputIterator1>::value_type __v1; 1219253159Stheraven typedef typename iterator_traits<_InputIterator2>::value_type __v2; 1220253159Stheraven return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(), 1221253159Stheraven typename iterator_traits<_InputIterator1>::iterator_category(), 1222253159Stheraven typename iterator_traits<_InputIterator2>::iterator_category()); 1223253159Stheraven} 1224253159Stheraven#endif 1225253159Stheraven 1226227825Stheraven// is_permutation 1227227825Stheraven 1228227825Stheraventemplate<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> 1229227825Stheravenbool 1230227825Stheravenis_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 1231227825Stheraven _ForwardIterator2 __first2, _BinaryPredicate __pred) 1232227825Stheraven{ 1233227825Stheraven // shorten sequences as much as possible by lopping of any equal parts 1234227825Stheraven for (; __first1 != __last1; ++__first1, ++__first2) 1235227825Stheraven if (!__pred(*__first1, *__first2)) 1236227825Stheraven goto __not_done; 1237227825Stheraven return true; 1238227825Stheraven__not_done: 1239227825Stheraven // __first1 != __last1 && *__first1 != *__first2 1240227825Stheraven typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1; 1241227825Stheraven _D1 __l1 = _VSTD::distance(__first1, __last1); 1242227825Stheraven if (__l1 == _D1(1)) 1243227825Stheraven return false; 1244227825Stheraven _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1); 1245227825Stheraven // For each element in [f1, l1) see if there are the same number of 1246227825Stheraven // equal elements in [f2, l2) 1247227825Stheraven for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i) 1248227825Stheraven { 1249227825Stheraven // Have we already counted the number of *__i in [f1, l1)? 1250227825Stheraven for (_ForwardIterator1 __j = __first1; __j != __i; ++__j) 1251227825Stheraven if (__pred(*__j, *__i)) 1252227825Stheraven goto __next_iter; 1253227825Stheraven { 1254227825Stheraven // Count number of *__i in [f2, l2) 1255227825Stheraven _D1 __c2 = 0; 1256227825Stheraven for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j) 1257227825Stheraven if (__pred(*__i, *__j)) 1258227825Stheraven ++__c2; 1259227825Stheraven if (__c2 == 0) 1260227825Stheraven return false; 1261227825Stheraven // Count number of *__i in [__i, l1) (we can start with 1) 1262227825Stheraven _D1 __c1 = 1; 1263227825Stheraven for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j) 1264227825Stheraven if (__pred(*__i, *__j)) 1265227825Stheraven ++__c1; 1266227825Stheraven if (__c1 != __c2) 1267227825Stheraven return false; 1268227825Stheraven } 1269227825Stheraven__next_iter:; 1270227825Stheraven } 1271227825Stheraven return true; 1272227825Stheraven} 1273227825Stheraven 1274227825Stheraventemplate<class _ForwardIterator1, class _ForwardIterator2> 1275227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1276227825Stheravenbool 1277227825Stheravenis_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 1278227825Stheraven _ForwardIterator2 __first2) 1279227825Stheraven{ 1280227825Stheraven typedef typename iterator_traits<_ForwardIterator1>::value_type __v1; 1281227825Stheraven typedef typename iterator_traits<_ForwardIterator2>::value_type __v2; 1282227825Stheraven return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>()); 1283227825Stheraven} 1284227825Stheraven 1285253159Stheraven#if _LIBCPP_STD_VER > 11 1286253159Stheraventemplate<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2> 1287253159Stheravenbool 1288253159Stheraven__is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 1289253159Stheraven _ForwardIterator2 __first2, _ForwardIterator2 __last2, 1290253159Stheraven _BinaryPredicate __pred, 1291253159Stheraven forward_iterator_tag, forward_iterator_tag ) 1292253159Stheraven{ 1293253159Stheraven // shorten sequences as much as possible by lopping of any equal parts 1294253159Stheraven for (; __first1 != __last1 && __first2 != __last2; ++__first1, ++__first2) 1295253159Stheraven if (!__pred(*__first1, *__first2)) 1296253159Stheraven goto __not_done; 1297253159Stheraven return __first1 == __last1 && __first2 == __last2; 1298253159Stheraven__not_done: 1299253159Stheraven // __first1 != __last1 && __first2 != __last2 && *__first1 != *__first2 1300253159Stheraven typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1; 1301253159Stheraven _D1 __l1 = _VSTD::distance(__first1, __last1); 1302253159Stheraven 1303253159Stheraven typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2; 1304253159Stheraven _D2 __l2 = _VSTD::distance(__first2, __last2); 1305253159Stheraven if (__l1 != __l2) 1306253159Stheraven return false; 1307253159Stheraven 1308253159Stheraven // For each element in [f1, l1) see if there are the same number of 1309253159Stheraven // equal elements in [f2, l2) 1310253159Stheraven for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i) 1311253159Stheraven { 1312253159Stheraven // Have we already counted the number of *__i in [f1, l1)? 1313253159Stheraven for (_ForwardIterator1 __j = __first1; __j != __i; ++__j) 1314253159Stheraven if (__pred(*__j, *__i)) 1315253159Stheraven goto __next_iter; 1316253159Stheraven { 1317253159Stheraven // Count number of *__i in [f2, l2) 1318253159Stheraven _D1 __c2 = 0; 1319253159Stheraven for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j) 1320253159Stheraven if (__pred(*__i, *__j)) 1321253159Stheraven ++__c2; 1322253159Stheraven if (__c2 == 0) 1323253159Stheraven return false; 1324253159Stheraven // Count number of *__i in [__i, l1) (we can start with 1) 1325253159Stheraven _D1 __c1 = 1; 1326253159Stheraven for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j) 1327253159Stheraven if (__pred(*__i, *__j)) 1328253159Stheraven ++__c1; 1329253159Stheraven if (__c1 != __c2) 1330253159Stheraven return false; 1331253159Stheraven } 1332253159Stheraven__next_iter:; 1333253159Stheraven } 1334253159Stheraven return true; 1335253159Stheraven} 1336253159Stheraven 1337253159Stheraventemplate<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2> 1338253159Stheravenbool 1339253159Stheraven__is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1, 1340253159Stheraven _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2, 1341253159Stheraven _BinaryPredicate __pred, 1342253159Stheraven random_access_iterator_tag, random_access_iterator_tag ) 1343253159Stheraven{ 1344253159Stheraven if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2)) 1345253159Stheraven return false; 1346253159Stheraven return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2, 1347253159Stheraven typename add_lvalue_reference<_BinaryPredicate>::type> 1348253159Stheraven (__first1, __last1, __first2, __pred ); 1349253159Stheraven} 1350253159Stheraven 1351253159Stheraventemplate<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> 1352253159Stheraveninline _LIBCPP_INLINE_VISIBILITY 1353253159Stheravenbool 1354253159Stheravenis_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 1355253159Stheraven _ForwardIterator2 __first2, _ForwardIterator2 __last2, 1356253159Stheraven _BinaryPredicate __pred ) 1357253159Stheraven{ 1358253159Stheraven return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type> 1359253159Stheraven (__first1, __last1, __first2, __last2, __pred, 1360253159Stheraven typename iterator_traits<_ForwardIterator1>::iterator_category(), 1361253159Stheraven typename iterator_traits<_ForwardIterator2>::iterator_category()); 1362253159Stheraven} 1363253159Stheraven 1364253159Stheraventemplate<class _ForwardIterator1, class _ForwardIterator2> 1365253159Stheraveninline _LIBCPP_INLINE_VISIBILITY 1366253159Stheravenbool 1367253159Stheravenis_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 1368253159Stheraven _ForwardIterator2 __first2, _ForwardIterator2 __last2) 1369253159Stheraven{ 1370253159Stheraven typedef typename iterator_traits<_ForwardIterator1>::value_type __v1; 1371253159Stheraven typedef typename iterator_traits<_ForwardIterator2>::value_type __v2; 1372253159Stheraven return _VSTD::__is_permutation(__first1, __last1, __first2, __last2, 1373253159Stheraven __equal_to<__v1, __v2>(), 1374253159Stheraven typename iterator_traits<_ForwardIterator1>::iterator_category(), 1375253159Stheraven typename iterator_traits<_ForwardIterator2>::iterator_category()); 1376253159Stheraven} 1377253159Stheraven#endif 1378253159Stheraven 1379227825Stheraven// search 1380227825Stheraven 1381227825Stheraventemplate <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2> 1382227825Stheraven_ForwardIterator1 1383227825Stheraven__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 1384227825Stheraven _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred, 1385227825Stheraven forward_iterator_tag, forward_iterator_tag) 1386227825Stheraven{ 1387227825Stheraven if (__first2 == __last2) 1388227825Stheraven return __first1; // Everything matches an empty sequence 1389227825Stheraven while (true) 1390227825Stheraven { 1391227825Stheraven // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks 1392227825Stheraven while (true) 1393227825Stheraven { 1394227825Stheraven if (__first1 == __last1) // return __last1 if no element matches *__first2 1395227825Stheraven return __last1; 1396227825Stheraven if (__pred(*__first1, *__first2)) 1397227825Stheraven break; 1398227825Stheraven ++__first1; 1399227825Stheraven } 1400227825Stheraven // *__first1 matches *__first2, now match elements after here 1401227825Stheraven _ForwardIterator1 __m1 = __first1; 1402227825Stheraven _ForwardIterator2 __m2 = __first2; 1403227825Stheraven while (true) 1404227825Stheraven { 1405227825Stheraven if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern) 1406227825Stheraven return __first1; 1407227825Stheraven if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found 1408227825Stheraven return __last1; 1409227825Stheraven if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1 1410227825Stheraven { 1411227825Stheraven ++__first1; 1412227825Stheraven break; 1413227825Stheraven } // else there is a match, check next elements 1414227825Stheraven } 1415227825Stheraven } 1416227825Stheraven} 1417227825Stheraven 1418227825Stheraventemplate <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2> 1419227825Stheraven_RandomAccessIterator1 1420227825Stheraven__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, 1421227825Stheraven _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred, 1422227825Stheraven random_access_iterator_tag, random_access_iterator_tag) 1423227825Stheraven{ 1424227825Stheraven typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _D1; 1425227825Stheraven typedef typename std::iterator_traits<_RandomAccessIterator2>::difference_type _D2; 1426227825Stheraven // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern 1427227825Stheraven _D2 __len2 = __last2 - __first2; 1428227825Stheraven if (__len2 == 0) 1429227825Stheraven return __first1; 1430227825Stheraven _D1 __len1 = __last1 - __first1; 1431227825Stheraven if (__len1 < __len2) 1432227825Stheraven return __last1; 1433227825Stheraven const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here 1434227825Stheraven while (true) 1435227825Stheraven { 1436227825Stheraven#if !_LIBCPP_UNROLL_LOOPS 1437227825Stheraven while (true) 1438227825Stheraven { 1439227825Stheraven if (__first1 == __s) 1440227825Stheraven return __last1; 1441227825Stheraven if (__pred(*__first1, *__first2)) 1442227825Stheraven break; 1443227825Stheraven ++__first1; 1444227825Stheraven } 1445227825Stheraven#else // !_LIBCPP_UNROLL_LOOPS 1446227825Stheraven for (_D1 __loop_unroll = (__s - __first1) / 4; __loop_unroll > 0; --__loop_unroll) 1447227825Stheraven { 1448227825Stheraven if (__pred(*__first1, *__first2)) 1449227825Stheraven goto __phase2; 1450227825Stheraven if (__pred(*++__first1, *__first2)) 1451227825Stheraven goto __phase2; 1452227825Stheraven if (__pred(*++__first1, *__first2)) 1453227825Stheraven goto __phase2; 1454227825Stheraven if (__pred(*++__first1, *__first2)) 1455227825Stheraven goto __phase2; 1456227825Stheraven ++__first1; 1457227825Stheraven } 1458227825Stheraven switch (__s - __first1) 1459227825Stheraven { 1460227825Stheraven case 3: 1461227825Stheraven if (__pred(*__first1, *__first2)) 1462227825Stheraven break; 1463227825Stheraven ++__first1; 1464227825Stheraven case 2: 1465227825Stheraven if (__pred(*__first1, *__first2)) 1466227825Stheraven break; 1467227825Stheraven ++__first1; 1468227825Stheraven case 1: 1469227825Stheraven if (__pred(*__first1, *__first2)) 1470227825Stheraven break; 1471227825Stheraven case 0: 1472227825Stheraven return __last1; 1473227825Stheraven } 1474227825Stheraven __phase2: 1475227825Stheraven#endif // !_LIBCPP_UNROLL_LOOPS 1476227825Stheraven _RandomAccessIterator1 __m1 = __first1; 1477227825Stheraven _RandomAccessIterator2 __m2 = __first2; 1478227825Stheraven#if !_LIBCPP_UNROLL_LOOPS 1479227825Stheraven while (true) 1480227825Stheraven { 1481227825Stheraven if (++__m2 == __last2) 1482227825Stheraven return __first1; 1483227825Stheraven ++__m1; // no need to check range on __m1 because __s guarantees we have enough source 1484227825Stheraven if (!__pred(*__m1, *__m2)) 1485227825Stheraven { 1486227825Stheraven ++__first1; 1487227825Stheraven break; 1488227825Stheraven } 1489227825Stheraven } 1490227825Stheraven#else // !_LIBCPP_UNROLL_LOOPS 1491227825Stheraven ++__m2; 1492227825Stheraven ++__m1; 1493227825Stheraven for (_D2 __loop_unroll = (__last2 - __m2) / 4; __loop_unroll > 0; --__loop_unroll) 1494227825Stheraven { 1495227825Stheraven if (!__pred(*__m1, *__m2)) 1496227825Stheraven goto __continue; 1497227825Stheraven if (!__pred(*++__m1, *++__m2)) 1498227825Stheraven goto __continue; 1499227825Stheraven if (!__pred(*++__m1, *++__m2)) 1500227825Stheraven goto __continue; 1501227825Stheraven if (!__pred(*++__m1, *++__m2)) 1502227825Stheraven goto __continue; 1503227825Stheraven ++__m1; 1504227825Stheraven ++__m2; 1505227825Stheraven } 1506227825Stheraven switch (__last2 - __m2) 1507227825Stheraven { 1508227825Stheraven case 3: 1509227825Stheraven if (!__pred(*__m1, *__m2)) 1510227825Stheraven break; 1511227825Stheraven ++__m1; 1512227825Stheraven ++__m2; 1513227825Stheraven case 2: 1514227825Stheraven if (!__pred(*__m1, *__m2)) 1515227825Stheraven break; 1516227825Stheraven ++__m1; 1517227825Stheraven ++__m2; 1518227825Stheraven case 1: 1519227825Stheraven if (!__pred(*__m1, *__m2)) 1520227825Stheraven break; 1521227825Stheraven case 0: 1522227825Stheraven return __first1; 1523227825Stheraven } 1524227825Stheraven __continue: 1525227825Stheraven ++__first1; 1526227825Stheraven#endif // !_LIBCPP_UNROLL_LOOPS 1527227825Stheraven } 1528227825Stheraven} 1529227825Stheraven 1530227825Stheraventemplate <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> 1531227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1532227825Stheraven_ForwardIterator1 1533227825Stheravensearch(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 1534227825Stheraven _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred) 1535227825Stheraven{ 1536227825Stheraven return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type> 1537227825Stheraven (__first1, __last1, __first2, __last2, __pred, 1538227825Stheraven typename std::iterator_traits<_ForwardIterator1>::iterator_category(), 1539227825Stheraven typename std::iterator_traits<_ForwardIterator2>::iterator_category()); 1540227825Stheraven} 1541227825Stheraven 1542227825Stheraventemplate <class _ForwardIterator1, class _ForwardIterator2> 1543227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1544227825Stheraven_ForwardIterator1 1545227825Stheravensearch(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 1546227825Stheraven _ForwardIterator2 __first2, _ForwardIterator2 __last2) 1547227825Stheraven{ 1548227825Stheraven typedef typename std::iterator_traits<_ForwardIterator1>::value_type __v1; 1549227825Stheraven typedef typename std::iterator_traits<_ForwardIterator2>::value_type __v2; 1550227825Stheraven return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>()); 1551227825Stheraven} 1552227825Stheraven 1553227825Stheraven// search_n 1554227825Stheraven 1555227825Stheraventemplate <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp> 1556227825Stheraven_ForwardIterator 1557227825Stheraven__search_n(_ForwardIterator __first, _ForwardIterator __last, 1558227825Stheraven _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag) 1559227825Stheraven{ 1560227825Stheraven if (__count <= 0) 1561227825Stheraven return __first; 1562227825Stheraven while (true) 1563227825Stheraven { 1564227825Stheraven // Find first element in sequence that matchs __value_, with a mininum of loop checks 1565227825Stheraven while (true) 1566227825Stheraven { 1567227825Stheraven if (__first == __last) // return __last if no element matches __value_ 1568227825Stheraven return __last; 1569227825Stheraven if (__pred(*__first, __value_)) 1570227825Stheraven break; 1571227825Stheraven ++__first; 1572227825Stheraven } 1573227825Stheraven // *__first matches __value_, now match elements after here 1574227825Stheraven _ForwardIterator __m = __first; 1575227825Stheraven _Size __c(0); 1576227825Stheraven while (true) 1577227825Stheraven { 1578227825Stheraven if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern) 1579227825Stheraven return __first; 1580227825Stheraven if (++__m == __last) // Otherwise if source exhaused, pattern not found 1581227825Stheraven return __last; 1582227825Stheraven if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first 1583227825Stheraven { 1584227825Stheraven __first = __m; 1585227825Stheraven ++__first; 1586227825Stheraven break; 1587227825Stheraven } // else there is a match, check next elements 1588227825Stheraven } 1589227825Stheraven } 1590227825Stheraven} 1591227825Stheraven 1592227825Stheraventemplate <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp> 1593227825Stheraven_RandomAccessIterator 1594227825Stheraven__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last, 1595227825Stheraven _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag) 1596227825Stheraven{ 1597227825Stheraven if (__count <= 0) 1598227825Stheraven return __first; 1599227825Stheraven _Size __len = static_cast<_Size>(__last - __first); 1600227825Stheraven if (__len < __count) 1601227825Stheraven return __last; 1602227825Stheraven const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here 1603227825Stheraven while (true) 1604227825Stheraven { 1605227825Stheraven // Find first element in sequence that matchs __value_, with a mininum of loop checks 1606227825Stheraven while (true) 1607227825Stheraven { 1608249998Sdim if (__first >= __s) // return __last if no element matches __value_ 1609227825Stheraven return __last; 1610227825Stheraven if (__pred(*__first, __value_)) 1611227825Stheraven break; 1612227825Stheraven ++__first; 1613227825Stheraven } 1614227825Stheraven // *__first matches __value_, now match elements after here 1615227825Stheraven _RandomAccessIterator __m = __first; 1616227825Stheraven _Size __c(0); 1617227825Stheraven while (true) 1618227825Stheraven { 1619227825Stheraven if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern) 1620227825Stheraven return __first; 1621227825Stheraven ++__m; // no need to check range on __m because __s guarantees we have enough source 1622227825Stheraven if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first 1623227825Stheraven { 1624227825Stheraven __first = __m; 1625227825Stheraven ++__first; 1626227825Stheraven break; 1627227825Stheraven } // else there is a match, check next elements 1628227825Stheraven } 1629227825Stheraven } 1630227825Stheraven} 1631227825Stheraven 1632227825Stheraventemplate <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate> 1633227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1634227825Stheraven_ForwardIterator 1635227825Stheravensearch_n(_ForwardIterator __first, _ForwardIterator __last, 1636227825Stheraven _Size __count, const _Tp& __value_, _BinaryPredicate __pred) 1637227825Stheraven{ 1638227825Stheraven return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type> 1639227825Stheraven (__first, __last, __count, __value_, __pred, typename iterator_traits<_ForwardIterator>::iterator_category()); 1640227825Stheraven} 1641227825Stheraven 1642227825Stheraventemplate <class _ForwardIterator, class _Size, class _Tp> 1643227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1644227825Stheraven_ForwardIterator 1645227825Stheravensearch_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_) 1646227825Stheraven{ 1647227825Stheraven typedef typename iterator_traits<_ForwardIterator>::value_type __v; 1648227825Stheraven return _VSTD::search_n(__first, __last, __count, __value_, __equal_to<__v, _Tp>()); 1649227825Stheraven} 1650227825Stheraven 1651227825Stheraven// copy 1652227825Stheraven 1653227825Stheraventemplate <class _Iter> 1654227825Stheravenstruct __libcpp_is_trivial_iterator 1655227825Stheraven{ 1656227825Stheraven static const bool value = is_pointer<_Iter>::value; 1657227825Stheraven}; 1658227825Stheraven 1659227825Stheraventemplate <class _Iter> 1660227825Stheravenstruct __libcpp_is_trivial_iterator<move_iterator<_Iter> > 1661227825Stheraven{ 1662227825Stheraven static const bool value = is_pointer<_Iter>::value; 1663227825Stheraven}; 1664227825Stheraven 1665227825Stheraventemplate <class _Iter> 1666227825Stheravenstruct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> > 1667227825Stheraven{ 1668227825Stheraven static const bool value = is_pointer<_Iter>::value; 1669227825Stheraven}; 1670227825Stheraven 1671227825Stheraventemplate <class _Iter> 1672227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1673227825Stheraven_Iter 1674227825Stheraven__unwrap_iter(_Iter __i) 1675227825Stheraven{ 1676227825Stheraven return __i; 1677227825Stheraven} 1678227825Stheraven 1679227825Stheraventemplate <class _Tp> 1680227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1681227825Stheraventypename enable_if 1682227825Stheraven< 1683227825Stheraven is_trivially_copy_assignable<_Tp>::value, 1684227825Stheraven _Tp* 1685227825Stheraven>::type 1686227825Stheraven__unwrap_iter(move_iterator<_Tp*> __i) 1687227825Stheraven{ 1688227825Stheraven return __i.base(); 1689227825Stheraven} 1690227825Stheraven 1691227825Stheraventemplate <class _Tp> 1692227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1693227825Stheraventypename enable_if 1694227825Stheraven< 1695227825Stheraven is_trivially_copy_assignable<_Tp>::value, 1696227825Stheraven _Tp* 1697227825Stheraven>::type 1698227825Stheraven__unwrap_iter(__wrap_iter<_Tp*> __i) 1699227825Stheraven{ 1700227825Stheraven return __i.base(); 1701227825Stheraven} 1702227825Stheraven 1703227825Stheraventemplate <class _InputIterator, class _OutputIterator> 1704227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1705227825Stheraven_OutputIterator 1706227825Stheraven__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result) 1707227825Stheraven{ 1708227825Stheraven for (; __first != __last; ++__first, ++__result) 1709227825Stheraven *__result = *__first; 1710227825Stheraven return __result; 1711227825Stheraven} 1712227825Stheraven 1713227825Stheraventemplate <class _Tp, class _Up> 1714227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1715227825Stheraventypename enable_if 1716227825Stheraven< 1717227825Stheraven is_same<typename remove_const<_Tp>::type, _Up>::value && 1718227825Stheraven is_trivially_copy_assignable<_Up>::value, 1719227825Stheraven _Up* 1720227825Stheraven>::type 1721227825Stheraven__copy(_Tp* __first, _Tp* __last, _Up* __result) 1722227825Stheraven{ 1723227825Stheraven const size_t __n = static_cast<size_t>(__last - __first); 1724227825Stheraven _VSTD::memmove(__result, __first, __n * sizeof(_Up)); 1725227825Stheraven return __result + __n; 1726227825Stheraven} 1727227825Stheraven 1728227825Stheraventemplate <class _InputIterator, class _OutputIterator> 1729227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1730227825Stheraven_OutputIterator 1731227825Stheravencopy(_InputIterator __first, _InputIterator __last, _OutputIterator __result) 1732227825Stheraven{ 1733227825Stheraven return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result)); 1734227825Stheraven} 1735227825Stheraven 1736227825Stheraven// copy_backward 1737227825Stheraven 1738246487Stheraventemplate <class _BidirectionalIterator, class _OutputIterator> 1739227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1740227825Stheraven_OutputIterator 1741246487Stheraven__copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result) 1742227825Stheraven{ 1743227825Stheraven while (__first != __last) 1744227825Stheraven *--__result = *--__last; 1745227825Stheraven return __result; 1746227825Stheraven} 1747227825Stheraven 1748227825Stheraventemplate <class _Tp, class _Up> 1749227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1750227825Stheraventypename enable_if 1751227825Stheraven< 1752227825Stheraven is_same<typename remove_const<_Tp>::type, _Up>::value && 1753227825Stheraven is_trivially_copy_assignable<_Up>::value, 1754227825Stheraven _Up* 1755227825Stheraven>::type 1756227825Stheraven__copy_backward(_Tp* __first, _Tp* __last, _Up* __result) 1757227825Stheraven{ 1758227825Stheraven const size_t __n = static_cast<size_t>(__last - __first); 1759227825Stheraven __result -= __n; 1760227825Stheraven _VSTD::memmove(__result, __first, __n * sizeof(_Up)); 1761227825Stheraven return __result; 1762227825Stheraven} 1763227825Stheraven 1764227825Stheraventemplate <class _BidirectionalIterator1, class _BidirectionalIterator2> 1765227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1766227825Stheraven_BidirectionalIterator2 1767227825Stheravencopy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last, 1768227825Stheraven _BidirectionalIterator2 __result) 1769227825Stheraven{ 1770227825Stheraven return _VSTD::__copy_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result)); 1771227825Stheraven} 1772227825Stheraven 1773227825Stheraven// copy_if 1774227825Stheraven 1775227825Stheraventemplate<class _InputIterator, class _OutputIterator, class _Predicate> 1776227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1777227825Stheraven_OutputIterator 1778227825Stheravencopy_if(_InputIterator __first, _InputIterator __last, 1779227825Stheraven _OutputIterator __result, _Predicate __pred) 1780227825Stheraven{ 1781227825Stheraven for (; __first != __last; ++__first) 1782227825Stheraven { 1783227825Stheraven if (__pred(*__first)) 1784227825Stheraven { 1785227825Stheraven *__result = *__first; 1786227825Stheraven ++__result; 1787227825Stheraven } 1788227825Stheraven } 1789227825Stheraven return __result; 1790227825Stheraven} 1791227825Stheraven 1792227825Stheraven// copy_n 1793227825Stheraven 1794227825Stheraventemplate<class _InputIterator, class _Size, class _OutputIterator> 1795227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1796227825Stheraventypename enable_if 1797227825Stheraven< 1798227825Stheraven __is_input_iterator<_InputIterator>::value && 1799227825Stheraven !__is_random_access_iterator<_InputIterator>::value, 1800227825Stheraven _OutputIterator 1801227825Stheraven>::type 1802227825Stheravencopy_n(_InputIterator __first, _Size __n, _OutputIterator __result) 1803227825Stheraven{ 1804227825Stheraven if (__n > 0) 1805227825Stheraven { 1806227825Stheraven *__result = *__first; 1807227825Stheraven ++__result; 1808227825Stheraven for (--__n; __n > 0; --__n) 1809227825Stheraven { 1810227825Stheraven ++__first; 1811227825Stheraven *__result = *__first; 1812227825Stheraven ++__result; 1813227825Stheraven } 1814227825Stheraven } 1815227825Stheraven return __result; 1816227825Stheraven} 1817227825Stheraven 1818227825Stheraventemplate<class _InputIterator, class _Size, class _OutputIterator> 1819227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1820227825Stheraventypename enable_if 1821227825Stheraven< 1822227825Stheraven __is_random_access_iterator<_InputIterator>::value, 1823227825Stheraven _OutputIterator 1824227825Stheraven>::type 1825227825Stheravencopy_n(_InputIterator __first, _Size __n, _OutputIterator __result) 1826227825Stheraven{ 1827227825Stheraven return _VSTD::copy(__first, __first + __n, __result); 1828227825Stheraven} 1829227825Stheraven 1830227825Stheraven// move 1831227825Stheraven 1832227825Stheraventemplate <class _InputIterator, class _OutputIterator> 1833227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1834227825Stheraven_OutputIterator 1835227825Stheraven__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result) 1836227825Stheraven{ 1837227825Stheraven for (; __first != __last; ++__first, ++__result) 1838227825Stheraven *__result = _VSTD::move(*__first); 1839227825Stheraven return __result; 1840227825Stheraven} 1841227825Stheraven 1842227825Stheraventemplate <class _Tp, class _Up> 1843227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1844227825Stheraventypename enable_if 1845227825Stheraven< 1846227825Stheraven is_same<typename remove_const<_Tp>::type, _Up>::value && 1847227825Stheraven is_trivially_copy_assignable<_Up>::value, 1848227825Stheraven _Up* 1849227825Stheraven>::type 1850227825Stheraven__move(_Tp* __first, _Tp* __last, _Up* __result) 1851227825Stheraven{ 1852227825Stheraven const size_t __n = static_cast<size_t>(__last - __first); 1853227825Stheraven _VSTD::memmove(__result, __first, __n * sizeof(_Up)); 1854227825Stheraven return __result + __n; 1855227825Stheraven} 1856227825Stheraven 1857227825Stheraventemplate <class _InputIterator, class _OutputIterator> 1858227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1859227825Stheraven_OutputIterator 1860227825Stheravenmove(_InputIterator __first, _InputIterator __last, _OutputIterator __result) 1861227825Stheraven{ 1862227825Stheraven return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result)); 1863227825Stheraven} 1864227825Stheraven 1865227825Stheraven// move_backward 1866227825Stheraven 1867227825Stheraventemplate <class _InputIterator, class _OutputIterator> 1868227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1869227825Stheraven_OutputIterator 1870227825Stheraven__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result) 1871227825Stheraven{ 1872227825Stheraven while (__first != __last) 1873227825Stheraven *--__result = _VSTD::move(*--__last); 1874227825Stheraven return __result; 1875227825Stheraven} 1876227825Stheraven 1877227825Stheraventemplate <class _Tp, class _Up> 1878227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1879227825Stheraventypename enable_if 1880227825Stheraven< 1881227825Stheraven is_same<typename remove_const<_Tp>::type, _Up>::value && 1882227825Stheraven is_trivially_copy_assignable<_Up>::value, 1883227825Stheraven _Up* 1884227825Stheraven>::type 1885227825Stheraven__move_backward(_Tp* __first, _Tp* __last, _Up* __result) 1886227825Stheraven{ 1887227825Stheraven const size_t __n = static_cast<size_t>(__last - __first); 1888227825Stheraven __result -= __n; 1889227825Stheraven _VSTD::memmove(__result, __first, __n * sizeof(_Up)); 1890227825Stheraven return __result; 1891227825Stheraven} 1892227825Stheraven 1893227825Stheraventemplate <class _BidirectionalIterator1, class _BidirectionalIterator2> 1894227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1895227825Stheraven_BidirectionalIterator2 1896227825Stheravenmove_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last, 1897227825Stheraven _BidirectionalIterator2 __result) 1898227825Stheraven{ 1899227825Stheraven return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result)); 1900227825Stheraven} 1901227825Stheraven 1902227825Stheraven// iter_swap 1903227825Stheraven 1904227825Stheraven// moved to <type_traits> for better swap / noexcept support 1905227825Stheraven 1906227825Stheraven// transform 1907227825Stheraven 1908227825Stheraventemplate <class _InputIterator, class _OutputIterator, class _UnaryOperation> 1909227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1910227825Stheraven_OutputIterator 1911227825Stheraventransform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op) 1912227825Stheraven{ 1913227825Stheraven for (; __first != __last; ++__first, ++__result) 1914227825Stheraven *__result = __op(*__first); 1915227825Stheraven return __result; 1916227825Stheraven} 1917227825Stheraven 1918227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation> 1919227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1920227825Stheraven_OutputIterator 1921227825Stheraventransform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, 1922227825Stheraven _OutputIterator __result, _BinaryOperation __binary_op) 1923227825Stheraven{ 1924227825Stheraven for (; __first1 != __last1; ++__first1, ++__first2, ++__result) 1925227825Stheraven *__result = __binary_op(*__first1, *__first2); 1926227825Stheraven return __result; 1927227825Stheraven} 1928227825Stheraven 1929227825Stheraven// replace 1930227825Stheraven 1931227825Stheraventemplate <class _ForwardIterator, class _Tp> 1932227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1933227825Stheravenvoid 1934227825Stheravenreplace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value) 1935227825Stheraven{ 1936227825Stheraven for (; __first != __last; ++__first) 1937227825Stheraven if (*__first == __old_value) 1938227825Stheraven *__first = __new_value; 1939227825Stheraven} 1940227825Stheraven 1941227825Stheraven// replace_if 1942227825Stheraven 1943227825Stheraventemplate <class _ForwardIterator, class _Predicate, class _Tp> 1944227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1945227825Stheravenvoid 1946227825Stheravenreplace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value) 1947227825Stheraven{ 1948227825Stheraven for (; __first != __last; ++__first) 1949227825Stheraven if (__pred(*__first)) 1950227825Stheraven *__first = __new_value; 1951227825Stheraven} 1952227825Stheraven 1953227825Stheraven// replace_copy 1954227825Stheraven 1955227825Stheraventemplate <class _InputIterator, class _OutputIterator, class _Tp> 1956227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1957227825Stheraven_OutputIterator 1958227825Stheravenreplace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, 1959227825Stheraven const _Tp& __old_value, const _Tp& __new_value) 1960227825Stheraven{ 1961227825Stheraven for (; __first != __last; ++__first, ++__result) 1962227825Stheraven if (*__first == __old_value) 1963227825Stheraven *__result = __new_value; 1964227825Stheraven else 1965227825Stheraven *__result = *__first; 1966227825Stheraven return __result; 1967227825Stheraven} 1968227825Stheraven 1969227825Stheraven// replace_copy_if 1970227825Stheraven 1971227825Stheraventemplate <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp> 1972227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1973227825Stheraven_OutputIterator 1974227825Stheravenreplace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, 1975227825Stheraven _Predicate __pred, const _Tp& __new_value) 1976227825Stheraven{ 1977227825Stheraven for (; __first != __last; ++__first, ++__result) 1978227825Stheraven if (__pred(*__first)) 1979227825Stheraven *__result = __new_value; 1980227825Stheraven else 1981227825Stheraven *__result = *__first; 1982227825Stheraven return __result; 1983227825Stheraven} 1984227825Stheraven 1985227825Stheraven// fill_n 1986227825Stheraven 1987227825Stheraventemplate <class _OutputIterator, class _Size, class _Tp> 1988227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1989227825Stheraven_OutputIterator 1990227825Stheraven__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_, false_type) 1991227825Stheraven{ 1992227825Stheraven for (; __n > 0; ++__first, --__n) 1993227825Stheraven *__first = __value_; 1994227825Stheraven return __first; 1995227825Stheraven} 1996227825Stheraven 1997227825Stheraventemplate <class _OutputIterator, class _Size, class _Tp> 1998227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 1999227825Stheraven_OutputIterator 2000227825Stheraven__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_, true_type) 2001227825Stheraven{ 2002227825Stheraven if (__n > 0) 2003227825Stheraven _VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n)); 2004227825Stheraven return __first + __n; 2005227825Stheraven} 2006227825Stheraven 2007227825Stheraventemplate <class _OutputIterator, class _Size, class _Tp> 2008227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2009227825Stheraven_OutputIterator 2010227825Stheravenfill_n(_OutputIterator __first, _Size __n, const _Tp& __value_) 2011227825Stheraven{ 2012227825Stheraven return _VSTD::__fill_n(__first, __n, __value_, integral_constant<bool, 2013227825Stheraven is_pointer<_OutputIterator>::value && 2014227825Stheraven is_trivially_copy_assignable<_Tp>::value && 2015227825Stheraven sizeof(_Tp) == 1>()); 2016227825Stheraven} 2017227825Stheraven 2018227825Stheraven// fill 2019227825Stheraven 2020227825Stheraventemplate <class _ForwardIterator, class _Tp> 2021227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2022227825Stheravenvoid 2023227825Stheraven__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag) 2024227825Stheraven{ 2025227825Stheraven for (; __first != __last; ++__first) 2026227825Stheraven *__first = __value_; 2027227825Stheraven} 2028227825Stheraven 2029227825Stheraventemplate <class _RandomAccessIterator, class _Tp> 2030227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2031227825Stheravenvoid 2032227825Stheraven__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag) 2033227825Stheraven{ 2034227825Stheraven _VSTD::fill_n(__first, __last - __first, __value_); 2035227825Stheraven} 2036227825Stheraven 2037227825Stheraventemplate <class _ForwardIterator, class _Tp> 2038227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2039227825Stheravenvoid 2040227825Stheravenfill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_) 2041227825Stheraven{ 2042227825Stheraven _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category()); 2043227825Stheraven} 2044227825Stheraven 2045227825Stheraven// generate 2046227825Stheraven 2047227825Stheraventemplate <class _ForwardIterator, class _Generator> 2048227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2049227825Stheravenvoid 2050227825Stheravengenerate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen) 2051227825Stheraven{ 2052227825Stheraven for (; __first != __last; ++__first) 2053227825Stheraven *__first = __gen(); 2054227825Stheraven} 2055227825Stheraven 2056227825Stheraven// generate_n 2057227825Stheraven 2058227825Stheraventemplate <class _OutputIterator, class _Size, class _Generator> 2059227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2060227825Stheraven_OutputIterator 2061227825Stheravengenerate_n(_OutputIterator __first, _Size __n, _Generator __gen) 2062227825Stheraven{ 2063227825Stheraven for (; __n > 0; ++__first, --__n) 2064227825Stheraven *__first = __gen(); 2065227825Stheraven return __first; 2066227825Stheraven} 2067227825Stheraven 2068227825Stheraven// remove 2069227825Stheraven 2070227825Stheraventemplate <class _ForwardIterator, class _Tp> 2071227825Stheraven_ForwardIterator 2072227825Stheravenremove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_) 2073227825Stheraven{ 2074227825Stheraven __first = _VSTD::find(__first, __last, __value_); 2075227825Stheraven if (__first != __last) 2076227825Stheraven { 2077227825Stheraven _ForwardIterator __i = __first; 2078227825Stheraven while (++__i != __last) 2079227825Stheraven { 2080227825Stheraven if (!(*__i == __value_)) 2081227825Stheraven { 2082227825Stheraven *__first = _VSTD::move(*__i); 2083227825Stheraven ++__first; 2084227825Stheraven } 2085227825Stheraven } 2086227825Stheraven } 2087227825Stheraven return __first; 2088227825Stheraven} 2089227825Stheraven 2090227825Stheraven// remove_if 2091227825Stheraven 2092227825Stheraventemplate <class _ForwardIterator, class _Predicate> 2093227825Stheraven_ForwardIterator 2094227825Stheravenremove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) 2095227825Stheraven{ 2096227825Stheraven __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type> 2097227825Stheraven (__first, __last, __pred); 2098227825Stheraven if (__first != __last) 2099227825Stheraven { 2100227825Stheraven _ForwardIterator __i = __first; 2101227825Stheraven while (++__i != __last) 2102227825Stheraven { 2103227825Stheraven if (!__pred(*__i)) 2104227825Stheraven { 2105227825Stheraven *__first = _VSTD::move(*__i); 2106227825Stheraven ++__first; 2107227825Stheraven } 2108227825Stheraven } 2109227825Stheraven } 2110227825Stheraven return __first; 2111227825Stheraven} 2112227825Stheraven 2113227825Stheraven// remove_copy 2114227825Stheraven 2115227825Stheraventemplate <class _InputIterator, class _OutputIterator, class _Tp> 2116227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2117227825Stheraven_OutputIterator 2118227825Stheravenremove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_) 2119227825Stheraven{ 2120227825Stheraven for (; __first != __last; ++__first) 2121227825Stheraven { 2122227825Stheraven if (!(*__first == __value_)) 2123227825Stheraven { 2124227825Stheraven *__result = *__first; 2125227825Stheraven ++__result; 2126227825Stheraven } 2127227825Stheraven } 2128227825Stheraven return __result; 2129227825Stheraven} 2130227825Stheraven 2131227825Stheraven// remove_copy_if 2132227825Stheraven 2133227825Stheraventemplate <class _InputIterator, class _OutputIterator, class _Predicate> 2134227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2135227825Stheraven_OutputIterator 2136227825Stheravenremove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred) 2137227825Stheraven{ 2138227825Stheraven for (; __first != __last; ++__first) 2139227825Stheraven { 2140227825Stheraven if (!__pred(*__first)) 2141227825Stheraven { 2142227825Stheraven *__result = *__first; 2143227825Stheraven ++__result; 2144227825Stheraven } 2145227825Stheraven } 2146227825Stheraven return __result; 2147227825Stheraven} 2148227825Stheraven 2149227825Stheraven// unique 2150227825Stheraven 2151227825Stheraventemplate <class _ForwardIterator, class _BinaryPredicate> 2152227825Stheraven_ForwardIterator 2153227825Stheravenunique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred) 2154227825Stheraven{ 2155227825Stheraven __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type> 2156227825Stheraven (__first, __last, __pred); 2157227825Stheraven if (__first != __last) 2158227825Stheraven { 2159227825Stheraven // ... a a ? ... 2160227825Stheraven // f i 2161227825Stheraven _ForwardIterator __i = __first; 2162227825Stheraven for (++__i; ++__i != __last;) 2163227825Stheraven if (!__pred(*__first, *__i)) 2164227825Stheraven *++__first = _VSTD::move(*__i); 2165227825Stheraven ++__first; 2166227825Stheraven } 2167227825Stheraven return __first; 2168227825Stheraven} 2169227825Stheraven 2170227825Stheraventemplate <class _ForwardIterator> 2171227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2172227825Stheraven_ForwardIterator 2173227825Stheravenunique(_ForwardIterator __first, _ForwardIterator __last) 2174227825Stheraven{ 2175227825Stheraven typedef typename iterator_traits<_ForwardIterator>::value_type __v; 2176227825Stheraven return _VSTD::unique(__first, __last, __equal_to<__v>()); 2177227825Stheraven} 2178227825Stheraven 2179227825Stheraven// unique_copy 2180227825Stheraven 2181227825Stheraventemplate <class _BinaryPredicate, class _InputIterator, class _OutputIterator> 2182227825Stheraven_OutputIterator 2183227825Stheraven__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred, 2184227825Stheraven input_iterator_tag, output_iterator_tag) 2185227825Stheraven{ 2186227825Stheraven if (__first != __last) 2187227825Stheraven { 2188227825Stheraven typename iterator_traits<_InputIterator>::value_type __t(*__first); 2189227825Stheraven *__result = __t; 2190227825Stheraven ++__result; 2191227825Stheraven while (++__first != __last) 2192227825Stheraven { 2193227825Stheraven if (!__pred(__t, *__first)) 2194227825Stheraven { 2195227825Stheraven __t = *__first; 2196227825Stheraven *__result = __t; 2197227825Stheraven ++__result; 2198227825Stheraven } 2199227825Stheraven } 2200227825Stheraven } 2201227825Stheraven return __result; 2202227825Stheraven} 2203227825Stheraven 2204227825Stheraventemplate <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator> 2205227825Stheraven_OutputIterator 2206227825Stheraven__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred, 2207227825Stheraven forward_iterator_tag, output_iterator_tag) 2208227825Stheraven{ 2209227825Stheraven if (__first != __last) 2210227825Stheraven { 2211227825Stheraven _ForwardIterator __i = __first; 2212227825Stheraven *__result = *__i; 2213227825Stheraven ++__result; 2214227825Stheraven while (++__first != __last) 2215227825Stheraven { 2216227825Stheraven if (!__pred(*__i, *__first)) 2217227825Stheraven { 2218227825Stheraven *__result = *__first; 2219227825Stheraven ++__result; 2220227825Stheraven __i = __first; 2221227825Stheraven } 2222227825Stheraven } 2223227825Stheraven } 2224227825Stheraven return __result; 2225227825Stheraven} 2226227825Stheraven 2227227825Stheraventemplate <class _BinaryPredicate, class _InputIterator, class _ForwardIterator> 2228227825Stheraven_ForwardIterator 2229227825Stheraven__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred, 2230227825Stheraven input_iterator_tag, forward_iterator_tag) 2231227825Stheraven{ 2232227825Stheraven if (__first != __last) 2233227825Stheraven { 2234227825Stheraven *__result = *__first; 2235227825Stheraven while (++__first != __last) 2236227825Stheraven if (!__pred(*__result, *__first)) 2237227825Stheraven *++__result = *__first; 2238227825Stheraven ++__result; 2239227825Stheraven } 2240227825Stheraven return __result; 2241227825Stheraven} 2242227825Stheraven 2243227825Stheraventemplate <class _InputIterator, class _OutputIterator, class _BinaryPredicate> 2244227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2245227825Stheraven_OutputIterator 2246227825Stheravenunique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred) 2247227825Stheraven{ 2248227825Stheraven return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type> 2249227825Stheraven (__first, __last, __result, __pred, 2250227825Stheraven typename iterator_traits<_InputIterator>::iterator_category(), 2251227825Stheraven typename iterator_traits<_OutputIterator>::iterator_category()); 2252227825Stheraven} 2253227825Stheraven 2254227825Stheraventemplate <class _InputIterator, class _OutputIterator> 2255227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2256227825Stheraven_OutputIterator 2257227825Stheravenunique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result) 2258227825Stheraven{ 2259227825Stheraven typedef typename iterator_traits<_InputIterator>::value_type __v; 2260227825Stheraven return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>()); 2261227825Stheraven} 2262227825Stheraven 2263227825Stheraven// reverse 2264227825Stheraven 2265227825Stheraventemplate <class _BidirectionalIterator> 2266227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2267227825Stheravenvoid 2268227825Stheraven__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag) 2269227825Stheraven{ 2270227825Stheraven while (__first != __last) 2271227825Stheraven { 2272227825Stheraven if (__first == --__last) 2273227825Stheraven break; 2274227825Stheraven swap(*__first, *__last); 2275227825Stheraven ++__first; 2276227825Stheraven } 2277227825Stheraven} 2278227825Stheraven 2279227825Stheraventemplate <class _RandomAccessIterator> 2280227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2281227825Stheravenvoid 2282227825Stheraven__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag) 2283227825Stheraven{ 2284227825Stheraven if (__first != __last) 2285227825Stheraven for (; __first < --__last; ++__first) 2286227825Stheraven swap(*__first, *__last); 2287227825Stheraven} 2288227825Stheraven 2289227825Stheraventemplate <class _BidirectionalIterator> 2290227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2291227825Stheravenvoid 2292227825Stheravenreverse(_BidirectionalIterator __first, _BidirectionalIterator __last) 2293227825Stheraven{ 2294227825Stheraven _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category()); 2295227825Stheraven} 2296227825Stheraven 2297227825Stheraven// reverse_copy 2298227825Stheraven 2299227825Stheraventemplate <class _BidirectionalIterator, class _OutputIterator> 2300227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2301227825Stheraven_OutputIterator 2302227825Stheravenreverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result) 2303227825Stheraven{ 2304227825Stheraven for (; __first != __last; ++__result) 2305227825Stheraven *__result = *--__last; 2306227825Stheraven return __result; 2307227825Stheraven} 2308227825Stheraven 2309227825Stheraven// rotate 2310227825Stheraven 2311227825Stheraventemplate <class _ForwardIterator> 2312227825Stheraven_ForwardIterator 2313241903Sdim__rotate_left(_ForwardIterator __first, _ForwardIterator __last) 2314227825Stheraven{ 2315241903Sdim typedef typename iterator_traits<_ForwardIterator>::value_type value_type; 2316241903Sdim value_type __tmp = _VSTD::move(*__first); 2317241903Sdim _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first); 2318241903Sdim *__lm1 = _VSTD::move(__tmp); 2319241903Sdim return __lm1; 2320241903Sdim} 2321241903Sdim 2322241903Sdimtemplate <class _BidirectionalIterator> 2323241903Sdim_BidirectionalIterator 2324241903Sdim__rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last) 2325241903Sdim{ 2326241903Sdim typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type; 2327241903Sdim _BidirectionalIterator __lm1 = _VSTD::prev(__last); 2328241903Sdim value_type __tmp = _VSTD::move(*__lm1); 2329241903Sdim _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last); 2330241903Sdim *__first = _VSTD::move(__tmp); 2331241903Sdim return __fp1; 2332241903Sdim} 2333241903Sdim 2334241903Sdimtemplate <class _ForwardIterator> 2335241903Sdim_ForwardIterator 2336241903Sdim__rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last) 2337241903Sdim{ 2338227825Stheraven _ForwardIterator __i = __middle; 2339227825Stheraven while (true) 2340227825Stheraven { 2341227825Stheraven swap(*__first, *__i); 2342227825Stheraven ++__first; 2343227825Stheraven if (++__i == __last) 2344227825Stheraven break; 2345227825Stheraven if (__first == __middle) 2346227825Stheraven __middle = __i; 2347227825Stheraven } 2348227825Stheraven _ForwardIterator __r = __first; 2349227825Stheraven if (__first != __middle) 2350227825Stheraven { 2351227825Stheraven __i = __middle; 2352227825Stheraven while (true) 2353227825Stheraven { 2354227825Stheraven swap(*__first, *__i); 2355227825Stheraven ++__first; 2356227825Stheraven if (++__i == __last) 2357227825Stheraven { 2358227825Stheraven if (__first == __middle) 2359227825Stheraven break; 2360227825Stheraven __i = __middle; 2361227825Stheraven } 2362227825Stheraven else if (__first == __middle) 2363227825Stheraven __middle = __i; 2364227825Stheraven } 2365227825Stheraven } 2366227825Stheraven return __r; 2367227825Stheraven} 2368227825Stheraven 2369227825Stheraventemplate<typename _Integral> 2370227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2371227825Stheraven_Integral 2372227825Stheraven__gcd(_Integral __x, _Integral __y) 2373227825Stheraven{ 2374227825Stheraven do 2375227825Stheraven { 2376227825Stheraven _Integral __t = __x % __y; 2377227825Stheraven __x = __y; 2378227825Stheraven __y = __t; 2379227825Stheraven } while (__y); 2380227825Stheraven return __x; 2381227825Stheraven} 2382227825Stheraven 2383227825Stheraventemplate<typename _RandomAccessIterator> 2384227825Stheraven_RandomAccessIterator 2385241903Sdim__rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last) 2386227825Stheraven{ 2387227825Stheraven typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; 2388227825Stheraven typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type; 2389227825Stheraven 2390227825Stheraven const difference_type __m1 = __middle - __first; 2391227825Stheraven const difference_type __m2 = __last - __middle; 2392227825Stheraven if (__m1 == __m2) 2393227825Stheraven { 2394227825Stheraven _VSTD::swap_ranges(__first, __middle, __middle); 2395227825Stheraven return __middle; 2396227825Stheraven } 2397241903Sdim const difference_type __g = _VSTD::__gcd(__m1, __m2); 2398227825Stheraven for (_RandomAccessIterator __p = __first + __g; __p != __first;) 2399227825Stheraven { 2400241903Sdim value_type __t(_VSTD::move(*--__p)); 2401227825Stheraven _RandomAccessIterator __p1 = __p; 2402227825Stheraven _RandomAccessIterator __p2 = __p1 + __m1; 2403227825Stheraven do 2404227825Stheraven { 2405241903Sdim *__p1 = _VSTD::move(*__p2); 2406227825Stheraven __p1 = __p2; 2407227825Stheraven const difference_type __d = __last - __p2; 2408227825Stheraven if (__m1 < __d) 2409227825Stheraven __p2 += __m1; 2410227825Stheraven else 2411227825Stheraven __p2 = __first + (__m1 - __d); 2412227825Stheraven } while (__p2 != __p); 2413241903Sdim *__p1 = _VSTD::move(__t); 2414227825Stheraven } 2415227825Stheraven return __first + __m2; 2416227825Stheraven} 2417227825Stheraven 2418227825Stheraventemplate <class _ForwardIterator> 2419227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2420227825Stheraven_ForwardIterator 2421241903Sdim__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, 2422241903Sdim _VSTD::forward_iterator_tag) 2423241903Sdim{ 2424241903Sdim typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type; 2425241903Sdim if (_VSTD::is_trivially_move_assignable<value_type>::value) 2426241903Sdim { 2427241903Sdim if (_VSTD::next(__first) == __middle) 2428241903Sdim return _VSTD::__rotate_left(__first, __last); 2429241903Sdim } 2430241903Sdim return _VSTD::__rotate_forward(__first, __middle, __last); 2431241903Sdim} 2432241903Sdim 2433241903Sdimtemplate <class _BidirectionalIterator> 2434241903Sdiminline _LIBCPP_INLINE_VISIBILITY 2435241903Sdim_BidirectionalIterator 2436241903Sdim__rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last, 2437241903Sdim _VSTD::bidirectional_iterator_tag) 2438241903Sdim{ 2439241903Sdim typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type; 2440241903Sdim if (_VSTD::is_trivially_move_assignable<value_type>::value) 2441241903Sdim { 2442241903Sdim if (_VSTD::next(__first) == __middle) 2443241903Sdim return _VSTD::__rotate_left(__first, __last); 2444241903Sdim if (_VSTD::next(__middle) == __last) 2445241903Sdim return _VSTD::__rotate_right(__first, __last); 2446241903Sdim } 2447241903Sdim return _VSTD::__rotate_forward(__first, __middle, __last); 2448241903Sdim} 2449241903Sdim 2450241903Sdimtemplate <class _RandomAccessIterator> 2451241903Sdiminline _LIBCPP_INLINE_VISIBILITY 2452241903Sdim_RandomAccessIterator 2453241903Sdim__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last, 2454241903Sdim _VSTD::random_access_iterator_tag) 2455241903Sdim{ 2456241903Sdim typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type; 2457241903Sdim if (_VSTD::is_trivially_move_assignable<value_type>::value) 2458241903Sdim { 2459241903Sdim if (_VSTD::next(__first) == __middle) 2460241903Sdim return _VSTD::__rotate_left(__first, __last); 2461241903Sdim if (_VSTD::next(__middle) == __last) 2462241903Sdim return _VSTD::__rotate_right(__first, __last); 2463241903Sdim return _VSTD::__rotate_gcd(__first, __middle, __last); 2464241903Sdim } 2465241903Sdim return _VSTD::__rotate_forward(__first, __middle, __last); 2466241903Sdim} 2467241903Sdim 2468241903Sdimtemplate <class _ForwardIterator> 2469241903Sdiminline _LIBCPP_INLINE_VISIBILITY 2470241903Sdim_ForwardIterator 2471227825Stheravenrotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last) 2472227825Stheraven{ 2473241903Sdim if (__first == __middle) 2474241903Sdim return __last; 2475241903Sdim if (__middle == __last) 2476241903Sdim return __first; 2477227825Stheraven return _VSTD::__rotate(__first, __middle, __last, 2478241903Sdim typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category()); 2479227825Stheraven} 2480227825Stheraven 2481227825Stheraven// rotate_copy 2482227825Stheraven 2483227825Stheraventemplate <class _ForwardIterator, class _OutputIterator> 2484227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2485227825Stheraven_OutputIterator 2486227825Stheravenrotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result) 2487227825Stheraven{ 2488227825Stheraven return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result)); 2489227825Stheraven} 2490227825Stheraven 2491227825Stheraven// min_element 2492227825Stheraven 2493227825Stheraventemplate <class _ForwardIterator, class _Compare> 2494227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2495227825Stheraven_ForwardIterator 2496227825Stheravenmin_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) 2497227825Stheraven{ 2498227825Stheraven if (__first != __last) 2499227825Stheraven { 2500227825Stheraven _ForwardIterator __i = __first; 2501227825Stheraven while (++__i != __last) 2502227825Stheraven if (__comp(*__i, *__first)) 2503227825Stheraven __first = __i; 2504227825Stheraven } 2505227825Stheraven return __first; 2506227825Stheraven} 2507227825Stheraven 2508227825Stheraventemplate <class _ForwardIterator> 2509227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2510227825Stheraven_ForwardIterator 2511227825Stheravenmin_element(_ForwardIterator __first, _ForwardIterator __last) 2512227825Stheraven{ 2513227825Stheraven return _VSTD::min_element(__first, __last, 2514227825Stheraven __less<typename iterator_traits<_ForwardIterator>::value_type>()); 2515227825Stheraven} 2516227825Stheraven 2517227825Stheraven// min 2518227825Stheraven 2519227825Stheraventemplate <class _Tp, class _Compare> 2520227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2521227825Stheravenconst _Tp& 2522227825Stheravenmin(const _Tp& __a, const _Tp& __b, _Compare __comp) 2523227825Stheraven{ 2524227825Stheraven return __comp(__b, __a) ? __b : __a; 2525227825Stheraven} 2526227825Stheraven 2527227825Stheraventemplate <class _Tp> 2528227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2529227825Stheravenconst _Tp& 2530227825Stheravenmin(const _Tp& __a, const _Tp& __b) 2531227825Stheraven{ 2532227825Stheraven return _VSTD::min(__a, __b, __less<_Tp>()); 2533227825Stheraven} 2534227825Stheraven 2535227825Stheraven#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 2536227825Stheraven 2537227825Stheraventemplate<class _Tp, class _Compare> 2538227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2539227825Stheraven_Tp 2540227825Stheravenmin(initializer_list<_Tp> __t, _Compare __comp) 2541227825Stheraven{ 2542227825Stheraven return *_VSTD::min_element(__t.begin(), __t.end(), __comp); 2543227825Stheraven} 2544227825Stheraven 2545227825Stheraventemplate<class _Tp> 2546227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2547227825Stheraven_Tp 2548227825Stheravenmin(initializer_list<_Tp> __t) 2549227825Stheraven{ 2550227825Stheraven return *_VSTD::min_element(__t.begin(), __t.end()); 2551227825Stheraven} 2552227825Stheraven 2553227825Stheraven#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 2554227825Stheraven 2555227825Stheraven// max_element 2556227825Stheraven 2557227825Stheraventemplate <class _ForwardIterator, class _Compare> 2558227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2559227825Stheraven_ForwardIterator 2560227825Stheravenmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) 2561227825Stheraven{ 2562227825Stheraven if (__first != __last) 2563227825Stheraven { 2564227825Stheraven _ForwardIterator __i = __first; 2565227825Stheraven while (++__i != __last) 2566227825Stheraven if (__comp(*__first, *__i)) 2567227825Stheraven __first = __i; 2568227825Stheraven } 2569227825Stheraven return __first; 2570227825Stheraven} 2571227825Stheraven 2572227825Stheraventemplate <class _ForwardIterator> 2573227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2574227825Stheraven_ForwardIterator 2575227825Stheravenmax_element(_ForwardIterator __first, _ForwardIterator __last) 2576227825Stheraven{ 2577227825Stheraven return _VSTD::max_element(__first, __last, 2578227825Stheraven __less<typename iterator_traits<_ForwardIterator>::value_type>()); 2579227825Stheraven} 2580227825Stheraven 2581227825Stheraven// max 2582227825Stheraven 2583227825Stheraventemplate <class _Tp, class _Compare> 2584227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2585227825Stheravenconst _Tp& 2586227825Stheravenmax(const _Tp& __a, const _Tp& __b, _Compare __comp) 2587227825Stheraven{ 2588227825Stheraven return __comp(__a, __b) ? __b : __a; 2589227825Stheraven} 2590227825Stheraven 2591227825Stheraventemplate <class _Tp> 2592227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2593227825Stheravenconst _Tp& 2594227825Stheravenmax(const _Tp& __a, const _Tp& __b) 2595227825Stheraven{ 2596227825Stheraven return _VSTD::max(__a, __b, __less<_Tp>()); 2597227825Stheraven} 2598227825Stheraven 2599227825Stheraven#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 2600227825Stheraven 2601227825Stheraventemplate<class _Tp, class _Compare> 2602227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2603227825Stheraven_Tp 2604227825Stheravenmax(initializer_list<_Tp> __t, _Compare __comp) 2605227825Stheraven{ 2606227825Stheraven return *_VSTD::max_element(__t.begin(), __t.end(), __comp); 2607227825Stheraven} 2608227825Stheraven 2609227825Stheraventemplate<class _Tp> 2610227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2611227825Stheraven_Tp 2612227825Stheravenmax(initializer_list<_Tp> __t) 2613227825Stheraven{ 2614227825Stheraven return *_VSTD::max_element(__t.begin(), __t.end()); 2615227825Stheraven} 2616227825Stheraven 2617227825Stheraven#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 2618227825Stheraven 2619227825Stheraven// minmax_element 2620227825Stheraven 2621227825Stheraventemplate <class _ForwardIterator, class _Compare> 2622227825Stheravenstd::pair<_ForwardIterator, _ForwardIterator> 2623227825Stheravenminmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) 2624227825Stheraven{ 2625227825Stheraven std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first); 2626227825Stheraven if (__first != __last) 2627227825Stheraven { 2628227825Stheraven if (++__first != __last) 2629227825Stheraven { 2630227825Stheraven if (__comp(*__first, *__result.first)) 2631227825Stheraven __result.first = __first; 2632227825Stheraven else 2633227825Stheraven __result.second = __first; 2634227825Stheraven while (++__first != __last) 2635227825Stheraven { 2636227825Stheraven _ForwardIterator __i = __first; 2637227825Stheraven if (++__first == __last) 2638227825Stheraven { 2639227825Stheraven if (__comp(*__i, *__result.first)) 2640227825Stheraven __result.first = __i; 2641227825Stheraven else if (!__comp(*__i, *__result.second)) 2642227825Stheraven __result.second = __i; 2643227825Stheraven break; 2644227825Stheraven } 2645227825Stheraven else 2646227825Stheraven { 2647227825Stheraven if (__comp(*__first, *__i)) 2648227825Stheraven { 2649227825Stheraven if (__comp(*__first, *__result.first)) 2650227825Stheraven __result.first = __first; 2651227825Stheraven if (!__comp(*__i, *__result.second)) 2652227825Stheraven __result.second = __i; 2653227825Stheraven } 2654227825Stheraven else 2655227825Stheraven { 2656227825Stheraven if (__comp(*__i, *__result.first)) 2657227825Stheraven __result.first = __i; 2658227825Stheraven if (!__comp(*__first, *__result.second)) 2659227825Stheraven __result.second = __first; 2660227825Stheraven } 2661227825Stheraven } 2662227825Stheraven } 2663227825Stheraven } 2664227825Stheraven } 2665227825Stheraven return __result; 2666227825Stheraven} 2667227825Stheraven 2668227825Stheraventemplate <class _ForwardIterator> 2669227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2670227825Stheravenstd::pair<_ForwardIterator, _ForwardIterator> 2671227825Stheravenminmax_element(_ForwardIterator __first, _ForwardIterator __last) 2672227825Stheraven{ 2673227825Stheraven return _VSTD::minmax_element(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>()); 2674227825Stheraven} 2675227825Stheraven 2676227825Stheraven// minmax 2677227825Stheraven 2678227825Stheraventemplate<class _Tp, class _Compare> 2679227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2680227825Stheravenpair<const _Tp&, const _Tp&> 2681227825Stheravenminmax(const _Tp& __a, const _Tp& __b, _Compare __comp) 2682227825Stheraven{ 2683227825Stheraven return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) : 2684227825Stheraven pair<const _Tp&, const _Tp&>(__a, __b); 2685227825Stheraven} 2686227825Stheraven 2687227825Stheraventemplate<class _Tp> 2688227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2689227825Stheravenpair<const _Tp&, const _Tp&> 2690227825Stheravenminmax(const _Tp& __a, const _Tp& __b) 2691227825Stheraven{ 2692227825Stheraven return _VSTD::minmax(__a, __b, __less<_Tp>()); 2693227825Stheraven} 2694227825Stheraven 2695227825Stheraven#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 2696227825Stheraven 2697227825Stheraventemplate<class _Tp> 2698227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2699227825Stheravenpair<_Tp, _Tp> 2700227825Stheravenminmax(initializer_list<_Tp> __t) 2701227825Stheraven{ 2702227825Stheraven pair<const _Tp*, const _Tp*> __p = 2703227825Stheraven _VSTD::minmax_element(__t.begin(), __t.end()); 2704227825Stheraven return pair<_Tp, _Tp>(*__p.first, *__p.second); 2705227825Stheraven} 2706227825Stheraven 2707227825Stheraventemplate<class _Tp, class _Compare> 2708227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 2709227825Stheravenpair<_Tp, _Tp> 2710227825Stheravenminmax(initializer_list<_Tp> __t, _Compare __comp) 2711227825Stheraven{ 2712227825Stheraven pair<const _Tp*, const _Tp*> __p = 2713227825Stheraven _VSTD::minmax_element(__t.begin(), __t.end(), __comp); 2714227825Stheraven return pair<_Tp, _Tp>(*__p.first, *__p.second); 2715227825Stheraven} 2716227825Stheraven 2717227825Stheraven#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 2718227825Stheraven 2719227825Stheraven// random_shuffle 2720227825Stheraven 2721227825Stheraven// __independent_bits_engine 2722227825Stheraven 2723232950Stheraventemplate <unsigned long long _Xp, size_t _Rp> 2724227825Stheravenstruct __log2_imp 2725227825Stheraven{ 2726232950Stheraven static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp 2727232950Stheraven : __log2_imp<_Xp, _Rp - 1>::value; 2728227825Stheraven}; 2729227825Stheraven 2730232950Stheraventemplate <unsigned long long _Xp> 2731232950Stheravenstruct __log2_imp<_Xp, 0> 2732227825Stheraven{ 2733227825Stheraven static const size_t value = 0; 2734227825Stheraven}; 2735227825Stheraven 2736232950Stheraventemplate <size_t _Rp> 2737232950Stheravenstruct __log2_imp<0, _Rp> 2738227825Stheraven{ 2739232950Stheraven static const size_t value = _Rp + 1; 2740227825Stheraven}; 2741227825Stheraven 2742232950Stheraventemplate <class _UI, _UI _Xp> 2743227825Stheravenstruct __log2 2744227825Stheraven{ 2745232950Stheraven static const size_t value = __log2_imp<_Xp, 2746227825Stheraven sizeof(_UI) * __CHAR_BIT__ - 1>::value; 2747227825Stheraven}; 2748227825Stheraven 2749227825Stheraventemplate<class _Engine, class _UIntType> 2750227825Stheravenclass __independent_bits_engine 2751227825Stheraven{ 2752227825Stheravenpublic: 2753227825Stheraven // types 2754227825Stheraven typedef _UIntType result_type; 2755227825Stheraven 2756227825Stheravenprivate: 2757227825Stheraven typedef typename _Engine::result_type _Engine_result_type; 2758227825Stheraven typedef typename conditional 2759227825Stheraven < 2760227825Stheraven sizeof(_Engine_result_type) <= sizeof(result_type), 2761227825Stheraven result_type, 2762227825Stheraven _Engine_result_type 2763227825Stheraven >::type _Working_result_type; 2764227825Stheraven 2765227825Stheraven _Engine& __e_; 2766227825Stheraven size_t __w_; 2767227825Stheraven size_t __w0_; 2768227825Stheraven size_t __n_; 2769227825Stheraven size_t __n0_; 2770227825Stheraven _Working_result_type __y0_; 2771227825Stheraven _Working_result_type __y1_; 2772227825Stheraven _Engine_result_type __mask0_; 2773227825Stheraven _Engine_result_type __mask1_; 2774227825Stheraven 2775234976Stheraven#ifdef _LIBCPP_HAS_NO_CONSTEXPR 2776232950Stheraven static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min 2777234976Stheraven + _Working_result_type(1); 2778234976Stheraven#else 2779234976Stheraven static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min() 2780234976Stheraven + _Working_result_type(1); 2781234976Stheraven#endif 2782234976Stheraven static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value; 2783234976Stheraven static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits; 2784234976Stheraven static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits; 2785227825Stheraven 2786227825Stheravenpublic: 2787227825Stheraven // constructors and seeding functions 2788227825Stheraven __independent_bits_engine(_Engine& __e, size_t __w); 2789227825Stheraven 2790227825Stheraven // generating functions 2791232950Stheraven result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());} 2792227825Stheraven 2793227825Stheravenprivate: 2794227825Stheraven result_type __eval(false_type); 2795227825Stheraven result_type __eval(true_type); 2796227825Stheraven}; 2797227825Stheraven 2798227825Stheraventemplate<class _Engine, class _UIntType> 2799227825Stheraven__independent_bits_engine<_Engine, _UIntType> 2800227825Stheraven ::__independent_bits_engine(_Engine& __e, size_t __w) 2801227825Stheraven : __e_(__e), 2802227825Stheraven __w_(__w) 2803227825Stheraven{ 2804227825Stheraven __n_ = __w_ / __m + (__w_ % __m != 0); 2805227825Stheraven __w0_ = __w_ / __n_; 2806232950Stheraven if (_Rp == 0) 2807232950Stheraven __y0_ = _Rp; 2808227825Stheraven else if (__w0_ < _WDt) 2809232950Stheraven __y0_ = (_Rp >> __w0_) << __w0_; 2810227825Stheraven else 2811227825Stheraven __y0_ = 0; 2812232950Stheraven if (_Rp - __y0_ > __y0_ / __n_) 2813227825Stheraven { 2814227825Stheraven ++__n_; 2815227825Stheraven __w0_ = __w_ / __n_; 2816227825Stheraven if (__w0_ < _WDt) 2817232950Stheraven __y0_ = (_Rp >> __w0_) << __w0_; 2818227825Stheraven else 2819227825Stheraven __y0_ = 0; 2820227825Stheraven } 2821227825Stheraven __n0_ = __n_ - __w_ % __n_; 2822227825Stheraven if (__w0_ < _WDt - 1) 2823232950Stheraven __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1); 2824227825Stheraven else 2825227825Stheraven __y1_ = 0; 2826227825Stheraven __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) : 2827227825Stheraven _Engine_result_type(0); 2828227825Stheraven __mask1_ = __w0_ < _EDt - 1 ? 2829227825Stheraven _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) : 2830227825Stheraven _Engine_result_type(~0); 2831227825Stheraven} 2832227825Stheraven 2833227825Stheraventemplate<class _Engine, class _UIntType> 2834227825Stheraveninline 2835227825Stheraven_UIntType 2836227825Stheraven__independent_bits_engine<_Engine, _UIntType>::__eval(false_type) 2837227825Stheraven{ 2838227825Stheraven return static_cast<result_type>(__e_() & __mask0_); 2839227825Stheraven} 2840227825Stheraven 2841227825Stheraventemplate<class _Engine, class _UIntType> 2842227825Stheraven_UIntType 2843227825Stheraven__independent_bits_engine<_Engine, _UIntType>::__eval(true_type) 2844227825Stheraven{ 2845232950Stheraven result_type _Sp = 0; 2846227825Stheraven for (size_t __k = 0; __k < __n0_; ++__k) 2847227825Stheraven { 2848227825Stheraven _Engine_result_type __u; 2849227825Stheraven do 2850227825Stheraven { 2851227825Stheraven __u = __e_() - _Engine::min(); 2852227825Stheraven } while (__u >= __y0_); 2853227825Stheraven if (__w0_ < _WDt) 2854232950Stheraven _Sp <<= __w0_; 2855227825Stheraven else 2856232950Stheraven _Sp = 0; 2857232950Stheraven _Sp += __u & __mask0_; 2858227825Stheraven } 2859227825Stheraven for (size_t __k = __n0_; __k < __n_; ++__k) 2860227825Stheraven { 2861227825Stheraven _Engine_result_type __u; 2862227825Stheraven do 2863227825Stheraven { 2864227825Stheraven __u = __e_() - _Engine::min(); 2865227825Stheraven } while (__u >= __y1_); 2866227825Stheraven if (__w0_ < _WDt - 1) 2867232950Stheraven _Sp <<= __w0_ + 1; 2868227825Stheraven else 2869232950Stheraven _Sp = 0; 2870232950Stheraven _Sp += __u & __mask1_; 2871227825Stheraven } 2872232950Stheraven return _Sp; 2873227825Stheraven} 2874227825Stheraven 2875227825Stheraven// uniform_int_distribution 2876227825Stheraven 2877227825Stheraventemplate<class _IntType = int> 2878227825Stheravenclass uniform_int_distribution 2879227825Stheraven{ 2880227825Stheravenpublic: 2881227825Stheraven // types 2882227825Stheraven typedef _IntType result_type; 2883227825Stheraven 2884227825Stheraven class param_type 2885227825Stheraven { 2886227825Stheraven result_type __a_; 2887227825Stheraven result_type __b_; 2888227825Stheraven public: 2889227825Stheraven typedef uniform_int_distribution distribution_type; 2890227825Stheraven 2891227825Stheraven explicit param_type(result_type __a = 0, 2892227825Stheraven result_type __b = numeric_limits<result_type>::max()) 2893227825Stheraven : __a_(__a), __b_(__b) {} 2894227825Stheraven 2895227825Stheraven result_type a() const {return __a_;} 2896227825Stheraven result_type b() const {return __b_;} 2897227825Stheraven 2898227825Stheraven friend bool operator==(const param_type& __x, const param_type& __y) 2899227825Stheraven {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} 2900227825Stheraven friend bool operator!=(const param_type& __x, const param_type& __y) 2901227825Stheraven {return !(__x == __y);} 2902227825Stheraven }; 2903227825Stheraven 2904227825Stheravenprivate: 2905227825Stheraven param_type __p_; 2906227825Stheraven 2907227825Stheravenpublic: 2908227825Stheraven // constructors and reset functions 2909227825Stheraven explicit uniform_int_distribution(result_type __a = 0, 2910227825Stheraven result_type __b = numeric_limits<result_type>::max()) 2911227825Stheraven : __p_(param_type(__a, __b)) {} 2912227825Stheraven explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {} 2913227825Stheraven void reset() {} 2914227825Stheraven 2915227825Stheraven // generating functions 2916227825Stheraven template<class _URNG> result_type operator()(_URNG& __g) 2917227825Stheraven {return (*this)(__g, __p_);} 2918227825Stheraven template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); 2919227825Stheraven 2920227825Stheraven // property functions 2921227825Stheraven result_type a() const {return __p_.a();} 2922227825Stheraven result_type b() const {return __p_.b();} 2923227825Stheraven 2924227825Stheraven param_type param() const {return __p_;} 2925227825Stheraven void param(const param_type& __p) {__p_ = __p;} 2926227825Stheraven 2927227825Stheraven result_type min() const {return a();} 2928227825Stheraven result_type max() const {return b();} 2929227825Stheraven 2930227825Stheraven friend bool operator==(const uniform_int_distribution& __x, 2931227825Stheraven const uniform_int_distribution& __y) 2932227825Stheraven {return __x.__p_ == __y.__p_;} 2933227825Stheraven friend bool operator!=(const uniform_int_distribution& __x, 2934227825Stheraven const uniform_int_distribution& __y) 2935227825Stheraven {return !(__x == __y);} 2936227825Stheraven}; 2937227825Stheraven 2938227825Stheraventemplate<class _IntType> 2939227825Stheraventemplate<class _URNG> 2940227825Stheraventypename uniform_int_distribution<_IntType>::result_type 2941227825Stheravenuniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p) 2942227825Stheraven{ 2943227825Stheraven typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t), 2944227825Stheraven uint32_t, uint64_t>::type _UIntType; 2945232950Stheraven const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1); 2946232950Stheraven if (_Rp == 1) 2947227825Stheraven return __p.a(); 2948227825Stheraven const size_t _Dt = numeric_limits<_UIntType>::digits; 2949227825Stheraven typedef __independent_bits_engine<_URNG, _UIntType> _Eng; 2950232950Stheraven if (_Rp == 0) 2951227825Stheraven return static_cast<result_type>(_Eng(__g, _Dt)()); 2952232950Stheraven size_t __w = _Dt - __clz(_Rp) - 1; 2953232950Stheraven if ((_Rp & (_UIntType(~0) >> (_Dt - __w))) != 0) 2954227825Stheraven ++__w; 2955227825Stheraven _Eng __e(__g, __w); 2956227825Stheraven _UIntType __u; 2957227825Stheraven do 2958227825Stheraven { 2959227825Stheraven __u = __e(); 2960232950Stheraven } while (__u >= _Rp); 2961227825Stheraven return static_cast<result_type>(__u + __p.a()); 2962227825Stheraven} 2963227825Stheraven 2964227825Stheravenclass __rs_default; 2965227825Stheraven 2966227825Stheraven__rs_default __rs_get(); 2967227825Stheraven 2968227825Stheravenclass __rs_default 2969227825Stheraven{ 2970227825Stheraven static unsigned __c_; 2971227825Stheraven 2972227825Stheraven __rs_default(); 2973227825Stheravenpublic: 2974249998Sdim typedef uint_fast32_t result_type; 2975227825Stheraven 2976227825Stheraven static const result_type _Min = 0; 2977227825Stheraven static const result_type _Max = 0xFFFFFFFF; 2978227825Stheraven 2979227825Stheraven __rs_default(const __rs_default&); 2980227825Stheraven ~__rs_default(); 2981227825Stheraven 2982227825Stheraven result_type operator()(); 2983227825Stheraven 2984234976Stheraven static _LIBCPP_CONSTEXPR result_type min() {return _Min;} 2985234976Stheraven static _LIBCPP_CONSTEXPR result_type max() {return _Max;} 2986227825Stheraven 2987227825Stheraven friend __rs_default __rs_get(); 2988227825Stheraven}; 2989227825Stheraven 2990227825Stheraven__rs_default __rs_get(); 2991227825Stheraven 2992227825Stheraventemplate <class _RandomAccessIterator> 2993227825Stheravenvoid 2994227825Stheravenrandom_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last) 2995227825Stheraven{ 2996227825Stheraven typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; 2997232950Stheraven typedef uniform_int_distribution<ptrdiff_t> _Dp; 2998232950Stheraven typedef typename _Dp::param_type _Pp; 2999227825Stheraven difference_type __d = __last - __first; 3000227825Stheraven if (__d > 1) 3001227825Stheraven { 3002232950Stheraven _Dp __uid; 3003227825Stheraven __rs_default __g = __rs_get(); 3004227825Stheraven for (--__last, --__d; __first < __last; ++__first, --__d) 3005227825Stheraven { 3006232950Stheraven difference_type __i = __uid(__g, _Pp(0, __d)); 3007227825Stheraven if (__i != difference_type(0)) 3008227825Stheraven swap(*__first, *(__first + __i)); 3009227825Stheraven } 3010227825Stheraven } 3011227825Stheraven} 3012227825Stheraven 3013227825Stheraventemplate <class _RandomAccessIterator, class _RandomNumberGenerator> 3014227825Stheravenvoid 3015227825Stheravenrandom_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last, 3016227825Stheraven#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 3017227825Stheraven _RandomNumberGenerator&& __rand) 3018227825Stheraven#else 3019227825Stheraven _RandomNumberGenerator& __rand) 3020227825Stheraven#endif 3021227825Stheraven{ 3022227825Stheraven typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; 3023227825Stheraven difference_type __d = __last - __first; 3024227825Stheraven if (__d > 1) 3025227825Stheraven { 3026227825Stheraven for (--__last; __first < __last; ++__first, --__d) 3027227825Stheraven { 3028227825Stheraven difference_type __i = __rand(__d); 3029227825Stheraven swap(*__first, *(__first + __i)); 3030227825Stheraven } 3031227825Stheraven } 3032227825Stheraven} 3033227825Stheraven 3034227825Stheraventemplate<class _RandomAccessIterator, class _UniformRandomNumberGenerator> 3035227825Stheraven void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last, 3036227825Stheraven#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 3037227825Stheraven _UniformRandomNumberGenerator&& __g) 3038227825Stheraven#else 3039227825Stheraven _UniformRandomNumberGenerator& __g) 3040227825Stheraven#endif 3041227825Stheraven{ 3042227825Stheraven typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; 3043232950Stheraven typedef uniform_int_distribution<ptrdiff_t> _Dp; 3044232950Stheraven typedef typename _Dp::param_type _Pp; 3045227825Stheraven difference_type __d = __last - __first; 3046227825Stheraven if (__d > 1) 3047227825Stheraven { 3048232950Stheraven _Dp __uid; 3049227825Stheraven for (--__last, --__d; __first < __last; ++__first, --__d) 3050227825Stheraven { 3051232950Stheraven difference_type __i = __uid(__g, _Pp(0, __d)); 3052227825Stheraven if (__i != difference_type(0)) 3053227825Stheraven swap(*__first, *(__first + __i)); 3054227825Stheraven } 3055227825Stheraven } 3056227825Stheraven} 3057227825Stheraven 3058227825Stheraventemplate <class _InputIterator, class _Predicate> 3059227825Stheravenbool 3060227825Stheravenis_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred) 3061227825Stheraven{ 3062227825Stheraven for (; __first != __last; ++__first) 3063227825Stheraven if (!__pred(*__first)) 3064227825Stheraven break; 3065227825Stheraven for (; __first != __last; ++__first) 3066227825Stheraven if (__pred(*__first)) 3067227825Stheraven return false; 3068227825Stheraven return true; 3069227825Stheraven} 3070227825Stheraven 3071227825Stheraven// partition 3072227825Stheraven 3073227825Stheraventemplate <class _Predicate, class _ForwardIterator> 3074227825Stheraven_ForwardIterator 3075227825Stheraven__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag) 3076227825Stheraven{ 3077227825Stheraven while (true) 3078227825Stheraven { 3079227825Stheraven if (__first == __last) 3080227825Stheraven return __first; 3081227825Stheraven if (!__pred(*__first)) 3082227825Stheraven break; 3083227825Stheraven ++__first; 3084227825Stheraven } 3085227825Stheraven for (_ForwardIterator __p = __first; ++__p != __last;) 3086227825Stheraven { 3087227825Stheraven if (__pred(*__p)) 3088227825Stheraven { 3089227825Stheraven swap(*__first, *__p); 3090227825Stheraven ++__first; 3091227825Stheraven } 3092227825Stheraven } 3093227825Stheraven return __first; 3094227825Stheraven} 3095227825Stheraven 3096227825Stheraventemplate <class _Predicate, class _BidirectionalIterator> 3097227825Stheraven_BidirectionalIterator 3098227825Stheraven__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred, 3099227825Stheraven bidirectional_iterator_tag) 3100227825Stheraven{ 3101227825Stheraven while (true) 3102227825Stheraven { 3103227825Stheraven while (true) 3104227825Stheraven { 3105227825Stheraven if (__first == __last) 3106227825Stheraven return __first; 3107227825Stheraven if (!__pred(*__first)) 3108227825Stheraven break; 3109227825Stheraven ++__first; 3110227825Stheraven } 3111227825Stheraven do 3112227825Stheraven { 3113227825Stheraven if (__first == --__last) 3114227825Stheraven return __first; 3115227825Stheraven } while (!__pred(*__last)); 3116227825Stheraven swap(*__first, *__last); 3117227825Stheraven ++__first; 3118227825Stheraven } 3119227825Stheraven} 3120227825Stheraven 3121227825Stheraventemplate <class _ForwardIterator, class _Predicate> 3122227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 3123227825Stheraven_ForwardIterator 3124227825Stheravenpartition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) 3125227825Stheraven{ 3126227825Stheraven return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type> 3127227825Stheraven (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category()); 3128227825Stheraven} 3129227825Stheraven 3130227825Stheraven// partition_copy 3131227825Stheraven 3132227825Stheraventemplate <class _InputIterator, class _OutputIterator1, 3133227825Stheraven class _OutputIterator2, class _Predicate> 3134227825Stheravenpair<_OutputIterator1, _OutputIterator2> 3135227825Stheravenpartition_copy(_InputIterator __first, _InputIterator __last, 3136227825Stheraven _OutputIterator1 __out_true, _OutputIterator2 __out_false, 3137227825Stheraven _Predicate __pred) 3138227825Stheraven{ 3139227825Stheraven for (; __first != __last; ++__first) 3140227825Stheraven { 3141227825Stheraven if (__pred(*__first)) 3142227825Stheraven { 3143227825Stheraven *__out_true = *__first; 3144227825Stheraven ++__out_true; 3145227825Stheraven } 3146227825Stheraven else 3147227825Stheraven { 3148227825Stheraven *__out_false = *__first; 3149227825Stheraven ++__out_false; 3150227825Stheraven } 3151227825Stheraven } 3152227825Stheraven return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false); 3153227825Stheraven} 3154227825Stheraven 3155227825Stheraven// partition_point 3156227825Stheraven 3157227825Stheraventemplate<class _ForwardIterator, class _Predicate> 3158227825Stheraven_ForwardIterator 3159227825Stheravenpartition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) 3160227825Stheraven{ 3161227825Stheraven typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type; 3162227825Stheraven difference_type __len = _VSTD::distance(__first, __last); 3163227825Stheraven while (__len != 0) 3164227825Stheraven { 3165227825Stheraven difference_type __l2 = __len / 2; 3166227825Stheraven _ForwardIterator __m = __first; 3167227825Stheraven _VSTD::advance(__m, __l2); 3168227825Stheraven if (__pred(*__m)) 3169227825Stheraven { 3170227825Stheraven __first = ++__m; 3171227825Stheraven __len -= __l2 + 1; 3172227825Stheraven } 3173227825Stheraven else 3174227825Stheraven __len = __l2; 3175227825Stheraven } 3176227825Stheraven return __first; 3177227825Stheraven} 3178227825Stheraven 3179227825Stheraven// stable_partition 3180227825Stheraven 3181227825Stheraventemplate <class _Predicate, class _ForwardIterator, class _Distance, class _Pair> 3182227825Stheraven_ForwardIterator 3183227825Stheraven__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, 3184227825Stheraven _Distance __len, _Pair __p, forward_iterator_tag __fit) 3185227825Stheraven{ 3186227825Stheraven // *__first is known to be false 3187227825Stheraven // __len >= 1 3188227825Stheraven if (__len == 1) 3189227825Stheraven return __first; 3190227825Stheraven if (__len == 2) 3191227825Stheraven { 3192227825Stheraven _ForwardIterator __m = __first; 3193227825Stheraven if (__pred(*++__m)) 3194227825Stheraven { 3195227825Stheraven swap(*__first, *__m); 3196227825Stheraven return __m; 3197227825Stheraven } 3198227825Stheraven return __first; 3199227825Stheraven } 3200227825Stheraven if (__len <= __p.second) 3201227825Stheraven { // The buffer is big enough to use 3202227825Stheraven typedef typename iterator_traits<_ForwardIterator>::value_type value_type; 3203227825Stheraven __destruct_n __d(0); 3204227825Stheraven unique_ptr<value_type, __destruct_n&> __h(__p.first, __d); 3205227825Stheraven // Move the falses into the temporary buffer, and the trues to the front of the line 3206227825Stheraven // Update __first to always point to the end of the trues 3207227825Stheraven value_type* __t = __p.first; 3208227825Stheraven ::new(__t) value_type(_VSTD::move(*__first)); 3209227825Stheraven __d.__incr((value_type*)0); 3210227825Stheraven ++__t; 3211227825Stheraven _ForwardIterator __i = __first; 3212227825Stheraven while (++__i != __last) 3213227825Stheraven { 3214227825Stheraven if (__pred(*__i)) 3215227825Stheraven { 3216227825Stheraven *__first = _VSTD::move(*__i); 3217227825Stheraven ++__first; 3218227825Stheraven } 3219227825Stheraven else 3220227825Stheraven { 3221227825Stheraven ::new(__t) value_type(_VSTD::move(*__i)); 3222227825Stheraven __d.__incr((value_type*)0); 3223227825Stheraven ++__t; 3224227825Stheraven } 3225227825Stheraven } 3226227825Stheraven // All trues now at start of range, all falses in buffer 3227227825Stheraven // Move falses back into range, but don't mess up __first which points to first false 3228227825Stheraven __i = __first; 3229227825Stheraven for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i) 3230227825Stheraven *__i = _VSTD::move(*__t2); 3231227825Stheraven // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer 3232227825Stheraven return __first; 3233227825Stheraven } 3234227825Stheraven // Else not enough buffer, do in place 3235227825Stheraven // __len >= 3 3236227825Stheraven _ForwardIterator __m = __first; 3237227825Stheraven _Distance __len2 = __len / 2; // __len2 >= 2 3238227825Stheraven _VSTD::advance(__m, __len2); 3239227825Stheraven // recurse on [__first, __m), *__first know to be false 3240227825Stheraven // F????????????????? 3241227825Stheraven // f m l 3242227825Stheraven typedef typename add_lvalue_reference<_Predicate>::type _PredRef; 3243227825Stheraven _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit); 3244227825Stheraven // TTTFFFFF?????????? 3245227825Stheraven // f ff m l 3246227825Stheraven // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true 3247227825Stheraven _ForwardIterator __m1 = __m; 3248227825Stheraven _ForwardIterator __second_false = __last; 3249227825Stheraven _Distance __len_half = __len - __len2; 3250227825Stheraven while (__pred(*__m1)) 3251227825Stheraven { 3252227825Stheraven if (++__m1 == __last) 3253227825Stheraven goto __second_half_done; 3254227825Stheraven --__len_half; 3255227825Stheraven } 3256227825Stheraven // TTTFFFFFTTTF?????? 3257227825Stheraven // f ff m m1 l 3258227825Stheraven __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit); 3259227825Stheraven__second_half_done: 3260227825Stheraven // TTTFFFFFTTTTTFFFFF 3261227825Stheraven // f ff m sf l 3262227825Stheraven return _VSTD::rotate(__first_false, __m, __second_false); 3263227825Stheraven // TTTTTTTTFFFFFFFFFF 3264227825Stheraven // | 3265227825Stheraven} 3266227825Stheraven 3267227825Stheravenstruct __return_temporary_buffer 3268227825Stheraven{ 3269227825Stheraven template <class _Tp> 3270227825Stheraven _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);} 3271227825Stheraven}; 3272227825Stheraven 3273227825Stheraventemplate <class _Predicate, class _ForwardIterator> 3274227825Stheraven_ForwardIterator 3275227825Stheraven__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, 3276227825Stheraven forward_iterator_tag) 3277227825Stheraven{ 3278227825Stheraven const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment 3279227825Stheraven // Either prove all true and return __first or point to first false 3280227825Stheraven while (true) 3281227825Stheraven { 3282227825Stheraven if (__first == __last) 3283227825Stheraven return __first; 3284227825Stheraven if (!__pred(*__first)) 3285227825Stheraven break; 3286227825Stheraven ++__first; 3287227825Stheraven } 3288227825Stheraven // We now have a reduced range [__first, __last) 3289227825Stheraven // *__first is known to be false 3290227825Stheraven typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type; 3291227825Stheraven typedef typename iterator_traits<_ForwardIterator>::value_type value_type; 3292227825Stheraven difference_type __len = _VSTD::distance(__first, __last); 3293227825Stheraven pair<value_type*, ptrdiff_t> __p(0, 0); 3294227825Stheraven unique_ptr<value_type, __return_temporary_buffer> __h; 3295227825Stheraven if (__len >= __alloc_limit) 3296227825Stheraven { 3297227825Stheraven __p = _VSTD::get_temporary_buffer<value_type>(__len); 3298227825Stheraven __h.reset(__p.first); 3299227825Stheraven } 3300227825Stheraven return __stable_partition<typename add_lvalue_reference<_Predicate>::type> 3301227825Stheraven (__first, __last, __pred, __len, __p, forward_iterator_tag()); 3302227825Stheraven} 3303227825Stheraven 3304227825Stheraventemplate <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair> 3305227825Stheraven_BidirectionalIterator 3306227825Stheraven__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred, 3307227825Stheraven _Distance __len, _Pair __p, bidirectional_iterator_tag __bit) 3308227825Stheraven{ 3309227825Stheraven // *__first is known to be false 3310227825Stheraven // *__last is known to be true 3311227825Stheraven // __len >= 2 3312227825Stheraven if (__len == 2) 3313227825Stheraven { 3314227825Stheraven swap(*__first, *__last); 3315227825Stheraven return __last; 3316227825Stheraven } 3317227825Stheraven if (__len == 3) 3318227825Stheraven { 3319227825Stheraven _BidirectionalIterator __m = __first; 3320227825Stheraven if (__pred(*++__m)) 3321227825Stheraven { 3322227825Stheraven swap(*__first, *__m); 3323227825Stheraven swap(*__m, *__last); 3324227825Stheraven return __last; 3325227825Stheraven } 3326227825Stheraven swap(*__m, *__last); 3327227825Stheraven swap(*__first, *__m); 3328227825Stheraven return __m; 3329227825Stheraven } 3330227825Stheraven if (__len <= __p.second) 3331227825Stheraven { // The buffer is big enough to use 3332227825Stheraven typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type; 3333227825Stheraven __destruct_n __d(0); 3334227825Stheraven unique_ptr<value_type, __destruct_n&> __h(__p.first, __d); 3335227825Stheraven // Move the falses into the temporary buffer, and the trues to the front of the line 3336227825Stheraven // Update __first to always point to the end of the trues 3337227825Stheraven value_type* __t = __p.first; 3338227825Stheraven ::new(__t) value_type(_VSTD::move(*__first)); 3339227825Stheraven __d.__incr((value_type*)0); 3340227825Stheraven ++__t; 3341227825Stheraven _BidirectionalIterator __i = __first; 3342227825Stheraven while (++__i != __last) 3343227825Stheraven { 3344227825Stheraven if (__pred(*__i)) 3345227825Stheraven { 3346227825Stheraven *__first = _VSTD::move(*__i); 3347227825Stheraven ++__first; 3348227825Stheraven } 3349227825Stheraven else 3350227825Stheraven { 3351227825Stheraven ::new(__t) value_type(_VSTD::move(*__i)); 3352227825Stheraven __d.__incr((value_type*)0); 3353227825Stheraven ++__t; 3354227825Stheraven } 3355227825Stheraven } 3356227825Stheraven // move *__last, known to be true 3357227825Stheraven *__first = _VSTD::move(*__i); 3358227825Stheraven __i = ++__first; 3359227825Stheraven // All trues now at start of range, all falses in buffer 3360227825Stheraven // Move falses back into range, but don't mess up __first which points to first false 3361227825Stheraven for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i) 3362227825Stheraven *__i = _VSTD::move(*__t2); 3363227825Stheraven // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer 3364227825Stheraven return __first; 3365227825Stheraven } 3366227825Stheraven // Else not enough buffer, do in place 3367227825Stheraven // __len >= 4 3368227825Stheraven _BidirectionalIterator __m = __first; 3369227825Stheraven _Distance __len2 = __len / 2; // __len2 >= 2 3370227825Stheraven _VSTD::advance(__m, __len2); 3371227825Stheraven // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false 3372227825Stheraven // F????????????????T 3373227825Stheraven // f m l 3374227825Stheraven _BidirectionalIterator __m1 = __m; 3375227825Stheraven _BidirectionalIterator __first_false = __first; 3376227825Stheraven _Distance __len_half = __len2; 3377227825Stheraven while (!__pred(*--__m1)) 3378227825Stheraven { 3379227825Stheraven if (__m1 == __first) 3380227825Stheraven goto __first_half_done; 3381227825Stheraven --__len_half; 3382227825Stheraven } 3383227825Stheraven // F???TFFF?????????T 3384227825Stheraven // f m1 m l 3385227825Stheraven typedef typename add_lvalue_reference<_Predicate>::type _PredRef; 3386227825Stheraven __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit); 3387227825Stheraven__first_half_done: 3388227825Stheraven // TTTFFFFF?????????T 3389227825Stheraven // f ff m l 3390227825Stheraven // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true 3391227825Stheraven __m1 = __m; 3392227825Stheraven _BidirectionalIterator __second_false = __last; 3393227825Stheraven ++__second_false; 3394227825Stheraven __len_half = __len - __len2; 3395227825Stheraven while (__pred(*__m1)) 3396227825Stheraven { 3397227825Stheraven if (++__m1 == __last) 3398227825Stheraven goto __second_half_done; 3399227825Stheraven --__len_half; 3400227825Stheraven } 3401227825Stheraven // TTTFFFFFTTTF?????T 3402227825Stheraven // f ff m m1 l 3403227825Stheraven __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit); 3404227825Stheraven__second_half_done: 3405227825Stheraven // TTTFFFFFTTTTTFFFFF 3406227825Stheraven // f ff m sf l 3407227825Stheraven return _VSTD::rotate(__first_false, __m, __second_false); 3408227825Stheraven // TTTTTTTTFFFFFFFFFF 3409227825Stheraven // | 3410227825Stheraven} 3411227825Stheraven 3412227825Stheraventemplate <class _Predicate, class _BidirectionalIterator> 3413227825Stheraven_BidirectionalIterator 3414227825Stheraven__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred, 3415227825Stheraven bidirectional_iterator_tag) 3416227825Stheraven{ 3417227825Stheraven typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type; 3418227825Stheraven typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type; 3419227825Stheraven const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment 3420227825Stheraven // Either prove all true and return __first or point to first false 3421227825Stheraven while (true) 3422227825Stheraven { 3423227825Stheraven if (__first == __last) 3424227825Stheraven return __first; 3425227825Stheraven if (!__pred(*__first)) 3426227825Stheraven break; 3427227825Stheraven ++__first; 3428227825Stheraven } 3429227825Stheraven // __first points to first false, everything prior to __first is already set. 3430227825Stheraven // Either prove [__first, __last) is all false and return __first, or point __last to last true 3431227825Stheraven do 3432227825Stheraven { 3433227825Stheraven if (__first == --__last) 3434227825Stheraven return __first; 3435227825Stheraven } while (!__pred(*__last)); 3436227825Stheraven // We now have a reduced range [__first, __last] 3437227825Stheraven // *__first is known to be false 3438227825Stheraven // *__last is known to be true 3439227825Stheraven // __len >= 2 3440227825Stheraven difference_type __len = _VSTD::distance(__first, __last) + 1; 3441227825Stheraven pair<value_type*, ptrdiff_t> __p(0, 0); 3442227825Stheraven unique_ptr<value_type, __return_temporary_buffer> __h; 3443227825Stheraven if (__len >= __alloc_limit) 3444227825Stheraven { 3445227825Stheraven __p = _VSTD::get_temporary_buffer<value_type>(__len); 3446227825Stheraven __h.reset(__p.first); 3447227825Stheraven } 3448227825Stheraven return __stable_partition<typename add_lvalue_reference<_Predicate>::type> 3449227825Stheraven (__first, __last, __pred, __len, __p, bidirectional_iterator_tag()); 3450227825Stheraven} 3451227825Stheraven 3452227825Stheraventemplate <class _ForwardIterator, class _Predicate> 3453227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 3454227825Stheraven_ForwardIterator 3455227825Stheravenstable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) 3456227825Stheraven{ 3457227825Stheraven return __stable_partition<typename add_lvalue_reference<_Predicate>::type> 3458227825Stheraven (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category()); 3459227825Stheraven} 3460227825Stheraven 3461227825Stheraven// is_sorted_until 3462227825Stheraven 3463227825Stheraventemplate <class _ForwardIterator, class _Compare> 3464227825Stheraven_ForwardIterator 3465227825Stheravenis_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) 3466227825Stheraven{ 3467227825Stheraven if (__first != __last) 3468227825Stheraven { 3469227825Stheraven _ForwardIterator __i = __first; 3470227825Stheraven while (++__i != __last) 3471227825Stheraven { 3472227825Stheraven if (__comp(*__i, *__first)) 3473227825Stheraven return __i; 3474227825Stheraven __first = __i; 3475227825Stheraven } 3476227825Stheraven } 3477227825Stheraven return __last; 3478227825Stheraven} 3479227825Stheraven 3480227825Stheraventemplate<class _ForwardIterator> 3481227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 3482227825Stheraven_ForwardIterator 3483227825Stheravenis_sorted_until(_ForwardIterator __first, _ForwardIterator __last) 3484227825Stheraven{ 3485227825Stheraven return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>()); 3486227825Stheraven} 3487227825Stheraven 3488227825Stheraven// is_sorted 3489227825Stheraven 3490227825Stheraventemplate <class _ForwardIterator, class _Compare> 3491227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 3492227825Stheravenbool 3493227825Stheravenis_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) 3494227825Stheraven{ 3495227825Stheraven return _VSTD::is_sorted_until(__first, __last, __comp) == __last; 3496227825Stheraven} 3497227825Stheraven 3498227825Stheraventemplate<class _ForwardIterator> 3499227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 3500227825Stheravenbool 3501227825Stheravenis_sorted(_ForwardIterator __first, _ForwardIterator __last) 3502227825Stheraven{ 3503227825Stheraven return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>()); 3504227825Stheraven} 3505227825Stheraven 3506227825Stheraven// sort 3507227825Stheraven 3508227825Stheraven// stable, 2-3 compares, 0-2 swaps 3509227825Stheraven 3510227825Stheraventemplate <class _Compare, class _ForwardIterator> 3511227825Stheravenunsigned 3512227825Stheraven__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c) 3513227825Stheraven{ 3514227825Stheraven unsigned __r = 0; 3515227825Stheraven if (!__c(*__y, *__x)) // if x <= y 3516227825Stheraven { 3517227825Stheraven if (!__c(*__z, *__y)) // if y <= z 3518227825Stheraven return __r; // x <= y && y <= z 3519227825Stheraven // x <= y && y > z 3520227825Stheraven swap(*__y, *__z); // x <= z && y < z 3521227825Stheraven __r = 1; 3522227825Stheraven if (__c(*__y, *__x)) // if x > y 3523227825Stheraven { 3524227825Stheraven swap(*__x, *__y); // x < y && y <= z 3525227825Stheraven __r = 2; 3526227825Stheraven } 3527227825Stheraven return __r; // x <= y && y < z 3528227825Stheraven } 3529227825Stheraven if (__c(*__z, *__y)) // x > y, if y > z 3530227825Stheraven { 3531227825Stheraven swap(*__x, *__z); // x < y && y < z 3532227825Stheraven __r = 1; 3533227825Stheraven return __r; 3534227825Stheraven } 3535227825Stheraven swap(*__x, *__y); // x > y && y <= z 3536227825Stheraven __r = 1; // x < y && x <= z 3537227825Stheraven if (__c(*__z, *__y)) // if y > z 3538227825Stheraven { 3539227825Stheraven swap(*__y, *__z); // x <= y && y < z 3540227825Stheraven __r = 2; 3541227825Stheraven } 3542227825Stheraven return __r; 3543227825Stheraven} // x <= y && y <= z 3544227825Stheraven 3545227825Stheraven// stable, 3-6 compares, 0-5 swaps 3546227825Stheraven 3547227825Stheraventemplate <class _Compare, class _ForwardIterator> 3548227825Stheravenunsigned 3549227825Stheraven__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3, 3550227825Stheraven _ForwardIterator __x4, _Compare __c) 3551227825Stheraven{ 3552227825Stheraven unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c); 3553227825Stheraven if (__c(*__x4, *__x3)) 3554227825Stheraven { 3555227825Stheraven swap(*__x3, *__x4); 3556227825Stheraven ++__r; 3557227825Stheraven if (__c(*__x3, *__x2)) 3558227825Stheraven { 3559227825Stheraven swap(*__x2, *__x3); 3560227825Stheraven ++__r; 3561227825Stheraven if (__c(*__x2, *__x1)) 3562227825Stheraven { 3563227825Stheraven swap(*__x1, *__x2); 3564227825Stheraven ++__r; 3565227825Stheraven } 3566227825Stheraven } 3567227825Stheraven } 3568227825Stheraven return __r; 3569227825Stheraven} 3570227825Stheraven 3571227825Stheraven// stable, 4-10 compares, 0-9 swaps 3572227825Stheraven 3573227825Stheraventemplate <class _Compare, class _ForwardIterator> 3574227825Stheravenunsigned 3575227825Stheraven__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3, 3576227825Stheraven _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c) 3577227825Stheraven{ 3578227825Stheraven unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c); 3579227825Stheraven if (__c(*__x5, *__x4)) 3580227825Stheraven { 3581227825Stheraven swap(*__x4, *__x5); 3582227825Stheraven ++__r; 3583227825Stheraven if (__c(*__x4, *__x3)) 3584227825Stheraven { 3585227825Stheraven swap(*__x3, *__x4); 3586227825Stheraven ++__r; 3587227825Stheraven if (__c(*__x3, *__x2)) 3588227825Stheraven { 3589227825Stheraven swap(*__x2, *__x3); 3590227825Stheraven ++__r; 3591227825Stheraven if (__c(*__x2, *__x1)) 3592227825Stheraven { 3593227825Stheraven swap(*__x1, *__x2); 3594227825Stheraven ++__r; 3595227825Stheraven } 3596227825Stheraven } 3597227825Stheraven } 3598227825Stheraven } 3599227825Stheraven return __r; 3600227825Stheraven} 3601227825Stheraven 3602227825Stheraven// Assumes size > 0 3603227825Stheraventemplate <class _Compare, class _BirdirectionalIterator> 3604227825Stheravenvoid 3605227825Stheraven__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp) 3606227825Stheraven{ 3607227825Stheraven _BirdirectionalIterator __lm1 = __last; 3608227825Stheraven for (--__lm1; __first != __lm1; ++__first) 3609227825Stheraven { 3610227825Stheraven _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator, 3611227825Stheraven typename add_lvalue_reference<_Compare>::type> 3612227825Stheraven (__first, __last, __comp); 3613227825Stheraven if (__i != __first) 3614227825Stheraven swap(*__first, *__i); 3615227825Stheraven } 3616227825Stheraven} 3617227825Stheraven 3618227825Stheraventemplate <class _Compare, class _BirdirectionalIterator> 3619227825Stheravenvoid 3620227825Stheraven__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp) 3621227825Stheraven{ 3622227825Stheraven typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type; 3623227825Stheraven if (__first != __last) 3624227825Stheraven { 3625227825Stheraven _BirdirectionalIterator __i = __first; 3626227825Stheraven for (++__i; __i != __last; ++__i) 3627227825Stheraven { 3628227825Stheraven _BirdirectionalIterator __j = __i; 3629227825Stheraven value_type __t(_VSTD::move(*__j)); 3630227825Stheraven for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j) 3631227825Stheraven *__j = _VSTD::move(*__k); 3632227825Stheraven *__j = _VSTD::move(__t); 3633227825Stheraven } 3634227825Stheraven } 3635227825Stheraven} 3636227825Stheraven 3637227825Stheraventemplate <class _Compare, class _RandomAccessIterator> 3638227825Stheravenvoid 3639227825Stheraven__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 3640227825Stheraven{ 3641227825Stheraven typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type; 3642227825Stheraven _RandomAccessIterator __j = __first+2; 3643227825Stheraven __sort3<_Compare>(__first, __first+1, __j, __comp); 3644227825Stheraven for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i) 3645227825Stheraven { 3646227825Stheraven if (__comp(*__i, *__j)) 3647227825Stheraven { 3648227825Stheraven value_type __t(_VSTD::move(*__i)); 3649227825Stheraven _RandomAccessIterator __k = __j; 3650227825Stheraven __j = __i; 3651227825Stheraven do 3652227825Stheraven { 3653227825Stheraven *__j = _VSTD::move(*__k); 3654227825Stheraven __j = __k; 3655227825Stheraven } while (__j != __first && __comp(__t, *--__k)); 3656227825Stheraven *__j = _VSTD::move(__t); 3657227825Stheraven } 3658227825Stheraven __j = __i; 3659227825Stheraven } 3660227825Stheraven} 3661227825Stheraven 3662227825Stheraventemplate <class _Compare, class _RandomAccessIterator> 3663227825Stheravenbool 3664227825Stheraven__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 3665227825Stheraven{ 3666227825Stheraven switch (__last - __first) 3667227825Stheraven { 3668227825Stheraven case 0: 3669227825Stheraven case 1: 3670227825Stheraven return true; 3671227825Stheraven case 2: 3672227825Stheraven if (__comp(*--__last, *__first)) 3673227825Stheraven swap(*__first, *__last); 3674227825Stheraven return true; 3675227825Stheraven case 3: 3676227825Stheraven _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp); 3677227825Stheraven return true; 3678227825Stheraven case 4: 3679227825Stheraven _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp); 3680227825Stheraven return true; 3681227825Stheraven case 5: 3682227825Stheraven _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp); 3683227825Stheraven return true; 3684227825Stheraven } 3685227825Stheraven typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type; 3686227825Stheraven _RandomAccessIterator __j = __first+2; 3687227825Stheraven __sort3<_Compare>(__first, __first+1, __j, __comp); 3688227825Stheraven const unsigned __limit = 8; 3689227825Stheraven unsigned __count = 0; 3690227825Stheraven for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i) 3691227825Stheraven { 3692227825Stheraven if (__comp(*__i, *__j)) 3693227825Stheraven { 3694227825Stheraven value_type __t(_VSTD::move(*__i)); 3695227825Stheraven _RandomAccessIterator __k = __j; 3696227825Stheraven __j = __i; 3697227825Stheraven do 3698227825Stheraven { 3699227825Stheraven *__j = _VSTD::move(*__k); 3700227825Stheraven __j = __k; 3701227825Stheraven } while (__j != __first && __comp(__t, *--__k)); 3702227825Stheraven *__j = _VSTD::move(__t); 3703227825Stheraven if (++__count == __limit) 3704227825Stheraven return ++__i == __last; 3705227825Stheraven } 3706227825Stheraven __j = __i; 3707227825Stheraven } 3708227825Stheraven return true; 3709227825Stheraven} 3710227825Stheraven 3711227825Stheraventemplate <class _Compare, class _BirdirectionalIterator> 3712227825Stheravenvoid 3713227825Stheraven__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1, 3714227825Stheraven typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp) 3715227825Stheraven{ 3716227825Stheraven typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type; 3717227825Stheraven if (__first1 != __last1) 3718227825Stheraven { 3719227825Stheraven __destruct_n __d(0); 3720227825Stheraven unique_ptr<value_type, __destruct_n&> __h(__first2, __d); 3721227825Stheraven value_type* __last2 = __first2; 3722227825Stheraven ::new(__last2) value_type(_VSTD::move(*__first1)); 3723227825Stheraven __d.__incr((value_type*)0); 3724227825Stheraven for (++__last2; ++__first1 != __last1; ++__last2) 3725227825Stheraven { 3726227825Stheraven value_type* __j2 = __last2; 3727227825Stheraven value_type* __i2 = __j2; 3728227825Stheraven if (__comp(*__first1, *--__i2)) 3729227825Stheraven { 3730227825Stheraven ::new(__j2) value_type(_VSTD::move(*__i2)); 3731227825Stheraven __d.__incr((value_type*)0); 3732227825Stheraven for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2) 3733227825Stheraven *__j2 = _VSTD::move(*__i2); 3734227825Stheraven *__j2 = _VSTD::move(*__first1); 3735227825Stheraven } 3736227825Stheraven else 3737227825Stheraven { 3738227825Stheraven ::new(__j2) value_type(_VSTD::move(*__first1)); 3739227825Stheraven __d.__incr((value_type*)0); 3740227825Stheraven } 3741227825Stheraven } 3742227825Stheraven __h.release(); 3743227825Stheraven } 3744227825Stheraven} 3745227825Stheraven 3746227825Stheraventemplate <class _Compare, class _RandomAccessIterator> 3747227825Stheravenvoid 3748227825Stheraven__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 3749227825Stheraven{ 3750227825Stheraven // _Compare is known to be a reference type 3751227825Stheraven typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; 3752227825Stheraven typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type; 3753227825Stheraven const difference_type __limit = is_trivially_copy_constructible<value_type>::value && 3754227825Stheraven is_trivially_copy_assignable<value_type>::value ? 30 : 6; 3755227825Stheraven while (true) 3756227825Stheraven { 3757227825Stheraven __restart: 3758227825Stheraven difference_type __len = __last - __first; 3759227825Stheraven switch (__len) 3760227825Stheraven { 3761227825Stheraven case 0: 3762227825Stheraven case 1: 3763227825Stheraven return; 3764227825Stheraven case 2: 3765227825Stheraven if (__comp(*--__last, *__first)) 3766227825Stheraven swap(*__first, *__last); 3767227825Stheraven return; 3768227825Stheraven case 3: 3769227825Stheraven _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp); 3770227825Stheraven return; 3771227825Stheraven case 4: 3772227825Stheraven _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp); 3773227825Stheraven return; 3774227825Stheraven case 5: 3775227825Stheraven _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp); 3776227825Stheraven return; 3777227825Stheraven } 3778227825Stheraven if (__len <= __limit) 3779227825Stheraven { 3780227825Stheraven _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp); 3781227825Stheraven return; 3782227825Stheraven } 3783227825Stheraven // __len > 5 3784227825Stheraven _RandomAccessIterator __m = __first; 3785227825Stheraven _RandomAccessIterator __lm1 = __last; 3786227825Stheraven --__lm1; 3787227825Stheraven unsigned __n_swaps; 3788227825Stheraven { 3789227825Stheraven difference_type __delta; 3790227825Stheraven if (__len >= 1000) 3791227825Stheraven { 3792227825Stheraven __delta = __len/2; 3793227825Stheraven __m += __delta; 3794227825Stheraven __delta /= 2; 3795227825Stheraven __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp); 3796227825Stheraven } 3797227825Stheraven else 3798227825Stheraven { 3799227825Stheraven __delta = __len/2; 3800227825Stheraven __m += __delta; 3801227825Stheraven __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp); 3802227825Stheraven } 3803227825Stheraven } 3804227825Stheraven // *__m is median 3805227825Stheraven // partition [__first, __m) < *__m and *__m <= [__m, __last) 3806227825Stheraven // (this inhibits tossing elements equivalent to __m around unnecessarily) 3807227825Stheraven _RandomAccessIterator __i = __first; 3808227825Stheraven _RandomAccessIterator __j = __lm1; 3809227825Stheraven // j points beyond range to be tested, *__m is known to be <= *__lm1 3810227825Stheraven // The search going up is known to be guarded but the search coming down isn't. 3811227825Stheraven // Prime the downward search with a guard. 3812227825Stheraven if (!__comp(*__i, *__m)) // if *__first == *__m 3813227825Stheraven { 3814227825Stheraven // *__first == *__m, *__first doesn't go in first part 3815227825Stheraven // manually guard downward moving __j against __i 3816227825Stheraven while (true) 3817227825Stheraven { 3818227825Stheraven if (__i == --__j) 3819227825Stheraven { 3820227825Stheraven // *__first == *__m, *__m <= all other elements 3821227825Stheraven // Parition instead into [__first, __i) == *__first and *__first < [__i, __last) 3822227825Stheraven ++__i; // __first + 1 3823227825Stheraven __j = __last; 3824227825Stheraven if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1) 3825227825Stheraven { 3826227825Stheraven while (true) 3827227825Stheraven { 3828227825Stheraven if (__i == __j) 3829227825Stheraven return; // [__first, __last) all equivalent elements 3830227825Stheraven if (__comp(*__first, *__i)) 3831227825Stheraven { 3832227825Stheraven swap(*__i, *__j); 3833227825Stheraven ++__n_swaps; 3834227825Stheraven ++__i; 3835227825Stheraven break; 3836227825Stheraven } 3837227825Stheraven ++__i; 3838227825Stheraven } 3839227825Stheraven } 3840227825Stheraven // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1 3841227825Stheraven if (__i == __j) 3842227825Stheraven return; 3843227825Stheraven while (true) 3844227825Stheraven { 3845227825Stheraven while (!__comp(*__first, *__i)) 3846227825Stheraven ++__i; 3847227825Stheraven while (__comp(*__first, *--__j)) 3848227825Stheraven ; 3849227825Stheraven if (__i >= __j) 3850227825Stheraven break; 3851227825Stheraven swap(*__i, *__j); 3852227825Stheraven ++__n_swaps; 3853227825Stheraven ++__i; 3854227825Stheraven } 3855227825Stheraven // [__first, __i) == *__first and *__first < [__i, __last) 3856227825Stheraven // The first part is sorted, sort the secod part 3857227825Stheraven // _VSTD::__sort<_Compare>(__i, __last, __comp); 3858227825Stheraven __first = __i; 3859227825Stheraven goto __restart; 3860227825Stheraven } 3861227825Stheraven if (__comp(*__j, *__m)) 3862227825Stheraven { 3863227825Stheraven swap(*__i, *__j); 3864227825Stheraven ++__n_swaps; 3865227825Stheraven break; // found guard for downward moving __j, now use unguarded partition 3866227825Stheraven } 3867227825Stheraven } 3868227825Stheraven } 3869227825Stheraven // It is known that *__i < *__m 3870227825Stheraven ++__i; 3871227825Stheraven // j points beyond range to be tested, *__m is known to be <= *__lm1 3872227825Stheraven // if not yet partitioned... 3873227825Stheraven if (__i < __j) 3874227825Stheraven { 3875227825Stheraven // known that *(__i - 1) < *__m 3876227825Stheraven // known that __i <= __m 3877227825Stheraven while (true) 3878227825Stheraven { 3879227825Stheraven // __m still guards upward moving __i 3880227825Stheraven while (__comp(*__i, *__m)) 3881227825Stheraven ++__i; 3882227825Stheraven // It is now known that a guard exists for downward moving __j 3883227825Stheraven while (!__comp(*--__j, *__m)) 3884227825Stheraven ; 3885227825Stheraven if (__i > __j) 3886227825Stheraven break; 3887227825Stheraven swap(*__i, *__j); 3888227825Stheraven ++__n_swaps; 3889227825Stheraven // It is known that __m != __j 3890227825Stheraven // If __m just moved, follow it 3891227825Stheraven if (__m == __i) 3892227825Stheraven __m = __j; 3893227825Stheraven ++__i; 3894227825Stheraven } 3895227825Stheraven } 3896227825Stheraven // [__first, __i) < *__m and *__m <= [__i, __last) 3897227825Stheraven if (__i != __m && __comp(*__m, *__i)) 3898227825Stheraven { 3899227825Stheraven swap(*__i, *__m); 3900227825Stheraven ++__n_swaps; 3901227825Stheraven } 3902227825Stheraven // [__first, __i) < *__i and *__i <= [__i+1, __last) 3903227825Stheraven // If we were given a perfect partition, see if insertion sort is quick... 3904227825Stheraven if (__n_swaps == 0) 3905227825Stheraven { 3906227825Stheraven bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp); 3907227825Stheraven if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp)) 3908227825Stheraven { 3909227825Stheraven if (__fs) 3910227825Stheraven return; 3911227825Stheraven __last = __i; 3912227825Stheraven continue; 3913227825Stheraven } 3914227825Stheraven else 3915227825Stheraven { 3916227825Stheraven if (__fs) 3917227825Stheraven { 3918227825Stheraven __first = ++__i; 3919227825Stheraven continue; 3920227825Stheraven } 3921227825Stheraven } 3922227825Stheraven } 3923227825Stheraven // sort smaller range with recursive call and larger with tail recursion elimination 3924227825Stheraven if (__i - __first < __last - __i) 3925227825Stheraven { 3926227825Stheraven _VSTD::__sort<_Compare>(__first, __i, __comp); 3927227825Stheraven // _VSTD::__sort<_Compare>(__i+1, __last, __comp); 3928227825Stheraven __first = ++__i; 3929227825Stheraven } 3930227825Stheraven else 3931227825Stheraven { 3932227825Stheraven _VSTD::__sort<_Compare>(__i+1, __last, __comp); 3933227825Stheraven // _VSTD::__sort<_Compare>(__first, __i, __comp); 3934227825Stheraven __last = __i; 3935227825Stheraven } 3936227825Stheraven } 3937227825Stheraven} 3938227825Stheraven 3939227825Stheraven// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare 3940227825Stheraventemplate <class _RandomAccessIterator, class _Compare> 3941227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 3942227825Stheravenvoid 3943227825Stheravensort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 3944227825Stheraven{ 3945227825Stheraven#ifdef _LIBCPP_DEBUG2 3946227825Stheraven typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 3947227825Stheraven __debug_less<_Compare> __c(__comp); 3948227825Stheraven __sort<_Comp_ref>(__first, __last, __c); 3949227825Stheraven#else // _LIBCPP_DEBUG2 3950227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 3951227825Stheraven __sort<_Comp_ref>(__first, __last, __comp); 3952227825Stheraven#endif // _LIBCPP_DEBUG2 3953227825Stheraven} 3954227825Stheraven 3955227825Stheraventemplate <class _RandomAccessIterator> 3956227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 3957227825Stheravenvoid 3958227825Stheravensort(_RandomAccessIterator __first, _RandomAccessIterator __last) 3959227825Stheraven{ 3960227825Stheraven _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 3961227825Stheraven} 3962227825Stheraven 3963227825Stheraventemplate <class _Tp> 3964227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 3965227825Stheravenvoid 3966227825Stheravensort(_Tp** __first, _Tp** __last) 3967227825Stheraven{ 3968227825Stheraven _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>()); 3969227825Stheraven} 3970227825Stheraven 3971227825Stheraventemplate <class _Tp> 3972227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 3973227825Stheravenvoid 3974227825Stheravensort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last) 3975227825Stheraven{ 3976227825Stheraven _VSTD::sort(__first.base(), __last.base()); 3977227825Stheraven} 3978227825Stheraven 3979227825Stheraventemplate <class _Tp, class _Compare> 3980227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 3981227825Stheravenvoid 3982227825Stheravensort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp) 3983227825Stheraven{ 3984227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 3985227825Stheraven _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp); 3986227825Stheraven} 3987227825Stheraven 3988227825Stheraven#ifdef _MSC_VER 3989227825Stheraven#pragma warning( push ) 3990227825Stheraven#pragma warning( disable: 4231) 3991227825Stheraven#endif // _MSC_VER 3992242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<char>&, char*>(char*, char*, __less<char>&)) 3993242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&)) 3994242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&)) 3995242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&)) 3996242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<short>&, short*>(short*, short*, __less<short>&)) 3997242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&)) 3998242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<int>&, int*>(int*, int*, __less<int>&)) 3999242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&)) 4000242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<long>&, long*>(long*, long*, __less<long>&)) 4001242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&)) 4002242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&)) 4003242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&)) 4004242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<float>&, float*>(float*, float*, __less<float>&)) 4005242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<double>&, double*>(double*, double*, __less<double>&)) 4006242945Stheraven_LIBCPP_EXTERN_TEMPLATE(void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&)) 4007227825Stheraven 4008242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&)) 4009242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&)) 4010242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&)) 4011242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&)) 4012242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&)) 4013242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&)) 4014242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&)) 4015242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&)) 4016242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&)) 4017242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&)) 4018242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&)) 4019242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&)) 4020242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&)) 4021242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&)) 4022242945Stheraven_LIBCPP_EXTERN_TEMPLATE(bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&)) 4023227825Stheraven 4024242945Stheraven_LIBCPP_EXTERN_TEMPLATE(unsigned __sort5<__less<long double>&, long double*>(long double*, long double*, long double*, long double*, long double*, __less<long double>&)) 4025227825Stheraven#ifdef _MSC_VER 4026227825Stheraven#pragma warning( pop ) 4027232950Stheraven#endif // _MSC_VER 4028227825Stheraven 4029227825Stheraven// lower_bound 4030227825Stheraven 4031227825Stheraventemplate <class _Compare, class _ForwardIterator, class _Tp> 4032227825Stheraven_ForwardIterator 4033227825Stheraven__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) 4034227825Stheraven{ 4035227825Stheraven typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type; 4036227825Stheraven difference_type __len = _VSTD::distance(__first, __last); 4037227825Stheraven while (__len != 0) 4038227825Stheraven { 4039227825Stheraven difference_type __l2 = __len / 2; 4040227825Stheraven _ForwardIterator __m = __first; 4041227825Stheraven _VSTD::advance(__m, __l2); 4042227825Stheraven if (__comp(*__m, __value_)) 4043227825Stheraven { 4044227825Stheraven __first = ++__m; 4045227825Stheraven __len -= __l2 + 1; 4046227825Stheraven } 4047227825Stheraven else 4048227825Stheraven __len = __l2; 4049227825Stheraven } 4050227825Stheraven return __first; 4051227825Stheraven} 4052227825Stheraven 4053227825Stheraventemplate <class _ForwardIterator, class _Tp, class _Compare> 4054227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4055227825Stheraven_ForwardIterator 4056227825Stheravenlower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) 4057227825Stheraven{ 4058227825Stheraven#ifdef _LIBCPP_DEBUG2 4059227825Stheraven typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 4060227825Stheraven __debug_less<_Compare> __c(__comp); 4061227825Stheraven return __lower_bound<_Comp_ref>(__first, __last, __value_, __c); 4062227825Stheraven#else // _LIBCPP_DEBUG2 4063227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 4064227825Stheraven return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp); 4065227825Stheraven#endif // _LIBCPP_DEBUG2 4066227825Stheraven} 4067227825Stheraven 4068227825Stheraventemplate <class _ForwardIterator, class _Tp> 4069227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4070227825Stheraven_ForwardIterator 4071227825Stheravenlower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_) 4072227825Stheraven{ 4073227825Stheraven return _VSTD::lower_bound(__first, __last, __value_, 4074227825Stheraven __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>()); 4075227825Stheraven} 4076227825Stheraven 4077227825Stheraven// upper_bound 4078227825Stheraven 4079227825Stheraventemplate <class _Compare, class _ForwardIterator, class _Tp> 4080227825Stheraven_ForwardIterator 4081227825Stheraven__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) 4082227825Stheraven{ 4083227825Stheraven typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type; 4084227825Stheraven difference_type __len = _VSTD::distance(__first, __last); 4085227825Stheraven while (__len != 0) 4086227825Stheraven { 4087227825Stheraven difference_type __l2 = __len / 2; 4088227825Stheraven _ForwardIterator __m = __first; 4089227825Stheraven _VSTD::advance(__m, __l2); 4090227825Stheraven if (__comp(__value_, *__m)) 4091227825Stheraven __len = __l2; 4092227825Stheraven else 4093227825Stheraven { 4094227825Stheraven __first = ++__m; 4095227825Stheraven __len -= __l2 + 1; 4096227825Stheraven } 4097227825Stheraven } 4098227825Stheraven return __first; 4099227825Stheraven} 4100227825Stheraven 4101227825Stheraventemplate <class _ForwardIterator, class _Tp, class _Compare> 4102227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4103227825Stheraven_ForwardIterator 4104227825Stheravenupper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) 4105227825Stheraven{ 4106227825Stheraven#ifdef _LIBCPP_DEBUG2 4107227825Stheraven typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 4108227825Stheraven __debug_less<_Compare> __c(__comp); 4109227825Stheraven return __upper_bound<_Comp_ref>(__first, __last, __value_, __c); 4110227825Stheraven#else // _LIBCPP_DEBUG2 4111227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 4112227825Stheraven return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp); 4113227825Stheraven#endif // _LIBCPP_DEBUG2 4114227825Stheraven} 4115227825Stheraven 4116227825Stheraventemplate <class _ForwardIterator, class _Tp> 4117227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4118227825Stheraven_ForwardIterator 4119227825Stheravenupper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_) 4120227825Stheraven{ 4121227825Stheraven return _VSTD::upper_bound(__first, __last, __value_, 4122227825Stheraven __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>()); 4123227825Stheraven} 4124227825Stheraven 4125227825Stheraven// equal_range 4126227825Stheraven 4127227825Stheraventemplate <class _Compare, class _ForwardIterator, class _Tp> 4128227825Stheravenpair<_ForwardIterator, _ForwardIterator> 4129227825Stheraven__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) 4130227825Stheraven{ 4131227825Stheraven typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type; 4132227825Stheraven difference_type __len = _VSTD::distance(__first, __last); 4133227825Stheraven while (__len != 0) 4134227825Stheraven { 4135227825Stheraven difference_type __l2 = __len / 2; 4136227825Stheraven _ForwardIterator __m = __first; 4137227825Stheraven _VSTD::advance(__m, __l2); 4138227825Stheraven if (__comp(*__m, __value_)) 4139227825Stheraven { 4140227825Stheraven __first = ++__m; 4141227825Stheraven __len -= __l2 + 1; 4142227825Stheraven } 4143227825Stheraven else if (__comp(__value_, *__m)) 4144227825Stheraven { 4145227825Stheraven __last = __m; 4146227825Stheraven __len = __l2; 4147227825Stheraven } 4148227825Stheraven else 4149227825Stheraven { 4150227825Stheraven _ForwardIterator __mp1 = __m; 4151227825Stheraven return pair<_ForwardIterator, _ForwardIterator> 4152227825Stheraven ( 4153227825Stheraven __lower_bound<_Compare>(__first, __m, __value_, __comp), 4154227825Stheraven __upper_bound<_Compare>(++__mp1, __last, __value_, __comp) 4155227825Stheraven ); 4156227825Stheraven } 4157227825Stheraven } 4158227825Stheraven return pair<_ForwardIterator, _ForwardIterator>(__first, __first); 4159227825Stheraven} 4160227825Stheraven 4161227825Stheraventemplate <class _ForwardIterator, class _Tp, class _Compare> 4162227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4163227825Stheravenpair<_ForwardIterator, _ForwardIterator> 4164227825Stheravenequal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) 4165227825Stheraven{ 4166227825Stheraven#ifdef _LIBCPP_DEBUG2 4167227825Stheraven typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 4168227825Stheraven __debug_less<_Compare> __c(__comp); 4169227825Stheraven return __equal_range<_Comp_ref>(__first, __last, __value_, __c); 4170227825Stheraven#else // _LIBCPP_DEBUG2 4171227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 4172227825Stheraven return __equal_range<_Comp_ref>(__first, __last, __value_, __comp); 4173227825Stheraven#endif // _LIBCPP_DEBUG2 4174227825Stheraven} 4175227825Stheraven 4176227825Stheraventemplate <class _ForwardIterator, class _Tp> 4177227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4178227825Stheravenpair<_ForwardIterator, _ForwardIterator> 4179227825Stheravenequal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_) 4180227825Stheraven{ 4181227825Stheraven return _VSTD::equal_range(__first, __last, __value_, 4182227825Stheraven __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>()); 4183227825Stheraven} 4184227825Stheraven 4185227825Stheraven// binary_search 4186227825Stheraven 4187227825Stheraventemplate <class _Compare, class _ForwardIterator, class _Tp> 4188227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4189227825Stheravenbool 4190227825Stheraven__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) 4191227825Stheraven{ 4192227825Stheraven __first = __lower_bound<_Compare>(__first, __last, __value_, __comp); 4193227825Stheraven return __first != __last && !__comp(__value_, *__first); 4194227825Stheraven} 4195227825Stheraven 4196227825Stheraventemplate <class _ForwardIterator, class _Tp, class _Compare> 4197227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4198227825Stheravenbool 4199227825Stheravenbinary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) 4200227825Stheraven{ 4201227825Stheraven#ifdef _LIBCPP_DEBUG2 4202227825Stheraven typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 4203227825Stheraven __debug_less<_Compare> __c(__comp); 4204227825Stheraven return __binary_search<_Comp_ref>(__first, __last, __value_, __c); 4205227825Stheraven#else // _LIBCPP_DEBUG2 4206227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 4207227825Stheraven return __binary_search<_Comp_ref>(__first, __last, __value_, __comp); 4208227825Stheraven#endif // _LIBCPP_DEBUG2 4209227825Stheraven} 4210227825Stheraven 4211227825Stheraventemplate <class _ForwardIterator, class _Tp> 4212227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4213227825Stheravenbool 4214227825Stheravenbinary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_) 4215227825Stheraven{ 4216227825Stheraven return _VSTD::binary_search(__first, __last, __value_, 4217227825Stheraven __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>()); 4218227825Stheraven} 4219227825Stheraven 4220227825Stheraven// merge 4221227825Stheraven 4222227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator> 4223227825Stheraven_OutputIterator 4224227825Stheraven__merge(_InputIterator1 __first1, _InputIterator1 __last1, 4225227825Stheraven _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) 4226227825Stheraven{ 4227227825Stheraven for (; __first1 != __last1; ++__result) 4228227825Stheraven { 4229227825Stheraven if (__first2 == __last2) 4230227825Stheraven return _VSTD::copy(__first1, __last1, __result); 4231227825Stheraven if (__comp(*__first2, *__first1)) 4232227825Stheraven { 4233227825Stheraven *__result = *__first2; 4234227825Stheraven ++__first2; 4235227825Stheraven } 4236227825Stheraven else 4237227825Stheraven { 4238227825Stheraven *__result = *__first1; 4239227825Stheraven ++__first1; 4240227825Stheraven } 4241227825Stheraven } 4242227825Stheraven return _VSTD::copy(__first2, __last2, __result); 4243227825Stheraven} 4244227825Stheraven 4245227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare> 4246227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4247227825Stheraven_OutputIterator 4248227825Stheravenmerge(_InputIterator1 __first1, _InputIterator1 __last1, 4249227825Stheraven _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) 4250227825Stheraven{ 4251227825Stheraven#ifdef _LIBCPP_DEBUG2 4252227825Stheraven typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 4253227825Stheraven __debug_less<_Compare> __c(__comp); 4254227825Stheraven return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c); 4255227825Stheraven#else // _LIBCPP_DEBUG2 4256227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 4257227825Stheraven return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp); 4258227825Stheraven#endif // _LIBCPP_DEBUG2 4259227825Stheraven} 4260227825Stheraven 4261227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator> 4262227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4263227825Stheraven_OutputIterator 4264227825Stheravenmerge(_InputIterator1 __first1, _InputIterator1 __last1, 4265227825Stheraven _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result) 4266227825Stheraven{ 4267227825Stheraven typedef typename iterator_traits<_InputIterator1>::value_type __v1; 4268227825Stheraven typedef typename iterator_traits<_InputIterator2>::value_type __v2; 4269227825Stheraven return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>()); 4270227825Stheraven} 4271227825Stheraven 4272227825Stheraven// inplace_merge 4273227825Stheraven 4274227825Stheraventemplate <class _Compare, class _BidirectionalIterator> 4275227825Stheravenvoid 4276227825Stheraven__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last, 4277227825Stheraven _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1, 4278227825Stheraven typename iterator_traits<_BidirectionalIterator>::difference_type __len2, 4279227825Stheraven typename iterator_traits<_BidirectionalIterator>::value_type* __buff) 4280227825Stheraven{ 4281227825Stheraven typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type; 4282227825Stheraven typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type; 4283227825Stheraven typedef typename iterator_traits<_BidirectionalIterator>::pointer pointer; 4284227825Stheraven __destruct_n __d(0); 4285227825Stheraven unique_ptr<value_type, __destruct_n&> __h2(__buff, __d); 4286227825Stheraven if (__len1 <= __len2) 4287227825Stheraven { 4288227825Stheraven value_type* __p = __buff; 4289227825Stheraven for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), ++__i, ++__p) 4290227825Stheraven ::new(__p) value_type(_VSTD::move(*__i)); 4291227825Stheraven __merge<_Compare>(move_iterator<value_type*>(__buff), 4292227825Stheraven move_iterator<value_type*>(__p), 4293227825Stheraven move_iterator<_BidirectionalIterator>(__middle), 4294227825Stheraven move_iterator<_BidirectionalIterator>(__last), 4295227825Stheraven __first, __comp); 4296227825Stheraven } 4297227825Stheraven else 4298227825Stheraven { 4299227825Stheraven value_type* __p = __buff; 4300227825Stheraven for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), ++__i, ++__p) 4301227825Stheraven ::new(__p) value_type(_VSTD::move(*__i)); 4302227825Stheraven typedef reverse_iterator<_BidirectionalIterator> _RBi; 4303227825Stheraven typedef reverse_iterator<value_type*> _Rv; 4304227825Stheraven __merge(move_iterator<_RBi>(_RBi(__middle)), move_iterator<_RBi>(_RBi(__first)), 4305227825Stheraven move_iterator<_Rv>(_Rv(__p)), move_iterator<_Rv>(_Rv(__buff)), 4306227825Stheraven _RBi(__last), __negate<_Compare>(__comp)); 4307227825Stheraven } 4308227825Stheraven} 4309227825Stheraven 4310227825Stheraventemplate <class _Compare, class _BidirectionalIterator> 4311227825Stheravenvoid 4312227825Stheraven__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last, 4313227825Stheraven _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1, 4314227825Stheraven typename iterator_traits<_BidirectionalIterator>::difference_type __len2, 4315227825Stheraven typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size) 4316227825Stheraven{ 4317227825Stheraven typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type; 4318227825Stheraven typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type; 4319227825Stheraven while (true) 4320227825Stheraven { 4321227825Stheraven // if __middle == __last, we're done 4322227825Stheraven if (__len2 == 0) 4323227825Stheraven return; 4324227825Stheraven // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0 4325227825Stheraven for (; true; ++__first, --__len1) 4326227825Stheraven { 4327227825Stheraven if (__len1 == 0) 4328227825Stheraven return; 4329227825Stheraven if (__comp(*__middle, *__first)) 4330227825Stheraven break; 4331227825Stheraven } 4332227825Stheraven if (__len1 <= __buff_size || __len2 <= __buff_size) 4333227825Stheraven { 4334227825Stheraven __buffered_inplace_merge<_Compare>(__first, __middle, __last, __comp, __len1, __len2, __buff); 4335227825Stheraven return; 4336227825Stheraven } 4337227825Stheraven // __first < __middle < __last 4338227825Stheraven // *__first > *__middle 4339227825Stheraven // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that 4340227825Stheraven // all elements in: 4341227825Stheraven // [__first, __m1) <= [__middle, __m2) 4342227825Stheraven // [__middle, __m2) < [__m1, __middle) 4343227825Stheraven // [__m1, __middle) <= [__m2, __last) 4344227825Stheraven // and __m1 or __m2 is in the middle of its range 4345227825Stheraven _BidirectionalIterator __m1; // "median" of [__first, __middle) 4346227825Stheraven _BidirectionalIterator __m2; // "median" of [__middle, __last) 4347227825Stheraven difference_type __len11; // distance(__first, __m1) 4348227825Stheraven difference_type __len21; // distance(__middle, __m2) 4349227825Stheraven // binary search smaller range 4350227825Stheraven if (__len1 < __len2) 4351227825Stheraven { // __len >= 1, __len2 >= 2 4352227825Stheraven __len21 = __len2 / 2; 4353227825Stheraven __m2 = __middle; 4354227825Stheraven _VSTD::advance(__m2, __len21); 4355227825Stheraven __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp); 4356227825Stheraven __len11 = _VSTD::distance(__first, __m1); 4357227825Stheraven } 4358227825Stheraven else 4359227825Stheraven { 4360227825Stheraven if (__len1 == 1) 4361227825Stheraven { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1 4362227825Stheraven // It is known *__first > *__middle 4363227825Stheraven swap(*__first, *__middle); 4364227825Stheraven return; 4365227825Stheraven } 4366227825Stheraven // __len1 >= 2, __len2 >= 1 4367227825Stheraven __len11 = __len1 / 2; 4368227825Stheraven __m1 = __first; 4369227825Stheraven _VSTD::advance(__m1, __len11); 4370227825Stheraven __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp); 4371227825Stheraven __len21 = _VSTD::distance(__middle, __m2); 4372227825Stheraven } 4373227825Stheraven difference_type __len12 = __len1 - __len11; // distance(__m1, __middle) 4374227825Stheraven difference_type __len22 = __len2 - __len21; // distance(__m2, __last) 4375227825Stheraven // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) 4376227825Stheraven // swap middle two partitions 4377227825Stheraven __middle = _VSTD::rotate(__m1, __middle, __m2); 4378227825Stheraven // __len12 and __len21 now have swapped meanings 4379227825Stheraven // merge smaller range with recurisve call and larger with tail recursion elimination 4380227825Stheraven if (__len11 + __len21 < __len12 + __len22) 4381227825Stheraven { 4382227825Stheraven __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size); 4383227825Stheraven// __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size); 4384227825Stheraven __first = __middle; 4385227825Stheraven __middle = __m2; 4386227825Stheraven __len1 = __len12; 4387227825Stheraven __len2 = __len22; 4388227825Stheraven } 4389227825Stheraven else 4390227825Stheraven { 4391227825Stheraven __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size); 4392227825Stheraven// __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size); 4393227825Stheraven __last = __middle; 4394227825Stheraven __middle = __m1; 4395227825Stheraven __len1 = __len11; 4396227825Stheraven __len2 = __len21; 4397227825Stheraven } 4398227825Stheraven } 4399227825Stheraven} 4400227825Stheraven 4401227825Stheraventemplate <class _Tp> 4402227825Stheravenstruct __inplace_merge_switch 4403227825Stheraven{ 4404227825Stheraven static const unsigned value = is_trivially_copy_assignable<_Tp>::value; 4405227825Stheraven}; 4406227825Stheraven 4407227825Stheraventemplate <class _BidirectionalIterator, class _Compare> 4408227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4409227825Stheravenvoid 4410227825Stheraveninplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last, 4411227825Stheraven _Compare __comp) 4412227825Stheraven{ 4413227825Stheraven typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type; 4414227825Stheraven typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type; 4415227825Stheraven difference_type __len1 = _VSTD::distance(__first, __middle); 4416227825Stheraven difference_type __len2 = _VSTD::distance(__middle, __last); 4417227825Stheraven difference_type __buf_size = _VSTD::min(__len1, __len2); 4418227825Stheraven pair<value_type*, ptrdiff_t> __buf(0, 0); 4419227825Stheraven unique_ptr<value_type, __return_temporary_buffer> __h; 4420227825Stheraven if (__inplace_merge_switch<value_type>::value && __buf_size > 8) 4421227825Stheraven { 4422227825Stheraven __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size); 4423227825Stheraven __h.reset(__buf.first); 4424227825Stheraven } 4425227825Stheraven#ifdef _LIBCPP_DEBUG2 4426227825Stheraven typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 4427227825Stheraven __debug_less<_Compare> __c(__comp); 4428227825Stheraven return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2, 4429227825Stheraven __buf.first, __buf.second); 4430227825Stheraven#else // _LIBCPP_DEBUG2 4431227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 4432227825Stheraven return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2, 4433227825Stheraven __buf.first, __buf.second); 4434227825Stheraven#endif // _LIBCPP_DEBUG2 4435227825Stheraven} 4436227825Stheraven 4437227825Stheraventemplate <class _BidirectionalIterator> 4438227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4439227825Stheravenvoid 4440227825Stheraveninplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last) 4441227825Stheraven{ 4442227825Stheraven _VSTD::inplace_merge(__first, __middle, __last, 4443227825Stheraven __less<typename iterator_traits<_BidirectionalIterator>::value_type>()); 4444227825Stheraven} 4445227825Stheraven 4446227825Stheraven// stable_sort 4447227825Stheraven 4448227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2> 4449227825Stheravenvoid 4450227825Stheraven__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1, 4451227825Stheraven _InputIterator2 __first2, _InputIterator2 __last2, 4452227825Stheraven typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp) 4453227825Stheraven{ 4454227825Stheraven typedef typename iterator_traits<_InputIterator1>::value_type value_type; 4455227825Stheraven __destruct_n __d(0); 4456227825Stheraven unique_ptr<value_type, __destruct_n&> __h(__result, __d); 4457227825Stheraven for (; true; ++__result) 4458227825Stheraven { 4459227825Stheraven if (__first1 == __last1) 4460227825Stheraven { 4461227825Stheraven for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0)) 4462227825Stheraven ::new (__result) value_type(_VSTD::move(*__first2)); 4463227825Stheraven __h.release(); 4464227825Stheraven return; 4465227825Stheraven } 4466227825Stheraven if (__first2 == __last2) 4467227825Stheraven { 4468227825Stheraven for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0)) 4469227825Stheraven ::new (__result) value_type(_VSTD::move(*__first1)); 4470227825Stheraven __h.release(); 4471227825Stheraven return; 4472227825Stheraven } 4473227825Stheraven if (__comp(*__first2, *__first1)) 4474227825Stheraven { 4475227825Stheraven ::new (__result) value_type(_VSTD::move(*__first2)); 4476227825Stheraven __d.__incr((value_type*)0); 4477227825Stheraven ++__first2; 4478227825Stheraven } 4479227825Stheraven else 4480227825Stheraven { 4481227825Stheraven ::new (__result) value_type(_VSTD::move(*__first1)); 4482227825Stheraven __d.__incr((value_type*)0); 4483227825Stheraven ++__first1; 4484227825Stheraven } 4485227825Stheraven } 4486227825Stheraven} 4487227825Stheraven 4488227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator> 4489227825Stheravenvoid 4490227825Stheraven__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1, 4491227825Stheraven _InputIterator2 __first2, _InputIterator2 __last2, 4492227825Stheraven _OutputIterator __result, _Compare __comp) 4493227825Stheraven{ 4494227825Stheraven for (; __first1 != __last1; ++__result) 4495227825Stheraven { 4496227825Stheraven if (__first2 == __last2) 4497227825Stheraven { 4498227825Stheraven for (; __first1 != __last1; ++__first1, ++__result) 4499227825Stheraven *__result = _VSTD::move(*__first1); 4500227825Stheraven return; 4501227825Stheraven } 4502227825Stheraven if (__comp(*__first2, *__first1)) 4503227825Stheraven { 4504227825Stheraven *__result = _VSTD::move(*__first2); 4505227825Stheraven ++__first2; 4506227825Stheraven } 4507227825Stheraven else 4508227825Stheraven { 4509227825Stheraven *__result = _VSTD::move(*__first1); 4510227825Stheraven ++__first1; 4511227825Stheraven } 4512227825Stheraven } 4513227825Stheraven for (; __first2 != __last2; ++__first2, ++__result) 4514227825Stheraven *__result = _VSTD::move(*__first2); 4515227825Stheraven} 4516227825Stheraven 4517227825Stheraventemplate <class _Compare, class _RandomAccessIterator> 4518227825Stheravenvoid 4519227825Stheraven__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp, 4520227825Stheraven typename iterator_traits<_RandomAccessIterator>::difference_type __len, 4521227825Stheraven typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size); 4522227825Stheraven 4523227825Stheraventemplate <class _Compare, class _RandomAccessIterator> 4524227825Stheravenvoid 4525227825Stheraven__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp, 4526227825Stheraven typename iterator_traits<_RandomAccessIterator>::difference_type __len, 4527227825Stheraven typename iterator_traits<_RandomAccessIterator>::value_type* __first2) 4528227825Stheraven{ 4529227825Stheraven typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type; 4530227825Stheraven switch (__len) 4531227825Stheraven { 4532227825Stheraven case 0: 4533227825Stheraven return; 4534227825Stheraven case 1: 4535227825Stheraven ::new(__first2) value_type(_VSTD::move(*__first1)); 4536227825Stheraven return; 4537227825Stheraven case 2: 4538227825Stheraven __destruct_n __d(0); 4539227825Stheraven unique_ptr<value_type, __destruct_n&> __h2(__first2, __d); 4540227825Stheraven if (__comp(*--__last1, *__first1)) 4541227825Stheraven { 4542227825Stheraven ::new(__first2) value_type(_VSTD::move(*__last1)); 4543227825Stheraven __d.__incr((value_type*)0); 4544227825Stheraven ++__first2; 4545227825Stheraven ::new(__first2) value_type(_VSTD::move(*__first1)); 4546227825Stheraven } 4547227825Stheraven else 4548227825Stheraven { 4549227825Stheraven ::new(__first2) value_type(_VSTD::move(*__first1)); 4550227825Stheraven __d.__incr((value_type*)0); 4551227825Stheraven ++__first2; 4552227825Stheraven ::new(__first2) value_type(_VSTD::move(*__last1)); 4553227825Stheraven } 4554227825Stheraven __h2.release(); 4555227825Stheraven return; 4556227825Stheraven } 4557227825Stheraven if (__len <= 8) 4558227825Stheraven { 4559227825Stheraven __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp); 4560227825Stheraven return; 4561227825Stheraven } 4562227825Stheraven typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2; 4563227825Stheraven _RandomAccessIterator __m = __first1 + __l2; 4564227825Stheraven __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2); 4565227825Stheraven __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2); 4566227825Stheraven __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp); 4567227825Stheraven} 4568227825Stheraven 4569227825Stheraventemplate <class _Tp> 4570227825Stheravenstruct __stable_sort_switch 4571227825Stheraven{ 4572227825Stheraven static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value; 4573227825Stheraven}; 4574227825Stheraven 4575227825Stheraventemplate <class _Compare, class _RandomAccessIterator> 4576227825Stheravenvoid 4577227825Stheraven__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp, 4578227825Stheraven typename iterator_traits<_RandomAccessIterator>::difference_type __len, 4579227825Stheraven typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size) 4580227825Stheraven{ 4581227825Stheraven typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type; 4582227825Stheraven typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; 4583227825Stheraven switch (__len) 4584227825Stheraven { 4585227825Stheraven case 0: 4586227825Stheraven case 1: 4587227825Stheraven return; 4588227825Stheraven case 2: 4589227825Stheraven if (__comp(*--__last, *__first)) 4590227825Stheraven swap(*__first, *__last); 4591227825Stheraven return; 4592227825Stheraven } 4593227825Stheraven if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value)) 4594227825Stheraven { 4595227825Stheraven __insertion_sort<_Compare>(__first, __last, __comp); 4596227825Stheraven return; 4597227825Stheraven } 4598227825Stheraven typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2; 4599227825Stheraven _RandomAccessIterator __m = __first + __l2; 4600227825Stheraven if (__len <= __buff_size) 4601227825Stheraven { 4602227825Stheraven __destruct_n __d(0); 4603227825Stheraven unique_ptr<value_type, __destruct_n&> __h2(__buff, __d); 4604227825Stheraven __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff); 4605227825Stheraven __d.__set(__l2, (value_type*)0); 4606227825Stheraven __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2); 4607227825Stheraven __d.__set(__len, (value_type*)0); 4608227825Stheraven __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp); 4609227825Stheraven// __merge<_Compare>(move_iterator<value_type*>(__buff), 4610227825Stheraven// move_iterator<value_type*>(__buff + __l2), 4611227825Stheraven// move_iterator<_RandomAccessIterator>(__buff + __l2), 4612227825Stheraven// move_iterator<_RandomAccessIterator>(__buff + __len), 4613227825Stheraven// __first, __comp); 4614227825Stheraven return; 4615227825Stheraven } 4616227825Stheraven __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size); 4617227825Stheraven __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size); 4618227825Stheraven __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size); 4619227825Stheraven} 4620227825Stheraven 4621227825Stheraventemplate <class _RandomAccessIterator, class _Compare> 4622227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4623227825Stheravenvoid 4624227825Stheravenstable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 4625227825Stheraven{ 4626227825Stheraven typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type; 4627227825Stheraven typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; 4628227825Stheraven difference_type __len = __last - __first; 4629227825Stheraven pair<value_type*, ptrdiff_t> __buf(0, 0); 4630227825Stheraven unique_ptr<value_type, __return_temporary_buffer> __h; 4631227825Stheraven if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value)) 4632227825Stheraven { 4633227825Stheraven __buf = _VSTD::get_temporary_buffer<value_type>(__len); 4634227825Stheraven __h.reset(__buf.first); 4635227825Stheraven } 4636227825Stheraven#ifdef _LIBCPP_DEBUG2 4637227825Stheraven typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 4638227825Stheraven __debug_less<_Compare> __c(__comp); 4639227825Stheraven __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second); 4640227825Stheraven#else // _LIBCPP_DEBUG2 4641227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 4642227825Stheraven __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second); 4643227825Stheraven#endif // _LIBCPP_DEBUG2 4644227825Stheraven} 4645227825Stheraven 4646227825Stheraventemplate <class _RandomAccessIterator> 4647227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4648227825Stheravenvoid 4649227825Stheravenstable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last) 4650227825Stheraven{ 4651227825Stheraven _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 4652227825Stheraven} 4653227825Stheraven 4654227825Stheraven// is_heap_until 4655227825Stheraven 4656227825Stheraventemplate <class _RandomAccessIterator, class _Compare> 4657227825Stheraven_RandomAccessIterator 4658227825Stheravenis_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 4659227825Stheraven{ 4660227825Stheraven typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type; 4661227825Stheraven difference_type __len = __last - __first; 4662227825Stheraven difference_type __p = 0; 4663227825Stheraven difference_type __c = 1; 4664227825Stheraven _RandomAccessIterator __pp = __first; 4665227825Stheraven while (__c < __len) 4666227825Stheraven { 4667227825Stheraven _RandomAccessIterator __cp = __first + __c; 4668227825Stheraven if (__comp(*__pp, *__cp)) 4669227825Stheraven return __cp; 4670227825Stheraven ++__c; 4671227825Stheraven ++__cp; 4672227825Stheraven if (__c == __len) 4673227825Stheraven return __last; 4674227825Stheraven if (__comp(*__pp, *__cp)) 4675227825Stheraven return __cp; 4676227825Stheraven ++__p; 4677227825Stheraven ++__pp; 4678227825Stheraven __c = 2 * __p + 1; 4679227825Stheraven } 4680227825Stheraven return __last; 4681227825Stheraven} 4682227825Stheraven 4683227825Stheraventemplate<class _RandomAccessIterator> 4684227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4685227825Stheraven_RandomAccessIterator 4686227825Stheravenis_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last) 4687227825Stheraven{ 4688227825Stheraven return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 4689227825Stheraven} 4690227825Stheraven 4691227825Stheraven// is_heap 4692227825Stheraven 4693227825Stheraventemplate <class _RandomAccessIterator, class _Compare> 4694227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4695227825Stheravenbool 4696227825Stheravenis_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 4697227825Stheraven{ 4698227825Stheraven return _VSTD::is_heap_until(__first, __last, __comp) == __last; 4699227825Stheraven} 4700227825Stheraven 4701227825Stheraventemplate<class _RandomAccessIterator> 4702227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4703227825Stheravenbool 4704227825Stheravenis_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) 4705227825Stheraven{ 4706227825Stheraven return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 4707227825Stheraven} 4708227825Stheraven 4709227825Stheraven// push_heap 4710227825Stheraven 4711227825Stheraventemplate <class _Compare, class _RandomAccessIterator> 4712227825Stheravenvoid 4713227825Stheraven__push_heap_front(_RandomAccessIterator __first, _RandomAccessIterator, _Compare __comp, 4714227825Stheraven typename iterator_traits<_RandomAccessIterator>::difference_type __len) 4715227825Stheraven{ 4716227825Stheraven typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; 4717227825Stheraven typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type; 4718227825Stheraven if (__len > 1) 4719227825Stheraven { 4720227825Stheraven difference_type __p = 0; 4721227825Stheraven _RandomAccessIterator __pp = __first; 4722227825Stheraven difference_type __c = 2; 4723227825Stheraven _RandomAccessIterator __cp = __first + __c; 4724227825Stheraven if (__c == __len || __comp(*__cp, *(__cp - 1))) 4725227825Stheraven { 4726227825Stheraven --__c; 4727227825Stheraven --__cp; 4728227825Stheraven } 4729227825Stheraven if (__comp(*__pp, *__cp)) 4730227825Stheraven { 4731227825Stheraven value_type __t(_VSTD::move(*__pp)); 4732227825Stheraven do 4733227825Stheraven { 4734227825Stheraven *__pp = _VSTD::move(*__cp); 4735227825Stheraven __pp = __cp; 4736227825Stheraven __p = __c; 4737227825Stheraven __c = (__p + 1) * 2; 4738227825Stheraven if (__c > __len) 4739227825Stheraven break; 4740227825Stheraven __cp = __first + __c; 4741227825Stheraven if (__c == __len || __comp(*__cp, *(__cp - 1))) 4742227825Stheraven { 4743227825Stheraven --__c; 4744227825Stheraven --__cp; 4745227825Stheraven } 4746227825Stheraven } while (__comp(__t, *__cp)); 4747227825Stheraven *__pp = _VSTD::move(__t); 4748227825Stheraven } 4749227825Stheraven } 4750227825Stheraven} 4751227825Stheraven 4752227825Stheraventemplate <class _Compare, class _RandomAccessIterator> 4753227825Stheravenvoid 4754227825Stheraven__push_heap_back(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp, 4755227825Stheraven typename iterator_traits<_RandomAccessIterator>::difference_type __len) 4756227825Stheraven{ 4757227825Stheraven typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; 4758227825Stheraven typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type; 4759227825Stheraven if (__len > 1) 4760227825Stheraven { 4761227825Stheraven __len = (__len - 2) / 2; 4762227825Stheraven _RandomAccessIterator __ptr = __first + __len; 4763227825Stheraven if (__comp(*__ptr, *--__last)) 4764227825Stheraven { 4765227825Stheraven value_type __t(_VSTD::move(*__last)); 4766227825Stheraven do 4767227825Stheraven { 4768227825Stheraven *__last = _VSTD::move(*__ptr); 4769227825Stheraven __last = __ptr; 4770227825Stheraven if (__len == 0) 4771227825Stheraven break; 4772227825Stheraven __len = (__len - 1) / 2; 4773227825Stheraven __ptr = __first + __len; 4774227825Stheraven } while (__comp(*__ptr, __t)); 4775227825Stheraven *__last = _VSTD::move(__t); 4776227825Stheraven } 4777227825Stheraven } 4778227825Stheraven} 4779227825Stheraven 4780227825Stheraventemplate <class _RandomAccessIterator, class _Compare> 4781227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4782227825Stheravenvoid 4783227825Stheravenpush_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 4784227825Stheraven{ 4785227825Stheraven#ifdef _LIBCPP_DEBUG2 4786227825Stheraven typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 4787227825Stheraven __debug_less<_Compare> __c(__comp); 4788227825Stheraven __push_heap_back<_Comp_ref>(__first, __last, __c, __last - __first); 4789227825Stheraven#else // _LIBCPP_DEBUG2 4790227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 4791227825Stheraven __push_heap_back<_Comp_ref>(__first, __last, __comp, __last - __first); 4792227825Stheraven#endif // _LIBCPP_DEBUG2 4793227825Stheraven} 4794227825Stheraven 4795227825Stheraventemplate <class _RandomAccessIterator> 4796227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4797227825Stheravenvoid 4798227825Stheravenpush_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) 4799227825Stheraven{ 4800227825Stheraven _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 4801227825Stheraven} 4802227825Stheraven 4803227825Stheraven// pop_heap 4804227825Stheraven 4805227825Stheraventemplate <class _Compare, class _RandomAccessIterator> 4806227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4807227825Stheravenvoid 4808227825Stheraven__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp, 4809227825Stheraven typename iterator_traits<_RandomAccessIterator>::difference_type __len) 4810227825Stheraven{ 4811227825Stheraven if (__len > 1) 4812227825Stheraven { 4813227825Stheraven swap(*__first, *--__last); 4814227825Stheraven __push_heap_front<_Compare>(__first, __last, __comp, __len-1); 4815227825Stheraven } 4816227825Stheraven} 4817227825Stheraven 4818227825Stheraventemplate <class _RandomAccessIterator, class _Compare> 4819227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4820227825Stheravenvoid 4821227825Stheravenpop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 4822227825Stheraven{ 4823227825Stheraven#ifdef _LIBCPP_DEBUG2 4824227825Stheraven typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 4825227825Stheraven __debug_less<_Compare> __c(__comp); 4826227825Stheraven __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first); 4827227825Stheraven#else // _LIBCPP_DEBUG2 4828227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 4829227825Stheraven __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first); 4830227825Stheraven#endif // _LIBCPP_DEBUG2 4831227825Stheraven} 4832227825Stheraven 4833227825Stheraventemplate <class _RandomAccessIterator> 4834227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4835227825Stheravenvoid 4836227825Stheravenpop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) 4837227825Stheraven{ 4838227825Stheraven _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 4839227825Stheraven} 4840227825Stheraven 4841227825Stheraven// make_heap 4842227825Stheraven 4843227825Stheraventemplate <class _Compare, class _RandomAccessIterator> 4844227825Stheravenvoid 4845227825Stheraven__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 4846227825Stheraven{ 4847227825Stheraven typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; 4848227825Stheraven difference_type __n = __last - __first; 4849227825Stheraven if (__n > 1) 4850227825Stheraven { 4851227825Stheraven __last = __first; 4852227825Stheraven ++__last; 4853227825Stheraven for (difference_type __i = 1; __i < __n;) 4854227825Stheraven __push_heap_back<_Compare>(__first, ++__last, __comp, ++__i); 4855227825Stheraven } 4856227825Stheraven} 4857227825Stheraven 4858227825Stheraventemplate <class _RandomAccessIterator, class _Compare> 4859227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4860227825Stheravenvoid 4861227825Stheravenmake_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 4862227825Stheraven{ 4863227825Stheraven#ifdef _LIBCPP_DEBUG2 4864227825Stheraven typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 4865227825Stheraven __debug_less<_Compare> __c(__comp); 4866227825Stheraven __make_heap<_Comp_ref>(__first, __last, __c); 4867227825Stheraven#else // _LIBCPP_DEBUG2 4868227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 4869227825Stheraven __make_heap<_Comp_ref>(__first, __last, __comp); 4870227825Stheraven#endif // _LIBCPP_DEBUG2 4871227825Stheraven} 4872227825Stheraven 4873227825Stheraventemplate <class _RandomAccessIterator> 4874227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4875227825Stheravenvoid 4876227825Stheravenmake_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) 4877227825Stheraven{ 4878227825Stheraven _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 4879227825Stheraven} 4880227825Stheraven 4881227825Stheraven// sort_heap 4882227825Stheraven 4883227825Stheraventemplate <class _Compare, class _RandomAccessIterator> 4884227825Stheravenvoid 4885227825Stheraven__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 4886227825Stheraven{ 4887227825Stheraven typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; 4888227825Stheraven for (difference_type __n = __last - __first; __n > 1; --__last, --__n) 4889227825Stheraven __pop_heap<_Compare>(__first, __last, __comp, __n); 4890227825Stheraven} 4891227825Stheraven 4892227825Stheraventemplate <class _RandomAccessIterator, class _Compare> 4893227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4894227825Stheravenvoid 4895227825Stheravensort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 4896227825Stheraven{ 4897227825Stheraven#ifdef _LIBCPP_DEBUG2 4898227825Stheraven typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 4899227825Stheraven __debug_less<_Compare> __c(__comp); 4900227825Stheraven __sort_heap<_Comp_ref>(__first, __last, __c); 4901227825Stheraven#else // _LIBCPP_DEBUG2 4902227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 4903227825Stheraven __sort_heap<_Comp_ref>(__first, __last, __comp); 4904227825Stheraven#endif // _LIBCPP_DEBUG2 4905227825Stheraven} 4906227825Stheraven 4907227825Stheraventemplate <class _RandomAccessIterator> 4908227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4909227825Stheravenvoid 4910227825Stheravensort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) 4911227825Stheraven{ 4912227825Stheraven _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 4913227825Stheraven} 4914227825Stheraven 4915227825Stheraven// partial_sort 4916227825Stheraven 4917227825Stheraventemplate <class _Compare, class _RandomAccessIterator> 4918227825Stheravenvoid 4919227825Stheraven__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last, 4920227825Stheraven _Compare __comp) 4921227825Stheraven{ 4922227825Stheraven __make_heap<_Compare>(__first, __middle, __comp); 4923227825Stheraven typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first; 4924227825Stheraven for (_RandomAccessIterator __i = __middle; __i != __last; ++__i) 4925227825Stheraven { 4926227825Stheraven if (__comp(*__i, *__first)) 4927227825Stheraven { 4928227825Stheraven swap(*__i, *__first); 4929227825Stheraven __push_heap_front<_Compare>(__first, __middle, __comp, __len); 4930227825Stheraven } 4931227825Stheraven } 4932227825Stheraven __sort_heap<_Compare>(__first, __middle, __comp); 4933227825Stheraven} 4934227825Stheraven 4935227825Stheraventemplate <class _RandomAccessIterator, class _Compare> 4936227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4937227825Stheravenvoid 4938227825Stheravenpartial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last, 4939227825Stheraven _Compare __comp) 4940227825Stheraven{ 4941227825Stheraven#ifdef _LIBCPP_DEBUG2 4942227825Stheraven typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 4943227825Stheraven __debug_less<_Compare> __c(__comp); 4944227825Stheraven __partial_sort<_Comp_ref>(__first, __middle, __last, __c); 4945227825Stheraven#else // _LIBCPP_DEBUG2 4946227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 4947227825Stheraven __partial_sort<_Comp_ref>(__first, __middle, __last, __comp); 4948227825Stheraven#endif // _LIBCPP_DEBUG2 4949227825Stheraven} 4950227825Stheraven 4951227825Stheraventemplate <class _RandomAccessIterator> 4952227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4953227825Stheravenvoid 4954227825Stheravenpartial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last) 4955227825Stheraven{ 4956227825Stheraven _VSTD::partial_sort(__first, __middle, __last, 4957227825Stheraven __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 4958227825Stheraven} 4959227825Stheraven 4960227825Stheraven// partial_sort_copy 4961227825Stheraven 4962227825Stheraventemplate <class _Compare, class _InputIterator, class _RandomAccessIterator> 4963227825Stheraven_RandomAccessIterator 4964227825Stheraven__partial_sort_copy(_InputIterator __first, _InputIterator __last, 4965227825Stheraven _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp) 4966227825Stheraven{ 4967227825Stheraven _RandomAccessIterator __r = __result_first; 4968227825Stheraven if (__r != __result_last) 4969227825Stheraven { 4970227825Stheraven typename iterator_traits<_RandomAccessIterator>::difference_type __len = 0; 4971227825Stheraven for (; __first != __last && __r != __result_last; ++__first, ++__r, ++__len) 4972227825Stheraven *__r = *__first; 4973227825Stheraven __make_heap<_Compare>(__result_first, __r, __comp); 4974227825Stheraven for (; __first != __last; ++__first) 4975227825Stheraven if (__comp(*__first, *__result_first)) 4976227825Stheraven { 4977227825Stheraven *__result_first = *__first; 4978227825Stheraven __push_heap_front<_Compare>(__result_first, __r, __comp, __len); 4979227825Stheraven } 4980227825Stheraven __sort_heap<_Compare>(__result_first, __r, __comp); 4981227825Stheraven } 4982227825Stheraven return __r; 4983227825Stheraven} 4984227825Stheraven 4985227825Stheraventemplate <class _InputIterator, class _RandomAccessIterator, class _Compare> 4986227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 4987227825Stheraven_RandomAccessIterator 4988227825Stheravenpartial_sort_copy(_InputIterator __first, _InputIterator __last, 4989227825Stheraven _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp) 4990227825Stheraven{ 4991227825Stheraven#ifdef _LIBCPP_DEBUG2 4992227825Stheraven typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 4993227825Stheraven __debug_less<_Compare> __c(__comp); 4994227825Stheraven return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c); 4995227825Stheraven#else // _LIBCPP_DEBUG2 4996227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 4997227825Stheraven return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp); 4998227825Stheraven#endif // _LIBCPP_DEBUG2 4999227825Stheraven} 5000227825Stheraven 5001227825Stheraventemplate <class _InputIterator, class _RandomAccessIterator> 5002227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 5003227825Stheraven_RandomAccessIterator 5004227825Stheravenpartial_sort_copy(_InputIterator __first, _InputIterator __last, 5005227825Stheraven _RandomAccessIterator __result_first, _RandomAccessIterator __result_last) 5006227825Stheraven{ 5007227825Stheraven return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last, 5008227825Stheraven __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 5009227825Stheraven} 5010227825Stheraven 5011227825Stheraven// nth_element 5012227825Stheraven 5013227825Stheraventemplate <class _Compare, class _RandomAccessIterator> 5014227825Stheravenvoid 5015227825Stheraven__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp) 5016227825Stheraven{ 5017227825Stheraven // _Compare is known to be a reference type 5018227825Stheraven typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; 5019227825Stheraven const difference_type __limit = 7; 5020227825Stheraven while (true) 5021227825Stheraven { 5022227825Stheraven __restart: 5023232950Stheraven if (__nth == __last) 5024232950Stheraven return; 5025227825Stheraven difference_type __len = __last - __first; 5026227825Stheraven switch (__len) 5027227825Stheraven { 5028227825Stheraven case 0: 5029227825Stheraven case 1: 5030227825Stheraven return; 5031227825Stheraven case 2: 5032227825Stheraven if (__comp(*--__last, *__first)) 5033227825Stheraven swap(*__first, *__last); 5034227825Stheraven return; 5035227825Stheraven case 3: 5036227825Stheraven { 5037227825Stheraven _RandomAccessIterator __m = __first; 5038227825Stheraven _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp); 5039227825Stheraven return; 5040227825Stheraven } 5041227825Stheraven } 5042227825Stheraven if (__len <= __limit) 5043227825Stheraven { 5044227825Stheraven __selection_sort<_Compare>(__first, __last, __comp); 5045227825Stheraven return; 5046227825Stheraven } 5047227825Stheraven // __len > __limit >= 3 5048227825Stheraven _RandomAccessIterator __m = __first + __len/2; 5049227825Stheraven _RandomAccessIterator __lm1 = __last; 5050227825Stheraven unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp); 5051227825Stheraven // *__m is median 5052227825Stheraven // partition [__first, __m) < *__m and *__m <= [__m, __last) 5053227825Stheraven // (this inhibits tossing elements equivalent to __m around unnecessarily) 5054227825Stheraven _RandomAccessIterator __i = __first; 5055227825Stheraven _RandomAccessIterator __j = __lm1; 5056227825Stheraven // j points beyond range to be tested, *__lm1 is known to be <= *__m 5057227825Stheraven // The search going up is known to be guarded but the search coming down isn't. 5058227825Stheraven // Prime the downward search with a guard. 5059227825Stheraven if (!__comp(*__i, *__m)) // if *__first == *__m 5060227825Stheraven { 5061227825Stheraven // *__first == *__m, *__first doesn't go in first part 5062227825Stheraven // manually guard downward moving __j against __i 5063227825Stheraven while (true) 5064227825Stheraven { 5065227825Stheraven if (__i == --__j) 5066227825Stheraven { 5067227825Stheraven // *__first == *__m, *__m <= all other elements 5068227825Stheraven // Parition instead into [__first, __i) == *__first and *__first < [__i, __last) 5069227825Stheraven ++__i; // __first + 1 5070227825Stheraven __j = __last; 5071227825Stheraven if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1) 5072227825Stheraven { 5073227825Stheraven while (true) 5074227825Stheraven { 5075227825Stheraven if (__i == __j) 5076227825Stheraven return; // [__first, __last) all equivalent elements 5077227825Stheraven if (__comp(*__first, *__i)) 5078227825Stheraven { 5079227825Stheraven swap(*__i, *__j); 5080227825Stheraven ++__n_swaps; 5081227825Stheraven ++__i; 5082227825Stheraven break; 5083227825Stheraven } 5084227825Stheraven ++__i; 5085227825Stheraven } 5086227825Stheraven } 5087227825Stheraven // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1 5088227825Stheraven if (__i == __j) 5089227825Stheraven return; 5090227825Stheraven while (true) 5091227825Stheraven { 5092227825Stheraven while (!__comp(*__first, *__i)) 5093227825Stheraven ++__i; 5094227825Stheraven while (__comp(*__first, *--__j)) 5095227825Stheraven ; 5096227825Stheraven if (__i >= __j) 5097227825Stheraven break; 5098227825Stheraven swap(*__i, *__j); 5099227825Stheraven ++__n_swaps; 5100227825Stheraven ++__i; 5101227825Stheraven } 5102227825Stheraven // [__first, __i) == *__first and *__first < [__i, __last) 5103227825Stheraven // The first part is sorted, 5104227825Stheraven if (__nth < __i) 5105227825Stheraven return; 5106227825Stheraven // __nth_element the secod part 5107227825Stheraven // __nth_element<_Compare>(__i, __nth, __last, __comp); 5108227825Stheraven __first = __i; 5109227825Stheraven goto __restart; 5110227825Stheraven } 5111227825Stheraven if (__comp(*__j, *__m)) 5112227825Stheraven { 5113227825Stheraven swap(*__i, *__j); 5114227825Stheraven ++__n_swaps; 5115227825Stheraven break; // found guard for downward moving __j, now use unguarded partition 5116227825Stheraven } 5117227825Stheraven } 5118227825Stheraven } 5119227825Stheraven ++__i; 5120227825Stheraven // j points beyond range to be tested, *__lm1 is known to be <= *__m 5121227825Stheraven // if not yet partitioned... 5122227825Stheraven if (__i < __j) 5123227825Stheraven { 5124227825Stheraven // known that *(__i - 1) < *__m 5125227825Stheraven while (true) 5126227825Stheraven { 5127227825Stheraven // __m still guards upward moving __i 5128227825Stheraven while (__comp(*__i, *__m)) 5129227825Stheraven ++__i; 5130227825Stheraven // It is now known that a guard exists for downward moving __j 5131227825Stheraven while (!__comp(*--__j, *__m)) 5132227825Stheraven ; 5133227825Stheraven if (__i >= __j) 5134227825Stheraven break; 5135227825Stheraven swap(*__i, *__j); 5136227825Stheraven ++__n_swaps; 5137227825Stheraven // It is known that __m != __j 5138227825Stheraven // If __m just moved, follow it 5139227825Stheraven if (__m == __i) 5140227825Stheraven __m = __j; 5141227825Stheraven ++__i; 5142227825Stheraven } 5143227825Stheraven } 5144227825Stheraven // [__first, __i) < *__m and *__m <= [__i, __last) 5145227825Stheraven if (__i != __m && __comp(*__m, *__i)) 5146227825Stheraven { 5147227825Stheraven swap(*__i, *__m); 5148227825Stheraven ++__n_swaps; 5149227825Stheraven } 5150227825Stheraven // [__first, __i) < *__i and *__i <= [__i+1, __last) 5151227825Stheraven if (__nth == __i) 5152227825Stheraven return; 5153227825Stheraven if (__n_swaps == 0) 5154227825Stheraven { 5155227825Stheraven // We were given a perfectly partitioned sequence. Coincidence? 5156227825Stheraven if (__nth < __i) 5157227825Stheraven { 5158227825Stheraven // Check for [__first, __i) already sorted 5159227825Stheraven __j = __m = __first; 5160227825Stheraven while (++__j != __i) 5161227825Stheraven { 5162227825Stheraven if (__comp(*__j, *__m)) 5163227825Stheraven // not yet sorted, so sort 5164227825Stheraven goto not_sorted; 5165227825Stheraven __m = __j; 5166227825Stheraven } 5167227825Stheraven // [__first, __i) sorted 5168227825Stheraven return; 5169227825Stheraven } 5170227825Stheraven else 5171227825Stheraven { 5172227825Stheraven // Check for [__i, __last) already sorted 5173227825Stheraven __j = __m = __i; 5174227825Stheraven while (++__j != __last) 5175227825Stheraven { 5176227825Stheraven if (__comp(*__j, *__m)) 5177227825Stheraven // not yet sorted, so sort 5178227825Stheraven goto not_sorted; 5179227825Stheraven __m = __j; 5180227825Stheraven } 5181227825Stheraven // [__i, __last) sorted 5182227825Stheraven return; 5183227825Stheraven } 5184227825Stheraven } 5185227825Stheravennot_sorted: 5186227825Stheraven // __nth_element on range containing __nth 5187227825Stheraven if (__nth < __i) 5188227825Stheraven { 5189227825Stheraven // __nth_element<_Compare>(__first, __nth, __i, __comp); 5190227825Stheraven __last = __i; 5191227825Stheraven } 5192227825Stheraven else 5193227825Stheraven { 5194227825Stheraven // __nth_element<_Compare>(__i+1, __nth, __last, __comp); 5195227825Stheraven __first = ++__i; 5196227825Stheraven } 5197227825Stheraven } 5198227825Stheraven} 5199227825Stheraven 5200227825Stheraventemplate <class _RandomAccessIterator, class _Compare> 5201227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 5202227825Stheravenvoid 5203227825Stheravennth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp) 5204227825Stheraven{ 5205227825Stheraven#ifdef _LIBCPP_DEBUG2 5206227825Stheraven typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 5207227825Stheraven __debug_less<_Compare> __c(__comp); 5208227825Stheraven __nth_element<_Comp_ref>(__first, __nth, __last, __c); 5209227825Stheraven#else // _LIBCPP_DEBUG2 5210227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 5211227825Stheraven __nth_element<_Comp_ref>(__first, __nth, __last, __comp); 5212227825Stheraven#endif // _LIBCPP_DEBUG2 5213227825Stheraven} 5214227825Stheraven 5215227825Stheraventemplate <class _RandomAccessIterator> 5216227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 5217227825Stheravenvoid 5218227825Stheravennth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last) 5219227825Stheraven{ 5220227825Stheraven _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 5221227825Stheraven} 5222227825Stheraven 5223227825Stheraven// includes 5224227825Stheraven 5225227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2> 5226227825Stheravenbool 5227227825Stheraven__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2, 5228227825Stheraven _Compare __comp) 5229227825Stheraven{ 5230227825Stheraven for (; __first2 != __last2; ++__first1) 5231227825Stheraven { 5232227825Stheraven if (__first1 == __last1 || __comp(*__first2, *__first1)) 5233227825Stheraven return false; 5234227825Stheraven if (!__comp(*__first1, *__first2)) 5235227825Stheraven ++__first2; 5236227825Stheraven } 5237227825Stheraven return true; 5238227825Stheraven} 5239227825Stheraven 5240227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _Compare> 5241227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 5242227825Stheravenbool 5243227825Stheravenincludes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2, 5244227825Stheraven _Compare __comp) 5245227825Stheraven{ 5246227825Stheraven#ifdef _LIBCPP_DEBUG2 5247227825Stheraven typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 5248227825Stheraven __debug_less<_Compare> __c(__comp); 5249227825Stheraven return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c); 5250227825Stheraven#else // _LIBCPP_DEBUG2 5251227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 5252227825Stheraven return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp); 5253227825Stheraven#endif // _LIBCPP_DEBUG2 5254227825Stheraven} 5255227825Stheraven 5256227825Stheraventemplate <class _InputIterator1, class _InputIterator2> 5257227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 5258227825Stheravenbool 5259227825Stheravenincludes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2) 5260227825Stheraven{ 5261227825Stheraven return _VSTD::includes(__first1, __last1, __first2, __last2, 5262227825Stheraven __less<typename iterator_traits<_InputIterator1>::value_type, 5263227825Stheraven typename iterator_traits<_InputIterator2>::value_type>()); 5264227825Stheraven} 5265227825Stheraven 5266227825Stheraven// set_union 5267227825Stheraven 5268227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator> 5269227825Stheraven_OutputIterator 5270227825Stheraven__set_union(_InputIterator1 __first1, _InputIterator1 __last1, 5271227825Stheraven _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) 5272227825Stheraven{ 5273227825Stheraven for (; __first1 != __last1; ++__result) 5274227825Stheraven { 5275227825Stheraven if (__first2 == __last2) 5276227825Stheraven return _VSTD::copy(__first1, __last1, __result); 5277227825Stheraven if (__comp(*__first2, *__first1)) 5278227825Stheraven { 5279227825Stheraven *__result = *__first2; 5280227825Stheraven ++__first2; 5281227825Stheraven } 5282227825Stheraven else 5283227825Stheraven { 5284227825Stheraven *__result = *__first1; 5285227825Stheraven if (!__comp(*__first1, *__first2)) 5286227825Stheraven ++__first2; 5287227825Stheraven ++__first1; 5288227825Stheraven } 5289227825Stheraven } 5290227825Stheraven return _VSTD::copy(__first2, __last2, __result); 5291227825Stheraven} 5292227825Stheraven 5293227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare> 5294227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 5295227825Stheraven_OutputIterator 5296227825Stheravenset_union(_InputIterator1 __first1, _InputIterator1 __last1, 5297227825Stheraven _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) 5298227825Stheraven{ 5299227825Stheraven#ifdef _LIBCPP_DEBUG2 5300227825Stheraven typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 5301227825Stheraven __debug_less<_Compare> __c(__comp); 5302227825Stheraven return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c); 5303227825Stheraven#else // _LIBCPP_DEBUG2 5304227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 5305227825Stheraven return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp); 5306227825Stheraven#endif // _LIBCPP_DEBUG2 5307227825Stheraven} 5308227825Stheraven 5309227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator> 5310227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 5311227825Stheraven_OutputIterator 5312227825Stheravenset_union(_InputIterator1 __first1, _InputIterator1 __last1, 5313227825Stheraven _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result) 5314227825Stheraven{ 5315227825Stheraven return _VSTD::set_union(__first1, __last1, __first2, __last2, __result, 5316227825Stheraven __less<typename iterator_traits<_InputIterator1>::value_type, 5317227825Stheraven typename iterator_traits<_InputIterator2>::value_type>()); 5318227825Stheraven} 5319227825Stheraven 5320227825Stheraven// set_intersection 5321227825Stheraven 5322227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator> 5323227825Stheraven_OutputIterator 5324227825Stheraven__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1, 5325227825Stheraven _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) 5326227825Stheraven{ 5327227825Stheraven while (__first1 != __last1 && __first2 != __last2) 5328227825Stheraven { 5329227825Stheraven if (__comp(*__first1, *__first2)) 5330227825Stheraven ++__first1; 5331227825Stheraven else 5332227825Stheraven { 5333227825Stheraven if (!__comp(*__first2, *__first1)) 5334227825Stheraven { 5335227825Stheraven *__result = *__first1; 5336227825Stheraven ++__result; 5337227825Stheraven ++__first1; 5338227825Stheraven } 5339227825Stheraven ++__first2; 5340227825Stheraven } 5341227825Stheraven } 5342227825Stheraven return __result; 5343227825Stheraven} 5344227825Stheraven 5345227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare> 5346227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 5347227825Stheraven_OutputIterator 5348227825Stheravenset_intersection(_InputIterator1 __first1, _InputIterator1 __last1, 5349227825Stheraven _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) 5350227825Stheraven{ 5351227825Stheraven#ifdef _LIBCPP_DEBUG2 5352227825Stheraven typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 5353227825Stheraven __debug_less<_Compare> __c(__comp); 5354227825Stheraven return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c); 5355227825Stheraven#else // _LIBCPP_DEBUG2 5356227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 5357227825Stheraven return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp); 5358227825Stheraven#endif // _LIBCPP_DEBUG2 5359227825Stheraven} 5360227825Stheraven 5361227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator> 5362227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 5363227825Stheraven_OutputIterator 5364227825Stheravenset_intersection(_InputIterator1 __first1, _InputIterator1 __last1, 5365227825Stheraven _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result) 5366227825Stheraven{ 5367227825Stheraven return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result, 5368227825Stheraven __less<typename iterator_traits<_InputIterator1>::value_type, 5369227825Stheraven typename iterator_traits<_InputIterator2>::value_type>()); 5370227825Stheraven} 5371227825Stheraven 5372227825Stheraven// set_difference 5373227825Stheraven 5374227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator> 5375227825Stheraven_OutputIterator 5376227825Stheraven__set_difference(_InputIterator1 __first1, _InputIterator1 __last1, 5377227825Stheraven _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) 5378227825Stheraven{ 5379227825Stheraven while (__first1 != __last1) 5380227825Stheraven { 5381227825Stheraven if (__first2 == __last2) 5382227825Stheraven return _VSTD::copy(__first1, __last1, __result); 5383227825Stheraven if (__comp(*__first1, *__first2)) 5384227825Stheraven { 5385227825Stheraven *__result = *__first1; 5386227825Stheraven ++__result; 5387227825Stheraven ++__first1; 5388227825Stheraven } 5389227825Stheraven else 5390227825Stheraven { 5391227825Stheraven if (!__comp(*__first2, *__first1)) 5392227825Stheraven ++__first1; 5393227825Stheraven ++__first2; 5394227825Stheraven } 5395227825Stheraven } 5396227825Stheraven return __result; 5397227825Stheraven} 5398227825Stheraven 5399227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare> 5400227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 5401227825Stheraven_OutputIterator 5402227825Stheravenset_difference(_InputIterator1 __first1, _InputIterator1 __last1, 5403227825Stheraven _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) 5404227825Stheraven{ 5405227825Stheraven#ifdef _LIBCPP_DEBUG2 5406227825Stheraven typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 5407227825Stheraven __debug_less<_Compare> __c(__comp); 5408227825Stheraven return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c); 5409227825Stheraven#else // _LIBCPP_DEBUG2 5410227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 5411227825Stheraven return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp); 5412227825Stheraven#endif // _LIBCPP_DEBUG2 5413227825Stheraven} 5414227825Stheraven 5415227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator> 5416227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 5417227825Stheraven_OutputIterator 5418227825Stheravenset_difference(_InputIterator1 __first1, _InputIterator1 __last1, 5419227825Stheraven _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result) 5420227825Stheraven{ 5421227825Stheraven return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result, 5422227825Stheraven __less<typename iterator_traits<_InputIterator1>::value_type, 5423227825Stheraven typename iterator_traits<_InputIterator2>::value_type>()); 5424227825Stheraven} 5425227825Stheraven 5426227825Stheraven// set_symmetric_difference 5427227825Stheraven 5428227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator> 5429227825Stheraven_OutputIterator 5430227825Stheraven__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1, 5431227825Stheraven _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) 5432227825Stheraven{ 5433227825Stheraven while (__first1 != __last1) 5434227825Stheraven { 5435227825Stheraven if (__first2 == __last2) 5436227825Stheraven return _VSTD::copy(__first1, __last1, __result); 5437227825Stheraven if (__comp(*__first1, *__first2)) 5438227825Stheraven { 5439227825Stheraven *__result = *__first1; 5440227825Stheraven ++__result; 5441227825Stheraven ++__first1; 5442227825Stheraven } 5443227825Stheraven else 5444227825Stheraven { 5445227825Stheraven if (__comp(*__first2, *__first1)) 5446227825Stheraven { 5447227825Stheraven *__result = *__first2; 5448227825Stheraven ++__result; 5449227825Stheraven } 5450227825Stheraven else 5451227825Stheraven ++__first1; 5452227825Stheraven ++__first2; 5453227825Stheraven } 5454227825Stheraven } 5455227825Stheraven return _VSTD::copy(__first2, __last2, __result); 5456227825Stheraven} 5457227825Stheraven 5458227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare> 5459227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 5460227825Stheraven_OutputIterator 5461227825Stheravenset_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1, 5462227825Stheraven _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) 5463227825Stheraven{ 5464227825Stheraven#ifdef _LIBCPP_DEBUG2 5465227825Stheraven typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 5466227825Stheraven __debug_less<_Compare> __c(__comp); 5467227825Stheraven return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c); 5468227825Stheraven#else // _LIBCPP_DEBUG2 5469227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 5470227825Stheraven return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp); 5471227825Stheraven#endif // _LIBCPP_DEBUG2 5472227825Stheraven} 5473227825Stheraven 5474227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator> 5475227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 5476227825Stheraven_OutputIterator 5477227825Stheravenset_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1, 5478227825Stheraven _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result) 5479227825Stheraven{ 5480227825Stheraven return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result, 5481227825Stheraven __less<typename iterator_traits<_InputIterator1>::value_type, 5482227825Stheraven typename iterator_traits<_InputIterator2>::value_type>()); 5483227825Stheraven} 5484227825Stheraven 5485227825Stheraven// lexicographical_compare 5486227825Stheraven 5487227825Stheraventemplate <class _Compare, class _InputIterator1, class _InputIterator2> 5488227825Stheravenbool 5489227825Stheraven__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1, 5490227825Stheraven _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp) 5491227825Stheraven{ 5492227825Stheraven for (; __first2 != __last2; ++__first1, ++__first2) 5493227825Stheraven { 5494227825Stheraven if (__first1 == __last1 || __comp(*__first1, *__first2)) 5495227825Stheraven return true; 5496227825Stheraven if (__comp(*__first2, *__first1)) 5497227825Stheraven return false; 5498227825Stheraven } 5499227825Stheraven return false; 5500227825Stheraven} 5501227825Stheraven 5502227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _Compare> 5503227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 5504227825Stheravenbool 5505227825Stheravenlexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1, 5506227825Stheraven _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp) 5507227825Stheraven{ 5508227825Stheraven#ifdef _LIBCPP_DEBUG2 5509227825Stheraven typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 5510227825Stheraven __debug_less<_Compare> __c(__comp); 5511227825Stheraven return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c); 5512227825Stheraven#else // _LIBCPP_DEBUG2 5513227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 5514227825Stheraven return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp); 5515227825Stheraven#endif // _LIBCPP_DEBUG2 5516227825Stheraven} 5517227825Stheraven 5518227825Stheraventemplate <class _InputIterator1, class _InputIterator2> 5519227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 5520227825Stheravenbool 5521227825Stheravenlexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1, 5522227825Stheraven _InputIterator2 __first2, _InputIterator2 __last2) 5523227825Stheraven{ 5524227825Stheraven return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2, 5525227825Stheraven __less<typename iterator_traits<_InputIterator1>::value_type, 5526227825Stheraven typename iterator_traits<_InputIterator2>::value_type>()); 5527227825Stheraven} 5528227825Stheraven 5529227825Stheraven// next_permutation 5530227825Stheraven 5531227825Stheraventemplate <class _Compare, class _BidirectionalIterator> 5532227825Stheravenbool 5533227825Stheraven__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp) 5534227825Stheraven{ 5535227825Stheraven _BidirectionalIterator __i = __last; 5536227825Stheraven if (__first == __last || __first == --__i) 5537227825Stheraven return false; 5538227825Stheraven while (true) 5539227825Stheraven { 5540227825Stheraven _BidirectionalIterator __ip1 = __i; 5541227825Stheraven if (__comp(*--__i, *__ip1)) 5542227825Stheraven { 5543227825Stheraven _BidirectionalIterator __j = __last; 5544227825Stheraven while (!__comp(*__i, *--__j)) 5545227825Stheraven ; 5546227825Stheraven swap(*__i, *__j); 5547227825Stheraven _VSTD::reverse(__ip1, __last); 5548227825Stheraven return true; 5549227825Stheraven } 5550227825Stheraven if (__i == __first) 5551227825Stheraven { 5552227825Stheraven _VSTD::reverse(__first, __last); 5553227825Stheraven return false; 5554227825Stheraven } 5555227825Stheraven } 5556227825Stheraven} 5557227825Stheraven 5558227825Stheraventemplate <class _BidirectionalIterator, class _Compare> 5559227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 5560227825Stheravenbool 5561227825Stheravennext_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp) 5562227825Stheraven{ 5563227825Stheraven#ifdef _LIBCPP_DEBUG2 5564227825Stheraven typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 5565227825Stheraven __debug_less<_Compare> __c(__comp); 5566227825Stheraven return __next_permutation<_Comp_ref>(__first, __last, __c); 5567227825Stheraven#else // _LIBCPP_DEBUG2 5568227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 5569227825Stheraven return __next_permutation<_Comp_ref>(__first, __last, __comp); 5570227825Stheraven#endif // _LIBCPP_DEBUG2 5571227825Stheraven} 5572227825Stheraven 5573227825Stheraventemplate <class _BidirectionalIterator> 5574227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 5575227825Stheravenbool 5576227825Stheravennext_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last) 5577227825Stheraven{ 5578227825Stheraven return _VSTD::next_permutation(__first, __last, 5579227825Stheraven __less<typename iterator_traits<_BidirectionalIterator>::value_type>()); 5580227825Stheraven} 5581227825Stheraven 5582227825Stheraven// prev_permutation 5583227825Stheraven 5584227825Stheraventemplate <class _Compare, class _BidirectionalIterator> 5585227825Stheravenbool 5586227825Stheraven__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp) 5587227825Stheraven{ 5588227825Stheraven _BidirectionalIterator __i = __last; 5589227825Stheraven if (__first == __last || __first == --__i) 5590227825Stheraven return false; 5591227825Stheraven while (true) 5592227825Stheraven { 5593227825Stheraven _BidirectionalIterator __ip1 = __i; 5594227825Stheraven if (__comp(*__ip1, *--__i)) 5595227825Stheraven { 5596227825Stheraven _BidirectionalIterator __j = __last; 5597227825Stheraven while (!__comp(*--__j, *__i)) 5598227825Stheraven ; 5599227825Stheraven swap(*__i, *__j); 5600227825Stheraven _VSTD::reverse(__ip1, __last); 5601227825Stheraven return true; 5602227825Stheraven } 5603227825Stheraven if (__i == __first) 5604227825Stheraven { 5605227825Stheraven _VSTD::reverse(__first, __last); 5606227825Stheraven return false; 5607227825Stheraven } 5608227825Stheraven } 5609227825Stheraven} 5610227825Stheraven 5611227825Stheraventemplate <class _BidirectionalIterator, class _Compare> 5612227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 5613227825Stheravenbool 5614227825Stheravenprev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp) 5615227825Stheraven{ 5616227825Stheraven#ifdef _LIBCPP_DEBUG2 5617227825Stheraven typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 5618227825Stheraven __debug_less<_Compare> __c(__comp); 5619227825Stheraven return __prev_permutation<_Comp_ref>(__first, __last, __c); 5620227825Stheraven#else // _LIBCPP_DEBUG2 5621227825Stheraven typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 5622227825Stheraven return __prev_permutation<_Comp_ref>(__first, __last, __comp); 5623227825Stheraven#endif // _LIBCPP_DEBUG2 5624227825Stheraven} 5625227825Stheraven 5626227825Stheraventemplate <class _BidirectionalIterator> 5627227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 5628227825Stheravenbool 5629227825Stheravenprev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last) 5630227825Stheraven{ 5631227825Stheraven return _VSTD::prev_permutation(__first, __last, 5632227825Stheraven __less<typename iterator_traits<_BidirectionalIterator>::value_type>()); 5633227825Stheraven} 5634227825Stheraven 5635227825Stheraventemplate <class _Tp> 5636227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 5637227825Stheraventypename enable_if 5638227825Stheraven< 5639227825Stheraven is_integral<_Tp>::value, 5640227825Stheraven _Tp 5641227825Stheraven>::type 5642227825Stheraven__rotate_left(_Tp __t, _Tp __n = 1) 5643227825Stheraven{ 5644227825Stheraven const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1); 5645227825Stheraven __n &= __bits; 5646227825Stheraven return static_cast<_Tp>((__t << __n) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> (__bits - __n))); 5647227825Stheraven} 5648227825Stheraven 5649227825Stheraventemplate <class _Tp> 5650227825Stheraveninline _LIBCPP_INLINE_VISIBILITY 5651227825Stheraventypename enable_if 5652227825Stheraven< 5653227825Stheraven is_integral<_Tp>::value, 5654227825Stheraven _Tp 5655227825Stheraven>::type 5656227825Stheraven__rotate_right(_Tp __t, _Tp __n = 1) 5657227825Stheraven{ 5658227825Stheraven const unsigned __bits = static_cast<unsigned>(sizeof(_Tp) * __CHAR_BIT__ - 1); 5659227825Stheraven __n &= __bits; 5660227825Stheraven return static_cast<_Tp>((__t << (__bits - __n)) | (static_cast<typename make_unsigned<_Tp>::type>(__t) >> __n)); 5661227825Stheraven} 5662227825Stheraven 5663227825Stheraven_LIBCPP_END_NAMESPACE_STD 5664227825Stheraven 5665227825Stheraven#endif // _LIBCPP_ALGORITHM 5666