Skip to content

Commit

Permalink
Fix Cnf::new() to borrow top-level slice instead of vector (#167)
Browse files Browse the repository at this point in the history
Cherry-picking this change over from #163, to make it easier to review (and to revert this if necessary).
  • Loading branch information
mattxwang authored Aug 3, 2023
1 parent af21705 commit 36ce136
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
14 changes: 7 additions & 7 deletions src/repr/cnf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ impl Iterator for AssignmentIter {
}

impl Cnf {
pub fn new(clauses: Vec<Vec<Literal>>) -> Cnf {
pub fn new(clauses: &[Vec<Literal>]) -> Cnf {
let clauses: Vec<Vec<Literal>> = clauses
.iter()
.filter(|clause| !clause.is_empty())
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -580,7 +580,7 @@ impl Cnf {
}
})
.collect();
Cnf::new(new_cnf)
Cnf::new(&new_cnf)
}

pub fn interaction_graph(&self) -> UnGraph<VarLabel, ()> {
Expand Down Expand Up @@ -680,7 +680,7 @@ impl Arbitrary for Cnf {
}
clauses.push(clause);
}
Cnf::new(clauses)
Cnf::new(&clauses)
}
}

Expand Down Expand Up @@ -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,
(
Expand Down
12 changes: 6 additions & 6 deletions src/repr/unit_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) => {
Expand All @@ -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)) => {
Expand Down Expand Up @@ -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)) => {
Expand Down Expand Up @@ -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));
Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/util/hypergraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<Literal>>) -> 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() }

Expand All @@ -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<Vec<Literal>>) -> 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() }

Expand Down

0 comments on commit 36ce136

Please sign in to comment.