From e430006da2fda135740093bc13495374554854ac Mon Sep 17 00:00:00 2001 From: Lucas Scharenbroich Date: Mon, 5 Dec 2016 23:35:22 -0600 Subject: [PATCH] Broken code which converting state representation --- .../SpriteGeneratorHeuristicFunction.cs | 8 ++-- .../Problem/SpriteGeneratorState.cs | 45 +++++++++---------- 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/SpriteCompiler/Problem/SpriteGeneratorHeuristicFunction.cs b/SpriteCompiler/Problem/SpriteGeneratorHeuristicFunction.cs index c6d0c0d..738d415 100644 --- a/SpriteCompiler/Problem/SpriteGeneratorHeuristicFunction.cs +++ b/SpriteCompiler/Problem/SpriteGeneratorHeuristicFunction.cs @@ -46,17 +46,15 @@ // An even-length run can be done, at best in 4 cycles/word // An odd-length run is even + 3 cycles/byte - var count = state.Bytes.Count; + if (state.IsEmpty) return 0; - if (count == 0) return 0; - - var offsets = state.Bytes.Select(x => x.Offset).OrderBy(x => x).ToList(); + var offsets = state.RemainingBytes(); var start = offsets[0]; var stack = state.S.Value; var curr = start; var cost = 0; - for (int i = 1; i < count; i++) + for (int i = 1; i < offsets.Count; i++) { var prev = curr; curr = offsets[i]; diff --git a/SpriteCompiler/Problem/SpriteGeneratorState.cs b/SpriteCompiler/Problem/SpriteGeneratorState.cs index 267b387..f184b68 100644 --- a/SpriteCompiler/Problem/SpriteGeneratorState.cs +++ b/SpriteCompiler/Problem/SpriteGeneratorState.cs @@ -6,19 +6,13 @@ public class SpriteGeneratorState : IEquatable { + // Single static reference to the original data set + public static List DATASET = null; + public SpriteGeneratorState() - : this(new SpriteByte[0]) { - } - - public SpriteGeneratorState(byte[] data) - : this(data.Select((x, i) => new SpriteByte(x, (ushort)i))) - { - } - - public SpriteGeneratorState(IEnumerable bytes) - { - Bytes = bytes.ToList(); + // The closed list contains all of the bytes that have been written + Closed = new HashSet(); // Initialize the CPU state A = Register.INITIAL_OFFSET; // the address to draw the sprite is passed in, this is a run-time value @@ -33,7 +27,7 @@ private SpriteGeneratorState(SpriteGeneratorState other) { - Bytes = new List(other.Bytes); + Closed = new HashSet(other.Closed); A = other.A; X = other.X; Y = other.Y; @@ -49,20 +43,21 @@ public void RemoveWord(ushort offset) { - var total = Bytes.RemoveAll(x => x.Offset == offset || x.Offset == (offset + 1)); - if (total != 2) - { - throw new ArgumentException(string.Format("Cannot remove word at {0}", offset)); - } + Closed.Add(offset); + Closed.Add((ushort)(offset + 1)); } public void RemoveByte(ushort offset) { - var total = Bytes.RemoveAll(x => x.Offset == offset); - if (total != 1) - { - throw new ArgumentException(string.Format("Cannot remove byte at {0}", offset)); - } + Closed.Add(offset); + } + + public List RemainingBytes() + { + return DATASET + .Select(x => x.Offset) + .Where(x => !Closed.Contains(x)) + .ToList(); } public SpriteGeneratorState Clone(Action f = null) @@ -81,8 +76,8 @@ // data and mask array. Then the state is just the locations and registers, rather // than a full copy of the data - public List Bytes { get; private set; } - public bool IsEmpty { get { return Bytes.Count == 0; } } + public ISet Closed { get; private set; } + public bool IsEmpty { get { return Closed.Count == DATASET.Count; } } public bool LongA { get { return (P & 0x10) == 0x10; } } public bool LongI { get { return (P & 0x20) == 0x20; } } @@ -107,7 +102,7 @@ public bool Equals(SpriteGeneratorState other) { // Two states are equal if the bytes are the same and all registers are the same - return Bytes.SequenceEqual(other.Bytes) && + return Closed.SetEquals(other.Closed) && A.Equals(other.A) && X.Equals(other.X) && Y.Equals(other.Y) &&