#trie #tree #performance #structure

fast_trie_tree

A fast TrieTree for multiple data types

4 releases

0.1.3 Jul 21, 2023
0.1.2 Jun 28, 2023
0.1.1 Jun 26, 2023
0.1.0 Jun 26, 2023

#2103 in Data structures

34 downloads per month

MIT license

36KB
795 lines

Fast Trie Tree

  • no use Rc | RefCell...
  • Structures such as Rc and RefCell are not used, so this will be slightly faster than TrieTree implementations that use those structures
  • Use Iterator<T: Hash + Eq> to generalize, not only for a data type such as char, u8

Examples

# use fast_trie_tree::TrieTree;

let mut ctree: TrieTree<char> = TrieTree::new();
assert!(ctree.insert("abc".chars()));
assert_eq!(format!("{:#?}", ctree), concat!(
        "TrieTree {\n",
        "    count: 1,\n",
        "    root: (/): {\n",
        "        ('a'): {\n",
        "            ('b'): {\n",
        "                ['c']: {}\n",
        "            }\n",
        "        }\n",
        "    },\n",
        "}"
));
assert_eq!(ctree.query_iter("a".chars())
           .unwrap().collect::<Vec<_>>(), [[&'b', &'c']]);
assert!(ctree.insert("ace".chars()));
assert!(ctree.query("ace".chars()));
assert!(ctree.query("abc".chars()));
assert!(! ctree.query("ab".chars()));

let byte_tree = TrieTree::from_iter(
    [
        [0u8, 2, 3, 8],
        [4, 5, 7, 9],
        [4, 2, 6, 1],
    ]);
assert_eq!(byte_tree.count(), 3);
assert!(byte_tree.query([0, 2, 3, 8]));
assert!(byte_tree.query([4, 5, 7, 9]));
assert!(byte_tree.query([4, 2, 6, 1]));
let mut res = byte_tree.query_iter([4]).unwrap().collect::<Vec<_>>();
res.sort();
assert_eq!(res, [[&2, &6, &1], [&5, &7, &9]]);
assert_eq!(byte_tree.count(), 3);

Info

No runtime deps