Skip to content

Commit 7d53a8d

Browse files
Refactor Lit structure and related functions into a new module for better organization and clarity
1 parent 21ce4bb commit 7d53a8d

2 files changed

Lines changed: 96 additions & 94 deletions

File tree

src/lib.rs

Lines changed: 3 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
//! engine.add_clause(vec![neg(a), pos(b)]); // (¬a ∨ b)
2020
//! engine.assert(pos(a)); // Forces b to be True
2121
//! ```
22+
pub use lit::{FALSE_LIT, Lit, TRUE_LIT, neg, pos};
2223
use std::{
23-
cmp::Ordering,
2424
collections::{HashMap, HashSet, VecDeque},
2525
fmt::{Display, Formatter, Result},
2626
mem,
27-
ops::Not,
2827
};
2928

29+
mod lit;
30+
3031
type Callback = Box<dyn Fn(usize, LBool)>;
3132

3233
#[derive(Clone, Debug, PartialEq, Eq, Default)]
@@ -49,98 +50,6 @@ impl Display for LBool {
4950
}
5051
}
5152

52-
/// A literal is represented as a variable index and a sign (true for positive, false for negative).
53-
///
54-
/// # Examples
55-
/// ```
56-
/// # use watchsat::{Lit, pos, neg};
57-
/// let a = pos(0); // Represents the literal b0
58-
/// let not_a = neg(0); // Represents the literal ¬b0
59-
///
60-
/// assert_eq!(a.var(), 0);
61-
/// assert!(a.is_positive());
62-
/// assert_eq!(not_a.var(), 0);
63-
/// assert!(!not_a.is_positive());
64-
/// ```
65-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
66-
pub struct Lit {
67-
x: usize,
68-
sign: bool,
69-
}
70-
71-
pub const TRUE_LIT: Lit = Lit { x: 0, sign: true };
72-
pub const FALSE_LIT: Lit = Lit { x: 0, sign: false };
73-
74-
impl Lit {
75-
pub fn new(x: usize, sign: bool) -> Self {
76-
Lit { x, sign }
77-
}
78-
79-
pub fn pos(x: usize) -> Self {
80-
Lit { x, sign: true }
81-
}
82-
83-
pub fn neg(x: usize) -> Self {
84-
Lit { x, sign: false }
85-
}
86-
87-
pub fn var(&self) -> usize {
88-
self.x
89-
}
90-
91-
pub fn is_positive(&self) -> bool {
92-
self.sign
93-
}
94-
}
95-
96-
pub fn pos(x: usize) -> Lit {
97-
Lit::pos(x)
98-
}
99-
100-
pub fn neg(x: usize) -> Lit {
101-
Lit::neg(x)
102-
}
103-
104-
impl Default for Lit {
105-
fn default() -> Self {
106-
Lit { x: usize::MAX, sign: false }
107-
}
108-
}
109-
110-
impl Display for Lit {
111-
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
112-
match self.sign {
113-
true => write!(f, "b{}", self.x),
114-
false => write!(f, "¬b{}", self.x),
115-
}
116-
}
117-
}
118-
119-
impl Not for Lit {
120-
type Output = Lit;
121-
122-
fn not(self) -> Lit {
123-
Lit { x: self.x, sign: !self.sign }
124-
}
125-
}
126-
127-
impl Not for &Lit {
128-
type Output = Lit;
129-
130-
fn not(self) -> Lit {
131-
Lit { x: self.x, sign: !self.sign }
132-
}
133-
}
134-
135-
impl PartialOrd for Lit {
136-
fn partial_cmp(&self, other: &Lit) -> Option<Ordering> {
137-
match self.x.partial_cmp(&other.x) {
138-
Some(Ordering::Equal) => self.sign.partial_cmp(&other.sign),
139-
ord => ord,
140-
}
141-
}
142-
}
143-
14453
pub struct Clause {
14554
pub lits: Vec<Lit>,
14655
}

src/lit.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use std::{cmp, fmt, ops};
2+
3+
/// A literal is represented as a variable index and a sign (true for positive, false for negative).
4+
///
5+
/// # Examples
6+
/// ```
7+
/// # use watchsat::{Lit, pos, neg};
8+
/// let a = pos(0); // Represents the literal b0
9+
/// let not_a = neg(0); // Represents the literal ¬b0
10+
///
11+
/// assert_eq!(a.var(), 0);
12+
/// assert!(a.is_positive());
13+
/// assert_eq!(not_a.var(), 0);
14+
/// assert!(!not_a.is_positive());
15+
/// ```
16+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17+
pub struct Lit {
18+
x: usize,
19+
sign: bool,
20+
}
21+
22+
pub const TRUE_LIT: Lit = Lit { x: 0, sign: true };
23+
pub const FALSE_LIT: Lit = Lit { x: 0, sign: false };
24+
25+
impl Lit {
26+
pub fn new(x: usize, sign: bool) -> Self {
27+
Lit { x, sign }
28+
}
29+
30+
pub fn pos(x: usize) -> Self {
31+
Lit { x, sign: true }
32+
}
33+
34+
pub fn neg(x: usize) -> Self {
35+
Lit { x, sign: false }
36+
}
37+
38+
pub fn var(&self) -> usize {
39+
self.x
40+
}
41+
42+
pub fn is_positive(&self) -> bool {
43+
self.sign
44+
}
45+
}
46+
47+
pub fn pos(x: usize) -> Lit {
48+
Lit::pos(x)
49+
}
50+
51+
pub fn neg(x: usize) -> Lit {
52+
Lit::neg(x)
53+
}
54+
55+
impl Default for Lit {
56+
fn default() -> Self {
57+
Lit { x: usize::MAX, sign: false }
58+
}
59+
}
60+
61+
impl fmt::Display for Lit {
62+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63+
match self.sign {
64+
true => write!(f, "b{}", self.x),
65+
false => write!(f, "¬b{}", self.x),
66+
}
67+
}
68+
}
69+
70+
impl ops::Not for Lit {
71+
type Output = Lit;
72+
73+
fn not(self) -> Lit {
74+
Lit { x: self.x, sign: !self.sign }
75+
}
76+
}
77+
78+
impl ops::Not for &Lit {
79+
type Output = Lit;
80+
81+
fn not(self) -> Lit {
82+
Lit { x: self.x, sign: !self.sign }
83+
}
84+
}
85+
86+
impl PartialOrd for Lit {
87+
fn partial_cmp(&self, other: &Lit) -> Option<cmp::Ordering> {
88+
match self.x.partial_cmp(&other.x) {
89+
Some(cmp::Ordering::Equal) => self.sign.partial_cmp(&other.sign),
90+
ord => ord,
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)