UcxMap now separates internal non-const keys from public const keys

Thu, 21 Jun 2018 16:00:37 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 21 Jun 2018 16:00:37 +0200
changeset 327
fbc33813265b
parent 326
3dd7d21fb76b
child 328
2bf1da3c411e

UcxMap now separates internal non-const keys from public const keys

This simplifies function calls with constant keys like scstr_t or const char*.

src/map.c file | annotate | diff | comparison | revisions
src/ucx/map.h file | annotate | diff | comparison | revisions
test/map_tests.c file | annotate | diff | comparison | revisions
     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  
     2.1 --- a/src/ucx/map.h	Wed May 30 11:13:52 2018 +0200
     2.2 +++ b/src/ucx/map.h	Thu Jun 21 16:00:37 2018 +0200
     2.3 @@ -89,26 +89,36 @@
     2.4      size_t        count;
     2.5  };
     2.6  
     2.7 -/** Structure for a key of a UcxMap. */
     2.8 +/** Structure to publicly denote a key of a UcxMap. */
     2.9  struct UcxKey {
    2.10      /** The key data. */
    2.11 -    void   *data;
    2.12 +    const void *data;
    2.13      /** The length of the key data. */
    2.14 -    size_t len;
    2.15 +    size_t     len;
    2.16 +    /** A cache for the hash value of the key data. */
    2.17 +    int        hash;
    2.18 +};
    2.19 +
    2.20 +/** Internal structure for a key of a UcxMap. */
    2.21 +struct UcxMapKey {
    2.22 +    /** The key data. */
    2.23 +    void    *data;
    2.24 +    /** The length of the key data. */
    2.25 +    size_t  len;
    2.26      /** The hash value of the key data. */
    2.27 -    int    hash;
    2.28 +    int     hash;
    2.29  };
    2.30  
    2.31  /** Structure for an element of a UcxMap. */
    2.32  struct UcxMapElement {
    2.33      /** The value data. */
    2.34 -    void          *data;
    2.35 +    void              *data;
    2.36      
    2.37      /** A pointer to the next element in the current list. */
    2.38 -    UcxMapElement *next;
    2.39 +    UcxMapElement     *next;
    2.40      
    2.41      /** The corresponding key. */
    2.42 -    UcxKey        key;
    2.43 +    struct UcxMapKey  key;
    2.44  };
    2.45  
    2.46  /** Structure for an iterator over a UcxMap. */
    2.47 @@ -285,7 +295,7 @@
    2.48   * @see ucx_map_put()
    2.49   */
    2.50  #define ucx_map_cstr_put(map, key, value) \
    2.51 -    ucx_map_put(map, ucx_key((void*)key, strlen(key)), (void*)value)
    2.52 +    ucx_map_put(map, ucx_key(key, strlen(key)), (void*)value)
    2.53  
    2.54  /**
    2.55   * Shorthand for putting data with an integer key into the map.
    2.56 @@ -296,7 +306,7 @@
    2.57   * @see ucx_map_put()
    2.58   */
    2.59  #define ucx_map_int_put(map, key, value) \
    2.60 -    ucx_map_put(map, ucx_key((void*)&key, sizeof(key)), (void*)value)
    2.61 +    ucx_map_put(map, ucx_key(&key, sizeof(key)), (void*)value)
    2.62  
    2.63  /**
    2.64   * Shorthand for getting data from the map with a sstr_t key.
    2.65 @@ -316,7 +326,7 @@
    2.66   * @see ucx_map_get()
    2.67   */
    2.68  #define ucx_map_cstr_get(map, key) \
    2.69 -    ucx_map_get(map, ucx_key((void*)key, strlen(key)))
    2.70 +    ucx_map_get(map, ucx_key(key, strlen(key)))
    2.71  
    2.72  /**
    2.73   * Shorthand for getting data from the map with an integer key.
    2.74 @@ -326,7 +336,7 @@
    2.75   * @see ucx_map_get()
    2.76   */
    2.77  #define ucx_map_int_get(map, key) \
    2.78 -    ucx_map_get(map, ucx_key((void*)&key, sizeof(int)))
    2.79 +    ucx_map_get(map, ucx_key(&key, sizeof(int)))
    2.80  
    2.81  /**
    2.82   * Shorthand for removing data from the map with a sstr_t key.
    2.83 @@ -346,7 +356,7 @@
    2.84   * @see ucx_map_remove()
    2.85   */
    2.86  #define ucx_map_cstr_remove(map, key) \
    2.87 -    ucx_map_remove(map, ucx_key((void*)key, strlen(key)))
    2.88 +    ucx_map_remove(map, ucx_key(key, strlen(key)))
    2.89  
    2.90  /**
    2.91   * Shorthand for removing data from the map with an integer key.
    2.92 @@ -356,7 +366,7 @@
    2.93   * @see ucx_map_remove()
    2.94   */
    2.95  #define ucx_map_int_remove(map, key) \
    2.96 -    ucx_map_remove(map, ucx_key((void*)&key, sizeof(key)))
    2.97 +    ucx_map_remove(map, ucx_key(&key, sizeof(key)))
    2.98  
    2.99  /**
   2.100   * Creates a UcxKey based on the given data.
   2.101 @@ -368,7 +378,7 @@
   2.102   * @return a UcxKey with implicitly computed hash
   2.103   * @see ucx_hash()
   2.104   */
   2.105 -UcxKey ucx_key(void *data, size_t len);
   2.106 +UcxKey ucx_key(const void *data, size_t len);
   2.107  
   2.108  /**
   2.109   * Computes a murmur hash-2.
     3.1 --- a/test/map_tests.c	Wed May 30 11:13:52 2018 +0200
     3.2 +++ b/test/map_tests.c	Thu Jun 21 16:00:37 2018 +0200
     3.3 @@ -39,7 +39,7 @@
     3.4  }
     3.5  
     3.6  UCX_TEST(test_ucx_key) {
     3.7 -    UcxKey key = ucx_key((void*)"This is a text.", 15);
     3.8 +    UcxKey key = ucx_key("This is a text.", 15);
     3.9      UCX_TEST_BEGIN
    3.10      UCX_TEST_ASSERT(strncmp((const char*)key.data, "This is a text.", 15) == 0,
    3.11              "failed");

mercurial