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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

86 lines
2.6 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 indexes to some other type.
/// Space requirement is O(highest index).
2019-01-06 22:57:39 +00:00
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ArenaMap<IDX, V> {
2020-03-19 15:00:11 +00:00
v: Vec<Option<V>>,
_ty: PhantomData<IDX>,
2019-01-06 22:57:39 +00:00
}
2020-03-19 15:00:11 +00:00
impl<T, V> ArenaMap<Idx<T>, V> {
2022-08-03 09:35:31 +00:00
/// Creates a new empty map.
pub const fn new() -> Self {
Self { v: Vec::new(), _ty: PhantomData }
}
/// Create a new empty map with specific capacity.
pub fn with_capacity(capacity: usize) -> Self {
Self { v: Vec::with_capacity(capacity), _ty: PhantomData }
}
/// Inserts a value associated with a given arena index into the map.
pub fn insert(&mut self, idx: Idx<T>, t: V) {
let idx = Self::to_idx(idx);
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);
}
/// Returns a reference to the value associated with the provided index
/// if it is present.
pub fn get(&self, idx: Idx<T>) -> Option<&V> {
self.v.get(Self::to_idx(idx)).and_then(|it| it.as_ref())
2019-01-06 22:57:39 +00:00
}
/// Returns a mutable reference to the value associated with the provided index
/// if it is present.
pub fn get_mut(&mut self, idx: Idx<T>) -> Option<&mut V> {
self.v.get_mut(Self::to_idx(idx)).and_then(|it| it.as_mut())
2019-01-25 07:29:00 +00:00
}
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())
}
/// Returns an iterator over the arena indexes 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
}
fn to_idx(idx: Idx<T>) -> usize {
u32::from(idx.into_raw()) as usize
2019-01-06 22:57:39 +00:00
}
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;
fn index(&self, idx: Idx<V>) -> &T {
self.v[Self::to_idx(idx)].as_ref().unwrap()
2019-01-06 22:57:39 +00:00
}
}
impl<T, V> std::ops::IndexMut<Idx<V>> for ArenaMap<Idx<V>, T> {
fn index_mut(&mut self, idx: Idx<V>) -> &mut T {
self.v[Self::to_idx(idx)].as_mut().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 {
2022-08-03 09:35:31 +00:00
Self::new()
2019-01-06 22:57:39 +00:00
}
}