pub struct Cnf<L: Literal = PackedLiteral, S: LiteralStorage<L> = SmallVec<[L; 8]>> {
pub clauses: Vec<Clause<L, S>>,
pub num_vars: usize,
pub vars: Vec<Variable>,
pub lits: Vec<L>,
pub non_learnt_idx: usize,
}Expand description
Represents a boolean formula in Conjunctive Normal Form (CNF).
A CNF formula is a collection of clauses. The formula is satisfied if and only if all its clauses are satisfied.
§Type Parameters
L: The type ofLiteralused in the clauses. Defaults toPackedLiteral.S: TheLiteralStoragetype used within eachClause. Defaults toSmallVec<[L; 8]>.
Fields§
§clauses: Vec<Clause<L, S>>The list of clauses that make up the CNF formula.
num_vars: usizeThe highest variable identifier encountered in the formula, plus one.
This represents the number of distinct variables if they are numbered contiguously from 0 or 1.
If variables are 1-indexed v1, ..., vn, num_vars would be n+1.
If variables are 0-indexed v0, ..., v(n-1), num_vars would be n.
vars: Vec<Variable>A flat list of all variable identifiers present in the formula. May contain duplicates.
lits: Vec<L>A flat list of all literals present across all clauses. May contain duplicates.
non_learnt_idx: usizeThe index in clauses vector that separates original problem clauses from learnt clauses.
Clauses from 0 to non_learnt_idx - 1 are original.
Clauses from non_learnt_idx onwards are learnt during solving.
When a Cnf is first created from a problem, non_learnt_idx is equal to clauses.len().
Implementations§
Source§impl<T: Literal, S: LiteralStorage<T>> Cnf<T, S>
impl<T: Literal, S: LiteralStorage<T>> Cnf<T, S>
Sourcepub fn new<J: IntoIterator<Item = i32>, I: IntoIterator<Item = J>>(
clauses_iter: I,
) -> Self
pub fn new<J: IntoIterator<Item = i32>, I: IntoIterator<Item = J>>( clauses_iter: I, ) -> Self
Creates a new Cnf instance from an iterator of clauses, where each clause
is itself an iterator of i32 (DIMACS literals).
Example: Cnf::new(vec![vec![1, -2], vec![2, 3]]) creates a CNF for
(x1 OR !x2) AND (x2 OR x3).
During construction, it determines num_vars based on the maximum variable ID
encountered. vars and lits collect all variables and literals.
non_learnt_idx is set to the total number of initial clauses.
§Type Parameters for Arguments
J: An iterator yieldingi32(DIMACS literals for a single clause).I: An iterator yieldingJ(an iterator of clauses).
Sourcepub fn iter(&self) -> impl Iterator<Item = &Clause<T, S>>
pub fn iter(&self) -> impl Iterator<Item = &Clause<T, S>>
Returns an iterator over the clauses in the CNF.
Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Clause<T, S>>
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Clause<T, S>>
Returns a mutable iterator over the clauses in the CNF.
Sourcepub fn add_clause(&mut self, clause: Clause<T, S>)
pub fn add_clause(&mut self, clause: Clause<T, S>)
Adds a new clause to the CNF.
The clause is added to the end of the clauses list. If it’s considered a learnt
clause, it should be added after non_learnt_idx. This function implicitly adds
it as if it’s a new problem clause if non_learnt_idx isn’t managed externally
before calling this for learnt clauses.
Updates num_vars, vars, and lits based on the new clause.
§Arguments
clause: TheClause<T, S>to add.
Sourcepub fn add_clause_vec(&mut self, clause_dimacs: Vec<i32>)
pub fn add_clause_vec(&mut self, clause_dimacs: Vec<i32>)
Adds a new clause specified as a Vec<i32> (DIMACS literals).
Converts clause_dimacs to a Clause<T, S> and then calls add_clause.
§Arguments
clause_dimacs: A vector ofi32representing the clause.
Sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Returns the total number of clauses in the CNF (both original and learnt).
Sourcepub fn verify(&self, solutions: &Solutions) -> bool
pub fn verify(&self, solutions: &Solutions) -> bool
Verifies if a given set of solutions satisfies the CNF formula.
A CNF is satisfied if every clause in it is satisfied. A clause is satisfied if at least one of its literals is true under the given assignment.
§Arguments
solutions: ASolutionsobject providing the truth assignment for variables.Solutionshandles DIMACS-style variable IDs (1-indexed, signed).
§Returns
true if solutions satisfies all clauses in the CNF, false otherwise.
Sourcepub fn convert<TargetL: Literal, TargetS: LiteralStorage<TargetL>>(
&self,
) -> Cnf<TargetL, TargetS>
pub fn convert<TargetL: Literal, TargetS: LiteralStorage<TargetL>>( &self, ) -> Cnf<TargetL, TargetS>
Converts this Cnf<T, S> into a Cnf<L, U> with different literal or storage types.
Each clause is converted using Clause::convert. Metadata like num_vars,
vars, lits, and non_learnt_idx are also transformed or cloned.
§Type Parameters
L: The targetLiteraltype for the new CNF.U: The targetLiteralStorage<L>type for clauses in the new CNF.
§Returns
A new Cnf<L, U> instance.
Trait Implementations§
Source§impl<L: Literal, S: LiteralStorage<L>> FromIterator<Clause<L, S>> for Cnf<L, S>
impl<L: Literal, S: LiteralStorage<L>> FromIterator<Clause<L, S>> for Cnf<L, S>
Source§fn from_iter<IterClauses: IntoIterator<Item = Clause<L, S>>>(
iter: IterClauses,
) -> Self
fn from_iter<IterClauses: IntoIterator<Item = Clause<L, S>>>( iter: IterClauses, ) -> Self
Creates a Cnf from an iterator of Clause<L, S>.
Each clause from the iterator is added using self.add_clause.
This initialises a default Cnf and then populates it.
non_learnt_idx will be implicitly managed by add_clause if it updates it,
or will remain 0 if add_clause only appends to clauses.
A more robust way would be to collect clauses, then initialise Cnf fields properly.
Source§impl<L: Ord + Literal, S: Ord + LiteralStorage<L>> Ord for Cnf<L, S>
impl<L: Ord + Literal, S: Ord + LiteralStorage<L>> Ord for Cnf<L, S>
Source§impl<L: PartialOrd + Literal, S: PartialOrd + LiteralStorage<L>> PartialOrd for Cnf<L, S>
impl<L: PartialOrd + Literal, S: PartialOrd + LiteralStorage<L>> PartialOrd for Cnf<L, S>
Source§impl<T: Literal, S: LiteralStorage<T>> TryFrom<Cnf<T, S>> for Expr
impl<T: Literal, S: LiteralStorage<T>> TryFrom<Cnf<T, S>> for Expr
impl<L: Eq + Literal, S: Eq + LiteralStorage<L>> Eq for Cnf<L, S>
impl<L: Literal, S: LiteralStorage<L>> StructuralPartialEq for Cnf<L, S>
Auto Trait Implementations§
impl<L, S> Freeze for Cnf<L, S>
impl<L, S> RefUnwindSafe for Cnf<L, S>where
L: RefUnwindSafe,
S: RefUnwindSafe,
impl<L = PackedLiteral, S = SmallVec<[L; 8]>> !Send for Cnf<L, S>
impl<L = PackedLiteral, S = SmallVec<[L; 8]>> !Sync for Cnf<L, S>
impl<L, S> Unpin for Cnf<L, S>
impl<L, S> UnwindSafe for Cnf<L, S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more