Skip to content

Commit

Permalink
feat: ipasir-display feature
Browse files Browse the repository at this point in the history
enabling this feature changes the behaviour of the `Var` and `Lit`
implementations of the `Display` trait to use IPASIR indexing
  • Loading branch information
chrjabs committed Apr 29, 2024
1 parent 81c27d4 commit 28c68a1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
1 change: 1 addition & 0 deletions rustsat/Cargo.toml
Expand Up @@ -39,6 +39,7 @@ multiopt = ["optimization"]
compression = ["dep:bzip2", "dep:flate2", "dep:xz2"]
rand = ["dep:rand"]
bench = []
ipasir-display = []
all = ["multiopt", "compression", "rand", "fxhash"]

[package.metadata.docs.rs]
Expand Down
31 changes: 31 additions & 0 deletions rustsat/examples/print-lits.rs
@@ -0,0 +1,31 @@
//! Example to showcase `ipasir-display` feature

macro_rules! show_ipasir_var {
($ipasir:expr) => {{
println!(
"`Display` IPASIR literal {:3}: {:>16}",
$ipasir,
format!("{}", rustsat::ipasir_lit![$ipasir])
);
println!(
"`Display` IPASIR literal {:3}: {:>16}",
-$ipasir,
format!("{}", rustsat::ipasir_lit![-$ipasir])
);
println!(
" `Debug` IPASIR literal {:3}: {:>2?}",
$ipasir,
rustsat::ipasir_lit![$ipasir]
);
println!(
" `Debug` IPASIR literal {:3}: {:>2?}",
-$ipasir,
rustsat::ipasir_lit![-$ipasir]
);
}};
}

fn main() {
show_ipasir_var![1];
show_ipasir_var![42];
}
7 changes: 4 additions & 3 deletions rustsat/src/lib.rs
Expand Up @@ -47,13 +47,14 @@
//!
//! | Feature name | Description |
//! | --- | --- |
//! | `internals` | Make some internal data structures for e.g. encodings public. This is useful when basing a more complex encoding on the `rustsat` implementation of another encoding. Note that the internal API might change between releases. |
//! | `fxhash` | Use the faster firefox hash function from `rustc-hash` in `rustsat`. |
//! | `optimization` | Include optimization (MaxSAT) data structures etc. |
//! | `multiopt` | Include data structures etc. for multi-objective optimization. |
//! | `compression` | Enable parsing and writing compressed input. |
//! | `bench` | Enable benchmark tests. Behind feature flag since it requires unstable Rust. |
//! | `fxhash` | Use the faster firefox hash function from `rustc-hash` in `rustsat`. |
//! | `rand` | Enable randomization features. (Shuffling clauses etc.) |
//! | `ipasir-display` | Changes `Display` trait for `Lit` and `Var` types to follow IPASIR variables indexing. |
//! | `bench` | Enable benchmark tests. Behind feature flag since it requires unstable Rust. |
//! | `internals` | Make some internal data structures for e.g. encodings public. This is useful when basing a more complex encoding on the `rustsat` implementation of another encoding. Note that the internal API might change between releases. |
//!
//! ## Examples
//!
Expand Down
11 changes: 6 additions & 5 deletions rustsat/src/types.rs
Expand Up @@ -204,7 +204,11 @@ impl ops::SubAssign<u32> for Var {
/// Variables can be printed with the [`Display`](std::fmt::Display) trait
impl fmt::Display for Var {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "x{}", self.idx)
if cfg!(feature = "ipasir-display") {
write!(f, "x{}", self.to_ipasir())
} else {
write!(f, "x{}", self.idx())
}
}
}

Expand Down Expand Up @@ -460,10 +464,7 @@ impl ops::Sub<u32> for Lit {
/// Literals can be printed with the [`Display`](std::fmt::Display) trait
impl fmt::Display for Lit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.is_neg() {
true => write!(f, "~x{}", self.vidx()),
false => write!(f, "x{}", self.vidx()),
}
write!(f, "{}{}", if self.is_neg() { "~" } else { "" }, self.var())
}
}

Expand Down

0 comments on commit 28c68a1

Please sign in to comment.