added ucx_map_clean()

Thu, 15 Oct 2015 12:34:10 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 15 Oct 2015 12:34:10 +0200
changeset 206
58b77eb51afd
parent 205
54a7ceb9151f
child 207
1de85ecf6adc

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
--- 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);
--- 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;
--- 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);
--- 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;
 }
--- 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.
+ * 
+ * <b>Note:</b> the contents are <b>not</b> 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.
  * 
  * <b>Note:</b> The destination map does not need to be empty. However, if it

mercurial