public bool[] Invoke(params bool[] inputs) { bool?[] ands = new bool?[AndMatrix.GetLength(1)]; // nullable due to buggy init with true bool[] ors = new bool[OrMatrix.GetLength(0)]; if (inputs.Length != AndMatrix.GetLength(0)) { throw new System.ArgumentOutOfRangeException(); } // calculate AND Elements, iterate through for (int a = 0; a < ands.Length; a++) { for (int i = 0; i < inputs.Length; i++) { if (AndMatrix[i, a] == ePROMSet.BothSet) { ands[a] = false; // a&-a = alwaysfalse } else if (AndMatrix[i, a] == ePROMSet.PositiveSet) { ands[a] = ands[a].HasValue ? ands[a] & inputs[i] : inputs[i]; } else if (AndMatrix[i, a] == ePROMSet.NegativeSet) { ands[a] = ands[a].HasValue ? ands[a] & !inputs[i] : !inputs[i]; } if (ands[a].HasValue && !ands[a].Value) { break; // performance } } if (!ands[a].HasValue) { ands[a] = false; // wasn't processed at all, so set to false } } // calculate or/outputs with AND inputs for (int o = 0; o < ors.Length; o++) { ors[o] = false; for (int a = 0; a < ands.Length; a++) { if (OrMatrix[o, a]) { ors[o] |= ands[a].Value; } if (ors[o]) { break; // performance } } } return(ors); }
public override string ToString() { string headlenHalf = new string('=', AndMatrix.GetLength(1)); var sb = new System.Text.StringBuilder(headlenHalf + "AND" + headlenHalf + "\n"); for (int i = 0; i < AndMatrix.GetLength(0); i++) { for (int a = 0; a < AndMatrix.GetLength(1); a++) { switch (AndMatrix[i, a]) { case ePROMSet.PositiveSet: sb.Append("+ "); break; case ePROMSet.NegativeSet: sb.Append("- "); break; case ePROMSet.BothSet: sb.Append("# "); break; default: sb.Append(" "); break; } } sb.AppendLine(); } sb.AppendLine(headlenHalf + "OR" + headlenHalf + "\n"); for (int a = 0; a < OrMatrix.GetLength(0); a++) { for (int o = 0; o < OrMatrix.GetLength(1); o++) { sb.Append(OrMatrix[a, o] ? "1 " : "0 "); } sb.AppendLine(); } return(sb.ToString()); }