OpenClonk
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 () override
 
void Reset (TimeType iToTime)
 
void SetDumpFile (StdStrBuf &szFile)
 
TimeType GetStartTime () const override
 
TimeType GetEndTime () const override
 
ValueType GetValue (TimeType iAtTime) const override
 
ValueType GetAtValue (TimeType iAtTime) const
 
void SetAvgValue (TimeType iAtTime, ValueType iValue) const
 
ValueType GetMedianValue (TimeType iStartTime, TimeType iEndTime) const override
 
ValueType GetMinValue () const override
 
ValueType GetMaxValue () const override
 
void RecordValue (ValueType iValue)
 
virtual bool DumpToFile (const StdStrBuf &rszFilename, bool fAppend) const
 
int GetSeriesCount () const override
 
const C4GraphGetSeries (int iIndex) const override
 
void SetAverageTime (int iToTime) override
 
void Update () const override
 
void SetMultiplier (ValueType fToVal) override
 
void SetTitle (const char *szNewTitle)
 
void SetColorDw (DWORD dwClr)
 
DWORD GetColorDw () const
 
const char * GetTitle () const
 

Detailed Description

Definition at line 66 of file C4Network2Stats.h.

Member Typedef Documentation

◆ TimeType

typedef int C4Graph::TimeType
inherited

Definition at line 28 of file C4Network2Stats.h.

◆ ValueType

typedef float C4Graph::ValueType
inherited

Definition at line 27 of file C4Network2Stats.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
DefaultBlockLength 

Definition at line 86 of file C4Network2Stats.h.

86 { DefaultBlockLength = 256 }; // default backlog

Constructor & Destructor Documentation

◆ C4TableGraph()

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

Definition at line 32 of file C4Network2Stats.cpp.

33  : iBackLogLength(iBackLogLength), iInitialStartTime(iStartTime), iTime(iStartTime), iAveragedTime(iStartTime)
34 {
35  // create value buffer
36  assert(iBackLogLength);
37  pValues = pAveragedValues = new ValueType[iBackLogLength];
38  *pValues = 0;
39 }
float ValueType

◆ ~C4TableGraph()

C4TableGraph::~C4TableGraph ( )
override

Definition at line 41 of file C4Network2Stats.cpp.

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

References Reset().

Here is the call graph for this function:

Member Function Documentation

◆ DumpToFile()

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

Definition at line 156 of file C4Network2Stats.cpp.

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

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

Referenced by RecordValue(), and Reset().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetAtValue()

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

Definition at line 85 of file C4Network2Stats.cpp.

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

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

Referenced by Update().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetColorDw()

DWORD C4Graph::GetColorDw ( ) const
inlineinherited

Definition at line 55 of file C4Network2Stats.h.

55 { return dwColor; }

Referenced by C4Chart::DrawElement().

Here is the caller graph for this function:

◆ GetEndTime()

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

Implements C4Graph.

Definition at line 71 of file C4Network2Stats.cpp.

72 {
73  // end time is current
74  return iTime;
75 }

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

Here is the caller graph for this function:

◆ GetMaxValue()

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

Implements C4Graph.

Definition at line 126 of file C4Network2Stats.cpp.

127 {
128  int iPos0 = iBackLogPos ? iBackLogPos-1 : iBackLogPos;
129  ValueType iMaxVal = pAveragedValues[iPos0];
130  int i = iPos0; ValueType *p = pAveragedValues;
131  while (i--) iMaxVal = std::max(iMaxVal, *p++);
132  if (fWrapped)
133  {
134  i = iBackLogLength - iPos0;
135  while (--i) iMaxVal = std::max(iMaxVal, *++p);
136  }
137  return iMaxVal * fMultiplier;
138 }

◆ GetMedianValue()

C4Graph::ValueType C4TableGraph::GetMedianValue ( TimeType  iStartTime,
TimeType  iEndTime 
) const
overridevirtual

Implements C4Graph.

Definition at line 101 of file C4Network2Stats.cpp.

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

References GetValue().

Here is the call graph for this function:

◆ GetMinValue()

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

Implements C4Graph.

Definition at line 112 of file C4Network2Stats.cpp.

113 {
114  int iPos0 = iBackLogPos ? iBackLogPos-1 : iBackLogPos;
115  ValueType iMinVal = pAveragedValues[iPos0];
116  int i = iPos0; ValueType *p = pAveragedValues;
117  while (i--) iMinVal = std::min(iMinVal, *p++);
118  if (fWrapped)
119  {
120  i = iBackLogLength - iPos0;
121  while (--i) iMinVal = std::min(iMinVal, *++p);
122  }
123  return iMinVal * fMultiplier;
124 }

