NeoPZ
pzstack.h
Go to the documentation of this file.
1 
6 #ifndef PZSTACK_H
7 #define PZSTACK_H
8 
9 #include <iostream>
10 #include <cstdlib>
11 #include "pzerror.h"
12 #include "pzmanvector.h"
13 
23 template< class T, int NumExtAlloc = DEFAULTVEC_ALLOC >
24 class TPZStack : public TPZManVector< T, NumExtAlloc >
25 {
26 public:
28  T & Peek() const;
29 
36  TPZStack();
37 
38  TPZStack(int nel, const T Object) : TPZManVector<T,NumExtAlloc>(nel,Object)
39  {
40 
41  }
42 
47  void Push(const T object);
48  void push_back(const T object)
49  {
50  Push(object);
51  }
52 
62  T Pop();
63 
68  operator T*() const { return this->fStore; }
69 };
70 
71 //--| IMPLEMENTATION |----------------------------------------------------------
72 
73 template<class T, int NumExtAlloc >
75  this->Expand(NumExtAlloc);
76 }
77 
78 // Puts an object on the stack
79 template<class T, int NumExtAlloc >
80 void TPZStack<T, NumExtAlloc>::Push(const T object) {
81  this->Resize(this->NElements()+1);
82  this->operator[](this->NElements()-1) = object;
83 }
84 
85 // Retrieve an object from the stack
86 template<class T, int NumExtAlloc >
88  this->fNElements--;
89  if(this->fNElements <0){
90  this->fNElements = 0;
91  PZError << "TPZStack popping beyond the stack object" << std::endl;
92  PZError.flush();
93  T temp(0);
94  return temp;
95  }
96  return this->fStore[this->fNElements];
97 }
98 
99 template <class T, int NumExtAlloc >
101  if(this->NElements() <= 0) {
102  PZError << "TPZStack peek beyond the stack object" << std::endl;
103  PZError.flush();
104  exit(-1);
105  }
106  return this->operator[](this->NElements()-1);
107 }
108 
109 #endif // PZSTACK_H
110 
TPZStack(int nel, const T Object)
Definition: pzstack.h:38
Implements a vector class which allows to use external storage provided by the user. Utility.
Definition: pzquad.h:16
T & operator[](const int64_t index) const
Access operator, will perform bounds checking unless the variable NODEBUG is defined.
Definition: pzvec.h:148
Defines PZError.
void Expand(const int64_t newsize)
Expands the allocated storage to fit the newsize parameter.
Definition: pzmanvector.h:367
virtual void Resize(const int64_t newsize, const T &object)
Resizes the vector object.
Definition: pzmanvector.h:426
TPZStack()
Create the stack object, indicates the stack increments.
Definition: pzstack.h:74
void Push(const T object)
Pushes a copy of the object on the stack.
Definition: pzstack.h:80
Free store vector implementation.
T * fStore
Allocated storage for the vector object.
Definition: pzvec.h:230
T Pop()
Retrieve an object from the stack.
Definition: pzstack.h:87
int64_t fNElements
Number of elements of the vector object.
Definition: pzvec.h:233
T & Peek() const
Definition: pzstack.h:100
This class implements a stack object. Utility.
Definition: pzcheckmesh.h:14
int64_t NElements() const
Returns the number of elements of the vector.
Definition: pzvec.h:190
void push_back(const T object)
Definition: pzstack.h:48
#define PZError
Defines the output device to error messages and the DebugStop() function.
Definition: pzerror.h:15