/// <summary> /// Creates a new map matrix between two arrays of expressions by mapping expressions according to which pairs pass a qualifying proposition (see remarks). /// Arrays don't have to be the same size. /// </summary> /// <remarks> /// If given the following two arrays and proposition /// {0, 1, 2, 3, 4, 5, 6, 7} /// {4, 4, 3, 3, 2, 2, 1, 1} /// (x, y) => x > y /// Would yeild the following map (if we aren't doing a 1-way matrix): /// {4 -> 2, 2 -> 5, 5 -> 2, 6 -> 1, 1 -> 7, 7 -> 1} /// </remarks> /// <param name="ExpressionList1">The first array of expressions</param> /// <param name="ExpressionList2">The second array of expressions</param> /// <param name="QualifyingProposition">The qualifying proposition that a pair of expressions must pass in order to be mapped</param> public MapMatrix(Expression[] ExpressionList1, Expression[] ExpressionList2, Proposition QualifyingProposition) { if (QualifyingProposition.NumberParameters != 2) throw new Exceptions.SimplexMathException("Unable to construct map matrix - Qualifying proposition must have 2 parameters"); if (ExpressionList1 == null || ExpressionList2 == null) throw new Exceptions.SimplexMathException("Unable to construct map matrix - Null input expression array(s)"); if (ExpressionList1.Length == 0 || ExpressionList2.Length == 0) throw new Exceptions.SimplexMathException("Unable to construct map matrix - Empty input expression array(s)"); this.InnerMatrix = new Dictionary<Expression, Expression>(); for (int i = 0; i < ExpressionList1.Length; i++) { for (int j = 0; j < ExpressionList2.Length; j++) { if (QualifyingProposition.Evaluate(ExpressionList1[i], ExpressionList2[j])) { this[ExpressionList1[i]] = ExpressionList2[j]; } } } }
/// <summary> /// Determines if this map matrix has a particular expression mapped that matches a given proposition. /// </summary> /// <param name="Input">The qualifying proposition</param> public bool ContainsMap(Proposition QualifyingProposition) { foreach (var key in this.InnerMatrix.Keys) { if (QualifyingProposition.Evaluate(key)) return true; } return false; }
/// <summary> /// Gets or sets the mapping for an array of key expressions that match a qualifying proposition for both the keys and their respective maps (see remarks). /// </summary> /// <param name="KeyQualifyingProposition">The qualifying proposition a key must pass in order to be considered for a get/set</param> /// <param name="ValueQualifyingProposition">The qualifying proposition the map of a passing key must pass in order to be get/set</param> public virtual List<Expression> this[Proposition KeyQualifyingProposition, Proposition ValueQualifyingProposition] { get { List<Expression> OutputList = new List<Expression>(); foreach (var key in this.InnerMatrix.Keys) { if (KeyQualifyingProposition.Evaluate(key)) if (ValueQualifyingProposition.Evaluate(this[key])) OutputList.Add(this[key]); } OutputList.Capacity = OutputList.Count; return OutputList; } set { int SetIndex = 0; foreach (var key in this.InnerMatrix.Keys) { if (KeyQualifyingProposition.Evaluate(key)) { if (ValueQualifyingProposition.Evaluate(this[key])) { this[key] = value[SetIndex]; if (SetIndex == value.Count - 1) SetIndex = 0; else SetIndex++; } } } } }