mirror-chatterino2/src/messages/LimitedQueueSnapshot.hpp

58 lines
1.2 KiB
C++
Raw Normal View History

#pragma once
2017-02-02 20:35:12 +01:00
2018-01-05 13:42:23 +01:00
#include <cassert>
2017-02-02 20:35:12 +01:00
#include <memory>
#include <vector>
namespace chatterino {
template <typename T>
class LimitedQueueSnapshot
{
public:
LimitedQueueSnapshot() = default;
LimitedQueueSnapshot(std::shared_ptr<std::vector<std::shared_ptr<std::vector<T>>>> _chunks,
size_t _length, size_t _firstChunkOffset, size_t _lastChunkEnd)
: chunks(_chunks)
, length(_length)
, firstChunkOffset(_firstChunkOffset)
, lastChunkEnd(_lastChunkEnd)
2017-02-02 20:35:12 +01:00
{
}
std::size_t getLength()
2017-02-02 20:35:12 +01:00
{
return this->length;
2017-02-02 20:35:12 +01:00
}
T const &operator[](std::size_t index) const
2017-02-02 20:35:12 +01:00
{
index += this->firstChunkOffset;
size_t x = 0;
for (size_t i = 0; i < this->chunks->size(); i++) {
auto &chunk = this->chunks->at(i);
if (x <= index && x + chunk->size() > index) {
return chunk->at(index - x);
}
x += chunk->size();
}
assert(false && "out of range");
return this->chunks->at(0)->at(0);
2017-02-02 20:35:12 +01:00
}
private:
std::shared_ptr<std::vector<std::shared_ptr<std::vector<T>>>> chunks;
2017-02-02 20:35:12 +01:00
size_t length = 0;
size_t firstChunkOffset = 0;
size_t lastChunkEnd = 0;
2017-02-02 20:35:12 +01:00
};
2017-04-14 17:52:22 +02:00
} // namespace chatterino