mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
refactored limitedqueue
This commit is contained in:
parent
95c7ae9f18
commit
5cdc5de30a
1 changed files with 31 additions and 33 deletions
|
@ -15,61 +15,60 @@ class LimitedQueue
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
LimitedQueue(int limit = 100, int buffer = 25)
|
LimitedQueue(int limit = 100, int buffer = 25)
|
||||||
: mutex()
|
: _offset(0)
|
||||||
, offset(0)
|
, _limit(limit)
|
||||||
, limit(limit)
|
, _buffer(buffer)
|
||||||
, buffer(buffer)
|
|
||||||
{
|
{
|
||||||
vector = std::make_shared<std::vector<T>>();
|
_vector = std::make_shared<std::vector<T>>();
|
||||||
vector->reserve(this->limit + this->buffer);
|
_vector->reserve(_limit + _buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear()
|
void clear()
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(this->mutex);
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
|
|
||||||
this->vector = std::make_shared<std::vector<T>>();
|
_vector = std::make_shared<std::vector<T>>();
|
||||||
this->vector->reserve(this->limit + this->buffer);
|
_vector->reserve(_limit + _buffer);
|
||||||
|
|
||||||
this->offset = 0;
|
_offset = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// return true if an item was deleted
|
// return true if an item was deleted
|
||||||
// deleted will be set if the item was deleted
|
// deleted will be set if the item was deleted
|
||||||
bool appendItem(const T &item, T &deleted)
|
bool appendItem(const T &item, T &deleted)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(this->mutex);
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
|
|
||||||
if (this->vector->size() >= this->limit) {
|
if (_vector->size() >= _limit) {
|
||||||
// vector is full
|
// vector is full
|
||||||
if (this->offset == this->buffer) {
|
if (_offset == _buffer) {
|
||||||
deleted = this->vector->at(this->offset);
|
deleted = _vector->at(_offset);
|
||||||
|
|
||||||
// create new vector
|
// create new vector
|
||||||
auto newVector = std::make_shared<std::vector<T>>();
|
auto newVector = std::make_shared<std::vector<T>>();
|
||||||
newVector->reserve(this->limit + this->buffer);
|
newVector->reserve(_limit + _buffer);
|
||||||
|
|
||||||
for (unsigned int i = 0; i < this->limit - 1; i++) {
|
for (unsigned int i = 0; i < _limit - 1; i++) {
|
||||||
newVector->push_back(this->vector->at(i + this->offset));
|
newVector->push_back(_vector->at(i + _offset));
|
||||||
}
|
}
|
||||||
newVector->push_back(item);
|
newVector->push_back(item);
|
||||||
|
|
||||||
this->offset = 0;
|
_offset = 0;
|
||||||
this->vector = newVector;
|
_vector = newVector;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
deleted = this->vector->at(this->offset);
|
deleted = _vector->at(_offset);
|
||||||
|
|
||||||
// append item and increment offset("deleting" first element)
|
// append item and increment offset("deleting" first element)
|
||||||
this->vector->push_back(item);
|
_vector->push_back(item);
|
||||||
this->offset++;
|
_offset++;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// append item
|
// append item
|
||||||
this->vector->push_back(item);
|
_vector->push_back(item);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -77,23 +76,22 @@ public:
|
||||||
|
|
||||||
messages::LimitedQueueSnapshot<T> getSnapshot()
|
messages::LimitedQueueSnapshot<T> getSnapshot()
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(this->mutex);
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
|
|
||||||
if (vector->size() < limit) {
|
if (_vector->size() < _limit) {
|
||||||
return LimitedQueueSnapshot<T>(this->vector, this->offset, this->vector->size());
|
return LimitedQueueSnapshot<T>(_vector, _offset, _vector->size());
|
||||||
} else {
|
} else {
|
||||||
return LimitedQueueSnapshot<T>(this->vector, this->offset, this->limit);
|
return LimitedQueueSnapshot<T>(_vector, _offset, _limit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<std::vector<T>> vector;
|
std::shared_ptr<std::vector<T>> _vector;
|
||||||
|
std::mutex _mutex;
|
||||||
|
|
||||||
std::mutex mutex;
|
unsigned int _offset;
|
||||||
|
unsigned int _limit;
|
||||||
unsigned int offset;
|
unsigned int _buffer;
|
||||||
unsigned int limit;
|
|
||||||
unsigned int buffer;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace messages
|
} // namespace messages
|
||||||
|
|
Loading…
Reference in a new issue