A Discrete-Event Network Simulator
API
histogram.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009 INESC Porto
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License version 2 as
6 // published by the Free Software Foundation;
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // Author: Pedro Fortuna <pedro.fortuna@inescporto.pt> <pedro.fortuna@gmail.com>
18 //
19 
20 #include "histogram.h"
21 
22 #include "ns3/log.h"
23 #include "ns3/simulator.h"
24 
25 #include <cmath>
26 
27 #define DEFAULT_BIN_WIDTH 1
28 
29 // #define RESERVED_BINS_INC 10
30 
31 namespace ns3
32 {
33 
34 NS_LOG_COMPONENT_DEFINE("Histogram");
35 
36 // uint32_t
37 // Histogram::GetSize () const
38 // {
39 // return m_histogram.size ();
40 // }
41 
42 uint32_t
44 {
45  return m_histogram.size();
46 }
47 
48 double
49 Histogram::GetBinStart(uint32_t index) const
50 {
51  return index * m_binWidth;
52 }
53 
54 double
55 Histogram::GetBinEnd(uint32_t index) const
56 {
57  return (index + 1) * m_binWidth;
58 }
59 
60 double
61 Histogram::GetBinWidth(uint32_t index) const
62 {
63  return m_binWidth;
64 }
65 
66 void
68 {
69  NS_ASSERT(m_histogram.empty()); // we can only change the bin width if no values were added
70  m_binWidth = binWidth;
71 }
72 
73 uint32_t
74 Histogram::GetBinCount(uint32_t index) const
75 {
76  NS_ASSERT(index < m_histogram.size());
77  return m_histogram[index];
78 }
79 
80 void
81 Histogram::AddValue(double value)
82 {
83  auto index = (uint32_t)std::floor(value / m_binWidth);
84 
85  // check if we need to resize the vector
86  NS_LOG_DEBUG("AddValue: index=" << index << ", m_histogram.size()=" << m_histogram.size());
87 
88  if (index >= m_histogram.size())
89  {
90  m_histogram.resize(index + 1, 0);
91  }
92  m_histogram[index]++;
93 }
94 
95 void
97 {
98  m_histogram.clear();
99 }
100 
101 Histogram::Histogram(double binWidth)
102 {
103  m_binWidth = binWidth;
104 }
105 
107 {
109 }
110 
111 void
112 Histogram::SerializeToXmlStream(std::ostream& os, uint16_t indent, std::string elementName) const
113 {
114  os << std::string(indent, ' ') << "<" << elementName // << " binWidth=\"" << m_binWidth << "\""
115  << " nBins=\"" << m_histogram.size() << "\""
116  << " >\n";
117  indent += 2;
118 
119 #if 1 // two alternative forms of representing bin data, one more verbose than the other one
120  for (uint32_t index = 0; index < m_histogram.size(); index++)
121  {
122  if (m_histogram[index])
123  {
124  os << std::string(indent, ' ');
125  os << "<bin"
126  << " index=\"" << (index) << "\""
127  << " start=\"" << (index * m_binWidth) << "\""
128  << " width=\"" << m_binWidth << "\""
129  << " count=\"" << m_histogram[index] << "\""
130  << " />\n";
131  }
132  }
133 #else
134  os << std::string(indent + 2, ' ');
135  for (uint32_t index = 0; index < m_histogram.size(); index++)
136  {
137  if (index > 0)
138  {
139  os << " ";
140  }
141  os << m_histogram[index];
142  }
143  os << "\n";
144 #endif
145  indent -= 2;
146  os << std::string(indent, ' ') << "</" << elementName << ">\n";
147 }
148 
149 } // namespace ns3
void Clear()
Clear the histogram content.
Definition: histogram.cc:96
double GetBinWidth(uint32_t index) const
Returns the bin width.
Definition: histogram.cc:61
uint32_t GetBinCount(uint32_t index) const
Get the number of data added to the bin.
Definition: histogram.cc:74
std::vector< uint32_t > m_histogram
Histogram data.
Definition: histogram.h:119
void SetDefaultBinWidth(double binWidth)
Set the bin width.
Definition: histogram.cc:67
double m_binWidth
Bin width.
Definition: histogram.h:120
uint32_t GetNBins() const
Returns the number of bins in the histogram.
Definition: histogram.cc:43
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:112
double GetBinEnd(uint32_t index) const
Returns the bin end, i.e., (index+1)*binWidth.
Definition: histogram.cc:55
void AddValue(double value)
Add a value to the histogram.
Definition: histogram.cc:81
double GetBinStart(uint32_t index) const
Returns the bin start, i.e., index*binWidth.
Definition: histogram.cc:49
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define DEFAULT_BIN_WIDTH
Definition: histogram.cc:27
Every class exported by the ns3 library is enclosed in the ns3 namespace.