1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
pub mod metrics; use std::borrow::Borrow; use std::collections::{hash_map, HashMap}; use std::fmt::{self, Debug, Formatter}; use std::iter::Extend; use std::default::Default; /// A trait for a *metric* (distance function). /// /// Implementations should follow the metric axioms: /// /// * **Zero**: `distance(a, b) == 0` if and only if `a == b` /// * **Symmetry**: `distance(a, b) == distance(b, a)` /// * **Triangle inequality**: `distance(a, c) <= distance(a, b) + distance(b, c)` /// /// If any of these rules are broken, then the BK-tree may give unexpected /// results. pub trait Metric<K: ?Sized> { fn distance(&self, a: &K, b: &K) -> u64; } /// A node within the [BK-tree](https://en.wikipedia.org/wiki/BK-tree). pub struct BKNode<K> { /// The key determining the node. pub key: K, /// A hash-map of children, indexed by their distance from this node based /// on the metric being used by the tree. pub children: HashMap<u64, BKNode<K>>, } impl<K> BKNode<K> { /// Constructs a new `BKNode<K>`. pub fn new(key: K) -> BKNode<K> { BKNode { key: key, children: HashMap::new(), } } /// Add a child to the node. /// /// Given the distance from this node's key, add the given key as a child /// node. *Warning:* this does not test the invariant that the distance as /// measured by the tree between this node's key and the provided key /// actually matches the distance passed in. /// /// # Examples /// /// ``` /// use bk_tree::BKNode; /// /// let mut foo = BKNode::new("foo"); /// foo.add_child(1, "fop"); /// ``` pub fn add_child(&mut self, distance: u64, key: K) { self.children.insert(distance, BKNode::new(key)); } } impl<K> Debug for BKNode<K> where K: Debug { fn fmt(&self, f: &mut Formatter) -> fmt::Result { f.debug_map().entry(&self.key, &self.children).finish() } } /// A representation of a [BK-tree](https://en.wikipedia.org/wiki/BK-tree). #[derive(Debug)] pub struct BKTree<K, M = metrics::Levenshtein> { /// The root node. May be empty if nothing has been put in the tree yet. pub root: Option<BKNode<K>>, /// The metric being used to determine the distance between nodes on the /// tree. metric: M, } impl<K, M> BKTree<K, M> where M: Metric<K> { /// Constructs a new `BKTree<K>` using the provided metric. /// /// Note that we make no assumptions about the metric function provided. /// *Ideally* it is actually a /// [valid metric](https://en.wikipedia.org/wiki/Metric_(mathematics)), /// but you may choose to use one that is not technically a valid metric. /// If you do not use a valid metric, however, you may find that the tree /// behaves confusingly for some values. /// /// # Examples /// /// ``` /// use bk_tree::{BKTree, metrics}; /// /// let tree: BKTree<&str> = BKTree::new(metrics::Levenshtein); /// ``` pub fn new(metric: M) -> BKTree<K, M> { BKTree { root: None, metric: metric, } } /// Adds a key to the tree. /// /// If the tree is empty, this simply sets the root to /// `Some(BKNode::new(key))`. Otherwise, we iterate downwards through the /// tree until we see a node that does not have a child with the same /// distance. If we encounter a node that is exactly the same distance from /// the root node, then the new key is the same as that node's key and so we /// do nothing. **Note**: This means that if your metric allows for unequal /// keys to return 0, you will see improper behavior! /// /// # Examples /// /// ``` /// use bk_tree::{BKTree, metrics}; /// /// let mut tree: BKTree<&str> = BKTree::new(metrics::Levenshtein); /// /// tree.add("foo"); /// tree.add("bar"); /// ``` pub fn add(&mut self, key: K) { match self.root { Some(ref mut root) => { let mut cur_node = root; let mut cur_dist = self.metric.distance(&cur_node.key, &key); while cur_node.children.contains_key(&cur_dist) && cur_dist > 0 { // We have to do some moving around here to safely get the // child corresponding to the current distance away without // accidentally trying to mutate the wrong thing. // let current = cur_node; let next_node = current.children.get_mut(&cur_dist).unwrap(); cur_node = next_node; cur_dist = self.metric.distance(&cur_node.key, &key); } cur_node.add_child(cur_dist, key); } None => { self.root = Some(BKNode::new(key)); } } } /// Searches for a key in the BK-tree given a certain tolerance. /// /// This traverses the tree searching for all keys with distance within /// `tolerance` of of the key provided. The tolerance may be zero, in which /// case this searches for exact matches. The results are returned as an /// iterator of `(distance, key)` pairs. /// /// *Note:* There is no guarantee on the order of elements yielded by the /// iterator. The elements returned may or may not be sorted in terms of /// distance from the provided key. /// /// # Examples /// ``` /// use bk_tree::{BKTree, metrics}; /// /// let mut tree: BKTree<&str> = BKTree::new(metrics::Levenshtein); /// /// tree.add("foo"); /// tree.add("fop"); /// tree.add("bar"); /// /// assert_eq!(tree.find("foo", 0).collect::<Vec<_>>(), vec![(0, &"foo")]); /// assert_eq!(tree.find("foo", 1).collect::<Vec<_>>(), vec![(0, &"foo"), (1, &"fop")]); /// assert!(tree.find("foz", 0).next().is_none()); /// ``` pub fn find<'a, 'q, Q: ?Sized>(&'a self, key: &'q Q, tolerance: u64) -> Find<'a, 'q, K, Q, M> where K: Borrow<Q>, M: Metric<Q> { Find { root: self.root.as_ref(), stack: Vec::new(), tolerance: tolerance, metric: &self.metric, key: key, } } /// Searches for an exact match in the tree. /// /// This is equivalent to calling `find` with a tolerance of 0, then picking /// out the first result. /// /// # Examples /// ``` /// use bk_tree::{BKTree, metrics}; /// /// let mut tree: BKTree<&str> = BKTree::new(metrics::Levenshtein); /// /// tree.add("foo"); /// tree.add("fop"); /// tree.add("bar"); /// /// assert_eq!(tree.find_exact("foz"), None); /// assert_eq!(tree.find_exact("foo"), Some(&"foo")); /// ``` pub fn find_exact<Q: ?Sized>(&self, key: &Q) -> Option<&K> where K: Borrow<Q>, M: Metric<Q> { self.find(key, 0).next().map(|(_, found_key)| found_key) } } impl<K, M: Metric<K>> Extend<K> for BKTree<K, M> { /// Adds multiple keys to the tree. /// /// Given an iterator with items of type `K`, this method simply adds every /// item to the tree. /// /// # Examples /// /// ``` /// use bk_tree::{BKTree, metrics}; /// /// let mut tree: BKTree<&str> = BKTree::new(metrics::Levenshtein); /// /// tree.extend(vec!["foo", "bar"]); /// ``` fn extend<I: IntoIterator<Item = K>>(&mut self, keys: I) { for key in keys { self.add(key); } } } impl<K: AsRef<str>> Default for BKTree<K> { fn default() -> BKTree<K> { BKTree::new(metrics::Levenshtein) } } /// Iterator for the results of `BKTree::find`. pub struct Find<'a, 'q, K: 'a, Q: 'q + ?Sized, M: 'a> { /// Root node. root: Option<&'a BKNode<K>>, /// Iterator stack. Because of the inversion of control in play, we must /// implement the traversal using an explicit stack. stack: Vec<StackItem<'a, K>>, tolerance: u64, metric: &'a M, key: &'q Q, } /// An element of the iteration stack. struct StackItem<'a, K: 'a> { cur_dist: u64, children_iter: hash_map::Iter<'a, u64, BKNode<K>>, } /// Delayed action type. Because of Rust's borrowing rules, we can't inspect /// and modify the stack at the same time. We instead record the modification /// and apply it at the end of the procedure. enum StackAction<'a, K: 'a> { Push(&'a BKNode<K>), Pop, } impl<'a, 'q, K, Q: ?Sized, M> Iterator for Find<'a, 'q, K, Q, M> where K: Borrow<Q>, M: Metric<Q> { type Item = (u64, &'a K); fn next(&mut self) -> Option<(u64, &'a K)> { // Special case the root node if let Some(root) = self.root.take() { let cur_dist = self.metric.distance(self.key, root.key.borrow() as &Q); self.stack.push(StackItem { cur_dist: cur_dist, children_iter: root.children.iter(), }); if cur_dist <= self.tolerance { return Some((cur_dist, &root.key)); } } loop { let action = match self.stack.last_mut() { Some(stack_top) => { // Find the first child node within an appropriate distance let min_dist = stack_top.cur_dist.saturating_sub(self.tolerance); let max_dist = stack_top.cur_dist.saturating_add(self.tolerance); let mut action = StackAction::Pop; for (dist, child_node) in &mut stack_top.children_iter { if min_dist <= *dist && *dist <= max_dist { action = StackAction::Push(child_node); break; } } action }, None => return None, }; match action { StackAction::Push(child_node) => { // Push this child node onto the stack (to inspect later) let cur_dist = self.metric.distance(self.key, child_node.key.borrow() as &Q); self.stack.push(StackItem { cur_dist: cur_dist, children_iter: child_node.children.iter(), }); // If this node is also close enough to the key, yield it if cur_dist <= self.tolerance { return Some((cur_dist, &child_node.key)); } }, StackAction::Pop => { self.stack.pop(); }, } } } }