Skip to content

Commit

Permalink
Make RTree methods take owned values where it makes sense (#189)
Browse files Browse the repository at this point in the history
- [X] I agree to follow the project's [code of
conduct](https://github.com/georust/geo/blob/master/CODE_OF_CONDUCT.md).
- [X] I added an entry to `rstar/CHANGELOG.md` if knowledge of this
change could be valuable to users.
---

Fixes #188.

This makes the API nicer, and I don't think I touched anything low-level
enough for it to have performance implications.
  • Loading branch information
kristoff3r authored Dec 29, 2024
1 parent c1b704d commit f718353
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 88 deletions.
3 changes: 3 additions & 0 deletions rstar/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
## Added
- Added missing re-exports of nearest neighbor iterators so they can be named in downstream crates. ([PR](https://github.com/georust/rstar/pull/186))

## Changed
- Made `RStar` methods take `Point` and `Envelope` as owned values where it makes sense ([PR](https://github.com/georust/rstar/pull/189))

# 0.12.2

## Changed
Expand Down
6 changes: 3 additions & 3 deletions rstar/src/algorithm/iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,10 @@ mod test {

let query_points = create_random_points(20, SEED_1);

for p in &query_points {
for p in query_points {
let contained_sequential: Vec<_> = rectangles
.iter()
.filter(|rectangle| rectangle.envelope().contains_point(p))
.filter(|rectangle| rectangle.envelope().contains_point(&p))
.cloned()
.collect();

Expand All @@ -317,7 +317,7 @@ mod test {
.collect();
let len = contained_in_envelope.len();
assert!(10 < len && len < 90, "unexpected point distribution");
let located: Vec<_> = tree.locate_in_envelope(&envelope).cloned().collect();
let located: Vec<_> = tree.locate_in_envelope(envelope).cloned().collect();
assert_eq!(len, located.len());
for point in &contained_in_envelope {
assert!(located.contains(point));
Expand Down
16 changes: 8 additions & 8 deletions rstar/src/algorithm/nearest_neighbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ where
ref mut nodes,
ref query_point,
} = self;
nodes.extend(children.iter().map(|child| {
nodes.extend(children.iter().map(|child: &RTreeNode<T>| {
let distance = match child {
RTreeNode::Parent(ref data) => data.envelope.distance_2(query_point),
RTreeNode::Leaf(ref t) => t.distance_2(query_point),
Expand Down Expand Up @@ -331,7 +331,7 @@ mod test {
#[test]
fn test_nearest_neighbor_empty() {
let tree: RTree<[f32; 2]> = RTree::new();
assert!(tree.nearest_neighbor(&[0.0, 213.0]).is_none());
assert!(tree.nearest_neighbor([0.0, 213.0]).is_none());
}

#[test]
Expand All @@ -340,7 +340,7 @@ mod test {
let tree = RTree::bulk_load(points.clone());

let sample_points = create_random_points(100, SEED_2);
for sample_point in &sample_points {
for sample_point in sample_points {
let mut nearest = None;
let mut closest_dist = f64::INFINITY;
for point in &points {
Expand Down Expand Up @@ -387,10 +387,10 @@ mod test {
let tree = RTree::bulk_load(points.clone());

let sample_points = create_random_points(50, SEED_2);
for sample_point in &sample_points {
for sample_point in sample_points {
points.sort_by(|r, l| {
r.distance_2(sample_point)
.partial_cmp(&l.distance_2(sample_point))
r.distance_2(&sample_point)
.partial_cmp(&l.distance_2(&sample_point))
.unwrap()
});
let collected: Vec<_> = tree.nearest_neighbor_iter(sample_point).cloned().collect();
Expand All @@ -404,10 +404,10 @@ mod test {
let tree = RTree::bulk_load(points);

let sample_points = create_random_points(50, SEED_1);
for sample_point in &sample_points {
for sample_point in sample_points {
let mut last_distance = 0.0;
for (point, distance) in tree.nearest_neighbor_iter_with_distance_2(sample_point) {
assert_eq!(point.distance_2(sample_point), distance);
assert_eq!(point.distance_2(&sample_point), distance);
assert!(last_distance < distance);
last_distance = distance;
}
Expand Down
8 changes: 4 additions & 4 deletions rstar/src/algorithm/removal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,14 @@ mod test {
let later_insertions = create_random_points(SIZE, SEED_2);
let mut tree = RTree::bulk_load(points.clone());
for (point_to_remove, point_to_add) in points.iter().zip(later_insertions.iter()) {
assert!(tree.remove_at_point(point_to_remove).is_some());
assert!(tree.remove_at_point(*point_to_remove).is_some());
tree.insert(*point_to_add);
}
assert_eq!(tree.size(), SIZE);
assert!(points.iter().all(|p| !tree.contains(p)));
assert!(later_insertions.iter().all(|p| tree.contains(p)));
for point in &later_insertions {
assert!(tree.remove_at_point(point).is_some());
assert!(tree.remove_at_point(*point).is_some());
}
assert_eq!(tree.size(), 0);
}
Expand Down Expand Up @@ -308,10 +308,10 @@ mod test {
fn test_remove_at_point() {
let points = create_random_points(1000, SEED_1);
let mut tree = RTree::bulk_load(points.clone());
for point in &points {
for point in points {
let size_before_removal = tree.size();
assert!(tree.remove_at_point(point).is_some());
assert!(tree.remove_at_point(&[1000.0, 1000.0]).is_none());
assert!(tree.remove_at_point([1000.0, 1000.0]).is_none());
assert_eq!(size_before_removal - 1, tree.size());
}
}
Expand Down
2 changes: 1 addition & 1 deletion rstar/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
//!
//! rtree.insert(mint_point2);
//!
//! assert_eq!(rtree.nearest_neighbor(&mint_point1), Some(&mint_point2));
//! assert_eq!(rtree.nearest_neighbor(mint_point1), Some(&mint_point2));
//! ```
use crate::{Point, RTreeNum};
Expand Down
6 changes: 3 additions & 3 deletions rstar/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ use crate::point::{Point, PointExt};
///
/// // Now we are ready to ask some questions!
/// let envelope = AABB::from_point([0.5, 0.5]);
/// let likely_sarah_croft = tree.locate_in_envelope(&envelope).next();
/// let likely_sarah_croft = tree.locate_in_envelope(envelope).next();
/// println!("Found {:?} lurking around at (0.5, 0.5)!", likely_sarah_croft.unwrap().name);
/// # assert!(likely_sarah_croft.is_some());
///
/// let unit_square = AABB::from_corners([-1.0, -1.0], [1., 1.]);
/// for player in tree.locate_in_envelope(&unit_square) {
/// for player in tree.locate_in_envelope(unit_square) {
/// println!("And here is {:?} spelunking in the unit square.", player.name);
/// }
/// # assert_eq!(tree.locate_in_envelope(&unit_square).count(), 2);
/// # assert_eq!(tree.locate_in_envelope(unit_square).count(), 2);
/// ```
pub trait RTreeObject {
/// The object's envelope type. Usually, [AABB] will be the right choice.
Expand Down
2 changes: 1 addition & 1 deletion rstar/src/primitives/cached_envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ mod test {
));
let my_location = [0.0, 0.0];
// Now find the closest line
let place = lines.nearest_neighbor(&my_location).unwrap();
let place = lines.nearest_neighbor(my_location).unwrap();

assert_eq!(place.data, "Line A");
}
Expand Down
4 changes: 2 additions & 2 deletions rstar/src/primitives/geom_with_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{object::RTreeObject, point::Point};
/// let my_location = [0.0, 0.0];
///
/// // Now find the closest restaurant!
/// let place = restaurants.nearest_neighbor(&my_location).unwrap();
/// let place = restaurants.nearest_neighbor(my_location).unwrap();
/// println!("Let's go to {}", place.data);
/// println!("It's really close, only {} miles", place.distance_2(&my_location));
/// ```
Expand Down Expand Up @@ -129,7 +129,7 @@ mod test {
));
let my_location = [0.0, 0.0];
// Now find the closest line
let place = lines.nearest_neighbor(&my_location).unwrap();
let place = lines.nearest_neighbor(my_location).unwrap();

assert_eq!(place.data, "Line A");
}
Expand Down
2 changes: 1 addition & 1 deletion rstar/src/primitives/point_with_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{Point, PointDistance, RTreeObject, AABB};
/// let my_location = [0.0, 0.0];
///
/// // Now find the closest restaurant!
/// let place = restaurants.nearest_neighbor(&my_location).unwrap();
/// let place = restaurants.nearest_neighbor(my_location).unwrap();
/// println!("Let's go to {}", place.data);
/// println!("It's really close, only {} miles", place.distance_2(&my_location))
/// ```
Expand Down
Loading

0 comments on commit f718353

Please sign in to comment.