Skip to content

Commit

Permalink
refactor(bdd): moved duplicate code to restrict_and_prune & union_and…
Browse files Browse the repository at this point in the history
…_extend
  • Loading branch information
AurumTheEnd committed May 10, 2024
1 parent cbd0171 commit b9afa91
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 61 deletions.
52 changes: 51 additions & 1 deletion src/bdd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ mod iterators;
mod traits;
mod utils;

use crate::bdd::utils::extend_bdd_variables;
use crate::bdd::utils::{extend_bdd_variables, prune_bdd_variables};
use crate::traits::BooleanValuation;
use biodivine_lib_bdd::{Bdd as InnerBdd, BddVariable, BddVariableSet};
use std::collections::BTreeSet;
use std::fmt::Debug;
Expand Down Expand Up @@ -70,4 +71,53 @@ impl<TLiteral: Debug + Clone + Eq + Ord> Bdd<TLiteral> {
pub(crate) fn inner(&self) -> &InnerBdd {
&self.bdd

Check warning on line 72 in src/bdd/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/mod.rs#L71-L72

Added lines #L71 - L72 were not covered by tests
}

fn restrict_and_prune_map(

Check warning on line 75 in src/bdd/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/mod.rs#L75

Added line #L75 was not covered by tests
&self,
valuation: &BooleanValuation<TLiteral>,
new_bdd: &Bdd<TLiteral>,
) -> Bdd<TLiteral> {
self.restrict_and_prune_common(valuation, new_bdd, |set, var| set.contains_key(var))

Check warning on line 80 in src/bdd/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/mod.rs#L80

Added line #L80 was not covered by tests
}

fn restrict_and_prune_set(

Check warning on line 83 in src/bdd/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/mod.rs#L83

Added line #L83 was not covered by tests
&self,
valuation: &BTreeSet<TLiteral>,
new_bdd: &Bdd<TLiteral>,
) -> Bdd<TLiteral> {
self.restrict_and_prune_common(valuation, new_bdd, |set, var| set.contains(var))

Check warning on line 88 in src/bdd/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/mod.rs#L88

Added line #L88 was not covered by tests
}

fn restrict_and_prune_common<TCollection, P: Fn(&TCollection, &&TLiteral) -> bool>(

Check warning on line 91 in src/bdd/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/mod.rs#L91

Added line #L91 was not covered by tests
&self,
valuation: &TCollection,
new_bdd: &Bdd<TLiteral>,
contains: P,
) -> Bdd<TLiteral> {
let restricted_inputs = self
.inputs

Check warning on line 98 in src/bdd/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/mod.rs#L97-L98

Added lines #L97 - L98 were not covered by tests
.iter()
.filter(|var| !contains(valuation, var))

Check warning on line 100 in src/bdd/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/mod.rs#L100

Added line #L100 was not covered by tests
.cloned()
.collect::<Vec<_>>();
prune_bdd_variables(new_bdd, &restricted_inputs)

Check warning on line 103 in src/bdd/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/mod.rs#L103

Added line #L103 was not covered by tests
}

fn union_and_extend(

Check warning on line 106 in src/bdd/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/mod.rs#L106

Added line #L106 was not covered by tests
&self,
other: &Bdd<TLiteral>,
) -> (Bdd<TLiteral>, Bdd<TLiteral>, Vec<TLiteral>) {
let mut common_inputs = self.inputs.clone();
for other in &other.inputs {
if !common_inputs.contains(other) {
common_inputs.push(other.clone());

Check warning on line 113 in src/bdd/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/mod.rs#L110-L113

Added lines #L110 - L113 were not covered by tests
}
}
common_inputs.sort();

Check warning on line 116 in src/bdd/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/mod.rs#L116

Added line #L116 was not covered by tests

let self_lifted = extend_bdd_variables(self, &common_inputs);
let other_lifted = extend_bdd_variables(other, &common_inputs);

Check warning on line 119 in src/bdd/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/mod.rs#L118-L119

Added lines #L118 - L119 were not covered by tests

(self_lifted, other_lifted, common_inputs)

Check warning on line 121 in src/bdd/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/mod.rs#L121

Added line #L121 was not covered by tests
}
}
13 changes: 2 additions & 11 deletions src/bdd/traits/bit/and.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::bdd::{extend_bdd_variables, Bdd};
use crate::bdd::Bdd;
use std::fmt::Debug;
use std::ops::BitAnd;

Expand All @@ -12,16 +12,7 @@ impl<TLiteral: Debug + Clone + Eq + Ord + 'static> BitAnd for Bdd<TLiteral> {
bdd: self.bdd.and(&rhs.bdd),

Check warning on line 12 in src/bdd/traits/bit/and.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/traits/bit/and.rs#L11-L12

