mirror of
https://github.com/ellmau/adf-obdd.git
synced 2025-12-19 09:29:36 +01:00
Add cache to ite and restrict
This commit is contained in:
parent
8269fbac9d
commit
7000c41a61
@ -19,6 +19,10 @@ pub struct Bdd {
|
|||||||
cache: HashMap<BddNode, Term>,
|
cache: HashMap<BddNode, Term>,
|
||||||
#[serde(skip, default = "Bdd::default_count_cache")]
|
#[serde(skip, default = "Bdd::default_count_cache")]
|
||||||
count_cache: RefCell<HashMap<Term, CountNode>>,
|
count_cache: RefCell<HashMap<Term, CountNode>>,
|
||||||
|
#[serde(skip)]
|
||||||
|
ite_cache: HashMap<(Term, Term, Term), Term>,
|
||||||
|
#[serde(skip)]
|
||||||
|
restrict_cache: HashMap<(Term, Var, bool), Term>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Bdd {
|
impl Display for Bdd {
|
||||||
@ -49,6 +53,8 @@ impl Bdd {
|
|||||||
var_deps: vec![HashSet::new(), HashSet::new()],
|
var_deps: vec![HashSet::new(), HashSet::new()],
|
||||||
cache: HashMap::new(),
|
cache: HashMap::new(),
|
||||||
count_cache: RefCell::new(HashMap::new()),
|
count_cache: RefCell::new(HashMap::new()),
|
||||||
|
ite_cache: HashMap::new(),
|
||||||
|
restrict_cache: HashMap::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[cfg(feature = "adhoccounting")]
|
#[cfg(feature = "adhoccounting")]
|
||||||
@ -59,6 +65,8 @@ impl Bdd {
|
|||||||
var_deps: vec![HashSet::new(), HashSet::new()],
|
var_deps: vec![HashSet::new(), HashSet::new()],
|
||||||
cache: HashMap::new(),
|
cache: HashMap::new(),
|
||||||
count_cache: RefCell::new(HashMap::new()),
|
count_cache: RefCell::new(HashMap::new()),
|
||||||
|
ite_cache: HashMap::new(),
|
||||||
|
restrict_cache: HashMap::new(),
|
||||||
};
|
};
|
||||||
result
|
result
|
||||||
.count_cache
|
.count_cache
|
||||||
@ -176,6 +184,9 @@ impl Bdd {
|
|||||||
|
|
||||||
/// Restrict the value of a given [variable][crate::datatypes::Var] to **val**.
|
/// Restrict the value of a given [variable][crate::datatypes::Var] to **val**.
|
||||||
pub fn restrict(&mut self, tree: Term, var: Var, val: bool) -> Term {
|
pub fn restrict(&mut self, tree: Term, var: Var, val: bool) -> Term {
|
||||||
|
if let Some(result) = self.restrict_cache.get(&(tree, var, val)) {
|
||||||
|
*result
|
||||||
|
} else {
|
||||||
let node = self.nodes[tree.0];
|
let node = self.nodes[tree.0];
|
||||||
#[cfg(feature = "variablelist")]
|
#[cfg(feature = "variablelist")]
|
||||||
{
|
{
|
||||||
@ -193,9 +204,14 @@ impl Bdd {
|
|||||||
self.node(node.var(), lonode, hinode)
|
self.node(node.var(), lonode, hinode)
|
||||||
} else {
|
} else {
|
||||||
if val {
|
if val {
|
||||||
self.restrict(node.hi(), var, val)
|
let result = self.restrict(node.hi(), var, val);
|
||||||
|
self.restrict_cache.insert((tree, var, val), result);
|
||||||
|
result
|
||||||
} else {
|
} else {
|
||||||
self.restrict(node.lo(), var, val)
|
let result = self.restrict(node.lo(), var, val);
|
||||||
|
self.restrict_cache.insert((tree, var, val), result);
|
||||||
|
result
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -210,6 +226,8 @@ impl Bdd {
|
|||||||
t
|
t
|
||||||
} else if t == Term::TOP && e == Term::BOT {
|
} else if t == Term::TOP && e == Term::BOT {
|
||||||
i
|
i
|
||||||
|
} else if let Some(result) = self.ite_cache.get(&(i, t, e)) {
|
||||||
|
*result
|
||||||
} else {
|
} else {
|
||||||
let minvar = Var(min(
|
let minvar = Var(min(
|
||||||
self.nodes[i.value()].var().value(),
|
self.nodes[i.value()].var().value(),
|
||||||
@ -227,7 +245,9 @@ impl Bdd {
|
|||||||
|
|
||||||
let top_ite = self.if_then_else(itop, ttop, etop);
|
let top_ite = self.if_then_else(itop, ttop, etop);
|
||||||
let bot_ite = self.if_then_else(ibot, tbot, ebot);
|
let bot_ite = self.if_then_else(ibot, tbot, ebot);
|
||||||
self.node(minvar, bot_ite, top_ite)
|
let result = self.node(minvar, bot_ite, top_ite);
|
||||||
|
self.ite_cache.insert((i, t, e), result);
|
||||||
|
result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user