17a984708SDavid Chisnall// -*- C++ -*- 27a984708SDavid Chisnall//===-------------------------- algorithm ---------------------------------===// 37a984708SDavid Chisnall// 47a984708SDavid Chisnall// The LLVM Compiler Infrastructure 57a984708SDavid Chisnall// 67a984708SDavid Chisnall// This file is dual licensed under the MIT and the University of Illinois Open 77a984708SDavid Chisnall// Source Licenses. See LICENSE.TXT for details. 87a984708SDavid Chisnall// 97a984708SDavid Chisnall//===----------------------------------------------------------------------===// 107a984708SDavid Chisnall 117a984708SDavid Chisnall#ifndef _LIBCPP_ALGORITHM 127a984708SDavid Chisnall#define _LIBCPP_ALGORITHM 137a984708SDavid Chisnall 147a984708SDavid Chisnall/* 157a984708SDavid Chisnall algorithm synopsis 167a984708SDavid Chisnall 177a984708SDavid Chisnall#include <initializer_list> 187a984708SDavid Chisnall 197a984708SDavid Chisnallnamespace std 207a984708SDavid Chisnall{ 217a984708SDavid Chisnall 227a984708SDavid Chisnalltemplate <class InputIterator, class Predicate> 237a984708SDavid Chisnall bool 247a984708SDavid Chisnall all_of(InputIterator first, InputIterator last, Predicate pred); 257a984708SDavid Chisnall 267a984708SDavid Chisnalltemplate <class InputIterator, class Predicate> 277a984708SDavid Chisnall bool 287a984708SDavid Chisnall any_of(InputIterator first, InputIterator last, Predicate pred); 297a984708SDavid Chisnall 307a984708SDavid Chisnalltemplate <class InputIterator, class Predicate> 317a984708SDavid Chisnall bool 327a984708SDavid Chisnall none_of(InputIterator first, InputIterator last, Predicate pred); 337a984708SDavid Chisnall 347a984708SDavid Chisnalltemplate <class InputIterator, class Function> 357a984708SDavid Chisnall Function 367a984708SDavid Chisnall for_each(InputIterator first, InputIterator last, Function f); 377a984708SDavid Chisnall 38302affcbSDimitry Andrictemplate<class InputIterator, class Size, class Function> 39302affcbSDimitry Andric InputIterator for_each_n(InputIterator first, Size n, Function f); // C++17 40302affcbSDimitry Andric 417a984708SDavid Chisnalltemplate <class InputIterator, class T> 427a984708SDavid Chisnall InputIterator 437a984708SDavid Chisnall find(InputIterator first, InputIterator last, const T& value); 447a984708SDavid Chisnall 457a984708SDavid Chisnalltemplate <class InputIterator, class Predicate> 467a984708SDavid Chisnall InputIterator 477a984708SDavid Chisnall find_if(InputIterator first, InputIterator last, Predicate pred); 487a984708SDavid Chisnall 497a984708SDavid Chisnalltemplate<class InputIterator, class Predicate> 507a984708SDavid Chisnall InputIterator 517a984708SDavid Chisnall find_if_not(InputIterator first, InputIterator last, Predicate pred); 527a984708SDavid Chisnall 537a984708SDavid Chisnalltemplate <class ForwardIterator1, class ForwardIterator2> 547a984708SDavid Chisnall ForwardIterator1 557a984708SDavid Chisnall find_end(ForwardIterator1 first1, ForwardIterator1 last1, 567a984708SDavid Chisnall ForwardIterator2 first2, ForwardIterator2 last2); 577a984708SDavid Chisnall 587a984708SDavid Chisnalltemplate <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> 597a984708SDavid Chisnall ForwardIterator1 607a984708SDavid Chisnall find_end(ForwardIterator1 first1, ForwardIterator1 last1, 617a984708SDavid Chisnall ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred); 627a984708SDavid Chisnall 637a984708SDavid Chisnalltemplate <class ForwardIterator1, class ForwardIterator2> 647a984708SDavid Chisnall ForwardIterator1 657a984708SDavid Chisnall find_first_of(ForwardIterator1 first1, ForwardIterator1 last1, 667a984708SDavid Chisnall ForwardIterator2 first2, ForwardIterator2 last2); 677a984708SDavid Chisnall 687a984708SDavid Chisnalltemplate <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> 697a984708SDavid Chisnall ForwardIterator1 707a984708SDavid Chisnall find_first_of(ForwardIterator1 first1, ForwardIterator1 last1, 717a984708SDavid Chisnall ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred); 727a984708SDavid Chisnall 737a984708SDavid Chisnalltemplate <class ForwardIterator> 747a984708SDavid Chisnall ForwardIterator 757a984708SDavid Chisnall adjacent_find(ForwardIterator first, ForwardIterator last); 767a984708SDavid Chisnall 777a984708SDavid Chisnalltemplate <class ForwardIterator, class BinaryPredicate> 787a984708SDavid Chisnall ForwardIterator 797a984708SDavid Chisnall adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred); 807a984708SDavid Chisnall 817a984708SDavid Chisnalltemplate <class InputIterator, class T> 827a984708SDavid Chisnall typename iterator_traits<InputIterator>::difference_type 837a984708SDavid Chisnall count(InputIterator first, InputIterator last, const T& value); 847a984708SDavid Chisnall 857a984708SDavid Chisnalltemplate <class InputIterator, class Predicate> 867a984708SDavid Chisnall typename iterator_traits<InputIterator>::difference_type 877a984708SDavid Chisnall count_if(InputIterator first, InputIterator last, Predicate pred); 887a984708SDavid Chisnall 897a984708SDavid Chisnalltemplate <class InputIterator1, class InputIterator2> 907a984708SDavid Chisnall pair<InputIterator1, InputIterator2> 917a984708SDavid Chisnall mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2); 927a984708SDavid Chisnall 934bab9fd9SDavid Chisnalltemplate <class InputIterator1, class InputIterator2> 944bab9fd9SDavid Chisnall pair<InputIterator1, InputIterator2> 954bab9fd9SDavid Chisnall mismatch(InputIterator1 first1, InputIterator1 last1, 964bab9fd9SDavid Chisnall InputIterator2 first2, InputIterator2 last2); // **C++14** 974bab9fd9SDavid Chisnall 987a984708SDavid Chisnalltemplate <class InputIterator1, class InputIterator2, class BinaryPredicate> 997a984708SDavid Chisnall pair<InputIterator1, InputIterator2> 1007a984708SDavid Chisnall mismatch(InputIterator1 first1, InputIterator1 last1, 1017a984708SDavid Chisnall InputIterator2 first2, BinaryPredicate pred); 1027a984708SDavid Chisnall 1034bab9fd9SDavid Chisnalltemplate <class InputIterator1, class InputIterator2, class BinaryPredicate> 1044bab9fd9SDavid Chisnall pair<InputIterator1, InputIterator2> 1054bab9fd9SDavid Chisnall mismatch(InputIterator1 first1, InputIterator1 last1, 1064bab9fd9SDavid Chisnall InputIterator2 first2, InputIterator2 last2, 1074bab9fd9SDavid Chisnall BinaryPredicate pred); // **C++14** 1084bab9fd9SDavid Chisnall 1097a984708SDavid Chisnalltemplate <class InputIterator1, class InputIterator2> 1107a984708SDavid Chisnall bool 1117a984708SDavid Chisnall equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2); 1127a984708SDavid Chisnall 1134bab9fd9SDavid Chisnalltemplate <class InputIterator1, class InputIterator2> 1144bab9fd9SDavid Chisnall bool 1154bab9fd9SDavid Chisnall equal(InputIterator1 first1, InputIterator1 last1, 1164bab9fd9SDavid Chisnall InputIterator2 first2, InputIterator2 last2); // **C++14** 1174bab9fd9SDavid Chisnall 1187a984708SDavid Chisnalltemplate <class InputIterator1, class InputIterator2, class BinaryPredicate> 1197a984708SDavid Chisnall bool 1207a984708SDavid Chisnall equal(InputIterator1 first1, InputIterator1 last1, 1217a984708SDavid Chisnall InputIterator2 first2, BinaryPredicate pred); 1227a984708SDavid Chisnall 1234bab9fd9SDavid Chisnalltemplate <class InputIterator1, class InputIterator2, class BinaryPredicate> 1244bab9fd9SDavid Chisnall bool 1254bab9fd9SDavid Chisnall equal(InputIterator1 first1, InputIterator1 last1, 1264bab9fd9SDavid Chisnall InputIterator2 first2, InputIterator2 last2, 1274bab9fd9SDavid Chisnall BinaryPredicate pred); // **C++14** 1284bab9fd9SDavid Chisnall 1297a984708SDavid Chisnalltemplate<class ForwardIterator1, class ForwardIterator2> 1307a984708SDavid Chisnall bool 1317a984708SDavid Chisnall is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, 1327a984708SDavid Chisnall ForwardIterator2 first2); 1337a984708SDavid Chisnall 1344bab9fd9SDavid Chisnalltemplate<class ForwardIterator1, class ForwardIterator2> 1354bab9fd9SDavid Chisnall bool 1364bab9fd9SDavid Chisnall is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, 1374bab9fd9SDavid Chisnall ForwardIterator2 first2, ForwardIterator2 last2); // **C++14** 1384bab9fd9SDavid Chisnall 1397a984708SDavid Chisnalltemplate<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> 1407a984708SDavid Chisnall bool 1417a984708SDavid Chisnall is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, 1427a984708SDavid Chisnall ForwardIterator2 first2, BinaryPredicate pred); 1437a984708SDavid Chisnall 1444bab9fd9SDavid Chisnalltemplate<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> 1454bab9fd9SDavid Chisnall bool 1464bab9fd9SDavid Chisnall is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, 1474bab9fd9SDavid Chisnall ForwardIterator2 first2, ForwardIterator2 last2, 1484bab9fd9SDavid Chisnall BinaryPredicate pred); // **C++14** 1494bab9fd9SDavid Chisnall 1507a984708SDavid Chisnalltemplate <class ForwardIterator1, class ForwardIterator2> 1517a984708SDavid Chisnall ForwardIterator1 1527a984708SDavid Chisnall search(ForwardIterator1 first1, ForwardIterator1 last1, 1537a984708SDavid Chisnall ForwardIterator2 first2, ForwardIterator2 last2); 1547a984708SDavid Chisnall 1557a984708SDavid Chisnalltemplate <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> 1567a984708SDavid Chisnall ForwardIterator1 1577a984708SDavid Chisnall search(ForwardIterator1 first1, ForwardIterator1 last1, 1587a984708SDavid Chisnall ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred); 1597a984708SDavid Chisnall 1607a984708SDavid Chisnalltemplate <class ForwardIterator, class Size, class T> 1617a984708SDavid Chisnall ForwardIterator 1627a984708SDavid Chisnall search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value); 1637a984708SDavid Chisnall 1647a984708SDavid Chisnalltemplate <class ForwardIterator, class Size, class T, class BinaryPredicate> 1657a984708SDavid Chisnall ForwardIterator 1667a984708SDavid Chisnall search_n(ForwardIterator first, ForwardIterator last, 1677a984708SDavid Chisnall Size count, const T& value, BinaryPredicate pred); 1687a984708SDavid Chisnall 1697a984708SDavid Chisnalltemplate <class InputIterator, class OutputIterator> 1707a984708SDavid Chisnall OutputIterator 1717a984708SDavid Chisnall copy(InputIterator first, InputIterator last, OutputIterator result); 1727a984708SDavid Chisnall 1737a984708SDavid Chisnalltemplate<class InputIterator, class OutputIterator, class Predicate> 1747a984708SDavid Chisnall OutputIterator 1757a984708SDavid Chisnall copy_if(InputIterator first, InputIterator last, 1767a984708SDavid Chisnall OutputIterator result, Predicate pred); 1777a984708SDavid Chisnall 1787a984708SDavid Chisnalltemplate<class InputIterator, class Size, class OutputIterator> 1797a984708SDavid Chisnall OutputIterator 1807a984708SDavid Chisnall copy_n(InputIterator first, Size n, OutputIterator result); 1817a984708SDavid Chisnall 1827a984708SDavid Chisnalltemplate <class BidirectionalIterator1, class BidirectionalIterator2> 1837a984708SDavid Chisnall BidirectionalIterator2 1847a984708SDavid Chisnall copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last, 1857a984708SDavid Chisnall BidirectionalIterator2 result); 1867a984708SDavid Chisnall 1877a984708SDavid Chisnalltemplate <class ForwardIterator1, class ForwardIterator2> 1887a984708SDavid Chisnall ForwardIterator2 1897a984708SDavid Chisnall swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2); 1907a984708SDavid Chisnall 1917a984708SDavid Chisnalltemplate <class ForwardIterator1, class ForwardIterator2> 1927a984708SDavid Chisnall void 1937a984708SDavid Chisnall iter_swap(ForwardIterator1 a, ForwardIterator2 b); 1947a984708SDavid Chisnall 1957a984708SDavid Chisnalltemplate <class InputIterator, class OutputIterator, class UnaryOperation> 1967a984708SDavid Chisnall OutputIterator 1977a984708SDavid Chisnall transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op); 1987a984708SDavid Chisnall 1997a984708SDavid Chisnalltemplate <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation> 2007a984708SDavid Chisnall OutputIterator 2017a984708SDavid Chisnall transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, 2027a984708SDavid Chisnall OutputIterator result, BinaryOperation binary_op); 2037a984708SDavid Chisnall 2047a984708SDavid Chisnalltemplate <class ForwardIterator, class T> 2057a984708SDavid Chisnall void 2067a984708SDavid Chisnall replace(ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value); 2077a984708SDavid Chisnall 2087a984708SDavid Chisnalltemplate <class ForwardIterator, class Predicate, class T> 2097a984708SDavid Chisnall void 2107a984708SDavid Chisnall replace_if(ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value); 2117a984708SDavid Chisnall 2127a984708SDavid Chisnalltemplate <class InputIterator, class OutputIterator, class T> 2137a984708SDavid Chisnall OutputIterator 2147a984708SDavid Chisnall replace_copy(InputIterator first, InputIterator last, OutputIterator result, 2157a984708SDavid Chisnall const T& old_value, const T& new_value); 2167a984708SDavid Chisnall 2177a984708SDavid Chisnalltemplate <class InputIterator, class OutputIterator, class Predicate, class T> 2187a984708SDavid Chisnall OutputIterator 2197a984708SDavid Chisnall replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred, const T& new_value); 2207a984708SDavid Chisnall 2217a984708SDavid Chisnalltemplate <class ForwardIterator, class T> 2227a984708SDavid Chisnall void 2237a984708SDavid Chisnall fill(ForwardIterator first, ForwardIterator last, const T& value); 2247a984708SDavid Chisnall 2257a984708SDavid Chisnalltemplate <class OutputIterator, class Size, class T> 2267a984708SDavid Chisnall OutputIterator 2277a984708SDavid Chisnall fill_n(OutputIterator first, Size n, const T& value); 2287a984708SDavid Chisnall 2297a984708SDavid Chisnalltemplate <class ForwardIterator, class Generator> 2307a984708SDavid Chisnall void 2317a984708SDavid Chisnall generate(ForwardIterator first, ForwardIterator last, Generator gen); 2327a984708SDavid Chisnall 2337a984708SDavid Chisnalltemplate <class OutputIterator, class Size, class Generator> 2347a984708SDavid Chisnall OutputIterator 2357a984708SDavid Chisnall generate_n(OutputIterator first, Size n, Generator gen); 2367a984708SDavid Chisnall 2377a984708SDavid Chisnalltemplate <class ForwardIterator, class T> 2387a984708SDavid Chisnall ForwardIterator 2397a984708SDavid Chisnall remove(ForwardIterator first, ForwardIterator last, const T& value); 2407a984708SDavid Chisnall 2417a984708SDavid Chisnalltemplate <class ForwardIterator, class Predicate> 2427a984708SDavid Chisnall ForwardIterator 2437a984708SDavid Chisnall remove_if(ForwardIterator first, ForwardIterator last, Predicate pred); 2447a984708SDavid Chisnall 2457a984708SDavid Chisnalltemplate <class InputIterator, class OutputIterator, class T> 2467a984708SDavid Chisnall OutputIterator 2477a984708SDavid Chisnall remove_copy(InputIterator first, InputIterator last, OutputIterator result, const T& value); 2487a984708SDavid Chisnall 2497a984708SDavid Chisnalltemplate <class InputIterator, class OutputIterator, class Predicate> 2507a984708SDavid Chisnall OutputIterator 2517a984708SDavid Chisnall remove_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate pred); 2527a984708SDavid Chisnall 2537a984708SDavid Chisnalltemplate <class ForwardIterator> 2547a984708SDavid Chisnall ForwardIterator 2557a984708SDavid Chisnall unique(ForwardIterator first, ForwardIterator last); 2567a984708SDavid Chisnall 2577a984708SDavid Chisnalltemplate <class ForwardIterator, class BinaryPredicate> 2587a984708SDavid Chisnall ForwardIterator 2597a984708SDavid Chisnall unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred); 2607a984708SDavid Chisnall 2617a984708SDavid Chisnalltemplate <class InputIterator, class OutputIterator> 2627a984708SDavid Chisnall OutputIterator 2637a984708SDavid Chisnall unique_copy(InputIterator first, InputIterator last, OutputIterator result); 2647a984708SDavid Chisnall 2657a984708SDavid Chisnalltemplate <class InputIterator, class OutputIterator, class BinaryPredicate> 2667a984708SDavid Chisnall OutputIterator 2677a984708SDavid Chisnall unique_copy(InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred); 2687a984708SDavid Chisnall 2697a984708SDavid Chisnalltemplate <class BidirectionalIterator> 2707a984708SDavid Chisnall void 2717a984708SDavid Chisnall reverse(BidirectionalIterator first, BidirectionalIterator last); 2727a984708SDavid Chisnall 2737a984708SDavid Chisnalltemplate <class BidirectionalIterator, class OutputIterator> 2747a984708SDavid Chisnall OutputIterator 2757a984708SDavid Chisnall reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result); 2767a984708SDavid Chisnall 2777a984708SDavid Chisnalltemplate <class ForwardIterator> 2787a984708SDavid Chisnall ForwardIterator 2797a984708SDavid Chisnall rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last); 2807a984708SDavid Chisnall 2817a984708SDavid Chisnalltemplate <class ForwardIterator, class OutputIterator> 2827a984708SDavid Chisnall OutputIterator 2837a984708SDavid Chisnall rotate_copy(ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result); 2847a984708SDavid Chisnall 2857a984708SDavid Chisnalltemplate <class RandomAccessIterator> 2867a984708SDavid Chisnall void 287540d2a8bSDimitry Andric random_shuffle(RandomAccessIterator first, RandomAccessIterator last); // deprecated in C++14, removed in C++17 2887a984708SDavid Chisnall 2897a984708SDavid Chisnalltemplate <class RandomAccessIterator, class RandomNumberGenerator> 2907a984708SDavid Chisnall void 291d72607e9SDimitry Andric random_shuffle(RandomAccessIterator first, RandomAccessIterator last, 292540d2a8bSDimitry Andric RandomNumberGenerator& rand); // deprecated in C++14, removed in C++17 2937a984708SDavid Chisnall 294aed8d94eSDimitry Andrictemplate<class PopulationIterator, class SampleIterator, 295aed8d94eSDimitry Andric class Distance, class UniformRandomBitGenerator> 296aed8d94eSDimitry Andric SampleIterator sample(PopulationIterator first, PopulationIterator last, 297aed8d94eSDimitry Andric SampleIterator out, Distance n, 298aed8d94eSDimitry Andric UniformRandomBitGenerator&& g); // C++17 299aed8d94eSDimitry Andric 3007a984708SDavid Chisnalltemplate<class RandomAccessIterator, class UniformRandomNumberGenerator> 3017a984708SDavid Chisnall void shuffle(RandomAccessIterator first, RandomAccessIterator last, 3027a984708SDavid Chisnall UniformRandomNumberGenerator&& g); 3037a984708SDavid Chisnall 3047a984708SDavid Chisnalltemplate <class InputIterator, class Predicate> 3057a984708SDavid Chisnall bool 3067a984708SDavid Chisnall is_partitioned(InputIterator first, InputIterator last, Predicate pred); 3077a984708SDavid Chisnall 3087a984708SDavid Chisnalltemplate <class ForwardIterator, class Predicate> 3097a984708SDavid Chisnall ForwardIterator 3107a984708SDavid Chisnall partition(ForwardIterator first, ForwardIterator last, Predicate pred); 3117a984708SDavid Chisnall 3127a984708SDavid Chisnalltemplate <class InputIterator, class OutputIterator1, 3137a984708SDavid Chisnall class OutputIterator2, class Predicate> 3147a984708SDavid Chisnall pair<OutputIterator1, OutputIterator2> 3157a984708SDavid Chisnall partition_copy(InputIterator first, InputIterator last, 3167a984708SDavid Chisnall OutputIterator1 out_true, OutputIterator2 out_false, 3177a984708SDavid Chisnall Predicate pred); 3187a984708SDavid Chisnall 3197a984708SDavid Chisnalltemplate <class ForwardIterator, class Predicate> 3207a984708SDavid Chisnall ForwardIterator 3217a984708SDavid Chisnall stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred); 3227a984708SDavid Chisnall 3237a984708SDavid Chisnalltemplate<class ForwardIterator, class Predicate> 3247a984708SDavid Chisnall ForwardIterator 3257a984708SDavid Chisnall partition_point(ForwardIterator first, ForwardIterator last, Predicate pred); 3267a984708SDavid Chisnall 3277a984708SDavid Chisnalltemplate <class ForwardIterator> 3287a984708SDavid Chisnall bool 3297a984708SDavid Chisnall is_sorted(ForwardIterator first, ForwardIterator last); 3307a984708SDavid Chisnall 3317a984708SDavid Chisnalltemplate <class ForwardIterator, class Compare> 3327a984708SDavid Chisnall bool 3337a984708SDavid Chisnall is_sorted(ForwardIterator first, ForwardIterator last, Compare comp); 3347a984708SDavid Chisnall 3357a984708SDavid Chisnalltemplate<class ForwardIterator> 3367a984708SDavid Chisnall ForwardIterator 3377a984708SDavid Chisnall is_sorted_until(ForwardIterator first, ForwardIterator last); 3387a984708SDavid Chisnall 3397a984708SDavid Chisnalltemplate <class ForwardIterator, class Compare> 3407a984708SDavid Chisnall ForwardIterator 3417a984708SDavid Chisnall is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp); 3427a984708SDavid Chisnall 3437a984708SDavid Chisnalltemplate <class RandomAccessIterator> 3447a984708SDavid Chisnall void 3457a984708SDavid Chisnall sort(RandomAccessIterator first, RandomAccessIterator last); 3467a984708SDavid Chisnall 3477a984708SDavid Chisnalltemplate <class RandomAccessIterator, class Compare> 3487a984708SDavid Chisnall void 3497a984708SDavid Chisnall sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp); 3507a984708SDavid Chisnall 3517a984708SDavid Chisnalltemplate <class RandomAccessIterator> 3527a984708SDavid Chisnall void 3537a984708SDavid Chisnall stable_sort(RandomAccessIterator first, RandomAccessIterator last); 3547a984708SDavid Chisnall 3557a984708SDavid Chisnalltemplate <class RandomAccessIterator, class Compare> 3567a984708SDavid Chisnall void 3577a984708SDavid Chisnall stable_sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp); 3587a984708SDavid Chisnall 3597a984708SDavid Chisnalltemplate <class RandomAccessIterator> 3607a984708SDavid Chisnall void 3617a984708SDavid Chisnall partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last); 3627a984708SDavid Chisnall 3637a984708SDavid Chisnalltemplate <class RandomAccessIterator, class Compare> 3647a984708SDavid Chisnall void 3657a984708SDavid Chisnall partial_sort(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp); 3667a984708SDavid Chisnall 3677a984708SDavid Chisnalltemplate <class InputIterator, class RandomAccessIterator> 3687a984708SDavid Chisnall RandomAccessIterator 3697a984708SDavid Chisnall partial_sort_copy(InputIterator first, InputIterator last, 3707a984708SDavid Chisnall RandomAccessIterator result_first, RandomAccessIterator result_last); 3717a984708SDavid Chisnall 3727a984708SDavid Chisnalltemplate <class InputIterator, class RandomAccessIterator, class Compare> 3737a984708SDavid Chisnall RandomAccessIterator 3747a984708SDavid Chisnall partial_sort_copy(InputIterator first, InputIterator last, 3757a984708SDavid Chisnall RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp); 3767a984708SDavid Chisnall 3777a984708SDavid Chisnalltemplate <class RandomAccessIterator> 3787a984708SDavid Chisnall void 3797a984708SDavid Chisnall nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last); 3807a984708SDavid Chisnall 3817a984708SDavid Chisnalltemplate <class RandomAccessIterator, class Compare> 3827a984708SDavid Chisnall void 3837a984708SDavid Chisnall nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp); 3847a984708SDavid Chisnall 3857a984708SDavid Chisnalltemplate <class ForwardIterator, class T> 3867a984708SDavid Chisnall ForwardIterator 3877a984708SDavid Chisnall lower_bound(ForwardIterator first, ForwardIterator last, const T& value); 3887a984708SDavid Chisnall 3897a984708SDavid Chisnalltemplate <class ForwardIterator, class T, class Compare> 3907a984708SDavid Chisnall ForwardIterator 3917a984708SDavid Chisnall lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp); 3927a984708SDavid Chisnall 3937a984708SDavid Chisnalltemplate <class ForwardIterator, class T> 3947a984708SDavid Chisnall ForwardIterator 3957a984708SDavid Chisnall upper_bound(ForwardIterator first, ForwardIterator last, const T& value); 3967a984708SDavid Chisnall 3977a984708SDavid Chisnalltemplate <class ForwardIterator, class T, class Compare> 3987a984708SDavid Chisnall ForwardIterator 3997a984708SDavid Chisnall upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp); 4007a984708SDavid Chisnall 4017a984708SDavid Chisnalltemplate <class ForwardIterator, class T> 4027a984708SDavid Chisnall pair<ForwardIterator, ForwardIterator> 4037a984708SDavid Chisnall equal_range(ForwardIterator first, ForwardIterator last, const T& value); 4047a984708SDavid Chisnall 4057a984708SDavid Chisnalltemplate <class ForwardIterator, class T, class Compare> 4067a984708SDavid Chisnall pair<ForwardIterator, ForwardIterator> 4077a984708SDavid Chisnall equal_range(ForwardIterator first, ForwardIterator last, const T& value, Compare comp); 4087a984708SDavid Chisnall 4097a984708SDavid Chisnalltemplate <class ForwardIterator, class T> 4107a984708SDavid Chisnall bool 4117a984708SDavid Chisnall binary_search(ForwardIterator first, ForwardIterator last, const T& value); 4127a984708SDavid Chisnall 4137a984708SDavid Chisnalltemplate <class ForwardIterator, class T, class Compare> 4147a984708SDavid Chisnall bool 4157a984708SDavid Chisnall binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp); 4167a984708SDavid Chisnall 4177a984708SDavid Chisnalltemplate <class InputIterator1, class InputIterator2, class OutputIterator> 4187a984708SDavid Chisnall OutputIterator 4197a984708SDavid Chisnall merge(InputIterator1 first1, InputIterator1 last1, 4207a984708SDavid Chisnall InputIterator2 first2, InputIterator2 last2, OutputIterator result); 4217a984708SDavid Chisnall 4227a984708SDavid Chisnalltemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> 4237a984708SDavid Chisnall OutputIterator 4247a984708SDavid Chisnall merge(InputIterator1 first1, InputIterator1 last1, 4257a984708SDavid Chisnall InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); 4267a984708SDavid Chisnall 4277a984708SDavid Chisnalltemplate <class BidirectionalIterator> 4287a984708SDavid Chisnall void 4297a984708SDavid Chisnall inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last); 4307a984708SDavid Chisnall 4317a984708SDavid Chisnalltemplate <class BidirectionalIterator, class Compare> 4327a984708SDavid Chisnall void 4337a984708SDavid Chisnall inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp); 4347a984708SDavid Chisnall 4357a984708SDavid Chisnalltemplate <class InputIterator1, class InputIterator2> 4367a984708SDavid Chisnall bool 4377a984708SDavid Chisnall includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2); 4387a984708SDavid Chisnall 4397a984708SDavid Chisnalltemplate <class InputIterator1, class InputIterator2, class Compare> 4407a984708SDavid Chisnall bool 4417a984708SDavid Chisnall includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp); 4427a984708SDavid Chisnall 4437a984708SDavid Chisnalltemplate <class InputIterator1, class InputIterator2, class OutputIterator> 4447a984708SDavid Chisnall OutputIterator 4457a984708SDavid Chisnall set_union(InputIterator1 first1, InputIterator1 last1, 4467a984708SDavid Chisnall InputIterator2 first2, InputIterator2 last2, OutputIterator result); 4477a984708SDavid Chisnall 4487a984708SDavid Chisnalltemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> 4497a984708SDavid Chisnall OutputIterator 4507a984708SDavid Chisnall set_union(InputIterator1 first1, InputIterator1 last1, 4517a984708SDavid Chisnall InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); 4527a984708SDavid Chisnall 4537a984708SDavid Chisnalltemplate <class InputIterator1, class InputIterator2, class OutputIterator> 4547a984708SDavid Chisnall OutputIterator 4557a984708SDavid Chisnall set_intersection(InputIterator1 first1, InputIterator1 last1, 4567a984708SDavid Chisnall InputIterator2 first2, InputIterator2 last2, OutputIterator result); 4577a984708SDavid Chisnall 4587a984708SDavid Chisnalltemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> 4597a984708SDavid Chisnall OutputIterator 4607a984708SDavid Chisnall set_intersection(InputIterator1 first1, InputIterator1 last1, 4617a984708SDavid Chisnall InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); 4627a984708SDavid Chisnall 4637a984708SDavid Chisnalltemplate <class InputIterator1, class InputIterator2, class OutputIterator> 4647a984708SDavid Chisnall OutputIterator 4657a984708SDavid Chisnall set_difference(InputIterator1 first1, InputIterator1 last1, 4667a984708SDavid Chisnall InputIterator2 first2, InputIterator2 last2, OutputIterator result); 4677a984708SDavid Chisnall 4687a984708SDavid Chisnalltemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> 4697a984708SDavid Chisnall OutputIterator 4707a984708SDavid Chisnall set_difference(InputIterator1 first1, InputIterator1 last1, 4717a984708SDavid Chisnall InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); 4727a984708SDavid Chisnall 4737a984708SDavid Chisnalltemplate <class InputIterator1, class InputIterator2, class OutputIterator> 4747a984708SDavid Chisnall OutputIterator 4757a984708SDavid Chisnall set_symmetric_difference(InputIterator1 first1, InputIterator1 last1, 4767a984708SDavid Chisnall InputIterator2 first2, InputIterator2 last2, OutputIterator result); 4777a984708SDavid Chisnall 4787a984708SDavid Chisnalltemplate <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> 4797a984708SDavid Chisnall OutputIterator 4807a984708SDavid Chisnall set_symmetric_difference(InputIterator1 first1, InputIterator1 last1, 4817a984708SDavid Chisnall InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); 4827a984708SDavid Chisnall 4837a984708SDavid Chisnalltemplate <class RandomAccessIterator> 4847a984708SDavid Chisnall void 4857a984708SDavid Chisnall push_heap(RandomAccessIterator first, RandomAccessIterator last); 4867a984708SDavid Chisnall 4877a984708SDavid Chisnalltemplate <class RandomAccessIterator, class Compare> 4887a984708SDavid Chisnall void 4897a984708SDavid Chisnall push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp); 4907a984708SDavid Chisnall 4917a984708SDavid Chisnalltemplate <class RandomAccessIterator> 4927a984708SDavid Chisnall void 4937a984708SDavid Chisnall pop_heap(RandomAccessIterator first, RandomAccessIterator last); 4947a984708SDavid Chisnall 4957a984708SDavid Chisnalltemplate <class RandomAccessIterator, class Compare> 4967a984708SDavid Chisnall void 4977a984708SDavid Chisnall pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp); 4987a984708SDavid Chisnall 4997a984708SDavid Chisnalltemplate <class RandomAccessIterator> 5007a984708SDavid Chisnall void 5017a984708SDavid Chisnall make_heap(RandomAccessIterator first, RandomAccessIterator last); 5027a984708SDavid Chisnall 5037a984708SDavid Chisnalltemplate <class RandomAccessIterator, class Compare> 5047a984708SDavid Chisnall void 5057a984708SDavid Chisnall make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp); 5067a984708SDavid Chisnall 5077a984708SDavid Chisnalltemplate <class RandomAccessIterator> 5087a984708SDavid Chisnall void 5097a984708SDavid Chisnall sort_heap(RandomAccessIterator first, RandomAccessIterator last); 5107a984708SDavid Chisnall 5117a984708SDavid Chisnalltemplate <class RandomAccessIterator, class Compare> 5127a984708SDavid Chisnall void 5137a984708SDavid Chisnall sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp); 5147a984708SDavid Chisnall 5157a984708SDavid Chisnalltemplate <class RandomAccessIterator> 5167a984708SDavid Chisnall bool 5177a984708SDavid Chisnall is_heap(RandomAccessIterator first, RandomAccessiterator last); 5187a984708SDavid Chisnall 5197a984708SDavid Chisnalltemplate <class RandomAccessIterator, class Compare> 5207a984708SDavid Chisnall bool 5217a984708SDavid Chisnall is_heap(RandomAccessIterator first, RandomAccessiterator last, Compare comp); 5227a984708SDavid Chisnall 5237a984708SDavid Chisnalltemplate <class RandomAccessIterator> 5247a984708SDavid Chisnall RandomAccessIterator 5257a984708SDavid Chisnall is_heap_until(RandomAccessIterator first, RandomAccessiterator last); 5267a984708SDavid Chisnall 5277a984708SDavid Chisnalltemplate <class RandomAccessIterator, class Compare> 5287a984708SDavid Chisnall RandomAccessIterator 5297a984708SDavid Chisnall is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp); 5307a984708SDavid Chisnall 5317a984708SDavid Chisnalltemplate <class ForwardIterator> 5327a984708SDavid Chisnall ForwardIterator 533854fa44bSDimitry Andric min_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14 5347a984708SDavid Chisnall 5357a984708SDavid Chisnalltemplate <class ForwardIterator, class Compare> 5367a984708SDavid Chisnall ForwardIterator 537854fa44bSDimitry Andric min_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14 5387a984708SDavid Chisnall 5397a984708SDavid Chisnalltemplate <class T> 5407a984708SDavid Chisnall const T& 541d72607e9SDimitry Andric min(const T& a, const T& b); // constexpr in C++14 5427a984708SDavid Chisnall 5437a984708SDavid Chisnalltemplate <class T, class Compare> 5447a984708SDavid Chisnall const T& 545d72607e9SDimitry Andric min(const T& a, const T& b, Compare comp); // constexpr in C++14 5467a984708SDavid Chisnall 5477a984708SDavid Chisnalltemplate<class T> 5487a984708SDavid Chisnall T 549d72607e9SDimitry Andric min(initializer_list<T> t); // constexpr in C++14 5507a984708SDavid Chisnall 5517a984708SDavid Chisnalltemplate<class T, class Compare> 5527a984708SDavid Chisnall T 553d72607e9SDimitry Andric min(initializer_list<T> t, Compare comp); // constexpr in C++14 5547a984708SDavid Chisnall 5557c82a1ecSDimitry Andrictemplate<class T> 5567c82a1ecSDimitry Andric constexpr const T& clamp( const T& v, const T& lo, const T& hi ); // C++17 5577c82a1ecSDimitry Andric 5587c82a1ecSDimitry Andrictemplate<class T, class Compare> 5597c82a1ecSDimitry Andric constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp ); // C++17 5607c82a1ecSDimitry Andric 5617a984708SDavid Chisnalltemplate <class ForwardIterator> 5627a984708SDavid Chisnall ForwardIterator 563854fa44bSDimitry Andric max_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14 5647a984708SDavid Chisnall 5657a984708SDavid Chisnalltemplate <class ForwardIterator, class Compare> 5667a984708SDavid Chisnall ForwardIterator 567854fa44bSDimitry Andric max_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14 5687a984708SDavid Chisnall 5697a984708SDavid Chisnalltemplate <class T> 5707a984708SDavid Chisnall const T& 571d72607e9SDimitry Andric max(const T& a, const T& b); // constexpr in C++14 5727a984708SDavid Chisnall 5737a984708SDavid Chisnalltemplate <class T, class Compare> 5747a984708SDavid Chisnall const T& 575d72607e9SDimitry Andric max(const T& a, const T& b, Compare comp); // constexpr in C++14 5767a984708SDavid Chisnall 5777a984708SDavid Chisnalltemplate<class T> 5787a984708SDavid Chisnall T 579d72607e9SDimitry Andric max(initializer_list<T> t); // constexpr in C++14 5807a984708SDavid Chisnall 5817a984708SDavid Chisnalltemplate<class T, class Compare> 5827a984708SDavid Chisnall T 583d72607e9SDimitry Andric max(initializer_list<T> t, Compare comp); // constexpr in C++14 5847a984708SDavid Chisnall 5857a984708SDavid Chisnalltemplate<class ForwardIterator> 5867a984708SDavid Chisnall pair<ForwardIterator, ForwardIterator> 587854fa44bSDimitry Andric minmax_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14 5887a984708SDavid Chisnall 5897a984708SDavid Chisnalltemplate<class ForwardIterator, class Compare> 5907a984708SDavid Chisnall pair<ForwardIterator, ForwardIterator> 591854fa44bSDimitry Andric minmax_element(ForwardIterator first, ForwardIterator last, Compare comp); // constexpr in C++14 5927a984708SDavid Chisnall 5937a984708SDavid Chisnalltemplate<class T> 5947a984708SDavid Chisnall pair<const T&, const T&> 595d72607e9SDimitry Andric minmax(const T& a, const T& b); // constexpr in C++14 5967a984708SDavid Chisnall 5977a984708SDavid Chisnalltemplate<class T, class Compare> 5987a984708SDavid Chisnall pair<const T&, const T&> 599d72607e9SDimitry Andric minmax(const T& a, const T& b, Compare comp); // constexpr in C++14 6007a984708SDavid Chisnall 6017a984708SDavid Chisnalltemplate<class T> 6027a984708SDavid Chisnall pair<T, T> 603d72607e9SDimitry Andric minmax(initializer_list<T> t); // constexpr in C++14 6047a984708SDavid Chisnall 6057a984708SDavid Chisnalltemplate<class T, class Compare> 6067a984708SDavid Chisnall pair<T, T> 607d72607e9SDimitry Andric minmax(initializer_list<T> t, Compare comp); // constexpr in C++14 6087a984708SDavid Chisnall 6097a984708SDavid Chisnalltemplate <class InputIterator1, class InputIterator2> 6107a984708SDavid Chisnall bool 6117a984708SDavid Chisnall lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2); 6127a984708SDavid Chisnall 6137a984708SDavid Chisnalltemplate <class InputIterator1, class InputIterator2, class Compare> 6147a984708SDavid Chisnall bool 6157a984708SDavid Chisnall lexicographical_compare(InputIterator1 first1, InputIterator1 last1, 6167a984708SDavid Chisnall InputIterator2 first2, InputIterator2 last2, Compare comp); 6177a984708SDavid Chisnall 6187a984708SDavid Chisnalltemplate <class BidirectionalIterator> 6197a984708SDavid Chisnall bool 6207a984708SDavid Chisnall next_permutation(BidirectionalIterator first, BidirectionalIterator last); 6217a984708SDavid Chisnall 6227a984708SDavid Chisnalltemplate <class BidirectionalIterator, class Compare> 6237a984708SDavid Chisnall bool 6247a984708SDavid Chisnall next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp); 6257a984708SDavid Chisnall 6267a984708SDavid Chisnalltemplate <class BidirectionalIterator> 6277a984708SDavid Chisnall bool 6287a984708SDavid Chisnall prev_permutation(BidirectionalIterator first, BidirectionalIterator last); 6297a984708SDavid Chisnall 6307a984708SDavid Chisnalltemplate <class BidirectionalIterator, class Compare> 6317a984708SDavid Chisnall bool 6327a984708SDavid Chisnall prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp); 6337a984708SDavid Chisnall 6347a984708SDavid Chisnall} // std 6357a984708SDavid Chisnall 6367a984708SDavid Chisnall*/ 6377a984708SDavid Chisnall 6387a984708SDavid Chisnall#include <__config> 6397a984708SDavid Chisnall#include <initializer_list> 6407a984708SDavid Chisnall#include <type_traits> 6417a984708SDavid Chisnall#include <cstring> 6427c82a1ecSDimitry Andric#include <utility> // needed to provide swap_ranges. 6437a984708SDavid Chisnall#include <memory> 6447a984708SDavid Chisnall#include <iterator> 645936e9439SDimitry Andric#include <cstddef> 6467a984708SDavid Chisnall 6474f7ab58eSDimitry Andric#if defined(__IBMCPP__) 6484f7ab58eSDimitry Andric#include "support/ibm/support.h" 6494f7ab58eSDimitry Andric#endif 6505517e702SDimitry Andric#if defined(_LIBCPP_COMPILER_MSVC) 6515517e702SDimitry Andric#include <intrin.h> 6524f7ab58eSDimitry Andric#endif 6534f7ab58eSDimitry Andric 65494e3ee44SDavid Chisnall#include <__undef_min_max> 65594e3ee44SDavid Chisnall 656d72607e9SDimitry Andric#include <__debug> 657d72607e9SDimitry Andric 6587a984708SDavid Chisnall#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 6597a984708SDavid Chisnall#pragma GCC system_header 6607a984708SDavid Chisnall#endif 6617a984708SDavid Chisnall 6627a984708SDavid Chisnall_LIBCPP_BEGIN_NAMESPACE_STD 6637a984708SDavid Chisnall 664d72607e9SDimitry Andric// I'd like to replace these with _VSTD::equal_to<void>, but can't because: 665d72607e9SDimitry Andric// * That only works with C++14 and later, and 666d72607e9SDimitry Andric// * We haven't included <functional> here. 6677a984708SDavid Chisnalltemplate <class _T1, class _T2 = _T1> 6687a984708SDavid Chisnallstruct __equal_to 6697a984708SDavid Chisnall{ 6707a984708SDavid Chisnall _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;} 6717a984708SDavid Chisnall _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T2& __y) const {return __x == __y;} 6727a984708SDavid Chisnall _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T1& __y) const {return __x == __y;} 6737a984708SDavid Chisnall _LIBCPP_INLINE_VISIBILITY bool operator()(const _T2& __x, const _T2& __y) const {return __x == __y;} 6747a984708SDavid Chisnall}; 6757a984708SDavid Chisnall 6767a984708SDavid Chisnalltemplate <class _T1> 6777a984708SDavid Chisnallstruct __equal_to<_T1, _T1> 6787a984708SDavid Chisnall{ 679d72607e9SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 680d72607e9SDimitry Andric bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;} 6817a984708SDavid Chisnall}; 6827a984708SDavid Chisnall 6837a984708SDavid Chisnalltemplate <class _T1> 6847a984708SDavid Chisnallstruct __equal_to<const _T1, _T1> 6857a984708SDavid Chisnall{ 686d72607e9SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 687d72607e9SDimitry Andric bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;} 6887a984708SDavid Chisnall}; 6897a984708SDavid Chisnall 6907a984708SDavid Chisnalltemplate <class _T1> 6917a984708SDavid Chisnallstruct __equal_to<_T1, const _T1> 6927a984708SDavid Chisnall{ 693d72607e9SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 694d72607e9SDimitry Andric bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;} 6957a984708SDavid Chisnall}; 6967a984708SDavid Chisnall 6977a984708SDavid Chisnalltemplate <class _T1, class _T2 = _T1> 6987a984708SDavid Chisnallstruct __less 6997a984708SDavid Chisnall{ 700d72607e9SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 701d72607e9SDimitry Andric bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;} 702d72607e9SDimitry Andric 703d72607e9SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 704d72607e9SDimitry Andric bool operator()(const _T1& __x, const _T2& __y) const {return __x < __y;} 705d72607e9SDimitry Andric 706d72607e9SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 707d72607e9SDimitry Andric bool operator()(const _T2& __x, const _T1& __y) const {return __x < __y;} 708d72607e9SDimitry Andric 709d72607e9SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 710d72607e9SDimitry Andric bool operator()(const _T2& __x, const _T2& __y) const {return __x < __y;} 7117a984708SDavid Chisnall}; 7127a984708SDavid Chisnall 7137a984708SDavid Chisnalltemplate <class _T1> 7147a984708SDavid Chisnallstruct __less<_T1, _T1> 7157a984708SDavid Chisnall{ 716d72607e9SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 717d72607e9SDimitry Andric bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;} 7187a984708SDavid Chisnall}; 7197a984708SDavid Chisnall 7207a984708SDavid Chisnalltemplate <class _T1> 7217a984708SDavid Chisnallstruct __less<const _T1, _T1> 7227a984708SDavid Chisnall{ 723d72607e9SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 724d72607e9SDimitry Andric bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;} 7257a984708SDavid Chisnall}; 7267a984708SDavid Chisnall 7277a984708SDavid Chisnalltemplate <class _T1> 7287a984708SDavid Chisnallstruct __less<_T1, const _T1> 7297a984708SDavid Chisnall{ 730d72607e9SDimitry Andric _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 731d72607e9SDimitry Andric bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;} 7327a984708SDavid Chisnall}; 7337a984708SDavid Chisnall 7347a984708SDavid Chisnalltemplate <class _Predicate> 7357a984708SDavid Chisnallclass __negate 7367a984708SDavid Chisnall{ 7377a984708SDavid Chisnallprivate: 7387a984708SDavid Chisnall _Predicate __p_; 7397a984708SDavid Chisnallpublic: 7407a984708SDavid Chisnall _LIBCPP_INLINE_VISIBILITY __negate() {} 7417a984708SDavid Chisnall 7427a984708SDavid Chisnall _LIBCPP_INLINE_VISIBILITY 7437a984708SDavid Chisnall explicit __negate(_Predicate __p) : __p_(__p) {} 7447a984708SDavid Chisnall 7457a984708SDavid Chisnall template <class _T1> 7467a984708SDavid Chisnall _LIBCPP_INLINE_VISIBILITY 7477a984708SDavid Chisnall bool operator()(const _T1& __x) {return !__p_(__x);} 7487a984708SDavid Chisnall 7497a984708SDavid Chisnall template <class _T1, class _T2> 7507a984708SDavid Chisnall _LIBCPP_INLINE_VISIBILITY 7517a984708SDavid Chisnall bool operator()(const _T1& __x, const _T2& __y) {return !__p_(__x, __y);} 7527a984708SDavid Chisnall}; 7537a984708SDavid Chisnall 7544f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 7557a984708SDavid Chisnall 7567a984708SDavid Chisnalltemplate <class _Compare> 7577a984708SDavid Chisnallstruct __debug_less 7587a984708SDavid Chisnall{ 7597a984708SDavid Chisnall _Compare __comp_; 7607a984708SDavid Chisnall __debug_less(_Compare& __c) : __comp_(__c) {} 761aed8d94eSDimitry Andric 7627a984708SDavid Chisnall template <class _Tp, class _Up> 7637a984708SDavid Chisnall bool operator()(const _Tp& __x, const _Up& __y) 7647a984708SDavid Chisnall { 7657a984708SDavid Chisnall bool __r = __comp_(__x, __y); 7667a984708SDavid Chisnall if (__r) 767aed8d94eSDimitry Andric __do_compare_assert(0, __y, __x); 7687a984708SDavid Chisnall return __r; 7697a984708SDavid Chisnall } 770aed8d94eSDimitry Andric 771aed8d94eSDimitry Andric template <class _LHS, class _RHS> 772aed8d94eSDimitry Andric inline _LIBCPP_INLINE_VISIBILITY 773aed8d94eSDimitry Andric decltype((void)_VSTD::declval<_Compare&>()( 774aed8d94eSDimitry Andric _VSTD::declval<_LHS const&>(), _VSTD::declval<_RHS const&>())) 775aed8d94eSDimitry Andric __do_compare_assert(int, _LHS const& __l, _RHS const& __r) { 776aed8d94eSDimitry Andric _LIBCPP_ASSERT(!__comp_(__l, __r), 777aed8d94eSDimitry Andric "Comparator does not induce a strict weak ordering"); 778aed8d94eSDimitry Andric } 779aed8d94eSDimitry Andric 780aed8d94eSDimitry Andric template <class _LHS, class _RHS> 781aed8d94eSDimitry Andric inline _LIBCPP_INLINE_VISIBILITY 782aed8d94eSDimitry Andric void __do_compare_assert(long, _LHS const&, _RHS const&) {} 7837a984708SDavid Chisnall}; 7847a984708SDavid Chisnall 7854f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 7867a984708SDavid Chisnall 7877a984708SDavid Chisnall// Precondition: __x != 0 78894e3ee44SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 7895517e702SDimitry Andricunsigned __ctz(unsigned __x) { 7905517e702SDimitry Andric#ifndef _LIBCPP_COMPILER_MSVC 79194e3ee44SDavid Chisnall return static_cast<unsigned>(__builtin_ctz(__x)); 7925517e702SDimitry Andric#else 7935517e702SDimitry Andric static_assert(sizeof(unsigned) == sizeof(unsigned long), ""); 7945517e702SDimitry Andric static_assert(sizeof(unsigned long) == 4, ""); 7955517e702SDimitry Andric unsigned long where; 7965517e702SDimitry Andric // Search from LSB to MSB for first set bit. 7975517e702SDimitry Andric // Returns zero if no set bit is found. 7985517e702SDimitry Andric if (_BitScanForward(&where, mask)) 7995517e702SDimitry Andric return where; 8005517e702SDimitry Andric return 32; 8015517e702SDimitry Andric#endif 80294e3ee44SDavid Chisnall} 80394e3ee44SDavid Chisnall 80494e3ee44SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 8055517e702SDimitry Andricunsigned long __ctz(unsigned long __x) { 8065517e702SDimitry Andric#ifndef _LIBCPP_COMPILER_MSVC 80794e3ee44SDavid Chisnall return static_cast<unsigned long>(__builtin_ctzl(__x)); 8085517e702SDimitry Andric#else 8095517e702SDimitry Andric static_assert(sizeof(unsigned long) == sizeof(unsigned), ""); 8105517e702SDimitry Andric return __ctz(static_cast<unsigned>(__x)); 8115517e702SDimitry Andric#endif 81294e3ee44SDavid Chisnall} 81394e3ee44SDavid Chisnall 81494e3ee44SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 8155517e702SDimitry Andricunsigned long long __ctz(unsigned long long __x) { 8165517e702SDimitry Andric#ifndef _LIBCPP_COMPILER_MSVC 81794e3ee44SDavid Chisnall return static_cast<unsigned long long>(__builtin_ctzll(__x)); 8185517e702SDimitry Andric#else 8195517e702SDimitry Andric unsigned long where; 8205517e702SDimitry Andric// Search from LSB to MSB for first set bit. 8215517e702SDimitry Andric// Returns zero if no set bit is found. 8225517e702SDimitry Andric#if defined(_LIBCPP_HAS_BITSCAN64) 8235517e702SDimitry Andric (defined(_M_AMD64) || defined(__x86_64__)) 8245517e702SDimitry Andric if (_BitScanForward64(&where, mask)) 8255517e702SDimitry Andric return static_cast<int>(where); 8265517e702SDimitry Andric#else 8275517e702SDimitry Andric // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls. 8285517e702SDimitry Andric // Scan the Low Word. 8295517e702SDimitry Andric if (_BitScanForward(&where, static_cast<unsigned long>(mask))) 8305517e702SDimitry Andric return where; 8315517e702SDimitry Andric // Scan the High Word. 8325517e702SDimitry Andric if (_BitScanForward(&where, static_cast<unsigned long>(mask >> 32))) 8335517e702SDimitry Andric return where + 32; // Create a bit offset from the LSB. 8345517e702SDimitry Andric#endif 8355517e702SDimitry Andric return 64; 8365517e702SDimitry Andric#endif // _LIBCPP_COMPILER_MSVC 83794e3ee44SDavid Chisnall} 8387a984708SDavid Chisnall 8397a984708SDavid Chisnall// Precondition: __x != 0 84094e3ee44SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 8415517e702SDimitry Andricunsigned __clz(unsigned __x) { 8425517e702SDimitry Andric#ifndef _LIBCPP_COMPILER_MSVC 84394e3ee44SDavid Chisnall return static_cast<unsigned>(__builtin_clz(__x)); 8445517e702SDimitry Andric#else 8455517e702SDimitry Andric static_assert(sizeof(unsigned) == sizeof(unsigned long), ""); 8465517e702SDimitry Andric static_assert(sizeof(unsigned long) == 4, ""); 8475517e702SDimitry Andric unsigned long where; 8485517e702SDimitry Andric // Search from LSB to MSB for first set bit. 8495517e702SDimitry Andric // Returns zero if no set bit is found. 8505517e702SDimitry Andric if (_BitScanReverse(&where, mask)) 8515517e702SDimitry Andric return 31 - where; 8525517e702SDimitry Andric return 32; // Undefined Behavior. 8535517e702SDimitry Andric#endif 85494e3ee44SDavid Chisnall} 85594e3ee44SDavid Chisnall 85694e3ee44SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 8575517e702SDimitry Andricunsigned long __clz(unsigned long __x) { 8585517e702SDimitry Andric#ifndef _LIBCPP_COMPILER_MSVC 85994e3ee44SDavid Chisnall return static_cast<unsigned long>(__builtin_clzl (__x)); 8605517e702SDimitry Andric#else 8615517e702SDimitry Andric static_assert(sizeof(unsigned) == sizeof(unsigned long), ""); 8625517e702SDimitry Andric return __clz(static_cast<unsigned>(__x)); 8635517e702SDimitry Andric#endif 86494e3ee44SDavid Chisnall} 86594e3ee44SDavid Chisnall 86694e3ee44SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 8675517e702SDimitry Andricunsigned long long __clz(unsigned long long __x) { 8685517e702SDimitry Andric#ifndef _LIBCPP_COMPILER_MSVC 86994e3ee44SDavid Chisnall return static_cast<unsigned long long>(__builtin_clzll(__x)); 8705517e702SDimitry Andric#else 8715517e702SDimitry Andric unsigned long where; 8725517e702SDimitry Andric// BitScanReverse scans from MSB to LSB for first set bit. 8735517e702SDimitry Andric// Returns 0 if no set bit is found. 8745517e702SDimitry Andric#if defined(_LIBCPP_HAS_BITSCAN64) 8755517e702SDimitry Andric if (_BitScanReverse64(&where, mask)) 8765517e702SDimitry Andric return static_cast<int>(63 - where); 8775517e702SDimitry Andric#else 8785517e702SDimitry Andric // Scan the high 32 bits. 8795517e702SDimitry Andric if (_BitScanReverse(&where, static_cast<unsigned long>(mask >> 32))) 8805517e702SDimitry Andric return 63 - (where + 32); // Create a bit offset from the MSB. 8815517e702SDimitry Andric // Scan the low 32 bits. 8825517e702SDimitry Andric if (_BitScanReverse(&where, static_cast<unsigned long>(mask))) 8835517e702SDimitry Andric return 63 - where; 8845517e702SDimitry Andric#endif 8855517e702SDimitry Andric return 64; // Undefined Behavior. 8865517e702SDimitry Andric#endif // _LIBCPP_COMPILER_MSVC 88794e3ee44SDavid Chisnall} 8887a984708SDavid Chisnall 8895517e702SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned __x) { 8905517e702SDimitry Andric#ifndef _LIBCPP_COMPILER_MSVC 8915517e702SDimitry Andric return __builtin_popcount (__x); 8925517e702SDimitry Andric#else 8935517e702SDimitry Andric static_assert(sizeof(unsigned) == 4, ""); 8945517e702SDimitry Andric return __popcnt(__x); 8955517e702SDimitry Andric#endif 8965517e702SDimitry Andric} 8975517e702SDimitry Andric 8985517e702SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long __x) { 8995517e702SDimitry Andric#ifndef _LIBCPP_COMPILER_MSVC 9005517e702SDimitry Andric return __builtin_popcountl (__x); 9015517e702SDimitry Andric#else 9025517e702SDimitry Andric static_assert(sizeof(unsigned long) == 4, ""); 9035517e702SDimitry Andric return __popcnt(__x); 9045517e702SDimitry Andric#endif 9055517e702SDimitry Andric} 9065517e702SDimitry Andric 9075517e702SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) { 9085517e702SDimitry Andric#ifndef _LIBCPP_COMPILER_MSVC 9095517e702SDimitry Andric return __builtin_popcountll(__x); 9105517e702SDimitry Andric#else 9115517e702SDimitry Andric static_assert(sizeof(unsigned long long) == 8, ""); 9125517e702SDimitry Andric return __popcnt64(__x); 9135517e702SDimitry Andric#endif 9145517e702SDimitry Andric} 9157a984708SDavid Chisnall 9167a984708SDavid Chisnall// all_of 9177a984708SDavid Chisnall 9187a984708SDavid Chisnalltemplate <class _InputIterator, class _Predicate> 9197a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 9207a984708SDavid Chisnallbool 9217a984708SDavid Chisnallall_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) 9227a984708SDavid Chisnall{ 9237a984708SDavid Chisnall for (; __first != __last; ++__first) 9247a984708SDavid Chisnall if (!__pred(*__first)) 9257a984708SDavid Chisnall return false; 9267a984708SDavid Chisnall return true; 9277a984708SDavid Chisnall} 9287a984708SDavid Chisnall 9297a984708SDavid Chisnall// any_of 9307a984708SDavid Chisnall 9317a984708SDavid Chisnalltemplate <class _InputIterator, class _Predicate> 9327a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 9337a984708SDavid Chisnallbool 9347a984708SDavid Chisnallany_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) 9357a984708SDavid Chisnall{ 9367a984708SDavid Chisnall for (; __first != __last; ++__first) 9377a984708SDavid Chisnall if (__pred(*__first)) 9387a984708SDavid Chisnall return true; 9397a984708SDavid Chisnall return false; 9407a984708SDavid Chisnall} 9417a984708SDavid Chisnall 9427a984708SDavid Chisnall// none_of 9437a984708SDavid Chisnall 9447a984708SDavid Chisnalltemplate <class _InputIterator, class _Predicate> 9457a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 9467a984708SDavid Chisnallbool 9477a984708SDavid Chisnallnone_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) 9487a984708SDavid Chisnall{ 9497a984708SDavid Chisnall for (; __first != __last; ++__first) 9507a984708SDavid Chisnall if (__pred(*__first)) 9517a984708SDavid Chisnall return false; 9527a984708SDavid Chisnall return true; 9537a984708SDavid Chisnall} 9547a984708SDavid Chisnall 9557a984708SDavid Chisnall// for_each 9567a984708SDavid Chisnall 9577a984708SDavid Chisnalltemplate <class _InputIterator, class _Function> 9587a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 9597a984708SDavid Chisnall_Function 9607a984708SDavid Chisnallfor_each(_InputIterator __first, _InputIterator __last, _Function __f) 9617a984708SDavid Chisnall{ 9627a984708SDavid Chisnall for (; __first != __last; ++__first) 9637a984708SDavid Chisnall __f(*__first); 964aed8d94eSDimitry Andric return __f; 9657a984708SDavid Chisnall} 9667a984708SDavid Chisnall 967302affcbSDimitry Andric#if _LIBCPP_STD_VER > 14 968302affcbSDimitry Andric// for_each_n 969302affcbSDimitry Andric 970302affcbSDimitry Andrictemplate <class _InputIterator, class _Size, class _Function> 971302affcbSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 972302affcbSDimitry Andric_InputIterator 973302affcbSDimitry Andricfor_each_n(_InputIterator __first, _Size __orig_n, _Function __f) 974302affcbSDimitry Andric{ 975302affcbSDimitry Andric typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize; 976302affcbSDimitry Andric _IntegralSize __n = __orig_n; 977302affcbSDimitry Andric while (__n > 0) 978302affcbSDimitry Andric { 979302affcbSDimitry Andric __f(*__first); 980302affcbSDimitry Andric ++__first; 981302affcbSDimitry Andric --__n; 982302affcbSDimitry Andric } 983302affcbSDimitry Andric return __first; 984302affcbSDimitry Andric} 985302affcbSDimitry Andric#endif 986302affcbSDimitry Andric 9877a984708SDavid Chisnall// find 9887a984708SDavid Chisnall 9897a984708SDavid Chisnalltemplate <class _InputIterator, class _Tp> 9907a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 9917a984708SDavid Chisnall_InputIterator 9927a984708SDavid Chisnallfind(_InputIterator __first, _InputIterator __last, const _Tp& __value_) 9937a984708SDavid Chisnall{ 9947a984708SDavid Chisnall for (; __first != __last; ++__first) 9957a984708SDavid Chisnall if (*__first == __value_) 9967a984708SDavid Chisnall break; 9977a984708SDavid Chisnall return __first; 9987a984708SDavid Chisnall} 9997a984708SDavid Chisnall 10007a984708SDavid Chisnall// find_if 10017a984708SDavid Chisnall 10027a984708SDavid Chisnalltemplate <class _InputIterator, class _Predicate> 10037a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 10047a984708SDavid Chisnall_InputIterator 10057a984708SDavid Chisnallfind_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) 10067a984708SDavid Chisnall{ 10077a984708SDavid Chisnall for (; __first != __last; ++__first) 10087a984708SDavid Chisnall if (__pred(*__first)) 10097a984708SDavid Chisnall break; 10107a984708SDavid Chisnall return __first; 10117a984708SDavid Chisnall} 10127a984708SDavid Chisnall 10137a984708SDavid Chisnall// find_if_not 10147a984708SDavid Chisnall 10157a984708SDavid Chisnalltemplate<class _InputIterator, class _Predicate> 10167a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 10177a984708SDavid Chisnall_InputIterator 10187a984708SDavid Chisnallfind_if_not(_InputIterator __first, _InputIterator __last, _Predicate __pred) 10197a984708SDavid Chisnall{ 10207a984708SDavid Chisnall for (; __first != __last; ++__first) 10217a984708SDavid Chisnall if (!__pred(*__first)) 10227a984708SDavid Chisnall break; 10237a984708SDavid Chisnall return __first; 10247a984708SDavid Chisnall} 10257a984708SDavid Chisnall 10267a984708SDavid Chisnall// find_end 10277a984708SDavid Chisnall 10287a984708SDavid Chisnalltemplate <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2> 10297a984708SDavid Chisnall_ForwardIterator1 10307a984708SDavid Chisnall__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 10317a984708SDavid Chisnall _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred, 10327a984708SDavid Chisnall forward_iterator_tag, forward_iterator_tag) 10337a984708SDavid Chisnall{ 10347a984708SDavid Chisnall // modeled after search algorithm 10357a984708SDavid Chisnall _ForwardIterator1 __r = __last1; // __last1 is the "default" answer 10367a984708SDavid Chisnall if (__first2 == __last2) 10377a984708SDavid Chisnall return __r; 10387a984708SDavid Chisnall while (true) 10397a984708SDavid Chisnall { 10407a984708SDavid Chisnall while (true) 10417a984708SDavid Chisnall { 10427a984708SDavid Chisnall if (__first1 == __last1) // if source exhausted return last correct answer 10437a984708SDavid Chisnall return __r; // (or __last1 if never found) 10447a984708SDavid Chisnall if (__pred(*__first1, *__first2)) 10457a984708SDavid Chisnall break; 10467a984708SDavid Chisnall ++__first1; 10477a984708SDavid Chisnall } 10487a984708SDavid Chisnall // *__first1 matches *__first2, now match elements after here 10497a984708SDavid Chisnall _ForwardIterator1 __m1 = __first1; 10507a984708SDavid Chisnall _ForwardIterator2 __m2 = __first2; 10517a984708SDavid Chisnall while (true) 10527a984708SDavid Chisnall { 10537a984708SDavid Chisnall if (++__m2 == __last2) 10547a984708SDavid Chisnall { // Pattern exhaused, record answer and search for another one 10557a984708SDavid Chisnall __r = __first1; 10567a984708SDavid Chisnall ++__first1; 10577a984708SDavid Chisnall break; 10587a984708SDavid Chisnall } 10597a984708SDavid Chisnall if (++__m1 == __last1) // Source exhausted, return last answer 10607a984708SDavid Chisnall return __r; 10617a984708SDavid Chisnall if (!__pred(*__m1, *__m2)) // mismatch, restart with a new __first 10627a984708SDavid Chisnall { 10637a984708SDavid Chisnall ++__first1; 10647a984708SDavid Chisnall break; 10657a984708SDavid Chisnall } // else there is a match, check next elements 10667a984708SDavid Chisnall } 10677a984708SDavid Chisnall } 10687a984708SDavid Chisnall} 10697a984708SDavid Chisnall 10707a984708SDavid Chisnalltemplate <class _BinaryPredicate, class _BidirectionalIterator1, class _BidirectionalIterator2> 10717a984708SDavid Chisnall_BidirectionalIterator1 10727a984708SDavid Chisnall__find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1, 10737a984708SDavid Chisnall _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2, _BinaryPredicate __pred, 10747a984708SDavid Chisnall bidirectional_iterator_tag, bidirectional_iterator_tag) 10757a984708SDavid Chisnall{ 10767a984708SDavid Chisnall // modeled after search algorithm (in reverse) 10777a984708SDavid Chisnall if (__first2 == __last2) 10787a984708SDavid Chisnall return __last1; // Everything matches an empty sequence 10797a984708SDavid Chisnall _BidirectionalIterator1 __l1 = __last1; 10807a984708SDavid Chisnall _BidirectionalIterator2 __l2 = __last2; 10817a984708SDavid Chisnall --__l2; 10827a984708SDavid Chisnall while (true) 10837a984708SDavid Chisnall { 10847a984708SDavid Chisnall // Find last element in sequence 1 that matchs *(__last2-1), with a mininum of loop checks 10857a984708SDavid Chisnall while (true) 10867a984708SDavid Chisnall { 10877a984708SDavid Chisnall if (__first1 == __l1) // return __last1 if no element matches *__first2 10887a984708SDavid Chisnall return __last1; 10897a984708SDavid Chisnall if (__pred(*--__l1, *__l2)) 10907a984708SDavid Chisnall break; 10917a984708SDavid Chisnall } 10927a984708SDavid Chisnall // *__l1 matches *__l2, now match elements before here 10937a984708SDavid Chisnall _BidirectionalIterator1 __m1 = __l1; 10947a984708SDavid Chisnall _BidirectionalIterator2 __m2 = __l2; 10957a984708SDavid Chisnall while (true) 10967a984708SDavid Chisnall { 10977a984708SDavid Chisnall if (__m2 == __first2) // If pattern exhausted, __m1 is the answer (works for 1 element pattern) 10987a984708SDavid Chisnall return __m1; 10997a984708SDavid Chisnall if (__m1 == __first1) // Otherwise if source exhaused, pattern not found 11007a984708SDavid Chisnall return __last1; 11017a984708SDavid Chisnall if (!__pred(*--__m1, *--__m2)) // if there is a mismatch, restart with a new __l1 11027a984708SDavid Chisnall { 11037a984708SDavid Chisnall break; 11047a984708SDavid Chisnall } // else there is a match, check next elements 11057a984708SDavid Chisnall } 11067a984708SDavid Chisnall } 11077a984708SDavid Chisnall} 11087a984708SDavid Chisnall 11097a984708SDavid Chisnalltemplate <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2> 1110d72607e9SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1 11117a984708SDavid Chisnall__find_end(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, 11127a984708SDavid Chisnall _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred, 11137a984708SDavid Chisnall random_access_iterator_tag, random_access_iterator_tag) 11147a984708SDavid Chisnall{ 11157a984708SDavid Chisnall // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern 11167a984708SDavid Chisnall typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2; 11177a984708SDavid Chisnall if (__len2 == 0) 11187a984708SDavid Chisnall return __last1; 11197a984708SDavid Chisnall typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1; 11207a984708SDavid Chisnall if (__len1 < __len2) 11217a984708SDavid Chisnall return __last1; 11227a984708SDavid Chisnall const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here 11237a984708SDavid Chisnall _RandomAccessIterator1 __l1 = __last1; 11247a984708SDavid Chisnall _RandomAccessIterator2 __l2 = __last2; 11257a984708SDavid Chisnall --__l2; 11267a984708SDavid Chisnall while (true) 11277a984708SDavid Chisnall { 11287a984708SDavid Chisnall while (true) 11297a984708SDavid Chisnall { 11307a984708SDavid Chisnall if (__s == __l1) 11317a984708SDavid Chisnall return __last1; 11327a984708SDavid Chisnall if (__pred(*--__l1, *__l2)) 11337a984708SDavid Chisnall break; 11347a984708SDavid Chisnall } 11357a984708SDavid Chisnall _RandomAccessIterator1 __m1 = __l1; 11367a984708SDavid Chisnall _RandomAccessIterator2 __m2 = __l2; 11377a984708SDavid Chisnall while (true) 11387a984708SDavid Chisnall { 11397a984708SDavid Chisnall if (__m2 == __first2) 11407a984708SDavid Chisnall return __m1; 11417a984708SDavid Chisnall // no need to check range on __m1 because __s guarantees we have enough source 11427a984708SDavid Chisnall if (!__pred(*--__m1, *--__m2)) 11437a984708SDavid Chisnall { 11447a984708SDavid Chisnall break; 11457a984708SDavid Chisnall } 11467a984708SDavid Chisnall } 11477a984708SDavid Chisnall } 11487a984708SDavid Chisnall} 11497a984708SDavid Chisnall 11507a984708SDavid Chisnalltemplate <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> 11517a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 11527a984708SDavid Chisnall_ForwardIterator1 11537a984708SDavid Chisnallfind_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 11547a984708SDavid Chisnall _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred) 11557a984708SDavid Chisnall{ 11567a984708SDavid Chisnall return _VSTD::__find_end<typename add_lvalue_reference<_BinaryPredicate>::type> 11577a984708SDavid Chisnall (__first1, __last1, __first2, __last2, __pred, 11587a984708SDavid Chisnall typename iterator_traits<_ForwardIterator1>::iterator_category(), 11597a984708SDavid Chisnall typename iterator_traits<_ForwardIterator2>::iterator_category()); 11607a984708SDavid Chisnall} 11617a984708SDavid Chisnall 11627a984708SDavid Chisnalltemplate <class _ForwardIterator1, class _ForwardIterator2> 11637a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 11647a984708SDavid Chisnall_ForwardIterator1 11657a984708SDavid Chisnallfind_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 11667a984708SDavid Chisnall _ForwardIterator2 __first2, _ForwardIterator2 __last2) 11677a984708SDavid Chisnall{ 11687a984708SDavid Chisnall typedef typename iterator_traits<_ForwardIterator1>::value_type __v1; 11697a984708SDavid Chisnall typedef typename iterator_traits<_ForwardIterator2>::value_type __v2; 11707a984708SDavid Chisnall return _VSTD::find_end(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>()); 11717a984708SDavid Chisnall} 11727a984708SDavid Chisnall 11737a984708SDavid Chisnall// find_first_of 11747a984708SDavid Chisnall 11757a984708SDavid Chisnalltemplate <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> 1176d72607e9SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1 1177d72607e9SDimitry Andric__find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 11787a984708SDavid Chisnall _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred) 11797a984708SDavid Chisnall{ 11807a984708SDavid Chisnall for (; __first1 != __last1; ++__first1) 11817a984708SDavid Chisnall for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j) 11827a984708SDavid Chisnall if (__pred(*__first1, *__j)) 11837a984708SDavid Chisnall return __first1; 11847a984708SDavid Chisnall return __last1; 11857a984708SDavid Chisnall} 11867a984708SDavid Chisnall 1187d72607e9SDimitry Andric 1188d72607e9SDimitry Andrictemplate <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> 1189d72607e9SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 1190d72607e9SDimitry Andric_ForwardIterator1 1191d72607e9SDimitry Andricfind_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 1192d72607e9SDimitry Andric _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred) 1193d72607e9SDimitry Andric{ 1194d72607e9SDimitry Andric return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __pred); 1195d72607e9SDimitry Andric} 1196d72607e9SDimitry Andric 11977a984708SDavid Chisnalltemplate <class _ForwardIterator1, class _ForwardIterator2> 11987a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 11997a984708SDavid Chisnall_ForwardIterator1 12007a984708SDavid Chisnallfind_first_of(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 12017a984708SDavid Chisnall _ForwardIterator2 __first2, _ForwardIterator2 __last2) 12027a984708SDavid Chisnall{ 12037a984708SDavid Chisnall typedef typename iterator_traits<_ForwardIterator1>::value_type __v1; 12047a984708SDavid Chisnall typedef typename iterator_traits<_ForwardIterator2>::value_type __v2; 1205d72607e9SDimitry Andric return _VSTD::__find_first_of_ce(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>()); 12067a984708SDavid Chisnall} 12077a984708SDavid Chisnall 12087a984708SDavid Chisnall// adjacent_find 12097a984708SDavid Chisnall 12107a984708SDavid Chisnalltemplate <class _ForwardIterator, class _BinaryPredicate> 12117a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 12127a984708SDavid Chisnall_ForwardIterator 12137a984708SDavid Chisnalladjacent_find(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred) 12147a984708SDavid Chisnall{ 12157a984708SDavid Chisnall if (__first != __last) 12167a984708SDavid Chisnall { 12177a984708SDavid Chisnall _ForwardIterator __i = __first; 12187a984708SDavid Chisnall while (++__i != __last) 12197a984708SDavid Chisnall { 12207a984708SDavid Chisnall if (__pred(*__first, *__i)) 12217a984708SDavid Chisnall return __first; 12227a984708SDavid Chisnall __first = __i; 12237a984708SDavid Chisnall } 12247a984708SDavid Chisnall } 12257a984708SDavid Chisnall return __last; 12267a984708SDavid Chisnall} 12277a984708SDavid Chisnall 12287a984708SDavid Chisnalltemplate <class _ForwardIterator> 12297a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 12307a984708SDavid Chisnall_ForwardIterator 12317a984708SDavid Chisnalladjacent_find(_ForwardIterator __first, _ForwardIterator __last) 12327a984708SDavid Chisnall{ 12337a984708SDavid Chisnall typedef typename iterator_traits<_ForwardIterator>::value_type __v; 12347a984708SDavid Chisnall return _VSTD::adjacent_find(__first, __last, __equal_to<__v>()); 12357a984708SDavid Chisnall} 12367a984708SDavid Chisnall 12377a984708SDavid Chisnall// count 12387a984708SDavid Chisnall 12397a984708SDavid Chisnalltemplate <class _InputIterator, class _Tp> 12407a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 12417a984708SDavid Chisnalltypename iterator_traits<_InputIterator>::difference_type 12427a984708SDavid Chisnallcount(_InputIterator __first, _InputIterator __last, const _Tp& __value_) 12437a984708SDavid Chisnall{ 12447a984708SDavid Chisnall typename iterator_traits<_InputIterator>::difference_type __r(0); 12457a984708SDavid Chisnall for (; __first != __last; ++__first) 12467a984708SDavid Chisnall if (*__first == __value_) 12477a984708SDavid Chisnall ++__r; 12487a984708SDavid Chisnall return __r; 12497a984708SDavid Chisnall} 12507a984708SDavid Chisnall 12517a984708SDavid Chisnall// count_if 12527a984708SDavid Chisnall 12537a984708SDavid Chisnalltemplate <class _InputIterator, class _Predicate> 12547a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 12557a984708SDavid Chisnalltypename iterator_traits<_InputIterator>::difference_type 12567a984708SDavid Chisnallcount_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) 12577a984708SDavid Chisnall{ 12587a984708SDavid Chisnall typename iterator_traits<_InputIterator>::difference_type __r(0); 12597a984708SDavid Chisnall for (; __first != __last; ++__first) 12607a984708SDavid Chisnall if (__pred(*__first)) 12617a984708SDavid Chisnall ++__r; 12627a984708SDavid Chisnall return __r; 12637a984708SDavid Chisnall} 12647a984708SDavid Chisnall 12657a984708SDavid Chisnall// mismatch 12667a984708SDavid Chisnall 12677a984708SDavid Chisnalltemplate <class _InputIterator1, class _InputIterator2, class _BinaryPredicate> 12687a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 12697a984708SDavid Chisnallpair<_InputIterator1, _InputIterator2> 12707a984708SDavid Chisnallmismatch(_InputIterator1 __first1, _InputIterator1 __last1, 12717a984708SDavid Chisnall _InputIterator2 __first2, _BinaryPredicate __pred) 12727a984708SDavid Chisnall{ 1273d72607e9SDimitry Andric for (; __first1 != __last1; ++__first1, (void) ++__first2) 12747a984708SDavid Chisnall if (!__pred(*__first1, *__first2)) 12757a984708SDavid Chisnall break; 12767a984708SDavid Chisnall return pair<_InputIterator1, _InputIterator2>(__first1, __first2); 12777a984708SDavid Chisnall} 12787a984708SDavid Chisnall 12797a984708SDavid Chisnalltemplate <class _InputIterator1, class _InputIterator2> 12807a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 12817a984708SDavid Chisnallpair<_InputIterator1, _InputIterator2> 12827a984708SDavid Chisnallmismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2) 12837a984708SDavid Chisnall{ 12847a984708SDavid Chisnall typedef typename iterator_traits<_InputIterator1>::value_type __v1; 12857a984708SDavid Chisnall typedef typename iterator_traits<_InputIterator2>::value_type __v2; 12867a984708SDavid Chisnall return _VSTD::mismatch(__first1, __last1, __first2, __equal_to<__v1, __v2>()); 12877a984708SDavid Chisnall} 12887a984708SDavid Chisnall 12894bab9fd9SDavid Chisnall#if _LIBCPP_STD_VER > 11 12904bab9fd9SDavid Chisnalltemplate <class _InputIterator1, class _InputIterator2, class _BinaryPredicate> 12914bab9fd9SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 12924bab9fd9SDavid Chisnallpair<_InputIterator1, _InputIterator2> 12934bab9fd9SDavid Chisnallmismatch(_InputIterator1 __first1, _InputIterator1 __last1, 12944bab9fd9SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2, 12954bab9fd9SDavid Chisnall _BinaryPredicate __pred) 12964bab9fd9SDavid Chisnall{ 1297d72607e9SDimitry Andric for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2) 12984bab9fd9SDavid Chisnall if (!__pred(*__first1, *__first2)) 12994bab9fd9SDavid Chisnall break; 13004bab9fd9SDavid Chisnall return pair<_InputIterator1, _InputIterator2>(__first1, __first2); 13014bab9fd9SDavid Chisnall} 13024bab9fd9SDavid Chisnall 13034bab9fd9SDavid Chisnalltemplate <class _InputIterator1, class _InputIterator2> 13044bab9fd9SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 13054bab9fd9SDavid Chisnallpair<_InputIterator1, _InputIterator2> 13064bab9fd9SDavid Chisnallmismatch(_InputIterator1 __first1, _InputIterator1 __last1, 13074bab9fd9SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2) 13084bab9fd9SDavid Chisnall{ 13094bab9fd9SDavid Chisnall typedef typename iterator_traits<_InputIterator1>::value_type __v1; 13104bab9fd9SDavid Chisnall typedef typename iterator_traits<_InputIterator2>::value_type __v2; 13114bab9fd9SDavid Chisnall return _VSTD::mismatch(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>()); 13124bab9fd9SDavid Chisnall} 13134bab9fd9SDavid Chisnall#endif 13144bab9fd9SDavid Chisnall 13157a984708SDavid Chisnall// equal 13167a984708SDavid Chisnall 13177a984708SDavid Chisnalltemplate <class _InputIterator1, class _InputIterator2, class _BinaryPredicate> 13187a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 13197a984708SDavid Chisnallbool 13207a984708SDavid Chisnallequal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred) 13217a984708SDavid Chisnall{ 1322d72607e9SDimitry Andric for (; __first1 != __last1; ++__first1, (void) ++__first2) 13237a984708SDavid Chisnall if (!__pred(*__first1, *__first2)) 13247a984708SDavid Chisnall return false; 13257a984708SDavid Chisnall return true; 13267a984708SDavid Chisnall} 13277a984708SDavid Chisnall 13287a984708SDavid Chisnalltemplate <class _InputIterator1, class _InputIterator2> 13297a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 13307a984708SDavid Chisnallbool 13317a984708SDavid Chisnallequal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2) 13327a984708SDavid Chisnall{ 13337a984708SDavid Chisnall typedef typename iterator_traits<_InputIterator1>::value_type __v1; 13347a984708SDavid Chisnall typedef typename iterator_traits<_InputIterator2>::value_type __v2; 13357a984708SDavid Chisnall return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>()); 13367a984708SDavid Chisnall} 13377a984708SDavid Chisnall 13384bab9fd9SDavid Chisnall#if _LIBCPP_STD_VER > 11 13394bab9fd9SDavid Chisnalltemplate <class _BinaryPredicate, class _InputIterator1, class _InputIterator2> 13404bab9fd9SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 13414bab9fd9SDavid Chisnallbool 13424bab9fd9SDavid Chisnall__equal(_InputIterator1 __first1, _InputIterator1 __last1, 13434bab9fd9SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred, 13444bab9fd9SDavid Chisnall input_iterator_tag, input_iterator_tag ) 13454bab9fd9SDavid Chisnall{ 1346d72607e9SDimitry Andric for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2) 13474bab9fd9SDavid Chisnall if (!__pred(*__first1, *__first2)) 13484bab9fd9SDavid Chisnall return false; 13494bab9fd9SDavid Chisnall return __first1 == __last1 && __first2 == __last2; 13504bab9fd9SDavid Chisnall} 13514bab9fd9SDavid Chisnall 13524bab9fd9SDavid Chisnalltemplate <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2> 13534bab9fd9SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 13544bab9fd9SDavid Chisnallbool 13554bab9fd9SDavid Chisnall__equal(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, 13564bab9fd9SDavid Chisnall _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred, 13574bab9fd9SDavid Chisnall random_access_iterator_tag, random_access_iterator_tag ) 13584bab9fd9SDavid Chisnall{ 13594bab9fd9SDavid Chisnall if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2)) 13604bab9fd9SDavid Chisnall return false; 13614bab9fd9SDavid Chisnall return _VSTD::equal<_RandomAccessIterator1, _RandomAccessIterator2, 13624bab9fd9SDavid Chisnall typename add_lvalue_reference<_BinaryPredicate>::type> 13634bab9fd9SDavid Chisnall (__first1, __last1, __first2, __pred ); 13644bab9fd9SDavid Chisnall} 13654bab9fd9SDavid Chisnall 13664bab9fd9SDavid Chisnalltemplate <class _InputIterator1, class _InputIterator2, class _BinaryPredicate> 13674bab9fd9SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 13684bab9fd9SDavid Chisnallbool 13694bab9fd9SDavid Chisnallequal(_InputIterator1 __first1, _InputIterator1 __last1, 13704bab9fd9SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2, _BinaryPredicate __pred ) 13714bab9fd9SDavid Chisnall{ 13724bab9fd9SDavid Chisnall return _VSTD::__equal<typename add_lvalue_reference<_BinaryPredicate>::type> 13734bab9fd9SDavid Chisnall (__first1, __last1, __first2, __last2, __pred, 13744bab9fd9SDavid Chisnall typename iterator_traits<_InputIterator1>::iterator_category(), 13754bab9fd9SDavid Chisnall typename iterator_traits<_InputIterator2>::iterator_category()); 13764bab9fd9SDavid Chisnall} 13774bab9fd9SDavid Chisnall 13784bab9fd9SDavid Chisnalltemplate <class _InputIterator1, class _InputIterator2> 13794bab9fd9SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 13804bab9fd9SDavid Chisnallbool 13814bab9fd9SDavid Chisnallequal(_InputIterator1 __first1, _InputIterator1 __last1, 13824bab9fd9SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2) 13834bab9fd9SDavid Chisnall{ 13844bab9fd9SDavid Chisnall typedef typename iterator_traits<_InputIterator1>::value_type __v1; 13854bab9fd9SDavid Chisnall typedef typename iterator_traits<_InputIterator2>::value_type __v2; 13864bab9fd9SDavid Chisnall return _VSTD::__equal(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>(), 13874bab9fd9SDavid Chisnall typename iterator_traits<_InputIterator1>::iterator_category(), 13884bab9fd9SDavid Chisnall typename iterator_traits<_InputIterator2>::iterator_category()); 13894bab9fd9SDavid Chisnall} 13904bab9fd9SDavid Chisnall#endif 13914bab9fd9SDavid Chisnall 13927a984708SDavid Chisnall// is_permutation 13937a984708SDavid Chisnall 13947a984708SDavid Chisnalltemplate<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> 13957a984708SDavid Chisnallbool 13967a984708SDavid Chisnallis_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 13977a984708SDavid Chisnall _ForwardIterator2 __first2, _BinaryPredicate __pred) 13987a984708SDavid Chisnall{ 13997a984708SDavid Chisnall // shorten sequences as much as possible by lopping of any equal parts 1400d72607e9SDimitry Andric for (; __first1 != __last1; ++__first1, (void) ++__first2) 14017a984708SDavid Chisnall if (!__pred(*__first1, *__first2)) 14027a984708SDavid Chisnall goto __not_done; 14037a984708SDavid Chisnall return true; 14047a984708SDavid Chisnall__not_done: 14057a984708SDavid Chisnall // __first1 != __last1 && *__first1 != *__first2 14067a984708SDavid Chisnall typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1; 14077a984708SDavid Chisnall _D1 __l1 = _VSTD::distance(__first1, __last1); 14087a984708SDavid Chisnall if (__l1 == _D1(1)) 14097a984708SDavid Chisnall return false; 14107a984708SDavid Chisnall _ForwardIterator2 __last2 = _VSTD::next(__first2, __l1); 14117a984708SDavid Chisnall // For each element in [f1, l1) see if there are the same number of 14127a984708SDavid Chisnall // equal elements in [f2, l2) 14137a984708SDavid Chisnall for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i) 14147a984708SDavid Chisnall { 14157a984708SDavid Chisnall // Have we already counted the number of *__i in [f1, l1)? 14167a984708SDavid Chisnall for (_ForwardIterator1 __j = __first1; __j != __i; ++__j) 14177a984708SDavid Chisnall if (__pred(*__j, *__i)) 14187a984708SDavid Chisnall goto __next_iter; 14197a984708SDavid Chisnall { 14207a984708SDavid Chisnall // Count number of *__i in [f2, l2) 14217a984708SDavid Chisnall _D1 __c2 = 0; 14227a984708SDavid Chisnall for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j) 14237a984708SDavid Chisnall if (__pred(*__i, *__j)) 14247a984708SDavid Chisnall ++__c2; 14257a984708SDavid Chisnall if (__c2 == 0) 14267a984708SDavid Chisnall return false; 14277a984708SDavid Chisnall // Count number of *__i in [__i, l1) (we can start with 1) 14287a984708SDavid Chisnall _D1 __c1 = 1; 14297a984708SDavid Chisnall for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j) 14307a984708SDavid Chisnall if (__pred(*__i, *__j)) 14317a984708SDavid Chisnall ++__c1; 14327a984708SDavid Chisnall if (__c1 != __c2) 14337a984708SDavid Chisnall return false; 14347a984708SDavid Chisnall } 14357a984708SDavid Chisnall__next_iter:; 14367a984708SDavid Chisnall } 14377a984708SDavid Chisnall return true; 14387a984708SDavid Chisnall} 14397a984708SDavid Chisnall 14407a984708SDavid Chisnalltemplate<class _ForwardIterator1, class _ForwardIterator2> 14417a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 14427a984708SDavid Chisnallbool 14437a984708SDavid Chisnallis_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 14447a984708SDavid Chisnall _ForwardIterator2 __first2) 14457a984708SDavid Chisnall{ 14467a984708SDavid Chisnall typedef typename iterator_traits<_ForwardIterator1>::value_type __v1; 14477a984708SDavid Chisnall typedef typename iterator_traits<_ForwardIterator2>::value_type __v2; 14487a984708SDavid Chisnall return _VSTD::is_permutation(__first1, __last1, __first2, __equal_to<__v1, __v2>()); 14497a984708SDavid Chisnall} 14507a984708SDavid Chisnall 14514bab9fd9SDavid Chisnall#if _LIBCPP_STD_VER > 11 14524bab9fd9SDavid Chisnalltemplate<class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2> 14534bab9fd9SDavid Chisnallbool 14544bab9fd9SDavid Chisnall__is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 14554bab9fd9SDavid Chisnall _ForwardIterator2 __first2, _ForwardIterator2 __last2, 14564bab9fd9SDavid Chisnall _BinaryPredicate __pred, 14574bab9fd9SDavid Chisnall forward_iterator_tag, forward_iterator_tag ) 14584bab9fd9SDavid Chisnall{ 14594bab9fd9SDavid Chisnall // shorten sequences as much as possible by lopping of any equal parts 1460d72607e9SDimitry Andric for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void) ++__first2) 14614bab9fd9SDavid Chisnall if (!__pred(*__first1, *__first2)) 14624bab9fd9SDavid Chisnall goto __not_done; 14634bab9fd9SDavid Chisnall return __first1 == __last1 && __first2 == __last2; 14644bab9fd9SDavid Chisnall__not_done: 14654bab9fd9SDavid Chisnall // __first1 != __last1 && __first2 != __last2 && *__first1 != *__first2 14664bab9fd9SDavid Chisnall typedef typename iterator_traits<_ForwardIterator1>::difference_type _D1; 14674bab9fd9SDavid Chisnall _D1 __l1 = _VSTD::distance(__first1, __last1); 14684bab9fd9SDavid Chisnall 14694bab9fd9SDavid Chisnall typedef typename iterator_traits<_ForwardIterator2>::difference_type _D2; 14704bab9fd9SDavid Chisnall _D2 __l2 = _VSTD::distance(__first2, __last2); 14714bab9fd9SDavid Chisnall if (__l1 != __l2) 14724bab9fd9SDavid Chisnall return false; 14734bab9fd9SDavid Chisnall 14744bab9fd9SDavid Chisnall // For each element in [f1, l1) see if there are the same number of 14754bab9fd9SDavid Chisnall // equal elements in [f2, l2) 14764bab9fd9SDavid Chisnall for (_ForwardIterator1 __i = __first1; __i != __last1; ++__i) 14774bab9fd9SDavid Chisnall { 14784bab9fd9SDavid Chisnall // Have we already counted the number of *__i in [f1, l1)? 14794bab9fd9SDavid Chisnall for (_ForwardIterator1 __j = __first1; __j != __i; ++__j) 14804bab9fd9SDavid Chisnall if (__pred(*__j, *__i)) 14814bab9fd9SDavid Chisnall goto __next_iter; 14824bab9fd9SDavid Chisnall { 14834bab9fd9SDavid Chisnall // Count number of *__i in [f2, l2) 14844bab9fd9SDavid Chisnall _D1 __c2 = 0; 14854bab9fd9SDavid Chisnall for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j) 14864bab9fd9SDavid Chisnall if (__pred(*__i, *__j)) 14874bab9fd9SDavid Chisnall ++__c2; 14884bab9fd9SDavid Chisnall if (__c2 == 0) 14894bab9fd9SDavid Chisnall return false; 14904bab9fd9SDavid Chisnall // Count number of *__i in [__i, l1) (we can start with 1) 14914bab9fd9SDavid Chisnall _D1 __c1 = 1; 14924bab9fd9SDavid Chisnall for (_ForwardIterator1 __j = _VSTD::next(__i); __j != __last1; ++__j) 14934bab9fd9SDavid Chisnall if (__pred(*__i, *__j)) 14944bab9fd9SDavid Chisnall ++__c1; 14954bab9fd9SDavid Chisnall if (__c1 != __c2) 14964bab9fd9SDavid Chisnall return false; 14974bab9fd9SDavid Chisnall } 14984bab9fd9SDavid Chisnall__next_iter:; 14994bab9fd9SDavid Chisnall } 15004bab9fd9SDavid Chisnall return true; 15014bab9fd9SDavid Chisnall} 15024bab9fd9SDavid Chisnall 15034bab9fd9SDavid Chisnalltemplate<class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2> 15044bab9fd9SDavid Chisnallbool 15054bab9fd9SDavid Chisnall__is_permutation(_RandomAccessIterator1 __first1, _RandomAccessIterator2 __last1, 15064bab9fd9SDavid Chisnall _RandomAccessIterator1 __first2, _RandomAccessIterator2 __last2, 15074bab9fd9SDavid Chisnall _BinaryPredicate __pred, 15084bab9fd9SDavid Chisnall random_access_iterator_tag, random_access_iterator_tag ) 15094bab9fd9SDavid Chisnall{ 15104bab9fd9SDavid Chisnall if ( _VSTD::distance(__first1, __last1) != _VSTD::distance(__first2, __last2)) 15114bab9fd9SDavid Chisnall return false; 15124bab9fd9SDavid Chisnall return _VSTD::is_permutation<_RandomAccessIterator1, _RandomAccessIterator2, 15134bab9fd9SDavid Chisnall typename add_lvalue_reference<_BinaryPredicate>::type> 15144bab9fd9SDavid Chisnall (__first1, __last1, __first2, __pred ); 15154bab9fd9SDavid Chisnall} 15164bab9fd9SDavid Chisnall 15174bab9fd9SDavid Chisnalltemplate<class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> 15184bab9fd9SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 15194bab9fd9SDavid Chisnallbool 15204bab9fd9SDavid Chisnallis_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 15214bab9fd9SDavid Chisnall _ForwardIterator2 __first2, _ForwardIterator2 __last2, 15224bab9fd9SDavid Chisnall _BinaryPredicate __pred ) 15234bab9fd9SDavid Chisnall{ 15244bab9fd9SDavid Chisnall return _VSTD::__is_permutation<typename add_lvalue_reference<_BinaryPredicate>::type> 15254bab9fd9SDavid Chisnall (__first1, __last1, __first2, __last2, __pred, 15264bab9fd9SDavid Chisnall typename iterator_traits<_ForwardIterator1>::iterator_category(), 15274bab9fd9SDavid Chisnall typename iterator_traits<_ForwardIterator2>::iterator_category()); 15284bab9fd9SDavid Chisnall} 15294bab9fd9SDavid Chisnall 15304bab9fd9SDavid Chisnalltemplate<class _ForwardIterator1, class _ForwardIterator2> 15314bab9fd9SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 15324bab9fd9SDavid Chisnallbool 15334bab9fd9SDavid Chisnallis_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 15344bab9fd9SDavid Chisnall _ForwardIterator2 __first2, _ForwardIterator2 __last2) 15354bab9fd9SDavid Chisnall{ 15364bab9fd9SDavid Chisnall typedef typename iterator_traits<_ForwardIterator1>::value_type __v1; 15374bab9fd9SDavid Chisnall typedef typename iterator_traits<_ForwardIterator2>::value_type __v2; 15384bab9fd9SDavid Chisnall return _VSTD::__is_permutation(__first1, __last1, __first2, __last2, 15394bab9fd9SDavid Chisnall __equal_to<__v1, __v2>(), 15404bab9fd9SDavid Chisnall typename iterator_traits<_ForwardIterator1>::iterator_category(), 15414bab9fd9SDavid Chisnall typename iterator_traits<_ForwardIterator2>::iterator_category()); 15424bab9fd9SDavid Chisnall} 15434bab9fd9SDavid Chisnall#endif 15444bab9fd9SDavid Chisnall 15457a984708SDavid Chisnall// search 15467a984708SDavid Chisnall 15477a984708SDavid Chisnalltemplate <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2> 15487c82a1ecSDimitry Andricpair<_ForwardIterator1, _ForwardIterator1> 15497a984708SDavid Chisnall__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 15507a984708SDavid Chisnall _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred, 15517a984708SDavid Chisnall forward_iterator_tag, forward_iterator_tag) 15527a984708SDavid Chisnall{ 15537a984708SDavid Chisnall if (__first2 == __last2) 15547c82a1ecSDimitry Andric return make_pair(__first1, __first1); // Everything matches an empty sequence 15557a984708SDavid Chisnall while (true) 15567a984708SDavid Chisnall { 15577a984708SDavid Chisnall // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks 15587a984708SDavid Chisnall while (true) 15597a984708SDavid Chisnall { 15607a984708SDavid Chisnall if (__first1 == __last1) // return __last1 if no element matches *__first2 15617c82a1ecSDimitry Andric return make_pair(__last1, __last1); 15627a984708SDavid Chisnall if (__pred(*__first1, *__first2)) 15637a984708SDavid Chisnall break; 15647a984708SDavid Chisnall ++__first1; 15657a984708SDavid Chisnall } 15667a984708SDavid Chisnall // *__first1 matches *__first2, now match elements after here 15677a984708SDavid Chisnall _ForwardIterator1 __m1 = __first1; 15687a984708SDavid Chisnall _ForwardIterator2 __m2 = __first2; 15697a984708SDavid Chisnall while (true) 15707a984708SDavid Chisnall { 15717a984708SDavid Chisnall if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern) 15727c82a1ecSDimitry Andric return make_pair(__first1, __m1); 15737a984708SDavid Chisnall if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found 15747c82a1ecSDimitry Andric return make_pair(__last1, __last1); 15757a984708SDavid Chisnall if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1 15767a984708SDavid Chisnall { 15777a984708SDavid Chisnall ++__first1; 15787a984708SDavid Chisnall break; 15797a984708SDavid Chisnall } // else there is a match, check next elements 15807a984708SDavid Chisnall } 15817a984708SDavid Chisnall } 15827a984708SDavid Chisnall} 15837a984708SDavid Chisnall 15847a984708SDavid Chisnalltemplate <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2> 15857c82a1ecSDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX11 15867c82a1ecSDimitry Andricpair<_RandomAccessIterator1, _RandomAccessIterator1> 15877a984708SDavid Chisnall__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, 15887a984708SDavid Chisnall _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred, 15897a984708SDavid Chisnall random_access_iterator_tag, random_access_iterator_tag) 15907a984708SDavid Chisnall{ 15917c82a1ecSDimitry Andric typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1; 15927c82a1ecSDimitry Andric typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2; 15937a984708SDavid Chisnall // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern 15947c82a1ecSDimitry Andric const _D2 __len2 = __last2 - __first2; 15957a984708SDavid Chisnall if (__len2 == 0) 15967c82a1ecSDimitry Andric return make_pair(__first1, __first1); 15977c82a1ecSDimitry Andric const _D1 __len1 = __last1 - __first1; 15987a984708SDavid Chisnall if (__len1 < __len2) 15997c82a1ecSDimitry Andric return make_pair(__last1, __last1); 16007a984708SDavid Chisnall const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here 1601aed8d94eSDimitry Andric 16027a984708SDavid Chisnall while (true) 16037a984708SDavid Chisnall { 16047a984708SDavid Chisnall while (true) 16057a984708SDavid Chisnall { 16067a984708SDavid Chisnall if (__first1 == __s) 16077c82a1ecSDimitry Andric return make_pair(__last1, __last1); 16087a984708SDavid Chisnall if (__pred(*__first1, *__first2)) 16097a984708SDavid Chisnall break; 16107a984708SDavid Chisnall ++__first1; 16117a984708SDavid Chisnall } 1612aed8d94eSDimitry Andric 16137a984708SDavid Chisnall _RandomAccessIterator1 __m1 = __first1; 16147a984708SDavid Chisnall _RandomAccessIterator2 __m2 = __first2; 16157a984708SDavid Chisnall while (true) 16167a984708SDavid Chisnall { 16177a984708SDavid Chisnall if (++__m2 == __last2) 16187c82a1ecSDimitry Andric return make_pair(__first1, __first1 + __len2); 16197a984708SDavid Chisnall ++__m1; // no need to check range on __m1 because __s guarantees we have enough source 16207a984708SDavid Chisnall if (!__pred(*__m1, *__m2)) 16217a984708SDavid Chisnall { 16227a984708SDavid Chisnall ++__first1; 16237a984708SDavid Chisnall break; 16247a984708SDavid Chisnall } 16257a984708SDavid Chisnall } 16267a984708SDavid Chisnall } 16277a984708SDavid Chisnall} 16287a984708SDavid Chisnall 16297a984708SDavid Chisnalltemplate <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate> 16307a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 16317a984708SDavid Chisnall_ForwardIterator1 16327a984708SDavid Chisnallsearch(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 16337a984708SDavid Chisnall _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred) 16347a984708SDavid Chisnall{ 16357a984708SDavid Chisnall return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type> 16367a984708SDavid Chisnall (__first1, __last1, __first2, __last2, __pred, 16377c82a1ecSDimitry Andric typename iterator_traits<_ForwardIterator1>::iterator_category(), 16387c82a1ecSDimitry Andric typename iterator_traits<_ForwardIterator2>::iterator_category()) 16397c82a1ecSDimitry Andric .first; 16407a984708SDavid Chisnall} 16417a984708SDavid Chisnall 16427a984708SDavid Chisnalltemplate <class _ForwardIterator1, class _ForwardIterator2> 16437a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 16447a984708SDavid Chisnall_ForwardIterator1 16457a984708SDavid Chisnallsearch(_ForwardIterator1 __first1, _ForwardIterator1 __last1, 16467a984708SDavid Chisnall _ForwardIterator2 __first2, _ForwardIterator2 __last2) 16477a984708SDavid Chisnall{ 16487c82a1ecSDimitry Andric typedef typename iterator_traits<_ForwardIterator1>::value_type __v1; 16497c82a1ecSDimitry Andric typedef typename iterator_traits<_ForwardIterator2>::value_type __v2; 16507a984708SDavid Chisnall return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>()); 16517a984708SDavid Chisnall} 16527a984708SDavid Chisnall 16537a984708SDavid Chisnall// search_n 16547a984708SDavid Chisnall 16557a984708SDavid Chisnalltemplate <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp> 16567a984708SDavid Chisnall_ForwardIterator 16577a984708SDavid Chisnall__search_n(_ForwardIterator __first, _ForwardIterator __last, 16587a984708SDavid Chisnall _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag) 16597a984708SDavid Chisnall{ 16607a984708SDavid Chisnall if (__count <= 0) 16617a984708SDavid Chisnall return __first; 16627a984708SDavid Chisnall while (true) 16637a984708SDavid Chisnall { 16647a984708SDavid Chisnall // Find first element in sequence that matchs __value_, with a mininum of loop checks 16657a984708SDavid Chisnall while (true) 16667a984708SDavid Chisnall { 16677a984708SDavid Chisnall if (__first == __last) // return __last if no element matches __value_ 16687a984708SDavid Chisnall return __last; 16697a984708SDavid Chisnall if (__pred(*__first, __value_)) 16707a984708SDavid Chisnall break; 16717a984708SDavid Chisnall ++__first; 16727a984708SDavid Chisnall } 16737a984708SDavid Chisnall // *__first matches __value_, now match elements after here 16747a984708SDavid Chisnall _ForwardIterator __m = __first; 16757a984708SDavid Chisnall _Size __c(0); 16767a984708SDavid Chisnall while (true) 16777a984708SDavid Chisnall { 16787a984708SDavid Chisnall if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern) 16797a984708SDavid Chisnall return __first; 16807a984708SDavid Chisnall if (++__m == __last) // Otherwise if source exhaused, pattern not found 16817a984708SDavid Chisnall return __last; 16827a984708SDavid Chisnall if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first 16837a984708SDavid Chisnall { 16847a984708SDavid Chisnall __first = __m; 16857a984708SDavid Chisnall ++__first; 16867a984708SDavid Chisnall break; 16877a984708SDavid Chisnall } // else there is a match, check next elements 16887a984708SDavid Chisnall } 16897a984708SDavid Chisnall } 16907a984708SDavid Chisnall} 16917a984708SDavid Chisnall 16927a984708SDavid Chisnalltemplate <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp> 16937a984708SDavid Chisnall_RandomAccessIterator 16947a984708SDavid Chisnall__search_n(_RandomAccessIterator __first, _RandomAccessIterator __last, 16957a984708SDavid Chisnall _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag) 16967a984708SDavid Chisnall{ 16977a984708SDavid Chisnall if (__count <= 0) 16987a984708SDavid Chisnall return __first; 16997a984708SDavid Chisnall _Size __len = static_cast<_Size>(__last - __first); 17007a984708SDavid Chisnall if (__len < __count) 17017a984708SDavid Chisnall return __last; 17027a984708SDavid Chisnall const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here 17037a984708SDavid Chisnall while (true) 17047a984708SDavid Chisnall { 17057a984708SDavid Chisnall // Find first element in sequence that matchs __value_, with a mininum of loop checks 17067a984708SDavid Chisnall while (true) 17077a984708SDavid Chisnall { 17081bf9f7c1SDimitry Andric if (__first >= __s) // return __last if no element matches __value_ 17097a984708SDavid Chisnall return __last; 17107a984708SDavid Chisnall if (__pred(*__first, __value_)) 17117a984708SDavid Chisnall break; 17127a984708SDavid Chisnall ++__first; 17137a984708SDavid Chisnall } 17147a984708SDavid Chisnall // *__first matches __value_, now match elements after here 17157a984708SDavid Chisnall _RandomAccessIterator __m = __first; 17167a984708SDavid Chisnall _Size __c(0); 17177a984708SDavid Chisnall while (true) 17187a984708SDavid Chisnall { 17197a984708SDavid Chisnall if (++__c == __count) // If pattern exhausted, __first is the answer (works for 1 element pattern) 17207a984708SDavid Chisnall return __first; 17217a984708SDavid Chisnall ++__m; // no need to check range on __m because __s guarantees we have enough source 17227a984708SDavid Chisnall if (!__pred(*__m, __value_)) // if there is a mismatch, restart with a new __first 17237a984708SDavid Chisnall { 17247a984708SDavid Chisnall __first = __m; 17257a984708SDavid Chisnall ++__first; 17267a984708SDavid Chisnall break; 17277a984708SDavid Chisnall } // else there is a match, check next elements 17287a984708SDavid Chisnall } 17297a984708SDavid Chisnall } 17307a984708SDavid Chisnall} 17317a984708SDavid Chisnall 17327a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate> 17337a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 17347a984708SDavid Chisnall_ForwardIterator 17357a984708SDavid Chisnallsearch_n(_ForwardIterator __first, _ForwardIterator __last, 17367a984708SDavid Chisnall _Size __count, const _Tp& __value_, _BinaryPredicate __pred) 17377a984708SDavid Chisnall{ 17387a984708SDavid Chisnall return _VSTD::__search_n<typename add_lvalue_reference<_BinaryPredicate>::type> 1739854fa44bSDimitry Andric (__first, __last, __convert_to_integral(__count), __value_, __pred, 1740854fa44bSDimitry Andric typename iterator_traits<_ForwardIterator>::iterator_category()); 17417a984708SDavid Chisnall} 17427a984708SDavid Chisnall 17437a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Size, class _Tp> 17447a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 17457a984708SDavid Chisnall_ForwardIterator 17467a984708SDavid Chisnallsearch_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_) 17477a984708SDavid Chisnall{ 17487a984708SDavid Chisnall typedef typename iterator_traits<_ForwardIterator>::value_type __v; 1749854fa44bSDimitry Andric return _VSTD::search_n(__first, __last, __convert_to_integral(__count), 1750854fa44bSDimitry Andric __value_, __equal_to<__v, _Tp>()); 17517a984708SDavid Chisnall} 17527a984708SDavid Chisnall 17537a984708SDavid Chisnall// copy 17547a984708SDavid Chisnalltemplate <class _Iter> 17557a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 17567a984708SDavid Chisnall_Iter 17577a984708SDavid Chisnall__unwrap_iter(_Iter __i) 17587a984708SDavid Chisnall{ 17597a984708SDavid Chisnall return __i; 17607a984708SDavid Chisnall} 17617a984708SDavid Chisnall 17627a984708SDavid Chisnalltemplate <class _Tp> 17637a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 17647a984708SDavid Chisnalltypename enable_if 17657a984708SDavid Chisnall< 17667a984708SDavid Chisnall is_trivially_copy_assignable<_Tp>::value, 17677a984708SDavid Chisnall _Tp* 17687a984708SDavid Chisnall>::type 17697a984708SDavid Chisnall__unwrap_iter(move_iterator<_Tp*> __i) 17707a984708SDavid Chisnall{ 17717a984708SDavid Chisnall return __i.base(); 17727a984708SDavid Chisnall} 17737a984708SDavid Chisnall 17744f7ab58eSDimitry Andric#if _LIBCPP_DEBUG_LEVEL < 2 17754f7ab58eSDimitry Andric 17767a984708SDavid Chisnalltemplate <class _Tp> 17777a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 17787a984708SDavid Chisnalltypename enable_if 17797a984708SDavid Chisnall< 17807a984708SDavid Chisnall is_trivially_copy_assignable<_Tp>::value, 17817a984708SDavid Chisnall _Tp* 17827a984708SDavid Chisnall>::type 17837a984708SDavid Chisnall__unwrap_iter(__wrap_iter<_Tp*> __i) 17847a984708SDavid Chisnall{ 17857a984708SDavid Chisnall return __i.base(); 17867a984708SDavid Chisnall} 17877a984708SDavid Chisnall 1788aed8d94eSDimitry Andric#else 1789aed8d94eSDimitry Andric 1790aed8d94eSDimitry Andrictemplate <class _Tp> 1791aed8d94eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 1792aed8d94eSDimitry Andrictypename enable_if 1793aed8d94eSDimitry Andric< 1794aed8d94eSDimitry Andric is_trivially_copy_assignable<_Tp>::value, 1795aed8d94eSDimitry Andric __wrap_iter<_Tp*> 1796aed8d94eSDimitry Andric>::type 1797aed8d94eSDimitry Andric__unwrap_iter(__wrap_iter<_Tp*> __i) 1798aed8d94eSDimitry Andric{ 1799aed8d94eSDimitry Andric return __i; 1800aed8d94eSDimitry Andric} 1801aed8d94eSDimitry Andric 18024f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG_LEVEL < 2 18034f7ab58eSDimitry Andric 18047a984708SDavid Chisnalltemplate <class _InputIterator, class _OutputIterator> 18057a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 18067a984708SDavid Chisnall_OutputIterator 18077a984708SDavid Chisnall__copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result) 18087a984708SDavid Chisnall{ 1809d72607e9SDimitry Andric for (; __first != __last; ++__first, (void) ++__result) 18107a984708SDavid Chisnall *__result = *__first; 18117a984708SDavid Chisnall return __result; 18127a984708SDavid Chisnall} 18137a984708SDavid Chisnall 18147a984708SDavid Chisnalltemplate <class _Tp, class _Up> 18157a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 18167a984708SDavid Chisnalltypename enable_if 18177a984708SDavid Chisnall< 18187a984708SDavid Chisnall is_same<typename remove_const<_Tp>::type, _Up>::value && 18197a984708SDavid Chisnall is_trivially_copy_assignable<_Up>::value, 18207a984708SDavid Chisnall _Up* 18217a984708SDavid Chisnall>::type 18227a984708SDavid Chisnall__copy(_Tp* __first, _Tp* __last, _Up* __result) 18237a984708SDavid Chisnall{ 18247a984708SDavid Chisnall const size_t __n = static_cast<size_t>(__last - __first); 1825854fa44bSDimitry Andric if (__n > 0) 18267a984708SDavid Chisnall _VSTD::memmove(__result, __first, __n * sizeof(_Up)); 18277a984708SDavid Chisnall return __result + __n; 18287a984708SDavid Chisnall} 18297a984708SDavid Chisnall 18307a984708SDavid Chisnalltemplate <class _InputIterator, class _OutputIterator> 18317a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 18327a984708SDavid Chisnall_OutputIterator 18337a984708SDavid Chisnallcopy(_InputIterator __first, _InputIterator __last, _OutputIterator __result) 18347a984708SDavid Chisnall{ 18357a984708SDavid Chisnall return _VSTD::__copy(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result)); 18367a984708SDavid Chisnall} 18377a984708SDavid Chisnall 18387a984708SDavid Chisnall// copy_backward 18397a984708SDavid Chisnall 1840cfdf2879SDavid Chisnalltemplate <class _BidirectionalIterator, class _OutputIterator> 18417a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 18427a984708SDavid Chisnall_OutputIterator 1843cfdf2879SDavid Chisnall__copy_backward(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result) 18447a984708SDavid Chisnall{ 18457a984708SDavid Chisnall while (__first != __last) 18467a984708SDavid Chisnall *--__result = *--__last; 18477a984708SDavid Chisnall return __result; 18487a984708SDavid Chisnall} 18497a984708SDavid Chisnall 18507a984708SDavid Chisnalltemplate <class _Tp, class _Up> 18517a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 18527a984708SDavid Chisnalltypename enable_if 18537a984708SDavid Chisnall< 18547a984708SDavid Chisnall is_same<typename remove_const<_Tp>::type, _Up>::value && 18557a984708SDavid Chisnall is_trivially_copy_assignable<_Up>::value, 18567a984708SDavid Chisnall _Up* 18577a984708SDavid Chisnall>::type 18587a984708SDavid Chisnall__copy_backward(_Tp* __first, _Tp* __last, _Up* __result) 18597a984708SDavid Chisnall{ 18607a984708SDavid Chisnall const size_t __n = static_cast<size_t>(__last - __first); 1861854fa44bSDimitry Andric if (__n > 0) 1862854fa44bSDimitry Andric { 18637a984708SDavid Chisnall __result -= __n; 18647a984708SDavid Chisnall _VSTD::memmove(__result, __first, __n * sizeof(_Up)); 1865854fa44bSDimitry Andric } 18667a984708SDavid Chisnall return __result; 18677a984708SDavid Chisnall} 18687a984708SDavid Chisnall 18697a984708SDavid Chisnalltemplate <class _BidirectionalIterator1, class _BidirectionalIterator2> 18707a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 18717a984708SDavid Chisnall_BidirectionalIterator2 18727a984708SDavid Chisnallcopy_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last, 18737a984708SDavid Chisnall _BidirectionalIterator2 __result) 18747a984708SDavid Chisnall{ 1875aed8d94eSDimitry Andric return _VSTD::__copy_backward(__unwrap_iter(__first), 1876aed8d94eSDimitry Andric __unwrap_iter(__last), 1877aed8d94eSDimitry Andric __unwrap_iter(__result)); 18787a984708SDavid Chisnall} 18797a984708SDavid Chisnall 18807a984708SDavid Chisnall// copy_if 18817a984708SDavid Chisnall 18827a984708SDavid Chisnalltemplate<class _InputIterator, class _OutputIterator, class _Predicate> 18837a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 18847a984708SDavid Chisnall_OutputIterator 18857a984708SDavid Chisnallcopy_if(_InputIterator __first, _InputIterator __last, 18867a984708SDavid Chisnall _OutputIterator __result, _Predicate __pred) 18877a984708SDavid Chisnall{ 18887a984708SDavid Chisnall for (; __first != __last; ++__first) 18897a984708SDavid Chisnall { 18907a984708SDavid Chisnall if (__pred(*__first)) 18917a984708SDavid Chisnall { 18927a984708SDavid Chisnall *__result = *__first; 18937a984708SDavid Chisnall ++__result; 18947a984708SDavid Chisnall } 18957a984708SDavid Chisnall } 18967a984708SDavid Chisnall return __result; 18977a984708SDavid Chisnall} 18987a984708SDavid Chisnall 18997a984708SDavid Chisnall// copy_n 19007a984708SDavid Chisnall 19017a984708SDavid Chisnalltemplate<class _InputIterator, class _Size, class _OutputIterator> 19027a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 19037a984708SDavid Chisnalltypename enable_if 19047a984708SDavid Chisnall< 19057a984708SDavid Chisnall __is_input_iterator<_InputIterator>::value && 19067a984708SDavid Chisnall !__is_random_access_iterator<_InputIterator>::value, 19077a984708SDavid Chisnall _OutputIterator 19087a984708SDavid Chisnall>::type 1909854fa44bSDimitry Andriccopy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result) 19107a984708SDavid Chisnall{ 1911854fa44bSDimitry Andric typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize; 1912854fa44bSDimitry Andric _IntegralSize __n = __orig_n; 19137a984708SDavid Chisnall if (__n > 0) 19147a984708SDavid Chisnall { 19157a984708SDavid Chisnall *__result = *__first; 19167a984708SDavid Chisnall ++__result; 19177a984708SDavid Chisnall for (--__n; __n > 0; --__n) 19187a984708SDavid Chisnall { 19197a984708SDavid Chisnall ++__first; 19207a984708SDavid Chisnall *__result = *__first; 19217a984708SDavid Chisnall ++__result; 19227a984708SDavid Chisnall } 19237a984708SDavid Chisnall } 19247a984708SDavid Chisnall return __result; 19257a984708SDavid Chisnall} 19267a984708SDavid Chisnall 19277a984708SDavid Chisnalltemplate<class _InputIterator, class _Size, class _OutputIterator> 19287a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 19297a984708SDavid Chisnalltypename enable_if 19307a984708SDavid Chisnall< 19317a984708SDavid Chisnall __is_random_access_iterator<_InputIterator>::value, 19327a984708SDavid Chisnall _OutputIterator 19337a984708SDavid Chisnall>::type 1934854fa44bSDimitry Andriccopy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result) 19357a984708SDavid Chisnall{ 1936854fa44bSDimitry Andric typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize; 1937854fa44bSDimitry Andric _IntegralSize __n = __orig_n; 19387a984708SDavid Chisnall return _VSTD::copy(__first, __first + __n, __result); 19397a984708SDavid Chisnall} 19407a984708SDavid Chisnall 19417a984708SDavid Chisnall// move 19427a984708SDavid Chisnall 19437a984708SDavid Chisnalltemplate <class _InputIterator, class _OutputIterator> 19447a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 19457a984708SDavid Chisnall_OutputIterator 19467a984708SDavid Chisnall__move(_InputIterator __first, _InputIterator __last, _OutputIterator __result) 19477a984708SDavid Chisnall{ 1948d72607e9SDimitry Andric for (; __first != __last; ++__first, (void) ++__result) 19497a984708SDavid Chisnall *__result = _VSTD::move(*__first); 19507a984708SDavid Chisnall return __result; 19517a984708SDavid Chisnall} 19527a984708SDavid Chisnall 19537a984708SDavid Chisnalltemplate <class _Tp, class _Up> 19547a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 19557a984708SDavid Chisnalltypename enable_if 19567a984708SDavid Chisnall< 19577a984708SDavid Chisnall is_same<typename remove_const<_Tp>::type, _Up>::value && 19587a984708SDavid Chisnall is_trivially_copy_assignable<_Up>::value, 19597a984708SDavid Chisnall _Up* 19607a984708SDavid Chisnall>::type 19617a984708SDavid Chisnall__move(_Tp* __first, _Tp* __last, _Up* __result) 19627a984708SDavid Chisnall{ 19637a984708SDavid Chisnall const size_t __n = static_cast<size_t>(__last - __first); 1964854fa44bSDimitry Andric if (__n > 0) 19657a984708SDavid Chisnall _VSTD::memmove(__result, __first, __n * sizeof(_Up)); 19667a984708SDavid Chisnall return __result + __n; 19677a984708SDavid Chisnall} 19687a984708SDavid Chisnall 19697a984708SDavid Chisnalltemplate <class _InputIterator, class _OutputIterator> 19707a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 19717a984708SDavid Chisnall_OutputIterator 19727a984708SDavid Chisnallmove(_InputIterator __first, _InputIterator __last, _OutputIterator __result) 19737a984708SDavid Chisnall{ 19747a984708SDavid Chisnall return _VSTD::__move(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result)); 19757a984708SDavid Chisnall} 19767a984708SDavid Chisnall 19777a984708SDavid Chisnall// move_backward 19787a984708SDavid Chisnall 19797a984708SDavid Chisnalltemplate <class _InputIterator, class _OutputIterator> 19807a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 19817a984708SDavid Chisnall_OutputIterator 19827a984708SDavid Chisnall__move_backward(_InputIterator __first, _InputIterator __last, _OutputIterator __result) 19837a984708SDavid Chisnall{ 19847a984708SDavid Chisnall while (__first != __last) 19857a984708SDavid Chisnall *--__result = _VSTD::move(*--__last); 19867a984708SDavid Chisnall return __result; 19877a984708SDavid Chisnall} 19887a984708SDavid Chisnall 19897a984708SDavid Chisnalltemplate <class _Tp, class _Up> 19907a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 19917a984708SDavid Chisnalltypename enable_if 19927a984708SDavid Chisnall< 19937a984708SDavid Chisnall is_same<typename remove_const<_Tp>::type, _Up>::value && 19947a984708SDavid Chisnall is_trivially_copy_assignable<_Up>::value, 19957a984708SDavid Chisnall _Up* 19967a984708SDavid Chisnall>::type 19977a984708SDavid Chisnall__move_backward(_Tp* __first, _Tp* __last, _Up* __result) 19987a984708SDavid Chisnall{ 19997a984708SDavid Chisnall const size_t __n = static_cast<size_t>(__last - __first); 2000854fa44bSDimitry Andric if (__n > 0) 2001854fa44bSDimitry Andric { 20027a984708SDavid Chisnall __result -= __n; 20037a984708SDavid Chisnall _VSTD::memmove(__result, __first, __n * sizeof(_Up)); 2004854fa44bSDimitry Andric } 20057a984708SDavid Chisnall return __result; 20067a984708SDavid Chisnall} 20077a984708SDavid Chisnall 20087a984708SDavid Chisnalltemplate <class _BidirectionalIterator1, class _BidirectionalIterator2> 20097a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 20107a984708SDavid Chisnall_BidirectionalIterator2 20117a984708SDavid Chisnallmove_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last, 20127a984708SDavid Chisnall _BidirectionalIterator2 __result) 20137a984708SDavid Chisnall{ 20147a984708SDavid Chisnall return _VSTD::__move_backward(__unwrap_iter(__first), __unwrap_iter(__last), __unwrap_iter(__result)); 20157a984708SDavid Chisnall} 20167a984708SDavid Chisnall 20177a984708SDavid Chisnall// iter_swap 20187a984708SDavid Chisnall 20197a984708SDavid Chisnall// moved to <type_traits> for better swap / noexcept support 20207a984708SDavid Chisnall 20217a984708SDavid Chisnall// transform 20227a984708SDavid Chisnall 20237a984708SDavid Chisnalltemplate <class _InputIterator, class _OutputIterator, class _UnaryOperation> 20247a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 20257a984708SDavid Chisnall_OutputIterator 20267a984708SDavid Chisnalltransform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op) 20277a984708SDavid Chisnall{ 2028d72607e9SDimitry Andric for (; __first != __last; ++__first, (void) ++__result) 20297a984708SDavid Chisnall *__result = __op(*__first); 20307a984708SDavid Chisnall return __result; 20317a984708SDavid Chisnall} 20327a984708SDavid Chisnall 20337a984708SDavid Chisnalltemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation> 20347a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 20357a984708SDavid Chisnall_OutputIterator 20367a984708SDavid Chisnalltransform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, 20377a984708SDavid Chisnall _OutputIterator __result, _BinaryOperation __binary_op) 20387a984708SDavid Chisnall{ 2039d72607e9SDimitry Andric for (; __first1 != __last1; ++__first1, (void) ++__first2, ++__result) 20407a984708SDavid Chisnall *__result = __binary_op(*__first1, *__first2); 20417a984708SDavid Chisnall return __result; 20427a984708SDavid Chisnall} 20437a984708SDavid Chisnall 20447a984708SDavid Chisnall// replace 20457a984708SDavid Chisnall 20467a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Tp> 20477a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 20487a984708SDavid Chisnallvoid 20497a984708SDavid Chisnallreplace(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __old_value, const _Tp& __new_value) 20507a984708SDavid Chisnall{ 20517a984708SDavid Chisnall for (; __first != __last; ++__first) 20527a984708SDavid Chisnall if (*__first == __old_value) 20537a984708SDavid Chisnall *__first = __new_value; 20547a984708SDavid Chisnall} 20557a984708SDavid Chisnall 20567a984708SDavid Chisnall// replace_if 20577a984708SDavid Chisnall 20587a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Predicate, class _Tp> 20597a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 20607a984708SDavid Chisnallvoid 20617a984708SDavid Chisnallreplace_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, const _Tp& __new_value) 20627a984708SDavid Chisnall{ 20637a984708SDavid Chisnall for (; __first != __last; ++__first) 20647a984708SDavid Chisnall if (__pred(*__first)) 20657a984708SDavid Chisnall *__first = __new_value; 20667a984708SDavid Chisnall} 20677a984708SDavid Chisnall 20687a984708SDavid Chisnall// replace_copy 20697a984708SDavid Chisnall 20707a984708SDavid Chisnalltemplate <class _InputIterator, class _OutputIterator, class _Tp> 20717a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 20727a984708SDavid Chisnall_OutputIterator 20737a984708SDavid Chisnallreplace_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, 20747a984708SDavid Chisnall const _Tp& __old_value, const _Tp& __new_value) 20757a984708SDavid Chisnall{ 2076d72607e9SDimitry Andric for (; __first != __last; ++__first, (void) ++__result) 20777a984708SDavid Chisnall if (*__first == __old_value) 20787a984708SDavid Chisnall *__result = __new_value; 20797a984708SDavid Chisnall else 20807a984708SDavid Chisnall *__result = *__first; 20817a984708SDavid Chisnall return __result; 20827a984708SDavid Chisnall} 20837a984708SDavid Chisnall 20847a984708SDavid Chisnall// replace_copy_if 20857a984708SDavid Chisnall 20867a984708SDavid Chisnalltemplate <class _InputIterator, class _OutputIterator, class _Predicate, class _Tp> 20877a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 20887a984708SDavid Chisnall_OutputIterator 20897a984708SDavid Chisnallreplace_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, 20907a984708SDavid Chisnall _Predicate __pred, const _Tp& __new_value) 20917a984708SDavid Chisnall{ 2092d72607e9SDimitry Andric for (; __first != __last; ++__first, (void) ++__result) 20937a984708SDavid Chisnall if (__pred(*__first)) 20947a984708SDavid Chisnall *__result = __new_value; 20957a984708SDavid Chisnall else 20967a984708SDavid Chisnall *__result = *__first; 20977a984708SDavid Chisnall return __result; 20987a984708SDavid Chisnall} 20997a984708SDavid Chisnall 21007a984708SDavid Chisnall// fill_n 21017a984708SDavid Chisnall 21027a984708SDavid Chisnalltemplate <class _OutputIterator, class _Size, class _Tp> 21037a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 21047a984708SDavid Chisnall_OutputIterator 21054f7ab58eSDimitry Andric__fill_n(_OutputIterator __first, _Size __n, const _Tp& __value_) 21067a984708SDavid Chisnall{ 2107d72607e9SDimitry Andric for (; __n > 0; ++__first, (void) --__n) 21087a984708SDavid Chisnall *__first = __value_; 21097a984708SDavid Chisnall return __first; 21107a984708SDavid Chisnall} 21117a984708SDavid Chisnall 21124f7ab58eSDimitry Andrictemplate <class _Tp, class _Size, class _Up> 21137a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 21144f7ab58eSDimitry Andrictypename enable_if 21154f7ab58eSDimitry Andric< 21164f7ab58eSDimitry Andric is_integral<_Tp>::value && sizeof(_Tp) == 1 && 21174f7ab58eSDimitry Andric !is_same<_Tp, bool>::value && 21184f7ab58eSDimitry Andric is_integral<_Up>::value && sizeof(_Up) == 1, 21194f7ab58eSDimitry Andric _Tp* 21204f7ab58eSDimitry Andric>::type 21214f7ab58eSDimitry Andric__fill_n(_Tp* __first, _Size __n,_Up __value_) 21227a984708SDavid Chisnall{ 21237a984708SDavid Chisnall if (__n > 0) 21247a984708SDavid Chisnall _VSTD::memset(__first, (unsigned char)__value_, (size_t)(__n)); 21257a984708SDavid Chisnall return __first + __n; 21267a984708SDavid Chisnall} 21277a984708SDavid Chisnall 21287a984708SDavid Chisnalltemplate <class _OutputIterator, class _Size, class _Tp> 21297a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 21307a984708SDavid Chisnall_OutputIterator 21317a984708SDavid Chisnallfill_n(_OutputIterator __first, _Size __n, const _Tp& __value_) 21327a984708SDavid Chisnall{ 2133854fa44bSDimitry Andric return _VSTD::__fill_n(__first, __convert_to_integral(__n), __value_); 21347a984708SDavid Chisnall} 21357a984708SDavid Chisnall 21367a984708SDavid Chisnall// fill 21377a984708SDavid Chisnall 21387a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Tp> 21397a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 21407a984708SDavid Chisnallvoid 21417a984708SDavid Chisnall__fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, forward_iterator_tag) 21427a984708SDavid Chisnall{ 21437a984708SDavid Chisnall for (; __first != __last; ++__first) 21447a984708SDavid Chisnall *__first = __value_; 21457a984708SDavid Chisnall} 21467a984708SDavid Chisnall 21477a984708SDavid Chisnalltemplate <class _RandomAccessIterator, class _Tp> 21487a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 21497a984708SDavid Chisnallvoid 21507a984708SDavid Chisnall__fill(_RandomAccessIterator __first, _RandomAccessIterator __last, const _Tp& __value_, random_access_iterator_tag) 21517a984708SDavid Chisnall{ 21527a984708SDavid Chisnall _VSTD::fill_n(__first, __last - __first, __value_); 21537a984708SDavid Chisnall} 21547a984708SDavid Chisnall 21557a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Tp> 21567a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 21577a984708SDavid Chisnallvoid 21587a984708SDavid Chisnallfill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_) 21597a984708SDavid Chisnall{ 21607a984708SDavid Chisnall _VSTD::__fill(__first, __last, __value_, typename iterator_traits<_ForwardIterator>::iterator_category()); 21617a984708SDavid Chisnall} 21627a984708SDavid Chisnall 21637a984708SDavid Chisnall// generate 21647a984708SDavid Chisnall 21657a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Generator> 21667a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 21677a984708SDavid Chisnallvoid 21687a984708SDavid Chisnallgenerate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen) 21697a984708SDavid Chisnall{ 21707a984708SDavid Chisnall for (; __first != __last; ++__first) 21717a984708SDavid Chisnall *__first = __gen(); 21727a984708SDavid Chisnall} 21737a984708SDavid Chisnall 21747a984708SDavid Chisnall// generate_n 21757a984708SDavid Chisnall 21767a984708SDavid Chisnalltemplate <class _OutputIterator, class _Size, class _Generator> 21777a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 21787a984708SDavid Chisnall_OutputIterator 2179854fa44bSDimitry Andricgenerate_n(_OutputIterator __first, _Size __orig_n, _Generator __gen) 21807a984708SDavid Chisnall{ 2181854fa44bSDimitry Andric typedef decltype(__convert_to_integral(__orig_n)) _IntegralSize; 2182854fa44bSDimitry Andric _IntegralSize __n = __orig_n; 2183d72607e9SDimitry Andric for (; __n > 0; ++__first, (void) --__n) 21847a984708SDavid Chisnall *__first = __gen(); 21857a984708SDavid Chisnall return __first; 21867a984708SDavid Chisnall} 21877a984708SDavid Chisnall 21887a984708SDavid Chisnall// remove 21897a984708SDavid Chisnall 21907a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Tp> 21917a984708SDavid Chisnall_ForwardIterator 21927a984708SDavid Chisnallremove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_) 21937a984708SDavid Chisnall{ 21947a984708SDavid Chisnall __first = _VSTD::find(__first, __last, __value_); 21957a984708SDavid Chisnall if (__first != __last) 21967a984708SDavid Chisnall { 21977a984708SDavid Chisnall _ForwardIterator __i = __first; 21987a984708SDavid Chisnall while (++__i != __last) 21997a984708SDavid Chisnall { 22007a984708SDavid Chisnall if (!(*__i == __value_)) 22017a984708SDavid Chisnall { 22027a984708SDavid Chisnall *__first = _VSTD::move(*__i); 22037a984708SDavid Chisnall ++__first; 22047a984708SDavid Chisnall } 22057a984708SDavid Chisnall } 22067a984708SDavid Chisnall } 22077a984708SDavid Chisnall return __first; 22087a984708SDavid Chisnall} 22097a984708SDavid Chisnall 22107a984708SDavid Chisnall// remove_if 22117a984708SDavid Chisnall 22127a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Predicate> 22137a984708SDavid Chisnall_ForwardIterator 22147a984708SDavid Chisnallremove_if(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) 22157a984708SDavid Chisnall{ 22167a984708SDavid Chisnall __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type> 22177a984708SDavid Chisnall (__first, __last, __pred); 22187a984708SDavid Chisnall if (__first != __last) 22197a984708SDavid Chisnall { 22207a984708SDavid Chisnall _ForwardIterator __i = __first; 22217a984708SDavid Chisnall while (++__i != __last) 22227a984708SDavid Chisnall { 22237a984708SDavid Chisnall if (!__pred(*__i)) 22247a984708SDavid Chisnall { 22257a984708SDavid Chisnall *__first = _VSTD::move(*__i); 22267a984708SDavid Chisnall ++__first; 22277a984708SDavid Chisnall } 22287a984708SDavid Chisnall } 22297a984708SDavid Chisnall } 22307a984708SDavid Chisnall return __first; 22317a984708SDavid Chisnall} 22327a984708SDavid Chisnall 22337a984708SDavid Chisnall// remove_copy 22347a984708SDavid Chisnall 22357a984708SDavid Chisnalltemplate <class _InputIterator, class _OutputIterator, class _Tp> 22367a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 22377a984708SDavid Chisnall_OutputIterator 22387a984708SDavid Chisnallremove_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, const _Tp& __value_) 22397a984708SDavid Chisnall{ 22407a984708SDavid Chisnall for (; __first != __last; ++__first) 22417a984708SDavid Chisnall { 22427a984708SDavid Chisnall if (!(*__first == __value_)) 22437a984708SDavid Chisnall { 22447a984708SDavid Chisnall *__result = *__first; 22457a984708SDavid Chisnall ++__result; 22467a984708SDavid Chisnall } 22477a984708SDavid Chisnall } 22487a984708SDavid Chisnall return __result; 22497a984708SDavid Chisnall} 22507a984708SDavid Chisnall 22517a984708SDavid Chisnall// remove_copy_if 22527a984708SDavid Chisnall 22537a984708SDavid Chisnalltemplate <class _InputIterator, class _OutputIterator, class _Predicate> 22547a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 22557a984708SDavid Chisnall_OutputIterator 22567a984708SDavid Chisnallremove_copy_if(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _Predicate __pred) 22577a984708SDavid Chisnall{ 22587a984708SDavid Chisnall for (; __first != __last; ++__first) 22597a984708SDavid Chisnall { 22607a984708SDavid Chisnall if (!__pred(*__first)) 22617a984708SDavid Chisnall { 22627a984708SDavid Chisnall *__result = *__first; 22637a984708SDavid Chisnall ++__result; 22647a984708SDavid Chisnall } 22657a984708SDavid Chisnall } 22667a984708SDavid Chisnall return __result; 22677a984708SDavid Chisnall} 22687a984708SDavid Chisnall 22697a984708SDavid Chisnall// unique 22707a984708SDavid Chisnall 22717a984708SDavid Chisnalltemplate <class _ForwardIterator, class _BinaryPredicate> 22727a984708SDavid Chisnall_ForwardIterator 22737a984708SDavid Chisnallunique(_ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __pred) 22747a984708SDavid Chisnall{ 22757a984708SDavid Chisnall __first = _VSTD::adjacent_find<_ForwardIterator, typename add_lvalue_reference<_BinaryPredicate>::type> 22767a984708SDavid Chisnall (__first, __last, __pred); 22777a984708SDavid Chisnall if (__first != __last) 22787a984708SDavid Chisnall { 22797a984708SDavid Chisnall // ... a a ? ... 22807a984708SDavid Chisnall // f i 22817a984708SDavid Chisnall _ForwardIterator __i = __first; 22827a984708SDavid Chisnall for (++__i; ++__i != __last;) 22837a984708SDavid Chisnall if (!__pred(*__first, *__i)) 22847a984708SDavid Chisnall *++__first = _VSTD::move(*__i); 22857a984708SDavid Chisnall ++__first; 22867a984708SDavid Chisnall } 22877a984708SDavid Chisnall return __first; 22887a984708SDavid Chisnall} 22897a984708SDavid Chisnall 22907a984708SDavid Chisnalltemplate <class _ForwardIterator> 22917a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 22927a984708SDavid Chisnall_ForwardIterator 22937a984708SDavid Chisnallunique(_ForwardIterator __first, _ForwardIterator __last) 22947a984708SDavid Chisnall{ 22957a984708SDavid Chisnall typedef typename iterator_traits<_ForwardIterator>::value_type __v; 22967a984708SDavid Chisnall return _VSTD::unique(__first, __last, __equal_to<__v>()); 22977a984708SDavid Chisnall} 22987a984708SDavid Chisnall 22997a984708SDavid Chisnall// unique_copy 23007a984708SDavid Chisnall 23017a984708SDavid Chisnalltemplate <class _BinaryPredicate, class _InputIterator, class _OutputIterator> 23027a984708SDavid Chisnall_OutputIterator 23037a984708SDavid Chisnall__unique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred, 23047a984708SDavid Chisnall input_iterator_tag, output_iterator_tag) 23057a984708SDavid Chisnall{ 23067a984708SDavid Chisnall if (__first != __last) 23077a984708SDavid Chisnall { 23087a984708SDavid Chisnall typename iterator_traits<_InputIterator>::value_type __t(*__first); 23097a984708SDavid Chisnall *__result = __t; 23107a984708SDavid Chisnall ++__result; 23117a984708SDavid Chisnall while (++__first != __last) 23127a984708SDavid Chisnall { 23137a984708SDavid Chisnall if (!__pred(__t, *__first)) 23147a984708SDavid Chisnall { 23157a984708SDavid Chisnall __t = *__first; 23167a984708SDavid Chisnall *__result = __t; 23177a984708SDavid Chisnall ++__result; 23187a984708SDavid Chisnall } 23197a984708SDavid Chisnall } 23207a984708SDavid Chisnall } 23217a984708SDavid Chisnall return __result; 23227a984708SDavid Chisnall} 23237a984708SDavid Chisnall 23247a984708SDavid Chisnalltemplate <class _BinaryPredicate, class _ForwardIterator, class _OutputIterator> 23257a984708SDavid Chisnall_OutputIterator 23267a984708SDavid Chisnall__unique_copy(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __result, _BinaryPredicate __pred, 23277a984708SDavid Chisnall forward_iterator_tag, output_iterator_tag) 23287a984708SDavid Chisnall{ 23297a984708SDavid Chisnall if (__first != __last) 23307a984708SDavid Chisnall { 23317a984708SDavid Chisnall _ForwardIterator __i = __first; 23327a984708SDavid Chisnall *__result = *__i; 23337a984708SDavid Chisnall ++__result; 23347a984708SDavid Chisnall while (++__first != __last) 23357a984708SDavid Chisnall { 23367a984708SDavid Chisnall if (!__pred(*__i, *__first)) 23377a984708SDavid Chisnall { 23387a984708SDavid Chisnall *__result = *__first; 23397a984708SDavid Chisnall ++__result; 23407a984708SDavid Chisnall __i = __first; 23417a984708SDavid Chisnall } 23427a984708SDavid Chisnall } 23437a984708SDavid Chisnall } 23447a984708SDavid Chisnall return __result; 23457a984708SDavid Chisnall} 23467a984708SDavid Chisnall 23477a984708SDavid Chisnalltemplate <class _BinaryPredicate, class _InputIterator, class _ForwardIterator> 23487a984708SDavid Chisnall_ForwardIterator 23497a984708SDavid Chisnall__unique_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result, _BinaryPredicate __pred, 23507a984708SDavid Chisnall input_iterator_tag, forward_iterator_tag) 23517a984708SDavid Chisnall{ 23527a984708SDavid Chisnall if (__first != __last) 23537a984708SDavid Chisnall { 23547a984708SDavid Chisnall *__result = *__first; 23557a984708SDavid Chisnall while (++__first != __last) 23567a984708SDavid Chisnall if (!__pred(*__result, *__first)) 23577a984708SDavid Chisnall *++__result = *__first; 23587a984708SDavid Chisnall ++__result; 23597a984708SDavid Chisnall } 23607a984708SDavid Chisnall return __result; 23617a984708SDavid Chisnall} 23627a984708SDavid Chisnall 23637a984708SDavid Chisnalltemplate <class _InputIterator, class _OutputIterator, class _BinaryPredicate> 23647a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 23657a984708SDavid Chisnall_OutputIterator 23667a984708SDavid Chisnallunique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _BinaryPredicate __pred) 23677a984708SDavid Chisnall{ 23687a984708SDavid Chisnall return _VSTD::__unique_copy<typename add_lvalue_reference<_BinaryPredicate>::type> 23697a984708SDavid Chisnall (__first, __last, __result, __pred, 23707a984708SDavid Chisnall typename iterator_traits<_InputIterator>::iterator_category(), 23717a984708SDavid Chisnall typename iterator_traits<_OutputIterator>::iterator_category()); 23727a984708SDavid Chisnall} 23737a984708SDavid Chisnall 23747a984708SDavid Chisnalltemplate <class _InputIterator, class _OutputIterator> 23757a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 23767a984708SDavid Chisnall_OutputIterator 23777a984708SDavid Chisnallunique_copy(_InputIterator __first, _InputIterator __last, _OutputIterator __result) 23787a984708SDavid Chisnall{ 23797a984708SDavid Chisnall typedef typename iterator_traits<_InputIterator>::value_type __v; 23807a984708SDavid Chisnall return _VSTD::unique_copy(__first, __last, __result, __equal_to<__v>()); 23817a984708SDavid Chisnall} 23827a984708SDavid Chisnall 23837a984708SDavid Chisnall// reverse 23847a984708SDavid Chisnall 23857a984708SDavid Chisnalltemplate <class _BidirectionalIterator> 23867a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 23877a984708SDavid Chisnallvoid 23887a984708SDavid Chisnall__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag) 23897a984708SDavid Chisnall{ 23907a984708SDavid Chisnall while (__first != __last) 23917a984708SDavid Chisnall { 23927a984708SDavid Chisnall if (__first == --__last) 23937a984708SDavid Chisnall break; 23949729cf09SDimitry Andric _VSTD::iter_swap(__first, __last); 23957a984708SDavid Chisnall ++__first; 23967a984708SDavid Chisnall } 23977a984708SDavid Chisnall} 23987a984708SDavid Chisnall 23997a984708SDavid Chisnalltemplate <class _RandomAccessIterator> 24007a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 24017a984708SDavid Chisnallvoid 24027a984708SDavid Chisnall__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag) 24037a984708SDavid Chisnall{ 24047a984708SDavid Chisnall if (__first != __last) 24057a984708SDavid Chisnall for (; __first < --__last; ++__first) 24069729cf09SDimitry Andric _VSTD::iter_swap(__first, __last); 24077a984708SDavid Chisnall} 24087a984708SDavid Chisnall 24097a984708SDavid Chisnalltemplate <class _BidirectionalIterator> 24107a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 24117a984708SDavid Chisnallvoid 24127a984708SDavid Chisnallreverse(_BidirectionalIterator __first, _BidirectionalIterator __last) 24137a984708SDavid Chisnall{ 24147a984708SDavid Chisnall _VSTD::__reverse(__first, __last, typename iterator_traits<_BidirectionalIterator>::iterator_category()); 24157a984708SDavid Chisnall} 24167a984708SDavid Chisnall 24177a984708SDavid Chisnall// reverse_copy 24187a984708SDavid Chisnall 24197a984708SDavid Chisnalltemplate <class _BidirectionalIterator, class _OutputIterator> 24207a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 24217a984708SDavid Chisnall_OutputIterator 24227a984708SDavid Chisnallreverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, _OutputIterator __result) 24237a984708SDavid Chisnall{ 24247a984708SDavid Chisnall for (; __first != __last; ++__result) 24257a984708SDavid Chisnall *__result = *--__last; 24267a984708SDavid Chisnall return __result; 24277a984708SDavid Chisnall} 24287a984708SDavid Chisnall 24297a984708SDavid Chisnall// rotate 24307a984708SDavid Chisnall 24317a984708SDavid Chisnalltemplate <class _ForwardIterator> 24327a984708SDavid Chisnall_ForwardIterator 2433936e9439SDimitry Andric__rotate_left(_ForwardIterator __first, _ForwardIterator __last) 24347a984708SDavid Chisnall{ 2435936e9439SDimitry Andric typedef typename iterator_traits<_ForwardIterator>::value_type value_type; 2436936e9439SDimitry Andric value_type __tmp = _VSTD::move(*__first); 2437936e9439SDimitry Andric _ForwardIterator __lm1 = _VSTD::move(_VSTD::next(__first), __last, __first); 2438936e9439SDimitry Andric *__lm1 = _VSTD::move(__tmp); 2439936e9439SDimitry Andric return __lm1; 2440936e9439SDimitry Andric} 2441936e9439SDimitry Andric 2442936e9439SDimitry Andrictemplate <class _BidirectionalIterator> 2443936e9439SDimitry Andric_BidirectionalIterator 2444936e9439SDimitry Andric__rotate_right(_BidirectionalIterator __first, _BidirectionalIterator __last) 2445936e9439SDimitry Andric{ 2446936e9439SDimitry Andric typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type; 2447936e9439SDimitry Andric _BidirectionalIterator __lm1 = _VSTD::prev(__last); 2448936e9439SDimitry Andric value_type __tmp = _VSTD::move(*__lm1); 2449936e9439SDimitry Andric _BidirectionalIterator __fp1 = _VSTD::move_backward(__first, __lm1, __last); 2450936e9439SDimitry Andric *__first = _VSTD::move(__tmp); 2451936e9439SDimitry Andric return __fp1; 2452936e9439SDimitry Andric} 2453936e9439SDimitry Andric 2454936e9439SDimitry Andrictemplate <class _ForwardIterator> 2455936e9439SDimitry Andric_ForwardIterator 2456936e9439SDimitry Andric__rotate_forward(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last) 2457936e9439SDimitry Andric{ 24587a984708SDavid Chisnall _ForwardIterator __i = __middle; 24597a984708SDavid Chisnall while (true) 24607a984708SDavid Chisnall { 24617a984708SDavid Chisnall swap(*__first, *__i); 24627a984708SDavid Chisnall ++__first; 24637a984708SDavid Chisnall if (++__i == __last) 24647a984708SDavid Chisnall break; 24657a984708SDavid Chisnall if (__first == __middle) 24667a984708SDavid Chisnall __middle = __i; 24677a984708SDavid Chisnall } 24687a984708SDavid Chisnall _ForwardIterator __r = __first; 24697a984708SDavid Chisnall if (__first != __middle) 24707a984708SDavid Chisnall { 24717a984708SDavid Chisnall __i = __middle; 24727a984708SDavid Chisnall while (true) 24737a984708SDavid Chisnall { 24747a984708SDavid Chisnall swap(*__first, *__i); 24757a984708SDavid Chisnall ++__first; 24767a984708SDavid Chisnall if (++__i == __last) 24777a984708SDavid Chisnall { 24787a984708SDavid Chisnall if (__first == __middle) 24797a984708SDavid Chisnall break; 24807a984708SDavid Chisnall __i = __middle; 24817a984708SDavid Chisnall } 24827a984708SDavid Chisnall else if (__first == __middle) 24837a984708SDavid Chisnall __middle = __i; 24847a984708SDavid Chisnall } 24857a984708SDavid Chisnall } 24867a984708SDavid Chisnall return __r; 24877a984708SDavid Chisnall} 24887a984708SDavid Chisnall 24897a984708SDavid Chisnalltemplate<typename _Integral> 24907a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 24917a984708SDavid Chisnall_Integral 2492aed8d94eSDimitry Andric__algo_gcd(_Integral __x, _Integral __y) 24937a984708SDavid Chisnall{ 24947a984708SDavid Chisnall do 24957a984708SDavid Chisnall { 24967a984708SDavid Chisnall _Integral __t = __x % __y; 24977a984708SDavid Chisnall __x = __y; 24987a984708SDavid Chisnall __y = __t; 24997a984708SDavid Chisnall } while (__y); 25007a984708SDavid Chisnall return __x; 25017a984708SDavid Chisnall} 25027a984708SDavid Chisnall 25037a984708SDavid Chisnalltemplate<typename _RandomAccessIterator> 25047a984708SDavid Chisnall_RandomAccessIterator 2505936e9439SDimitry Andric__rotate_gcd(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last) 25067a984708SDavid Chisnall{ 25077a984708SDavid Chisnall typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; 25087a984708SDavid Chisnall typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type; 25097a984708SDavid Chisnall 25107a984708SDavid Chisnall const difference_type __m1 = __middle - __first; 25117a984708SDavid Chisnall const difference_type __m2 = __last - __middle; 25127a984708SDavid Chisnall if (__m1 == __m2) 25137a984708SDavid Chisnall { 25147a984708SDavid Chisnall _VSTD::swap_ranges(__first, __middle, __middle); 25157a984708SDavid Chisnall return __middle; 25167a984708SDavid Chisnall } 2517aed8d94eSDimitry Andric const difference_type __g = _VSTD::__algo_gcd(__m1, __m2); 25187a984708SDavid Chisnall for (_RandomAccessIterator __p = __first + __g; __p != __first;) 25197a984708SDavid Chisnall { 2520936e9439SDimitry Andric value_type __t(_VSTD::move(*--__p)); 25217a984708SDavid Chisnall _RandomAccessIterator __p1 = __p; 25227a984708SDavid Chisnall _RandomAccessIterator __p2 = __p1 + __m1; 25237a984708SDavid Chisnall do 25247a984708SDavid Chisnall { 2525936e9439SDimitry Andric *__p1 = _VSTD::move(*__p2); 25267a984708SDavid Chisnall __p1 = __p2; 25277a984708SDavid Chisnall const difference_type __d = __last - __p2; 25287a984708SDavid Chisnall if (__m1 < __d) 25297a984708SDavid Chisnall __p2 += __m1; 25307a984708SDavid Chisnall else 25317a984708SDavid Chisnall __p2 = __first + (__m1 - __d); 25327a984708SDavid Chisnall } while (__p2 != __p); 2533936e9439SDimitry Andric *__p1 = _VSTD::move(__t); 25347a984708SDavid Chisnall } 25357a984708SDavid Chisnall return __first + __m2; 25367a984708SDavid Chisnall} 25377a984708SDavid Chisnall 25387a984708SDavid Chisnalltemplate <class _ForwardIterator> 25397a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 25407a984708SDavid Chisnall_ForwardIterator 2541936e9439SDimitry Andric__rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, 2542936e9439SDimitry Andric _VSTD::forward_iterator_tag) 2543936e9439SDimitry Andric{ 2544936e9439SDimitry Andric typedef typename _VSTD::iterator_traits<_ForwardIterator>::value_type value_type; 2545936e9439SDimitry Andric if (_VSTD::is_trivially_move_assignable<value_type>::value) 2546936e9439SDimitry Andric { 2547936e9439SDimitry Andric if (_VSTD::next(__first) == __middle) 2548936e9439SDimitry Andric return _VSTD::__rotate_left(__first, __last); 2549936e9439SDimitry Andric } 2550936e9439SDimitry Andric return _VSTD::__rotate_forward(__first, __middle, __last); 2551936e9439SDimitry Andric} 2552936e9439SDimitry Andric 2553936e9439SDimitry Andrictemplate <class _BidirectionalIterator> 2554936e9439SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 2555936e9439SDimitry Andric_BidirectionalIterator 2556936e9439SDimitry Andric__rotate(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last, 2557936e9439SDimitry Andric _VSTD::bidirectional_iterator_tag) 2558936e9439SDimitry Andric{ 2559936e9439SDimitry Andric typedef typename _VSTD::iterator_traits<_BidirectionalIterator>::value_type value_type; 2560936e9439SDimitry Andric if (_VSTD::is_trivially_move_assignable<value_type>::value) 2561936e9439SDimitry Andric { 2562936e9439SDimitry Andric if (_VSTD::next(__first) == __middle) 2563936e9439SDimitry Andric return _VSTD::__rotate_left(__first, __last); 2564936e9439SDimitry Andric if (_VSTD::next(__middle) == __last) 2565936e9439SDimitry Andric return _VSTD::__rotate_right(__first, __last); 2566936e9439SDimitry Andric } 2567936e9439SDimitry Andric return _VSTD::__rotate_forward(__first, __middle, __last); 2568936e9439SDimitry Andric} 2569936e9439SDimitry Andric 2570936e9439SDimitry Andrictemplate <class _RandomAccessIterator> 2571936e9439SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 2572936e9439SDimitry Andric_RandomAccessIterator 2573936e9439SDimitry Andric__rotate(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last, 2574936e9439SDimitry Andric _VSTD::random_access_iterator_tag) 2575936e9439SDimitry Andric{ 2576936e9439SDimitry Andric typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::value_type value_type; 2577936e9439SDimitry Andric if (_VSTD::is_trivially_move_assignable<value_type>::value) 2578936e9439SDimitry Andric { 2579936e9439SDimitry Andric if (_VSTD::next(__first) == __middle) 2580936e9439SDimitry Andric return _VSTD::__rotate_left(__first, __last); 2581936e9439SDimitry Andric if (_VSTD::next(__middle) == __last) 2582936e9439SDimitry Andric return _VSTD::__rotate_right(__first, __last); 2583936e9439SDimitry Andric return _VSTD::__rotate_gcd(__first, __middle, __last); 2584936e9439SDimitry Andric } 2585936e9439SDimitry Andric return _VSTD::__rotate_forward(__first, __middle, __last); 2586936e9439SDimitry Andric} 2587936e9439SDimitry Andric 2588936e9439SDimitry Andrictemplate <class _ForwardIterator> 2589936e9439SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 2590936e9439SDimitry Andric_ForwardIterator 25917a984708SDavid Chisnallrotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last) 25927a984708SDavid Chisnall{ 2593936e9439SDimitry Andric if (__first == __middle) 2594936e9439SDimitry Andric return __last; 2595936e9439SDimitry Andric if (__middle == __last) 2596936e9439SDimitry Andric return __first; 25977a984708SDavid Chisnall return _VSTD::__rotate(__first, __middle, __last, 2598936e9439SDimitry Andric typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category()); 25997a984708SDavid Chisnall} 26007a984708SDavid Chisnall 26017a984708SDavid Chisnall// rotate_copy 26027a984708SDavid Chisnall 26037a984708SDavid Chisnalltemplate <class _ForwardIterator, class _OutputIterator> 26047a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 26057a984708SDavid Chisnall_OutputIterator 26067a984708SDavid Chisnallrotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last, _OutputIterator __result) 26077a984708SDavid Chisnall{ 26087a984708SDavid Chisnall return _VSTD::copy(__first, __middle, _VSTD::copy(__middle, __last, __result)); 26097a984708SDavid Chisnall} 26107a984708SDavid Chisnall 26117a984708SDavid Chisnall// min_element 26127a984708SDavid Chisnall 26137a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Compare> 2614d72607e9SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 26157a984708SDavid Chisnall_ForwardIterator 2616854fa44bSDimitry Andricmin_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) 26177a984708SDavid Chisnall{ 26187a984708SDavid Chisnall if (__first != __last) 26197a984708SDavid Chisnall { 26207a984708SDavid Chisnall _ForwardIterator __i = __first; 26217a984708SDavid Chisnall while (++__i != __last) 26227a984708SDavid Chisnall if (__comp(*__i, *__first)) 26237a984708SDavid Chisnall __first = __i; 26247a984708SDavid Chisnall } 26257a984708SDavid Chisnall return __first; 26267a984708SDavid Chisnall} 26277a984708SDavid Chisnall 26287a984708SDavid Chisnalltemplate <class _ForwardIterator> 2629854fa44bSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 26307a984708SDavid Chisnall_ForwardIterator 26317a984708SDavid Chisnallmin_element(_ForwardIterator __first, _ForwardIterator __last) 26327a984708SDavid Chisnall{ 2633854fa44bSDimitry Andric return _VSTD::min_element(__first, __last, 26347a984708SDavid Chisnall __less<typename iterator_traits<_ForwardIterator>::value_type>()); 26357a984708SDavid Chisnall} 26367a984708SDavid Chisnall 26377a984708SDavid Chisnall// min 26387a984708SDavid Chisnall 26397a984708SDavid Chisnalltemplate <class _Tp, class _Compare> 2640d72607e9SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 26417a984708SDavid Chisnallconst _Tp& 26427a984708SDavid Chisnallmin(const _Tp& __a, const _Tp& __b, _Compare __comp) 26437a984708SDavid Chisnall{ 26447a984708SDavid Chisnall return __comp(__b, __a) ? __b : __a; 26457a984708SDavid Chisnall} 26467a984708SDavid Chisnall 26477a984708SDavid Chisnalltemplate <class _Tp> 2648d72607e9SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 26497a984708SDavid Chisnallconst _Tp& 26507a984708SDavid Chisnallmin(const _Tp& __a, const _Tp& __b) 26517a984708SDavid Chisnall{ 26527a984708SDavid Chisnall return _VSTD::min(__a, __b, __less<_Tp>()); 26537a984708SDavid Chisnall} 26547a984708SDavid Chisnall 2655540d2a8bSDimitry Andric#ifndef _LIBCPP_CXX03_LANG 26567a984708SDavid Chisnall 26577a984708SDavid Chisnalltemplate<class _Tp, class _Compare> 2658d72607e9SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 26597a984708SDavid Chisnall_Tp 26607a984708SDavid Chisnallmin(initializer_list<_Tp> __t, _Compare __comp) 26617a984708SDavid Chisnall{ 2662854fa44bSDimitry Andric return *_VSTD::min_element(__t.begin(), __t.end(), __comp); 26637a984708SDavid Chisnall} 26647a984708SDavid Chisnall 26657a984708SDavid Chisnalltemplate<class _Tp> 2666d72607e9SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 26677a984708SDavid Chisnall_Tp 26687a984708SDavid Chisnallmin(initializer_list<_Tp> __t) 26697a984708SDavid Chisnall{ 2670854fa44bSDimitry Andric return *_VSTD::min_element(__t.begin(), __t.end(), __less<_Tp>()); 26717a984708SDavid Chisnall} 26727a984708SDavid Chisnall 2673540d2a8bSDimitry Andric#endif // _LIBCPP_CXX03_LANG 26747a984708SDavid Chisnall 26757a984708SDavid Chisnall// max_element 26767a984708SDavid Chisnall 26777a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Compare> 2678d72607e9SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 26797a984708SDavid Chisnall_ForwardIterator 2680854fa44bSDimitry Andricmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) 26817a984708SDavid Chisnall{ 26827a984708SDavid Chisnall if (__first != __last) 26837a984708SDavid Chisnall { 26847a984708SDavid Chisnall _ForwardIterator __i = __first; 26857a984708SDavid Chisnall while (++__i != __last) 26867a984708SDavid Chisnall if (__comp(*__first, *__i)) 26877a984708SDavid Chisnall __first = __i; 26887a984708SDavid Chisnall } 26897a984708SDavid Chisnall return __first; 26907a984708SDavid Chisnall} 26917a984708SDavid Chisnall 2692d72607e9SDimitry Andric 26937a984708SDavid Chisnalltemplate <class _ForwardIterator> 2694854fa44bSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 26957a984708SDavid Chisnall_ForwardIterator 26967a984708SDavid Chisnallmax_element(_ForwardIterator __first, _ForwardIterator __last) 26977a984708SDavid Chisnall{ 2698854fa44bSDimitry Andric return _VSTD::max_element(__first, __last, 26997a984708SDavid Chisnall __less<typename iterator_traits<_ForwardIterator>::value_type>()); 27007a984708SDavid Chisnall} 27017a984708SDavid Chisnall 27027a984708SDavid Chisnall// max 27037a984708SDavid Chisnall 27047a984708SDavid Chisnalltemplate <class _Tp, class _Compare> 2705d72607e9SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 27067a984708SDavid Chisnallconst _Tp& 27077a984708SDavid Chisnallmax(const _Tp& __a, const _Tp& __b, _Compare __comp) 27087a984708SDavid Chisnall{ 27097a984708SDavid Chisnall return __comp(__a, __b) ? __b : __a; 27107a984708SDavid Chisnall} 27117a984708SDavid Chisnall 27127a984708SDavid Chisnalltemplate <class _Tp> 2713d72607e9SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 27147a984708SDavid Chisnallconst _Tp& 27157a984708SDavid Chisnallmax(const _Tp& __a, const _Tp& __b) 27167a984708SDavid Chisnall{ 27177a984708SDavid Chisnall return _VSTD::max(__a, __b, __less<_Tp>()); 27187a984708SDavid Chisnall} 27197a984708SDavid Chisnall 2720540d2a8bSDimitry Andric#ifndef _LIBCPP_CXX03_LANG 27217a984708SDavid Chisnall 27227a984708SDavid Chisnalltemplate<class _Tp, class _Compare> 2723d72607e9SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 27247a984708SDavid Chisnall_Tp 27257a984708SDavid Chisnallmax(initializer_list<_Tp> __t, _Compare __comp) 27267a984708SDavid Chisnall{ 2727854fa44bSDimitry Andric return *_VSTD::max_element(__t.begin(), __t.end(), __comp); 27287a984708SDavid Chisnall} 27297a984708SDavid Chisnall 27307a984708SDavid Chisnalltemplate<class _Tp> 2731d72607e9SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 27327a984708SDavid Chisnall_Tp 27337a984708SDavid Chisnallmax(initializer_list<_Tp> __t) 27347a984708SDavid Chisnall{ 2735854fa44bSDimitry Andric return *_VSTD::max_element(__t.begin(), __t.end(), __less<_Tp>()); 27367a984708SDavid Chisnall} 27377a984708SDavid Chisnall 2738540d2a8bSDimitry Andric#endif // _LIBCPP_CXX03_LANG 27397a984708SDavid Chisnall 27407c82a1ecSDimitry Andric#if _LIBCPP_STD_VER > 14 27417c82a1ecSDimitry Andric// clamp 27427c82a1ecSDimitry Andrictemplate<class _Tp, class _Compare> 27437c82a1ecSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR 27447c82a1ecSDimitry Andricconst _Tp& 27457c82a1ecSDimitry Andricclamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp) 27467c82a1ecSDimitry Andric{ 27477c82a1ecSDimitry Andric _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp"); 27487c82a1ecSDimitry Andric return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v; 27497c82a1ecSDimitry Andric 27507c82a1ecSDimitry Andric} 27517c82a1ecSDimitry Andric 27527c82a1ecSDimitry Andrictemplate<class _Tp> 27537c82a1ecSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR 27547c82a1ecSDimitry Andricconst _Tp& 27557c82a1ecSDimitry Andricclamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi) 27567c82a1ecSDimitry Andric{ 27577c82a1ecSDimitry Andric return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>()); 27587c82a1ecSDimitry Andric} 27597c82a1ecSDimitry Andric#endif 27607c82a1ecSDimitry Andric 27617a984708SDavid Chisnall// minmax_element 27627a984708SDavid Chisnall 27637a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Compare> 2764854fa44bSDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX11 27657a984708SDavid Chisnallstd::pair<_ForwardIterator, _ForwardIterator> 27667a984708SDavid Chisnallminmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) 27677a984708SDavid Chisnall{ 27687a984708SDavid Chisnall std::pair<_ForwardIterator, _ForwardIterator> __result(__first, __first); 27697a984708SDavid Chisnall if (__first != __last) 27707a984708SDavid Chisnall { 27717a984708SDavid Chisnall if (++__first != __last) 27727a984708SDavid Chisnall { 27737a984708SDavid Chisnall if (__comp(*__first, *__result.first)) 27747a984708SDavid Chisnall __result.first = __first; 27757a984708SDavid Chisnall else 27767a984708SDavid Chisnall __result.second = __first; 27777a984708SDavid Chisnall while (++__first != __last) 27787a984708SDavid Chisnall { 27797a984708SDavid Chisnall _ForwardIterator __i = __first; 27807a984708SDavid Chisnall if (++__first == __last) 27817a984708SDavid Chisnall { 27827a984708SDavid Chisnall if (__comp(*__i, *__result.first)) 27837a984708SDavid Chisnall __result.first = __i; 27847a984708SDavid Chisnall else if (!__comp(*__i, *__result.second)) 27857a984708SDavid Chisnall __result.second = __i; 27867a984708SDavid Chisnall break; 27877a984708SDavid Chisnall } 27887a984708SDavid Chisnall else 27897a984708SDavid Chisnall { 27907a984708SDavid Chisnall if (__comp(*__first, *__i)) 27917a984708SDavid Chisnall { 27927a984708SDavid Chisnall if (__comp(*__first, *__result.first)) 27937a984708SDavid Chisnall __result.first = __first; 27947a984708SDavid Chisnall if (!__comp(*__i, *__result.second)) 27957a984708SDavid Chisnall __result.second = __i; 27967a984708SDavid Chisnall } 27977a984708SDavid Chisnall else 27987a984708SDavid Chisnall { 27997a984708SDavid Chisnall if (__comp(*__i, *__result.first)) 28007a984708SDavid Chisnall __result.first = __i; 28017a984708SDavid Chisnall if (!__comp(*__first, *__result.second)) 28027a984708SDavid Chisnall __result.second = __first; 28037a984708SDavid Chisnall } 28047a984708SDavid Chisnall } 28057a984708SDavid Chisnall } 28067a984708SDavid Chisnall } 28077a984708SDavid Chisnall } 28087a984708SDavid Chisnall return __result; 28097a984708SDavid Chisnall} 28107a984708SDavid Chisnall 28117a984708SDavid Chisnalltemplate <class _ForwardIterator> 2812854fa44bSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 28137a984708SDavid Chisnallstd::pair<_ForwardIterator, _ForwardIterator> 28147a984708SDavid Chisnallminmax_element(_ForwardIterator __first, _ForwardIterator __last) 28157a984708SDavid Chisnall{ 2816d72607e9SDimitry Andric return _VSTD::minmax_element(__first, __last, 2817d72607e9SDimitry Andric __less<typename iterator_traits<_ForwardIterator>::value_type>()); 28187a984708SDavid Chisnall} 28197a984708SDavid Chisnall 28207a984708SDavid Chisnall// minmax 28217a984708SDavid Chisnall 28227a984708SDavid Chisnalltemplate<class _Tp, class _Compare> 2823d72607e9SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 28247a984708SDavid Chisnallpair<const _Tp&, const _Tp&> 28257a984708SDavid Chisnallminmax(const _Tp& __a, const _Tp& __b, _Compare __comp) 28267a984708SDavid Chisnall{ 28277a984708SDavid Chisnall return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) : 28287a984708SDavid Chisnall pair<const _Tp&, const _Tp&>(__a, __b); 28297a984708SDavid Chisnall} 28307a984708SDavid Chisnall 28317a984708SDavid Chisnalltemplate<class _Tp> 2832d72607e9SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 28337a984708SDavid Chisnallpair<const _Tp&, const _Tp&> 28347a984708SDavid Chisnallminmax(const _Tp& __a, const _Tp& __b) 28357a984708SDavid Chisnall{ 28367a984708SDavid Chisnall return _VSTD::minmax(__a, __b, __less<_Tp>()); 28377a984708SDavid Chisnall} 28387a984708SDavid Chisnall 2839540d2a8bSDimitry Andric#ifndef _LIBCPP_CXX03_LANG 28407a984708SDavid Chisnall 28417a984708SDavid Chisnalltemplate<class _Tp, class _Compare> 2842d72607e9SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 28437a984708SDavid Chisnallpair<_Tp, _Tp> 28447a984708SDavid Chisnallminmax(initializer_list<_Tp> __t, _Compare __comp) 28457a984708SDavid Chisnall{ 2846d72607e9SDimitry Andric typedef typename initializer_list<_Tp>::const_iterator _Iter; 2847d72607e9SDimitry Andric _Iter __first = __t.begin(); 2848d72607e9SDimitry Andric _Iter __last = __t.end(); 2849d72607e9SDimitry Andric std::pair<_Tp, _Tp> __result(*__first, *__first); 2850d72607e9SDimitry Andric 2851d72607e9SDimitry Andric ++__first; 2852d72607e9SDimitry Andric if (__t.size() % 2 == 0) 2853d72607e9SDimitry Andric { 2854d72607e9SDimitry Andric if (__comp(*__first, __result.first)) 2855d72607e9SDimitry Andric __result.first = *__first; 2856d72607e9SDimitry Andric else 2857d72607e9SDimitry Andric __result.second = *__first; 2858d72607e9SDimitry Andric ++__first; 2859d72607e9SDimitry Andric } 2860d72607e9SDimitry Andric 2861d72607e9SDimitry Andric while (__first != __last) 2862d72607e9SDimitry Andric { 2863d72607e9SDimitry Andric _Tp __prev = *__first++; 2864854fa44bSDimitry Andric if (__comp(*__first, __prev)) { 2865854fa44bSDimitry Andric if ( __comp(*__first, __result.first)) __result.first = *__first; 2866854fa44bSDimitry Andric if (!__comp(__prev, __result.second)) __result.second = __prev; 2867d72607e9SDimitry Andric } 2868d72607e9SDimitry Andric else { 2869854fa44bSDimitry Andric if ( __comp(__prev, __result.first)) __result.first = __prev; 2870854fa44bSDimitry Andric if (!__comp(*__first, __result.second)) __result.second = *__first; 2871d72607e9SDimitry Andric } 2872d72607e9SDimitry Andric 2873d72607e9SDimitry Andric __first++; 2874d72607e9SDimitry Andric } 2875d72607e9SDimitry Andric return __result; 2876d72607e9SDimitry Andric} 2877d72607e9SDimitry Andric 2878d72607e9SDimitry Andrictemplate<class _Tp> 2879d72607e9SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 2880d72607e9SDimitry Andricpair<_Tp, _Tp> 2881d72607e9SDimitry Andricminmax(initializer_list<_Tp> __t) 2882d72607e9SDimitry Andric{ 2883d72607e9SDimitry Andric return _VSTD::minmax(__t, __less<_Tp>()); 28847a984708SDavid Chisnall} 28857a984708SDavid Chisnall 2886540d2a8bSDimitry Andric#endif // _LIBCPP_CXX03_LANG 28877a984708SDavid Chisnall 28887a984708SDavid Chisnall// random_shuffle 28897a984708SDavid Chisnall 28907a984708SDavid Chisnall// __independent_bits_engine 28917a984708SDavid Chisnall 289294e3ee44SDavid Chisnalltemplate <unsigned long long _Xp, size_t _Rp> 28937a984708SDavid Chisnallstruct __log2_imp 28947a984708SDavid Chisnall{ 289594e3ee44SDavid Chisnall static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp 289694e3ee44SDavid Chisnall : __log2_imp<_Xp, _Rp - 1>::value; 28977a984708SDavid Chisnall}; 28987a984708SDavid Chisnall 289994e3ee44SDavid Chisnalltemplate <unsigned long long _Xp> 290094e3ee44SDavid Chisnallstruct __log2_imp<_Xp, 0> 29017a984708SDavid Chisnall{ 29027a984708SDavid Chisnall static const size_t value = 0; 29037a984708SDavid Chisnall}; 29047a984708SDavid Chisnall 290594e3ee44SDavid Chisnalltemplate <size_t _Rp> 290694e3ee44SDavid Chisnallstruct __log2_imp<0, _Rp> 29077a984708SDavid Chisnall{ 290894e3ee44SDavid Chisnall static const size_t value = _Rp + 1; 29097a984708SDavid Chisnall}; 29107a984708SDavid Chisnall 291194e3ee44SDavid Chisnalltemplate <class _UI, _UI _Xp> 29127a984708SDavid Chisnallstruct __log2 29137a984708SDavid Chisnall{ 291494e3ee44SDavid Chisnall static const size_t value = __log2_imp<_Xp, 29157a984708SDavid Chisnall sizeof(_UI) * __CHAR_BIT__ - 1>::value; 29167a984708SDavid Chisnall}; 29177a984708SDavid Chisnall 29187a984708SDavid Chisnalltemplate<class _Engine, class _UIntType> 29197a984708SDavid Chisnallclass __independent_bits_engine 29207a984708SDavid Chisnall{ 29217a984708SDavid Chisnallpublic: 29227a984708SDavid Chisnall // types 29237a984708SDavid Chisnall typedef _UIntType result_type; 29247a984708SDavid Chisnall 29257a984708SDavid Chisnallprivate: 29267a984708SDavid Chisnall typedef typename _Engine::result_type _Engine_result_type; 29277a984708SDavid Chisnall typedef typename conditional 29287a984708SDavid Chisnall < 29297a984708SDavid Chisnall sizeof(_Engine_result_type) <= sizeof(result_type), 29307a984708SDavid Chisnall result_type, 29317a984708SDavid Chisnall _Engine_result_type 29327a984708SDavid Chisnall >::type _Working_result_type; 29337a984708SDavid Chisnall 29347a984708SDavid Chisnall _Engine& __e_; 29357a984708SDavid Chisnall size_t __w_; 29367a984708SDavid Chisnall size_t __w0_; 29377a984708SDavid Chisnall size_t __n_; 29387a984708SDavid Chisnall size_t __n0_; 29397a984708SDavid Chisnall _Working_result_type __y0_; 29407a984708SDavid Chisnall _Working_result_type __y1_; 29417a984708SDavid Chisnall _Engine_result_type __mask0_; 29427a984708SDavid Chisnall _Engine_result_type __mask1_; 29437a984708SDavid Chisnall 2944540d2a8bSDimitry Andric#ifdef _LIBCPP_CXX03_LANG 294594e3ee44SDavid Chisnall static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min 29467a984708SDavid Chisnall + _Working_result_type(1); 2947b03f91a8SDavid Chisnall#else 2948b03f91a8SDavid Chisnall static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min() 2949b03f91a8SDavid Chisnall + _Working_result_type(1); 2950b03f91a8SDavid Chisnall#endif 2951b03f91a8SDavid Chisnall static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value; 2952b03f91a8SDavid Chisnall static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits; 2953b03f91a8SDavid Chisnall static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits; 29547a984708SDavid Chisnall 29557a984708SDavid Chisnallpublic: 29567a984708SDavid Chisnall // constructors and seeding functions 29577a984708SDavid Chisnall __independent_bits_engine(_Engine& __e, size_t __w); 29587a984708SDavid Chisnall 29597a984708SDavid Chisnall // generating functions 296094e3ee44SDavid Chisnall result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());} 29617a984708SDavid Chisnall 29627a984708SDavid Chisnallprivate: 29637a984708SDavid Chisnall result_type __eval(false_type); 29647a984708SDavid Chisnall result_type __eval(true_type); 29657a984708SDavid Chisnall}; 29667a984708SDavid Chisnall 29677a984708SDavid Chisnalltemplate<class _Engine, class _UIntType> 29687a984708SDavid Chisnall__independent_bits_engine<_Engine, _UIntType> 29697a984708SDavid Chisnall ::__independent_bits_engine(_Engine& __e, size_t __w) 29707a984708SDavid Chisnall : __e_(__e), 29717a984708SDavid Chisnall __w_(__w) 29727a984708SDavid Chisnall{ 29737a984708SDavid Chisnall __n_ = __w_ / __m + (__w_ % __m != 0); 29747a984708SDavid Chisnall __w0_ = __w_ / __n_; 297594e3ee44SDavid Chisnall if (_Rp == 0) 297694e3ee44SDavid Chisnall __y0_ = _Rp; 29777a984708SDavid Chisnall else if (__w0_ < _WDt) 297894e3ee44SDavid Chisnall __y0_ = (_Rp >> __w0_) << __w0_; 29797a984708SDavid Chisnall else 29807a984708SDavid Chisnall __y0_ = 0; 298194e3ee44SDavid Chisnall if (_Rp - __y0_ > __y0_ / __n_) 29827a984708SDavid Chisnall { 29837a984708SDavid Chisnall ++__n_; 29847a984708SDavid Chisnall __w0_ = __w_ / __n_; 29857a984708SDavid Chisnall if (__w0_ < _WDt) 298694e3ee44SDavid Chisnall __y0_ = (_Rp >> __w0_) << __w0_; 29877a984708SDavid Chisnall else 29887a984708SDavid Chisnall __y0_ = 0; 29897a984708SDavid Chisnall } 29907a984708SDavid Chisnall __n0_ = __n_ - __w_ % __n_; 29917a984708SDavid Chisnall if (__w0_ < _WDt - 1) 299294e3ee44SDavid Chisnall __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1); 29937a984708SDavid Chisnall else 29947a984708SDavid Chisnall __y1_ = 0; 29957a984708SDavid Chisnall __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) : 29967a984708SDavid Chisnall _Engine_result_type(0); 29977a984708SDavid Chisnall __mask1_ = __w0_ < _EDt - 1 ? 29987a984708SDavid Chisnall _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) : 29997a984708SDavid Chisnall _Engine_result_type(~0); 30007a984708SDavid Chisnall} 30017a984708SDavid Chisnall 30027a984708SDavid Chisnalltemplate<class _Engine, class _UIntType> 30037a984708SDavid Chisnallinline 30047a984708SDavid Chisnall_UIntType 30057a984708SDavid Chisnall__independent_bits_engine<_Engine, _UIntType>::__eval(false_type) 30067a984708SDavid Chisnall{ 30077a984708SDavid Chisnall return static_cast<result_type>(__e_() & __mask0_); 30087a984708SDavid Chisnall} 30097a984708SDavid Chisnall 30107a984708SDavid Chisnalltemplate<class _Engine, class _UIntType> 30117a984708SDavid Chisnall_UIntType 30127a984708SDavid Chisnall__independent_bits_engine<_Engine, _UIntType>::__eval(true_type) 30137a984708SDavid Chisnall{ 301494e3ee44SDavid Chisnall result_type _Sp = 0; 30157a984708SDavid Chisnall for (size_t __k = 0; __k < __n0_; ++__k) 30167a984708SDavid Chisnall { 30177a984708SDavid Chisnall _Engine_result_type __u; 30187a984708SDavid Chisnall do 30197a984708SDavid Chisnall { 30207a984708SDavid Chisnall __u = __e_() - _Engine::min(); 30217a984708SDavid Chisnall } while (__u >= __y0_); 30227a984708SDavid Chisnall if (__w0_ < _WDt) 302394e3ee44SDavid Chisnall _Sp <<= __w0_; 30247a984708SDavid Chisnall else 302594e3ee44SDavid Chisnall _Sp = 0; 302694e3ee44SDavid Chisnall _Sp += __u & __mask0_; 30277a984708SDavid Chisnall } 30287a984708SDavid Chisnall for (size_t __k = __n0_; __k < __n_; ++__k) 30297a984708SDavid Chisnall { 30307a984708SDavid Chisnall _Engine_result_type __u; 30317a984708SDavid Chisnall do 30327a984708SDavid Chisnall { 30337a984708SDavid Chisnall __u = __e_() - _Engine::min(); 30347a984708SDavid Chisnall } while (__u >= __y1_); 30357a984708SDavid Chisnall if (__w0_ < _WDt - 1) 303694e3ee44SDavid Chisnall _Sp <<= __w0_ + 1; 30377a984708SDavid Chisnall else 303894e3ee44SDavid Chisnall _Sp = 0; 303994e3ee44SDavid Chisnall _Sp += __u & __mask1_; 30407a984708SDavid Chisnall } 304194e3ee44SDavid Chisnall return _Sp; 30427a984708SDavid Chisnall} 30437a984708SDavid Chisnall 30447a984708SDavid Chisnall// uniform_int_distribution 30457a984708SDavid Chisnall 30467a984708SDavid Chisnalltemplate<class _IntType = int> 30477a984708SDavid Chisnallclass uniform_int_distribution 30487a984708SDavid Chisnall{ 30497a984708SDavid Chisnallpublic: 30507a984708SDavid Chisnall // types 30517a984708SDavid Chisnall typedef _IntType result_type; 30527a984708SDavid Chisnall 30537a984708SDavid Chisnall class param_type 30547a984708SDavid Chisnall { 30557a984708SDavid Chisnall result_type __a_; 30567a984708SDavid Chisnall result_type __b_; 30577a984708SDavid Chisnall public: 30587a984708SDavid Chisnall typedef uniform_int_distribution distribution_type; 30597a984708SDavid Chisnall 30607a984708SDavid Chisnall explicit param_type(result_type __a = 0, 30617a984708SDavid Chisnall result_type __b = numeric_limits<result_type>::max()) 30627a984708SDavid Chisnall : __a_(__a), __b_(__b) {} 30637a984708SDavid Chisnall 30647a984708SDavid Chisnall result_type a() const {return __a_;} 30657a984708SDavid Chisnall result_type b() const {return __b_;} 30667a984708SDavid Chisnall 30677a984708SDavid Chisnall friend bool operator==(const param_type& __x, const param_type& __y) 30687a984708SDavid Chisnall {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;} 30697a984708SDavid Chisnall friend bool operator!=(const param_type& __x, const param_type& __y) 30707a984708SDavid Chisnall {return !(__x == __y);} 30717a984708SDavid Chisnall }; 30727a984708SDavid Chisnall 30737a984708SDavid Chisnallprivate: 30747a984708SDavid Chisnall param_type __p_; 30757a984708SDavid Chisnall 30767a984708SDavid Chisnallpublic: 30777a984708SDavid Chisnall // constructors and reset functions 30787a984708SDavid Chisnall explicit uniform_int_distribution(result_type __a = 0, 30797a984708SDavid Chisnall result_type __b = numeric_limits<result_type>::max()) 30807a984708SDavid Chisnall : __p_(param_type(__a, __b)) {} 30817a984708SDavid Chisnall explicit uniform_int_distribution(const param_type& __p) : __p_(__p) {} 30827a984708SDavid Chisnall void reset() {} 30837a984708SDavid Chisnall 30847a984708SDavid Chisnall // generating functions 30857a984708SDavid Chisnall template<class _URNG> result_type operator()(_URNG& __g) 30867a984708SDavid Chisnall {return (*this)(__g, __p_);} 30877a984708SDavid Chisnall template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p); 30887a984708SDavid Chisnall 30897a984708SDavid Chisnall // property functions 30907a984708SDavid Chisnall result_type a() const {return __p_.a();} 30917a984708SDavid Chisnall result_type b() const {return __p_.b();} 30927a984708SDavid Chisnall 30937a984708SDavid Chisnall param_type param() const {return __p_;} 30947a984708SDavid Chisnall void param(const param_type& __p) {__p_ = __p;} 30957a984708SDavid Chisnall 30967a984708SDavid Chisnall result_type min() const {return a();} 30977a984708SDavid Chisnall result_type max() const {return b();} 30987a984708SDavid Chisnall 30997a984708SDavid Chisnall friend bool operator==(const uniform_int_distribution& __x, 31007a984708SDavid Chisnall const uniform_int_distribution& __y) 31017a984708SDavid Chisnall {return __x.__p_ == __y.__p_;} 31027a984708SDavid Chisnall friend bool operator!=(const uniform_int_distribution& __x, 31037a984708SDavid Chisnall const uniform_int_distribution& __y) 31047a984708SDavid Chisnall {return !(__x == __y);} 31057a984708SDavid Chisnall}; 31067a984708SDavid Chisnall 31077a984708SDavid Chisnalltemplate<class _IntType> 31087a984708SDavid Chisnalltemplate<class _URNG> 31097a984708SDavid Chisnalltypename uniform_int_distribution<_IntType>::result_type 31107a984708SDavid Chisnalluniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p) 31117a984708SDavid Chisnall{ 31127a984708SDavid Chisnall typedef typename conditional<sizeof(result_type) <= sizeof(uint32_t), 31137a984708SDavid Chisnall uint32_t, uint64_t>::type _UIntType; 311494e3ee44SDavid Chisnall const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1); 311594e3ee44SDavid Chisnall if (_Rp == 1) 31167a984708SDavid Chisnall return __p.a(); 31177a984708SDavid Chisnall const size_t _Dt = numeric_limits<_UIntType>::digits; 31187a984708SDavid Chisnall typedef __independent_bits_engine<_URNG, _UIntType> _Eng; 311994e3ee44SDavid Chisnall if (_Rp == 0) 31207a984708SDavid Chisnall return static_cast<result_type>(_Eng(__g, _Dt)()); 312194e3ee44SDavid Chisnall size_t __w = _Dt - __clz(_Rp) - 1; 31229729cf09SDimitry Andric if ((_Rp & (std::numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0) 31237a984708SDavid Chisnall ++__w; 31247a984708SDavid Chisnall _Eng __e(__g, __w); 31257a984708SDavid Chisnall _UIntType __u; 31267a984708SDavid Chisnall do 31277a984708SDavid Chisnall { 31287a984708SDavid Chisnall __u = __e(); 312994e3ee44SDavid Chisnall } while (__u >= _Rp); 31307a984708SDavid Chisnall return static_cast<result_type>(__u + __p.a()); 31317a984708SDavid Chisnall} 31327a984708SDavid Chisnall 3133540d2a8bSDimitry Andric#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE) \ 3134540d2a8bSDimitry Andric || defined(_LIBCPP_BUILDING_LIBRARY) 31354f7ab58eSDimitry Andricclass _LIBCPP_TYPE_VIS __rs_default; 31367a984708SDavid Chisnall 31374f7ab58eSDimitry Andric_LIBCPP_FUNC_VIS __rs_default __rs_get(); 31387a984708SDavid Chisnall 31394f7ab58eSDimitry Andricclass _LIBCPP_TYPE_VIS __rs_default 31407a984708SDavid Chisnall{ 31417a984708SDavid Chisnall static unsigned __c_; 31427a984708SDavid Chisnall 31437a984708SDavid Chisnall __rs_default(); 31447a984708SDavid Chisnallpublic: 31451bf9f7c1SDimitry Andric typedef uint_fast32_t result_type; 31467a984708SDavid Chisnall 31477a984708SDavid Chisnall static const result_type _Min = 0; 31487a984708SDavid Chisnall static const result_type _Max = 0xFFFFFFFF; 31497a984708SDavid Chisnall 31507a984708SDavid Chisnall __rs_default(const __rs_default&); 31517a984708SDavid Chisnall ~__rs_default(); 31527a984708SDavid Chisnall 31537a984708SDavid Chisnall result_type operator()(); 31547a984708SDavid Chisnall 3155b03f91a8SDavid Chisnall static _LIBCPP_CONSTEXPR result_type min() {return _Min;} 3156b03f91a8SDavid Chisnall static _LIBCPP_CONSTEXPR result_type max() {return _Max;} 31577a984708SDavid Chisnall 31584f7ab58eSDimitry Andric friend _LIBCPP_FUNC_VIS __rs_default __rs_get(); 31597a984708SDavid Chisnall}; 31607a984708SDavid Chisnall 31614f7ab58eSDimitry Andric_LIBCPP_FUNC_VIS __rs_default __rs_get(); 31627a984708SDavid Chisnall 31637a984708SDavid Chisnalltemplate <class _RandomAccessIterator> 31647a984708SDavid Chisnallvoid 31657a984708SDavid Chisnallrandom_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last) 31667a984708SDavid Chisnall{ 31677a984708SDavid Chisnall typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; 316894e3ee44SDavid Chisnall typedef uniform_int_distribution<ptrdiff_t> _Dp; 316994e3ee44SDavid Chisnall typedef typename _Dp::param_type _Pp; 31707a984708SDavid Chisnall difference_type __d = __last - __first; 31717a984708SDavid Chisnall if (__d > 1) 31727a984708SDavid Chisnall { 317394e3ee44SDavid Chisnall _Dp __uid; 31747a984708SDavid Chisnall __rs_default __g = __rs_get(); 31757a984708SDavid Chisnall for (--__last, --__d; __first < __last; ++__first, --__d) 31767a984708SDavid Chisnall { 317794e3ee44SDavid Chisnall difference_type __i = __uid(__g, _Pp(0, __d)); 31787a984708SDavid Chisnall if (__i != difference_type(0)) 31797a984708SDavid Chisnall swap(*__first, *(__first + __i)); 31807a984708SDavid Chisnall } 31817a984708SDavid Chisnall } 31827a984708SDavid Chisnall} 31837a984708SDavid Chisnall 31847a984708SDavid Chisnalltemplate <class _RandomAccessIterator, class _RandomNumberGenerator> 31857a984708SDavid Chisnallvoid 31867a984708SDavid Chisnallrandom_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last, 3187540d2a8bSDimitry Andric#ifndef _LIBCPP_CXX03_LANG 31887a984708SDavid Chisnall _RandomNumberGenerator&& __rand) 31897a984708SDavid Chisnall#else 31907a984708SDavid Chisnall _RandomNumberGenerator& __rand) 31917a984708SDavid Chisnall#endif 31927a984708SDavid Chisnall{ 31937a984708SDavid Chisnall typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; 31947a984708SDavid Chisnall difference_type __d = __last - __first; 31957a984708SDavid Chisnall if (__d > 1) 31967a984708SDavid Chisnall { 31977a984708SDavid Chisnall for (--__last; __first < __last; ++__first, --__d) 31987a984708SDavid Chisnall { 31997a984708SDavid Chisnall difference_type __i = __rand(__d); 32007a984708SDavid Chisnall swap(*__first, *(__first + __i)); 32017a984708SDavid Chisnall } 32027a984708SDavid Chisnall } 32037a984708SDavid Chisnall} 3204540d2a8bSDimitry Andric#endif 32057a984708SDavid Chisnall 3206aed8d94eSDimitry Andrictemplate <class _PopulationIterator, class _SampleIterator, class _Distance, 3207aed8d94eSDimitry Andric class _UniformRandomNumberGenerator> 3208aed8d94eSDimitry Andric_LIBCPP_INLINE_VISIBILITY 3209aed8d94eSDimitry Andric_SampleIterator __sample(_PopulationIterator __first, 321080779b37SDimitry Andric _PopulationIterator __last, _SampleIterator __output, 3211aed8d94eSDimitry Andric _Distance __n, 3212aed8d94eSDimitry Andric _UniformRandomNumberGenerator & __g, 3213aed8d94eSDimitry Andric input_iterator_tag) { 3214aed8d94eSDimitry Andric 3215aed8d94eSDimitry Andric _Distance __k = 0; 3216aed8d94eSDimitry Andric for (; __first != __last && __k < __n; ++__first, (void)++__k) 321780779b37SDimitry Andric __output[__k] = *__first; 3218aed8d94eSDimitry Andric _Distance __sz = __k; 3219aed8d94eSDimitry Andric for (; __first != __last; ++__first, (void)++__k) { 3220aed8d94eSDimitry Andric _Distance __r = _VSTD::uniform_int_distribution<_Distance>(0, __k)(__g); 3221aed8d94eSDimitry Andric if (__r < __sz) 322280779b37SDimitry Andric __output[__r] = *__first; 3223aed8d94eSDimitry Andric } 322480779b37SDimitry Andric return __output + _VSTD::min(__n, __k); 3225aed8d94eSDimitry Andric} 3226aed8d94eSDimitry Andric 3227aed8d94eSDimitry Andrictemplate <class _PopulationIterator, class _SampleIterator, class _Distance, 3228aed8d94eSDimitry Andric class _UniformRandomNumberGenerator> 3229aed8d94eSDimitry Andric_LIBCPP_INLINE_VISIBILITY 3230aed8d94eSDimitry Andric_SampleIterator __sample(_PopulationIterator __first, 323180779b37SDimitry Andric _PopulationIterator __last, _SampleIterator __output, 3232aed8d94eSDimitry Andric _Distance __n, 3233aed8d94eSDimitry Andric _UniformRandomNumberGenerator& __g, 3234aed8d94eSDimitry Andric forward_iterator_tag) { 3235aed8d94eSDimitry Andric _Distance __unsampled_sz = _VSTD::distance(__first, __last); 3236aed8d94eSDimitry Andric for (__n = _VSTD::min(__n, __unsampled_sz); __n != 0; ++__first) { 3237aed8d94eSDimitry Andric _Distance __r = 3238aed8d94eSDimitry Andric _VSTD::uniform_int_distribution<_Distance>(0, --__unsampled_sz)(__g); 3239aed8d94eSDimitry Andric if (__r < __n) { 324080779b37SDimitry Andric *__output++ = *__first; 3241aed8d94eSDimitry Andric --__n; 3242aed8d94eSDimitry Andric } 3243aed8d94eSDimitry Andric } 324480779b37SDimitry Andric return __output; 3245aed8d94eSDimitry Andric} 3246aed8d94eSDimitry Andric 3247aed8d94eSDimitry Andrictemplate <class _PopulationIterator, class _SampleIterator, class _Distance, 3248aed8d94eSDimitry Andric class _UniformRandomNumberGenerator> 3249aed8d94eSDimitry Andric_LIBCPP_INLINE_VISIBILITY 3250aed8d94eSDimitry Andric_SampleIterator __sample(_PopulationIterator __first, 325180779b37SDimitry Andric _PopulationIterator __last, _SampleIterator __output, 3252aed8d94eSDimitry Andric _Distance __n, _UniformRandomNumberGenerator& __g) { 3253aed8d94eSDimitry Andric typedef typename iterator_traits<_PopulationIterator>::iterator_category 3254aed8d94eSDimitry Andric _PopCategory; 3255aed8d94eSDimitry Andric typedef typename iterator_traits<_PopulationIterator>::difference_type 3256aed8d94eSDimitry Andric _Difference; 3257aed8d94eSDimitry Andric static_assert(__is_forward_iterator<_PopulationIterator>::value || 3258aed8d94eSDimitry Andric __is_random_access_iterator<_SampleIterator>::value, 3259aed8d94eSDimitry Andric "SampleIterator must meet the requirements of RandomAccessIterator"); 3260aed8d94eSDimitry Andric typedef typename common_type<_Distance, _Difference>::type _CommonType; 3261aed8d94eSDimitry Andric _LIBCPP_ASSERT(__n >= 0, "N must be a positive number."); 3262aed8d94eSDimitry Andric return _VSTD::__sample( 326380779b37SDimitry Andric __first, __last, __output, _CommonType(__n), 3264aed8d94eSDimitry Andric __g, _PopCategory()); 3265aed8d94eSDimitry Andric} 3266aed8d94eSDimitry Andric 3267aed8d94eSDimitry Andric#if _LIBCPP_STD_VER > 14 3268aed8d94eSDimitry Andrictemplate <class _PopulationIterator, class _SampleIterator, class _Distance, 3269aed8d94eSDimitry Andric class _UniformRandomNumberGenerator> 3270aed8d94eSDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 3271aed8d94eSDimitry Andric_SampleIterator sample(_PopulationIterator __first, 327280779b37SDimitry Andric _PopulationIterator __last, _SampleIterator __output, 3273aed8d94eSDimitry Andric _Distance __n, _UniformRandomNumberGenerator&& __g) { 327480779b37SDimitry Andric return _VSTD::__sample(__first, __last, __output, __n, __g); 3275aed8d94eSDimitry Andric} 3276aed8d94eSDimitry Andric#endif // _LIBCPP_STD_VER > 14 3277aed8d94eSDimitry Andric 32787a984708SDavid Chisnalltemplate<class _RandomAccessIterator, class _UniformRandomNumberGenerator> 32797a984708SDavid Chisnall void shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last, 3280540d2a8bSDimitry Andric#ifndef _LIBCPP_CXX03_LANG 32817a984708SDavid Chisnall _UniformRandomNumberGenerator&& __g) 32827a984708SDavid Chisnall#else 32837a984708SDavid Chisnall _UniformRandomNumberGenerator& __g) 32847a984708SDavid Chisnall#endif 32857a984708SDavid Chisnall{ 32867a984708SDavid Chisnall typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; 328794e3ee44SDavid Chisnall typedef uniform_int_distribution<ptrdiff_t> _Dp; 328894e3ee44SDavid Chisnall typedef typename _Dp::param_type _Pp; 32897a984708SDavid Chisnall difference_type __d = __last - __first; 32907a984708SDavid Chisnall if (__d > 1) 32917a984708SDavid Chisnall { 329294e3ee44SDavid Chisnall _Dp __uid; 32937a984708SDavid Chisnall for (--__last, --__d; __first < __last; ++__first, --__d) 32947a984708SDavid Chisnall { 329594e3ee44SDavid Chisnall difference_type __i = __uid(__g, _Pp(0, __d)); 32967a984708SDavid Chisnall if (__i != difference_type(0)) 32977a984708SDavid Chisnall swap(*__first, *(__first + __i)); 32987a984708SDavid Chisnall } 32997a984708SDavid Chisnall } 33007a984708SDavid Chisnall} 33017a984708SDavid Chisnall 33027a984708SDavid Chisnalltemplate <class _InputIterator, class _Predicate> 33037a984708SDavid Chisnallbool 33047a984708SDavid Chisnallis_partitioned(_InputIterator __first, _InputIterator __last, _Predicate __pred) 33057a984708SDavid Chisnall{ 33067a984708SDavid Chisnall for (; __first != __last; ++__first) 33077a984708SDavid Chisnall if (!__pred(*__first)) 33087a984708SDavid Chisnall break; 3309854fa44bSDimitry Andric if ( __first == __last ) 3310854fa44bSDimitry Andric return true; 3311854fa44bSDimitry Andric ++__first; 33127a984708SDavid Chisnall for (; __first != __last; ++__first) 33137a984708SDavid Chisnall if (__pred(*__first)) 33147a984708SDavid Chisnall return false; 33157a984708SDavid Chisnall return true; 33167a984708SDavid Chisnall} 33177a984708SDavid Chisnall 33187a984708SDavid Chisnall// partition 33197a984708SDavid Chisnall 33207a984708SDavid Chisnalltemplate <class _Predicate, class _ForwardIterator> 33217a984708SDavid Chisnall_ForwardIterator 33227a984708SDavid Chisnall__partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, forward_iterator_tag) 33237a984708SDavid Chisnall{ 33247a984708SDavid Chisnall while (true) 33257a984708SDavid Chisnall { 33267a984708SDavid Chisnall if (__first == __last) 33277a984708SDavid Chisnall return __first; 33287a984708SDavid Chisnall if (!__pred(*__first)) 33297a984708SDavid Chisnall break; 33307a984708SDavid Chisnall ++__first; 33317a984708SDavid Chisnall } 33327a984708SDavid Chisnall for (_ForwardIterator __p = __first; ++__p != __last;) 33337a984708SDavid Chisnall { 33347a984708SDavid Chisnall if (__pred(*__p)) 33357a984708SDavid Chisnall { 33367a984708SDavid Chisnall swap(*__first, *__p); 33377a984708SDavid Chisnall ++__first; 33387a984708SDavid Chisnall } 33397a984708SDavid Chisnall } 33407a984708SDavid Chisnall return __first; 33417a984708SDavid Chisnall} 33427a984708SDavid Chisnall 33437a984708SDavid Chisnalltemplate <class _Predicate, class _BidirectionalIterator> 33447a984708SDavid Chisnall_BidirectionalIterator 33457a984708SDavid Chisnall__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred, 33467a984708SDavid Chisnall bidirectional_iterator_tag) 33477a984708SDavid Chisnall{ 33487a984708SDavid Chisnall while (true) 33497a984708SDavid Chisnall { 33507a984708SDavid Chisnall while (true) 33517a984708SDavid Chisnall { 33527a984708SDavid Chisnall if (__first == __last) 33537a984708SDavid Chisnall return __first; 33547a984708SDavid Chisnall if (!__pred(*__first)) 33557a984708SDavid Chisnall break; 33567a984708SDavid Chisnall ++__first; 33577a984708SDavid Chisnall } 33587a984708SDavid Chisnall do 33597a984708SDavid Chisnall { 33607a984708SDavid Chisnall if (__first == --__last) 33617a984708SDavid Chisnall return __first; 33627a984708SDavid Chisnall } while (!__pred(*__last)); 33637a984708SDavid Chisnall swap(*__first, *__last); 33647a984708SDavid Chisnall ++__first; 33657a984708SDavid Chisnall } 33667a984708SDavid Chisnall} 33677a984708SDavid Chisnall 33687a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Predicate> 33697a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 33707a984708SDavid Chisnall_ForwardIterator 33717a984708SDavid Chisnallpartition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) 33727a984708SDavid Chisnall{ 33737a984708SDavid Chisnall return _VSTD::__partition<typename add_lvalue_reference<_Predicate>::type> 33747a984708SDavid Chisnall (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category()); 33757a984708SDavid Chisnall} 33767a984708SDavid Chisnall 33777a984708SDavid Chisnall// partition_copy 33787a984708SDavid Chisnall 33797a984708SDavid Chisnalltemplate <class _InputIterator, class _OutputIterator1, 33807a984708SDavid Chisnall class _OutputIterator2, class _Predicate> 33817a984708SDavid Chisnallpair<_OutputIterator1, _OutputIterator2> 33827a984708SDavid Chisnallpartition_copy(_InputIterator __first, _InputIterator __last, 33837a984708SDavid Chisnall _OutputIterator1 __out_true, _OutputIterator2 __out_false, 33847a984708SDavid Chisnall _Predicate __pred) 33857a984708SDavid Chisnall{ 33867a984708SDavid Chisnall for (; __first != __last; ++__first) 33877a984708SDavid Chisnall { 33887a984708SDavid Chisnall if (__pred(*__first)) 33897a984708SDavid Chisnall { 33907a984708SDavid Chisnall *__out_true = *__first; 33917a984708SDavid Chisnall ++__out_true; 33927a984708SDavid Chisnall } 33937a984708SDavid Chisnall else 33947a984708SDavid Chisnall { 33957a984708SDavid Chisnall *__out_false = *__first; 33967a984708SDavid Chisnall ++__out_false; 33977a984708SDavid Chisnall } 33987a984708SDavid Chisnall } 33997a984708SDavid Chisnall return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false); 34007a984708SDavid Chisnall} 34017a984708SDavid Chisnall 34027a984708SDavid Chisnall// partition_point 34037a984708SDavid Chisnall 34047a984708SDavid Chisnalltemplate<class _ForwardIterator, class _Predicate> 34057a984708SDavid Chisnall_ForwardIterator 34067a984708SDavid Chisnallpartition_point(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) 34077a984708SDavid Chisnall{ 34087a984708SDavid Chisnall typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type; 34097a984708SDavid Chisnall difference_type __len = _VSTD::distance(__first, __last); 34107a984708SDavid Chisnall while (__len != 0) 34117a984708SDavid Chisnall { 34127a984708SDavid Chisnall difference_type __l2 = __len / 2; 34137a984708SDavid Chisnall _ForwardIterator __m = __first; 34147a984708SDavid Chisnall _VSTD::advance(__m, __l2); 34157a984708SDavid Chisnall if (__pred(*__m)) 34167a984708SDavid Chisnall { 34177a984708SDavid Chisnall __first = ++__m; 34187a984708SDavid Chisnall __len -= __l2 + 1; 34197a984708SDavid Chisnall } 34207a984708SDavid Chisnall else 34217a984708SDavid Chisnall __len = __l2; 34227a984708SDavid Chisnall } 34237a984708SDavid Chisnall return __first; 34247a984708SDavid Chisnall} 34257a984708SDavid Chisnall 34267a984708SDavid Chisnall// stable_partition 34277a984708SDavid Chisnall 34287a984708SDavid Chisnalltemplate <class _Predicate, class _ForwardIterator, class _Distance, class _Pair> 34297a984708SDavid Chisnall_ForwardIterator 34307a984708SDavid Chisnall__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, 34317a984708SDavid Chisnall _Distance __len, _Pair __p, forward_iterator_tag __fit) 34327a984708SDavid Chisnall{ 34337a984708SDavid Chisnall // *__first is known to be false 34347a984708SDavid Chisnall // __len >= 1 34357a984708SDavid Chisnall if (__len == 1) 34367a984708SDavid Chisnall return __first; 34377a984708SDavid Chisnall if (__len == 2) 34387a984708SDavid Chisnall { 34397a984708SDavid Chisnall _ForwardIterator __m = __first; 34407a984708SDavid Chisnall if (__pred(*++__m)) 34417a984708SDavid Chisnall { 34427a984708SDavid Chisnall swap(*__first, *__m); 34437a984708SDavid Chisnall return __m; 34447a984708SDavid Chisnall } 34457a984708SDavid Chisnall return __first; 34467a984708SDavid Chisnall } 34477a984708SDavid Chisnall if (__len <= __p.second) 34487a984708SDavid Chisnall { // The buffer is big enough to use 34497a984708SDavid Chisnall typedef typename iterator_traits<_ForwardIterator>::value_type value_type; 34507a984708SDavid Chisnall __destruct_n __d(0); 34517a984708SDavid Chisnall unique_ptr<value_type, __destruct_n&> __h(__p.first, __d); 34527a984708SDavid Chisnall // Move the falses into the temporary buffer, and the trues to the front of the line 34537a984708SDavid Chisnall // Update __first to always point to the end of the trues 34547a984708SDavid Chisnall value_type* __t = __p.first; 34557a984708SDavid Chisnall ::new(__t) value_type(_VSTD::move(*__first)); 34567a984708SDavid Chisnall __d.__incr((value_type*)0); 34577a984708SDavid Chisnall ++__t; 34587a984708SDavid Chisnall _ForwardIterator __i = __first; 34597a984708SDavid Chisnall while (++__i != __last) 34607a984708SDavid Chisnall { 34617a984708SDavid Chisnall if (__pred(*__i)) 34627a984708SDavid Chisnall { 34637a984708SDavid Chisnall *__first = _VSTD::move(*__i); 34647a984708SDavid Chisnall ++__first; 34657a984708SDavid Chisnall } 34667a984708SDavid Chisnall else 34677a984708SDavid Chisnall { 34687a984708SDavid Chisnall ::new(__t) value_type(_VSTD::move(*__i)); 34697a984708SDavid Chisnall __d.__incr((value_type*)0); 34707a984708SDavid Chisnall ++__t; 34717a984708SDavid Chisnall } 34727a984708SDavid Chisnall } 34737a984708SDavid Chisnall // All trues now at start of range, all falses in buffer 34747a984708SDavid Chisnall // Move falses back into range, but don't mess up __first which points to first false 34757a984708SDavid Chisnall __i = __first; 34767a984708SDavid Chisnall for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i) 34777a984708SDavid Chisnall *__i = _VSTD::move(*__t2); 34787a984708SDavid Chisnall // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer 34797a984708SDavid Chisnall return __first; 34807a984708SDavid Chisnall } 34817a984708SDavid Chisnall // Else not enough buffer, do in place 34827a984708SDavid Chisnall // __len >= 3 34837a984708SDavid Chisnall _ForwardIterator __m = __first; 34847a984708SDavid Chisnall _Distance __len2 = __len / 2; // __len2 >= 2 34857a984708SDavid Chisnall _VSTD::advance(__m, __len2); 34867a984708SDavid Chisnall // recurse on [__first, __m), *__first know to be false 34877a984708SDavid Chisnall // F????????????????? 34887a984708SDavid Chisnall // f m l 34897a984708SDavid Chisnall typedef typename add_lvalue_reference<_Predicate>::type _PredRef; 34907a984708SDavid Chisnall _ForwardIterator __first_false = __stable_partition<_PredRef>(__first, __m, __pred, __len2, __p, __fit); 34917a984708SDavid Chisnall // TTTFFFFF?????????? 34927a984708SDavid Chisnall // f ff m l 34937a984708SDavid Chisnall // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true 34947a984708SDavid Chisnall _ForwardIterator __m1 = __m; 34957a984708SDavid Chisnall _ForwardIterator __second_false = __last; 34967a984708SDavid Chisnall _Distance __len_half = __len - __len2; 34977a984708SDavid Chisnall while (__pred(*__m1)) 34987a984708SDavid Chisnall { 34997a984708SDavid Chisnall if (++__m1 == __last) 35007a984708SDavid Chisnall goto __second_half_done; 35017a984708SDavid Chisnall --__len_half; 35027a984708SDavid Chisnall } 35037a984708SDavid Chisnall // TTTFFFFFTTTF?????? 35047a984708SDavid Chisnall // f ff m m1 l 35057a984708SDavid Chisnall __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __fit); 35067a984708SDavid Chisnall__second_half_done: 35077a984708SDavid Chisnall // TTTFFFFFTTTTTFFFFF 35087a984708SDavid Chisnall // f ff m sf l 35097a984708SDavid Chisnall return _VSTD::rotate(__first_false, __m, __second_false); 35107a984708SDavid Chisnall // TTTTTTTTFFFFFFFFFF 35117a984708SDavid Chisnall // | 35127a984708SDavid Chisnall} 35137a984708SDavid Chisnall 35147a984708SDavid Chisnallstruct __return_temporary_buffer 35157a984708SDavid Chisnall{ 35167a984708SDavid Chisnall template <class _Tp> 35177a984708SDavid Chisnall _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) const {_VSTD::return_temporary_buffer(__p);} 35187a984708SDavid Chisnall}; 35197a984708SDavid Chisnall 35207a984708SDavid Chisnalltemplate <class _Predicate, class _ForwardIterator> 35217a984708SDavid Chisnall_ForwardIterator 35227a984708SDavid Chisnall__stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred, 35237a984708SDavid Chisnall forward_iterator_tag) 35247a984708SDavid Chisnall{ 35257a984708SDavid Chisnall const unsigned __alloc_limit = 3; // might want to make this a function of trivial assignment 35267a984708SDavid Chisnall // Either prove all true and return __first or point to first false 35277a984708SDavid Chisnall while (true) 35287a984708SDavid Chisnall { 35297a984708SDavid Chisnall if (__first == __last) 35307a984708SDavid Chisnall return __first; 35317a984708SDavid Chisnall if (!__pred(*__first)) 35327a984708SDavid Chisnall break; 35337a984708SDavid Chisnall ++__first; 35347a984708SDavid Chisnall } 35357a984708SDavid Chisnall // We now have a reduced range [__first, __last) 35367a984708SDavid Chisnall // *__first is known to be false 35377a984708SDavid Chisnall typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type; 35387a984708SDavid Chisnall typedef typename iterator_traits<_ForwardIterator>::value_type value_type; 35397a984708SDavid Chisnall difference_type __len = _VSTD::distance(__first, __last); 35407a984708SDavid Chisnall pair<value_type*, ptrdiff_t> __p(0, 0); 35417a984708SDavid Chisnall unique_ptr<value_type, __return_temporary_buffer> __h; 35427a984708SDavid Chisnall if (__len >= __alloc_limit) 35437a984708SDavid Chisnall { 35447a984708SDavid Chisnall __p = _VSTD::get_temporary_buffer<value_type>(__len); 35457a984708SDavid Chisnall __h.reset(__p.first); 35467a984708SDavid Chisnall } 35477a984708SDavid Chisnall return __stable_partition<typename add_lvalue_reference<_Predicate>::type> 35487a984708SDavid Chisnall (__first, __last, __pred, __len, __p, forward_iterator_tag()); 35497a984708SDavid Chisnall} 35507a984708SDavid Chisnall 35517a984708SDavid Chisnalltemplate <class _Predicate, class _BidirectionalIterator, class _Distance, class _Pair> 35527a984708SDavid Chisnall_BidirectionalIterator 35537a984708SDavid Chisnall__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred, 35547a984708SDavid Chisnall _Distance __len, _Pair __p, bidirectional_iterator_tag __bit) 35557a984708SDavid Chisnall{ 35567a984708SDavid Chisnall // *__first is known to be false 35577a984708SDavid Chisnall // *__last is known to be true 35587a984708SDavid Chisnall // __len >= 2 35597a984708SDavid Chisnall if (__len == 2) 35607a984708SDavid Chisnall { 35617a984708SDavid Chisnall swap(*__first, *__last); 35627a984708SDavid Chisnall return __last; 35637a984708SDavid Chisnall } 35647a984708SDavid Chisnall if (__len == 3) 35657a984708SDavid Chisnall { 35667a984708SDavid Chisnall _BidirectionalIterator __m = __first; 35677a984708SDavid Chisnall if (__pred(*++__m)) 35687a984708SDavid Chisnall { 35697a984708SDavid Chisnall swap(*__first, *__m); 35707a984708SDavid Chisnall swap(*__m, *__last); 35717a984708SDavid Chisnall return __last; 35727a984708SDavid Chisnall } 35737a984708SDavid Chisnall swap(*__m, *__last); 35747a984708SDavid Chisnall swap(*__first, *__m); 35757a984708SDavid Chisnall return __m; 35767a984708SDavid Chisnall } 35777a984708SDavid Chisnall if (__len <= __p.second) 35787a984708SDavid Chisnall { // The buffer is big enough to use 35797a984708SDavid Chisnall typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type; 35807a984708SDavid Chisnall __destruct_n __d(0); 35817a984708SDavid Chisnall unique_ptr<value_type, __destruct_n&> __h(__p.first, __d); 35827a984708SDavid Chisnall // Move the falses into the temporary buffer, and the trues to the front of the line 35837a984708SDavid Chisnall // Update __first to always point to the end of the trues 35847a984708SDavid Chisnall value_type* __t = __p.first; 35857a984708SDavid Chisnall ::new(__t) value_type(_VSTD::move(*__first)); 35867a984708SDavid Chisnall __d.__incr((value_type*)0); 35877a984708SDavid Chisnall ++__t; 35887a984708SDavid Chisnall _BidirectionalIterator __i = __first; 35897a984708SDavid Chisnall while (++__i != __last) 35907a984708SDavid Chisnall { 35917a984708SDavid Chisnall if (__pred(*__i)) 35927a984708SDavid Chisnall { 35937a984708SDavid Chisnall *__first = _VSTD::move(*__i); 35947a984708SDavid Chisnall ++__first; 35957a984708SDavid Chisnall } 35967a984708SDavid Chisnall else 35977a984708SDavid Chisnall { 35987a984708SDavid Chisnall ::new(__t) value_type(_VSTD::move(*__i)); 35997a984708SDavid Chisnall __d.__incr((value_type*)0); 36007a984708SDavid Chisnall ++__t; 36017a984708SDavid Chisnall } 36027a984708SDavid Chisnall } 36037a984708SDavid Chisnall // move *__last, known to be true 36047a984708SDavid Chisnall *__first = _VSTD::move(*__i); 36057a984708SDavid Chisnall __i = ++__first; 36067a984708SDavid Chisnall // All trues now at start of range, all falses in buffer 36077a984708SDavid Chisnall // Move falses back into range, but don't mess up __first which points to first false 36087a984708SDavid Chisnall for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i) 36097a984708SDavid Chisnall *__i = _VSTD::move(*__t2); 36107a984708SDavid Chisnall // __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer 36117a984708SDavid Chisnall return __first; 36127a984708SDavid Chisnall } 36137a984708SDavid Chisnall // Else not enough buffer, do in place 36147a984708SDavid Chisnall // __len >= 4 36157a984708SDavid Chisnall _BidirectionalIterator __m = __first; 36167a984708SDavid Chisnall _Distance __len2 = __len / 2; // __len2 >= 2 36177a984708SDavid Chisnall _VSTD::advance(__m, __len2); 36187a984708SDavid Chisnall // recurse on [__first, __m-1], except reduce __m-1 until *(__m-1) is true, *__first know to be false 36197a984708SDavid Chisnall // F????????????????T 36207a984708SDavid Chisnall // f m l 36217a984708SDavid Chisnall _BidirectionalIterator __m1 = __m; 36227a984708SDavid Chisnall _BidirectionalIterator __first_false = __first; 36237a984708SDavid Chisnall _Distance __len_half = __len2; 36247a984708SDavid Chisnall while (!__pred(*--__m1)) 36257a984708SDavid Chisnall { 36267a984708SDavid Chisnall if (__m1 == __first) 36277a984708SDavid Chisnall goto __first_half_done; 36287a984708SDavid Chisnall --__len_half; 36297a984708SDavid Chisnall } 36307a984708SDavid Chisnall // F???TFFF?????????T 36317a984708SDavid Chisnall // f m1 m l 36327a984708SDavid Chisnall typedef typename add_lvalue_reference<_Predicate>::type _PredRef; 36337a984708SDavid Chisnall __first_false = __stable_partition<_PredRef>(__first, __m1, __pred, __len_half, __p, __bit); 36347a984708SDavid Chisnall__first_half_done: 36357a984708SDavid Chisnall // TTTFFFFF?????????T 36367a984708SDavid Chisnall // f ff m l 36377a984708SDavid Chisnall // recurse on [__m, __last], except increase __m until *(__m) is false, *__last know to be true 36387a984708SDavid Chisnall __m1 = __m; 36397a984708SDavid Chisnall _BidirectionalIterator __second_false = __last; 36407a984708SDavid Chisnall ++__second_false; 36417a984708SDavid Chisnall __len_half = __len - __len2; 36427a984708SDavid Chisnall while (__pred(*__m1)) 36437a984708SDavid Chisnall { 36447a984708SDavid Chisnall if (++__m1 == __last) 36457a984708SDavid Chisnall goto __second_half_done; 36467a984708SDavid Chisnall --__len_half; 36477a984708SDavid Chisnall } 36487a984708SDavid Chisnall // TTTFFFFFTTTF?????T 36497a984708SDavid Chisnall // f ff m m1 l 36507a984708SDavid Chisnall __second_false = __stable_partition<_PredRef>(__m1, __last, __pred, __len_half, __p, __bit); 36517a984708SDavid Chisnall__second_half_done: 36527a984708SDavid Chisnall // TTTFFFFFTTTTTFFFFF 36537a984708SDavid Chisnall // f ff m sf l 36547a984708SDavid Chisnall return _VSTD::rotate(__first_false, __m, __second_false); 36557a984708SDavid Chisnall // TTTTTTTTFFFFFFFFFF 36567a984708SDavid Chisnall // | 36577a984708SDavid Chisnall} 36587a984708SDavid Chisnall 36597a984708SDavid Chisnalltemplate <class _Predicate, class _BidirectionalIterator> 36607a984708SDavid Chisnall_BidirectionalIterator 36617a984708SDavid Chisnall__stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last, _Predicate __pred, 36627a984708SDavid Chisnall bidirectional_iterator_tag) 36637a984708SDavid Chisnall{ 36647a984708SDavid Chisnall typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type; 36657a984708SDavid Chisnall typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type; 36667a984708SDavid Chisnall const difference_type __alloc_limit = 4; // might want to make this a function of trivial assignment 36677a984708SDavid Chisnall // Either prove all true and return __first or point to first false 36687a984708SDavid Chisnall while (true) 36697a984708SDavid Chisnall { 36707a984708SDavid Chisnall if (__first == __last) 36717a984708SDavid Chisnall return __first; 36727a984708SDavid Chisnall if (!__pred(*__first)) 36737a984708SDavid Chisnall break; 36747a984708SDavid Chisnall ++__first; 36757a984708SDavid Chisnall } 36767a984708SDavid Chisnall // __first points to first false, everything prior to __first is already set. 36777a984708SDavid Chisnall // Either prove [__first, __last) is all false and return __first, or point __last to last true 36787a984708SDavid Chisnall do 36797a984708SDavid Chisnall { 36807a984708SDavid Chisnall if (__first == --__last) 36817a984708SDavid Chisnall return __first; 36827a984708SDavid Chisnall } while (!__pred(*__last)); 36837a984708SDavid Chisnall // We now have a reduced range [__first, __last] 36847a984708SDavid Chisnall // *__first is known to be false 36857a984708SDavid Chisnall // *__last is known to be true 36867a984708SDavid Chisnall // __len >= 2 36877a984708SDavid Chisnall difference_type __len = _VSTD::distance(__first, __last) + 1; 36887a984708SDavid Chisnall pair<value_type*, ptrdiff_t> __p(0, 0); 36897a984708SDavid Chisnall unique_ptr<value_type, __return_temporary_buffer> __h; 36907a984708SDavid Chisnall if (__len >= __alloc_limit) 36917a984708SDavid Chisnall { 36927a984708SDavid Chisnall __p = _VSTD::get_temporary_buffer<value_type>(__len); 36937a984708SDavid Chisnall __h.reset(__p.first); 36947a984708SDavid Chisnall } 36957a984708SDavid Chisnall return __stable_partition<typename add_lvalue_reference<_Predicate>::type> 36967a984708SDavid Chisnall (__first, __last, __pred, __len, __p, bidirectional_iterator_tag()); 36977a984708SDavid Chisnall} 36987a984708SDavid Chisnall 36997a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Predicate> 37007a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 37017a984708SDavid Chisnall_ForwardIterator 37027a984708SDavid Chisnallstable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) 37037a984708SDavid Chisnall{ 37047a984708SDavid Chisnall return __stable_partition<typename add_lvalue_reference<_Predicate>::type> 37057a984708SDavid Chisnall (__first, __last, __pred, typename iterator_traits<_ForwardIterator>::iterator_category()); 37067a984708SDavid Chisnall} 37077a984708SDavid Chisnall 37087a984708SDavid Chisnall// is_sorted_until 37097a984708SDavid Chisnall 37107a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Compare> 37117a984708SDavid Chisnall_ForwardIterator 37127a984708SDavid Chisnallis_sorted_until(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) 37137a984708SDavid Chisnall{ 37147a984708SDavid Chisnall if (__first != __last) 37157a984708SDavid Chisnall { 37167a984708SDavid Chisnall _ForwardIterator __i = __first; 37177a984708SDavid Chisnall while (++__i != __last) 37187a984708SDavid Chisnall { 37197a984708SDavid Chisnall if (__comp(*__i, *__first)) 37207a984708SDavid Chisnall return __i; 37217a984708SDavid Chisnall __first = __i; 37227a984708SDavid Chisnall } 37237a984708SDavid Chisnall } 37247a984708SDavid Chisnall return __last; 37257a984708SDavid Chisnall} 37267a984708SDavid Chisnall 37277a984708SDavid Chisnalltemplate<class _ForwardIterator> 37287a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 37297a984708SDavid Chisnall_ForwardIterator 37307a984708SDavid Chisnallis_sorted_until(_ForwardIterator __first, _ForwardIterator __last) 37317a984708SDavid Chisnall{ 37327a984708SDavid Chisnall return _VSTD::is_sorted_until(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>()); 37337a984708SDavid Chisnall} 37347a984708SDavid Chisnall 37357a984708SDavid Chisnall// is_sorted 37367a984708SDavid Chisnall 37377a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Compare> 37387a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 37397a984708SDavid Chisnallbool 37407a984708SDavid Chisnallis_sorted(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) 37417a984708SDavid Chisnall{ 37427a984708SDavid Chisnall return _VSTD::is_sorted_until(__first, __last, __comp) == __last; 37437a984708SDavid Chisnall} 37447a984708SDavid Chisnall 37457a984708SDavid Chisnalltemplate<class _ForwardIterator> 37467a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 37477a984708SDavid Chisnallbool 37487a984708SDavid Chisnallis_sorted(_ForwardIterator __first, _ForwardIterator __last) 37497a984708SDavid Chisnall{ 37507a984708SDavid Chisnall return _VSTD::is_sorted(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>()); 37517a984708SDavid Chisnall} 37527a984708SDavid Chisnall 37537a984708SDavid Chisnall// sort 37547a984708SDavid Chisnall 37557a984708SDavid Chisnall// stable, 2-3 compares, 0-2 swaps 37567a984708SDavid Chisnall 37577a984708SDavid Chisnalltemplate <class _Compare, class _ForwardIterator> 37587a984708SDavid Chisnallunsigned 37597a984708SDavid Chisnall__sort3(_ForwardIterator __x, _ForwardIterator __y, _ForwardIterator __z, _Compare __c) 37607a984708SDavid Chisnall{ 37617a984708SDavid Chisnall unsigned __r = 0; 37627a984708SDavid Chisnall if (!__c(*__y, *__x)) // if x <= y 37637a984708SDavid Chisnall { 37647a984708SDavid Chisnall if (!__c(*__z, *__y)) // if y <= z 37657a984708SDavid Chisnall return __r; // x <= y && y <= z 37667a984708SDavid Chisnall // x <= y && y > z 37677a984708SDavid Chisnall swap(*__y, *__z); // x <= z && y < z 37687a984708SDavid Chisnall __r = 1; 37697a984708SDavid Chisnall if (__c(*__y, *__x)) // if x > y 37707a984708SDavid Chisnall { 37717a984708SDavid Chisnall swap(*__x, *__y); // x < y && y <= z 37727a984708SDavid Chisnall __r = 2; 37737a984708SDavid Chisnall } 37747a984708SDavid Chisnall return __r; // x <= y && y < z 37757a984708SDavid Chisnall } 37767a984708SDavid Chisnall if (__c(*__z, *__y)) // x > y, if y > z 37777a984708SDavid Chisnall { 37787a984708SDavid Chisnall swap(*__x, *__z); // x < y && y < z 37797a984708SDavid Chisnall __r = 1; 37807a984708SDavid Chisnall return __r; 37817a984708SDavid Chisnall } 37827a984708SDavid Chisnall swap(*__x, *__y); // x > y && y <= z 37837a984708SDavid Chisnall __r = 1; // x < y && x <= z 37847a984708SDavid Chisnall if (__c(*__z, *__y)) // if y > z 37857a984708SDavid Chisnall { 37867a984708SDavid Chisnall swap(*__y, *__z); // x <= y && y < z 37877a984708SDavid Chisnall __r = 2; 37887a984708SDavid Chisnall } 37897a984708SDavid Chisnall return __r; 37907a984708SDavid Chisnall} // x <= y && y <= z 37917a984708SDavid Chisnall 37927a984708SDavid Chisnall// stable, 3-6 compares, 0-5 swaps 37937a984708SDavid Chisnall 37947a984708SDavid Chisnalltemplate <class _Compare, class _ForwardIterator> 37957a984708SDavid Chisnallunsigned 37967a984708SDavid Chisnall__sort4(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3, 37977a984708SDavid Chisnall _ForwardIterator __x4, _Compare __c) 37987a984708SDavid Chisnall{ 37997a984708SDavid Chisnall unsigned __r = __sort3<_Compare>(__x1, __x2, __x3, __c); 38007a984708SDavid Chisnall if (__c(*__x4, *__x3)) 38017a984708SDavid Chisnall { 38027a984708SDavid Chisnall swap(*__x3, *__x4); 38037a984708SDavid Chisnall ++__r; 38047a984708SDavid Chisnall if (__c(*__x3, *__x2)) 38057a984708SDavid Chisnall { 38067a984708SDavid Chisnall swap(*__x2, *__x3); 38077a984708SDavid Chisnall ++__r; 38087a984708SDavid Chisnall if (__c(*__x2, *__x1)) 38097a984708SDavid Chisnall { 38107a984708SDavid Chisnall swap(*__x1, *__x2); 38117a984708SDavid Chisnall ++__r; 38127a984708SDavid Chisnall } 38137a984708SDavid Chisnall } 38147a984708SDavid Chisnall } 38157a984708SDavid Chisnall return __r; 38167a984708SDavid Chisnall} 38177a984708SDavid Chisnall 38187a984708SDavid Chisnall// stable, 4-10 compares, 0-9 swaps 38197a984708SDavid Chisnall 38207a984708SDavid Chisnalltemplate <class _Compare, class _ForwardIterator> 38217a984708SDavid Chisnallunsigned 38227a984708SDavid Chisnall__sort5(_ForwardIterator __x1, _ForwardIterator __x2, _ForwardIterator __x3, 38237a984708SDavid Chisnall _ForwardIterator __x4, _ForwardIterator __x5, _Compare __c) 38247a984708SDavid Chisnall{ 38257a984708SDavid Chisnall unsigned __r = __sort4<_Compare>(__x1, __x2, __x3, __x4, __c); 38267a984708SDavid Chisnall if (__c(*__x5, *__x4)) 38277a984708SDavid Chisnall { 38287a984708SDavid Chisnall swap(*__x4, *__x5); 38297a984708SDavid Chisnall ++__r; 38307a984708SDavid Chisnall if (__c(*__x4, *__x3)) 38317a984708SDavid Chisnall { 38327a984708SDavid Chisnall swap(*__x3, *__x4); 38337a984708SDavid Chisnall ++__r; 38347a984708SDavid Chisnall if (__c(*__x3, *__x2)) 38357a984708SDavid Chisnall { 38367a984708SDavid Chisnall swap(*__x2, *__x3); 38377a984708SDavid Chisnall ++__r; 38387a984708SDavid Chisnall if (__c(*__x2, *__x1)) 38397a984708SDavid Chisnall { 38407a984708SDavid Chisnall swap(*__x1, *__x2); 38417a984708SDavid Chisnall ++__r; 38427a984708SDavid Chisnall } 38437a984708SDavid Chisnall } 38447a984708SDavid Chisnall } 38457a984708SDavid Chisnall } 38467a984708SDavid Chisnall return __r; 38477a984708SDavid Chisnall} 38487a984708SDavid Chisnall 38497a984708SDavid Chisnall// Assumes size > 0 38507a984708SDavid Chisnalltemplate <class _Compare, class _BirdirectionalIterator> 38517a984708SDavid Chisnallvoid 38527a984708SDavid Chisnall__selection_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp) 38537a984708SDavid Chisnall{ 38547a984708SDavid Chisnall _BirdirectionalIterator __lm1 = __last; 38557a984708SDavid Chisnall for (--__lm1; __first != __lm1; ++__first) 38567a984708SDavid Chisnall { 38577a984708SDavid Chisnall _BirdirectionalIterator __i = _VSTD::min_element<_BirdirectionalIterator, 38587a984708SDavid Chisnall typename add_lvalue_reference<_Compare>::type> 38597a984708SDavid Chisnall (__first, __last, __comp); 38607a984708SDavid Chisnall if (__i != __first) 38617a984708SDavid Chisnall swap(*__first, *__i); 38627a984708SDavid Chisnall } 38637a984708SDavid Chisnall} 38647a984708SDavid Chisnall 38657a984708SDavid Chisnalltemplate <class _Compare, class _BirdirectionalIterator> 38667a984708SDavid Chisnallvoid 38677a984708SDavid Chisnall__insertion_sort(_BirdirectionalIterator __first, _BirdirectionalIterator __last, _Compare __comp) 38687a984708SDavid Chisnall{ 38697a984708SDavid Chisnall typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type; 38707a984708SDavid Chisnall if (__first != __last) 38717a984708SDavid Chisnall { 38727a984708SDavid Chisnall _BirdirectionalIterator __i = __first; 38737a984708SDavid Chisnall for (++__i; __i != __last; ++__i) 38747a984708SDavid Chisnall { 38757a984708SDavid Chisnall _BirdirectionalIterator __j = __i; 38767a984708SDavid Chisnall value_type __t(_VSTD::move(*__j)); 38777a984708SDavid Chisnall for (_BirdirectionalIterator __k = __i; __k != __first && __comp(__t, *--__k); --__j) 38787a984708SDavid Chisnall *__j = _VSTD::move(*__k); 38797a984708SDavid Chisnall *__j = _VSTD::move(__t); 38807a984708SDavid Chisnall } 38817a984708SDavid Chisnall } 38827a984708SDavid Chisnall} 38837a984708SDavid Chisnall 38847a984708SDavid Chisnalltemplate <class _Compare, class _RandomAccessIterator> 38857a984708SDavid Chisnallvoid 38867a984708SDavid Chisnall__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 38877a984708SDavid Chisnall{ 38887a984708SDavid Chisnall typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type; 38897a984708SDavid Chisnall _RandomAccessIterator __j = __first+2; 38907a984708SDavid Chisnall __sort3<_Compare>(__first, __first+1, __j, __comp); 38917a984708SDavid Chisnall for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i) 38927a984708SDavid Chisnall { 38937a984708SDavid Chisnall if (__comp(*__i, *__j)) 38947a984708SDavid Chisnall { 38957a984708SDavid Chisnall value_type __t(_VSTD::move(*__i)); 38967a984708SDavid Chisnall _RandomAccessIterator __k = __j; 38977a984708SDavid Chisnall __j = __i; 38987a984708SDavid Chisnall do 38997a984708SDavid Chisnall { 39007a984708SDavid Chisnall *__j = _VSTD::move(*__k); 39017a984708SDavid Chisnall __j = __k; 39027a984708SDavid Chisnall } while (__j != __first && __comp(__t, *--__k)); 39037a984708SDavid Chisnall *__j = _VSTD::move(__t); 39047a984708SDavid Chisnall } 39057a984708SDavid Chisnall __j = __i; 39067a984708SDavid Chisnall } 39077a984708SDavid Chisnall} 39087a984708SDavid Chisnall 39097a984708SDavid Chisnalltemplate <class _Compare, class _RandomAccessIterator> 39107a984708SDavid Chisnallbool 39117a984708SDavid Chisnall__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 39127a984708SDavid Chisnall{ 39137a984708SDavid Chisnall switch (__last - __first) 39147a984708SDavid Chisnall { 39157a984708SDavid Chisnall case 0: 39167a984708SDavid Chisnall case 1: 39177a984708SDavid Chisnall return true; 39187a984708SDavid Chisnall case 2: 39197a984708SDavid Chisnall if (__comp(*--__last, *__first)) 39207a984708SDavid Chisnall swap(*__first, *__last); 39217a984708SDavid Chisnall return true; 39227a984708SDavid Chisnall case 3: 39237a984708SDavid Chisnall _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp); 39247a984708SDavid Chisnall return true; 39257a984708SDavid Chisnall case 4: 39267a984708SDavid Chisnall _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp); 39277a984708SDavid Chisnall return true; 39287a984708SDavid Chisnall case 5: 39297a984708SDavid Chisnall _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp); 39307a984708SDavid Chisnall return true; 39317a984708SDavid Chisnall } 39327a984708SDavid Chisnall typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type; 39337a984708SDavid Chisnall _RandomAccessIterator __j = __first+2; 39347a984708SDavid Chisnall __sort3<_Compare>(__first, __first+1, __j, __comp); 39357a984708SDavid Chisnall const unsigned __limit = 8; 39367a984708SDavid Chisnall unsigned __count = 0; 39377a984708SDavid Chisnall for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i) 39387a984708SDavid Chisnall { 39397a984708SDavid Chisnall if (__comp(*__i, *__j)) 39407a984708SDavid Chisnall { 39417a984708SDavid Chisnall value_type __t(_VSTD::move(*__i)); 39427a984708SDavid Chisnall _RandomAccessIterator __k = __j; 39437a984708SDavid Chisnall __j = __i; 39447a984708SDavid Chisnall do 39457a984708SDavid Chisnall { 39467a984708SDavid Chisnall *__j = _VSTD::move(*__k); 39477a984708SDavid Chisnall __j = __k; 39487a984708SDavid Chisnall } while (__j != __first && __comp(__t, *--__k)); 39497a984708SDavid Chisnall *__j = _VSTD::move(__t); 39507a984708SDavid Chisnall if (++__count == __limit) 39517a984708SDavid Chisnall return ++__i == __last; 39527a984708SDavid Chisnall } 39537a984708SDavid Chisnall __j = __i; 39547a984708SDavid Chisnall } 39557a984708SDavid Chisnall return true; 39567a984708SDavid Chisnall} 39577a984708SDavid Chisnall 39587a984708SDavid Chisnalltemplate <class _Compare, class _BirdirectionalIterator> 39597a984708SDavid Chisnallvoid 39607a984708SDavid Chisnall__insertion_sort_move(_BirdirectionalIterator __first1, _BirdirectionalIterator __last1, 39617a984708SDavid Chisnall typename iterator_traits<_BirdirectionalIterator>::value_type* __first2, _Compare __comp) 39627a984708SDavid Chisnall{ 39637a984708SDavid Chisnall typedef typename iterator_traits<_BirdirectionalIterator>::value_type value_type; 39647a984708SDavid Chisnall if (__first1 != __last1) 39657a984708SDavid Chisnall { 39667a984708SDavid Chisnall __destruct_n __d(0); 39677a984708SDavid Chisnall unique_ptr<value_type, __destruct_n&> __h(__first2, __d); 39687a984708SDavid Chisnall value_type* __last2 = __first2; 39697a984708SDavid Chisnall ::new(__last2) value_type(_VSTD::move(*__first1)); 39707a984708SDavid Chisnall __d.__incr((value_type*)0); 39717a984708SDavid Chisnall for (++__last2; ++__first1 != __last1; ++__last2) 39727a984708SDavid Chisnall { 39737a984708SDavid Chisnall value_type* __j2 = __last2; 39747a984708SDavid Chisnall value_type* __i2 = __j2; 39757a984708SDavid Chisnall if (__comp(*__first1, *--__i2)) 39767a984708SDavid Chisnall { 39777a984708SDavid Chisnall ::new(__j2) value_type(_VSTD::move(*__i2)); 39787a984708SDavid Chisnall __d.__incr((value_type*)0); 39797a984708SDavid Chisnall for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2) 39807a984708SDavid Chisnall *__j2 = _VSTD::move(*__i2); 39817a984708SDavid Chisnall *__j2 = _VSTD::move(*__first1); 39827a984708SDavid Chisnall } 39837a984708SDavid Chisnall else 39847a984708SDavid Chisnall { 39857a984708SDavid Chisnall ::new(__j2) value_type(_VSTD::move(*__first1)); 39867a984708SDavid Chisnall __d.__incr((value_type*)0); 39877a984708SDavid Chisnall } 39887a984708SDavid Chisnall } 39897a984708SDavid Chisnall __h.release(); 39907a984708SDavid Chisnall } 39917a984708SDavid Chisnall} 39927a984708SDavid Chisnall 39937a984708SDavid Chisnalltemplate <class _Compare, class _RandomAccessIterator> 39947a984708SDavid Chisnallvoid 39957a984708SDavid Chisnall__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 39967a984708SDavid Chisnall{ 39977a984708SDavid Chisnall // _Compare is known to be a reference type 39987a984708SDavid Chisnall typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; 39997a984708SDavid Chisnall typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type; 40007a984708SDavid Chisnall const difference_type __limit = is_trivially_copy_constructible<value_type>::value && 40017a984708SDavid Chisnall is_trivially_copy_assignable<value_type>::value ? 30 : 6; 40027a984708SDavid Chisnall while (true) 40037a984708SDavid Chisnall { 40047a984708SDavid Chisnall __restart: 40057a984708SDavid Chisnall difference_type __len = __last - __first; 40067a984708SDavid Chisnall switch (__len) 40077a984708SDavid Chisnall { 40087a984708SDavid Chisnall case 0: 40097a984708SDavid Chisnall case 1: 40107a984708SDavid Chisnall return; 40117a984708SDavid Chisnall case 2: 40127a984708SDavid Chisnall if (__comp(*--__last, *__first)) 40137a984708SDavid Chisnall swap(*__first, *__last); 40147a984708SDavid Chisnall return; 40157a984708SDavid Chisnall case 3: 40167a984708SDavid Chisnall _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp); 40177a984708SDavid Chisnall return; 40187a984708SDavid Chisnall case 4: 40197a984708SDavid Chisnall _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp); 40207a984708SDavid Chisnall return; 40217a984708SDavid Chisnall case 5: 40227a984708SDavid Chisnall _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp); 40237a984708SDavid Chisnall return; 40247a984708SDavid Chisnall } 40257a984708SDavid Chisnall if (__len <= __limit) 40267a984708SDavid Chisnall { 40277a984708SDavid Chisnall _VSTD::__insertion_sort_3<_Compare>(__first, __last, __comp); 40287a984708SDavid Chisnall return; 40297a984708SDavid Chisnall } 40307a984708SDavid Chisnall // __len > 5 40317a984708SDavid Chisnall _RandomAccessIterator __m = __first; 40327a984708SDavid Chisnall _RandomAccessIterator __lm1 = __last; 40337a984708SDavid Chisnall --__lm1; 40347a984708SDavid Chisnall unsigned __n_swaps; 40357a984708SDavid Chisnall { 40367a984708SDavid Chisnall difference_type __delta; 40377a984708SDavid Chisnall if (__len >= 1000) 40387a984708SDavid Chisnall { 40397a984708SDavid Chisnall __delta = __len/2; 40407a984708SDavid Chisnall __m += __delta; 40417a984708SDavid Chisnall __delta /= 2; 40427a984708SDavid Chisnall __n_swaps = _VSTD::__sort5<_Compare>(__first, __first + __delta, __m, __m+__delta, __lm1, __comp); 40437a984708SDavid Chisnall } 40447a984708SDavid Chisnall else 40457a984708SDavid Chisnall { 40467a984708SDavid Chisnall __delta = __len/2; 40477a984708SDavid Chisnall __m += __delta; 40487a984708SDavid Chisnall __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, __lm1, __comp); 40497a984708SDavid Chisnall } 40507a984708SDavid Chisnall } 40517a984708SDavid Chisnall // *__m is median 40527a984708SDavid Chisnall // partition [__first, __m) < *__m and *__m <= [__m, __last) 40537a984708SDavid Chisnall // (this inhibits tossing elements equivalent to __m around unnecessarily) 40547a984708SDavid Chisnall _RandomAccessIterator __i = __first; 40557a984708SDavid Chisnall _RandomAccessIterator __j = __lm1; 40567a984708SDavid Chisnall // j points beyond range to be tested, *__m is known to be <= *__lm1 40577a984708SDavid Chisnall // The search going up is known to be guarded but the search coming down isn't. 40587a984708SDavid Chisnall // Prime the downward search with a guard. 40597a984708SDavid Chisnall if (!__comp(*__i, *__m)) // if *__first == *__m 40607a984708SDavid Chisnall { 40617a984708SDavid Chisnall // *__first == *__m, *__first doesn't go in first part 40627a984708SDavid Chisnall // manually guard downward moving __j against __i 40637a984708SDavid Chisnall while (true) 40647a984708SDavid Chisnall { 40657a984708SDavid Chisnall if (__i == --__j) 40667a984708SDavid Chisnall { 40677a984708SDavid Chisnall // *__first == *__m, *__m <= all other elements 40687a984708SDavid Chisnall // Parition instead into [__first, __i) == *__first and *__first < [__i, __last) 40697a984708SDavid Chisnall ++__i; // __first + 1 40707a984708SDavid Chisnall __j = __last; 40717a984708SDavid Chisnall if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1) 40727a984708SDavid Chisnall { 40737a984708SDavid Chisnall while (true) 40747a984708SDavid Chisnall { 40757a984708SDavid Chisnall if (__i == __j) 40767a984708SDavid Chisnall return; // [__first, __last) all equivalent elements 40777a984708SDavid Chisnall if (__comp(*__first, *__i)) 40787a984708SDavid Chisnall { 40797a984708SDavid Chisnall swap(*__i, *__j); 40807a984708SDavid Chisnall ++__n_swaps; 40817a984708SDavid Chisnall ++__i; 40827a984708SDavid Chisnall break; 40837a984708SDavid Chisnall } 40847a984708SDavid Chisnall ++__i; 40857a984708SDavid Chisnall } 40867a984708SDavid Chisnall } 40877a984708SDavid Chisnall // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1 40887a984708SDavid Chisnall if (__i == __j) 40897a984708SDavid Chisnall return; 40907a984708SDavid Chisnall while (true) 40917a984708SDavid Chisnall { 40927a984708SDavid Chisnall while (!__comp(*__first, *__i)) 40937a984708SDavid Chisnall ++__i; 40947a984708SDavid Chisnall while (__comp(*__first, *--__j)) 40957a984708SDavid Chisnall ; 40967a984708SDavid Chisnall if (__i >= __j) 40977a984708SDavid Chisnall break; 40987a984708SDavid Chisnall swap(*__i, *__j); 40997a984708SDavid Chisnall ++__n_swaps; 41007a984708SDavid Chisnall ++__i; 41017a984708SDavid Chisnall } 41027a984708SDavid Chisnall // [__first, __i) == *__first and *__first < [__i, __last) 41037a984708SDavid Chisnall // The first part is sorted, sort the secod part 41047a984708SDavid Chisnall // _VSTD::__sort<_Compare>(__i, __last, __comp); 41057a984708SDavid Chisnall __first = __i; 41067a984708SDavid Chisnall goto __restart; 41077a984708SDavid Chisnall } 41087a984708SDavid Chisnall if (__comp(*__j, *__m)) 41097a984708SDavid Chisnall { 41107a984708SDavid Chisnall swap(*__i, *__j); 41117a984708SDavid Chisnall ++__n_swaps; 41127a984708SDavid Chisnall break; // found guard for downward moving __j, now use unguarded partition 41137a984708SDavid Chisnall } 41147a984708SDavid Chisnall } 41157a984708SDavid Chisnall } 41167a984708SDavid Chisnall // It is known that *__i < *__m 41177a984708SDavid Chisnall ++__i; 41187a984708SDavid Chisnall // j points beyond range to be tested, *__m is known to be <= *__lm1 41197a984708SDavid Chisnall // if not yet partitioned... 41207a984708SDavid Chisnall if (__i < __j) 41217a984708SDavid Chisnall { 41227a984708SDavid Chisnall // known that *(__i - 1) < *__m 41237a984708SDavid Chisnall // known that __i <= __m 41247a984708SDavid Chisnall while (true) 41257a984708SDavid Chisnall { 41267a984708SDavid Chisnall // __m still guards upward moving __i 41277a984708SDavid Chisnall while (__comp(*__i, *__m)) 41287a984708SDavid Chisnall ++__i; 41297a984708SDavid Chisnall // It is now known that a guard exists for downward moving __j 41307a984708SDavid Chisnall while (!__comp(*--__j, *__m)) 41317a984708SDavid Chisnall ; 41327a984708SDavid Chisnall if (__i > __j) 41337a984708SDavid Chisnall break; 41347a984708SDavid Chisnall swap(*__i, *__j); 41357a984708SDavid Chisnall ++__n_swaps; 41367a984708SDavid Chisnall // It is known that __m != __j 41377a984708SDavid Chisnall // If __m just moved, follow it 41387a984708SDavid Chisnall if (__m == __i) 41397a984708SDavid Chisnall __m = __j; 41407a984708SDavid Chisnall ++__i; 41417a984708SDavid Chisnall } 41427a984708SDavid Chisnall } 41437a984708SDavid Chisnall // [__first, __i) < *__m and *__m <= [__i, __last) 41447a984708SDavid Chisnall if (__i != __m && __comp(*__m, *__i)) 41457a984708SDavid Chisnall { 41467a984708SDavid Chisnall swap(*__i, *__m); 41477a984708SDavid Chisnall ++__n_swaps; 41487a984708SDavid Chisnall } 41497a984708SDavid Chisnall // [__first, __i) < *__i and *__i <= [__i+1, __last) 41507a984708SDavid Chisnall // If we were given a perfect partition, see if insertion sort is quick... 41517a984708SDavid Chisnall if (__n_swaps == 0) 41527a984708SDavid Chisnall { 41537a984708SDavid Chisnall bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp); 41547a984708SDavid Chisnall if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp)) 41557a984708SDavid Chisnall { 41567a984708SDavid Chisnall if (__fs) 41577a984708SDavid Chisnall return; 41587a984708SDavid Chisnall __last = __i; 41597a984708SDavid Chisnall continue; 41607a984708SDavid Chisnall } 41617a984708SDavid Chisnall else 41627a984708SDavid Chisnall { 41637a984708SDavid Chisnall if (__fs) 41647a984708SDavid Chisnall { 41657a984708SDavid Chisnall __first = ++__i; 41667a984708SDavid Chisnall continue; 41677a984708SDavid Chisnall } 41687a984708SDavid Chisnall } 41697a984708SDavid Chisnall } 41707a984708SDavid Chisnall // sort smaller range with recursive call and larger with tail recursion elimination 41717a984708SDavid Chisnall if (__i - __first < __last - __i) 41727a984708SDavid Chisnall { 41737a984708SDavid Chisnall _VSTD::__sort<_Compare>(__first, __i, __comp); 41747a984708SDavid Chisnall // _VSTD::__sort<_Compare>(__i+1, __last, __comp); 41757a984708SDavid Chisnall __first = ++__i; 41767a984708SDavid Chisnall } 41777a984708SDavid Chisnall else 41787a984708SDavid Chisnall { 41797a984708SDavid Chisnall _VSTD::__sort<_Compare>(__i+1, __last, __comp); 41807a984708SDavid Chisnall // _VSTD::__sort<_Compare>(__first, __i, __comp); 41817a984708SDavid Chisnall __last = __i; 41827a984708SDavid Chisnall } 41837a984708SDavid Chisnall } 41847a984708SDavid Chisnall} 41857a984708SDavid Chisnall 41867a984708SDavid Chisnall// This forwarder keeps the top call and the recursive calls using the same instantiation, forcing a reference _Compare 41877a984708SDavid Chisnalltemplate <class _RandomAccessIterator, class _Compare> 41887a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 41897a984708SDavid Chisnallvoid 41907a984708SDavid Chisnallsort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 41917a984708SDavid Chisnall{ 41924f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 41937a984708SDavid Chisnall typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 41947a984708SDavid Chisnall __debug_less<_Compare> __c(__comp); 41957a984708SDavid Chisnall __sort<_Comp_ref>(__first, __last, __c); 41964f7ab58eSDimitry Andric#else // _LIBCPP_DEBUG 41977a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 41987a984708SDavid Chisnall __sort<_Comp_ref>(__first, __last, __comp); 41994f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 42007a984708SDavid Chisnall} 42017a984708SDavid Chisnall 42027a984708SDavid Chisnalltemplate <class _RandomAccessIterator> 42037a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 42047a984708SDavid Chisnallvoid 42057a984708SDavid Chisnallsort(_RandomAccessIterator __first, _RandomAccessIterator __last) 42067a984708SDavid Chisnall{ 42077a984708SDavid Chisnall _VSTD::sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 42087a984708SDavid Chisnall} 42097a984708SDavid Chisnall 42107a984708SDavid Chisnalltemplate <class _Tp> 42117a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 42127a984708SDavid Chisnallvoid 42137a984708SDavid Chisnallsort(_Tp** __first, _Tp** __last) 42147a984708SDavid Chisnall{ 42157a984708SDavid Chisnall _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>()); 42167a984708SDavid Chisnall} 42177a984708SDavid Chisnall 42187a984708SDavid Chisnalltemplate <class _Tp> 42197a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 42207a984708SDavid Chisnallvoid 42217a984708SDavid Chisnallsort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last) 42227a984708SDavid Chisnall{ 42237a984708SDavid Chisnall _VSTD::sort(__first.base(), __last.base()); 42247a984708SDavid Chisnall} 42257a984708SDavid Chisnall 42267a984708SDavid Chisnalltemplate <class _Tp, class _Compare> 42277a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 42287a984708SDavid Chisnallvoid 42297a984708SDavid Chisnallsort(__wrap_iter<_Tp*> __first, __wrap_iter<_Tp*> __last, _Compare __comp) 42307a984708SDavid Chisnall{ 42317a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 42327a984708SDavid Chisnall _VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp); 42337a984708SDavid Chisnall} 42347a984708SDavid Chisnall 42354f7ab58eSDimitry Andric#ifdef _LIBCPP_MSVC 42367a984708SDavid Chisnall#pragma warning( push ) 42377a984708SDavid Chisnall#pragma warning( disable: 4231) 42384f7ab58eSDimitry Andric#endif // _LIBCPP_MSVC 42394f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<char>&, char*>(char*, char*, __less<char>&)) 42404f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&)) 42414f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&)) 42424f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&)) 42434f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<short>&, short*>(short*, short*, __less<short>&)) 42444f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&)) 42454f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<int>&, int*>(int*, int*, __less<int>&)) 42464f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&)) 42474f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long>&, long*>(long*, long*, __less<long>&)) 42484f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&)) 42494f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long long>&, long long*>(long long*, long long*, __less<long long>&)) 42504f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&)) 42514f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<float>&, float*>(float*, float*, __less<float>&)) 42524f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<double>&, double*>(double*, double*, __less<double>&)) 42534f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS void __sort<__less<long double>&, long double*>(long double*, long double*, __less<long double>&)) 42547a984708SDavid Chisnall 42554f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<char>&, char*>(char*, char*, __less<char>&)) 42564f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<wchar_t>&, wchar_t*>(wchar_t*, wchar_t*, __less<wchar_t>&)) 42574f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<signed char>&, signed char*>(signed char*, signed char*, __less<signed char>&)) 42584f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned char>&, unsigned char*>(unsigned char*, unsigned char*, __less<unsigned char>&)) 42594f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<short>&, short*>(short*, short*, __less<short>&)) 42604f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned short>&, unsigned short*>(unsigned short*, unsigned short*, __less<unsigned short>&)) 42614f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<int>&, int*>(int*, int*, __less<int>&)) 42624f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned>&, unsigned*>(unsigned*, unsigned*, __less<unsigned>&)) 42634f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long>&, long*>(long*, long*, __less<long>&)) 42644f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long>&, unsigned long*>(unsigned long*, unsigned long*, __less<unsigned long>&)) 42654f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long long>&, long long*>(long long*, long long*, __less<long long>&)) 42664f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<unsigned long long>&, unsigned long long*>(unsigned long long*, unsigned long long*, __less<unsigned long long>&)) 42674f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<float>&, float*>(float*, float*, __less<float>&)) 42684f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<double>&, double*>(double*, double*, __less<double>&)) 42694f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS bool __insertion_sort_incomplete<__less<long double>&, long double*>(long double*, long double*, __less<long double>&)) 42707a984708SDavid Chisnall 42714f7ab58eSDimitry Andric_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS unsigned __sort5<__less<long double>&, long double*>(long double*, long double*, long double*, long double*, long double*, __less<long double>&)) 42724f7ab58eSDimitry Andric#ifdef _LIBCPP_MSVC 42737a984708SDavid Chisnall#pragma warning( pop ) 42744f7ab58eSDimitry Andric#endif // _LIBCPP_MSVC 42757a984708SDavid Chisnall 42767a984708SDavid Chisnall// lower_bound 42777a984708SDavid Chisnall 42787a984708SDavid Chisnalltemplate <class _Compare, class _ForwardIterator, class _Tp> 42797a984708SDavid Chisnall_ForwardIterator 42807a984708SDavid Chisnall__lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) 42817a984708SDavid Chisnall{ 42827a984708SDavid Chisnall typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type; 42837a984708SDavid Chisnall difference_type __len = _VSTD::distance(__first, __last); 42847a984708SDavid Chisnall while (__len != 0) 42857a984708SDavid Chisnall { 42867a984708SDavid Chisnall difference_type __l2 = __len / 2; 42877a984708SDavid Chisnall _ForwardIterator __m = __first; 42887a984708SDavid Chisnall _VSTD::advance(__m, __l2); 42897a984708SDavid Chisnall if (__comp(*__m, __value_)) 42907a984708SDavid Chisnall { 42917a984708SDavid Chisnall __first = ++__m; 42927a984708SDavid Chisnall __len -= __l2 + 1; 42937a984708SDavid Chisnall } 42947a984708SDavid Chisnall else 42957a984708SDavid Chisnall __len = __l2; 42967a984708SDavid Chisnall } 42977a984708SDavid Chisnall return __first; 42987a984708SDavid Chisnall} 42997a984708SDavid Chisnall 43007a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Tp, class _Compare> 43017a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 43027a984708SDavid Chisnall_ForwardIterator 43037a984708SDavid Chisnalllower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) 43047a984708SDavid Chisnall{ 43054f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 43067a984708SDavid Chisnall typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 43077a984708SDavid Chisnall __debug_less<_Compare> __c(__comp); 43087a984708SDavid Chisnall return __lower_bound<_Comp_ref>(__first, __last, __value_, __c); 43094f7ab58eSDimitry Andric#else // _LIBCPP_DEBUG 43107a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 43117a984708SDavid Chisnall return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp); 43124f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 43137a984708SDavid Chisnall} 43147a984708SDavid Chisnall 43157a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Tp> 43167a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 43177a984708SDavid Chisnall_ForwardIterator 43187a984708SDavid Chisnalllower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_) 43197a984708SDavid Chisnall{ 43207a984708SDavid Chisnall return _VSTD::lower_bound(__first, __last, __value_, 43217a984708SDavid Chisnall __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>()); 43227a984708SDavid Chisnall} 43237a984708SDavid Chisnall 43247a984708SDavid Chisnall// upper_bound 43257a984708SDavid Chisnall 43267a984708SDavid Chisnalltemplate <class _Compare, class _ForwardIterator, class _Tp> 43277a984708SDavid Chisnall_ForwardIterator 43287a984708SDavid Chisnall__upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) 43297a984708SDavid Chisnall{ 43307a984708SDavid Chisnall typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type; 43317a984708SDavid Chisnall difference_type __len = _VSTD::distance(__first, __last); 43327a984708SDavid Chisnall while (__len != 0) 43337a984708SDavid Chisnall { 43347a984708SDavid Chisnall difference_type __l2 = __len / 2; 43357a984708SDavid Chisnall _ForwardIterator __m = __first; 43367a984708SDavid Chisnall _VSTD::advance(__m, __l2); 43377a984708SDavid Chisnall if (__comp(__value_, *__m)) 43387a984708SDavid Chisnall __len = __l2; 43397a984708SDavid Chisnall else 43407a984708SDavid Chisnall { 43417a984708SDavid Chisnall __first = ++__m; 43427a984708SDavid Chisnall __len -= __l2 + 1; 43437a984708SDavid Chisnall } 43447a984708SDavid Chisnall } 43457a984708SDavid Chisnall return __first; 43467a984708SDavid Chisnall} 43477a984708SDavid Chisnall 43487a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Tp, class _Compare> 43497a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 43507a984708SDavid Chisnall_ForwardIterator 43517a984708SDavid Chisnallupper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) 43527a984708SDavid Chisnall{ 43534f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 43547a984708SDavid Chisnall typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 43557a984708SDavid Chisnall __debug_less<_Compare> __c(__comp); 43567a984708SDavid Chisnall return __upper_bound<_Comp_ref>(__first, __last, __value_, __c); 43574f7ab58eSDimitry Andric#else // _LIBCPP_DEBUG 43587a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 43597a984708SDavid Chisnall return __upper_bound<_Comp_ref>(__first, __last, __value_, __comp); 43604f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 43617a984708SDavid Chisnall} 43627a984708SDavid Chisnall 43637a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Tp> 43647a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 43657a984708SDavid Chisnall_ForwardIterator 43667a984708SDavid Chisnallupper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_) 43677a984708SDavid Chisnall{ 43687a984708SDavid Chisnall return _VSTD::upper_bound(__first, __last, __value_, 43697a984708SDavid Chisnall __less<_Tp, typename iterator_traits<_ForwardIterator>::value_type>()); 43707a984708SDavid Chisnall} 43717a984708SDavid Chisnall 43727a984708SDavid Chisnall// equal_range 43737a984708SDavid Chisnall 43747a984708SDavid Chisnalltemplate <class _Compare, class _ForwardIterator, class _Tp> 43757a984708SDavid Chisnallpair<_ForwardIterator, _ForwardIterator> 43767a984708SDavid Chisnall__equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) 43777a984708SDavid Chisnall{ 43787a984708SDavid Chisnall typedef typename iterator_traits<_ForwardIterator>::difference_type difference_type; 43797a984708SDavid Chisnall difference_type __len = _VSTD::distance(__first, __last); 43807a984708SDavid Chisnall while (__len != 0) 43817a984708SDavid Chisnall { 43827a984708SDavid Chisnall difference_type __l2 = __len / 2; 43837a984708SDavid Chisnall _ForwardIterator __m = __first; 43847a984708SDavid Chisnall _VSTD::advance(__m, __l2); 43857a984708SDavid Chisnall if (__comp(*__m, __value_)) 43867a984708SDavid Chisnall { 43877a984708SDavid Chisnall __first = ++__m; 43887a984708SDavid Chisnall __len -= __l2 + 1; 43897a984708SDavid Chisnall } 43907a984708SDavid Chisnall else if (__comp(__value_, *__m)) 43917a984708SDavid Chisnall { 43927a984708SDavid Chisnall __last = __m; 43937a984708SDavid Chisnall __len = __l2; 43947a984708SDavid Chisnall } 43957a984708SDavid Chisnall else 43967a984708SDavid Chisnall { 43977a984708SDavid Chisnall _ForwardIterator __mp1 = __m; 43987a984708SDavid Chisnall return pair<_ForwardIterator, _ForwardIterator> 43997a984708SDavid Chisnall ( 44007a984708SDavid Chisnall __lower_bound<_Compare>(__first, __m, __value_, __comp), 44017a984708SDavid Chisnall __upper_bound<_Compare>(++__mp1, __last, __value_, __comp) 44027a984708SDavid Chisnall ); 44037a984708SDavid Chisnall } 44047a984708SDavid Chisnall } 44057a984708SDavid Chisnall return pair<_ForwardIterator, _ForwardIterator>(__first, __first); 44067a984708SDavid Chisnall} 44077a984708SDavid Chisnall 44087a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Tp, class _Compare> 44097a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 44107a984708SDavid Chisnallpair<_ForwardIterator, _ForwardIterator> 44117a984708SDavid Chisnallequal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) 44127a984708SDavid Chisnall{ 44134f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 44147a984708SDavid Chisnall typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 44157a984708SDavid Chisnall __debug_less<_Compare> __c(__comp); 44167a984708SDavid Chisnall return __equal_range<_Comp_ref>(__first, __last, __value_, __c); 44174f7ab58eSDimitry Andric#else // _LIBCPP_DEBUG 44187a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 44197a984708SDavid Chisnall return __equal_range<_Comp_ref>(__first, __last, __value_, __comp); 44204f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 44217a984708SDavid Chisnall} 44227a984708SDavid Chisnall 44237a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Tp> 44247a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 44257a984708SDavid Chisnallpair<_ForwardIterator, _ForwardIterator> 44267a984708SDavid Chisnallequal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_) 44277a984708SDavid Chisnall{ 44287a984708SDavid Chisnall return _VSTD::equal_range(__first, __last, __value_, 44297a984708SDavid Chisnall __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>()); 44307a984708SDavid Chisnall} 44317a984708SDavid Chisnall 44327a984708SDavid Chisnall// binary_search 44337a984708SDavid Chisnall 44347a984708SDavid Chisnalltemplate <class _Compare, class _ForwardIterator, class _Tp> 44357a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 44367a984708SDavid Chisnallbool 44377a984708SDavid Chisnall__binary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) 44387a984708SDavid Chisnall{ 44397a984708SDavid Chisnall __first = __lower_bound<_Compare>(__first, __last, __value_, __comp); 44407a984708SDavid Chisnall return __first != __last && !__comp(__value_, *__first); 44417a984708SDavid Chisnall} 44427a984708SDavid Chisnall 44437a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Tp, class _Compare> 44447a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 44457a984708SDavid Chisnallbool 44467a984708SDavid Chisnallbinary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_, _Compare __comp) 44477a984708SDavid Chisnall{ 44484f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 44497a984708SDavid Chisnall typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 44507a984708SDavid Chisnall __debug_less<_Compare> __c(__comp); 44517a984708SDavid Chisnall return __binary_search<_Comp_ref>(__first, __last, __value_, __c); 44524f7ab58eSDimitry Andric#else // _LIBCPP_DEBUG 44537a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 44547a984708SDavid Chisnall return __binary_search<_Comp_ref>(__first, __last, __value_, __comp); 44554f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 44567a984708SDavid Chisnall} 44577a984708SDavid Chisnall 44587a984708SDavid Chisnalltemplate <class _ForwardIterator, class _Tp> 44597a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 44607a984708SDavid Chisnallbool 44617a984708SDavid Chisnallbinary_search(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value_) 44627a984708SDavid Chisnall{ 44637a984708SDavid Chisnall return _VSTD::binary_search(__first, __last, __value_, 44647a984708SDavid Chisnall __less<typename iterator_traits<_ForwardIterator>::value_type, _Tp>()); 44657a984708SDavid Chisnall} 44667a984708SDavid Chisnall 44677a984708SDavid Chisnall// merge 44687a984708SDavid Chisnall 44697a984708SDavid Chisnalltemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator> 44707a984708SDavid Chisnall_OutputIterator 44717a984708SDavid Chisnall__merge(_InputIterator1 __first1, _InputIterator1 __last1, 44727a984708SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) 44737a984708SDavid Chisnall{ 44747a984708SDavid Chisnall for (; __first1 != __last1; ++__result) 44757a984708SDavid Chisnall { 44767a984708SDavid Chisnall if (__first2 == __last2) 44777a984708SDavid Chisnall return _VSTD::copy(__first1, __last1, __result); 44787a984708SDavid Chisnall if (__comp(*__first2, *__first1)) 44797a984708SDavid Chisnall { 44807a984708SDavid Chisnall *__result = *__first2; 44817a984708SDavid Chisnall ++__first2; 44827a984708SDavid Chisnall } 44837a984708SDavid Chisnall else 44847a984708SDavid Chisnall { 44857a984708SDavid Chisnall *__result = *__first1; 44867a984708SDavid Chisnall ++__first1; 44877a984708SDavid Chisnall } 44887a984708SDavid Chisnall } 44897a984708SDavid Chisnall return _VSTD::copy(__first2, __last2, __result); 44907a984708SDavid Chisnall} 44917a984708SDavid Chisnall 44927a984708SDavid Chisnalltemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare> 44937a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 44947a984708SDavid Chisnall_OutputIterator 44957a984708SDavid Chisnallmerge(_InputIterator1 __first1, _InputIterator1 __last1, 44967a984708SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) 44977a984708SDavid Chisnall{ 44984f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 44997a984708SDavid Chisnall typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 45007a984708SDavid Chisnall __debug_less<_Compare> __c(__comp); 45017a984708SDavid Chisnall return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c); 45024f7ab58eSDimitry Andric#else // _LIBCPP_DEBUG 45037a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 45047a984708SDavid Chisnall return _VSTD::__merge<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp); 45054f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 45067a984708SDavid Chisnall} 45077a984708SDavid Chisnall 45087a984708SDavid Chisnalltemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator> 45097a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 45107a984708SDavid Chisnall_OutputIterator 45117a984708SDavid Chisnallmerge(_InputIterator1 __first1, _InputIterator1 __last1, 45127a984708SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result) 45137a984708SDavid Chisnall{ 45147a984708SDavid Chisnall typedef typename iterator_traits<_InputIterator1>::value_type __v1; 45157a984708SDavid Chisnall typedef typename iterator_traits<_InputIterator2>::value_type __v2; 45167a984708SDavid Chisnall return merge(__first1, __last1, __first2, __last2, __result, __less<__v1, __v2>()); 45177a984708SDavid Chisnall} 45187a984708SDavid Chisnall 45197a984708SDavid Chisnall// inplace_merge 45207a984708SDavid Chisnall 4521854fa44bSDimitry Andrictemplate <class _Compare, class _InputIterator1, class _InputIterator2, 4522854fa44bSDimitry Andric class _OutputIterator> 4523854fa44bSDimitry Andricvoid __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1, 4524854fa44bSDimitry Andric _InputIterator2 __first2, _InputIterator2 __last2, 4525854fa44bSDimitry Andric _OutputIterator __result, _Compare __comp) 4526854fa44bSDimitry Andric{ 4527854fa44bSDimitry Andric for (; __first1 != __last1; ++__result) 4528854fa44bSDimitry Andric { 4529854fa44bSDimitry Andric if (__first2 == __last2) 4530854fa44bSDimitry Andric { 4531854fa44bSDimitry Andric _VSTD::move(__first1, __last1, __result); 4532854fa44bSDimitry Andric return; 4533854fa44bSDimitry Andric } 4534854fa44bSDimitry Andric 4535854fa44bSDimitry Andric if (__comp(*__first2, *__first1)) 4536854fa44bSDimitry Andric { 4537854fa44bSDimitry Andric *__result = _VSTD::move(*__first2); 4538854fa44bSDimitry Andric ++__first2; 4539854fa44bSDimitry Andric } 4540854fa44bSDimitry Andric else 4541854fa44bSDimitry Andric { 4542854fa44bSDimitry Andric *__result = _VSTD::move(*__first1); 4543854fa44bSDimitry Andric ++__first1; 4544854fa44bSDimitry Andric } 4545854fa44bSDimitry Andric } 4546854fa44bSDimitry Andric // __first2 through __last2 are already in the right spot. 4547854fa44bSDimitry Andric} 4548854fa44bSDimitry Andric 45497a984708SDavid Chisnalltemplate <class _Compare, class _BidirectionalIterator> 45507a984708SDavid Chisnallvoid 45517a984708SDavid Chisnall__buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last, 45527a984708SDavid Chisnall _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1, 45537a984708SDavid Chisnall typename iterator_traits<_BidirectionalIterator>::difference_type __len2, 45547a984708SDavid Chisnall typename iterator_traits<_BidirectionalIterator>::value_type* __buff) 45557a984708SDavid Chisnall{ 45567a984708SDavid Chisnall typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type; 45577a984708SDavid Chisnall __destruct_n __d(0); 45587a984708SDavid Chisnall unique_ptr<value_type, __destruct_n&> __h2(__buff, __d); 45597a984708SDavid Chisnall if (__len1 <= __len2) 45607a984708SDavid Chisnall { 45617a984708SDavid Chisnall value_type* __p = __buff; 4562d72607e9SDimitry Andric for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p) 45637a984708SDavid Chisnall ::new(__p) value_type(_VSTD::move(*__i)); 4564854fa44bSDimitry Andric __half_inplace_merge(__buff, __p, __middle, __last, __first, __comp); 45657a984708SDavid Chisnall } 45667a984708SDavid Chisnall else 45677a984708SDavid Chisnall { 45687a984708SDavid Chisnall value_type* __p = __buff; 4569d72607e9SDimitry Andric for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p) 45707a984708SDavid Chisnall ::new(__p) value_type(_VSTD::move(*__i)); 45717a984708SDavid Chisnall typedef reverse_iterator<_BidirectionalIterator> _RBi; 45727a984708SDavid Chisnall typedef reverse_iterator<value_type*> _Rv; 4573854fa44bSDimitry Andric __half_inplace_merge(_Rv(__p), _Rv(__buff), 4574854fa44bSDimitry Andric _RBi(__middle), _RBi(__first), 45757a984708SDavid Chisnall _RBi(__last), __negate<_Compare>(__comp)); 45767a984708SDavid Chisnall } 45777a984708SDavid Chisnall} 45787a984708SDavid Chisnall 45797a984708SDavid Chisnalltemplate <class _Compare, class _BidirectionalIterator> 45807a984708SDavid Chisnallvoid 45817a984708SDavid Chisnall__inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last, 45827a984708SDavid Chisnall _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1, 45837a984708SDavid Chisnall typename iterator_traits<_BidirectionalIterator>::difference_type __len2, 45847a984708SDavid Chisnall typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size) 45857a984708SDavid Chisnall{ 45867a984708SDavid Chisnall typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type; 45877a984708SDavid Chisnall while (true) 45887a984708SDavid Chisnall { 45897a984708SDavid Chisnall // if __middle == __last, we're done 45907a984708SDavid Chisnall if (__len2 == 0) 45917a984708SDavid Chisnall return; 4592854fa44bSDimitry Andric if (__len1 <= __buff_size || __len2 <= __buff_size) 4593854fa44bSDimitry Andric return __buffered_inplace_merge<_Compare> 4594854fa44bSDimitry Andric (__first, __middle, __last, __comp, __len1, __len2, __buff); 45957a984708SDavid Chisnall // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0 4596d72607e9SDimitry Andric for (; true; ++__first, (void) --__len1) 45977a984708SDavid Chisnall { 45987a984708SDavid Chisnall if (__len1 == 0) 45997a984708SDavid Chisnall return; 46007a984708SDavid Chisnall if (__comp(*__middle, *__first)) 46017a984708SDavid Chisnall break; 46027a984708SDavid Chisnall } 46037a984708SDavid Chisnall // __first < __middle < __last 46047a984708SDavid Chisnall // *__first > *__middle 46057a984708SDavid Chisnall // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that 46067a984708SDavid Chisnall // all elements in: 46077a984708SDavid Chisnall // [__first, __m1) <= [__middle, __m2) 46087a984708SDavid Chisnall // [__middle, __m2) < [__m1, __middle) 46097a984708SDavid Chisnall // [__m1, __middle) <= [__m2, __last) 46107a984708SDavid Chisnall // and __m1 or __m2 is in the middle of its range 46117a984708SDavid Chisnall _BidirectionalIterator __m1; // "median" of [__first, __middle) 46127a984708SDavid Chisnall _BidirectionalIterator __m2; // "median" of [__middle, __last) 46137a984708SDavid Chisnall difference_type __len11; // distance(__first, __m1) 46147a984708SDavid Chisnall difference_type __len21; // distance(__middle, __m2) 46157a984708SDavid Chisnall // binary search smaller range 46167a984708SDavid Chisnall if (__len1 < __len2) 46177a984708SDavid Chisnall { // __len >= 1, __len2 >= 2 46187a984708SDavid Chisnall __len21 = __len2 / 2; 46197a984708SDavid Chisnall __m2 = __middle; 46207a984708SDavid Chisnall _VSTD::advance(__m2, __len21); 46217a984708SDavid Chisnall __m1 = __upper_bound<_Compare>(__first, __middle, *__m2, __comp); 46227a984708SDavid Chisnall __len11 = _VSTD::distance(__first, __m1); 46237a984708SDavid Chisnall } 46247a984708SDavid Chisnall else 46257a984708SDavid Chisnall { 46267a984708SDavid Chisnall if (__len1 == 1) 46277a984708SDavid Chisnall { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1 46287a984708SDavid Chisnall // It is known *__first > *__middle 46297a984708SDavid Chisnall swap(*__first, *__middle); 46307a984708SDavid Chisnall return; 46317a984708SDavid Chisnall } 46327a984708SDavid Chisnall // __len1 >= 2, __len2 >= 1 46337a984708SDavid Chisnall __len11 = __len1 / 2; 46347a984708SDavid Chisnall __m1 = __first; 46357a984708SDavid Chisnall _VSTD::advance(__m1, __len11); 46367a984708SDavid Chisnall __m2 = __lower_bound<_Compare>(__middle, __last, *__m1, __comp); 46377a984708SDavid Chisnall __len21 = _VSTD::distance(__middle, __m2); 46387a984708SDavid Chisnall } 46397a984708SDavid Chisnall difference_type __len12 = __len1 - __len11; // distance(__m1, __middle) 46407a984708SDavid Chisnall difference_type __len22 = __len2 - __len21; // distance(__m2, __last) 46417a984708SDavid Chisnall // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) 46427a984708SDavid Chisnall // swap middle two partitions 46437a984708SDavid Chisnall __middle = _VSTD::rotate(__m1, __middle, __m2); 46447a984708SDavid Chisnall // __len12 and __len21 now have swapped meanings 46457a984708SDavid Chisnall // merge smaller range with recurisve call and larger with tail recursion elimination 46467a984708SDavid Chisnall if (__len11 + __len21 < __len12 + __len22) 46477a984708SDavid Chisnall { 46487a984708SDavid Chisnall __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size); 46497a984708SDavid Chisnall// __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size); 46507a984708SDavid Chisnall __first = __middle; 46517a984708SDavid Chisnall __middle = __m2; 46527a984708SDavid Chisnall __len1 = __len12; 46537a984708SDavid Chisnall __len2 = __len22; 46547a984708SDavid Chisnall } 46557a984708SDavid Chisnall else 46567a984708SDavid Chisnall { 46577a984708SDavid Chisnall __inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size); 46587a984708SDavid Chisnall// __inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size); 46597a984708SDavid Chisnall __last = __middle; 46607a984708SDavid Chisnall __middle = __m1; 46617a984708SDavid Chisnall __len1 = __len11; 46627a984708SDavid Chisnall __len2 = __len21; 46637a984708SDavid Chisnall } 46647a984708SDavid Chisnall } 46657a984708SDavid Chisnall} 46667a984708SDavid Chisnall 46677a984708SDavid Chisnalltemplate <class _BidirectionalIterator, class _Compare> 46687a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 46697a984708SDavid Chisnallvoid 46707a984708SDavid Chisnallinplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last, 46717a984708SDavid Chisnall _Compare __comp) 46727a984708SDavid Chisnall{ 46737a984708SDavid Chisnall typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type; 46747a984708SDavid Chisnall typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type; 46757a984708SDavid Chisnall difference_type __len1 = _VSTD::distance(__first, __middle); 46767a984708SDavid Chisnall difference_type __len2 = _VSTD::distance(__middle, __last); 46777a984708SDavid Chisnall difference_type __buf_size = _VSTD::min(__len1, __len2); 4678854fa44bSDimitry Andric pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size); 4679854fa44bSDimitry Andric unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first); 4680854fa44bSDimitry Andric 46814f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 46827a984708SDavid Chisnall typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 46837a984708SDavid Chisnall __debug_less<_Compare> __c(__comp); 46847a984708SDavid Chisnall return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __c, __len1, __len2, 46857a984708SDavid Chisnall __buf.first, __buf.second); 46864f7ab58eSDimitry Andric#else // _LIBCPP_DEBUG 46877a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 46887a984708SDavid Chisnall return _VSTD::__inplace_merge<_Comp_ref>(__first, __middle, __last, __comp, __len1, __len2, 46897a984708SDavid Chisnall __buf.first, __buf.second); 46904f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 46917a984708SDavid Chisnall} 46927a984708SDavid Chisnall 46937a984708SDavid Chisnalltemplate <class _BidirectionalIterator> 46947a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 46957a984708SDavid Chisnallvoid 46967a984708SDavid Chisnallinplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last) 46977a984708SDavid Chisnall{ 46987a984708SDavid Chisnall _VSTD::inplace_merge(__first, __middle, __last, 46997a984708SDavid Chisnall __less<typename iterator_traits<_BidirectionalIterator>::value_type>()); 47007a984708SDavid Chisnall} 47017a984708SDavid Chisnall 47027a984708SDavid Chisnall// stable_sort 47037a984708SDavid Chisnall 47047a984708SDavid Chisnalltemplate <class _Compare, class _InputIterator1, class _InputIterator2> 47057a984708SDavid Chisnallvoid 47067a984708SDavid Chisnall__merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1, 47077a984708SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2, 47087a984708SDavid Chisnall typename iterator_traits<_InputIterator1>::value_type* __result, _Compare __comp) 47097a984708SDavid Chisnall{ 47107a984708SDavid Chisnall typedef typename iterator_traits<_InputIterator1>::value_type value_type; 47117a984708SDavid Chisnall __destruct_n __d(0); 47127a984708SDavid Chisnall unique_ptr<value_type, __destruct_n&> __h(__result, __d); 47137a984708SDavid Chisnall for (; true; ++__result) 47147a984708SDavid Chisnall { 47157a984708SDavid Chisnall if (__first1 == __last1) 47167a984708SDavid Chisnall { 47177a984708SDavid Chisnall for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0)) 47187a984708SDavid Chisnall ::new (__result) value_type(_VSTD::move(*__first2)); 47197a984708SDavid Chisnall __h.release(); 47207a984708SDavid Chisnall return; 47217a984708SDavid Chisnall } 47227a984708SDavid Chisnall if (__first2 == __last2) 47237a984708SDavid Chisnall { 47247a984708SDavid Chisnall for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0)) 47257a984708SDavid Chisnall ::new (__result) value_type(_VSTD::move(*__first1)); 47267a984708SDavid Chisnall __h.release(); 47277a984708SDavid Chisnall return; 47287a984708SDavid Chisnall } 47297a984708SDavid Chisnall if (__comp(*__first2, *__first1)) 47307a984708SDavid Chisnall { 47317a984708SDavid Chisnall ::new (__result) value_type(_VSTD::move(*__first2)); 47327a984708SDavid Chisnall __d.__incr((value_type*)0); 47337a984708SDavid Chisnall ++__first2; 47347a984708SDavid Chisnall } 47357a984708SDavid Chisnall else 47367a984708SDavid Chisnall { 47377a984708SDavid Chisnall ::new (__result) value_type(_VSTD::move(*__first1)); 47387a984708SDavid Chisnall __d.__incr((value_type*)0); 47397a984708SDavid Chisnall ++__first1; 47407a984708SDavid Chisnall } 47417a984708SDavid Chisnall } 47427a984708SDavid Chisnall} 47437a984708SDavid Chisnall 47447a984708SDavid Chisnalltemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator> 47457a984708SDavid Chisnallvoid 47467a984708SDavid Chisnall__merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1, 47477a984708SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2, 47487a984708SDavid Chisnall _OutputIterator __result, _Compare __comp) 47497a984708SDavid Chisnall{ 47507a984708SDavid Chisnall for (; __first1 != __last1; ++__result) 47517a984708SDavid Chisnall { 47527a984708SDavid Chisnall if (__first2 == __last2) 47537a984708SDavid Chisnall { 47547a984708SDavid Chisnall for (; __first1 != __last1; ++__first1, ++__result) 47557a984708SDavid Chisnall *__result = _VSTD::move(*__first1); 47567a984708SDavid Chisnall return; 47577a984708SDavid Chisnall } 47587a984708SDavid Chisnall if (__comp(*__first2, *__first1)) 47597a984708SDavid Chisnall { 47607a984708SDavid Chisnall *__result = _VSTD::move(*__first2); 47617a984708SDavid Chisnall ++__first2; 47627a984708SDavid Chisnall } 47637a984708SDavid Chisnall else 47647a984708SDavid Chisnall { 47657a984708SDavid Chisnall *__result = _VSTD::move(*__first1); 47667a984708SDavid Chisnall ++__first1; 47677a984708SDavid Chisnall } 47687a984708SDavid Chisnall } 47697a984708SDavid Chisnall for (; __first2 != __last2; ++__first2, ++__result) 47707a984708SDavid Chisnall *__result = _VSTD::move(*__first2); 47717a984708SDavid Chisnall} 47727a984708SDavid Chisnall 47737a984708SDavid Chisnalltemplate <class _Compare, class _RandomAccessIterator> 47747a984708SDavid Chisnallvoid 47757a984708SDavid Chisnall__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp, 47767a984708SDavid Chisnall typename iterator_traits<_RandomAccessIterator>::difference_type __len, 47777a984708SDavid Chisnall typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size); 47787a984708SDavid Chisnall 47797a984708SDavid Chisnalltemplate <class _Compare, class _RandomAccessIterator> 47807a984708SDavid Chisnallvoid 47817a984708SDavid Chisnall__stable_sort_move(_RandomAccessIterator __first1, _RandomAccessIterator __last1, _Compare __comp, 47827a984708SDavid Chisnall typename iterator_traits<_RandomAccessIterator>::difference_type __len, 47837a984708SDavid Chisnall typename iterator_traits<_RandomAccessIterator>::value_type* __first2) 47847a984708SDavid Chisnall{ 47857a984708SDavid Chisnall typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type; 47867a984708SDavid Chisnall switch (__len) 47877a984708SDavid Chisnall { 47887a984708SDavid Chisnall case 0: 47897a984708SDavid Chisnall return; 47907a984708SDavid Chisnall case 1: 47917a984708SDavid Chisnall ::new(__first2) value_type(_VSTD::move(*__first1)); 47927a984708SDavid Chisnall return; 47937a984708SDavid Chisnall case 2: 47947a984708SDavid Chisnall __destruct_n __d(0); 47957a984708SDavid Chisnall unique_ptr<value_type, __destruct_n&> __h2(__first2, __d); 47967a984708SDavid Chisnall if (__comp(*--__last1, *__first1)) 47977a984708SDavid Chisnall { 47987a984708SDavid Chisnall ::new(__first2) value_type(_VSTD::move(*__last1)); 47997a984708SDavid Chisnall __d.__incr((value_type*)0); 48007a984708SDavid Chisnall ++__first2; 48017a984708SDavid Chisnall ::new(__first2) value_type(_VSTD::move(*__first1)); 48027a984708SDavid Chisnall } 48037a984708SDavid Chisnall else 48047a984708SDavid Chisnall { 48057a984708SDavid Chisnall ::new(__first2) value_type(_VSTD::move(*__first1)); 48067a984708SDavid Chisnall __d.__incr((value_type*)0); 48077a984708SDavid Chisnall ++__first2; 48087a984708SDavid Chisnall ::new(__first2) value_type(_VSTD::move(*__last1)); 48097a984708SDavid Chisnall } 48107a984708SDavid Chisnall __h2.release(); 48117a984708SDavid Chisnall return; 48127a984708SDavid Chisnall } 48137a984708SDavid Chisnall if (__len <= 8) 48147a984708SDavid Chisnall { 48157a984708SDavid Chisnall __insertion_sort_move<_Compare>(__first1, __last1, __first2, __comp); 48167a984708SDavid Chisnall return; 48177a984708SDavid Chisnall } 48187a984708SDavid Chisnall typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2; 48197a984708SDavid Chisnall _RandomAccessIterator __m = __first1 + __l2; 48207a984708SDavid Chisnall __stable_sort<_Compare>(__first1, __m, __comp, __l2, __first2, __l2); 48217a984708SDavid Chisnall __stable_sort<_Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2); 48227a984708SDavid Chisnall __merge_move_construct<_Compare>(__first1, __m, __m, __last1, __first2, __comp); 48237a984708SDavid Chisnall} 48247a984708SDavid Chisnall 48257a984708SDavid Chisnalltemplate <class _Tp> 48267a984708SDavid Chisnallstruct __stable_sort_switch 48277a984708SDavid Chisnall{ 48287a984708SDavid Chisnall static const unsigned value = 128*is_trivially_copy_assignable<_Tp>::value; 48297a984708SDavid Chisnall}; 48307a984708SDavid Chisnall 48317a984708SDavid Chisnalltemplate <class _Compare, class _RandomAccessIterator> 48327a984708SDavid Chisnallvoid 48337a984708SDavid Chisnall__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp, 48347a984708SDavid Chisnall typename iterator_traits<_RandomAccessIterator>::difference_type __len, 48357a984708SDavid Chisnall typename iterator_traits<_RandomAccessIterator>::value_type* __buff, ptrdiff_t __buff_size) 48367a984708SDavid Chisnall{ 48377a984708SDavid Chisnall typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type; 48387a984708SDavid Chisnall typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; 48397a984708SDavid Chisnall switch (__len) 48407a984708SDavid Chisnall { 48417a984708SDavid Chisnall case 0: 48427a984708SDavid Chisnall case 1: 48437a984708SDavid Chisnall return; 48447a984708SDavid Chisnall case 2: 48457a984708SDavid Chisnall if (__comp(*--__last, *__first)) 48467a984708SDavid Chisnall swap(*__first, *__last); 48477a984708SDavid Chisnall return; 48487a984708SDavid Chisnall } 48497a984708SDavid Chisnall if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value)) 48507a984708SDavid Chisnall { 48517a984708SDavid Chisnall __insertion_sort<_Compare>(__first, __last, __comp); 48527a984708SDavid Chisnall return; 48537a984708SDavid Chisnall } 48547a984708SDavid Chisnall typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2; 48557a984708SDavid Chisnall _RandomAccessIterator __m = __first + __l2; 48567a984708SDavid Chisnall if (__len <= __buff_size) 48577a984708SDavid Chisnall { 48587a984708SDavid Chisnall __destruct_n __d(0); 48597a984708SDavid Chisnall unique_ptr<value_type, __destruct_n&> __h2(__buff, __d); 48607a984708SDavid Chisnall __stable_sort_move<_Compare>(__first, __m, __comp, __l2, __buff); 48617a984708SDavid Chisnall __d.__set(__l2, (value_type*)0); 48627a984708SDavid Chisnall __stable_sort_move<_Compare>(__m, __last, __comp, __len - __l2, __buff + __l2); 48637a984708SDavid Chisnall __d.__set(__len, (value_type*)0); 48647a984708SDavid Chisnall __merge_move_assign<_Compare>(__buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp); 48657a984708SDavid Chisnall// __merge<_Compare>(move_iterator<value_type*>(__buff), 48667a984708SDavid Chisnall// move_iterator<value_type*>(__buff + __l2), 48677a984708SDavid Chisnall// move_iterator<_RandomAccessIterator>(__buff + __l2), 48687a984708SDavid Chisnall// move_iterator<_RandomAccessIterator>(__buff + __len), 48697a984708SDavid Chisnall// __first, __comp); 48707a984708SDavid Chisnall return; 48717a984708SDavid Chisnall } 48727a984708SDavid Chisnall __stable_sort<_Compare>(__first, __m, __comp, __l2, __buff, __buff_size); 48737a984708SDavid Chisnall __stable_sort<_Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size); 48747a984708SDavid Chisnall __inplace_merge<_Compare>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size); 48757a984708SDavid Chisnall} 48767a984708SDavid Chisnall 48777a984708SDavid Chisnalltemplate <class _RandomAccessIterator, class _Compare> 48787a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 48797a984708SDavid Chisnallvoid 48807a984708SDavid Chisnallstable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 48817a984708SDavid Chisnall{ 48827a984708SDavid Chisnall typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type; 48837a984708SDavid Chisnall typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; 48847a984708SDavid Chisnall difference_type __len = __last - __first; 48857a984708SDavid Chisnall pair<value_type*, ptrdiff_t> __buf(0, 0); 48867a984708SDavid Chisnall unique_ptr<value_type, __return_temporary_buffer> __h; 48877a984708SDavid Chisnall if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value)) 48887a984708SDavid Chisnall { 48897a984708SDavid Chisnall __buf = _VSTD::get_temporary_buffer<value_type>(__len); 48907a984708SDavid Chisnall __h.reset(__buf.first); 48917a984708SDavid Chisnall } 48924f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 48937a984708SDavid Chisnall typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 48947a984708SDavid Chisnall __debug_less<_Compare> __c(__comp); 48957a984708SDavid Chisnall __stable_sort<_Comp_ref>(__first, __last, __c, __len, __buf.first, __buf.second); 48964f7ab58eSDimitry Andric#else // _LIBCPP_DEBUG 48977a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 48987a984708SDavid Chisnall __stable_sort<_Comp_ref>(__first, __last, __comp, __len, __buf.first, __buf.second); 48994f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 49007a984708SDavid Chisnall} 49017a984708SDavid Chisnall 49027a984708SDavid Chisnalltemplate <class _RandomAccessIterator> 49037a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 49047a984708SDavid Chisnallvoid 49057a984708SDavid Chisnallstable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last) 49067a984708SDavid Chisnall{ 49077a984708SDavid Chisnall _VSTD::stable_sort(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 49087a984708SDavid Chisnall} 49097a984708SDavid Chisnall 49107a984708SDavid Chisnall// is_heap_until 49117a984708SDavid Chisnall 49127a984708SDavid Chisnalltemplate <class _RandomAccessIterator, class _Compare> 49137a984708SDavid Chisnall_RandomAccessIterator 49147a984708SDavid Chisnallis_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 49157a984708SDavid Chisnall{ 49167a984708SDavid Chisnall typedef typename _VSTD::iterator_traits<_RandomAccessIterator>::difference_type difference_type; 49177a984708SDavid Chisnall difference_type __len = __last - __first; 49187a984708SDavid Chisnall difference_type __p = 0; 49197a984708SDavid Chisnall difference_type __c = 1; 49207a984708SDavid Chisnall _RandomAccessIterator __pp = __first; 49217a984708SDavid Chisnall while (__c < __len) 49227a984708SDavid Chisnall { 49237a984708SDavid Chisnall _RandomAccessIterator __cp = __first + __c; 49247a984708SDavid Chisnall if (__comp(*__pp, *__cp)) 49257a984708SDavid Chisnall return __cp; 49267a984708SDavid Chisnall ++__c; 49277a984708SDavid Chisnall ++__cp; 49287a984708SDavid Chisnall if (__c == __len) 49297a984708SDavid Chisnall return __last; 49307a984708SDavid Chisnall if (__comp(*__pp, *__cp)) 49317a984708SDavid Chisnall return __cp; 49327a984708SDavid Chisnall ++__p; 49337a984708SDavid Chisnall ++__pp; 49347a984708SDavid Chisnall __c = 2 * __p + 1; 49357a984708SDavid Chisnall } 49367a984708SDavid Chisnall return __last; 49377a984708SDavid Chisnall} 49387a984708SDavid Chisnall 49397a984708SDavid Chisnalltemplate<class _RandomAccessIterator> 49407a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 49417a984708SDavid Chisnall_RandomAccessIterator 49427a984708SDavid Chisnallis_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last) 49437a984708SDavid Chisnall{ 49447a984708SDavid Chisnall return _VSTD::is_heap_until(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 49457a984708SDavid Chisnall} 49467a984708SDavid Chisnall 49477a984708SDavid Chisnall// is_heap 49487a984708SDavid Chisnall 49497a984708SDavid Chisnalltemplate <class _RandomAccessIterator, class _Compare> 49507a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 49517a984708SDavid Chisnallbool 49527a984708SDavid Chisnallis_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 49537a984708SDavid Chisnall{ 49547a984708SDavid Chisnall return _VSTD::is_heap_until(__first, __last, __comp) == __last; 49557a984708SDavid Chisnall} 49567a984708SDavid Chisnall 49577a984708SDavid Chisnalltemplate<class _RandomAccessIterator> 49587a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 49597a984708SDavid Chisnallbool 49607a984708SDavid Chisnallis_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) 49617a984708SDavid Chisnall{ 49627a984708SDavid Chisnall return _VSTD::is_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 49637a984708SDavid Chisnall} 49647a984708SDavid Chisnall 49657a984708SDavid Chisnall// push_heap 49667a984708SDavid Chisnall 49677a984708SDavid Chisnalltemplate <class _Compare, class _RandomAccessIterator> 49687a984708SDavid Chisnallvoid 4969d72607e9SDimitry Andric__sift_up(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp, 49707a984708SDavid Chisnall typename iterator_traits<_RandomAccessIterator>::difference_type __len) 49717a984708SDavid Chisnall{ 49727a984708SDavid Chisnall typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type; 49737a984708SDavid Chisnall if (__len > 1) 49747a984708SDavid Chisnall { 49757a984708SDavid Chisnall __len = (__len - 2) / 2; 49767a984708SDavid Chisnall _RandomAccessIterator __ptr = __first + __len; 49777a984708SDavid Chisnall if (__comp(*__ptr, *--__last)) 49787a984708SDavid Chisnall { 49797a984708SDavid Chisnall value_type __t(_VSTD::move(*__last)); 49807a984708SDavid Chisnall do 49817a984708SDavid Chisnall { 49827a984708SDavid Chisnall *__last = _VSTD::move(*__ptr); 49837a984708SDavid Chisnall __last = __ptr; 49847a984708SDavid Chisnall if (__len == 0) 49857a984708SDavid Chisnall break; 49867a984708SDavid Chisnall __len = (__len - 1) / 2; 49877a984708SDavid Chisnall __ptr = __first + __len; 49887a984708SDavid Chisnall } while (__comp(*__ptr, __t)); 49897a984708SDavid Chisnall *__last = _VSTD::move(__t); 49907a984708SDavid Chisnall } 49917a984708SDavid Chisnall } 49927a984708SDavid Chisnall} 49937a984708SDavid Chisnall 49947a984708SDavid Chisnalltemplate <class _RandomAccessIterator, class _Compare> 49957a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 49967a984708SDavid Chisnallvoid 49977a984708SDavid Chisnallpush_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 49987a984708SDavid Chisnall{ 49994f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 50007a984708SDavid Chisnall typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 50017a984708SDavid Chisnall __debug_less<_Compare> __c(__comp); 5002d72607e9SDimitry Andric __sift_up<_Comp_ref>(__first, __last, __c, __last - __first); 50034f7ab58eSDimitry Andric#else // _LIBCPP_DEBUG 50047a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 5005d72607e9SDimitry Andric __sift_up<_Comp_ref>(__first, __last, __comp, __last - __first); 50064f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 50077a984708SDavid Chisnall} 50087a984708SDavid Chisnall 50097a984708SDavid Chisnalltemplate <class _RandomAccessIterator> 50107a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 50117a984708SDavid Chisnallvoid 50127a984708SDavid Chisnallpush_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) 50137a984708SDavid Chisnall{ 50147a984708SDavid Chisnall _VSTD::push_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 50157a984708SDavid Chisnall} 50167a984708SDavid Chisnall 50177a984708SDavid Chisnall// pop_heap 50187a984708SDavid Chisnall 50197a984708SDavid Chisnalltemplate <class _Compare, class _RandomAccessIterator> 5020d72607e9SDimitry Andricvoid 5021aed8d94eSDimitry Andric__sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/, 5022aed8d94eSDimitry Andric _Compare __comp, 5023d72607e9SDimitry Andric typename iterator_traits<_RandomAccessIterator>::difference_type __len, 5024d72607e9SDimitry Andric _RandomAccessIterator __start) 5025d72607e9SDimitry Andric{ 5026d72607e9SDimitry Andric typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; 5027d72607e9SDimitry Andric typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type; 5028d72607e9SDimitry Andric // left-child of __start is at 2 * __start + 1 5029d72607e9SDimitry Andric // right-child of __start is at 2 * __start + 2 5030d72607e9SDimitry Andric difference_type __child = __start - __first; 5031d72607e9SDimitry Andric 5032d72607e9SDimitry Andric if (__len < 2 || (__len - 2) / 2 < __child) 5033d72607e9SDimitry Andric return; 5034d72607e9SDimitry Andric 5035d72607e9SDimitry Andric __child = 2 * __child + 1; 5036d72607e9SDimitry Andric _RandomAccessIterator __child_i = __first + __child; 5037d72607e9SDimitry Andric 5038d72607e9SDimitry Andric if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) { 5039d72607e9SDimitry Andric // right-child exists and is greater than left-child 5040d72607e9SDimitry Andric ++__child_i; 5041d72607e9SDimitry Andric ++__child; 5042d72607e9SDimitry Andric } 5043d72607e9SDimitry Andric 5044d72607e9SDimitry Andric // check if we are in heap-order 5045d72607e9SDimitry Andric if (__comp(*__child_i, *__start)) 5046d72607e9SDimitry Andric // we are, __start is larger than it's largest child 5047d72607e9SDimitry Andric return; 5048d72607e9SDimitry Andric 5049d72607e9SDimitry Andric value_type __top(_VSTD::move(*__start)); 5050d72607e9SDimitry Andric do 5051d72607e9SDimitry Andric { 5052d72607e9SDimitry Andric // we are not in heap-order, swap the parent with it's largest child 5053d72607e9SDimitry Andric *__start = _VSTD::move(*__child_i); 5054d72607e9SDimitry Andric __start = __child_i; 5055d72607e9SDimitry Andric 5056d72607e9SDimitry Andric if ((__len - 2) / 2 < __child) 5057d72607e9SDimitry Andric break; 5058d72607e9SDimitry Andric 5059d72607e9SDimitry Andric // recompute the child based off of the updated parent 5060d72607e9SDimitry Andric __child = 2 * __child + 1; 5061d72607e9SDimitry Andric __child_i = __first + __child; 5062d72607e9SDimitry Andric 5063d72607e9SDimitry Andric if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) { 5064d72607e9SDimitry Andric // right-child exists and is greater than left-child 5065d72607e9SDimitry Andric ++__child_i; 5066d72607e9SDimitry Andric ++__child; 5067d72607e9SDimitry Andric } 5068d72607e9SDimitry Andric 5069d72607e9SDimitry Andric // check if we are in heap-order 5070d72607e9SDimitry Andric } while (!__comp(*__child_i, __top)); 5071d72607e9SDimitry Andric *__start = _VSTD::move(__top); 5072d72607e9SDimitry Andric} 5073d72607e9SDimitry Andric 5074d72607e9SDimitry Andrictemplate <class _Compare, class _RandomAccessIterator> 50757a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 50767a984708SDavid Chisnallvoid 50777a984708SDavid Chisnall__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp, 50787a984708SDavid Chisnall typename iterator_traits<_RandomAccessIterator>::difference_type __len) 50797a984708SDavid Chisnall{ 50807a984708SDavid Chisnall if (__len > 1) 50817a984708SDavid Chisnall { 50827a984708SDavid Chisnall swap(*__first, *--__last); 5083d72607e9SDimitry Andric __sift_down<_Compare>(__first, __last, __comp, __len - 1, __first); 50847a984708SDavid Chisnall } 50857a984708SDavid Chisnall} 50867a984708SDavid Chisnall 50877a984708SDavid Chisnalltemplate <class _RandomAccessIterator, class _Compare> 50887a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 50897a984708SDavid Chisnallvoid 50907a984708SDavid Chisnallpop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 50917a984708SDavid Chisnall{ 50924f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 50937a984708SDavid Chisnall typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 50947a984708SDavid Chisnall __debug_less<_Compare> __c(__comp); 50957a984708SDavid Chisnall __pop_heap<_Comp_ref>(__first, __last, __c, __last - __first); 50964f7ab58eSDimitry Andric#else // _LIBCPP_DEBUG 50977a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 50987a984708SDavid Chisnall __pop_heap<_Comp_ref>(__first, __last, __comp, __last - __first); 50994f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 51007a984708SDavid Chisnall} 51017a984708SDavid Chisnall 51027a984708SDavid Chisnalltemplate <class _RandomAccessIterator> 51037a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 51047a984708SDavid Chisnallvoid 51057a984708SDavid Chisnallpop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) 51067a984708SDavid Chisnall{ 51077a984708SDavid Chisnall _VSTD::pop_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 51087a984708SDavid Chisnall} 51097a984708SDavid Chisnall 51107a984708SDavid Chisnall// make_heap 51117a984708SDavid Chisnall 51127a984708SDavid Chisnalltemplate <class _Compare, class _RandomAccessIterator> 51137a984708SDavid Chisnallvoid 51147a984708SDavid Chisnall__make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 51157a984708SDavid Chisnall{ 51167a984708SDavid Chisnall typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; 51177a984708SDavid Chisnall difference_type __n = __last - __first; 51187a984708SDavid Chisnall if (__n > 1) 51197a984708SDavid Chisnall { 5120d72607e9SDimitry Andric // start from the first parent, there is no need to consider children 5121d72607e9SDimitry Andric for (difference_type __start = (__n - 2) / 2; __start >= 0; --__start) 5122d72607e9SDimitry Andric { 5123d72607e9SDimitry Andric __sift_down<_Compare>(__first, __last, __comp, __n, __first + __start); 5124d72607e9SDimitry Andric } 51257a984708SDavid Chisnall } 51267a984708SDavid Chisnall} 51277a984708SDavid Chisnall 51287a984708SDavid Chisnalltemplate <class _RandomAccessIterator, class _Compare> 51297a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 51307a984708SDavid Chisnallvoid 51317a984708SDavid Chisnallmake_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 51327a984708SDavid Chisnall{ 51334f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 51347a984708SDavid Chisnall typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 51357a984708SDavid Chisnall __debug_less<_Compare> __c(__comp); 51367a984708SDavid Chisnall __make_heap<_Comp_ref>(__first, __last, __c); 51374f7ab58eSDimitry Andric#else // _LIBCPP_DEBUG 51387a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 51397a984708SDavid Chisnall __make_heap<_Comp_ref>(__first, __last, __comp); 51404f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 51417a984708SDavid Chisnall} 51427a984708SDavid Chisnall 51437a984708SDavid Chisnalltemplate <class _RandomAccessIterator> 51447a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 51457a984708SDavid Chisnallvoid 51467a984708SDavid Chisnallmake_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) 51477a984708SDavid Chisnall{ 51487a984708SDavid Chisnall _VSTD::make_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 51497a984708SDavid Chisnall} 51507a984708SDavid Chisnall 51517a984708SDavid Chisnall// sort_heap 51527a984708SDavid Chisnall 51537a984708SDavid Chisnalltemplate <class _Compare, class _RandomAccessIterator> 51547a984708SDavid Chisnallvoid 51557a984708SDavid Chisnall__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 51567a984708SDavid Chisnall{ 51577a984708SDavid Chisnall typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; 51587a984708SDavid Chisnall for (difference_type __n = __last - __first; __n > 1; --__last, --__n) 51597a984708SDavid Chisnall __pop_heap<_Compare>(__first, __last, __comp, __n); 51607a984708SDavid Chisnall} 51617a984708SDavid Chisnall 51627a984708SDavid Chisnalltemplate <class _RandomAccessIterator, class _Compare> 51637a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 51647a984708SDavid Chisnallvoid 51657a984708SDavid Chisnallsort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) 51667a984708SDavid Chisnall{ 51674f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 51687a984708SDavid Chisnall typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 51697a984708SDavid Chisnall __debug_less<_Compare> __c(__comp); 51707a984708SDavid Chisnall __sort_heap<_Comp_ref>(__first, __last, __c); 51714f7ab58eSDimitry Andric#else // _LIBCPP_DEBUG 51727a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 51737a984708SDavid Chisnall __sort_heap<_Comp_ref>(__first, __last, __comp); 51744f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 51757a984708SDavid Chisnall} 51767a984708SDavid Chisnall 51777a984708SDavid Chisnalltemplate <class _RandomAccessIterator> 51787a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 51797a984708SDavid Chisnallvoid 51807a984708SDavid Chisnallsort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) 51817a984708SDavid Chisnall{ 51827a984708SDavid Chisnall _VSTD::sort_heap(__first, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 51837a984708SDavid Chisnall} 51847a984708SDavid Chisnall 51857a984708SDavid Chisnall// partial_sort 51867a984708SDavid Chisnall 51877a984708SDavid Chisnalltemplate <class _Compare, class _RandomAccessIterator> 51887a984708SDavid Chisnallvoid 51897a984708SDavid Chisnall__partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last, 51907a984708SDavid Chisnall _Compare __comp) 51917a984708SDavid Chisnall{ 51927a984708SDavid Chisnall __make_heap<_Compare>(__first, __middle, __comp); 51937a984708SDavid Chisnall typename iterator_traits<_RandomAccessIterator>::difference_type __len = __middle - __first; 51947a984708SDavid Chisnall for (_RandomAccessIterator __i = __middle; __i != __last; ++__i) 51957a984708SDavid Chisnall { 51967a984708SDavid Chisnall if (__comp(*__i, *__first)) 51977a984708SDavid Chisnall { 51987a984708SDavid Chisnall swap(*__i, *__first); 5199d72607e9SDimitry Andric __sift_down<_Compare>(__first, __middle, __comp, __len, __first); 52007a984708SDavid Chisnall } 52017a984708SDavid Chisnall } 52027a984708SDavid Chisnall __sort_heap<_Compare>(__first, __middle, __comp); 52037a984708SDavid Chisnall} 52047a984708SDavid Chisnall 52057a984708SDavid Chisnalltemplate <class _RandomAccessIterator, class _Compare> 52067a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 52077a984708SDavid Chisnallvoid 52087a984708SDavid Chisnallpartial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last, 52097a984708SDavid Chisnall _Compare __comp) 52107a984708SDavid Chisnall{ 52114f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 52127a984708SDavid Chisnall typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 52137a984708SDavid Chisnall __debug_less<_Compare> __c(__comp); 52147a984708SDavid Chisnall __partial_sort<_Comp_ref>(__first, __middle, __last, __c); 52154f7ab58eSDimitry Andric#else // _LIBCPP_DEBUG 52167a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 52177a984708SDavid Chisnall __partial_sort<_Comp_ref>(__first, __middle, __last, __comp); 52184f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 52197a984708SDavid Chisnall} 52207a984708SDavid Chisnall 52217a984708SDavid Chisnalltemplate <class _RandomAccessIterator> 52227a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 52237a984708SDavid Chisnallvoid 52247a984708SDavid Chisnallpartial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last) 52257a984708SDavid Chisnall{ 52267a984708SDavid Chisnall _VSTD::partial_sort(__first, __middle, __last, 52277a984708SDavid Chisnall __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 52287a984708SDavid Chisnall} 52297a984708SDavid Chisnall 52307a984708SDavid Chisnall// partial_sort_copy 52317a984708SDavid Chisnall 52327a984708SDavid Chisnalltemplate <class _Compare, class _InputIterator, class _RandomAccessIterator> 52337a984708SDavid Chisnall_RandomAccessIterator 52347a984708SDavid Chisnall__partial_sort_copy(_InputIterator __first, _InputIterator __last, 52357a984708SDavid Chisnall _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp) 52367a984708SDavid Chisnall{ 52377a984708SDavid Chisnall _RandomAccessIterator __r = __result_first; 52387a984708SDavid Chisnall if (__r != __result_last) 52397a984708SDavid Chisnall { 5240d72607e9SDimitry Andric for (; __first != __last && __r != __result_last; (void) ++__first, ++__r) 52417a984708SDavid Chisnall *__r = *__first; 52427a984708SDavid Chisnall __make_heap<_Compare>(__result_first, __r, __comp); 5243d72607e9SDimitry Andric typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first; 52447a984708SDavid Chisnall for (; __first != __last; ++__first) 52457a984708SDavid Chisnall if (__comp(*__first, *__result_first)) 52467a984708SDavid Chisnall { 52477a984708SDavid Chisnall *__result_first = *__first; 5248d72607e9SDimitry Andric __sift_down<_Compare>(__result_first, __r, __comp, __len, __result_first); 52497a984708SDavid Chisnall } 52507a984708SDavid Chisnall __sort_heap<_Compare>(__result_first, __r, __comp); 52517a984708SDavid Chisnall } 52527a984708SDavid Chisnall return __r; 52537a984708SDavid Chisnall} 52547a984708SDavid Chisnall 52557a984708SDavid Chisnalltemplate <class _InputIterator, class _RandomAccessIterator, class _Compare> 52567a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 52577a984708SDavid Chisnall_RandomAccessIterator 52587a984708SDavid Chisnallpartial_sort_copy(_InputIterator __first, _InputIterator __last, 52597a984708SDavid Chisnall _RandomAccessIterator __result_first, _RandomAccessIterator __result_last, _Compare __comp) 52607a984708SDavid Chisnall{ 52614f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 52627a984708SDavid Chisnall typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 52637a984708SDavid Chisnall __debug_less<_Compare> __c(__comp); 52647a984708SDavid Chisnall return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __c); 52654f7ab58eSDimitry Andric#else // _LIBCPP_DEBUG 52667a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 52677a984708SDavid Chisnall return __partial_sort_copy<_Comp_ref>(__first, __last, __result_first, __result_last, __comp); 52684f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 52697a984708SDavid Chisnall} 52707a984708SDavid Chisnall 52717a984708SDavid Chisnalltemplate <class _InputIterator, class _RandomAccessIterator> 52727a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 52737a984708SDavid Chisnall_RandomAccessIterator 52747a984708SDavid Chisnallpartial_sort_copy(_InputIterator __first, _InputIterator __last, 52757a984708SDavid Chisnall _RandomAccessIterator __result_first, _RandomAccessIterator __result_last) 52767a984708SDavid Chisnall{ 52777a984708SDavid Chisnall return _VSTD::partial_sort_copy(__first, __last, __result_first, __result_last, 52787a984708SDavid Chisnall __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 52797a984708SDavid Chisnall} 52807a984708SDavid Chisnall 52817a984708SDavid Chisnall// nth_element 52827a984708SDavid Chisnall 52837a984708SDavid Chisnalltemplate <class _Compare, class _RandomAccessIterator> 52847a984708SDavid Chisnallvoid 52857a984708SDavid Chisnall__nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp) 52867a984708SDavid Chisnall{ 52877a984708SDavid Chisnall // _Compare is known to be a reference type 52887a984708SDavid Chisnall typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type; 52897a984708SDavid Chisnall const difference_type __limit = 7; 52907a984708SDavid Chisnall while (true) 52917a984708SDavid Chisnall { 52927a984708SDavid Chisnall __restart: 529394e3ee44SDavid Chisnall if (__nth == __last) 529494e3ee44SDavid Chisnall return; 52957a984708SDavid Chisnall difference_type __len = __last - __first; 52967a984708SDavid Chisnall switch (__len) 52977a984708SDavid Chisnall { 52987a984708SDavid Chisnall case 0: 52997a984708SDavid Chisnall case 1: 53007a984708SDavid Chisnall return; 53017a984708SDavid Chisnall case 2: 53027a984708SDavid Chisnall if (__comp(*--__last, *__first)) 53037a984708SDavid Chisnall swap(*__first, *__last); 53047a984708SDavid Chisnall return; 53057a984708SDavid Chisnall case 3: 53067a984708SDavid Chisnall { 53077a984708SDavid Chisnall _RandomAccessIterator __m = __first; 53087a984708SDavid Chisnall _VSTD::__sort3<_Compare>(__first, ++__m, --__last, __comp); 53097a984708SDavid Chisnall return; 53107a984708SDavid Chisnall } 53117a984708SDavid Chisnall } 53127a984708SDavid Chisnall if (__len <= __limit) 53137a984708SDavid Chisnall { 53147a984708SDavid Chisnall __selection_sort<_Compare>(__first, __last, __comp); 53157a984708SDavid Chisnall return; 53167a984708SDavid Chisnall } 53177a984708SDavid Chisnall // __len > __limit >= 3 53187a984708SDavid Chisnall _RandomAccessIterator __m = __first + __len/2; 53197a984708SDavid Chisnall _RandomAccessIterator __lm1 = __last; 53207a984708SDavid Chisnall unsigned __n_swaps = _VSTD::__sort3<_Compare>(__first, __m, --__lm1, __comp); 53217a984708SDavid Chisnall // *__m is median 53227a984708SDavid Chisnall // partition [__first, __m) < *__m and *__m <= [__m, __last) 53237a984708SDavid Chisnall // (this inhibits tossing elements equivalent to __m around unnecessarily) 53247a984708SDavid Chisnall _RandomAccessIterator __i = __first; 53257a984708SDavid Chisnall _RandomAccessIterator __j = __lm1; 53267a984708SDavid Chisnall // j points beyond range to be tested, *__lm1 is known to be <= *__m 53277a984708SDavid Chisnall // The search going up is known to be guarded but the search coming down isn't. 53287a984708SDavid Chisnall // Prime the downward search with a guard. 53297a984708SDavid Chisnall if (!__comp(*__i, *__m)) // if *__first == *__m 53307a984708SDavid Chisnall { 53317a984708SDavid Chisnall // *__first == *__m, *__first doesn't go in first part 53327a984708SDavid Chisnall // manually guard downward moving __j against __i 53337a984708SDavid Chisnall while (true) 53347a984708SDavid Chisnall { 53357a984708SDavid Chisnall if (__i == --__j) 53367a984708SDavid Chisnall { 53377a984708SDavid Chisnall // *__first == *__m, *__m <= all other elements 53387a984708SDavid Chisnall // Parition instead into [__first, __i) == *__first and *__first < [__i, __last) 53397a984708SDavid Chisnall ++__i; // __first + 1 53407a984708SDavid Chisnall __j = __last; 53417a984708SDavid Chisnall if (!__comp(*__first, *--__j)) // we need a guard if *__first == *(__last-1) 53427a984708SDavid Chisnall { 53437a984708SDavid Chisnall while (true) 53447a984708SDavid Chisnall { 53457a984708SDavid Chisnall if (__i == __j) 53467a984708SDavid Chisnall return; // [__first, __last) all equivalent elements 53477a984708SDavid Chisnall if (__comp(*__first, *__i)) 53487a984708SDavid Chisnall { 53497a984708SDavid Chisnall swap(*__i, *__j); 53507a984708SDavid Chisnall ++__n_swaps; 53517a984708SDavid Chisnall ++__i; 53527a984708SDavid Chisnall break; 53537a984708SDavid Chisnall } 53547a984708SDavid Chisnall ++__i; 53557a984708SDavid Chisnall } 53567a984708SDavid Chisnall } 53577a984708SDavid Chisnall // [__first, __i) == *__first and *__first < [__j, __last) and __j == __last - 1 53587a984708SDavid Chisnall if (__i == __j) 53597a984708SDavid Chisnall return; 53607a984708SDavid Chisnall while (true) 53617a984708SDavid Chisnall { 53627a984708SDavid Chisnall while (!__comp(*__first, *__i)) 53637a984708SDavid Chisnall ++__i; 53647a984708SDavid Chisnall while (__comp(*__first, *--__j)) 53657a984708SDavid Chisnall ; 53667a984708SDavid Chisnall if (__i >= __j) 53677a984708SDavid Chisnall break; 53687a984708SDavid Chisnall swap(*__i, *__j); 53697a984708SDavid Chisnall ++__n_swaps; 53707a984708SDavid Chisnall ++__i; 53717a984708SDavid Chisnall } 53727a984708SDavid Chisnall // [__first, __i) == *__first and *__first < [__i, __last) 53737a984708SDavid Chisnall // The first part is sorted, 53747a984708SDavid Chisnall if (__nth < __i) 53757a984708SDavid Chisnall return; 53767a984708SDavid Chisnall // __nth_element the secod part 53777a984708SDavid Chisnall // __nth_element<_Compare>(__i, __nth, __last, __comp); 53787a984708SDavid Chisnall __first = __i; 53797a984708SDavid Chisnall goto __restart; 53807a984708SDavid Chisnall } 53817a984708SDavid Chisnall if (__comp(*__j, *__m)) 53827a984708SDavid Chisnall { 53837a984708SDavid Chisnall swap(*__i, *__j); 53847a984708SDavid Chisnall ++__n_swaps; 53857a984708SDavid Chisnall break; // found guard for downward moving __j, now use unguarded partition 53867a984708SDavid Chisnall } 53877a984708SDavid Chisnall } 53887a984708SDavid Chisnall } 53897a984708SDavid Chisnall ++__i; 53907a984708SDavid Chisnall // j points beyond range to be tested, *__lm1 is known to be <= *__m 53917a984708SDavid Chisnall // if not yet partitioned... 53927a984708SDavid Chisnall if (__i < __j) 53937a984708SDavid Chisnall { 53947a984708SDavid Chisnall // known that *(__i - 1) < *__m 53957a984708SDavid Chisnall while (true) 53967a984708SDavid Chisnall { 53977a984708SDavid Chisnall // __m still guards upward moving __i 53987a984708SDavid Chisnall while (__comp(*__i, *__m)) 53997a984708SDavid Chisnall ++__i; 54007a984708SDavid Chisnall // It is now known that a guard exists for downward moving __j 54017a984708SDavid Chisnall while (!__comp(*--__j, *__m)) 54027a984708SDavid Chisnall ; 54037a984708SDavid Chisnall if (__i >= __j) 54047a984708SDavid Chisnall break; 54057a984708SDavid Chisnall swap(*__i, *__j); 54067a984708SDavid Chisnall ++__n_swaps; 54077a984708SDavid Chisnall // It is known that __m != __j 54087a984708SDavid Chisnall // If __m just moved, follow it 54097a984708SDavid Chisnall if (__m == __i) 54107a984708SDavid Chisnall __m = __j; 54117a984708SDavid Chisnall ++__i; 54127a984708SDavid Chisnall } 54137a984708SDavid Chisnall } 54147a984708SDavid Chisnall // [__first, __i) < *__m and *__m <= [__i, __last) 54157a984708SDavid Chisnall if (__i != __m && __comp(*__m, *__i)) 54167a984708SDavid Chisnall { 54177a984708SDavid Chisnall swap(*__i, *__m); 54187a984708SDavid Chisnall ++__n_swaps; 54197a984708SDavid Chisnall } 54207a984708SDavid Chisnall // [__first, __i) < *__i and *__i <= [__i+1, __last) 54217a984708SDavid Chisnall if (__nth == __i) 54227a984708SDavid Chisnall return; 54237a984708SDavid Chisnall if (__n_swaps == 0) 54247a984708SDavid Chisnall { 54257a984708SDavid Chisnall // We were given a perfectly partitioned sequence. Coincidence? 54267a984708SDavid Chisnall if (__nth < __i) 54277a984708SDavid Chisnall { 54287a984708SDavid Chisnall // Check for [__first, __i) already sorted 54297a984708SDavid Chisnall __j = __m = __first; 54307a984708SDavid Chisnall while (++__j != __i) 54317a984708SDavid Chisnall { 54327a984708SDavid Chisnall if (__comp(*__j, *__m)) 54337a984708SDavid Chisnall // not yet sorted, so sort 54347a984708SDavid Chisnall goto not_sorted; 54357a984708SDavid Chisnall __m = __j; 54367a984708SDavid Chisnall } 54377a984708SDavid Chisnall // [__first, __i) sorted 54387a984708SDavid Chisnall return; 54397a984708SDavid Chisnall } 54407a984708SDavid Chisnall else 54417a984708SDavid Chisnall { 54427a984708SDavid Chisnall // Check for [__i, __last) already sorted 54437a984708SDavid Chisnall __j = __m = __i; 54447a984708SDavid Chisnall while (++__j != __last) 54457a984708SDavid Chisnall { 54467a984708SDavid Chisnall if (__comp(*__j, *__m)) 54477a984708SDavid Chisnall // not yet sorted, so sort 54487a984708SDavid Chisnall goto not_sorted; 54497a984708SDavid Chisnall __m = __j; 54507a984708SDavid Chisnall } 54517a984708SDavid Chisnall // [__i, __last) sorted 54527a984708SDavid Chisnall return; 54537a984708SDavid Chisnall } 54547a984708SDavid Chisnall } 54557a984708SDavid Chisnallnot_sorted: 54567a984708SDavid Chisnall // __nth_element on range containing __nth 54577a984708SDavid Chisnall if (__nth < __i) 54587a984708SDavid Chisnall { 54597a984708SDavid Chisnall // __nth_element<_Compare>(__first, __nth, __i, __comp); 54607a984708SDavid Chisnall __last = __i; 54617a984708SDavid Chisnall } 54627a984708SDavid Chisnall else 54637a984708SDavid Chisnall { 54647a984708SDavid Chisnall // __nth_element<_Compare>(__i+1, __nth, __last, __comp); 54657a984708SDavid Chisnall __first = ++__i; 54667a984708SDavid Chisnall } 54677a984708SDavid Chisnall } 54687a984708SDavid Chisnall} 54697a984708SDavid Chisnall 54707a984708SDavid Chisnalltemplate <class _RandomAccessIterator, class _Compare> 54717a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 54727a984708SDavid Chisnallvoid 54737a984708SDavid Chisnallnth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp) 54747a984708SDavid Chisnall{ 54754f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 54767a984708SDavid Chisnall typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 54777a984708SDavid Chisnall __debug_less<_Compare> __c(__comp); 54787a984708SDavid Chisnall __nth_element<_Comp_ref>(__first, __nth, __last, __c); 54794f7ab58eSDimitry Andric#else // _LIBCPP_DEBUG 54807a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 54817a984708SDavid Chisnall __nth_element<_Comp_ref>(__first, __nth, __last, __comp); 54824f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 54837a984708SDavid Chisnall} 54847a984708SDavid Chisnall 54857a984708SDavid Chisnalltemplate <class _RandomAccessIterator> 54867a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 54877a984708SDavid Chisnallvoid 54887a984708SDavid Chisnallnth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last) 54897a984708SDavid Chisnall{ 54907a984708SDavid Chisnall _VSTD::nth_element(__first, __nth, __last, __less<typename iterator_traits<_RandomAccessIterator>::value_type>()); 54917a984708SDavid Chisnall} 54927a984708SDavid Chisnall 54937a984708SDavid Chisnall// includes 54947a984708SDavid Chisnall 54957a984708SDavid Chisnalltemplate <class _Compare, class _InputIterator1, class _InputIterator2> 54967a984708SDavid Chisnallbool 54977a984708SDavid Chisnall__includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2, 54987a984708SDavid Chisnall _Compare __comp) 54997a984708SDavid Chisnall{ 55007a984708SDavid Chisnall for (; __first2 != __last2; ++__first1) 55017a984708SDavid Chisnall { 55027a984708SDavid Chisnall if (__first1 == __last1 || __comp(*__first2, *__first1)) 55037a984708SDavid Chisnall return false; 55047a984708SDavid Chisnall if (!__comp(*__first1, *__first2)) 55057a984708SDavid Chisnall ++__first2; 55067a984708SDavid Chisnall } 55077a984708SDavid Chisnall return true; 55087a984708SDavid Chisnall} 55097a984708SDavid Chisnall 55107a984708SDavid Chisnalltemplate <class _InputIterator1, class _InputIterator2, class _Compare> 55117a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 55127a984708SDavid Chisnallbool 55137a984708SDavid Chisnallincludes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2, 55147a984708SDavid Chisnall _Compare __comp) 55157a984708SDavid Chisnall{ 55164f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 55177a984708SDavid Chisnall typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 55187a984708SDavid Chisnall __debug_less<_Compare> __c(__comp); 55197a984708SDavid Chisnall return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __c); 55204f7ab58eSDimitry Andric#else // _LIBCPP_DEBUG 55217a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 55227a984708SDavid Chisnall return __includes<_Comp_ref>(__first1, __last1, __first2, __last2, __comp); 55234f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 55247a984708SDavid Chisnall} 55257a984708SDavid Chisnall 55267a984708SDavid Chisnalltemplate <class _InputIterator1, class _InputIterator2> 55277a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 55287a984708SDavid Chisnallbool 55297a984708SDavid Chisnallincludes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2) 55307a984708SDavid Chisnall{ 55317a984708SDavid Chisnall return _VSTD::includes(__first1, __last1, __first2, __last2, 55327a984708SDavid Chisnall __less<typename iterator_traits<_InputIterator1>::value_type, 55337a984708SDavid Chisnall typename iterator_traits<_InputIterator2>::value_type>()); 55347a984708SDavid Chisnall} 55357a984708SDavid Chisnall 55367a984708SDavid Chisnall// set_union 55377a984708SDavid Chisnall 55387a984708SDavid Chisnalltemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator> 55397a984708SDavid Chisnall_OutputIterator 55407a984708SDavid Chisnall__set_union(_InputIterator1 __first1, _InputIterator1 __last1, 55417a984708SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) 55427a984708SDavid Chisnall{ 55437a984708SDavid Chisnall for (; __first1 != __last1; ++__result) 55447a984708SDavid Chisnall { 55457a984708SDavid Chisnall if (__first2 == __last2) 55467a984708SDavid Chisnall return _VSTD::copy(__first1, __last1, __result); 55477a984708SDavid Chisnall if (__comp(*__first2, *__first1)) 55487a984708SDavid Chisnall { 55497a984708SDavid Chisnall *__result = *__first2; 55507a984708SDavid Chisnall ++__first2; 55517a984708SDavid Chisnall } 55527a984708SDavid Chisnall else 55537a984708SDavid Chisnall { 55547a984708SDavid Chisnall *__result = *__first1; 55557a984708SDavid Chisnall if (!__comp(*__first1, *__first2)) 55567a984708SDavid Chisnall ++__first2; 55577a984708SDavid Chisnall ++__first1; 55587a984708SDavid Chisnall } 55597a984708SDavid Chisnall } 55607a984708SDavid Chisnall return _VSTD::copy(__first2, __last2, __result); 55617a984708SDavid Chisnall} 55627a984708SDavid Chisnall 55637a984708SDavid Chisnalltemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare> 55647a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 55657a984708SDavid Chisnall_OutputIterator 55667a984708SDavid Chisnallset_union(_InputIterator1 __first1, _InputIterator1 __last1, 55677a984708SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) 55687a984708SDavid Chisnall{ 55694f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 55707a984708SDavid Chisnall typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 55717a984708SDavid Chisnall __debug_less<_Compare> __c(__comp); 55727a984708SDavid Chisnall return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c); 55734f7ab58eSDimitry Andric#else // _LIBCPP_DEBUG 55747a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 55757a984708SDavid Chisnall return __set_union<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp); 55764f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 55777a984708SDavid Chisnall} 55787a984708SDavid Chisnall 55797a984708SDavid Chisnalltemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator> 55807a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 55817a984708SDavid Chisnall_OutputIterator 55827a984708SDavid Chisnallset_union(_InputIterator1 __first1, _InputIterator1 __last1, 55837a984708SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result) 55847a984708SDavid Chisnall{ 55857a984708SDavid Chisnall return _VSTD::set_union(__first1, __last1, __first2, __last2, __result, 55867a984708SDavid Chisnall __less<typename iterator_traits<_InputIterator1>::value_type, 55877a984708SDavid Chisnall typename iterator_traits<_InputIterator2>::value_type>()); 55887a984708SDavid Chisnall} 55897a984708SDavid Chisnall 55907a984708SDavid Chisnall// set_intersection 55917a984708SDavid Chisnall 55927a984708SDavid Chisnalltemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator> 55937a984708SDavid Chisnall_OutputIterator 55947a984708SDavid Chisnall__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1, 55957a984708SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) 55967a984708SDavid Chisnall{ 55977a984708SDavid Chisnall while (__first1 != __last1 && __first2 != __last2) 55987a984708SDavid Chisnall { 55997a984708SDavid Chisnall if (__comp(*__first1, *__first2)) 56007a984708SDavid Chisnall ++__first1; 56017a984708SDavid Chisnall else 56027a984708SDavid Chisnall { 56037a984708SDavid Chisnall if (!__comp(*__first2, *__first1)) 56047a984708SDavid Chisnall { 56057a984708SDavid Chisnall *__result = *__first1; 56067a984708SDavid Chisnall ++__result; 56077a984708SDavid Chisnall ++__first1; 56087a984708SDavid Chisnall } 56097a984708SDavid Chisnall ++__first2; 56107a984708SDavid Chisnall } 56117a984708SDavid Chisnall } 56127a984708SDavid Chisnall return __result; 56137a984708SDavid Chisnall} 56147a984708SDavid Chisnall 56157a984708SDavid Chisnalltemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare> 56167a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 56177a984708SDavid Chisnall_OutputIterator 56187a984708SDavid Chisnallset_intersection(_InputIterator1 __first1, _InputIterator1 __last1, 56197a984708SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) 56207a984708SDavid Chisnall{ 56214f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 56227a984708SDavid Chisnall typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 56237a984708SDavid Chisnall __debug_less<_Compare> __c(__comp); 56247a984708SDavid Chisnall return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c); 56254f7ab58eSDimitry Andric#else // _LIBCPP_DEBUG 56267a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 56277a984708SDavid Chisnall return __set_intersection<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp); 56284f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 56297a984708SDavid Chisnall} 56307a984708SDavid Chisnall 56317a984708SDavid Chisnalltemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator> 56327a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 56337a984708SDavid Chisnall_OutputIterator 56347a984708SDavid Chisnallset_intersection(_InputIterator1 __first1, _InputIterator1 __last1, 56357a984708SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result) 56367a984708SDavid Chisnall{ 56377a984708SDavid Chisnall return _VSTD::set_intersection(__first1, __last1, __first2, __last2, __result, 56387a984708SDavid Chisnall __less<typename iterator_traits<_InputIterator1>::value_type, 56397a984708SDavid Chisnall typename iterator_traits<_InputIterator2>::value_type>()); 56407a984708SDavid Chisnall} 56417a984708SDavid Chisnall 56427a984708SDavid Chisnall// set_difference 56437a984708SDavid Chisnall 56447a984708SDavid Chisnalltemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator> 56457a984708SDavid Chisnall_OutputIterator 56467a984708SDavid Chisnall__set_difference(_InputIterator1 __first1, _InputIterator1 __last1, 56477a984708SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) 56487a984708SDavid Chisnall{ 56497a984708SDavid Chisnall while (__first1 != __last1) 56507a984708SDavid Chisnall { 56517a984708SDavid Chisnall if (__first2 == __last2) 56527a984708SDavid Chisnall return _VSTD::copy(__first1, __last1, __result); 56537a984708SDavid Chisnall if (__comp(*__first1, *__first2)) 56547a984708SDavid Chisnall { 56557a984708SDavid Chisnall *__result = *__first1; 56567a984708SDavid Chisnall ++__result; 56577a984708SDavid Chisnall ++__first1; 56587a984708SDavid Chisnall } 56597a984708SDavid Chisnall else 56607a984708SDavid Chisnall { 56617a984708SDavid Chisnall if (!__comp(*__first2, *__first1)) 56627a984708SDavid Chisnall ++__first1; 56637a984708SDavid Chisnall ++__first2; 56647a984708SDavid Chisnall } 56657a984708SDavid Chisnall } 56667a984708SDavid Chisnall return __result; 56677a984708SDavid Chisnall} 56687a984708SDavid Chisnall 56697a984708SDavid Chisnalltemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare> 56707a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 56717a984708SDavid Chisnall_OutputIterator 56727a984708SDavid Chisnallset_difference(_InputIterator1 __first1, _InputIterator1 __last1, 56737a984708SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) 56747a984708SDavid Chisnall{ 56754f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 56767a984708SDavid Chisnall typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 56777a984708SDavid Chisnall __debug_less<_Compare> __c(__comp); 56787a984708SDavid Chisnall return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c); 56794f7ab58eSDimitry Andric#else // _LIBCPP_DEBUG 56807a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 56817a984708SDavid Chisnall return __set_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp); 56824f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 56837a984708SDavid Chisnall} 56847a984708SDavid Chisnall 56857a984708SDavid Chisnalltemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator> 56867a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 56877a984708SDavid Chisnall_OutputIterator 56887a984708SDavid Chisnallset_difference(_InputIterator1 __first1, _InputIterator1 __last1, 56897a984708SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result) 56907a984708SDavid Chisnall{ 56917a984708SDavid Chisnall return _VSTD::set_difference(__first1, __last1, __first2, __last2, __result, 56927a984708SDavid Chisnall __less<typename iterator_traits<_InputIterator1>::value_type, 56937a984708SDavid Chisnall typename iterator_traits<_InputIterator2>::value_type>()); 56947a984708SDavid Chisnall} 56957a984708SDavid Chisnall 56967a984708SDavid Chisnall// set_symmetric_difference 56977a984708SDavid Chisnall 56987a984708SDavid Chisnalltemplate <class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator> 56997a984708SDavid Chisnall_OutputIterator 57007a984708SDavid Chisnall__set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1, 57017a984708SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) 57027a984708SDavid Chisnall{ 57037a984708SDavid Chisnall while (__first1 != __last1) 57047a984708SDavid Chisnall { 57057a984708SDavid Chisnall if (__first2 == __last2) 57067a984708SDavid Chisnall return _VSTD::copy(__first1, __last1, __result); 57077a984708SDavid Chisnall if (__comp(*__first1, *__first2)) 57087a984708SDavid Chisnall { 57097a984708SDavid Chisnall *__result = *__first1; 57107a984708SDavid Chisnall ++__result; 57117a984708SDavid Chisnall ++__first1; 57127a984708SDavid Chisnall } 57137a984708SDavid Chisnall else 57147a984708SDavid Chisnall { 57157a984708SDavid Chisnall if (__comp(*__first2, *__first1)) 57167a984708SDavid Chisnall { 57177a984708SDavid Chisnall *__result = *__first2; 57187a984708SDavid Chisnall ++__result; 57197a984708SDavid Chisnall } 57207a984708SDavid Chisnall else 57217a984708SDavid Chisnall ++__first1; 57227a984708SDavid Chisnall ++__first2; 57237a984708SDavid Chisnall } 57247a984708SDavid Chisnall } 57257a984708SDavid Chisnall return _VSTD::copy(__first2, __last2, __result); 57267a984708SDavid Chisnall} 57277a984708SDavid Chisnall 57287a984708SDavid Chisnalltemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare> 57297a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 57307a984708SDavid Chisnall_OutputIterator 57317a984708SDavid Chisnallset_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1, 57327a984708SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result, _Compare __comp) 57337a984708SDavid Chisnall{ 57344f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 57357a984708SDavid Chisnall typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 57367a984708SDavid Chisnall __debug_less<_Compare> __c(__comp); 57377a984708SDavid Chisnall return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __c); 57384f7ab58eSDimitry Andric#else // _LIBCPP_DEBUG 57397a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 57407a984708SDavid Chisnall return __set_symmetric_difference<_Comp_ref>(__first1, __last1, __first2, __last2, __result, __comp); 57414f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 57427a984708SDavid Chisnall} 57437a984708SDavid Chisnall 57447a984708SDavid Chisnalltemplate <class _InputIterator1, class _InputIterator2, class _OutputIterator> 57457a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 57467a984708SDavid Chisnall_OutputIterator 57477a984708SDavid Chisnallset_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1, 57487a984708SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2, _OutputIterator __result) 57497a984708SDavid Chisnall{ 57507a984708SDavid Chisnall return _VSTD::set_symmetric_difference(__first1, __last1, __first2, __last2, __result, 57517a984708SDavid Chisnall __less<typename iterator_traits<_InputIterator1>::value_type, 57527a984708SDavid Chisnall typename iterator_traits<_InputIterator2>::value_type>()); 57537a984708SDavid Chisnall} 57547a984708SDavid Chisnall 57557a984708SDavid Chisnall// lexicographical_compare 57567a984708SDavid Chisnall 57577a984708SDavid Chisnalltemplate <class _Compare, class _InputIterator1, class _InputIterator2> 57587a984708SDavid Chisnallbool 57597a984708SDavid Chisnall__lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1, 57607a984708SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp) 57617a984708SDavid Chisnall{ 5762d72607e9SDimitry Andric for (; __first2 != __last2; ++__first1, (void) ++__first2) 57637a984708SDavid Chisnall { 57647a984708SDavid Chisnall if (__first1 == __last1 || __comp(*__first1, *__first2)) 57657a984708SDavid Chisnall return true; 57667a984708SDavid Chisnall if (__comp(*__first2, *__first1)) 57677a984708SDavid Chisnall return false; 57687a984708SDavid Chisnall } 57697a984708SDavid Chisnall return false; 57707a984708SDavid Chisnall} 57717a984708SDavid Chisnall 57727a984708SDavid Chisnalltemplate <class _InputIterator1, class _InputIterator2, class _Compare> 57737a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 57747a984708SDavid Chisnallbool 57757a984708SDavid Chisnalllexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1, 57767a984708SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp) 57777a984708SDavid Chisnall{ 57784f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 57797a984708SDavid Chisnall typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 57807a984708SDavid Chisnall __debug_less<_Compare> __c(__comp); 57817a984708SDavid Chisnall return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __c); 57824f7ab58eSDimitry Andric#else // _LIBCPP_DEBUG 57837a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 57847a984708SDavid Chisnall return __lexicographical_compare<_Comp_ref>(__first1, __last1, __first2, __last2, __comp); 57854f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 57867a984708SDavid Chisnall} 57877a984708SDavid Chisnall 57887a984708SDavid Chisnalltemplate <class _InputIterator1, class _InputIterator2> 57897a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 57907a984708SDavid Chisnallbool 57917a984708SDavid Chisnalllexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1, 57927a984708SDavid Chisnall _InputIterator2 __first2, _InputIterator2 __last2) 57937a984708SDavid Chisnall{ 57947a984708SDavid Chisnall return _VSTD::lexicographical_compare(__first1, __last1, __first2, __last2, 57957a984708SDavid Chisnall __less<typename iterator_traits<_InputIterator1>::value_type, 57967a984708SDavid Chisnall typename iterator_traits<_InputIterator2>::value_type>()); 57977a984708SDavid Chisnall} 57987a984708SDavid Chisnall 57997a984708SDavid Chisnall// next_permutation 58007a984708SDavid Chisnall 58017a984708SDavid Chisnalltemplate <class _Compare, class _BidirectionalIterator> 58027a984708SDavid Chisnallbool 58037a984708SDavid Chisnall__next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp) 58047a984708SDavid Chisnall{ 58057a984708SDavid Chisnall _BidirectionalIterator __i = __last; 58067a984708SDavid Chisnall if (__first == __last || __first == --__i) 58077a984708SDavid Chisnall return false; 58087a984708SDavid Chisnall while (true) 58097a984708SDavid Chisnall { 58107a984708SDavid Chisnall _BidirectionalIterator __ip1 = __i; 58117a984708SDavid Chisnall if (__comp(*--__i, *__ip1)) 58127a984708SDavid Chisnall { 58137a984708SDavid Chisnall _BidirectionalIterator __j = __last; 58147a984708SDavid Chisnall while (!__comp(*__i, *--__j)) 58157a984708SDavid Chisnall ; 58167a984708SDavid Chisnall swap(*__i, *__j); 58177a984708SDavid Chisnall _VSTD::reverse(__ip1, __last); 58187a984708SDavid Chisnall return true; 58197a984708SDavid Chisnall } 58207a984708SDavid Chisnall if (__i == __first) 58217a984708SDavid Chisnall { 58227a984708SDavid Chisnall _VSTD::reverse(__first, __last); 58237a984708SDavid Chisnall return false; 58247a984708SDavid Chisnall } 58257a984708SDavid Chisnall } 58267a984708SDavid Chisnall} 58277a984708SDavid Chisnall 58287a984708SDavid Chisnalltemplate <class _BidirectionalIterator, class _Compare> 58297a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 58307a984708SDavid Chisnallbool 58317a984708SDavid Chisnallnext_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp) 58327a984708SDavid Chisnall{ 58334f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 58347a984708SDavid Chisnall typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 58357a984708SDavid Chisnall __debug_less<_Compare> __c(__comp); 58367a984708SDavid Chisnall return __next_permutation<_Comp_ref>(__first, __last, __c); 58374f7ab58eSDimitry Andric#else // _LIBCPP_DEBUG 58387a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 58397a984708SDavid Chisnall return __next_permutation<_Comp_ref>(__first, __last, __comp); 58404f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 58417a984708SDavid Chisnall} 58427a984708SDavid Chisnall 58437a984708SDavid Chisnalltemplate <class _BidirectionalIterator> 58447a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 58457a984708SDavid Chisnallbool 58467a984708SDavid Chisnallnext_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last) 58477a984708SDavid Chisnall{ 58487a984708SDavid Chisnall return _VSTD::next_permutation(__first, __last, 58497a984708SDavid Chisnall __less<typename iterator_traits<_BidirectionalIterator>::value_type>()); 58507a984708SDavid Chisnall} 58517a984708SDavid Chisnall 58527a984708SDavid Chisnall// prev_permutation 58537a984708SDavid Chisnall 58547a984708SDavid Chisnalltemplate <class _Compare, class _BidirectionalIterator> 58557a984708SDavid Chisnallbool 58567a984708SDavid Chisnall__prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp) 58577a984708SDavid Chisnall{ 58587a984708SDavid Chisnall _BidirectionalIterator __i = __last; 58597a984708SDavid Chisnall if (__first == __last || __first == --__i) 58607a984708SDavid Chisnall return false; 58617a984708SDavid Chisnall while (true) 58627a984708SDavid Chisnall { 58637a984708SDavid Chisnall _BidirectionalIterator __ip1 = __i; 58647a984708SDavid Chisnall if (__comp(*__ip1, *--__i)) 58657a984708SDavid Chisnall { 58667a984708SDavid Chisnall _BidirectionalIterator __j = __last; 58677a984708SDavid Chisnall while (!__comp(*--__j, *__i)) 58687a984708SDavid Chisnall ; 58697a984708SDavid Chisnall swap(*__i, *__j); 58707a984708SDavid Chisnall _VSTD::reverse(__ip1, __last); 58717a984708SDavid Chisnall return true; 58727a984708SDavid Chisnall } 58737a984708SDavid Chisnall if (__i == __first) 58747a984708SDavid Chisnall { 58757a984708SDavid Chisnall _VSTD::reverse(__first, __last); 58767a984708SDavid Chisnall return false; 58777a984708SDavid Chisnall } 58787a984708SDavid Chisnall } 58797a984708SDavid Chisnall} 58807a984708SDavid Chisnall 58817a984708SDavid Chisnalltemplate <class _BidirectionalIterator, class _Compare> 58827a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 58837a984708SDavid Chisnallbool 58847a984708SDavid Chisnallprev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp) 58857a984708SDavid Chisnall{ 58864f7ab58eSDimitry Andric#ifdef _LIBCPP_DEBUG 58877a984708SDavid Chisnall typedef typename add_lvalue_reference<__debug_less<_Compare> >::type _Comp_ref; 58887a984708SDavid Chisnall __debug_less<_Compare> __c(__comp); 58897a984708SDavid Chisnall return __prev_permutation<_Comp_ref>(__first, __last, __c); 58904f7ab58eSDimitry Andric#else // _LIBCPP_DEBUG 58917a984708SDavid Chisnall typedef typename add_lvalue_reference<_Compare>::type _Comp_ref; 58927a984708SDavid Chisnall return __prev_permutation<_Comp_ref>(__first, __last, __comp); 58934f7ab58eSDimitry Andric#endif // _LIBCPP_DEBUG 58947a984708SDavid Chisnall} 58957a984708SDavid Chisnall 58967a984708SDavid Chisnalltemplate <class _BidirectionalIterator> 58977a984708SDavid Chisnallinline _LIBCPP_INLINE_VISIBILITY 58987a984708SDavid Chisnallbool 58997a984708SDavid Chisnallprev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last) 59007a984708SDavid Chisnall{ 59017a984708SDavid Chisnall return _VSTD::prev_permutation(__first, __last, 59027a984708SDavid Chisnall __less<typename iterator_traits<_BidirectionalIterator>::value_type>()); 59037a984708SDavid Chisnall} 59047a984708SDavid Chisnall 59057a984708SDavid Chisnall_LIBCPP_END_NAMESPACE_STD 59067a984708SDavid Chisnall 59077a984708SDavid Chisnall#endif // _LIBCPP_ALGORITHM 5908