diff options
Diffstat (limited to 'lib/arena/src/map.rs')
-rw-r--r-- | lib/arena/src/map.rs | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/lib/arena/src/map.rs b/lib/arena/src/map.rs index 0f33907c0..980198247 100644 --- a/lib/arena/src/map.rs +++ b/lib/arena/src/map.rs | |||
@@ -12,6 +12,7 @@ pub struct ArenaMap<ID, V> { | |||
12 | } | 12 | } |
13 | 13 | ||
14 | impl<T, V> ArenaMap<Idx<T>, V> { | 14 | impl<T, V> ArenaMap<Idx<T>, V> { |
15 | /// Inserts a value associated with a given arena ID into the map. | ||
15 | pub fn insert(&mut self, id: Idx<T>, t: V) { | 16 | pub fn insert(&mut self, id: Idx<T>, t: V) { |
16 | let idx = Self::to_idx(id); | 17 | let idx = Self::to_idx(id); |
17 | 18 | ||
@@ -19,22 +20,27 @@ impl<T, V> ArenaMap<Idx<T>, V> { | |||
19 | self.v[idx] = Some(t); | 20 | self.v[idx] = Some(t); |
20 | } | 21 | } |
21 | 22 | ||
23 | /// Returns a reference to the value associated with the provided ID if it is present. | ||
22 | pub fn get(&self, id: Idx<T>) -> Option<&V> { | 24 | pub fn get(&self, id: Idx<T>) -> Option<&V> { |
23 | self.v.get(Self::to_idx(id)).and_then(|it| it.as_ref()) | 25 | self.v.get(Self::to_idx(id)).and_then(|it| it.as_ref()) |
24 | } | 26 | } |
25 | 27 | ||
28 | /// Returns a mutable reference to the value associated with the provided ID if it is present. | ||
26 | pub fn get_mut(&mut self, id: Idx<T>) -> Option<&mut V> { | 29 | pub fn get_mut(&mut self, id: Idx<T>) -> Option<&mut V> { |
27 | self.v.get_mut(Self::to_idx(id)).and_then(|it| it.as_mut()) | 30 | self.v.get_mut(Self::to_idx(id)).and_then(|it| it.as_mut()) |
28 | } | 31 | } |
29 | 32 | ||
33 | /// Returns an iterator over the values in the map. | ||
30 | pub fn values(&self) -> impl Iterator<Item = &V> { | 34 | pub fn values(&self) -> impl Iterator<Item = &V> { |
31 | self.v.iter().filter_map(|o| o.as_ref()) | 35 | self.v.iter().filter_map(|o| o.as_ref()) |
32 | } | 36 | } |
33 | 37 | ||
38 | /// Returns an iterator over mutable references to the values in the map. | ||
34 | pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V> { | 39 | pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V> { |
35 | self.v.iter_mut().filter_map(|o| o.as_mut()) | 40 | self.v.iter_mut().filter_map(|o| o.as_mut()) |
36 | } | 41 | } |
37 | 42 | ||
43 | /// Returns an iterator over the arena IDs and values in the map. | ||
38 | pub fn iter(&self) -> impl Iterator<Item = (Idx<T>, &V)> { | 44 | pub fn iter(&self) -> impl Iterator<Item = (Idx<T>, &V)> { |
39 | self.v.iter().enumerate().filter_map(|(idx, o)| Some((Self::from_idx(idx), o.as_ref()?))) | 45 | self.v.iter().enumerate().filter_map(|(idx, o)| Some((Self::from_idx(idx), o.as_ref()?))) |
40 | } | 46 | } |