From 5cf4cea9b0aa5c298f321d57970646ae31da466a Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 25 Aug 2022 15:48:52 +0300 Subject: [PATCH] trait SplitTo --- src/main.rs | 58 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index 049a466..dc76b42 100644 --- a/src/main.rs +++ b/src/main.rs @@ -216,29 +216,43 @@ fn parse_file(file: impl io::Read) -> Result(src: &'a [T], num: usize) -> Vec<&'a [T]> { - let part_len = src.len() / num; - let add_len = src.len() % num; - let mut result = Vec::<&'a [T]>::with_capacity(num); - - if 0 == part_len { - result.push(src); - return result; - } - for i in 0..num { - let size = if (num - 1) == i { - part_len + add_len - } else { - part_len - }; - let start = part_len * i; - result.push(&src[start..(start + size)]); - } - result +// split vector to a vector of [num] slices +trait SplitTo { + fn split_to<'a>(&'a self, num: usize) -> Vec<&'a [T]>; } +impl SplitTo for Vec { + fn split_to<'a>(&'a self, num: usize) -> Vec<&'a [T]> { + let part_len = self.len() / num; + let add_len = self.len() % num; + let mut result = Vec::<&'a [T]>::with_capacity(num); + + if 0 == part_len { + result.push(self); + return result; + } + for i in 0..num { + let size = if (num - 1) == i { + part_len + add_len + } else { + part_len + }; + let start = part_len * i; + result.push(&self[start..(start + size)]); + } + result + } +} + + fn process_files(files: &&[PathBuf]) { + if files.len() < 1 { + return; + } + + let start_file = files[0].to_str().unwrap(); + println!("-> start from \"{}\" ({} files)", start_file, files.len()); + let zip_file = fs::File::open(BASE_FILENAME).unwrap(); let zip_reader = io::BufReader::new(zip_file); let mut archive = zip::ZipArchive::new(zip_reader).unwrap(); @@ -259,6 +273,8 @@ fn process_files(files: &&[PathBuf]) { let mut outfile = fs::File::create(outfilename).unwrap(); data.write_pretty(&mut outfile, 1).unwrap(); }); + + println!("<- done {} files (from \"{}\")", files.len(), start_file); } fn main() -> Result<(), Box> { @@ -294,7 +310,7 @@ fn main() -> Result<(), Box> { println!("processing {} files with {} threads...", source_files.len(), rayon::current_num_threads()); // split vector and process its parts in parallel - split_vec(&source_files, rayon::current_num_threads()) + source_files.split_to(rayon::current_num_threads()) .par_iter() .for_each(process_files);