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
#![doc = include_str!("../README.md")]

use std::{borrow::Borrow, collections::HashMap, hash::Hash};

#[derive(Clone, Debug)]
pub struct TrainMap<'a, K, V> {
    map: HashMap<K, V>,
    parent: Option<&'a TrainMap<'a, K, V>>,
}

impl<'a, K: Eq + Hash, V> TrainMap<'a, K, V> {
    pub fn new() -> Self {
        Self {
            map: Default::default(),
            parent: None,
        }
    }

    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            map: HashMap::with_capacity(capacity),
            parent: None,
        }
    }

    pub fn get<Q: Eq + Hash + ?Sized>(&self, key: &Q) -> Option<&V>
    where
        K: Borrow<Q>,
    {
        if let Some(value) = self.map.get(key) {
            Some(value)
        } else if let Some(parent) = self.parent {
            parent.get(key)
        } else {
            None
        }
    }

    pub fn insert(&mut self, key: K, value: V) -> Option<V> {
        self.map.insert(key, value)
    }

    pub fn fork(&'a self) -> Self {
        Self {
            map: Default::default(),
            parent: Some(self),
        }
    }
}

impl<'a, K: Eq + Hash, V> Default for TrainMap<'a, K, V> {
    fn default() -> Self {
        Self::new()
    }
}

impl<'a, K: Eq + Hash, V> Extend<(K, V)> for TrainMap<'a, K, V> {
    fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iterator: T) {
        self.map.extend(iterator)
    }
}