src/hash_map.c

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

mercurial