Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2665 Removed "this" qualifier

File size: 5.7 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[offset];
121    }
122
123    public void SetTop(T value) {
124      data[Count - 1] = value;
125    }
126
127    public T this[int index]
128    {
129      get { return data[Count - 1 - index]; }
130      set { data[Count - 1 - index] = value; }
131    }
132
133    public void Swap(int count) {
134      var top = Top;
135      var bottomIndex = Count - count;
136
137      for (var i = Count - 1; i > bottomIndex; i--)
138        data[i] = data[i - 1];
139
140      data[bottomIndex] = top;
141    }
142
143    public void Yank(int index) {
144      if (index == Count - 1) return;
145
146      var item = ReverseElementAt(index);
147      data.RemoveAt(index);
148      data.Add(item);
149    }
150
151    public T Pop() {
152      var top = Top;
153      data.RemoveAt(Count - 1);
154
155      return top;
156    }
157
158    public T[] Pop(int count) {
159      var items = Peek(count);
160      Remove(count);
161
162      return items;
163    }
164
165    public T Peek() {
166      return Top;
167    }
168
169    public T[] Peek(int count) {
170      var startIndex = Count - count;
171
172      var items = new T[count];
173      data.CopyTo(startIndex, items, 0, count);
174
175      return items;
176    }
177
178    public void Push(T item) {
179      if (!IsEnabled) return;
180
181      data.Add(item);
182    }
183
184    public void Push(T item1, T item2) {
185      if (!IsEnabled) return;
186
187      data.Add(item1);
188      data.Add(item2);
189    }
190
191    public void Push(T item1, T item2, T item3) {
192      if (!IsEnabled) return;
193
194      data.Add(item1);
195      data.Add(item2);
196      data.Add(item3);
197    }
198
199    public void Push(T item1, T item2, T item3, T item4) {
200      if (!IsEnabled) return;
201
202      data.Add(item1);
203      data.Add(item2);
204      data.Add(item3);
205      data.Add(item4);
206    }
207
208    public void PushResult(int count, Func<T[], T> templateFunc) {
209      if (!IsEnabled) return;
210
211      var startIndex = Count - count;
212      var items = new T[count];
213
214      data.CopyTo(startIndex, items, 0, count);
215      Remove(count - 1);
216
217      data[Count - 1] = templateFunc(items);
218    }
219
220    public void Push(IReadOnlyList<T> items, int startIndex) {
221      if (!IsEnabled) return;
222
223      for (var i = startIndex; i < items.Count; i++) data.Add(items[i]);
224    }
225
226    public void Push(IReadOnlyList<T> items) {
227      if (!IsEnabled) return;
228
229      data.AddRange(items);
230    }
231
232    public void Insert(int index, T item) {
233      if (!IsEnabled) return;
234
235      data.Insert(Count - index, item);
236    }
237
238    public bool Remove(T item) {
239      return data.Remove(item);
240    }
241
242    public void RemoveTop() {
243      data.RemoveAt(Count - 1);
244    }
245
246    public void Remove(int count) {
247      data.RemoveRange(Count - count, count);
248    }
249
250    public void RemoveAt(int index) {
251      data.RemoveAt(index);
252    }
253
254    public void RemoveAt(int index, int count) {
255      data.RemoveRange(index, count);
256    }
257
258    public bool TryPop(out T item) {
259      if (Count > 0) {
260        item = Pop();
261        return true;
262      }
263      item = default(T);
264      return false;
265    }
266
267    IEnumerator IEnumerable.GetEnumerator() {
268      return data.GetEnumerator();
269    }
270
271    public override string ToString() {
272      var data = this.data as IEnumerable<T>;
273      return string.Join(Delimiter, data.Reverse());
274    }
275  }
276}
Note: See TracBrowser for help on using the repository browser.