Skip to content

Commit 3903438

Browse files
authored
[libcxx] adds ranges::fold_left_with_iter and ranges::fold_left (#75259)
Notable things in this commit: * refactors `__indirect_binary_left_foldable`, making it slightly different (but equivalent) to _`indirect-binary-left-foldable`_, which improves readability (a [patch to the Working Paper][patch] was made) * omits `__cpo` namespace, since it is not required for implementing niebloids (a cleanup should happen in 2024) * puts tests ensuring invocable robustness and dangling correctness inside the correctness testing to ensure that the algorithms' results are still correct [patch]: cplusplus/draft#6734
1 parent 037c220 commit 3903438

File tree

18 files changed

+1062
-32
lines changed

18 files changed

+1062
-32
lines changed

libcxx/docs/Status/RangesAlgorithms.csv

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ 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,`fold_left_with_iter <https://wg21.link/p2322r6>`_,Christopher Di Bella,N/A,Complete
14+
C++23,`fold_left <https://wg21.link/p2322r6>`_,Christopher Di Bella,N/A,Complete
15+
C++23,`fold_left_first_with_iter <https://wg21.link/p2322r6>`_,Christopher Di Bella,N/A,In progress
16+
C++23,`fold_left_first <https://wg21.link/p2322r6>`_,Christopher Di Bella,N/A,In progress
17+
C++23,`fold_right <https://wg21.link/p2322r6>`_,Christopher Di Bella,N/A,In progress
18+
C++23,`fold_right_last <https://wg21.link/p2322r6>`_,Christopher Di Bella,N/A,In progress

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ set(files
2323
__algorithm/find_if.h
2424
__algorithm/find_if_not.h
2525
__algorithm/find_segment_if.h
26+
__algorithm/fold.h
2627
__algorithm/for_each.h
2728
__algorithm/for_each_n.h
2829
__algorithm/for_each_segment.h

libcxx/include/__algorithm/fold.h

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// -*- C++ -*-
2+
//===----------------------------------------------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef _LIBCPP___ALGORITHM_FOLD_H
11+
#define _LIBCPP___ALGORITHM_FOLD_H
12+
13+
#include <__concepts/assignable.h>
14+
#include <__concepts/convertible_to.h>
15+
#include <__concepts/invocable.h>
16+
#include <__concepts/movable.h>
17+
#include <__config>
18+
#include <__functional/invoke.h>
19+
#include <__functional/reference_wrapper.h>
20+
#include <__iterator/concepts.h>
21+
#include <__iterator/iterator_traits.h>
22+
#include <__iterator/next.h>
23+
#include <__ranges/access.h>
24+
#include <__ranges/concepts.h>
25+
#include <__ranges/dangling.h>
26+
#include <__type_traits/decay.h>
27+
#include <__type_traits/invoke.h>
28+
#include <__utility/forward.h>
29+
#include <__utility/move.h>
30+
31+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
32+
# pragma GCC system_header
33+
#endif
34+
35+
_LIBCPP_BEGIN_NAMESPACE_STD
36+
37+
#if _LIBCPP_STD_VER >= 23
38+
39+
namespace ranges {
40+
template <class _Ip, class _Tp>
41+
struct in_value_result {
42+
_LIBCPP_NO_UNIQUE_ADDRESS _Ip in;
43+
_LIBCPP_NO_UNIQUE_ADDRESS _Tp value;
44+
45+
template <class _I2, class _T2>
46+
requires convertible_to<const _Ip&, _I2> && convertible_to<const _Tp&, _T2>
47+
_LIBCPP_HIDE_FROM_ABI constexpr operator in_value_result<_I2, _T2>() const& {
48+
return {in, value};
49+
}
50+
51+
template <class _I2, class _T2>
52+
requires convertible_to<_Ip, _I2> && convertible_to<_Tp, _T2>
53+
_LIBCPP_HIDE_FROM_ABI constexpr operator in_value_result<_I2, _T2>() && {
54+
return {std::move(in), std::move(value)};
55+
}
56+
};
57+
58+
template <class _Ip, class _Tp>
59+
using fold_left_with_iter_result = in_value_result<_Ip, _Tp>;
60+
61+
template <class _Fp, class _Tp, class _Ip, class _Rp, class _Up = decay_t<_Rp>>
62+
concept __indirectly_binary_left_foldable_impl =
63+
convertible_to<_Rp, _Up> && //
64+
movable<_Tp> && //
65+
movable<_Up> && //
66+
convertible_to<_Tp, _Up> && //
67+
invocable<_Fp&, _Up, iter_reference_t<_Ip>> && //
68+
assignable_from<_Up&, invoke_result_t<_Fp&, _Up, iter_reference_t<_Ip>>>;
69+
70+
template <class _Fp, class _Tp, class _Ip>
71+
concept __indirectly_binary_left_foldable =
72+
copy_constructible<_Fp> && //
73+
invocable<_Fp&, _Tp, iter_reference_t<_Ip>> && //
74+
__indirectly_binary_left_foldable_impl<_Fp, _Tp, _Ip, invoke_result_t<_Fp&, _Tp, iter_reference_t<_Ip>>>;
75+
76+
struct __fold_left_with_iter {
77+
template <input_iterator _Ip, sentinel_for<_Ip> _Sp, class _Tp, __indirectly_binary_left_foldable<_Tp, _Ip> _Fp>
78+
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI static constexpr auto
79+
operator()(_Ip __first, _Sp __last, _Tp __init, _Fp __f) {
80+
using _Up = decay_t<invoke_result_t<_Fp&, _Tp, iter_reference_t<_Ip>>>;
81+
82+
if (__first == __last) {
83+
return fold_left_with_iter_result<_Ip, _Up>{std::move(__first), _Up(std::move(__init))};
84+
}
85+
86+
_Up __result = std::invoke(__f, std::move(__init), *__first);
87+
for (++__first; __first != __last; ++__first) {
88+
__result = std::invoke(__f, std::move(__result), *__first);
89+
}
90+
91+
return fold_left_with_iter_result<_Ip, _Up>{std::move(__first), std::move(__result)};
92+
}
93+
94+
template <input_range _Rp, class _Tp, __indirectly_binary_left_foldable<_Tp, iterator_t<_Rp>> _Fp>
95+
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Rp&& __r, _Tp __init, _Fp __f) {
96+
auto __result = operator()(ranges::begin(__r), ranges::end(__r), std::move(__init), std::ref(__f));
97+
98+
using _Up = decay_t<invoke_result_t<_Fp&, _Tp, range_reference_t<_Rp>>>;
99+
return fold_left_with_iter_result<borrowed_iterator_t<_Rp>, _Up>{std::move(__result.in), std::move(__result.value)};
100+
}
101+
};
102+
103+
inline constexpr auto fold_left_with_iter = __fold_left_with_iter();
104+
105+
struct __fold_left {
106+
template <input_iterator _Ip, sentinel_for<_Ip> _Sp, class _Tp, __indirectly_binary_left_foldable<_Tp, _Ip> _Fp>
107+
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI static constexpr auto
108+
operator()(_Ip __first, _Sp __last, _Tp __init, _Fp __f) {
109+
return fold_left_with_iter(std::move(__first), std::move(__last), std::move(__init), std::ref(__f)).value;
110+
}
111+
112+
template <input_range _Rp, class _Tp, __indirectly_binary_left_foldable<_Tp, iterator_t<_Rp>> _Fp>
113+
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Rp&& __r, _Tp __init, _Fp __f) {
114+
return fold_left_with_iter(ranges::begin(__r), ranges::end(__r), std::move(__init), std::ref(__f)).value;
115+
}
116+
};
117+
118+
inline constexpr auto fold_left = __fold_left();
119+
} // namespace ranges
120+
121+
#endif // _LIBCPP_STD_VER >= 23
122+
123+
_LIBCPP_END_NAMESPACE_STD
124+
125+
#endif // _LIBCPP___ALGORITHM_FOLD_H

