From cb28d0578942d2c4f98b47567b10ea3e534db9e0 Mon Sep 17 00:00:00 2001 From: Matthew Wang Date: Thu, 3 Aug 2023 14:14:21 -0400 Subject: [PATCH] Fix `Cnf::new()` to borrow top-level slice instead of vector --- src/repr/cnf.rs | 14 +++++++------- src/repr/unit_prop.rs | 12 ++++++------ src/util/hypergraph.rs | 2 +- tests/test.rs | 4 ++-- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/repr/cnf.rs b/src/repr/cnf.rs index d8c48ad2..63e34f9a 100644 --- a/src/repr/cnf.rs +++ b/src/repr/cnf.rs @@ -263,7 +263,7 @@ impl Iterator for AssignmentIter { } impl Cnf { - pub fn new(clauses: Vec>) -> Cnf { + pub fn new(clauses: &[Vec]) -> Cnf { let clauses: Vec> = clauses .iter() .filter(|clause| !clause.is_empty()) @@ -320,7 +320,7 @@ impl Cnf { } clause_vec.push(lit_vec); } - Cnf::new(clause_vec) + Cnf::new(&clause_vec) } /// Parses a CNF string into a CNF @@ -354,7 +354,7 @@ impl Cnf { } clause_vec.push(c); } - Cnf::new(clause_vec) + Cnf::new(&clause_vec) } pub fn rand_cnf(rng: &mut ThreadRng, num_vars: usize, num_clauses: usize) -> Cnf { @@ -380,7 +380,7 @@ impl Cnf { clause_vec.push(vec![var]); } } - Cnf::new(clause_vec) + Cnf::new(&clause_vec) } pub fn num_vars(&self) -> usize { @@ -580,7 +580,7 @@ impl Cnf { } }) .collect(); - Cnf::new(new_cnf) + Cnf::new(&new_cnf) } pub fn interaction_graph(&self) -> UnGraph { @@ -680,7 +680,7 @@ impl Arbitrary for Cnf { } clauses.push(clause); } - Cnf::new(clauses) + Cnf::new(&clauses) } } @@ -721,7 +721,7 @@ fn test_cnf_wmc() { Literal::new(VarLabel::new(0), true), Literal::new(VarLabel::new(1), false), ]]; - let cnf = Cnf::new(v); + let cnf = Cnf::new(&v); let weights: std::collections::HashMap< VarLabel, ( diff --git a/src/repr/unit_prop.rs b/src/repr/unit_prop.rs index 29dcf725..9b0dbadc 100644 --- a/src/repr/unit_prop.rs +++ b/src/repr/unit_prop.rs @@ -497,7 +497,7 @@ fn test_unit_propagate_1() { ], ]; - let cnf = Cnf::new(v); + let cnf = Cnf::new(&v); match UnitPropagate::new(cnf) { None => panic!("test failed - no partial model generated"), Some((_, assgn)) => { @@ -521,7 +521,7 @@ fn test_unit_propagate_2() { ], ]; - let cnf = Cnf::new(v); + let cnf = Cnf::new(&v); match UnitPropagate::new(cnf) { None => panic!("test failed - no partial model generated"), Some((_, assgn)) => { @@ -550,7 +550,7 @@ fn test_unit_propagate_3() { ], ]; - let cnf = Cnf::new(v); + let cnf = Cnf::new(&v); match UnitPropagate::new(cnf) { None => panic!("test failed - no partial model generated"), Some((_, assgn)) => { @@ -579,7 +579,7 @@ fn test_unit_propagate_3() { // ], // ]; -// let cnf = Cnf::new(v); +// let cnf = Cnf::new(&v); // let up = UnitPropagate::new(&cnf).unwrap(); // let assgn = up.get_assgn().get_vec(); // assert_eq!(assgn[0], Some(false)); @@ -596,7 +596,7 @@ fn test_unit_propagate_3() { // ], // ]; -// let cnf = Cnf::new(v); +// let cnf = Cnf::new(&v); // let mut up = UnitPropagate::new(&cnf).unwrap(); // let v1 = up.decide(Literal::new(VarLabel::new(0), false)); // assert!(v1); @@ -611,7 +611,7 @@ fn test_unit_propagate_3() { // fn test_unit_propagate_5() { // let v = vec![vec![Literal::new(VarLabel::new(1), true), Literal::new(VarLabel::new(3), true)], // vec![Literal::new(VarLabel::new(3), false), Literal::new(VarLabel::new(2), true), Literal::new(VarLabel::new(4), true)]]; -// let cnf = Cnf::new(v); +// let cnf = Cnf::new(&v); // let mut up = UnitPropagate::new(&cnf).unwrap(); // let v1 = up.decide(Literal::new(VarLabel::new(3), true)); // assert!(v1); diff --git a/src/util/hypergraph.rs b/src/util/hypergraph.rs index 9f6890e0..3f8faef9 100644 --- a/src/util/hypergraph.rs +++ b/src/util/hypergraph.rs @@ -275,7 +275,7 @@ mod test { ], vec![Literal::new(VarLabel::new(5), true)], ]; - let cnf = Cnf::new(v); + let cnf = Cnf::new(&v); let ig = cnf.interaction_graph(); println!("{:?}", Dot::with_config(&ig, &[Config::EdgeNoLabel])); let _nodes = ig diff --git a/tests/test.rs b/tests/test.rs index 8aa069d0..938deb9d 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -751,7 +751,7 @@ mod test_sdd_builder { /// test that the same CNF compiled by both an SDD and BDD have the same weighted model count /// with an even_split ordering fn sdd_wmc_eq_even_split(clauses: Vec>) -> TestResult { - let cnf = Cnf::new(clauses); + let cnf = Cnf::new(&clauses); if cnf.num_vars() < 8 || cnf.num_vars() > 16 { return TestResult::discard() } if cnf.clauses().len() > 16 { return TestResult::discard() } @@ -774,7 +774,7 @@ mod test_sdd_builder { /// test that the same CNF compiled by both an SDD and BDD have the same weighted model count /// with a dtree ordering fn sdd_wmc_eq(clauses: Vec>) -> TestResult { - let cnf = Cnf::new(clauses); + let cnf = Cnf::new(&clauses); if cnf.num_vars() < 8 || cnf.num_vars() > 16 { return TestResult::discard() } if cnf.clauses().len() > 16 { return TestResult::discard() }