Skip to content

Commit 11ed8cf

Browse files
Moved the statistics calculation into a separate file.
1 parent e21ef51 commit 11ed8cf

File tree

6 files changed

+132
-62
lines changed

6 files changed

+132
-62
lines changed

CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ project ( morebin )
77
include ( CheckIncludeFiles )
88
# stdint
99
check_include_files ( stdint.h stdint )
10-
if ( stdint )
11-
add_definitions ( -DHAVE_STDINT_H )
12-
endif ( stdint )
10+
if ( NOT stdint )
11+
MESSAGE ( FATAL_ERROR "stdint.h required to build" )
12+
endif ( NOT stdint )
1313
# tclap - needs to go
1414
#find_path ( TCLAP_INCLUDE_DIR tclap/CmdLine.h )
1515
#include_directories ( ${TCLAP_INCLUDE_DIR} )
@@ -25,7 +25,9 @@ set ( SRC_FILES
2525
byte_swap.h
2626
more_bin.cpp
2727
renderer.cpp
28-
renderer.hpp )
28+
renderer.hpp
29+
statistics.cpp
30+
statistics.hpp )
2931

3032
add_executable( morebin ${SRC_FILES} )
3133
target_link_libraries ( morebin ${Boost_LIBRARIES} )

bin_file.hpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,10 @@
44
#include <boost/shared_ptr.hpp>
55
#include <fstream>
66
#include <sstream>
7+
#include <stdint.h>
78
#include <string>
89
#include <vector>
910

10-
#ifdef HAVE_STDINT_H
11-
#include <stdint.h>
12-
#else
13-
typedef signed char int8_t;
14-
typedef short int int16_t;
15-
typedef int int32_t;
16-
typedef long int int64_t;
17-
typedef unsigned char uint8_t;
18-
typedef unsigned short int uint16_t;
19-
typedef unsigned int uint32_t;
20-
typedef unsigned long int uint64_t;
21-
#endif
22-
2311
class BinFile
2412
{
2513
public:

renderer.cpp

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include <algorithm>
2-
#include <limits>
32
#include <iostream>
43
#include <sstream>
54
#include <stdexcept>
65
#include "renderer.hpp"
6+
#include "statistics.hpp"
77

88
using std::cout;
99
using std::endl;
@@ -93,47 +93,6 @@ void Renderer::showLines(const bool showLines)
9393
this->m_showLines = showLines;
9494
}
9595

96-
template <typename NumT>
97-
void getMinMax(NumT &min, NumT &max)
98-
{
99-
min = std::numeric_limits<NumT>::max();
100-
max = std::numeric_limits<NumT>::min();
101-
}
102-
103-
template <>
104-
void getMinMax<double>(double &min, double &max)
105-
{
106-
min = std::numeric_limits<double>::max();
107-
max = -1. * std::numeric_limits<double>::max();
108-
}
109-
110-
template <>
111-
void getMinMax<float>(float &min, float &max)
112-
{
113-
min = std::numeric_limits<float>::max();
114-
max = -1. * std::numeric_limits<float>::max();
115-
}
116-
117-
// TODO put this in a separate class
118-
// TODO add in mean value
119-
template <typename NumT>
120-
void Renderer::showStatistics(vector<NumT> &data)
121-
{
122-
NumT min;
123-
NumT max;
124-
getMinMax(min, max);
125-
126-
size_t size = data.size();
127-
for (size_t i = 0; i < size; i++) {
128-
if (data[i] < min)
129-
min = data[i];
130-
if (data[i] > max)
131-
max = data[i];
132-
}
133-
134-
cout << "MIN " << min << " MAX: " << max << " TOTAL ELEMENTS: " << size << endl;
135-
}
136-
13796
template <typename NumT>
13897
void Renderer::innerShowData(BinFile &file, size_t offset, size_t length)
13998
{
@@ -142,17 +101,19 @@ void Renderer::innerShowData(BinFile &file, size_t offset, size_t length)
142101
if ((offset % sizeof(NumT)) == 0)
143102
myOffset = offset / sizeof(NumT);
144103

104+
Statistics<NumT> stats; // object for generating statistics
145105
vector<NumT> data;
146106
file.read(data, length);
147107
if (!(data.empty())) {
108+
stats.parseData(data);
148109
for (size_t i = 0; i < data.size(); i++) {
149110
if (this->m_showLines)
150111
cout << (myOffset + i + 1) << " "; // start counting with one
151112
cout << data[i] << EOL;
152113
}
153114

154-
this->showStatistics(data);
155115
}
116+
cout << stats << endl;
156117
}
157118

158119
/// Special version for strings

