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 <math.h>
22 #include "histogram.h"
23 #include "ns3/simulator.h"
24 #include "ns3/log.h"
25 
26 #define DEFAULT_BIN_WIDTH 1
27 // #define RESERVED_BINS_INC 10
28 
29 
30 namespace ns3 {
31 
32 NS_LOG_COMPONENT_DEFINE ("Histogram");
33 
34 // uint32_t
35 // Histogram::GetSize () const
36 // {
37 // return m_histogram.size ();
38 // }
39 
40 uint32_t
42 {
43  return m_histogram.size ();
44 }
45 
46 double
47 Histogram::GetBinStart (uint32_t index)
48 {
49  return index*m_binWidth;
50 }
51 
52 double
53 Histogram::GetBinEnd (uint32_t index)
54 {
55  return (index + 1) * m_binWidth;
56 }
57 
58 double
59 Histogram::GetBinWidth (uint32_t index) const
60 {
61  return m_binWidth;
62 }
63 
64 void
66 {
67  NS_ASSERT (m_histogram.size () == 0); //we can only change the bin width if no values were added
68  m_binWidth = binWidth;
69 }
70 
71 uint32_t
72 Histogram::GetBinCount (uint32_t index)
73 {
74  NS_ASSERT (index < m_histogram.size ());
75  return m_histogram[index];
76 }
77 
78 void
79 Histogram::AddValue (double value)
80 {
81  uint32_t index = (uint32_t)floor (value/m_binWidth);
82 
83  //check if we need to resize the vector
84  NS_LOG_DEBUG ("AddValue: index=" << index << ", m_histogram.size()=" << m_histogram.size ());
85 
86  if (index >= m_histogram.size ())
87  {
88  m_histogram.resize (index + 1, 0);
89  }
90  m_histogram[index]++;
91 }
92 
93 Histogram::Histogram (double binWidth)
94 {
95  m_binWidth = binWidth;
96 }
97 
99 {
101 }
102 
103 
104 void
105 Histogram::SerializeToXmlStream (std::ostream &os, int indent, std::string elementName) const
106 {
107 #define INDENT(level) for (int __xpto = 0; __xpto < level; __xpto++) os << ' ';
108 
109  INDENT (indent); os << "<" << elementName // << " binWidth=\"" << m_binWidth << "\""
110  << " nBins=\"" << m_histogram.size () << "\""
111  << " >\n";
112  indent += 2;
113 
114 #if 1 // two alternative forms of representing bin data, one more verbose than the other one
115  for (uint32_t index = 0; index < m_histogram.size (); index++)
116  {
117  if (m_histogram[index])
118  {
119  INDENT (indent);
120  os << "<bin"
121  << " index=\"" << (index) << "\""
122  << " start=\"" << (index*m_binWidth) << "\""
123  << " width=\"" << m_binWidth << "\""
124  << " count=\"" << m_histogram[index] << "\""
125  << " />\n";
126  }
127  }
128 #else
129  INDENT (indent + 2);
130  for (uint32_t index = 0; index < m_histogram.size (); index++)
131  {
132  if (index > 0)
133  {
134  os << " ";
135  }
136  os << m_histogram[index];
137  }
138  os << "\n";
139 #endif
140  indent -= 2;
141  INDENT (indent); os << "</" << elementName << ">\n";
142 #undef INDENT
143 }
144 
145 
146 
147 
148 } // namespace ns3
149 
150