commit 5b3c7d4

default  ·  2023-07-03 14:41:51 +0000 UTC
parent 7eeeeb3
Backport from xs.
3 files changed,  +20, -16
M xs.h
M xs.h
+18, -0
 1@@ -122,6 +122,7 @@ xs_str *xs_hex_enc(const xs_val *data, int size);
 2 xs_val *xs_hex_dec(const xs_str *hex, int *size);
 3 int xs_is_hex(const char *str);
 4 
 5+unsigned int xs_hash_func(const char *data, int size);
 6 
 7 #ifdef XS_ASSERT
 8 #include <assert.h>
 9@@ -136,6 +137,8 @@ extern xs_val xs_stock_null[];
10 extern xs_val xs_stock_true[];
11 extern xs_val xs_stock_false[];
12 
13+#define xs_return(v) xs_val *__r = v; v = NULL; return __r
14+
15 
16 #ifdef XS_IMPLEMENTATION
17 
18@@ -1186,6 +1189,21 @@ int xs_is_hex(const char *str)
19 }
20 
21 
22+unsigned int xs_hash_func(const char *data, int size)
23+/* a general purpose hashing function */
24+{
25+    unsigned int hash = 0x666;
26+    int n;
27+
28+    for (n = 0; n < size; n++) {
29+        hash ^= data[n];
30+        hash *= 111111111;
31+    }
32+
33+    return hash ^ hash >> 16;
34+}
35+
36+
37 #endif /* XS_IMPLEMENTATION */
38 
39 #endif /* _XS_H */
+1, -15
 1@@ -51,26 +51,12 @@ void xs_set_free(xs_set *s)
 2 }
 3 
 4 
 5-static unsigned int _calc_hash(const char *data, int size)
 6-{
 7-    unsigned int hash = 0x666;
 8-    int n;
 9-
10-    for (n = 0; n < size; n++) {
11-        hash ^= data[n];
12-        hash *= 111111111;
13-    }
14-
15-    return hash ^ hash >> 16;
16-}
17-
18-
19 static int _store_hash(xs_set *s, const char *data, int value)
20 {
21     unsigned int hash, i;
22     int sz = xs_size(data);
23 
24-    hash = _calc_hash(data, sz);
25+    hash = xs_hash_func(data, sz);
26 
27     while (s->hash[(i = hash % s->elems)]) {
28         /* get the pointer to the stored data */
+1, -1
1@@ -1 +1 @@
2-/* 567d70ecbe16b2358873b8bc971a6e092c3c0074 */
3+/* dada4e932155b621ab6d66820f62ea9cf09cbb70 */