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 () 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

typedef int C4Graph::TimeType
inherited

Definition at line 28 of file C4Network2Stats.h.

typedef float C4Graph::ValueType
inherited

Definition at line 27 of file C4Network2Stats.h.

Member Enumeration Documentation

anonymous enum
Enumerator
DefaultBlockLength 

Definition at line 86 of file C4Network2Stats.h.

86 { DefaultBlockLength = 256 }; // default backlog

Constructor & Destructor Documentation

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 ( )
override

Definition at line 41 of file C4Network2Stats.cpp.

References Reset().

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)

Here is the call graph for this function:

Member Function Documentation

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

Definition at line 156 of file C4Network2Stats.cpp.

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

Referenced by RecordValue(), and Reset().

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 }
const char * getData() const
Definition: StdBuf.h:442
ValueType GetValue(TimeType iAtTime) const override
bool Create(const char *szFileName, bool fCompressed=false, bool fExecutable=false, bool fMemory=false)
Definition: CStdFile.cpp:49
void Format(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: StdBuf.cpp:174
TimeType GetEndTime() const override
TimeType GetStartTime() const override
bool Append(const char *szFilename, bool text=false)
Definition: CStdFile.cpp:132
bool WriteString(const char *szStr)
Definition: CStdFile.cpp:264

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 85 of file C4Network2Stats.cpp.

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

Referenced by Update().

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 }
TimeType GetEndTime() const override
TimeType GetStartTime() const override
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:43

Here is the call graph for this function:

Here is the caller graph for this function:

DWORD C4Graph::GetColorDw ( ) const
inlineinherited

Definition at line 55 of file C4Network2Stats.h.

Referenced by C4Chart::DrawElement().

55 { return dwColor; }

Here is the caller graph for this function:

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

Implements C4Graph.

Definition at line 71 of file C4Network2Stats.cpp.

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

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

Here is the caller graph for this function:

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 }
float ValueType
C4Graph::ValueType C4TableGraph::GetMedianValue ( TimeType  iStartTime,
TimeType  iEndTime 
) const
overridevirtual

Implements C4Graph.

Definition at line 101 of file C4Network2Stats.cpp.

References GetValue().

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 }
ValueType GetValue(TimeType iAtTime) const override
float ValueType

Here is the call graph for this function:

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 }
float ValueType
const C4Graph* C4TableGraph::GetSeries ( int  iIndex) const
inlineoverridevirtual

Implements C4Graph.

Definition at line 116 of file C4Network2Stats.h.

116 { return iIndex ? nullptr : this; }
int C4TableGraph::GetSeriesCount ( ) const
inlineoverridevirtual

Implements C4Graph.

Definition at line 115 of file C4Network2Stats.h.

115 { return 1; }
C4Graph::TimeType C4TableGraph::GetStartTime ( ) const
overridevirtual

Implements C4Graph.

Definition at line 63 of file C4Network2Stats.cpp.

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

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 }

Here is the caller graph for this function:

const char* C4Graph::GetTitle ( ) const
inlineinherited

Definition at line 56 of file C4Network2Stats.h.

References StdStrBuf::getData().

Referenced by C4Chart::DrawElement().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

Implements C4Graph.

Definition at line 77 of file C4Network2Stats.cpp.

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

Referenced by DumpToFile(), and GetMedianValue().

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 }
TimeType GetEndTime() const override
TimeType GetStartTime() const override
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:43

Here is the call graph for this function:

Here is the caller graph for this function:

void C4TableGraph::RecordValue ( ValueType  iValue)

Definition at line 140 of file C4Network2Stats.cpp.

References DumpToFile().

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

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

Here is the call graph for this function:

Here is the caller graph for this function:

void C4TableGraph::Reset ( TimeType  iToTime)

Definition at line 50 of file C4Network2Stats.cpp.

References DumpToFile().

Referenced by ~C4TableGraph().

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 }
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)
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 }
void C4TableGraph::SetAvgValue ( TimeType  iAtTime,
ValueType  iValue 
) const

Definition at line 93 of file C4Network2Stats.cpp.

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

Referenced by Update().

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 }
TimeType GetEndTime() const override
TimeType GetStartTime() const override
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:43

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 39 of file C4Network2Stats.h.

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

39 { dwColor = dwClr; }

Here is the caller graph for this function:

void C4TableGraph::SetDumpFile ( StdStrBuf szFile)
inline

Definition at line 94 of file C4Network2Stats.h.

94 { szDumpFile = szFile; }
void C4TableGraph::SetMultiplier ( ValueType  fToVal)
inlineoverridevirtual

Implements C4Graph.

Definition at line 121 of file C4Network2Stats.h.

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

Definition at line 38 of file C4Network2Stats.h.

References StdStrBuf::Copy().

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

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

Here is the call graph for this function:

Here is the caller graph for this function:

void C4TableGraph::Update ( ) const
overridevirtual

Reimplemented from C4Graph.

Definition at line 193 of file C4Network2Stats.cpp.

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

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 }
TimeType GetStartTime() const override
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:42

Here is the call graph for this function:


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