dpss/
lib.rs

1mod dp_module;
2
3#[cfg(feature = "python")]
4mod py_module;
5
6pub use self::dp_module::*;
7
8#[cfg(feature = "wasm")]
9use wasm_bindgen::prelude::*;
10
11#[cfg(feature = "wasm")]
12#[wasm_bindgen]
13pub fn wasm_find_subset(
14    keys: String,
15    targets: String,
16    max_key_length: usize,
17    max_target_length: usize,
18    n_candidates: usize,
19    use_all_keys: bool,
20    use_all_targets: bool,
21) -> String {
22    let mut keys: Vec<i32> = keys
23        .split("\n")
24        .map(|x| x.trim().parse::<i32>().unwrap())
25        .collect();
26    if targets.contains("\n") {
27        let mut targets: Vec<i32> = targets
28            .split("\n")
29            .map(|x| x.trim().parse::<i32>().unwrap())
30            .collect();
31        let result: Vec<dp::AnswerElement> = match dp::sequence_matcher(
32            &mut keys,
33            &mut targets,
34            max_key_length,
35            max_target_length,
36            n_candidates,
37            use_all_keys,
38            use_all_targets,
39        ) {
40            Ok(res) => res,
41            Err(err) => return err,
42        };
43        if result.len() == 0 {
44            return "No solution. You might want to increase maximum length.".to_string();
45        }
46        dp::sequence_matcher_formatter(result)
47    } else {
48        let res: Vec<Vec<i32>> =
49            dp::find_subset(keys, targets.parse::<i32>().unwrap(), max_target_length);
50        let mut r3: Vec<String> = vec![];
51        if res.len() == 0 {
52            return "No solution. You might want to increase maximum subset length.".to_string();
53        }
54        for r in res {
55            let mut r2: String = r
56                .into_iter()
57                .map(|x| format!("{}, ", x.to_string()))
58                .collect::<String>();
59            r2 = format!("[{}]", r2).replace(", ]", "]");
60            r3.push(r2);
61        }
62        let s: String = r3.join(", ");
63        s
64    }
65}