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() {
|
---|
24 | data = new LinkedList<T>();
|
---|
25 | IsEnabled = true;
|
---|
26 | }
|
---|
27 |
|
---|
28 | public bool IsEnabled { get; set; }
|
---|
29 |
|
---|
30 | public T Top
|
---|
31 | {
|
---|
32 | get
|
---|
33 | {
|
---|
34 | return data.Last.Value;
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | public T TopOrDefault
|
---|
39 | {
|
---|
40 | get
|
---|
41 | {
|
---|
42 | return Count > 0 ? Top : default(T);
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public T Bottom
|
---|
47 | {
|
---|
48 | get
|
---|
49 | {
|
---|
50 | return data.First.Value;
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | public T BottomOrDefault
|
---|
55 | {
|
---|
56 | get
|
---|
57 | {
|
---|
58 | return Count > 0 ? Bottom : default(T);
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public int Count
|
---|
63 | {
|
---|
64 | get
|
---|
65 | {
|
---|
66 | return data.Count;
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | public bool IsEmpty
|
---|
71 | {
|
---|
72 | get
|
---|
73 | {
|
---|
74 | return Count == 0;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | public bool IsReadOnly
|
---|
79 | {
|
---|
80 | get
|
---|
81 | {
|
---|
82 | return false;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | public void Add(T item) {
|
---|
87 | Push(item);
|
---|
88 | }
|
---|
89 |
|
---|
90 | public void Clear() {
|
---|
91 | data.Clear();
|
---|
92 | }
|
---|
93 |
|
---|
94 | public bool Contains(T item) {
|
---|
95 | return data.Contains(item);
|
---|
96 | }
|
---|
97 |
|
---|
98 | public void CopyTo(T[] array, int arrayIndex) {
|
---|
99 | data.CopyTo(array, arrayIndex);
|
---|
100 | }
|
---|
101 |
|
---|
102 | public IEnumerator<T> GetEnumerator() {
|
---|
103 | return data.GetEnumerator();
|
---|
104 | }
|
---|
105 |
|
---|
106 | public T ElementAt(int index) {
|
---|
107 | return data.ElementAt(index);
|
---|
108 | }
|
---|
109 |
|
---|
110 | public T ReverseElementAt(int offset) {
|
---|
111 | return ElementAt(Count - 1 - offset);
|
---|
112 | }
|
---|
113 |
|
---|
114 | public void SetTop(T value) {
|
---|
115 | data.Last.Value = value;
|
---|
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) {
|
---|
140 | var topValue = Top;
|
---|
141 | var current = data.Last;
|
---|
142 | var bottomIndex = Count - count;
|
---|
143 |
|
---|
144 |
|
---|
145 | for (var i = Count - 1; i > bottomIndex; i--) {
|
---|
146 | current.Value = current.Previous.Value;
|
---|
147 | current = current.Previous;
|
---|
148 | }
|
---|
149 |
|
---|
150 | data.Last.Value = topValue;
|
---|
151 | }
|
---|
152 |
|
---|
153 | public void Yank(int index) {
|
---|
154 | if (index == Count - 1) return;
|
---|
155 |
|
---|
156 | var item = GetByIndex(index);
|
---|
157 | data.Remove(item);
|
---|
158 | data.AddLast(item);
|
---|
159 | }
|
---|
160 |
|
---|
161 | public T Pop() {
|
---|
162 | var value = Top;
|
---|
163 | data.RemoveLast();
|
---|
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() {
|
---|
180 | return Top;
|
---|
181 | }
|
---|
182 |
|
---|
183 | public T[] Peek(int count) {
|
---|
184 | var items = new T[count];
|
---|
185 | var current = data.Last;
|
---|
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) {
|
---|
196 | if (!IsEnabled) return;
|
---|
197 |
|
---|
198 | data.AddLast(item);
|
---|
199 | }
|
---|
200 |
|
---|
201 | public void Push(T item1, T item2) {
|
---|
202 | if (!IsEnabled) return;
|
---|
203 |
|
---|
204 | data.AddLast(item1);
|
---|
205 | data.AddLast(item2);
|
---|
206 | }
|
---|
207 |
|
---|
208 | public void Push(T item1, T item2, T item3) {
|
---|
209 | if (!IsEnabled) return;
|
---|
210 |
|
---|
211 | data.AddLast(item1);
|
---|
212 | data.AddLast(item2);
|
---|
213 | data.AddLast(item3);
|
---|
214 | }
|
---|
215 |
|
---|
216 | public void Push(T item1, T item2, T item3, T item4) {
|
---|
217 | if (!IsEnabled) return;
|
---|
218 |
|
---|
219 | data.AddLast(item1);
|
---|
220 | data.AddLast(item2);
|
---|
221 | data.AddLast(item3);
|
---|
222 | data.AddLast(item4);
|
---|
223 | }
|
---|
224 |
|
---|
225 | public void PushResult(int count, Func<T[], T> templateFunc) {
|
---|
226 | if (!IsEnabled) return;
|
---|
227 |
|
---|
228 | Push(templateFunc(Pop(count)));
|
---|
229 | }
|
---|
230 |
|
---|
231 | public void PushRange(IReadOnlyList<T> items) {
|
---|
232 | if (!IsEnabled) return;
|
---|
233 |
|
---|
234 | for (var i = 0; i < items.Count; i++) data.AddLast(items[i]);
|
---|
235 | }
|
---|
236 |
|
---|
237 | public void Insert(int index, T value) {
|
---|
238 | if (!IsEnabled) return;
|
---|
239 |
|
---|
240 | var node = GetByIndex(index);
|
---|
241 | data.AddBefore(node, value);
|
---|
242 | }
|
---|
243 |
|
---|
244 | public bool Remove(T item) {
|
---|
245 | return data.Remove(item);
|
---|
246 | }
|
---|
247 |
|
---|
248 | public void RemoveTop() {
|
---|
249 | data.RemoveLast();
|
---|
250 | }
|
---|
251 |
|
---|
252 | public void Remove(int count) {
|
---|
253 | for (var i = 0; i < count; i++) data.RemoveLast();
|
---|
254 | }
|
---|
255 |
|
---|
256 | public void RemoveAt(int index) {
|
---|
257 | var node = GetByIndex(index);
|
---|
258 |
|
---|
259 | data.Remove(node);
|
---|
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) {
|
---|
273 | if (Count > 0) {
|
---|
274 | item = Pop();
|
---|
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 | } |
---|