Skip to content

Commit b9b4b94

Browse files
Refactor Clause struct to use named fields and update Engine to access literals accordingly
1 parent 5a97da2 commit b9b4b94

1 file changed

Lines changed: 16 additions & 14 deletions

File tree

src/lib.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,13 @@ impl PartialOrd for Lit {
117117
}
118118
}
119119

120-
pub struct Clause(pub Vec<Lit>);
120+
struct Clause {
121+
lits: Vec<Lit>,
122+
}
121123

122124
impl Display for Clause {
123125
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
124-
let lits: Vec<String> = self.0.iter().map(|l| l.to_string()).collect();
126+
let lits: Vec<String> = self.lits.iter().map(|l| l.to_string()).collect();
125127
write!(f, "{}", lits.join(" ∨ "))
126128
}
127129
}
@@ -214,7 +216,7 @@ impl Engine {
214216
self.neg_watches[lit.var()].push(clause_id);
215217
}
216218
}
217-
self.clauses.push(Clause(lits));
219+
self.clauses.push(Clause { lits });
218220
true
219221
}
220222

@@ -278,7 +280,7 @@ impl Engine {
278280

279281
loop {
280282
// 1. Process the current clause (either the conflict or a reason)
281-
for lit in &self.clauses[clause].0 {
283+
for lit in &self.clauses[clause].lits {
282284
let v = lit.var();
283285

284286
// Skip the variable we are currently resolving away
@@ -337,11 +339,11 @@ impl Engine {
337339

338340
fn propagate(&mut self, clause_id: usize, lit: Lit) -> bool {
339341
// Ensure the first literal is not the one that was just assigned
340-
if self.clauses[clause_id].0[0].var() == lit.var() {
341-
self.clauses[clause_id].0.swap(0, 1);
342+
if self.clauses[clause_id].lits[0].var() == lit.var() {
343+
self.clauses[clause_id].lits.swap(0, 1);
342344
}
343345
// Check if clause is already satisfied
344-
if self.lit_value(&self.clauses[clause_id].0[0]) == LBool::True {
346+
if self.lit_value(&self.clauses[clause_id].lits[0]) == LBool::True {
345347
// Re-add the clause to the watch list
346348
if lit.is_positive() {
347349
self.pos_watches[lit.var()].push(clause_id);
@@ -352,15 +354,15 @@ impl Engine {
352354
}
353355

354356
// Find the next unassigned literal
355-
for i in 2..self.clauses[clause_id].0.len() {
356-
if self.lit_value(&self.clauses[clause_id].0[i]) != LBool::False {
357+
for i in 2..self.clauses[clause_id].lits.len() {
358+
if self.lit_value(&self.clauses[clause_id].lits[i]) != LBool::False {
357359
// Move this literal to the second position
358-
self.clauses[clause_id].0.swap(1, i);
360+
self.clauses[clause_id].lits.swap(1, i);
359361
// Update watch lists
360-
if self.clauses[clause_id].0[1].is_positive() {
361-
self.pos_watches[self.clauses[clause_id].0[1].var()].push(clause_id);
362+
if self.clauses[clause_id].lits[1].is_positive() {
363+
self.pos_watches[self.clauses[clause_id].lits[1].var()].push(clause_id);
362364
} else {
363-
self.neg_watches[self.clauses[clause_id].0[1].var()].push(clause_id);
365+
self.neg_watches[self.clauses[clause_id].lits[1].var()].push(clause_id);
364366
}
365367
return true;
366368
}
@@ -372,7 +374,7 @@ impl Engine {
372374
} else {
373375
self.pos_watches[lit.var()].push(clause_id);
374376
}
375-
self.enqueue(self.clauses[clause_id].0[0], Some(clause_id))
377+
self.enqueue(self.clauses[clause_id].lits[0], Some(clause_id))
376378
}
377379

378380
pub fn add_listener<F>(&mut self, var: usize, listener: F)

0 commit comments

Comments
 (0)