5 #include <boost/circular_buffer.hpp> 6 #include <boost/optional.hpp> 10 #include <shared_mutex> 30 [[nodiscard]]
size_t limit()
const 40 [[nodiscard]]
size_t space()
const 42 return this->limit() - this->buffer_.size();
49 [[nodiscard]]
bool empty()
const 51 std::shared_lock lock(this->mutex_);
53 return this->buffer_.empty();
65 [[nodiscard]] boost::optional<T>
get(
size_t index)
const 67 std::shared_lock lock(this->mutex_);
69 if (index >= this->buffer_.size())
74 return this->buffer_[index];
82 [[nodiscard]] boost::optional<T>
first()
const 84 std::shared_lock lock(this->mutex_);
86 if (this->buffer_.empty())
91 return this->buffer_.front();
99 [[nodiscard]] boost::optional<T>
last()
const 101 std::shared_lock lock(this->mutex_);
103 if (this->buffer_.empty())
108 return this->buffer_.back();
116 std::unique_lock lock(this->mutex_);
118 this->buffer_.clear();
130 std::unique_lock lock(this->mutex_);
132 bool full = this->buffer_.full();
135 deleted = this->buffer_.front();
137 this->buffer_.push_back(item);
149 std::unique_lock lock(this->mutex_);
151 bool full = this->buffer_.full();
152 this->buffer_.push_back(item);
168 std::unique_lock lock(this->mutex_);
170 size_t numToPush = std::min(items.size(), this->space());
171 std::vector<T> pushed;
172 pushed.reserve(numToPush);
174 size_t f = items.size() - numToPush;
175 size_t b = items.size() - 1;
176 for (; f < items.size(); ++f, --b)
178 this->buffer_.push_front(items[b]);
179 pushed.push_back(items[f]);
193 template <
typename Equals = std::equal_to<T>>
196 std::unique_lock lock(this->mutex_);
199 for (
int i = 0; i < this->buffer_.size(); ++i)
201 if (eq(this->buffer_[i], needle))
203 this->buffer_[i] = replacement;
219 std::unique_lock lock(this->mutex_);
221 if (index >= this->buffer_.size())
226 this->buffer_[index] = replacement;
238 template <
typename Equals = std::equal_to<T>>
241 std::unique_lock lock(this->mutex_);
244 for (
auto it = this->buffer_.begin(); it != this->buffer_.end(); ++it)
248 this->buffer_.insert(it, item);
264 template <
typename Equals = std::equal_to<T>>
267 std::unique_lock lock(this->mutex_);
270 for (
auto it = this->buffer_.begin(); it != this->buffer_.end(); ++it)
275 this->buffer_.insert(it, item);
285 std::shared_lock lock(this->mutex_);
302 template <
typename Predicate>
303 [[nodiscard]] boost::optional<T>
find(Predicate pred)
const 305 std::shared_lock lock(this->mutex_);
307 for (
const auto &item : this->buffer_)
329 template <
typename Predicate>
330 [[nodiscard]] boost::optional<T>
rfind(Predicate pred)
const 332 std::shared_lock lock(this->mutex_);
334 for (
auto it = this->buffer_.rbegin(); it != this->buffer_.rend(); ++it)
346 mutable std::shared_mutex mutex_;
349 boost::circular_buffer<T> buffer_;
bool pushBack(const T &item, T &deleted)
Push an item to the end of the queue.
Definition: LimitedQueue.hpp:128
Definition: LimitedQueue.hpp:16
boost::optional< T > first() const
Get the first item from the queue.
Definition: LimitedQueue.hpp:82
Definition: LimitedQueueSnapshot.hpp:15
Definition: Application.cpp:48
bool insertAfter(const T &needle, const T &item)
Inserts the given item after another item.
Definition: LimitedQueue.hpp:265
bool insertBefore(const T &needle, const T &item)
Inserts the given item before another item.
Definition: LimitedQueue.hpp:239
std::vector< T > pushFront(const std::vector< T > &items)
Push items into beginning of queue.
Definition: LimitedQueue.hpp:166
LimitedQueue(size_t limit=1000)
Definition: LimitedQueue.hpp:19
bool pushBack(const T &item)
Push an item to the end of the queue.
Definition: LimitedQueue.hpp:147
bool replaceItem(size_t index, const T &replacement)
Replace the item at index with the given item.
Definition: LimitedQueue.hpp:217
bool empty() const
Return true if the buffer is empty.
Definition: LimitedQueue.hpp:49
boost::optional< T > rfind(Predicate pred) const
Returns the first item matching a predicate, checking in reverse.
Definition: LimitedQueue.hpp:330
void clear()
Modifiers.
Definition: LimitedQueue.hpp:114
boost::optional< T > find(Predicate pred) const
Returns the first item matching a predicate.
Definition: LimitedQueue.hpp:303
int replaceItem(const T &needle, const T &replacement)
Replace the needle with the given item.
Definition: LimitedQueue.hpp:194
LimitedQueueSnapshot< T > getSnapshot() const
Definition: LimitedQueue.hpp:283
boost::optional< T > last() const
Get the last item from the queue.
Definition: LimitedQueue.hpp:99