OpenFusion/src/Bucket.hpp
Gent Semaj feaca861d5
Optimize performance
Introduces a generic, fixed-size `Bucket` type in place of the inner
vector. This does add size generics but I think it's a very good
tradeoff considering how performance-sensitive this codepath is.
2024-10-28 20:40:52 -07:00

40 lines
617 B
C++

#pragma once
#include <array>
#include <optional>
template<class T, size_t N>
class Bucket {
std::array<T, N> buf;
size_t sz;
public:
Bucket() {
sz = 0;
}
void add(const T& item) {
if (sz < N) {
buf[sz++] = item;
}
}
std::optional<T> get(size_t idx) const {
if (idx < sz) {
return buf[idx];
}
return std::nullopt;
}
size_t size() const {
return sz;
}
bool isFull() const {
return sz == N;
}
void clear() {
sz = 0;
}
};