src/map.c

changeset 327
fbc33813265b
parent 287
98da78a1e69a
child 328
2bf1da3c411e
     1.1 --- a/src/map.c	Wed May 30 11:13:52 2018 +0200
     1.2 +++ b/src/map.c	Thu Jun 21 16:00:37 2018 +0200
     1.3 @@ -154,19 +154,22 @@
     1.4      UcxAllocator *allocator = map->allocator;
     1.5      
     1.6      if (key.hash == 0) {
     1.7 -        key.hash = ucx_hash((char*)key.data, key.len);
     1.8 +        key.hash = ucx_hash(key.data, key.len);
     1.9      }
    1.10 +    
    1.11 +    struct UcxMapKey mapkey;
    1.12 +    mapkey.hash = key.hash;
    1.13  
    1.14 -    size_t slot = key.hash%map->size;
    1.15 +    size_t slot = mapkey.hash%map->size;
    1.16      UcxMapElement *elm = map->map[slot];
    1.17      UcxMapElement *prev = NULL;
    1.18  
    1.19 -    while (elm && elm->key.hash < key.hash) {
    1.20 +    while (elm && elm->key.hash < mapkey.hash) {
    1.21          prev = elm;
    1.22          elm = elm->next;
    1.23      }
    1.24      
    1.25 -    if (!elm || elm->key.hash != key.hash) {
    1.26 +    if (!elm || elm->key.hash != mapkey.hash) {
    1.27          UcxMapElement *e = (UcxMapElement*)almalloc(
    1.28                  allocator, sizeof(UcxMapElement));
    1.29          if (!e) {
    1.30 @@ -188,8 +191,9 @@
    1.31              return -1;
    1.32          }
    1.33          memcpy(kd, key.data, key.len);
    1.34 -        key.data = kd;
    1.35 -        elm->key = key;
    1.36 +        mapkey.data = kd;
    1.37 +        mapkey.len = key.len;
    1.38 +        elm->key = mapkey;
    1.39          map->count++;
    1.40      }
    1.41      elm->data = data;
    1.42 @@ -239,11 +243,11 @@
    1.43      return ucx_map_get_and_remove(map, key, 1);
    1.44  }
    1.45  
    1.46 -UcxKey ucx_key(void *data, size_t len) {
    1.47 +UcxKey ucx_key(const void *data, size_t len) {
    1.48      UcxKey key;
    1.49      key.data = data;
    1.50      key.len = len;
    1.51 -    key.hash = ucx_hash((const char*) data, len);
    1.52 +    key.hash = ucx_hash(data, len);
    1.53      return key;
    1.54  }
    1.55  
    1.56 @@ -312,7 +316,9 @@
    1.57              if (e->data) {
    1.58                  i->cur = e;
    1.59                  *elm = e->data;
    1.60 -                *key = e->key;
    1.61 +                key->data = e->key.data;
    1.62 +                key->hash = e->key.hash;
    1.63 +                key->len = e->key.len;
    1.64                  return 1;
    1.65              }
    1.66  

mercurial