laysakura / trie-rs

Memory efficient trie (prefix tree) library based on LOUDS
https://crates.io/crates/trie-rs
Apache License 2.0
90 stars 10 forks source link

Map values dropped if longer path is added first #32

Closed palant closed 3 months ago

palant commented 3 months ago

It seems that constructing a trie will only work correctly if values are inserted starting with the shortest path. Here is a modified code example from the documentation:

use trie_rs::map::Trie;

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)]);

The first assert succeeds, the second fails – app is not present, it was dropped because apple was already present when it was supposed to be added.

shanecelis commented 3 months ago

Many thanks. I thought this had been addressed. Will make a test. Thank you for the code. It should definitely be insert order independent.

shanecelis commented 3 months ago

Thank you. This has been fixed and published in version 0.4.2. Let me know if you have any further troubles. Thank you for reporting the issue.

palant commented 3 months ago

Confirmed, I removed the work-around and my tests are still passing.

shanecelis commented 3 months ago

Great. Thank you. Closing issue.