cpp-common
Public Member Functions | Private Attributes | List of all members
nvsl::StatsFreq< T > Class Template Reference

Stat to measure freq of elements with a name and a description. More...

#include <stats.hh>

Inheritance diagram for nvsl::StatsFreq< T >:
Inheritance graph
[legend]
Collaboration diagram for nvsl::StatsFreq< T >:
Collaboration graph
[legend]

Public Member Functions

 StatsFreq (bool reg=true)
 
void init (const std::string &name, const std::string &desc, size_t bucket_cnt, T bucket_min, T bucket_max)
 Initialize the stats' buckets. More...
 
void add (T val, size_t count=1)
 Add a value to the frequency map. More...
 
size_t total () const
 Get the total number of samples. More...
 
size_t bucket_count (size_t bucket) const
 Get the number of samples in a bucket. More...
 
size_t uoflow_count (bool underflow_cnt, bool overflow_cnt) const
 Get the number of samples in overflow and underflow buckets. More...
 
std::string str () const
 Generate a string representation of the frequency map.
 
- Public Member Functions inherited from nvsl::StatsBase
 StatsBase (bool reg)
 
void init (const std::string &name, const std::string &desc)
 
virtual double avg () const
 
virtual std::string latex (const std::string &prefix="") const
 
 StatsBase (bool reg)
 
void init (const std::string &name, const std::string &desc)
 
virtual double avg () const
 
virtual std::string latex (const std::string &prefix="") const
 

Private Attributes

size_t bucket_cnt
 
bucket_min
 
bucket_max
 
bucket_sz
 
size_t * counts
 
size_t underflow_cnt
 
size_t overflow_cnt
 

Additional Inherited Members

- Protected Attributes inherited from nvsl::StatsBase
std::string stat_name
 
std::string stat_desc
 

Detailed Description

template<typename T = size_t>
class nvsl::StatsFreq< T >

Stat to measure freq of elements with a name and a description.

Definition at line 74 of file stats.hh.

Member Function Documentation

◆ add()

template<typename T = size_t>
void nvsl::StatsFreq< T >::add ( val,
size_t  count = 1 
)
inline

Add a value to the frequency map.

Parameters
[in]valValue to sample
[in]count=1Number of times to add this value to the map

Definition at line 126 of file stats.hh.

126  {
127  if (val < bucket_min) {
128  underflow_cnt++;
129  } else if (val >= bucket_max) {
130  overflow_cnt++;
131  } else {
132  size_t bucket_idx = (val-bucket_min)/bucket_sz;
133  counts[bucket_idx]++;
134  }
135  }

◆ bucket_count()

template<typename T = size_t>
size_t nvsl::StatsFreq< T >::bucket_count ( size_t  bucket) const
inline

Get the number of samples in a bucket.

Parameters
[in]bucketBucket index
Returns
Number of samples in the bucket

Definition at line 151 of file stats.hh.

151  {
152  return counts[bucket];
153  }

◆ init()

template<typename T = size_t>
void nvsl::StatsFreq< T >::init ( const std::string &  name,
const std::string &  desc,
size_t  bucket_cnt,
bucket_min,
bucket_max 
)
inline

Initialize the stats' buckets.

Parameters
nameName of the stat
descDescription of the stat
bucket_cntNumber of buckets
bucket_minMinimum value of the bucket
bucket_maxMaximum value of the bucket
bucket_szSize of the bucket

Definition at line 96 of file stats.hh.

97  {
98 #if defined(DBGE)
99  NVSL_ASSERT(bucket_cnt != 0, "Bucket size cannot be zero");
100  NVSL_ASSERT(bucket_max > bucket_min,
101  "Bucket max cannot be smaller than bucket min");
102 #else
103  assert(bucket_cnt != 0 && "Bucket size cannot be zero");
104  assert(bucket_max > bucket_min &&
105  "Bucket max cannot be smaller than bucket min");
106 #endif // DBGE
107 
108  StatsBase::init(name, desc);
109 
110  this->bucket_cnt = bucket_cnt;
111  this->bucket_min = bucket_min;
112  this->bucket_max = bucket_max;
113  this->bucket_sz = (bucket_max-bucket_min)/bucket_cnt;
114  this->underflow_cnt = 0;
115  this->overflow_cnt = 0;
116 
117  this->counts = new size_t[bucket_cnt];
118  memset(counts, 0, sizeof(counts[0])*bucket_cnt);
119  }

◆ total()

template<typename T = size_t>
size_t nvsl::StatsFreq< T >::total ( ) const
inline

Get the total number of samples.

Returns
Total number of samples

Definition at line 141 of file stats.hh.

141  {
142  return underflow_cnt + overflow_cnt +
143  std::accumulate(counts, counts+bucket_cnt, 0);
144  }

◆ uoflow_count()

template<typename T = size_t>
size_t nvsl::StatsFreq< T >::uoflow_count ( bool  underflow_cnt,
bool  overflow_cnt 
) const
inline

Get the number of samples in overflow and underflow buckets.

Parameters
[in]underflow_cntReturn the number of samples in underflow bucket
[in]overflow_cntReturn the number of samples in overflow bucket
Returns
Number of samples in overflow and underflow buckets (whichever is enabled)

Definition at line 161 of file stats.hh.

161  {
162  return (underflow_cnt ? this->underflow_cnt : 0) +
163  (overflow_cnt ? this->overflow_cnt : 0);
164  }

The documentation for this class was generated from the following file:
NVSL_ASSERT
#define NVSL_ASSERT(cond, msg)
Assert a condition w/ msg and generate backtrace on fail.
Definition: error.hh:73