Skip to content

Commit

Permalink
Merge pull request #44 from JayXon/matcher
Browse files Browse the repository at this point in the history
Add match_final
  • Loading branch information
JayXon authored Feb 6, 2024
2 parents 5b4f38c + d54b9a2 commit 702da33
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn is_leaf_expr(op_idx: OpIndex, length: usize) -> bool {
fn save(level: &mut CacheLevel, expr: Expr, n: usize, cache: &Cache, hashset_cache: &HashSetCache) {
const ALL_MASK: Mask = (1 << INPUTS.len()) - 1;

if (!USE_ALL_VARS || expr.var_mask == ALL_MASK) && Matcher::new().match_all(&expr) {
if (!USE_ALL_VARS || expr.var_mask == ALL_MASK) && Matcher::match_all(&expr) {
println!("{expr}");
return;
}
Expand Down Expand Up @@ -133,8 +133,9 @@ fn find_binary_operators(
Some(o) => matcher.match_one(i, o),
None => false,
})
&& matcher.match_final(Some(el), er, op_idx)
{
println!("{}{}{}", el, op_idx, er);
println!("{el}{op_idx}{er}");
}
} else if let Some(output) = op.vec_apply(el.output.clone(), &er.output) {
save(
Expand Down Expand Up @@ -206,8 +207,9 @@ fn find_unary_operators(
.iter()
.enumerate()
.all(|(i, &or)| matcher.match_one(i, (op.apply)(or)))
&& matcher.match_final(None, er, op_idx)
{
println!("{}{}", op_idx, er);
println!("{op_idx}{er}");
}
} else {
save(
Expand Down
19 changes: 15 additions & 4 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,30 @@ pub const INPUTS: &[Input] = &[Input {
pub struct Matcher {}

impl Matcher {
pub fn new() -> Matcher {
Matcher {}
pub fn new() -> Self {
Self {}
}

pub fn match_one(&mut self, index: usize, output: Num) -> bool {
output == GOAL[index]
}

pub fn match_all(&mut self, expr: &Expr) -> bool {
// Will be called after match_one returns true for all outputs
pub fn match_final(self, _el: Option<&Expr>, _er: &Expr, _op: OpIndex) -> bool {
true
}

pub fn match_all(expr: &Expr) -> bool {
let mut matcher = Self::new();
expr.output
.iter()
.enumerate()
.all(|(i, &o)| self.match_one(i, o))
.all(|(i, &o)| matcher.match_one(i, o))
&& matcher.match_final(
expr.left.map(|e| unsafe { e.as_ref() }),
unsafe { expr.right.unwrap().as_ref() },
expr.op_idx,
)
}
}

Expand Down

0 comments on commit 702da33

Please sign in to comment.