add value_min/value_max

This commit is contained in:
lixianjing 2022-11-07 17:31:41 +08:00
parent 074104c7c3
commit 2e9611ed52
6 changed files with 393 additions and 50 deletions

View File

@ -1,5 +1,8 @@
# 最新动态
2022/11/07
* 增加函数value\_min/value\_max。
2022/11/03
* 导出conf\_node\_count\_children函数(感谢智明提供补丁)
* 修复纯黑做半透明的融合时候颜色不对的问题(感谢智明提供补丁)

View File

@ -732,7 +732,7 @@ ret_t tk_str_append(char* str, uint32_t max_len, const char* s) {
return RET_OK;
}
int32_t tk_str_cmp(const char* a, const char* b) {
int32_t tk_strcmp(const char* a, const char* b) {
if (a == b) {
return 0;
}
@ -748,7 +748,7 @@ int32_t tk_str_cmp(const char* a, const char* b) {
return strcmp(a, b);
}
int32_t tk_str_icmp(const char* a, const char* b) {
int32_t tk_stricmp(const char* a, const char* b) {
if (a == b) {
return 0;
}
@ -764,6 +764,38 @@ int32_t tk_str_icmp(const char* a, const char* b) {
return strcasecmp(a, b);
}
int32_t tk_wstrcmp(const wchar_t* a, const wchar_t* b) {
if (a == b) {
return 0;
}
if (a == NULL) {
return -1;
}
if (b == NULL) {
return 1;
}
return wcscmp(a, b);
}
int32_t tk_wstricmp(const wchar_t* a, const wchar_t* b) {
if (a == b) {
return 0;
}
if (a == NULL) {
return -1;
}
if (b == NULL) {
return 1;
}
return wcscasecmp(a, b);
}
char* tk_str_copy(char* dst, const char* src) {
if (src != NULL) {
uint32_t size = strlen(src) + 1;
@ -1527,10 +1559,11 @@ bool_t tk_is_ui_thread(void) {
return s_ui_thread_id == tk_thread_self();
}
uint32_t tk_strnlen(const char *str, uint32_t maxlen) {
uint32_t tk_strnlen(const char* str, uint32_t maxlen) {
const char* s;
return_value_if_fail(str != NULL, 0);
for (s = str; maxlen-- && *s != '\0'; ++s);
for (s = str; maxlen-- && *s != '\0'; ++s)
;
return s - str;
}

View File

@ -91,7 +91,7 @@ bool_t tk_atob(const char* str);
double tk_atof(const char* str);
/**
* @method tk_str_cmp
* @method tk_strcmp
*
*
*
@ -100,10 +100,10 @@ double tk_atof(const char* str);
*
* @return {int32_t} =-1a为NULL=1b为NULL<0a小于b>0a大于b=0a等于b
*/
int32_t tk_str_cmp(const char* a, const char* b);
int32_t tk_strcmp(const char* a, const char* b);
/**
* @method tk_str_icmp
* @method tk_stricmp
*
*
*
@ -112,7 +112,31 @@ int32_t tk_str_cmp(const char* a, const char* b);
*
* @return {int32_t} =-1a为NULL=1b为NULL<0a小于b>0a大于b=0a等于b
*/
int32_t tk_str_icmp(const char* a, const char* b);
int32_t tk_stricmp(const char* a, const char* b);
/**
* @method tk_wstrcmp
*
*
*
* @param {const wchar_t*} a
* @param {const wchar_t*} b
*
* @return {int32_t} =-1a为NULL=1b为NULL<0a小于b>0a大于b=0a等于b
*/
int32_t tk_wstrcmp(const wchar_t* a, const wchar_t* b);
/**
* @method tk_wstricmp
*
*
*
* @param {const wchar_t*} a
* @param {const wchar_t*} b
*
* @return {int32_t} =-1a为NULL=1b为NULL<0a小于b>0a大于b=0a等于b
*/
int32_t tk_wstricmp(const wchar_t* a, const wchar_t* b);
/**
* @method tk_watoi
@ -344,7 +368,7 @@ uint32_t tk_strlen(const char* str);
*
* @return {uint32_t}
*/
uint32_t tk_strnlen(const char *str, uint32_t maxlen);
uint32_t tk_strnlen(const char* str, uint32_t maxlen);
/**
* @method tk_strrstr
@ -906,6 +930,9 @@ ret_t xml_file_expand(const char* filename, str_t* s, const char* data);
#define TK_STRDUP(str) ((str) != NULL) ? strdup(str) : NULL
#define TK_STRNDUP(str) ((str) != NULL) ? strndup(str) : NULL
#define tk_str_cmp tk_strcmp
#define tk_str_icmp tk_stricmp
END_C_DECLS
#endif /*TK_UTILS_H*/

View File

@ -2186,3 +2186,107 @@ ret_t value_expt(value_t* v, value_t* other, value_t* result) {
return RET_OK;
}
ret_t value_min(value_t* arr, uint32_t size, value_t* result) {
uint32_t i = 0;
uint32_t r = 0;
uint32_t type = 0;
return_value_if_fail(arr != NULL && size > 0 && result != NULL, RET_BAD_PARAMS);
type = arr[0].type;
if (type >= VALUE_TYPE_INT8 && type < VALUE_TYPE_UINT64) {
for (i = 0; i < size; i++) {
if (value_int64(arr + i) < value_int64(arr + r)) {
r = i;
}
}
return value_copy(result, arr + r);
} else if (type == VALUE_TYPE_UINT64) {
for (i = 0; i < size; i++) {
if (value_uint64(arr + i) < value_uint64(arr + r)) {
r = i;
}
}
return value_copy(result, arr + r);
} else if (type == VALUE_TYPE_STRING) {
for (i = 0; i < size; i++) {
if (tk_strcmp(value_str(arr + i), value_str(arr + r)) < 0) {
r = i;
}
}
return value_copy(result, arr + r);
} else if (type == VALUE_TYPE_WSTRING) {
for (i = 0; i < size; i++) {
if (tk_wstrcmp(value_wstr(arr + i), value_wstr(arr + r)) < 0) {
r = i;
}
}
return value_copy(result, arr + r);
} else if (type >= VALUE_TYPE_FLOAT && type <= VALUE_TYPE_DOUBLE) {
for (i = 0; i < size; i++) {
if (value_double(arr + i) < value_double(arr + r)) {
r = i;
}
}
return value_copy(result, arr + r);
} else {
log_warn("not impl for this type");
return RET_NOT_IMPL;
}
return RET_OK;
}
ret_t value_max(value_t* arr, uint32_t size, value_t* result) {
uint32_t i = 0;
uint32_t r = 0;
uint32_t type = 0;
return_value_if_fail(arr != NULL && size > 0 && result != NULL, RET_BAD_PARAMS);
type = arr[0].type;
if (type >= VALUE_TYPE_INT8 && type < VALUE_TYPE_UINT64) {
for (i = 0; i < size; i++) {
if (value_int64(arr + i) > value_int64(arr + r)) {
r = i;
}
}
return value_copy(result, arr + r);
} else if (type == VALUE_TYPE_UINT64) {
for (i = 0; i < size; i++) {
if (value_uint64(arr + i) > value_uint64(arr + r)) {
r = i;
}
}
return value_copy(result, arr + r);
} else if (type == VALUE_TYPE_STRING) {
for (i = 0; i < size; i++) {
if (tk_strcmp(value_str(arr + i), value_str(arr + r)) > 0) {
r = i;
}
}
return value_copy(result, arr + r);
} else if (type == VALUE_TYPE_WSTRING) {
for (i = 0; i < size; i++) {
if (tk_wstrcmp(value_wstr(arr + i), value_wstr(arr + r)) > 0) {
r = i;
}
}
return value_copy(result, arr + r);
} else if (type >= VALUE_TYPE_FLOAT && type <= VALUE_TYPE_DOUBLE) {
for (i = 0; i < size; i++) {
if (value_double(arr + i) > value_double(arr + r)) {
r = i;
}
}
return value_copy(result, arr + r);
} else {
log_warn("not impl for this type");
return RET_NOT_IMPL;
}
return RET_OK;
}

View File

@ -1123,6 +1123,28 @@ ret_t value_expt(value_t* v, value_t* other, value_t* result);
*/
ret_t value_mod(value_t* v, value_t* other, value_t* result);
/**
* @method value_min
* result对象
* @param {value_t*} arr
* @param {uint32_t} size
* @param {value_t*} result value对象
*
* @return {ret_t} RET_OK表示成功
*/
ret_t value_min(value_t* arr, uint32_t size, value_t* result);
/**
* @method value_max
* result对象
* @param {value_t*} arr
* @param {uint32_t} size
* @param {value_t*} result value对象
*
* @return {ret_t} RET_OK表示成功
*/
ret_t value_max(value_t* arr, uint32_t size, value_t* result);
END_C_DECLS
#endif /*TK_VALUE_H*/

View File

@ -940,7 +940,6 @@ TEST(value, bit_and_or_xor) {
ASSERT_EQ(r.type, VALUE_TYPE_UINT8);
ASSERT_EQ(value_uint8(&r), (uint8_t)0xff);
value_set_int16(&v1, 0xf0);
value_set_int16(&v2, 0x0f);
ASSERT_EQ(value_bit_and(&v1, &v2, &r), RET_OK);
@ -1300,7 +1299,8 @@ TEST(value, add_double) {
ASSERT_EQ(value_div(&v1, &v2, &o), RET_OK);
ASSERT_EQ(o.type == v1.type, TRUE);
ASSERT_EQ(tk_fequal(value_double(&o), 4.2), TRUE);;
ASSERT_EQ(tk_fequal(value_double(&o), 4.2), TRUE);
;
}
TEST(value, mod) {
@ -1372,3 +1372,157 @@ TEST(value, expt) {
ASSERT_EQ(tk_fequal(value_double(&o), 0.001), TRUE);
ASSERT_EQ(o.type == VALUE_TYPE_DOUBLE, TRUE);
}
TEST(value, min) {
int32_t i = 0;
value_t v[10];
value_t o;
for (i = 0; i < ARRAY_SIZE(v); i++) {
value_set_int8(v + i, i * ((i % 2 == 0) ? -1 : 1));
}
ASSERT_EQ(value_min(v, ARRAY_SIZE(v), &o), RET_OK);
ASSERT_EQ(o.type, VALUE_TYPE_INT8);
ASSERT_EQ(value_int8(&o), -8);
for (i = 0; i < ARRAY_SIZE(v); i++) {
value_set_uint8(v + i, i + 1);
}
ASSERT_EQ(value_min(v, ARRAY_SIZE(v), &o), RET_OK);
ASSERT_EQ(o.type, VALUE_TYPE_UINT8);
ASSERT_EQ(value_uint8(&o), 1);
for (i = 0; i < ARRAY_SIZE(v); i++) {
value_set_int16(v + i, i * ((i % 2 == 0) ? -1 : 1));
}
ASSERT_EQ(value_min(v, ARRAY_SIZE(v), &o), RET_OK);
ASSERT_EQ(o.type, VALUE_TYPE_INT16);
ASSERT_EQ(value_int16(&o), -8);
for (i = 0; i < ARRAY_SIZE(v); i++) {
value_set_uint16(v + i, i + 1);
}
ASSERT_EQ(value_min(v, ARRAY_SIZE(v), &o), RET_OK);
ASSERT_EQ(o.type, VALUE_TYPE_UINT16);
ASSERT_EQ(value_uint16(&o), 1);
for (i = 0; i < ARRAY_SIZE(v); i++) {
value_set_int32(v + i, i * ((i % 2 == 0) ? -1 : 1));
}
ASSERT_EQ(value_min(v, ARRAY_SIZE(v), &o), RET_OK);
ASSERT_EQ(o.type, VALUE_TYPE_INT32);
ASSERT_EQ(value_int32(&o), -8);
for (i = 0; i < ARRAY_SIZE(v); i++) {
value_set_uint32(v + i, i + 1);
}
ASSERT_EQ(value_min(v, ARRAY_SIZE(v), &o), RET_OK);
ASSERT_EQ(o.type, VALUE_TYPE_UINT32);
ASSERT_EQ(value_uint32(&o), 1);
for (i = 0; i < ARRAY_SIZE(v); i++) {
value_set_int64(v + i, i * ((i % 2 == 0) ? -1 : 1));
}
ASSERT_EQ(value_min(v, ARRAY_SIZE(v), &o), RET_OK);
ASSERT_EQ(o.type, VALUE_TYPE_INT64);
ASSERT_EQ(value_int64(&o), -8);
for (i = 0; i < ARRAY_SIZE(v); i++) {
value_set_uint64(v + i, i + 1);
}
ASSERT_EQ(value_min(v, ARRAY_SIZE(v), &o), RET_OK);
ASSERT_EQ(o.type, VALUE_TYPE_UINT64);
ASSERT_EQ(value_uint64(&o), 1);
for (i = 0; i < ARRAY_SIZE(v); i++) {
value_set_double(v + i, i + 1);
}
ASSERT_EQ(value_min(v, ARRAY_SIZE(v), &o), RET_OK);
ASSERT_EQ(o.type, VALUE_TYPE_DOUBLE);
ASSERT_EQ(value_double(&o), 1);
const char* str[] = {"a", "b", "c", "0", "A", "B", "C", "1", "2", "3"};
for (i = 0; i < ARRAY_SIZE(v); i++) {
value_set_str(v + i, str[i]);
}
ASSERT_EQ(value_min(v, ARRAY_SIZE(v), &o), RET_OK);
ASSERT_EQ(o.type, VALUE_TYPE_STRING);
ASSERT_STREQ(value_str(&o), "0");
}
TEST(value, max) {
int32_t i = 0;
value_t v[10];
value_t o;
for (i = 0; i < ARRAY_SIZE(v); i++) {
value_set_int8(v + i, i * ((i % 2 == 0) ? -1 : 1));
}
ASSERT_EQ(value_max(v, ARRAY_SIZE(v), &o), RET_OK);
ASSERT_EQ(o.type, VALUE_TYPE_INT8);
ASSERT_EQ(value_int8(&o), 9);
for (i = 0; i < ARRAY_SIZE(v); i++) {
value_set_uint8(v + i, i + 1);
}
ASSERT_EQ(value_max(v, ARRAY_SIZE(v), &o), RET_OK);
ASSERT_EQ(o.type, VALUE_TYPE_UINT8);
ASSERT_EQ(value_uint8(&o), 10);
for (i = 0; i < ARRAY_SIZE(v); i++) {
value_set_int16(v + i, i * ((i % 2 == 0) ? -1 : 1));
}
ASSERT_EQ(value_max(v, ARRAY_SIZE(v), &o), RET_OK);
ASSERT_EQ(o.type, VALUE_TYPE_INT16);
ASSERT_EQ(value_int16(&o), 9);
for (i = 0; i < ARRAY_SIZE(v); i++) {
value_set_uint16(v + i, i + 1);
}
ASSERT_EQ(value_max(v, ARRAY_SIZE(v), &o), RET_OK);
ASSERT_EQ(o.type, VALUE_TYPE_UINT16);
ASSERT_EQ(value_uint16(&o), 10);
for (i = 0; i < ARRAY_SIZE(v); i++) {
value_set_int32(v + i, i * ((i % 2 == 0) ? -1 : 1));
}
ASSERT_EQ(value_max(v, ARRAY_SIZE(v), &o), RET_OK);
ASSERT_EQ(o.type, VALUE_TYPE_INT32);
ASSERT_EQ(value_int32(&o), 9);
for (i = 0; i < ARRAY_SIZE(v); i++) {
value_set_uint32(v + i, i + 1);
}
ASSERT_EQ(value_max(v, ARRAY_SIZE(v), &o), RET_OK);
ASSERT_EQ(o.type, VALUE_TYPE_UINT32);
ASSERT_EQ(value_uint32(&o), 10);
for (i = 0; i < ARRAY_SIZE(v); i++) {
value_set_int64(v + i, i * ((i % 2 == 0) ? -1 : 1));
}
ASSERT_EQ(value_max(v, ARRAY_SIZE(v), &o), RET_OK);
ASSERT_EQ(o.type, VALUE_TYPE_INT64);
ASSERT_EQ(value_int64(&o), 9);
for (i = 0; i < ARRAY_SIZE(v); i++) {
value_set_uint64(v + i, i + 1);
}
ASSERT_EQ(value_max(v, ARRAY_SIZE(v), &o), RET_OK);
ASSERT_EQ(o.type, VALUE_TYPE_UINT64);
ASSERT_EQ(value_uint64(&o), 10);
for (i = 0; i < ARRAY_SIZE(v); i++) {
value_set_double(v + i, i + 1);
}
ASSERT_EQ(value_max(v, ARRAY_SIZE(v), &o), RET_OK);
ASSERT_EQ(o.type, VALUE_TYPE_DOUBLE);
ASSERT_EQ(value_double(&o), 10);
const char* str[] = {"a", "b", "c", "0", "A", "B", "C", "1", "2", "3"};
for (i = 0; i < ARRAY_SIZE(v); i++) {
value_set_str(v + i, str[i]);
}
ASSERT_EQ(value_max(v, ARRAY_SIZE(v), &o), RET_OK);
ASSERT_EQ(o.type, VALUE_TYPE_STRING);
ASSERT_STREQ(value_str(&o), "c");
}