Thu, 04 Oct 2012 18:46:57 +0200
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 |
1.1 --- a/test/main.c Thu Oct 04 18:23:32 2012 +0200 1.2 +++ b/test/main.c Thu Oct 04 18:46:57 2012 +0200 1.3 @@ -148,6 +148,7 @@ 1.4 ucx_test_register(suite, test_ucx_map_iterator); 1.5 ucx_test_register(suite, test_ucx_map_iterator_chain); 1.6 ucx_test_register(suite, test_ucx_map_store_load); 1.7 + ucx_test_register(suite, test_ucx_map_clone); 1.8 1.9 /* sstring Tests */ 1.10 ucx_test_register(suite, test_sstrsplit);
2.1 --- a/test/map_tests.c Thu Oct 04 18:23:32 2012 +0200 2.2 +++ b/test/map_tests.c Thu Oct 04 18:46:57 2012 +0200 2.3 @@ -4,6 +4,10 @@ 2.4 2.5 #include "map_tests.h" 2.6 2.7 +#ifndef _WIN32 2.8 +#include <unistd.h> 2.9 +#endif /* not _WIN32 */ 2.10 + 2.11 UCX_TEST_IMPLEMENT(test_ucx_map_new) { 2.12 UcxMap *map = ucx_map_new(16); 2.13 UCX_TEST_BEGIN 2.14 @@ -194,3 +198,40 @@ 2.15 2.16 unlink("test_ucx_map_store"); 2.17 } 2.18 + 2.19 +UCX_TEST_IMPLEMENT(test_ucx_map_clone) { 2.20 + UcxMap *map = ucx_map_new(4); 2.21 + 2.22 + ucx_map_cstr_put(map, "key1", "value1"); 2.23 + ucx_map_cstr_put(map, "key2", "value2"); 2.24 + ucx_map_cstr_put(map, "key3", "value3"); 2.25 + 2.26 + UcxMap *clone = ucx_map_clone(map, NULL, NULL); 2.27 + 2.28 + char *v1 = ucx_map_cstr_get(map, "key1"); 2.29 + char *v2 = ucx_map_cstr_get(map, "key2"); 2.30 + char *v3 = ucx_map_cstr_get(map, "key3"); 2.31 + 2.32 + UCX_TEST_BEGIN 2.33 + 2.34 + UCX_TEST_ASSERT(v1 != NULL, "failed key 1"); 2.35 + UCX_TEST_ASSERT(v2 != NULL, "failed key 2"); 2.36 + UCX_TEST_ASSERT(v3 != NULL, "failed key 3"); 2.37 + 2.38 + char *c1 = ucx_map_cstr_get(clone, "key1"); 2.39 + char *c2 = ucx_map_cstr_get(clone, "key2"); 2.40 + char *c3 = ucx_map_cstr_get(clone, "key3"); 2.41 + 2.42 + UCX_TEST_ASSERT(c1 != NULL, "failed key 1 (clone)"); 2.43 + UCX_TEST_ASSERT(c2 != NULL, "failed key 2 (clone)"); 2.44 + UCX_TEST_ASSERT(c3 != NULL, "failed key 3 (clone)"); 2.45 + 2.46 + UCX_TEST_ASSERT(strcmp(c1, v1) == 0, "value error for key1"); 2.47 + UCX_TEST_ASSERT(strcmp(c2, v2) == 0, "value error for key2"); 2.48 + UCX_TEST_ASSERT(strcmp(c3, v3) == 0, "value error for key3"); 2.49 + 2.50 + UCX_TEST_END 2.51 + 2.52 + ucx_map_free(map); 2.53 + ucx_map_free(clone); 2.54 +}
3.1 --- a/test/map_tests.h Thu Oct 04 18:23:32 2012 +0200 3.2 +++ b/test/map_tests.h Thu Oct 04 18:46:57 2012 +0200 3.3 @@ -19,6 +19,7 @@ 3.4 UCX_TEST_DECLARE(test_ucx_map_iterator) 3.5 UCX_TEST_DECLARE(test_ucx_map_iterator_chain) 3.6 UCX_TEST_DECLARE(test_ucx_map_store_load) 3.7 +UCX_TEST_DECLARE(test_ucx_map_clone) 3.8 3.9 3.10 #ifdef __cplusplus
4.1 --- a/ucx/map.c Thu Oct 04 18:23:32 2012 +0200 4.2 +++ b/ucx/map.c Thu Oct 04 18:46:57 2012 +0200 4.3 @@ -4,9 +4,6 @@ 4.4 4.5 #include <stdlib.h> 4.6 #include <string.h> 4.7 -#ifndef _WIN32 4.8 -#include <unistd.h> 4.9 -#endif /* not _WIN32 */ 4.10 4.11 #include "map.h" 4.12 4.13 @@ -42,6 +39,16 @@ 4.14 free(map); 4.15 } 4.16 4.17 +UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data) { 4.18 + UcxMap *newmap = ucx_map_new(map->size); 4.19 + UcxMapIterator i = ucx_map_iterator(map); 4.20 + void *value; 4.21 + UCX_MAP_FOREACH(value, i) { 4.22 + ucx_map_put(newmap, i.cur->key, fnc ? fnc(value, data) : value); 4.23 + } 4.24 + return newmap; 4.25 +} 4.26 + 4.27 int ucx_map_put(UcxMap *map, UcxKey key, void *data) { 4.28 if(key.hash == 0) { 4.29 key.hash = ucx_hash((char*)key.data, key.len);
5.1 --- a/ucx/map.h Thu Oct 04 18:23:32 2012 +0200 5.2 +++ b/ucx/map.h Thu Oct 04 18:46:57 2012 +0200 5.3 @@ -47,6 +47,7 @@ 5.4 5.5 UcxMap *ucx_map_new(size_t size); 5.6 void ucx_map_free(UcxMap *map); 5.7 +UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data); 5.8 5.9 int ucx_map_put(UcxMap *map, UcxKey key, void *data); 5.10 void* ucx_map_get(UcxMap *map, UcxKey key);