home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tlx501.zip / STATS.H < prev    next >
C/C++ Source or Header  |  1996-07-02  |  5KB  |  168 lines

  1. /****************************************************************************
  2.     $Id: stats.h 501.0 1995/03/07 12:26:46 RON Exp $
  3.  
  4.     Copyright (c) 1991-95 Tarma Software Research. All rights reserved.
  5.  
  6.     Project:    Tarma Library for C++ V5.0
  7.     Author:    Ron van der Wal
  8.  
  9.     Declarations for statistics gathering classes.
  10.  
  11.     $Log: stats.h $
  12.     Revision 501.0  1995/03/07 12:26:46  RON
  13.     Updated for TLX 5.01
  14.     Revision 1.6  1995/01/31 16:29:24  RON
  15.     Update for release 012
  16.     Added partial support for SunPro C++ compiler
  17.     Revision 1.5  1995/01/06  16:01:33  ron
  18.     Added Sum() member function
  19.  
  20.     Revision 1.4  1995/01/05  15:34:07  ron
  21.     Widened count members of TLStats1<T>
  22.  
  23.     Revision 1.3  1994/10/07  17:08:26  ron
  24.     Renamed Insert() operations to Add()
  25.  
  26.     Revision 1.2  1994/10/06  17:49:17  ron
  27.     Replaced TLRealStats with a template class
  28.  
  29.     Revision 1.1  1994/10/05  18:24:38  ron
  30.     Initial revision
  31.  
  32. ****************************************************************************/
  33.  
  34. #ifndef _TLX_STATS_H
  35. #define _TLX_STATS_H
  36.  
  37. #ifndef _TLX_ITER_H
  38. #include <tlx\501\iter.h>
  39. #endif
  40.  
  41. class _RTLCLASS ostream;
  42.  
  43. /*---------------------------------------------------------------------------
  44.     TLStats1<T> -
  45.  
  46.     Statistics collecting class for values of type T in a single data
  47.     series. The class employs lazy evalution techniques to minimize
  48.     the calculations involved.
  49.  
  50.     Note: type T must allow arithmetic operations and be convertable
  51.     from/to type double.
  52. ---------------------------------------------------------------------------*/
  53.  
  54. template<class T> class _TLXCLASS TLStats1
  55. {
  56.     // For efficiency reasons, we permanently store only a few values
  57.     // that can be recalculated with little effort whenever a datum is
  58.     // added. The other statistics are calculated on demand, although
  59.     // their previous value is cached and will be reused if possible.
  60.     // The following flags indicate which cached values are valid at a
  61.     // given moment.
  62.  
  63.     enum {
  64.     cvNone        = 0x0000,    // No cached values are valid
  65.     cvMax        = 0x0001,    // Cached max value still valid
  66.     cvMin        = 0x0002,    // Cached min value still valid
  67.     cvAverage    = 0x0004,    // Cached average value still valid
  68.     cvVariance    = 0x0008,    // Cached variance value still valid
  69.     cvAll        = 0xffff    // All values are valid
  70.     };
  71.  
  72.     // The following values are always valid: they can and will be updated
  73.     // on each insertion.
  74.  
  75.     uint32        mCount;
  76.     T            mSum;
  77.     T            mSumSquares;
  78.     T            mMax;
  79.     T            mMin;
  80.  
  81.     // The next values are subject to caching; their validity depends on the
  82.     // cache flags.
  83.  
  84.     mutable int        mCacheFlags;
  85.     mutable T        mAverage;
  86.     mutable T        mVariance;
  87.  
  88. public:
  89.     TLStats1();
  90.  
  91.     // Addition of individual items and data series, and wholesale removal.
  92.  
  93.     void        Add(T);
  94.     void        Add(const T *, size_t);
  95.     void        Add(TLValueIter<T> &);
  96.     void        Clear();
  97.  
  98.     // Statistics of the data series.
  99.  
  100.     uint32        Count() const;
  101.     T            Sum() const;
  102.     T            Max() const;
  103.     T            Min() const;
  104.     T            Average() const;
  105.     T            Variance() const;
  106.     T            StdDev() const;
  107.  
  108.     // Output operator
  109.  
  110. #if !defined(_MSC_VER) && !defined(__SC__)
  111.     // Microsoft/Symantec C++ quirk
  112.     friend ostream &    operator <<(ostream &, const TLStats1<T> &);
  113. #endif
  114.  
  115. private:
  116.     // Implementation helper functions, primarily for cache maintenance.
  117.  
  118.     bool        IsCacheInvalid(int) const;
  119.     bool        IsCacheValid(int) const;
  120.     void        ValidateCache(int) const;
  121.     void        InvalidateCache(int) const;
  122. };
  123.  
  124. #if defined(_MSC_VER) || defined(__SC__)
  125. template<class T>
  126. ostream & _TLXFUNC operator <<(ostream &, const TLStats1<T> &);
  127. #endif
  128.  
  129. /*---------------------------------------------------------------------------
  130.     Inline functions
  131. ---------------------------------------------------------------------------*/
  132.  
  133. //-----    TLStats1<T>
  134.  
  135. template<class T> inline uint32 TLStats1<T>::Count() const
  136.     { return mCount; }
  137.  
  138. template<class T> inline T TLStats1<T>::Sum() const
  139.     { return mSum; }
  140.  
  141. template<class T> inline bool TLStats1<T>::IsCacheInvalid(int aFlags) const
  142.     { return (mCacheFlags & aFlags) != aFlags; }
  143.  
  144. template<class T> inline bool TLStats1<T>::IsCacheValid(int aFlags) const
  145.     { return (mCacheFlags & aFlags) == aFlags; }
  146.  
  147. template<class T> inline void TLStats1<T>::ValidateCache(int aFlags) const
  148. #ifdef _NOMUTABLE
  149.     { ((TLStats1<T> *)this)->mCacheFlags |= aFlags; }
  150. #else
  151.     { mCacheFlags |= aFlags; }
  152. #endif
  153.  
  154. template<class T> inline void TLStats1<T>::InvalidateCache(int aFlags) const
  155. #ifdef _NOMUTABLE
  156.     { ((TLStats1<T> *)this)->mCacheFlags &= ~aFlags; }
  157. #else
  158.     { mCacheFlags &= ~aFlags; }
  159. #endif
  160.  
  161. template<class T> inline T TLStats1<T>::Max() const
  162.     { return mMax; }
  163.  
  164. template<class T> inline T TLStats1<T>::Min() const
  165.     { return mMin; }
  166.  
  167. #endif    // _TLX_STATS_H
  168.