CLHEP 2.0.4.7 Reference Documentation
CLHEP Home Page CLHEP Documentation CLHEP Bug Reports |
00001 // -*- C++ -*- 00002 // CLASSDOC OFF 00003 // --------------------------------------------------------------------------- 00004 // CLASSDOC ON 00005 // 00006 // This file is a part of the CLHEP - a Class Library for High Energy Physics. 00007 // 00008 // 00009 // Copyright Cornell University 1993, 1996, All Rights Reserved. 00010 // 00011 // This software written by Nobu Katayama and Mike Smyth, Cornell University. 00012 // 00013 // Redistribution and use in source and binary forms, with or without 00014 // modification, are permitted provided that the following conditions 00015 // are met: 00016 // 1. Redistributions of source code must retain the above copyright 00017 // notice and author attribution, this list of conditions and the 00018 // following disclaimer. 00019 // 2. Redistributions in binary form must reproduce the above copyright 00020 // notice and author attribution, this list of conditions and the 00021 // following disclaimer in the documentation and/or other materials 00022 // provided with the distribution. 00023 // 3. Neither the name of the University nor the names of its contributors 00024 // may be used to endorse or promote products derived from this software 00025 // without specific prior written permission. 00026 // 00027 // Creation of derivative forms of this software for commercial 00028 // utilization may be subject to restriction; written permission may be 00029 // obtained from Cornell University. 00030 // 00031 // CORNELL MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. By way 00032 // of example, but not limitation, CORNELL MAKES NO REPRESENTATIONS OR 00033 // WARRANTIES OF MERCANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT 00034 // THE USE OF THIS SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, 00035 // COPYRIGHTS, TRADEMARKS, OR OTHER RIGHTS. Cornell University shall not be 00036 // held liable for any liability with respect to any claim by the user or any 00037 // other party arising from use of the program. 00038 // 00039 00040 // This file contains an attempt to make the template "pile". A pile is 00041 // a finite size LIFO stack. When a element is pushed on that increases 00042 // the stack beyond its maximum size, the oldest element is deleted from 00043 // the stack. A subroutine can be used on that oldest element first. 00044 00045 // The orginal use of this stack was to store old double arrays. When 00046 // a new array is needed, we can simply pull one off the pile. However, 00047 // we don't want to keep too many old array's around, after a while we just 00048 // want to start getting rid of them. When the pile gets too large, or 00049 // when the pile is destroyed, we want to call subroutines to get rid of 00050 // some of these arrays. 00051 00052 // Unfortunately, in version 2.2 of g++ templates don't seem to work unless 00053 // they are declared inline. So this class has ridiculously long inline 00054 // functions. Also, g++ doesn't seem to allow multiple arguements to 00055 // templates, so the size of the pile is hardwired in. To change the size, 00056 // change the value of the const int sz. 00057 00058 // A pile is easy to use. Just declare pile<X> X_pile. To add a X to the 00059 // pile, say X_pile.push(X item). To get an item from the pile, first 00060 // check that the pile is not empty, and then say item=X_pile.pop(). It 00061 // is an error to try and pop from an empty pile. To check if a pile is 00062 // empty, say X_pile.is_empty(). If this is TRUE, then the pile is empty. 00063 // Otherwise it is FALSE. The subroutine called when the stack begins to 00064 // overflow is set by X_pile.destroy(void (*function)(X)), or it can be 00065 // set in the construction pile<X> X_pile(void (*function)(X)). It is 00066 // okay to not supply a function, in that case nothing is done when an 00067 // item falls off the bottom of the pile. It is simply lost. 00068 00069 #ifndef _PILE_H 00070 #define _PILE_H 00071 00072 #include <iostream> 00073 #include "CLHEP/Matrix/defs.h" 00074 00080 namespace CLHEP { 00081 00082 template<class T> 00083 class HepPile 00084 { 00085 public: 00086 // Destructor 00087 // (defined first in templated class due to a bug in VxWorks) 00088 ~HepPile() 00089 { 00090 while(bottom != top) 00091 { 00092 #if 1 00093 destroy(stack[bottom]); 00094 #else 00095 delete [] stack[bottom]; 00096 #endif 00097 next(&bottom); 00098 } 00099 } 00100 00101 HepPile(void (*f)(T)=0): top(0), bottom(0) { destroy_fun = f;} 00102 00103 void set_destroy(void (*f)(T)) { destroy_fun = f;} 00104 void push(T item) 00105 { 00106 stack[top]=item; 00107 next(&top); 00108 if (top==bottom) 00109 { 00110 #if 1 00111 destroy(stack[bottom]); 00112 #else 00113 delete [] stack[bottom]; 00114 #endif 00115 next(&bottom); 00116 } 00117 } 00118 bool is_empty() const { return top == bottom ?true :false;} 00119 T pop() 00120 { 00121 if (is_empty()) 00122 { 00123 std::cerr << "Attempt to pop empty pile.\n--- Exiting to system." 00124 << std::endl; 00125 exit(1); 00126 } 00127 previous(&top); 00128 return stack[top]; 00129 } 00130 00131 private: 00132 enum {sz = 50}; 00133 T stack[sz+1]; 00134 int top,bottom; 00135 void (*destroy_fun)(T); 00136 void next(int *n) const {if (++(*n) >= sz+1) *n = 0;} 00137 void previous(int *n) const {if (--(*n) < 0) *n = sz;} 00138 void destroy(T t) { if (destroy_fun) (*destroy_fun)(t); } 00139 }; 00140 00141 } // namespace CLHEP 00142 00143 #ifdef ENABLE_BACKWARDS_COMPATIBILITY 00144 // backwards compatibility will be enabled ONLY in CLHEP 1.9 00145 using namespace CLHEP; 00146 #endif 00147 00148 #endif /*_PILE_H */