aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/sliding_window
diff options
context:
space:
mode:
authorAndrey Fomichev <andrey.fomichev@gmail.com>2022-02-10 16:49:12 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:49:12 +0300
commite542cc14db4240643a06bb0dde87ecf361f101ab (patch)
tree3b03a4b94acd11a4eca042532035806e5fa08ad1 /library/cpp/sliding_window
parente2ac73225f30f7fcf7df3cb225cba257f56144c1 (diff)
downloadydb-e542cc14db4240643a06bb0dde87ecf361f101ab.tar.gz
Restoring authorship annotation for Andrey Fomichev <andrey.fomichev@gmail.com>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/sliding_window')
-rw-r--r--library/cpp/sliding_window/sliding_window.h30
-rw-r--r--library/cpp/sliding_window/sliding_window_ut.cpp10
2 files changed, 20 insertions, 20 deletions
diff --git a/library/cpp/sliding_window/sliding_window.h b/library/cpp/sliding_window/sliding_window.h
index 180bdf93d0..8c6bc516f8 100644
--- a/library/cpp/sliding_window/sliding_window.h
+++ b/library/cpp/sliding_window/sliding_window.h
@@ -1,26 +1,26 @@
-#pragma once
-
+#pragma once
+
#include <util/datetime/base.h>
#include <util/generic/vector.h>
#include <util/system/guard.h>
#include <util/system/mutex.h>
#include <util/system/types.h>
#include <util/system/yassert.h>
-
+
#include <functional>
#include <limits>
-
+
namespace NSlidingWindow {
namespace NPrivate {
template <class TValueType_, class TCmp, TValueType_ initialValue> // std::less for max, std::greater for min
struct TMinMaxOperationImpl {
using TValueType = TValueType_;
using TValueVector = TVector<TValueType>;
-
+
static constexpr TValueType InitialValue() {
return initialValue;
}
-
+
// Updates value in current bucket and returns window value
static TValueType UpdateBucket(TValueType windowValue, TValueVector& buckets, size_t index, TValueType newVal) {
Y_ASSERT(index < buckets.size());
@@ -34,7 +34,7 @@ namespace NSlidingWindow {
}
return windowValue;
}
-
+
static TValueType ClearBuckets(TValueType windowValue, TValueVector& buckets, const size_t firstElemIndex, const size_t bucketsToClear) {
Y_ASSERT(!buckets.empty());
Y_ASSERT(firstElemIndex < buckets.size());
@@ -72,7 +72,7 @@ namespace NSlidingWindow {
};
}
-
+
template <class TValueType>
using TMaxOperation = NPrivate::TMinMaxOperationImpl<TValueType, std::less<TValueType>, std::numeric_limits<TValueType>::min()>;
@@ -111,7 +111,7 @@ namespace NSlidingWindow {
return windowValue;
}
};
-
+
/////////////////////////////////////////////////////////////////////////////////////////
// TSlidingWindow
/////////////////////////////////////////////////////////////////////////////////////////
@@ -121,7 +121,7 @@ namespace NSlidingWindow {
using TValueType = typename TOperation::TValueType;
using TValueVector = TVector<TValueType>;
using TSizeType = typename TValueVector::size_type;
-
+
public:
TSlidingWindow(const TDuration& length, TSizeType partsNum)
: Mutex()
@@ -133,7 +133,7 @@ namespace NSlidingWindow {
, MicroSecondsPerBucket(length.MicroSeconds() / partsNum)
{
}
-
+
TSlidingWindow(const TSlidingWindow& w)
: Mutex()
{
@@ -175,20 +175,20 @@ namespace NSlidingWindow {
TValueType GetValue() const {
TGuard<TMutexImpl> guard(&Mutex);
return WindowValue;
- }
-
+ }
+
private:
void UpdateCurrentBucket(TValueType val) {
const TSizeType arraySize = Buckets.size();
const TSizeType pos = (FirstElem + arraySize - 1) % arraySize;
WindowValue = TOperation::UpdateBucket(WindowValue, Buckets, pos, val);
}
-
+
void AdvanceTime(const TInstant& time) {
if (time < PeriodStart + Length) {
return;
}
-
+
if (PeriodStart.MicroSeconds() == 0) {
PeriodStart = time - Length;
return;
diff --git a/library/cpp/sliding_window/sliding_window_ut.cpp b/library/cpp/sliding_window/sliding_window_ut.cpp
index 1e7343a8d3..d655f0bb0c 100644
--- a/library/cpp/sliding_window/sliding_window_ut.cpp
+++ b/library/cpp/sliding_window/sliding_window_ut.cpp
@@ -1,9 +1,9 @@
#include "sliding_window.h"
#include <library/cpp/testing/unittest/registar.h>
-
+
using namespace NSlidingWindow;
-
+
Y_UNIT_TEST_SUITE(TSlidingWindowTest) {
Y_UNIT_TEST(TestSlidingWindowMax) {
TSlidingWindow<TMaxOperation<unsigned>> w(TDuration::Minutes(5), 5);
@@ -29,7 +29,7 @@ Y_UNIT_TEST_SUITE(TSlidingWindowTest) {
now += TDuration::Minutes(5);
w.Update(1, now); // ~ 1 ~ ~ ~
UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 1); // ^
-
+
// update current bucket
w.Update(2, now); // ~ 2 ~ ~ ~
UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 2); // ^
@@ -44,8 +44,8 @@ Y_UNIT_TEST_SUITE(TSlidingWindowTest) {
now += TDuration::Minutes(5); // ~ ~ ~ ~ ~
UNIT_ASSERT_VALUES_EQUAL(w.Update(now), 0);
- }
-
+ }
+
Y_UNIT_TEST(TestSlidingWindowMin) {
TSlidingWindow<TMinOperation<unsigned>> w(TDuration::Minutes(5), 5);
TInstant start = TInstant::MicroSeconds(TDuration::Hours(1).MicroSeconds());