improve darray

This commit is contained in:
lixianjing 2021-09-04 17:56:01 +08:00
parent 2b4728b824
commit b9eb740c8f
2 changed files with 60 additions and 8 deletions

View File

@ -77,17 +77,21 @@ static bool_t darray_extend(darray_t* darray) {
}
}
int32_t darray_find_index(darray_t* darray, void* data) {
int32_t darray_find_index_ex(darray_t* darray, tk_compare_t compare, void* data) {
int32_t i = 0;
int32_t size = 0;
void** elms = NULL;
return_value_if_fail(darray != NULL, -1);
elms = darray->elms;
size = darray->size;
if (compare == NULL) {
compare = darray->compare ? darray->compare : pointer_compare;
}
for (i = 0; i < size; i++) {
void* iter = elms[i];
if (darray->compare(iter, data) == 0) {
if (compare(iter, data) == 0) {
return i;
}
}
@ -95,6 +99,10 @@ int32_t darray_find_index(darray_t* darray, void* data) {
return -1;
}
int32_t darray_find_index(darray_t* darray, void* data) {
return darray_find_index_ex(darray, NULL, data);
}
ret_t darray_remove_index(darray_t* darray, uint32_t index) {
int32_t i = 0;
int32_t size = 0;
@ -115,11 +123,11 @@ ret_t darray_remove_index(darray_t* darray, uint32_t index) {
return RET_OK;
}
ret_t darray_remove(darray_t* darray, void* data) {
ret_t darray_remove_ex(darray_t* darray, tk_compare_t cmp, void* ctx) {
int32_t index = 0;
return_value_if_fail(darray != NULL, RET_BAD_PARAMS);
index = darray_find_index(darray, data);
index = darray_find_index_ex(darray, cmp, ctx);
if (index < 0) {
return RET_NOT_FOUND;
} else {
@ -127,6 +135,10 @@ ret_t darray_remove(darray_t* darray, void* data) {
}
}
ret_t darray_remove(darray_t* darray, void* data) {
return darray_remove_ex(darray, NULL, data);
}
ret_t darray_remove_all(darray_t* darray, tk_compare_t cmp, void* ctx) {
int32_t i = 0;
int32_t k = 0;
@ -182,12 +194,16 @@ ret_t darray_find_all(darray_t* darray, tk_compare_t cmp, void* ctx, darray_t* m
return RET_OK;
}
void* darray_find(darray_t* darray, void* data) {
int32_t pos = darray_find_index(darray, data);
void* darray_find_ex(darray_t* darray, tk_compare_t cmp, void* ctx) {
int32_t pos = darray_find_index_ex(darray, cmp, ctx);
return pos >= 0 ? darray->elms[pos] : NULL;
}
void* darray_find(darray_t* darray, void* data) {
return darray_find_ex(darray, NULL, data);
}
void* darray_pop(darray_t* darray) {
return_value_if_fail(darray != NULL, NULL);
@ -372,8 +388,11 @@ static int32_t darray_bsearch_index_impl(darray_t* darray, tk_compare_t cmp, voi
int32_t high = 0;
int32_t result = 0;
void* iter = NULL;
return_value_if_fail(darray != NULL && darray->size > 0, -1);
return_value_if_fail(darray != NULL, -1);
if (darray->size == 0) {
return -1;
}
if (cmp == NULL) {
cmp = darray->compare;
}

View File

@ -119,6 +119,17 @@ darray_t* darray_init(darray_t* darray, uint32_t capacity, tk_destroy_t destroy,
*/
void* darray_find(darray_t* darray, void* ctx);
/**
* @method darray_find_ex
*
* @param {darray_t*} darray
* @param {tk_compare_t} cmp NULL则使用内置的比较函数
* @param {void*} ctx
*
* @return {void*} NULL
*/
void* darray_find_ex(darray_t* darray, tk_compare_t cmp, void* ctx);
/**
* @method darray_bsearch_index
* ()
@ -185,6 +196,17 @@ ret_t darray_replace(darray_t* darray, uint32_t index, void* data);
*/
int32_t darray_find_index(darray_t* darray, void* ctx);
/**
* @method darray_find_index_ex
*
* @param {darray_t*} darray
* @param {tk_compare_t} cmp NULL则使用内置的比较函数
* @param {void*} ctx
*
* @return {int32_t} -1
*/
int32_t darray_find_index_ex(darray_t* darray, tk_compare_t cmp, void* ctx);
/**
* @method darray_remove
*
@ -195,6 +217,17 @@ int32_t darray_find_index(darray_t* darray, void* ctx);
*/
ret_t darray_remove(darray_t* darray, void* ctx);
/**
* @method darray_remove_ex
*
* @param {darray_t*} darray
* @param {tk_compare_t} cmp NULL则使用内置的比较函数
* @param {void*} ctx
*
* @return {ret_t} RET_OK表示成功
*/
ret_t darray_remove_ex(darray_t* darray, tk_compare_t cmp, void* ctx);
/**
* @method darray_remove_index
*