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