Commit 5f23e9c5 authored by Ranadeep Biswas's avatar Ranadeep Biswas
Browse files

using vector instead of string

parent 1c5e427f
use hashbrown::{HashMap, HashSet}; use hashbrown::{HashMap, HashSet};
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
use std::default::Default;
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
use std::path::PathBuf; use std::path::PathBuf;
...@@ -17,26 +19,29 @@ pub enum Edge { ...@@ -17,26 +19,29 @@ pub enum Edge {
WW(usize), WW(usize),
} }
#[derive(Default, Debug)] #[derive(Debug)]
struct CNF { struct CNF {
cnf_string: Vec<u8>, clauses: Vec<Vec<(bool, usize)>>,
n_clause: usize,
n_variable: usize, n_variable: usize,
} }
impl Default for CNF {
fn default() -> Self {
CNF {
clauses: vec![Vec::new()],
n_variable: 0,
}
}
}
impl CNF { impl CNF {
fn add_variable(&mut self, var: usize, sign: bool) { fn add_variable(&mut self, var: usize, sign: bool) {
self.n_variable = std::cmp::max(self.n_variable, var); self.n_variable = std::cmp::max(self.n_variable, var);
if sign { self.clauses.last_mut().unwrap().push((sign, var));
write!(self.cnf_string, "{} ", var).expect("cnf write failed");
} else {
write!(self.cnf_string, "-{} ", var).expect("cnf write failed");
}
} }
fn finish_clause(&mut self) { fn finish_clause(&mut self) {
writeln!(self.cnf_string, " 0").expect("cnf write failed"); self.clauses.push(Vec::new());
self.n_clause += 1;
} }
fn write_to_file(&self, path: &PathBuf) { fn write_to_file(&self, path: &PathBuf) {
...@@ -47,10 +52,18 @@ impl CNF { ...@@ -47,10 +52,18 @@ impl CNF {
.open(path) .open(path)
.expect("couldn't create"); .expect("couldn't create");
writeln!(file, "p cnf {} {}", self.n_variable, self.n_clause) writeln!(file, "p cnf {} {}", self.n_variable, self.clauses.len())
.expect("failed to write parameters"); .expect("failed to write parameters");
file.write_all(&self.cnf_string) for clause in self.clauses.iter().rev().skip(1) {
.expect("failed to write clauses"); for (sign, literal) in clause {
if *sign {
write!(file, "{} ", literal).expect("failed to write cnf to file");
} else {
write!(file, "-{} ", literal).expect("failed to write cnf to file");
}
}
writeln!(file, "0").expect("failed to write cnf to file");
}
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment