[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 PushStack3<T> {
|
---|
| 19 | private const string Delimiter = " ";
|
---|
| 20 |
|
---|
| 21 | private readonly LinkedList<T> data;
|
---|
| 22 |
|
---|
| 23 | public PushStack3() {
|
---|
[14908] | 24 | data = new LinkedList<T>();
|
---|
| 25 | IsEnabled = true;
|
---|
[14777] | 26 | }
|
---|
| 27 |
|
---|
| 28 | public bool IsEnabled { get; set; }
|
---|
| 29 |
|
---|
| 30 | public T Top
|
---|
| 31 | {
|
---|
| 32 | get
|
---|
| 33 | {
|
---|
[14908] | 34 | return data.Last.Value;
|
---|
[14777] | 35 | }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | public T TopOrDefault
|
---|
| 39 | {
|
---|
| 40 | get
|
---|
| 41 | {
|
---|
[14908] | 42 | return Count > 0 ? Top : default(T);
|
---|
[14777] | 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public T Bottom
|
---|
| 47 | {
|
---|
| 48 | get
|
---|
| 49 | {
|
---|
[14908] | 50 | return data.First.Value;
|
---|
[14777] | 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public T BottomOrDefault
|
---|
| 55 | {
|
---|
| 56 | get
|
---|
| 57 | {
|
---|
[14908] | 58 | return Count > 0 ? Bottom : default(T);
|
---|
[14777] | 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public int Count
|
---|
| 63 | {
|
---|
| 64 | get
|
---|
| 65 | {
|
---|
[14908] | 66 | return data.Count;
|
---|
[14777] | 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | public bool IsEmpty
|
---|
| 71 | {
|
---|
| 72 | get
|
---|
| 73 | {
|
---|
[14908] | 74 | return Count == 0;
|
---|
[14777] | 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | public bool IsReadOnly
|
---|
| 79 | {
|
---|
| 80 | get
|
---|
| 81 | {
|
---|
| 82 | return false;
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | public void Add(T item) {
|
---|
[14908] | 87 | Push(item);
|
---|
[14777] | 88 | }
|
---|
| 89 |
|
---|
| 90 | public void Clear() {
|
---|
[14908] | 91 | data.Clear();
|
---|
[14777] | 92 | }
|
---|
| 93 |
|
---|
| 94 | public bool Contains(T item) {
|
---|
[14908] | 95 | return data.Contains(item);
|
---|
[14777] | 96 | }
|
---|
| 97 |
|
---|
| 98 | public void CopyTo(T[] array, int arrayIndex) {
|
---|
[14908] | 99 | data.CopyTo(array, arrayIndex);
|
---|
[14777] | 100 | }
|
---|
| 101 |
|
---|
| 102 | public IEnumerator<T> GetEnumerator() {
|
---|
[14908] | 103 | return data.GetEnumerator();
|
---|
[14777] | 104 | }
|
---|
| 105 |
|
---|
| 106 | public T ElementAt(int index) {
|
---|
[14908] | 107 | return data.ElementAt(index);
|
---|
[14777] | 108 | }
|
---|
| 109 |
|
---|
| 110 | public T ReverseElementAt(int offset) {
|
---|
| 111 | return ElementAt(Count - 1 - offset);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | public void SetTop(T value) {
|
---|
[14908] | 115 | data.Last.Value = value;
|
---|
[14777] | 116 | }
|
---|
| 117 |
|
---|
| 118 | private LinkedListNode<T> GetByIndex(int index) {
|
---|
| 119 | var current = data.First;
|
---|
| 120 | for (var i = 0; i <= index; i++) {
|
---|
| 121 | current = current.Next;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | return current;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | public T this[int key]
|
---|
| 128 | {
|
---|
| 129 | get
|
---|
| 130 | {
|
---|
| 131 | return ElementAt(key);
|
---|
| 132 | }
|
---|
| 133 | set
|
---|
| 134 | {
|
---|
| 135 | GetByIndex(key).Value = value;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | public void Swap(int count) {
|
---|
[14908] | 140 | var topValue = Top;
|
---|
| 141 | var current = data.Last;
|
---|
| 142 | var bottomIndex = Count - count;
|
---|
[14777] | 143 |
|
---|
| 144 |
|
---|
[14908] | 145 | for (var i = Count - 1; i > bottomIndex; i--) {
|
---|
[14777] | 146 | current.Value = current.Previous.Value;
|
---|
| 147 | current = current.Previous;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[14908] | 150 | data.Last.Value = topValue;
|
---|
[14777] | 151 | }
|
---|
| 152 |
|
---|
| 153 | public void Yank(int index) {
|
---|
[14908] | 154 | if (index == Count - 1) return;
|
---|
[14777] | 155 |
|
---|
[14908] | 156 | var item = GetByIndex(index);
|
---|
| 157 | data.Remove(item);
|
---|
| 158 | data.AddLast(item);
|
---|
[14777] | 159 | }
|
---|
| 160 |
|
---|
| 161 | public T Pop() {
|
---|
| 162 | var value = Top;
|
---|
[14908] | 163 | data.RemoveLast();
|
---|
[14777] | 164 |
|
---|
| 165 | return value;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | public T[] Pop(int count) {
|
---|
| 169 | var items = new T[count];
|
---|
| 170 |
|
---|
| 171 | for (var i = 0; i < count; i++) {
|
---|
| 172 | items[count - i] = Top;
|
---|
| 173 | data.RemoveLast();
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | return items;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | public T Peek() {
|
---|
[14908] | 180 | return Top;
|
---|
[14777] | 181 | }
|
---|
| 182 |
|
---|
| 183 | public T[] Peek(int count) {
|
---|
| 184 | var items = new T[count];
|
---|
[14908] | 185 | var current = data.Last;
|
---|
[14777] | 186 |
|
---|
| 187 | for (var i = 0; i < count; i++) {
|
---|
| 188 | items[count - i] = current.Value;
|
---|
| 189 | current = current.Previous;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | return items;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | public void Push(T item) {
|
---|
[14908] | 196 | if (!IsEnabled) return;
|
---|
[14777] | 197 |
|
---|
[14908] | 198 | data.AddLast(item);
|
---|
[14777] | 199 | }
|
---|
| 200 |
|
---|
| 201 | public void Push(T item1, T item2) {
|
---|
[14908] | 202 | if (!IsEnabled) return;
|
---|
[14777] | 203 |
|
---|
[14908] | 204 | data.AddLast(item1);
|
---|
| 205 | data.AddLast(item2);
|
---|
[14777] | 206 | }
|
---|
| 207 |
|
---|
| 208 | public void Push(T item1, T item2, T item3) {
|
---|
[14908] | 209 | if (!IsEnabled) return;
|
---|
[14777] | 210 |
|
---|
[14908] | 211 | data.AddLast(item1);
|
---|
| 212 | data.AddLast(item2);
|
---|
| 213 | data.AddLast(item3);
|
---|
[14777] | 214 | }
|
---|
| 215 |
|
---|
| 216 | public void Push(T item1, T item2, T item3, T item4) {
|
---|
[14908] | 217 | if (!IsEnabled) return;
|
---|
[14777] | 218 |
|
---|
[14908] | 219 | data.AddLast(item1);
|
---|
| 220 | data.AddLast(item2);
|
---|
| 221 | data.AddLast(item3);
|
---|
| 222 | data.AddLast(item4);
|
---|
[14777] | 223 | }
|
---|
| 224 |
|
---|
| 225 | public void PushResult(int count, Func<T[], T> templateFunc) {
|
---|
[14908] | 226 | if (!IsEnabled) return;
|
---|
[14777] | 227 |
|
---|
| 228 | Push(templateFunc(Pop(count)));
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | public void PushRange(IReadOnlyList<T> items) {
|
---|
[14908] | 232 | if (!IsEnabled) return;
|
---|
[14777] | 233 |
|
---|
[14908] | 234 | for (var i = 0; i < items.Count; i++) data.AddLast(items[i]);
|
---|
[14777] | 235 | }
|
---|
| 236 |
|
---|
| 237 | public void Insert(int index, T value) {
|
---|
[14908] | 238 | if (!IsEnabled) return;
|
---|
[14777] | 239 |
|
---|
| 240 | var node = GetByIndex(index);
|
---|
[14908] | 241 | data.AddBefore(node, value);
|
---|
[14777] | 242 | }
|
---|
| 243 |
|
---|
| 244 | public bool Remove(T item) {
|
---|
[14908] | 245 | return data.Remove(item);
|
---|
[14777] | 246 | }
|
---|
| 247 |
|
---|
| 248 | public void RemoveTop() {
|
---|
[14908] | 249 | data.RemoveLast();
|
---|
[14777] | 250 | }
|
---|
| 251 |
|
---|
| 252 | public void Remove(int count) {
|
---|
[14908] | 253 | for (var i = 0; i < count; i++) data.RemoveLast();
|
---|
[14777] | 254 | }
|
---|
| 255 |
|
---|
| 256 | public void RemoveAt(int index) {
|
---|
| 257 | var node = GetByIndex(index);
|
---|
| 258 |
|
---|
[14908] | 259 | data.Remove(node);
|
---|
[14777] | 260 | }
|
---|
| 261 |
|
---|
| 262 | public void RemoveAt(int index, int count) {
|
---|
| 263 |
|
---|
| 264 | var node = GetByIndex(index);
|
---|
| 265 |
|
---|
| 266 | for (var i = 0; i < count; i++) {
|
---|
| 267 | data.Remove(node);
|
---|
| 268 | node = node.Next;
|
---|
| 269 | }
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | public bool TryPop(out T item) {
|
---|
[14908] | 273 | if (Count > 0) {
|
---|
| 274 | item = Pop();
|
---|
[14777] | 275 | return true;
|
---|
| 276 | }
|
---|
| 277 | item = default(T);
|
---|
| 278 | return false;
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | public override string ToString() {
|
---|
| 282 | var data = this.data as IEnumerable<T>;
|
---|
| 283 | return string.Join(Delimiter, data.Reverse());
|
---|
| 284 | }
|
---|
| 285 | }
|
---|
| 286 | } |
---|