Skip to content

Commit

Permalink
move varsize
Browse files Browse the repository at this point in the history
  • Loading branch information
Anatoly03 committed Jan 3, 2024
1 parent 30f732c commit 7f5a0e0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 60 deletions.
30 changes: 30 additions & 0 deletions src/algs/flatten.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::sat::{KnfSat, Variable};

/**
* **Algorithm**: Rename Variables to fill [0..n]
* **Complexity**: Polynomial `O(n^2)`
*/
pub fn flatten(mut sat: KnfSat) -> KnfSat {
let mut reference = vec![];

for clausel in &mut sat.eq {
for literal in clausel {
let pos = reference.iter().position(|&r| r == literal.abs());

let new_val: Variable = if let Some(p) = pos {
p as Variable + 1
} else {
reference.push((literal).abs());
reference.len() as Variable
};

if literal > &mut 0 {
*literal = new_val;
} else {
*literal = -new_val;
}
}
}

sat
}
2 changes: 2 additions & 0 deletions src/algs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub mod bf0;
pub mod flatten;
pub mod knf3;
pub mod varsize;

// pub fn get_algorithm<T: SatSolver>(sat: KnfSat, alg: u8) -> T {
// match alg {
Expand Down
23 changes: 23 additions & 0 deletions src/algs/varsize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::sat::KnfSat;

impl KnfSat {
/**
* **Algorithm**: Count Variables in Equation
* **Complexity**: Polynomial
*/
pub fn varsize(sat: &KnfSat) -> usize {
let mut reference = vec![];

for clausel in &sat.eq {
for literal in clausel {
let pos = reference.iter().position(|&r| r == literal.abs());

if let None = pos {
reference.push((literal).abs());
};
}
}

reference.len()
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn main() {
}

if let Ok(mut sat) = knf_sat {
sat.flatten();
// sat.flatten();
// TODO
// let mut algorithm = algs::get_algorithm(sat, version);
// algorithm.optimise();
Expand Down
57 changes: 0 additions & 57 deletions src/sat/knf_sat.rs

This file was deleted.

11 changes: 9 additions & 2 deletions src/sat/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
pub mod knf_sat;

/**
* A Variable is denoted as a non-zero number with negatives implying negations of the variable.
*/
Expand Down Expand Up @@ -32,3 +30,12 @@ pub type SolveSat = dyn Fn(KnfSat) -> Vec<Variable>;
* SatSolver
*/
pub type SolveAllSat = dyn Fn(KnfSat) -> Vec<Vec<Variable>>;

/**
* Implementations
*/
impl KnfSat {
pub fn new() -> Self {
Self { eq: vec![vec![]] }
}
}

0 comments on commit 7f5a0e0

Please sign in to comment.