Morning improvements
This commit is contained in:
parent
3e9fba5303
commit
53fff3efaa
41 changed files with 2438 additions and 637 deletions
|
|
@ -138,6 +138,26 @@ impl GridIndex {
|
|||
result
|
||||
}
|
||||
|
||||
/// Count the number of row indices within the given bounds without allocating.
|
||||
/// O(grid cells in bounds) — much cheaper than query() for threshold decisions.
|
||||
pub fn count_in_bounds(&self, south: f64, west: f64, north: f64, east: f64) -> usize {
|
||||
let Some((row_min, row_max, col_min, col_max)) =
|
||||
self.clamp_bounds(south, west, north, east)
|
||||
else {
|
||||
return 0;
|
||||
};
|
||||
|
||||
let mut count = 0usize;
|
||||
for row in row_min..=row_max {
|
||||
let row_start = row * self.cols;
|
||||
for col in col_min..=col_max {
|
||||
let cell_idx = row_start + col;
|
||||
count += (self.offsets[cell_idx + 1] - self.offsets[cell_idx]) as usize;
|
||||
}
|
||||
}
|
||||
count
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn for_each_in_bounds(
|
||||
&self,
|
||||
|
|
@ -334,4 +354,27 @@ mod tests {
|
|||
let result = grid.query(-90.0, -180.0, 90.0, 180.0);
|
||||
assert_eq!(result.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn count_in_bounds_matches_query_len() {
|
||||
let lat = vec![51.5_f32, 51.6, 51.7, 52.0];
|
||||
let lon = vec![-0.1_f32, -0.1, -0.1, -0.1];
|
||||
let grid = GridIndex::build(&lat, &lon, 0.01);
|
||||
|
||||
let bounds = (51.4, -0.2, 51.8, 0.0);
|
||||
assert_eq!(
|
||||
grid.count_in_bounds(bounds.0, bounds.1, bounds.2, bounds.3),
|
||||
grid.query(bounds.0, bounds.1, bounds.2, bounds.3).len()
|
||||
);
|
||||
|
||||
// Full bounds
|
||||
let full = (50.0, -1.0, 53.0, 1.0);
|
||||
assert_eq!(
|
||||
grid.count_in_bounds(full.0, full.1, full.2, full.3),
|
||||
grid.query(full.0, full.1, full.2, full.3).len()
|
||||
);
|
||||
|
||||
// Empty bounds
|
||||
assert_eq!(grid.count_in_bounds(0.0, 0.0, 1.0, 1.0), 0);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue