pub struct Literal {
pub var: u32,
pub negated: bool,
}Expand description
A propositional variable with optional negation.
Literals are the atomic units of SAT formulas. A literal is either a variable (positive literal) or the negation of a variable (negative literal).
Variables are 0-indexed internally but convert to 1-indexed for DIMACS format.
§Memory Layout
The struct is designed for efficient GPU transfer with a compact representation:
var: 4 bytes (u32 variable index)negated: 1 byte (bool), with potential padding
For bulk GPU operations, consider using the packed representation via to_packed().
Fields§
§var: u32The variable index (0-indexed).
negated: boolWhether this literal is negated.
Implementations§
Source§impl Literal
impl Literal
Sourcepub const fn new(var: u32, negated: bool) -> Self
pub const fn new(var: u32, negated: bool) -> Self
Creates a literal from a variable and sign.
§Arguments
var- The variable index (0-indexed)negated- Whether the literal is negated
Sourcepub const fn negate(self) -> Self
pub const fn negate(self) -> Self
Returns the negation of this literal.
§Example
use xlog_solve::Literal;
let pos = Literal::positive(5);
let neg = pos.negate();
assert!(neg.negated);
assert_eq!(neg.var, 5);Sourcepub fn eval(self, assignment: &[bool]) -> bool
pub fn eval(self, assignment: &[bool]) -> bool
Evaluates this literal under a given assignment.
§Arguments
assignment- A slice of boolean values where index i represents variable i
§Returns
true if the literal is satisfied by the assignment, false otherwise.
§Panics
Panics if self.var >= assignment.len() (variable index out of bounds).
§Example
use xlog_solve::Literal;
let lit = Literal::positive(1);
assert!(lit.eval(&[false, true, false])); // x1 = trueSourcepub fn to_dimacs(self) -> i32
pub fn to_dimacs(self) -> i32
Converts this literal to DIMACS format.
DIMACS format uses 1-indexed variables, with negative numbers for negated literals.
§Returns
A signed integer where:
- Positive values represent positive literals (var + 1)
- Negative values represent negative literals (-(var + 1))
§Example
use xlog_solve::Literal;
assert_eq!(Literal::positive(0).to_dimacs(), 1);
assert_eq!(Literal::negative(2).to_dimacs(), -3);Sourcepub fn from_dimacs(dimacs: i32) -> Self
pub fn from_dimacs(dimacs: i32) -> Self
Creates a literal from DIMACS format.
DIMACS format uses 1-indexed variables, with negative numbers for negated literals.
§Arguments
dimacs- A non-zero signed integer in DIMACS format
§Panics
Panics if dimacs == 0 (zero is not a valid DIMACS literal).
§Example
use xlog_solve::Literal;
let pos = Literal::from_dimacs(3);
assert_eq!(pos.var, 2);
assert!(!pos.negated);
let neg = Literal::from_dimacs(-5);
assert_eq!(neg.var, 4);
assert!(neg.negated);Sourcepub fn to_packed(self) -> u32
pub fn to_packed(self) -> u32
Returns a packed 32-bit representation suitable for GPU transfer.
The packed format stores the variable in the lower 31 bits and the negation flag in the most significant bit.
§Returns
A u32 where bit 31 is the negation flag and bits 0-30 are the variable.
Sourcepub fn from_packed(packed: u32) -> Self
pub fn from_packed(packed: u32) -> Self
Creates a literal from a packed 32-bit representation.
§Arguments
packed- A u32 with negation in bit 31 and variable in bits 0-30
Trait Implementations§
Source§impl FromIterator<Literal> for Clause
impl FromIterator<Literal> for Clause
Source§impl Ord for Literal
impl Ord for Literal
Source§fn cmp(&self, other: &Self) -> Ordering
fn cmp(&self, other: &Self) -> Ordering
Orders literals first by variable index, then by negation (positive before negative).