mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-30 02:19:35 +08:00
async queue - overrun oldsest policy option
This commit is contained in:
@@ -15,20 +15,20 @@ public:
|
||||
using item_type = T;
|
||||
|
||||
explicit circular_q(size_t max_items)
|
||||
: max_items_(max_items + 1)
|
||||
: max_items_(max_items + 1) // one item is reserved as marker for full q
|
||||
, v_(max_items_)
|
||||
{
|
||||
}
|
||||
|
||||
// push back, overrun last item if no room left
|
||||
// push back, overrun (oldest) item if no room left
|
||||
void push_back(T &&item)
|
||||
{
|
||||
v_[head_] = std::move(item);
|
||||
head_ = (head_ + 1) % max_items_;
|
||||
v_[tail_] = std::move(item);
|
||||
tail_ = (tail_ + 1) % max_items_;
|
||||
|
||||
if (head_ == tail_)
|
||||
if (tail_ == head_) // overrun last item if full
|
||||
{
|
||||
tail_ = (tail_ + 1) % max_items_;
|
||||
head_ = (head_ + 1) % max_items_;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,25 +36,26 @@ public:
|
||||
// If there are no elements in the container, the behavior is undefined.
|
||||
void pop_front(T &popped_item)
|
||||
{
|
||||
popped_item = std::move(v_[tail_]);
|
||||
tail_ = (tail_ + 1) % max_items_;
|
||||
popped_item = std::move(v_[head_]);
|
||||
head_ = (head_ + 1) % max_items_;
|
||||
}
|
||||
|
||||
bool empty()
|
||||
{
|
||||
return head_ == tail_;
|
||||
return tail_ == head_;
|
||||
}
|
||||
|
||||
bool full()
|
||||
{
|
||||
// tail is ahead of the head by 1
|
||||
return ((head_ + 1) % max_items_) == tail_;
|
||||
// head is ahead of the tail by 1
|
||||
return ((tail_ + 1) % max_items_) == head_;
|
||||
}
|
||||
|
||||
private:
|
||||
size_t max_items_;
|
||||
typename std::vector<T>::size_type head_ = 0;
|
||||
typename std::vector<T>::size_type tail_ = 0;
|
||||
|
||||
std::vector<T> v_;
|
||||
};
|
||||
} // namespace details
|
||||
|
Reference in New Issue
Block a user