AK: Allow Statistics to be used with any container type

This not only facilitates using non-owning types, but also those that
cannot be default-constructed.
This commit is contained in:
kleines Filmröllchen 2023-08-13 14:15:37 +02:00 committed by Jelle Raaijmakers
parent 9aaf516bdd
commit 3d0da734ee
Notes: sideshowbarker 2024-07-17 03:03:37 +09:00

View file

@ -17,12 +17,19 @@ namespace AK {
static constexpr int ODD_NAIVE_MEDIAN_CUTOFF = 200;
static constexpr int EVEN_NAIVE_MEDIAN_CUTOFF = 350;
template<Arithmetic T = float>
template<Arithmetic T = float, typename ContainerType = Vector<T>>
class Statistics {
public:
Statistics() = default;
~Statistics() = default;
explicit Statistics(ContainerType&& existing_container)
: m_values(forward<ContainerType>(existing_container))
{
for (auto const& value : m_values)
m_sum += value;
}
void add(T const& value)
{
// FIXME: Check for an overflow
@ -107,11 +114,11 @@ public:
return summation;
}
Vector<T> const& values() const { return m_values; }
ContainerType const& values() const { return m_values; }
size_t size() const { return m_values.size(); }
private:
Vector<T> m_values;
ContainerType m_values;
T m_sum {};
};