|
1 | | -use std::fmt::Debug; |
| 1 | +use super::leaf::Leaf; |
| 2 | +use super::node::Node; |
2 | 3 |
|
3 | | -/// Represent an item added to the regex radix tree |
4 | | -pub trait NodeItem: Debug + Clone + Send + Sync + 'static { |
5 | | - /// Return the regex used by this item |
| 4 | +#[derive(Debug)] |
| 5 | +pub enum Item<V> { |
| 6 | + Empty(bool), |
| 7 | + Node(Node<V>), |
| 8 | + Leaf(Leaf<V>), |
| 9 | +} |
| 10 | + |
| 11 | +impl<V> Clone for Item<V> |
| 12 | +where |
| 13 | + V: Clone, |
| 14 | +{ |
| 15 | + fn clone(&self) -> Self { |
| 16 | + match self { |
| 17 | + Item::Empty(ignore_case) => Item::Empty(*ignore_case), |
| 18 | + Item::Node(node) => Item::Node(node.clone()), |
| 19 | + Item::Leaf(leaf) => Item::Leaf(leaf.clone()), |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +impl<V> Item<V> { |
| 25 | + /// Insert a new item into this node |
| 26 | + pub fn insert(self, regex: &str, id: String, item: V) -> Item<V> { |
| 27 | + match self { |
| 28 | + Item::Empty(ignore_case) => Item::Leaf(Leaf::new(regex, id, item, ignore_case)), |
| 29 | + Item::Node(node) => node.insert(regex, id, item), |
| 30 | + Item::Leaf(leaf) => leaf.insert(regex, id, item), |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + /// Find values associated to this haystack |
| 35 | + pub fn find(&self, haystack: &str) -> Vec<&V> { |
| 36 | + match self { |
| 37 | + Item::Empty(_) => Vec::new(), |
| 38 | + Item::Node(node) => node.find(haystack), |
| 39 | + Item::Leaf(leaf) => leaf.find(haystack), |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + pub fn get(&self, regex: &str) -> Vec<&V> { |
| 44 | + match self { |
| 45 | + Item::Empty(_) => Vec::new(), |
| 46 | + Item::Node(node) => node.get(regex), |
| 47 | + Item::Leaf(leaf) => leaf.get(regex), |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + pub fn get_mut(&mut self, regex: &str) -> Vec<&mut V> { |
| 52 | + match self { |
| 53 | + Item::Empty(_) => Vec::new(), |
| 54 | + Item::Node(node) => node.get_mut(regex), |
| 55 | + Item::Leaf(leaf) => leaf.get_mut(regex), |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /// Remove an item on this tree |
6 | 60 | /// |
7 | | - /// Regex should not contains `^` at the start and `$` at the end, those are inserted for you |
8 | | - /// automatically |
9 | | - fn regex(&self) -> &str; |
| 61 | + /// This method returns true if there is no more data so it can be cleaned up |
| 62 | + pub fn remove(self, id: &str) -> (Self, Option<V>) { |
| 63 | + match self { |
| 64 | + Item::Empty(_) => (self, None), |
| 65 | + Item::Node(node) => node.remove(id), |
| 66 | + Item::Leaf(leaf) => leaf.remove(id), |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /// Length of node |
| 71 | + pub fn len(&self) -> usize { |
| 72 | + match self { |
| 73 | + Item::Empty(_) => 0, |
| 74 | + Item::Node(node) => node.len(), |
| 75 | + Item::Leaf(leaf) => leaf.len(), |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + pub fn regex(&self) -> &str { |
| 80 | + match self { |
| 81 | + Item::Empty(_) => "", |
| 82 | + Item::Node(node) => node.regex(), |
| 83 | + Item::Leaf(leaf) => leaf.regex(), |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /// Cache current regex according to a limit and a level |
| 88 | + /// |
| 89 | + /// This method must return new limit of element cached (passed limit minus number of element cached) |
| 90 | + /// which allow other node to avoid caching extra node |
| 91 | + /// |
| 92 | + /// Implementation must not cache item if limit is equal to 0 |
| 93 | + /// Implementation must not cache item if not caching on the current node level |
| 94 | + /// |
| 95 | + /// Level argument allow to build cache on first level of the tree by priority |
| 96 | + /// Implementation must retain at which level this node is build and not do any caching |
| 97 | + /// if we are not on the current level |
| 98 | + pub fn cache(&mut self, left: u64, cache_level: u64, current_level: u64) -> u64 { |
| 99 | + if left <= 0 { |
| 100 | + return left; |
| 101 | + } |
| 102 | + |
| 103 | + if current_level > cache_level { |
| 104 | + return left; |
| 105 | + } |
10 | 106 |
|
11 | | - /// Should the regex be case insensitive |
12 | | - fn case_insensitive(&self) -> bool; |
| 107 | + match self { |
| 108 | + Item::Empty(_) => left, |
| 109 | + Item::Node(node) => node.cache(left, cache_level, current_level), |
| 110 | + Item::Leaf(leaf) => { |
| 111 | + if cache_level == current_level { |
| 112 | + leaf.cache(left) |
| 113 | + } else { |
| 114 | + left |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
13 | 119 | } |
0 commit comments