Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

average.h

Go to the documentation of this file.
00001 //**************************************************************************
00002 #ifndef _AVERAGE_H_
00003 #define _AVERAGE_H_
00004 //**************************************************************************
00005 #ifdef HPUX
00006 #include <iostream.h>
00007 #else
00008 #include <iostream>
00009 #endif
00010 //**************************************************************************
00011 #define NUM_AVG_ELEMS 50
00012 //**************************************************************************
00013 template <class Etype>
00014 class Average
00015 {
00016 private:
00017         int num;
00018         int calls;
00019         Etype x;
00020         Etype p_max;
00021         Etype p_min;
00022         Etype p_sum;
00023         Etype buffer[NUM_AVG_ELEMS];
00024 
00025 public:
00026         Average();
00027         Average(Etype a);
00028         ~Average();
00029         int push(Etype a);
00030         Etype average();
00031         Etype average(int n);
00032         Etype last();
00033         Etype last(int pos);
00034         Etype min();
00035         Etype max();
00036         Average &operator= (const Etype &I);
00037         Etype operator() (void);
00038         void reset();
00039 };
00040 //**************************************************************************
00041 template <class Etype>
00042 inline void Average<Etype>::reset()
00043 {
00044 p_min = (Etype) INT_MAX;
00045 p_max = (Etype) -INT_MAX;
00046 p_sum = (Etype) 0;
00047 calls = 0;
00048 }
00049 //**************************************************************************
00050 template <class Etype>
00051 inline Average<Etype>::Average()
00052 {
00053 reset();
00054 }
00055 //**************************************************************************
00056 template <class Etype>
00057 inline Average<Etype>::Average(Etype a)
00058 {
00059 reset();
00060 push(a);
00061 }
00062 //**************************************************************************
00063 template <class Etype>
00064 inline Average<Etype>::~Average()
00065 {
00066 }
00067 //**************************************************************************
00068 template <class Etype>
00069 inline int Average<Etype>::push(Etype a)
00070 {
00071 buffer[calls%NUM_AVG_ELEMS] = a;
00072 ++calls;
00073 x = a;
00074 p_sum += x;
00075 if (x < p_min) p_min = x;
00076 if (x > p_max) p_max = x;
00077 }
00078 //**************************************************************************
00079 template <class Etype>
00080 inline Etype Average<Etype>::average()
00081 {
00082 Etype avg = (Etype) 0;
00083 if (calls == 0) return avg;
00084 avg = p_sum/((Etype) calls);
00085 return avg;
00086 }
00087 //**************************************************************************
00088 template <class Etype>
00089 inline Etype Average<Etype>::average(int n)
00090 {
00091 if (n > NUM_AVG_ELEMS) return average();
00092 Etype my_sum = (Etype) 0;
00093 for (int i = 1; i <= n; i++)
00094         my_sum += last(i);
00095 return (my_sum/((Etype) n));
00096 }
00097 //**************************************************************************
00098 template <class Etype>
00099 inline Etype Average<Etype>::min()
00100 {
00101 if (calls == 0) return (Etype) 0;
00102 return p_min;
00103 }
00104 //**************************************************************************
00105 template <class Etype>
00106 inline Etype Average<Etype>::last()
00107 {
00108 return x;
00109 }
00110 //**************************************************************************
00111 template <class Etype>
00112 inline Etype Average<Etype>::last(int pos)
00113 {
00114 if (pos > NUM_AVG_ELEMS) return last();
00115 return buffer[(calls-pos+NUM_AVG_ELEMS)%NUM_AVG_ELEMS];
00116 }
00117 //**************************************************************************
00118 template <class Etype>
00119 inline Etype Average<Etype>::max()
00120 {
00121 if (calls == 0) return (Etype) 0;
00122 return p_max;
00123 }
00124 //**************************************************************************
00125 template <class Etype>
00126 inline Average<Etype> &Average<Etype>::operator= (const Etype &I)
00127 {
00128 push(I);
00129 
00130 return *this;
00131 }
00132 //**************************************************************************
00133 template <class Etype>
00134 inline Etype Average<Etype>::operator() (void)
00135 {
00136 return average();
00137 }
00138 //**************************************************************************
00139 #endif  // _AVERAGE_H_
00140 //**************************************************************************

Generated on Sun Jun 16 17:36:41 2002 for Anirudh's Vortex-Wake Simulation Code by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001