save works

This commit is contained in:
Rongsong Shen 2025-12-18 14:55:58 +08:00
parent d0174f2f30
commit 4db6b039c5
8 changed files with 140 additions and 34 deletions

View file

@ -6,7 +6,7 @@
#include "utils.h"
#define HASH_KEY_HASH(t, key) \
(((t)->fn.hash != NULL) ? ((t)->fn.hash)(key) : (unsigned long)key)
(((t)->fn.hash != NULL) ? ((t)->fn.hash)(key) : hash64((uint64_t)key))
#define HASH_KEY_CMP(t, k1, k2) \
(((t)->fn.cmp != NULL) ? ((t)->fn.cmp)((k1), (k2)) : ((k1) != (k2)))
@ -21,21 +21,9 @@
} \
} while (0)
static unsigned int hash_code(const char* key)
{
unsigned int hc = 5381;
int c;
while ((c = *key++)) {
hc = ((hc << 5) + hc) + c;
}
return hc;
}
static unsigned int string_hash(void* data)
{
return hash_code((const char*)data);
return hash_string((const char*)data);
}
static int string_cmp(void* d1, void* d2)

View file

@ -3,6 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#if defined(__GLIBC__) && (__GLIBC__ == 2 && __GLIBC_MINOR__ < 28)
#include <pthread.h>
#else
@ -587,3 +588,74 @@ void block_sig(int signo)
sigaddset(&mask, signo);
sigprocmask(SIG_BLOCK, &mask, NULL);
}
#define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
#define mix(a, b, c) \
{ \
a -= c; \
a ^= rot(c, 4); \
c += b; \
b -= a; \
b ^= rot(a, 6); \
a += c; \
c -= b; \
c ^= rot(b, 8); \
b += a; \
a -= c; \
a ^= rot(c, 16); \
c += b; \
b -= a; \
b ^= rot(a, 19); \
a += c; \
c -= b; \
c ^= rot(b, 4); \
b += a; \
}
#define final(a, b, c) \
{ \
c ^= b; \
c -= rot(b, 14); \
a ^= c; \
a -= rot(c, 11); \
b ^= a; \
b -= rot(a, 25); \
c ^= b; \
c -= rot(b, 16); \
a ^= c; \
a -= rot(c, 4); \
b ^= a; \
b -= rot(a, 14); \
c ^= b; \
c -= rot(b, 24); \
}
uint32_t hash(uint32_t a, uint32_t b,uint32_t c, uint32_t d) {
uint32_t i1 = a, i2 = b, i3 = c, i4 = d;
mix(i1,i2,i3);
mix(i2,i3,i4);
mix(i3,i4,i1);
final(i4,i1,i2);
return i2;
}
uint32_t hash64(uint64_t x) {
uint32_t a = (uint32_t)(x & UMAX_BITS(32));
uint32_t b = (uint32_t)(rot(x, 16) & UMAX_BITS(32));
uint32_t c = (uint32_t)(rot(x,32) & UMAX_BITS(32));
uint32_t d = (uint32_t)(rot(x, 48) & UMAX_BITS(32));
return hash(a,b,c,d);
}
uint32_t hash_string(const char *key) {
uint32_t hc = 5381;
int c;
while ((c = *key ++)) {
hc = ((hc << 5) + hc) + c;
}
return hc;
}