Skip to content

Commit a455f42

Browse files
committed
[libc++] Implement ranges::iota: Adding helper function to implementation and updating docs.
1 parent c44d7d6 commit a455f42

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

libcxx/docs/Status/RangesAlgorithms.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ C++23,`shift_right <https://wg21.link/p2440r1>`_,Unassigned,No patch yet,Not sta
1010
C++23,`iota (algorithm) <https://wg21.link/p2440r1>`_,Unassigned,No patch yet,Not started
1111
C++23,`fold <https://wg21.link/p2322r5>`_,Unassigned,No patch yet,Not started
1212
C++23,`contains <https://wg21.link/p2302r2>`_,Zijun Zhao,No patch yet,In Progress
13+
C++23,`ranges::iota <https://wg21.link/P2440R1>`_, James E T Smith, `PR68494 <https://github.com/llvm/llvm-project/pull/68494>`_, In Progress

libcxx/docs/Status/RangesMajorFeatures.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ Standard,Name,Assignee,CL,Status
22
C++23,`ranges::to <https://wg21.link/P1206R7>`_,Konstantin Varlamov,`D142335 <https://reviews.llvm.org/D142335>`_,Complete
33
C++23,`Pipe support for user-defined range adaptors <https://wg21.link/P2387R3>`_,Unassigned,No patch yet,Not started
44
C++23,`Formatting Ranges <https://wg21.link/P2286R8>`_,Mark de Wever,Various,Complete
5+
C++23, `ranges::iota <https://wg21.link/P2440R1>`_, James E T Smith, `PR68494 <https://github.com/llvm/llvm-project/pull/68494>`_, In Progress

libcxx/include/__numeric/ranges_iota.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <__config>
1515
#include <__ranges/concepts.h>
1616
#include <__utility/as_const.h>
17+
#include <__utility/move.h>
1718

1819
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1920
# pragma GCC system_header
@@ -26,10 +27,12 @@ namespace ranges {
2627
template <typename _Out, typename _Tp>
2728
using iota_result = ranges::out_value_result<_Out, _Tp>;
2829

30+
namespace __ranges_iota {
2931
struct __iota_fn {
30-
template <input_or_output_iterator _Out, sentinel_for<_Out> _Sent, weakly_incrementable _Tp>
31-
requires indirectly_writable<_Out, const _Tp&>
32-
constexpr iota_result<_Out, _Tp> operator()(_Out __first, _Sent __last, _Tp __value) const {
32+
private:
33+
// Private helper function
34+
template <class _Out, class _Sent, class _Tp>
35+
_LIBCPP_HIDE_FROM_ABI static constexpr iota_result<_Out, _Tp> __iota_impl(_Out __first, _Sent __last, _Tp __value) {
3336
while (__first != __last) {
3437
*__first = std::as_const(__value);
3538
++__first;
@@ -38,13 +41,24 @@ struct __iota_fn {
3841
return {std::move(__first), std::move(__value)};
3942
}
4043

44+
public:
45+
// Public facing interfaces
46+
template <input_or_output_iterator _Out, sentinel_for<_Out> _Sent, weakly_incrementable _Tp>
47+
requires indirectly_writable<_Out, const _Tp&>
48+
constexpr iota_result<_Out, _Tp> operator()(_Out __first, _Sent __last, _Tp __value) const {
49+
return __iota_impl(std::move(__first), std::move(__last), std::move(__value));
50+
}
51+
4152
template <weakly_incrementable _Tp, ranges::output_range<const _Tp&> _Range>
4253
constexpr iota_result<ranges::borrowed_iterator_t<_Range>, _Tp> operator()(_Range&& __r, _Tp __value) const {
43-
return (*this)(ranges::begin(__r), ranges::end(__r), std::move(__value));
54+
return __iota_impl(ranges::begin(__r), ranges::end(__r), std::move(__value));
4455
}
4556
};
57+
} // namespace __ranges_iota
4658

47-
inline constexpr __iota_fn iota{};
59+
inline namespace __cpo {
60+
inline constexpr auto iota = __ranges_iota::__iota_fn{};
61+
} // namespace __cpo
4862
} // namespace ranges
4963

5064
#endif // _LIBCPP_STD_VER >= 23

libcxx/test/std/algorithms/algorithms.results/out_value_result.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,4 @@ int main(int, char**) {
121121
test();
122122
static_assert(test());
123123
return 0;
124-
}
124+
}

libcxx/test/std/numerics/numeric.ops/numeric.iota/ranges.iota.pass.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ concept HasIotaIter = requires(Iter&& iter, Sent&& sent, Value&& val) {
2626
};
2727

2828
template <class Range, class Value = int>
29-
concept HasIotaRange =
30-
requires(Range&& range, Value&& val) { std::ranges::iota(std::forward<Range>(range), std::forward<Value>(val)); };
29+
concept HasIotaRange = requires(Range&& range, Value&& val) {
30+
std::ranges::iota(std::forward<Range>(range), std::forward<Value>(val));
31+
};
3132

3233
constexpr void test_constraints() {
3334
// Test constraints of the iterator/sentinel overload
@@ -120,4 +121,4 @@ int main(int, char**) {
120121
test_constraints();
121122
test_results();
122123
return 0;
123-
}
124+
}

0 commit comments

Comments
 (0)