From 081bce351146dd6b849774d11e6071fad6d80c49 Mon Sep 17 00:00:00 2001 From: Burak Ok Date: Tue, 7 Feb 2017 20:40:43 +0100 Subject: [PATCH 1/2] Cleaned up LimitedQueue and its Snapshot class --- messages/limitedqueue.h | 55 +++++++++++++++------------------ messages/limitedqueuesnapshot.h | 14 +++------ 2 files changed, 29 insertions(+), 40 deletions(-) diff --git a/messages/limitedqueue.h b/messages/limitedqueue.h index 9bb3d53fb..64e3e6c2e 100644 --- a/messages/limitedqueue.h +++ b/messages/limitedqueue.h @@ -3,8 +3,6 @@ #include "messages/limitedqueuesnapshot.h" -#include - #include #include #include @@ -17,14 +15,13 @@ class LimitedQueue { public: LimitedQueue(int limit = 100, int buffer = 25) - : vector(new std::vector(limit + buffer)) - , vectorPtr(this->vector) - , mutex() + : mutex() , offset(0) - , length(0) , limit(limit) , buffer(buffer) { + vector = std::make_shared>(); + vector->reserve(this->limit + this->buffer); } void @@ -32,11 +29,10 @@ public: { std::lock_guard lock(this->mutex); - this->vector = new std::vector(this->limit + this->buffer); - this->vectorPtr = std::shared_ptr>(this->vector); + this->vector = std::make_shared>(); + this->vector->reserve(this->limit + this->buffer); this->offset = 0; - this->length = 0; } // return true if an item was deleted @@ -46,39 +42,36 @@ public: { std::lock_guard lock(this->mutex); - if (this->length == this->limit) { + if (this->vector->size() >= this->limit) { // vector is full if (this->offset == this->buffer) { deleted = this->vector->at(this->offset); // create new vector - auto *vector = new std::vector(this->limit + this->buffer); + auto newVector = std::make_shared>(); + newVector->reserve(this->limit + this->buffer); - for (int i = 0; i < this->limit; i++) { - vector->at(i) = this->vector->at(i + this->offset); + for (unsigned int i = 0; i < this->limit - 1; i++) { + newVector->push_back(this->vector->at(i + this->offset)); } - - vector->at(limit - 1) = item; + newVector->push_back(item); this->offset = 0; - - this->vector = vector; - this->vectorPtr = std::shared_ptr>(vector); + this->vector = newVector; return true; } else { - // append item and remove first deleted = this->vector->at(this->offset); - this->vector->at(this->length + this->offset) = item; + //append item and increment offset("deleting" first element) + this->vector->push_back(item); this->offset++; return true; } } else { // append item - this->vector->at(this->length) = item; - this->length++; + this->vector->push_back(item); return false; } @@ -87,21 +80,23 @@ public: messages::LimitedQueueSnapshot getSnapshot() { - std::lock_guard lock(mutex); + std::lock_guard lock(this->mutex); - return LimitedQueueSnapshot(vectorPtr, offset, length); + if(vector->size() < limit) { + return LimitedQueueSnapshot(this->vector, this->offset, this->vector->size()); + } else { + return LimitedQueueSnapshot(this->vector, this->offset, this->limit); + } } private: - std::vector *vector; - std::shared_ptr> vectorPtr; + std::shared_ptr> vector; std::mutex mutex; - int offset; - int length; - int limit; - int buffer; + unsigned int offset; + unsigned int limit; + unsigned int buffer; }; } // namespace messages diff --git a/messages/limitedqueuesnapshot.h b/messages/limitedqueuesnapshot.h index c1816814e..fd76b4489 100644 --- a/messages/limitedqueuesnapshot.h +++ b/messages/limitedqueuesnapshot.h @@ -1,7 +1,6 @@ #ifndef LIMITEDQUEUESNAPSHOT_H #define LIMITEDQUEUESNAPSHOT_H -#include #include #include @@ -14,8 +13,7 @@ class LimitedQueueSnapshot public: LimitedQueueSnapshot(std::shared_ptr> ptr, int offset, int length) - : vectorPtr(ptr) - , vector(ptr.get()) + : vector(ptr) , offset(offset) , length(length) { @@ -24,20 +22,16 @@ public: int getLength() { - return length; + return this->length; } T const &operator[](int index) const { - // assert(index >= 0); - // assert(index < length); - - return vector->at(index + offset); + return this->vector->at(index + this->offset); } private: - std::shared_ptr> vectorPtr; - std::vector *vector; + std::shared_ptr> vector; int offset; int length; From c03705d263ed5c3b2c3cb20e00717a5cab1e1fa6 Mon Sep 17 00:00:00 2001 From: hemirt Date: Thu, 9 Feb 2017 00:03:46 +0100 Subject: [PATCH 2/2] Fix typo (#30) --- emotes.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/emotes.cpp b/emotes.cpp index dd8ab5b1d..7fa16e9e3 100644 --- a/emotes.cpp +++ b/emotes.cpp @@ -89,7 +89,7 @@ Emotes::loadBttvEmotes() void Emotes::loadFfzEmotes() { - // bttv + // ffz QNetworkAccessManager *manager = new QNetworkAccessManager(); QUrl url("https://api.frankerfacez.com/v1/set/global");