Skip to content

Commit d5949dc

Browse files
committed
feat(api): pass current_depth to retain's' callback
1 parent f1ac051 commit d5949dc

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

src/data_tree/retain.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,30 @@ where
77
Self: Send,
88
Size: size::Size,
99
{
10-
/// Recursively cull all descendants that do not satisfy given `predicate`, in parallel.
11-
pub fn par_retain(&mut self, predicate: impl Fn(&Self) -> bool + Copy + Sync) {
12-
self.children.retain(predicate);
10+
/// Internal function to be used by [`Self::par_retain`].
11+
fn par_retain_with_depth(
12+
&mut self,
13+
current_depth: u64,
14+
predicate: impl Fn(&Self, u64) -> bool + Copy + Sync,
15+
) {
16+
self.children
17+
.retain(|child| predicate(child, current_depth));
18+
let next_depth = current_depth + 1;
1319
self.children
1420
.par_iter_mut()
15-
.for_each(|child| child.par_retain(predicate));
21+
.for_each(|child| child.par_retain_with_depth(next_depth, predicate))
22+
}
23+
24+
/// Recursively cull all descendants that do not satisfy given `predicate`, in parallel.
25+
pub fn par_retain(&mut self, predicate: impl Fn(&Self, u64) -> bool + Copy + Sync) {
26+
self.par_retain_with_depth(0, predicate)
1627
}
1728

1829
/// Process the tree via [`par_retain`](Self::par_retain) method.
19-
pub fn into_par_retained(mut self, predicate: impl Fn(&Self) -> bool + Copy + Sync) -> Self {
30+
pub fn into_par_retained(
31+
mut self,
32+
predicate: impl Fn(&Self, u64) -> bool + Copy + Sync,
33+
) -> Self {
2034
self.par_retain(predicate);
2135
self
2236
}
@@ -28,7 +42,7 @@ where
2842
Size: Into<u64>,
2943
{
3044
let minimal = self.size().into() as f32 * min_ratio;
31-
self.par_retain(|descendant| descendant.size().into() as f32 >= minimal);
45+
self.par_retain(|descendant, _| descendant.size().into() as f32 >= minimal);
3246
}
3347

3448
/// Process the tree via [`par_cull_insignificant_data`](Self::par_cull_insignificant_data) method.

src/data_tree/retain/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ fn edge_cases() {
157157
),
158158
],
159159
)
160-
.into_par_retained(|descendant| descendant.name().starts_with('!').not())
160+
.into_par_retained(|descendant, _| descendant.name().starts_with('!').not())
161161
.into_reflection();
162162
let expected = dir(
163163
"root",

0 commit comments

Comments
 (0)