44:46356d74e873 | 45:dd03226c1a6b |
---|---|
6 #include <string.h> | 6 #include <string.h> |
7 | 7 |
8 #include "map.h" | 8 #include "map.h" |
9 | 9 |
10 UcxMap *ucx_map_new(size_t size) { | 10 UcxMap *ucx_map_new(size_t size) { |
11 if(size == 0) { | |
12 size = 16; | |
13 } | |
14 | |
11 UcxMap *map = (UcxMap*)malloc(sizeof(UcxMap)); | 15 UcxMap *map = (UcxMap*)malloc(sizeof(UcxMap)); |
12 if(map == NULL) { | 16 if(map == NULL) { |
13 return NULL; | 17 return NULL; |
14 } | 18 } |
15 | 19 |
17 if(map->map == NULL) { | 21 if(map->map == NULL) { |
18 free(map); | 22 free(map); |
19 return NULL; | 23 return NULL; |
20 } | 24 } |
21 map->size = size; | 25 map->size = size; |
26 map->count = 0; | |
22 | 27 |
23 return map; | 28 return map; |
24 } | 29 } |
25 | 30 |
26 void ucx_map_free(UcxMap *map) { | 31 void ucx_map_free(UcxMap *map) { |
38 free(map->map); | 43 free(map->map); |
39 free(map); | 44 free(map); |
40 } | 45 } |
41 | 46 |
42 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data) { | 47 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data) { |
43 UcxMap *newmap = ucx_map_new(map->size); | 48 size_t bs = (map->count * 5) >> 2; |
49 UcxMap *newmap = ucx_map_new(bs > map->size ? bs : map->size); | |
44 UcxMapIterator i = ucx_map_iterator(map); | 50 UcxMapIterator i = ucx_map_iterator(map); |
45 void *value; | 51 void *value; |
46 UCX_MAP_FOREACH(value, i) { | 52 UCX_MAP_FOREACH(value, i) { |
47 ucx_map_put(newmap, i.cur->key, fnc ? fnc(value, data) : value); | 53 ucx_map_put(newmap, i.cur->key, fnc ? fnc(value, data) : value); |
48 } | 54 } |
84 return -1; | 90 return -1; |
85 } | 91 } |
86 memcpy(kd, key.data, key.len); | 92 memcpy(kd, key.data, key.len); |
87 key.data = kd; | 93 key.data = kd; |
88 elm->key = key; | 94 elm->key = key; |
95 map->count++; | |
89 } | 96 } |
90 elm->data = data; | 97 elm->data = data; |
91 | 98 |
92 return 0; | 99 return 0; |
93 } | 100 } |