Added lines #L11 - L12 were not covered by tests
}
} else {
let mut common_inputs = self.inputs.clone();
for other in &rhs.inputs {
if !common_inputs.contains(other) {
common_inputs.push(other.clone());
}
}
common_inputs.sort();

let self_lifted = extend_bdd_variables(&self, &common_inputs);
let rhs_lifted = extend_bdd_variables(&rhs, &common_inputs);
let (self_lifted, rhs_lifted, common_inputs) = self.union_and_extend(&rhs);

Check warning on line 15 in src/bdd/traits/bit/and.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/traits/bit/and.rs#L15

Added line #L15 was not covered by tests

Bdd {
inputs: common_inputs,
Expand Down
60 changes: 11 additions & 49 deletions src/bdd/traits/boolean_function.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::bdd::iterators::{ImageIterator, SupportIterator};
use crate::bdd::utils::{extend_bdd_variables, prune_bdd_variables};
use crate::bdd::Bdd;
use crate::iterators::DomainIterator;
use crate::traits::{BooleanFunction, BooleanPoint, BooleanValuation};
Expand Down Expand Up @@ -71,13 +70,8 @@ impl<T: Debug + Clone + Ord> BooleanFunction<T> for Bdd<T> {
inputs: self.inputs.clone(),
bdd: self.bdd.restrict(&lib_bdd_valuation),

Check warning on line 71 in src/bdd/traits/boolean_function.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/traits/boolean_function.rs#L70-L71

Added lines #L70 - L71 were not covered by tests
};
let restricted_inputs = self
.inputs
.iter()
.filter(|var| !valuation.contains_key(var))
.cloned()
.collect::<Vec<_>>();
prune_bdd_variables(&new_bdd, &restricted_inputs)

self.restrict_and_prune_map(valuation, &new_bdd)

Check warning on line 74 in src/bdd/traits/boolean_function.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/traits/boolean_function.rs#L74

Added line #L74 was not covered by tests
}

fn substitute(&self, _mapping: &BTreeMap<T, Self>) -> Self {

Check warning on line 77 in src/bdd/traits/boolean_function.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/traits/boolean_function.rs#L77

Added line #L77 was not covered by tests
Expand All @@ -102,13 +96,8 @@ impl<T: Debug + Clone + Ord> BooleanFunction<T> for Bdd<T> {
inputs: self.inputs.clone(),
bdd: self.bdd.exists(&lib_bdd_variables),

Check warning on line 97 in src/bdd/traits/boolean_function.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/traits/boolean_function.rs#L96-L97

Added lines #L96 - L97 were not covered by tests
};
let restricted_inputs = self
.inputs
.iter()
.filter(|var| !variables.contains(var))
.cloned()
.collect::<Vec<_>>();
prune_bdd_variables(&new_bdd, &restricted_inputs)

self.restrict_and_prune_set(&variables, &new_bdd)

Check warning on line 100 in src/bdd/traits/boolean_function.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/traits/boolean_function.rs#L100

Added line #L100 was not covered by tests
}

fn universal_quantification(&self, variables: BTreeSet<T>) -> Self {
Expand All @@ -120,13 +109,8 @@ impl<T: Debug + Clone + Ord> BooleanFunction<T> for Bdd<T> {
inputs: self.inputs.clone(),
bdd: self.bdd.for_all(&lib_bdd_variables),

Check warning on line 110 in src/bdd/traits/boolean_function.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/traits/boolean_function.rs#L109-L110

Added lines #L109 - L110 were not covered by tests
};
let restricted_inputs = self
.inputs
.iter()
.filter(|var| !variables.contains(var))
.cloned()
.collect::<Vec<_>>();
prune_bdd_variables(&new_bdd, &restricted_inputs)

self.restrict_and_prune_set(&variables, &new_bdd)

Check warning on line 113 in src/bdd/traits/boolean_function.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/traits/boolean_function.rs#L113

Added line #L113 was not covered by tests
}

fn derivative(&self, variables: BTreeSet<T>) -> Self {
Expand All @@ -147,40 +131,18 @@ impl<T: Debug + Clone + Ord> BooleanFunction<T> for Bdd<T> {
),
};

let restricted_inputs = self
.inputs
.iter()
.filter(|var| !variables.contains(var))
.cloned()
.collect::<Vec<_>>();
prune_bdd_variables(&new_bdd, &restricted_inputs)
self.restrict_and_prune_set(&variables, &new_bdd)

Check warning on line 134 in src/bdd/traits/boolean_function.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/traits/boolean_function.rs#L134

Added line #L134 was not covered by tests
}

fn is_equivalent(&self, other: &Self) -> bool {
let mut common_inputs = self.inputs.clone();
for other in &other.inputs {
if !common_inputs.contains(other) {
common_inputs.push(other.clone());
}
}
common_inputs.sort();

let self_lifted = extend_bdd_variables(self, &common_inputs);
let other_lifted = extend_bdd_variables(other, &common_inputs);
let (self_lifted, other_lifted, _common_inputs) = self.union_and_extend(other);

Check warning on line 138 in src/bdd/traits/boolean_function.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/traits/boolean_function.rs#L137-L138

Added lines #L137 - L138 were not covered by tests

self_lifted.bdd == other_lifted.bdd

Check warning on line 140 in src/bdd/traits/boolean_function.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/traits/boolean_function.rs#L140

Added line #L140 was not covered by tests
}

fn is_implied_by(&self, other: &Self) -> bool {
let mut common_inputs = self.inputs.clone();
for other in &other.inputs {
if !common_inputs.contains(other) {
common_inputs.push(other.clone());
}
}
common_inputs.sort();

let self_lifted = extend_bdd_variables(self, &common_inputs);
let other_lifted = extend_bdd_variables(other, &common_inputs);
let (self_lifted, other_lifted, _common_inputs) = self.union_and_extend(other);

Check warning on line 144 in src/bdd/traits/boolean_function.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/traits/boolean_function.rs#L143-L144

Added lines #L143 - L144 were not covered by tests

other_lifted.bdd.imp(&self_lifted.bdd).is_true()

Check warning on line 146 in src/bdd/traits/boolean_function.rs

View check run for this annotation

Codecov / codecov/patch

src/bdd/traits/boolean_function.rs#L146

Added line #L146 was not covered by tests
}
}

0 comments on commit b9afa91

Please sign in to comment.