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