Skip to content

Commit 45c5dec

Browse files
Philippe-Choletjswrenn
authored andcommitted
Deprecate tree_fold1 for tree_reduce
And rename various identifiers.
1 parent 78a9e7e commit 45c5dec

File tree

5 files changed

+43
-33
lines changed

5 files changed

+43
-33
lines changed

benches/tree_reduce.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use itertools::{cloned, Itertools};
66
trait IterEx: Iterator {
77
// Another efficient implementation against which to compare,
88
// but needs `std` so is less desirable.
9-
fn tree_fold1_vec<F>(self, mut f: F) -> Option<Self::Item>
9+
fn tree_reduce_vec<F>(self, mut f: F) -> Option<Self::Item>
1010
where
1111
F: FnMut(Self::Item, Self::Item) -> Self::Item,
1212
Self: Sized,
@@ -91,14 +91,14 @@ def_benchs! {
9191

9292
def_benchs! {
9393
10_000,
94-
tree_fold1,
95-
tree_fold1_stack_10k,
94+
tree_reduce,
95+
tree_reduce_stack_10k,
9696
}
9797

9898
def_benchs! {
9999
10_000,
100-
tree_fold1_vec,
101-
tree_fold1_vec_10k,
100+
tree_reduce_vec,
101+
tree_reduce_vec_10k,
102102
}
103103

104104
def_benchs! {
@@ -109,14 +109,14 @@ def_benchs! {
109109

110110
def_benchs! {
111111
100,
112-
tree_fold1,
113-
tree_fold1_stack_100,
112+
tree_reduce,
113+
tree_reduce_stack_100,
114114
}
115115

116116
def_benchs! {
117117
100,
118-
tree_fold1_vec,
119-
tree_fold1_vec_100,
118+
tree_reduce_vec,
119+
tree_reduce_vec_100,
120120
}
121121

122122
def_benchs! {
@@ -127,24 +127,24 @@ def_benchs! {
127127

128128
def_benchs! {
129129
8,
130-
tree_fold1,
131-
tree_fold1_stack_08,
130+
tree_reduce,
131+
tree_reduce_stack_08,
132132
}
133133

134134
def_benchs! {
135135
8,
136-
tree_fold1_vec,
137-
tree_fold1_vec_08,
136+
tree_reduce_vec,
137+
tree_reduce_vec_08,
138138
}
139139

140140
criterion_main!(
141141
fold1_10k,
142-
tree_fold1_stack_10k,
143-
tree_fold1_vec_10k,
142+
tree_reduce_stack_10k,
143+
tree_reduce_vec_10k,
144144
fold1_100,
145-
tree_fold1_stack_100,
146-
tree_fold1_vec_100,
145+
tree_reduce_stack_100,
146+
tree_reduce_vec_100,
147147
fold1_08,
148-
tree_fold1_stack_08,
149-
tree_fold1_vec_08,
148+
tree_reduce_stack_08,
149+
tree_reduce_vec_08,
150150
);

src/lib.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2466,7 +2466,7 @@ pub trait Itertools: Iterator {
24662466
/// - if `f` is a trivial operation like `u32::wrapping_add`, prefer the normal
24672467
/// [`Iterator::reduce`] instead since it will most likely result in the generation of simpler
24682468
/// code because the compiler is able to optimize it
2469-
/// - otherwise if `f` is non-trivial like `format!`, you should use `tree_fold1` since it
2469+
/// - otherwise if `f` is non-trivial like `format!`, you should use `tree_reduce` since it
24702470
/// reduces the number of operations from `O(n)` to `O(ln(n))`
24712471
///
24722472
/// Here "non-trivial" means:
@@ -2479,20 +2479,20 @@ pub trait Itertools: Iterator {
24792479
///
24802480
/// // The same tree as above
24812481
/// let num_strings = (1..8).map(|x| x.to_string());
2482-
/// assert_eq!(num_strings.tree_fold1(|x, y| format!("f({}, {})", x, y)),
2482+
/// assert_eq!(num_strings.tree_reduce(|x, y| format!("f({}, {})", x, y)),
24832483
/// Some(String::from("f(f(f(1, 2), f(3, 4)), f(f(5, 6), 7))")));
24842484
///
24852485
/// // Like fold1, an empty iterator produces None
2486-
/// assert_eq!((0..0).tree_fold1(|x, y| x * y), None);
2486+
/// assert_eq!((0..0).tree_reduce(|x, y| x * y), None);
24872487
///
2488-
/// // tree_fold1 matches fold1 for associative operations...
2489-
/// assert_eq!((0..10).tree_fold1(|x, y| x + y),
2488+
/// // tree_reduce matches fold1 for associative operations...
2489+
/// assert_eq!((0..10).tree_reduce(|x, y| x + y),
24902490
/// (0..10).fold1(|x, y| x + y));
24912491
/// // ...but not for non-associative ones
2492-
/// assert_ne!((0..10).tree_fold1(|x, y| x - y),
2492+
/// assert_ne!((0..10).tree_reduce(|x, y| x - y),
24932493
/// (0..10).fold1(|x, y| x - y));
24942494
/// ```
2495-
fn tree_fold1<F>(mut self, mut f: F) -> Option<Self::Item>
2495+
fn tree_reduce<F>(mut self, mut f: F) -> Option<Self::Item>
24962496
where
24972497
F: FnMut(Self::Item, Self::Item) -> Self::Item,
24982498
Self: Sized,
@@ -2505,7 +2505,7 @@ pub trait Itertools: Iterator {
25052505
FF: FnMut(T, T) -> T,
25062506
{
25072507
// This function could be replaced with `it.next().ok_or(None)`,
2508-
// but half the useful tree_fold1 work is combining adjacent items,
2508+
// but half the useful tree_reduce work is combining adjacent items,
25092509
// so put that in a form that LLVM is more likely to optimize well.
25102510

25112511
let a = if let Some(v) = it.next() {
@@ -2555,6 +2555,16 @@ pub trait Itertools: Iterator {
25552555
}
25562556
}
25572557

2558+
/// See [`.tree_reduce()`](Itertools::tree_reduce).
2559+
#[deprecated(note = "Use .tree_reduce() instead", since = "0.13.0")]
2560+
fn tree_fold1<F>(self, f: F) -> Option<Self::Item>
2561+
where
2562+
F: FnMut(Self::Item, Self::Item) -> Self::Item,
2563+
Self: Sized,
2564+
{
2565+
self.tree_reduce(f)
2566+
}
2567+
25582568
/// An iterator method that applies a function, producing a single, final value.
25592569
///
25602570
/// `fold_while()` is basically equivalent to [`Iterator::fold`] but with additional support for

tests/quick.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,7 @@ quickcheck! {
13341334
}
13351335

13361336
quickcheck! {
1337-
fn tree_fold1_f64(mut a: Vec<f64>) -> TestResult {
1337+
fn tree_reduce_f64(mut a: Vec<f64>) -> TestResult {
13381338
fn collapse_adjacent<F>(x: Vec<f64>, mut f: F) -> Vec<f64>
13391339
where F: FnMut(f64, f64) -> f64
13401340
{
@@ -1353,7 +1353,7 @@ quickcheck! {
13531353
return TestResult::discard();
13541354
}
13551355

1356-
let actual = a.iter().cloned().tree_fold1(f64::atan2);
1356+
let actual = a.iter().cloned().tree_reduce(f64::atan2);
13571357

13581358
while a.len() > 1 {
13591359
a = collapse_adjacent(a, f64::atan2);

tests/test_core.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,9 @@ fn part() {
278278
}
279279

280280
#[test]
281-
fn tree_fold1() {
281+
fn tree_reduce() {
282282
for i in 0..100 {
283-
assert_eq!((0..i).tree_fold1(|x, y| x + y), (0..i).fold1(|x, y| x + y));
283+
assert_eq!((0..i).tree_reduce(|x, y| x + y), (0..i).fold1(|x, y| x + y));
284284
}
285285
}
286286

tests/test_std.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,7 @@ fn fold_while() {
14341434
}
14351435

14361436
#[test]
1437-
fn tree_fold1() {
1437+
fn tree_reduce() {
14381438
let x = [
14391439
"",
14401440
"0",
@@ -1461,7 +1461,7 @@ fn tree_fold1() {
14611461
Some(s.to_string())
14621462
};
14631463
let num_strings = (0..i).map(|x| x.to_string());
1464-
let actual = num_strings.tree_fold1(|a, b| format!("{} {} x", a, b));
1464+
let actual = num_strings.tree_reduce(|a, b| format!("{} {} x", a, b));
14651465
assert_eq!(actual, expected);
14661466
}
14671467
}

0 commit comments

Comments
 (0)