Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Stack/PushStack.cs @ 17839

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

#2665 Fixed small issues, testet benchmark suite, added INX Expressions

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