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