INET Framework for OMNeT++/OMNEST
inet::httptools::rdHistogram Class Reference

Histogram distribution random object. More...

#include <HttpRandom.h>

Inheritance diagram for inet::httptools::rdHistogram:
inet::httptools::rdObject

Classes

struct  rdHistogramBin
 

Public Member Functions

 rdHistogram (rdHistogramBins bins, bool zeroBased=false)
 
 rdHistogram (cXMLAttributeMap attributes)
 
double draw () override
 
- Public Member Functions inherited from inet::httptools::rdObject
virtual ~rdObject ()
 
DISTR_TYPE getType ()
 
std::string typeStr ()
 
virtual std::string toString ()
 

Protected Types

typedef std::vector< rdHistogramBinrdHistogramBins
 

Protected Attributes

rdHistogramBins m_bins
 
bool m_zeroBased = false
 
- Protected Attributes inherited from inet::httptools::rdObject
DISTR_TYPE m_type = dt_normal
 

Private Member Functions

void __parseBinString (std::string binstr)
 
void __normalizeBins ()
 

Additional Inherited Members

- Protected Member Functions inherited from inet::httptools::rdObject
bool _hasKey (cXMLAttributeMap attributes, std::string key)
 

Detailed Description

Histogram distribution random object.

Member Typedef Documentation

Constructor & Destructor Documentation

inet::httptools::rdHistogram::rdHistogram ( rdHistogramBins  bins,
bool  zeroBased = false 
)
146 {
147  m_zeroBased = zeroBased;
148  m_bins = rdHistogramBins(bins);
149  __normalizeBins();
150 }
void __normalizeBins()
Definition: HttpRandom.cc:212
bool m_zeroBased
Definition: HttpRandom.h:192
rdHistogramBins m_bins
Definition: HttpRandom.h:191
std::vector< rdHistogramBin > rdHistogramBins
Definition: HttpRandom.h:189
inet::httptools::rdHistogram::rdHistogram ( cXMLAttributeMap  attributes)
153 {
155  if (!_hasKey(attributes, "bins"))
156  throw "No bins specified for a histogram distribution";
157  std::string binstr = attributes["bins"];
158  if (_hasKey(attributes, "zeroBased"))
159  m_zeroBased = strcmp(attributes["zeroBased"].c_str(), "true") == 0;
160  __parseBinString(binstr);
161  __normalizeBins();
162 }
void __parseBinString(std::string binstr)
Definition: HttpRandom.cc:188
void __normalizeBins()
Definition: HttpRandom.cc:212
bool m_zeroBased
Definition: HttpRandom.h:192
DISTR_TYPE m_type
Definition: HttpRandom.h:49
Definition: HttpRandom.h:33
bool _hasKey(cXMLAttributeMap attributes, std::string key)
Definition: HttpRandom.h:52

Member Function Documentation

void inet::httptools::rdHistogram::__normalizeBins ( )
private
213 {
214  double sum = 0;
215  for (auto & elem : m_bins)
216  sum += elem.sum;
217  if (sum != 0)
218  for (auto & elem : m_bins)
219  elem.sum = elem.sum / sum;
220 
221 }
rdHistogramBins m_bins
Definition: HttpRandom.h:191
void inet::httptools::rdHistogram::__parseBinString ( std::string  binstr)
private
189 {
190  // The bins string is of the form [(count1,sum1);(count2,sum2);...;(countn,sumn)]
191  binstr = trimLeft(binstr, "[");
192  binstr = trimRight(binstr, "]");
193  cStringTokenizer tokenizer = cStringTokenizer(binstr.c_str(), ";");
194  std::string curtuple, countstr, sumstr;
195  std::vector<std::string> res = tokenizer.asVector();
196  for (auto & re : res) {
197  curtuple = (re);
198  curtuple = trimLeft(curtuple, "(");
199  curtuple = trimRight(curtuple, ")");
200  int pos = curtuple.find(',');
201  if (pos == -1)
202  continue; // Invalid tuple -- raise error here?
203  countstr = curtuple.substr(0, pos);
204  sumstr = curtuple.substr(pos + 1, curtuple.size() - pos - 1);
205  rdHistogramBin bin;
206  bin.count = safeatoi(countstr.c_str(), 0);
207  bin.sum = safeatof(sumstr.c_str(), 0.0);
208  m_bins.push_back(bin);
209  }
210 }
double safeatof(const char *strval, double defaultVal)
Definition: HttpUtils.cc:179
std::string trimLeft(std::string s)
Definition: HttpUtils.cc:29
int safeatoi(const char *strval, int defaultVal)
Definition: HttpUtils.cc:189
std::string trimRight(std::string s)
Definition: HttpUtils.cc:41
rdHistogramBins m_bins
Definition: HttpRandom.h:191
double inet::httptools::rdHistogram::draw ( )
overridevirtual

Implements inet::httptools::rdObject.

165 {
166  rdHistogramBin bin;
167  double val = RNGCONTEXT uniform(0, 1);
168  double cumsum = 0;
169  int cumcount = 0;
170  int count = m_bins.size();
171  for (int i = 0; i < count; i++) {
172  // First select the bin
173  bin = m_bins[i];
174  cumsum += bin.sum;
175  if (cumsum >= val) {
176  // Then choose from the elements in the bin
177  double n = RNGCONTEXT uniform(1, bin.count) + cumcount;
178  if (m_zeroBased)
179  return n - 1.0;
180  else
181  return n;
182  }
183  cumcount += bin.count; // Keep the running count for the elements
184  }
185  return -1.0; // Default return in case something weird happens
186 }
int count(const std::vector< T > &v, const T &a)
Definition: stlutils.h:58
#define RNGCONTEXT
Definition: INETDefs.h:85
bool m_zeroBased
Definition: HttpRandom.h:192
rdHistogramBins m_bins
Definition: HttpRandom.h:191

Member Data Documentation

rdHistogramBins inet::httptools::rdHistogram::m_bins
protected
bool inet::httptools::rdHistogram::m_zeroBased = false
protected

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