From 3d0da734ee1c5f239db927c5cbb83ef94337bfc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Sun, 13 Aug 2023 14:15:37 +0200 Subject: [PATCH] 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. --- AK/Statistics.h | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/AK/Statistics.h b/AK/Statistics.h index 0c193d7ed4..071f016475 100644 --- a/AK/Statistics.h +++ b/AK/Statistics.h @@ -17,12 +17,19 @@ namespace AK { static constexpr int ODD_NAIVE_MEDIAN_CUTOFF = 200; static constexpr int EVEN_NAIVE_MEDIAN_CUTOFF = 350; -template +template> class Statistics { public: Statistics() = default; ~Statistics() = default; + explicit Statistics(ContainerType&& existing_container) + : m_values(forward(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 const& values() const { return m_values; } + ContainerType const& values() const { return m_values; } size_t size() const { return m_values.size(); } private: - Vector m_values; + ContainerType m_values; T m_sum {}; };