Changeset 9815 for branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Compiler/SymbolicExpressionTreeLinearCompiler.cs
- Timestamp:
- 07/30/13 13:58:45 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Compiler/SymbolicExpressionTreeLinearCompiler.cs
r9758 r9815 21 21 22 22 using System; 23 using System.Collections.Generic;24 using System.Linq;25 23 26 24 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 28 26 public static LinearInstruction[] Compile(ISymbolicExpressionTree tree, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper) { 29 27 var root = tree.Root.GetSubtree(0).GetSubtree(0); 30 var code = new List<LinearInstruction> { 31 new LinearInstruction { dynamicNode = root, nArguments = (byte)root.SubtreeCount, opCode = opCodeMapper(root) } 32 }; 33 // iterate breadth-wise over tree nodes and produce an array of instructions 34 var nodes = root.IterateNodesBreadth().ToArray(); 35 for (int i = 0; i != nodes.Length; ++i) { 36 code[i].childIndex = (byte)code.Count; 37 code.AddRange(nodes[i].Subtrees.Select(s => new LinearInstruction { dynamicNode = s, nArguments = (byte)s.SubtreeCount, opCode = opCodeMapper(s) })); 28 var code = new LinearInstruction[root.GetLength()]; 29 code[0] = new LinearInstruction { dynamicNode = root, nArguments = (byte)root.SubtreeCount, opCode = opCodeMapper(root) }; 30 int c = 1, i = 0; 31 foreach (var node in root.IterateNodesBreadth()) { 32 for (int j = 0; j != node.SubtreeCount; ++j) { 33 var s = node.GetSubtree(j); 34 code[c + j] = new LinearInstruction { dynamicNode = s, nArguments = (byte)s.SubtreeCount, opCode = opCodeMapper(s) }; 35 } 36 code[i].childIndex = (byte)c; 37 c += node.SubtreeCount; 38 ++i; 38 39 } 39 40 return code.ToArray(); 40 return code; 41 41 } 42 42 }
Note: See TracChangeset
for help on using the changeset viewer.