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
use std::ffi::c_char;

use crate::VariableId;

#[derive(Copy, Clone, Debug)]
pub enum ConstraintType {
    LessThanEq,
    Eq,
    GreaterThanEq,
}

impl ConstraintType {
    pub(crate) fn into_raw(self) -> c_char {
        match self {
            ConstraintType::LessThanEq => 'L' as c_char,
            ConstraintType::Eq => 'E' as c_char,
            ConstraintType::GreaterThanEq => 'G' as c_char,
        }
    }
}

#[derive(Clone, Debug)]
pub struct Constraint {
    weights: Vec<(VariableId, f64)>,
    type_: ConstraintType,
    rhs: f64,
    name: Option<String>,
}

impl Constraint {
    pub fn new(
        ty: ConstraintType,
        rhs: f64,
        name: Option<String>,
        vars: Vec<(VariableId, f64)>,
    ) -> Constraint {
        Constraint {
            weights: vars,
            type_: ty,
            rhs,
            name,
        }
    }

    pub fn name(&self) -> Option<&str> {
        self.name.as_deref()
    }

    pub fn weights(&self) -> &[(VariableId, f64)] {
        &self.weights
    }

    pub fn rhs(&self) -> f64 {
        self.rhs
    }

    pub fn type_(&self) -> ConstraintType {
        self.type_
    }
}