From c631434cee84860714d257a924c797c4a3718953 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sat, 22 Jun 2019 13:03:57 +0200 Subject: [PATCH] Make use of our "Chunk" and "ChunkVector" aliases better --- src/messages/LimitedQueue.hpp | 45 ++++++++++++++++------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/src/messages/LimitedQueue.hpp b/src/messages/LimitedQueue.hpp index 3142d8eca..2ed68fb47 100644 --- a/src/messages/LimitedQueue.hpp +++ b/src/messages/LimitedQueue.hpp @@ -28,8 +28,8 @@ template class LimitedQueue { protected: - typedef std::shared_ptr> Chunk; - typedef std::shared_ptr> ChunkVector; + using Chunk = std::vector; + using ChunkVector = std::vector>; public: LimitedQueue(size_t limit = 1000) @@ -42,9 +42,8 @@ public: { std::lock_guard lock(this->mutex_); - this->chunks_ = - std::make_shared>>>(); - Chunk chunk = std::make_shared>(); + this->chunks_ = std::make_shared(); + auto chunk = std::make_shared(); chunk->resize(this->chunkSize_); this->chunks_->push_back(chunk); this->firstChunkOffset_ = 0; @@ -57,23 +56,21 @@ public: { std::lock_guard lock(this->mutex_); - Chunk lastChunk = this->chunks_->back(); + auto lastChunk = this->chunks_->back(); - // still space in the last chunk if (lastChunk->size() <= this->lastChunkEnd_) { - // create new chunk vector - ChunkVector newVector = std::make_shared< - std::vector>>>(); + // Last chunk is full, create a new one and rebuild our chunk vector + auto newVector = std::make_shared(); // copy chunks - for (Chunk &chunk : *this->chunks_) + for (auto &chunk : *this->chunks_) { newVector->push_back(chunk); } // push back new chunk - Chunk newChunk = std::make_shared>(); + auto newChunk = std::make_shared(); newChunk->resize(this->chunkSize_); newVector->push_back(newChunk); @@ -98,8 +95,7 @@ public: std::lock_guard lock(this->mutex_); // create new vector to clone chunks into - ChunkVector newChunks = std::make_shared< - std::vector>>>(); + auto newChunks = std::make_shared(); newChunks->resize(this->chunks_->size()); @@ -112,7 +108,7 @@ public: // create new chunk for the first one size_t offset = std::min(this->space(), static_cast(items.size())); - Chunk newFirstChunk = std::make_shared>(); + auto newFirstChunk = std::make_shared(); newFirstChunk->resize(this->chunks_->front()->size() + offset); for (size_t i = 0; i < offset; i++) @@ -150,7 +146,7 @@ public: for (size_t i = 0; i < this->chunks_->size(); i++) { - Chunk &chunk = this->chunks_->at(i); + auto &chunk = this->chunks_->at(i); size_t start = i == 0 ? this->firstChunkOffset_ : 0; size_t end = @@ -160,7 +156,7 @@ public: { if (chunk->at(j) == item) { - Chunk newChunk = std::make_shared>(); + auto newChunk = std::make_shared(); newChunk->resize(chunk->size()); for (size_t k = 0; k < chunk->size(); k++) @@ -189,7 +185,7 @@ public: for (size_t i = 0; i < this->chunks_->size(); i++) { - Chunk &chunk = this->chunks_->at(i); + auto &chunk = this->chunks_->at(i); size_t start = i == 0 ? this->firstChunkOffset_ : 0; size_t end = @@ -199,7 +195,7 @@ public: { if (x == index) { - Chunk newChunk = std::make_shared>(); + auto newChunk = std::make_shared(); newChunk->resize(chunk->size()); for (size_t k = 0; k < chunk->size(); k++) @@ -233,7 +229,7 @@ private: qsizetype space() { size_t totalSize = 0; - for (Chunk &chunk : *this->chunks_) + for (auto &chunk : *this->chunks_) { totalSize += chunk->size(); } @@ -263,12 +259,11 @@ private: if (this->firstChunkOffset_ == this->chunks_->front()->size() - 1) { // copy the chunk vector - ChunkVector newVector = std::make_shared< - std::vector>>>(); + auto newVector = std::make_shared(); // delete first chunk bool first = true; - for (Chunk &chunk : *this->chunks_) + for (auto &chunk : *this->chunks_) { if (!first) { @@ -283,12 +278,12 @@ private: return true; } - ChunkVector chunks_; + std::shared_ptr chunks_; std::mutex mutex_; size_t firstChunkOffset_; size_t lastChunkEnd_; - size_t limit_; + const size_t limit_; const size_t chunkSize_ = 100; };