◆ GetSeries()

const C4Graph* C4TableGraph::GetSeries ( int  iIndex) const
inlineoverridevirtual

Implements C4Graph.

Definition at line 116 of file C4Network2Stats.h.

116 { return iIndex ? nullptr : this; }

◆ GetSeriesCount()

int C4TableGraph::GetSeriesCount ( ) const
inlineoverridevirtual

Implements C4Graph.

Definition at line 115 of file C4Network2Stats.h.

115 { return 1; }

◆ GetStartTime()

C4Graph::TimeType C4TableGraph::GetStartTime ( ) const
overridevirtual

Implements C4Graph.

Definition at line 63 of file C4Network2Stats.cpp.

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

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

Here is the caller graph for this function:

◆ GetTitle()

const char* C4Graph::GetTitle ( ) const
inlineinherited

Definition at line 56 of file C4Network2Stats.h.

56 { return szTitle.getData(); }

References StdStrBuf::getData().

Referenced by C4Chart::DrawElement().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetValue()

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

Implements C4Graph.

Definition at line 77 of file C4Network2Stats.cpp.

78 {
79  // must be inside buffer
80  assert(Inside(iAtTime, GetStartTime(), GetEndTime()-1));
81  // query it - can't be negative if inside start/end-time
82  return pAveragedValues[(iAtTime - iInitialStartTime) % iBackLogLength] * fMultiplier;
83 }

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

Referenced by DumpToFile(), and GetMedianValue().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ RecordValue()

void C4TableGraph::RecordValue ( ValueType  iValue)

Definition at line 140 of file C4Network2Stats.cpp.

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

References DumpToFile().

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Reset()

void C4TableGraph::Reset ( TimeType  iToTime)

Definition at line 50 of file C4Network2Stats.cpp.

51 {
52  // flush buffer
53  if (!!szDumpFile) DumpToFile(szDumpFile, fWrapped);
54  // reset stuff
55  iInitialStartTime = iTime = iToTime;
56  fWrapped = false;
57  iBackLogPos = 0;
58  *pValues = 0;
59 }

References DumpToFile().

Referenced by ~C4TableGraph().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetAverageTime()

void C4TableGraph::SetAverageTime ( int  iToTime)
overridevirtual

Implements C4Graph.

Definition at line 181 of file C4Network2Stats.cpp.

182 {
183  // set new time; resetting valid, averaged range
184  if (iAveragedTime == iToTime) return;
185  assert(iToTime > 0);
186  iAvgRange = iToTime;
187  iAveragedTime = iInitialStartTime;
188 }

◆ SetAvgValue()

void C4TableGraph::SetAvgValue ( TimeType  iAtTime,
ValueType  iValue 
) const

Definition at line 93 of file C4Network2Stats.cpp.

94 {
95  // must be inside buffer
96  assert(Inside(iAtTime, GetStartTime(), GetEndTime()-1));
97  // set it - can't be negative if inside start/end-time
98  pAveragedValues[(iAtTime - iInitialStartTime) % iBackLogLength] = iValue;
99 }

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

Referenced by Update().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetColorDw()

void C4Graph::SetColorDw ( DWORD  dwClr)
inlineinherited

Definition at line 39 of file C4Network2Stats.h.

39 { dwColor = dwClr; }

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

Here is the caller graph for this function:

◆ SetDumpFile()

void C4TableGraph::SetDumpFile ( StdStrBuf szFile)
inline

Definition at line 94 of file C4Network2Stats.h.

94 { szDumpFile = szFile; }

◆ SetMultiplier()

void C4TableGraph::SetMultiplier ( ValueType  fToVal)
inlineoverridevirtual

Implements C4Graph.

Definition at line 121 of file C4Network2Stats.h.

121 { fMultiplier = fToVal; }

◆ SetTitle()

void C4Graph::SetTitle ( const char *  szNewTitle)
inlineinherited

Definition at line 38 of file C4Network2Stats.h.

38 { szTitle.Copy(szNewTitle); }
void Copy()
Definition: StdBuf.h:467

References StdStrBuf::Copy().

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Update()

void C4TableGraph::Update ( ) const
overridevirtual

Reimplemented from C4Graph.

Definition at line 193 of file C4Network2Stats.cpp.

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

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

Here is the call graph for this function:

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