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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <algorithm>
#include <compare>
#include <cstddef>
#include <iterator>
#include <memory>
#include <type_traits>
#include <utility>
#include <android-base/stringprintf.h>
namespace android {
// A fixed-size ring buffer of elements.
//
// Elements can only be removed from the front/back or added to the front/back, but with O(1)
// performance. Elements from the opposing side are evicted when new elements are pushed onto a full
// buffer.
template <typename T>
class RingBuffer {
public:
using value_type = T;
using size_type = size_t;
using difference_type = ptrdiff_t;
using reference = value_type&;
using const_reference = const value_type&;
using pointer = value_type*;
using const_pointer = const value_type*;
template <typename U>
class Iterator;
using iterator = Iterator<T>;
using const_iterator = Iterator<const T>;
// Creates an empty ring buffer that can hold some capacity.
explicit RingBuffer(size_type capacity)
: mBuffer(std::allocator<value_type>().allocate(capacity)), mCapacity(capacity) {}
// Creates a full ring buffer holding a fixed number of elements initialised to some value.
explicit RingBuffer(size_type count, const_reference value) : RingBuffer(count) {
while (count) {
pushBack(value);
--count;
}
}
RingBuffer(const RingBuffer& other) : RingBuffer(other.capacity()) {
for (const auto& element : other) {
pushBack(element);
}
}
RingBuffer(RingBuffer&& other) noexcept { *this = std::move(other); }
~RingBuffer() {
if (mBuffer) {
clear();
std::allocator<value_type>().deallocate(mBuffer, mCapacity);
}
}
RingBuffer& operator=(const RingBuffer& other) { return *this = RingBuffer(other); }
RingBuffer& operator=(RingBuffer&& other) noexcept {
if (this == &other) {
return *this;
}
if (mBuffer) {
clear();
std::allocator<value_type>().deallocate(mBuffer, mCapacity);
}
mBuffer = std::move(other.mBuffer);
mCapacity = other.mCapacity;
mBegin = other.mBegin;
mSize = other.mSize;
other.mBuffer = nullptr;
other.mCapacity = 0;
other.mBegin = 0;
other.mSize = 0;
return *this;
}
iterator begin() { return {*this, 0}; }
const_iterator begin() const { return {*this, 0}; }
iterator end() { return {*this, mSize}; }
const_iterator end() const { return {*this, mSize}; }
reference front() { return mBuffer[mBegin]; }
const_reference front() const { return mBuffer[mBegin]; }
reference back() { return mBuffer[bufferIndex(mSize - 1)]; }
const_reference back() const { return mBuffer[bufferIndex(mSize - 1)]; }
reference operator[](size_type i) { return mBuffer[bufferIndex(i)]; }
const_reference operator[](size_type i) const { return mBuffer[bufferIndex(i)]; }
// Removes all elements from the buffer.
void clear() {
std::destroy(begin(), end());
mSize = 0;
}
// Removes and returns the first element from the buffer.
value_type popFront() {
value_type element = mBuffer[mBegin];
std::destroy_at(std::addressof(mBuffer[mBegin]));
mBegin = next(mBegin);
--mSize;
return element;
}
// Removes and returns the last element from the buffer.
value_type popBack() {
size_type backIndex = bufferIndex(mSize - 1);
value_type element = mBuffer[backIndex];
std::destroy_at(std::addressof(mBuffer[backIndex]));
--mSize;
return element;
}
// Adds an element to the front of the buffer.
void pushFront(const value_type& element) { pushFront(value_type(element)); }
void pushFront(value_type&& element) {
mBegin = previous(mBegin);
if (size() == capacity()) {
mBuffer[mBegin] = std::forward<value_type>(element);
} else {
// The space at mBuffer[mBegin] is uninitialised.
// TODO: Use std::construct_at when it becomes available.
new (std::addressof(mBuffer[mBegin])) value_type(std::forward<value_type>(element));
++mSize;
}
}
// Adds an element to the back of the buffer.
void pushBack(const value_type& element) { pushBack(value_type(element)); }
void pushBack(value_type&& element) {
if (size() == capacity()) {
mBuffer[mBegin] = std::forward<value_type>(element);
mBegin = next(mBegin);
} else {
// The space at mBuffer[...] is uninitialised.
// TODO: Use std::construct_at when it becomes available.
new (std::addressof(mBuffer[bufferIndex(mSize)]))
value_type(std::forward<value_type>(element));
++mSize;
}
}
bool empty() const { return mSize == 0; }
size_type capacity() const { return mCapacity; }
size_type size() const { return mSize; }
void swap(RingBuffer& other) noexcept {
using std::swap;
swap(mBuffer, other.mBuffer);
swap(mCapacity, other.mCapacity);
swap(mBegin, other.mBegin);
swap(mSize, other.mSize);
}
friend void swap(RingBuffer& lhs, RingBuffer& rhs) noexcept { lhs.swap(rhs); }
template <typename U>
class Iterator {
private:
using ContainerType = std::conditional_t<std::is_const_v<U>, const RingBuffer, RingBuffer>;
public:
using iterator_category = std::random_access_iterator_tag;
using size_type = ContainerType::size_type;
using difference_type = ContainerType::difference_type;
using value_type = std::remove_cv_t<U>;
using pointer = U*;
using reference = U&;
Iterator(ContainerType& container, size_type index)
: mContainer(container), mIndex(index) {}
Iterator(const Iterator&) = default;
Iterator& operator=(const Iterator&) = default;
Iterator& operator++() {
++mIndex;
return *this;
}
Iterator operator++(int) {
Iterator iterator(*this);
++(*this);
return iterator;
}
Iterator& operator--() {
--mIndex;
return *this;
}
Iterator operator--(int) {
Iterator iterator(*this);
--(*this);
return iterator;
}
Iterator& operator+=(difference_type n) {
mIndex += n;
return *this;
}
Iterator operator+(difference_type n) {
Iterator iterator(*this);
return iterator += n;
}
Iterator& operator-=(difference_type n) { return *this += -n; }
Iterator operator-(difference_type n) {
Iterator iterator(*this);
return iterator -= n;
}
difference_type operator-(const Iterator& other) { return mIndex - other.mIndex; }
bool operator==(const Iterator& rhs) const { return mIndex == rhs.mIndex; }
bool operator!=(const Iterator& rhs) const { return !(*this == rhs); }
friend auto operator<=>(const Iterator& lhs, const Iterator& rhs) {
return lhs.mIndex <=> rhs.mIndex;
}
reference operator[](difference_type n) { return *(*this + n); }
reference operator*() const { return mContainer[mIndex]; }
pointer operator->() const { return std::addressof(mContainer[mIndex]); }
private:
ContainerType& mContainer;
size_type mIndex = 0;
};
private:
// Returns the index of the next element in mBuffer.
size_type next(size_type index) const {
if (index == capacity() - 1) {
return 0;
} else {
return index + 1;
}
}
// Returns the index of the previous element in mBuffer.
size_type previous(size_type index) const {
if (index == 0) {
return capacity() - 1;
} else {
return index - 1;
}
}
// Converts the index of an element in [0, size()] to its corresponding index in mBuffer.
size_type bufferIndex(size_type elementIndex) const {
if (elementIndex > size()) {
abort();
}
size_type index = mBegin + elementIndex;
if (index >= capacity()) {
index -= capacity();
}
if (index >= capacity()) {
abort();
}
return index;
}
pointer mBuffer = nullptr;
size_type mCapacity = 0; // Total capacity of mBuffer.
size_type mBegin = 0; // Index of the first initialised element in mBuffer.
size_type mSize = 0; // Total number of initialised elements.
};
} // namespace android
|