1#![doc = include_str!("../README.md")]
2
3use std::{
4 borrow::Borrow,
5 collections::HashMap,
6 hash::{BuildHasher, Hash, RandomState},
7};
8
9#[derive(Clone, Debug)]
10pub struct TrainMap<'a, K, V, S = RandomState> {
11 map: HashMap<K, V, S>,
12 parent: Option<&'a TrainMap<'a, K, V, S>>,
13}
14
15impl<'a, K: Eq + Hash, V> TrainMap<'a, K, V> {
16 pub fn new() -> Self {
17 Self::default()
18 }
19
20 pub fn with_capacity(capacity: usize) -> Self {
21 Self::with_capacity_and_hasher(capacity, Default::default())
22 }
23}
24
25impl<'a, K: Eq + Hash, V, S: BuildHasher> TrainMap<'a, K, V, S> {
26 pub fn with_hasher(hasher: S) -> Self {
27 Self {
28 map: HashMap::with_hasher(hasher),
29 parent: None,
30 }
31 }
32
33 pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self {
34 Self {
35 map: HashMap::with_capacity_and_hasher(capacity, hasher),
36 parent: None,
37 }
38 }
39
40 pub fn hasher(&self) -> &S {
41 self.map.hasher()
42 }
43
44 pub fn get<Q: Eq + Hash + ?Sized>(&self, key: &Q) -> Option<&V>
45 where
46 K: Borrow<Q>,
47 {
48 if let Some(value) = self.map.get(key) {
49 Some(value)
50 } else if let Some(parent) = self.parent {
51 parent.get(key)
52 } else {
53 None
54 }
55 }
56
57 pub fn insert(&mut self, key: K, value: V) -> Option<V> {
58 self.map.insert(key, value)
59 }
60
61 pub fn fork(&'a self) -> Self
62 where
63 S: Clone,
64 {
65 Self {
66 map: HashMap::with_hasher(self.map.hasher().clone()),
67 parent: Some(self),
68 }
69 }
70}
71
72impl<'a, K: Eq + Hash, V, S: Default> Default for TrainMap<'a, K, V, S> {
73 fn default() -> Self {
74 Self {
75 map: Default::default(),
76 parent: None,
77 }
78 }
79}
80
81impl<'a, K: Eq + Hash, V, S: BuildHasher> Extend<(K, V)> for TrainMap<'a, K, V, S> {
82 fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iterator: T) {
83 self.map.extend(iterator)
84 }
85}
86
87#[cfg(test)]
88mod tests {
89 use super::*;
90 use std::hash::{BuildHasherDefault, DefaultHasher};
91
92 #[derive(Clone, Debug, Default, PartialEq)]
93 struct TaggedState(usize);
94
95 impl BuildHasher for TaggedState {
96 type Hasher = DefaultHasher;
97
98 fn build_hasher(&self) -> Self::Hasher {
99 DefaultHasher::new()
100 }
101 }
102
103 #[test]
104 fn create_with_hasher() {
105 let mut map = TrainMap::with_hasher(BuildHasherDefault::<DefaultHasher>::default());
106
107 map.insert("foo", 42);
108
109 assert_eq!(map.get("foo"), Some(&42));
110 }
111
112 #[test]
113 fn create_with_capacity_and_hasher() {
114 let mut map =
115 TrainMap::with_capacity_and_hasher(1, BuildHasherDefault::<DefaultHasher>::default());
116
117 map.insert("foo", 42);
118
119 assert_eq!(map.get("foo"), Some(&42));
120 }
121
122 #[test]
123 fn create_default_with_hasher() {
124 let mut map = TrainMap::<_, _, BuildHasherDefault<DefaultHasher>>::default();
125
126 map.insert("foo", 42);
127
128 assert_eq!(map.get("foo"), Some(&42));
129 }
130
131 #[test]
132 fn get_hasher() {
133 assert_eq!(
134 TrainMap::<(), (), _>::with_hasher(TaggedState(42)).hasher(),
135 &TaggedState(42)
136 );
137 }
138
139 #[test]
140 fn fork_with_hasher() {
141 let mut parent = TrainMap::with_hasher(TaggedState(42));
142
143 parent.insert("foo", 1);
144
145 let mut child = parent.fork();
146
147 child.insert("bar", 2);
148
149 assert_eq!(child.hasher(), &TaggedState(42));
150 assert_eq!(child.get("foo"), Some(&1));
151 assert_eq!(child.get("bar"), Some(&2));
152 assert_eq!(parent.get("bar"), None);
153 }
154}