PSCF v1.1
ArrayStack.h
1#ifndef UTIL_ARRAY_STACK_H
2#define UTIL_ARRAY_STACK_H
3
4/*
5* Util Package - C++ Utilities for Scientific Computation
6*
7* Copyright 2010 - 2017, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/misc/Memory.h>
12#include <util/global.h>
13
14namespace Util
15{
16
28 template <typename Data>
30 {
31
32 public:
33
37 ArrayStack();
38
42 virtual ~ArrayStack();
43
49 void allocate(int capacity);
50
52
53
59 void push(Data& data);
60
66 Data& pop();
67
69
71
77 int capacity() const;
78
82 int size() const;
83
87 Data& peek();
88
92 const Data& peek() const;
93
97 bool isValid() const;
98
102 bool isAllocated() const;
103
105
106 private:
107
109 Data **ptrs_;
110
112 int capacity_;
113
115 int size_;
116
118 ArrayStack(ArrayStack const &);
119
121 ArrayStack& operator = (ArrayStack const &);
122
123 };
124
125
126 /*
127 * Default constructor.
128 */
129 template <typename Data>
131 : ptrs_(0),
132 capacity_(0),
133 size_(0)
134 {}
135
136 /*
137 * Destructor.
138 */
139 template <typename Data>
141 {
142 if (ptrs_) {
143 Memory::deallocate<Data*>(ptrs_, capacity_);
144 }
145 }
146
147 /*
148 * Allocate and initialize required memory.
149 */
150 template <typename Data>
151 void ArrayStack<Data>::allocate(int capacity)
152 {
153 if (capacity <=0) {
154 UTIL_THROW("Cannot allocate ArrayStack with capacity <= 0");
155 }
156 if (ptrs_) {
157 UTIL_THROW("Attempt to re-allocate an ArrayStack");
158 }
159 Memory::allocate<Data*>(ptrs_, capacity);
160 capacity_ = capacity;
161 size_ = 0;
162
163 // Nullify all Data* pointer elements
164 for (int i = 0; i < capacity_; ++i) {
165 ptrs_[i] = 0;
166 }
167 }
168
169 /*
170 * Return physical capacity of the associated Array.
171 */
172 template <typename Data>
174 { return capacity_; }
175
176 /*
177 * Return the current logical size.
178 */
179 template <typename Data>
180 inline int ArrayStack<Data>::size() const
181 { return size_; }
182
183 /*
184 * Push a Data object onto the stack.
185 */
186 template <typename Data>
187 void ArrayStack<Data>::push(Data& data)
188 {
189 if (size_ == capacity_) {
190 UTIL_THROW("Attempt to push onto full stack");
191 }
192 ptrs_[size_] = &data;
193 ++size_;
194 }
195
196 /*
197 * Pop a Data object off the stack.
198 */
199 template <typename Data>
201 {
202 if (size_ == 0) {
203 UTIL_THROW("Attempt to pop from empty stack");
204 }
205 Data *ptr = ptrs_[size_-1];
206 ptrs_[size_-1] = 0;
207 --size_;
208 return *ptr;
209 }
210
211 /*
212 * Return a reference to the top element, without popping.
213 */
214 template <typename Data>
216 { return *ptrs_[size_-1]; }
217
218 /*
219 * Return a const reference to the top element, without popping.
220 */
221 template <typename Data>
222 inline const Data& ArrayStack<Data>::peek() const
223 { return *ptrs_[size_-1]; }
224
225 /*
226 * Return true if valid, or throw an exception.
227 */
228 template <typename Data>
230 {
231 if (capacity_ < 0) {
232 UTIL_THROW("Negative capacity_");
233 }
234 if (size_ < 0) {
235 UTIL_THROW("Negative size_");
236 }
237 if (size_ > capacity_) {
238 UTIL_THROW("size_ > capacity_");
239 }
240
241 if (ptrs_ != 0) {
242 int i;
243 for (i = 0; i < size_ ; ++i) {
244 if (ptrs_[i] == 0) {
245 UTIL_THROW("Null ptrs_[i] for i < size_");
246 }
247 }
248 for (i = size_; i < capacity_ ; ++i) {
249 if (ptrs_[i] != 0) {
250 UTIL_THROW("Non-null ptrs_[i] for i >= size_");
251 }
252 }
253 } else {
254 if (capacity_ != 0) {
255 UTIL_THROW("Unallocated stack but capacity != 0");
256 }
257 }
258 return true;
259 }
260
261 /*
262 * Return true if the ArrayStack has been allocated, false otherwise.
263 */
264 template <typename Data>
266 { return (ptrs_ != 0); }
267
268}
269#endif
A stack of fixed capacity, which stores pointers to elements.
Definition: ArrayStack.h:30
bool isAllocated() const
Return true only if the ArrayStack has been allocated.
Definition: ArrayStack.h:265
Data & pop()
Pop an element off the stack.
Definition: ArrayStack.h:200
ArrayStack()
Default constructor.
Definition: ArrayStack.h:130
bool isValid() const
Return true if the ArrayStack is valid, or throw an exception.
Definition: ArrayStack.h:229
int size() const
Get the number of elements in the stack.
Definition: ArrayStack.h:180
virtual ~ArrayStack()
Destructor.
Definition: ArrayStack.h:140
Data & peek()
Return a reference to the top element (don't pop).
Definition: ArrayStack.h:215
void push(Data &data)
Push an element onto the Stack.
Definition: ArrayStack.h:187
int capacity() const
Return capacity of the underlying array.
Definition: ArrayStack.h:173
void allocate(int capacity)
Initialize and allocate required memory.
Definition: ArrayStack.h:151
File containing preprocessor macros for error handling.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
Utility classes for scientific computation.
Definition: accumulators.mod:1