Changeset 14952 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/ExpressionTable.cs
- Timestamp:
- 05/10/17 11:23:05 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/ExpressionTable.cs
r14909 r14952 9 9 10 10 public static class ExpressionTable { 11 public static readonly IReadOnlyDictionary< string, Expression> StatelessExpressionTable;12 public static readonly IReadOnlyDictionary< string, Func<Expression>> StatefulExpressionFactory;11 public static readonly IReadOnlyDictionary<Type, Expression> StatelessExpressionTable; 12 public static readonly IReadOnlyDictionary<Type, Func<Expression>> StatefulExpressionFactory; 13 13 public static readonly IReadOnlyList<string> ExpressionNames; 14 14 public static readonly int ExpressionCount; … … 30 30 StatelessExpressionTable = GetStatelessExpressionTable(); 31 31 StatefulExpressionFactory = GetStatefulExpressionFactory(); 32 ExpressionNames = StatelessExpressionTable.Keys.Concat(StatefulExpressionFactory.Keys).ToArray(); 32 33 ExpressionNames = StatelessExpressionTable.Keys 34 .Concat(StatefulExpressionFactory.Keys) 35 .Select(type => typeToNameTable[type]) 36 .ToArray(); 33 37 34 38 ExpressionCount = StatelessExpressionTable.Count + StatefulExpressionFactory.Count; … … 42 46 public static IReadOnlyDictionary<Type, PushExpressionAttribute> TypeToAttributeTable { get { return typeToAttributeTable; } } 43 47 44 private static Dictionary< string, Expression> GetStatelessExpressionTable() {45 var dictionary = new Dictionary< string, Expression>();48 private static Dictionary<Type, Expression> GetStatelessExpressionTable() { 49 var dictionary = new Dictionary<Type, Expression>(); 46 50 47 51 foreach (var type in StatelessExpressionTypes) { … … 50 54 51 55 typeToAttributeTable.Add(type, attribute); 52 dictionary.Add( attribute.ExpressionName, expression);56 dictionary.Add(type, expression); 53 57 indexToNameTable.Add(indexToNameTable.Keys.Count, attribute.ExpressionName); 54 58 typeToNameTable.Add(type, attribute.ExpressionName); … … 72 76 } 73 77 74 private static Dictionary< string, Func<Expression>> GetStatefulExpressionFactory() {75 var dictionary = new Dictionary< string, Func<Expression>>();78 private static Dictionary<Type, Func<Expression>> GetStatefulExpressionFactory() { 79 var dictionary = new Dictionary<Type, Func<Expression>>(); 76 80 77 81 foreach (var type in StatefulExpressionTypes) { … … 84 88 85 89 typeToAttributeTable.Add(type, attribute); 86 dictionary.Add( attribute.ExpressionName, creator);90 dictionary.Add(type, creator); 87 91 indexToNameTable.Add(indexToNameTable.Keys.Count, attribute.ExpressionName); 88 92 typeToNameTable.Add(type, attribute.ExpressionName); … … 180 184 181 185 public static Expression GetStatelessExpression(string name) { 182 if ( StatelessExpressionTable.ContainsKey(name))183 return StatelessExpressionTable[ name];186 if (NameToTypeTable.ContainsKey(name) && StatelessExpressionTable.ContainsKey(NameToTypeTable[name])) 187 return StatelessExpressionTable[NameToTypeTable[name]]; 184 188 throw new NotSupportedException("Expression not supported: " + name); 185 189 } 186 190 187 191 public static bool TryGetStatelessExpression(string name, out Expression expression) { 188 if (! StatelessExpressionTable.ContainsKey(name)) {192 if (!NameToTypeTable.ContainsKey(name) || !StatelessExpressionTable.ContainsKey(NameToTypeTable[name])) { 189 193 expression = null; 190 194 return false; 191 195 } 192 196 193 expression = StatelessExpressionTable[name]; 194 197 expression = StatelessExpressionTable[NameToTypeTable[name]]; 195 198 return true; 196 199 } … … 201 204 202 205 public static Expression GetStatefulExpression(string name) { 203 Func<Expression> creator; 204 if (StatefulExpressionFactory.TryGetValue(name, out creator)) 205 return creator(); 206 if (NameToTypeTable.ContainsKey(name)) { 207 Func<Expression> creator; 208 var type = NameToTypeTable[name]; 209 210 if (StatefulExpressionFactory.TryGetValue(type, out creator)) 211 return creator(); 212 } 213 206 214 throw new NotSupportedException("Expression not supported: " + name); 207 215 } 208 216 209 217 public static bool TryGetStatefulExpression(string name, out Expression expression) { 210 if ( StatefulExpressionFactory.ContainsKey(name)) {211 expression = StatefulExpressionFactory[ name]();218 if (NameToTypeTable.ContainsKey(name)) { 219 expression = StatefulExpressionFactory[NameToTypeTable[name]](); 212 220 return true; 213 221 }
Note: See TracChangeset
for help on using the changeset viewer.