pub struct Tree<Item: MetricSpace<Impl> + Clone, Impl = (), Ownership = Owned<()>> { /* private fields */ }Expand description
The VP-Tree.
Implementations§
Source§impl<Item: MetricSpace<Impl, UserData = ()> + Clone, Impl> Tree<Item, Impl, Owned<()>>
impl<Item: MetricSpace<Impl, UserData = ()> + Clone, Impl> Tree<Item, Impl, Owned<()>>
Sourcepub fn new(items: &[Item]) -> Self
pub fn new(items: &[Item]) -> Self
Creates a new tree from items. Maximum number of items is 2^31.
See Tree::new_with_user_data_owned.
Examples found in repository?
More examples
examples/radius_nn.rs (line 88)
82fn main() {
83 let points = vec![
84 PointN::new([2.0, 3.0]),
85 PointN::new([0.0, 1.0]),
86 PointN::new([4.0, 5.0]),
87 ];
88 let tree = vpsearch::Tree::new(&points);
89
90 // Search with a distance of 0, expect no points to be returned
91 let expected = HashSet::new();
92 let actual = tree.find_nearest_custom(
93 &PointN::new([1.0, 2.0]),
94 &(),
95 RadiusBasedNeighborhood::new(0.0f32),
96 );
97 assert_eq!(actual, expected);
98
99 // Search with a distance of 100, expect all points to be returned
100 let expected = [0, 1, 2].iter().copied().collect::<HashSet<usize>>();
101 let actual = tree.find_nearest_custom(
102 &PointN::new([1.0, 2.0]),
103 &(),
104 RadiusBasedNeighborhood::new(100.0f32),
105 );
106 assert_eq!(actual, expected);
107}examples/knn.rs (line 133)
127fn main() {
128 let points = vec![
129 PointN::new([2.0, 3.0]),
130 PointN::new([0.0, 1.0]),
131 PointN::new([4.0, 5.0]),
132 ];
133 let tree = vpsearch::Tree::new(&points);
134
135 // Search with a neigboord size of 1, expect a single points to be returned
136 let actual = tree.find_nearest_custom(
137 &PointN::new([1.0, 2.0]),
138 &(),
139 CountBasedNeighborhood::new(1),
140 );
141 assert_eq!(actual.len(), 1);
142
143 // Search with a neigboord size of 2, expect a two points to be returned
144 let expected = [0, 1].iter().copied().collect::<HashSet<usize>>();
145 let actual = tree.find_nearest_custom(
146 &PointN::new([1.0, 2.0]),
147 &(),
148 CountBasedNeighborhood::new(2),
149 );
150 assert_eq!(actual, expected);
151
152 // Search with a neigboord size of 10, expect all points to be returned
153 let expected = [0, 1, 2].iter().copied().collect::<HashSet<usize>>();
154 let actual = tree.find_nearest_custom(
155 &PointN::new([1.0, 2.0]),
156 &(),
157 CountBasedNeighborhood::new(10),
158 );
159 assert_eq!(actual, expected);
160}Source§impl<U, Impl, Item: MetricSpace<Impl, UserData = U> + Clone> Tree<Item, Impl, Owned<U>>
impl<U, Impl, Item: MetricSpace<Impl, UserData = U> + Clone> Tree<Item, Impl, Owned<U>>
Sourcepub fn find_nearest(&self, needle: &Item) -> (usize, Item::Distance)
pub fn find_nearest(&self, needle: &Item) -> (usize, Item::Distance)
Finds item closest to the given needle (that can be any item) and returns index of the item in items array from new().
Returns the index of the nearest item (index from the items slice passed to new()) found and the distance from the nearest item.
Examples found in repository?
More examples
Source§impl<Item: MetricSpace<Impl> + Clone, Impl> Tree<Item, Impl, Owned<Item::UserData>>
impl<Item: MetricSpace<Impl> + Clone, Impl> Tree<Item, Impl, Owned<Item::UserData>>
Sourcepub fn new_with_user_data_owned(
items: &[Item],
user_data: Item::UserData,
) -> Self
pub fn new_with_user_data_owned( items: &[Item], user_data: Item::UserData, ) -> Self
Create a Vantage Point tree for fast nearest neighbor search.
items— Array of items that will be searched.user_data— Reference to any object that is passed down toitem.distance()
Source§impl<Item: MetricSpace<Impl> + Clone, Impl> Tree<Item, Impl, ()>
impl<Item: MetricSpace<Impl> + Clone, Impl> Tree<Item, Impl, ()>
Sourcepub fn new_with_user_data_ref(
items: &[Item],
user_data: &Item::UserData,
) -> Self
pub fn new_with_user_data_ref( items: &[Item], user_data: &Item::UserData, ) -> Self
The tree doesn’t have to own the UserData. You can keep passing it to find_nearest().
pub fn find_nearest( &self, needle: &Item, user_data: &Item::UserData, ) -> (usize, Item::Distance)
Source§impl<Item: MetricSpace<Impl> + Clone, Ownership, Impl> Tree<Item, Impl, Ownership>
impl<Item: MetricSpace<Impl> + Clone, Ownership, Impl> Tree<Item, Impl, Ownership>
Sourcepub fn find_nearest_custom<ReturnBy: BestCandidate<Item, Impl>>(
&self,
needle: &Item,
user_data: &Item::UserData,
best_candidate: ReturnBy,
) -> ReturnBy::Output
pub fn find_nearest_custom<ReturnBy: BestCandidate<Item, Impl>>( &self, needle: &Item, user_data: &Item::UserData, best_candidate: ReturnBy, ) -> ReturnBy::Output
All the bells and whistles version. For best_candidate implement BestCandidate<Item, Impl> trait.
Examples found in repository?
examples/radius_nn.rs (lines 92-96)
82fn main() {
83 let points = vec![
84 PointN::new([2.0, 3.0]),
85 PointN::new([0.0, 1.0]),
86 PointN::new([4.0, 5.0]),
87 ];
88 let tree = vpsearch::Tree::new(&points);
89
90 // Search with a distance of 0, expect no points to be returned
91 let expected = HashSet::new();
92 let actual = tree.find_nearest_custom(
93 &PointN::new([1.0, 2.0]),
94 &(),
95 RadiusBasedNeighborhood::new(0.0f32),
96 );
97 assert_eq!(actual, expected);
98
99 // Search with a distance of 100, expect all points to be returned
100 let expected = [0, 1, 2].iter().copied().collect::<HashSet<usize>>();
101 let actual = tree.find_nearest_custom(
102 &PointN::new([1.0, 2.0]),
103 &(),
104 RadiusBasedNeighborhood::new(100.0f32),
105 );
106 assert_eq!(actual, expected);
107}More examples
examples/knn.rs (lines 136-140)
127fn main() {
128 let points = vec![
129 PointN::new([2.0, 3.0]),
130 PointN::new([0.0, 1.0]),
131 PointN::new([4.0, 5.0]),
132 ];
133 let tree = vpsearch::Tree::new(&points);
134
135 // Search with a neigboord size of 1, expect a single points to be returned
136 let actual = tree.find_nearest_custom(
137 &PointN::new([1.0, 2.0]),
138 &(),
139 CountBasedNeighborhood::new(1),
140 );
141 assert_eq!(actual.len(), 1);
142
143 // Search with a neigboord size of 2, expect a two points to be returned
144 let expected = [0, 1].iter().copied().collect::<HashSet<usize>>();
145 let actual = tree.find_nearest_custom(
146 &PointN::new([1.0, 2.0]),
147 &(),
148 CountBasedNeighborhood::new(2),
149 );
150 assert_eq!(actual, expected);
151
152 // Search with a neigboord size of 10, expect all points to be returned
153 let expected = [0, 1, 2].iter().copied().collect::<HashSet<usize>>();
154 let actual = tree.find_nearest_custom(
155 &PointN::new([1.0, 2.0]),
156 &(),
157 CountBasedNeighborhood::new(10),
158 );
159 assert_eq!(actual, expected);
160}Trait Implementations§
Auto Trait Implementations§
impl<Item, Impl, Ownership> Freeze for Tree<Item, Impl, Ownership>where
Ownership: Freeze,
impl<Item, Impl, Ownership> RefUnwindSafe for Tree<Item, Impl, Ownership>where
Ownership: RefUnwindSafe,
Item: RefUnwindSafe,
<Item as MetricSpace<Impl>>::Distance: RefUnwindSafe,
impl<Item, Impl, Ownership> Send for Tree<Item, Impl, Ownership>
impl<Item, Impl, Ownership> Sync for Tree<Item, Impl, Ownership>
impl<Item, Impl, Ownership> Unpin for Tree<Item, Impl, Ownership>
impl<Item, Impl, Ownership> UnwindSafe for Tree<Item, Impl, Ownership>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more