[libc] [clang] [libcxx] [flang] [llvm] [compiler-rt] [clang-tools-extra] [libc++] Implement ranges::iota (PR #68494)
https://github.com/jamesETsmith updated https://github.com/llvm/llvm-project/pull/68494 >From c4a3ccfbad090ad8314aa8ad53092edc8d5432bc Mon Sep 17 00:00:00 2001 From: James Smith Date: Thu, 28 Sep 2023 10:11:15 -0400 Subject: [PATCH 01/16] [libc++] Implement ranges::iota and ranges::out_value_result --- libcxx/include/CMakeLists.txt | 2 + libcxx/include/__algorithm/out_value_result.h | 52 + libcxx/include/__numeric/ranges_iota.h| 53 + libcxx/include/algorithm | 4 + libcxx/include/numeric| 1 + libcxx/include/version| 2 +- .../out_value_result.pass.cpp | 102 ++ .../numeric.iota/ranges.iota.pass.cpp | 52 + 8 files changed, 267 insertions(+), 1 deletion(-) create mode 100644 libcxx/include/__algorithm/out_value_result.h create mode 100644 libcxx/include/__numeric/ranges_iota.h create mode 100644 libcxx/test/std/algorithms/algorithms.results/out_value_result.pass.cpp create mode 100644 libcxx/test/std/numerics/numeric.ops/numeric.iota/ranges.iota.pass.cpp diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt index 2ec755236dbaee2..c6eb03f1d68e984 100644 --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -63,6 +63,7 @@ set(files __algorithm/next_permutation.h __algorithm/none_of.h __algorithm/nth_element.h + __algorithm/out_value_result.h __algorithm/partial_sort.h __algorithm/partial_sort_copy.h __algorithm/partition.h @@ -561,6 +562,7 @@ set(files __numeric/partial_sum.h __numeric/pstl_reduce.h __numeric/pstl_transform_reduce.h + __numeric/ranges_iota.h __numeric/reduce.h __numeric/transform_exclusive_scan.h __numeric/transform_inclusive_scan.h diff --git a/libcxx/include/__algorithm/out_value_result.h b/libcxx/include/__algorithm/out_value_result.h new file mode 100644 index 000..8baffec7b9ef4da --- /dev/null +++ b/libcxx/include/__algorithm/out_value_result.h @@ -0,0 +1,52 @@ +// -*- C++ -*- +//===--===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#ifndef _LIBCPP___ALGORITHM_OUT_VALUE_RESULT_H +#define _LIBCPP___ALGORITHM_OUT_VALUE_RESULT_H + +#include <__concepts/convertible_to.h> +#include <__config> +#include <__utility/move.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +_LIBCPP_PUSH_MACROS +#include <__undef_macros> + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if _LIBCPP_STD_VER >= 23 + +namespace ranges { + +template +struct out_value_result { + _LIBCPP_NO_UNIQUE_ADDRESS _OutIter1 out; + _LIBCPP_NO_UNIQUE_ADDRESS _ValType1 value; + + template +requires convertible_to && convertible_to + constexpr operator out_value_result<_OutIter2, _ValType2>() const& { return {out, value}; } + + template +requires convertible_to<_OutIter1, _OutIter2> && convertible_to<_ValType1, _ValType2> + constexpr operator out_value_result<_OutIter2, _ValType2>() && { return {std::move(out), std::move(value)}; } +}; + +} // namespace ranges + +#endif // _LIBCPP_STD_VER >= 23 + +_LIBCPP_END_NAMESPACE_STD + +_LIBCPP_POP_MACROS + +#endif // _LIBCPP___ALGORITHM_OUT_VALUE_RESULT_H diff --git a/libcxx/include/__numeric/ranges_iota.h b/libcxx/include/__numeric/ranges_iota.h new file mode 100644 index 000..20311a68c2a348c --- /dev/null +++ b/libcxx/include/__numeric/ranges_iota.h @@ -0,0 +1,53 @@ +// -*- C++ -*- +//===--===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#ifndef _LIBCPP___NUMERIC_RANGES_IOTA_H +#define _LIBCPP___NUMERIC_RANGES_IOTA_H + +#include <__algorithm/out_value_result.h> +#include <__config> +#include <__ranges/concepts.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if _LIBCPP_STD_VER >= 23 +namespace ranges { +template +using iota_result = ranges::out_value_result<_Out, _Tp>; + +struct __iota_fn { + template _Sent, weakly_incrementable _Tp> +requires indirectly_writable<_Out, const _Tp&> + constexpr iota_result<_Out, _Tp> operator()(_Out __first, _Sent __last, _Tp __value) const { +while (__first != __last) { + *__first = static_cast(__value); + ++__first; + ++__value; +} +return {std::move(__first), std::move(__valu
[libc] [clang] [libcxx] [flang] [llvm] [compiler-rt] [clang-tools-extra] [libc++] Implement ranges::iota (PR #68494)
https://github.com/jamesETsmith updated https://github.com/llvm/llvm-project/pull/68494 >From c4a3ccfbad090ad8314aa8ad53092edc8d5432bc Mon Sep 17 00:00:00 2001 From: James Smith Date: Thu, 28 Sep 2023 10:11:15 -0400 Subject: [PATCH 01/16] [libc++] Implement ranges::iota and ranges::out_value_result --- libcxx/include/CMakeLists.txt | 2 + libcxx/include/__algorithm/out_value_result.h | 52 + libcxx/include/__numeric/ranges_iota.h| 53 + libcxx/include/algorithm | 4 + libcxx/include/numeric| 1 + libcxx/include/version| 2 +- .../out_value_result.pass.cpp | 102 ++ .../numeric.iota/ranges.iota.pass.cpp | 52 + 8 files changed, 267 insertions(+), 1 deletion(-) create mode 100644 libcxx/include/__algorithm/out_value_result.h create mode 100644 libcxx/include/__numeric/ranges_iota.h create mode 100644 libcxx/test/std/algorithms/algorithms.results/out_value_result.pass.cpp create mode 100644 libcxx/test/std/numerics/numeric.ops/numeric.iota/ranges.iota.pass.cpp diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt index 2ec755236dbaee2..c6eb03f1d68e984 100644 --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -63,6 +63,7 @@ set(files __algorithm/next_permutation.h __algorithm/none_of.h __algorithm/nth_element.h + __algorithm/out_value_result.h __algorithm/partial_sort.h __algorithm/partial_sort_copy.h __algorithm/partition.h @@ -561,6 +562,7 @@ set(files __numeric/partial_sum.h __numeric/pstl_reduce.h __numeric/pstl_transform_reduce.h + __numeric/ranges_iota.h __numeric/reduce.h __numeric/transform_exclusive_scan.h __numeric/transform_inclusive_scan.h diff --git a/libcxx/include/__algorithm/out_value_result.h b/libcxx/include/__algorithm/out_value_result.h new file mode 100644 index 000..8baffec7b9ef4da --- /dev/null +++ b/libcxx/include/__algorithm/out_value_result.h @@ -0,0 +1,52 @@ +// -*- C++ -*- +//===--===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#ifndef _LIBCPP___ALGORITHM_OUT_VALUE_RESULT_H +#define _LIBCPP___ALGORITHM_OUT_VALUE_RESULT_H + +#include <__concepts/convertible_to.h> +#include <__config> +#include <__utility/move.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +_LIBCPP_PUSH_MACROS +#include <__undef_macros> + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if _LIBCPP_STD_VER >= 23 + +namespace ranges { + +template +struct out_value_result { + _LIBCPP_NO_UNIQUE_ADDRESS _OutIter1 out; + _LIBCPP_NO_UNIQUE_ADDRESS _ValType1 value; + + template +requires convertible_to && convertible_to + constexpr operator out_value_result<_OutIter2, _ValType2>() const& { return {out, value}; } + + template +requires convertible_to<_OutIter1, _OutIter2> && convertible_to<_ValType1, _ValType2> + constexpr operator out_value_result<_OutIter2, _ValType2>() && { return {std::move(out), std::move(value)}; } +}; + +} // namespace ranges + +#endif // _LIBCPP_STD_VER >= 23 + +_LIBCPP_END_NAMESPACE_STD + +_LIBCPP_POP_MACROS + +#endif // _LIBCPP___ALGORITHM_OUT_VALUE_RESULT_H diff --git a/libcxx/include/__numeric/ranges_iota.h b/libcxx/include/__numeric/ranges_iota.h new file mode 100644 index 000..20311a68c2a348c --- /dev/null +++ b/libcxx/include/__numeric/ranges_iota.h @@ -0,0 +1,53 @@ +// -*- C++ -*- +//===--===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#ifndef _LIBCPP___NUMERIC_RANGES_IOTA_H +#define _LIBCPP___NUMERIC_RANGES_IOTA_H + +#include <__algorithm/out_value_result.h> +#include <__config> +#include <__ranges/concepts.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if _LIBCPP_STD_VER >= 23 +namespace ranges { +template +using iota_result = ranges::out_value_result<_Out, _Tp>; + +struct __iota_fn { + template _Sent, weakly_incrementable _Tp> +requires indirectly_writable<_Out, const _Tp&> + constexpr iota_result<_Out, _Tp> operator()(_Out __first, _Sent __last, _Tp __value) const { +while (__first != __last) { + *__first = static_cast(__value); + ++__first; + ++__value; +} +return {std::move(__first), std::move(__valu