Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2665 Renamings due to typos, ManagedPool tests, Skip Noops in Debugger

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