Cnf

Struct Cnf 

Source
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 of Literal used in the clauses. Defaults to PackedLiteral.
  • S: The LiteralStorage type used within each Clause. Defaults to SmallVec<[L; 8]>.

Fields§

§clauses: Vec<Clause<L, S>>

The list of clauses that make up the CNF formula.

§num_vars: usize

The 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: usize

The 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>

Source

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 yielding i32 (DIMACS literals for a single clause).
  • I: An iterator yielding J (an iterator of clauses).
Source

pub fn remove(&mut self, idx: usize)

Removes a clause at the specified index.

§Arguments
  • idx: The index of the clause to remove.
§Panics

Panics if idx is out of bounds.

Source

pub fn iter(&self) -> impl Iterator<Item = &Clause<T, S>>

Returns an iterator over the clauses in the CNF.

Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Clause<T, S>>

Returns a mutable iterator over the clauses in the CNF.

Source

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: The Clause<T, S> to add.
Source

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 of i32 representing the clause.
Source

pub const fn len(&self) -> usize

Returns the total number of clauses in the CNF (both original and learnt).

Source

pub const fn is_empty(&self) -> bool

Returns true if the CNF contains no clauses.

Source

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: A Solutions object providing the truth assignment for variables. Solutions handles DIMACS-style variable IDs (1-indexed, signed).
§Returns

true if solutions satisfies all clauses in the CNF, false otherwise.

Source

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 target Literal type for the new CNF.
  • U: The target LiteralStorage<L> type for clauses in the new CNF.
§Returns

A new Cnf<L, U> instance.

Trait Implementations§

Source§

impl<L: Clone + Literal, S: Clone + LiteralStorage<L>> Clone for Cnf<L, S>

Source§

fn clone(&self) -> Cnf<L, S>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<L: Debug + Literal, S: Debug + LiteralStorage<L>> Debug for Cnf<L, S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<L: Default + Literal, S: Default + LiteralStorage<L>> Default for Cnf<L, S>

Source§

fn default() -> Cnf<L, S>

Returns the “default value” for a type. Read more
Source§

impl<L: Literal, S: LiteralStorage<L>> Display for Cnf<L, S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the CNF into DIMACS CNF format.

Example output:

c Generated by CNF
p cnf <num_vars> <num_clauses>
1 -2 0
2 3 0
Source§

impl<T: Literal, S: LiteralStorage<T>> From<Expr> for Cnf<T, S>

Source§

fn from(expr: Expr) -> Self

Converts an Expr into a Cnf<T, S>. This is a convenience wrapper around to_cnf.

Source§

impl<L: Literal, S: LiteralStorage<L>> From<Vec<Clause<L, S>>> for Cnf<L, S>

Source§

fn from(clauses: Vec<Clause<L, S>>) -> Self

Converts a Vec<Clause<L, S>> directly into a Cnf<L, S>. Uses from_iter for consistent initialisation.

Source§

impl<L: Literal, S: LiteralStorage<L>> From<Vec<Vec<i32>>> for Cnf<L, S>

Source§

fn from(value: Vec<Vec<i32>>) -> Self

Converts Vec<Vec<i32>> (DIMACS clauses) into a Cnf<L, S>. Uses Cnf::new for construction.

Source§

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

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: Hash + Literal, S: Hash + LiteralStorage<L>> Hash for Cnf<L, S>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Literal, S: LiteralStorage<T>> Index<usize> for Cnf<T, S>

Source§

fn index(&self, index: usize) -> &Self::Output

Accesses the clause at the given index.

§Panics

Panics if index is out of bounds.

§Safety

This implementation uses get_unchecked for performance

Source§

type Output = Clause<T, S>

The returned type after indexing.
Source§

impl<T: Literal, S: LiteralStorage<T>> IndexMut<usize> for Cnf<T, S>

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<L: Ord + Literal, S: Ord + LiteralStorage<L>> Ord for Cnf<L, S>

Source§

fn cmp(&self, other: &Cnf<L, S>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<L: PartialEq + Literal, S: PartialEq + LiteralStorage<L>> PartialEq for Cnf<L, S>

Source§

fn eq(&self, other: &Cnf<L, S>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<L: PartialOrd + Literal, S: PartialOrd + LiteralStorage<L>> PartialOrd for Cnf<L, S>

Source§

fn partial_cmp(&self, other: &Cnf<L, S>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Literal, S: LiteralStorage<T>> TryFrom<Cnf<T, S>> for Expr

Source§

fn try_from(cnf: Cnf<T, S>) -> Result<Self, Self::Error>

Attempts to convert a Cnf<T, S> back into an Expr. The resulting Expr will be a conjunction of disjunctions. Returns an error if the CNF is empty (contains no clauses).

Source§

type Error = &'static str

The type returned in the event of a conversion error.
Source§

impl<L: Eq + Literal, S: Eq + LiteralStorage<L>> Eq for Cnf<L, S>

Source§

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>

§

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>
where L: Unpin, S: Unpin,

§

impl<L, S> UnwindSafe for Cnf<L, S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.