|
1 | | -pub mod lit; |
2 | | - |
3 | 1 | use std::{ |
| 2 | + cmp::Ordering, |
4 | 3 | collections::{HashMap, VecDeque}, |
5 | 4 | fmt::{Display, Formatter, Result}, |
6 | 5 | mem, |
| 6 | + ops::Not, |
7 | 7 | }; |
8 | 8 |
|
9 | | -use crate::lit::LBool; |
10 | | -pub use lit::Lit; |
11 | 9 | type Callback = Box<dyn Fn(&Engine, usize)>; |
12 | 10 |
|
| 11 | +#[derive(Clone, Debug, PartialEq, Eq, Default)] |
| 12 | +pub enum LBool { |
| 13 | + True, |
| 14 | + False, |
| 15 | + #[default] |
| 16 | + Undef, |
| 17 | +} |
| 18 | + |
| 19 | +impl Display for LBool { |
| 20 | + fn fmt(&self, f: &mut Formatter<'_>) -> Result { |
| 21 | + let s = match self { |
| 22 | + LBool::True => "True", |
| 23 | + LBool::False => "False", |
| 24 | + LBool::Undef => "Undef", |
| 25 | + }; |
| 26 | + write!(f, "{}", s) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 31 | +pub struct Lit { |
| 32 | + x: usize, |
| 33 | + sign: bool, |
| 34 | +} |
| 35 | + |
| 36 | +impl Lit { |
| 37 | + pub fn new(x: usize, sign: bool) -> Self { |
| 38 | + Lit { x, sign } |
| 39 | + } |
| 40 | + |
| 41 | + pub fn var(&self) -> usize { |
| 42 | + self.x |
| 43 | + } |
| 44 | + |
| 45 | + pub fn is_positive(&self) -> bool { |
| 46 | + self.sign |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +pub fn pos(x: usize) -> Lit { |
| 51 | + Lit::new(x, true) |
| 52 | +} |
| 53 | + |
| 54 | +pub fn neg(x: usize) -> Lit { |
| 55 | + Lit::new(x, false) |
| 56 | +} |
| 57 | + |
| 58 | +impl Display for Lit { |
| 59 | + fn fmt(&self, f: &mut Formatter<'_>) -> Result { |
| 60 | + match self.sign { |
| 61 | + true => write!(f, "b{}", self.x), |
| 62 | + false => write!(f, "¬b{}", self.x), |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl Not for Lit { |
| 68 | + type Output = Lit; |
| 69 | + |
| 70 | + fn not(self) -> Lit { |
| 71 | + Lit { x: self.x, sign: !self.sign } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl PartialOrd for Lit { |
| 76 | + fn partial_cmp(&self, other: &Lit) -> Option<Ordering> { |
| 77 | + match self.x.partial_cmp(&other.x) { |
| 78 | + Some(Ordering::Equal) => self.sign.partial_cmp(&other.sign), |
| 79 | + ord => ord, |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
13 | 84 | #[derive(Default)] |
14 | 85 | pub struct Engine { |
15 | 86 | assigns: Vec<LBool>, // Current assignments of variables |
@@ -209,7 +280,6 @@ impl Display for Engine { |
209 | 280 | #[cfg(test)] |
210 | 281 | mod tests { |
211 | 282 | use super::*; |
212 | | - use crate::lit::{neg, pos}; |
213 | 283 |
|
214 | 284 | #[test] |
215 | 285 | fn test_basic_assignment_and_value() { |
|
0 commit comments