From 4c4e54ac8a9782439744fe15aa31a3bedab92b74 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 14 Jan 2021 18:47:42 +0300 Subject: prepare to publish el libro de arena --- lib/arena/src/lib.rs | 152 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 lib/arena/src/lib.rs (limited to 'lib/arena/src/lib.rs') diff --git a/lib/arena/src/lib.rs b/lib/arena/src/lib.rs new file mode 100644 index 000000000..3169aa5b8 --- /dev/null +++ b/lib/arena/src/lib.rs @@ -0,0 +1,152 @@ +//! Yet another index-based arena. + +use std::{ + fmt, + hash::{Hash, Hasher}, + iter::FromIterator, + marker::PhantomData, + ops::{Index, IndexMut}, +}; + +pub mod map; + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct RawId(u32); + +impl From for u32 { + fn from(raw: RawId) -> u32 { + raw.0 + } +} + +impl From 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) + } +} + +pub struct Idx { + raw: RawId, + _ty: PhantomData T>, +} + +impl Clone for Idx { + fn clone(&self) -> Self { + *self + } +} +impl Copy for Idx {} + +impl PartialEq for Idx { + fn eq(&self, other: &Idx) -> bool { + self.raw == other.raw + } +} +impl Eq for Idx {} + +impl Hash for Idx { + fn hash(&self, state: &mut H) { + self.raw.hash(state) + } +} + +impl fmt::Debug for Idx { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut type_name = std::any::type_name::(); + if let Some(idx) = type_name.rfind(':') { + type_name = &type_name[idx + 1..] + } + write!(f, "Idx::<{}>({})", type_name, self.raw) + } +} + +impl Idx { + 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 { + data: Vec, +} + +impl fmt::Debug for Arena { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("Arena").field("len", &self.len()).field("data", &self.data).finish() + } +} + +impl Arena { + pub const fn new() -> Arena { + Arena { data: Vec::new() } + } + pub fn clear(&mut self) { + self.data.clear(); + } + + pub fn len(&self) -> usize { + self.data.len() + } + pub fn is_empty(&self) -> bool { + self.data.is_empty() + } + pub fn alloc(&mut self, value: T) -> Idx { + let id = RawId(self.data.len() as u32); + self.data.push(value); + Idx::from_raw(id) + } + pub fn iter( + &self, + ) -> impl Iterator, &T)> + ExactSizeIterator + DoubleEndedIterator { + self.data.iter().enumerate().map(|(idx, value)| (Idx::from_raw(RawId(idx as u32)), value)) + } + pub fn shrink_to_fit(&mut self) { + self.data.shrink_to_fit(); + } +} + +impl Default for Arena { + fn default() -> Arena { + Arena { data: Vec::new() } + } +} + +impl Index> for Arena { + type Output = T; + fn index(&self, idx: Idx) -> &T { + let idx = idx.into_raw().0 as usize; + &self.data[idx] + } +} + +impl IndexMut> for Arena { + fn index_mut(&mut self, idx: Idx) -> &mut T { + let idx = idx.into_raw().0 as usize; + &mut self.data[idx] + } +} + +impl FromIterator for Arena { + fn from_iter(iter: I) -> Self + where + I: IntoIterator, + { + Arena { data: Vec::from_iter(iter) } + } +} -- cgit v1.2.3