A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
20
namespace
ns3
21
{
22
23
NS_LOG_COMPONENT_DEFINE
(
"Histogram"
);
24
25
// uint32_t
26
// Histogram::GetSize () const
27
// {
28
// return m_histogram.size ();
29
// }
30
31
uint32_t
32
Histogram::GetNBins
()
const
33
{
34
return
m_histogram
.size();
35
}
36
37
double
38
Histogram::GetBinStart
(
uint32_t
index)
const
39
{
40
return
index *
m_binWidth
;
41
}
42
43
double
44
Histogram::GetBinEnd
(
uint32_t
index)
const
45
{
46
return
(index + 1) *
m_binWidth
;
47
}
48
49
double
50
Histogram::GetBinWidth
(
uint32_t
index)
const
51
{
52
return
m_binWidth
;
53
}
54
55
void
56
Histogram::SetDefaultBinWidth
(
double
binWidth)
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
62
uint32_t
63
Histogram::GetBinCount
(
uint32_t
index)
const
64
{
65
NS_ASSERT
(index <
m_histogram
.size());
66
return
m_histogram
[index];
67
}
68
69
void
70
Histogram::AddValue
(
double
value)
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
84
void
85
Histogram::Clear
()
86
{
87
m_histogram
.clear();
88
}
89
90
Histogram::Histogram
(
double
binWidth)
91
{
92
m_binWidth
= binWidth;
93
}
94
95
Histogram::Histogram
()
96
{
97
m_binWidth
=
DEFAULT_BIN_WIDTH
;
98
}
99
100
void
101
Histogram::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
ns3::Histogram::Clear
void Clear()
Clear the histogram content.
Definition
histogram.cc:85
ns3::Histogram::GetBinWidth
double GetBinWidth(uint32_t index) const
Returns the bin width.
Definition
histogram.cc:50
ns3::Histogram::GetBinCount
uint32_t GetBinCount(uint32_t index) const
Get the number of data added to the bin.
Definition
histogram.cc:63
ns3::Histogram::m_histogram
std::vector< uint32_t > m_histogram
Histogram data.
Definition
histogram.h:108
ns3::Histogram::SetDefaultBinWidth
void SetDefaultBinWidth(double binWidth)
Set the bin width.
Definition
histogram.cc:56
ns3::Histogram::m_binWidth
double m_binWidth
Bin width.
Definition
histogram.h:109
ns3::Histogram::GetNBins
uint32_t GetNBins() const
Returns the number of bins in the histogram.
Definition
histogram.cc:32
ns3::Histogram::SerializeToXmlStream
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
ns3::Histogram::GetBinEnd
double GetBinEnd(uint32_t index) const
Returns the bin end, i.e., (index+1)*binWidth.
Definition
histogram.cc:44
ns3::Histogram::AddValue
void AddValue(double value)
Add a value to the histogram.
Definition
histogram.cc:70
ns3::Histogram::Histogram
Histogram()
Definition
histogram.cc:95
ns3::Histogram::GetBinStart
double GetBinStart(uint32_t index) const
Returns the bin start, i.e., index*binWidth.
Definition
histogram.cc:38
uint32_t
NS_ASSERT
#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
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition
log.h:191
NS_LOG_DEBUG
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition
log.h:257
DEFAULT_BIN_WIDTH
#define DEFAULT_BIN_WIDTH
Definition
histogram.cc:16
histogram.h
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
src
stats
model
histogram.cc
Generated on Fri Nov 8 2024 13:59:06 for ns-3 by
1.11.0