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)
75 {
76  NS_ASSERT(index < m_histogram.size());
77  return m_histogram[index];
78 }
79 
80 void
82 {
83  uint32_t 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 Histogram::Histogram(double binWidth)
96 {
97  m_binWidth = binWidth;
98 }
99 
101 {
103 }
104 
105 void
106 Histogram::SerializeToXmlStream(std::ostream& os, uint16_t indent, std::string elementName) const
107 {
108  os << std::string(indent, ' ') << "<" << elementName // << " binWidth=\"" << m_binWidth << "\""
109  << " nBins=\"" << m_histogram.size() << "\""
110  << " >\n";
111  indent += 2;
112 
113 #if 1 // two alternative forms of representing bin data, one more verbose than the other one
114  for (uint32_t index = 0; index < m_histogram.size(); index++)
115  {
116  if (m_histogram[index])
117  {
118  os << std::string(indent, ' ');
119  os << "<bin"
120  << " index=\"" << (index) << "\""
121  << " start=\"" << (index * m_binWidth) << "\""
122  << " width=\"" << m_binWidth << "\""
123  << " count=\"" << m_histogram[index] << "\""
124  << " />\n";
125  }
126  }
127 #else
128  os << std::string(indent + 2, ' ');
129  for (uint32_t index = 0; index < m_histogram.size(); index++)
130  {
131  if (index > 0)
132  {
133  os << " ";
134  }
135  os << m_histogram[index];
136  }
137  os << "\n";
138 #endif
139  indent -= 2;
140  os << std::string(indent, ' ') << "</" << elementName << ">\n";
141 }
142 
143 } // namespace ns3
uint32_t GetBinCount(uint32_t index)
Get the number of data added to the bin.
Definition: histogram.cc:74
double GetBinWidth(uint32_t index) const
Returns the bin width.
Definition: histogram.cc:61
std::vector< uint32_t > m_histogram
Histogram data.
Definition: histogram.h:114
void SetDefaultBinWidth(double binWidth)
Set the bin width.
Definition: histogram.cc:67
double m_binWidth
Bin width.
Definition: histogram.h:115
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:106
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.
value
Definition: second.py:41