Created
January 11, 2017 20:55
-
-
Save gaucheph/bf565160d5df6e24870b5cd297e5a905 to your computer and use it in GitHub Desktop.
Given lists of possible values for 2 parameters and a list of positive relations between them, generate a comprehensive list of requirements.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Combinations | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
List<Parameter> moduleParameters = new List<Parameter> | |
{ | |
new Parameter(1), | |
new Parameter(2), | |
new Parameter(3) | |
}; | |
List<Parameter> featureParameters = new List<Parameter> | |
{ | |
new Parameter(13), | |
new Parameter(17), | |
new Parameter(19) | |
}; | |
List<ParameterRelation> moduleFeatureRelations = new List<ParameterRelation> | |
{ | |
new ParameterRelation(new Parameter(1), new Parameter(13)), | |
new ParameterRelation(new Parameter(1), new Parameter(19)), | |
new ParameterRelation(new Parameter(2), new Parameter(17)), | |
new ParameterRelation(new Parameter(3), new Parameter(13)), | |
new ParameterRelation(new Parameter(3), new Parameter(17)), | |
new ParameterRelation(new Parameter(3), new Parameter(19)) | |
}; | |
var requirements = GetRequirementsList<Parameter, ParameterRelation, ParameterRelationRequirement>(moduleParameters, featureParameters, moduleFeatureRelations); | |
Console.ReadLine(); | |
} | |
public static IEnumerable<T3> GetRequirementsList<T1, T2, T3>(IEnumerable<T1> parameter1, IEnumerable<T1> parameter2, IEnumerable<T2> allowedRelations) | |
where T1 : Parameter | |
where T2 : ParameterRelation | |
where T3 : ParameterRelationRequirement | |
{ | |
var allCombinations = parameter1.SelectMany(p1 => parameter2.Select(p2 => new {p1, p2})).ToList(); | |
var allowedCombinations = allowedRelations.Select(a => new {p1 = (T1) a.Parameter1, p2 = (T1) a.Parameter2}).ToList(); | |
var disallowedCombinations = allCombinations.Except(allowedCombinations).Select(d => new { d.p1, d.p2 }).ToList(); | |
var requirements = | |
allowedCombinations | |
.Select(a => new ParameterRelationRequirement(new ParameterRelation(a.p1, a.p2), true)) | |
.Union( | |
disallowedCombinations | |
.Select(d => new ParameterRelationRequirement(new ParameterRelation(d.p1, d.p2), false))) | |
.ToList(); | |
return requirements as IEnumerable<T3>; | |
} | |
} | |
public class ParameterRelationRequirement : IEquatable<ParameterRelationRequirement> | |
{ | |
public ParameterRelation ParameterRelation; | |
public bool HasAccess; | |
public ParameterRelationRequirement(ParameterRelation parameterRelation, bool hasAccess) | |
{ | |
ParameterRelation = parameterRelation; | |
HasAccess = hasAccess; | |
} | |
#region IEquatable implementation generated automatically by ReSharper | |
public bool Equals(ParameterRelationRequirement other) | |
{ | |
if (ReferenceEquals(null, other)) return false; | |
if (ReferenceEquals(this, other)) return true; | |
return Equals(ParameterRelation, other.ParameterRelation) && HasAccess == other.HasAccess; | |
} | |
public override bool Equals(object obj) | |
{ | |
if (ReferenceEquals(null, obj)) return false; | |
if (ReferenceEquals(this, obj)) return true; | |
if (obj.GetType() != this.GetType()) return false; | |
return Equals((ParameterRelationRequirement) obj); | |
} | |
public override int GetHashCode() | |
{ | |
unchecked | |
{ | |
return ((ParameterRelation != null ? ParameterRelation.GetHashCode() : 0) * 397) ^ HasAccess.GetHashCode(); | |
} | |
} | |
#endregion | |
} | |
public class ParameterRelation : IEquatable<ParameterRelation> | |
{ | |
public Parameter Parameter1; | |
public Parameter Parameter2; | |
public ParameterRelation(Parameter parameter1, Parameter parameter2) | |
{ | |
Parameter1 = parameter1; | |
Parameter2 = parameter2; | |
} | |
#region IEquatable implementation generated automatically by ReSharper | |
public bool Equals(ParameterRelation other) | |
{ | |
if (ReferenceEquals(null, other)) return false; | |
if (ReferenceEquals(this, other)) return true; | |
return Equals(Parameter1, other.Parameter1) && Equals(Parameter2, other.Parameter2); | |
} | |
public override bool Equals(object obj) | |
{ | |
if (ReferenceEquals(null, obj)) return false; | |
if (ReferenceEquals(this, obj)) return true; | |
if (obj.GetType() != this.GetType()) return false; | |
return Equals((ParameterRelation) obj); | |
} | |
public override int GetHashCode() | |
{ | |
unchecked | |
{ | |
return ((Parameter1 != null ? Parameter1.GetHashCode() : 0) * 397) ^ (Parameter2 != null ? Parameter2.GetHashCode() : 0); | |
} | |
} | |
#endregion | |
} | |
public class Parameter : IEquatable<Parameter> | |
{ | |
public object Value; | |
public Parameter(object value) | |
{ | |
Value = value; | |
} | |
#region IEquatable implementation generated automatically by ReSharper | |
public bool Equals(Parameter other) | |
{ | |
if (ReferenceEquals(null, other)) return false; | |
if (ReferenceEquals(this, other)) return true; | |
return Equals(Value, other.Value); | |
} | |
public override bool Equals(object obj) | |
{ | |
if (ReferenceEquals(null, obj)) return false; | |
if (ReferenceEquals(this, obj)) return true; | |
if (obj.GetType() != this.GetType()) return false; | |
return Equals((Parameter) obj); | |
} | |
public override int GetHashCode() | |
{ | |
return (Value != null ? Value.GetHashCode() : 0); | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment