Skip to content

Commit

Permalink
Merge pull request #10 from TianyiShi2001/remove_by
Browse files Browse the repository at this point in the history
adds a generic remove_residues function (remove by predicate)
  • Loading branch information
douweschulte committed Jan 3, 2021
2 parents f3d5e4f + 2ba3415 commit 2bffce5
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/structs/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,19 @@ impl Chain {
}
}

pub fn remove_residues_by<F>(&mut self, predicate: F) where F: Fn(&Residue) -> bool {
let residues = std::mem::replace(&mut self.residues, Vec::default());
self.residues.extend(residues.into_iter().filter(|residue|!predicate(residue)));
}

/// Remove the Residue specified.
///
/// ## Arguments
/// * `index` - the index of the Residue to remove
///
/// ## Panics
/// It panics when the index is outside bounds.
pub fn remove_residue(&mut self, index: usize) {
pub fn remove_residue_by_id(&mut self, index: usize) {
self.residues.remove(index);
}

Expand All @@ -267,7 +272,7 @@ impl Chain {
.position(|a| a.serial_number() == serial_number);

if let Some(i) = index {
self.remove_residue(i);
self.remove_residue_by_id(i);
true
} else {
false
Expand All @@ -283,7 +288,7 @@ impl Chain {
let index = self.residues.iter().position(|a| a.id() == id);

if let Some(i) = index {
self.remove_residue(i);
self.remove_residue_by_id(i);
true
} else {
false
Expand Down

0 comments on commit 2bffce5

Please sign in to comment.