ucx/map.c

changeset 51
1c78cd19fb6b
parent 48
621a4430c404
child 52
34f50d0bada4
equal deleted inserted replaced
50:ff194559eb41 51:1c78cd19fb6b
43 free(map->map); 43 free(map->map);
44 free(map); 44 free(map);
45 } 45 }
46 46
47 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data) { 47 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data) {
48 size_t bs = (map->count * 5) >> 2; 48 size_t bs = (map->count * 5) >> 1;
49 UcxMap *newmap = ucx_map_new(bs > map->size ? bs : map->size); 49 UcxMap *newmap = ucx_map_new(bs > map->size ? bs : map->size);
50 UcxMapIterator i = ucx_map_iterator(map); 50 UcxMapIterator i = ucx_map_iterator(map);
51 void *value; 51 void *value;
52 UCX_MAP_FOREACH(value, i) { 52 UCX_MAP_FOREACH(value, i) {
53 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);
54 } 54 }
55 return newmap; 55 return newmap;
56 }
57
58 UcxMap *ucx_map_rehash(UcxMap *map) {
59 size_t load = (map->size * 3) >> 2;
60 if (map->count > load) {
61 UcxMap *newmap = ucx_map_clone(map, NULL, NULL);
62 ucx_map_free(map);
63 return newmap;
64 } else {
65 return map;
66 }
56 } 67 }
57 68
58 int ucx_map_put(UcxMap *map, UcxKey key, void *data) { 69 int ucx_map_put(UcxMap *map, UcxKey key, void *data) {
59 if(key.hash == 0) { 70 if(key.hash == 0) {
60 key.hash = ucx_hash((char*)key.data, key.len); 71 key.hash = ucx_hash((char*)key.data, key.len);

mercurial