1#ifndef UTIL_ARRAY_STACK_H
2#define UTIL_ARRAY_STACK_H
11#include <util/misc/Memory.h>
28 template <
typename Data>
59 void push(Data& data);
92 const Data&
peek()
const;
129 template <
typename Data>
139 template <
typename Data>
143 Memory::deallocate<Data*>(ptrs_, capacity_);
150 template <
typename Data>
154 UTIL_THROW(
"Cannot allocate ArrayStack with capacity <= 0");
157 UTIL_THROW(
"Attempt to re-allocate an ArrayStack");
159 Memory::allocate<Data*>(ptrs_, capacity);
160 capacity_ = capacity;
164 for (
int i = 0; i < capacity_; ++i) {
172 template <
typename Data>
174 {
return capacity_; }
179 template <
typename Data>
186 template <
typename Data>
189 if (size_ == capacity_) {
190 UTIL_THROW(
"Attempt to push onto full stack");
192 ptrs_[size_] = &data;
199 template <
typename Data>
203 UTIL_THROW(
"Attempt to pop from empty stack");
205 Data *ptr = ptrs_[size_-1];
214 template <
typename Data>
216 {
return *ptrs_[size_-1]; }
221 template <
typename Data>
223 {
return *ptrs_[size_-1]; }
228 template <
typename Data>
237 if (size_ > capacity_) {
243 for (i = 0; i < size_ ; ++i) {
248 for (i = size_; i < capacity_ ; ++i) {
250 UTIL_THROW(
"Non-null ptrs_[i] for i >= size_");
254 if (capacity_ != 0) {
255 UTIL_THROW(
"Unallocated stack but capacity != 0");
264 template <
typename Data>
266 {
return (ptrs_ != 0); }
A stack of fixed capacity, which stores pointers to elements.
bool isAllocated() const
Return true only if the ArrayStack has been allocated.
Data & pop()
Pop an element off the stack.
ArrayStack()
Default constructor.
bool isValid() const
Return true if the ArrayStack is valid, or throw an exception.
int size() const
Get the number of elements in the stack.
virtual ~ArrayStack()
Destructor.
Data & peek()
Return a reference to the top element (don't pop).
void push(Data &data)
Push an element onto the Stack.
int capacity() const
Return capacity of the underlying array.
void allocate(int capacity)
Initialize and allocate required memory.
File containing preprocessor macros for error handling.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Utility classes for scientific computation.