50 lines
1.7 KiB
C
50 lines
1.7 KiB
C
#ifndef INCLUDED_C_PTRBAG_H
|
|||
|
|
#define INCLUDED_C_PTRBAG_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; // 存储 void* 指针的连续数组(本质上还是连续线性表)
|
||
|
|
c_size_t capacity; // 当前袋子的最大指针容量
|
||
|
|
c_size_t size; // 当前袋子中持有的有效指针个数
|
||
|
|
c_Allocator_t allocator; // 绑定的通用内存管理器
|
||
|
|
} c_PtrBag_t;
|
||
|
|
|
||
|
|
/* ------------------------------------------------------------------------------------------------------------------ */
|
||
|
|
/* */
|
||
|
|
|
||
|
|
c_err_t c_PtrBag_Init(c_PtrBag_t* self, c_size_t capacity, c_Allocator_t* allocator);
|
||
|
|
void c_PtrBag_Destroy(c_PtrBag_t* self);
|
||
|
|
|
||
|
|
// 核心指针袋操作 API
|
||
|
|
c_err_t c_PtrBag_Add(c_PtrBag_t* self, void* ptr);
|
||
|
|
c_err_t c_PtrBag_Remove(c_PtrBag_t* self, void* ptr);
|
||
|
|
c_err_t c_PtrBag_RemoveAt(c_PtrBag_t* self, c_size_t index, void** out_ptr);
|
||
|
|
bool c_PtrBag_Contains(const c_PtrBag_t* self, const void* ptr);
|
||
|
|
c_err_t c_PtrBag_Resize(c_PtrBag_t* self, c_size_t new_capacity);
|
||
|
|
void c_PtrBag_Clear(c_PtrBag_t* self);
|
||
|
|
|
||
|
|
// 内联高频辅助接口
|
||
|
|
C_STATIC_FORCE_INLINE
|
||
|
|
c_size_t c_PtrBag_Size(const c_PtrBag_t* self) {
|
||
|
|
if (!self) return 0;
|
||
|
|
return self->size;
|
||
|
|
}
|
||
|
|
|
||
|
|
C_STATIC_FORCE_INLINE
|
||
|
|
void* c_PtrBag_Get(const c_PtrBag_t* self, c_size_t index) {
|
||
|
|
if (!self || index >= self->size) return NULL;
|
||
|
|
return self->array[index];
|
||
|
|
}
|
||
|
|
|
||
|
|
#endif /*INCLUDED_C_PTRBAG_H*/
|