Thu, 15 Oct 2015 12:34:10 +0200
added ucx_map_clean()
test/main.c | file | annotate | diff | comparison | revisions | |
test/map_tests.c | file | annotate | diff | comparison | revisions | |
test/map_tests.h | file | annotate | diff | comparison | revisions | |
ucx/map.c | file | annotate | diff | comparison | revisions | |
ucx/map.h | file | annotate | diff | comparison | revisions |
1.1 --- a/test/main.c Tue May 19 17:01:28 2015 +0200 1.2 +++ b/test/main.c Thu Oct 15 12:34:10 2015 +0200 1.3 @@ -173,6 +173,7 @@ 1.4 ucx_test_register(suite, test_ucx_map_put); 1.5 ucx_test_register(suite, test_ucx_map_get); 1.6 ucx_test_register(suite, test_ucx_map_remove); 1.7 + ucx_test_register(suite, test_ucx_map_clear); 1.8 ucx_test_register(suite, test_ucx_map_iterator); 1.9 ucx_test_register(suite, test_ucx_map_iterator_chain); 1.10 ucx_test_register(suite, test_ucx_map_clone);
2.1 --- a/test/map_tests.c Tue May 19 17:01:28 2015 +0200 2.2 +++ b/test/map_tests.c Thu Oct 15 12:34:10 2015 +0200 2.3 @@ -163,6 +163,37 @@ 2.4 ucx_map_free(map); 2.5 } 2.6 2.7 +UCX_TEST(test_ucx_map_clear) { 2.8 + UcxMap *map = ucx_map_new(4); 2.9 + 2.10 + int value = 42; 2.11 + 2.12 + ucx_map_cstr_put(map, "Key0", &value); 2.13 + ucx_map_cstr_put(map, "Key1", &value); 2.14 + ucx_map_cstr_put(map, "Key2", &value); 2.15 + ucx_map_cstr_put(map, "Key3", &value); 2.16 + ucx_map_cstr_put(map, "Key4", &value); 2.17 + ucx_map_cstr_put(map, "Key5", &value); 2.18 + ucx_map_cstr_put(map, "Key6", &value); 2.19 + UCX_TEST_BEGIN 2.20 + 2.21 + ucx_map_clear(map); 2.22 + 2.23 + UCX_TEST_ASSERT(map->count == 0, "map has not been cleared"); 2.24 + UCX_TEST_ASSERT(map->size == 4, "map size has changed unexpectedly"); 2.25 + 2.26 + UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key0")==NULL, "element not removed"); 2.27 + UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key1")==NULL, "element not removed"); 2.28 + UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key2")==NULL, "element not removed"); 2.29 + UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key3")==NULL, "element not removed"); 2.30 + UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key4")==NULL, "element not removed"); 2.31 + UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key5")==NULL, "element not removed"); 2.32 + UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key6")==NULL, "element not removed"); 2.33 + 2.34 + UCX_TEST_END 2.35 + ucx_map_free(map); 2.36 +} 2.37 + 2.38 UCX_TEST_SUBROUTINE(test_ucx_map_itersrt, UcxMap *map) { 2.39 int v1 = 10; 2.40 int v2 = 15;
3.1 --- a/test/map_tests.h Tue May 19 17:01:28 2015 +0200 3.2 +++ b/test/map_tests.h Thu Oct 15 12:34:10 2015 +0200 3.3 @@ -41,6 +41,7 @@ 3.4 UCX_TEST(test_ucx_map_put); 3.5 UCX_TEST(test_ucx_map_get); 3.6 UCX_TEST(test_ucx_map_remove); 3.7 +UCX_TEST(test_ucx_map_clear); 3.8 UCX_TEST(test_ucx_map_iterator); 3.9 UCX_TEST(test_ucx_map_iterator_chain); 3.10 UCX_TEST(test_ucx_map_clone);
4.1 --- a/ucx/map.c Tue May 19 17:01:28 2015 +0200 4.2 +++ b/ucx/map.c Thu Oct 15 12:34:10 2015 +0200 4.3 @@ -62,7 +62,7 @@ 4.4 return map; 4.5 } 4.6 4.7 -static void ucx_map_free_elmlist(UcxMap *map) { 4.8 +static void ucx_map_free_elmlist_contents(UcxMap *map) { 4.9 for (size_t n = 0 ; n < map->size ; n++) { 4.10 UcxMapElement *elem = map->map[n]; 4.11 if (elem != NULL) { 4.12 @@ -74,14 +74,20 @@ 4.13 } while (elem != NULL); 4.14 } 4.15 } 4.16 - alfree(map->allocator, map->map); 4.17 } 4.18 4.19 void ucx_map_free(UcxMap *map) { 4.20 - ucx_map_free_elmlist(map); 4.21 + ucx_map_free_elmlist_contents(map); 4.22 + alfree(map->allocator, map->map); 4.23 alfree(map->allocator, map); 4.24 } 4.25 4.26 +void ucx_map_clear(UcxMap *map) { 4.27 + ucx_map_free_elmlist_contents(map); 4.28 + memset(map->map, 0, map->size*sizeof(UcxMapElement*)); 4.29 + map->count = 0; 4.30 +} 4.31 + 4.32 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to, 4.33 copy_func fnc, void *data) { 4.34 UcxMapIterator i = ucx_map_iterator(from); 4.35 @@ -124,7 +130,8 @@ 4.36 ucx_map_copy(&oldmap, map, NULL, NULL); 4.37 4.38 /* free the UcxMapElement list of oldmap */ 4.39 - ucx_map_free_elmlist(&oldmap); 4.40 + ucx_map_free_elmlist_contents(&oldmap); 4.41 + alfree(map->allocator, oldmap.map); 4.42 } 4.43 return 0; 4.44 }
5.1 --- a/ucx/map.h Tue May 19 17:01:28 2015 +0200 5.2 +++ b/ucx/map.h Thu Oct 15 12:34:10 2015 +0200 5.3 @@ -154,6 +154,15 @@ 5.4 void ucx_map_free(UcxMap *map); 5.5 5.6 /** 5.7 + * Clears a hash map. 5.8 + * 5.9 + * <b>Note:</b> the contents are <b>not</b> freed. 5.10 + * 5.11 + * @param map the map to be freed 5.12 + */ 5.13 +void ucx_map_clear(UcxMap *map); 5.14 + 5.15 +/** 5.16 * Copies contents from a map to another map using a copy function. 5.17 * 5.18 * <b>Note:</b> The destination map does not need to be empty. However, if it