1 | using System;
|
---|
2 | using System.Collections;
|
---|
3 | using System.Collections.Generic;
|
---|
4 |
|
---|
5 | namespace HeuristicLab.Algorithms.PushGP.Stack
|
---|
6 | {
|
---|
7 | /// <summary>
|
---|
8 | /// While Push's stacks are generally treated as genuine stacks---that is, instructions take their arguments from the tops of
|
---|
9 | /// the stacks and push their results onto the tops of the stacks---a few instructions (like YANK and SHOVE) do allow direct access
|
---|
10 | /// to "deep" stack elements by means of integer indices. To this extent the stacks can be used as general, random access memory
|
---|
11 | /// structures. This is one of the features that ensures the Turing-completeness of Push (another being the arbitrary name/value
|
---|
12 | /// bindings supported by the NAME data type and DEFINE methods; see below).
|
---|
13 | /// </summary>
|
---|
14 | /// <typeparam name="T">The item type of the collection.</typeparam>
|
---|
15 | public class PushGPStack<T> : IStack<T>
|
---|
16 | {
|
---|
17 | private List<T> data = new List<T>();
|
---|
18 |
|
---|
19 | public T Top => this.data[Count - 1];
|
---|
20 | public int Count => data.Count;
|
---|
21 | public bool IsEmpty => this.Count == 0;
|
---|
22 |
|
---|
23 | public bool IsReadOnly
|
---|
24 | {
|
---|
25 | get
|
---|
26 | {
|
---|
27 | throw new NotImplementedException();
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 | public void Add(T item)
|
---|
32 | {
|
---|
33 | this.Push(item);
|
---|
34 | }
|
---|
35 |
|
---|
36 | public void Clear()
|
---|
37 | {
|
---|
38 | this.data.Clear();
|
---|
39 | }
|
---|
40 |
|
---|
41 | public bool Contains(T item)
|
---|
42 | {
|
---|
43 | return this.data.Contains(item);
|
---|
44 | }
|
---|
45 |
|
---|
46 | public void CopyTo(T[] array, int arrayIndex)
|
---|
47 | {
|
---|
48 | this.data.CopyTo(array, arrayIndex);
|
---|
49 | }
|
---|
50 |
|
---|
51 | public IEnumerator<T> GetEnumerator()
|
---|
52 | {
|
---|
53 | return this.data.GetEnumerator();
|
---|
54 | }
|
---|
55 |
|
---|
56 | public T ElementAt(int index)
|
---|
57 | {
|
---|
58 | return this.data[index];
|
---|
59 | }
|
---|
60 |
|
---|
61 | public T Pop()
|
---|
62 | {
|
---|
63 | var index = Count - 1;
|
---|
64 | var value = this.data[index];
|
---|
65 | this.data.RemoveAt(index);
|
---|
66 |
|
---|
67 | return value;
|
---|
68 | }
|
---|
69 |
|
---|
70 | public T[] Pop(int count)
|
---|
71 | {
|
---|
72 | var startIndex = Count - count;
|
---|
73 | var items = this.data.GetRange(startIndex, count);
|
---|
74 |
|
---|
75 | this.data.RemoveRange(startIndex, count);
|
---|
76 |
|
---|
77 | items.Reverse();
|
---|
78 | return items.ToArray();
|
---|
79 | }
|
---|
80 |
|
---|
81 | public void Push(T item)
|
---|
82 | {
|
---|
83 | this.data.Add(item);
|
---|
84 | }
|
---|
85 |
|
---|
86 | public void Push(params T[] items)
|
---|
87 | {
|
---|
88 | this.data.AddRange(items);
|
---|
89 | }
|
---|
90 |
|
---|
91 | public void Push(IEnumerable<T> items)
|
---|
92 | {
|
---|
93 | this.data.AddRange(items);
|
---|
94 | }
|
---|
95 |
|
---|
96 | public void Insert(int index, T item)
|
---|
97 | {
|
---|
98 | this.data.Insert(index, item);
|
---|
99 | }
|
---|
100 |
|
---|
101 | public void Insert(int index, params T[] items)
|
---|
102 | {
|
---|
103 | this.data.InsertRange(index, items);
|
---|
104 | }
|
---|
105 |
|
---|
106 | public void Insert(int index, IEnumerable<T> items)
|
---|
107 | {
|
---|
108 | this.data.InsertRange(index, items);
|
---|
109 | }
|
---|
110 |
|
---|
111 | public bool Remove(T item)
|
---|
112 | {
|
---|
113 | var index = Count - 1;
|
---|
114 | if (this.data.Count > 0 && this.data[index].Equals(item))
|
---|
115 | {
|
---|
116 | this.data.RemoveAt(index);
|
---|
117 | return true;
|
---|
118 | }
|
---|
119 | else
|
---|
120 | {
|
---|
121 | return false;
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | public bool TryPop(out T item)
|
---|
126 | {
|
---|
127 | if (this.data.Count > 0)
|
---|
128 | {
|
---|
129 | item = this.Pop();
|
---|
130 | return true;
|
---|
131 | }
|
---|
132 | else
|
---|
133 | {
|
---|
134 | item = default(T);
|
---|
135 | return false;
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | IEnumerator IEnumerable.GetEnumerator()
|
---|
140 | {
|
---|
141 | return (this.data as IEnumerable).GetEnumerator();
|
---|
142 | }
|
---|
143 |
|
---|
144 | public override string ToString()
|
---|
145 | {
|
---|
146 | return string.Join(", ", this.data);
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|