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