src/map.c

changeset 374
be77fb2da242
parent 328
2bf1da3c411e
     1.1 --- a/src/map.c	Thu Dec 19 18:47:23 2019 +0100
     1.2 +++ b/src/map.c	Thu Dec 19 19:58:41 2019 +0100
     1.3 @@ -103,7 +103,7 @@
     1.4      map->count = 0;
     1.5  }
     1.6  
     1.7 -int ucx_map_copy(UcxMap *from, UcxMap *to, copy_func fnc, void *data) {
     1.8 +int ucx_map_copy(UcxMap const *from, UcxMap *to, copy_func fnc, void *data) {
     1.9      UcxMapIterator i = ucx_map_iterator(from);
    1.10      void *value;
    1.11      UCX_MAP_FOREACH(key, value, i) {
    1.12 @@ -114,9 +114,14 @@
    1.13      return 0;
    1.14  }
    1.15  
    1.16 -UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data) {
    1.17 +UcxMap *ucx_map_clone(UcxMap const *map, copy_func fnc, void *data) {
    1.18 +    return ucx_map_clone_a(ucx_default_allocator(), map, fnc, data);
    1.19 +}
    1.20 +
    1.21 +UcxMap *ucx_map_clone_a(UcxAllocator *allocator,
    1.22 +        UcxMap const *map, copy_func fnc, void *data) {
    1.23      size_t bs = (map->count * 5) >> 1;
    1.24 -    UcxMap *newmap = ucx_map_new(bs > map->size ? bs : map->size);
    1.25 +    UcxMap *newmap = ucx_map_new_a(allocator, bs > map->size ? bs : map->size);
    1.26      if (!newmap) {
    1.27          return NULL;
    1.28      }
    1.29 @@ -235,8 +240,8 @@
    1.30      return NULL;
    1.31  }
    1.32  
    1.33 -void *ucx_map_get(UcxMap *map, UcxKey key) {
    1.34 -    return ucx_map_get_and_remove(map, key, 0);
    1.35 +void *ucx_map_get(UcxMap const *map, UcxKey key) {
    1.36 +    return ucx_map_get_and_remove((UcxMap *)map, key, 0);
    1.37  }
    1.38  
    1.39  void *ucx_map_remove(UcxMap *map, UcxKey key) {
    1.40 @@ -294,7 +299,7 @@
    1.41      return h;
    1.42  }
    1.43  
    1.44 -UcxMapIterator ucx_map_iterator(UcxMap *map) {
    1.45 +UcxMapIterator ucx_map_iterator(UcxMap const *map) {
    1.46      UcxMapIterator i;
    1.47      i.map = map;
    1.48      i.cur = NULL;
    1.49 @@ -335,3 +340,63 @@
    1.50      return 0;
    1.51  }
    1.52  
    1.53 +UcxMap* ucx_map_union(const UcxMap *first, const UcxMap *second,
    1.54 +                      copy_func cpfnc, void* cpdata) {
    1.55 +    return ucx_map_union_a(ucx_default_allocator(),
    1.56 +            first, second, cpfnc, cpdata);
    1.57 +}
    1.58 +
    1.59 +UcxMap* ucx_map_union_a(UcxAllocator *allocator,
    1.60 +                        const UcxMap *first, const UcxMap *second,
    1.61 +                        copy_func cpfnc, void* cpdata) {
    1.62 +    UcxMap* result = ucx_map_clone_a(allocator, first, cpfnc, cpdata);
    1.63 +    ucx_map_copy(second, result, cpfnc, cpdata);
    1.64 +    return result;
    1.65 +}
    1.66 +
    1.67 +UcxMap* ucx_map_intersection(const UcxMap *first, const UcxMap *second,
    1.68 +                             copy_func cpfnc, void* cpdata) {
    1.69 +    return ucx_map_intersection_a(ucx_default_allocator(),
    1.70 +            first, second, cpfnc, cpdata);
    1.71 +}
    1.72 +
    1.73 +UcxMap* ucx_map_intersection_a(UcxAllocator *allocator,
    1.74 +                               const UcxMap *first, const UcxMap *second,
    1.75 +                               copy_func cpfnc, void* cpdata) {
    1.76 +    UcxMap *result = ucx_map_new_a(allocator, first->size < second->size ?
    1.77 +            first->size : second->size);
    1.78 +
    1.79 +    UcxMapIterator iter = ucx_map_iterator(first);
    1.80 +    void* value;
    1.81 +    UCX_MAP_FOREACH(key, value, iter) {
    1.82 +        if (ucx_map_get(second, key)) {
    1.83 +            ucx_map_put(result, key, cpfnc ? cpfnc(value, cpdata) : value);
    1.84 +        }
    1.85 +    }
    1.86 +
    1.87 +    return result;
    1.88 +}
    1.89 +
    1.90 +UcxMap* ucx_map_difference(const UcxMap *first, const UcxMap *second,
    1.91 +                           copy_func cpfnc, void* cpdata) {
    1.92 +    return ucx_map_difference_a(ucx_default_allocator(),
    1.93 +            first, second, cpfnc, cpdata);
    1.94 +}
    1.95 +
    1.96 +UcxMap* ucx_map_difference_a(UcxAllocator *allocator,
    1.97 +                             const UcxMap *first, const UcxMap *second,
    1.98 +                             copy_func cpfnc, void* cpdata) {
    1.99 +
   1.100 +    UcxMap *result = ucx_map_new_a(allocator, first->size - second->count);
   1.101 +
   1.102 +    UcxMapIterator iter = ucx_map_iterator(first);
   1.103 +    void* value;
   1.104 +    UCX_MAP_FOREACH(key, value, iter) {
   1.105 +        if (!ucx_map_get(second, key)) {
   1.106 +            ucx_map_put(result, key, cpfnc ? cpfnc(value, cpdata) : value);
   1.107 +        }
   1.108 +    }
   1.109 +
   1.110 +    ucx_map_rehash(result);
   1.111 +    return result;
   1.112 +}
   1.113 \ No newline at end of file

mercurial