Browse Source

Added convenience macros

clause_generator
Thomas Johnson 3 years ago
parent
commit
4e6ac7dee9
  1. 103
      src/clause.rs
  2. 4
      src/main.rs

103
src/clause.rs

@ -286,16 +286,64 @@ impl core::ops::IndexMut<usize> for ClauseList {
}
}
macro_rules! vars {
[] => {};
[$varname:ident$(,)?] => {
let $varname = Variable::from_name(stringify!($varname).into());
};
[$varname:ident, $($varnames:ident),+] => {
let $varname = Variable::from_name(stringify!($varname).into());
vars![$($varnames),+]
};
}
macro_rules! assign_vars {
{ } => {};
{$var:ident = 1; $($vars:ident$( = $vals:tt)?;)*} =>
{
$var.assign(true);
assign_vars! { $($vars$( = $vals)?;)* } //$($vars = $vals;)* }
};
{$var:ident = 0; $($vars:ident$( = $vals:tt)?;)*} =>
{
$var.assign(false);
assign_vars! { $($vars$( = $vals)?;)* }
};
{$var:ident; $($vars:ident$( = $vals:tt)?;)*} =>
{
$var.unassign();
assign_vars! { $($vars$( = $vals)?;)* }
};
}
macro_rules! add_terms {
($varname:ident) => {};
($varname:ident - $literal:ident $(| $(-)?$rest:ident)* ) => {
$varname.push($literal.get_neg_literal());
add_terms!($varname $($rest)|*)
};
($varname:ident $literal:ident $(| $(-)?$rest:ident)* ) => {
$varname.push($literal.get_pos_literal());
add_terms!($varname $($rest)|*)
};
}
macro_rules! make_clause {
{ } => { Clause::new() };
( $($contents:tt)* ) => {{
let mut clause = Clause::new();
add_terms!(clause $($contents)*);
clause
}};
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn clause_test() {
let x = Variable::from_name("x".into());
let y = Variable::from_name("y".into());
let z = Variable::from_name("z".into());
let w = Variable::from_name("w".into());
vars![x, y, z, w];
println!(
"names: {} {} {} {}",
x.get_name(),
@ -304,9 +352,11 @@ mod test {
w.get_name()
);
println!("x: {}, y: {}, z: {}, w: {}", x, y, z, w);
x.assign(true);
y.assign(false);
z.unassign();
assign_vars! {
x = 1;
y = 0;
z;
}
println!("x: {}, y: {}, z: {}, w: {}", x, y, z, w);
assert_eq!(x.get_assignment(), Some(true));
assert_eq!(y.get_assignment(), Some(false));
@ -321,10 +371,12 @@ mod test {
assert_eq!(yl.get_assignment(), Some(true));
assert_eq!(zl.get_assignment(), None);
assert_eq!(wl.get_assignment(), None);
xl.assign(false);
yl.assign(true);
zl.assign(true);
wl.assign(false);
assign_vars! {
xl = 0;
yl = 1;
zl = 1;
wl = 0;
}
println!("assigned literals");
println!("x: {}, y: {}, z: {}, w: {}", x, y, z, w);
assert_eq!(x.get_assignment(), Some(false));
@ -336,10 +388,12 @@ mod test {
assert_eq!(yl.get_assignment(), Some(true));
assert_eq!(zl.get_assignment(), Some(true));
assert_eq!(wl.get_assignment(), Some(false));
xl.unassign();
yl.unassign();
zl.unassign();
wl.unassign();
assign_vars! {
xl;
yl;
zl;
wl;
}
println!("unassigned literals");
println!("x: {}, y: {}, z: {}, w: {}", x, y, z, w);
assert_eq!(x.get_assignment(), None);
@ -360,11 +414,11 @@ mod test {
println!("clause: {}", clause);
println!("value: {:?}", clause.eval());
assert_eq!(clause.eval(), None);
x.assign(true);
assign_vars! { x = 1; }
println!("clause: {}", clause);
println!("value: {:?}", clause.eval());
assert_eq!(clause.eval(), Some(true));
x.assign(false);
assign_vars! { x = 0; }
println!("clause: {}", clause);
println!("value: {:?}", clause.eval());
assert_eq!(clause.eval(), Some(false));
@ -372,7 +426,7 @@ mod test {
println!("clause: {}", clause);
println!("value: {:?}", clause.eval());
assert_eq!(clause.eval(), None);
y.assign(true);
assign_vars! { y = 1; }
println!("clause: {}", clause);
println!("value: {:?}", clause.eval());
assert_eq!(clause.eval(), Some(false));
@ -381,25 +435,20 @@ mod test {
println!("clause: {}", clause);
println!("value: {:?}", clause.eval());
assert_eq!(clause.eval(), None);
z.assign(false);
w.assign(true);
assign_vars! { z = 0; w = 1; }
println!("clause: {}", clause);
println!("value: {:?}", clause.eval());
assert_eq!(clause.eval(), Some(false));
x.assign(true);
assign_vars! { x = 1; }
println!("clause: {}", clause);
println!("value: {:?}", clause.eval());
assert_eq!(clause.eval(), Some(true));
y.assign(false);
assign_vars! { y = 0; }
println!("clause: {}", clause);
println!("value: {:?}", clause.eval());
assert_eq!(clause.eval(), Some(true));
let mut clause2 = Clause::new();
clause2.push(x.get_neg_literal());
clause2.push(y.get_neg_literal());
clause2.push(z.get_pos_literal());
clause2.push(w.get_pos_literal());
let clause2 = make_clause!(-x | -y | z | w);
let mut clauselist = ClauseList::new();
clauselist.push(clause);
clauselist.push(clause2);

4
src/main.rs

@ -1,5 +1,9 @@
// vim: ts=4 sw=4 et
#![feature(trace_macros)]
#![feature(log_syntax)]
#[macro_use]
pub mod clause;
pub mod hash_wrapper;

Loading…
Cancel
Save