src/hash_key.c

changeset 1400
7bc88ae62755
parent 1364
556e4e7608b1
--- a/src/hash_key.c	Sat Sep 27 17:47:10 2025 +0200
+++ b/src/hash_key.c	Sat Sep 27 17:49:13 2025 +0200
@@ -27,6 +27,7 @@
  */
 
 #include "cx/hash_key.h"
+#include "cx/compare.h"
 #include <string.h>
 
 void cx_hash_murmur(CxHashKey *key) {
@@ -81,6 +82,21 @@
     key->hash = h;
 }
 
+
+uint32_t cx_hash_u32(uint32_t x) {
+    x = ((x >> 16) ^ x) * 0x45d9f3bu;
+    x = ((x >> 16) ^ x) * 0x45d9f3bu;
+    x = (x >> 16) ^ x;
+    return x;
+}
+
+uint64_t cx_hash_u64(uint64_t x) {
+    x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
+    x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb);
+    x = x ^ (x >> 31);
+    return x;
+}
+
 CxHashKey cx_hash_key_str(const char *str) {
     CxHashKey key;
     key.data = str;
@@ -110,3 +126,29 @@
     cx_hash_murmur(&key);
     return key;
 }
+
+CxHashKey cx_hash_key_u32(uint32_t x) {
+    CxHashKey key;
+    key.data = NULL;
+    key.len = 0;
+    key.hash = cx_hash_u32(x);
+    return key;
+}
+
+CxHashKey cx_hash_key_u64(uint64_t x) {
+    CxHashKey key;
+    key.data = NULL;
+    key.len = 0;
+    key.hash = cx_hash_u64(x);
+    return key;
+}
+
+int cx_hash_key_cmp(const CxHashKey *left, const CxHashKey *right) {
+    int d;
+    d = cx_vcmp_uint64(left->hash, right->hash);
+    if (d != 0) return d;
+    d = cx_vcmp_size(left->len, right->len);
+    if (d != 0) return d;
+    if (left->len == 0) return 0;
+    return memcmp(left->data, right->data, left->len);
+}

mercurial