[libcxx] r262871 - Implement P0025R0: 'An algorithm to clamp a value between a pair of boundary values' for C++17
Marshall Clow via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 7 14:43:49 PST 2016
Author: marshall
Date: Mon Mar 7 16:43:49 2016
New Revision: 262871
URL: http://llvm.org/viewvc/llvm-project?rev=262871&view=rev
Log:
Implement P0025R0: 'An algorithm to clamp a value between a pair of boundary values' for C++17
Added:
libcxx/trunk/test/std/algorithms/alg.sorting/alg.clamp/
libcxx/trunk/test/std/algorithms/alg.sorting/alg.clamp/clamp.comp.pass.cpp
libcxx/trunk/test/std/algorithms/alg.sorting/alg.clamp/clamp.pass.cpp
Modified:
libcxx/trunk/include/algorithm
libcxx/trunk/www/cxx1z_status.html
Modified: libcxx/trunk/include/algorithm
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/algorithm?rev=262871&r1=262870&r2=262871&view=diff
==============================================================================
--- libcxx/trunk/include/algorithm (original)
+++ libcxx/trunk/include/algorithm Mon Mar 7 16:43:49 2016
@@ -543,6 +543,12 @@ template<class T, class Compare>
T
min(initializer_list<T> t, Compare comp); // constexpr in C++14
+template<class T>
+ constexpr const T& clamp( const T& v, const T& lo, const T& hi ); // C++17
+
+template<class T, class Compare>
+ constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp ); // C++17
+
template <class ForwardIterator>
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<class _Tp, class _Compare>
+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<class _Tp>
+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 <class _ForwardIterator, class _Compare>
Added: libcxx/trunk/test/std/algorithms/alg.sorting/alg.clamp/clamp.comp.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/algorithms/alg.sorting/alg.clamp/clamp.comp.pass.cpp?rev=262871&view=auto
==============================================================================
--- libcxx/trunk/test/std/algorithms/alg.sorting/alg.clamp/clamp.comp.pass.cpp (added)
+++ libcxx/trunk/test/std/algorithms/alg.sorting/alg.clamp/clamp.comp.pass.cpp Mon Mar 7 16:43:49 2016
@@ -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.
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+// XFAIL: c++03, c++11, c++14
+
+// template<class T, class Compare>
+// const T&
+// clamp(const T& v, const T& lo, const T& hi, Compare comp);
+
+#include <algorithm>
+#include <functional>
+#include <cassert>
+
+template <class T, class C>
+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<int>(), x);
+ test(y, x, z, std::greater<int>(), y);
+ }
+ {
+ int x = 0;
+ int y = 1;
+ int z = -1;
+ test(x, y, z, std::greater<int>(), x);
+ test(y, x, z, std::greater<int>(), x);
+ }
+ {
+ int x = 1;
+ int y = 0;
+ int z = 0;
+ test(x, y, z, std::greater<int>(), y);
+ test(y, x, z, std::greater<int>(), 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<T>()) == y, "" );
+ static_assert(std::clamp(y, x, z, std::greater<T>()) == y, "" );
+ }
+#endif
+}
Added: libcxx/trunk/test/std/algorithms/alg.sorting/alg.clamp/clamp.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/algorithms/alg.sorting/alg.clamp/clamp.pass.cpp?rev=262871&view=auto
==============================================================================
--- libcxx/trunk/test/std/algorithms/alg.sorting/alg.clamp/clamp.pass.cpp (added)
+++ libcxx/trunk/test/std/algorithms/alg.sorting/alg.clamp/clamp.pass.cpp Mon Mar 7 16:43:49 2016
@@ -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.
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+// XFAIL: c++03, c++11, c++14
+
+// template<class T>
+// const T&
+// clamp(const T& v, const T& lo, const T& hi);
+
+#include <algorithm>
+#include <cassert>
+
+template <class T>
+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
+}
Modified: libcxx/trunk/www/cxx1z_status.html
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/www/cxx1z_status.html?rev=262871&r1=262870&r2=262871&view=diff
==============================================================================
--- libcxx/trunk/www/cxx1z_status.html (original)
+++ libcxx/trunk/www/cxx1z_status.html Mon Mar 7 16:43:49 2016
@@ -87,7 +87,7 @@
<tr><td><a href="http://wg21.link/P0152R1">P0152R1</a></td><td>LWG</td><td>constexpr atomic::is_always_lock_free</td><td>Jacksonville</td><td></td><td></td></tr>
<tr><td><a href="http://wg21.link/P0185R1">P0185R1</a></td><td>LWG</td><td>Adding [nothrow-]swappable traits</td><td>Jacksonville</td><td></td><td></td></tr>
<tr><td><a href="http://wg21.link/P0253R1">P0253R1</a></td><td>LWG</td><td>Fixing a design mistake in the searchers interface</td><td>Jacksonville</td><td></td><td></td></tr>
- <tr><td><a href="http://wg21.link/P0025R0">P0025R0</a></td><td>LWG</td><td>An algorithm to "clamp" a value between a pair of boundary values</td><td>Jacksonville</td><td></td><td></td></tr>
+ <tr><td><a href="http://wg21.link/P0025R0">P0025R0</a></td><td>LWG</td><td>An algorithm to "clamp" a value between a pair of boundary values</td><td>Jacksonville</td><td>Complete</td><td>3.9</td></tr>
<tr><td><a href="http://wg21.link/P0154R1">P0154R1</a></td><td>LWG</td><td>constexpr std::hardware_{constructive,destructive}_interference_size</td><td>Jacksonville</td><td></td><td></td></tr>
<tr><td><a href="http://wg21.link/P0030R1">P0030R1</a></td><td>LWG</td><td>Proposal to Introduce a 3-Argument Overload to std::hypot</td><td>Jacksonville</td><td></td><td></td></tr>
<tr><td><a href="http://wg21.link/P0031R0">P0031R0</a></td><td>LWG</td><td>A Proposal to Add Constexpr Modifiers to reverse_iterator, move_iterator, array and Range Access</td><td>Jacksonville</td><td></td><td></td></tr>
@@ -238,7 +238,7 @@
<!-- <tr><td></td><td></td><td></td><td></td></tr> -->
</table>
- <p>Last Updated: 6-Mar-2016</p>
+ <p>Last Updated: 7-Mar-2016</p>
</div>
</body>
</html>
More information about the cfe-commits
mailing list