Tree

Struct Tree 

Source
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<()>>

Source

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?
examples/orphan_rule.rs (line 17)
15fn main() {
16    let source_data = vec![vec![0; 64], vec![5; 64], vec![10; 64]];
17    let vp = vpsearch::Tree::new(&source_data);
18    let (index, dist) = vp.find_nearest(&vec![6; 64]);
19    println!("The element at {index} is the nearest, off by {dist}");
20}
More examples
Hide additional examples
examples/points.rs (line 22)
18fn main() {
19
20    let points = vec![Point{x:2.0,y:3.0}, Point{x:0.0,y:1.0}, Point{x:4.0,y:5.0}];
21
22    let vp = vpsearch::Tree::new(&points);
23
24    let (index, _) = vp.find_nearest(&Point{x:1.0,y:2.0});
25
26    println!("The nearest point is at ({}, {})", points[index].x, points[index].y);
27}
examples/newtype_ref.rs (line 20)
17fn main() {
18    let source_data = [[0; 64], [5; 64], [10; 64]];
19    let reference_data: Vec<_> = source_data.iter().map(LotsaDimensions).collect();
20    let vp = vpsearch::Tree::new(&reference_data);
21    let (index, dist) = vp.find_nearest(&LotsaDimensions(&[6; 64]));
22    println!("The element at {index} is the nearest, off by {dist}");
23}
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>>

Source

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?
examples/orphan_rule.rs (line 18)
15fn main() {
16    let source_data = vec![vec![0; 64], vec![5; 64], vec![10; 64]];
17    let vp = vpsearch::Tree::new(&source_data);
18    let (index, dist) = vp.find_nearest(&vec![6; 64]);
19    println!("The element at {index} is the nearest, off by {dist}");
20}
More examples
Hide additional examples
examples/points.rs (line 24)
18fn main() {
19
20    let points = vec![Point{x:2.0,y:3.0}, Point{x:0.0,y:1.0}, Point{x:4.0,y:5.0}];
21
22    let vp = vpsearch::Tree::new(&points);
23
24    let (index, _) = vp.find_nearest(&Point{x:1.0,y:2.0});
25
26    println!("The nearest point is at ({}, {})", points[index].x, points[index].y);
27}
examples/newtype_ref.rs (line 21)
17fn main() {
18    let source_data = [[0; 64], [5; 64], [10; 64]];
19    let reference_data: Vec<_> = source_data.iter().map(LotsaDimensions).collect();
20    let vp = vpsearch::Tree::new(&reference_data);
21    let (index, dist) = vp.find_nearest(&LotsaDimensions(&[6; 64]));
22    println!("The element at {index} is the nearest, off by {dist}");
23}
Source§

impl<Item: MetricSpace<Impl> + Clone, Impl> Tree<Item, Impl, Owned<Item::UserData>>

Source

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 to item.distance()
Source§

impl<Item: MetricSpace<Impl> + Clone, Impl> Tree<Item, Impl, ()>

Source

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().

Source

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>

Source

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
Hide additional 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§

Source§

impl<Item: Debug + Clone + MetricSpace<UserImpl>, UserImpl, Ownership> Debug for Tree<Item, UserImpl, Ownership>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

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>
where Ownership: Send, Item: Send, <Item as MetricSpace<Impl>>::Distance: Send,

§

impl<Item, Impl, Ownership> Sync for Tree<Item, Impl, Ownership>
where Ownership: Sync, Item: Sync, <Item as MetricSpace<Impl>>::Distance: Sync,

§

impl<Item, Impl, Ownership> Unpin for Tree<Item, Impl, Ownership>
where Ownership: Unpin, Item: Unpin, <Item as MetricSpace<Impl>>::Distance: Unpin,

§

impl<Item, Impl, Ownership> UnwindSafe for Tree<Item, Impl, Ownership>
where Ownership: UnwindSafe, Item: UnwindSafe, <Item as MetricSpace<Impl>>::Distance: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.