-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstatistics.cpp
131 lines (108 loc) · 3.52 KB
/
statistics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <stddef.h>
#include <stdint.h>
#include <limits>
#include <sstream>
#include <string>
#include "statistics.hpp"
#include "string_util.hpp"
using std::ostream;
using std::string;
using std::stringstream;
// ------------------------------ StringStatistics
StringStatistics::StringStatistics()
{
this->number = static_cast<std::size_t>(0);
}
StringStatistics::~StringStatistics()
{
}
void StringStatistics::parseData(std::stringstream &data)
{
this->number += data.str().size(); // TODO there is a faster way
}
ostream& operator<<(ostream &os, const StringStatistics & thing)
{
os << "SIZE: " << thing.number << " characters";
return os;
}
// ------------------------------ PrenexusStatistics<ComplexT>
// ------------------------------ Statistics<NumT>
namespace { // anonymous namespace to hide from others
static const string MIN("MIN: ");
static const string MAX(" MAX: ");
template <typename NumT>
void getMinMax(NumT &min, NumT &max)
{
min = std::numeric_limits<NumT>::max();
max = std::numeric_limits<NumT>::min();
}
template <>
void getMinMax<double>(double &min, double &max)
{
min = std::numeric_limits<double>::max();
max = -1. * std::numeric_limits<double>::max();
}
template <>
void getMinMax<float>(float &min, float &max)
{
min = std::numeric_limits<float>::max();
max = -1.f * std::numeric_limits<float>::max();
}
}
template <typename NumT>
Statistics<NumT>::Statistics()
{
this->total = static_cast<NumT>(0.);
this->number = static_cast<std::size_t>(0);
getMinMax(this->min, this->max);
}
template <typename NumT>
Statistics<NumT>::~Statistics()
{
}
template <typename NumT>
void Statistics<NumT>::parseData(std::vector<NumT> & data)
{
size_t size = data.size();
this->number += size;
NumT item;
for (size_t i = 0; i < size; i++) {
item = data[i];
if (item < this->min)
this->min = item;
if (item > this->max)
this->max = item;
this->total += static_cast<double>(item);
}
}
template <typename NumT>
ostream& operator<<(ostream &os, const Statistics<NumT>& thing)
{
if (thing.number > 0)
os << "MEAN: " << (thing.total / static_cast<double>(thing.number)) << " ";
os << MIN << toStr(thing.min) << MAX << toStr(thing.max) << "\n";
os << "SIZE: " << (thing.number*sizeof(thing.min)) << " bytes = "
<< thing.number << " x " << sizeof(thing.min) << " bytes";
return os;
}
// concrete instantiations to make the compiler happy
template class Statistics<uint8_t>;
template class Statistics<int8_t>;
template class Statistics<uint16_t>;
template class Statistics<int16_t>;
template class Statistics<uint32_t>;
template class Statistics<int32_t>;
template class Statistics<uint64_t>;
template class Statistics<int64_t>;
template class Statistics<float>;
template class Statistics<double>;
template std::ostream& operator<<(std::ostream &, const Statistics<uint8_t> &);
template std::ostream& operator<<(std::ostream &, const Statistics<int8_t> &);
template std::ostream& operator<<(std::ostream &, const Statistics<uint16_t> &);
template std::ostream& operator<<(std::ostream &, const Statistics<int16_t> &);
template std::ostream& operator<<(std::ostream &, const Statistics<uint32_t> &);
template std::ostream& operator<<(std::ostream &, const Statistics<int32_t> &);
template std::ostream& operator<<(std::ostream &, const Statistics<uint64_t> &);
template std::ostream& operator<<(std::ostream &, const Statistics<int64_t> &);
template std::ostream& operator<<(std::ostream &, const Statistics<float> &);
template std::ostream& operator<<(std::ostream &, const Statistics<double> &);