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
|
|
|
|
2021-01-15 00:02:08 +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)]
|
2021-01-15 00:02:08 +00:00
|
|
|
pub struct ArenaMap<IDX, V> {
|
2020-03-19 15:00:11 +00:00
|
|
|
v: Vec<Option<V>>,
|
2021-01-15 00:02:08 +00:00
|
|
|
_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 }
|
|
|
|
}
|
|
|
|
|
2021-01-15 00:02:08 +00:00
|
|
|
/// 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-30 20:15:28 +00:00
|
|
|
|
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-15 00:02:08 +00:00
|
|
|
/// 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
|
|
|
}
|
|
|
|
|
2021-01-15 00:02:08 +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())
|
|
|
|
}
|
|
|
|
|
2021-01-15 00:02:08 +00:00
|
|
|
/// 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
|
|
|
}
|
|
|
|
|
2021-01-15 00:02:08 +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;
|
2021-01-15 00:02:08 +00:00
|
|
|
fn index(&self, idx: Idx<V>) -> &T {
|
|
|
|
self.v[Self::to_idx(idx)].as_ref().unwrap()
|
2019-01-06 22:57:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 08:29:22 +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
|
|
|
}
|
|
|
|
}
|