Backport from xs.

This commit is contained in:
default 2023-07-03 16:41:51 +02:00
parent 7eeeeb3022
commit 5b3c7d45bb
3 changed files with 20 additions and 16 deletions

18
xs.h
View file

@ -122,6 +122,7 @@ xs_str *xs_hex_enc(const xs_val *data, int size);
xs_val *xs_hex_dec(const xs_str *hex, int *size); xs_val *xs_hex_dec(const xs_str *hex, int *size);
int xs_is_hex(const char *str); int xs_is_hex(const char *str);
unsigned int xs_hash_func(const char *data, int size);
#ifdef XS_ASSERT #ifdef XS_ASSERT
#include <assert.h> #include <assert.h>
@ -136,6 +137,8 @@ extern xs_val xs_stock_null[];
extern xs_val xs_stock_true[]; extern xs_val xs_stock_true[];
extern xs_val xs_stock_false[]; extern xs_val xs_stock_false[];
#define xs_return(v) xs_val *__r = v; v = NULL; return __r
#ifdef XS_IMPLEMENTATION #ifdef XS_IMPLEMENTATION
@ -1186,6 +1189,21 @@ int xs_is_hex(const char *str)
} }
unsigned int xs_hash_func(const char *data, int size)
/* a general purpose hashing function */
{
unsigned int hash = 0x666;
int n;
for (n = 0; n < size; n++) {
hash ^= data[n];
hash *= 111111111;
}
return hash ^ hash >> 16;
}
#endif /* XS_IMPLEMENTATION */ #endif /* XS_IMPLEMENTATION */
#endif /* _XS_H */ #endif /* _XS_H */

View file

@ -51,26 +51,12 @@ void xs_set_free(xs_set *s)
} }
static unsigned int _calc_hash(const char *data, int size)
{
unsigned int hash = 0x666;
int n;
for (n = 0; n < size; n++) {
hash ^= data[n];
hash *= 111111111;
}
return hash ^ hash >> 16;
}
static int _store_hash(xs_set *s, const char *data, int value) static int _store_hash(xs_set *s, const char *data, int value)
{ {
unsigned int hash, i; unsigned int hash, i;
int sz = xs_size(data); int sz = xs_size(data);
hash = _calc_hash(data, sz); hash = xs_hash_func(data, sz);
while (s->hash[(i = hash % s->elems)]) { while (s->hash[(i = hash % s->elems)]) {
/* get the pointer to the stored data */ /* get the pointer to the stored data */

View file

@ -1 +1 @@
/* 567d70ecbe16b2358873b8bc971a6e092c3c0074 */ /* dada4e932155b621ab6d66820f62ea9cf09cbb70 */