CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
Pile.h
Go to the documentation of this file.
1// -*- C++ -*-
2// CLASSDOC OFF
3// ---------------------------------------------------------------------------
4// CLASSDOC ON
5//
6// This file is a part of the CLHEP - a Class Library for High Energy Physics.
7//
8// This software written by Nobu Katayama and Mike Smyth, Cornell University.
9//
10// This file contains an attempt to make the template "pile". A pile is
11// a finite size LIFO stack. When a element is pushed on that increases
12// the stack beyond its maximum size, the oldest element is deleted from
13// the stack. A subroutine can be used on that oldest element first.
14
15// The orginal use of this stack was to store old double arrays. When
16// a new array is needed, we can simply pull one off the pile. However,
17// we don't want to keep too many old array's around, after a while we just
18// want to start getting rid of them. When the pile gets too large, or
19// when the pile is destroyed, we want to call subroutines to get rid of
20// some of these arrays.
21
22// Unfortunately, in version 2.2 of g++ templates don't seem to work unless
23// they are declared inline. So this class has ridiculously long inline
24// functions. Also, g++ doesn't seem to allow multiple arguements to
25// templates, so the size of the pile is hardwired in. To change the size,
26// change the value of the const int sz.
27
28// A pile is easy to use. Just declare pile<X> X_pile. To add a X to the
29// pile, say X_pile.push(X item). To get an item from the pile, first
30// check that the pile is not empty, and then say item=X_pile.pop(). It
31// is an error to try and pop from an empty pile. To check if a pile is
32// empty, say X_pile.is_empty(). If this is TRUE, then the pile is empty.
33// Otherwise it is FALSE. The subroutine called when the stack begins to
34// overflow is set by X_pile.destroy(void (*function)(X)), or it can be
35// set in the construction pile<X> X_pile(void (*function)(X)). It is
36// okay to not supply a function, in that case nothing is done when an
37// item falls off the bottom of the pile. It is simply lost.
38
39#ifndef _PILE_H
40#define _PILE_H
41
42#include <iostream>
43#include "CLHEP/Matrix/defs.h"
44
45/**
46 * @author
47 * @ingroup matrix
48 */
49
50namespace CLHEP {
51
52template<class T>
53class HepPile
54{
55public:
56 // Destructor
57 // (defined first in templated class due to a bug in VxWorks)
59 {
60 while(bottom != top)
61 {
62#if 1
63 destroy(stack[bottom]);
64#else
65 delete [] stack[bottom];
66#endif
67 next(&bottom);
68 }
69 }
70
71 HepPile(void (*f)(T)=0): top(0), bottom(0) { destroy_fun = f;}
72
73 void set_destroy(void (*f)(T)) { destroy_fun = f;}
74 void push(T item)
75 {
76 stack[top]=item;
77 next(&top);
78 if (top==bottom)
79 {
80#if 1
81 destroy(stack[bottom]);
82#else
83 delete [] stack[bottom];
84#endif
85 next(&bottom);
86 }
87 }
88 bool is_empty() const { return top == bottom ?true :false;}
89 T pop()
90 {
91 if (is_empty())
92 {
93 std::cerr << "Attempt to pop empty pile.\n--- Exiting to system."
94 << std::endl;
95 exit(1);
96 }
97 previous(&top);
98 return stack[top];
99 }
100
101private:
102 enum {sz = 50};
103 T stack[sz+1];
104 int top,bottom;
105 void (*destroy_fun)(T);
106 void next(int *n) const {if (++(*n) >= sz+1) *n = 0;}
107 void previous(int *n) const {if (--(*n) < 0) *n = sz;}
108 void destroy(T t) { if (destroy_fun) (*destroy_fun)(t); }
109};
110
111} // namespace CLHEP
112
113#ifdef ENABLE_BACKWARDS_COMPATIBILITY
114// backwards compatibility will be enabled ONLY in CLHEP 1.9
115using namespace CLHEP;
116#endif
117
118#endif /*_PILE_H */
void set_destroy(void(*f)(T))
Definition: Pile.h:73
void push(T item)
Definition: Pile.h:74
bool is_empty() const
Definition: Pile.h:88
HepPile(void(*f)(T)=0)
Definition: Pile.h:71
T pop()
Definition: Pile.h:89
~HepPile()
Definition: Pile.h:58
void f(void g())
Definition: excDblThrow.cc:38
#define exit(x)