libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
stack.hpp
1#pragma once
2
3#include "libxr_def.hpp"
4#include "mutex.hpp"
5
6namespace LibXR
7{
19template <typename Data>
20class Stack
21{
22 private:
27
28 public:
34
42 {
43 if (index >= 0)
44 {
45 ASSERT(static_cast<uint32_t>(index) < depth_);
46 return stack_[index];
47 }
48 else
49 {
50 ASSERT(static_cast<int32_t>(depth_) + index >= 0);
51 return stack_[top_ + index];
52 }
53 }
54
58 uint32_t Size() const { return top_; }
59
63 uint32_t EmptySize() const { return (depth_ - top_); }
64
72 ErrorCode Push(const Data &data)
73 {
74 mutex_.Lock();
75
76 if (top_ >= depth_)
77 {
78 mutex_.Unlock();
79 return ErrorCode::FULL;
80 }
81 stack_[top_++] = data;
82 mutex_.Unlock();
83 return ErrorCode::OK;
84 }
85
93 ErrorCode Pop(Data &data)
94 {
95 mutex_.Lock();
96
97 if (top_ == 0)
98 {
99 mutex_.Unlock();
100 return ErrorCode::EMPTY;
101 }
102 data = stack_[--top_];
103 mutex_.Unlock();
104 return ErrorCode::OK;
105 }
106
114 ErrorCode Pop()
115 {
116 mutex_.Lock();
117
118 if (top_ == 0)
119 {
120 mutex_.Unlock();
121 return ErrorCode::EMPTY;
122 }
123 --top_;
124 mutex_.Unlock();
125 return ErrorCode::OK;
126 }
127
135 ErrorCode Peek(Data &data)
136 {
137 mutex_.Lock();
138
139 if (top_ == 0)
140 {
141 mutex_.Unlock();
142
143 return ErrorCode::EMPTY;
144 }
145 data = stack_[top_ - 1];
146 mutex_.Unlock();
147
148 return ErrorCode::OK;
149 }
150
160 ErrorCode Insert(const Data &data, uint32_t index)
161 {
162 mutex_.Lock();
163 if (top_ >= depth_)
164 {
165 mutex_.Unlock();
166 return ErrorCode::FULL;
167 }
168
169 if (index > top_)
170 {
171 mutex_.Unlock();
172 return ErrorCode::OUT_OF_RANGE;
173 }
174
175 for (uint32_t i = top_ + 1; i > index; i--)
176 {
177 stack_[i] = stack_[i - 1];
178 }
179
180 stack_[index] = data;
181 top_++;
182
183 mutex_.Unlock();
184
185 return ErrorCode::OK;
186 }
187
196 {
197 mutex_.Lock();
198 if (index >= top_)
199 {
200 mutex_.Unlock();
201 return ErrorCode::OUT_OF_RANGE;
202 }
203
204 for (uint32_t i = index; i < top_ - 1; i++)
205 {
206 stack_[i] = stack_[i + 1];
207 }
208 top_--;
209 mutex_.Unlock();
210 return ErrorCode::OK;
211 }
212
219 void Reset()
220 {
221 mutex_.Lock();
222 top_ = 0;
223 mutex_.Unlock();
224 }
225};
226} // namespace LibXR
互斥锁类,提供线程同步机制 (Mutex class providing thread synchronization mechanisms).
Definition mutex.hpp:18
ErrorCode Lock()
加锁,如果锁已被占用,则阻塞等待 (Lock the mutex, blocking if it is already locked).
Definition mutex.cpp:13
void Unlock()
解锁互斥锁 (Unlock the mutex).
Definition mutex.cpp:27
线程安全的栈数据结构 / Thread-safe stack data structure
Definition stack.hpp:21
ErrorCode Delete(uint32_t index)
删除指定位置的数据 / Deletes data at a specified position
Definition stack.hpp:195
uint32_t EmptySize() const
获取栈的剩余可用空间 / Returns the remaining available space in the stack
Definition stack.hpp:63
ErrorCode Peek(Data &data)
获取栈顶数据但不弹出 / Retrieves the top data of the stack without popping
Definition stack.hpp:135
uint32_t depth_
栈的最大容量 / Maximum capacity of the stack
Definition stack.hpp:25
Data & operator[](int32_t index)
获取指定索引的元素 / Retrieves the element at a specified index
Definition stack.hpp:41
ErrorCode Insert(const Data &data, uint32_t index)
在指定位置插入数据 / Inserts data at a specified position
Definition stack.hpp:160
void Reset()
重置栈 / Resets the stack
Definition stack.hpp:219
Data * stack_
栈存储数组 / Stack storage array
Definition stack.hpp:23
ErrorCode Push(const Data &data)
向栈中推入数据 / Pushes data onto the stack
Definition stack.hpp:72
ErrorCode Pop(Data &data)
从栈中弹出数据 / Pops data from the stack
Definition stack.hpp:93
ErrorCode Pop()
从栈中弹出数据(不返回数据) / Pops data from the stack (without returning data)
Definition stack.hpp:114
uint32_t top_
当前栈顶索引 / Current top index of the stack
Definition stack.hpp:24
LibXR::Mutex mutex_
互斥锁,确保线程安全 / Mutex to ensure thread safety
Definition stack.hpp:26
Stack(uint32_t depth)
栈的构造函数 / Stack constructor
Definition stack.hpp:33
uint32_t Size() const
获取栈中当前元素数量 / Returns the number of elements currently in the stack
Definition stack.hpp:58
LibXR Color Control Library / LibXR终端颜色控制库
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值