diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm index 71e5df37dfeb..7dba11e3002b 100644 --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -543,6 +543,12 @@ template T min(initializer_list t, Compare comp); // constexpr in C++14 +template + constexpr const T& clamp( const T& v, const T& lo, const T& hi ); // C++17 + +template + constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp ); // C++17 + template ForwardIterator max_element(ForwardIterator first, ForwardIterator last); // constexpr in C++14 @@ -2657,6 +2663,27 @@ max(initializer_list<_Tp> __t) #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS +#if _LIBCPP_STD_VER > 14 +// clamp +template +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR +const _Tp& +clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi, _Compare __comp) +{ + _LIBCPP_ASSERT(!__comp(__hi, __lo), "Bad bounds passed to std::clamp"); + return __comp(__v, __lo) ? __lo : __comp(__hi, __v) ? __hi : __v; + +} + +template +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR +const _Tp& +clamp(const _Tp& __v, const _Tp& __lo, const _Tp& __hi) +{ + return _VSTD::clamp(__v, __lo, __hi, __less<_Tp>()); +} +#endif + // minmax_element template diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.clamp/clamp.comp.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.clamp/clamp.comp.pass.cpp new file mode 100644 index 000000000000..e0d8127a7b70 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.sorting/alg.clamp/clamp.comp.pass.cpp @@ -0,0 +1,61 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// +// XFAIL: c++03, c++11, c++14 + +// template +// const T& +// clamp(const T& v, const T& lo, const T& hi, Compare comp); + +#include +#include +#include + +template +void +test(const T& v, const T& lo, const T& hi, C c, const T& x) +{ + assert(&std::clamp(v, lo, hi, c) == &x); +} + +int main() +{ + { + int x = 0; + int y = 0; + int z = 0; + test(x, y, z, std::greater(), x); + test(y, x, z, std::greater(), y); + } + { + int x = 0; + int y = 1; + int z = -1; + test(x, y, z, std::greater(), x); + test(y, x, z, std::greater(), x); + } + { + int x = 1; + int y = 0; + int z = 0; + test(x, y, z, std::greater(), y); + test(y, x, z, std::greater(), y); + } +#if _LIBCPP_STD_VER > 11 + { + typedef int T; + constexpr T x = 1; + constexpr T y = 0; + constexpr T z = 0; + static_assert(std::clamp(x, y, z, std::greater()) == y, "" ); + static_assert(std::clamp(y, x, z, std::greater()) == y, "" ); + } +#endif +} diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.clamp/clamp.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.clamp/clamp.pass.cpp new file mode 100644 index 000000000000..838aaed9a3f6 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.sorting/alg.clamp/clamp.pass.cpp @@ -0,0 +1,60 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// +// XFAIL: c++03, c++11, c++14 + +// template +// const T& +// clamp(const T& v, const T& lo, const T& hi); + +#include +#include + +template +void +test(const T& a, const T& lo, const T& hi, const T& x) +{ + assert(&std::clamp(a, lo, hi) == &x); +} + +int main() +{ + { + int x = 0; + int y = 0; + int z = 0; + test(x, y, z, x); + test(y, x, z, y); + } + { + int x = 0; + int y = 1; + int z = 2; + test(x, y, z, y); + test(y, x, z, y); + } + { + int x = 1; + int y = 0; + int z = 1; + test(x, y, z, x); + test(y, x, z, x); + } +#if _LIBCPP_STD_VER > 11 + { + typedef int T; + constexpr T x = 1; + constexpr T y = 0; + constexpr T z = 1; + static_assert(std::clamp(x, y, z) == x, "" ); + static_assert(std::clamp(y, x, z) == x, "" ); + } +#endif +} diff --git a/libcxx/www/cxx1z_status.html b/libcxx/www/cxx1z_status.html index 59fb7a8a1bfe..2c69beca0b5b 100644 --- a/libcxx/www/cxx1z_status.html +++ b/libcxx/www/cxx1z_status.html @@ -87,7 +87,7 @@ P0152R1LWGconstexpr atomic::is_always_lock_freeJacksonville P0185R1LWGAdding [nothrow-]swappable traitsJacksonville P0253R1LWGFixing a design mistake in the searchers interfaceJacksonville - P0025R0LWGAn algorithm to "clamp" a value between a pair of boundary valuesJacksonville + P0025R0LWGAn algorithm to "clamp" a value between a pair of boundary valuesJacksonvilleComplete3.9 P0154R1LWGconstexpr std::hardware_{constructive,destructive}_interference_sizeJacksonville P0030R1LWGProposal to Introduce a 3-Argument Overload to std::hypotJacksonville P0031R0LWGA Proposal to Add Constexpr Modifiers to reverse_iterator, move_iterator, array and Range AccessJacksonville @@ -238,7 +238,7 @@ -

Last Updated: 6-Mar-2016

+

Last Updated: 7-Mar-2016