2023-02-28 23:20:56 -05:00
|
|
|
/*
|
|
|
|
* Created by Brett on 28/02/23.
|
|
|
|
* Licensed under GNU General Public License V3.0
|
|
|
|
* See LICENSE file for license detail
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef BLT_TESTS_AVERAGES_H
|
|
|
|
#define BLT_TESTS_AVERAGES_H
|
|
|
|
|
2024-09-02 12:38:50 -04:00
|
|
|
namespace blt
|
|
|
|
{
|
2023-02-28 23:20:56 -05:00
|
|
|
|
|
|
|
template<typename T, int Size>
|
2024-09-02 12:38:50 -04:00
|
|
|
class averagizer_o_matic
|
|
|
|
{
|
2023-02-28 23:20:56 -05:00
|
|
|
private:
|
|
|
|
T* data;
|
|
|
|
int index = 0;
|
|
|
|
int m_default = 0;
|
|
|
|
public:
|
2024-09-02 12:38:50 -04:00
|
|
|
averagizer_o_matic(): averagizer_o_matic(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
explicit averagizer_o_matic(T default_value)
|
|
|
|
{
|
2023-02-28 23:20:56 -05:00
|
|
|
data = new T[Size];
|
2024-09-02 12:38:50 -04:00
|
|
|
for (int i = 0; i < Size; i++)
|
|
|
|
{
|
2023-02-28 23:20:56 -05:00
|
|
|
data[i] = default_value;
|
|
|
|
}
|
|
|
|
m_default = default_value;
|
|
|
|
}
|
|
|
|
|
2024-09-02 12:38:50 -04:00
|
|
|
void insert(T t)
|
|
|
|
{
|
2023-02-28 23:20:56 -05:00
|
|
|
data[index++] = t;
|
|
|
|
if (index >= Size)
|
|
|
|
index = 0;
|
|
|
|
}
|
|
|
|
|
2024-09-02 12:38:50 -04:00
|
|
|
T average()
|
|
|
|
{
|
2023-02-28 23:20:56 -05:00
|
|
|
T total = 0;
|
2024-09-02 12:38:50 -04:00
|
|
|
for (int i = 0; i < Size; i++)
|
|
|
|
{
|
2023-02-28 23:20:56 -05:00
|
|
|
total += data[i];
|
|
|
|
}
|
|
|
|
return total / Size;
|
|
|
|
}
|
|
|
|
|
2024-09-02 12:38:50 -04:00
|
|
|
~averagizer_o_matic()
|
|
|
|
{
|
2023-02-28 23:20:56 -05:00
|
|
|
delete[] data;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-09-02 12:38:50 -04:00
|
|
|
template<typename A, typename B>
|
|
|
|
double average(A a, B b)
|
|
|
|
{
|
|
|
|
if (b == 0)
|
|
|
|
return 0;
|
|
|
|
return static_cast<double>(a) / static_cast<double>(b);
|
|
|
|
}
|
|
|
|
|
2023-02-28 23:20:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif //BLT_TESTS_AVERAGES_H
|