重构
This commit is contained in:
+90
-65
@@ -1,101 +1,126 @@
|
||||
#include <c_ArrayList.h>
|
||||
#include <c_Memory.h>
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* */
|
||||
|
||||
#define DEFAULT_INITIAL_CAPACITY 4
|
||||
#define MIN_SHRINK_CAPACITY 4
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* */
|
||||
|
||||
c_err_t c_ArrayList_Init(c_ArrayList_t* self, c_size_t obj_size, c_size_t capacity) {
|
||||
if (!self || obj_size == 0) return C_ERR_PARAM;
|
||||
|
||||
self->obj_size = (int)obj_size;
|
||||
self->size = 0;
|
||||
c_err_t c_ArrayList_Init(c_ArrayList_t *self, c_size_t item_size, c_size_t capacity, c_Allocator_t *allocator) {
|
||||
if (!self || item_size==0) return C_ERR_PARAM;
|
||||
self->allocator = (allocator!=NULL)?*allocator:c_DefaultAllocator;
|
||||
self->item_size = item_size;
|
||||
self->capacity = capacity;
|
||||
self->size = 0;
|
||||
|
||||
if (capacity==0) {
|
||||
if (self->capacity==0) {
|
||||
self->array = NULL;
|
||||
}else {
|
||||
self->array = C_ALLOC(self->capacity * self->obj_size);
|
||||
self->array = c_Allocator_Alloc(&self->allocator, self->capacity * self->item_size);
|
||||
if (!self->array) {
|
||||
self->capacity = 0;
|
||||
return C_ERR_NOMEM;
|
||||
}
|
||||
}
|
||||
|
||||
return C_ERR_OK;
|
||||
}
|
||||
|
||||
void c_ArrayList_Destroy(c_ArrayList_t* self) {
|
||||
if (!self) return;
|
||||
C_FREE(self->array);
|
||||
self->capacity = 0;
|
||||
if (!self) {
|
||||
return;
|
||||
}
|
||||
if (self->array) {
|
||||
c_Allocator_Free(&self->allocator, self->array);
|
||||
self->array = NULL;
|
||||
}
|
||||
self->size = 0;
|
||||
self->obj_size=0;
|
||||
self->capacity = 0;
|
||||
}
|
||||
|
||||
c_err_t c_ArrayList_Add(c_ArrayList_t* self, void* obj) {
|
||||
if (!self || !obj) return C_ERR_PARAM;
|
||||
|
||||
// 動態擴容邏輯
|
||||
if (self->size >= self->capacity) {
|
||||
const c_size_t new_capacity = (self->capacity==0)?DEFAULT_INITIAL_CAPACITY:(self->capacity<<1);
|
||||
void* new_array = C_REALLOC(self->array, new_capacity * self->obj_size);
|
||||
if (!new_array) {
|
||||
return C_ERR_NOMEM;
|
||||
c_err_t c_ArrayList_Resize(c_ArrayList_t* self, c_size_t new_capacity) {
|
||||
if (!self) return C_ERR_PARAM;
|
||||
// 情况 1: 如果新旧容量完全相同,无需任何操作,直接返回成功
|
||||
if (new_capacity == self->capacity) {
|
||||
return C_ERR_OK;
|
||||
}
|
||||
// 情况 2: 如果新容量为 0,等同于清空并释放内部数据缓冲区
|
||||
if (new_capacity==0) {
|
||||
if (self->array) {
|
||||
c_Allocator_Free(&self->allocator, self->array);
|
||||
self->array = NULL;
|
||||
}
|
||||
self->array = new_array;
|
||||
self->capacity = new_capacity;
|
||||
self->capacity = 0;
|
||||
self->size = 0;
|
||||
return C_ERR_OK;
|
||||
}
|
||||
|
||||
// 計算目標記憶體地址並將物件內容複製進去
|
||||
char* target_addr = (char*)self->array + (self->size * self->obj_size);
|
||||
memcpy(target_addr, obj, self->obj_size);
|
||||
// 溢出检查:防止 new_capacity * item_size 导致乘法回绕引发堆破坏
|
||||
if (new_capacity > (c_size_t)-1 / self->item_size) {
|
||||
return C_ERR_OUTOFBOUND;
|
||||
}
|
||||
|
||||
const c_size_t old_bytes = self->capacity * self->item_size;
|
||||
const c_size_t new_bytes = new_capacity * self->item_size;
|
||||
|
||||
// 调用绑定的自定义/伙伴系统 Realloc 接口
|
||||
void* new_array = c_Allocator_Realloc(&self->allocator, self->array, old_bytes, new_bytes);
|
||||
if (!new_array) {
|
||||
return C_ERR_NOMEM; // 内存分配失败,保持原有状态不变(强异常安全性)
|
||||
}
|
||||
|
||||
// 更新内部状态
|
||||
self->array = new_array;
|
||||
self->capacity = new_capacity;
|
||||
|
||||
// 安全边界截断:如果新容量调小了,且当前已有元素数量超过了新容量,则被迫截断
|
||||
if (self->size > new_capacity) {
|
||||
self->size = new_capacity;
|
||||
}
|
||||
|
||||
return C_ERR_OK;
|
||||
}
|
||||
|
||||
c_err_t c_ArrayList_Add(c_ArrayList_t* self, const void* item) {
|
||||
if (!self || !item) return C_ERR_PARAM;
|
||||
if (self->size>=self->capacity) {
|
||||
c_err_t err = c_ArrayList_Resize(self, (self->capacity==0)?4:(self->capacity<<1));
|
||||
if (err!=C_ERR_OK) return err;
|
||||
}
|
||||
char* target = (char*)self->array + (self->size * self->item_size);
|
||||
memcpy(target, item, self->item_size);
|
||||
self->size++;
|
||||
return C_ERR_OK;
|
||||
}
|
||||
|
||||
void* c_ArrayList_Get(c_ArrayList_t* self, c_size_t index) {
|
||||
if (!self || !self->array || index >= self->size) {
|
||||
return NULL;
|
||||
}
|
||||
// 回傳內部記憶體塊中該元素的實際起始地址
|
||||
return (char*)self->array + (index * self->obj_size);
|
||||
void* c_ArrayList_Get(const c_ArrayList_t* self, c_size_t index) {
|
||||
if (!self || index>=self->size) return NULL;
|
||||
return (uint8_t*)self->array + (index * self->item_size);
|
||||
}
|
||||
|
||||
c_err_t c_ArrayList_Remove(c_ArrayList_t* self, c_size_t index) {
|
||||
if (!self || !self->array) return C_ERR_PARAM;
|
||||
if (index >= self->size) return C_ERR_OUTOFBOUND;
|
||||
|
||||
// 如果刪除的不是最後一個元素,需要將後方所有物件往前平移一個單位
|
||||
if (index < self->size - 1) {
|
||||
char* dest = (char*)self->array + (index * self->obj_size);
|
||||
const char* src = dest + self->obj_size;
|
||||
const c_size_t num_elements_to_move = self->size - index - 1;
|
||||
|
||||
// 使用 memmove 處理重疊記憶體區塊的複製
|
||||
memmove(dest, src, num_elements_to_move * self->obj_size);
|
||||
c_err_t c_ArrayList_Read(const c_ArrayList_t* self, c_size_t index, void* out_item) {
|
||||
if (!self || index >= self->size) {
|
||||
return C_ERR_PARAM;
|
||||
}
|
||||
|
||||
self->size--;
|
||||
// 计算内部源数据的绝对物理字节地址
|
||||
const char* source = (const char*)self->array + (index * self->item_size);
|
||||
|
||||
// 策略:当实际大小少于等于容量的 1/4,且缩容后的容量不低于设定的最小阈值时触发
|
||||
if (self->size > 0 && self->size <= (self->capacity >> 2)) {
|
||||
c_size_t new_capacity = self->capacity >> 1; // 容量减半
|
||||
void* new_array = C_REALLOC(self->array, new_capacity * self->obj_size);
|
||||
if (new_array) { // 如果 realloc 失败不影响原有数据安全,这里采用安全赋值
|
||||
self->array = new_array;
|
||||
self->capacity = new_capacity;
|
||||
}
|
||||
// 将底层数据值深度拷贝(复制)到用户提供的缓冲区中
|
||||
if (out_item) {
|
||||
memcpy(out_item, source, self->item_size);
|
||||
}
|
||||
|
||||
|
||||
return C_ERR_OK;
|
||||
}
|
||||
|
||||
c_err_t c_ArrayList_Remove(c_ArrayList_t* self, c_size_t index, void* out_item) {
|
||||
if (!self || index>=self->size) return C_ERR_PARAM;
|
||||
char* target = (char*)self->array + (index * self->item_size);
|
||||
|
||||
if (out_item) {
|
||||
memcpy(out_item, target, self->item_size);
|
||||
}
|
||||
|
||||
if (index < self->size - 1) {
|
||||
char* next = target + self->item_size;
|
||||
c_size_t move_bytes = (self->size - index - 1) * self->item_size;
|
||||
memmove(target, next, move_bytes);
|
||||
}
|
||||
|
||||
self->size--;
|
||||
return C_ERR_OK;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user