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
use crate::tree::Tree; /// A map implemented with prefix tree. #[derive(Debug, Clone, Default)] pub struct PrefixTreeMap<K, V> { root: Tree<K, V>, length: usize, } impl<K: Eq + Clone, V> PrefixTreeMap<K, V> { /// Creates an empty `PrefixTreeMap`. /// /// # Examples /// /// ``` /// use prefix_tree::PrefixTreeMap; /// /// let mut map: PrefixTreeMap<u8, i32> = PrefixTreeMap::new(); /// ``` pub fn new() -> PrefixTreeMap<K, V> { PrefixTreeMap { root: Tree::empty(), length: 0, } } /// Returns `true` if the map contains a value for the specified key. /// /// # Examples /// /// ``` /// use prefix_tree::PrefixTreeMap; /// /// let mut map: PrefixTreeMap<u8, i32> = PrefixTreeMap::new(); /// map.insert("foo", 1); /// assert_eq!(map.contains_key("foo"), true); /// assert_eq!(map.contains_key("bar"), false); /// ``` pub fn contains_key<Q>(&self, key: Q) -> bool where Q: AsRef<[K]>, { self.get(key).is_some() } /// Clears the map, removing all key-value pairs. /// /// # Examples /// /// ``` /// use prefix_tree::PrefixTreeMap; /// /// let mut map: PrefixTreeMap<u8, i32> = PrefixTreeMap::new(); /// map.insert("foo", 1); /// map.clear(); /// assert!(map.is_empty()); /// ``` pub fn clear(&mut self) { *self = PrefixTreeMap::new(); } /// Returns a reference to the value corresponding to the key. /// /// # Examples /// /// ``` /// use prefix_tree::PrefixTreeMap; /// /// let mut map: PrefixTreeMap<u8, i32> = PrefixTreeMap::new(); /// map.insert("foo", 1); /// assert_eq!(map.get("foo"), Some(&1)); /// assert_eq!(map.get("bar"), None); /// ``` pub fn get<Q>(&self, key: Q) -> Option<&V> where Q: AsRef<[K]>, { self.root.find(key.as_ref()).and_then(|x| x.value.as_ref()) } /// Returns a mutable reference to the value corresponding to the key. /// /// # Examples /// /// ``` /// use prefix_tree::PrefixTreeMap; /// /// let mut map: PrefixTreeMap<u8, i32> = PrefixTreeMap::new(); /// map.insert("foo", 1); /// if let Some(x) = map.get_mut("foo") { /// *x = 2; /// } /// assert_eq!(map.get("foo"), Some(&2)); /// ``` pub fn get_mut<Q>(&mut self, key: Q) -> Option<&mut V> where Q: AsRef<[K]>, { self.root .find_mut(key.as_ref()) .and_then(|x| x.value.as_mut()) } /// Inserts a key-value pair into the map. /// /// # Examples /// /// ``` /// use prefix_tree::PrefixTreeMap; /// /// let mut map: PrefixTreeMap<u8, i32> = PrefixTreeMap::new(); /// assert_eq!(map.insert("a", 42), None); /// assert_eq!(map.is_empty(), false); /// assert_eq!(map.insert("a", 5), Some(42)); /// assert_eq!(map.get("a"), Some(&5)); /// ``` pub fn insert<Q>(&mut self, key: Q, value: V) -> Option<V> where Q: AsRef<[K]>, { let old = self.root.insert(key.as_ref(), value); if old.is_none() { self.length += 1; } old } /// Returns `true` if the map contains no elements. /// /// # Examples /// /// ``` /// use prefix_tree::PrefixTreeMap; /// /// let mut map: PrefixTreeMap<u8, i32> = PrefixTreeMap::new(); /// assert_eq!(map.is_empty(), true); /// map.insert("foo", 1); /// assert_eq!(map.is_empty(), false); /// ``` pub fn is_empty(&self) -> bool { self.len() == 0 } /// Returns the number of elements in the map. /// /// # Examples /// /// ``` /// use prefix_tree::PrefixTreeMap; /// /// let mut map: PrefixTreeMap<u8, i32> = PrefixTreeMap::new(); /// assert_eq!(map.len(), 0); /// map.insert("foo", 1); /// assert_eq!(map.len(), 1); /// ``` pub fn len(&self) -> usize { self.length } }