improve hash maps

This commit is contained in:
2025-05-03 18:01:50 +02:00
parent ad21ec0fa4
commit 8e880f59be
6 changed files with 88 additions and 9 deletions

View File

@@ -42,14 +42,17 @@ int main(void) {
return 1;
}
/* Add a new key-value */
ret = cws_hm_set(str_hashmap, (void *)key, (void *)value);
if (!ret) {
CWS_LOG_WARNING("Unable to set %s:%s", key, value);
}
/* Get the added key-value */
cws_bucket *bucket = cws_hm_get(str_hashmap, key);
CWS_LOG_DEBUG("Set %s:%s", (char *)bucket->key, (char *)bucket->value);
/* Update the value */
char *another_value = strdup("another value1");
ret = cws_hm_set(str_hashmap, (void *)key, (void *)another_value);
if (!ret) {
@@ -59,6 +62,17 @@ int main(void) {
bucket = cws_hm_get(str_hashmap, key);
CWS_LOG_DEBUG("Set %s:%s", (char *)bucket->key, (char *)bucket->value);
/* Remove the key-value */
ret = cws_hm_remove(str_hashmap, (void *)key);
if (ret) {
CWS_LOG_DEBUG("test1 removed");
}
/* Can't use key, it has been freed */
bucket = cws_hm_get(str_hashmap, (void *)"test1");
if (bucket == NULL) {
CWS_LOG_DEBUG("test1 not found");
}
cws_hm_free(str_hashmap);
return 0;