diff options
author | nga <nga@yandex-team.ru> | 2022-02-10 16:48:09 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:48:09 +0300 |
commit | 1f553f46fb4f3c5eec631352cdd900a0709016af (patch) | |
tree | a231fba2c03b440becaea6c86a2702d0bfb0336e /library/cpp/messagebus/actor/ring_buffer.h | |
parent | c4de7efdedc25b49cbea74bd589eecb61b55b60a (diff) | |
download | ydb-1f553f46fb4f3c5eec631352cdd900a0709016af.tar.gz |
Restoring authorship annotation for <nga@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/messagebus/actor/ring_buffer.h')
-rw-r--r-- | library/cpp/messagebus/actor/ring_buffer.h | 236 |
1 files changed, 118 insertions, 118 deletions
diff --git a/library/cpp/messagebus/actor/ring_buffer.h b/library/cpp/messagebus/actor/ring_buffer.h index ec5706f7c7..a0e2f481bf 100644 --- a/library/cpp/messagebus/actor/ring_buffer.h +++ b/library/cpp/messagebus/actor/ring_buffer.h @@ -1,135 +1,135 @@ -#pragma once - +#pragma once + #include <util/generic/array_ref.h> #include <util/generic/maybe.h> #include <util/generic/utility.h> #include <util/generic/vector.h> -#include <util/system/yassert.h> - -template <typename T> -struct TRingBuffer { -private: - ui32 CapacityPow; - ui32 CapacityMask; - ui32 Capacity; - ui32 WritePos; - ui32 ReadPos; +#include <util/system/yassert.h> + +template <typename T> +struct TRingBuffer { +private: + ui32 CapacityPow; + ui32 CapacityMask; + ui32 Capacity; + ui32 WritePos; + ui32 ReadPos; TVector<T> Data; - - void StateCheck() const { + + void StateCheck() const { Y_ASSERT(Capacity == Data.size()); Y_ASSERT(Capacity == (1u << CapacityPow)); Y_ASSERT((Capacity & CapacityMask) == 0u); Y_ASSERT(Capacity - CapacityMask == 1u); Y_ASSERT(WritePos < Capacity); Y_ASSERT(ReadPos < Capacity); - } - - size_t Writable() const { - return (Capacity + ReadPos - WritePos - 1) & CapacityMask; - } - - void ReserveWritable(ui32 sz) { - if (sz <= Writable()) - return; - - ui32 newCapacityPow = CapacityPow; + } + + size_t Writable() const { + return (Capacity + ReadPos - WritePos - 1) & CapacityMask; + } + + void ReserveWritable(ui32 sz) { + if (sz <= Writable()) + return; + + ui32 newCapacityPow = CapacityPow; while ((1u << newCapacityPow) < sz + ui32(Size()) + 1u) { - ++newCapacityPow; - } - ui32 newCapacity = 1u << newCapacityPow; - ui32 newCapacityMask = newCapacity - 1u; + ++newCapacityPow; + } + ui32 newCapacity = 1u << newCapacityPow; + ui32 newCapacityMask = newCapacity - 1u; TVector<T> newData(newCapacity); - ui32 oldSize = Size(); - // Copy old elements - for (size_t i = 0; i < oldSize; ++i) { - newData[i] = Get(i); - } - - CapacityPow = newCapacityPow; - Capacity = newCapacity; - CapacityMask = newCapacityMask; - Data.swap(newData); - ReadPos = 0; - WritePos = oldSize; - - StateCheck(); - } - - const T& Get(ui32 i) const { - return Data[(ReadPos + i) & CapacityMask]; - } - -public: - TRingBuffer() - : CapacityPow(0) - , CapacityMask(0) - , Capacity(1 << CapacityPow) - , WritePos(0) - , ReadPos(0) - , Data(Capacity) - { - StateCheck(); - } - - size_t Size() const { - return (Capacity + WritePos - ReadPos) & CapacityMask; - } - - bool Empty() const { - return WritePos == ReadPos; - } - + ui32 oldSize = Size(); + // Copy old elements + for (size_t i = 0; i < oldSize; ++i) { + newData[i] = Get(i); + } + + CapacityPow = newCapacityPow; + Capacity = newCapacity; + CapacityMask = newCapacityMask; + Data.swap(newData); + ReadPos = 0; + WritePos = oldSize; + + StateCheck(); + } + + const T& Get(ui32 i) const { + return Data[(ReadPos + i) & CapacityMask]; + } + +public: + TRingBuffer() + : CapacityPow(0) + , CapacityMask(0) + , Capacity(1 << CapacityPow) + , WritePos(0) + , ReadPos(0) + , Data(Capacity) + { + StateCheck(); + } + + size_t Size() const { + return (Capacity + WritePos - ReadPos) & CapacityMask; + } + + bool Empty() const { + return WritePos == ReadPos; + } + void PushAll(TArrayRef<const T> value) { - ReserveWritable(value.size()); - - ui32 secondSize; - ui32 firstSize; - - if (WritePos + value.size() <= Capacity) { - firstSize = value.size(); - secondSize = 0; - } else { - firstSize = Capacity - WritePos; - secondSize = value.size() - firstSize; - } - - for (size_t i = 0; i < firstSize; ++i) { + ReserveWritable(value.size()); + + ui32 secondSize; + ui32 firstSize; + + if (WritePos + value.size() <= Capacity) { + firstSize = value.size(); + secondSize = 0; + } else { + firstSize = Capacity - WritePos; + secondSize = value.size() - firstSize; + } + + for (size_t i = 0; i < firstSize; ++i) { Data[WritePos + i] = value[i]; - } - - for (size_t i = 0; i < secondSize; ++i) { + } + + for (size_t i = 0; i < secondSize; ++i) { Data[i] = value[firstSize + i]; - } - - WritePos = (WritePos + value.size()) & CapacityMask; - StateCheck(); - } - - void Push(const T& t) { + } + + WritePos = (WritePos + value.size()) & CapacityMask; + StateCheck(); + } + + void Push(const T& t) { PushAll(MakeArrayRef(&t, 1)); - } - - bool TryPop(T* r) { - StateCheck(); - if (Empty()) { - return false; - } - *r = Data[ReadPos]; - ReadPos = (ReadPos + 1) & CapacityMask; - return true; - } - - TMaybe<T> TryPop() { - T tmp; - if (TryPop(&tmp)) { - return tmp; - } else { - return TMaybe<T>(); - } - } - - T Pop() { - return *TryPop(); - } -}; + } + + bool TryPop(T* r) { + StateCheck(); + if (Empty()) { + return false; + } + *r = Data[ReadPos]; + ReadPos = (ReadPos + 1) & CapacityMask; + return true; + } + + TMaybe<T> TryPop() { + T tmp; + if (TryPop(&tmp)) { + return tmp; + } else { + return TMaybe<T>(); + } + } + + T Pop() { + return *TryPop(); + } +}; |