utils: Add hashmap_count()

Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
This commit is contained in:
Daniel Silverstone 2020-02-23 20:18:08 +00:00
parent 3e02961ec8
commit 088917641f
No known key found for this signature in database
GPG Key ID: C30DF439F2987D74
3 changed files with 34 additions and 4 deletions

View File

@ -197,7 +197,7 @@ basic_fixture_teardown(void)
START_TEST(empty_hashmap_create_destroy)
{
/* Do nothing in here, all the checks are in the fixture */
ck_assert_int_eq(hashmap_count(test_hashmap), 0);
}
END_TEST
@ -213,6 +213,7 @@ START_TEST(insert_works)
hashmap_test_value_t *value = hashmap_insert(test_hashmap, corestring_nsurl_about_blank);
ck_assert(value != NULL);
ck_assert(value->key == corestring_nsurl_about_blank);
ck_assert_int_eq(hashmap_count(test_hashmap), 1);
}
END_TEST
@ -229,9 +230,11 @@ START_TEST(insert_then_remove)
ck_assert(value->key == corestring_nsurl_about_blank);
ck_assert_int_eq(keys, 1);
ck_assert_int_eq(values, 1);
ck_assert_int_eq(hashmap_count(test_hashmap), 1);
ck_assert(hashmap_remove(test_hashmap, corestring_nsurl_about_blank) == true);
ck_assert_int_eq(keys, 0);
ck_assert_int_eq(values, 0);
ck_assert_int_eq(hashmap_count(test_hashmap), 0);
}
END_TEST
@ -450,6 +453,7 @@ START_TEST(chain_add_all_twice_remove_all_iterate)
iteration_stop = 0;
ck_assert(hashmap_iterate(test_hashmap, hashmap_test_iterator_cb, &iteration_ctx) == false);
ck_assert_int_eq(iteration_counter, chain_count);
ck_assert_int_eq(hashmap_count(test_hashmap), chain_count);
iteration_counter = 0;
iteration_stop = chain_count;
@ -469,6 +473,7 @@ START_TEST(chain_add_all_twice_remove_all_iterate)
ck_assert_int_eq(keys, 0);
ck_assert_int_eq(values, 0);
ck_assert_int_eq(hashmap_count(test_hashmap), 0);
}
END_TEST

View File

@ -55,6 +55,11 @@ struct hashmap_s {
* The number of buckets in this map
*/
uint32_t bucket_count;
/**
* The number of entries in this map
*/
size_t entry_count;
};
/* Exported function, documented in hashmap.h */
@ -65,14 +70,16 @@ hashmap_create(hashmap_parameters_t *params)
ret->params = params;
ret->bucket_count = DEFAULT_HASHMAP_BUCKETS;
ret->entry_count = 0;
ret->buckets = malloc(ret->bucket_count * sizeof(hashmap_entry_t *));
memset(ret->buckets, 0, ret->bucket_count * sizeof(hashmap_entry_t *));
if (ret->buckets == NULL) {
free(ret);
return NULL;
}
memset(ret->buckets, 0, ret->bucket_count * sizeof(hashmap_entry_t *));
return ret;
}
@ -176,7 +183,9 @@ hashmap_insert(hashmap_t *hashmap, void *key)
}
hashmap->buckets[bucket] = entry;
hashmap->entry_count++;
return entry->value;
err:
@ -207,6 +216,7 @@ hashmap_remove(hashmap_t *hashmap, void *key)
}
*entry->prevptr = entry->next;
free(entry);
hashmap->entry_count--;
return true;
}
}
@ -233,3 +243,10 @@ hashmap_iterate(hashmap_t *hashmap, hashmap_iteration_cb_t cb, void *ctx)
return false;
}
/* Exported function, documented in hashmap.h */
size_t
hashmap_count(hashmap_t *hashmap)
{
return hashmap->entry_count;
}

View File

@ -186,4 +186,12 @@ bool hashmap_remove(hashmap_t *hashmap, void *key);
*/
bool hashmap_iterate(hashmap_t *hashmap, hashmap_iteration_cb_t cb, void *ctx);
/**
* Get the number of entries in this map
*
* \param hashmap The hashmap to retrieve the entry count from
* \return The number of entries in the hashmap
*/
size_t hashmap_count(hashmap_t *hashmap);
#endif