blob: c4ff851dfa6afbc34fe60aa805c164a71f5bbe1d (
plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
#pragma once
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <iterator>
template <typename T>
class RingBuffer {
public:
class Sentinel {};
class Iterator {
public:
friend class RingBuffer;
using difference_type = ptrdiff_t;
using value_type = T;
using pointer = T*;
using reference = T&;
using iterator_category = std::random_access_iterator_tag;
private:
RingBuffer* mContainer;
size_t mCurr;
bool mNeedsWrapAround;
bool mHasWrapped = false;
public:
// NOTE: default constructed Iterator is in an undefined state
T& operator*() const {
return mContainer->mRing[mCurr];
}
Iterator& operator++() {
assert(*this != Sentinel{});
++mCurr;
if (mNeedsWrapAround && mCurr == mContainer->mCapacity) {
mHasWrapped = true;
mCurr = 0;
}
return *this;
}
bool operator==(const Iterator& that) const {
assert(this->mContainer == that.mContainer);
return this->mCurr == that.mCurr;
}
bool operator==(const Sentinel&) const {
return mCurr == mContainer->mTailIdx && (!mNeedsWrapAround || mHasWrapped);
}
};
using value_type = T;
using reference = T&;
using const_reference = const T&;
using iterator = Iterator;
// using const_iterator = void; // TODO
using difference_type = ptrdiff_t;
using size_type = size_t;
private:
T* mRing = nullptr;
size_t mHeadIdx = 0;
size_t mTailIdx = 0;
size_t mCapacity = 0;
size_t mSize = 0;
public:
RingBuffer() noexcept = default;
~RingBuffer() noexcept {
delete mRing;
}
RingBuffer(const RingBuffer&) noexcept = delete;
RingBuffer& operator=(const RingBuffer&) noexcept = delete;
RingBuffer(RingBuffer&& that) noexcept
: mRing{ that.mRing }
, mHeadIdx{ that.mHeadIdx }
, mTailIdx{ that.mTailIdx }
, mCapacity{ that.mCapacity }
, mSize{ that.mSize } {
that.mRing = nullptr;
}
RingBuffer& operator=(RingBuffer&& that) noexcept {
if (this != &that) {
auto oldThisRing = this->mRing;
this->mRing = that.mRing;
that.mRing = nullptr;
delete oldThisRing;
this->mHeadIdx = that.mHeadIdx;
this->mTailIdx = that.mTailIdx;
this->mCapacity = that.mCapacity;
this->mSize = that.mSize;
}
return *this;
}
Iterator begin() {
Iterator it;
it.mContainer = this;
it.mCurr = mHeadIdx;
it.mNeedsWrapAround = mHeadIdx >= mTailIdx;
return it;
}
Sentinel end() {
return Sentinel{};
}
const T& operator[](size_t i) const {
size_t idx = mHeadIdx + i;
if (idx >= mCapacity) {
idx -= mCapacity;
}
return mRing[idx];
}
T& operator[](size_t i) { return const_cast<T&>(const_cast<const RingBuffer&>(*this)[i]); }
void push_back(T t) {
if (mTailIdx == mCapacity) {
// Ring buffer is filled to the right, warp around to the beginning
// mHeadIdx > 0 must be true, since we checked that as condition (1) above
mRing[0] = std::move(t);
mTailIdx = 1;
} else {
mRing[mTailIdx] = std::move(t);
mTailIdx += 1;
}
// Push mHeadIdx backwards if overwrote element in a filled buffer
bool bufferFilled = mSize == mCapacity;
if (bufferFilled && mTailIdx > mHeadIdx) {
mHeadIdx += 1;
if (mHeadIdx == mCapacity) {
mHeadIdx = 0;
}
}
if (!bufferFilled) {
++mSize;
}
}
[[nodiscard]] size_t capacity() const {
return mCapacity;
}
[[nodiscard]] size_t size() const {
return mSize;
}
[[nodiscard]] T* GetBuffer() const { return mRing; }
[[nodiscard]] size_t GetHeadIdx() const { return mHeadIdx; }
[[nodiscard]] size_t GetTailIdx() const { return mTailIdx; }
void resize(size_t newCapacity) {
auto size = this->size();
auto oldRing = mRing;
auto newRing = mRing = new T[newCapacity];
std::rotate_copy(oldRing, oldRing + mHeadIdx, oldRing + mCapacity, newRing);
delete oldRing;
mCapacity = newCapacity;
mHeadIdx = 0;
mTailIdx = size;
}
};
|