mirror-chatterino2/src/messages/LimitedQueueSnapshot.hpp

61 lines
1.3 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;
2018-08-06 21:17:03 +02:00
LimitedQueueSnapshot(
std::shared_ptr<std::vector<std::shared_ptr<std::vector<T>>>> chunks,
size_t length, size_t firstChunkOffset, size_t lastChunkEnd)
2018-07-06 19:23:47 +02:00
: chunks_(chunks)
, length_(length)
, firstChunkOffset_(firstChunkOffset)
, lastChunkEnd_(lastChunkEnd)
2017-02-02 20:35:12 +01:00
{
}
2019-09-04 00:29:58 +02:00
std::size_t size() const
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
}
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_;
size_t x = 0;
2018-10-21 13:43:02 +02:00
for (size_t i = 0; i < this->chunks_->size(); i++)
{
2018-07-06 19:23:47 +02:00
auto &chunk = this->chunks_->at(i);
2018-10-21 13:43:02 +02: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-04-14 17:52:22 +02:00
} // namespace chatterino