From 00683722d64a9c2f619e6c5378ab7b571b32ecc8 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Mon, 10 Apr 2023 19:17:26 -0400 Subject: [PATCH] Make Archive::remove parallel; Add Archive::pop method; --- src/tar/mod.rs | 15 +++++++++++++-- test/2.tar | Bin 10240 -> 10240 bytes 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/tar/mod.rs b/src/tar/mod.rs index 004b94e..f94798d 100644 --- a/src/tar/mod.rs +++ b/src/tar/mod.rs @@ -4,7 +4,7 @@ use std::{ path::PathBuf, }; -use rayon::prelude::{IntoParallelRefIterator, ParallelIterator}; +use rayon::prelude::{IntoParallelRefIterator, ParallelIterator, IndexedParallelIterator}; mod error; mod header; @@ -143,7 +143,7 @@ impl Archive { pub fn remove(&mut self, filename: &str) -> Result { let mut name = [0u8; 100]; name[..filename.len()].copy_from_slice(filename.as_bytes()); - if let Some(i) = &self.nodes.iter().position(|x| x.header.fname == name) { + if let Some(i) = &self.nodes.par_iter().position_any(|x| x.header.fname == name) { self.nodes.remove(*i); return Ok(true); } @@ -166,6 +166,17 @@ impl Archive { x.header.file_path() == Ok(PathBuf::from(filename)) }).cloned() } + + pub fn pop(&mut self, filename: &str) -> Option { + if let Some(i) = self.nodes.par_iter().position_any(|x| { + x.header.file_path() == Ok(PathBuf::from(filename)) + }) { + let node = self.nodes.get(i).cloned(); + self.nodes.remove(i); + return node; + } + None + } } #[cfg(test)] diff --git a/test/2.tar b/test/2.tar index 98902ce9c96775fa2ed4c0b8194684c56b3bd9ca..d37f5f88b97f1e407fb36b86a6fbcba352351702 100644 GIT binary patch delta 33 ocmZn&Xb70lBWP-9XkcJs#$aG*U~Xo_pkOdLkuha6BjbNH0Ge+I;s5{u delta 33 ocmZn&Xb70lBWPq|U~FV)#$aG*U~Xp2pkOdLkuha6BjbNH0Gg8s