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