|
||||
File indexing completed on 2025-01-18 09:54:37
0001 // -*- C++ -*- 0002 // CLASSDOC OFF 0003 // --------------------------------------------------------------------------- 0004 // CLASSDOC ON 0005 // 0006 // This file is a part of the CLHEP - a Class Library for High Energy Physics. 0007 // 0008 // This software written by Nobu Katayama and Mike Smyth, Cornell University. 0009 // 0010 // This file contains an attempt to make the template "pile". A pile is 0011 // a finite size LIFO stack. When a element is pushed on that increases 0012 // the stack beyond its maximum size, the oldest element is deleted from 0013 // the stack. A subroutine can be used on that oldest element first. 0014 0015 // The orginal use of this stack was to store old double arrays. When 0016 // a new array is needed, we can simply pull one off the pile. However, 0017 // we don't want to keep too many old array's around, after a while we just 0018 // want to start getting rid of them. When the pile gets too large, or 0019 // when the pile is destroyed, we want to call subroutines to get rid of 0020 // some of these arrays. 0021 0022 // Unfortunately, in version 2.2 of g++ templates don't seem to work unless 0023 // they are declared inline. So this class has ridiculously long inline 0024 // functions. Also, g++ doesn't seem to allow multiple arguements to 0025 // templates, so the size of the pile is hardwired in. To change the size, 0026 // change the value of the const int sz. 0027 0028 // A pile is easy to use. Just declare pile<X> X_pile. To add a X to the 0029 // pile, say X_pile.push(X item). To get an item from the pile, first 0030 // check that the pile is not empty, and then say item=X_pile.pop(). It 0031 // is an error to try and pop from an empty pile. To check if a pile is 0032 // empty, say X_pile.is_empty(). If this is TRUE, then the pile is empty. 0033 // Otherwise it is FALSE. The subroutine called when the stack begins to 0034 // overflow is set by X_pile.destroy(void (*function)(X)), or it can be 0035 // set in the construction pile<X> X_pile(void (*function)(X)). It is 0036 // okay to not supply a function, in that case nothing is done when an 0037 // item falls off the bottom of the pile. It is simply lost. 0038 0039 #ifndef _PILE_H 0040 #define _PILE_H 0041 0042 #include <iostream> 0043 #include "CLHEP/Matrix/defs.h" 0044 0045 /** 0046 * @author 0047 * @ingroup matrix 0048 */ 0049 0050 namespace CLHEP { 0051 0052 template<class T> 0053 class HepPile 0054 { 0055 public: 0056 // Destructor 0057 // (defined first in templated class due to a bug in VxWorks) 0058 ~HepPile() 0059 { 0060 while(bottom != top) 0061 { 0062 #if 1 0063 destroy(stack[bottom]); 0064 #else 0065 delete [] stack[bottom]; 0066 #endif 0067 next(&bottom); 0068 } 0069 } 0070 0071 HepPile(void (*f)(T)=0): top(0), bottom(0) { destroy_fun = f;} 0072 0073 void set_destroy(void (*f)(T)) { destroy_fun = f;} 0074 void push(T item) 0075 { 0076 stack[top]=item; 0077 next(&top); 0078 if (top==bottom) 0079 { 0080 #if 1 0081 destroy(stack[bottom]); 0082 #else 0083 delete [] stack[bottom]; 0084 #endif 0085 next(&bottom); 0086 } 0087 } 0088 bool is_empty() const { return top == bottom ?true :false;} 0089 T pop() 0090 { 0091 if (is_empty()) 0092 { 0093 std::cerr << "Attempt to pop empty pile.\n--- Exiting to system." 0094 << std::endl; 0095 exit(1); 0096 } 0097 previous(&top); 0098 return stack[top]; 0099 } 0100 0101 private: 0102 enum {sz = 50}; 0103 T stack[sz+1]; 0104 int top,bottom; 0105 void (*destroy_fun)(T); 0106 void next(int *n) const {if (++(*n) >= sz+1) *n = 0;} 0107 void previous(int *n) const {if (--(*n) < 0) *n = sz;} 0108 void destroy(T t) { if (destroy_fun) (*destroy_fun)(t); } 0109 }; 0110 0111 } // namespace CLHEP 0112 0113 #ifdef ENABLE_BACKWARDS_COMPATIBILITY 0114 // backwards compatibility will be enabled ONLY in CLHEP 1.9 0115 using namespace CLHEP; 0116 #endif 0117 0118 #endif /*_PILE_H */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |