2017-06-07 10:09:24 +02:00
|
|
|
#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:
|
2018-04-03 02:55:32 +02:00
|
|
|
LimitedQueueSnapshot() = default;
|
2018-01-05 11:22:51 +01:00
|
|
|
|
2018-07-06 19:23:47 +02: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-07-06 19:23:47 +02: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-07-06 19:23:47 +02:00
|
|
|
index += this->firstChunkOffset_;
|
2018-01-01 22:29:21 +01:00
|
|
|
|
|
|
|
size_t x = 0;
|
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
for (size_t i = 0; i < this->chunks_->size(); i++) {
|
|
|
|
auto &chunk = this->chunks_->at(i);
|
2018-01-01 22:29:21 +01:00
|
|
|
|
|
|
|
if (x <= index && x + chunk->size() > index) {
|
|
|
|
return chunk->at(index - x);
|
|
|
|
}
|
|
|
|
x += chunk->size();
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(false && "out of range");
|
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
return this->chunks_->at(0)->at(0);
|
2017-02-02 20:35:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-07-06 19:23:47 +02:00
|
|
|
std::shared_ptr<std::vector<std::shared_ptr<std::vector<T>>>> chunks_;
|
2017-02-02 20:35:12 +01:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
size_t length_ = 0;
|
|
|
|
size_t firstChunkOffset_ = 0;
|
|
|
|
size_t lastChunkEnd_ = 0;
|
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 chatterino
|