Make Archive::remove parallel; Add Archive::pop method;

This commit is contained in:
Nathan Fisher 2023-04-10 19:17:26 -04:00
parent 43d8c82c08
commit 00683722d6
2 changed files with 13 additions and 2 deletions

View File

@ -4,7 +4,7 @@ use std::{
path::PathBuf, path::PathBuf,
}; };
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator}; use rayon::prelude::{IntoParallelRefIterator, ParallelIterator, IndexedParallelIterator};
mod error; mod error;
mod header; mod header;
@ -143,7 +143,7 @@ impl Archive {
pub fn remove(&mut self, filename: &str) -> Result<bool, Error> { pub fn remove(&mut self, filename: &str) -> Result<bool, Error> {
let mut name = [0u8; 100]; let mut name = [0u8; 100];
name[..filename.len()].copy_from_slice(filename.as_bytes()); 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); self.nodes.remove(*i);
return Ok(true); return Ok(true);
} }
@ -166,6 +166,17 @@ impl Archive {
x.header.file_path() == Ok(PathBuf::from(filename)) x.header.file_path() == Ok(PathBuf::from(filename))
}).cloned() }).cloned()
} }
pub fn pop(&mut self, filename: &str) -> Option<Node> {
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)] #[cfg(test)]

Binary file not shown.