A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
31 namespace ns3 {
32 
33 NS_LOG_COMPONENT_DEFINE ("Histogram")
34  ;
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)
50 {
51  return index*m_binWidth;
52 }
53 
54 double
55 Histogram::GetBinEnd (uint32_t index)
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.size () == 0); //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
81 Histogram::AddValue (double value)
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 
106 void
107 Histogram::SerializeToXmlStream (std::ostream &os, int indent, std::string elementName) const
108 {
109 #define INDENT(level) for (int __xpto = 0; __xpto < level; __xpto++) os << ' ';
110 
111  INDENT (indent); os << "<" << elementName // << " binWidth=\"" << m_binWidth << "\""
112  << " nBins=\"" << m_histogram.size () << "\""
113  << " >\n";
114  indent += 2;
115 
116 #if 1 // two alternative forms of representing bin data, one more verbose than the other one
117  for (uint32_t index = 0; index < m_histogram.size (); index++)
118  {
119  if (m_histogram[index])
120  {
121  INDENT (indent);
122  os << "<bin"
123  << " index=\"" << (index) << "\""
124  << " start=\"" << (index*m_binWidth) << "\""
125  << " width=\"" << m_binWidth << "\""
126  << " count=\"" << m_histogram[index] << "\""
127  << " />\n";
128  }
129  }
130 #else
131  INDENT (indent + 2);
132  for (uint32_t index = 0; index < m_histogram.size (); index++)
133  {
134  if (index > 0)
135  {
136  os << " ";
137  }
138  os << m_histogram[index];
139  }
140  os << "\n";
141 #endif
142  indent -= 2;
143  INDENT (indent); os << "</" << elementName << ">\n";
144 #undef INDENT
145 }
146 
147 
148 
149 
150 } // namespace ns3
151 
152 
double GetBinEnd(uint32_t index)
Returns the bin end, i.e., (index+1)*binWidth.
Definition: histogram.cc:55
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
#define NS_ASSERT(condition)
Definition: assert.h:64
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:116
#define INDENT(level)
double m_binWidth
Bin width.
Definition: histogram.h:117
#define DEFAULT_BIN_WIDTH
Definition: histogram.cc:27
uint32_t GetNBins() const
Returns the number of bins in the histogram.
Definition: histogram.cc:43
void SetDefaultBinWidth(double binWidth)
Set the bin width.
Definition: histogram.cc:67
uint32_t GetBinCount(uint32_t index)
Get the number of data added to the bin.
Definition: histogram.cc:74
double GetBinStart(uint32_t index)
Returns the bin start, i.e., index*binWidth.
Definition: histogram.cc:49
#define NS_LOG_DEBUG(msg)
Definition: log.h:289
void AddValue(double value)
Add a value to the histogram.
Definition: histogram.cc:81
void SerializeToXmlStream(std::ostream &os, int indent, std::string elementName) const
Serializes the results to an std::ostream in XML format.
Definition: histogram.cc:107