pub struct BKTreeMap<K, M, V> { /* private fields */ }
Expand description
a tree for quickly finding items separated by a small discrete distance
Implementations§
Source§impl<K, M, V> BKTreeMap<K, M, V>
impl<K, M, V> BKTreeMap<K, M, V>
Sourcepub fn append<M2>(&mut self, other: &mut BKTreeMap<K, M2, V>)where
M: DiscreteMetric<K, K>,
pub fn append<M2>(&mut self, other: &mut BKTreeMap<K, M2, V>)where
M: DiscreteMetric<K, K>,
moves all elements from other into self, leaving other empty. If a key from other is already present in self, the respective value from self will be overwritten with the respective value from other
Sourcepub fn close_iter<Q>(
&self,
key: Q,
maxdistance: usize,
) -> CloseMapIter<'_, K, M, Q, V> ⓘwhere
M: DiscreteMetric<K, Q>,
pub fn close_iter<Q>(
&self,
key: Q,
maxdistance: usize,
) -> CloseMapIter<'_, K, M, Q, V> ⓘwhere
M: DiscreteMetric<K, Q>,
gets the key value pairs whose distance is at most max distance from key
Sourcepub fn close_keys<Q>(
&self,
key: Q,
maxdistance: usize,
) -> CloseKeyIter<'_, K, M, Q, V> ⓘwhere
M: DiscreteMetric<K, Q>,
pub fn close_keys<Q>(
&self,
key: Q,
maxdistance: usize,
) -> CloseKeyIter<'_, K, M, Q, V> ⓘwhere
M: DiscreteMetric<K, Q>,
gets the keys whose distance is at most max distance from key
Sourcepub fn close_values<Q>(
&self,
key: Q,
maxdistance: usize,
) -> CloseValIter<'_, K, M, Q, V> ⓘwhere
M: DiscreteMetric<K, Q>,
pub fn close_values<Q>(
&self,
key: Q,
maxdistance: usize,
) -> CloseValIter<'_, K, M, Q, V> ⓘwhere
M: DiscreteMetric<K, Q>,
gets the values whose keys are at most max distance from key
Sourcepub fn close_sorted<'a, Q: ?Sized>(
&self,
key: &Q,
maxdistance: usize,
) -> Vec<(&K, &V, usize)>where
M: DiscreteMetric<K, Q>,
pub fn close_sorted<'a, Q: ?Sized>(
&self,
key: &Q,
maxdistance: usize,
) -> Vec<(&K, &V, usize)>where
M: DiscreteMetric<K, Q>,
returns the key value pairs at most maxdistance from the key, sorted by distance
Sourcepub fn close_sorted_mut<'a, Q: ?Sized>(
&mut self,
key: &Q,
maxdistance: usize,
) -> Vec<(&K, &mut V, usize)>where
M: DiscreteMetric<K, Q>,
pub fn close_sorted_mut<'a, Q: ?Sized>(
&mut self,
key: &Q,
maxdistance: usize,
) -> Vec<(&K, &mut V, usize)>where
M: DiscreteMetric<K, Q>,
returns the key value pairs at most maxdistance from the key, sorted by distance
Sourcepub fn contains_key<Q: ?Sized>(&self, key: &Q, maxdistance: usize) -> boolwhere
M: DiscreteMetric<K, Q>,
pub fn contains_key<Q: ?Sized>(&self, key: &Q, maxdistance: usize) -> boolwhere
M: DiscreteMetric<K, Q>,
tests if a key at most maxdistance from the given key is in the map
Sourcepub fn get<Q: ?Sized>(&self, key: &Q, maxdistance: usize) -> Option<(&V, usize)>where
M: DiscreteMetric<K, Q>,
pub fn get<Q: ?Sized>(&self, key: &Q, maxdistance: usize) -> Option<(&V, usize)>where
M: DiscreteMetric<K, Q>,
gets the value whose key is closest to the given key, or None if the map contains no key at most max distance from the given key. If there are multiple closest keys, exactly which is returned is unspecified
Examples found in repository?
1pub fn main(){
2 let mut tree=BKTreeMap::new(Levenshtein::new());
3 tree.insert("calculate","mathematics");
4 tree.insert("cat","pet");
5 tree.insert("kat","name");
6 tree.insert("hello","greeting");
7 tree.insert("hi","greeting");
8 tree.insert("linear","mathematics");
9
10 println!("{}",tree.get("calculator",2).map(|(s,_d)|*s).unwrap_or("not found"));
11 println!("{}",tree.get("hey",2).map(|(s,_d)|*s).unwrap_or("not found"));
12 println!("{}",tree.get("kate",2).map(|(s,_d)|*s).unwrap_or("not found"));
13 println!("{}",tree.get("line",2).map(|(s,_d)|*s).unwrap_or("not found"));
14 println!("{}",tree.get("serotonin",2).map(|(s,_d)|*s).unwrap_or("not found"));
15}
Sourcepub fn get_key_value<Q: ?Sized>(
&self,
key: &Q,
maxdistance: usize,
) -> Option<(&K, &V, usize)>where
M: DiscreteMetric<K, Q>,
pub fn get_key_value<Q: ?Sized>(
&self,
key: &Q,
maxdistance: usize,
) -> Option<(&K, &V, usize)>where
M: DiscreteMetric<K, Q>,
gets the key value pair and distance whose key is closest to the given key, or None if the map contains no key at most max distance from the given key. If there are multiple closest keys, exactly which is returned is unspecified
Sourcepub fn get_mut<Q: ?Sized>(
&mut self,
key: &Q,
maxdistance: usize,
) -> Option<(&mut V, usize)>where
M: DiscreteMetric<K, Q>,
pub fn get_mut<Q: ?Sized>(
&mut self,
key: &Q,
maxdistance: usize,
) -> Option<(&mut V, usize)>where
M: DiscreteMetric<K, Q>,
gets the value whose key is closest to the given key, or None if the map contains no key at most max distance from the given key. If there are multiple closest keys, exactly which is returned is unspecified
Sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>where
M: DiscreteMetric<K, K>,
pub fn insert(&mut self, key: K, value: V) -> Option<V>where
M: DiscreteMetric<K, K>,
inserts a key-value pair into the map, returning the previous value at that key, or None if there was no previous value. Keys are considered equal if the the distance between them is 0. If the old value is returned the key is not updated.
Examples found in repository?
1pub fn main(){
2 let mut tree=BKTreeMap::new(Levenshtein::new());
3 tree.insert("calculate","mathematics");
4 tree.insert("cat","pet");
5 tree.insert("kat","name");
6 tree.insert("hello","greeting");
7 tree.insert("hi","greeting");
8 tree.insert("linear","mathematics");
9
10 println!("{}",tree.get("calculator",2).map(|(s,_d)|*s).unwrap_or("not found"));
11 println!("{}",tree.get("hey",2).map(|(s,_d)|*s).unwrap_or("not found"));
12 println!("{}",tree.get("kate",2).map(|(s,_d)|*s).unwrap_or("not found"));
13 println!("{}",tree.get("line",2).map(|(s,_d)|*s).unwrap_or("not found"));
14 println!("{}",tree.get("serotonin",2).map(|(s,_d)|*s).unwrap_or("not found"));
15}
Sourcepub fn into_keys(self) -> IntoKeysIter<K, V> ⓘ
pub fn into_keys(self) -> IntoKeysIter<K, V> ⓘ
makes an iterator over the keys
Sourcepub fn into_values(self) -> IntoValsIter<K, V> ⓘ
pub fn into_values(self) -> IntoValsIter<K, V> ⓘ
makes an iterator over the values
Sourcepub fn iter_mut(&mut self) -> MapIterMut<'_, K, V> ⓘ
pub fn iter_mut(&mut self) -> MapIterMut<'_, K, V> ⓘ
makes an iterator over the mappings
Sourcepub fn metric(&self) -> &M
pub fn metric(&self) -> &M
references the metric. avoid modifying the metric in a way that changes the distances because that will most likely cause unspecified incorrect behavior
Sourcepub fn new(metric: M) -> Self
pub fn new(metric: M) -> Self
creates a new tree
Examples found in repository?
1pub fn main(){
2 let mut tree=BKTreeMap::new(Levenshtein::new());
3 tree.insert("calculate","mathematics");
4 tree.insert("cat","pet");
5 tree.insert("kat","name");
6 tree.insert("hello","greeting");
7 tree.insert("hi","greeting");
8 tree.insert("linear","mathematics");
9
10 println!("{}",tree.get("calculator",2).map(|(s,_d)|*s).unwrap_or("not found"));
11 println!("{}",tree.get("hey",2).map(|(s,_d)|*s).unwrap_or("not found"));
12 println!("{}",tree.get("kate",2).map(|(s,_d)|*s).unwrap_or("not found"));
13 println!("{}",tree.get("line",2).map(|(s,_d)|*s).unwrap_or("not found"));
14 println!("{}",tree.get("serotonin",2).map(|(s,_d)|*s).unwrap_or("not found"));
15}
Sourcepub fn remove<Q: ?Sized>(
&mut self,
key: &Q,
maxdistance: usize,
) -> Option<(V, usize)>where
M: DiscreteMetric<K, K> + DiscreteMetric<K, Q>,
pub fn remove<Q: ?Sized>(
&mut self,
key: &Q,
maxdistance: usize,
) -> Option<(V, usize)>where
M: DiscreteMetric<K, K> + DiscreteMetric<K, Q>,
removes the closest mapping whose key at most maxdistance from the given key. If there are multiple closest keys, exactly which is removed is unspecified. This particular tree type doesn’t allow super efficient removal, so try to avoid using too much.
Sourcepub fn remove_entry<Q: ?Sized>(
&mut self,
key: &Q,
maxdistance: usize,
) -> Option<(K, V, usize)>where
M: DiscreteMetric<K, K> + DiscreteMetric<K, Q>,
pub fn remove_entry<Q: ?Sized>(
&mut self,
key: &Q,
maxdistance: usize,
) -> Option<(K, V, usize)>where
M: DiscreteMetric<K, K> + DiscreteMetric<K, Q>,
removes the closest mapping whose key at most maxdistance from the given key. If there are multiple closest keys, exactly which is removed is unspecified. This particular tree type doesn’t allow super efficient removal, so try to avoid using too much.
Sourcepub fn retain<F: FnMut(&K, &mut V) -> bool>(&mut self, f: F)where
M: DiscreteMetric<K, K>,
pub fn retain<F: FnMut(&K, &mut V) -> bool>(&mut self, f: F)where
M: DiscreteMetric<K, K>,
removes all the mappings for which f returns false
Sourcepub fn values_mut(&mut self) -> ValIterMut<'_, K, V> ⓘ
pub fn values_mut(&mut self) -> ValIterMut<'_, K, V> ⓘ
makes an iterator over the values
Trait Implementations§
Source§impl<K, M: DiscreteMetric<K, K>, V> Extend<(K, V)> for BKTreeMap<K, M, V>
impl<K, M: DiscreteMetric<K, K>, V> Extend<(K, V)> for BKTreeMap<K, M, V>
Source§fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)