home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tlx501.zip / TEMPLATE / STATS1.CPP < prev    next >
C/C++ Source or Header  |  1996-09-27  |  6KB  |  217 lines

  1. /****************************************************************************
  2.     $Id: stats1.cpp 501.0 1995/03/07 12:27:02 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.     Implementation of class TLStats1<T>, a class to gather statistics for
  10.     a single data series.
  11.  
  12.     $Log: stats1.cpp $
  13.     Revision 501.0  1995/03/07 12:27:02  RON
  14.     Updated for TLX 5.01
  15.     Revision 1.6  1995/01/31 16:30:52  RON
  16.     Update for release 012
  17.     Added partial support for SunPro C++ compiler
  18.     Revision 1.5  1995/01/06  16:04:32  ron
  19.     Added sum to statistics output
  20.  
  21.     Revision 1.4  1995/01/05  15:39:38  ron
  22.     Widened counting members to 32 bits
  23.     Changed output format slightly
  24.  
  25.     Revision 1.3  1994/11/16  15:33:56  ron
  26.     Changed TLX_TRACE calls to incorporate group
  27.  
  28.     Revision 1.2  1994/10/07  17:10:23  ron
  29.     Renamed Insert() to Add()
  30.  
  31.     Revision 1.1  1994/10/06  17:51:43  ron
  32.     Initial revision
  33.  
  34. ****************************************************************************/
  35.  
  36. #ifndef _TLX_STATS1_CPP
  37. #define _TLX_STATS1_CPP
  38.  
  39. #include <iostream.h>
  40. #include <math.h>
  41.  
  42. #ifndef _TLX_DEBUG_H
  43. #include <tlx\501\debug.h>
  44. #endif
  45. #ifndef _TLX_RTTI_H
  46. #include <tlx\501\rtti.h>
  47. #endif
  48. #ifndef _TLX_STATS_H
  49. #include <tlx\501\stats.h>
  50. #endif
  51. #ifndef _TLX_UTIL_H
  52. #include <tlx\501\util.h>
  53. #endif
  54.  
  55. /*-------------------------------------------------------------------------*/
  56.     template<class T> TLStats1<T>::TLStats1()
  57.  
  58. /*  Default constructor; resets all statistics.
  59. ---------------------------------------------------------------------------*/
  60. {
  61.     Clear();
  62. }
  63.  
  64. /*-------------------------------------------------------------------------*/
  65.     template<class T> void TLStats1<T>::Add(T aValue)
  66.  
  67. /*  Adds a single value to the data series, updating the statistics as
  68.     necessary.
  69. ---------------------------------------------------------------------------*/
  70. {
  71.     mSum     += aValue;
  72.     mSumSquares += aValue * aValue;
  73.     mMax      = mCount ? tlMax(mMax, aValue) : aValue;
  74.     mMin      = mCount ? tlMin(mMin, aValue) : aValue;
  75.  
  76.     mCount++;
  77.     InvalidateCache(cvAverage | cvVariance);
  78. }
  79.  
  80. /*-------------------------------------------------------------------------*/
  81.     template<class T>
  82.     void TLStats1<T>::Add(const T *aVector, size_t aCount)
  83.  
  84. /*  Adds all values of a given C-style vector to the data series.
  85. ---------------------------------------------------------------------------*/
  86. {
  87.     TLX_ASSERT_PTR(aVector);
  88.  
  89.     for (size_t i = 0; i < aCount; i++)
  90.     Add(aVector[i]);
  91. }
  92.  
  93. /*-------------------------------------------------------------------------*/
  94.     template<class T> void TLStats1<T>::Add(TLValueIter<T> &aIter)
  95.  
  96. /*  Adds all values addressable by the iterator to the data series.
  97. ---------------------------------------------------------------------------*/
  98. {
  99.     for (aIter.Reset(); aIter.Next(); )
  100.     Add(aIter.Peek());
  101. }
  102.  
  103. /*-------------------------------------------------------------------------*/
  104.     template<class T> T TLStats1<T>::Average() const
  105.  
  106. /*  Returns the average of the data series so far. If the series is empty,
  107.     0 is returned.
  108. ---------------------------------------------------------------------------*/
  109. {
  110.     if (IsCacheInvalid(cvAverage))
  111.     {
  112.     if (Count() < 1)
  113.     {
  114.         TLX_TRACE_WARN(TLX, ("Average of empty series requested"));
  115.         TLX_ASSERT(mAverage == 0);
  116.     }
  117.     else
  118. #ifdef _NOMUTABLE
  119.         ((TLStats1<T> *)this)->mAverage = mSum / Count();
  120. #else
  121.         mAverage = mSum / Count();
  122. #endif
  123.     ValidateCache(cvAverage);
  124.     }
  125.     TLX_ASSERT(IsCacheValid(cvAverage));
  126.  
  127.     return mAverage;
  128. }
  129.  
  130. /*-------------------------------------------------------------------------*/
  131.     template<class T> void TLStats1<T>::Clear()
  132.  
  133. /*  Resets all statistics.
  134. ---------------------------------------------------------------------------*/
  135. {
  136.     mCount     = 0;
  137.     mCacheFlags = cvNone;
  138.     mSum     =
  139.     mSumSquares =
  140.     mMax     =
  141.     mMin     =
  142.     mAverage     =
  143.     mVariance   = 0;
  144. }
  145.  
  146. /*-------------------------------------------------------------------------*/
  147.     template<class T> T TLStats1<T>::StdDev() const
  148.  
  149. /*  Returns the standard deviation of the data series so far. If the number
  150.     of data elements is < 2, 0 is returned.
  151. ---------------------------------------------------------------------------*/
  152. {
  153.     TLX_ASSERT(Variance() >= 0.0);
  154.  
  155. #ifdef _MSC_VER
  156.     return sqrt((double)Variance());
  157. #else
  158.     return sqrt(Variance());
  159. #endif
  160. }
  161.  
  162. /*-------------------------------------------------------------------------*/
  163.     template<class T> T TLStats1<T>::Variance() const
  164.  
  165. /*  Returns the variance of the data series so far. If the number of data
  166.     elements is < 2, 0 is returned.
  167. ---------------------------------------------------------------------------*/
  168. {
  169.     if (IsCacheInvalid(cvVariance))
  170.     {
  171.     if (Count() < 2)
  172.     {
  173.         TLX_TRACE_WARN(TLX, ("Variance of series length %u requested",
  174.                       Count()));
  175.         TLX_ASSERT(mVariance == 0);
  176.     }
  177.     else
  178.     {
  179. #ifdef _NOMUTABLE
  180.         ((TLStats1<T> *)this)->mVariance = (Count() * mSumSquares - 
  181.         mSum * mSum) / (Count() * (Count() - 1));
  182. #else
  183.         mVariance = (Count() * mSumSquares - mSum * mSum) /
  184.             (Count() * (Count() - 1));
  185. #endif
  186.     }
  187.     ValidateCache(cvVariance);
  188.     }
  189.     TLX_ASSERT(IsCacheValid(cvVariance));
  190.  
  191.     return mVariance;
  192. }
  193.  
  194. /*-------------------------------------------------------------------------*/
  195.     template<class T>
  196.     ostream & _TLXFUNC operator <<(ostream &os, const TLStats1<T> &aStats)
  197.  
  198. /*  Prints the statistics on the given output stream.
  199. ---------------------------------------------------------------------------*/
  200. {
  201. #ifdef _NOMUTABLE
  202.     TLStats1<T> &s = CONSTCAST(TLStats1<T> &, aStats);
  203. #else
  204.     #define s aStats
  205. #endif
  206.  
  207.     os << "Cnt=" << s.Count() << ", Sum=" << s.Sum();
  208.     os << ", Distri=[" << s.Min();
  209.     if (s.Count() > 0) os << "," << s.Average();
  210.     os << "," << s.Max() << "]";
  211.     if (s.Count() > 1) os << ", Sdev=" << s.StdDev();
  212.  
  213.     return os;
  214. }
  215.  
  216. #endif    // _TLX_STATS1_CPP
  217.