HOG2
StatCollection.cpp
Go to the documentation of this file.
1 /*
2  * $Id: StatCollection.cpp
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 6/1/05.
6  * Modified by Nathan Sturtevant on 02/29/20.
7  *
8  * This file is part of HOG2. See https://github.com/nathansttt/hog2 for licensing information.
9  *
10  */
11 
12 #include "StatCollection.h"
13 #include <cstring>
14 #include <cstdio>
15 
16 
17 // typedef union { double fval; long lval; } statValue;
18 
19 //class stat {
20 //public:
21 // int category, owner;
22 // statValue value;
23 //};
24 
25 //std::vector<char *> categories;
26 //std::vector<char *> owners;
27 //std::vector<stat> stats;
28 
30 :categories(), owners(), stats()
31 {
32  printOutput = false;
33 }
34 
36 {
37  /*
38  for (unsigned int x = 0; x < categories.size(); x++)
39  {
40  delete [] categories[x];
41  categories[x] = 0;
42  }
43 
44  for (unsigned int x = 0; x < owners.size(); x++)
45  {
46  delete [] owners[x];
47  owners[x] = 0;
48  }
49 
50  for (unsigned int x = 0; x < excludeFilters.size(); x++)
51  {
52  delete [] excludeFilters[x];
53  excludeFilters[x] = 0;
54  }
55 
56  for (unsigned int x = 0; x < includeFilters.size(); x++)
57  {
58  delete [] includeFilters[x];
59  includeFilters[x] = 0;
60  }
61  */
62 }
63 
67 void StatCollection::AddStat(const char *category, const char *owner, double value)
68 {
69  if (!passFilter(category))
70  return;
71  int catID, ownerID;
72  catID = addCategory(category);
73  ownerID = addOwner(owner);
74  stats.resize(stats.size()+1);
75  stats[stats.size()-1].category = catID;
76  stats[stats.size()-1].owner = ownerID;
77  stats[stats.size()-1].value.fval = value;
78  stats[stats.size()-1].sType = floatStored;
79 
80  if (printOutput)
81  printf("%s\t%s\t%1.2f\n", category, owner, value);
82 }
83 
87 void StatCollection::AddStat(const char *category, const char *owner, long value)
88 {
89  if (!passFilter(category))
90  return;
91  int catID, ownerID;
92  catID = addCategory(category);
93  ownerID = addOwner(owner);
94  stats.resize(stats.size()+1);
95  stats[stats.size()-1].category = catID;
96  stats[stats.size()-1].owner = ownerID;
97  stats[stats.size()-1].value.lval = value;
98  stats[stats.size()-1].sType = longStored;
99  if (printOutput)
100  printf("%s\t%s\t%ld\n", category, owner, value);
101 }
102 
109 void StatCollection::SumStat(const char *category, const char *owner, double value)
110 {
111  statValue *sv = getLastStat(category, owner);
112  if (sv)
113  {
114 // if (sv->sType != floatStored)
115 // printf("Warning: Adding double to value previous stored as long\n");
116  sv->fval += value;
117  if (printOutput)
118  printf("%s\t%s\t%1.2f\n", category, owner, sv->fval);
119  }
120  else
121  AddStat(category, owner, value);
122 }
123 
130 void StatCollection::SumStat(const char *category, const char *owner, long value)
131 {
132  statValue *sv = getLastStat(category, owner);
133  if (sv)
134  {
135 // if (sv->sType != floatStored)
136 // printf("Warning: Adding long to value previous stored as double\n");
137  sv->lval += value;
138  if (printOutput)
139  printf("%s\t%s\t%ld\n", category, owner, sv->lval);
140  }
141  else
142  AddStat(category, owner, value);
143 }
144 
149 {
150  stats.resize(0);
151 }
152 
153 //void StatCollection::clearOwnerStats(char *owner);
154 //void StatCollection::clearCategoryStats(char *category);
159 {
160  return (int)stats.size();
161 }
162 
166 const statistics *StatCollection::GetStatNum(int which) const
167 {
168  return &stats[which];
169 }
170 
174 const char *StatCollection::lookupCategoryID(int id) const
175 {
176  return categories[id].c_str();
177 }
178 
182 const char *StatCollection::LookupOwnerID(int id) const
183 {
184  return owners[id].c_str();
185 }
186 
192 void StatCollection::AddFilter(const char *category)
193 {
194  AddIncludeFilter(category);
195 }
196 
197 void StatCollection::AddIncludeFilter(const char *category) // include only added categories
198 {
199  includeFilters.push_back(category);
200  /*
201  char *str = new char [strlen(category)+1];
202  strcpy(str, category);
203  includeFilters.push_back(str);
204  */
205 }
206 
207 void StatCollection::AddExcludeFilter(const char *category) // exclude only added categories
208 {
209  excludeFilters.push_back(category);
210  /*
211  char *str = new char [strlen(category)+1];
212  strcpy(str, category);
213  excludeFilters.push_back(str);
214  */
215 }
216 
221 {
222  /*
223  for (unsigned int x = 0; x < excludeFilters.size(); x++)
224  {
225  delete [] excludeFilters[x];
226  excludeFilters[x] = 0;
227  }
228  for (unsigned int x = 0; x < includeFilters.size(); x++)
229  {
230  delete [] includeFilters[x];
231  includeFilters[x] = 0;
232  }
233  */
234  excludeFilters.resize(0);
235  includeFilters.resize(0);
236 }
237 
241 int StatCollection::LookupCategory(const char *category) const
242 {
243  for (unsigned int x = 0; x < categories.size(); x++)
244  {
245  if (categories[x] == category)
246  return x;
247  }
248  //if (strcmp(category, categories[x]) == 0)
249  //return x;
250  return -1;
251 }
252 
253 
258 int StatCollection::addCategory(const char *category)
259 {
260  int id = LookupCategory(category);
261  if (id != -1)
262  return id;
263  categories.push_back(category);
264  /*
265  char *str = new char [strlen(category)+1];
266  strcpy(str, category);
267  categories.push_back(str);
268  */
269  return categories.size()-1;
270 }
271 
275 int StatCollection::LookupOwner(const char *owner) const
276 {
277  for (unsigned int x = 0; x < owners.size(); x++)
278  if (owners[x] == owner)
279  return x;
280  //if (strcmp(owner, owners[x]) == 0)
281  //return x;
282  return -1;
283 }
284 
289 int StatCollection::addOwner(const char *owner)
290 {
291  int id = LookupOwner(owner);
292  if (id != -1)
293  return id;
294  owners.push_back(owner);
295  /*
296  char *str = new char [strlen(owner)+1];
297  strcpy(str, owner);
298  owners.push_back(str);
299  */
300  return owners.size()-1;
301 }
302 
307 bool StatCollection::LookupStat(const char *category, const char *owner, statValue &v) const
308 {
309  if (!passFilter(category))
310  {
311  return false;
312  }
313  int catID, ownerID;
314  catID = LookupCategory(category);
315  ownerID = LookupOwner(owner);
316  for (int x = (int)stats.size()-1; x >= 0; x--)
317  {
318  if ((stats[x].category == catID) && (stats[x].owner == ownerID))
319  {
320  v = stats[x].value;
321  return true;
322  }
323  }
324  return false;
325 }
326 
327 bool StatCollection::LookupStat(unsigned int index, statValue &v) const
328 {
329  if (index < stats.size())
330  {
331  v = stats[index].value;
332  return true;
333  }
334  return false;
335 }
336 
341 statValue *StatCollection::getLastStat(const char *category, const char *owner)
342 {
343  if (!passFilter(category))
344  return 0;
345  int catID, ownerID;
346  catID = LookupCategory(category);
347  ownerID = LookupOwner(owner);
348  for (int x = (int)stats.size()-1; x >= 0; x--)
349  {
350  if ((stats[x].category == catID) && (stats[x].owner == ownerID))
351  return &stats[x].value;
352  }
353  return 0;
354 }
355 
359 bool StatCollection::passFilter(const char *category) const
360 {
361  if ((includeFilters.size() == 0) && (excludeFilters.size() == 0))
362  return true;
363 
364  for (unsigned int x = 0; x < excludeFilters.size(); x++)
365  if (excludeFilters[x] == category)
366  return false;
367  //if (strcmp(excludeFilters[x], category) == 0)
368  //return false;
369 
370  if (includeFilters.size() == 0)
371  return true;
372 
373  for (unsigned int x = 0; x < includeFilters.size(); x++)
374  {
375  if (includeFilters[x] == category)
376  return true;
377  //if (strcmp(includeFilters[x], category) == 0)
378  //return true;
379  }
380 
381  return false;
382 }
383 
388 int StatCollection::FindNextStat(const char *category, const char *owner, int startIndex) const
389 {
390  // Check if the category has been registered
391  if (!passFilter(category))
392  return -1;
393 
394  // If the given startIndex is out of range of the stats table size, reset it to zero
395  if (startIndex < 0 || startIndex > (int)stats.size()-1)
396  startIndex = 0;
397 
398  int catID, ownerID;
399  catID = LookupCategory(category);
400  ownerID = LookupOwner(owner);
401 
402  for (int x = startIndex; x < (int)stats.size(); x++)
403  {
404  if ((stats[x].category == catID) && (stats[x].owner == ownerID))
405  return x;
406  }
407 
408  return -1;
409 }
410 
415 int StatCollection::FindPrevStat(const char *category, const char *owner, int startIndex) const
416 {
417  // Check if the category has been registered
418  if (!passFilter(category))
419  return -1;
420 
421  // If the given startIndex is out of range of the stats table size, reset it to the last
422  // index
423  if (startIndex < 0 || startIndex > (int)stats.size() - 1)
424  startIndex = (int) stats.size() -1;
425 
426  int catID, ownerID;
427  catID = LookupCategory(category);
428  ownerID = LookupOwner(owner);
429 
430  for (int x = startIndex; x > 0; x--)
431  {
432  if ((stats[x].category == catID) && (stats[x].owner == ownerID))
433  return x;
434  }
435 
436  return -1;
437 }
438 
444 int StatCollection::FindNextStat(const char *what, bool findCategory, int startIndex) const
445 {
446  // Check if the category has been registered
447  if (findCategory && (!passFilter(what)))
448  return -1;
449 
450  // If the given startIndex is out of range of the stats table size, reset it to zero
451  if (startIndex < 0 || startIndex > (int)stats.size()-1)
452  startIndex = 0;
453 
454  int ID;
455  if (findCategory)
456  ID = LookupCategory(what);
457  else
458  ID = LookupOwner(what);
459 
460  for (int x = startIndex; x < (int)stats.size(); x++)
461  {
462  if ((findCategory && (stats[x].category == ID)) ||
463  (!findCategory && (stats[x].owner == ID)))
464  return x;
465  }
466 
467  return -1;
468 }
469 
475 int StatCollection::FindPrevStat(const char *what, bool findCategory, int startIndex) const
476 {
477  // Check if the category has been registered
478  if (findCategory && (!passFilter(what)))
479  return -1;
480 
481  // If the given startIndex is out of range of the stats table size, reset it to the last
482  // index
483  if (startIndex < 0 || startIndex > (int)stats.size() - 1)
484  startIndex = (int) stats.size() -1;
485 
486  int ID;
487  if (findCategory)
488  ID = LookupCategory(what);
489  else
490  ID = LookupOwner(what);
491 
492  for (int x = startIndex; x > 0; x--)
493  {
494  if ((findCategory && (stats[x].category == ID)) ||
495  (!findCategory && (stats[x].owner == ID)))
496  return x;
497  }
498 
499  return -1;
500 }
501 
502 
503 /*
504  * Print the Stats Table for debugging purpose for now.
505  */
507 {
508  printf("Stats Table:\n----------------------------\n");
509 
510  for (int x = 0; x < (int)stats.size(); x++)
511  if (stats[x].sType == floatStored)
512  printf("%d \t%s \t%s \t%le\n", x, categories[stats[x].category].c_str(),
513  owners[stats[x].owner].c_str(), stats[x].value.fval);
514  else
515  printf("%d \t%s \t%s \t%ld\n", x, categories[stats[x].category].c_str(),
516  owners[stats[x].owner].c_str(), stats[x].value.lval);
517 }
StatCollection::FindPrevStat
int FindPrevStat(const char *category, const char *owner, int startIndex=-1) const
Find the previous stat entry that matches the given category and owner name.
Definition: StatCollection.cpp:415
StatCollection::passFilter
bool passFilter(const char *category) const
Check to see if the category stats should be saved.
Definition: StatCollection.cpp:359
floatStored
@ floatStored
Definition: StatCollection.h:19
StatCollection.h
StatCollection::AddFilter
void AddFilter(const char *category)
Adding a filter will cause only stats of the given category to be collected.
Definition: StatCollection.cpp:192
statValue
Definition: StatCollection.h:18
StatCollection::categories
std::vector< std::string > categories
Definition: StatCollection.h:76
StatCollection::LookupStat
bool LookupStat(const char *category, const char *owner, statValue &) const
Find the last stat entered that matches the category and owner.
Definition: StatCollection.cpp:307
StatCollection::LookupOwnerID
const char * LookupOwnerID(int id) const
Given a owner ID, return the text description.
Definition: StatCollection.cpp:182
StatCollection::stats
std::vector< statistics > stats
Definition: StatCollection.h:80
StatCollection::StatCollection
StatCollection()
Definition: StatCollection.cpp:29
StatCollection::PrintStatsTable
void PrintStatsTable() const
Definition: StatCollection.cpp:506
StatCollection::~StatCollection
~StatCollection()
Definition: StatCollection.cpp:35
StatCollection::LookupCategory
int LookupCategory(const char *category) const
Given a category, look up the ID.
Definition: StatCollection.cpp:241
StatCollection::SumStat
void SumStat(const char *category, const char *owner, double value)
Given stats for the category and owner, find an existing stat (chronologically backwards search) with...
Definition: StatCollection.cpp:109
statValue::fval
double fval
Definition: StatCollection.h:18
StatCollection::ClearAllStats
void ClearAllStats()
Remove all stat entries from the collection.
Definition: StatCollection.cpp:148
StatCollection::includeFilters
std::vector< std::string > includeFilters
Definition: StatCollection.h:78
StatCollection::getLastStat
statValue * getLastStat(const char *category, const char *owner)
Find the last stat entered that matches the category and owner.
Definition: StatCollection.cpp:341
StatCollection::AddExcludeFilter
void AddExcludeFilter(const char *category)
Definition: StatCollection.cpp:207
StatCollection::ClearFilters
void ClearFilters()
Clear any filters being used for stat entry.
Definition: StatCollection.cpp:220
StatCollection::lookupCategoryID
const char * lookupCategoryID(int id) const
Given a category ID, return the text description.
Definition: StatCollection.cpp:174
StatCollection::addCategory
int addCategory(const char *category)
Add a new category to the category list.
Definition: StatCollection.cpp:258
StatCollection::GetStatNum
const statistics * GetStatNum(int which) const
Return the nth stat which has been collected.
Definition: StatCollection.cpp:166
StatCollection::excludeFilters
std::vector< std::string > excludeFilters
Definition: StatCollection.h:79
StatCollection::printOutput
bool printOutput
Definition: StatCollection.h:81
statistics
Definition: StatCollection.h:21
StatCollection::owners
std::vector< std::string > owners
Definition: StatCollection.h:77
statValue::lval
long lval
Definition: StatCollection.h:18
StatCollection::addOwner
int addOwner(const char *owner)
Add a new owner to the owner list.
Definition: StatCollection.cpp:289
StatCollection::AddStat
void AddStat(const char *category, const char *owner, double value)
Add a new stat entry for the given category, owner and value.
Definition: StatCollection.cpp:67
StatCollection::AddIncludeFilter
void AddIncludeFilter(const char *category)
Definition: StatCollection.cpp:197
longStored
@ longStored
Definition: StatCollection.h:19
StatCollection::GetNumStats
int GetNumStats() const
The number of stats collected so far.
Definition: StatCollection.cpp:158
StatCollection::FindNextStat
int FindNextStat(const char *category, const char *owner, int startIndex=0) const
Find the next stat entry that matches the given category and owner name.
Definition: StatCollection.cpp:388
StatCollection::LookupOwner
int LookupOwner(const char *owner) const
Given an owner, look up the ID.
Definition: StatCollection.cpp:275