Geant4  10.01.p02
stack.icc
Go to the documentation of this file.
1 // -*- C++ -*-
2 // $Id:$
3 // ---------------------------------------------------------------------------
4 
5 #ifndef HEP_STACK_SRC
6 #define HEP_STACK_SRC
7 
8 /*
9  * Simplified stack class.
10  * It is intended to be used as a replacement of the standard class where
11  * full functionality of <stack> is not required, but it is essential
12  * to have highly portable and effective code.
13  *
14  * This file should be used exclusively inside *.cc files.
15  * Usage inside header files can result to a clash with standard <stack>.
16  *
17  * @author Evgeni Chernyaev <Evgueni.Tcherniaev@cern.ch>
18  */
19 template<class T>
20 class stack {
21  private:
22  int k, max_size;
23  T * v;
24 
25  public:
26  stack() : k(0), max_size(20), v(new T[20]) {}
27  ~stack() { delete [] v; }
28 
29  int size() const { return k; }
30  T top () const { return v[k-1]; }
31  T & top () { return v[k-1]; }
32  void pop () { k--; }
33  void push(T a) {
34  if (k == max_size) {
35  T * w = v;
36  max_size *= 2;
37  v = new T[max_size];
38  for (int i=0; i<k; i++) v[i] = w[i];
39  delete [] w;
40  }
41  v[k++] = a;
42  }
43 };
44 
45 #endif /* HEP_STACK_SRC */