-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayAverager.cpp
82 lines (71 loc) · 2.32 KB
/
ArrayAverager.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
#include <stdexcept>
#include <cstdint>
#include "ArrayAverager.h"
template <typename T>
ArrayAverager<T>::ArrayAverager(size_t sampleSize, size_t arraySize) : sampleSize(sampleSize), arraySize(arraySize), pos(0) {
if (sampleSize <= 0 || arraySize <= 0) {
throw std::invalid_argument("sampleSize and arraySize must be greater than 0");
}
sum.resize(sampleSize * arraySize);
}
template <typename T>
ArrayAverager<T>::ArrayAverager(const ArrayAverager& other)
: sampleSize(other.sampleSize), arraySize(other.arraySize), pos(other.pos) {
sum = other.sum;
}
template <typename T>
ArrayAverager<T>::ArrayAverager(ArrayAverager&& other) noexcept
: sampleSize(other.sampleSize), arraySize(other.arraySize), pos(other.pos), sum(std::move(other.sum)) {
other.pos = 0;
other.sampleSize = 0;
other.arraySize = 0;
}
template <typename T>
ArrayAverager<T>& ArrayAverager<T>::operator=(const ArrayAverager& other) {
if (this != &other) {
sampleSize = other.sampleSize;
arraySize = other.arraySize;
pos = other.pos;
sum = other.sum;
}
return *this;
}
template <typename T>
ArrayAverager<T>& ArrayAverager<T>::operator=(ArrayAverager&& other) noexcept {
if (this != &other) {
sampleSize = other.sampleSize;
arraySize = other.arraySize;
pos = other.pos;
sum = std::move(other.sum);
other.pos = 0;
other.sampleSize = 0;
other.arraySize = 0;
}
return *this;
}
template <typename T>
void ArrayAverager<T>::add(const T* array) {
for (size_t i = 0; i < arraySize; ++i) {
sum[pos * arraySize + i] = array[i];
}
pos++;
if (pos >= sampleSize) {
pos = 0;
}
}
template <typename T>
template <typename T2> // Type of summing array - double would be almost universally appropriate, but it's slow
void ArrayAverager<T>::getAverage(T* average) {
std::vector<T2> temp(arraySize, T2(0));
// Sum all the values
for (size_t i = 0; i < sampleSize; ++i) {
for (size_t j = 0; j < arraySize; ++j) {
temp[j] += sum[i * arraySize + j];
}
}
for (size_t i = 0; i < arraySize; ++i) {
average[i] = static_cast<T>(temp[i] / sampleSize);
}
}
template class ArrayAverager<uint8_t>;
template void ArrayAverager<uint8_t>::getAverage<uint64_t>(uint8_t*);