2019-01-04 07:01:06 -06:00
|
|
|
//! Yet another index-based arena.
|
|
|
|
|
|
|
|
use std::{
|
|
|
|
fmt,
|
2020-03-19 10:00:11 -05:00
|
|
|
hash::{Hash, Hasher},
|
2019-07-04 15:05:17 -05:00
|
|
|
iter::FromIterator,
|
2019-01-04 07:01:06 -06:00
|
|
|
marker::PhantomData,
|
|
|
|
ops::{Index, IndexMut},
|
|
|
|
};
|
|
|
|
|
2019-01-06 16:57:39 -06:00
|
|
|
pub mod map;
|
|
|
|
|
2019-01-04 07:01:06 -06: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 10:00:11 -05:00
|
|
|
pub struct Idx<T> {
|
|
|
|
raw: RawId,
|
|
|
|
_ty: PhantomData<fn() -> T>,
|
2019-01-04 07:01:06 -06:00
|
|
|
}
|
|
|
|
|
2020-03-19 10:00:11 -05:00
|
|
|
impl<T> Clone for Idx<T> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
*self
|
2019-01-10 13:47:05 -06:00
|
|
|
}
|
|
|
|
}
|
2020-03-19 10:00:11 -05:00
|
|
|
impl<T> Copy for Idx<T> {}
|
2019-01-10 13:47:05 -06:00
|
|
|
|
2020-03-19 10:00:11 -05:00
|
|
|
impl<T> PartialEq for Idx<T> {
|
|
|
|
fn eq(&self, other: &Idx<T>) -> bool {
|
|
|
|
self.raw == other.raw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<T> Eq for Idx<T> {}
|
|
|
|
|
|
|
|
impl<T> Hash for Idx<T> {
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
self.raw.hash(state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> fmt::Debug for Idx<T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
let mut type_name = std::any::type_name::<T>();
|
|
|
|
if let Some(idx) = type_name.rfind(':') {
|
|
|
|
type_name = &type_name[idx + 1..]
|
2019-01-04 07:01:06 -06:00
|
|
|
}
|
2020-03-19 10:00:11 -05:00
|
|
|
write!(f, "Idx::<{}>({})", type_name, self.raw)
|
|
|
|
}
|
2019-01-04 07:01:06 -06:00
|
|
|
}
|
|
|
|
|
2020-03-19 10:00:11 -05:00
|
|
|
impl<T> Idx<T> {
|
|
|
|
pub fn from_raw(raw: RawId) -> Self {
|
|
|
|
Idx { raw, _ty: PhantomData }
|
|
|
|
}
|
|
|
|
pub fn into_raw(self) -> RawId {
|
|
|
|
self.raw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
|
|
|
pub struct Arena<T> {
|
|
|
|
data: Vec<T>,
|
2019-01-04 07:01:06 -06:00
|
|
|
}
|
|
|
|
|
2020-03-19 10:00:11 -05:00
|
|
|
impl<T: fmt::Debug> fmt::Debug for Arena<T> {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt.debug_struct("Arena").field("len", &self.len()).field("data", &self.data).finish()
|
2019-11-24 13:44:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 10:00:11 -05:00
|
|
|
impl<T> Arena<T> {
|
|
|
|
pub const fn new() -> Arena<T> {
|
|
|
|
Arena { data: Vec::new() }
|
|
|
|
}
|
2020-04-25 14:04:04 -05:00
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.data.clear();
|
|
|
|
}
|
2020-03-19 10:00:11 -05:00
|
|
|
|
2019-01-08 06:53:32 -06:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.data.len()
|
|
|
|
}
|
2019-04-26 10:42:10 -05:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.data.is_empty()
|
|
|
|
}
|
2020-03-19 10:00:11 -05:00
|
|
|
pub fn alloc(&mut self, value: T) -> Idx<T> {
|
2019-01-04 07:01:06 -06:00
|
|
|
let id = RawId(self.data.len() as u32);
|
|
|
|
self.data.push(value);
|
2020-03-19 10:00:11 -05:00
|
|
|
Idx::from_raw(id)
|
2019-01-04 07:01:06 -06:00
|
|
|
}
|
2020-03-19 10:00:11 -05:00
|
|
|
pub fn iter(
|
|
|
|
&self,
|
|
|
|
) -> impl Iterator<Item = (Idx<T>, &T)> + ExactSizeIterator + DoubleEndedIterator {
|
|
|
|
self.data.iter().enumerate().map(|(idx, value)| (Idx::from_raw(RawId(idx as u32)), value))
|
2019-01-04 07:01:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 10:00:11 -05:00
|
|
|
impl<T> Default for Arena<T> {
|
|
|
|
fn default() -> Arena<T> {
|
|
|
|
Arena { data: Vec::new() }
|
2019-01-04 07:01:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 10:00:11 -05:00
|
|
|
impl<T> Index<Idx<T>> for Arena<T> {
|
2019-01-04 07:01:06 -06:00
|
|
|
type Output = T;
|
2020-03-19 10:00:11 -05:00
|
|
|
fn index(&self, idx: Idx<T>) -> &T {
|
2019-01-04 07:01:06 -06:00
|
|
|
let idx = idx.into_raw().0 as usize;
|
|
|
|
&self.data[idx]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 10:00:11 -05:00
|
|
|
impl<T> IndexMut<Idx<T>> for Arena<T> {
|
|
|
|
fn index_mut(&mut self, idx: Idx<T>) -> &mut T {
|
2019-01-04 07:01:06 -06:00
|
|
|
let idx = idx.into_raw().0 as usize;
|
|
|
|
&mut self.data[idx]
|
|
|
|
}
|
|
|
|
}
|
2019-01-25 05:21:14 -06:00
|
|
|
|
2020-03-19 10:00:11 -05:00
|
|
|
impl<T> FromIterator<T> for Arena<T> {
|
2019-01-25 05:21:14 -06:00
|
|
|
fn from_iter<I>(iter: I) -> Self
|
|
|
|
where
|
|
|
|
I: IntoIterator<Item = T>,
|
|
|
|
{
|
2020-03-19 10:00:11 -05:00
|
|
|
Arena { data: Vec::from_iter(iter) }
|
2019-01-25 05:21:14 -06:00
|
|
|
}
|
|
|
|
}
|