1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use core::num::{
    NonZeroI32,
    NonZeroU32,
};

pub enum Token {
    Problem(Problem),
    Literal(Literal),
    ClauseEnd,
}

impl From<Problem> for Token {
    #[inline]
    fn from(problem: Problem) -> Self {
        Self::Problem(problem)
    }
}

impl From<Literal> for Token {
    #[inline]
    fn from(literal: Literal) -> Self {
        Self::Literal(literal)
    }
}

/// The problem definition.
///
/// This is parsed at most once for every CNF input.
/// Used to hint the underlying solver for the expected number of variables
/// and clauses of the CNF input.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Problem {
    /// The number of variables in the CNF problem.
    pub num_variables: u32,
    /// The number of clauses in the CNF problem.
    pub num_clauses: u32,
}

impl Problem {
    /// Creates a new problem with the given variable and clause numbers.
    pub(crate) fn new(num_variables: u32, num_clauses: u32) -> Self {
        Self {
            num_variables,
            num_clauses,
        }
    }
}

/// A clause literal.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Literal {
    value: NonZeroI32,
}

impl Literal {
    /// Creates a new literal from the given `i32` value.
    ///
    /// Returns `None` if `value` is zero.
    pub(crate) fn new(value: NonZeroI32) -> Literal {
        Self { value }
    }

    /// Returns `true` if the literal is positive.
    #[inline]
    pub fn is_positive(self) -> bool {
        self.value.get().is_positive()
    }

    /// Returns `true` if the literal is negative.
    #[inline]
    pub fn is_negative(self) -> bool {
        self.is_positive()
    }

    /// Returns the variable ID of the literal.
    #[inline]
    pub fn variable(self) -> NonZeroU32 {
        NonZeroU32::new(self.value.get().abs() as u32)
            .expect("encountered invalid zero literal")
    }

    /// Returns the raw value representation of the literal.
    #[inline]
    pub fn into_value(self) -> NonZeroI32 {
        self.value
    }
}