renderer.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ class Renderer
2020
private:
2121
template <typename NumT>
2222
void innerShowData(BinFile &file, size_t offset, size_t length);
23-
template <typename NumT>
24-
void showStatistics(std::vector<NumT> &data);
2523

2624
std::vector<std::string> * m_dataDescr;
2725
bool m_showLines;

statistics.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <limits>
2+
#include <stdint.h>
3+
#include "statistics.hpp"
4+
5+
namespace { // anonymous namespace to hide from others
6+
7+
template <typename NumT>
8+
void getMinMax(NumT &min, NumT &max)
9+
{
10+
min = std::numeric_limits<NumT>::max();
11+
max = std::numeric_limits<NumT>::min();
12+
}
13+
14+
template <>
15+
void getMinMax<double>(double &min, double &max)
16+
{
17+
min = std::numeric_limits<double>::max();
18+
max = -1. * std::numeric_limits<double>::max();
19+
}
20+
21+
template <>
22+
void getMinMax<float>(float &min, float &max)
23+
{
24+
min = std::numeric_limits<float>::max();
25+
max = -1. * std::numeric_limits<float>::max();
26+
}
27+
28+
}
29+
30+
template <typename NumT>
31+
Statistics<NumT>::Statistics()
32+
{
33+
this->total = static_cast<NumT>(0.);
34+
this->number = static_cast<std::size_t>(0);
35+
getMinMax(this->min, this->max);
36+
}
37+
38+
template <typename NumT>
39+
Statistics<NumT>::~Statistics()
40+
{
41+
}
42+
43+
template <typename NumT>
44+
void Statistics<NumT>::parseData(std::vector<NumT> & data)
45+
{
46+
size_t size = data.size();
47+
this->number += size;
48+
49+
NumT item;
50+
for (size_t i = 0; i < size; i++) {
51+
item = data[i];
52+
if (item < this->min)
53+
this->min = item;
54+
if (item > this->max)
55+
this->max = item;
56+
this->total += item;
57+
}
58+
}
59+
60+
template <typename NumT>
61+
std::ostream& operator<<(std::ostream &os, const Statistics<NumT> & thing) {
62+
if (thing.number > 0) {
63+
double mean = static_cast<double>(thing.total);
64+
mean = mean / static_cast<double>(thing.number);
65+
os << "MEAN " << mean << " ";
66+
}
67+
68+
os << "MIN " << thing.min << " MAX: " << thing.max
69+
<< " TOTAL ELEMENTS: " << thing.number;
70+
return os;
71+
}
72+
73+
// concrete instantiations to make the compiler happy
74+
template class Statistics<uint8_t>;
75+
template class Statistics<int8_t>;
76+
template class Statistics<uint16_t>;
77+
template class Statistics<int16_t>;
78+
template class Statistics<uint32_t>;
79+
template class Statistics<int32_t>;
80+
template class Statistics<uint64_t>;
81+
template class Statistics<int64_t>;
82+
template class Statistics<float>;
83+
template class Statistics<double>;
84+
85+
template std::ostream& operator<<(std::ostream &, const Statistics<uint8_t> &);
86+
template std::ostream& operator<<(std::ostream &, const Statistics<int8_t> &);
87+
template std::ostream& operator<<(std::ostream &, const Statistics<uint16_t> &);
88+
template std::ostream& operator<<(std::ostream &, const Statistics<int16_t> &);
89+
template std::ostream& operator<<(std::ostream &, const Statistics<uint32_t> &);
90+
template std::ostream& operator<<(std::ostream &, const Statistics<int32_t> &);
91+
template std::ostream& operator<<(std::ostream &, const Statistics<uint64_t> &);
92+
template std::ostream& operator<<(std::ostream &, const Statistics<int64_t> &);
93+
template std::ostream& operator<<(std::ostream &, const Statistics<float> &);
94+
template std::ostream& operator<<(std::ostream &, const Statistics<double> &);

statistics.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef _STATISTICS_HPP
2+
#define _STATISTICS_HPP 1
3+
4+
#include <iostream>
5+
#include <vector>
6+
7+
template <typename NumT>
8+
class Statistics
9+
{
10+
template <typename TYPE>
11+
friend std::ostream& operator<<(std::ostream &os,
12+
const Statistics<TYPE> & thing);
13+
14+
public:
15+
Statistics();
16+
virtual ~Statistics();
17+
18+
void parseData(std::vector<NumT> & data);
19+
20+
private:
21+
NumT total;
22+
NumT min;
23+
NumT max;
24+
std::size_t number;
25+
};
26+
27+
#endif

0 commit comments

Comments
 (0)