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