trie_map: rename prefix to key in some places

* In the get method, it makes more sense to call the parameter "key",
  because we are trying to match the prefixes against it, not using it
  as a prefix.
This commit is contained in:
Peter Cai 2021-04-05 16:51:01 +08:00
parent fc1f5a589e
commit 4ddf8c536a
2 changed files with 15 additions and 15 deletions

View file

@ -61,7 +61,7 @@ impl OverrideResolver {
self.respond_with_addr(question, addr) self.respond_with_addr(question, addr)
} else if let Some(addr) = self } else if let Some(addr) = self
.suffix_matches .suffix_matches
.get_prefix(name.chars().rev().collect::<String>()) .get_by_prefix(name.chars().rev().collect::<String>())
{ {
self.respond_with_addr(question, addr) self.respond_with_addr(question, addr)
} else { } else {

View file

@ -16,36 +16,36 @@ impl<T> TrieMapNode<T> {
fn traverse_trie_mut<'a, 'b>( fn traverse_trie_mut<'a, 'b>(
&'a mut self, &'a mut self,
prefix: &'b [u8], key: &'b [u8],
) -> (&'a mut TrieMapNode<T>, &'b [u8]) { ) -> (&'a mut TrieMapNode<T>, &'b [u8]) {
if prefix.len() == 0 { if key.len() == 0 {
return (self, prefix); return (self, key);
} }
if let Some(idx) = self.find_child(prefix[0]) { if let Some(idx) = self.find_child(key[0]) {
self.children[idx].traverse_trie_mut(&prefix[1..]) self.children[idx].traverse_trie_mut(&key[1..])
} else { } else {
(self, prefix) (self, key)
} }
} }
fn traverse_trie_for_value<'a, 'b>( fn traverse_trie_for_value<'a, 'b>(
&'a self, &'a self,
prefix: &'b [u8], key: &'b [u8],
mut last_value: Option<&'a T>, mut last_value: Option<&'a T>,
) -> (&'a TrieMapNode<T>, Option<&'a T>, &'b [u8]) { ) -> (&'a TrieMapNode<T>, Option<&'a T>, &'b [u8]) {
if self.value.is_some() { if self.value.is_some() {
last_value = self.value.as_ref(); last_value = self.value.as_ref();
} }
if prefix.len() == 0 { if key.len() == 0 {
return (self, last_value, prefix); return (self, last_value, key);
} }
if let Some(idx) = self.find_child(prefix[0]) { if let Some(idx) = self.find_child(key[0]) {
self.children[idx].traverse_trie_for_value(&prefix[1..], last_value) self.children[idx].traverse_trie_for_value(&key[1..], last_value)
} else { } else {
(self, last_value, prefix) (self, last_value, key)
} }
} }
} }
@ -84,8 +84,8 @@ impl<T> TrieMap<T> {
node.value = Some(value.into()); node.value = Some(value.into());
} }
pub fn get_prefix(&self, prefix: impl AsRef<[u8]>) -> Option<&T> { pub fn get_by_prefix(&self, key: impl AsRef<[u8]>) -> Option<&T> {
let (_, value, _) = self.root.traverse_trie_for_value(prefix.as_ref(), None); let (_, value, _) = self.root.traverse_trie_for_value(key.as_ref(), None);
value value
} }
} }