src/hash_key.c

Fri, 12 Apr 2024 21:48:12 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 12 Apr 2024 21:48:12 +0200
changeset 849
edb9f875b7f9
parent 690
2c2304622981
permissions
-rw-r--r--

improves interface of cx_sprintf() variants

universe@563 1 /*
universe@563 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@563 3 *
universe@563 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@563 5 *
universe@563 6 * Redistribution and use in source and binary forms, with or without
universe@563 7 * modification, are permitted provided that the following conditions are met:
universe@563 8 *
universe@563 9 * 1. Redistributions of source code must retain the above copyright
universe@563 10 * notice, this list of conditions and the following disclaimer.
universe@563 11 *
universe@563 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@563 13 * notice, this list of conditions and the following disclaimer in the
universe@563 14 * documentation and/or other materials provided with the distribution.
universe@563 15 *
universe@563 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@563 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@563 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@563 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@563 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@563 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@563 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@563 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@563 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@563 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@563 26 * POSSIBILITY OF SUCH DAMAGE.
universe@563 27 */
universe@563 28
universe@563 29 #include "cx/hash_key.h"
universe@563 30 #include <string.h>
universe@563 31
universe@563 32 void cx_hash_murmur(CxHashKey *key) {
universe@690 33 unsigned char const *data = key->data;
universe@604 34 if (data == NULL) {
universe@628 35 // extension: special value for NULL
universe@604 36 key->hash = 1574210520u;
universe@604 37 return;
universe@604 38 }
universe@563 39 size_t len = key->len;
universe@563 40
universe@563 41 unsigned m = 0x5bd1e995;
universe@563 42 unsigned r = 24;
universe@563 43 unsigned h = 25 ^ len;
universe@563 44 unsigned i = 0;
universe@563 45 while (len >= 4) {
universe@563 46 unsigned k = data[i + 0] & 0xFF;
universe@563 47 k |= (data[i + 1] & 0xFF) << 8;
universe@563 48 k |= (data[i + 2] & 0xFF) << 16;
universe@563 49 k |= (data[i + 3] & 0xFF) << 24;
universe@563 50
universe@563 51 k *= m;
universe@563 52 k ^= k >> r;
universe@563 53 k *= m;
universe@563 54
universe@563 55 h *= m;
universe@563 56 h ^= k;
universe@563 57
universe@563 58 i += 4;
universe@563 59 len -= 4;
universe@563 60 }
universe@563 61
universe@563 62 switch (len) {
universe@563 63 case 3:
universe@563 64 h ^= (data[i + 2] & 0xFF) << 16;
universe@563 65 __attribute__((__fallthrough__));
universe@563 66 case 2:
universe@563 67 h ^= (data[i + 1] & 0xFF) << 8;
universe@563 68 __attribute__((__fallthrough__));
universe@563 69 case 1:
universe@563 70 h ^= (data[i + 0] & 0xFF);
universe@563 71 h *= m;
universe@563 72 __attribute__((__fallthrough__));
universe@628 73 default: // do nothing
universe@678 74 ;
universe@563 75 }
universe@563 76
universe@563 77 h ^= h >> 13;
universe@563 78 h *= m;
universe@563 79 h ^= h >> 15;
universe@563 80
universe@563 81 key->hash = h;
universe@563 82 }
universe@563 83
universe@563 84 CxHashKey cx_hash_key_str(char const *str) {
universe@563 85 CxHashKey key;
universe@690 86 key.data = str;
universe@604 87 key.len = str == NULL ? 0 : strlen(str);
universe@563 88 cx_hash_murmur(&key);
universe@563 89 return key;
universe@563 90 }
universe@563 91
universe@563 92 CxHashKey cx_hash_key_bytes(
universe@563 93 unsigned char const *bytes,
universe@563 94 size_t len
universe@563 95 ) {
universe@563 96 CxHashKey key;
universe@690 97 key.data = bytes;
universe@563 98 key.len = len;
universe@563 99 cx_hash_murmur(&key);
universe@563 100 return key;
universe@563 101 }
universe@563 102
universe@563 103 CxHashKey cx_hash_key(
universe@603 104 void const *obj,
universe@563 105 size_t len
universe@563 106 ) {
universe@563 107 CxHashKey key;
universe@690 108 key.data = obj;
universe@563 109 key.len = len;
universe@563 110 cx_hash_murmur(&key);
universe@563 111 return key;
universe@563 112 }

mercurial