Use ‘index’ terminology for arena consistently

This commit is contained in:
Aramis Razzaghipour 2021-01-15 11:02:08 +11:00
parent 3224ecea87
commit 1d103cf087
No known key found for this signature in database
GPG Key ID: F788F7E990136003
6 changed files with 56 additions and 53 deletions

View File

@ -13,7 +13,7 @@
//! See also a neighboring `body` module. //! See also a neighboring `body` module.
use hir_expand::name::Name; use hir_expand::name::Name;
use la_arena::{Idx, RawId}; use la_arena::{Idx, RawIdx};
use syntax::ast::RangeOp; use syntax::ast::RangeOp;
use crate::{ use crate::{
@ -24,7 +24,7 @@ use crate::{
pub type ExprId = Idx<Expr>; pub type ExprId = Idx<Expr>;
pub(crate) fn dummy_expr_id() -> ExprId { pub(crate) fn dummy_expr_id() -> ExprId {
ExprId::from_raw(RawId::from(!0)) ExprId::from_raw(RawIdx::from(!0))
} }
pub type PatId = Idx<Pat>; pub type PatId = Idx<Pat>;

View File

@ -20,7 +20,7 @@ use hir_expand::{
name::{name, AsName, Name}, name::{name, AsName, Name},
HirFileId, InFile, HirFileId, InFile,
}; };
use la_arena::{Arena, Idx, RawId}; use la_arena::{Arena, Idx, RawIdx};
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use smallvec::SmallVec; use smallvec::SmallVec;
use syntax::{ast, match_ast}; use syntax::{ast, match_ast};

View File

@ -683,12 +683,12 @@ impl Ctx {
} }
fn next_field_idx(&self) -> Idx<Field> { fn next_field_idx(&self) -> Idx<Field> {
Idx::from_raw(RawId::from( Idx::from_raw(RawIdx::from(
self.tree.data.as_ref().map_or(0, |data| data.fields.len() as u32), self.tree.data.as_ref().map_or(0, |data| data.fields.len() as u32),
)) ))
} }
fn next_variant_idx(&self) -> Idx<Variant> { fn next_variant_idx(&self) -> Idx<Variant> {
Idx::from_raw(RawId::from( Idx::from_raw(RawIdx::from(
self.tree.data.as_ref().map_or(0, |data| data.variants.len() as u32), self.tree.data.as_ref().map_or(0, |data| data.variants.len() as u32),
)) ))
} }

View File

@ -9,7 +9,7 @@
//! absolute offsets. The `Trace` structure (inspired, at least in name, by //! absolute offsets. The `Trace` structure (inspired, at least in name, by
//! Kotlin's `BindingTrace`) allows use the same code to compute both //! Kotlin's `BindingTrace`) allows use the same code to compute both
//! projections. //! projections.
use la_arena::{Arena, ArenaMap, Idx, RawId}; use la_arena::{Arena, ArenaMap, Idx, RawIdx};
pub(crate) struct Trace<T, V> { pub(crate) struct Trace<T, V> {
arena: Option<Arena<T>>, arena: Option<Arena<T>>,
@ -30,7 +30,7 @@ impl<T, V> Trace<T, V> {
let id = if let Some(arena) = &mut self.arena { let id = if let Some(arena) = &mut self.arena {
arena.alloc(data()) arena.alloc(data())
} else { } else {
let id = Idx::<T>::from_raw(RawId::from(self.len)); let id = Idx::<T>::from_raw(RawIdx::from(self.len));
self.len += 1; self.len += 1;
id id
}; };

View File

@ -1,4 +1,4 @@
//! Yet another ID-based arena. //! Yet another index-based arena.
#![warn(missing_docs)] #![warn(missing_docs)]
@ -13,37 +13,37 @@ use std::{
mod map; mod map;
pub use map::ArenaMap; pub use map::ArenaMap;
/// The raw ID of a value in an arena. /// The raw index of a value in an arena.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawId(u32); pub struct RawIdx(u32);
impl From<RawId> for u32 { impl From<RawIdx> for u32 {
fn from(raw: RawId) -> u32 { fn from(raw: RawIdx) -> u32 {
raw.0 raw.0
} }
} }
impl From<u32> for RawId { impl From<u32> for RawIdx {
fn from(id: u32) -> RawId { fn from(idx: u32) -> RawIdx {
RawId(id) RawIdx(idx)
} }
} }
impl fmt::Debug for RawId { impl fmt::Debug for RawIdx {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f) self.0.fmt(f)
} }
} }
impl fmt::Display for RawId { impl fmt::Display for RawIdx {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f) self.0.fmt(f)
} }
} }
/// The ID of a value allocated in an arena that holds `T`s. /// The index of a value allocated in an arena that holds `T`s.
pub struct Idx<T> { pub struct Idx<T> {
raw: RawId, raw: RawIdx,
_ty: PhantomData<fn() -> T>, _ty: PhantomData<fn() -> T>,
} }
@ -78,18 +78,18 @@ impl<T> fmt::Debug for Idx<T> {
} }
impl<T> Idx<T> { impl<T> Idx<T> {
/// Creates a new ID from a [`RawId`]. /// Creates a new index from a [`RawIdx`].
pub fn from_raw(raw: RawId) -> Self { pub fn from_raw(raw: RawIdx) -> Self {
Idx { raw, _ty: PhantomData } Idx { raw, _ty: PhantomData }
} }
/// Converts this ID into the underlying [`RawId`]. /// Converts this index into the underlying [`RawIdx`].
pub fn into_raw(self) -> RawId { pub fn into_raw(self) -> RawIdx {
self.raw self.raw
} }
} }
/// Yet another ID-based arena. /// Yet another index-based arena.
#[derive(Clone, PartialEq, Eq)] #[derive(Clone, PartialEq, Eq)]
pub struct Arena<T> { pub struct Arena<T> {
data: Vec<T>, data: Vec<T>,
@ -161,37 +161,37 @@ impl<T> Arena<T> {
self.data.is_empty() self.data.is_empty()
} }
/// Allocates a new value on the arena, returning the values ID. /// Allocates a new value on the arena, returning the values index.
/// ///
/// ``` /// ```
/// let mut arena = la_arena::Arena::new(); /// let mut arena = la_arena::Arena::new();
/// let id = arena.alloc(50); /// let idx = arena.alloc(50);
/// ///
/// assert_eq!(arena[id], 50); /// assert_eq!(arena[idx], 50);
/// ``` /// ```
pub fn alloc(&mut self, value: T) -> Idx<T> { pub fn alloc(&mut self, value: T) -> Idx<T> {
let id = RawId(self.data.len() as u32); let idx = RawIdx(self.data.len() as u32);
self.data.push(value); self.data.push(value);
Idx::from_raw(id) Idx::from_raw(idx)
} }
/// Returns an iterator over the arenas elements. /// Returns an iterator over the arenas elements.
/// ///
/// ``` /// ```
/// let mut arena = la_arena::Arena::new(); /// let mut arena = la_arena::Arena::new();
/// let id1 = arena.alloc(20); /// let idx1 = arena.alloc(20);
/// let id2 = arena.alloc(40); /// let idx2 = arena.alloc(40);
/// let id3 = arena.alloc(60); /// let idx3 = arena.alloc(60);
/// ///
/// let mut iterator = arena.iter(); /// let mut iterator = arena.iter();
/// assert_eq!(iterator.next(), Some((id1, &20))); /// assert_eq!(iterator.next(), Some((idx1, &20)));
/// assert_eq!(iterator.next(), Some((id2, &40))); /// assert_eq!(iterator.next(), Some((idx2, &40)));
/// assert_eq!(iterator.next(), Some((id3, &60))); /// assert_eq!(iterator.next(), Some((idx3, &60)));
/// ``` /// ```
pub fn iter( pub fn iter(
&self, &self,
) -> impl Iterator<Item = (Idx<T>, &T)> + ExactSizeIterator + DoubleEndedIterator { ) -> impl Iterator<Item = (Idx<T>, &T)> + ExactSizeIterator + DoubleEndedIterator {
self.data.iter().enumerate().map(|(idx, value)| (Idx::from_raw(RawId(idx as u32)), value)) self.data.iter().enumerate().map(|(idx, value)| (Idx::from_raw(RawIdx(idx as u32)), value))
} }
/// Reallocates the arena to make it take up as little space as possible. /// Reallocates the arena to make it take up as little space as possible.

View File

@ -2,30 +2,33 @@ use std::marker::PhantomData;
use crate::Idx; use crate::Idx;
/// A map from arena IDs to some other type. Space requirement is O(highest ID). /// A map from arena indexes to some other type.
/// Space requirement is O(highest index).
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ArenaMap<ID, V> { pub struct ArenaMap<IDX, V> {
v: Vec<Option<V>>, v: Vec<Option<V>>,
_ty: PhantomData<ID>, _ty: PhantomData<IDX>,
} }
impl<T, V> ArenaMap<Idx<T>, V> { impl<T, V> ArenaMap<Idx<T>, V> {
/// Inserts a value associated with a given arena ID into the map. /// Inserts a value associated with a given arena index into the map.
pub fn insert(&mut self, id: Idx<T>, t: V) { pub fn insert(&mut self, idx: Idx<T>, t: V) {
let idx = Self::to_idx(id); let idx = Self::to_idx(idx);
self.v.resize_with((idx + 1).max(self.v.len()), || None); self.v.resize_with((idx + 1).max(self.v.len()), || None);
self.v[idx] = Some(t); self.v[idx] = Some(t);
} }
/// Returns a reference to the value associated with the provided ID if it is present. /// Returns a reference to the value associated with the provided index
pub fn get(&self, id: Idx<T>) -> Option<&V> { /// if it is present.
self.v.get(Self::to_idx(id)).and_then(|it| it.as_ref()) pub fn get(&self, idx: Idx<T>) -> Option<&V> {
self.v.get(Self::to_idx(idx)).and_then(|it| it.as_ref())
} }
/// Returns a mutable reference to the value associated with the provided ID if it is present. /// Returns a mutable reference to the value associated with the provided index
pub fn get_mut(&mut self, id: Idx<T>) -> Option<&mut V> { /// if it is present.
self.v.get_mut(Self::to_idx(id)).and_then(|it| it.as_mut()) 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())
} }
/// Returns an iterator over the values in the map. /// Returns an iterator over the values in the map.
@ -38,13 +41,13 @@ impl<T, V> ArenaMap<Idx<T>, V> {
self.v.iter_mut().filter_map(|o| o.as_mut()) self.v.iter_mut().filter_map(|o| o.as_mut())
} }
/// Returns an iterator over the arena IDs and values in the map. /// Returns an iterator over the arena indexes and values in the map.
pub fn iter(&self) -> impl Iterator<Item = (Idx<T>, &V)> { pub fn iter(&self) -> impl Iterator<Item = (Idx<T>, &V)> {
self.v.iter().enumerate().filter_map(|(idx, o)| Some((Self::from_idx(idx), o.as_ref()?))) self.v.iter().enumerate().filter_map(|(idx, o)| Some((Self::from_idx(idx), o.as_ref()?)))
} }
fn to_idx(id: Idx<T>) -> usize { fn to_idx(idx: Idx<T>) -> usize {
u32::from(id.into_raw()) as usize u32::from(idx.into_raw()) as usize
} }
fn from_idx(idx: usize) -> Idx<T> { fn from_idx(idx: usize) -> Idx<T> {
@ -54,8 +57,8 @@ impl<T, V> ArenaMap<Idx<T>, V> {
impl<T, V> std::ops::Index<Idx<V>> for ArenaMap<Idx<V>, T> { impl<T, V> std::ops::Index<Idx<V>> for ArenaMap<Idx<V>, T> {
type Output = T; type Output = T;
fn index(&self, id: Idx<V>) -> &T { fn index(&self, idx: Idx<V>) -> &T {
self.v[Self::to_idx(id)].as_ref().unwrap() self.v[Self::to_idx(idx)].as_ref().unwrap()
} }
} }