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
//!
//!
use std::borrow::Cow;
use std::collections::{BTreeMap};
use crate::common::{StringHashFunction, StringTable};

/// Default implementation just maintains a btree keyed on a 32-bit fxhash value
#[derive(Clone)]
pub struct BTreeStringTable<'a>
{
    /// The hash function pointer
    hash_func: StringHashFunction<u32>,

    /// The internal map carrying the actual strings
    index: BTreeMap<u32, Cow<'a, str>>
}

impl <'a> BTreeStringTable<'a>
{
    /// Create a new string table with default initial capacity using the default hash function
    pub fn new() -> Self {
        BTreeStringTable{
            hash_func: fxhash::hash32,
            index: BTreeMap::default()
        }
    }
    /// Create a new string table with a user-defined string hashing function
    pub fn with_hash(f : StringHashFunction<u32>) -> Self{
        BTreeStringTable {
            hash_func: f,
            index: BTreeMap::new()
        }
    }
}

impl <'a> StringTable<'a, u32> for BTreeStringTable<'a> {
    fn add(&mut self, value: &str) -> u32 {
        let hash = (self.hash_func)(value);
        if !self.index.contains_key(&hash) {
            self.index.insert(hash, Cow::Owned(String::from(value)));
        }
        hash
    }

    fn contains(&self, value: &str) -> bool {
        let hash = (self.hash_func)(value);
        self.index.contains_key(&hash)
    }

    fn remove(&mut self, value: &str) -> () {
        let hash = (self.hash_func)(value);
        if self.index.contains_key(&hash) {
            self.index.remove(&hash);
        }
    }

    fn get(&self, key: u32) -> Option<&Cow<'a, str>> {
       self.index.get(&key)
    }

    fn len(&self) -> usize {
        self.index.len()
    }
}