1 | namespace HeuristicLab.Problems.ProgramSynthesis { |
---|
2 | using System.Collections.Generic;
|
---|
3 | using HeuristicLab.Common;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
6 |
|
---|
7 | [StorableClass]
|
---|
8 | public sealed class PlushVector : Item {
|
---|
9 |
|
---|
10 | [Storable]
|
---|
11 | private readonly List<PlushEntry> entries;
|
---|
12 | public IReadOnlyList<PlushEntry> Entries { get { return entries; } }
|
---|
13 |
|
---|
14 | public PlushVector() : this(0) { }
|
---|
15 |
|
---|
16 | public PlushVector(int length) {
|
---|
17 | entries = new List<PlushEntry>(length);
|
---|
18 | }
|
---|
19 |
|
---|
20 | [StorableConstructor]
|
---|
21 | private PlushVector(bool deserializing) : base(deserializing) {
|
---|
22 | }
|
---|
23 |
|
---|
24 | public PlushVector(PlushVector origin, Cloner cloner) : base(origin, cloner) {
|
---|
25 | entries = new List<PlushEntry>(origin.entries.Count);
|
---|
26 | for (var i = 0; i < origin.entries.Count; i++) {
|
---|
27 | entries.Add(cloner.Clone(origin.entries[i]));
|
---|
28 | }
|
---|
29 |
|
---|
30 | pushProgram = origin.pushProgram;
|
---|
31 | }
|
---|
32 |
|
---|
33 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
34 | return new PlushVector(this, cloner);
|
---|
35 | }
|
---|
36 |
|
---|
37 | private PushProgram pushProgram;
|
---|
38 |
|
---|
39 | public PushProgram PushProgram {
|
---|
40 | get {
|
---|
41 | if (pushProgram == null) CreatePushProgram();
|
---|
42 | return pushProgram;
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | private void CreatePushProgram() {
|
---|
47 | var close = 0;
|
---|
48 | var currentIndex = 0;
|
---|
49 |
|
---|
50 | pushProgram = FromPlush(ref currentIndex, ref close, 0);
|
---|
51 | }
|
---|
52 |
|
---|
53 | private PushProgram FromPlush(
|
---|
54 | ref int currentIndex,
|
---|
55 | ref int close,
|
---|
56 | int depth) {
|
---|
57 |
|
---|
58 | if (currentIndex >= entries.Count)
|
---|
59 | return PushProgram.Empty;
|
---|
60 |
|
---|
61 | var instructions = new List<Expression>();
|
---|
62 |
|
---|
63 | for (; currentIndex < entries.Count; currentIndex++) {
|
---|
64 | var entry = entries[currentIndex];
|
---|
65 |
|
---|
66 | if (entry.Silent)
|
---|
67 | continue;
|
---|
68 |
|
---|
69 | close += entry.Close;
|
---|
70 | instructions.Add(entry.Instruction);
|
---|
71 |
|
---|
72 | PushExpressionAttribute attribute;
|
---|
73 | var instructionType = entry.Instruction.GetType();
|
---|
74 |
|
---|
75 | if (ExpressionTable.TypeToAttributeTable.TryGetValue(instructionType, out attribute)) {
|
---|
76 | for (var blockIdx = 0u; blockIdx < attribute.RequiredBlockCount; blockIdx++) {
|
---|
77 | if (close == 0 && currentIndex < entries.Count) {
|
---|
78 | currentIndex++;
|
---|
79 | var subProgram = FromPlush(ref currentIndex, ref close, depth + 1);
|
---|
80 | instructions.Add(subProgram);
|
---|
81 | } else {
|
---|
82 | close--;
|
---|
83 | instructions.Add(PushProgram.Empty);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | if (close > 0 && depth > 0) {
|
---|
89 | close--;
|
---|
90 | break;
|
---|
91 | }
|
---|
92 |
|
---|
93 | if (depth == 0) {
|
---|
94 | close = 0;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | instructions.Reverse();
|
---|
99 | return new PushProgram(instructions);
|
---|
100 | }
|
---|
101 |
|
---|
102 | public void UpdatePushProgram() {
|
---|
103 | pushProgram = null;
|
---|
104 | }
|
---|
105 |
|
---|
106 | public void Add(PlushEntry entry) {
|
---|
107 | entries.Add(entry);
|
---|
108 | }
|
---|
109 |
|
---|
110 | public PlushEntry this[int key] {
|
---|
111 | get {
|
---|
112 | return entries[key];
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|