w11 - cpp 0.794
Backend server for Rlink and w11
Loading...
Searching...
No Matches
Rstats.cpp
Go to the documentation of this file.
1// $Id: Rstats.cpp 1186 2019-07-12 17:49:59Z mueller $
2// SPDX-License-Identifier: GPL-3.0-or-later
3// Copyright 2011-2019 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4//
5// Revision History:
6// Date Rev Version Comment
7// 2019-06-07 1160 1.0.6 add Reset(); drop operator-=() and operator*=()
8// 2018-12-18 1089 1.0.5 use c++ style casts
9// 2017-02-04 865 1.0.4 add NameMaxLength(); Print(): add counter name
10// 2017-02-18 851 1.0.3 add IncLogHist; fix + and * operator definition
11// 2013-02-03 481 1.0.2 use Rexception
12// 2011-03-06 367 1.0.1 use max from algorithm
13// 2011-02-06 359 1.0 Initial version
14// ---------------------------------------------------------------------------
15
20#include <algorithm>
21
22#include "Rstats.hpp"
23
24#include "RosFill.hpp"
25#include "RosPrintf.hpp"
26#include "Rexception.hpp"
27
28using namespace std;
29
35// all method definitions in namespace Retro
36namespace Retro {
37
38//------------------------------------------+-----------------------------------
40
42 : fValue(),
43 fName(),
44 fText(),
45 fHash(0),
46 fFormat("f"),
47 fWidth(12),
48 fPrec(0)
49{}
50
51//------------------------------------------+-----------------------------------
53
55 : fValue(rhs.fValue),
56 fName(rhs.fName),
57 fText(rhs.fText),
58 fHash(rhs.fHash),
59 fFormat(rhs.fFormat),
60 fWidth(rhs.fWidth),
61 fPrec(rhs.fPrec)
62{}
63
64//------------------------------------------+-----------------------------------
67{}
68
69//------------------------------------------+-----------------------------------
71
72void Rstats::Define(size_t ind, const std::string& name,
73 const std::string& text)
74{
75 // update hash
76 for (size_t i=0; i<name.length(); i++)
77 fHash = 69069*fHash + uint32_t(name[i]);
78 for (size_t i=0; i<text.length(); i++)
79 fHash = 69069*fHash + uint32_t(text[i]);
80
81 // in case it's the 'next' counter use push_back
82 if (ind == Size()) {
83 fValue.push_back(0.);
84 fName.push_back(name);
85 fText.push_back(text);
86
87 // otherwise resize and set
88 } else {
89 if (ind >= Size()) {
90 fValue.resize(ind+1);
91 fName.resize(ind+1);
92 fText.resize(ind+1);
93 }
94 fValue[ind] = 0.;
95 fName[ind] = name;
96 fText[ind] = text;
97 }
98
99 return;
100}
101
102//------------------------------------------+-----------------------------------
104
106{
107 for (auto& o: fValue) o = 0.;
108 return;
109}
110
111//------------------------------------------+-----------------------------------
113
114void Rstats::IncLogHist(size_t ind, size_t maskfirst,
115 size_t masklast, size_t val)
116{
117 if (val == 0) return;
118 size_t mask = maskfirst;
119 while (ind < fValue.size()) {
120 if (val <= mask || mask >= masklast) { // val in bin or last bin
121 Inc(ind);
122 return;
123 }
124 mask = (mask<<1) | 0x1;
125 ind += 1;
126 }
127
128 return;
129}
130
131//------------------------------------------+-----------------------------------
133
134void Rstats::SetFormat(const char* format, int width, int prec)
135{
136 fFormat = format;
137 fWidth = width;
138 fPrec = prec;
139 return;
140}
141
142//------------------------------------------+-----------------------------------
145{
146 size_t maxlen = 0;
147 for (size_t i=0; i<Size(); i++) {
148 size_t len = fName[i].length();
149 if (len > maxlen) maxlen = len;
150 }
151 return maxlen;
152}
153
154//------------------------------------------+-----------------------------------
156
157void Rstats::Print(std::ostream& os, const char* format,
158 int width, int prec) const
159{
160 if (format == nullptr || format[0]==0) {
161 format = fFormat.c_str();
162 width = fWidth;
163 prec = fPrec;
164 }
165
166 size_t maxlen = NameMaxLength();
167 for (size_t i=0; i<Size(); i++) {
168 os << RosPrintf(fValue[i], format, width, prec)
169 << " : " << RosPrintf(fName[i].c_str(),"-s",maxlen)
170 << " : " << fText[i] << endl;
171 }
172 return;
173}
174
175//------------------------------------------+-----------------------------------
177
178void Rstats::Dump(std::ostream& os, int ind, const char* text,
179 int detail) const
180{
181 RosFill bl(ind);
182 os << bl << (text?text:"--") << "Rstats @ " << this << endl;
183 if (detail >= 0) { // full dump
184 size_t maxlen=8;
185 for (size_t i=0; i<Size(); i++) maxlen = max(maxlen, fName[i].length());
186
187 for (size_t i=0; i<Size(); i++) {
188 os << bl << " " << fName[i] << ":" << RosFill(maxlen-fName[i].length()+1)
189 << RosPrintf(fValue[i], "f", 12)
190 << " '" << fText[i] << "'" << endl;
191 }
192 } else {
193 os << bl << " fValue.size: "
194 << RosPrintf(fValue.size(),"d",2) << endl;
195 }
196
197 os << bl << " fHash: " << RosPrintf(fHash,"x",8) << endl;
198 os << bl << " fFormat,Width,Prec: " << fFormat
199 << ", " << RosPrintf(fWidth,"d",2)
200 << ", " << RosPrintf(fPrec,"d",2) << endl;
201
202 return;
203}
204
205//------------------------------------------+-----------------------------------
207
209{
210 if (&rhs == this) return *this;
211
212 // in case this is freshly constructed, copy full context
213 if (Size() == 0) {
214 fValue = rhs.fValue;
215 fName = rhs.fName;
216 fText = rhs.fText;
217 fHash = rhs.fHash;
218 fFormat = rhs.fFormat;
219 fWidth = rhs.fWidth;
220 fPrec = rhs.fPrec;
221
222 // otherwise check hash and copy only values
223 } else {
224 if (Size() != rhs.Size() || fHash != rhs.fHash) {
225 throw Rexception("Rstats::oper=()",
226 "Bad args: assign incompatible stats");
227 }
228 fValue = rhs.fValue;
229 }
230
231 return *this;
232}
233
234} // end namespace Retro
FIXME_docs.
Definition: Rexception.hpp:29
I/O appicator to generate fill characters.
Definition: RosFill.hpp:24
FIXME_docs.
Definition: Rstats.hpp:28
std::vector< std::string > fText
counter text
Definition: Rstats.hpp:65
std::vector< std::string > fName
counter name
Definition: Rstats.hpp:64
void SetFormat(const char *format, int width=0, int prec=0)
FIXME_docs.
Definition: Rstats.cpp:134
size_t Size() const
FIXME_docs.
Definition: Rstats.ipp:38
void IncLogHist(size_t ind, size_t maskfirst, size_t masklast, size_t val)
FIXME_docs.
Definition: Rstats.cpp:114
void Dump(std::ostream &os, int ind=0, const char *text=0, int detail=0) const
FIXME_docs.
Definition: Rstats.cpp:178
int fPrec
default precision for Print
Definition: Rstats.hpp:69
~Rstats()
Destructor.
Definition: Rstats.cpp:66
void Inc(size_t ind, double val=1.)
FIXME_docs.
Definition: Rstats.ipp:29
void Print(std::ostream &os, const char *format=0, int width=0, int prec=0) const
FIXME_docs.
Definition: Rstats.cpp:157
size_t NameMaxLength() const
FIXME_docs.
Definition: Rstats.cpp:144
std::string fFormat
default format for Print
Definition: Rstats.hpp:67
std::vector< double > fValue
counter value
Definition: Rstats.hpp:63
void Define(size_t ind, const std::string &name, const std::string &text)
FIXME_docs.
Definition: Rstats.cpp:72
std::uint32_t fHash
hash value for name+text
Definition: Rstats.hpp:66
Rstats()
Default constructor.
Definition: Rstats.cpp:41
Rstats & operator=(const Rstats &rhs)
FIXME_docs.
Definition: Rstats.cpp:208
int fWidth
default width for Print
Definition: Rstats.hpp:68
void Reset()
FIXME_docs.
Definition: Rstats.cpp:105
RosPrintfS< bool > RosPrintf(bool value, const char *form=0, int width=0, int prec=0)
Creates a print object for the formatted output of a bool value.
Definition: RosPrintf.ipp:38
Declaration of class ReventLoop.
Definition: ReventLoop.cpp:47