added map clone

Thu, 04 Oct 2012 18:46:57 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 04 Oct 2012 18:46:57 +0200
changeset 44
46356d74e873
parent 43
02f38adea013
child 45
dd03226c1a6b

added map clone

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	Thu Oct 04 18:23:32 2012 +0200
+++ b/test/main.c	Thu Oct 04 18:46:57 2012 +0200
@@ -148,6 +148,7 @@
         ucx_test_register(suite, test_ucx_map_iterator);
         ucx_test_register(suite, test_ucx_map_iterator_chain);
         ucx_test_register(suite, test_ucx_map_store_load);
+        ucx_test_register(suite, test_ucx_map_clone);
         
         /* sstring Tests */
         ucx_test_register(suite, test_sstrsplit);
--- a/test/map_tests.c	Thu Oct 04 18:23:32 2012 +0200
+++ b/test/map_tests.c	Thu Oct 04 18:46:57 2012 +0200
@@ -4,6 +4,10 @@
 
 #include "map_tests.h"
 
+#ifndef _WIN32
+#include <unistd.h>
+#endif /* not _WIN32 */
+
 UCX_TEST_IMPLEMENT(test_ucx_map_new) {
     UcxMap *map = ucx_map_new(16);
     UCX_TEST_BEGIN
@@ -194,3 +198,40 @@
 
     unlink("test_ucx_map_store");
 }
+
+UCX_TEST_IMPLEMENT(test_ucx_map_clone) {
+    UcxMap *map = ucx_map_new(4);
+    
+    ucx_map_cstr_put(map, "key1", "value1");
+    ucx_map_cstr_put(map, "key2", "value2");
+    ucx_map_cstr_put(map, "key3", "value3");
+    
+    UcxMap *clone = ucx_map_clone(map, NULL, NULL);
+    
+    char *v1 = ucx_map_cstr_get(map, "key1");
+    char *v2 = ucx_map_cstr_get(map, "key2");
+    char *v3 = ucx_map_cstr_get(map, "key3");
+    
+    UCX_TEST_BEGIN
+    
+    UCX_TEST_ASSERT(v1 != NULL, "failed key 1");
+    UCX_TEST_ASSERT(v2 != NULL, "failed key 2");
+    UCX_TEST_ASSERT(v3 != NULL, "failed key 3");
+    
+    char *c1 = ucx_map_cstr_get(clone, "key1");
+    char *c2 = ucx_map_cstr_get(clone, "key2");
+    char *c3 = ucx_map_cstr_get(clone, "key3");
+    
+    UCX_TEST_ASSERT(c1 != NULL, "failed key 1 (clone)");
+    UCX_TEST_ASSERT(c2 != NULL, "failed key 2 (clone)");
+    UCX_TEST_ASSERT(c3 != NULL, "failed key 3 (clone)");
+    
+    UCX_TEST_ASSERT(strcmp(c1, v1) == 0, "value error for key1");
+    UCX_TEST_ASSERT(strcmp(c2, v2) == 0, "value error for key2");
+    UCX_TEST_ASSERT(strcmp(c3, v3) == 0, "value error for key3");
+    
+    UCX_TEST_END
+    
+    ucx_map_free(map);
+    ucx_map_free(clone);
+}
--- a/test/map_tests.h	Thu Oct 04 18:23:32 2012 +0200
+++ b/test/map_tests.h	Thu Oct 04 18:46:57 2012 +0200
@@ -19,6 +19,7 @@
 UCX_TEST_DECLARE(test_ucx_map_iterator)
 UCX_TEST_DECLARE(test_ucx_map_iterator_chain)
 UCX_TEST_DECLARE(test_ucx_map_store_load)
+UCX_TEST_DECLARE(test_ucx_map_clone)
 
 
 #ifdef	__cplusplus
--- a/ucx/map.c	Thu Oct 04 18:23:32 2012 +0200
+++ b/ucx/map.c	Thu Oct 04 18:46:57 2012 +0200
@@ -4,9 +4,6 @@
 
 #include <stdlib.h>
 #include <string.h>
-#ifndef _WIN32
-#include <unistd.h>
-#endif /* not _WIN32 */
 
 #include "map.h"
 
@@ -42,6 +39,16 @@
     free(map);
 }
 
+UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data) {
+    UcxMap *newmap = ucx_map_new(map->size);
+    UcxMapIterator i = ucx_map_iterator(map);
+    void *value;
+    UCX_MAP_FOREACH(value, i) {
+        ucx_map_put(newmap, i.cur->key, fnc ? fnc(value, data) : value);
+    }
+    return newmap;
+}
+
 int ucx_map_put(UcxMap *map, UcxKey key, void *data) {
     if(key.hash == 0) {
         key.hash = ucx_hash((char*)key.data, key.len);
--- a/ucx/map.h	Thu Oct 04 18:23:32 2012 +0200
+++ b/ucx/map.h	Thu Oct 04 18:46:57 2012 +0200
@@ -47,6 +47,7 @@
 
 UcxMap *ucx_map_new(size_t size);
 void ucx_map_free(UcxMap *map);
+UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data);
 
 int ucx_map_put(UcxMap *map, UcxKey key, void *data);
 void* ucx_map_get(UcxMap *map, UcxKey key);

mercurial