A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
histogram.cc
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 // uint32_t
36 // Histogram::GetSize () const
37 // {
38 // return m_histogram.size ();
39 // }
40 
41 uint32_t
42 Histogram::GetNBins () const
43 {
44  return m_histogram.size ();
45 }
46 
47 double
48 Histogram::GetBinStart (uint32_t index)
49 {
50  return index*m_binWidth;
51 }
52 
53 double
54 Histogram::GetBinEnd (uint32_t index)
55 {
56  return (index + 1) * m_binWidth;
57 }
58 
59 double
60 Histogram::GetBinWidth (uint32_t index) const
61 {
62  return m_binWidth;
63 }
64 
65 void
66 Histogram::SetDefaultBinWidth (double binWidth)
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 
72 uint32_t
73 Histogram::GetBinCount (uint32_t index)
74 {
75  NS_ASSERT (index < m_histogram.size ());
76  return m_histogram[index];
77 }
78 
79 void
80 Histogram::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 
94 Histogram::Histogram (double binWidth)
95 {
96  m_binWidth = binWidth;
97 }
98 
99 Histogram::Histogram ()
100 {
101  m_binWidth = DEFAULT_BIN_WIDTH;
102 }
103 
104 
105 void
106 Histogram::SerializeToXmlStream (std::ostream &os, int indent, std::string elementName) const
107 {
108 #define INDENT(level) for (int __xpto = 0; __xpto < level; __xpto++) os << ' ';
109 
110  INDENT (indent); os << "<" << elementName // << " binWidth=\"" << m_binWidth << "\""
111  << " nBins=\"" << m_histogram.size () << "\""
112  << " >\n";
113  indent += 2;
114 
115 #if 1 // two alternative forms of representing bin data, one more verbose than the other one
116  for (uint32_t index = 0; index < m_histogram.size (); index++)
117  {
118  if (m_histogram[index])
119  {
120  INDENT (indent);
121  os << "<bin"
122  << " index=\"" << (index) << "\""
123  << " start=\"" << (index*m_binWidth) << "\""
124  << " width=\"" << m_binWidth << "\""
125  << " count=\"" << m_histogram[index] << "\""
126  << " />\n";
127  }
128  }
129 #else
130  INDENT (indent + 2);
131  for (uint32_t index = 0; index < m_histogram.size (); index++)
132  {
133  if (index > 0)
134  {
135  os << " ";
136  }
137  os << m_histogram[index];
138  }
139  os << "\n";
140 #endif
141  indent -= 2;
142  INDENT (indent); os << "</" << elementName << ">\n";
143 #undef INDENT
144 }
145 
146 
147 
148 
149 } // namespace ns3
150 
151 
#define NS_ASSERT(condition)
Definition: assert.h:64
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
#define NS_LOG_DEBUG(msg)
Definition: log.h:255