use crate::value::*;
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::atomic::{AtomicU32, Ordering};
static NEXT_EPOCH: AtomicU32 = AtomicU32::new(1);
pub type Id = u32;
pub const NOT_INTERNED: Id = 1 << 31;
pub const IMMEDIATE: Id = 1 << 30;
const IMM_INT: i64 = 1 << 28;
#[inline]
fn immediate(v: &Value) -> Option<Id> {
match v {
Value::Bool(b) => Some(IMMEDIATE + *b as Id),
Value::Int(i) if (-IMM_INT..IMM_INT).contains(i) => Some(IMMEDIATE + 2 + (*i + IMM_INT) as Id),
_ => None,
}
}
#[inline]
fn immediate_value(x: Id) -> Value {
debug_assert!((IMMEDIATE..NOT_INTERNED).contains(&x));
match x - IMMEDIATE {
0 => Value::Bool(false),
1 => Value::Bool(true),
k => Value::Int((k - 2) as i64 - IMM_INT),
}
}
struct Table {
epoch: u64,
vals: Vec<Value>,
words: Vec<u64>,
shapes: Vec<u32>,
slots: Vec<u32>,
mask: usize,
}
impl Table {
fn new() -> Table {
let epoch = (NEXT_EPOCH.fetch_add(1, Ordering::Relaxed) as u64) << 32;
EPOCH.with(|e| e.set(epoch));
Table { epoch, vals: vec![Value::Undef], words: vec![0], shapes: vec![0], slots: vec![0; 1 << 12], mask: (1 << 12) - 1 }
}
#[inline]
fn is_mine(&self, tag: u64) -> bool {
tag & !0xFFFF_FFFF == self.epoch
}
fn canon(&mut self, v: &Value) -> Option<Value> {
match v {
Value::Int(_) | Value::Bool(_) | Value::Str(_) | Value::Model(_) => Some(v.clone()),
Value::Tuple(t) if self.is_mine(t.canon.get()) => Some(v.clone()),
Value::Record(r) if self.is_mine(r.canon.get()) => Some(v.clone()),
Value::Fcn(f) if self.is_mine(f.canon.get()) => Some(v.clone()),
Value::Set(s) if self.is_mine(s.canon.get()) => Some(v.clone()),
Value::Tuple(_) | Value::Record(_) | Value::Fcn(_) | Value::Set(_) => self.intern(v),
_ => None,
}
}
fn canon_all(&mut self, vs: &[Value]) -> Option<Option<Vec<Value>>> {
let mut out: Option<Vec<Value>> = None;
for (i, x) in vs.iter().enumerate() {
let c = self.canon(x)?;
if out.is_none() && !ident_child(&c, x) {
out = Some(vs[..i].to_vec());
}
if let Some(o) = &mut out {
o.push(c);
}
}
Some(out)
}
fn id(&mut self, v: &Value) -> Option<Id> {
if let Some(x) = immediate(v) {
return Some(x);
}
let c = match v {
Value::Int(_) | Value::Bool(_) | Value::Str(_) | Value::Model(_) => return Some(self.scalar_id(v)),
_ => self.canon(v)?,
};
Some(tag(&c) as Id)
}
fn scalar_id(&mut self, v: &Value) -> Id {
let w = value_word(v).expect("a scalar has a word");
let mut i = (spread(w) as usize) & self.mask;
loop {
let id = self.slots[i] as usize;
if id == 0 {
break;
}
if self.words[id] == w && ident_child(&self.vals[id], v) {
return id as Id;
}
i = (i + 1) & self.mask;
}
self.insert_at(i, v.clone(), w) as Id
}
fn insert_at(&mut self, slot: usize, node: Value, w: u64) -> usize {
let id = self.vals.len();
assert!(id < IMMEDIATE as usize, "more than 2^30 distinct values");
self.slots[slot] = id as u32;
self.shapes.push(Shape::of(&node).pack());
self.vals.push(node);
self.words.push(w);
if self.vals.len() * 2 > self.slots.len() {
self.grow();
}
id
}
#[cold]
fn intern(&mut self, v: &Value) -> Option<Value> {
let node = match v {
Value::Tuple(t) => match self.canon_all(&t.e)? {
None => v.clone(),
Some(e) => Value::Tuple(TupleV::rc(e)),
},
Value::Record(r) => match self.canon_all(&r.vals)? {
None => v.clone(),
Some(vals) => Value::Record(Rc::new(RecordV::new(r.names.clone(), vals))),
},
Value::Fcn(f) => {
let dom = self.canon_all(&f.dom)?;
let vals = self.canon_all(&f.vals)?;
match (dom, vals) {
(None, None) => v.clone(),
(dom, vals) => {
let dom = match dom {
None => f.dom.clone(),
Some(d) => Rc::new(d),
};
Value::Fcn(Rc::new(FcnV::new(dom, vals.unwrap_or_else(|| f.vals.clone()))))
}
}
}
Value::Set(s) => match &s.k {
SetK::Enum(e) => match self.canon_all(e)? {
None => v.clone(),
Some(e) => SetV::of(e),
},
_ => return None,
},
_ => return None,
};
let w = value_word(&node).ok()?;
let mut i = (spread(w) as usize) & self.mask;
loop {
let id = self.slots[i] as usize;
if id == 0 {
break;
}
if self.words[id] == w && same_node(&self.vals[id], &node) {
return Some(self.vals[id].clone());
}
i = (i + 1) & self.mask;
}
let tag = self.epoch | self.vals.len() as u64;
match &node {
Value::Tuple(t) => t.canon.set(tag),
Value::Record(r) => r.canon.set(tag),
Value::Fcn(f) => f.canon.set(tag),
Value::Set(s) => s.canon.set(tag),
_ => unreachable!(),
}
self.insert_at(i, node.clone(), w);
Some(node)
}
fn grow(&mut self) {
let cap = self.slots.len() * 2;
self.slots = vec![0; cap];
self.mask = cap - 1;
for id in 1..self.vals.len() {
let mut i = (spread(self.words[id]) as usize) & self.mask;
while self.slots[i] != 0 {
i = (i + 1) & self.mask;
}
self.slots[i] = id as u32;
}
}
}
#[inline]
fn spread(w: u64) -> u64 {
(w ^ (w >> 29)).wrapping_mul(0xBF58_476D_1CE4_E5B9) >> 7
}
#[inline]
fn ident_child(a: &Value, b: &Value) -> bool {
match (a, b) {
(Value::Int(x), Value::Int(y)) => x == y,
(Value::Bool(x), Value::Bool(y)) => x == y,
(Value::Str(x), Value::Str(y)) => x == y,
(Value::Model(x), Value::Model(y)) => x == y,
(Value::Tuple(x), Value::Tuple(y)) => Rc::ptr_eq(x, y),
(Value::Record(x), Value::Record(y)) => Rc::ptr_eq(x, y),
(Value::Fcn(x), Value::Fcn(y)) => Rc::ptr_eq(x, y),
(Value::Set(x), Value::Set(y)) => Rc::ptr_eq(x, y),
_ => false,
}
}
fn all_ident(a: &[Value], b: &[Value]) -> bool {
a.len() == b.len() && a.iter().zip(b).all(|(x, y)| ident_child(x, y))
}
fn same_node(a: &Value, b: &Value) -> bool {
match (a, b) {
(Value::Tuple(x), Value::Tuple(y)) => all_ident(&x.e, &y.e),
(Value::Record(x), Value::Record(y)) => (Rc::ptr_eq(&x.names, &y.names) || x.names[..] == y.names[..]) && all_ident(&x.vals, &y.vals),
(Value::Fcn(x), Value::Fcn(y)) => (Rc::ptr_eq(&x.dom, &y.dom) || all_ident(&x.dom, &y.dom)) && all_ident(&x.vals, &y.vals),
(Value::Set(x), Value::Set(y)) => match (&x.k, &y.k) {
(SetK::Enum(p), SetK::Enum(q)) => all_ident(p, q),
_ => false,
},
_ => false,
}
}
thread_local! {
static TABLE: RefCell<Table> = RefCell::new(Table::new());
static EPOCH: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
}
#[inline]
pub fn canon(v: &Value) -> Option<Value> {
match v {
Value::Int(_) | Value::Bool(_) | Value::Str(_) | Value::Model(_) => Some(v.clone()),
_ => TABLE.with(|t| t.borrow_mut().canon(v)),
}
}
#[inline]
pub fn id(v: &Value) -> Option<Id> {
match v {
Value::Tuple(t) if is_current(t.canon.get()) => Some(t.canon.get() as Id),
Value::Record(r) if is_current(r.canon.get()) => Some(r.canon.get() as Id),
Value::Fcn(f) if is_current(f.canon.get()) => Some(f.canon.get() as Id),
Value::Set(s) if is_current(s.canon.get()) => Some(s.canon.get() as Id),
_ => TABLE.with(|t| t.borrow_mut().id(v)),
}
}
#[inline]
pub fn id_if_canonical(v: &Value) -> Option<Id> {
if let Some(x) = immediate(v) {
return Some(x);
}
let t = tag(v);
if is_current(t) {
Some(t as Id)
} else {
None
}
}
#[inline]
pub fn value(id: Id) -> Value {
if id >= IMMEDIATE {
return immediate_value(id);
}
TABLE.with(|t| t.borrow().vals[id as usize].clone())
}
#[inline]
pub fn word(id: Id) -> u64 {
if id >= IMMEDIATE {
return value_word(&immediate_value(id)).expect("a scalar has a word");
}
TABLE.with(|t| t.borrow().words[id as usize])
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Shape {
pub kind: ShapeKind,
pub len: u32,
pub bag: bool,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum ShapeKind {
Other,
Fcn,
Set,
}
impl Shape {
fn of(v: &Value) -> Shape {
let other = Shape { kind: ShapeKind::Other, len: 0, bag: false };
let (kind, len, vals): (ShapeKind, usize, &[Value]) = match v {
Value::Tuple(t) => (ShapeKind::Fcn, t.len(), &t.e),
Value::Record(r) => (ShapeKind::Fcn, r.vals.len(), &r.vals),
Value::Fcn(f) => (ShapeKind::Fcn, f.vals.len(), &f.vals),
Value::Set(s) => match &s.k {
SetK::Enum(e) => (ShapeKind::Set, e.len(), &[]),
_ => return other,
},
_ => return other,
};
if len >= 1 << 28 {
return other;
}
let bag = kind == ShapeKind::Fcn && vals.iter().all(|x| matches!(x, Value::Int(n) if *n > 0));
Shape { kind, len: len as u32, bag }
}
fn pack(self) -> u32 {
(self.len << 4) | ((self.bag as u32) << 2) | self.kind as u32
}
fn unpack(x: u32) -> Shape {
let kind = match x & 3 {
1 => ShapeKind::Fcn,
2 => ShapeKind::Set,
_ => ShapeKind::Other,
};
Shape { kind, len: x >> 4, bag: x & 4 != 0 }
}
}
#[inline]
pub fn shape(id: Id) -> Shape {
if id >= IMMEDIATE {
return Shape { kind: ShapeKind::Other, len: 0, bag: false };
}
TABLE.with(|t| Shape::unpack(t.borrow().shapes[id as usize]))
}
#[derive(Clone, Copy)]
pub struct Local(*const RefCell<Table>);
impl Local {
#[inline]
pub fn get() -> Local {
TABLE.with(|t| Local(t as *const RefCell<Table>))
}
#[inline]
fn t(&self) -> &RefCell<Table> {
unsafe { &*self.0 }
}
#[inline]
pub fn word(&self, id: Id) -> u64 {
if id >= IMMEDIATE {
return value_word(&immediate_value(id)).expect("a scalar has a word");
}
self.t().borrow().words[id as usize]
}
#[inline]
pub fn shape(&self, id: Id) -> Shape {
if id >= IMMEDIATE {
return Shape { kind: ShapeKind::Other, len: 0, bag: false };
}
Shape::unpack(self.t().borrow().shapes[id as usize])
}
}
#[inline]
pub fn int_of(x: Id) -> Option<i64> {
if (IMMEDIATE + 2..NOT_INTERNED).contains(&x) {
Some((x - IMMEDIATE - 2) as i64 - IMM_INT)
} else {
None
}
}
#[inline]
pub fn int_id(i: i64) -> Option<Id> {
immediate(&Value::Int(i))
}
#[inline]
pub fn is_immediate(id: Id) -> bool {
(IMMEDIATE..NOT_INTERNED).contains(&id)
}
pub struct Ids<'a> {
vals: &'a [Value],
words: &'a [u64],
}
impl Ids<'_> {
#[inline]
pub fn value(&self, id: Id) -> Value {
if id >= IMMEDIATE { immediate_value(id) } else { self.vals[id as usize].clone() }
}
#[inline]
pub fn word(&self, id: Id) -> u64 {
if id >= IMMEDIATE { value_word(&immediate_value(id)).expect("a scalar has a word") } else { self.words[id as usize] }
}
}
#[inline]
pub fn with_ids<T>(f: impl FnOnce(&Ids) -> T) -> T {
TABLE.with(|t| {
let t = t.borrow();
f(&Ids { vals: &t.vals, words: &t.words })
})
}
#[inline]
fn is_current(tag: u64) -> bool {
tag != 0 && tag & !0xFFFF_FFFF == EPOCH.with(|e| e.get())
}
#[inline]
pub fn canon_or_keep(v: Value) -> Value {
match v {
Value::Int(_) | Value::Bool(_) | Value::Str(_) | Value::Model(_) => v,
_ => canon(&v).unwrap_or(v),
}
}
#[inline]
pub fn tag(v: &Value) -> u64 {
match v {
Value::Tuple(t) => t.canon.get(),
Value::Record(r) => r.canon.get(),
Value::Fcn(f) => f.canon.get(),
Value::Set(s) => s.canon.get(),
_ => 0,
}
}
#[inline]
pub fn ident(a: &Value, b: &Value) -> Option<bool> {
if ident_child(a, b) {
return Some(true);
}
let (ta, tb) = (tag(a), tag(b));
if ta != 0 && tb != 0 && ta >> 32 == tb >> 32 {
return Some(false);
}
match (a, b) {
(Value::Int(_), Value::Int(_)) | (Value::Bool(_), Value::Bool(_)) | (Value::Str(_), Value::Str(_)) => Some(false),
_ => None,
}
}
pub fn reset() {
TABLE.with(|t| *t.borrow_mut() = Table::new());
}
pub fn len() -> usize {
TABLE.with(|t| t.borrow().vals.len() - 1)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::intern::str_id;
fn tup(e: Vec<Value>) -> Value {
Value::Tuple(TupleV::rc(e))
}
#[test]
fn equal_values_share_one_node() {
reset();
let a = canon(&tup(vec![Value::Int(1), tup(vec![Value::Str(str_id("x"))])])).unwrap();
let b = canon(&tup(vec![Value::Int(1), tup(vec![Value::Str(str_id("x"))])])).unwrap();
assert_eq!(ident(&a, &b), Some(true));
let c = canon(&tup(vec![Value::Int(2), tup(vec![Value::Str(str_id("x"))])])).unwrap();
assert_eq!(ident(&a, &c), Some(false));
if let (Value::Tuple(x), Value::Tuple(y)) = (&a, &c) {
assert_eq!(ident(&x.e[1], &y.e[1]), Some(true));
}
}
#[test]
fn interning_keeps_the_fingerprint_word() {
reset();
let raw = SetV::of(vec![Value::Int(1), Value::Int(3)]);
let w = value_word(&SetV::of(vec![Value::Int(1), Value::Int(3)])).unwrap();
let c = canon(&raw).unwrap();
assert_eq!(value_word(&c).unwrap(), w);
}
#[test]
fn intervals_and_lazy_values_are_not_interned() {
reset();
let iv = Value::Set(Rc::new(SetV::new(SetK::Interval(1, 3))));
assert!(canon(&iv).is_none());
assert!(canon(&tup(vec![iv.clone()])).is_none());
let en = canon(&SetV::of(vec![Value::Int(1), Value::Int(2), Value::Int(3)])).unwrap();
assert_eq!(ident(&iv, &en), None);
}
#[test]
fn a_reset_forgets_earlier_nodes() {
reset();
let a = canon(&tup(vec![Value::Int(7)])).unwrap();
reset();
let b = canon(&tup(vec![Value::Int(7)])).unwrap();
assert_eq!(ident(&a, &b), None);
assert_eq!(len(), 1); }
#[test]
fn ids_name_values() {
reset();
let a = id(&tup(vec![Value::Int(1), Value::Str(str_id("y"))])).unwrap();
let b = id(&tup(vec![Value::Int(1), Value::Str(str_id("y"))])).unwrap();
assert_eq!(a, b);
let one = id(&Value::Int(1)).unwrap();
assert_ne!(one, a);
assert_eq!(id(&Value::Int(1)), Some(one));
assert_ne!(id(&Value::Str(str_id("y"))), id(&Value::Model(str_id("y"))));
assert_eq!(word(a), value_word(&value(a)).unwrap());
assert!(id(&Value::Set(Rc::new(SetV::new(SetK::Interval(1, 2))))).is_none());
}
}