OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4TableGraph Class Reference

#include <C4Network2Stats.h>

Inheritance diagram for C4TableGraph:
[legend]
Collaboration diagram for C4TableGraph:
[legend]

Public Types

enum  { DefaultBlockLength = 256 }
 
typedef float ValueType
 
typedef int TimeType
 

Public Member Functions

 C4TableGraph (int iBackLogLength=DefaultBlockLength, TimeType iStartTime=0)
 
 ~C4TableGraph ()
 
void Reset (TimeType iToTime)
 
void SetDumpFile (StdStrBuf &szFile)
 
virtual TimeType GetStartTime () const
 
virtual TimeType GetEndTime () const
 
virtual ValueType GetValue (TimeType iAtTime) const
 
ValueType GetAtValue (TimeType iAtTime) const
 
void SetAvgValue (TimeType iAtTime, ValueType iValue) const
 
virtual ValueType GetMedianValue (TimeType iStartTime, TimeType iEndTime) const
 
virtual ValueType GetMinValue () const
 
virtual ValueType GetMaxValue () const
 
void RecordValue (ValueType iValue)
 
virtual bool DumpToFile (const StdStrBuf &rszFilename, bool fAppend) const
 
virtual int GetSeriesCount () const
 
virtual const C4GraphGetSeries (int iIndex) const
 
virtual void SetAverageTime (int iToTime)
 
virtual void Update () const
 
virtual void SetMultiplier (ValueType fToVal)
 
void SetTitle (const char *szNewTitle)
 
void SetColorDw (DWORD dwClr)
 
DWORD GetColorDw () const
 
const char * GetTitle () const
 

Detailed Description

Definition at line 69 of file C4Network2Stats.h.

Member Typedef Documentation

typedef int C4Graph::TimeType
inherited

Definition at line 31 of file C4Network2Stats.h.

typedef float C4Graph::ValueType
inherited

Definition at line 30 of file C4Network2Stats.h.

Member Enumeration Documentation

anonymous enum
Enumerator
DefaultBlockLength 

Definition at line 89 of file C4Network2Stats.h.

89 { DefaultBlockLength = 256 }; // default backlog

Constructor & Destructor Documentation

C4TableGraph::C4TableGraph ( int  iBackLogLength = DefaultBlockLength,
TimeType  iStartTime = 0 
)

Definition at line 33 of file C4Network2Stats.cpp.

34  : iBackLogLength(iBackLogLength), pValues(nullptr), fMultiplier(1), pAveragedValues(nullptr), iBackLogPos(0), fWrapped(false)
35  , iInitialStartTime(iStartTime), iTime(iStartTime), iAveragedTime(iStartTime), iAvgRange(1)
36 {
37  // create value buffer
38  assert(iBackLogLength);
39  pValues = pAveragedValues = new ValueType[iBackLogLength];
40  *pValues = 0;
41 }
float ValueType
C4TableGraph::~C4TableGraph ( )

Definition at line 43 of file C4Network2Stats.cpp.

References Reset().

44 {
45  // flush stuff
46  Reset(-1);
47  // free value buffer(s)
48  if (pValues != pAveragedValues) delete [] pAveragedValues;
49  delete [] pValues;
50 }
void Reset(TimeType iToTime)

Here is the call graph for this function:

Member Function Documentation

bool C4TableGraph::DumpToFile ( const StdStrBuf rszFilename,
bool  fAppend 
) const
virtual

Definition at line 158 of file C4Network2Stats.cpp.

References CStdFile::Append(), CStdFile::Create(), StdStrBuf::Format(), StdStrBuf::getData(), GetEndTime(), GetStartTime(), GetValue(), and CStdFile::WriteString().

Referenced by RecordValue(), and Reset().

