1 | using System.Text;
|
---|
2 |
|
---|
3 |
|
---|
4 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Stack {
|
---|
5 | using System.Collections;
|
---|
6 | using System.Collections.Generic;
|
---|
7 |
|
---|
8 | using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions;
|
---|
9 |
|
---|
10 | /// <summary>
|
---|
11 | /// Optimized for string concatenation to reduce memory effort
|
---|
12 | /// </summary>
|
---|
13 | public class PrintStack : IPrintStack {
|
---|
14 | private string currentTop;
|
---|
15 | private readonly IPushStack<string> stack = new PushStack<string>();
|
---|
16 | private readonly StringBuilder stringBuilder = new StringBuilder();
|
---|
17 |
|
---|
18 | private string CurrentTop
|
---|
19 | {
|
---|
20 | get
|
---|
21 | {
|
---|
22 | return currentTop ?? (currentTop = stringBuilder.ToString());
|
---|
23 | }
|
---|
24 | }
|
---|
25 |
|
---|
26 | public IEnumerator<string> GetEnumerator() {
|
---|
27 | return stack.GetEnumerator();
|
---|
28 | }
|
---|
29 |
|
---|
30 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
31 | return GetEnumerator();
|
---|
32 | }
|
---|
33 |
|
---|
34 | public void Add(string item) {
|
---|
35 | Push(item);
|
---|
36 | }
|
---|
37 |
|
---|
38 | void IPushStack<string>.Clear() {
|
---|
39 | stack.Clear();
|
---|
40 | stringBuilder.Clear();
|
---|
41 | currentTop = null;
|
---|
42 | }
|
---|
43 |
|
---|
44 | public IReadOnlyList<string> Peek(int count) {
|
---|
45 | if (count == 1)
|
---|
46 | return new[] { CurrentTop };
|
---|
47 |
|
---|
48 | var peek = stack.Peek(count - 1);
|
---|
49 | var result = new List<string>(peek) { CurrentTop };
|
---|
50 |
|
---|
51 | return result;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public void Insert(int index, string item) {
|
---|
55 | if (index == 0)
|
---|
56 | Add(item);
|
---|
57 |
|
---|
58 | stack.Insert(index - 1, item);
|
---|
59 | }
|
---|
60 |
|
---|
61 | public string Pop() {
|
---|
62 | var result = CurrentTop;
|
---|
63 |
|
---|
64 | var top = stack.Pop();
|
---|
65 | SetBuilderToTop(top);
|
---|
66 |
|
---|
67 | return result;
|
---|
68 | }
|
---|
69 |
|
---|
70 | public IReadOnlyList<string> Pop(int count) {
|
---|
71 | if (count == 1)
|
---|
72 | return new[] { Pop() };
|
---|
73 |
|
---|
74 | var popped = stack.Pop(count - 1);
|
---|
75 | var result = new List<string>(popped) { CurrentTop };
|
---|
76 |
|
---|
77 | var newTop = stack.Pop();
|
---|
78 | SetBuilderToTop(newTop);
|
---|
79 |
|
---|
80 | return result;
|
---|
81 | }
|
---|
82 |
|
---|
83 | private void ClearTop() {
|
---|
84 | stringBuilder.Clear();
|
---|
85 | currentTop = null;
|
---|
86 | }
|
---|
87 |
|
---|
88 | private void SetBuilderToTop(string top) {
|
---|
89 | stringBuilder.Clear();
|
---|
90 | stringBuilder.Append(top);
|
---|
91 | currentTop = top;
|
---|
92 | }
|
---|
93 |
|
---|
94 | public bool TryPop(out string item) {
|
---|
95 | if (string.IsNullOrEmpty(CurrentTop)) {
|
---|
96 | item = null;
|
---|
97 | return false;
|
---|
98 | }
|
---|
99 |
|
---|
100 | item = Pop();
|
---|
101 | return true;
|
---|
102 | }
|
---|
103 |
|
---|
104 | public string ElementAt(int index) {
|
---|
105 | if (index == 0)
|
---|
106 | return CurrentTop;
|
---|
107 |
|
---|
108 | return stack.ElementAt(index - 1);
|
---|
109 | }
|
---|
110 |
|
---|
111 | public string Top
|
---|
112 | {
|
---|
113 | get
|
---|
114 | {
|
---|
115 | return CurrentTop;
|
---|
116 | }
|
---|
117 | set
|
---|
118 | {
|
---|
119 | SetBuilderToTop(value);
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | public string TopOrDefault { get { return CurrentTop; } }
|
---|
124 |
|
---|
125 | public string Bottom { get { return Count > 1 ? stack.Bottom : CurrentTop; } }
|
---|
126 |
|
---|
127 | public string BottomOrDefault { get { return Bottom; } }
|
---|
128 |
|
---|
129 | public string this[int key]
|
---|
130 | {
|
---|
131 | get
|
---|
132 | {
|
---|
133 | return key == 0 ? CurrentTop : stack[key - 1];
|
---|
134 | }
|
---|
135 | set
|
---|
136 | {
|
---|
137 | if (key == 0)
|
---|
138 | SetBuilderToTop(value);
|
---|
139 | else
|
---|
140 | stack[key - 1] = value;
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | public int Count
|
---|
145 | {
|
---|
146 | get
|
---|
147 | {
|
---|
148 | return stack.Count + (string.IsNullOrEmpty(CurrentTop) ? 0 : 1);
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | public void NewLine() {
|
---|
153 | if (IsEmpty) return;
|
---|
154 |
|
---|
155 | stack.Push(CurrentTop);
|
---|
156 | ClearTop();
|
---|
157 | }
|
---|
158 |
|
---|
159 | public void Push<T>(T item) {
|
---|
160 | stringBuilder.Append(item);
|
---|
161 | currentTop = null;
|
---|
162 | }
|
---|
163 |
|
---|
164 | public void Push(float item) {
|
---|
165 | stringBuilder.Concat(item);
|
---|
166 | currentTop = null;
|
---|
167 | }
|
---|
168 |
|
---|
169 | public void Push(double item) {
|
---|
170 | stringBuilder.Concat(item);
|
---|
171 | currentTop = null;
|
---|
172 | }
|
---|
173 |
|
---|
174 | public void Push(long item) {
|
---|
175 | stringBuilder.Concat(item);
|
---|
176 | currentTop = null;
|
---|
177 | }
|
---|
178 |
|
---|
179 | public void Push(int item) {
|
---|
180 | stringBuilder.Concat(item);
|
---|
181 | currentTop = null;
|
---|
182 | }
|
---|
183 |
|
---|
184 | public void Push(char item) {
|
---|
185 | stringBuilder.Append(item);
|
---|
186 | currentTop = null;
|
---|
187 | }
|
---|
188 |
|
---|
189 | public void Push(bool item) {
|
---|
190 | stringBuilder.Append(item);
|
---|
191 | currentTop = null;
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | public void Push(string item) {
|
---|
196 | stringBuilder.Append(item);
|
---|
197 | currentTop = null;
|
---|
198 | }
|
---|
199 |
|
---|
200 | public void Push(string item1, string item2) {
|
---|
201 | stringBuilder.Append(item1);
|
---|
202 | stringBuilder.Append(item2);
|
---|
203 | currentTop = null;
|
---|
204 | }
|
---|
205 |
|
---|
206 | public void Push(string item1, string item2, string item3) {
|
---|
207 | stringBuilder.Append(item1);
|
---|
208 | stringBuilder.Append(item2);
|
---|
209 | stringBuilder.Append(item3);
|
---|
210 | currentTop = null;
|
---|
211 | }
|
---|
212 |
|
---|
213 | public void Push(string item1, string item2, string item3, string item4) {
|
---|
214 | stringBuilder.Append(item1);
|
---|
215 | stringBuilder.Append(item2);
|
---|
216 | stringBuilder.Append(item3);
|
---|
217 | stringBuilder.Append(item4);
|
---|
218 | currentTop = null;
|
---|
219 | }
|
---|
220 |
|
---|
221 | public void Push(IReadOnlyList<string> items, int startIndex) {
|
---|
222 | for (var i = startIndex; i < items.Count; i++)
|
---|
223 | stringBuilder.Append(items[i]);
|
---|
224 |
|
---|
225 | currentTop = null;
|
---|
226 | }
|
---|
227 |
|
---|
228 | public void Push(IReadOnlyList<string> items) {
|
---|
229 | Push(items, 0);
|
---|
230 | }
|
---|
231 |
|
---|
232 | public void Push(IEnumerable<string> items) {
|
---|
233 | foreach (var item in items)
|
---|
234 | stringBuilder.Append(item);
|
---|
235 |
|
---|
236 | currentTop = null;
|
---|
237 | }
|
---|
238 |
|
---|
239 | void IPushStack.Clear() {
|
---|
240 | stringBuilder.Clear();
|
---|
241 | currentTop = null;
|
---|
242 | stack.Clear();
|
---|
243 | }
|
---|
244 |
|
---|
245 | public void Swap(int count) {
|
---|
246 | stack.Push(CurrentTop);
|
---|
247 | stack.Swap(count);
|
---|
248 |
|
---|
249 | var newTop = stack.Pop();
|
---|
250 | SetBuilderToTop(newTop);
|
---|
251 | }
|
---|
252 |
|
---|
253 | public void Yank(int index) {
|
---|
254 | if (index == Count - 1) return;
|
---|
255 |
|
---|
256 | stack.Push(CurrentTop);
|
---|
257 |
|
---|
258 | var item = stack[index];
|
---|
259 | stack.RemoveAt(index);
|
---|
260 | SetBuilderToTop(item);
|
---|
261 | }
|
---|
262 |
|
---|
263 | public void RemoveTop() {
|
---|
264 | var newTop = stack.Pop();
|
---|
265 | SetBuilderToTop(newTop);
|
---|
266 | }
|
---|
267 |
|
---|
268 | public void Remove(int count) {
|
---|
269 | if (count == 1)
|
---|
270 | RemoveTop();
|
---|
271 |
|
---|
272 | stack.Remove(count - 1);
|
---|
273 | var newTop = stack.Pop();
|
---|
274 | SetBuilderToTop(newTop);
|
---|
275 | }
|
---|
276 |
|
---|
277 | public void RemoveAt(int index) {
|
---|
278 | if (index == 0)
|
---|
279 | ClearTop();
|
---|
280 | else
|
---|
281 | stack.RemoveAt(index - 1);
|
---|
282 | }
|
---|
283 |
|
---|
284 | public void RemoveAt(int index, int count) {
|
---|
285 | if (index == 0)
|
---|
286 | count--;
|
---|
287 |
|
---|
288 | stack.RemoveAt(index - 1, count);
|
---|
289 |
|
---|
290 | if (index == 0) {
|
---|
291 | var newTop = stack.Pop();
|
---|
292 | SetBuilderToTop(newTop);
|
---|
293 | }
|
---|
294 | }
|
---|
295 |
|
---|
296 | public IEnumerable<string> AsStrings() {
|
---|
297 | foreach (var item in stack)
|
---|
298 | yield return item;
|
---|
299 |
|
---|
300 | if (!IsEmpty)
|
---|
301 | yield return CurrentTop;
|
---|
302 | }
|
---|
303 |
|
---|
304 | public IEnumerable<object> AsObjects() {
|
---|
305 | return AsStrings();
|
---|
306 | }
|
---|
307 |
|
---|
308 | void ICollection<string>.Clear() {
|
---|
309 | stack.Clear();
|
---|
310 | stringBuilder.Clear();
|
---|
311 | }
|
---|
312 |
|
---|
313 | public bool Contains(string item) {
|
---|
314 |
|
---|
315 | return CurrentTop.Equals(item) || stack.Contains(item);
|
---|
316 | }
|
---|
317 |
|
---|
318 | public void CopyTo(string[] array, int arrayIndex) {
|
---|
319 | stack.CopyTo(array, arrayIndex);
|
---|
320 | array[arrayIndex + stack.Count] = CurrentTop;
|
---|
321 | }
|
---|
322 |
|
---|
323 | public bool Remove(string item) {
|
---|
324 | if (item.Equals(CurrentTop)) {
|
---|
325 | RemoveTop();
|
---|
326 | return true;
|
---|
327 | }
|
---|
328 |
|
---|
329 | return stack.Remove(item);
|
---|
330 | }
|
---|
331 |
|
---|
332 | int IPushStack<string>.Count
|
---|
333 | {
|
---|
334 | get
|
---|
335 | {
|
---|
336 | return Count;
|
---|
337 | }
|
---|
338 | }
|
---|
339 |
|
---|
340 | public bool IsEmpty { get { return stack.IsEmpty && string.IsNullOrEmpty(CurrentTop); } }
|
---|
341 |
|
---|
342 | public bool IsEnabled { get; set; }
|
---|
343 |
|
---|
344 | int ICollection<string>.Count
|
---|
345 | {
|
---|
346 | get
|
---|
347 | {
|
---|
348 | return Count;
|
---|
349 | }
|
---|
350 | }
|
---|
351 |
|
---|
352 | public bool IsReadOnly { get { return false; } }
|
---|
353 | }
|
---|
354 | }
|
---|