2019-06-04 05:09:16 +08:00
|
|
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
2018-07-04 05:38:23 +08:00
|
|
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
|
|
|
2019-10-19 15:42:28 +08:00
|
|
|
// circular q view of std::vector.
|
2018-07-04 05:38:23 +08:00
|
|
|
#pragma once
|
|
|
|
|
2019-12-03 06:35:28 +08:00
|
|
|
#include <cassert>
|
2018-08-14 05:58:09 +08:00
|
|
|
#include <vector>
|
|
|
|
|
2018-07-04 05:38:23 +08:00
|
|
|
namespace spdlog {
|
2019-09-05 06:25:00 +08:00
|
|
|
namespace details {
|
|
|
|
template <typename T>
|
|
|
|
class circular_q {
|
|
|
|
size_t max_items_ = 0;
|
|
|
|
typename std::vector<T>::size_type head_ = 0;
|
|
|
|
typename std::vector<T>::size_type tail_ = 0;
|
|
|
|
size_t overrun_counter_ = 0;
|
|
|
|
std::vector<T> v_;
|
|
|
|
|
|
|
|
public:
|
2019-09-17 17:07:54 +08:00
|
|
|
using value_type = T;
|
2019-09-05 06:25:00 +08:00
|
|
|
|
2019-09-17 17:05:23 +08:00
|
|
|
// empty ctor - create a disabled queue with no elements allocated at all
|
2019-09-05 06:25:00 +08:00
|
|
|
circular_q() = default;
|
|
|
|
|
|
|
|
explicit circular_q(size_t max_items)
|
|
|
|
: max_items_(max_items + 1) // one item is reserved as marker for full q
|
|
|
|
,
|
|
|
|
v_(max_items_) {}
|
|
|
|
|
|
|
|
circular_q(const circular_q &) = default;
|
|
|
|
circular_q &operator=(const circular_q &) = default;
|
|
|
|
|
|
|
|
// move cannot be default,
|
|
|
|
// since we need to reset head_, tail_, etc to zero in the moved object
|
|
|
|
circular_q(circular_q &&other) SPDLOG_NOEXCEPT { copy_moveable(std::move(other)); }
|
|
|
|
|
|
|
|
circular_q &operator=(circular_q &&other) SPDLOG_NOEXCEPT {
|
|
|
|
copy_moveable(std::move(other));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// push back, overrun (oldest) item if no room left
|
|
|
|
void push_back(T &&item) {
|
|
|
|
if (max_items_ > 0) {
|
|
|
|
v_[tail_] = std::move(item);
|
|
|
|
tail_ = (tail_ + 1) % max_items_;
|
2019-09-05 05:46:08 +08:00
|
|
|
|
2019-09-05 06:25:00 +08:00
|
|
|
if (tail_ == head_) // overrun last item if full
|
2019-09-05 05:39:11 +08:00
|
|
|
{
|
2019-09-05 06:25:00 +08:00
|
|
|
head_ = (head_ + 1) % max_items_;
|
|
|
|
++overrun_counter_;
|
2019-09-05 05:39:11 +08:00
|
|
|
}
|
2019-09-05 06:25:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-17 16:56:17 +08:00
|
|
|
// Return reference to the front item.
|
|
|
|
// If there are no elements in the container, the behavior is undefined.
|
2019-09-21 23:16:38 +08:00
|
|
|
const T &front() const { return v_[head_]; }
|
2019-09-17 17:28:02 +08:00
|
|
|
|
2019-09-17 16:56:17 +08:00
|
|
|
T &front() { return v_[head_]; }
|
|
|
|
|
2019-11-09 02:25:31 +08:00
|
|
|
// Return number of elements actually stored
|
|
|
|
size_t size() const {
|
2019-11-10 02:26:49 +08:00
|
|
|
if (tail_ >= head_) {
|
2019-11-10 06:08:50 +08:00
|
|
|
return tail_ - head_;
|
2019-11-09 16:30:27 +08:00
|
|
|
} else {
|
2023-08-05 22:03:04 +08:00
|
|
|
return max_items_ - (head_ - tail_);
|
2019-11-09 16:30:27 +08:00
|
|
|
}
|
2019-11-09 02:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return const reference to item by index.
|
|
|
|
// If index is out of range 0…size()-1, the behavior is undefined.
|
|
|
|
const T &at(size_t i) const {
|
|
|
|
assert(i < size());
|
2019-11-10 06:08:50 +08:00
|
|
|
return v_[(head_ + i) % max_items_];
|
2019-11-09 02:25:31 +08:00
|
|
|
}
|
|
|
|
|
2019-09-05 06:25:00 +08:00
|
|
|
// Pop item from front.
|
|
|
|
// If there are no elements in the container, the behavior is undefined.
|
2019-09-17 16:56:17 +08:00
|
|
|
void pop_front() { head_ = (head_ + 1) % max_items_; }
|
2019-09-05 06:25:00 +08:00
|
|
|
|
|
|
|
bool empty() const { return tail_ == head_; }
|
|
|
|
|
2019-09-06 23:55:45 +08:00
|
|
|
bool full() const {
|
2019-09-05 06:25:00 +08:00
|
|
|
// head is ahead of the tail by 1
|
2019-09-21 23:16:38 +08:00
|
|
|
if (max_items_ > 0) {
|
2019-09-17 16:56:17 +08:00
|
|
|
return ((tail_ + 1) % max_items_) == head_;
|
|
|
|
}
|
2019-09-17 17:03:54 +08:00
|
|
|
return false;
|
2019-09-05 06:25:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t overrun_counter() const { return overrun_counter_; }
|
|
|
|
|
2022-06-09 19:39:57 +08:00
|
|
|
void reset_overrun_counter() { overrun_counter_ = 0; }
|
|
|
|
|
2019-09-05 06:25:00 +08:00
|
|
|
private:
|
2019-09-06 23:53:10 +08:00
|
|
|
// copy from other&& and reset it to disabled state
|
2019-09-08 01:11:35 +08:00
|
|
|
void copy_moveable(circular_q &&other) SPDLOG_NOEXCEPT {
|
2019-09-05 06:25:00 +08:00
|
|
|
max_items_ = other.max_items_;
|
|
|
|
head_ = other.head_;
|
|
|
|
tail_ = other.tail_;
|
2019-09-06 23:53:10 +08:00
|
|
|
overrun_counter_ = other.overrun_counter_;
|
|
|
|
v_ = std::move(other.v_);
|
|
|
|
|
|
|
|
// put &&other in disabled, but valid state
|
|
|
|
other.max_items_ = 0;
|
|
|
|
other.head_ = other.tail_ = 0;
|
|
|
|
other.overrun_counter_ = 0;
|
2019-09-05 06:25:00 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace details
|
2018-08-08 22:59:57 +08:00
|
|
|
} // namespace spdlog
|