Utils: Add stddev support to Statistics
[senf.git] / senf / Utils / Statistics.cci
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Statistics inline non-template implementation */
25
26 //#include "Statistics.ih"
27
28 // Custom includes
29 #include <float.h>
30 #include "Range.hh"
31
32 #define prefix_ inline
33 ///////////////////////////////cci.p///////////////////////////////////////
34
35 ///////////////////////////////////////////////////////////////////////////
36 // senf::StatisticsBase::Transform
37
38 prefix_ senf::StatisticsBase::Transform::result_type
39 senf::StatisticsBase::Transform::operator()(first_argument_type i)
40     const
41 {
42     return i.second;
43 }
44
45 ///////////////////////////////////////////////////////////////////////////
46 // senf::StatisticsBase::OutputEntry
47
48 prefix_ senf::StatisticsBase::OutputEntry::OutputEntry()
49     : n(), min(), avg(), max(), dev()
50 {
51     initDir();
52 }
53
54 prefix_ senf::StatisticsBase::OutputEntry::OutputEntry(unsigned n_)
55     : n(n_), min(), avg(), max(), dev()
56 {
57     initDir();
58 }
59
60 prefix_ senf::StatisticsBase::OutputEntry::OutputEntry(const OutputEntry& other)
61     : n(other.n), min(other.min), avg(other.avg), max(other.max), dev(other.dev)
62 {
63     initDir();
64 }
65
66 prefix_ void senf::StatisticsBase::OutputEntry::initDir()
67 {
68     dir.add("list", senf::membind(&OutputEntry::consoleList, this))
69         .doc("List all known connected targets. This list might not be complete.");
70 }
71
72 prefix_ senf::StatisticsBase::OutputEntry &
73 senf::StatisticsBase::OutputEntry::operator=(const OutputEntry& other)
74 {
75     n = other.n;
76     min = other.min;
77     avg = other.avg;
78     max = other.max;
79     dev = other.dev;
80     return *this;
81 }
82
83 prefix_ void senf::StatisticsBase::OutputEntry::consoleList(std::ostream & os)
84 {
85     for (boost::ptr_vector<TargetBase>::iterator i (targets_.begin());
86          i != targets_.end(); ++i)
87         if (! i->label.empty())
88             os << i->label << "\n";
89 }
90
91 /////////////////////////////////////////////////////////////////////////
92 // senf::StatisticsBase
93
94 prefix_ senf::StatisticsBase::StatisticsBase()
95     : min_ (0.0f), avg_ (0.0f), max_ (0.0f), maxQueueLen_ (0u)
96 {}
97
98 prefix_ senf::StatisticsBase::~StatisticsBase()
99 {}
100
101 prefix_ senf::StatisticsBase::CollectorRange senf::StatisticsBase::collectors()
102 {
103     return senf::make_transform_range(children_, Transform());
104 }
105
106 prefix_ float senf::StatisticsBase::min()
107     const
108 {
109     return min_;
110 }
111
112 prefix_ float senf::StatisticsBase::avg()
113     const
114 {
115     return avg_;
116 }
117
118 prefix_ float senf::StatisticsBase::max()
119     const
120 {
121     return max_;
122 }
123
124 prefix_ float senf::StatisticsBase::dev()
125     const
126 {
127     return dev_;
128 }
129
130 prefix_ unsigned senf::StatisticsBase::rank()
131     const
132 {
133     return 1;
134 }
135
136 prefix_ senf::Statistics & senf::StatisticsBase::base()
137 {
138     return v_base();
139 }
140
141 prefix_ std::string senf::StatisticsBase::path()
142     const
143 {
144     return v_path();
145 }
146
147 ///////////////////////////////////////////////////////////////////////////
148 // senf::Collector
149
150 prefix_ senf::Collector::Collector(StatisticsBase * owner, unsigned rank)
151     : rank_ (rank), i_ (0u), accMin_ (FLT_MAX), accSum_ (0.0f), accSumSq_ (0.0f), accMax_ (-FLT_MAX),
152       owner_ (owner)
153 {}
154
155 prefix_ unsigned senf::Collector::rank()
156     const
157 {
158     return rank_;
159 }
160
161 prefix_ senf::StatisticsBase::OutputProxy<senf::Collector>
162 senf::Collector::output(unsigned n)
163 {
164
165     return StatisticsBase::OutputProxy<Collector>(this, StatisticsBase::output(n));
166 }
167
168 ///////////////////////////////////////////////////////////////////////////
169 // senf::Statistics
170
171 prefix_ void senf::Statistics::operator()(float min, float avg, float max, float dev)
172 {
173     enter(min, avg, max, dev);
174 }
175
176 prefix_ void senf::Statistics::operator()(float value, float dev)
177 {
178     enter(value, value, value, dev);
179 }
180
181 prefix_ senf::StatisticsBase::OutputProxy<senf::Statistics>
182 senf::Statistics::output(unsigned n)
183 {
184     return StatisticsBase::OutputProxy<Statistics>(this, StatisticsBase::output(n));
185 }
186
187 ///////////////////////////////cci.e///////////////////////////////////////
188 #undef prefix_
189
190 \f
191 // Local Variables:
192 // mode: c++
193 // fill-column: 100
194 // comment-column: 40
195 // c-file-style: "senf"
196 // indent-tabs-mode: nil
197 // ispell-local-dictionary: "american"
198 // compile-command: "scons -u test"
199 // End: