2017-06-07 10:09:24 +02:00
|
|
|
#pragma once
|
2017-02-02 20:35:12 +01:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace messages {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class LimitedQueueSnapshot
|
|
|
|
{
|
|
|
|
public:
|
2018-01-05 11:22:51 +01:00
|
|
|
LimitedQueueSnapshot()
|
|
|
|
: length(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-01-01 22:29:21 +01:00
|
|
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
std::size_t getLength()
|
2017-02-02 20:35:12 +01:00
|
|
|
{
|
2018-01-01 22:29:21 +01:00
|
|
|
return this->length;
|
2017-02-02 20:35:12 +01:00
|
|
|
}
|
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
T const &operator[](std::size_t index) const
|
2017-02-02 20:35:12 +01:00
|
|
|
{
|
2018-01-01 22:29:21 +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:
|
2018-01-01 22:29:21 +01:00
|
|
|
std::shared_ptr<std::vector<std::shared_ptr<std::vector<T>>>> chunks;
|
2017-02-02 20:35:12 +01:00
|
|
|
|
2018-01-01 22:29:21 +01:00
|
|
|
size_t length;
|
|
|
|
size_t firstChunkOffset;
|
|
|
|
size_t lastChunkEnd;
|
2017-02-02 20:35:12 +01:00
|
|
|
};
|
2017-06-07 10:09:24 +02:00
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace messages
|
|
|
|
} // namespace chatterino
|