2017-06-07 10:09:24 +02:00
|
|
|
#pragma once
|
2017-02-02 20:35:12 +01:00
|
|
|
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "messages/LimitedQueueSnapshot.hpp"
|
2017-02-02 20:35:12 +01:00
|
|
|
|
2018-04-03 02:55:32 +02:00
|
|
|
#include <QDebug>
|
|
|
|
|
2017-02-02 20:35:12 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
#include <vector>
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
namespace chatterino {
|
2017-02-02 20:35:12 +01:00
|
|
|
|
2018-01-01 22:29:21 +01:00
|
|
|
//
|
|
|
|
// Warning:
|
|
|
|
// - this class is so overengineered it's not even funny anymore
|
|
|
|
//
|
|
|
|
// Explanation:
|
|
|
|
// - messages can be appended until 'limit' is reached
|
2018-08-06 21:17:03 +02:00
|
|
|
// - when the limit is reached for every message added one will be removed at
|
|
|
|
// the start
|
2018-01-01 22:29:21 +01:00
|
|
|
// - messages can only be added to the start when there is space for them,
|
|
|
|
// trying to add messages to the start when it's full will not add them
|
|
|
|
// - you are able to get a "Snapshot" which captures the state of this object
|
|
|
|
// - adding items to this class does not change the "items" of the snapshot
|
|
|
|
//
|
|
|
|
|
2017-02-02 20:35:12 +01:00
|
|
|
template <typename T>
|
|
|
|
class LimitedQueue
|
|
|
|
{
|
2018-01-01 22:29:21 +01:00
|
|
|
protected:
|
|
|
|
typedef std::shared_ptr<std::vector<T>> Chunk;
|
|
|
|
typedef std::shared_ptr<std::vector<Chunk>> ChunkVector;
|
|
|
|
|
2017-02-02 20:35:12 +01:00
|
|
|
public:
|
2018-07-06 19:23:47 +02:00
|
|
|
LimitedQueue(int limit = 1000)
|
|
|
|
: limit_(limit)
|
2017-02-02 20:35:12 +01:00
|
|
|
{
|
2018-01-01 22:29:21 +01:00
|
|
|
this->clear();
|
2017-02-02 22:15:09 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void clear()
|
2017-02-02 22:15:09 +01:00
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
std::lock_guard<std::mutex> lock(this->mutex_);
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
this->chunks_ =
|
|
|
|
std::make_shared<std::vector<std::shared_ptr<std::vector<T>>>>();
|
2018-01-01 22:29:21 +01:00
|
|
|
Chunk chunk = std::make_shared<std::vector<T>>();
|
2018-07-06 19:23:47 +02:00
|
|
|
chunk->resize(this->chunkSize_);
|
|
|
|
this->chunks_->push_back(chunk);
|
|
|
|
this->firstChunkOffset_ = 0;
|
|
|
|
this->lastChunkEnd_ = 0;
|
2017-02-02 20:35:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// return true if an item was deleted
|
|
|
|
// deleted will be set if the item was deleted
|
2018-01-01 22:29:21 +01:00
|
|
|
bool pushBack(const T &item, T &deleted)
|
2017-02-02 20:35:12 +01:00
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
std::lock_guard<std::mutex> lock(this->mutex_);
|
2017-02-02 20:35:12 +01:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
Chunk lastChunk = this->chunks_->back();
|
2017-02-02 20:35:12 +01:00
|
|
|
|
2018-01-01 22:29:21 +01:00
|
|
|
// still space in the last chunk
|
2018-07-06 19:23:47 +02:00
|
|
|
if (lastChunk->size() <= this->lastChunkEnd_) {
|
2018-01-01 22:29:21 +01:00
|
|
|
// create new chunk vector
|
2018-08-06 21:17:03 +02:00
|
|
|
ChunkVector newVector = std::make_shared<
|
|
|
|
std::vector<std::shared_ptr<std::vector<T>>>>();
|
2017-02-02 20:35:12 +01:00
|
|
|
|
2018-01-01 22:29:21 +01:00
|
|
|
// copy chunks
|
2018-07-06 19:23:47 +02:00
|
|
|
for (Chunk &chunk : *this->chunks_) {
|
2018-01-01 22:29:21 +01:00
|
|
|
newVector->push_back(chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
// push back new chunk
|
|
|
|
Chunk newChunk = std::make_shared<std::vector<T>>();
|
2018-07-06 19:23:47 +02:00
|
|
|
newChunk->resize(this->chunkSize_);
|
2018-01-01 22:29:21 +01:00
|
|
|
newVector->push_back(newChunk);
|
|
|
|
|
|
|
|
// replace current chunk vector
|
2018-07-06 19:23:47 +02:00
|
|
|
this->chunks_ = newVector;
|
|
|
|
this->lastChunkEnd_ = 0;
|
|
|
|
lastChunk = this->chunks_->back();
|
2018-01-01 22:29:21 +01:00
|
|
|
}
|
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
lastChunk->at(this->lastChunkEnd_++) = item;
|
2018-01-01 22:29:21 +01:00
|
|
|
|
|
|
|
return this->deleteFirstItem(deleted);
|
|
|
|
}
|
2017-02-02 20:35:12 +01:00
|
|
|
|
2018-01-01 22:29:21 +01:00
|
|
|
// returns a vector with all the accepted items
|
|
|
|
std::vector<T> pushFront(const std::vector<T> &items)
|
|
|
|
{
|
|
|
|
std::vector<T> acceptedItems;
|
|
|
|
|
|
|
|
if (this->space() > 0) {
|
2018-07-06 19:23:47 +02:00
|
|
|
std::lock_guard<std::mutex> lock(this->mutex_);
|
2017-02-02 20:35:12 +01:00
|
|
|
|
2018-01-01 22:29:21 +01:00
|
|
|
// create new vector to clone chunks into
|
2018-08-06 21:17:03 +02:00
|
|
|
ChunkVector newChunks = std::make_shared<
|
|
|
|
std::vector<std::shared_ptr<std::vector<T>>>>();
|
2017-02-02 20:35:12 +01:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
newChunks->resize(this->chunks_->size());
|
2017-02-02 20:35:12 +01:00
|
|
|
|
2018-01-01 22:29:21 +01:00
|
|
|
// copy chunks except for first one
|
2018-07-06 19:23:47 +02:00
|
|
|
for (size_t i = 1; i < this->chunks_->size(); i++) {
|
|
|
|
newChunks->at(i) = this->chunks_->at(i);
|
2017-02-02 20:35:12 +01:00
|
|
|
}
|
|
|
|
|
2018-01-01 22:29:21 +01:00
|
|
|
// create new chunk for the first one
|
|
|
|
size_t offset = std::min(this->space(), items.size());
|
|
|
|
Chunk newFirstChunk = std::make_shared<std::vector<T>>();
|
2018-07-06 19:23:47 +02:00
|
|
|
newFirstChunk->resize(this->chunks_->front()->size() + offset);
|
2018-01-01 22:29:21 +01:00
|
|
|
|
|
|
|
for (size_t i = 0; i < offset; i++) {
|
|
|
|
newFirstChunk->at(i) = items[items.size() - offset + i];
|
|
|
|
acceptedItems.push_back(items[items.size() - offset + i]);
|
|
|
|
}
|
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
for (size_t i = 0; i < this->chunks_->at(0)->size(); i++) {
|
|
|
|
newFirstChunk->at(i + offset) = this->chunks_->at(0)->at(i);
|
2018-01-01 22:29:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
newChunks->at(0) = newFirstChunk;
|
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
this->chunks_ = newChunks;
|
2018-01-19 22:45:33 +01:00
|
|
|
// qDebug() << acceptedItems.size();
|
|
|
|
// qDebug() << this->chunks->at(0)->size();
|
2018-01-01 22:29:21 +01:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
if (this->chunks_->size() == 1) {
|
|
|
|
this->lastChunkEnd_ += offset;
|
2018-01-01 22:29:21 +01:00
|
|
|
}
|
2017-02-02 20:35:12 +01:00
|
|
|
}
|
2018-01-01 22:29:21 +01:00
|
|
|
|
|
|
|
return acceptedItems;
|
2017-02-02 20:35:12 +01:00
|
|
|
}
|
|
|
|
|
2018-01-06 03:48:56 +01:00
|
|
|
// replace an single item, return index if successful, -1 if unsuccessful
|
2018-01-05 23:14:55 +01:00
|
|
|
int replaceItem(const T &item, const T &replacement)
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
std::lock_guard<std::mutex> lock(this->mutex_);
|
2018-01-05 23:14:55 +01:00
|
|
|
|
|
|
|
int x = 0;
|
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
for (size_t i = 0; i < this->chunks_->size(); i++) {
|
|
|
|
Chunk &chunk = this->chunks_->at(i);
|
2018-01-05 23:14:55 +01:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
size_t start = i == 0 ? this->firstChunkOffset_ : 0;
|
2018-08-06 21:17:03 +02:00
|
|
|
size_t end =
|
|
|
|
i == chunk->size() - 1 ? this->lastChunkEnd_ : chunk->size();
|
2018-01-05 23:14:55 +01:00
|
|
|
|
|
|
|
for (size_t j = start; j < end; j++) {
|
|
|
|
if (chunk->at(j) == item) {
|
|
|
|
Chunk newChunk = std::make_shared<std::vector<T>>();
|
|
|
|
newChunk->resize(chunk->size());
|
|
|
|
|
|
|
|
for (size_t k = 0; k < chunk->size(); k++) {
|
|
|
|
newChunk->at(k) = chunk->at(k);
|
|
|
|
}
|
|
|
|
|
|
|
|
newChunk->at(j) = replacement;
|
2018-07-06 19:23:47 +02:00
|
|
|
this->chunks_->at(i) = newChunk;
|
2018-01-05 23:14:55 +01:00
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
x++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-01-06 03:48:56 +01:00
|
|
|
// replace an item at index, return true if worked
|
|
|
|
bool replaceItem(size_t index, const T &replacement)
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
std::lock_guard<std::mutex> lock(this->mutex_);
|
2018-01-06 03:48:56 +01:00
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
size_t x = 0;
|
2018-01-06 03:48:56 +01:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
for (size_t i = 0; i < this->chunks_->size(); i++) {
|
|
|
|
Chunk &chunk = this->chunks_->at(i);
|
2018-01-06 03:48:56 +01:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
size_t start = i == 0 ? this->firstChunkOffset_ : 0;
|
2018-08-06 21:17:03 +02:00
|
|
|
size_t end =
|
|
|
|
i == chunk->size() - 1 ? this->lastChunkEnd_ : chunk->size();
|
2018-01-06 03:48:56 +01:00
|
|
|
|
|
|
|
for (size_t j = start; j < end; j++) {
|
|
|
|
if (x == index) {
|
|
|
|
Chunk newChunk = std::make_shared<std::vector<T>>();
|
|
|
|
newChunk->resize(chunk->size());
|
|
|
|
|
|
|
|
for (size_t k = 0; k < chunk->size(); k++) {
|
|
|
|
newChunk->at(k) = chunk->at(k);
|
|
|
|
}
|
|
|
|
|
|
|
|
newChunk->at(j) = replacement;
|
2018-07-06 19:23:47 +02:00
|
|
|
this->chunks_->at(i) = newChunk;
|
2018-01-06 03:48:56 +01:00
|
|
|
|
2018-01-11 20:16:25 +01:00
|
|
|
return true;
|
2018-01-06 03:48:56 +01:00
|
|
|
}
|
|
|
|
x++;
|
|
|
|
}
|
|
|
|
}
|
2018-01-11 20:16:25 +01:00
|
|
|
return false;
|
2018-01-06 03:48:56 +01:00
|
|
|
}
|
|
|
|
|
2018-01-01 22:29:21 +01:00
|
|
|
// void insertAfter(const std::vector<T> &items, const T &index)
|
|
|
|
|
2018-06-28 19:38:57 +02:00
|
|
|
LimitedQueueSnapshot<T> getSnapshot()
|
2017-02-02 20:35:12 +01:00
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
std::lock_guard<std::mutex> lock(this->mutex_);
|
2017-02-02 20:35:12 +01:00
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
return LimitedQueueSnapshot<T>(
|
|
|
|
this->chunks_, this->limit_ - this->space(),
|
|
|
|
this->firstChunkOffset_, this->lastChunkEnd_);
|
2017-02-02 20:35:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-01-01 22:29:21 +01:00
|
|
|
size_t space()
|
|
|
|
{
|
|
|
|
size_t totalSize = 0;
|
2018-07-06 19:23:47 +02:00
|
|
|
for (Chunk &chunk : *this->chunks_) {
|
2018-01-01 22:29:21 +01:00
|
|
|
totalSize += chunk->size();
|
|
|
|
}
|
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
totalSize -= this->chunks_->back()->size() - this->lastChunkEnd_;
|
|
|
|
if (this->chunks_->size() != 1) {
|
|
|
|
totalSize -= this->firstChunkOffset_;
|
2018-01-01 22:29:21 +01:00
|
|
|
}
|
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
return this->limit_ - totalSize;
|
2018-01-01 22:29:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool deleteFirstItem(T &deleted)
|
|
|
|
{
|
|
|
|
// determine if the first chunk should be deleted
|
|
|
|
if (space() > 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
deleted = this->chunks_->front()->at(this->firstChunkOffset_);
|
2018-01-01 22:29:21 +01:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
this->firstChunkOffset_++;
|
2018-01-01 22:29:21 +01:00
|
|
|
|
|
|
|
// need to delete the first chunk
|
2018-07-06 19:23:47 +02:00
|
|
|
if (this->firstChunkOffset_ == this->chunks_->front()->size() - 1) {
|
2018-01-01 22:29:21 +01:00
|
|
|
// copy the chunk vector
|
2018-08-06 21:17:03 +02:00
|
|
|
ChunkVector newVector = std::make_shared<
|
|
|
|
std::vector<std::shared_ptr<std::vector<T>>>>();
|
2018-01-01 22:29:21 +01:00
|
|
|
|
|
|
|
// delete first chunk
|
|
|
|
bool first = true;
|
2018-07-06 19:23:47 +02:00
|
|
|
for (Chunk &chunk : *this->chunks_) {
|
2018-01-01 22:29:21 +01:00
|
|
|
if (!first) {
|
|
|
|
newVector->push_back(chunk);
|
|
|
|
}
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
this->chunks_ = newVector;
|
|
|
|
this->firstChunkOffset_ = 0;
|
2018-01-01 22:29:21 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
ChunkVector chunks_;
|
|
|
|
std::mutex mutex_;
|
2017-02-02 20:35:12 +01:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
size_t firstChunkOffset_;
|
|
|
|
size_t lastChunkEnd_;
|
|
|
|
size_t limit_;
|
2018-01-01 22:29:21 +01:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
const size_t chunkSize_ = 100;
|
2017-02-02 20:35:12 +01:00
|
|
|
};
|
2017-02-06 11:38:26 +01:00
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace chatterino
|