trait SplitTo

This commit is contained in:
Dmitry Belyaev 2022-08-25 15:48:52 +03:00
parent ab9816ee97
commit 5cf4cea9b0
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3

View File

@ -216,29 +216,43 @@ fn parse_file(file: impl io::Read) -> Result<json::JsonValue, Box<dyn std::error
Ok(ctx.data.clone())
}
// split slice to a vector of slices
fn split_vec<'a, T>(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<T> {
fn split_to<'a>(&'a self, num: usize) -> Vec<&'a [T]>;
}
impl<T> SplitTo<T> for Vec<T> {
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<dyn std::error::Error>> {
@ -294,7 +310,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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);