refactor create AggregateData for filter.rs

This commit is contained in:
andy.boot
2023-01-05 00:13:08 +00:00
parent 04c4963a02
commit a3ab5bfe0f
2 changed files with 62 additions and 54 deletions
+41 -37
View File
@@ -5,14 +5,15 @@ use std::collections::HashSet;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
pub fn get_biggest( pub struct AggregateData {
top_level_nodes: Vec<Node>, pub min_size: Option<usize>,
min_size: Option<usize>, pub only_dir: bool,
only_dir: bool, pub number_of_lines: usize,
n: usize, pub depth: usize,
depth: usize, pub using_a_filter: bool,
using_a_filter: bool, }
) -> Option<DisplayNode> {
pub fn get_biggest(top_level_nodes: Vec<Node>, display_data: AggregateData) -> Option<DisplayNode> {
if top_level_nodes.is_empty() { if top_level_nodes.is_empty() {
// perhaps change this, bring back Error object? // perhaps change this, bring back Error object?
return None; return None;
@@ -31,69 +32,72 @@ pub fn get_biggest(
depth: 0, depth: 0,
}; };
// Always include the base nodes if we add a 'parent' (total) node // Always include the base nodes if we add a 'parent' (total) node
heap = add_children(using_a_filter, min_size, only_dir, &root, usize::MAX, heap); heap = always_add_children(&display_data, &root, heap);
} else { } else {
root = top_level_nodes.into_iter().next().unwrap(); root = top_level_nodes.into_iter().next().unwrap();
heap = add_children(using_a_filter, min_size, only_dir, &root, depth, heap); heap = add_children(&display_data, &root, heap);
} }
let remaining = n.checked_sub(number_top_level_nodes).unwrap_or(0); let nol = display_data.number_of_lines;
fill_remaining_lines( let remaining = nol.saturating_sub(number_top_level_nodes);
heap, fill_remaining_lines(heap, &root, remaining, display_data)
&root,
min_size,
only_dir,
remaining,
depth,
using_a_filter,
)
} }
pub fn fill_remaining_lines<'a>( pub fn fill_remaining_lines<'a>(
mut heap: BinaryHeap<&'a Node>, mut heap: BinaryHeap<&'a Node>,
root: &'a Node, root: &'a Node,
min_size: Option<usize>, remaining_lines: usize,
only_dir: bool, display_data: AggregateData,
remaining: usize,
depth: usize,
using_a_filter: bool,
) -> Option<DisplayNode> { ) -> Option<DisplayNode> {
let mut allowed_nodes = HashSet::new(); let mut allowed_nodes = HashSet::new();
allowed_nodes.insert(root.name.as_path()); allowed_nodes.insert(root.name.as_path());
for _ in 0..remaining { for _ in 0..remaining_lines {
let line = heap.pop(); let line = heap.pop();
match line { match line {
Some(line) => { Some(line) => {
allowed_nodes.insert(line.name.as_path()); allowed_nodes.insert(line.name.as_path());
heap = add_children(using_a_filter, min_size, only_dir, line, depth, heap); heap = add_children(&display_data, line, heap);
} }
None => break, None => break,
} }
} }
recursive_rebuilder(&allowed_nodes, &root) recursive_rebuilder(&allowed_nodes, root)
} }
fn add_children<'a>( fn add_children<'a>(
using_a_filter: bool, display_data: &AggregateData,
min_size: Option<usize>, file_or_folder: &'a Node,
only_dir: bool, heap: BinaryHeap<&'a Node>,
) -> BinaryHeap<&'a Node> {
if display_data.depth > file_or_folder.depth {
always_add_children(display_data, file_or_folder, heap)
} else {
heap
}
}
fn always_add_children<'a>(
display_data: &AggregateData,
file_or_folder: &'a Node, file_or_folder: &'a Node,
depth: usize,
mut heap: BinaryHeap<&'a Node>, mut heap: BinaryHeap<&'a Node>,
) -> BinaryHeap<&'a Node> { ) -> BinaryHeap<&'a Node> {
if depth > file_or_folder.depth {
heap.extend( heap.extend(
file_or_folder file_or_folder
.children .children
.iter() .iter()
.filter(|c| match min_size { .filter(|c| match display_data.min_size {
Some(ms) => c.size > ms as u64, Some(ms) => c.size > ms as u64,
None => !using_a_filter || c.name.is_file() || c.size > 0, None => !display_data.using_a_filter || c.name.is_file() || c.size > 0,
}) })
.filter(|c| if only_dir { c.name.is_dir() } else { true }), .filter(|c| {
) if display_data.only_dir {
c.name.is_dir()
} else {
true
} }
}),
);
heap heap
} }
+12 -8
View File
@@ -10,6 +10,8 @@ mod platform;
mod utils; mod utils;
use crate::cli::build_cli; use crate::cli::build_cli;
use dir_walker::WalkData;
use filter::AggregateData;
use std::collections::HashSet; use std::collections::HashSet;
use std::io::BufRead; use std::io::BufRead;
use std::process; use std::process;
@@ -18,7 +20,7 @@ use sysinfo::{System, SystemExt};
use self::display::draw_it; use self::display::draw_it;
use clap::Values; use clap::Values;
use config::get_config; use config::get_config;
use dir_walker::{walk_it, WalkData}; use dir_walker::walk_it;
use filter::get_biggest; use filter::get_biggest;
use filter_type::get_all_file_types; use filter_type::get_all_file_types;
use rayon::ThreadPoolBuildError; use rayon::ThreadPoolBuildError;
@@ -175,17 +177,19 @@ fn main() {
let iso = config.get_iso(&options); let iso = config.get_iso(&options);
let (top_level_nodes, has_errors) = walk_it(simplified_dirs, walk_data); let (top_level_nodes, has_errors) = walk_it(simplified_dirs, walk_data);
let tree = match summarize_file_types { let tree = match summarize_file_types {
true => get_all_file_types(&top_level_nodes, number_of_lines), true => get_all_file_types(&top_level_nodes, number_of_lines),
false => get_biggest( false => {
top_level_nodes, let agg_data = AggregateData {
config.get_min_size(&options, iso), min_size: config.get_min_size(&options, iso),
config.get_only_dir(&options), only_dir: config.get_only_dir(&options),
number_of_lines, number_of_lines,
depth, depth,
options.values_of("filter").is_some() || options.value_of("invert_filter").is_some(), using_a_filter: options.values_of("filter").is_some()
), || options.value_of("invert_filter").is_some(),
};
get_biggest(top_level_nodes, agg_data)
}
}; };
if has_errors { if has_errors {