159 {
160  assert(!!rszFilename);
161  // nothing to write?
162  if (!fWrapped && !iBackLogPos) return false;
163  // try append if desired; create if unsuccessful
164  CStdFile out;
165  if (fAppend) if (!out.Append(rszFilename.getData())) fAppend = false;
166  if (!fAppend)
167  {
168  if (!out.Create(rszFilename.getData())) return false;
169  // print header
170  out.WriteString("t\tv\n\r");
171  }
172  // write out current timeframe
173  int iEndTime = GetEndTime();
174  StdStrBuf buf;
175  for (int iWriteTime = GetStartTime(); iWriteTime < iEndTime; ++iWriteTime)
176  {
177  buf.Format("%d\t%d\n\r", (int) iWriteTime, (int) GetValue(iWriteTime));
178  out.WriteString(buf.getData());
179  }
180  return true;
181 }
const char * getData() const
Definition: StdBuf.h:450
bool Create(const char *szFileName, bool fCompressed=false, bool fExecutable=false, bool fMemory=false)
Definition: CStdFile.cpp:53
virtual TimeType GetStartTime() const
void Format(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: StdBuf.cpp:181
bool Append(const char *szFilename, bool text=false)
Definition: CStdFile.cpp:136
virtual ValueType GetValue(TimeType iAtTime) const
virtual TimeType GetEndTime() const
bool WriteString(const char *szStr)
Definition: CStdFile.cpp:268

Here is the call graph for this function:

Here is the caller graph for this function:

C4Graph::ValueType C4TableGraph::GetAtValue ( TimeType  iAtTime) const

Definition at line 87 of file C4Network2Stats.cpp.

References GetEndTime(), GetStartTime(), and Inside().

Referenced by Update().

88 {
89  // must be inside buffer
90  assert(Inside(iAtTime, GetStartTime(), GetEndTime()-1));
91  // query it - can't be negative if inside start/end-time
92  return pValues[(iAtTime - iInitialStartTime) % iBackLogLength];
93 }
virtual TimeType GetStartTime() const
virtual TimeType GetEndTime() const
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:45

Here is the call graph for this function:

Here is the caller graph for this function:

DWORD C4Graph::GetColorDw ( ) const
inlineinherited

Definition at line 58 of file C4Network2Stats.h.

Referenced by C4Chart::DrawElement().

58 { return dwColor; }

Here is the caller graph for this function:

C4Graph::TimeType C4TableGraph::GetEndTime ( ) const
virtual

Implements C4Graph.

Definition at line 73 of file C4Network2Stats.cpp.

Referenced by DumpToFile(), GetAtValue(), GetValue(), and SetAvgValue().

74 {
75  // end time is current
76  return iTime;
77 }

Here is the caller graph for this function:

C4Graph::ValueType C4TableGraph::GetMaxValue ( ) const
virtual

Implements C4Graph.

Definition at line 128 of file C4Network2Stats.cpp.

129 {
130  int iPos0 = iBackLogPos ? iBackLogPos-1 : iBackLogPos;
131  ValueType iMaxVal = pAveragedValues[iPos0];
132  int i = iPos0; ValueType *p = pAveragedValues;
133  while (i--) iMaxVal = std::max(iMaxVal, *p++);
134  if (fWrapped)
135  {
136  i = iBackLogLength - iPos0;
137  while (--i) iMaxVal = std::max(iMaxVal, *++p);
138  }
139  return iMaxVal * fMultiplier;
140 }
float ValueType
C4Graph::ValueType C4TableGraph::GetMedianValue ( TimeType  iStartTime,
TimeType  iEndTime 
) const
virtual

Implements C4Graph.

Definition at line 103 of file C4Network2Stats.cpp.

References GetValue().

104 {
105  assert(iStartTime < iEndTime);
106  // safety: Never build median if no values are recorded
107  if (!iBackLogPos && !fWrapped) return 0;
108  // sum up and divide in the end - let's hope this will never be called for really large values that could overflow ValueType
109  ValueType iSum = GetValue(iStartTime), iNum=1;
110  for (; ++iStartTime < iEndTime; ++iNum) iSum += GetValue(iStartTime);
111  return iSum / iNum;
112 }
virtual ValueType GetValue(TimeType iAtTime) const
float ValueType

Here is the call graph for this function:

C4Graph::ValueType C4TableGraph::GetMinValue ( ) const
virtual

Implements C4Graph.

Definition at line 114 of file C4Network2Stats.cpp.

115 {
116  int iPos0 = iBackLogPos ? iBackLogPos-1 : iBackLogPos;
117  ValueType iMinVal = pAveragedValues[iPos0];
118  int i = iPos0; ValueType *p = pAveragedValues;
119  while (i--) iMinVal = std::min(iMinVal, *p++);
120  if (fWrapped)
121  {
122  i = iBackLogLength - iPos0;
123  while (--i) iMinVal = std::min(iMinVal, *++p);
124  }
125  return iMinVal * fMultiplier;
126 }
float ValueType
virtual const C4Graph* C4TableGraph::GetSeries ( int  iIndex) const
inlinevirtual

Implements C4Graph.

Definition at line 119 of file C4Network2Stats.h.

119 { return iIndex ? nullptr : this; }
virtual int C4TableGraph::GetSeriesCount ( ) const
inlinevirtual

Implements C4Graph.

Definition at line 118 of file C4Network2Stats.h.

118 { return 1; }
C4Graph::TimeType C4TableGraph::GetStartTime ( ) const
virtual

Implements C4Graph.

Definition at line 65 of file C4Network2Stats.cpp.

Referenced by DumpToFile(), GetAtValue(), GetValue(), SetAvgValue(), and Update().

66 {
67  // wrap? -> whole buffer used
68  if (fWrapped) return iTime - iBackLogLength;
69  // otherwise, just buffer to the start used
70  return iTime - iBackLogPos;
71 }

Here is the caller graph for this function:

const char* C4Graph::GetTitle ( ) const
inlineinherited

Definition at line 59 of file C4Network2Stats.h.

References StdStrBuf::getData().

Referenced by C4Chart::DrawElement().

59 { return szTitle.getData(); }
const char * getData() const
Definition: StdBuf.h:450

Here is the call graph for this function:

Here is the caller graph for this function:

C4Graph::ValueType C4TableGraph::GetValue ( TimeType  iAtTime) const
virtual

Implements C4Graph.

Definition at line 79 of file C4Network2Stats.cpp.

References GetEndTime(), GetStartTime(), and Inside().

Referenced by DumpToFile(), and GetMedianValue().

80 {
81  // must be inside buffer
82  assert(Inside(iAtTime, GetStartTime(), GetEndTime()-1));
83  // query it - can't be negative if inside start/end-time
84  return pAveragedValues[(iAtTime - iInitialStartTime) % iBackLogLength] * fMultiplier;
85 }
virtual TimeType GetStartTime() const
virtual TimeType GetEndTime() const
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:45

Here is the call graph for this function:

Here is the caller graph for this function:

void C4TableGraph::RecordValue ( ValueType  iValue)

Definition at line 142 of file C4Network2Stats.cpp.

References DumpToFile().

Referenced by C4Network2Stats::ExecuteFrame(), and C4Network2Stats::ExecuteSecond().

143 {
144  // rec value
145  pValues[iBackLogPos] = iValue;
146  // calc time
147  ++iTime;
148  if (++iBackLogPos >= iBackLogLength)
149  {
150  // create dump before overwriting last buffer
151  if (!!szDumpFile) DumpToFile(szDumpFile, fWrapped);
152  // restart buffer
153  fWrapped = true;
154  iBackLogPos = 0;
155  }
156 }
virtual bool DumpToFile(const StdStrBuf &rszFilename, bool fAppend) const

Here is the call graph for this function:

Here is the caller graph for this function:

void C4TableGraph::Reset ( TimeType  iToTime)

Definition at line 52 of file C4Network2Stats.cpp.

References DumpToFile().

Referenced by ~C4TableGraph().

53 {
54  // flush buffer
55  if (!!szDumpFile) DumpToFile(szDumpFile, fWrapped);
56  // reset stuff
57  iInitialStartTime = iTime = iToTime;
58  fWrapped = false;
59  iBackLogPos = 0;
60  *pValues = 0;
61 }
virtual bool DumpToFile(const StdStrBuf &rszFilename, bool fAppend) const

Here is the call graph for this function:

Here is the caller graph for this function:

void C4TableGraph::SetAverageTime ( int  iToTime)
virtual

Implements C4Graph.

Definition at line 183 of file C4Network2Stats.cpp.

184 {
185  // set new time; resetting valid, averaged range
186  if (iAveragedTime == iToTime) return;
187  assert(iToTime > 0);
188  iAvgRange = iToTime;
189  iAveragedTime = iInitialStartTime;
190 }
void C4TableGraph::SetAvgValue ( TimeType  iAtTime,
ValueType  iValue 
) const

Definition at line 95 of file C4Network2Stats.cpp.

References GetEndTime(), GetStartTime(), and Inside().

Referenced by Update().

96 {
97  // must be inside buffer
98  assert(Inside(iAtTime, GetStartTime(), GetEndTime()-1));
99  // set it - can't be negative if inside start/end-time
100  pAveragedValues[(iAtTime - iInitialStartTime) % iBackLogLength] = iValue;
101 }
virtual TimeType GetStartTime() const
virtual TimeType GetEndTime() const
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:45

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Graph::SetColorDw ( DWORD  dwClr)
inlineinherited

Definition at line 42 of file C4Network2Stats.h.

Referenced by C4Network2Stats::C4Network2Stats(), and C4Player::CreateGraphs().

42 { dwColor = dwClr; }

Here is the caller graph for this function:

void C4TableGraph::SetDumpFile ( StdStrBuf szFile)
inline

Definition at line 97 of file C4Network2Stats.h.

97 { szDumpFile = szFile; }
virtual void C4TableGraph::SetMultiplier ( ValueType  fToVal)
inlinevirtual

Implements C4Graph.

Definition at line 124 of file C4Network2Stats.h.

124 { fMultiplier = fToVal; }
void C4Graph::SetTitle ( const char *  szNewTitle)
inlineinherited

Definition at line 41 of file C4Network2Stats.h.

References StdStrBuf::Copy().

Referenced by C4Network2Stats::C4Network2Stats(), and C4Player::CreateGraphs().

41 { szTitle.Copy(szNewTitle); }
void Copy()
Definition: StdBuf.h:475

Here is the call graph for this function:

Here is the caller graph for this function:

void C4TableGraph::Update ( ) const
virtual

Reimplemented from C4Graph.

Definition at line 195 of file C4Network2Stats.cpp.

References Abs(), FORWARD_AVERAGE_FACTOR, GetAtValue(), GetStartTime(), and SetAvgValue().

196 {
197  // no averaging necessary?
198  if (pAveragedValues == pValues)
199  {
200  if (iAvgRange == 1) return;
201  // averaging necessary, but buffer not yet created: Create it!
202  pAveragedValues = new ValueType[iBackLogLength];
203  }
204  // up-to-date?
205  if (iAveragedTime == iTime) return;
206  assert(iAveragedTime < iTime); // must not have gone back!
207  // update it
208  int iStartTime = GetStartTime();
209 #ifdef FORWARD_AVERAGE
210  int iAvgFwRange = iAvgRange/FORWARD_AVERAGE_FACTOR;
211 #else
212  int iAvgFwRange = 0;
213 #endif
214  for (int iUpdateTime = std::max(iAveragedTime-iAvgFwRange-1, iStartTime); iUpdateTime < iTime; ++iUpdateTime)
215  {
216  ValueType iSum=0, iSumWeight=0, iWeight;
217  for (int iSumTime = std::max(iUpdateTime - iAvgRange, iStartTime); iSumTime < std::min(iUpdateTime + iAvgFwRange+1, iTime); ++iSumTime)
218  {
219  iWeight = (ValueType) iAvgRange - Abs(iUpdateTime - iSumTime) + 1;
220  iSum += GetAtValue(iSumTime) * iWeight;
221  iSumWeight += iWeight;
222  }
223  SetAvgValue(iUpdateTime, iSum / iSumWeight);
224  }
225  // now it's all up-to-date
226  iAveragedTime = iTime;
227 }
virtual TimeType GetStartTime() const
ValueType GetAtValue(TimeType iAtTime) const
#define FORWARD_AVERAGE_FACTOR
float ValueType
void SetAvgValue(TimeType iAtTime, ValueType iValue) const
T Abs(T val)
Definition: Standard.h:44

Here is the call graph for this function:


The documentation for this class was generated from the following files: