Thu, 08 Jan 2026 18:06:04 +0100
update to ucx version 4.0
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | * |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | * |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | * |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | /** |
|
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
29 | * @file hash_key.h |
|
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
30 | * @brief Interface for map implementations. |
|
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
31 | * @author Mike Becker |
|
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
32 | * @author Olaf Wintermann |
|
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
33 | * @copyright 2-Clause BSD License |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34 | */ |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35 | |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
36 | |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
37 | #ifndef UCX_HASH_KEY_H |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
38 | #define UCX_HASH_KEY_H |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39 | |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
40 | #include "common.h" |
|
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
41 | #include "string.h" |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
42 | |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
43 | /** Internal structure for a key within a hash map. */ |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
44 | struct cx_hash_key_s { |
| 112 | 45 | /** |
| 46 | * The key data. | |
| 47 | * May be NULL when the hash is collision-free. | |
| 48 | */ | |
|
49
2f71f4ee247a
update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
49 | const void *data; |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
50 | /** |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
51 | * The key data length. |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
52 | */ |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
53 | size_t len; |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
54 | /** The hash value of the key data. */ |
| 112 | 55 | uint64_t hash; |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
56 | }; |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
57 | |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
58 | /** |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
59 | * Type for a hash key. |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
60 | */ |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
61 | typedef struct cx_hash_key_s CxHashKey; |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
62 | |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
63 | /** |
|
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
64 | * Computes a murmur2 32-bit hash. |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
65 | * |
|
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
66 | * You need to initialize @c data and @c len in the key struct. |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
67 | * The hash is then directly written to that struct. |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
68 | * |
|
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
69 | * Usually you should not need this function. |
|
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
70 | * Use cx_hash_key(), instead. |
|
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
71 | * |
|
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
72 | * @note If @c data is @c NULL, the hash is defined as 1574210520. |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
73 | * |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
74 | * @param key the key, the hash shall be computed for |
|
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
75 | * @see cx_hash_key() |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
76 | */ |
|
118
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
77 | CX_EXTERN CX_NONNULL |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
78 | void cx_hash_murmur(CxHashKey *key); |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
79 | |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
80 | /** |
| 112 | 81 | * Mixes up a 32-bit integer to be used as a hash. |
| 82 | * | |
| 83 | * This function produces no collisions and has a good statistical distribution. | |
| 84 | * | |
| 85 | * @param x the integer | |
| 86 | * @return the hash | |
| 87 | */ | |
|
118
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
88 | CX_INLINE |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
89 | uint32_t cx_hash_u32(uint32_t x) { |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
90 | x = ((x >> 16) ^ x) * 0x45d9f3bu; |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
91 | x = ((x >> 16) ^ x) * 0x45d9f3bu; |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
92 | x = (x >> 16) ^ x; |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
93 | return x; |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
94 | } |
| 112 | 95 | |
| 96 | /** | |
| 97 | * Mixes up a 64-bit integer to be used as a hash. | |
| 98 | * | |
| 99 | * This function produces no collisions and has a good statistical distribution. | |
| 100 | * | |
| 101 | * @param x the integer | |
| 102 | * @return the hash | |
| 103 | */ | |
|
118
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
104 | CX_INLINE |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
105 | uint64_t cx_hash_u64(uint64_t x){ |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
106 | x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9); |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
107 | x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb); |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
108 | x = x ^ (x >> 31); |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
109 | return x; |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
110 | } |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
111 | |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
112 | /** |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
113 | * Computes a hash key for an arbitrary object. |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
114 | * |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
115 | * The computation uses the in-memory representation that might not be |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
116 | * the same on different platforms. Therefore, this hash should not be |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
117 | * used for data exchange with different machines. |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
118 | * |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
119 | * @param obj a pointer to an arbitrary object |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
120 | * @param len the length of the object in memory |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
121 | * @return the hash key |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
122 | */ |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
123 | CX_EXTERN CX_NODISCARD CX_ACCESS_R(1, 2) |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
124 | CxHashKey cx_hash_key(const void *obj, size_t len); |
| 112 | 125 | |
| 126 | /** | |
| 127 | * Computes a hash key from a 32-bit integer. | |
| 128 | * | |
| 129 | * @param x the integer | |
| 130 | * @return the hash key | |
| 131 | */ | |
|
118
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
132 | CX_NODISCARD CX_INLINE |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
133 | CxHashKey cx_hash_key_u32(uint32_t x) { |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
134 | CxHashKey key; |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
135 | key.data = NULL; |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
136 | key.len = 0; |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
137 | key.hash = cx_hash_u32(x); |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
138 | return key; |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
139 | } |
| 112 | 140 | |
| 141 | /** | |
| 142 | * Computes a hash key from a 64-bit integer. | |
| 143 | * | |
| 144 | * @param x the integer | |
| 145 | * @return the hash key | |
| 146 | */ | |
|
118
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
147 | CX_NODISCARD CX_INLINE |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
148 | CxHashKey cx_hash_key_u64(uint64_t x) { |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
149 | CxHashKey key; |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
150 | key.data = NULL; |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
151 | key.len = 0; |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
152 | key.hash = cx_hash_u64(x); |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
153 | return key; |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
154 | } |
| 112 | 155 | |
| 156 | /** | |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
157 | * Computes a hash key from a string. |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
158 | * |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
159 | * The string needs to be zero-terminated. |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
160 | * |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
161 | * @param str the string |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
162 | * @return the hash key |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
163 | */ |
|
118
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
164 | CX_NODISCARD CX_CSTR_ARG(1) CX_INLINE |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
165 | CxHashKey cx_hash_key_str(const char *str) { |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
166 | return cx_hash_key((const void*)str, str == NULL ? 0 : strlen(str)); |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
167 | } |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
168 | |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
169 | /** |
| 112 | 170 | * Computes a hash key from a string. |
| 171 | * | |
| 172 | * Use this function when the string is represented | |
| 173 | * as an unsigned char array. | |
| 174 | * | |
| 175 | * The string needs to be zero-terminated. | |
| 176 | * | |
| 177 | * @param str the string | |
| 178 | * @return the hash key | |
| 179 | */ | |
|
118
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
180 | CX_NODISCARD CX_CSTR_ARG(1) CX_INLINE |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
181 | CxHashKey cx_hash_key_ustr(const unsigned char *str) { |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
182 | return cx_hash_key((const void*)str, str == NULL ? 0 : strlen((const char*)str)); |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
183 | } |
| 112 | 184 | |
| 185 | /** | |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
186 | * Computes a hash key from a byte array. |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
187 | * |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
188 | * @param bytes the array |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
189 | * @param len the length |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
190 | * @return the hash key |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
191 | */ |
|
118
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
192 | CX_NODISCARD CX_ACCESS_R(1, 2) CX_INLINE |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
193 | CxHashKey cx_hash_key_bytes(const unsigned char *bytes, size_t len) { |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
194 | return cx_hash_key((const void*)bytes, len); |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
195 | } |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
196 | |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
197 | /** |
|
118
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
198 | * Computes a hash key from a UCX string. |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
199 | * |
|
118
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
200 | * @param str the string |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
201 | * @return the hash key |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
202 | */ |
|
118
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
203 | CX_NODISCARD CX_INLINE |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
204 | CxHashKey cx_hash_key_cxstr(cxstring str) { |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
205 | return cx_hash_key((void*)str.ptr, str.length); |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
206 | } |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
207 | |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
208 | /** |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
209 | * Computes a hash key from a UCX string. |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
210 | * |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
211 | * @param str the string |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
212 | * @return the hash key |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
213 | */ |
|
118
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
214 | CX_NODISCARD CX_INLINE |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
215 | CxHashKey cx_hash_key_mutstr(cxmutstr str) { |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
216 | return cx_hash_key((void*)str.ptr, str.length); |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
217 | } |
| 112 | 218 | |
| 219 | /** | |
| 220 | * The identity function for the CX_HASH_KEY() macro. | |
| 221 | * You should never need to use this manually. | |
| 222 | * | |
| 223 | * @param key the key | |
|
118
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
224 | * @return a copy of the key (not the data) |
| 112 | 225 | */ |
|
118
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
226 | CX_NODISCARD CX_INLINE |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
227 | CxHashKey cx_hash_key_identity(CxHashKey key) { |
| 112 | 228 | return key; |
| 229 | } | |
| 230 | ||
|
118
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
231 | /** |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
232 | * The dereference function for the CX_HASH_KEY() macro. |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
233 | * You should never need to use this manually. |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
234 | * |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
235 | * @param key a pointer to a key |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
236 | * @return a copy of the key (not the data) |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
237 | */ |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
238 | CX_NODISCARD CX_INLINE |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
239 | CxHashKey cx_hash_key_deref(const CxHashKey *key) { |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
240 | return *key; |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
241 | } |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
242 | |
| 112 | 243 | #ifndef __cplusplus |
| 244 | /** | |
| 245 | * Creates a hash key from any of the supported types with implicit length. | |
| 246 | * | |
|
118
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
247 | * Does nothing when passing a CxHashKey and dereferences CxHashKey pointers. |
| 112 | 248 | * |
| 249 | * Supported types are UCX strings, zero-terminated C strings, | |
| 250 | * and 32-bit or 64-bit unsigned integers. | |
| 251 | * | |
| 252 | * @param key the key data | |
| 253 | * @returns the @c CxHashKey | |
| 254 | */ | |
| 255 | #define CX_HASH_KEY(key) _Generic((key), \ | |
|
118
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
256 | CxHashKey*: cx_hash_key_deref, \ |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
257 | const CxHashKey*: cx_hash_key_deref, \ |
| 112 | 258 | CxHashKey: cx_hash_key_identity, \ |
| 259 | cxstring: cx_hash_key_cxstr, \ | |
| 260 | cxmutstr: cx_hash_key_mutstr, \ | |
| 261 | char*: cx_hash_key_str, \ | |
| 262 | const char*: cx_hash_key_str, \ | |
| 263 | unsigned char*: cx_hash_key_ustr, \ | |
| 264 | const unsigned char*: cx_hash_key_ustr, \ | |
| 265 | uint32_t: cx_hash_key_u32, \ | |
| 266 | uint64_t: cx_hash_key_u64) \ | |
| 267 | (key) | |
| 268 | #endif // __cplusplus | |
| 269 | ||
| 270 | /** | |
| 271 | * Compare function for hash keys. | |
| 272 | * | |
| 113 | 273 | * The pointers are untyped to be compatible with the cx_compare_func signature. |
| 274 | * | |
| 275 | * @param left (@c CxHashKey*) the first key | |
| 276 | * @param right (@c CxHashKey*) the second key | |
| 112 | 277 | * @return zero when the keys equal, non-zero when they differ |
| 278 | */ | |
|
118
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
279 | CX_EXTERN CX_NODISCARD CX_NONNULL |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
280 | int cx_hash_key_cmp(const void *left, const void *right); |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
281 | |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
282 | /** |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
283 | * Interprets the key data as a string and returns it. |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
284 | * |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
285 | * @param key the key |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
286 | * @return the key data as a string |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
287 | */ |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
288 | CX_EXTERN |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
289 | cxstring cx_hash_key_as_string(const CxHashKey *key); |
| 112 | 290 | |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
291 | #ifdef __cplusplus |
| 112 | 292 | // ---------------------------------------------------------- |
| 293 | // Overloads of CX_HASH_KEY (the C++ version of a _Generic) | |
| 294 | // ---------------------------------------------------------- | |
| 295 | ||
| 113 | 296 | CX_CPPDECL CxHashKey CX_HASH_KEY(CxHashKey key) { |
| 112 | 297 | return key; |
| 298 | } | |
| 299 | ||
|
118
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
300 | CX_CPPDECL CxHashKey CX_HASH_KEY(const CxHashKey *key) { |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
301 | return *key; |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
302 | } |
|
6d0da97105d8
update to ucx version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
113
diff
changeset
|
303 | |
| 113 | 304 | CX_CPPDECL CxHashKey CX_HASH_KEY(cxstring str) { |
| 112 | 305 | return cx_hash_key_cxstr(str); |
| 306 | } | |
| 307 | ||
| 113 | 308 | CX_CPPDECL CxHashKey CX_HASH_KEY(cxmutstr str) { |
| 112 | 309 | return cx_hash_key_mutstr(str); |
| 310 | } | |
| 311 | ||
| 113 | 312 | CX_CPPDECL CxHashKey CX_HASH_KEY(const char *str) { |
| 112 | 313 | return cx_hash_key_str(str); |
| 314 | } | |
| 315 | ||
| 113 | 316 | CX_CPPDECL CxHashKey CX_HASH_KEY(const unsigned char *str) { |
| 112 | 317 | return cx_hash_key_ustr(str); |
| 318 | } | |
| 319 | ||
| 113 | 320 | CX_CPPDECL CxHashKey CX_HASH_KEY(uint32_t key) { |
| 112 | 321 | return cx_hash_key_u32(key); |
| 322 | } | |
| 323 | ||
| 113 | 324 | CX_CPPDECL CxHashKey CX_HASH_KEY(uint64_t key) { |
| 112 | 325 | return cx_hash_key_u64(key); |
| 326 | } | |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
327 | #endif |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
328 | |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
329 | #endif // UCX_HASH_KEY_H |