From 9f786f0550b6d267aabf79364b802f0fadc9629d Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Thu, 29 Jul 2021 12:00:18 +0000 Subject: [PATCH] epee: allow copying a rolling_median_t object --- contrib/epee/include/rolling_median.h | 15 ++++++++++++++- tests/unit_tests/rolling_median.cpp | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/contrib/epee/include/rolling_median.h b/contrib/epee/include/rolling_median.h index 8965a8268..e230fd974 100644 --- a/contrib/epee/include/rolling_median.h +++ b/contrib/epee/include/rolling_median.h @@ -126,7 +126,6 @@ private: protected: rolling_median_t &operator=(const rolling_median_t&) = delete; - rolling_median_t(const rolling_median_t&) = delete; public: //creates new rolling_median_t: to calculate `nItems` running median. @@ -139,6 +138,20 @@ public: clear(); } + rolling_median_t(const rolling_median_t &other) + { + N = other.N; + int size = N * (sizeof(Item) + sizeof(int) * 2); + data = (Item*)malloc(size); + memcpy(data, other.data, size); + pos = (int*) (data + N); + heap = pos + N + (N / 2); //points to middle of storage. + idx = other.idx; + minCt = other.minCt; + maxCt = other.maxCt; + sz = other.sz; + } + rolling_median_t(rolling_median_t &&m) { memcpy(this, &m, sizeof(rolling_median_t)); diff --git a/tests/unit_tests/rolling_median.cpp b/tests/unit_tests/rolling_median.cpp index 4d99bbb0d..07da0f0ef 100644 --- a/tests/unit_tests/rolling_median.cpp +++ b/tests/unit_tests/rolling_median.cpp @@ -211,3 +211,21 @@ TEST(rolling_median, size) ASSERT_EQ(m.size(), std::min(10, i + 2)); } } + +TEST(rolling_median, copy) +{ + epee::misc_utils::rolling_median_t m(100); + + for (int i = 0; i < 100; ++i) + m.insert(rand()); + + epee::misc_utils::rolling_median_t copy(m); + + for (int i = 0; i < 5000; ++i) + { + uint64_t v = rand(); + m.insert(v); + copy.insert(v); + ASSERT_EQ(m.median(), copy.median()); + } +}