Skip to content

Commit

Permalink
Ignore trailing None values when comparing BddPartialValuation ob…
Browse files Browse the repository at this point in the history
…jects.
  • Loading branch information
daemontus committed Dec 24, 2023
1 parent 1e95f83 commit 89c3456
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
49 changes: 49 additions & 0 deletions src/_impl_bdd_partial_valuation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{BddPartialValuation, BddValuation, BddVariable};
use std::cmp::min;
use std::convert::TryFrom;
use std::hash::{Hash, Hasher};
use std::ops::{Index, IndexMut};

impl BddPartialValuation {
Expand Down Expand Up @@ -136,9 +138,46 @@ impl IndexMut<BddVariable> for BddPartialValuation {
}
}

impl PartialEq for BddPartialValuation {
fn eq(&self, other: &Self) -> bool {
let min_len = min(self.0.len(), other.0.len());
for i in 0..min_len {
if self.0[i] != other.0[i] {
return false;
}
}
for j in min_len..self.0.len() {
if self.0[j].is_some() {
return false;
}
}
for j in min_len..other.0.len() {
if other.0[j].is_some() {
return false;
}
}
true
}
}

impl Eq for BddPartialValuation {}

impl Hash for BddPartialValuation {
fn hash<H: Hasher>(&self, state: &mut H) {
for (var, value) in self.0.iter().enumerate() {
if let Some(value) = value {
state.write_usize(var);
state.write_u8(u8::from(*value))
}
}
}
}

#[cfg(test)]
mod tests {
use crate::{BddPartialValuation, BddValuation, BddVariable};
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

#[test]
fn basic_partial_valuation_properties() {
Expand Down Expand Up @@ -166,6 +205,16 @@ mod tests {

assert_eq!(a, b);
assert_eq!(a, BddPartialValuation::from_values(&a.to_values()));

a.unset_value(v5);
let b = BddPartialValuation::from_values(&[(v1, false), (v2, false)]);
assert_eq!(a, b);

let mut hasher_a = DefaultHasher::new();
let mut hasher_b = DefaultHasher::new();
a.hash(&mut hasher_a);
b.hash(&mut hasher_b);
assert_eq!(hasher_a.finish(), hasher_b.finish());
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub struct BddValuation(Vec<bool>);
/// A partial valuation can be used to quickly construct simple conjunctive/disjunctive clauses.
/// It also exactly describes one path in a `Bdd` and hence can be used as an intermediate
/// value when traversing the valuations of a `Bdd`.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[derive(Clone, Debug)]
pub struct BddPartialValuation(Vec<Option<bool>>);

/// Exhaustively iterates over all valuations with a certain number of variables.
Expand Down

0 comments on commit 89c3456

Please sign in to comment.