parallel_disk_usage/data_tree/
retain.rs

1use super::DataTree;
2use crate::size;
3use rayon::prelude::*;
4
5impl<Name, Size> DataTree<Name, Size>
6where
7    Self: Send,
8    Size: size::Size,
9{
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);
13        self.children
14            .par_iter_mut()
15            .for_each(|child| child.par_retain(predicate));
16    }
17
18    /// 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 {
20        self.par_retain(predicate);
21        self
22    }
23
24    /// Recursively cull all descendants whose sizes are too small relative to root.
25    #[cfg(feature = "cli")]
26    pub fn par_cull_insignificant_data(&mut self, min_ratio: f32)
27    where
28        Size: Into<u64>,
29    {
30        let minimal = self.size().into() as f32 * min_ratio;
31        self.par_retain(|descendant| descendant.size().into() as f32 >= minimal);
32    }
33
34    /// Process the tree via [`par_cull_insignificant_data`](Self::par_cull_insignificant_data) method.
35    #[cfg(test)]
36    #[cfg(feature = "cli")]
37    fn into_insignificant_data_par_culled(mut self, min_ratio: f32) -> Self
38    where
39        Size: Into<u64>,
40    {
41        self.par_cull_insignificant_data(min_ratio);
42        self
43    }
44}
45
46#[cfg(test)]
47#[cfg(feature = "cli")]
48mod test;