Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2665 LexicaseSelector, Performance improvements, UI Fixes, Debugger only shows used stacks, fixed Debugger stepping, Added vector expressions, ERCOptions,

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