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