2019-01-04 13:01:06 +00:00
|
|
|
//! Yet another index-based arena.
|
|
|
|
|
|
|
|
use std::{
|
|
|
|
fmt,
|
|
|
|
marker::PhantomData,
|
|
|
|
ops::{Index, IndexMut},
|
2019-01-25 11:21:14 +00:00
|
|
|
iter::FromIterator,
|
2019-01-04 13:01:06 +00:00
|
|
|
};
|
|
|
|
|
2019-01-06 22:57:39 +00:00
|
|
|
pub mod map;
|
|
|
|
|
2019-01-04 13:01:06 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub struct RawId(u32);
|
|
|
|
|
|
|
|
impl From<RawId> for u32 {
|
|
|
|
fn from(raw: RawId) -> u32 {
|
|
|
|
raw.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<u32> for RawId {
|
|
|
|
fn from(id: u32) -> RawId {
|
|
|
|
RawId(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for RawId {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.0.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for RawId {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.0.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-10 19:47:05 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
2019-01-04 13:01:06 +00:00
|
|
|
pub struct Arena<ID: ArenaId, T> {
|
|
|
|
data: Vec<T>,
|
|
|
|
_ty: PhantomData<ID>,
|
|
|
|
}
|
|
|
|
|
2019-01-10 19:47:05 +00:00
|
|
|
impl<ID: ArenaId, T: fmt::Debug> fmt::Debug for Arena<ID, T> {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
2019-02-08 11:49:43 +00:00
|
|
|
fmt.debug_struct("Arena").field("len", &self.len()).field("data", &self.data).finish()
|
2019-01-10 19:47:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-04 13:01:06 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! impl_arena_id {
|
|
|
|
($name:ident) => {
|
|
|
|
impl $crate::ArenaId for $name {
|
|
|
|
fn from_raw(raw: $crate::RawId) -> Self {
|
|
|
|
$name(raw)
|
|
|
|
}
|
|
|
|
fn into_raw(self) -> $crate::RawId {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait ArenaId {
|
|
|
|
fn from_raw(raw: RawId) -> Self;
|
|
|
|
fn into_raw(self) -> RawId;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<ID: ArenaId, T> Arena<ID, T> {
|
2019-01-08 12:53:32 +00:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.data.len()
|
|
|
|
}
|
2019-04-26 15:42:10 +00:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.data.is_empty()
|
|
|
|
}
|
2019-01-04 13:01:06 +00:00
|
|
|
pub fn alloc(&mut self, value: T) -> ID {
|
|
|
|
let id = RawId(self.data.len() as u32);
|
|
|
|
self.data.push(value);
|
|
|
|
ID::from_raw(id)
|
|
|
|
}
|
2019-04-26 15:41:14 +00:00
|
|
|
pub fn iter(&self) -> impl Iterator<Item = (ID, &T)> {
|
2019-02-08 11:49:43 +00:00
|
|
|
self.data.iter().enumerate().map(|(idx, value)| (ID::from_raw(RawId(idx as u32)), value))
|
2019-01-04 13:01:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<ID: ArenaId, T> Default for Arena<ID, T> {
|
|
|
|
fn default() -> Arena<ID, T> {
|
2019-02-08 11:49:43 +00:00
|
|
|
Arena { data: Vec::new(), _ty: PhantomData }
|
2019-01-04 13:01:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<ID: ArenaId, T> Index<ID> for Arena<ID, T> {
|
|
|
|
type Output = T;
|
|
|
|
fn index(&self, idx: ID) -> &T {
|
|
|
|
let idx = idx.into_raw().0 as usize;
|
|
|
|
&self.data[idx]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<ID: ArenaId, T> IndexMut<ID> for Arena<ID, T> {
|
|
|
|
fn index_mut(&mut self, idx: ID) -> &mut T {
|
|
|
|
let idx = idx.into_raw().0 as usize;
|
|
|
|
&mut self.data[idx]
|
|
|
|
}
|
|
|
|
}
|
2019-01-25 11:21:14 +00:00
|
|
|
|
|
|
|
impl<ID: ArenaId, T> FromIterator<T> for Arena<ID, T> {
|
|
|
|
fn from_iter<I>(iter: I) -> Self
|
|
|
|
where
|
|
|
|
I: IntoIterator<Item = T>,
|
|
|
|
{
|
2019-02-08 11:49:43 +00:00
|
|
|
Arena { data: Vec::from_iter(iter), _ty: PhantomData }
|
2019-01-25 11:21:14 +00:00
|
|
|
}
|
|
|
|
}
|