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