public static BooleanExpression FindMininalExpressionForBinary(BooleanExpression targetBinary, BooleanFunction f, List <BooleanExpression> availableExpressions) { var queue = new ImprovisedPriorityQueue <BooleanExpression>(20); var knownTruthTables = new HashSet <byte>(); var knownExpressions = new HashSet <BooleanExpression>(); foreach (var expression in availableExpressions) { queue.TryEnqueue(expression, 1); } while (queue.Count != 0) { var curExperession = queue.Dequeue(); byte truthTable = curExperession.Eval(); if (knownTruthTables.Contains(truthTable)) { continue; } if ((curExperession.Eval() & 0xAA) == (targetBinary.Eval() & 0xAA)) { return(curExperession); } knownExpressions.Add(curExperession); knownTruthTables.Add(truthTable); foreach (var anotherExpression in knownExpressions) { foreach (var thirdExpression in knownExpressions) { foreach (var neighbourExpression in CombineTertiary(f, curExperession, anotherExpression, thirdExpression)) { queue.TryEnqueue(neighbourExpression, neighbourExpression.CountOps()); } } } } throw new CouldntFindExpressionException(); }
public override byte Eval() { byte aVal = Left.Eval(); byte bVal = Right.Eval(); int res; switch (Op) { case BooleanOperation.Zero: res = 0x00; break; case BooleanOperation.NOR: res = ~(aVal | bVal); break; case BooleanOperation.CoImp: res = aVal & ~bVal; break; case BooleanOperation.NotA: res = ~aVal; break; case BooleanOperation.BCoImp: res = ~aVal & bVal; break; case BooleanOperation.NotB: res = ~bVal; break; case BooleanOperation.Xor: res = aVal ^ bVal; break; case BooleanOperation.NAND: res = ~(aVal & bVal); break; case BooleanOperation.And: res = aVal & bVal; break; case BooleanOperation.Eq: res = aVal ^ (~bVal); break; case BooleanOperation.B: res = bVal; break; case BooleanOperation.BImp: res = aVal | ~bVal; break; case BooleanOperation.A: res = aVal; break; case BooleanOperation.Imp: res = ~aVal | bVal; break; case BooleanOperation.Or: res = aVal | bVal; break; case BooleanOperation.One: res = 0xFF; break; default: throw new ArgumentException(); } return((byte)res); }
public static BooleanExpression FindMininalExpressionForBinary(BooleanExpression targetBinary, BooleanFunction f, List<BooleanExpression> availableExpressions) { var queue = new ImprovisedPriorityQueue<BooleanExpression>(20); var knownTruthTables = new HashSet<byte>(); var knownExpressions = new HashSet<BooleanExpression>(); foreach(var expression in availableExpressions) { queue.TryEnqueue(expression, 1); } while(queue.Count != 0) { var curExperession = queue.Dequeue(); byte truthTable = curExperession.Eval(); if(knownTruthTables.Contains(truthTable)) { continue; } if((curExperession.Eval() & 0xAA) == (targetBinary.Eval() & 0xAA)) { return curExperession; } knownExpressions.Add(curExperession); knownTruthTables.Add(truthTable); foreach(var anotherExpression in knownExpressions) { foreach(var thirdExpression in knownExpressions) { foreach(var neighbourExpression in CombineTertiary(f, curExperession, anotherExpression, thirdExpression)) { queue.TryEnqueue(neighbourExpression, neighbourExpression.CountOps()); } } } } throw new CouldntFindExpressionException(); }