X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=lib%2Farena%2Fsrc%2Fmap.rs;h=d8acfe0518d09adcd4f2fba679f728393db07e85;hb=1d103cf087c574f66279490ffef8c76178aea5cc;hp=5ebaa9b82df86d2c2dc089127679924b3c450a27;hpb=3224ecea8734f850eda474c136ae4dcb7e1aa3f1;p=rust.git diff --git a/lib/arena/src/map.rs b/lib/arena/src/map.rs index 5ebaa9b82df..d8acfe0518d 100644 --- a/lib/arena/src/map.rs +++ b/lib/arena/src/map.rs @@ -2,30 +2,33 @@ use crate::Idx; -/// A map from arena IDs to some other type. Space requirement is O(highest ID). +/// A map from arena indexes to some other type. +/// Space requirement is O(highest index). #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct ArenaMap { +pub struct ArenaMap { v: Vec>, - _ty: PhantomData, + _ty: PhantomData, } impl ArenaMap, V> { - /// Inserts a value associated with a given arena ID into the map. - pub fn insert(&mut self, id: Idx, t: V) { - let idx = Self::to_idx(id); + /// Inserts a value associated with a given arena index into the map. + pub fn insert(&mut self, idx: Idx, t: V) { + let idx = Self::to_idx(idx); self.v.resize_with((idx + 1).max(self.v.len()), || None); self.v[idx] = Some(t); } - /// Returns a reference to the value associated with the provided ID if it is present. - pub fn get(&self, id: Idx) -> Option<&V> { - self.v.get(Self::to_idx(id)).and_then(|it| it.as_ref()) + /// Returns a reference to the value associated with the provided index + /// if it is present. + pub fn get(&self, idx: Idx) -> Option<&V> { + self.v.get(Self::to_idx(idx)).and_then(|it| it.as_ref()) } - /// Returns a mutable reference to the value associated with the provided ID if it is present. - pub fn get_mut(&mut self, id: Idx) -> Option<&mut V> { - self.v.get_mut(Self::to_idx(id)).and_then(|it| it.as_mut()) + /// Returns a mutable reference to the value associated with the provided index + /// if it is present. + pub fn get_mut(&mut self, idx: Idx) -> Option<&mut V> { + self.v.get_mut(Self::to_idx(idx)).and_then(|it| it.as_mut()) } /// Returns an iterator over the values in the map. @@ -38,13 +41,13 @@ pub fn values_mut(&mut self) -> impl Iterator { self.v.iter_mut().filter_map(|o| o.as_mut()) } - /// Returns an iterator over the arena IDs and values in the map. + /// Returns an iterator over the arena indexes and values in the map. pub fn iter(&self) -> impl Iterator, &V)> { self.v.iter().enumerate().filter_map(|(idx, o)| Some((Self::from_idx(idx), o.as_ref()?))) } - fn to_idx(id: Idx) -> usize { - u32::from(id.into_raw()) as usize + fn to_idx(idx: Idx) -> usize { + u32::from(idx.into_raw()) as usize } fn from_idx(idx: usize) -> Idx { @@ -54,8 +57,8 @@ fn from_idx(idx: usize) -> Idx { impl std::ops::Index> for ArenaMap, T> { type Output = T; - fn index(&self, id: Idx) -> &T { - self.v[Self::to_idx(id)].as_ref().unwrap() + fn index(&self, idx: Idx) -> &T { + self.v[Self::to_idx(idx)].as_ref().unwrap() } }