public ITruthTableRow Simplify(ITruthTableRow otherRow) { // Instead of creating a memory intense copy, we create a very simple row object // that will only hold the truth values in each cell and a result. int numberOfVariables = Cells.Length; TruthTableRow simplifiedRow = new TruthTableRow(uniqueVariables, numberOfVariables); simplifiedRow.Result = Result; int numberOfDeferringValues = 0; for (int i = 0; i < Cells.Length; i++) { if (Cells[i] != otherRow.Cells[i]) { // Then we deal with a don't care variable, which we indicate by null. simplifiedRow.Cells[i] = '*'; numberOfDeferringValues++; if (numberOfDeferringValues > 1) { return(null); } } else { simplifiedRow.Cells[i] = Cells[i]; } } return(simplifiedRow); }
private List <ITruthTableRow> SimplifiyRowSet(List <ITruthTableRow> rowSet) { List <ITruthTableRow> simplifiedSet = new List <ITruthTableRow>(); for (int i = 0; i < rowSet.Count; i++) { for (int j = 0; j < rowSet.Count; j++) { // Skip comparing the same row. if (j != i) { // Only attempt to simplify the row if they have the same result value. if (rowSet[i].Result == rowSet[j].Result) { // Try to simplify the two by a method call. // The method will return null if it was not succesfull // or a new row if it was successful. ITruthTableRow simplifiedRow = rowSet[i].Simplify(rowSet[j]); if (simplifiedRow != null) { // Only assign is simplified to row i since that's the one under inspection. rowSet[i].IsSimplified = true; // Here we ensure that we only return unique rows, // that means rows with all different symbols in the cells. if (IsRowInSet(simplifiedRow, simplifiedSet) == false) { simplifiedSet.Add(simplifiedRow); } } } } } // We exhausted our option, meaning that row i was either simplified or not simplified, // thus we have to add it to our set, ensuring one unique row is in it. if (rowSet[i].IsSimplified == false) { if (IsRowInSet(rowSet[i], simplifiedSet) == false) { simplifiedSet.Add(rowSet[i]); } } } // If the set could not be simplified any further we can rewturn the original. if (simplifiedSet.Count == 0) { return(rowSet); } // Else return the set of simplified rows. return(simplifiedSet); }
private bool IsRowInSet(ITruthTableRow row, List <ITruthTableRow> rowSet) { foreach (ITruthTableRow r in rowSet) { if (row.EqualTo(r)) { return(true); } } return(false); }
public bool EqualTo(ITruthTableRow other) { if (Cells.Length != other.Cells.Length) { return(false); } for (int i = 0; i < Cells.Length; i++) { if (Cells[i] != other.Cells[i]) { return(false); } } return(true); }