46 lines
1.6 KiB
C
46 lines
1.6 KiB
C
#ifndef INCLUDED_C_ARRAYSTACK_H
|
||
#define INCLUDED_C_ARRAYSTACK_H
|
||
|
||
#ifndef INCLUDED_C_TYPES_H
|
||
#include <c_Types.h>
|
||
#endif /*INCLUDED_C_TYPES_H*/
|
||
|
||
#ifndef INCLUDED_C_ALLOCATOR_H
|
||
#include <c_Allocator.h>
|
||
#endif /*INCLUDED_C_ALLOCATOR_H*/
|
||
|
||
|
||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||
/* */
|
||
|
||
typedef struct {
|
||
void* array; // 连续的数据存储区(直接存储数据值)
|
||
c_size_t item_size; // 单个元素的字节大小(例如 sizeof(int))
|
||
c_size_t capacity; // 当前栈的最大可容纳容量
|
||
c_size_t size; // 当前栈内的元素个数(同时也是下一个入栈元素的索引位置)
|
||
c_Allocator_t allocator; // 绑定的内存管理器
|
||
} c_ArrayStack_t;
|
||
|
||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||
/* */
|
||
|
||
c_err_t c_ArrayStack_Init(c_ArrayStack_t* self, c_size_t item_size, c_size_t capacity, c_Allocator_t* allocator);
|
||
void c_ArrayStack_Destroy(c_ArrayStack_t* self);
|
||
c_err_t c_ArrayStack_Resize(c_ArrayStack_t* self, c_size_t new_capacity);
|
||
c_err_t c_ArrayStack_Push(c_ArrayStack_t* self, const void* item);
|
||
c_err_t c_ArrayStack_Pop(c_ArrayStack_t* self, void* out_item) ;
|
||
void* c_ArrayStack_Peek(const c_ArrayStack_t* self);
|
||
|
||
C_STATIC_FORCE_INLINE
|
||
c_size_t c_ArrayStack_Size(const c_ArrayStack_t* self) {
|
||
if (!self) return 0;
|
||
return self->size;
|
||
}
|
||
|
||
C_STATIC_FORCE_INLINE
|
||
bool c_ArrayStack_IsEmpty(const c_ArrayStack_t* self) {
|
||
return c_ArrayStack_Size(self) == 0;
|
||
}
|
||
|
||
#endif /*INCLUDED_C_ARRAYSTACK_H*/
|