use crate::hashcons::{self as hc, Id, NOT_INTERNED};
use crate::value::*;
use std::cell::RefCell;
#[derive(Clone)]
pub enum Loc {
Whole(u32),
Elem(u32, Value),
Member(u32, Value),
}
#[derive(Clone)]
pub enum DOp {
Bag { e: Value, plus: bool },
Set { e: Value, add: bool },
Exc { path: Vec<Value>, v: Value },
}
#[derive(Clone)]
pub enum Write {
Delta(Vec<DOp>),
Val(Value),
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct LocId {
pub var: u32,
pub kind: LocKind,
pub key: Id,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum LocKind {
Whole,
Elem,
Member,
}
pub const ABSENT: Id = u32::MAX;
pub const IN: Id = u32::MAX - 1;
pub const OUT: Id = u32::MAX - 2;
pub enum Put {
Id { id: Id, word: u64 },
Delta { id: u64, ops: Box<[DOp]>, iops: Option<Box<[IdOp]>>, shift: Option<u64> },
}
pub enum IdOp {
Bag { e: Id, we: u64, plus: bool },
Set { e: Id, we: u64, add: bool },
Exc { path: Box<[(Id, u64)]>, wv: u64 },
}
fn id_op(op: &DOp) -> Option<IdOp> {
Some(match op {
DOp::Bag { e, plus } => IdOp::Bag { e: hc::id(e)?, we: value_word(e).ok()?, plus: *plus },
DOp::Set { e, add } => IdOp::Set { e: hc::id(e)?, we: value_word(e).ok()?, add: *add },
DOp::Exc { path, v } => IdOp::Exc { path: path.iter().map(|k| Some((hc::id(k)?, value_word(k).ok()?))).collect::<Option<Vec<_>>>()?.into_boxed_slice(), wv: value_word(v).ok()? },
})
}
pub struct Tmpl {
pub unit: u32,
pub bound: Box<[Id]>,
pub locs: Box<[LocId]>,
pub version: u64,
}
pub struct Leaf {
pub version: u64,
pub key: Box<[Id]>,
pub succs: Box<[Box<[(u32, Put)]>]>,
}
pub enum Entry {
Leaf(Leaf),
Tmpl(Tmpl),
}
pub fn vsize(v: &Value) -> usize {
16 + match v {
Value::Tuple(t) => 48 + t.iter().map(vsize).sum::<usize>(),
Value::Record(r) => 56 + r.vals.iter().map(vsize).sum::<usize>(),
Value::Fcn(f) => 96 + f.dom.iter().map(vsize).sum::<usize>() + f.vals.iter().map(vsize).sum::<usize>(),
Value::Set(s) => match &s.k {
SetK::Enum(e) => 80 + e.iter().map(vsize).sum::<usize>(),
SetK::Interval(..) => 80,
_ => usize::MAX / 4,
},
Value::Int(_) | Value::Bool(_) | Value::Str(_) | Value::Model(_) | Value::Undef => 0,
_ => usize::MAX / 4,
}
}
fn ops_size(ops: &[DOp]) -> usize {
ops.iter()
.map(|o| {
40 + match o {
DOp::Bag { e, .. } | DOp::Set { e, .. } => vsize(e),
DOp::Exc { path, v } => 24 + path.iter().map(vsize).sum::<usize>() + vsize(v),
}
})
.sum()
}
fn iops_size(ops: &[IdOp]) -> usize {
ops.iter()
.map(|o| {
24 + match o {
IdOp::Exc { path, .. } => 16 * path.len(),
_ => 0,
}
})
.sum()
}
fn entry_size(e: &Entry) -> usize {
64 + match e {
Entry::Tmpl(t) => 40 + 4 * t.bound.len() + 12 * t.locs.len(),
Entry::Leaf(l) => {
40 + 4 * l.key.len()
+ l.succs
.iter()
.map(|s| {
16 + s
.iter()
.map(|(_, p)| {
24 + match p {
Put::Id { .. } => 0,
Put::Delta { ops, iops, .. } => ops_size(ops) + iops.as_deref().map_or(0, iops_size),
}
})
.sum::<usize>()
})
.sum::<usize>()
}
}
}
#[derive(Default, Clone, Copy)]
pub struct CacheStats {
pub lookups: u64,
pub hits: u64,
pub stored: u64,
pub replaced: u64,
pub too_big: u64,
pub uncacheable: u64,
}
thread_local! {
static NEXT_ID: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
}
fn fresh() -> u64 {
NEXT_ID.with(|c| {
let n = c.get() + 1;
c.set(n);
n
})
}
pub struct Memo<T: Copy> {
slots: Box<[std::cell::Cell<(u64, Id, T)>]>,
mask: usize,
}
unsafe trait Zeroable {}
unsafe impl Zeroable for u32 {}
unsafe impl Zeroable for u64 {}
unsafe impl<T: Zeroable> Zeroable for std::cell::Cell<T> {}
unsafe impl<A: Zeroable, B: Zeroable, C: Zeroable> Zeroable for (A, B, C) {}
const _: () = {
let none: Option<std::rc::Rc<Slot>> = None;
let bytes: [u8; std::mem::size_of::<Option<std::rc::Rc<Slot>>>()] = unsafe { std::mem::transmute(none) };
let mut i = 0;
while i < bytes.len() {
assert!(bytes[i] == 0, "None of an Option<Rc<_>> is not zero bits: Bucket is not Zeroable");
i += 1;
}
};
unsafe impl Zeroable for Bucket {}
fn zeroed<T: Zeroable>(n: usize) -> Box<[T]> {
if n == 0 || std::mem::size_of::<T>() == 0 {
return (0..n).map(|_| unsafe { std::mem::zeroed() }).collect();
}
let layout = std::alloc::Layout::array::<T>(n).expect("a table of a size that fits");
unsafe {
let p = std::alloc::alloc_zeroed(layout) as *mut T;
if p.is_null() {
std::alloc::handle_alloc_error(layout);
}
Box::from_raw(std::ptr::slice_from_raw_parts_mut(p, n))
}
}
impl<T: Copy + Default> Memo<T> {
fn new(n: usize) -> Memo<T>
where
T: Zeroable,
{
Memo { slots: zeroed(n), mask: n - 1 }
}
#[inline]
fn get(&self, a: u64, b: Id) -> Option<T> {
let s = self.slots[(mix(a, b as u64) as usize) & self.mask].get();
(s.0 == a && s.1 == b).then_some(s.2)
}
#[inline]
fn put(&self, a: u64, b: Id, v: T) {
self.slots[(mix(a, b as u64) as usize) & self.mask].set((a, b, v));
}
}
pub type Applied = Memo<Id>;
pub type Words = Memo<u64>;
pub type Located = Memo<Id>;
pub type Slot = (u64, usize, Entry);
const DOMS: usize = 1 << 12;
const WAYS: usize = 4;
#[repr(align(64))]
#[derive(Default)]
struct Bucket {
h: [u64; WAYS],
e: [Option<std::rc::Rc<Slot>>; WAYS],
}
pub struct Table {
buckets: Box<[Bucket]>,
mask: usize,
pub applied: Applied,
pub words: Words,
doms: RefCell<Vec<(Id, Option<std::rc::Rc<[Id]>>)>>,
pub located: Located,
pub used: usize,
pub budget: usize,
pub peak: usize,
pub n: CacheStats,
pub memo: [std::cell::Cell<u64>; 4],
lists: std::collections::HashMap<Box<[Id]>, u64>,
}
impl Table {
pub fn new(budget_bytes: usize) -> Table {
let mut slots = (budget_bytes / 512).max(16 * WAYS);
if !slots.is_power_of_two() {
slots = slots.next_power_of_two() / 2;
}
let v = zeroed::<Bucket>(slots / WAYS);
let used = slots / WAYS * std::mem::size_of::<Bucket>();
Table {
buckets: v,
mask: slots / WAYS - 1,
applied: Applied::new(1 << 18),
words: Words::new(1 << 15),
doms: RefCell::new(vec![(0, None); DOMS]),
located: Located::new(1 << 16),
used,
budget: budget_bytes.max(used),
peak: used,
n: CacheStats::default(),
memo: Default::default(),
lists: Default::default(),
}
}
#[inline]
fn slot(&self, h: u64) -> Option<&std::rc::Rc<Slot>> {
let b = &self.buckets[(h as usize) & self.mask];
(0..WAYS).find(|&w| b.h[w] == h).and_then(|w| b.e[w].as_ref())
}
#[inline]
pub fn get(&self, h: u64) -> Option<&Entry> {
self.slot(h).map(|b| &b.2)
}
pub fn put(&mut self, h: u64, e: Entry) {
let size = entry_size(&e);
let b = &self.buckets[(h as usize) & self.mask];
let w = (0..WAYS)
.find(|&w| b.h[w] == h && b.e[w].is_some())
.or_else(|| (0..WAYS).find(|&w| b.e[w].is_none()))
.or_else(|| (0..WAYS).rev().find(|&w| matches!(b.e[w].as_deref(), Some((_, _, Entry::Leaf(_))))))
.unwrap_or(WAYS - 1);
let old = b.e[w].as_ref().map_or(0, |x| x.1);
if size > 4 * 1024 || self.used - old + size > self.budget {
self.n.too_big += 1;
return;
}
if old > 0 {
self.n.replaced += 1;
}
self.used = self.used - old + size;
self.peak = self.peak.max(self.used);
let b = &mut self.buckets[(h as usize) & self.mask];
b.e[w] = None;
b.h[..=w].rotate_right(1);
b.e[..=w].rotate_right(1);
b.h[0] = h;
b.e[0] = Some(std::rc::Rc::new((h, size, e)));
self.n.stored += 1;
}
fn list_number(&mut self, ops: &[DOp]) -> Option<u64> {
let mut key = Vec::with_capacity(ops.len() * 3);
for o in ops {
match o {
DOp::Bag { e, plus } => key.extend([1 + *plus as Id, hc::id(e)?]),
DOp::Set { e, add } => key.extend([3 + *add as Id, hc::id(e)?]),
DOp::Exc { path, v } => {
key.extend([5, path.len() as Id, hc::id(v)?]);
for p in path {
key.push(hc::id(p)?);
}
}
}
}
if self.lists.len() >= 1 << 20 {
self.lists.clear();
}
Some(*self.lists.entry(key.into_boxed_slice()).or_insert_with(fresh))
}
#[inline]
fn count(&self, i: usize) {
self.memo[i].set(self.memo[i].get() + 1);
}
#[inline]
pub fn template(&self, h0: u64, unit: u32, bound: &[Id]) -> Option<&Tmpl> {
match self.get(h0) {
Some(Entry::Tmpl(t)) if t.unit == unit && same_ids(&t.bound, bound) => Some(t),
_ => None,
}
}
#[inline]
pub fn leaf(&self, version: u64, key: &[Id]) -> Option<&Leaf> {
match self.get(leaf_hash(version, key)) {
Some(Entry::Leaf(l)) if l.version == version && same_ids(&l.key, key) => Some(l),
_ => None,
}
}
#[inline]
pub fn leaf_slot(&self, h: u64, version: u64, key: &[Id]) -> Option<(&std::rc::Rc<Slot>, &Leaf)> {
match self.slot(h) {
Some(b) => match &b.2 {
Entry::Leaf(l) if l.version == version && same_ids(&l.key, key) => Some((b, l)),
_ => None,
},
None => None,
}
}
#[inline(always)]
pub fn locate(&self, cur: &[Id], loc: LocId) -> Option<Id> {
let c = cur[loc.var as usize];
if c >= NOT_INTERNED {
return None;
}
if loc.kind == LocKind::Whole {
return Some(c);
}
self.locate_in(c, loc.kind, loc.key)
}
#[inline(always)]
pub fn locate_in(&self, c: Id, kind: LocKind, key: Id) -> Option<Id> {
let k = (((c as u64) << 32) | key as u64) + 1;
self.count(2);
if let Some(r) = self.located.get(k, kind as Id) {
return Some(r);
}
self.locate_miss(c, kind, key, k)
}
#[cold]
#[inline(never)]
fn locate_miss(&self, c: Id, kind: LocKind, key: Id, k: u64) -> Option<Id> {
self.count(3);
let r = locate_slow(c, LocId { var: 0, kind, key })?;
self.located.put(k, kind as Id, r);
Some(r)
}
#[inline]
pub fn domain_ids(&self, f: Id) -> Option<std::rc::Rc<[Id]>> {
if f >= NOT_INTERNED || hc::is_immediate(f) {
return None;
}
let slot = (mix(f as u64, 0xD0) as usize) & (DOMS - 1);
if let (x, Some(d)) = &self.doms.borrow()[slot] {
if *x == f {
return Some(d.clone());
}
}
let d: std::rc::Rc<[Id]> = match hc::value(f) {
Value::Fcn(fc) => fc.dom.iter().map(hc::id_if_canonical).collect::<Option<Vec<Id>>>()?.into(),
Value::Tuple(t) => (1..=t.len() as i64).map(|i| hc::int_id(i)).collect::<Option<Vec<Id>>>()?.into(),
Value::Record(r) => r.names.iter().map(|nm| hc::id(&Value::Str(*nm))).collect::<Option<Vec<Id>>>()?.into(),
_ => return None,
};
self.doms.borrow_mut()[slot] = (f, Some(d.clone()));
Some(d)
}
#[inline]
pub fn delta_word(&self, ids: hc::Local, parent: Id, list: u64, ops: &[IdOp]) -> Option<u64> {
if let Some(w) = self.words.get(list, parent) {
return Some(w);
}
let w = delta_word(self, ids, parent, ops)?;
self.words.put(list, parent, w);
Some(w)
}
#[inline]
pub fn applied(&self, parent: Id, list: u64, apply: impl FnOnce(&Value) -> Option<Value>) -> Option<Id> {
if parent >= NOT_INTERNED {
return None;
}
self.count(0);
if let Some(r) = self.applied.get(list, parent) {
return Some(r);
}
self.count(1);
let r = hc::id(&apply(&hc::value(parent))?)?;
self.applied.put(list, parent, r);
Some(r)
}
}
pub fn delta_word(t: &Table, ids: hc::Local, parent: Id, ops: &[IdOp]) -> Option<u64> {
if parent >= NOT_INTERNED {
return None;
}
let sh = ids.shape(parent);
let mut w = ids.word(parent);
let mut len = sh.len as usize;
let mut done: [(Id, Id); 8] = [(0, 0); 8];
let mut nd = 0;
for op in ops {
if nd == usize::MAX {
return None;
}
match op {
IdOp::Bag { e, we, plus } => {
if sh.kind != hc::ShapeKind::Fcn || !sh.bag {
return None;
}
let c = match done[..nd].iter().rev().find(|d| d.0 == *e) {
Some(d) => d.1,
None => t.locate_in(parent, LocKind::Elem, *e)?,
};
let old = if c == ABSENT { 0 } else { hc::int_of(c)? };
if old == 0 && !*plus {
continue;
}
let new = if *plus { old + 1 } else { old - 1 };
if new > i32::MAX as i64 {
return None;
}
if old > 0 {
w = w.wrapping_sub(entry_term(*we, int_word(old)));
} else {
w = w.wrapping_sub(fn_tag(len)).wrapping_add(fn_tag(len + 1));
len += 1;
}
if new > 0 {
w = w.wrapping_add(entry_term(*we, int_word(new)));
} else {
w = w.wrapping_sub(fn_tag(len)).wrapping_add(fn_tag(len - 1));
len -= 1;
}
if nd == done.len() {
return None;
}
done[nd] = (*e, if new > 0 { hc::int_id(new)? } else { ABSENT });
nd += 1;
}
IdOp::Set { e, we, add } => {
if sh.kind != hc::ShapeKind::Set {
return None;
}
let m = match done[..nd].iter().rev().find(|d| d.0 == *e) {
Some(d) => d.1,
None => t.locate_in(parent, LocKind::Member, *e)?,
};
if (m == IN) == *add {
continue;
}
let te = elem_term(*we);
if *add {
w = w.wrapping_sub(set_tag(len)).wrapping_add(set_tag(len + 1)).wrapping_add(te);
len += 1;
} else {
w = w.wrapping_sub(set_tag(len)).wrapping_add(set_tag(len - 1)).wrapping_sub(te);
len -= 1;
}
if nd == done.len() {
return None;
}
done[nd] = (*e, if *add { IN } else { OUT });
nd += 1;
}
IdOp::Exc { path, wv } => {
if nd > 0 || path.is_empty() {
return None;
}
w = exc_word(t, ids, parent, w, path, *wv)?;
nd = usize::MAX;
}
}
}
Some(w)
}
fn key_shift(t: &Table, var: u32, ops: &[IdOp], locs: &[LocId], key: &[Id]) -> Option<u64> {
let ids = hc::Local::get();
let mut seen: Vec<Id> = vec![];
let mut d = 0u64;
for op in ops {
let IdOp::Exc { path, wv } = op else { return None };
let (k, wk) = *path.first()?;
if seen.contains(&k) {
return None;
}
seen.push(k);
let j = locs.iter().position(|l| l.var == var && l.kind == LocKind::Elem && l.key == k)?;
let old = key[j];
if old == ABSENT || old >= NOT_INTERNED {
return None;
}
let wold = ids.word(old);
let wnew = if path.len() == 1 { *wv } else { exc_word(t, ids, old, wold, &path[1..], *wv)? };
d = d.wrapping_add(entry_term(wk, wnew)).wrapping_sub(entry_term(wk, wold));
}
Some(d)
}
fn exc_word(t: &Table, ids: hc::Local, f: Id, w: u64, path: &[(Id, u64)], wv: u64) -> Option<u64> {
if ids.shape(f).kind != hc::ShapeKind::Fcn {
return None;
}
let (k, wk) = path[0];
let old = t.locate_in(f, LocKind::Elem, k)?;
if old == ABSENT || old >= NOT_INTERNED {
return None;
}
let wold = ids.word(old);
let wnew = if path.len() == 1 { wv } else { exc_word(t, ids, old, wold, &path[1..], wv)? };
Some(w.wrapping_sub(entry_term(wk, wold)).wrapping_add(entry_term(wk, wnew)))
}
#[cold]
fn locate_slow(c: Id, loc: LocId) -> Option<Id> {
let (v, k) = (hc::value(c), hc::value(loc.key));
match loc.kind {
LocKind::Elem => match elem_at_checked(&v, &k)? {
Some(x) => hc::id(&x),
None => Some(ABSENT),
},
LocKind::Member => member_of(&v, &k).map(|b| if b { IN } else { OUT }),
LocKind::Whole => unreachable!(),
}
}
#[inline]
pub fn mix(h: u64, x: u64) -> u64 {
let m = ((h ^ x) as u128).wrapping_mul(0xD6E8_FEB8_6659_FD93u128);
((m as u64) ^ ((m >> 64) as u64)).wrapping_add(0x9E37_79B9_7F4A_7C15)
}
#[inline]
pub fn template_hash(unit: u32, bound: &[Id]) -> u64 {
let mut h = mix(0x5EED, unit as u64);
for &b in bound {
h = mix(h, b as u64);
}
h
}
#[inline(always)]
fn same_ids(a: &[Id], b: &[Id]) -> bool {
a.len() == b.len() && a.iter().zip(b).all(|(x, y)| x == y)
}
#[inline]
pub fn leaf_hash_start(version: u64) -> u64 {
mix(0x1EAF, version)
}
#[inline]
pub fn leaf_hash(version: u64, key: &[Id]) -> u64 {
let mut h = leaf_hash_start(version);
for &k in key {
h = mix(h, k as u64);
}
h
}
pub fn elem_at_checked(f: &Value, k: &Value) -> Option<Option<Value>> {
match f {
Value::Fcn(fc) => match bsearch(&fc.dom, k) {
Ok(Some(i)) => Some(Some(fc.vals[i].clone())),
Ok(None) => Some(None),
Err(_) => None,
},
Value::Tuple(_) if !matches!(k, Value::Int(_)) => None,
Value::Record(_) if !matches!(k, Value::Str(_)) => None,
_ => Some(elem_at(f, k)),
}
}
pub fn elem_at(f: &Value, k: &Value) -> Option<Value> {
match f {
Value::Fcn(fc) => match bsearch(&fc.dom, k) {
Ok(Some(i)) => Some(fc.vals[i].clone()),
_ => None,
},
Value::Tuple(t) => match k {
Value::Int(i) if *i >= 1 && *i as usize <= t.len() => Some(t[*i as usize - 1].clone()),
_ => None,
},
Value::Record(r) => match k {
Value::Str(s) => r.get(*s).cloned(),
_ => None,
},
_ => None,
}
}
pub fn member_of(v: &Value, e: &Value) -> Option<bool> {
match v {
Value::Set(s) => s.contains(e).ok(),
Value::Fcn(f) => bsearch(&f.dom, e).ok().map(|x| x.is_some()),
Value::Tuple(t) => match e {
Value::Int(i) => Some(*i >= 1 && *i as usize <= t.len()),
_ => None,
},
Value::Record(r) => match e {
Value::Str(s) => Some(r.get(*s).is_some()),
_ => None,
},
_ => None,
}
}
fn loc_id(l: &Loc) -> Option<LocId> {
Some(match l {
Loc::Whole(i) => LocId { var: *i, kind: LocKind::Whole, key: 0 },
Loc::Elem(i, k) => LocId { var: *i, kind: LocKind::Elem, key: hc::id(k)? },
Loc::Member(i, k) => LocId { var: *i, kind: LocKind::Member, key: hc::id(k)? },
})
}
pub struct Recorded<'a> {
pub unit: u32,
pub bound: &'a [Id],
pub reads: &'a [(Loc, Value)],
pub succs: &'a [Vec<Write>],
}
pub fn store(t: &mut Table, cur: &[Id], r: Recorded) -> bool {
if r.reads.iter().any(|(_, v)| vsize(v) > 512) {
return false;
}
let h0 = template_hash(r.unit, r.bound);
let (mut locs, version) = match t.template(h0, r.unit, r.bound) {
Some(tm) => (tm.locs.to_vec(), tm.version),
None => (vec![], 0),
};
let had = locs.len();
for (l, _) in r.reads {
let Some(l) = loc_id(l) else { return false };
if !locs.contains(&l) {
locs.push(l);
}
}
if locs.len() > 24 {
return false;
}
let mut key = Vec::with_capacity(locs.len());
for &l in &locs {
match t.locate(cur, l) {
Some(x) => key.push(x),
None => return false,
}
}
let mut succs = Vec::with_capacity(r.succs.len());
for s in r.succs {
let mut puts = vec![];
for (i, w) in s.iter().enumerate() {
match w {
Write::Delta(ops) if ops.is_empty() => {}
Write::Delta(ops) => {
let Some(ops) = ops.iter().map(canon_op).collect::<Option<Vec<DOp>>>() else { return false };
let Some(id) = t.list_number(&ops) else { return false };
let iops = ops.iter().map(id_op).collect::<Option<Vec<IdOp>>>().map(|v| v.into_boxed_slice());
let shift = iops.as_deref().and_then(|o| key_shift(t, i as u32, o, &locs, &key));
puts.push((i as u32, Put::Delta { id, ops: ops.into_boxed_slice(), iops, shift }));
}
Write::Val(v) => match hc::id(v) {
Some(x) => puts.push((i as u32, Put::Id { id: x, word: hc::word(x) })),
None => return false,
},
}
}
succs.push(puts.into_boxed_slice());
}
let version = if version == 0 || locs.len() != had {
let version = fresh();
t.put(h0, Entry::Tmpl(Tmpl { unit: r.unit, bound: r.bound.into(), locs: locs.into_boxed_slice(), version }));
version
} else {
version
};
t.put(leaf_hash(version, &key), Entry::Leaf(Leaf { version, key: key.into_boxed_slice(), succs: succs.into_boxed_slice() }));
true
}
fn canon_op(o: &DOp) -> Option<DOp> {
use hc::canon as c;
Some(match o {
DOp::Bag { e, plus } => DOp::Bag { e: c(e)?, plus: *plus },
DOp::Set { e, add } => DOp::Set { e: c(e)?, add: *add },
DOp::Exc { path, v } => DOp::Exc { path: path.iter().map(c).collect::<Option<Vec<_>>>()?, v: c(v)? },
})
}
pub fn ids_of(vals: &[Value], out: &mut Vec<Id>) {
out.clear();
out.extend(vals.iter().map(|v| hc::id(v).unwrap_or(NOT_INTERNED)));
}
#[derive(Default)]
pub struct Rows {
pub ids: Vec<Id>,
pub opaque: Vec<Value>,
pub meta: RowMeta,
}
pub struct Recipe {
pub slot: std::rc::Rc<Slot>,
pub succ: u32,
pub n: crate::ast::N,
}
impl Recipe {
pub fn puts(&self) -> &[(u32, Put)] {
match &self.slot.2 {
Entry::Leaf(l) => &l.succs[self.succ as usize],
Entry::Tmpl(_) => unreachable!("a recipe refers to a leaf"),
}
}
}
#[derive(Clone, Copy)]
pub struct Found {
pub fp: u64,
pub act: u32,
pub src: u32,
}
#[derive(Default)]
pub struct RowMeta {
pub found: Vec<Found>,
pub recipes: Vec<Recipe>,
pub cur_act: u32,
rows: u32,
}
pub const RECIPE: u32 = 1 << 31;
impl RowMeta {
#[inline]
pub fn added(&mut self, fp: u64) {
self.found.push(Found { fp, act: self.cur_act, src: self.rows });
self.rows += 1;
}
#[inline]
pub fn recipe(&mut self, fp: u64, r: Recipe) {
self.found.push(Found { fp, act: self.cur_act, src: RECIPE + self.recipes.len() as u32 });
self.recipes.push(r);
}
#[inline]
pub fn recipe_of(&self, i: usize) -> Option<&Recipe> {
let s = self.found[i].src;
(s >= RECIPE).then(|| &self.recipes[(s - RECIPE) as usize])
}
}
#[derive(Clone, Copy)]
pub struct RowsMark {
ids: usize,
found: usize,
recipes: usize,
rows: u32,
}
impl Rows {
pub fn clear(&mut self) {
self.ids.clear();
self.opaque.clear();
let m = &mut self.meta;
m.found.clear();
m.recipes.clear();
m.rows = 0;
}
#[inline]
pub fn mark(&self) -> RowsMark {
RowsMark { ids: self.ids.len(), found: self.meta.found.len(), recipes: self.meta.recipes.len(), rows: self.meta.rows }
}
pub fn truncate(&mut self, k: RowsMark) {
self.ids.truncate(k.ids);
let m = &mut self.meta;
m.found.truncate(k.found);
m.recipes.truncate(k.recipes);
m.rows = k.rows;
}
pub fn len(&self) -> usize {
self.meta.found.len()
}
#[inline]
pub fn row(&self, i: usize, nv: usize) -> Option<&[Id]> {
let s = self.meta.found[i].src;
if s >= RECIPE {
return None;
}
let s = s as usize;
Some(&self.ids[s * nv..(s + 1) * nv])
}
pub fn push_values(&mut self, vals: &[Value]) {
for v in vals {
let x = match hc::id_if_canonical(v) {
Some(x) => x,
None => {
self.opaque.push(v.clone());
NOT_INTERNED + (self.opaque.len() - 1) as Id
}
};
self.ids.push(x);
}
self.meta.added(0);
}
pub fn value(&self, x: Id) -> Value {
if x >= NOT_INTERNED {
self.opaque[(x - NOT_INTERNED) as usize].clone()
} else {
hc::value(x)
}
}
}