11 #include <util/containers/GStack.h> 27 template <
typename Data>
94 void push(Data& data);
114 const Data&
peek()
const;
156 template <
typename Data>
170 template <
typename Data>
176 assert(other.capacity_ >= other.size_);
177 if (other.ptrs_ == 0) {
178 assert(other.capacity_ == 0);
179 assert(other.size_ == 0);
184 assert(other.capacity_ > 0);
186 Memory::allocate<Data*>(ptrs_, other.capacity_);
187 capacity_ = other.capacity_;
191 for (
int i = 0; i < size_; ++i) {
192 ptrs_[i] = other.ptrs_[i];
196 if (capacity_ > size_) {
197 for (
int i = size_; i < capacity_; ++i) {
207 template <
typename Data>
220 template <
typename Data>
224 if (
this == &other)
return *
this;
227 for (
int i = 0; i < other.size_; ++i) {
236 template <
typename Data>
240 UTIL_THROW(
"Cannot reserve with capacity <=0");
243 assert(capacity_ == 0);
245 Memory::allocate<Data*>(ptrs_,
capacity);
248 for (
int i = 0; i < capacity_; ++i) {
251 }
else if (capacity > capacity_) {
252 assert(capacity_ > 0);
253 assert(capacity_ >= size_);
256 Memory::allocate<Data*>(newPtr,
capacity);
258 for (
int i = 0; i < size_; ++i) {
259 newPtr[i] = ptrs_[i];
262 if (size_ < capacity) {
263 for (
int i = size_; i <
capacity; ++i) {
267 Memory::deallocate<Data*>(ptrs_, capacity_);
276 template <
typename Data>
281 assert(capacity_ > 0);
282 Memory::deallocate<Data*>(ptrs_, capacity_);
290 template <
typename Data>
297 template <
typename Data>
301 assert(capacity_ == 0);
302 Memory::allocate<Data*>(ptrs_, 64);
305 for (
int i = 0; i < capacity_; ++i) {
308 }
else if (size_ == capacity_) {
309 assert(capacity_ > 0);
310 assert(capacity_ >= size_);
313 Memory::allocate<Data*>(newPtr, 2*capacity_);
315 for (
int i = 0; i < size_; ++i) {
316 newPtr[i] = ptrs_[i];
319 if (size_ < 2*capacity_) {
320 for (
int i = size_; i < 2*capacity_; ++i) {
324 Memory::deallocate<Data*>(ptrs_, capacity_);
326 capacity_ = 2*capacity_;
331 assert(size_ <= capacity_);
332 ptrs_[size_] = &data;
339 template <
typename Data>
343 UTIL_THROW(
"Attempt to pop from empty stack");
345 Data *ptr = ptrs_[size_-1];
354 template <
typename Data>
356 {
return *ptrs_[size_-1]; }
361 template <
typename Data>
363 {
return *ptrs_[size_-1]; }
368 template <
typename Data>
370 {
return capacity_; }
375 template <
typename Data>
382 template <
class Data>
384 {
return (
bool)ptrs_; }
389 template <
typename Data>
398 if (size_ > capacity_) {
404 for (i = 0; i < size_ ; ++i) {
409 for (i = size_; i < capacity_ ; ++i) {
411 UTIL_THROW(
"Non-null ptrs_[i] for i >= size_");
415 if (capacity_ != 0) {
416 UTIL_THROW(
"Unallocated stack but capacity != 0");
void push(Data &data)
Push an element onto the stack.
An automatically growable Stack.
Data & pop()
Pop an element off the stack.
void deallocate()
Deallocate (delete) underlying array of pointers.
int capacity() const
Return allocated size.
File containing preprocessor macros for error handling.
GStack< Data > & operator=(const GStack< Data > &other)
Assignment, element by element.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Data & peek()
Return a reference to the top element (don't pop).
bool isValid() const
Is this GStack in a valid internal state?
Utility classes for scientific computation.
void clear()
Reset to empty state.
bool isAllocated() const
Is this GStack allocated?
int size() const
Return logical size.
void reserve(int capacity)
Reserve memory for specified number of elements.
static void deallocate(Data *&ptr, size_t size)
Allocate a C array.