source: trunk/GDE/SINA/builddir/include/spdlog/details/circular_q.h

Last change on this file was 19170, checked in by westram, 2 years ago
  • sina source
    • unpack + remove tarball
    • no longer ignore sina builddir.
File size: 1.5 KB
Line 
1//
2// Copyright(c) 2018 Gabi Melman.
3// Distributed under the MIT License (http://opensource.org/licenses/MIT)
4//
5
6// cirucal q view of std::vector.
7#pragma once
8
9#include <vector>
10
11namespace spdlog {
12namespace details {
13template<typename T>
14class circular_q
15{
16public:
17    using item_type = T;
18
19    explicit circular_q(size_t max_items)
20        : max_items_(max_items + 1) // one item is reserved as marker for full q
21        , v_(max_items_)
22    {
23    }
24
25    // push back, overrun (oldest) item if no room left
26    void push_back(T &&item)
27    {
28        v_[tail_] = std::move(item);
29        tail_ = (tail_ + 1) % max_items_;
30
31        if (tail_ == head_) // overrun last item if full
32        {
33            head_ = (head_ + 1) % max_items_;
34            ++overrun_counter_;
35        }
36    }
37
38    // Pop item from front.
39    // If there are no elements in the container, the behavior is undefined.
40    void pop_front(T &popped_item)
41    {
42        popped_item = std::move(v_[head_]);
43        head_ = (head_ + 1) % max_items_;
44    }
45
46    bool empty()
47    {
48        return tail_ == head_;
49    }
50
51    bool full()
52    {
53        // head is ahead of the tail by 1
54        return ((tail_ + 1) % max_items_) == head_;
55    }
56
57    size_t overrun_counter() const
58    {
59        return overrun_counter_;
60    }
61
62private:
63    size_t max_items_;
64    typename std::vector<T>::size_type head_ = 0;
65    typename std::vector<T>::size_type tail_ = 0;
66
67    std::vector<T> v_;
68
69    size_t overrun_counter_ = 0;
70};
71} // namespace details
72} // namespace spdlog
Note: See TracBrowser for help on using the repository browser.