Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Stack/PushStack.cs @ 15017

Last change on this file since 15017 was 15017, checked in by pkimmesw, 7 years ago

#2665 Fixed Benchmark Problem Definition, Converted LoopExpressions to stateless expressions, Added several unit test to ensure funcionality, Fixed UI bugs

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