Merge pull request #28 from Bur0k/Clean_LimitedQueue

Cleaned up LimitedQueue and its Snapshot class
This commit is contained in:
fourtf 2017-02-07 20:48:43 +01:00 committed by GitHub
commit 42c459e83f
2 changed files with 29 additions and 40 deletions

View file

@ -3,8 +3,6 @@
#include "messages/limitedqueuesnapshot.h" #include "messages/limitedqueuesnapshot.h"
#include <boost/signals2.hpp>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <vector> #include <vector>
@ -17,14 +15,13 @@ class LimitedQueue
{ {
public: public:
LimitedQueue(int limit = 100, int buffer = 25) LimitedQueue(int limit = 100, int buffer = 25)
: vector(new std::vector<T>(limit + buffer)) : mutex()
, vectorPtr(this->vector)
, mutex()
, offset(0) , offset(0)
, length(0)
, limit(limit) , limit(limit)
, buffer(buffer) , buffer(buffer)
{ {
vector = std::make_shared<std::vector<T>>();
vector->reserve(this->limit + this->buffer);
} }
void void
@ -32,11 +29,10 @@ public:
{ {
std::lock_guard<std::mutex> lock(this->mutex); std::lock_guard<std::mutex> lock(this->mutex);
this->vector = new std::vector<T>(this->limit + this->buffer); this->vector = std::make_shared<std::vector<T>>();
this->vectorPtr = std::shared_ptr<std::vector<T>>(this->vector); this->vector->reserve(this->limit + this->buffer);
this->offset = 0; this->offset = 0;
this->length = 0;
} }
// return true if an item was deleted // return true if an item was deleted
@ -46,39 +42,36 @@ public:
{ {
std::lock_guard<std::mutex> lock(this->mutex); std::lock_guard<std::mutex> lock(this->mutex);
if (this->length == this->limit) { if (this->vector->size() >= this->limit) {
// vector is full // vector is full
if (this->offset == this->buffer) { if (this->offset == this->buffer) {
deleted = this->vector->at(this->offset); deleted = this->vector->at(this->offset);
// create new vector // create new vector
auto *vector = new std::vector<T>(this->limit + this->buffer); auto newVector = std::make_shared<std::vector<T>>();
newVector->reserve(this->limit + this->buffer);
for (int i = 0; i < this->limit; i++) { for (unsigned int i = 0; i < this->limit - 1; i++) {
vector->at(i) = this->vector->at(i + this->offset); newVector->push_back(this->vector->at(i + this->offset));
} }
newVector->push_back(item);
vector->at(limit - 1) = item;
this->offset = 0; this->offset = 0;
this->vector = newVector;
this->vector = vector;
this->vectorPtr = std::shared_ptr<std::vector<T>>(vector);
return true; return true;
} else { } else {
// append item and remove first
deleted = this->vector->at(this->offset); 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++; this->offset++;
return true; return true;
} }
} else { } else {
// append item // append item
this->vector->at(this->length) = item; this->vector->push_back(item);
this->length++;
return false; return false;
} }
@ -87,21 +80,23 @@ public:
messages::LimitedQueueSnapshot<T> messages::LimitedQueueSnapshot<T>
getSnapshot() getSnapshot()
{ {
std::lock_guard<std::mutex> lock(mutex); std::lock_guard<std::mutex> lock(this->mutex);
return LimitedQueueSnapshot<T>(vectorPtr, offset, length); if(vector->size() < limit) {
return LimitedQueueSnapshot<T>(this->vector, this->offset, this->vector->size());
} else {
return LimitedQueueSnapshot<T>(this->vector, this->offset, this->limit);
}
} }
private: private:
std::vector<T> *vector; std::shared_ptr<std::vector<T>> vector;
std::shared_ptr<std::vector<T>> vectorPtr;
std::mutex mutex; std::mutex mutex;
int offset; unsigned int offset;
int length; unsigned int limit;
int limit; unsigned int buffer;
int buffer;
}; };
} // namespace messages } // namespace messages

View file

@ -1,7 +1,6 @@
#ifndef LIMITEDQUEUESNAPSHOT_H #ifndef LIMITEDQUEUESNAPSHOT_H
#define LIMITEDQUEUESNAPSHOT_H #define LIMITEDQUEUESNAPSHOT_H
#include <cassert>
#include <memory> #include <memory>
#include <vector> #include <vector>
@ -14,8 +13,7 @@ class LimitedQueueSnapshot
public: public:
LimitedQueueSnapshot(std::shared_ptr<std::vector<T>> ptr, int offset, LimitedQueueSnapshot(std::shared_ptr<std::vector<T>> ptr, int offset,
int length) int length)
: vectorPtr(ptr) : vector(ptr)
, vector(ptr.get())
, offset(offset) , offset(offset)
, length(length) , length(length)
{ {
@ -24,20 +22,16 @@ public:
int int
getLength() getLength()
{ {
return length; return this->length;
} }
T const &operator[](int index) const T const &operator[](int index) const
{ {
// assert(index >= 0); return this->vector->at(index + this->offset);
// assert(index < length);
return vector->at(index + offset);
} }
private: private:
std::shared_ptr<std::vector<T>> vectorPtr; std::shared_ptr<std::vector<T>> vector;
std::vector<T> *vector;
int offset; int offset;
int length; int length;