rust-analyzer/lib/arena/src/map.rs

67 lines
2.1 KiB
Rust
Raw Normal View History

2019-01-06 22:57:39 +00:00
use std::marker::PhantomData;
2020-03-19 15:00:11 +00:00
use crate::Idx;
2019-01-06 22:57:39 +00:00
/// A map from arena IDs to some other type. Space requirement is O(highest ID).
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2020-03-19 15:00:11 +00:00
pub struct ArenaMap<ID, V> {
v: Vec<Option<V>>,
2019-01-06 22:57:39 +00:00
_ty: PhantomData<ID>,
}
2020-03-19 15:00:11 +00:00
impl<T, V> ArenaMap<Idx<T>, V> {
2021-01-14 23:37:09 +00:00
/// Inserts a value associated with a given arena ID into the map.
2020-03-19 15:00:11 +00:00
pub fn insert(&mut self, id: Idx<T>, t: V) {
2019-01-06 22:57:39 +00:00
let idx = Self::to_idx(id);
2020-03-31 13:02:12 +00:00
self.v.resize_with((idx + 1).max(self.v.len()), || None);
2019-01-06 22:57:39 +00:00
self.v[idx] = Some(t);
}
2021-01-14 23:37:09 +00:00
/// Returns a reference to the value associated with the provided ID if it is present.
2020-03-19 15:00:11 +00:00
pub fn get(&self, id: Idx<T>) -> Option<&V> {
2019-01-06 22:57:39 +00:00
self.v.get(Self::to_idx(id)).and_then(|it| it.as_ref())
}
2021-01-14 23:37:09 +00:00
/// Returns a mutable reference to the value associated with the provided ID if it is present.
2020-03-19 15:00:11 +00:00
pub fn get_mut(&mut self, id: Idx<T>) -> Option<&mut V> {
2019-01-25 07:29:00 +00:00
self.v.get_mut(Self::to_idx(id)).and_then(|it| it.as_mut())
}
2021-01-14 23:37:09 +00:00
/// Returns an iterator over the values in the map.
2020-03-19 15:00:11 +00:00
pub fn values(&self) -> impl Iterator<Item = &V> {
2019-01-06 22:57:39 +00:00
self.v.iter().filter_map(|o| o.as_ref())
}
2021-01-14 23:37:09 +00:00
/// Returns an iterator over mutable references to the values in the map.
2020-03-19 15:00:11 +00:00
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V> {
2019-01-06 22:57:39 +00:00
self.v.iter_mut().filter_map(|o| o.as_mut())
}
2021-01-14 23:37:09 +00:00
/// Returns an iterator over the arena IDs and values in the map.
2020-03-19 15:00:11 +00:00
pub fn iter(&self) -> impl Iterator<Item = (Idx<T>, &V)> {
2019-02-08 11:49:43 +00:00
self.v.iter().enumerate().filter_map(|(idx, o)| Some((Self::from_idx(idx), o.as_ref()?)))
2019-01-06 22:57:39 +00:00
}
2020-03-19 15:00:11 +00:00
fn to_idx(id: Idx<T>) -> usize {
2019-01-06 22:57:39 +00:00
u32::from(id.into_raw()) as usize
}
2020-03-19 15:00:11 +00:00
fn from_idx(idx: usize) -> Idx<T> {
Idx::from_raw((idx as u32).into())
2019-01-06 22:57:39 +00:00
}
}
2020-03-19 15:00:11 +00:00
impl<T, V> std::ops::Index<Idx<V>> for ArenaMap<Idx<V>, T> {
2019-01-06 22:57:39 +00:00
type Output = T;
2020-03-19 15:00:11 +00:00
fn index(&self, id: Idx<V>) -> &T {
2019-01-06 22:57:39 +00:00
self.v[Self::to_idx(id)].as_ref().unwrap()
}
}
2020-03-19 15:00:11 +00:00
impl<T, V> Default for ArenaMap<Idx<V>, T> {
2019-01-06 22:57:39 +00:00
fn default() -> Self {
2019-02-08 11:49:43 +00:00
ArenaMap { v: Vec::new(), _ty: PhantomData }
2019-01-06 22:57:39 +00:00
}
}