circular_q.h
Go to the documentation of this file.
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 
11 namespace spdlog {
12 namespace details {
13 template<typename T>
15 {
16 public:
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_;
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 
62 private:
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
void pop_front(T &popped_item)
Definition: circular_q.h:40
size_t overrun_counter() const
Definition: circular_q.h:57
circular_q(size_t max_items)
Definition: circular_q.h:19
std::vector< T > v_
Definition: circular_q.h:67
std::vector< T >::size_type tail_
Definition: circular_q.h:65
Definition: async.h:27
def move(depos, offset)
Definition: depos.py:107
void push_back(T &&item)
Definition: circular_q.h:26
std::vector< T >::size_type head_
Definition: circular_q.h:64