Skip to content

Commit

Permalink
Remove subtract()
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamtaranto committed Oct 12, 2024
1 parent 85a32f5 commit c4ce905
Showing 1 changed file with 0 additions and 64 deletions.
64 changes: 0 additions & 64 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,70 +846,6 @@ impl KmerCountTable {

Ok((total_added, new_keys))
}

/// Subtract counts from another KmerCountTable from this one.
///
/// # Arguments
///
/// * `other` - The KmerCountTable to subtract
///
/// # Returns
///
/// Returns a PyResult with the number of k-mer counts removed from the table
#[pyo3(signature = (other))]
pub fn subtract(&mut self, other: &KmerCountTable) -> PyResult<u64> {
// Check if ksizes match
if self.ksize != other.ksize {
return Err(PyValueError::new_err(
"KmerCountTables must have the same ksize",
));
}

// Use atomic counter for thread-safe updates
let total_counts_removed = AtomicU64::new(0);

// Create a vector to store updates
let updates: Vec<_> = other
.counts
.par_iter()
.filter_map(|(&hash, &count)| {
// Only include hashes that exist in self.counts
self.counts
.get(&hash)
.map(|&self_count| (hash, count, self_count))
})
.collect();

// Apply updates sequentially
for (hash, other_count, self_count) in updates {
let new_count = if self_count > other_count {
self_count - other_count
} else {
0
};

let removed = self_count - new_count;
total_counts_removed.fetch_add(removed, Ordering::Relaxed);

if new_count == 0 {
self.counts.remove(&hash);
} else {
self.counts.insert(hash, new_count);
}
}

// Update consumed bases
// Ensure consumed doesn't go negative
self.consumed = self.consumed.saturating_sub(other.consumed);

// Get final count of removed k-mers
let total_removed = total_counts_removed.load(Ordering::Relaxed);

// Print summary
println!("Removed {} k-mer counts from the table", total_removed);

Ok(total_removed)
}
}

#[pyclass]
Expand Down

0 comments on commit c4ce905

Please sign in to comment.