add hash_table_size

This commit is contained in:
lixianjing 2023-09-24 09:53:18 +08:00
parent 84cacc25f1
commit c95df6bb6a
4 changed files with 35 additions and 5 deletions

View File

@ -1,12 +1,13 @@
# 最新动态
2023/09/24
* 增加tk\_iostream\_tcp\_create\_client
* 增加函数tk\_iostream\_tcp\_create\_client
* 增加函数hash\_table\_size
2023/09/23
* 完善socket_helper
* 完善serial_helper
* 将serial_helper.c/.h移动到tkc目录。
* 完善socket\_helper
* 完善serial\_helper
* 将serial\_helper.c/.h移动到tkc目录。
2023/09/21
* 修改scroll\_view在children\_layout中设置ym垂直间距时底部没有间距的bug并在demouiold添加演示(感谢颖健提供补丁)

View File

@ -112,6 +112,24 @@ ret_t hash_table_remove_all(hash_table_t* hash_table, tk_compare_t cmp, void* ct
return RET_OK;
}
int32_t hash_table_size(hash_table_t* hash_table) {
uint32_t i = 0;
uint32_t n = 0;
int32_t count = 0;
darray_t** arrs = NULL;
return_value_if_fail(hash_table != NULL, 0);
n = hash_table->buckets.size;
arrs = (darray_t**)(hash_table->buckets.elms);
for (i = 0; i < n; i++) {
darray_t* iter = arrs[i];
count += iter->size;
}
return count;
}
int32_t hash_table_count(hash_table_t* hash_table, tk_compare_t cmp, void* ctx) {
uint32_t i = 0;
uint32_t n = 0;

View File

@ -150,10 +150,19 @@ ret_t hash_table_remove(hash_table_t* hash_table, tk_compare_t cmp, void* ctx);
*/
ret_t hash_table_remove_all(hash_table_t* hash_table, tk_compare_t cmp, void* ctx);
/**
* @method hash_table_size
*
* @param {hash_table_t*} hash_table
*
* @return {int32_t}
*/
int32_t hash_table_size(hash_table_t* hash_table);
/**
* @method hash_table_count
*
* @param {hash_table_t*} hash_table
* @param {hash_table_t*} hash_table
* @param {tk_compare_t} cmp NULL则使用内置的比较函数
* @param {void*} ctx
*

View File

@ -14,12 +14,14 @@ TEST(DHashTable, int_add_remove) {
for (i = 0; i < n; i++) {
ASSERT_EQ(hash_table_add(ht, tk_pointer_from_int(i), TRUE), RET_OK);
ASSERT_EQ(hash_table_count(ht, compare_always_equal, NULL), (i + 1));
ASSERT_EQ(hash_table_size(ht), (i + 1));
ASSERT_EQ(tk_pointer_to_int(hash_table_find(ht, int_compare, tk_pointer_from_int(i))), i);
}
for (i = 0; i < n; i++) {
ASSERT_EQ(hash_table_remove(ht, int_compare, tk_pointer_from_int(i)), RET_OK);
ASSERT_EQ(hash_table_count(ht, compare_always_equal, NULL), n - i - 1);
ASSERT_EQ(hash_table_size(ht), n - i - 1);
ASSERT_EQ(tk_pointer_to_int(hash_table_find(ht, int_compare, tk_pointer_from_int(i))), 0);
}