Nebula
Loading...
Searching...
No Matches
stack.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
12#include "core/types.h"
13#include "util/array.h"
14
15//------------------------------------------------------------------------------
16namespace Util
17{
18template<class TYPE> class Stack
19{
20public:
24 Stack(const Stack<TYPE>& rhs);
25
27 void operator=(const Stack<TYPE>& rhs);
29 TYPE& operator[](IndexT index) const;
31 bool operator==(const Stack<TYPE>& rhs) const;
33 bool operator!=(const Stack<TYPE>& rhs) const;
35 SizeT Size() const;
37 bool IsEmpty() const;
39 void Clear();
41 bool Contains(const TYPE& e) const;
43 void EraseIndex(const IndexT i);
44
46 void Push(const TYPE& e);
48 TYPE& Peek() const;
50 TYPE Pop();
51
52private:
54};
55
56//------------------------------------------------------------------------------
59template<class TYPE>
61{
62 // empty
63}
64
65//------------------------------------------------------------------------------
68template<class TYPE>
70{
71 this->stackArray = rhs.stackArray;
72}
73
74//------------------------------------------------------------------------------
77template<class TYPE>
78void
80{
81 this->stackArray = rhs.stackArray;
82}
83
84//------------------------------------------------------------------------------
87template<class TYPE>
88TYPE&
90{
91 return this->stackArray[this->stackArray.Size() - 1 - index];
92}
93
94//------------------------------------------------------------------------------
97template<class TYPE>
98bool
100{
101 return this->stackArray == rhs.stackArray;
102}
103
104//------------------------------------------------------------------------------
107template<class TYPE>
108bool
110{
111 return this->stackArray != rhs.stackArray;
112}
113
114//------------------------------------------------------------------------------
117template<class TYPE>
118bool
119Stack<TYPE>::Contains(const TYPE& e) const
120{
121 return (InvalidIndex != this->stackArray.FindIndex(e));
122}
123
124//------------------------------------------------------------------------------
127template<class TYPE>
128void
130{
131 this->stackArray.Clear();
132}
133
134//------------------------------------------------------------------------------
137template<class TYPE>
138SizeT
140{
141 return this->stackArray.Size();
142}
143
144//------------------------------------------------------------------------------
147template<class TYPE>
148bool
150{
151 return this->stackArray.IsEmpty();
152}
153
154//------------------------------------------------------------------------------
157template<class TYPE>
158void
160{
161 this->stackArray.EraseIndex(i);
162}
163
164//------------------------------------------------------------------------------
167template<class TYPE>
168void
169Stack<TYPE>::Push(const TYPE& e)
170{
171 this->stackArray.Append(e);
172}
173
174//------------------------------------------------------------------------------
177template<class TYPE>
178TYPE&
180{
181 return this->stackArray.Back();
182}
183
184//------------------------------------------------------------------------------
187template<class TYPE>
188TYPE
190{
191 TYPE e = this->stackArray.Back();
192 this->stackArray.EraseIndex(this->stackArray.Size() - 1);
193 return e;
194}
195
196
197} // namespace Util
198//------------------------------------------------------------------------------
Nebula's dynamic array class.
Definition array.h:60
Nebula's stack class (a FILO container).
Definition stack.h:19
Stack()
constructor
Definition stack.h:60
void Push(const TYPE &e)
push an element on the stack
Definition stack.h:169
TYPE Pop()
get topmost element of stack, remove element
Definition stack.h:189
bool Contains(const TYPE &e) const
return true if stack contains element
Definition stack.h:119
SizeT Size() const
returns number of elements on stack
Definition stack.h:139
void operator=(const Stack< TYPE > &rhs)
assignment operator
Definition stack.h:79
void EraseIndex(const IndexT i)
erase element at index
bool operator!=(const Stack< TYPE > &rhs) const
inequality operator
Definition stack.h:109
bool IsEmpty() const
returns true if stack is empty
Definition stack.h:149
void Clear()
remove all elements from the stack
Definition stack.h:129
TYPE & operator[](IndexT index) const
access element by index, 0 is the topmost element
Definition stack.h:89
TYPE & Peek() const
get reference of topmost element of stack, without removing it
Definition stack.h:179
Stack(const Stack< TYPE > &rhs)
copy constructor
Definition stack.h:69
Array< TYPE > stackArray
Definition stack.h:53
bool operator==(const Stack< TYPE > &rhs) const
equality operator
Definition stack.h:99
A pinned array is an array which manages its own virtual memory.
Definition String.cs:6
static const int InvalidIndex
Definition types.h:54
int SizeT
Definition types.h:49
int IndexT
Definition types.h:48