From f840fcb2f525c13809d6a736e434155edf075a06 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 19 Mar 2020 16:00:11 +0100 Subject: Simplify Arena to use a generic index --- crates/ra_arena/src/lib.rs | 101 ++++++++++++++++++++++++++++----------------- crates/ra_arena/src/map.rs | 32 +++++++------- 2 files changed, 79 insertions(+), 54 deletions(-) (limited to 'crates/ra_arena') diff --git a/crates/ra_arena/src/lib.rs b/crates/ra_arena/src/lib.rs index fc0f7c12f..ea98d5444 100644 --- a/crates/ra_arena/src/lib.rs +++ b/crates/ra_arena/src/lib.rs @@ -2,6 +2,7 @@ use std::{ fmt, + hash::{Hash, Hasher}, iter::FromIterator, marker::PhantomData, ops::{Index, IndexMut}, @@ -36,86 +37,110 @@ impl fmt::Display for RawId { } } -#[derive(Clone, PartialEq, Eq)] -pub struct Arena { - data: Vec, - _ty: PhantomData, +pub struct Idx { + raw: RawId, + _ty: PhantomData T>, } -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 Clone for Idx { + fn clone(&self) -> Self { + *self } } +impl Copy for Idx {} -#[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 - } +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) + } } -pub trait ArenaId { - fn from_raw(raw: RawId) -> Self; - fn into_raw(self) -> RawId; +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 Arena { - pub const fn new() -> Arena { - Arena { data: Vec::new(), _ty: PhantomData } +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 { +impl Arena { + pub const fn new() -> Arena { + Arena { data: Vec::new() } + } + 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) -> ID { + pub fn alloc(&mut self, value: T) -> Idx { let id = RawId(self.data.len() as u32); self.data.push(value); - ID::from_raw(id) + Idx::from_raw(id) } - pub fn iter(&self) -> impl Iterator + ExactSizeIterator + DoubleEndedIterator { - self.data.iter().enumerate().map(|(idx, value)| (ID::from_raw(RawId(idx as u32)), value)) + pub fn iter( + &self, + ) -> impl Iterator, &T)> + ExactSizeIterator + DoubleEndedIterator { + self.data.iter().enumerate().map(|(idx, value)| (Idx::from_raw(RawId(idx as u32)), value)) } } -impl Default for Arena { - fn default() -> Arena { - Arena { data: Vec::new(), _ty: PhantomData } +impl Default for Arena { + fn default() -> Arena { + Arena { data: Vec::new() } } } -impl Index for Arena { +impl Index> for Arena { type Output = T; - fn index(&self, idx: ID) -> &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: ID) -> &mut T { +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 { +impl FromIterator for Arena { fn from_iter(iter: I) -> Self where I: IntoIterator, { - Arena { data: Vec::from_iter(iter), _ty: PhantomData } + Arena { data: Vec::from_iter(iter) } } } diff --git a/crates/ra_arena/src/map.rs b/crates/ra_arena/src/map.rs index b73d4e365..5e764113d 100644 --- a/crates/ra_arena/src/map.rs +++ b/crates/ra_arena/src/map.rs @@ -2,17 +2,17 @@ use std::marker::PhantomData; -use super::ArenaId; +use crate::Idx; /// A map from arena IDs to some other type. Space requirement is O(highest ID). #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct ArenaMap { - v: Vec>, +pub struct ArenaMap { + v: Vec>, _ty: PhantomData, } -impl ArenaMap { - pub fn insert(&mut self, id: ID, t: T) { +impl ArenaMap, V> { + pub fn insert(&mut self, id: Idx, t: V) { let idx = Self::to_idx(id); if self.v.capacity() <= idx { self.v.reserve(idx + 1 - self.v.capacity()); @@ -25,43 +25,43 @@ impl ArenaMap { self.v[idx] = Some(t); } - pub fn get(&self, id: ID) -> Option<&T> { + pub fn get(&self, id: Idx) -> Option<&V> { self.v.get(Self::to_idx(id)).and_then(|it| it.as_ref()) } - pub fn get_mut(&mut self, id: ID) -> Option<&mut T> { + pub fn get_mut(&mut self, id: Idx) -> Option<&mut V> { self.v.get_mut(Self::to_idx(id)).and_then(|it| it.as_mut()) } - pub fn values(&self) -> impl Iterator { + pub fn values(&self) -> impl Iterator { self.v.iter().filter_map(|o| o.as_ref()) } - pub fn values_mut(&mut self) -> impl Iterator { + pub fn values_mut(&mut self) -> impl Iterator { self.v.iter_mut().filter_map(|o| o.as_mut()) } - pub fn iter(&self) -> impl Iterator { + pub fn iter(&self) -> impl Iterator, &V)> { self.v.iter().enumerate().filter_map(|(idx, o)| Some((Self::from_idx(idx), o.as_ref()?))) } - fn to_idx(id: ID) -> usize { + fn to_idx(id: Idx) -> usize { u32::from(id.into_raw()) as usize } - fn from_idx(idx: usize) -> ID { - ID::from_raw((idx as u32).into()) + fn from_idx(idx: usize) -> Idx { + Idx::from_raw((idx as u32).into()) } } -impl std::ops::Index for ArenaMap { +impl std::ops::Index> for ArenaMap, T> { type Output = T; - fn index(&self, id: ID) -> &T { + fn index(&self, id: Idx) -> &T { self.v[Self::to_idx(id)].as_ref().unwrap() } } -impl Default for ArenaMap { +impl Default for ArenaMap, T> { fn default() -> Self { ArenaMap { v: Vec::new(), _ty: PhantomData } } -- cgit v1.2.3