Skip to content

Commit ed33236

Browse files
Refactor LBool and Lit structures into lib.rs; remove lit.rs module
1 parent 76e375a commit ed33236

2 files changed

Lines changed: 75 additions & 145 deletions

File tree

src/lib.rs

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,86 @@
1-
pub mod lit;
2-
31
use std::{
2+
cmp::Ordering,
43
collections::{HashMap, VecDeque},
54
fmt::{Display, Formatter, Result},
65
mem,
6+
ops::Not,
77
};
88

9-
use crate::lit::LBool;
10-
pub use lit::Lit;
119
type Callback = Box<dyn Fn(&Engine, usize)>;
1210

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+
1384
#[derive(Default)]
1485
pub struct Engine {
1586
assigns: Vec<LBool>, // Current assignments of variables
@@ -209,7 +280,6 @@ impl Display for Engine {
209280
#[cfg(test)]
210281
mod tests {
211282
use super::*;
212-
use crate::lit::{neg, pos};
213283

214284
#[test]
215285
fn test_basic_assignment_and_value() {

src/lit.rs

Lines changed: 0 additions & 140 deletions
This file was deleted.

0 commit comments

Comments
 (0)