stack.icc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // $Id:$
00003 // ---------------------------------------------------------------------------
00004 
00005 #ifndef HEP_STACK_SRC
00006 #define HEP_STACK_SRC
00007 
00008 /*
00009  * Simplified stack class.
00010  * It is intended to be used as a replacement of the standard class where
00011  * full functionality of <stack> is not required, but it is essential
00012  * to have highly portable and effective code.
00013  *
00014  * This file should be used exclusively inside *.cc files.
00015  * Usage inside header files can result to a clash with standard <stack>.
00016  *
00017  * @author Evgeni Chernyaev  <Evgueni.Tcherniaev@cern.ch>
00018  */
00019 template<class T>
00020 class stack {
00021  private:
00022   int k, max_size;
00023   T * v;
00024 
00025  public:
00026   stack() :  k(0), max_size(20), v(new T[20]) {}
00027   ~stack() { delete [] v; }  
00028 
00029   int    size()    const { return k; }
00030   T      top ()    const { return v[k-1]; }
00031   T &    top ()          { return v[k-1]; }
00032   void   pop ()          { k--; }
00033   void   push(T a) {
00034     if (k == max_size) {
00035       T * w     = v;
00036       max_size *= 2;
00037       v         = new T[max_size];
00038       for (int i=0; i<k; i++) v[i] = w[i];
00039       delete [] w;
00040     }
00041     v[k++] = a;
00042   }
00043 };
00044 
00045 #endif /* HEP_STACK_SRC */

Generated on Mon May 27 17:50:35 2013 for Geant4 by  doxygen 1.4.7