# HG changeset patch # User Mike Becker # Date 1444905250 -7200 # Node ID 58b77eb51afd13beb01d603cb47f714c09cfabc6 # Parent 54a7ceb9151f38b451f6985b2a204eb592bf3c07 added ucx_map_clean() diff -r 54a7ceb9151f -r 58b77eb51afd test/main.c --- a/test/main.c Tue May 19 17:01:28 2015 +0200 +++ b/test/main.c Thu Oct 15 12:34:10 2015 +0200 @@ -173,6 +173,7 @@ ucx_test_register(suite, test_ucx_map_put); ucx_test_register(suite, test_ucx_map_get); ucx_test_register(suite, test_ucx_map_remove); + ucx_test_register(suite, test_ucx_map_clear); ucx_test_register(suite, test_ucx_map_iterator); ucx_test_register(suite, test_ucx_map_iterator_chain); ucx_test_register(suite, test_ucx_map_clone); diff -r 54a7ceb9151f -r 58b77eb51afd test/map_tests.c --- a/test/map_tests.c Tue May 19 17:01:28 2015 +0200 +++ b/test/map_tests.c Thu Oct 15 12:34:10 2015 +0200 @@ -163,6 +163,37 @@ ucx_map_free(map); } +UCX_TEST(test_ucx_map_clear) { + UcxMap *map = ucx_map_new(4); + + int value = 42; + + ucx_map_cstr_put(map, "Key0", &value); + ucx_map_cstr_put(map, "Key1", &value); + ucx_map_cstr_put(map, "Key2", &value); + ucx_map_cstr_put(map, "Key3", &value); + ucx_map_cstr_put(map, "Key4", &value); + ucx_map_cstr_put(map, "Key5", &value); + ucx_map_cstr_put(map, "Key6", &value); + UCX_TEST_BEGIN + + ucx_map_clear(map); + + UCX_TEST_ASSERT(map->count == 0, "map has not been cleared"); + UCX_TEST_ASSERT(map->size == 4, "map size has changed unexpectedly"); + + UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key0")==NULL, "element not removed"); + UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key1")==NULL, "element not removed"); + UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key2")==NULL, "element not removed"); + UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key3")==NULL, "element not removed"); + UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key4")==NULL, "element not removed"); + UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key5")==NULL, "element not removed"); + UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key6")==NULL, "element not removed"); + + UCX_TEST_END + ucx_map_free(map); +} + UCX_TEST_SUBROUTINE(test_ucx_map_itersrt, UcxMap *map) { int v1 = 10; int v2 = 15; diff -r 54a7ceb9151f -r 58b77eb51afd test/map_tests.h --- a/test/map_tests.h Tue May 19 17:01:28 2015 +0200 +++ b/test/map_tests.h Thu Oct 15 12:34:10 2015 +0200 @@ -41,6 +41,7 @@ UCX_TEST(test_ucx_map_put); UCX_TEST(test_ucx_map_get); UCX_TEST(test_ucx_map_remove); +UCX_TEST(test_ucx_map_clear); UCX_TEST(test_ucx_map_iterator); UCX_TEST(test_ucx_map_iterator_chain); UCX_TEST(test_ucx_map_clone); diff -r 54a7ceb9151f -r 58b77eb51afd ucx/map.c --- a/ucx/map.c Tue May 19 17:01:28 2015 +0200 +++ b/ucx/map.c Thu Oct 15 12:34:10 2015 +0200 @@ -62,7 +62,7 @@ return map; } -static void ucx_map_free_elmlist(UcxMap *map) { +static void ucx_map_free_elmlist_contents(UcxMap *map) { for (size_t n = 0 ; n < map->size ; n++) { UcxMapElement *elem = map->map[n]; if (elem != NULL) { @@ -74,14 +74,20 @@ } while (elem != NULL); } } - alfree(map->allocator, map->map); } void ucx_map_free(UcxMap *map) { - ucx_map_free_elmlist(map); + ucx_map_free_elmlist_contents(map); + alfree(map->allocator, map->map); alfree(map->allocator, map); } +void ucx_map_clear(UcxMap *map) { + ucx_map_free_elmlist_contents(map); + memset(map->map, 0, map->size*sizeof(UcxMapElement*)); + map->count = 0; +} + int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to, copy_func fnc, void *data) { UcxMapIterator i = ucx_map_iterator(from); @@ -124,7 +130,8 @@ ucx_map_copy(&oldmap, map, NULL, NULL); /* free the UcxMapElement list of oldmap */ - ucx_map_free_elmlist(&oldmap); + ucx_map_free_elmlist_contents(&oldmap); + alfree(map->allocator, oldmap.map); } return 0; } diff -r 54a7ceb9151f -r 58b77eb51afd ucx/map.h --- a/ucx/map.h Tue May 19 17:01:28 2015 +0200 +++ b/ucx/map.h Thu Oct 15 12:34:10 2015 +0200 @@ -154,6 +154,15 @@ void ucx_map_free(UcxMap *map); /** + * Clears a hash map. + * + * Note: the contents are not freed. + * + * @param map the map to be freed + */ +void ucx_map_clear(UcxMap *map); + +/** * Copies contents from a map to another map using a copy function. * * Note: The destination map does not need to be empty. However, if it