pub use semi_persistent_containers::{SparseSetId, UseListId, UseNodeId};
semi_persistent_containers::define_id31! {
pub struct ENodeId / StoredENodeId, "e";
}
semi_persistent_containers::define_id31! {
pub struct EClassKey / StoredEClassKey, "ec";
}
semi_persistent_containers::define_id31! {
pub struct SortId / StoredSortId, "sort";
}
semi_persistent_containers::define_id31! {
pub struct OpId / StoredOpId, "op";
}
semi_persistent_containers::define_id15! {
pub struct RuleId / StoredRuleId, "r";
}
semi_persistent_containers::define_id15! {
pub struct AxiomId / StoredAxiomId, "ax";
}
#[inline]
pub fn id_at<D: crate::containers::DenseId>(pos: usize) -> D {
D::try_new(pos).unwrap_or_else(|| panic!("position {pos} has no id: the id space is exhausted"))
}
#[inline]
pub fn id_at_index<D: crate::containers::DenseId>(pos: D::Index) -> D {
use crate::containers::IndexLike;
id_at(pos.as_usize())
}
#[inline]
pub fn ids_upto<D: crate::containers::DenseId>(n: usize) -> impl Iterator<Item = D> {
if n > 0 {
let _ = id_at::<D>(n - 1);
}
(0..n).map(D::from_usize)
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[repr(u8)]
pub enum ENodeKind {
Plain0 = 0,
Plain1 = 1,
Plain2 = 2,
Plain3 = 3,
PlainN = 4,
SPair = 5,
Seq = 6,
MSet = 7,
Set = 8,
Lit = 9,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::containers::DenseId;
#[test]
fn id_at_covers_the_whole_id_space() {
let bound = 1usize << 15; assert_eq!(id_at::<RuleId>(0).to_usize(), 0);
assert_eq!(id_at::<RuleId>(bound - 1).to_usize(), bound - 1);
}
#[test]
#[should_panic(expected = "the id space is exhausted")]
fn id_at_rejects_a_position_with_no_id() {
let _ = id_at::<RuleId>(1usize << 15);
}
#[test]
fn id_at_index_agrees_with_id_at() {
let pos: <RuleId as DenseId>::Index = 7;
assert_eq!(id_at_index::<RuleId>(pos), id_at::<RuleId>(7));
}
#[test]
#[should_panic(expected = "the id space is exhausted")]
fn id_at_index_rejects_a_position_with_no_id() {
let pos: <RuleId as DenseId>::Index = 1u16 << 15;
let _ = id_at_index::<RuleId>(pos);
}
#[test]
fn ids_upto_yields_every_position_in_order() {
let ids: Vec<usize> = ids_upto::<RuleId>(4).map(|r| r.to_usize()).collect();
assert_eq!(ids, vec![0, 1, 2, 3]);
assert_eq!(ids_upto::<RuleId>(0).count(), 0);
}
#[test]
fn ids_upto_accepts_a_full_id_space() {
let bound = 1usize << 15;
assert_eq!(ids_upto::<RuleId>(bound).count(), bound);
}
#[test]
#[should_panic(expected = "the id space is exhausted")]
fn ids_upto_rejects_a_population_larger_than_the_id_space() {
let _ = ids_upto::<RuleId>((1usize << 15) + 1);
}
}