Skip to content

Commit

Permalink
bug: Fix order dependent value erasure.
Browse files Browse the repository at this point in the history
This fixes issue#32.
  • Loading branch information
shanecelis committed May 12, 2024
1 parent 4a334a1 commit 2cbf824
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/internal_data_structure/naive_trie/naive_trie_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ impl<'trie, Label: Ord, Value> NaiveTrie<Label, Value> {

pub fn push<Arr: Iterator<Item = Label>>(&'trie mut self, word: Arr, value: Value) {
let mut trie = self;
let mut value = Some(value);
let mut word = word.peekable();
while let Some(chr) = word.next() {
let res = trie
.children()
Expand All @@ -32,9 +30,7 @@ impl<'trie, Label: Ord, Value> NaiveTrie<Label, Value> {
};
}
Err(j) => {
let is_terminal = word.peek().is_none();
let child_trie =
Self::make_interm_or_leaf(chr, is_terminal.then(|| value.take().unwrap()));
let child_trie = Self::make_interm_or_leaf(chr, None);
trie = match trie {
NaiveTrie::Root(node) => {
node.children.insert(j, child_trie);
Expand All @@ -49,6 +45,10 @@ impl<'trie, Label: Ord, Value> NaiveTrie<Label, Value> {
}
};
}
match trie {
NaiveTrie::IntermOrLeaf(node) => node.value = Some(value),
_ => panic!("Unexpected type"),
}
}

pub fn children(&self) -> &[Self] {
Expand Down
25 changes: 25 additions & 0 deletions src/map/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,31 @@ mod search_tests {
let _ = trie.common_prefix_search::<String, _>("").next();
}

#[test]
fn insert_order_dependent() {
let trie = Trie::from_iter([("a", 0), ("app", 1), ("apple", 2)]);
let results: Vec<(String, &u8)> = trie.iter().collect();
assert_eq!(
results,
[
("a".to_string(), &0u8),
("app".to_string(), &1u8),
("apple".to_string(), &2u8)
]
);

let trie = Trie::from_iter([("a", 0), ("apple", 2), ("app", 1)]);
let results: Vec<(String, &u8)> = trie.iter().collect();
assert_eq!(
results,
[
("a".to_string(), &0u8),
("app".to_string(), &1u8),
("apple".to_string(), &2u8)
]
);
}

mod exact_match_tests {
macro_rules! parameterized_tests {
($($name:ident: $value:expr,)*) => {
Expand Down

0 comments on commit 2cbf824

Please sign in to comment.