Wed, 18 May 2022 16:26:32 +0200
#189 declare basic map functions
universe@549 | 1 | /* |
universe@549 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@549 | 3 | * |
universe@549 | 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
universe@549 | 5 | * |
universe@549 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@549 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@549 | 8 | * |
universe@549 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@549 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@549 | 11 | * |
universe@549 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@549 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@549 | 14 | * documentation and/or other materials provided with the distribution. |
universe@549 | 15 | * |
universe@549 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@549 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@549 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@549 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@549 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@549 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@549 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@549 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@549 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@549 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@549 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@549 | 27 | */ |
universe@549 | 28 | |
universe@549 | 29 | #include <string.h> |
universe@549 | 30 | #include "cx/hash_map.h" |
universe@549 | 31 | |
universe@549 | 32 | /** |
universe@549 | 33 | * Computes a murmur hash-2. |
universe@549 | 34 | * |
universe@549 | 35 | * @param data the data to hash |
universe@549 | 36 | * @param len the length of the data |
universe@549 | 37 | * @return the murmur hash-2 of the data |
universe@549 | 38 | */ |
universe@549 | 39 | static unsigned cx_hash_map_murmur( |
universe@549 | 40 | unsigned char const *data, |
universe@549 | 41 | size_t len |
universe@549 | 42 | ) { |
universe@549 | 43 | unsigned m = 0x5bd1e995; |
universe@549 | 44 | unsigned r = 24; |
universe@549 | 45 | unsigned h = 25 ^ len; |
universe@549 | 46 | unsigned i = 0; |
universe@549 | 47 | while (len >= 4) { |
universe@549 | 48 | unsigned k = data[i + 0] & 0xFF; |
universe@549 | 49 | k |= (data[i + 1] & 0xFF) << 8; |
universe@549 | 50 | k |= (data[i + 2] & 0xFF) << 16; |
universe@549 | 51 | k |= (data[i + 3] & 0xFF) << 24; |
universe@549 | 52 | |
universe@549 | 53 | k *= m; |
universe@549 | 54 | k ^= k >> r; |
universe@549 | 55 | k *= m; |
universe@549 | 56 | |
universe@549 | 57 | h *= m; |
universe@549 | 58 | h ^= k; |
universe@549 | 59 | |
universe@549 | 60 | i += 4; |
universe@549 | 61 | len -= 4; |
universe@549 | 62 | } |
universe@549 | 63 | |
universe@549 | 64 | switch (len) { |
universe@549 | 65 | case 3: |
universe@549 | 66 | h ^= (data[i + 2] & 0xFF) << 16; |
universe@549 | 67 | __attribute__((__fallthrough__)); |
universe@549 | 68 | case 2: |
universe@549 | 69 | h ^= (data[i + 1] & 0xFF) << 8; |
universe@549 | 70 | __attribute__((__fallthrough__)); |
universe@549 | 71 | case 1: |
universe@549 | 72 | h ^= (data[i + 0] & 0xFF); |
universe@549 | 73 | h *= m; |
universe@549 | 74 | __attribute__((__fallthrough__)); |
universe@549 | 75 | default: |
universe@549 | 76 | /* do nothing */; |
universe@549 | 77 | } |
universe@549 | 78 | |
universe@549 | 79 | h ^= h >> 13; |
universe@549 | 80 | h *= m; |
universe@549 | 81 | h ^= h >> 15; |
universe@549 | 82 | |
universe@549 | 83 | return h; |
universe@549 | 84 | } |
universe@549 | 85 | |
universe@549 | 86 | |
universe@549 | 87 | /** |
universe@549 | 88 | * Creates a hash map key based on the given data. |
universe@549 | 89 | * |
universe@549 | 90 | * This function implicitly computes the hash. |
universe@549 | 91 | * |
universe@549 | 92 | * @attention The data will be copied to the key structure. |
universe@549 | 93 | * |
universe@549 | 94 | * @param data the data for the key |
universe@549 | 95 | * @return the computed key |
universe@549 | 96 | */ |
universe@549 | 97 | static struct cx_hash_map_key_s cx_hash_map_key(CxDataPtr data) { |
universe@549 | 98 | unsigned char *target = malloc(data.len); |
universe@549 | 99 | memcpy(target, data.data, data.len); |
universe@549 | 100 | struct cx_hash_map_key_s key; |
universe@549 | 101 | key.data.data = target; |
universe@549 | 102 | key.data.len = data.len; |
universe@549 | 103 | key.hash = cx_hash_map_murmur(target, data.len); |
universe@549 | 104 | return key; |
universe@549 | 105 | } |