mdtranslation/utils/
vec_map.rs1pub(crate) struct VecMap<K, V>(Vec<(K, V)>);
2
3impl<K: PartialEq, V> VecMap<K, V> {
4 pub(crate) fn get(&self, k: &K) -> Option<&V> {
5 for (existing_k, existing_v) in self.0.iter() {
6 if existing_k == k {
7 return Some(existing_v);
8 }
9 }
10 None
11 }
12
13 pub(crate) fn get_mut(&mut self, k: &K) -> Option<&mut V> {
14 for (existing_k, existing_v) in self.0.iter_mut() {
15 if existing_k == k {
16 return Some(existing_v);
17 }
18 }
19 None
20 }
21
22 pub(crate) fn contains(&self, k: &K) -> bool {
23 self.get(k).is_some()
24 }
25 pub(crate) fn insert(&mut self, k: K, v: V) {
26 if let Some(existing_v) = self.get_mut(&k) {
27 *existing_v = v;
28 } else {
29 self.0.push((k, v));
30 }
31 }
32}
33
34impl<K, V> IntoIterator for VecMap<K, V> {
35 type Item = (K, V);
36 type IntoIter = std::vec::IntoIter<(K, V)>;
37 fn into_iter(self) -> std::vec::IntoIter<(K, V)> {
38 self.0.into_iter()
39 }
40}
41
42impl<K: PartialEq, V> std::iter::FromIterator<(K, V)> for VecMap<K, V> {
43 fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
44 let mut result = VecMap::default();
45 for (k, v) in iter {
46 result.insert(k, v);
47 }
48 result
49 }
50}
51
52impl<K, V> Default for VecMap<K, V> {
53 fn default() -> Self {
54 VecMap(Default::default())
55 }
56}