1
Fork 0

coverage: Simplify creation of sum counters

This commit is contained in:
Zalathar 2024-09-11 17:24:31 +10:00
parent a7a3595668
commit 2344133ba6

View file

@ -155,12 +155,14 @@ impl CoverageCounters {
BcbCounter::Expression { id } BcbCounter::Expression { id }
} }
/// Variant of `make_expression` that makes `lhs` optional and assumes [`Op::Add`]. /// Creates a counter that is the sum of the given counters.
/// ///
/// This is useful when using [`Iterator::fold`] to build an arbitrary-length sum. /// Returns `None` if the given list of counters was empty.
fn make_sum_expression(&mut self, lhs: Option<BcbCounter>, rhs: BcbCounter) -> BcbCounter { fn make_sum(&mut self, counters: &[BcbCounter]) -> Option<BcbCounter> {
let Some(lhs) = lhs else { return rhs }; counters
self.make_expression(lhs, Op::Add, rhs) .iter()
.copied()
.reduce(|accum, counter| self.make_expression(accum, Op::Add, counter))
} }
pub(super) fn num_counters(&self) -> usize { pub(super) fn num_counters(&self) -> usize {
@ -322,12 +324,9 @@ impl<'a> MakeBcbCounters<'a> {
.filter(|&to_bcb| to_bcb != expression_to_bcb) .filter(|&to_bcb| to_bcb != expression_to_bcb)
.map(|to_bcb| self.get_or_make_edge_counter(from_bcb, to_bcb)) .map(|to_bcb| self.get_or_make_edge_counter(from_bcb, to_bcb))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let sum_of_all_other_out_edges: BcbCounter = other_out_edge_counters let sum_of_all_other_out_edges: BcbCounter = self
.iter() .coverage_counters
.copied() .make_sum(&other_out_edge_counters)
.fold(None, |accum, edge_counter| {
Some(self.coverage_counters.make_sum_expression(accum, edge_counter))
})
.expect("there must be at least one other out-edge"); .expect("there must be at least one other out-edge");
// Now create an expression for the chosen edge, by taking the counter // Now create an expression for the chosen edge, by taking the counter
@ -380,12 +379,9 @@ impl<'a> MakeBcbCounters<'a> {
.copied() .copied()
.map(|from_bcb| self.get_or_make_edge_counter(from_bcb, bcb)) .map(|from_bcb| self.get_or_make_edge_counter(from_bcb, bcb))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let sum_of_in_edges: BcbCounter = in_edge_counters let sum_of_in_edges: BcbCounter = self
.iter() .coverage_counters
.copied() .make_sum(&in_edge_counters)
.fold(None, |accum, edge_counter| {
Some(self.coverage_counters.make_sum_expression(accum, edge_counter))
})
.expect("there must be at least one in-edge"); .expect("there must be at least one in-edge");
debug!("{bcb:?} gets a new counter (sum of predecessor counters): {sum_of_in_edges:?}"); debug!("{bcb:?} gets a new counter (sum of predecessor counters): {sum_of_in_edges:?}");