mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 08:14:58 +00:00
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:
parent
9aaf516bdd
commit
3d0da734ee
1 changed files with 10 additions and 3 deletions
|
@ -17,12 +17,19 @@ namespace AK {
|
||||||
static constexpr int ODD_NAIVE_MEDIAN_CUTOFF = 200;
|
static constexpr int ODD_NAIVE_MEDIAN_CUTOFF = 200;
|
||||||
static constexpr int EVEN_NAIVE_MEDIAN_CUTOFF = 350;
|
static constexpr int EVEN_NAIVE_MEDIAN_CUTOFF = 350;
|
||||||
|
|
||||||
template<Arithmetic T = float>
|
template<Arithmetic T = float, typename ContainerType = Vector<T>>
|
||||||
class Statistics {
|
class Statistics {
|
||||||
public:
|
public:
|
||||||
Statistics() = default;
|
Statistics() = default;
|
||||||
~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)
|
void add(T const& value)
|
||||||
{
|
{
|
||||||
// FIXME: Check for an overflow
|
// FIXME: Check for an overflow
|
||||||
|
@ -107,11 +114,11 @@ public:
|
||||||
return summation;
|
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(); }
|
size_t size() const { return m_values.size(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Vector<T> m_values;
|
ContainerType m_values;
|
||||||
T m_sum {};
|
T m_sum {};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue