Agora  1.2.0
Agora project
circular_q.h
Go to the documentation of this file.
1 // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
3 
4 // circular q view of std::vector.
5 #pragma once
6 
7 #include <vector>
8 #include <cassert>
9 
10 namespace spdlog {
11 namespace details {
12 template<typename T>
14 {
15  size_t max_items_ = 0;
16  typename std::vector<T>::size_type head_ = 0;
17  typename std::vector<T>::size_type tail_ = 0;
18  size_t overrun_counter_ = 0;
19  std::vector<T> v_;
20 
21 public:
22  using value_type = T;
23 
24  // empty ctor - create a disabled queue with no elements allocated at all
25  circular_q() = default;
26 
27  explicit circular_q(size_t max_items)
28  : max_items_(max_items + 1) // one item is reserved as marker for full q
29  , v_(max_items_)
30  {}
31 
32  circular_q(const circular_q &) = default;
33  circular_q &operator=(const circular_q &) = default;
34 
35  // move cannot be default,
36  // since we need to reset head_, tail_, etc to zero in the moved object
38  {
39  copy_moveable(std::move(other));
40  }
41 
43  {
44  copy_moveable(std::move(other));
45  return *this;
46  }
47 
48  // push back, overrun (oldest) item if no room left
49  void push_back(T &&item)
50  {
51  if (max_items_ > 0)
52  {
53  v_[tail_] = std::move(item);
54  tail_ = (tail_ + 1) % max_items_;
55 
56  if (tail_ == head_) // overrun last item if full
57  {
58  head_ = (head_ + 1) % max_items_;
60  }
61  }
62  }
63 
64  // Return reference to the front item.
65  // If there are no elements in the container, the behavior is undefined.
66  const T &front() const
67  {
68  return v_[head_];
69  }
70 
71  T &front()
72  {
73  return v_[head_];
74  }
75 
76  // Return number of elements actually stored
77  size_t size() const
78  {
79  if (tail_ >= head_)
80  {
81  return tail_ - head_;
82  }
83  else
84  {
85  return max_items_ - (head_ - tail_);
86  }
87  }
88 
89  // Return const reference to item by index.
90  // If index is out of range 0…size()-1, the behavior is undefined.
91  const T &at(size_t i) const
92  {
93  assert(i < size());
94  return v_[(head_ + i) % max_items_];
95  }
96 
97  // Pop item from front.
98  // If there are no elements in the container, the behavior is undefined.
99  void pop_front()
100  {
101  head_ = (head_ + 1) % max_items_;
102  }
103 
104  bool empty() const
105  {
106  return tail_ == head_;
107  }
108 
109  bool full() const
110  {
111  // head is ahead of the tail by 1
112  if (max_items_ > 0)
113  {
114  return ((tail_ + 1) % max_items_) == head_;
115  }
116  return false;
117  }
118 
119  size_t overrun_counter() const
120  {
121  return overrun_counter_;
122  }
123 
124 private:
125  // copy from other&& and reset it to disabled state
127  {
128  max_items_ = other.max_items_;
129  head_ = other.head_;
130  tail_ = other.tail_;
131  overrun_counter_ = other.overrun_counter_;
132  v_ = std::move(other.v_);
133 
134  // put &&other in disabled, but valid state
135  other.max_items_ = 0;
136  other.head_ = other.tail_ = 0;
137  other.overrun_counter_ = 0;
138  }
139 };
140 } // namespace details
141 } // namespace spdlog
spdlog::details::circular_q::overrun_counter_
size_t overrun_counter_
Definition: circular_q.h:18
spdlog::details::circular_q::head_
std::vector< T >::size_type head_
Definition: circular_q.h:16
SPDLOG_NOEXCEPT
#define SPDLOG_NOEXCEPT
Definition: common.h:64
spdlog::details::circular_q::full
bool full() const
Definition: circular_q.h:109
spdlog::details::circular_q::copy_moveable
void copy_moveable(circular_q &&other) SPDLOG_NOEXCEPT
Definition: circular_q.h:126
spdlog::details::circular_q::pop_front
void pop_front()
Definition: circular_q.h:99
spdlog::details::circular_q::circular_q
circular_q()=default
spdlog::details::circular_q< filename_t >::value_type
filename_t value_type
Definition: circular_q.h:22
T
T
Definition: simulate_performance.m:4
spdlog::details::circular_q::circular_q
circular_q(size_t max_items)
Definition: circular_q.h:27
spdlog::details::circular_q::empty
bool empty() const
Definition: circular_q.h:104
spdlog
Definition: async.h:25
spdlog::details::circular_q::v_
std::vector< T > v_
Definition: circular_q.h:19
i
for i
Definition: generate_data.m:107
spdlog::details::circular_q::overrun_counter
size_t overrun_counter() const
Definition: circular_q.h:119
spdlog::details::circular_q::tail_
std::vector< T >::size_type tail_
Definition: circular_q.h:17
spdlog::details::circular_q::operator=
circular_q & operator=(const circular_q &)=default
spdlog::details::circular_q::operator=
circular_q & operator=(circular_q &&other) SPDLOG_NOEXCEPT
Definition: circular_q.h:42
spdlog::details::circular_q::front
T & front()
Definition: circular_q.h:71
spdlog::details::circular_q::size
size_t size() const
Definition: circular_q.h:77
spdlog::details::circular_q::max_items_
size_t max_items_
Definition: circular_q.h:15
spdlog::details::circular_q::circular_q
circular_q(circular_q &&other) SPDLOG_NOEXCEPT
Definition: circular_q.h:37
spdlog::details::circular_q::front
const T & front() const
Definition: circular_q.h:66
spdlog::details::circular_q::at
const T & at(size_t i) const
Definition: circular_q.h:91
spdlog::details::circular_q
Definition: circular_q.h:13
spdlog::details::circular_q::push_back
void push_back(T &&item)
Definition: circular_q.h:49