simplify CxHashKey

Fri, 21 Apr 2023 18:38:18 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 21 Apr 2023 18:38:18 +0200
changeset 690
2c2304622981
parent 689
5d0244c6fa3e
child 691
65baf7f45ac8

simplify CxHashKey

src/cx/hash_key.h file | annotate | diff | comparison | revisions
src/hash_key.c file | annotate | diff | comparison | revisions
src/hash_map.c file | annotate | diff | comparison | revisions
tests/test_hash_key.cpp file | annotate | diff | comparison | revisions
tests/test_map.cpp file | annotate | diff | comparison | revisions
--- a/src/cx/hash_key.h	Thu Apr 20 19:13:25 2023 +0200
+++ b/src/cx/hash_key.h	Fri Apr 21 18:38:18 2023 +0200
@@ -47,14 +47,7 @@
 /** Internal structure for a key within a hash map. */
 struct cx_hash_key_s {
     /** The key data. */
-    union {
-        unsigned char *bytes;
-        unsigned char const *cbytes;
-        char *str;
-        char const *cstr;
-        void *obj;
-        void const *cobj;
-    } data;
+    void const *data;
     /**
      * The key data length.
      */
--- a/src/hash_key.c	Thu Apr 20 19:13:25 2023 +0200
+++ b/src/hash_key.c	Fri Apr 21 18:38:18 2023 +0200
@@ -30,7 +30,7 @@
 #include <string.h>
 
 void cx_hash_murmur(CxHashKey *key) {
-    unsigned char const *data = key->data.cbytes;
+    unsigned char const *data = key->data;
     if (data == NULL) {
         // extension: special value for NULL
         key->hash = 1574210520u;
@@ -83,7 +83,7 @@
 
 CxHashKey cx_hash_key_str(char const *str) {
     CxHashKey key;
-    key.data.cstr = str;
+    key.data = str;
     key.len = str == NULL ? 0 : strlen(str);
     cx_hash_murmur(&key);
     return key;
@@ -94,7 +94,7 @@
         size_t len
 ) {
     CxHashKey key;
-    key.data.cbytes = bytes;
+    key.data = bytes;
     key.len = len;
     cx_hash_murmur(&key);
     return key;
@@ -105,7 +105,7 @@
         size_t len
 ) {
     CxHashKey key;
-    key.data.cobj = obj;
+    key.data = obj;
     key.len = len;
     cx_hash_murmur(&key);
     return key;
--- a/src/hash_map.c	Thu Apr 20 19:13:25 2023 +0200
+++ b/src/hash_map.c	Fri Apr 21 18:38:18 2023 +0200
@@ -51,7 +51,7 @@
                 // invoke the destructor
                 cx_invoke_destructor(map, elem->data);
                 // free the key data
-                cxFree(map->allocator, elem->key.data.obj);
+                cxFree(map->allocator, (void *) elem->key.data);
                 // free the node
                 cxFree(map->allocator, elem);
                 // proceed
@@ -100,7 +100,7 @@
     }
 
     if (elm != NULL && elm->key.hash == hash && elm->key.len == key.len &&
-        memcmp(elm->key.data.obj, key.data.obj, key.len) == 0) {
+        memcmp(elm->key.data, key.data, key.len) == 0) {
         // overwrite existing element
         if (map->store_pointer) {
             memcpy(elm->data, &value, sizeof(void *));
@@ -129,8 +129,8 @@
         if (kd == NULL) {
             return -1;
         }
-        memcpy(kd, key.data.obj, key.len);
-        e->key.data.obj = kd;
+        memcpy(kd, key.data, key.len);
+        e->key.data = kd;
         e->key.len = key.len;
         e->key.hash = hash;
 
@@ -162,7 +162,7 @@
         prev->next = elm->next;
     }
     // free element
-    cxFree(hash_map->base.allocator, elm->key.data.obj);
+    cxFree(hash_map->base.allocator, (void *) elm->key.data);
     cxFree(hash_map->base.allocator, elm);
     // decrease size
     hash_map->base.size--;
@@ -196,7 +196,7 @@
     struct cx_hash_map_element_s *prev = NULL;
     while (elm && elm->key.hash <= hash) {
         if (elm->key.hash == hash && elm->key.len == key.len) {
-            if (memcmp(elm->key.data.obj, key.data.obj, key.len) == 0) {
+            if (memcmp(elm->key.data, key.data, key.len) == 0) {
                 void *data = NULL;
                 if (destroy) {
                     cx_invoke_destructor(map, elm->data);
--- a/tests/test_hash_key.cpp	Thu Apr 20 19:13:25 2023 +0200
+++ b/tests/test_hash_key.cpp	Fri Apr 21 18:38:18 2023 +0200
@@ -49,10 +49,7 @@
     EXPECT_EQ(cxstr_key.len, len);
     EXPECT_EQ(bytes_key.len, len);
     EXPECT_EQ(bytes_key.len, len);
-    EXPECT_EQ(str_key.data.cstr, str);
-    EXPECT_EQ(bytes_key.data.cbytes, reinterpret_cast<unsigned char const *>(str));
-    EXPECT_EQ(bytes_key.data.cobj, reinterpret_cast<void const *>(str));
-    EXPECT_EQ(cxstr_key.data.cobj, reinterpret_cast<void const *>(str));
+    EXPECT_EQ(str_key.data, str);
 }
 
 TEST(cx_hash_key, empty_string) {
@@ -70,9 +67,7 @@
     EXPECT_EQ(str_key.len, 0);
     EXPECT_EQ(bytes_key.len, 0);
     EXPECT_EQ(bytes_key.len, 0);
-    EXPECT_EQ(str_key.data.cstr, str);
-    EXPECT_EQ(bytes_key.data.cbytes, reinterpret_cast<unsigned char const *>(str));
-    EXPECT_EQ(bytes_key.data.cobj, reinterpret_cast<void const *>(str));
+    EXPECT_EQ(str_key.data, str);
 }
 
 TEST(cx_hash_key, null_ptr) {
@@ -86,7 +81,5 @@
     EXPECT_EQ(str_key.len, 0);
     EXPECT_EQ(bytes_key.len, 0);
     EXPECT_EQ(bytes_key.len, 0);
-    EXPECT_EQ(str_key.data.cstr, nullptr);
-    EXPECT_EQ(bytes_key.data.cbytes, nullptr);
-    EXPECT_EQ(bytes_key.data.cobj, nullptr);
+    EXPECT_EQ(str_key.data, nullptr);
 }
--- a/tests/test_map.cpp	Thu Apr 20 19:13:25 2023 +0200
+++ b/tests/test_map.cpp	Fri Apr 21 18:38:18 2023 +0200
@@ -74,7 +74,7 @@
         auto keyiter = cxMapIteratorKeys(map);
         std::unordered_set<std::string> keys;
         cx_foreach(CxHashKey*, elem, keyiter) {
-            keys.insert(std::string(elem->data.cstr, elem->len));
+            keys.insert(std::string(reinterpret_cast<char const*>(elem->data), elem->len));
         }
         EXPECT_EQ(keyiter.index, map->size);
         ASSERT_EQ(keys.size(), map->size);
@@ -103,7 +103,7 @@
         auto pairiter = cxMapIterator(map);
         std::unordered_map<std::string, std::string> pairs;
         cx_foreach(CxMapEntry*, entry, pairiter) {
-            pairs[std::string(entry->key->data.cstr, entry->key->len)] = std::string((char *) entry->value);
+            pairs[std::string(reinterpret_cast<char const*>(entry->key->data), entry->key->len)] = std::string((char *) entry->value);
         }
         EXPECT_EQ(pairiter.index, map->size);
         ASSERT_EQ(pairs.size(), refmap.size());
@@ -214,7 +214,7 @@
 
     auto iter = cxMapMutIterator(map);
     cx_foreach(CxMapEntry*, entry, iter) {
-        if (entry->key->data.cstr[4] % 2 == 1) cxIteratorFlagRemoval(iter);
+        if (reinterpret_cast<char const*>(entry->key->data)[4] % 2 == 1) cxIteratorFlagRemoval(iter);
     }
     EXPECT_EQ(map->size, 3);
     EXPECT_EQ(iter.index, map->size);
@@ -405,7 +405,7 @@
     {
         auto iter = cxMapMutIteratorKeys(map);
         cx_foreach(CxHashKey*, key, iter) {
-            if (key->data.cstr[4] == '1') cxIteratorFlagRemoval(iter);
+            if (reinterpret_cast<char const*>(key->data)[4] == '1') cxIteratorFlagRemoval(iter);
         }
     }
     {

mercurial