kanata_parser/
trie.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! Wrapper around a trie type for (hopefully) easier swapping of libraries if desired.

use bytemuck::cast_slice;
use patricia_tree::map::PatriciaMap;

pub type TrieKeyElement = u16;

#[derive(Debug, Clone)]
pub struct Trie<T> {
    inner: patricia_tree::map::PatriciaMap<T>,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum GetOrDescendentExistsResult<T> {
    NotInTrie,
    InTrie,
    HasValue(T),
}

use GetOrDescendentExistsResult::*;

impl<T> Default for Trie<T> {
    fn default() -> Self {
        Self::new()
    }
}

fn key_len(k: impl AsRef<[u16]>) -> usize {
    debug_assert!(std::mem::size_of::<TrieKeyElement>() == 2 * std::mem::size_of::<u8>());
    k.as_ref().len() * 2
}

impl<T> Trie<T> {
    pub fn new() -> Self {
        Self {
            inner: PatriciaMap::new(),
        }
    }

    pub fn ancestor_exists(&self, key: impl AsRef<[u16]>) -> bool {
        self.inner
            .get_longest_common_prefix(cast_slice(key.as_ref()))
            .is_some()
    }

    pub fn descendant_exists(&self, key: impl AsRef<[u16]>) -> bool {
        // Length of the [u8] interpretation of the [u16] key is doubled.
        self.inner
            .longest_common_prefix_len(cast_slice(key.as_ref()))
            == key_len(key)
    }

    pub fn insert(&mut self, key: impl AsRef<[u16]>, val: T) {
        self.inner.insert(cast_slice(key.as_ref()), val);
    }

    pub fn get_or_descendant_exists(&self, key: impl AsRef<[u16]>) -> GetOrDescendentExistsResult<T>
    where
        T: Clone,
    {
        let mut descendants = self.inner.iter_prefix(cast_slice(key.as_ref()));
        match descendants.next() {
            None => NotInTrie,
            Some(descendant) => {
                if descendant.0.len() == key_len(key.as_ref()) {
                    HasValue(descendant.1.clone())
                } else {
                    InTrie
                }
            }
        }
    }

    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }
}