@@ -265,7 +265,6 @@ impl Cnf {
265265 pub fn new ( clauses : & [ Vec < Literal > ] ) -> Cnf {
266266 let clauses: Vec < Vec < Literal > > = clauses
267267 . iter ( )
268- . filter ( |clause| !clause. is_empty ( ) )
269268 . map ( |clause| {
270269 let mut clause = clause. clone ( ) ;
271270 clause. sort_by_key ( |a| a. label ( ) . value ( ) ) ;
@@ -552,30 +551,24 @@ impl Cnf {
552551 }
553552
554553 /// Updates the CNF to a new CNF that results from conditioning on the supplied literal
555- pub fn condition ( & mut self , lit : Literal ) -> Cnf {
556- let new_cnf: Vec < Vec < Literal > > = self
557- . clauses ( )
558- . iter ( )
559- . filter_map ( |clause| {
560- // first, check if there is a true literal -- if there is, filter out this clause
561- if clause
562- . iter ( )
563- . any ( |outer| outer . label ( ) == lit. label ( ) && outer . polarity ( ) == lit. polarity ( ) )
564- {
565- None
554+ pub fn condition ( & self , lit : Literal ) -> Cnf {
555+ let mut new_cnf : Vec < Vec < Literal > > = Vec :: new ( ) ;
556+ ' cnf : for clause in self . clauses . iter ( ) {
557+ let mut new_clause = Vec :: new ( ) ;
558+ ' clause : for l in clause . iter ( ) {
559+ if l . label ( ) == lit . label ( ) && l . polarity ( ) == lit . polarity ( ) {
560+ // skip over this whole clause
561+ continue ' cnf
562+ } else if l . label ( ) == lit. label ( ) && l . polarity ( ) != lit. polarity ( ) {
563+ // skip over this literal
564+ continue ' clause
566565 } else {
567- // next, filter out clauses with false literals
568- let filtered: Vec < Literal > = clause
569- . iter ( )
570- . filter ( |outer| {
571- !( lit. label ( ) == outer. label ( ) && lit. polarity ( ) != outer. polarity ( ) )
572- } )
573- . copied ( )
574- . collect ( ) ;
575- Some ( filtered)
566+ // push the literal
567+ new_clause. push ( * l) ;
576568 }
577- } )
578- . collect ( ) ;
569+ }
570+ new_cnf. push ( new_clause) ;
571+ }
579572 Cnf :: new ( & new_cnf)
580573 }
581574
@@ -730,3 +723,12 @@ fn test_cnf_wmc() {
730723 ] ) ;
731724 assert_eq ! ( cnf. wmc( & WmcParams :: new( weights) ) , FiniteField :: new( 3 ) ) ;
732725}
726+
727+ #[ test]
728+ fn test_cond ( ) {
729+ let v = vec ! [ vec![
730+ Literal :: new( VarLabel :: new( 0 ) , false ) ,
731+ ] ] ;
732+ let cnf = Cnf :: new ( & v) ;
733+ println ! ( "{:?}" , cnf. condition( Literal :: new( VarLabel :: new( 0 ) , true ) ) ) ;
734+ }
0 commit comments