From 98e5d974006989c505d7b2ec7b9e4b20b0f01e26 Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Sat, 21 Aug 2010 20:10:01 +0000 Subject: [PATCH] US 122, N3106 git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@111742 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/algorithm | 213 ++++++++++++++---- .../alg.min.max/max_init_list.pass.cpp | 16 +- .../alg.min.max/max_init_list_comp.pass.cpp | 17 +- .../alg.min.max/min_init_list.pass.cpp | 16 +- .../alg.min.max/min_init_list_comp.pass.cpp | 17 +- .../alg.min.max/minmax_comp.pass.cpp | 8 +- .../alg.min.max/minmax_init_list.pass.cpp | 12 +- .../minmax_init_list_comp.pass.cpp | 13 +- 8 files changed, 244 insertions(+), 68 deletions(-) diff --git a/include/algorithm b/include/algorithm index e0bdefa1..4e9487b8 100644 --- a/include/algorithm +++ b/include/algorithm @@ -485,6 +485,14 @@ template RandomAccessIterator is_heap_until(RandomAccessIterator first, RandomAccessiterator last, Compare comp); +template + ForwardIterator + min_element(ForwardIterator first, ForwardIterator last); + +template + ForwardIterator + min_element(ForwardIterator first, ForwardIterator last, Compare comp); + template const T& min(const T& a, const T& b); @@ -493,6 +501,22 @@ template const T& min(const T& a, const T& b, Compare comp); +template + T + min(initializer_list t); + +template + T + min(initializer_list t, Compare comp); + +template + ForwardIterator + max_element(ForwardIterator first, ForwardIterator last); + +template + ForwardIterator + max_element(ForwardIterator first, ForwardIterator last, Compare comp); + template const T& max(const T& a, const T& b); @@ -501,21 +525,37 @@ template const T& max(const T& a, const T& b, Compare comp); -template - ForwardIterator - min_element(ForwardIterator first, ForwardIterator last); +template + T + max(initializer_list t); -template - ForwardIterator - min_element(ForwardIterator first, ForwardIterator last, Compare comp); +template + T + max(initializer_list t, Compare comp); -template - ForwardIterator - max_element(ForwardIterator first, ForwardIterator last); +template + pair + minmax_element(ForwardIterator first, ForwardIterator last); -template - ForwardIterator - max_element(ForwardIterator first, ForwardIterator last, Compare comp); +template + pair + minmax_element(ForwardIterator first, ForwardIterator last, Compare comp); + +template + pair + minmax(const T& a, const T& b); + +template + pair + minmax(const T& a, const T& b, Compare comp); + +template + pair + minmax(initializer_list t); + +template + pair + minmax(initializer_list t, Compare comp); template bool @@ -2147,42 +2187,6 @@ rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterato return _STD::copy(__first, __middle, _STD::copy(__middle, __last, __result)); } -// min - -template -inline _LIBCPP_INLINE_VISIBILITY -const _Tp& -min(const _Tp& __a, const _Tp& __b, _Compare __comp) -{ - return __comp(__b, __a) ? __b : __a; -} - -template -inline _LIBCPP_INLINE_VISIBILITY -const _Tp& -min(const _Tp& __a, const _Tp& __b) -{ - return _STD::min(__a, __b, __less<_Tp>()); -} - -// max - -template -inline _LIBCPP_INLINE_VISIBILITY -const _Tp& -max(const _Tp& __a, const _Tp& __b, _Compare __comp) -{ - return __comp(__a, __b) ? __b : __a; -} - -template -inline _LIBCPP_INLINE_VISIBILITY -const _Tp& -max(const _Tp& __a, const _Tp& __b) -{ - return _STD::max(__a, __b, __less<_Tp>()); -} - // min_element template @@ -2205,7 +2209,42 @@ inline _LIBCPP_INLINE_VISIBILITY _ForwardIterator min_element(_ForwardIterator __first, _ForwardIterator __last) { - return _STD::min_element(__first, __last, __less::value_type>()); + return _STD::min_element(__first, __last, + __less::value_type>()); +} + +// min + +template +inline _LIBCPP_INLINE_VISIBILITY +const _Tp& +min(const _Tp& __a, const _Tp& __b, _Compare __comp) +{ + return __comp(__b, __a) ? __b : __a; +} + +template +inline _LIBCPP_INLINE_VISIBILITY +const _Tp& +min(const _Tp& __a, const _Tp& __b) +{ + return _STD::min(__a, __b, __less<_Tp>()); +} + +template +inline _LIBCPP_INLINE_VISIBILITY +_Tp +min(initializer_list<_Tp> __t, _Compare __comp) +{ + return *_STD::min_element(__t.begin(), __t.end(), __comp); +} + +template +inline _LIBCPP_INLINE_VISIBILITY +_Tp +min(initializer_list<_Tp> __t) +{ + return *_STD::min_element(__t.begin(), __t.end()); } // max_element @@ -2230,7 +2269,42 @@ inline _LIBCPP_INLINE_VISIBILITY _ForwardIterator max_element(_ForwardIterator __first, _ForwardIterator __last) { - return _STD::max_element(__first, __last, __less::value_type>()); + return _STD::max_element(__first, __last, + __less::value_type>()); +} + +// max + +template +inline _LIBCPP_INLINE_VISIBILITY +const _Tp& +max(const _Tp& __a, const _Tp& __b, _Compare __comp) +{ + return __comp(__a, __b) ? __b : __a; +} + +template +inline _LIBCPP_INLINE_VISIBILITY +const _Tp& +max(const _Tp& __a, const _Tp& __b) +{ + return _STD::max(__a, __b, __less<_Tp>()); +} + +template +inline _LIBCPP_INLINE_VISIBILITY +_Tp +max(initializer_list<_Tp> __t, _Compare __comp) +{ + return *_STD::max_element(__t.begin(), __t.end(), __comp); +} + +template +inline _LIBCPP_INLINE_VISIBILITY +_Tp +max(initializer_list<_Tp> __t) +{ + return *_STD::max_element(__t.begin(), __t.end()); } // minmax_element @@ -2293,6 +2367,45 @@ minmax_element(_ForwardIterator __first, _ForwardIterator __last) return _STD::minmax_element(__first, __last, __less::value_type>()); } +// minmax + +template +inline _LIBCPP_INLINE_VISIBILITY +pair +minmax(const _Tp& __a, const _Tp& __b, _Compare __comp) +{ + return __comp(__b, __a) ? pair(__b, __a) : + pair(__a, __b); +} + +template +inline _LIBCPP_INLINE_VISIBILITY +pair +minmax(const _Tp& __a, const _Tp& __b) +{ + return _STD::minmax(__a, __b, __less<_Tp>()); +} + +template +inline _LIBCPP_INLINE_VISIBILITY +pair<_Tp, _Tp> +minmax(initializer_list<_Tp> __t) +{ + pair __p = + _STD::minmax_element(__t.begin(), __t.end()); + return pair<_Tp, _Tp>(*__p.first, *__p.second); +} + +template +inline _LIBCPP_INLINE_VISIBILITY +pair<_Tp, _Tp> +minmax(initializer_list<_Tp> __t, _Compare __comp) +{ + pair __p = + _STD::minmax_element(__t.begin(), __t.end(), __comp); + return pair<_Tp, _Tp>(*__p.first, *__p.second); +} + // random_shuffle // __independent_bits_engine diff --git a/test/algorithms/alg.sorting/alg.min.max/max_init_list.pass.cpp b/test/algorithms/alg.sorting/alg.min.max/max_init_list.pass.cpp index a5b61d63..124ca61b 100644 --- a/test/algorithms/alg.sorting/alg.min.max/max_init_list.pass.cpp +++ b/test/algorithms/alg.sorting/alg.min.max/max_init_list.pass.cpp @@ -16,8 +16,20 @@ #include #include -#error max(initializer_list t) is not implemented - int main() { +#ifdef _LIBCPP_MOVE + int i = std::max({2, 3, 1}); + assert(i == 3); + i = std::max({2, 1, 3}); + assert(i == 3); + i = std::max({3, 1, 2}); + assert(i == 3); + i = std::max({3, 2, 1}); + assert(i == 3); + i = std::max({1, 2, 3}); + assert(i == 3); + i = std::max({1, 3, 2}); + assert(i == 3); +#endif } diff --git a/test/algorithms/alg.sorting/alg.min.max/max_init_list_comp.pass.cpp b/test/algorithms/alg.sorting/alg.min.max/max_init_list_comp.pass.cpp index 133e1045..8c78fbdb 100644 --- a/test/algorithms/alg.sorting/alg.min.max/max_init_list_comp.pass.cpp +++ b/test/algorithms/alg.sorting/alg.min.max/max_init_list_comp.pass.cpp @@ -14,10 +14,23 @@ // max(initializer_list t, Compare comp); #include +#include #include -#error max(initializer_list t, Compare comp) is not implemented - int main() { +#ifdef _LIBCPP_MOVE + int i = std::max({2, 3, 1}, std::greater()); + assert(i == 1); + i = std::max({2, 1, 3}, std::greater()); + assert(i == 1); + i = std::max({3, 1, 2}, std::greater()); + assert(i == 1); + i = std::max({3, 2, 1}, std::greater()); + assert(i == 1); + i = std::max({1, 2, 3}, std::greater()); + assert(i == 1); + i = std::max({1, 3, 2}, std::greater()); + assert(i == 1); +#endif } diff --git a/test/algorithms/alg.sorting/alg.min.max/min_init_list.pass.cpp b/test/algorithms/alg.sorting/alg.min.max/min_init_list.pass.cpp index 977f8b61..bb19d4fd 100644 --- a/test/algorithms/alg.sorting/alg.min.max/min_init_list.pass.cpp +++ b/test/algorithms/alg.sorting/alg.min.max/min_init_list.pass.cpp @@ -16,8 +16,20 @@ #include #include -#error min(initializer_list t) is not implemented - int main() { +#ifdef _LIBCPP_MOVE + int i = std::min({2, 3, 1}); + assert(i == 1); + i = std::min({2, 1, 3}); + assert(i == 1); + i = std::min({3, 1, 2}); + assert(i == 1); + i = std::min({3, 2, 1}); + assert(i == 1); + i = std::min({1, 2, 3}); + assert(i == 1); + i = std::min({1, 3, 2}); + assert(i == 1); +#endif } diff --git a/test/algorithms/alg.sorting/alg.min.max/min_init_list_comp.pass.cpp b/test/algorithms/alg.sorting/alg.min.max/min_init_list_comp.pass.cpp index 04bf2f99..161d6820 100644 --- a/test/algorithms/alg.sorting/alg.min.max/min_init_list_comp.pass.cpp +++ b/test/algorithms/alg.sorting/alg.min.max/min_init_list_comp.pass.cpp @@ -14,10 +14,23 @@ // min(initializer_list t, Compare comp); #include +#include #include -#error min(initializer_list t, Compare comp) is not implemented - int main() { +#ifdef _LIBCPP_MOVE + int i = std::min({2, 3, 1}, std::greater()); + assert(i == 3); + i = std::min({2, 1, 3}, std::greater()); + assert(i == 3); + i = std::min({3, 1, 2}, std::greater()); + assert(i == 3); + i = std::min({3, 2, 1}, std::greater()); + assert(i == 3); + i = std::min({1, 2, 3}, std::greater()); + assert(i == 3); + i = std::min({1, 3, 2}, std::greater()); + assert(i == 3); +#endif } diff --git a/test/algorithms/alg.sorting/alg.min.max/minmax_comp.pass.cpp b/test/algorithms/alg.sorting/alg.min.max/minmax_comp.pass.cpp index d35c3907..83fa53c8 100644 --- a/test/algorithms/alg.sorting/alg.min.max/minmax_comp.pass.cpp +++ b/test/algorithms/alg.sorting/alg.min.max/minmax_comp.pass.cpp @@ -38,13 +38,13 @@ int main() { int x = 0; int y = 1; - test(x, y, std::greater(), x, y); - test(y, x, std::greater(), x, y); + test(x, y, std::greater(), y, x); + test(y, x, std::greater(), y, x); } { int x = 1; int y = 0; - test(x, y, std::greater(), y, x); - test(y, x, std::greater(), y, x); + test(x, y, std::greater(), x, y); + test(y, x, std::greater(), x, y); } } diff --git a/test/algorithms/alg.sorting/alg.min.max/minmax_init_list.pass.cpp b/test/algorithms/alg.sorting/alg.min.max/minmax_init_list.pass.cpp index d10ee550..0dbb2117 100644 --- a/test/algorithms/alg.sorting/alg.min.max/minmax_init_list.pass.cpp +++ b/test/algorithms/alg.sorting/alg.min.max/minmax_init_list.pass.cpp @@ -10,14 +10,20 @@ // // template -// pair +// pair // minmax(initializer_list t); #include #include -#error minmax(initializer_list t) is not implemented - int main() { +#ifdef _LIBCPP_MOVE + assert((std::minmax({1, 2, 3}) == std::pair(1, 3))); + assert((std::minmax({1, 3, 2}) == std::pair(1, 3))); + assert((std::minmax({2, 1, 3}) == std::pair(1, 3))); + assert((std::minmax({2, 3, 1}) == std::pair(1, 3))); + assert((std::minmax({3, 1, 2}) == std::pair(1, 3))); + assert((std::minmax({3, 2, 1}) == std::pair(1, 3))); +#endif } diff --git a/test/algorithms/alg.sorting/alg.min.max/minmax_init_list_comp.pass.cpp b/test/algorithms/alg.sorting/alg.min.max/minmax_init_list_comp.pass.cpp index 5b47f1c6..4f3d1441 100644 --- a/test/algorithms/alg.sorting/alg.min.max/minmax_init_list_comp.pass.cpp +++ b/test/algorithms/alg.sorting/alg.min.max/minmax_init_list_comp.pass.cpp @@ -10,14 +10,21 @@ // // template -// pair +// pair // minmax(initializer_list t, Compare comp); #include +#include #include -#error minmax(initializer_list t, Compare comp) is not implemented - int main() { +#ifdef _LIBCPP_MOVE + assert((std::minmax({1, 2, 3}, std::greater()) == std::pair(3, 1))); + assert((std::minmax({1, 3, 2}, std::greater()) == std::pair(3, 1))); + assert((std::minmax({2, 1, 3}, std::greater()) == std::pair(3, 1))); + assert((std::minmax({2, 3, 1}, std::greater()) == std::pair(3, 1))); + assert((std::minmax({3, 1, 2}, std::greater()) == std::pair(3, 1))); + assert((std::minmax({3, 2, 1}, std::greater()) == std::pair(3, 1))); +#endif }