aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/actor/ring_buffer.h
blob: 937d8d5a3db0cc2452dfb27c5a5625ba6ca162e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#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;
    TVector<T> Data; 

    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;
        while ((1u << newCapacityPow) < sz + ui32(Size()) + 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;
    }

    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) {
            Data[WritePos + i] = value[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) {
        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();
    }
};