veloren_world/util/
map_vec.rs1use crate::util::DHashMap;
2use std::hash::Hash;
3
4#[derive(Clone, Debug)]
14pub struct MapVec<K, T> {
15 entries: DHashMap<K, T>,
20 default: T,
21}
22
23impl<K, T: Default> Default for MapVec<K, T> {
25 fn default() -> Self {
26 Self {
27 entries: Default::default(),
28 default: Default::default(),
29 }
30 }
31}
32
33impl<K: Copy + Eq + Hash, T: Clone> MapVec<K, T> {
34 pub fn from_list<'a>(i: impl IntoIterator<Item = &'a (K, T)>, default: T) -> Self
35 where
36 K: 'a,
37 T: 'a,
38 {
39 Self {
40 entries: i.into_iter().cloned().collect(),
41 default,
42 }
43 }
44
45 pub fn from_iter(i: impl Iterator<Item = (K, T)>, default: T) -> Self {
46 Self {
47 entries: i.collect(),
48 default,
49 }
50 }
51
52 pub fn from_default(default: T) -> Self {
53 Self {
54 entries: DHashMap::default(),
55 default,
56 }
57 }
58
59 pub fn get_mut(&mut self, entry: K) -> &mut T {
60 let default = &self.default;
61 self.entries.entry(entry).or_insert_with(|| default.clone())
62 }
63
64 pub fn get(&self, entry: K) -> &T { self.entries.get(&entry).unwrap_or(&self.default) }
65
66 pub fn map<U: Default>(self, mut f: impl FnMut(K, T) -> U) -> MapVec<K, U> {
67 MapVec {
68 entries: self
69 .entries
70 .into_iter()
71 .map(|(s, v)| (s, f(s, v)))
72 .collect(),
73 default: U::default(),
74 }
75 }
76
77 pub fn iter(&self) -> impl Iterator<Item = (K, &T)> + '_ {
78 self.entries.iter().map(|(s, v)| (*s, v))
79 }
80
81 pub fn iter_mut(&mut self) -> impl Iterator<Item = (K, &mut T)> + '_ {
82 self.entries.iter_mut().map(|(s, v)| (*s, v))
83 }
84}
85
86impl<K: Copy + Eq + Hash, T: Clone> std::ops::Index<K> for MapVec<K, T> {
87 type Output = T;
88
89 fn index(&self, entry: K) -> &Self::Output { self.get(entry) }
90}
91
92impl<K: Copy + Eq + Hash, T: Clone> std::ops::IndexMut<K> for MapVec<K, T> {
93 fn index_mut(&mut self, entry: K) -> &mut Self::Output { self.get_mut(entry) }
94}