A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
histogram.cc
Go to the documentation of this file.
1//
2// Copyright (c) 2009 INESC Porto
3//
4// SPDX-License-Identifier: GPL-2.0-only
5//
6// Author: Pedro Fortuna <pedro.fortuna@inescporto.pt> <pedro.fortuna@gmail.com>
7//
8
9#include "histogram.h"
10
11#include "ns3/log.h"
12#include "ns3/simulator.h"
13
14#include <cmath>
15
16#define DEFAULT_BIN_WIDTH 1
17
18// #define RESERVED_BINS_INC 10
19
20namespace ns3
21{
22
23NS_LOG_COMPONENT_DEFINE("Histogram");
24
25// uint32_t
26// Histogram::GetSize () const
27// {
28// return m_histogram.size ();
29// }
30
33{
34 return m_histogram.size();
35}
36
37double
39{
40 return index * m_binWidth;
41}
42
43double
45{
46 return (index + 1) * m_binWidth;
47}
48
49double
51{
52 return m_binWidth;
53}
54
55void
57{
58 NS_ASSERT(m_histogram.empty()); // we can only change the bin width if no values were added
59 m_binWidth = binWidth;
60}
61
64{
65 NS_ASSERT(index < m_histogram.size());
66 return m_histogram[index];
67}
68
69void
71{
72 auto index = (uint32_t)std::floor(value / m_binWidth);
73
74 // check if we need to resize the vector
75 NS_LOG_DEBUG("AddValue: index=" << index << ", m_histogram.size()=" << m_histogram.size());
76
77 if (index >= m_histogram.size())
78 {
79 m_histogram.resize(index + 1, 0);
80 }
81 m_histogram[index]++;
82}
83
84void
86{
87 m_histogram.clear();
88}
89
90Histogram::Histogram(double binWidth)
91{
92 m_binWidth = binWidth;
93}
94
99
100void
101Histogram::SerializeToXmlStream(std::ostream& os, uint16_t indent, std::string elementName) const
102{
103 os << std::string(indent, ' ') << "<" << elementName // << " binWidth=\"" << m_binWidth << "\""
104 << " nBins=\"" << m_histogram.size() << "\""
105 << " >\n";
106 indent += 2;
107
108#if 1 // two alternative forms of representing bin data, one more verbose than the other one
109 for (uint32_t index = 0; index < m_histogram.size(); index++)
110 {
111 if (m_histogram[index])
112 {
113 os << std::string(indent, ' ');
114 os << "<bin"
115 << " index=\"" << (index) << "\""
116 << " start=\"" << (index * m_binWidth) << "\""
117 << " width=\"" << m_binWidth << "\""
118 << " count=\"" << m_histogram[index] << "\""
119 << " />\n";
120 }
121 }
122#else
123 os << std::string(indent + 2, ' ');
124 for (uint32_t index = 0; index < m_histogram.size(); index++)
125 {
126 if (index > 0)
127 {
128 os << " ";
129 }
130 os << m_histogram[index];
131 }
132 os << "\n";
133#endif
134 indent -= 2;
135 os << std::string(indent, ' ') << "</" << elementName << ">\n";
136}
137
138} // namespace ns3
void Clear()
Clear the histogram content.
Definition histogram.cc:85
double GetBinWidth(uint32_t index) const
Returns the bin width.
Definition histogram.cc:50
uint32_t GetBinCount(uint32_t index) const
Get the number of data added to the bin.
Definition histogram.cc:63
std::vector< uint32_t > m_histogram
Histogram data.
Definition histogram.h:108
void SetDefaultBinWidth(double binWidth)
Set the bin width.
Definition histogram.cc:56
double m_binWidth
Bin width.
Definition histogram.h:109
uint32_t GetNBins() const
Returns the number of bins in the histogram.
Definition histogram.cc:32
void SerializeToXmlStream(std::ostream &os, uint16_t indent, std::string elementName) const
Serializes the results to an std::ostream in XML format.
Definition histogram.cc:101
double GetBinEnd(uint32_t index) const
Returns the bin end, i.e., (index+1)*binWidth.
Definition histogram.cc:44
void AddValue(double value)
Add a value to the histogram.
Definition histogram.cc:70
double GetBinStart(uint32_t index) const
Returns the bin start, i.e., index*binWidth.
Definition histogram.cc:38
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#define DEFAULT_BIN_WIDTH
Definition histogram.cc:16
Every class exported by the ns3 library is enclosed in the ns3 namespace.