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

Fix order dep map trie builder #21

Closed shanecelis closed 5 months ago

shanecelis commented 6 months ago

Caught a bug where the order of entries to map::TrieBuilder exposed a bug. Namely a long entry like "apple" if it came before a shorter entry "app" then it would not be found.

#[test]
    fn value_overwrite_long_first() {
        let mut builder = TrieBuilder::new();
        builder.push("apple", 2);
        builder.push("app", 1);
        let trie = builder.build();
        assert_eq!(trie.exact_match("apple"), Some(2));
        assert_eq!(trie.exact_match("app"), Some(1)); // This one fails.
    }

    #[test]
    fn value_overwrite_short() {
        let mut builder = TrieBuilder::new();
        builder.push("app", 1);
        builder.push("apple", 2);
        let trie = builder.build();
        assert_eq!(trie.exact_match("apple"), Some(2));
        assert_eq!(trie.exact_match("app"), Some(1)); // This one is fine.
    }
shanecelis commented 5 months ago

This has been superceded by https://github.com/laysakura/trie-rs/pull/22.