[15771] | 1 | namespace HeuristicLab.Problems.ProgramSynthesis { |
---|
[14744] | 2 | using System;
|
---|
| 3 | using System.Collections;
|
---|
| 4 | using System.Collections.Generic;
|
---|
| 5 | using System.Linq;
|
---|
| 6 |
|
---|
| 7 | /// <summary>
|
---|
[14914] | 8 | /// While Push's Stacks are generally treated as genuine Stacks---that is, inclassions take their arguments from the
|
---|
[14744] | 9 | /// tops of
|
---|
[14914] | 10 | /// the Stacks and push their results onto the tops of the Stacks---a few inclassions (like YANK and SHOVE) do allow
|
---|
[14744] | 11 | /// direct access
|
---|
[14914] | 12 | /// to "deep" stack elements by means of integer indices. To this extent the Stacks can be used as general, random
|
---|
[14744] | 13 | /// access memory
|
---|
| 14 | /// classures. This is one of the features that ensures the Turing-completeness of Push (another being the arbitrary
|
---|
| 15 | /// name/value
|
---|
| 16 | /// bindings supported by the NAME data type and DEFINE methods; see below).
|
---|
| 17 | /// </summary>
|
---|
| 18 | /// <typeparam name="T">The item type of the collection.</typeparam>
|
---|
[14834] | 19 | public class PushStack<T> : IPushStack<T> {
|
---|
[14744] | 20 | private const string Delimiter = " ";
|
---|
| 21 |
|
---|
| 22 | private readonly List<T> data;
|
---|
| 23 |
|
---|
| 24 | public PushStack(int capacity = 0) {
|
---|
| 25 | data = new List<T>(capacity);
|
---|
| 26 | IsEnabled = true;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | public bool IsEnabled { get; set; }
|
---|
| 30 |
|
---|
| 31 | public int Capacity
|
---|
| 32 | {
|
---|
| 33 | get
|
---|
| 34 | {
|
---|
[14777] | 35 | return data.Capacity;
|
---|
[14744] | 36 | }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | public T Top
|
---|
| 40 | {
|
---|
| 41 | get
|
---|
| 42 | {
|
---|
[14777] | 43 | return data[Count - 1];
|
---|
[14744] | 44 | }
|
---|
[15017] | 45 |
|
---|
| 46 | set
|
---|
| 47 | {
|
---|
| 48 | data[Count - 1] = value;
|
---|
| 49 | }
|
---|
[14744] | 50 | }
|
---|
| 51 |
|
---|
| 52 | public T TopOrDefault
|
---|
| 53 | {
|
---|
| 54 | get
|
---|
| 55 | {
|
---|
[14777] | 56 | return Count > 0 ? Top : default(T);
|
---|
[14744] | 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | public T Bottom
|
---|
| 61 | {
|
---|
| 62 | get
|
---|
| 63 | {
|
---|
[14777] | 64 | return data[0];
|
---|
[14744] | 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | public T BottomOrDefault
|
---|
| 69 | {
|
---|
| 70 | get
|
---|
| 71 | {
|
---|
[14777] | 72 | return Count > 0 ? Bottom : default(T);
|
---|
[14744] | 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | public int Count
|
---|
| 77 | {
|
---|
| 78 | get
|
---|
| 79 | {
|
---|
[14777] | 80 | return data.Count;
|
---|
[14744] | 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | public bool IsEmpty
|
---|
| 85 | {
|
---|
| 86 | get
|
---|
| 87 | {
|
---|
[14777] | 88 | return Count == 0;
|
---|
[14744] | 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | public bool IsReadOnly
|
---|
| 93 | {
|
---|
| 94 | get
|
---|
| 95 | {
|
---|
| 96 | return false;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | public void Add(T item) {
|
---|
[14777] | 101 | Push(item);
|
---|
[14744] | 102 | }
|
---|
| 103 |
|
---|
| 104 | public void Clear() {
|
---|
[14777] | 105 | data.Clear();
|
---|
[14744] | 106 | }
|
---|
| 107 |
|
---|
| 108 | public bool Contains(T item) {
|
---|
[14777] | 109 | return data.Contains(item);
|
---|
[14744] | 110 | }
|
---|
| 111 |
|
---|
| 112 | public void CopyTo(T[] array, int arrayIndex) {
|
---|
[14777] | 113 | data.CopyTo(array, arrayIndex);
|
---|
[14744] | 114 | }
|
---|
| 115 |
|
---|
| 116 | public IEnumerator<T> GetEnumerator() {
|
---|
[14777] | 117 | return data.GetEnumerator();
|
---|
[14744] | 118 | }
|
---|
| 119 |
|
---|
| 120 | public T ElementAt(int index) {
|
---|
[14777] | 121 | return data[index];
|
---|
[14744] | 122 | }
|
---|
| 123 |
|
---|
[14875] | 124 | public T this[int index]
|
---|
[14744] | 125 | {
|
---|
[14875] | 126 | get { return data[Count - 1 - index]; }
|
---|
| 127 | set { data[Count - 1 - index] = value; }
|
---|
[14744] | 128 | }
|
---|
| 129 |
|
---|
| 130 | public void Swap(int count) {
|
---|
[14777] | 131 | var top = Top;
|
---|
| 132 | var bottomIndex = Count - count;
|
---|
[14744] | 133 |
|
---|
[14875] | 134 | for (var i = Count - 1; i > bottomIndex; i--)
|
---|
| 135 | data[i] = data[i - 1];
|
---|
[14744] | 136 |
|
---|
[14777] | 137 | data[bottomIndex] = top;
|
---|
[14744] | 138 | }
|
---|
| 139 |
|
---|
| 140 | public void Yank(int index) {
|
---|
[14777] | 141 | if (index == Count - 1) return;
|
---|
[14744] | 142 |
|
---|
[15189] | 143 | var item = data[index];
|
---|
[14777] | 144 | data.RemoveAt(index);
|
---|
| 145 | data.Add(item);
|
---|
[14744] | 146 | }
|
---|
| 147 |
|
---|
| 148 | public T Pop() {
|
---|
[14875] | 149 | var top = Top;
|
---|
[14777] | 150 | data.RemoveAt(Count - 1);
|
---|
[14744] | 151 |
|
---|
[14875] | 152 | return top;
|
---|
[14744] | 153 | }
|
---|
| 154 |
|
---|
[15189] | 155 | public IReadOnlyList<T> Pop(int count) {
|
---|
[14744] | 156 | var items = Peek(count);
|
---|
[14777] | 157 | Remove(count);
|
---|
[14744] | 158 |
|
---|
| 159 | return items;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[15189] | 162 | public IReadOnlyList<T> Peek(int count) {
|
---|
[14777] | 163 | var startIndex = Count - count;
|
---|
[14744] | 164 |
|
---|
| 165 | var items = new T[count];
|
---|
[14777] | 166 | data.CopyTo(startIndex, items, 0, count);
|
---|
[14744] | 167 |
|
---|
| 168 | return items;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | public void Push(T item) {
|
---|
| 172 | if (!IsEnabled) return;
|
---|
| 173 |
|
---|
[14777] | 174 | data.Add(item);
|
---|
[14744] | 175 | }
|
---|
| 176 |
|
---|
[14777] | 177 | public void Push(T item1, T item2) {
|
---|
[14744] | 178 | if (!IsEnabled) return;
|
---|
| 179 |
|
---|
[14777] | 180 | data.Add(item1);
|
---|
| 181 | data.Add(item2);
|
---|
[14744] | 182 | }
|
---|
| 183 |
|
---|
[14777] | 184 | public void Push(T item1, T item2, T item3) {
|
---|
[14744] | 185 | if (!IsEnabled) return;
|
---|
| 186 |
|
---|
[14777] | 187 | data.Add(item1);
|
---|
| 188 | data.Add(item2);
|
---|
| 189 | data.Add(item3);
|
---|
[14744] | 190 | }
|
---|
| 191 |
|
---|
[14777] | 192 | public void Push(T item1, T item2, T item3, T item4) {
|
---|
[14744] | 193 | if (!IsEnabled) return;
|
---|
| 194 |
|
---|
[14777] | 195 | data.Add(item1);
|
---|
| 196 | data.Add(item2);
|
---|
| 197 | data.Add(item3);
|
---|
| 198 | data.Add(item4);
|
---|
[14744] | 199 | }
|
---|
| 200 |
|
---|
[14875] | 201 | public void Push(IReadOnlyList<T> items, int startIndex) {
|
---|
[14744] | 202 | if (!IsEnabled) return;
|
---|
| 203 |
|
---|
[14777] | 204 | for (var i = startIndex; i < items.Count; i++) data.Add(items[i]);
|
---|
[14744] | 205 | }
|
---|
| 206 |
|
---|
[14875] | 207 | public void Push(IReadOnlyList<T> items) {
|
---|
| 208 | if (!IsEnabled) return;
|
---|
| 209 |
|
---|
| 210 | data.AddRange(items);
|
---|
| 211 | }
|
---|
| 212 |
|
---|
[14909] | 213 | public void Push(IEnumerable<T> items) {
|
---|
| 214 | if (!IsEnabled) return;
|
---|
| 215 |
|
---|
| 216 | data.AddRange(items);
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[14777] | 219 | public void Insert(int index, T item) {
|
---|
[14744] | 220 | if (!IsEnabled) return;
|
---|
| 221 |
|
---|
[14875] | 222 | data.Insert(Count - index, item);
|
---|
[14744] | 223 | }
|
---|
| 224 |
|
---|
| 225 | public bool Remove(T item) {
|
---|
[14908] | 226 | return data.Remove(item);
|
---|
[14744] | 227 | }
|
---|
| 228 |
|
---|
| 229 | public void RemoveTop() {
|
---|
[14777] | 230 | data.RemoveAt(Count - 1);
|
---|
[14744] | 231 | }
|
---|
| 232 |
|
---|
| 233 | public void Remove(int count) {
|
---|
[14908] | 234 | data.RemoveRange(Count - count, count);
|
---|
[14744] | 235 | }
|
---|
| 236 |
|
---|
| 237 | public void RemoveAt(int index) {
|
---|
[14777] | 238 | data.RemoveAt(index);
|
---|
[14744] | 239 | }
|
---|
| 240 |
|
---|
| 241 | public void RemoveAt(int index, int count) {
|
---|
[14777] | 242 | data.RemoveRange(index, count);
|
---|
[14744] | 243 | }
|
---|
| 244 |
|
---|
| 245 | public bool TryPop(out T item) {
|
---|
[14777] | 246 | if (Count > 0) {
|
---|
| 247 | item = Pop();
|
---|
[14744] | 248 | return true;
|
---|
| 249 | }
|
---|
| 250 | item = default(T);
|
---|
| 251 | return false;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
| 255 | return data.GetEnumerator();
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | public override string ToString() {
|
---|
[15017] | 259 | return string.Join(Delimiter, data.Select(x => x.ToString()).Reverse());
|
---|
[14744] | 260 | }
|
---|
[14914] | 261 |
|
---|
| 262 | IEnumerable<string> IPushStack.AsStrings() {
|
---|
| 263 | return data.Select(x => x.ToString());
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | IEnumerable<object> IPushStack.AsObjects() {
|
---|
| 267 | return data.OfType<object>();
|
---|
| 268 | }
|
---|
[14744] | 269 | }
|
---|
| 270 | } |
---|