libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
queue.cpp
1#include "queue.hpp"
2
3using namespace LibXR;
4
5BaseQueue::BaseQueue(uint16_t element_size, size_t length, uint8_t *buffer)
6 : queue_array_(buffer),
7 ELEMENT_SIZE(element_size),
8 length_(length),
9 own_buffer_(false)
10{
11}
12
13BaseQueue::BaseQueue(uint16_t element_size, size_t length)
14 : queue_array_(new uint8_t[length * element_size]),
15 ELEMENT_SIZE(element_size),
16 length_(length),
17 own_buffer_(true)
18{
19}
20
22{
23 if (own_buffer_)
24 {
25 delete[] queue_array_;
26 }
27}
28
29[[nodiscard]] void *BaseQueue::operator[](uint32_t index)
30{
31 return &queue_array_[static_cast<size_t>(index * ELEMENT_SIZE)];
32}
33
34ErrorCode BaseQueue::Push(const void *data)
35{
36 ASSERT(data != nullptr);
37
38 if (is_full_)
39 {
40 return ErrorCode::FULL;
41 }
42
44
45 tail_ = (tail_ + 1) % length_;
46 if (head_ == tail_)
47 {
48 is_full_ = true;
49 }
50
51 return ErrorCode::OK;
52}
53
54ErrorCode BaseQueue::Peek(void *data)
55{
56 ASSERT(data != nullptr);
57
58 if (Size() > 0)
59 {
61 return ErrorCode::OK;
62 }
63 else
64 {
65 return ErrorCode::EMPTY;
66 }
67}
68
69ErrorCode BaseQueue::Pop(void *data)
70{
71 if (Size() == 0)
72 {
73 return ErrorCode::EMPTY;
74 }
75
76 if (data != nullptr)
77 {
79 }
80 head_ = (head_ + 1) % length_;
81 is_full_ = false;
82 return ErrorCode::OK;
83}
84
86{
87 if (Size() == 0)
88 {
89 return -1;
90 }
91 return static_cast<int>((tail_ + length_ - 1) % length_);
92}
93
95{
96 if (Size() == 0)
97 {
98 return -1;
99 }
100 return static_cast<int>(head_);
101}
102
103ErrorCode BaseQueue::PushBatch(const void *data, size_t size)
104{
105 ASSERT(data != nullptr);
106
107 auto avail = EmptySize();
108 if (avail < size)
109 {
110 return ErrorCode::FULL;
111 }
112
113 auto tmp = reinterpret_cast<const uint8_t *>(data);
114
115 size_t first_part = LibXR::min(size, length_ - tail_);
117 first_part * ELEMENT_SIZE);
118
119 if (size > first_part)
120 {
122 (size - first_part) * ELEMENT_SIZE);
123 }
124
125 tail_ = (tail_ + size) % length_;
126 if (head_ == tail_)
127 {
128 is_full_ = true;
129 }
130 return ErrorCode::OK;
131}
132
133ErrorCode BaseQueue::PopBatch(void *data, size_t size)
134{
135 if (Size() < size)
136 {
137 return ErrorCode::EMPTY;
138 }
139
140 if (size == 0)
141 {
142 return ErrorCode::OK;
143 }
144 is_full_ = false;
145
146 size_t first_part = LibXR::min(size, length_ - head_);
147 if (data != nullptr)
148 {
149 auto tmp = reinterpret_cast<uint8_t *>(data);
151 first_part * ELEMENT_SIZE);
152 if (size > first_part)
153 {
155 (size - first_part) * ELEMENT_SIZE);
156 }
157 }
158
159 head_ = (head_ + size) % length_;
160 return ErrorCode::OK;
161}
162
163ErrorCode BaseQueue::PeekBatch(void *data, size_t size)
164{
165 ASSERT(data != nullptr);
166
167 if (Size() < size)
168 {
169 return ErrorCode::EMPTY;
170 }
171
172 auto index = head_;
173
174 auto tmp = reinterpret_cast<uint8_t *>(data);
175
176 size_t first_part = LibXR::min(size, length_ - index);
178 first_part * ELEMENT_SIZE);
179
180 if (first_part < size)
181 {
183 (size - first_part) * ELEMENT_SIZE);
184 }
185
186 return ErrorCode::OK;
187}
188
189ErrorCode BaseQueue::Overwrite(const void *data)
190{
191 ASSERT(data != nullptr);
192
193 head_ = tail_ = 0;
194 is_full_ = false;
195
197
198 tail_ = (tail_ + 1) % length_;
199 if (head_ == tail_)
200 {
201 is_full_ = true;
202 }
203
204 return ErrorCode::OK;
205}
206
208{
209 head_ = tail_ = 0;
210 is_full_ = false;
211}
212
213[[nodiscard]] size_t BaseQueue::Size() const
214{
215 if (is_full_)
216 {
217 return length_;
218 }
219 else if (tail_ >= head_)
220 {
221 return tail_ - head_;
222 }
223 else
224 {
225 return length_ + tail_ - head_;
226 }
227}
228
229[[nodiscard]] size_t BaseQueue::EmptySize() const { return length_ - Size(); }
bool is_full_
队列是否已满 (Indicates if the queue is full).
Definition queue.hpp:153
size_t tail_
尾部索引 (Tail index).
Definition queue.hpp:152
ErrorCode Peek(void *data)
获取队列头部的元素但不移除 (Peek at the front element without removing it).
Definition queue.cpp:54
size_t head_
头部索引 (Head index).
Definition queue.hpp:151
size_t length_
队列最大容量 (Maximum queue capacity).
Definition queue.hpp:154
ErrorCode Overwrite(const void *data)
覆盖队列中的数据 (Overwrite the queue with new data).
Definition queue.cpp:189
ErrorCode PeekBatch(void *data, size_t size)
批量获取多个元素但不移除 (Peek at multiple elements without removing them).
Definition queue.cpp:163
void * operator[](uint32_t index)
访问指定索引的元素 (Access an element at a specified index).
Definition queue.cpp:29
ErrorCode PushBatch(const void *data, size_t size)
批量推入多个元素 (Push multiple elements into the queue).
Definition queue.cpp:103
size_t Size() const
获取队列中的元素个数 (Get the number of elements in the queue).
Definition queue.cpp:213
BaseQueue(uint16_t element_size, size_t length, uint8_t *buffer)
构造函数,初始化队列 (Constructor to initialize the queue).
Definition queue.cpp:5
bool own_buffer_
是否由队列自己管理缓冲区 (Owns buffer memory).
Definition queue.hpp:155
~BaseQueue()
析构函数,释放队列内存 (Destructor to free queue memory).
Definition queue.cpp:21
int GetFirstElementIndex() const
获取队列中第一个元素的索引 (Get the index of the first element in the queue).
Definition queue.cpp:94
ErrorCode Push(const void *data)
向队列中添加一个元素 (Push an element into the queue).
Definition queue.cpp:34
ErrorCode Pop(void *data=nullptr)
移除队列头部的元素 (Pop the front element from the queue).
Definition queue.cpp:69
void Reset()
重置队列,清空所有数据 (Reset the queue and clear all data).
Definition queue.cpp:207
size_t EmptySize() const
获取队列的空闲空间 (Get the available space in the queue).
Definition queue.cpp:229
ErrorCode PopBatch(void *data, size_t size)
批量移除多个元素 (Pop multiple elements from the queue).
Definition queue.cpp:133
int GetLastElementIndex() const
获取队列中最后一个元素的索引 (Get the index of the last element in the queue).
Definition queue.cpp:85
uint8_t * queue_array_
存储队列数据的数组 (Array storing queue data).
Definition queue.hpp:149
const uint16_t ELEMENT_SIZE
每个元素的大小 (Size of each element).
Definition queue.hpp:150
static void FastCopy(void *dst, const void *src, size_t size)
快速内存拷贝 / Fast memory copy
Definition libxr_mem.cpp:17
LibXR 命名空间
Definition ch32_gpio.hpp:9
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值