Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <algorithm> |
| 11 | |
Howard Hinnant | eb564e7 | 2010-08-22 00:08:10 +0000 | [diff] [blame] | 12 | // template<class T, class Compare> |
Howard Hinnant | 98e5d97 | 2010-08-21 20:10:01 +0000 | [diff] [blame] | 13 | // pair<T, T> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 14 | // minmax(initializer_list<T> t, Compare comp); |
Marshall Clow | 3024f86 | 2015-02-11 15:41:34 +0000 | [diff] [blame] | 15 | // |
| 16 | // Complexity: At most (3/2) * t.size() applications of the corresponding predicate. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 17 | |
| 18 | #include <algorithm> |
Howard Hinnant | 98e5d97 | 2010-08-21 20:10:01 +0000 | [diff] [blame] | 19 | #include <functional> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 20 | #include <cassert> |
| 21 | |
Marshall Clow | 3024f86 | 2015-02-11 15:41:34 +0000 | [diff] [blame] | 22 | #include "counting_predicates.hpp" |
| 23 | |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 24 | bool all_equal(int a, int b) { return false; } // everything is equal |
Marshall Clow | 3024f86 | 2015-02-11 15:41:34 +0000 | [diff] [blame] | 25 | |
Marshall Clow | e7d3925 | 2015-02-11 15:48:21 +0000 | [diff] [blame] | 26 | #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS |
Marshall Clow | 3024f86 | 2015-02-11 15:41:34 +0000 | [diff] [blame] | 27 | void test_all_equal(std::initializer_list<int> il) |
| 28 | { |
| 29 | binary_counting_predicate<bool(*)(int, int), int, int> pred (all_equal); |
| 30 | std::pair<int, int> p = std::minmax(il, std::ref(pred)); |
| 31 | const int *ptr = il.end(); |
| 32 | assert(p.first == *il.begin()); |
| 33 | assert(p.second == *--ptr); |
| 34 | assert(pred.count() <= ((3 * il.size()) / 2)); |
| 35 | } |
Marshall Clow | e7d3925 | 2015-02-11 15:48:21 +0000 | [diff] [blame] | 36 | #endif |
Marshall Clow | 3024f86 | 2015-02-11 15:41:34 +0000 | [diff] [blame] | 37 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 38 | int main() |
| 39 | { |
Howard Hinnant | e3e3291 | 2011-08-12 21:56:02 +0000 | [diff] [blame] | 40 | #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS |
Howard Hinnant | 98e5d97 | 2010-08-21 20:10:01 +0000 | [diff] [blame] | 41 | assert((std::minmax({1, 2, 3}, std::greater<int>()) == std::pair<int, int>(3, 1))); |
| 42 | assert((std::minmax({1, 3, 2}, std::greater<int>()) == std::pair<int, int>(3, 1))); |
| 43 | assert((std::minmax({2, 1, 3}, std::greater<int>()) == std::pair<int, int>(3, 1))); |
| 44 | assert((std::minmax({2, 3, 1}, std::greater<int>()) == std::pair<int, int>(3, 1))); |
| 45 | assert((std::minmax({3, 1, 2}, std::greater<int>()) == std::pair<int, int>(3, 1))); |
| 46 | assert((std::minmax({3, 2, 1}, std::greater<int>()) == std::pair<int, int>(3, 1))); |
Marshall Clow | 3024f86 | 2015-02-11 15:41:34 +0000 | [diff] [blame] | 47 | assert((std::minmax({1, 2, 3}, all_equal ) == std::pair<int, int>(1, 3))); |
| 48 | |
| 49 | binary_counting_predicate<std::greater<int>, int, int> pred ((std::greater<int>())); |
| 50 | assert((std::minmax({1, 2, 2, 3, 3, 3, 5, 5, 5, 5, 5, 3}, std::ref(pred)) == std::pair<int, int>(5, 1))); |
| 51 | assert(pred.count() <= 18); // size == 12 |
| 52 | |
| 53 | test_all_equal({0}); |
| 54 | test_all_equal({0,1}); |
| 55 | test_all_equal({0,1,2}); |
| 56 | test_all_equal({0,1,2,3}); |
| 57 | test_all_equal({0,1,2,3,4}); |
| 58 | test_all_equal({0,1,2,3,4,5}); |
| 59 | test_all_equal({0,1,2,3,4,5,6}); |
| 60 | test_all_equal({0,1,2,3,4,5,6,7}); |
| 61 | test_all_equal({0,1,2,3,4,5,6,7,8}); |
| 62 | test_all_equal({0,1,2,3,4,5,6,7,8,9}); |
| 63 | test_all_equal({0,1,2,3,4,5,6,7,8,9,10}); |
| 64 | test_all_equal({0,1,2,3,4,5,6,7,8,9,10,11}); |
| 65 | |
Marshall Clow | 9d9463a | 2014-02-19 16:51:35 +0000 | [diff] [blame] | 66 | #if _LIBCPP_STD_VER > 11 |
| 67 | { |
| 68 | static_assert((std::minmax({1, 2, 3}, std::greater<int>()) == std::pair<int, int>(3, 1)), ""); |
| 69 | static_assert((std::minmax({1, 3, 2}, std::greater<int>()) == std::pair<int, int>(3, 1)), ""); |
| 70 | static_assert((std::minmax({2, 1, 3}, std::greater<int>()) == std::pair<int, int>(3, 1)), ""); |
| 71 | static_assert((std::minmax({2, 3, 1}, std::greater<int>()) == std::pair<int, int>(3, 1)), ""); |
| 72 | static_assert((std::minmax({3, 1, 2}, std::greater<int>()) == std::pair<int, int>(3, 1)), ""); |
| 73 | static_assert((std::minmax({3, 2, 1}, std::greater<int>()) == std::pair<int, int>(3, 1)), ""); |
| 74 | } |
| 75 | #endif |
Howard Hinnant | e3e3291 | 2011-08-12 21:56:02 +0000 | [diff] [blame] | 76 | #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 77 | } |