use crate::canon::{MSetCanon, VarCanon};
use crate::config::EGraphConfig;
use crate::egraph::EGraph;
use crate::literal::LitVal;
use std::collections::HashMap;
type FastMap<K, V> = HashMap<K, V, foldhash::fast::RandomState>;
pub struct CcSnapshot<Cfg: EGraphConfig> {
by_op_contains: FastMap<(Cfg::O, Cfg::G), Vec<Cfg::G>>,
completion_nodes: Vec<Cfg::G>,
}
impl<Cfg: EGraphConfig> CcSnapshot<Cfg>
where
MSetCanon: VarCanon<Cfg::G, Cfg::C>,
{
pub fn build<L: LitVal, const TRACK: bool, const PROOFS: bool>(
eg: &EGraph<Cfg, L, TRACK, PROOFS>,
) -> Self {
let mut by_op_contains: FastMap<(Cfg::O, Cfg::G), Vec<Cfg::G>> = FastMap::default();
let mut completion_nodes: Vec<Cfg::G> = Vec::new();
let inactive = crate::node_types::FLAG_SUBSUMED | crate::node_types::FLAG_AC_COLLAPSED;
for gid in eg.node_ids() {
if eg.node_flags(gid) & inactive != 0 {
continue;
}
let op = eg.node_op(gid);
if !eg.ops().is_mset(op) && !eg.ops().is_set(op) {
continue;
}
completion_nodes.push(gid);
let mut seen: Vec<Cfg::G> = Vec::new();
eg.for_each_child(gid, |child, _mult| {
let cr = eg.class_repr(child);
if !seen.contains(&cr) {
seen.push(cr);
by_op_contains.entry((op, cr)).or_default().push(gid);
}
});
}
for v in by_op_contains.values_mut() {
v.sort_unstable();
v.dedup();
}
completion_nodes.sort_unstable();
completion_nodes.dedup();
Self {
by_op_contains,
completion_nodes,
}
}
pub fn completion_nodes(&self) -> &[Cfg::G] {
&self.completion_nodes
}
pub fn partners(&self, op: Cfg::O, child_repr: Cfg::G) -> &[Cfg::G] {
self.by_op_contains
.get(&(op, child_repr))
.map(|v| v.as_slice())
.unwrap_or(&[])
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::egraph::EGraph31;
use crate::literal::NiraLitVal;
fn ref_partners(
eg: &EGraph31<NiraLitVal, false, false>,
op: crate::id::OpId,
cr: crate::id::ENodeId,
) -> Vec<crate::id::ENodeId> {
let mut out = Vec::new();
let mut buf = Vec::new();
let inactive = crate::node_types::FLAG_SUBSUMED | crate::node_types::FLAG_AC_COLLAPSED;
for i in 0..eg.node_count() {
let gid = crate::id::ENodeId::new(i as u32);
if eg.node_flags(gid) & inactive != 0 {
continue;
}
if eg.node_op(gid) != op || !(eg.ops().is_mset(op) || eg.ops().is_set(op)) {
continue;
}
buf.clear();
eg.for_each_child(gid, |c, m| buf.push((c, m)));
if buf.iter().any(|&(c, _)| eg.class_repr(c) == cr) {
out.push(gid);
}
}
out.sort_unstable();
out.dedup();
out
}
#[test]
fn snapshot_matches_by_contains_variadic_fixture() {
let mut eg = EGraph31::<NiraLitVal, false, false>::new();
let int = eg.intern_sort("Int");
let plus = eg.register_mset("plus", int, int);
let x_op = eg.register_op0("x", int);
let y_op = eg.register_op0("y", int);
let z_op = eg.register_op0("z", int);
let x = eg.add(x_op, &[]);
let y = eg.add(y_op, &[]);
let z = eg.add(z_op, &[]);
let pxy = eg.add(plus, &[x, y]);
let pxz = eg.add(plus, &[x, z]);
let snap = CcSnapshot::build(&eg);
let mut cx = snap.partners(plus, x).to_vec();
cx.sort_unstable();
assert_eq!(cx, vec![pxy.min(pxz), pxy.max(pxz)]);
assert_eq!(snap.partners(plus, y), &[pxy]);
assert_eq!(snap.completion_nodes(), &[pxy.min(pxz), pxy.max(pxz)]);
}
#[test]
fn snapshot_only_indexes_ac_ops() {
let mut eg = EGraph31::<NiraLitVal, false, false>::new();
let int = eg.intern_sort("Int");
let plus = eg.register_mset("plus", int, int);
let f = eg.register_op1("f", int, int); let x_op = eg.register_op0("x", int);
let x = eg.add(x_op, &[]);
let fx = eg.add(f, &[x]);
let pfx = eg.add(plus, &[x, fx]);
let snap = CcSnapshot::build(&eg);
assert_eq!(snap.completion_nodes(), &[pfx]);
assert!(snap.partners(f, eg.class_repr(x)).is_empty());
assert_eq!(snap.partners(plus, eg.class_repr(x)), &[pfx]);
assert_eq!(snap.partners(plus, eg.class_repr(fx)), &[pfx]);
}
#[test]
fn snapshot_agrees_with_brute_force_after_merge() {
let mut eg = EGraph31::<NiraLitVal, false, false>::new();
let int = eg.intern_sort("Int");
let plus = eg.register_mset("plus", int, int);
let a_op = eg.register_op0("a", int);
let b_op = eg.register_op0("b", int);
let d_op = eg.register_op0("d", int);
let a = eg.add(a_op, &[]);
let b = eg.add(b_op, &[]);
let d = eg.add(d_op, &[]);
let _ab = eg.add(plus, &[a, b]);
let _bd = eg.add(plus, &[b, d]);
let _abb = eg.add(plus, &[a, b, b]);
eg.merge(a, d);
eg.rebuild();
let snap = CcSnapshot::build(&eg);
for x in [a, b, d] {
let cr = eg.class_repr(x);
let mut got = snap.partners(plus, cr).to_vec();
got.sort_unstable();
assert_eq!(
got,
ref_partners(&eg, plus, cr),
"mismatch for child {cr:?}"
);
}
}
#[test]
fn cc_collapsed_leaves_completion_set_but_stays_matchable() {
use crate::index::IndexStore;
let mut eg = EGraph31::<NiraLitVal, false, false>::new();
let int = eg.intern_sort("Int");
let plus = eg.register_mset("plus", int, int);
let a_op = eg.register_op0("a", int);
let b_op = eg.register_op0("b", int);
let a = eg.add(a_op, &[]);
let b = eg.add(b_op, &[]);
let pab = eg.add(plus, &[a, b]);
assert_eq!(CcSnapshot::build(&eg).completion_nodes(), &[pab]);
assert!(IndexStore::build(&eg).nodes_by_op(plus).contains(&pab));
eg.set_cc_collapsed(pab);
assert!(
CcSnapshot::build(&eg).completion_nodes().is_empty(),
"AC-collapsed node must leave the completion active set"
);
assert!(
IndexStore::build(&eg).nodes_by_op(plus).contains(&pab),
"AC-collapsed node must stay visible to the matcher (not subsumed)"
);
assert_eq!(eg.class_repr(pab), eg.class_repr(pab));
eg.subsume(pab);
assert!(
!IndexStore::build(&eg).nodes_by_op(plus).contains(&pab),
"subsumed node must be hidden from the matcher index"
);
}
}