Skip to content

Commit fb55290

Browse files
Refactor InfRational and Lin structs to streamline conversions and remove unused functions
1 parent b73f8e6 commit fb55290

4 files changed

Lines changed: 20 additions & 20 deletions

File tree

src/inf_rational.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,21 @@ impl InfRational {
3232
pub const EPSILON: Self = Self { rat: Rational::ZERO, inf: Rational::ONE };
3333
}
3434

35+
impl From<(Rational, Rational)> for InfRational {
36+
fn from(arg: (Rational, Rational)) -> Self {
37+
InfRational::new(arg.0, arg.1)
38+
}
39+
}
40+
3541
impl From<Rational> for InfRational {
3642
fn from(arg: Rational) -> Self {
37-
InfRational { rat: arg, inf: Rational::ZERO }
43+
InfRational::new(arg, Rational::ZERO)
3844
}
3945
}
4046

4147
impl From<i64> for InfRational {
4248
fn from(arg: i64) -> Self {
43-
InfRational { rat: Rational::from(arg), inf: Rational::ZERO }
49+
InfRational::new(Rational::from(arg), Rational::ZERO)
4450
}
4551
}
4652

src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ mod rational;
2424
mod var;
2525

2626
pub use inf_rational::InfRational;
27-
pub use lin::{Lin, vc};
27+
pub use lin::Lin;
2828
pub use rational::Rational;
2929
use std::{
3030
collections::{BTreeMap, HashMap, HashSet},
@@ -658,8 +658,6 @@ impl fmt::Display for Engine {
658658

659659
#[cfg(test)]
660660
mod tests {
661-
use crate::lin::vc;
662-
663661
use super::*;
664662

665663
#[test]
@@ -758,7 +756,7 @@ mod tests {
758756
let x = e.add_var();
759757
let y = e.add_var();
760758
// x + y <= 5
761-
let lhs = vc(x, 1) + vc(y, 1);
759+
let lhs = Lin::from(x) + Lin::from(y);
762760
assert!(e.new_le(&lhs, &Lin::from(5), None).is_ok());
763761
assert!(e.check().is_ok());
764762
assert!(e.ub(VarId(e.assignments.len() - 1)) <= &InfRational::from(Rational::from(5))); // slack ub
@@ -925,8 +923,8 @@ mod tests {
925923
let mut e = Engine::new();
926924
let x = e.add_var();
927925
let y = e.add_var();
928-
let s1 = e.add_lin_var(vc(x, -1) + vc(y, 1)); // s1 = y - x
929-
let s2 = e.add_lin_var(vc(x, 1) + vc(y, 1)); // s2 = x + y
926+
let s1 = e.add_lin_var(Lin::from(y) - Lin::from(x)); // s1 = y - x
927+
let s2 = e.add_lin_var(Lin::from(x) + Lin::from(y)); // s2 = x + y
930928

931929
let c0 = e.add_guard();
932930
assert!(e.new_le(&Lin::from(x), &Lin::from(-4), Some(c0)).is_ok()); // x <= -4
@@ -1010,7 +1008,7 @@ mod tests {
10101008

10111009
// Test lin_lb with negative coefficient: 10 - 2*x + 3*y
10121010
// lb = 10 - 2*ub(x) + 3*lb(y) = 10 - 2*5 + 3*3 = 10 - 10 + 9 = 9
1013-
let lin = Lin::from(10) + vc(x, -2) + vc(y, 3);
1011+
let lin = Lin::from(10) + Lin::from(x) * Rational::from(-2) + Lin::from(y) * Rational::from(3);
10141012
let lb = e.lin_lb(&lin);
10151013
assert_eq!(lb, InfRational::from(Rational::from(9)));
10161014

src/lin.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ impl Lin {
2020
Lin { vars, known_term }
2121
}
2222

23-
pub fn new_var(var: VarId, coeff: Rational) -> Self {
24-
let mut vars = HashMap::new();
25-
vars.insert(var, coeff);
26-
Lin::new(vars, Rational::ZERO)
27-
}
28-
2923
/// Substitutes a variable with another linear expression.
3024
///
3125
/// If this expression is $L$ and the substitution is $x_i = E$, this method
@@ -86,14 +80,10 @@ impl From<i64> for Lin {
8680

8781
impl From<VarId> for Lin {
8882
fn from(var: VarId) -> Self {
89-
Lin::new_var(var, Rational::ONE)
83+
Lin::new(HashMap::from([(var, Rational::ONE)]), Rational::ZERO)
9084
}
9185
}
9286

93-
pub fn vc(idx: VarId, coeff: i64) -> Lin {
94-
Lin::new_var(idx, Rational::from(coeff))
95-
}
96-
9787
impl fmt::Display for Lin {
9888
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
9989
// Sort terms to ensure deterministic formatting regardless of HashMap iteration order.

src/rational.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ impl Rational {
7474
pub const ONE: Self = Self { num: 1, den: 1 };
7575
}
7676

77+
impl From<(i64, i64)> for Rational {
78+
fn from(arg: (i64, i64)) -> Self {
79+
Rational::new(arg.0, arg.1)
80+
}
81+
}
82+
7783
impl From<i64> for Rational {
7884
fn from(arg: i64) -> Self {
7985
Rational::new(arg, 1)

0 commit comments

Comments
 (0)