From 3c0283aab11e7412192754637465494610902300 Mon Sep 17 00:00:00 2001 From: emsr Date: Fri, 15 Jul 2016 17:16:32 +0000 Subject: [PATCH] 2016-07-15 Edward Smith-Rowland <3dw4rd@verizon.net> Implement C++17 P0025 clamp. * include/bits/algorithmfwd.h: Declare clamp overloads. * include/bits/stl_algo.h: Implement clamp. Feature __cpp_lib_clamp. * testsuite/25_algorithms/clamp/1.cc: New test. * testsuite/25_algorithms/clamp/2.cc: New test. * testsuite/25_algorithms/clamp/constexpr.cc: New test. * testsuite/25_algorithms/clamp/requirements/explicit_instantiation/ 1.cc: New test. * testsuite/25_algorithms/clamp/requirements/explicit_instantiation/ pod.cc: New test. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@238383 138bc75d-0d04-0410-961f-82ee72b054a4 --- libstdc++-v3/ChangeLog | 13 +++ libstdc++-v3/include/bits/algorithmfwd.h | 13 +++ libstdc++-v3/include/bits/stl_algo.h | 41 ++++++++- libstdc++-v3/testsuite/25_algorithms/clamp/1.cc | 48 ++++++++++ libstdc++-v3/testsuite/25_algorithms/clamp/2.cc | 102 +++++++++++++++++++++ .../testsuite/25_algorithms/clamp/constexpr.cc | 31 +++++++ .../clamp/requirements/explicit_instantiation/1.cc | 44 +++++++++ .../requirements/explicit_instantiation/pod.cc | 38 ++++++++ 8 files changed, 329 insertions(+), 1 deletion(-) create mode 100644 libstdc++-v3/testsuite/25_algorithms/clamp/1.cc create mode 100644 libstdc++-v3/testsuite/25_algorithms/clamp/2.cc create mode 100644 libstdc++-v3/testsuite/25_algorithms/clamp/constexpr.cc create mode 100644 libstdc++-v3/testsuite/25_algorithms/clamp/requirements/explicit_instantiation/1.cc create mode 100644 libstdc++-v3/testsuite/25_algorithms/clamp/requirements/explicit_instantiation/pod.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 2386300807b..25b8191e2a8 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,16 @@ +2016-07-15 Edward Smith-Rowland <3dw4rd@verizon.net> + + Implement C++17 P0025 clamp. + * include/bits/algorithmfwd.h: Declare clamp overloads. + * include/bits/stl_algo.h: Implement clamp. Feature __cpp_lib_clamp. + * testsuite/25_algorithms/clamp/1.cc: New test. + * testsuite/25_algorithms/clamp/2.cc: New test. + * testsuite/25_algorithms/clamp/constexpr.cc: New test. + * testsuite/25_algorithms/clamp/requirements/explicit_instantiation/ + 1.cc: New test. + * testsuite/25_algorithms/clamp/requirements/explicit_instantiation/ + pod.cc: New test. + 2016-07-14 Ville Voutilainen Fix the constraints for any's assignment operator template to properly diff --git a/libstdc++-v3/include/bits/algorithmfwd.h b/libstdc++-v3/include/bits/algorithmfwd.h index cd98fe05949..1defb1d9080 100644 --- a/libstdc++-v3/include/bits/algorithmfwd.h +++ b/libstdc++-v3/include/bits/algorithmfwd.h @@ -48,6 +48,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION all_of (C++0x) any_of (C++0x) binary_search + clamp (C++17) copy copy_backward copy_if (C++0x) @@ -208,6 +209,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION bool binary_search(_FIter, _FIter, const _Tp&, _Compare); +#if __cplusplus > 201402L + template + _GLIBCXX14_CONSTEXPR + const _Tp& + clamp(const _Tp&, const _Tp&, const _Tp&); + + template + _GLIBCXX14_CONSTEXPR + const _Tp& + clamp(const _Tp&, const _Tp&, const _Tp&, _Compare); +#endif + template _OIter copy(_IIter, _IIter, _OIter); diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h index c2ac0317f17..a0820d4132f 100644 --- a/libstdc++-v3/include/bits/stl_algo.h +++ b/libstdc++-v3/include/bits/stl_algo.h @@ -3698,7 +3698,46 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return std::__is_permutation(__first1, __last1, __first2, __last2, __gnu_cxx::__ops::__iter_comp_iter(__pred)); } -#endif + +#if __cplusplus > 201402L + +#define __cpp_lib_clamp 201603 + + /** + * @brief Returns the value clamped between lo and hi. + * @ingroup sorting_algorithms + * @param __val A value of arbitrary type. + * @param __lo A lower limit of arbitrary type. + * @param __hi An upper limit of arbitrary type. + * @return max(__val, __lo) if __val < __hi or min(__val, __hi) otherwise. + */ + template + constexpr const _Tp& + clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi) + { + __glibcxx_assert(!(__hi < __lo)); + return (__val < __lo) ? __lo : (__hi < __val) ? __hi : __val; + } + + /** + * @brief Returns the value clamped between lo and hi. + * @ingroup sorting_algorithms + * @param __val A value of arbitrary type. + * @param __lo A lower limit of arbitrary type. + * @param __hi An upper limit of arbitrary type. + * @param __comp A comparison functor. + * @return max(__val, __lo, __comp) if __comp(__val, __hi) + * or min(__val, __hi, __comp) otherwise. + */ + template + constexpr const _Tp& + clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi, _Compare __comp) + { + __glibcxx_assert(!__comp(__hi, __lo)); + return __comp(__val, __lo) ? __lo : __comp(__hi, __val) ? __hi : __val; + } +#endif // C++17 +#endif // C++14 #ifdef _GLIBCXX_USE_C99_STDINT_TR1 /** diff --git a/libstdc++-v3/testsuite/25_algorithms/clamp/1.cc b/libstdc++-v3/testsuite/25_algorithms/clamp/1.cc new file mode 100644 index 00000000000..cf2e13150ac --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/clamp/1.cc @@ -0,0 +1,48 @@ +// { dg-options "-std=gnu++17" } + +// Copyright (C) 2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include +#include + +void test01() +{ + bool test __attribute__((unused)) = true; + + const int x = std::clamp(1, 2, 4); + const int y = std::clamp(3, 2, 4); + const int z = std::clamp(5, 2, 4); + VERIFY( x == 2 ); + VERIFY( y == 3 ); + VERIFY( z == 4 ); + + const int xc = std::clamp(1, 2, 4, std::greater()); + const int yc = std::clamp(3, 2, 4, std::greater()); + const int zc = std::clamp(5, 2, 4, std::greater()); + VERIFY( xc == 4 ); + VERIFY( yc == 2 ); + VERIFY( zc == 2 ); +} + +int +main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/25_algorithms/clamp/2.cc b/libstdc++-v3/testsuite/25_algorithms/clamp/2.cc new file mode 100644 index 00000000000..50c1a518ff2 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/clamp/2.cc @@ -0,0 +1,102 @@ +// { dg-options "-std=gnu++17" } + +// Copyright (C) 2000-2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include +#include + +template + struct A { static const T a; }; + +template +const T A::a = T(3); + +void test02() +{ + bool test __attribute__((unused)) = true; + + VERIFY( 3 == std::clamp(A::a, 2, 4) ); + VERIFY( 2 == std::clamp(A::a, 1, 2) ); + VERIFY( 4 == std::clamp(A::a, 4, 6) ); + + VERIFY( 3u == std::clamp(A::a, 2u, 4u) ); + VERIFY( 2u == std::clamp(A::a, 1u, 2u) ); + VERIFY( 4u == std::clamp(A::a, 4u, 6u) ); + + VERIFY( 3l == std::clamp(A::a, 2l, 4l) ); + VERIFY( 2l == std::clamp(A::a, 1l, 2l) ); + VERIFY( 4l == std::clamp(A::a, 4l, 6l) ); + + VERIFY( 3ul == std::clamp(A::a, 2ul, 4ul) ); + VERIFY( 2ul == std::clamp(A::a, 1ul, 2ul) ); + VERIFY( 4ul == std::clamp(A::a, 4ul, 6ul) ); + +#ifdef _GLIBCXX_USE_LONG_LONG + VERIFY( 3ll == std::clamp(A::a, 2ll, 4ll) ); + VERIFY( 2ll == std::clamp(A::a, 1ll, 2ll) ); + VERIFY( 4ll == std::clamp(A::a, 4ll, 6ll) ); + + VERIFY( 3ull == std::clamp(A::a, 2ull, 4ull) ); + VERIFY( 2ull == std::clamp(A::a, 1ull, 2ull) ); + VERIFY( 4ull == std::clamp(A::a, 4ull, 6ull) ); +#endif + + VERIFY( (short)3 == std::clamp(A::a, (short)2, (short)4) ); + VERIFY( (short)2 == std::clamp(A::a, (short)1, (short)2) ); + VERIFY( (short)4 == std::clamp(A::a, (short)4, (short)6) ); + + VERIFY( (unsigned short)3 == std::clamp(A::a, (unsigned short)2, (unsigned short)4) ); + VERIFY( (unsigned short)2 == std::clamp(A::a, (unsigned short)1, (unsigned short)2) ); + VERIFY( (unsigned short)4 == std::clamp(A::a, (unsigned short)4, (unsigned short)6) ); + + VERIFY( (char)3 == std::clamp(A::a, (char)2, (char)4) ); + VERIFY( (char)2 == std::clamp(A::a, (char)1, (char)2) ); + VERIFY( (char)4 == std::clamp(A::a, (char)4, (char)6) ); + + VERIFY( (signed char)3 == std::clamp(A::a, (signed char)2, (signed char)4) ); + VERIFY( (signed char)2 == std::clamp(A::a, (signed char)1, (signed char)2) ); + VERIFY( (signed char)4 == std::clamp(A::a, (signed char)4, (signed char)6) ); + + VERIFY( (unsigned char)3 == std::clamp(A::a, (unsigned char)2, (unsigned char)4) ); + VERIFY( (unsigned char)2 == std::clamp(A::a, (unsigned char)1, (unsigned char)2) ); + VERIFY( (unsigned char)4 == std::clamp(A::a, (unsigned char)4, (unsigned char)6) ); + + VERIFY( (wchar_t)3 == std::clamp(A::a, (wchar_t)2, (wchar_t)4) ); + VERIFY( (wchar_t)2 == std::clamp(A::a, (wchar_t)1, (wchar_t)2) ); + VERIFY( (wchar_t)4 == std::clamp(A::a, (wchar_t)4, (wchar_t)6) ); + + VERIFY( 3.0 == std::clamp(A::a, 2.0, 4.0) ); + VERIFY( 2.0 == std::clamp(A::a, 1.0, 2.0) ); + VERIFY( 4.0 == std::clamp(A::a, 4.0, 6.0) ); + + VERIFY( 3.0f == std::clamp(A::a, 2.0f, 4.0f) ); + VERIFY( 2.0f == std::clamp(A::a, 1.0f, 2.0f) ); + VERIFY( 4.0f == std::clamp(A::a, 4.0f, 6.0f) ); + + VERIFY( 3.0l == std::clamp(A::a, 2.0l, 4.0l) ); + VERIFY( 2.0l == std::clamp(A::a, 1.0l, 2.0l) ); + VERIFY( 4.0l == std::clamp(A::a, 4.0l, 6.0l) ); +} + +int +main() +{ + test02(); + return 0; +} diff --git a/libstdc++-v3/testsuite/25_algorithms/clamp/constexpr.cc b/libstdc++-v3/testsuite/25_algorithms/clamp/constexpr.cc new file mode 100644 index 00000000000..4ff8798cfae --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/clamp/constexpr.cc @@ -0,0 +1,31 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include + +#ifndef __cpp_lib_clamp +# error "Feature-test macro for clamp missing" +#elif __cpp_lib_clamp != 201603 +# error "Feature-test macro for clamp has wrong value" +#endif + +static_assert(std::clamp(2, 0, 1) == 1, ""); +static_assert(std::clamp(2, 0, 1, std::greater()) == 0, ""); diff --git a/libstdc++-v3/testsuite/25_algorithms/clamp/requirements/explicit_instantiation/1.cc b/libstdc++-v3/testsuite/25_algorithms/clamp/requirements/explicit_instantiation/1.cc new file mode 100644 index 00000000000..d8bf8d6dcee --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/clamp/requirements/explicit_instantiation/1.cc @@ -0,0 +1,44 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include +#include + +#ifndef __cpp_lib_clamp +# error "Feature-test macro for clamp missing" +#elif __cpp_lib_clamp != 201603 +# error "Feature-test macro for clamp has wrong value" +#endif + +namespace std +{ + using __gnu_test::NonDefaultConstructible; + + typedef NonDefaultConstructible value_type; + typedef value_type* iterator_type; + typedef std::less compare_type; + + template const value_type& clamp(const value_type&, + const value_type&, const value_type&); + template const value_type& clamp(const value_type&, + const value_type&, const value_type&, + compare_type); +} diff --git a/libstdc++-v3/testsuite/25_algorithms/clamp/requirements/explicit_instantiation/pod.cc b/libstdc++-v3/testsuite/25_algorithms/clamp/requirements/explicit_instantiation/pod.cc new file mode 100644 index 00000000000..acb6d0f26b8 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/clamp/requirements/explicit_instantiation/pod.cc @@ -0,0 +1,38 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2016 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + + +#include +#include + +namespace std +{ + using __gnu_test::pod_int; + + typedef pod_int value_type; + typedef value_type* iterator_type; + typedef std::less compare_type; + + template const value_type& clamp(const value_type&, + const value_type&, const value_type&); + template const value_type& clamp(const value_type&, + const value_type&, const value_type&, + compare_type); +} -- 2.11.4.GIT