aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/sliding_window/sliding_window.h
diff options
context:
space:
mode:
authorVasily Gerasimov <UgnineSirdis@gmail.com>2022-02-10 16:49:09 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:49:09 +0300
commit6cdc8f140213c595e4ad38bc3d97fcef1146b8c3 (patch)
treef69637041e6fed76ebae0c74ae1fa0c4be6ab5b4 /library/cpp/sliding_window/sliding_window.h
parente5d4696304c6689379ac7ce334512404d4b7836c (diff)
downloadydb-6cdc8f140213c595e4ad38bc3d97fcef1146b8c3.tar.gz
Restoring authorship annotation for Vasily Gerasimov <UgnineSirdis@gmail.com>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/sliding_window/sliding_window.h')
-rw-r--r--library/cpp/sliding_window/sliding_window.h64
1 files changed, 32 insertions, 32 deletions
diff --git a/library/cpp/sliding_window/sliding_window.h b/library/cpp/sliding_window/sliding_window.h
index 180bdf93d0..d140ec7f9c 100644
--- a/library/cpp/sliding_window/sliding_window.h
+++ b/library/cpp/sliding_window/sliding_window.h
@@ -1,16 +1,16 @@
#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 <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>
+#include <functional>
+#include <limits>
-namespace NSlidingWindow {
+namespace NSlidingWindow {
namespace NPrivate {
template <class TValueType_, class TCmp, TValueType_ initialValue> // std::less for max, std::greater for min
struct TMinMaxOperationImpl {
@@ -33,14 +33,14 @@ 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());
Y_ASSERT(bucketsToClear <= buckets.size());
TCmp cmp;
-
+
bool needRecalc = false;
size_t current = firstElemIndex;
const size_t arraySize = buckets.size();
@@ -51,7 +51,7 @@ namespace NSlidingWindow {
}
curVal = InitialValue();
current = (current + 1) % arraySize;
- }
+ }
if (needRecalc) {
windowValue = InitialValue();
for (size_t i = 0; i < firstElemIndex; ++i) {
@@ -66,28 +66,28 @@ namespace NSlidingWindow {
windowValue = val;
}
}
- }
+ }
return windowValue;
- }
+ }
};
- }
+ }
template <class TValueType>
using TMaxOperation = NPrivate::TMinMaxOperationImpl<TValueType, std::less<TValueType>, std::numeric_limits<TValueType>::min()>;
-
+
template <class TValueType>
using TMinOperation = NPrivate::TMinMaxOperationImpl<TValueType, std::greater<TValueType>, std::numeric_limits<TValueType>::max()>;
-
+
template <class TValueType_>
struct TSumOperation {
using TValueType = TValueType_;
using TValueVector = TVector<TValueType>;
-
+
static constexpr TValueType InitialValue() {
return TValueType(); // zero
}
-
+
// 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());
@@ -95,12 +95,12 @@ namespace NSlidingWindow {
windowValue += newVal;
return windowValue;
}
-
+
static TValueType ClearBuckets(TValueType windowValue, TValueVector& buckets, size_t firstElemIndex, size_t bucketsToClear) {
Y_ASSERT(!buckets.empty());
Y_ASSERT(firstElemIndex < buckets.size());
Y_ASSERT(bucketsToClear <= buckets.size());
-
+
const size_t arraySize = buckets.size();
for (size_t i = 0; i < bucketsToClear; ++i) {
TValueType& curVal = buckets[firstElemIndex];
@@ -145,17 +145,17 @@ namespace NSlidingWindow {
Length = w.Length;
MicroSecondsPerBucket = w.MicroSecondsPerBucket;
}
-
+
TSlidingWindow(TSlidingWindow&&) = default;
-
+
TSlidingWindow& operator=(TSlidingWindow&&) = default;
TSlidingWindow& operator=(const TSlidingWindow&) = delete;
-
+
// Period of time
const TDuration& GetDuration() const {
return Length;
}
-
+
// Update window with new value and time
TValueType Update(TValueType val, TInstant t) {
TGuard<TMutexImpl> guard(&Mutex);
@@ -163,14 +163,14 @@ namespace NSlidingWindow {
UpdateCurrentBucket(val);
return WindowValue;
}
-
+
// Update just time, without new values
TValueType Update(TInstant t) {
TGuard<TMutexImpl> guard(&Mutex);
AdvanceTime(t);
return WindowValue;
}
-
+
// Get current window value (without updating current time)
TValueType GetValue() const {
TGuard<TMutexImpl> guard(&Mutex);
@@ -182,7 +182,7 @@ namespace NSlidingWindow {
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) {
@@ -193,24 +193,24 @@ namespace NSlidingWindow {
PeriodStart = time - Length;
return;
}
-
+
const TInstant& newPeriodStart = time - Length;
const ui64 tmDiff = (newPeriodStart - PeriodStart).MicroSeconds();
const TSizeType bucketsDiff = tmDiff / MicroSecondsPerBucket;
const TSizeType arraySize = Buckets.size();
const TSizeType buckets = Min(bucketsDiff, arraySize);
-
+
WindowValue = TOperation::ClearBuckets(WindowValue, Buckets, FirstElem, buckets);
FirstElem = (FirstElem + buckets) % arraySize;
PeriodStart += TDuration::MicroSeconds(bucketsDiff * MicroSecondsPerBucket);
-
+
// Check that PeriodStart lags behind newPeriodStart
// (which is actual, uptodate, precise and equal to time - Length) not more
// then MicroSecondsPerBucket
Y_ASSERT(newPeriodStart >= PeriodStart);
Y_ASSERT((newPeriodStart - PeriodStart).MicroSeconds() <= MicroSecondsPerBucket);
}
-
+
mutable TMutexImpl Mutex;
TValueVector Buckets;