Fri, 21 Apr 2023 18:38:18 +0200
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 |
1.1 --- a/src/cx/hash_key.h Thu Apr 20 19:13:25 2023 +0200 1.2 +++ b/src/cx/hash_key.h Fri Apr 21 18:38:18 2023 +0200 1.3 @@ -47,14 +47,7 @@ 1.4 /** Internal structure for a key within a hash map. */ 1.5 struct cx_hash_key_s { 1.6 /** The key data. */ 1.7 - union { 1.8 - unsigned char *bytes; 1.9 - unsigned char const *cbytes; 1.10 - char *str; 1.11 - char const *cstr; 1.12 - void *obj; 1.13 - void const *cobj; 1.14 - } data; 1.15 + void const *data; 1.16 /** 1.17 * The key data length. 1.18 */
2.1 --- a/src/hash_key.c Thu Apr 20 19:13:25 2023 +0200 2.2 +++ b/src/hash_key.c Fri Apr 21 18:38:18 2023 +0200 2.3 @@ -30,7 +30,7 @@ 2.4 #include <string.h> 2.5 2.6 void cx_hash_murmur(CxHashKey *key) { 2.7 - unsigned char const *data = key->data.cbytes; 2.8 + unsigned char const *data = key->data; 2.9 if (data == NULL) { 2.10 // extension: special value for NULL 2.11 key->hash = 1574210520u; 2.12 @@ -83,7 +83,7 @@ 2.13 2.14 CxHashKey cx_hash_key_str(char const *str) { 2.15 CxHashKey key; 2.16 - key.data.cstr = str; 2.17 + key.data = str; 2.18 key.len = str == NULL ? 0 : strlen(str); 2.19 cx_hash_murmur(&key); 2.20 return key; 2.21 @@ -94,7 +94,7 @@ 2.22 size_t len 2.23 ) { 2.24 CxHashKey key; 2.25 - key.data.cbytes = bytes; 2.26 + key.data = bytes; 2.27 key.len = len; 2.28 cx_hash_murmur(&key); 2.29 return key; 2.30 @@ -105,7 +105,7 @@ 2.31 size_t len 2.32 ) { 2.33 CxHashKey key; 2.34 - key.data.cobj = obj; 2.35 + key.data = obj; 2.36 key.len = len; 2.37 cx_hash_murmur(&key); 2.38 return key;
3.1 --- a/src/hash_map.c Thu Apr 20 19:13:25 2023 +0200 3.2 +++ b/src/hash_map.c Fri Apr 21 18:38:18 2023 +0200 3.3 @@ -51,7 +51,7 @@ 3.4 // invoke the destructor 3.5 cx_invoke_destructor(map, elem->data); 3.6 // free the key data 3.7 - cxFree(map->allocator, elem->key.data.obj); 3.8 + cxFree(map->allocator, (void *) elem->key.data); 3.9 // free the node 3.10 cxFree(map->allocator, elem); 3.11 // proceed 3.12 @@ -100,7 +100,7 @@ 3.13 } 3.14 3.15 if (elm != NULL && elm->key.hash == hash && elm->key.len == key.len && 3.16 - memcmp(elm->key.data.obj, key.data.obj, key.len) == 0) { 3.17 + memcmp(elm->key.data, key.data, key.len) == 0) { 3.18 // overwrite existing element 3.19 if (map->store_pointer) { 3.20 memcpy(elm->data, &value, sizeof(void *)); 3.21 @@ -129,8 +129,8 @@ 3.22 if (kd == NULL) { 3.23 return -1; 3.24 } 3.25 - memcpy(kd, key.data.obj, key.len); 3.26 - e->key.data.obj = kd; 3.27 + memcpy(kd, key.data, key.len); 3.28 + e->key.data = kd; 3.29 e->key.len = key.len; 3.30 e->key.hash = hash; 3.31 3.32 @@ -162,7 +162,7 @@ 3.33 prev->next = elm->next; 3.34 } 3.35 // free element 3.36 - cxFree(hash_map->base.allocator, elm->key.data.obj); 3.37 + cxFree(hash_map->base.allocator, (void *) elm->key.data); 3.38 cxFree(hash_map->base.allocator, elm); 3.39 // decrease size 3.40 hash_map->base.size--; 3.41 @@ -196,7 +196,7 @@ 3.42 struct cx_hash_map_element_s *prev = NULL; 3.43 while (elm && elm->key.hash <= hash) { 3.44 if (elm->key.hash == hash && elm->key.len == key.len) { 3.45 - if (memcmp(elm->key.data.obj, key.data.obj, key.len) == 0) { 3.46 + if (memcmp(elm->key.data, key.data, key.len) == 0) { 3.47 void *data = NULL; 3.48 if (destroy) { 3.49 cx_invoke_destructor(map, elm->data);
4.1 --- a/tests/test_hash_key.cpp Thu Apr 20 19:13:25 2023 +0200 4.2 +++ b/tests/test_hash_key.cpp Fri Apr 21 18:38:18 2023 +0200 4.3 @@ -49,10 +49,7 @@ 4.4 EXPECT_EQ(cxstr_key.len, len); 4.5 EXPECT_EQ(bytes_key.len, len); 4.6 EXPECT_EQ(bytes_key.len, len); 4.7 - EXPECT_EQ(str_key.data.cstr, str); 4.8 - EXPECT_EQ(bytes_key.data.cbytes, reinterpret_cast<unsigned char const *>(str)); 4.9 - EXPECT_EQ(bytes_key.data.cobj, reinterpret_cast<void const *>(str)); 4.10 - EXPECT_EQ(cxstr_key.data.cobj, reinterpret_cast<void const *>(str)); 4.11 + EXPECT_EQ(str_key.data, str); 4.12 } 4.13 4.14 TEST(cx_hash_key, empty_string) { 4.15 @@ -70,9 +67,7 @@ 4.16 EXPECT_EQ(str_key.len, 0); 4.17 EXPECT_EQ(bytes_key.len, 0); 4.18 EXPECT_EQ(bytes_key.len, 0); 4.19 - EXPECT_EQ(str_key.data.cstr, str); 4.20 - EXPECT_EQ(bytes_key.data.cbytes, reinterpret_cast<unsigned char const *>(str)); 4.21 - EXPECT_EQ(bytes_key.data.cobj, reinterpret_cast<void const *>(str)); 4.22 + EXPECT_EQ(str_key.data, str); 4.23 } 4.24 4.25 TEST(cx_hash_key, null_ptr) { 4.26 @@ -86,7 +81,5 @@ 4.27 EXPECT_EQ(str_key.len, 0); 4.28 EXPECT_EQ(bytes_key.len, 0); 4.29 EXPECT_EQ(bytes_key.len, 0); 4.30 - EXPECT_EQ(str_key.data.cstr, nullptr); 4.31 - EXPECT_EQ(bytes_key.data.cbytes, nullptr); 4.32 - EXPECT_EQ(bytes_key.data.cobj, nullptr); 4.33 + EXPECT_EQ(str_key.data, nullptr); 4.34 }
5.1 --- a/tests/test_map.cpp Thu Apr 20 19:13:25 2023 +0200 5.2 +++ b/tests/test_map.cpp Fri Apr 21 18:38:18 2023 +0200 5.3 @@ -74,7 +74,7 @@ 5.4 auto keyiter = cxMapIteratorKeys(map); 5.5 std::unordered_set<std::string> keys; 5.6 cx_foreach(CxHashKey*, elem, keyiter) { 5.7 - keys.insert(std::string(elem->data.cstr, elem->len)); 5.8 + keys.insert(std::string(reinterpret_cast<char const*>(elem->data), elem->len)); 5.9 } 5.10 EXPECT_EQ(keyiter.index, map->size); 5.11 ASSERT_EQ(keys.size(), map->size); 5.12 @@ -103,7 +103,7 @@ 5.13 auto pairiter = cxMapIterator(map); 5.14 std::unordered_map<std::string, std::string> pairs; 5.15 cx_foreach(CxMapEntry*, entry, pairiter) { 5.16 - pairs[std::string(entry->key->data.cstr, entry->key->len)] = std::string((char *) entry->value); 5.17 + pairs[std::string(reinterpret_cast<char const*>(entry->key->data), entry->key->len)] = std::string((char *) entry->value); 5.18 } 5.19 EXPECT_EQ(pairiter.index, map->size); 5.20 ASSERT_EQ(pairs.size(), refmap.size()); 5.21 @@ -214,7 +214,7 @@ 5.22 5.23 auto iter = cxMapMutIterator(map); 5.24 cx_foreach(CxMapEntry*, entry, iter) { 5.25 - if (entry->key->data.cstr[4] % 2 == 1) cxIteratorFlagRemoval(iter); 5.26 + if (reinterpret_cast<char const*>(entry->key->data)[4] % 2 == 1) cxIteratorFlagRemoval(iter); 5.27 } 5.28 EXPECT_EQ(map->size, 3); 5.29 EXPECT_EQ(iter.index, map->size); 5.30 @@ -405,7 +405,7 @@ 5.31 { 5.32 auto iter = cxMapMutIteratorKeys(map); 5.33 cx_foreach(CxHashKey*, key, iter) { 5.34 - if (key->data.cstr[4] == '1') cxIteratorFlagRemoval(iter); 5.35 + if (reinterpret_cast<char const*>(key->data)[4] == '1') cxIteratorFlagRemoval(iter); 5.36 } 5.37 } 5.38 {