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