cl_structures: Rename deprecated_intern_pool to the more correct name "IndexMap"
Also, reverse the order of generic args, to make them consistent with other map collections
This commit is contained in:
parent
0cc0cb5cfb
commit
fa8a71addc
@ -1,10 +1,10 @@
|
||||
//! Trivially-copyable, easily comparable typed indices, and a [Pool] to contain them
|
||||
//! Trivially-copyable, easily comparable typed [indices](MapIndex), and a [Pool] to contain them
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
//! ```rust
|
||||
//! # use cl_structures::deprecated_intern_pool::*;
|
||||
//! // first, create a new InternKey type (this ensures type safety)
|
||||
//! # use cl_structures::index_map::*;
|
||||
//! // first, create a new MapIndex type (this ensures type safety)
|
||||
//! make_intern_key!{
|
||||
//! NumbersKey
|
||||
//! }
|
||||
@ -29,18 +29,16 @@
|
||||
|
||||
/// Creates newtype indices over [`usize`] for use as [Pool] keys.
|
||||
#[macro_export]
|
||||
macro_rules! make_intern_key {($($(#[$meta:meta])* $name:ident),*$(,)?) => {$(
|
||||
macro_rules! make_index {($($(#[$meta:meta])* $name:ident),*$(,)?) => {$(
|
||||
$(#[$meta])*
|
||||
#[repr(transparent)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct $name(usize);
|
||||
|
||||
impl $crate::deprecated_intern_pool::InternKey for $name {
|
||||
impl $crate::index_map::MapIndex for $name {
|
||||
#[doc = concat!("Constructs a [`", stringify!($name), "`] from a [`usize`] without checking bounds.\n")]
|
||||
/// # Safety
|
||||
///
|
||||
/// The provided value should be within the bounds of its associated container
|
||||
fn from_raw_unchecked(value: usize) -> Self {
|
||||
fn from_usize(value: usize) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
fn get(&self) -> usize {
|
||||
@ -56,93 +54,93 @@ macro_rules! make_intern_key {($($(#[$meta:meta])* $name:ident),*$(,)?) => {$(
|
||||
use core::slice::GetManyMutError;
|
||||
use std::ops::{Index, IndexMut};
|
||||
|
||||
pub use make_intern_key;
|
||||
pub use make_index;
|
||||
|
||||
use self::iter::InternKeyIter;
|
||||
use self::iter::MapIndexIter;
|
||||
|
||||
/// An index into a [Pool]. For full type-safety,
|
||||
/// there should be a unique [InternKey] for each [Pool]
|
||||
pub trait InternKey: std::fmt::Debug {
|
||||
/// Constructs an [`InternKey`] from a [`usize`] without checking bounds.
|
||||
pub trait MapIndex: std::fmt::Debug {
|
||||
/// Constructs an [`MapIndex`] from a [`usize`] without checking bounds.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The provided value should be within the bounds of its associated container.
|
||||
// ID::from_raw_unchecked here isn't *actually* unsafe, since bounds should always be
|
||||
// checked, however, the function has unverifiable preconditions.
|
||||
fn from_raw_unchecked(value: usize) -> Self;
|
||||
fn from_usize(value: usize) -> Self;
|
||||
/// Gets the index of the [`InternKey`] by value
|
||||
fn get(&self) -> usize;
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Pool<T, ID: InternKey> {
|
||||
pool: Vec<T>,
|
||||
id_type: std::marker::PhantomData<ID>,
|
||||
pub struct IndexMap<K: MapIndex, V> {
|
||||
pool: Vec<V>,
|
||||
id_type: std::marker::PhantomData<K>,
|
||||
}
|
||||
|
||||
impl<T, ID: InternKey> Pool<T, ID> {
|
||||
impl<V, K: MapIndex> IndexMap<K, V> {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
pub fn get(&self, index: ID) -> Option<&T> {
|
||||
pub fn get(&self, index: K) -> Option<&V> {
|
||||
self.pool.get(index.get())
|
||||
}
|
||||
pub fn get_mut(&mut self, index: ID) -> Option<&mut T> {
|
||||
pub fn get_mut(&mut self, index: K) -> Option<&mut V> {
|
||||
self.pool.get_mut(index.get())
|
||||
}
|
||||
|
||||
pub fn get_many_mut<const N: usize>(
|
||||
&mut self,
|
||||
indices: [ID; N],
|
||||
) -> Result<[&mut T; N], GetManyMutError<N>> {
|
||||
indices: [K; N],
|
||||
) -> Result<[&mut V; N], GetManyMutError<N>> {
|
||||
self.pool.get_many_mut(indices.map(|id| id.get()))
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> impl Iterator<Item = &T> {
|
||||
pub fn iter(&self) -> impl Iterator<Item = &V> {
|
||||
self.pool.iter()
|
||||
}
|
||||
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
|
||||
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut V> {
|
||||
self.pool.iter_mut()
|
||||
}
|
||||
pub fn key_iter(&self) -> iter::InternKeyIter<ID> {
|
||||
pub fn key_iter(&self) -> iter::MapIndexIter<K> {
|
||||
// Safety: Pool currently has pool.len() entries, and data cannot be removed
|
||||
InternKeyIter::new(0..self.pool.len())
|
||||
MapIndexIter::new(0..self.pool.len())
|
||||
}
|
||||
|
||||
/// Constructs an [ID](InternKey) from a [usize], if it's within bounds
|
||||
#[doc(hidden)]
|
||||
pub fn try_key_from(&self, value: usize) -> Option<ID> {
|
||||
(value < self.pool.len()).then(|| ID::from_raw_unchecked(value))
|
||||
pub fn try_key_from(&self, value: usize) -> Option<K> {
|
||||
(value < self.pool.len()).then(|| K::from_usize(value))
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, value: T) -> ID {
|
||||
pub fn insert(&mut self, value: V) -> K {
|
||||
let id = self.pool.len();
|
||||
self.pool.push(value);
|
||||
|
||||
// Safety: value was pushed to `self.pool[id]`
|
||||
ID::from_raw_unchecked(id)
|
||||
K::from_usize(id)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, ID: InternKey> Default for Pool<T, ID> {
|
||||
impl<K: MapIndex, V> Default for IndexMap<K, V> {
|
||||
fn default() -> Self {
|
||||
Self { pool: vec![], id_type: std::marker::PhantomData }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, ID: InternKey> Index<ID> for Pool<T, ID> {
|
||||
type Output = T;
|
||||
impl<K: MapIndex, V> Index<K> for IndexMap<K, V> {
|
||||
type Output = V;
|
||||
|
||||
fn index(&self, index: ID) -> &Self::Output {
|
||||
fn index(&self, index: K) -> &Self::Output {
|
||||
match self.pool.get(index.get()) {
|
||||
None => panic!("Index {:?} out of bounds in pool!", index),
|
||||
Some(value) => value,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T, ID: InternKey> IndexMut<ID> for Pool<T, ID> {
|
||||
fn index_mut(&mut self, index: ID) -> &mut Self::Output {
|
||||
impl<K: MapIndex, V> IndexMut<K> for IndexMap<K, V> {
|
||||
fn index_mut(&mut self, index: K) -> &mut Self::Output {
|
||||
match self.pool.get_mut(index.get()) {
|
||||
None => panic!("Index {:?} out of bounds in pool!", index),
|
||||
Some(value) => value,
|
||||
@ -153,7 +151,7 @@ impl<T, ID: InternKey> IndexMut<ID> for Pool<T, ID> {
|
||||
mod iter {
|
||||
use std::{marker::PhantomData, ops::Range};
|
||||
|
||||
use super::InternKey;
|
||||
use super::MapIndex;
|
||||
|
||||
/// Iterates over the keys of a [Pool](super::Pool) independently of the pool itself.
|
||||
///
|
||||
@ -161,12 +159,12 @@ mod iter {
|
||||
/// but is *NOT* guaranteed to iterate over all elements of the pool
|
||||
/// if the pool is extended during iteration.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct InternKeyIter<ID: InternKey> {
|
||||
pub struct MapIndexIter<K: MapIndex> {
|
||||
range: Range<usize>,
|
||||
_id: PhantomData<ID>,
|
||||
_id: PhantomData<K>,
|
||||
}
|
||||
|
||||
impl<ID: InternKey> InternKeyIter<ID> {
|
||||
impl<K: MapIndex> MapIndexIter<K> {
|
||||
/// Creates a new [InternKeyIter] producing the given [InternKey]
|
||||
///
|
||||
/// # Safety:
|
||||
@ -178,22 +176,22 @@ mod iter {
|
||||
}
|
||||
}
|
||||
|
||||
impl<ID: InternKey> Iterator for InternKeyIter<ID> {
|
||||
impl<ID: MapIndex> Iterator for MapIndexIter<ID> {
|
||||
type Item = ID;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
// Safety: InternKeyIter can only be created by InternKeyIter::new()
|
||||
Some(ID::from_raw_unchecked(self.range.next()?))
|
||||
Some(ID::from_usize(self.range.next()?))
|
||||
}
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.range.size_hint()
|
||||
}
|
||||
}
|
||||
impl<ID: InternKey> DoubleEndedIterator for InternKeyIter<ID> {
|
||||
impl<ID: MapIndex> DoubleEndedIterator for MapIndexIter<ID> {
|
||||
fn next_back(&mut self) -> Option<Self::Item> {
|
||||
// Safety: see above
|
||||
Some(ID::from_raw_unchecked(self.range.next_back()?))
|
||||
Some(ID::from_usize(self.range.next_back()?))
|
||||
}
|
||||
}
|
||||
impl<ID: InternKey> ExactSizeIterator for InternKeyIter<ID> {}
|
||||
impl<ID: MapIndex> ExactSizeIterator for MapIndexIter<ID> {}
|
||||
}
|
@ -18,4 +18,4 @@ pub mod tree;
|
||||
|
||||
pub mod stack;
|
||||
|
||||
pub mod deprecated_intern_pool;
|
||||
pub mod index_map;
|
||||
|
@ -1,7 +1,7 @@
|
||||
use cl_structures::deprecated_intern_pool::*;
|
||||
use cl_structures::index_map::*;
|
||||
|
||||
// define the index types
|
||||
make_intern_key! {
|
||||
make_index! {
|
||||
/// Uniquely represents a [Def][1] in the [Def][1] [Pool]
|
||||
///
|
||||
/// [1]: crate::definition::Def
|
||||
|
@ -1,7 +1,7 @@
|
||||
//! A [Module] is a node in the Module Tree (a component of a
|
||||
//! [Project](crate::project::Project))
|
||||
use cl_ast::Sym;
|
||||
use cl_structures::deprecated_intern_pool::InternKey;
|
||||
use cl_structures::index_map::MapIndex;
|
||||
|
||||
use crate::key::DefID;
|
||||
use std::collections::HashMap;
|
||||
|
@ -7,7 +7,7 @@ use crate::{
|
||||
path::Path,
|
||||
};
|
||||
use cl_ast::PathPart;
|
||||
use cl_structures::deprecated_intern_pool::Pool;
|
||||
use cl_structures::index_map::IndexMap;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
ops::{Index, IndexMut},
|
||||
@ -17,7 +17,7 @@ use self::evaluate::EvaluableTypeExpression;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Project<'a> {
|
||||
pub pool: Pool<Def<'a>, DefID>,
|
||||
pub pool: IndexMap<DefID, Def<'a>>,
|
||||
/// Stores anonymous tuples, function pointer types, etc.
|
||||
pub anon_types: HashMap<TypeKind, DefID>,
|
||||
pub root: DefID,
|
||||
@ -33,7 +33,7 @@ impl Default for Project<'_> {
|
||||
fn default() -> Self {
|
||||
const ROOT_PATH: cl_ast::Path = cl_ast::Path { absolute: true, parts: Vec::new() };
|
||||
|
||||
let mut pool = Pool::default();
|
||||
let mut pool = IndexMap::default();
|
||||
let root = pool.insert(Def {
|
||||
module: Default::default(),
|
||||
kind: DefKind::Type(TypeKind::Module),
|
||||
|
Loading…
Reference in New Issue
Block a user