Skip to content

Commit e0e56d4

Browse files
committed
improve darray/dlist
1 parent bf03470 commit e0e56d4

File tree

5 files changed

+488
-16
lines changed

5 files changed

+488
-16
lines changed

docs/changes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# 最新动态
2+
2025/07/26
3+
* 完善darray/dlist
4+
25
2025/07/24
36
* 删除多余的path_extname_is原型声明(感谢兆坤发现问题)
47
* 修复api comment

src/tkc/darray.c

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,18 @@ static bool_t darray_extend(darray_t* darray) {
6060
return TRUE;
6161
} else {
6262
void* elms = NULL;
63+
uint32_t old_capacity = darray->capacity;
6364
uint32_t capacity = (darray->capacity >> 1) + darray->capacity + 1;
6465

6566
elms = TKMEM_REALLOCT(void*, darray->elms, capacity);
6667
if (elms) {
6768
darray->elms = elms;
6869
darray->capacity = capacity;
70+
71+
/* 将新分配的内存区域初始化为NULL */
72+
if (capacity > old_capacity) {
73+
memset(darray->elms + old_capacity, 0, (capacity - old_capacity) * sizeof(void*));
74+
}
6975

7076
return TRUE;
7177
} else {
@@ -124,21 +130,26 @@ ret_t darray_remove_range(darray_t* darray, uint32_t start, uint32_t end) {
124130
return_value_if_fail(darray != NULL && start < end && end <= darray->size, RET_BAD_PARAMS);
125131

126132
if (darray->elms != NULL) {
127-
uint32_t i = 0, j = 0;
133+
uint32_t i = 0;
134+
uint32_t range_size = end - start;
128135
void** elms = darray->elms;
129136

130-
for (i = start, j = end; i < darray->size; i++, j++) {
131-
if (i < end) {
132-
void* iter = elms[i];
133-
darray->destroy(iter);
134-
}
135-
if (j < darray->size) {
136-
elms[i] = elms[j];
137-
} else {
138-
elms[i] = NULL;
139-
}
137+
/* 首先销毁要删除范围内的元素 */
138+
for (i = start; i < end; i++) {
139+
darray->destroy(elms[i]);
140+
}
141+
142+
/* 将end之后的元素向前移动 */
143+
for (i = start; i + range_size < darray->size; i++) {
144+
elms[i] = elms[i + range_size];
140145
}
141-
darray->size -= (end - start);
146+
147+
/* 清空尾部的元素指针 */
148+
for (i = darray->size - range_size; i < darray->size; i++) {
149+
elms[i] = NULL;
150+
}
151+
152+
darray->size -= range_size;
142153
}
143154

144155
return RET_OK;
@@ -284,12 +295,15 @@ ret_t darray_sorted_insert(darray_t* darray, void* data, tk_compare_t cmp,
284295
if (index >= 0) {
285296
if (replace_if_exist) {
286297
return darray_replace(darray, index, data);
298+
} else {
299+
/* 元素已存在但不替换,插入到找到位置之后 */
300+
index++;
287301
}
288302
} else {
289303
index = low;
290304
}
291305

292-
if (index >= darray->size) {
306+
if (index >= (int32_t)darray->size) {
293307
return darray_push(darray, data);
294308
} else {
295309
return darray_insert(darray, index, data);
@@ -422,6 +436,9 @@ int32_t darray_bsearch_index_ex(darray_t* darray, tk_compare_t cmp, void* ctx, i
422436
return_value_if_fail(darray != NULL, -1);
423437

424438
if (darray->size == 0) {
439+
if (ret_low != NULL) {
440+
*ret_low = 0;
441+
}
425442
return -1;
426443
}
427444
if (cmp == NULL) {

src/tkc/dlist.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,16 @@ static ret_t dlist_remove_node(dlist_t* dlist, dlist_node_t* node) {
167167
ret_t dlist_remove_ex(dlist_t* dlist, tk_compare_t compare, void* ctx, int32_t remove_size,
168168
bool_t reverse) {
169169
int32_t n = remove_size;
170+
tk_compare_t real_compare = NULL;
170171
return_value_if_fail(dlist != NULL, RET_BAD_PARAMS);
172+
return_value_if_fail(remove_size > 0, RET_BAD_PARAMS);
173+
174+
real_compare = (compare != NULL) ? compare : dlist->compare;
171175

172176
if (!reverse) {
173177
dlist_node_t* iter = dlist->first;
174178
while (iter != NULL) {
175-
if (compare(iter->data, ctx) == 0) {
179+
if (real_compare(iter->data, ctx) == 0) {
176180
dlist_node_t* next = iter->next;
177181
dlist_remove_node(dlist, iter);
178182
dlist_destroy_node(dlist, iter);
@@ -187,7 +191,7 @@ ret_t dlist_remove_ex(dlist_t* dlist, tk_compare_t compare, void* ctx, int32_t r
187191
} else {
188192
dlist_node_t* iter = dlist->last;
189193
while (iter != NULL) {
190-
if (compare(iter->data, ctx) == 0) {
194+
if (real_compare(iter->data, ctx) == 0) {
191195
dlist_node_t* prev = iter->prev;
192196
dlist_remove_node(dlist, iter);
193197
dlist_destroy_node(dlist, iter);

0 commit comments

Comments
 (0)