Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Stack/PushGPStack.cs @ 14328

Last change on this file since 14328 was 14328, checked in by pkimmesw, 8 years ago

#2665 Set .NET version to 4.5, C# version to 5.0, Added expression templates and factory

File size: 4.7 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Text;
5
6namespace HeuristicLab.Algorithms.PushGP.Stack
7{
8    /// <summary>
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
11    /// to "deep" stack elements by means of integer indices. To this extent the stacks can be used as general, random access memory
12    /// classures. This is one of the features that ensures the Turing-completeness of Push (another being the arbitrary name/value
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    {
18        private readonly List<T> data;
19        private const string delimiter = " ";
20
21        public PushGPStack(int capacity = 0)
22        {
23            this.data = new List<T>();
24        }
25
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
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        {
71            var value = this.data[Count - 1];
72            this.data.RemoveAt(Count - 1);
73
74            return value;
75        }
76
77        public T[] Pop(int count)
78        {
79            var startIndex = Count - count;
80            var items = this.data.GetRange(startIndex, count);
81
82            this.data.RemoveRange(startIndex, count);
83
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
90            return items.ToArray();
91        }
92
93        public void Push(T item)
94        {
95            this.data.Add(item);
96        }
97
98        public void Push(params T[] items)
99        {
100            foreach (var item in items)
101            {
102                this.data.Add(item);
103            }
104        }
105
106        public void Push(IEnumerable<T> items)
107        {
108            this.data.AddRange(items);
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        {
118            this.data.InsertRange(index, items);
119        }
120
121        public void Insert(int index, IEnumerable<T> items)
122        {
123            this.data.InsertRange(index, items);
124        }
125
126        public bool Remove(T item)
127        {
128            var index = Count - 1;
129            if (this.Count > 0 && this.data[index].Equals(item))
130            {
131                this.data.RemoveAt(index);
132                return true;
133            }
134            else
135            {
136                return false;
137            }
138        }
139
140        public bool TryPop(out T item)
141        {
142            if (this.Count > 0)
143            {
144                item = this.Pop();
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        {
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();
176        }
177    }
178}
Note: See TracBrowser for help on using the repository browser.