Skip to content

Commit

Permalink
Add close-enough compactor
Browse files Browse the repository at this point in the history
  • Loading branch information
JayKickliter committed Nov 24, 2023
1 parent c3fcd17 commit 5fdbb7c
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions hexit/src/elevation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,38 @@ impl Compactor<Elevation> for ReductionCompactor {
}
}
}

pub struct CloseEnoughCompactor {
// Maximum differance between min and max child elevations
// allowable for a cell to be coalesced.
pub tolerance: i16,
}

impl Compactor<Elevation> for CloseEnoughCompactor {
fn compact(&mut self, _cell: Cell, children: [Option<&Elevation>; 7]) -> Option<Elevation> {
if let [Some(v0), Some(v1), Some(v2), Some(v3), Some(v4), Some(v5), Some(v6)] = children {
let mut n_min = i16::MAX;
let mut n_sum = 0;
let mut n_max = i16::MIN;
let mut n_n = 0;
for Elevation { min, sum, max, n } in [v0, v1, v2, v3, v4, v5, v6] {
n_min = i16::min(n_min, *min);
n_sum += sum;
n_max = i16::max(n_max, *max);
n_n += n;
}
if (n_max - n_min) <= self.tolerance {
Some(Elevation {
min: n_min,
sum: n_sum,
max: n_max,
n: n_n,
})
} else {
None
}
} else {
None
}
}
}

0 comments on commit 5fdbb7c

Please sign in to comment.