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