BLT/include/blt/math/averages.h

68 lines
1.5 KiB
C
Raw Normal View History

/*
* 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
{
template<typename T, int Size>
2024-09-02 12:38:50 -04:00
class averagizer_o_matic
{
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)
{
data = new T[Size];
2024-09-02 12:38:50 -04:00
for (int i = 0; i < Size; i++)
{
data[i] = default_value;
}
m_default = default_value;
}
2024-09-02 12:38:50 -04:00
void insert(T t)
{
data[index++] = t;
if (index >= Size)
index = 0;
}
2024-09-02 12:38:50 -04:00
T average()
{
T total = 0;
2024-09-02 12:38:50 -04:00
for (int i = 0; i < Size; i++)
{
total += data[i];
}
return total / Size;
}
2024-09-02 12:38:50 -04:00
~averagizer_o_matic()
{
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);
}
}
#endif //BLT_TESTS_AVERAGES_H