From Jason Turner

[alg.fold]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpw_0n5uxb/{from.md → to.md} +136 -0
tmp/tmpw_0n5uxb/{from.md → to.md} RENAMED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### Fold <a id="alg.fold">[[alg.fold]]</a>
2
+
3
+ ``` cpp
4
+ template<input_iterator I, sentinel_for<I> S, class T, indirectly-binary-left-foldable<T, I> F>
5
+ constexpr auto ranges::fold_left(I first, S last, T init, F f);
6
+ template<input_range R, class T, indirectly-binary-left-foldable<T, iterator_t<R>> F>
7
+ constexpr auto ranges::fold_left(R&& r, T init, F f);
8
+ ```
9
+
10
+ *Returns:*
11
+
12
+ ``` cpp
13
+ ranges::fold_left_with_iter(std::move(first), last, std::move(init), f).value
14
+ ```
15
+
16
+ ``` cpp
17
+ template<input_iterator I, sentinel_for<I> S,
18
+ indirectly-binary-left-foldable<iter_value_t<I>, I> F>
19
+ requires constructible_from<iter_value_t<I>, iter_reference_t<I>>
20
+ constexpr auto ranges::fold_left_first(I first, S last, F f);
21
+ template<input_range R, indirectly-binary-left-foldable<range_value_t<R>, iterator_t<R>> F>
22
+ requires constructible_from<range_value_t<R>, range_reference_t<R>>
23
+ constexpr auto ranges::fold_left_first(R&& r, F f);
24
+ ```
25
+
26
+ *Returns:*
27
+
28
+ ``` cpp
29
+ ranges::fold_left_first_with_iter(std::move(first), last, f).value
30
+ ```
31
+
32
+ ``` cpp
33
+ template<bidirectional_iterator I, sentinel_for<I> S, class T,
34
+ indirectly-binary-right-foldable<T, I> F>
35
+ constexpr auto ranges::fold_right(I first, S last, T init, F f);
36
+ template<bidirectional_range R, class T,
37
+ indirectly-binary-right-foldable<T, iterator_t<R>> F>
38
+ constexpr auto ranges::fold_right(R&& r, T init, F f);
39
+ ```
40
+
41
+ *Effects:* Equivalent to:
42
+
43
+ ``` cpp
44
+ using U = decay_t<invoke_result_t<F&, iter_reference_t<I>, T>>;
45
+ if (first == last)
46
+ return U(std::move(init));
47
+ I tail = ranges::next(first, last);
48
+ U accum = invoke(f, *--tail, std::move(init));
49
+ while (first != tail)
50
+ accum = invoke(f, *--tail, std::move(accum));
51
+ return accum;
52
+ ```
53
+
54
+ ``` cpp
55
+ template<bidirectional_iterator I, sentinel_for<I> S,
56
+ indirectly-binary-right-foldable<iter_value_t<I>, I> F>
57
+ requires constructible_from<iter_value_t<I>, iter_reference_t<I>>
58
+ constexpr auto ranges::fold_right_last(I first, S last, F f);
59
+ template<bidirectional_range R,
60
+ indirectly-binary-right-foldable<range_value_t<R>, iterator_t<R>> F>
61
+ requires constructible_from<range_value_t<R>, range_reference_t<R>>
62
+ constexpr auto ranges::fold_right_last(R&& r, F f);
63
+ ```
64
+
65
+ Let `U` be
66
+ `decltype(ranges::fold_right(first, last, iter_value_t<I>(*first), f))`.
67
+
68
+ *Effects:* Equivalent to:
69
+
70
+ ``` cpp
71
+ if (first == last)
72
+ return optional<U>();
73
+ I tail = ranges::prev(ranges::next(first, std::move(last)));
74
+ return optional<U>(in_place,
75
+ ranges::fold_right(std::move(first), tail, iter_value_t<I>(*tail), std::move(f)));
76
+ ```
77
+
78
+ ``` cpp
79
+ template<input_iterator I, sentinel_for<I> S, class T,
80
+ indirectly-binary-left-foldable<T, I> F>
81
+ constexpr see below ranges::fold_left_with_iter(I first, S last, T init, F f);
82
+ template<input_range R, class T, indirectly-binary-left-foldable<T, iterator_t<R>> F>
83
+ constexpr see below ranges::fold_left_with_iter(R&& r, T init, F f);
84
+ ```
85
+
86
+ Let `U` be `decay_t<invoke_result_t<F&, T, iter_reference_t<I>>>`.
87
+
88
+ *Effects:* Equivalent to:
89
+
90
+ ``` cpp
91
+ if (first == last)
92
+ return {std::move(first), U(std::move(init))};
93
+ U accum = invoke(f, std::move(init), *first);
94
+ for (++first; first != last; ++first)
95
+ accum = invoke(f, std::move(accum), *first);
96
+ return {std::move(first), std::move(accum)};
97
+ ```
98
+
99
+ *Remarks:* The return type is `fold_left_with_iter_result<I, U>` for the
100
+ first overload and
101
+ `fold_left_with_iter_result<borrowed_iterator_t<R>, U>` for the second
102
+ overload.
103
+
104
+ ``` cpp
105
+ template<input_iterator I, sentinel_for<I> S,
106
+ indirectly-binary-left-foldable<iter_value_t<I>, I> F>
107
+ requires constructible_from<iter_value_t<I>, iter_reference_t<I>>
108
+ constexpr see below ranges::fold_left_first_with_iter(I first, S last, F f);
109
+ template<input_range R, indirectly-binary-left-foldable<range_value_t<R>, iterator_t<R>> F>
110
+ requires constructible_from<range_value_t<R>, range_reference_t<R>>
111
+ constexpr see below ranges::fold_left_first_with_iter(R&& r, F f);
112
+ ```
113
+
114
+ Let `U` be
115
+
116
+ ``` cpp
117
+ decltype(ranges::fold_left(std::move(first), last, iter_value_t<I>(*first), f))
118
+ ```
119
+
120
+ *Effects:* Equivalent to:
121
+
122
+ ``` cpp
123
+ if (first == last)
124
+ return {std::move(first), optional<U>()};
125
+ optional<U> init(in_place, *first);
126
+ for (++first; first != last; ++first)
127
+ *init = invoke(f, std::move(*init), *first);
128
+ return {std::move(first), std::move(init)};
129
+ ```
130
+
131
+ *Remarks:* The return type is
132
+ `fold_left_first_with_iter_result<I, optional<U>>` for the first
133
+ overload and
134
+ `fold_left_first_with_iter_result<borrowed_iterator_t<R>, optional<U>>`
135
+ for the second overload.
136
+