-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ObjectRef combinator to build tree referencing objects elsewhere. (…
…#178) - [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. --- As discussed in georust/geo#1246 (comment)
- Loading branch information
1 parent
7634435
commit f81b9a0
Showing
3 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use crate::envelope::Envelope; | ||
use crate::object::PointDistance; | ||
use crate::{object::RTreeObject, point::Point}; | ||
use core::ops::Deref; | ||
|
||
/// An [RTreeObject] that is a possibly short-lived reference to another object. | ||
/// | ||
/// Sometimes it can be useful to build an [RTree] that does not own its constituent | ||
/// objects but references them from elsewhere. Wrapping the bare references with this | ||
/// combinator makes this possible. | ||
/// | ||
/// **Note:** the wrapper implements [RTreeObject] and referenced object `T` can be | ||
/// accessed via an implementation of `Deref<Target=T>`. | ||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
pub struct ObjectRef<'a, T: RTreeObject> { | ||
inner: &'a T, | ||
} | ||
|
||
impl<'a, T: RTreeObject> RTreeObject for ObjectRef<'a, T> { | ||
type Envelope = T::Envelope; | ||
|
||
fn envelope(&self) -> Self::Envelope { | ||
self.inner.envelope() | ||
} | ||
} | ||
|
||
impl<'a, T: PointDistance> PointDistance for ObjectRef<'a, T> { | ||
fn distance_2( | ||
&self, | ||
point: &<Self::Envelope as Envelope>::Point, | ||
) -> <<Self::Envelope as Envelope>::Point as Point>::Scalar { | ||
self.inner.distance_2(point) | ||
} | ||
|
||
fn contains_point(&self, p: &<Self::Envelope as Envelope>::Point) -> bool { | ||
self.inner.contains_point(p) | ||
} | ||
|
||
fn distance_2_if_less_or_equal( | ||
&self, | ||
point: &<Self::Envelope as Envelope>::Point, | ||
max_distance_2: <<Self::Envelope as Envelope>::Point as Point>::Scalar, | ||
) -> Option<<<Self::Envelope as Envelope>::Point as Point>::Scalar> { | ||
self.inner | ||
.distance_2_if_less_or_equal(point, max_distance_2) | ||
} | ||
} | ||
|
||
impl<'a, T: RTreeObject> ObjectRef<'a, T> { | ||
/// Create a new [ObjectRef] struct using the object. | ||
pub fn new(inner: &'a T) -> Self { | ||
Self { inner } | ||
} | ||
} | ||
|
||
impl<'a, T: RTreeObject> Deref for ObjectRef<'a, T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.inner | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters