From 9b6a24e222078545afc2b9d7c03cba869d51797d Mon Sep 17 00:00:00 2001 From: Martin Berg Alstad Date: Thu, 4 Jul 2024 13:05:52 +0200 Subject: [PATCH] Merged a few branches in absorption law together --- src/expressions/operator.rs | 8 +++++++ src/expressions/simplify.rs | 42 +++++++------------------------------ 2 files changed, 15 insertions(+), 35 deletions(-) diff --git a/src/expressions/operator.rs b/src/expressions/operator.rs index 159a3a1..a09018a 100644 --- a/src/expressions/operator.rs +++ b/src/expressions/operator.rs @@ -16,4 +16,12 @@ impl BinaryOperator { BinaryOperator::Implication => !left || right, } } + + pub fn is_and(&self) -> bool { + matches!(self, BinaryOperator::And) + } + + pub fn is_or(&self) -> bool { + matches!(self, BinaryOperator::Or) + } } diff --git a/src/expressions/simplify.rs b/src/expressions/simplify.rs index cb9b2dc..f57fd92 100644 --- a/src/expressions/simplify.rs +++ b/src/expressions/simplify.rs @@ -124,6 +124,7 @@ impl From for Options { } } +// TODO refactor, remove unnecessary code and split up into smaller functions impl Expression { // TODO better track of operations pub fn simplify(&self, options: Options) -> (Self, Vec) { @@ -228,22 +229,22 @@ impl Expression { } left.absorption_law(operations, ignore_case) } - Expression::Binary { left, operator: BinaryOperator::And, right } - if left.is_in(right) && right.is_or() => { + Expression::Binary { left, operator, right } + if operator.is_and() && (right.is_in(left) && left.is_and() || left.is_in(right) && right.is_or()) => { if let Some(operation) = Operation::new(self, left, Law::AbsorptionLaw) { operations.push(operation); } left.absorption_law(operations, ignore_case) } - Expression::Binary { left, operator: BinaryOperator::And, right } - if right.is_in(left) && left.is_or() => { + Expression::Binary { left, operator, right } + if operator.is_and() && (right.is_in(left) && left.is_or() || left.is_in(right) && right.is_and()) => { if let Some(operation) = Operation::new(self, right, Law::AbsorptionLaw) { operations.push(operation); } right.absorption_law(operations, ignore_case) } - Expression::Binary { left, operator: BinaryOperator::Or, right } - if right.is_in(left) && left.is_and() => { + Expression::Binary { left, operator, right } + if operator.is_or() && (right.is_in(left) && (left.is_and() || left.is_or()) || left.is_in(right) && right.is_or()) => { if let Some(operation) = Operation::new(self, right, Law::AbsorptionLaw) { operations.push(operation); } @@ -256,35 +257,6 @@ impl Expression { } left.absorption_law(operations, ignore_case) } - Expression::Binary { left, operator: BinaryOperator::Or, right } - if left.is_in(right) && right.is_or() => { - if let Some(operation) = Operation::new(self, left, Law::AbsorptionLaw) { - operations.push(operation); - } - right.absorption_law(operations, ignore_case) - } - Expression::Binary { left, operator: BinaryOperator::Or, right } - if right.is_in(left) && left.is_or() => { - if let Some(operation) = Operation::new(self, right, Law::AbsorptionLaw) { - operations.push(operation); - } - right.absorption_law(operations, ignore_case) - } - // TODO remove And from pattern and call matches instead of .is_and() - Expression::Binary { left, operator: BinaryOperator::And, right } - if left.is_in(right) && right.is_and() => { - if let Some(operation) = Operation::new(self, right, Law::AbsorptionLaw) { - operations.push(operation); - } - right.absorption_law(operations, ignore_case) - } - Expression::Binary { left, operator: BinaryOperator::And, right } - if right.is_in(left) && left.is_and() => { - if let Some(operation) = Operation::new(self, left, Law::AbsorptionLaw) { - operations.push(operation); - } - left.absorption_law(operations, ignore_case) - } Expression::Binary { left, operator, right } => binary( left.absorption_law(operations, ignore_case), *operator,