Changeset 14875 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/ExpressionTable.cs
- Timestamp:
- 04/18/17 01:15:25 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/ExpressionTable.cs
r14834 r14875 4 4 using System.Linq; 5 5 6 using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;7 using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool;8 using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;6 using Attributes; 7 using Data.Pool; 8 using Stack; 9 9 10 10 public static class ExpressionTable { 11 public static readonly IDictionary<string, Expression> StatelessExpressionTable; 12 public static readonly IDictionary<string, Func<Expression>> StatefulExpressionFactory; 13 public static readonly IDictionary<StackTypes, IList<string>> StackTypeToNamesTable = new Dictionary<StackTypes, IList<string>>(); 14 public static readonly IDictionary<int, string> IndexToNameTable = new Dictionary<int, string>(); 15 public static readonly IDictionary<Type, string> TypeToNameTable = new Dictionary<Type, string>(); 16 public static readonly IDictionary<Type, PushExpressionAttribute> TypeToAttributeTable = new Dictionary<Type, PushExpressionAttribute>(); 17 public static readonly IDictionary<StackTypes, IList<string>> StackDependencyToNamesTable = new Dictionary<StackTypes, IList<string>>(); 18 public static readonly string[] ExpressionNames; 19 20 public static readonly Type[] StatelessExpressionTypes; 21 public static readonly Type[] StatefulExpressionTypes; 22 23 public static readonly int Count; 11 public static readonly IReadOnlyDictionary<string, Expression> StatelessExpressionTable; 12 public static readonly IReadOnlyDictionary<string, Func<Expression>> StatefulExpressionFactory; 13 public static readonly IReadOnlyList<string> ExpressionNames; 14 public static readonly int ExpressionCount; 15 16 private static readonly Dictionary<int, string> indexToNameTable = new Dictionary<int, string>(); 17 private static readonly Dictionary<Type, string> typeToNameTable = new Dictionary<Type, string>(); 18 private static readonly Dictionary<string, Type> nameToTypeTable = new Dictionary<string, Type>(); 19 private static readonly Dictionary<StackTypes, IList<string>> stackTypeToNamesTable = new Dictionary<StackTypes, IList<string>>(); 20 private static readonly Dictionary<StackTypes, IList<string>> stackDependencyToNamesTable = new Dictionary<StackTypes, IList<string>>(); 21 private static readonly Dictionary<Type, PushExpressionAttribute> typeToAttributeTable = new Dictionary<Type, PushExpressionAttribute>(); 22 23 public static readonly IReadOnlyList<Type> StatelessExpressionTypes; 24 public static readonly IReadOnlyList<Type> StatefulExpressionTypes; 24 25 25 26 static ExpressionTable() { … … 31 32 ExpressionNames = StatelessExpressionTable.Keys.Concat(StatefulExpressionFactory.Keys).ToArray(); 32 33 33 Count = StatelessExpressionTable.Count + StatefulExpressionFactory.Count; 34 } 35 36 public static int StatelessCount 37 { 38 get 39 { 40 return StatelessExpressionTable.Count; 41 } 42 } 43 44 public static int StatefullCount 45 { 46 get 47 { 48 return StatefulExpressionFactory.Count; 49 } 50 } 34 ExpressionCount = StatelessExpressionTable.Count + StatefulExpressionFactory.Count; 35 } 36 37 public static IReadOnlyDictionary<int, string> IndexToNameTable { get { return indexToNameTable; } } 38 public static IReadOnlyDictionary<Type, string> TypeToNameTable { get { return typeToNameTable; } } 39 public static IReadOnlyDictionary<string, Type> NameToTypeTable { get { return nameToTypeTable; } } 40 public static IReadOnlyDictionary<StackTypes, IList<string>> StackTypeToNamesTable { get { return stackTypeToNamesTable; } } 41 public static IReadOnlyDictionary<StackTypes, IList<string>> StackDependencyToNamesTable { get { return stackDependencyToNamesTable; } } 42 public static IReadOnlyDictionary<Type, PushExpressionAttribute> TypeToAttributeTable { get { return typeToAttributeTable; } } 51 43 52 44 private static Dictionary<string, Expression> GetStatelessExpressionTable() { … … 57 49 var attribute = (PushExpressionAttribute)Attribute.GetCustomAttribute(type, typeof(PushExpressionAttribute)); 58 50 59 TypeToAttributeTable.Add(type, attribute);51 typeToAttributeTable.Add(type, attribute); 60 52 dictionary.Add(attribute.ExpressionName, expression); 61 IndexToNameTable.Add(IndexToNameTable.Keys.Count, attribute.ExpressionName); 62 TypeToNameTable.Add(type, attribute.ExpressionName); 63 64 if (!StackTypeToNamesTable.ContainsKey(attribute.StackType)) { 65 StackTypeToNamesTable.Add(attribute.StackType, new List<string>()); 66 } 67 68 StackTypeToNamesTable[attribute.StackType].Add(attribute.ExpressionName); 53 indexToNameTable.Add(indexToNameTable.Keys.Count, attribute.ExpressionName); 54 typeToNameTable.Add(type, attribute.ExpressionName); 55 nameToTypeTable.Add(attribute.ExpressionName, type); 56 57 if (!stackTypeToNamesTable.ContainsKey(attribute.StackType)) { 58 stackTypeToNamesTable.Add(attribute.StackType, new List<string>()); 59 } 60 61 stackTypeToNamesTable[attribute.StackType].Add(attribute.ExpressionName); 69 62 70 63 var dependencies = attribute.StackType | attribute.AdditionalStackDependencies; 71 if (! StackDependencyToNamesTable.ContainsKey(dependencies)) {72 StackDependencyToNamesTable.Add(dependencies, new List<string>());73 } 74 75 StackDependencyToNamesTable[dependencies].Add(attribute.ExpressionName);64 if (!stackDependencyToNamesTable.ContainsKey(dependencies)) { 65 stackDependencyToNamesTable.Add(dependencies, new List<string>()); 66 } 67 68 stackDependencyToNamesTable[dependencies].Add(attribute.ExpressionName); 76 69 } 77 70 … … 90 83 var attribute = (PushExpressionAttribute)Attribute.GetCustomAttribute(type, typeof(PushExpressionAttribute)); 91 84 92 TypeToAttributeTable.Add(type, attribute);85 typeToAttributeTable.Add(type, attribute); 93 86 dictionary.Add(attribute.ExpressionName, creator); 94 IndexToNameTable.Add(IndexToNameTable.Keys.Count, attribute.ExpressionName); 95 TypeToNameTable.Add(type, attribute.ExpressionName); 96 97 if (!StackTypeToNamesTable.ContainsKey(attribute.StackType)) { 98 StackTypeToNamesTable.Add(attribute.StackType, new List<string>()); 99 } 100 101 StackTypeToNamesTable[attribute.StackType].Add(attribute.ExpressionName); 87 indexToNameTable.Add(indexToNameTable.Keys.Count, attribute.ExpressionName); 88 typeToNameTable.Add(type, attribute.ExpressionName); 89 nameToTypeTable.Add(attribute.ExpressionName, type); 90 91 if (!stackTypeToNamesTable.ContainsKey(attribute.StackType)) { 92 stackTypeToNamesTable.Add(attribute.StackType, new List<string>()); 93 } 94 95 stackTypeToNamesTable[attribute.StackType].Add(attribute.ExpressionName); 102 96 103 97 var dependencies = attribute.StackType | attribute.AdditionalStackDependencies; 104 if (! StackDependencyToNamesTable.ContainsKey(dependencies)) {105 StackDependencyToNamesTable.Add(dependencies, new List<string>());106 } 107 108 StackDependencyToNamesTable[dependencies].Add(attribute.ExpressionName);98 if (!stackDependencyToNamesTable.ContainsKey(dependencies)) { 99 stackDependencyToNamesTable.Add(dependencies, new List<string>()); 100 } 101 102 stackDependencyToNamesTable[dependencies].Add(attribute.ExpressionName); 109 103 } 110 104 … … 146 140 147 141 public static IReadOnlyList<string> GetExpressionsByStackTypes(StackTypes allowedTypes) { 148 return StackDependencyToNamesTable142 return stackDependencyToNamesTable 149 143 .Where(entry => (entry.Key & ~allowedTypes) == StackTypes.None) 150 144 .SelectMany(entry => entry.Value) … … 163 157 164 158 public static Expression GetExpression(int index) { 165 return GetExpression(IndexToNameTable[index]); 159 return GetExpression(indexToNameTable[index]); 160 } 161 162 public static Expression GetExpression(Type type) { 163 var name = TypeToNameTable[type]; 164 return GetExpression(name); 166 165 } 167 166 … … 177 176 178 177 public static Expression GetStatelessExpression<T>() where T : StatelessExpression { 179 return GetStatelessExpression( TypeToNameTable[typeof(T)]);178 return GetStatelessExpression(typeToNameTable[typeof(T)]); 180 179 } 181 180 … … 198 197 199 198 public static Expression GetStatefulExpression<T>() { 200 return GetStatefulExpression( TypeToNameTable[typeof(T)]);199 return GetStatefulExpression(typeToNameTable[typeof(T)]); 201 200 } 202 201
Note: See TracChangeset
for help on using the changeset viewer.