use std::cell::RefCell;
use std::sync::Arc;
use relate_ir::{BinaryOp, Expr};
use relate_names::{NamedToken, SmallName};
use super::ContainsCount;
use super::DegreeMoments;
use super::PatternStats;
#[derive(Clone, Copy)]
enum SeekOperandRef<'a> {
Parameter(&'a str),
Null,
Bool(bool),
Int(i64),
Float(f64),
Str(&'a str),
}
impl<'a> SeekOperandRef<'a> {
fn of(expr: &'a Expr) -> Option<Self> {
match expr {
Expr::Parameter(name) => Some(Self::Parameter(name.as_str())),
Expr::Null => Some(Self::Null),
Expr::Bool(value) => Some(Self::Bool(*value)),
Expr::Int(value) => Some(Self::Int(*value)),
Expr::Float(value) => Some(Self::Float(*value)),
Expr::String(text) => Some(Self::Str(text.as_str())),
_ => None,
}
}
}
enum SeekOperand {
Parameter(String),
Null,
Bool(bool),
Int(i64),
Float(f64),
Str(Arc<str>),
}
impl SeekOperand {
fn from_ref(operand: SeekOperandRef<'_>) -> Self {
match operand {
SeekOperandRef::Parameter(name) => SeekOperand::Parameter(name.to_owned()),
SeekOperandRef::Null => SeekOperand::Null,
SeekOperandRef::Bool(value) => SeekOperand::Bool(value),
SeekOperandRef::Int(value) => SeekOperand::Int(value),
SeekOperandRef::Float(value) => SeekOperand::Float(value),
SeekOperandRef::Str(text) => SeekOperand::Str(Arc::from(text)),
}
}
}
impl PartialEq<SeekOperandRef<'_>> for SeekOperand {
fn eq(&self, other: &SeekOperandRef<'_>) -> bool {
match (self, other) {
(SeekOperand::Parameter(name), SeekOperandRef::Parameter(other_name)) => {
name.as_str() == *other_name
}
(SeekOperand::Null, SeekOperandRef::Null) => true,
(SeekOperand::Bool(a), SeekOperandRef::Bool(b)) => a == b,
(SeekOperand::Int(a), SeekOperandRef::Int(b)) => a == b,
(SeekOperand::Float(a), SeekOperandRef::Float(b)) => a == b,
(SeekOperand::Str(a), SeekOperandRef::Str(b)) => &**a == *b,
_ => false,
}
}
}
struct PairQuestion {
label: SmallName,
property: SmallName,
operand: SeekOperand,
}
struct PairQuestionRef<'a> {
label: &'a SmallName,
property: &'a SmallName,
operand: SeekOperandRef<'a>,
}
impl PartialEq<PairQuestionRef<'_>> for PairQuestion {
fn eq(&self, other: &PairQuestionRef<'_>) -> bool {
&self.label == other.label
&& &self.property == other.property
&& self.operand == other.operand
}
}
struct RangeQuestion {
pair: PairQuestion,
op: BinaryOp,
}
struct RangeQuestionRef<'a> {
pair: PairQuestionRef<'a>,
op: BinaryOp,
}
impl PartialEq<RangeQuestionRef<'_>> for RangeQuestion {
fn eq(&self, other: &RangeQuestionRef<'_>) -> bool {
self.op == other.op && self.pair == other.pair
}
}
#[derive(Default)]
struct Consults {
equality: Vec<(PairQuestion, u64)>,
range: Vec<(RangeQuestion, u64)>,
contains: Vec<(PairQuestion, ContainsCount)>,
}
pub(crate) struct StatsMemo<'a> {
inner: &'a dyn PatternStats,
consults: RefCell<Consults>,
}
impl<'a> StatsMemo<'a> {
pub(crate) fn new(inner: &'a dyn PatternStats) -> Self {
Self {
inner,
consults: RefCell::default(),
}
}
fn consult<'q, K, Q, A>(
&self,
table: &mut Vec<(K, A)>,
operand: &'q Expr,
query: impl FnOnce(SeekOperandRef<'q>) -> Q,
question: impl FnOnce(SeekOperandRef<'q>) -> K,
ask: impl FnOnce() -> Option<A>,
) -> Option<A>
where
A: Copy,
K: PartialEq<Q>,
{
let Some(classified) = SeekOperandRef::of(operand) else {
return ask();
};
let query = query(classified);
if let Some((_, answer)) = table.iter().find(|(k, _)| *k == query) {
return Some(*answer);
}
let answer = ask()?;
table.push((question(classified), answer));
Some(answer)
}
fn pair_consult<A>(
&self,
table: &mut Vec<(PairQuestion, A)>,
label: &SmallName,
property: &SmallName,
operand: &Expr,
ask: impl FnOnce() -> Option<A>,
) -> Option<A>
where
A: Copy,
{
self.consult(
table,
operand,
|classified| PairQuestionRef {
label,
property,
operand: classified,
},
|classified| PairQuestion {
label: label.clone(),
property: property.clone(),
operand: SeekOperand::from_ref(classified),
},
ask,
)
}
}
impl PatternStats for StatsMemo<'_> {
fn label_rows(&self, label: &NamedToken) -> Option<u64> {
self.inner.label_rows(label)
}
fn equality_rows(
&self,
label: &NamedToken,
property: &NamedToken,
value: &Expr,
) -> Option<u64> {
self.pair_consult(
&mut self.consults.borrow_mut().equality,
&label.0,
&property.0,
value,
|| self.inner.equality_rows(label, property, value),
)
}
fn range_rows(
&self,
label: &NamedToken,
property: &NamedToken,
op: BinaryOp,
bound: &Expr,
) -> Option<u64> {
self.consult(
&mut self.consults.borrow_mut().range,
bound,
|classified| RangeQuestionRef {
pair: PairQuestionRef {
label: &label.0,
property: &property.0,
operand: classified,
},
op,
},
|classified| RangeQuestion {
pair: PairQuestion {
label: label.0.clone(),
property: property.0.clone(),
operand: SeekOperand::from_ref(classified),
},
op,
},
|| self.inner.range_rows(label, property, op, bound),
)
}
fn contains_rows(
&self,
label: &NamedToken,
property: &NamedToken,
needle: &Expr,
) -> Option<ContainsCount> {
self.pair_consult(
&mut self.consults.borrow_mut().contains,
&label.0,
&property.0,
needle,
|| self.inner.contains_rows(label, property, needle),
)
}
fn rel_type_rows(&self, rel_type: &NamedToken) -> Option<u64> {
self.inner.rel_type_rows(rel_type)
}
fn degree_tail(&self, label: &NamedToken, rel_type: &NamedToken) -> Option<u64> {
self.inner.degree_tail(label, rel_type)
}
fn any_degree_tail(&self, rel_type: Option<&SmallName>) -> Option<u64> {
self.inner.any_degree_tail(rel_type)
}
fn degree_moments(
&self,
labels: &[NamedToken],
rel_types: &[NamedToken],
) -> Option<DegreeMoments> {
self.inner.degree_moments(labels, rel_types)
}
fn in_degree_sum(&self, labels: &[NamedToken], rel_types: &[NamedToken]) -> Option<u128> {
self.inner.in_degree_sum(labels, rel_types)
}
}