libcxx/include/algorithm

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ namespace ranges {
4242
template <class I>
4343
struct in_found_result; // since C++20
4444
45+
template <class I, class T>
46+
struct in_value_result; // since C++23
47+
4548
template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
4649
indirect_strict_weak_order<projected<I, Proj>> Comp = ranges::less> // since C++20
4750
constexpr I min_element(I first, S last, Comp comp = {}, Proj proj = {});
@@ -881,6 +884,23 @@ namespace ranges {
881884
ranges::search_n(R&& r, range_difference_t<R> count,
882885
const T& value, Pred pred = {}, Proj proj = {}); // since C++20
883886
887+
template<input_iterator I, sentinel_for<I> S, class T,
888+
indirectly-binary-left-foldable<T, I> F>
889+
constexpr auto ranges::fold_left(I first, S last, T init, F f); // since C++23
890+
891+
template<input_range R, class T, indirectly-binary-left-foldable<T, iterator_t<R>> F>
892+
constexpr auto fold_left(R&& r, T init, F f); // since C++23
893+
894+
template<class I, class T>
895+
using fold_left_with_iter_result = in_value_result<I, T>; // since C++23
896+
897+
template<input_iterator I, sentinel_for<I> S, class T,
898+
indirectly-binary-left-foldable<T, I> F>
899+
constexpr see below fold_left_with_iter(I first, S last, T init, F f); // since C++23
900+
901+
template<input_range R, class T, indirectly-binary-left-foldable<T, iterator_t<R>> F>
902+
constexpr see below fold_left_with_iter(R&& r, T init, F f); // since C++23
903+
884904
template<forward_iterator I1, sentinel_for<I1> S1, forward_iterator I2, sentinel_for<I2> S2,
885905
class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
886906
requires indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
@@ -1786,6 +1806,7 @@ template <class BidirectionalIterator, class Compare>
17861806
#include <__algorithm/find_first_of.h>
17871807
#include <__algorithm/find_if.h>
17881808
#include <__algorithm/find_if_not.h>
1809+
#include <__algorithm/fold.h>
17891810
#include <__algorithm/for_each.h>
17901811
#include <__algorithm/for_each_n.h>
17911812
#include <__algorithm/generate.h>

libcxx/include/module.modulemap.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ module std_private_algorithm_find_first_of [system
666666
module std_private_algorithm_find_if [system] { header "__algorithm/find_if.h" }
667667
module std_private_algorithm_find_if_not [system] { header "__algorithm/find_if_not.h" }
668668
module std_private_algorithm_find_segment_if [system] { header "__algorithm/find_segment_if.h" }
669+
module std_private_algorithm_fold [system] { header "__algorithm/fold.h" }
669670
module std_private_algorithm_for_each [system] { header "__algorithm/for_each.h" }
670671
module std_private_algorithm_for_each_n [system] { header "__algorithm/for_each_n.h" }
671672
module std_private_algorithm_for_each_segment [system] { header "__algorithm/for_each_segment.h" }

libcxx/modules/std/algorithm.inc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export namespace std {
1616
using std::ranges::in_in_result;
1717
using std::ranges::in_out_out_result;
1818
using std::ranges::in_out_result;
19-
// using std::ranges::in_value_result;
19+
using std::ranges::in_value_result;
2020
using std::ranges::min_max_result;
2121
// using std::ranges::out_value_result;
2222
} // namespace ranges
@@ -157,15 +157,15 @@ export namespace std {
157157
// [alg.ends.with], ends with
158158
using std::ranges::ends_with;
159159

160-
# if 0
161160
// [alg.fold], fold
162161
using std::ranges::fold_left;
162+
using std::ranges::fold_left_with_iter;
163+
using std::ranges::fold_left_with_iter_result;
164+
# if 0
163165
using std::ranges::fold_left_first;
164166
using std::ranges::fold_right;
165167
using std::ranges::fold_right_last;
166168
using std::ranges::fold_left_with_iter;
167-
using std::ranges::fold_left_with_iter_result;
168-
using std::ranges::fold_left_with_iter;
169169
using std::ranges::fold_left_first_with_iter;
170170
using std::ranges::fold_left_first_with_iter;
171171
# endif

libcxx/test/libcxx/diagnostics/ranges.nodiscard_extensions.verify.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#include <algorithm>
1414

15+
#include "test_macros.h"
16+
1517
void test() {
1618
int range[1];
1719
int* iter = range;
@@ -87,4 +89,15 @@ void test() {
8789
std::ranges::unique(iter, iter); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
8890
std::ranges::upper_bound(range, 1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
8991
std::ranges::upper_bound(iter, iter, 1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
92+
93+
#if TEST_STD_VER >= 23
94+
std::ranges::fold_left(range, 0, std::plus());
95+
// expected-warning@-1{{ignoring return value of function declared with 'nodiscard' attribute}}
96+
std::ranges::fold_left(iter, iter, 0, std::plus());
97+
// expected-warning@-1{{ignoring return value of function declared with 'nodiscard' attribute}}
98+
std::ranges::fold_left_with_iter(range, 0, std::plus());
99+
// expected-warning@-1{{ignoring return value of function declared with 'nodiscard' attribute}}
100+
std::ranges::fold_left_with_iter(iter, iter, 0, std::plus());
101+
// expected-warning@-1{{ignoring return value of function declared with 'nodiscard' attribute}}
102+
#endif
90103
}

0 commit comments

Comments
 (0)