From 8dff087377d736c1f7eba5db602693b9e7c174a4 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Fri, 19 Mar 2021 18:14:41 +0100 Subject: [PATCH] DMF/KMF switch --- Cargo.toml | 2 +- index.html | 8 ++++++-- src/bin/svg-wasm.rs | 36 +++++++++++++++++++++--------------- src/lib.rs | 30 +++++++++++++++++++++++++++++- 4 files changed, 57 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 72a7917..9deafec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,4 +16,4 @@ itertools = "0.10.0" log = "0.4.14" svg_fmt = { path = "../rust_debug/svg_fmt" } wasm-bindgen = "0.2.71" -web-sys = { version = "0.3.48", features = ["Window", "Document", "Location", "HtmlCollection", "HtmlElement", "CssStyleDeclaration", "HtmlTextAreaElement", "HtmlInputElement"] } +web-sys = { version = "0.3.48", features = ["Window", "Document", "Location", "HtmlCollection", "HtmlElement", "HtmlSelectElement", "CssStyleDeclaration", "HtmlTextAreaElement", "HtmlInputElement"] } diff --git a/index.html b/index.html index 9620139..4a1e2c4 100644 --- a/index.html +++ b/index.html @@ -32,15 +32,19 @@
- +

- Change cells by scrolling (up => 1, down => 0), typing (=> 1, 0) or deleting (=> -). + Change output specification by scrolling (up => 1, down => 0), typing (=> 1, 0) or deleting (=> -).

Load example 1, example 2, example 3.

+
diff --git a/src/bin/svg-wasm.rs b/src/bin/svg-wasm.rs index f1e06f4..e6feb39 100644 --- a/src/bin/svg-wasm.rs +++ b/src/bin/svg-wasm.rs @@ -3,7 +3,7 @@ use wasm_bindgen::JsCast; use log::{Level, info}; use kv::*; -use web_sys::{EventTarget, HtmlElement, HtmlInputElement, HtmlTextAreaElement}; +use web_sys::{EventTarget, HtmlElement, HtmlInputElement, HtmlSelectElement, HtmlTextAreaElement}; macro_rules! web { ($($x:ident)*) => { @@ -77,15 +77,16 @@ fn span(text: &str) -> HtmlElement { pub fn run() { let document = web!(document); + let mode = get_mode(); let var_count = get_var_number(); let vars = (0..var_count).map(|x| 2usize.pow(x as u32)).collect::>(); let function: FunctionSpec = (0..2usize.pow(var_count as u32)).map(|idx| get_input(idx)).collect(); info!("function: {:?}", function); let hash = Some('#').into_iter().chain(function.iter().map::(|&x| x.into())).collect::(); web2!(location).set_hash(&hash).unwrap(); - let groups = find_groups(&function, &vars, One); - let (prime, other) = find_core(&function, &vars, One, &groups); - let solutions = real_solutions_idxed(function.clone(), &vars, One, &prime, &other); + let groups = find_groups(&function, &vars, mode); + let (prime, other) = find_core(&function, &vars, mode, &groups); + let solutions = real_solutions_idxed(function.clone(), &vars, mode, &prime, &other); let output_container = get_output_container(); if output_container.children().length() == 0 { @@ -106,13 +107,7 @@ pub fn run() { let svg = svg_container.children().item(0).unwrap().unchecked_into::(); svg.style().set_property("width", &format!("{}px", w * 80)).unwrap(); svg.style().set_property("height", &format!("{}px", h * 80)).unwrap(); - let mut text = String::new(); - for x in $sol { - if !text.is_empty() { - text += " ∨ "; - } - text += &print_implicant(x); - } + let mut text = if mode == Output::One { print_df($sol) } else { print_mf($sol) }; if text.is_empty() { text += "."; } @@ -124,13 +119,13 @@ pub fn run() { let mut block_masks: Vec<_> = prime.iter() .map(block_to_mask) .collect(); - render_svg!(&prime, &block_masks); + render_svg!(prime.iter().map(|x| *x), &block_masks); block_masks = prime.iter() .map(block_to_mask) .chain(other.iter().map(block_to_mask)) // also display non-core .collect(); - render_svg!(prime.iter().chain(other.iter()), &block_masks); + render_svg!(prime.iter().chain(other.iter()).map(|x| *x), &block_masks); let mut size = usize::MAX; for sol in &solutions { @@ -148,8 +143,8 @@ pub fn run() { } })) .collect(); - let blocks = prime.iter() - .chain(sol.iter().map(|&i| &other[i])); + let blocks = prime.iter().map(|x| *x) + .chain(sol.iter().map(|&i| other[i])); render_svg!(blocks, &block_masks); } @@ -180,6 +175,17 @@ fn get_var_number() -> usize { get_var_number_el().value().parse().unwrap() } +fn get_mode_el() -> HtmlSelectElement { + web!(document).get_element_by_id("mode-select").unwrap().unchecked_into() +} + +fn get_mode() -> Output { + match &*get_mode_el().value() { + "kmf" => Output::Zero, + "dmf" | _ => Output::One, + } +} + fn get_input_container() -> HtmlElement { web!(document).get_element_by_id("input-container").unwrap().unchecked_into::() } diff --git a/src/lib.rs b/src/lib.rs index 700b174..e906501 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -289,6 +289,21 @@ pub fn block_to_mask>(block: T) -> (Variable, Var pub fn check_mask(input: usize, mask: usize, inv_mask: usize) -> bool { input | mask == input && input & inv_mask == input } +pub fn print_mf<'a, I>(x: I) -> String +where I: IntoIterator +{ + let mut text = String::new(); + for x in x { + if !text.is_empty() { + text += ") ∧ ("; + } else { + text.push('('); + } + text += &print_implicate(x); + } + text.push(')'); + text +} pub fn print_implicate(vars: &[(Variable, Output)]) -> String { let mut s = String::new(); @@ -299,12 +314,25 @@ pub fn print_implicate(vars: &[(Variable, Output)]) -> String { s += "̅"; } if i != vars.len() - 1 { - s += " v "; + s += " ∨ "; } } s } +pub fn print_df<'a, I>(x: I) -> String +where I: IntoIterator +{ + let mut text = String::new(); + for x in x { + if !text.is_empty() { + text += " ∨ "; + } + text += &print_implicant(x); + } + text +} + pub fn print_implicant(vars: &[(Variable, Output)]) -> String { let mut s = String::new(); for i in 0..vars.len() {