1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use crate::{BddPartialValuation, BddVariable};
use std::convert::TryFrom;
impl BddPartialValuation {
pub fn empty() -> BddPartialValuation {
BddPartialValuation(Vec::new())
}
pub fn is_empty(&self) -> bool {
self.0.iter().all(|it| it.is_none())
}
pub fn cardinality(&self) -> u16 {
u16::try_from(self.0.iter().filter(|it| it.is_some()).count()).unwrap()
}
pub fn last_fixed_variable(&self) -> Option<BddVariable> {
for i in (0..self.0.len()).rev() {
if self.0[i].is_some() {
return Some(BddVariable(i as u16));
}
}
None
}
pub fn from_values(values: &[(BddVariable, bool)]) -> BddPartialValuation {
let mut result = Self::empty();
for (id, value) in values {
result.set_value(*id, *value)
}
result
}
pub fn to_values(&self) -> Vec<(BddVariable, bool)> {
self.0
.iter()
.enumerate()
.filter_map(|(i, value)| value.map(|value| (BddVariable(i as u16), value)))
.collect()
}
pub fn get_value(&self, id: BddVariable) -> Option<bool> {
let index = usize::from(id.0);
self.0.get(index).cloned().flatten()
}
pub fn has_value(&self, id: BddVariable) -> bool {
self.get_value(id).is_some()
}
pub fn set_value(&mut self, id: BddVariable, value: bool) {
let cell = self.mut_cell(id);
*cell = Some(value);
}
pub fn unset_value(&mut self, id: BddVariable) {
let cell = self.mut_cell(id);
*cell = None;
}
fn mut_cell(&mut self, id: BddVariable) -> &mut Option<bool> {
let index = usize::from(id.0);
while self.0.len() <= index {
self.0.push(None);
}
&mut self.0[index]
}
pub fn extends(&self, valuation: &BddPartialValuation) -> bool {
for var_id in 0..(valuation.0.len() as u16) {
let var = BddVariable(var_id);
let expected = valuation.get_value(var);
if expected.is_some() && self.get_value(var) != expected {
return false;
}
}
true
}
}
impl Default for BddPartialValuation {
fn default() -> Self {
Self::empty()
}
}
#[cfg(test)]
mod tests {
use crate::{BddPartialValuation, BddVariable};
#[test]
fn basic_partial_valuation_properties() {
let v1 = BddVariable(1);
let v2 = BddVariable(2);
let v5 = BddVariable(5);
let mut a = BddPartialValuation::default();
assert!(a.last_fixed_variable().is_none());
assert!(!a.has_value(v1));
a.set_value(v1, true);
assert!(a.has_value(v1));
assert_eq!(Some(true), a.get_value(v1));
a.set_value(v2, false);
a.unset_value(v1);
assert!(a.has_value(v2));
assert!(!a.has_value(v1));
a.set_value(v5, true);
assert_eq!(Some(v5), a.last_fixed_variable());
a.set_value(v1, false);
assert_eq!(3, a.cardinality());
let b = BddPartialValuation::from_values(&[(v1, false), (v5, true), (v2, false)]);
assert_eq!(a, b);
assert_eq!(a, BddPartialValuation::from_values(&a.to_values()));
}
}