ucx/map.c

changeset 52
34f50d0bada4
parent 51
1c78cd19fb6b
child 53
e533c170bfb8
     1.1 --- a/ucx/map.c	Fri Oct 05 14:06:40 2012 +0200
     1.2 +++ b/ucx/map.c	Fri Oct 05 16:59:14 2012 +0200
     1.3 @@ -44,26 +44,46 @@
     1.4      free(map);
     1.5  }
     1.6  
     1.7 +int ucx_map_copy(UcxMap *from, UcxMap *to, copy_func fnc, void *data) {
     1.8 +    UcxMapIterator i = ucx_map_iterator(from);
     1.9 +    void *value;
    1.10 +    UCX_MAP_FOREACH(value, i) {
    1.11 +        int ret = ucx_map_put(to, i.cur->key, fnc ? fnc(value, data) : value);
    1.12 +        if(ret != 0) {
    1.13 +            return 1;
    1.14 +        }
    1.15 +    }
    1.16 +    return 0;
    1.17 +}
    1.18 +
    1.19  UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data) {
    1.20      size_t bs = (map->count * 5) >> 1;
    1.21      UcxMap *newmap = ucx_map_new(bs > map->size ? bs : map->size);
    1.22 -    UcxMapIterator i = ucx_map_iterator(map);
    1.23 -    void *value;
    1.24 -    UCX_MAP_FOREACH(value, i) {
    1.25 -        ucx_map_put(newmap, i.cur->key, fnc ? fnc(value, data) : value);
    1.26 +    if(newmap == NULL) {
    1.27 +        return NULL;
    1.28      }
    1.29 +    ucx_map_copy(map, newmap, fnc, data);
    1.30      return newmap;
    1.31  }
    1.32  
    1.33 -UcxMap *ucx_map_rehash(UcxMap *map) {
    1.34 +int ucx_map_rehash(UcxMap *map) {
    1.35      size_t load = (map->size * 3) >> 2;
    1.36      if (map->count > load) {
    1.37 -        UcxMap *newmap = ucx_map_clone(map, NULL, NULL);
    1.38 -        ucx_map_free(map);
    1.39 -        return newmap;
    1.40 -    } else {
    1.41 -        return map;
    1.42 +        UcxMap oldmap;
    1.43 +        oldmap.map = map->map;
    1.44 +        oldmap.size = map->size;
    1.45 +        oldmap.count = map->count;
    1.46 +        
    1.47 +        map->size = (map->count * 5) >> 1;
    1.48 +        map->map = (UcxMapElement**)calloc(map->size, sizeof(UcxMapElement*));
    1.49 +        if(map->map == NULL) {
    1.50 +            *map = oldmap;
    1.51 +            return 1;
    1.52 +        }
    1.53 +        map->count = 0;
    1.54 +        ucx_map_copy(&oldmap, map, NULL, NULL);
    1.55      }
    1.56 +    return 0;
    1.57  }
    1.58  
    1.59  int ucx_map_put(UcxMap *map, UcxKey key, void *data) {

mercurial