6#include "libxr_def.hpp"
23template <
typename Data>
40 Stack(uint32_t depth) : stack_(new Data[depth]), depth_(depth) {}
62 ASSERT(
static_cast<uint32_t
>(index) < depth_);
67 ASSERT(
static_cast<int32_t
>(top_) + index >= 0);
68 return stack_[top_ + index];
75 [[nodiscard]] uint32_t
Size()
const {
return top_; }
80 [[nodiscard]] uint32_t
EmptySize()
const {
return depth_ - top_; }
98 stack_[top_++] = data;
119 data = stack_[--top_];
162 data = stack_[top_ - 1];
183 static_assert(std::is_trivially_copyable_v<Data>,
184 "Stack::Insert moves elements with memmove and requires a trivially "
185 "copyable element type");
200 memmove(&stack_[index + 1], &stack_[index], (top_ - index) *
sizeof(Data));
202 stack_[index] = data;
226 for (uint32_t i = index; i < top_ - 1; i++)
228 stack_[i] = stack_[i + 1];
互斥锁类,提供线程同步机制 Mutex class providing thread synchronization mechanisms.
ErrorCode Lock()
加锁,如果锁已被占用,则阻塞等待 Lock the mutex, blocking if it is already locked.
void Unlock()
解锁互斥锁 Unlock the mutex.
线程安全的栈数据结构 / Thread-safe stack data structure
ErrorCode Delete(uint32_t index)
删除指定位置的数据 / Deletes data at a specified position
uint32_t EmptySize() const
获取栈的剩余可用空间 / Returns the remaining available space in the stack
~Stack()
析构栈并释放存储数组 / Destroys the stack and frees the storage array
Stack(const Stack &)=delete
禁止复制构造 / Not copy-constructible
ErrorCode Peek(Data &data)
获取栈顶数据但不弹出 / Retrieves the top data of the stack without popping
Data & operator[](int32_t index)
获取指定索引的元素 / Retrieves the element at a specified index
Stack & operator=(const Stack &)=delete
禁止复制赋值 / Not copy-assignable
ErrorCode Insert(const Data &data, uint32_t index)
在指定位置插入数据 / Inserts data at a specified position
void Reset()
重置栈 / Resets the stack
ErrorCode Push(const Data &data)
向栈中推入数据 / Pushes data onto the stack
ErrorCode Pop(Data &data)
从栈中弹出数据 / Pops data from the stack
ErrorCode Pop()
从栈中弹出数据(不返回数据) / Pops data from the stack (without returning data)
Stack(uint32_t depth)
栈的构造函数 / Stack constructor
uint32_t Size() const
获取栈中当前元素数量 / Returns the number of elements currently in the stack
LibXR 命名空间 / LibXR namespace.
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
@ OUT_OF_RANGE
超出范围 / Out of range
@ OK
操作成功 / Operation successful