OpenCores
URL https://opencores.org/ocsvn/w11/w11/trunk

Subversion Repositories w11

[/] [w11/] [tags/] [w11a_V0.6/] [tools/] [src/] [librtools/] [Rstats.cpp] - Blame information for rev 19

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 wfjm
// $Id: Rstats.cpp 492 2013-02-24 22:14:47Z mueller $
2 10 wfjm
//
3 19 wfjm
// Copyright 2011-2013 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4 10 wfjm
//
5
// This program is free software; you may redistribute and/or modify it under
6
// the terms of the GNU General Public License as published by the Free
7
// Software Foundation, either version 2, or at your option any later version.
8
//
9
// This program is distributed in the hope that it will be useful, but
10
// WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
11
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
// for complete details.
13
// 
14
// Revision History: 
15
// Date         Rev Version  Comment
16 19 wfjm
// 2013-02-03   481   1.0.2  use Rexception
17 10 wfjm
// 2011-03-06   367   1.0.1  use max from algorithm
18
// 2011-02-06   359   1.0    Initial version
19
// ---------------------------------------------------------------------------
20
 
21
/*!
22
  \file
23 19 wfjm
  \version $Id: Rstats.cpp 492 2013-02-24 22:14:47Z mueller $
24 10 wfjm
  \brief   Implemenation of Rstats .
25
*/
26
 
27
#include <algorithm>
28
 
29
#include "Rstats.hpp"
30 19 wfjm
 
31 10 wfjm
#include "RosFill.hpp"
32
#include "RosPrintf.hpp"
33 19 wfjm
#include "Rexception.hpp"
34 10 wfjm
 
35
using namespace std;
36
 
37
/*!
38
  \class Retro::Rstats
39
  \brief FIXME_docs
40
*/
41 19 wfjm
 
42
// all method definitions in namespace Retro
43
namespace Retro {
44
 
45 10 wfjm
//------------------------------------------+-----------------------------------
46
//! Default constructor
47
 
48
Rstats::Rstats()
49
  : fValue(),
50
    fName(),
51
    fText(),
52
    fHash(0),
53
    fFormat("f"),
54
    fWidth(12),
55
    fPrec(0)
56
{}
57
 
58
//------------------------------------------+-----------------------------------
59
//! Copy constructor
60
 
61
Rstats::Rstats(const Rstats& rhs)
62
  : fValue(rhs.fValue),
63
    fName(rhs.fName),
64
    fText(rhs.fText),
65
    fHash(rhs.fHash),
66
    fFormat(rhs.fFormat),
67
    fWidth(rhs.fWidth),
68
    fPrec(rhs.fPrec)
69
{}
70
 
71
//------------------------------------------+-----------------------------------
72
//! Destructor
73
Rstats::~Rstats()
74
{}
75
 
76
//------------------------------------------+-----------------------------------
77
//! FIXME_docs
78
 
79
void Rstats::Define(size_t ind, const std::string& name,
80
                    const std::string& text)
81
{
82
  // update hash
83
  for (size_t i=0; i<name.length(); i++)
84
    fHash = 69069*fHash + (uint32_t) name[i];
85
  for (size_t i=0; i<text.length(); i++)
86
    fHash = 69069*fHash + (uint32_t) text[i];
87
 
88
  // in case it's the 'next' counter use push_back
89
  if (ind == Size()) {
90
    fValue.push_back(0.);
91
    fName.push_back(name);
92
    fText.push_back(text);
93
 
94
  // otherwise resize and set
95
  } else {
96
    if (ind >= Size()) {
97
      fValue.resize(ind+1);
98
      fName.resize(ind+1);
99
      fText.resize(ind+1);
100
    }
101
    fValue[ind] = 0.;
102
    fName[ind]  = name;
103
    fText[ind]  = text;
104
  }
105
 
106
  return;
107
}
108
 
109
//------------------------------------------+-----------------------------------
110
//! FIXME_docs
111
 
112
void Rstats::SetFormat(const char* format, int width, int prec)
113
{
114
  fFormat = format;
115
  fWidth  = width;
116
  fPrec   = prec;
117
  return;
118
}
119
 
120
//------------------------------------------+-----------------------------------
121
//! FIXME_docs
122
 
123
void Rstats::Print(std::ostream& os, const char* format,
124
                   int width, int prec) const
125
{
126
  if (format == 0 || format[0]==0) {
127
    format = fFormat.c_str();
128
    width  = fWidth;
129
    prec   = fPrec;
130
  }
131
 
132
  for (size_t i=0; i<Size(); i++) {
133
    os << RosPrintf(fValue[i], format, width, prec)
134
       << " : " << fText[i] << endl;
135
  }
136
  return;
137
}
138
 
139
//------------------------------------------+-----------------------------------
140
//! FIXME_docs
141
 
142
void Rstats::Dump(std::ostream& os, int ind, const char* text) const
143
{
144
  RosFill bl(ind);
145
  os << bl << (text?text:"--") << "Rstats @ " << this << endl;
146
 
147
  size_t maxlen=8;
148
  for (size_t i=0; i<Size(); i++) maxlen = max(maxlen, fName[i].length());
149
 
150
  for (size_t i=0; i<Size(); i++) {
151
    os << bl << "  " << fName[i] << ":" << RosFill(maxlen-fName[i].length()+1)
152
       << RosPrintf(fValue[i], "f", 12)
153 19 wfjm
       << "  '" << fText[i] << "'" << endl;
154 10 wfjm
  }
155
 
156
  return;
157
}
158
 
159
//------------------------------------------+-----------------------------------
160
//! FIXME_docs
161
 
162
Rstats& Rstats::operator=(const Rstats& rhs)
163
{
164
  if (&rhs == this) return *this;
165
 
166
  // in case this is freshly constructed, copy full context
167
  if (Size() == 0) {
168
    fValue  = rhs.fValue;
169
    fName   = rhs.fName;
170
    fText   = rhs.fText;
171
    fHash   = rhs.fHash;
172
    fFormat = rhs.fFormat;
173
    fWidth  = rhs.fWidth;
174
    fPrec   = rhs.fPrec;
175
 
176
  // otherwise check hash and copy only values
177
  } else {
178
    if (Size() != rhs.Size() || fHash != rhs.fHash) {
179 19 wfjm
      throw Rexception("Rstats::oper=()",
180
                       "Bad args: assign incompatible stats");
181 10 wfjm
    }
182
    fValue = rhs.fValue;
183
  }
184
 
185
  return *this;
186
}
187
 
188
//------------------------------------------+-----------------------------------
189
//! FIXME_docs
190
 
191
Rstats& Rstats::operator-(const Rstats& rhs)
192
{
193
  if (Size() != rhs.Size() || fHash != rhs.fHash) {
194 19 wfjm
    throw Rexception("Rstats::oper-()",
195
                     "Bad args: subtract incompatible stats");
196 10 wfjm
  }
197
 
198
  for (size_t i=0; i<fValue.size(); i++) {
199
    fValue[i] -= rhs.fValue[i];
200
  }
201
  return *this;
202
}
203
 
204
//------------------------------------------+-----------------------------------
205
//! FIXME_docs
206
 
207
Rstats& Rstats::operator*(double rhs)
208
{
209
  for (size_t i=0; i<fValue.size(); i++) {
210
    fValue[i] *= rhs;
211
  }
212
  return *this;
213
}
214
 
215 19 wfjm
} // end namespace Retro

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.