mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +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
Notes:
sideshowbarker
2024-07-17 03:03:37 +09:00
Author: https://github.com/kleinesfilmroellchen Commit: https://github.com/SerenityOS/serenity/commit/3d0da734ee Pull-request: https://github.com/SerenityOS/serenity/pull/19905 Reviewed-by: https://github.com/gmta ✅
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 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 {};
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue