Sun, 19 Oct 2025 21:20:08 +0200
update toolkit
|
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 | #ifdef __cplusplus |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
44 | extern "C" { |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
45 | #endif |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
46 | |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
47 | /** Internal structure for a key within a hash map. */ |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
48 | struct cx_hash_key_s { |
| 112 | 49 | /** |
| 50 | * The key data. | |
| 51 | * May be NULL when the hash is collision-free. | |
| 52 | */ | |
|
49
2f71f4ee247a
update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
53 | const void *data; |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
54 | /** |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
55 | * The key data length. |
|
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 | size_t len; |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
58 | /** The hash value of the key data. */ |
| 112 | 59 | uint64_t hash; |
|
0
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 | |
|
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 | * Type for a hash key. |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
64 | */ |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
65 | typedef struct cx_hash_key_s CxHashKey; |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
66 | |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
67 | /** |
|
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
68 | * Computes a murmur2 32-bit hash. |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
69 | * |
|
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
70 | * 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
|
71 | * The hash is then directly written to that struct. |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
72 | * |
|
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
73 | * Usually you should not need this function. |
|
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
74 | * Use cx_hash_key(), instead. |
|
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
75 | * |
|
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
76 | * @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
|
77 | * |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
78 | * @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
|
79 | * @see cx_hash_key() |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
80 | */ |
|
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
81 | cx_attr_nonnull |
|
102
64ded9f6a6c6
update libs (ucx, toolkit, libidav)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
82 | cx_attr_export |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
83 | void cx_hash_murmur(CxHashKey *key); |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
84 | |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
85 | /** |
| 112 | 86 | * Mixes up a 32-bit integer to be used as a hash. |
| 87 | * | |
| 88 | * This function produces no collisions and has a good statistical distribution. | |
| 89 | * | |
| 90 | * @param x the integer | |
| 91 | * @return the hash | |
| 92 | */ | |
| 93 | cx_attr_export | |
| 94 | uint32_t cx_hash_u32(uint32_t x); | |
| 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 | */ | |
| 104 | cx_attr_export | |
| 105 | uint64_t cx_hash_u64(uint64_t x); | |
| 106 | ||
| 107 | /** | |
| 108 | * Computes a hash key from a 32-bit integer. | |
| 109 | * | |
| 110 | * @param x the integer | |
| 111 | * @return the hash key | |
| 112 | */ | |
| 113 | cx_attr_nodiscard | |
| 114 | cx_attr_export | |
| 115 | CxHashKey cx_hash_key_u32(uint32_t x); | |
| 116 | ||
| 117 | /** | |
| 118 | * Computes a hash key from a 64-bit integer. | |
| 119 | * | |
| 120 | * @param x the integer | |
| 121 | * @return the hash key | |
| 122 | */ | |
| 123 | cx_attr_nodiscard | |
| 124 | cx_attr_export | |
| 125 | CxHashKey cx_hash_key_u64(uint64_t x); | |
| 126 | ||
| 127 | /** | |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
128 | * Computes a hash key from a string. |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
129 | * |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
130 | * The string needs to be zero-terminated. |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
131 | * |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
132 | * @param str the string |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
133 | * @return the hash key |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
134 | */ |
|
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
135 | cx_attr_nodiscard |
|
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
136 | cx_attr_cstr_arg(1) |
|
102
64ded9f6a6c6
update libs (ucx, toolkit, libidav)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
137 | cx_attr_export |
|
49
2f71f4ee247a
update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
138 | CxHashKey cx_hash_key_str(const char *str); |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
139 | |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
140 | /** |
| 112 | 141 | * Computes a hash key from a string. |
| 142 | * | |
| 143 | * Use this function when the string is represented | |
| 144 | * as an unsigned char array. | |
| 145 | * | |
| 146 | * The string needs to be zero-terminated. | |
| 147 | * | |
| 148 | * @param str the string | |
| 149 | * @return the hash key | |
| 150 | */ | |
| 151 | cx_attr_nodiscard | |
| 152 | cx_attr_cstr_arg(1) | |
| 153 | cx_attr_export | |
| 154 | static inline CxHashKey cx_hash_key_ustr(const unsigned char *str) { | |
| 155 | return cx_hash_key_str((const char*)str); | |
| 156 | } | |
| 157 | ||
| 158 | /** | |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
159 | * Computes a hash key from a byte array. |
|
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 bytes the array |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
162 | * @param len the length |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
163 | * @return the hash key |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
164 | */ |
|
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
165 | cx_attr_nodiscard |
|
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
166 | cx_attr_access_r(1, 2) |
|
102
64ded9f6a6c6
update libs (ucx, toolkit, libidav)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
167 | cx_attr_export |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
168 | CxHashKey cx_hash_key_bytes( |
|
49
2f71f4ee247a
update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
169 | const unsigned char *bytes, |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
170 | size_t len |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
171 | ); |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
172 | |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
173 | /** |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
174 | * Computes a hash key for an arbitrary object. |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
175 | * |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
176 | * The computation uses the in-memory representation that might not be |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
177 | * the same on different platforms. Therefore, this hash should not be |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
178 | * used for data exchange with different machines. |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
179 | * |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
180 | * @param obj a pointer to an arbitrary object |
| 112 | 181 | * @param len the length of the object in memory |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
182 | * @return the hash key |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
183 | */ |
|
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
184 | cx_attr_nodiscard |
|
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
185 | cx_attr_access_r(1, 2) |
|
102
64ded9f6a6c6
update libs (ucx, toolkit, libidav)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
101
diff
changeset
|
186 | cx_attr_export |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
187 | CxHashKey cx_hash_key( |
|
49
2f71f4ee247a
update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
2
diff
changeset
|
188 | const void *obj, |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
189 | size_t len |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
190 | ); |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
191 | |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
192 | /** |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
193 | * Computes a hash key from a UCX string. |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
194 | * |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
195 | * @param str the string |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
196 | * @return the hash key |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
197 | */ |
|
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
198 | cx_attr_nodiscard |
|
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
199 | static inline CxHashKey cx_hash_key_cxstr(cxstring str) { |
|
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
200 | return cx_hash_key(str.ptr, str.length); |
|
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
201 | } |
|
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
202 | |
|
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
203 | /** |
|
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
204 | * Computes a hash key from a UCX string. |
|
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
205 | * |
| 112 | 206 | * @param str the string |
| 207 | * @return the hash key | |
| 208 | */ | |
| 209 | cx_attr_nodiscard | |
| 210 | static inline CxHashKey cx_hash_key_mutstr(cxmutstr str) { | |
| 211 | return cx_hash_key(str.ptr, str.length); | |
| 212 | } | |
| 213 | ||
| 214 | /** | |
| 215 | * The identity function for the CX_HASH_KEY() macro. | |
| 216 | * You should never need to use this manually. | |
| 217 | * | |
| 218 | * @param key the key | |
| 219 | * @return a copy of the key | |
| 220 | */ | |
| 221 | cx_attr_nodiscard | |
| 222 | static inline CxHashKey cx_hash_key_identity(CxHashKey key) { | |
| 223 | return key; | |
| 224 | } | |
| 225 | ||
| 226 | #ifndef __cplusplus | |
| 227 | /** | |
| 228 | * Creates a hash key from any of the supported types with implicit length. | |
| 229 | * | |
| 230 | * Does nothing when passing a CxHashkey. | |
| 231 | * | |
| 232 | * Supported types are UCX strings, zero-terminated C strings, | |
| 233 | * and 32-bit or 64-bit unsigned integers. | |
| 234 | * | |
| 235 | * @param key the key data | |
| 236 | * @returns the @c CxHashKey | |
| 237 | */ | |
| 238 | #define CX_HASH_KEY(key) _Generic((key), \ | |
| 239 | CxHashKey: cx_hash_key_identity, \ | |
| 240 | cxstring: cx_hash_key_cxstr, \ | |
| 241 | cxmutstr: cx_hash_key_mutstr, \ | |
| 242 | char*: cx_hash_key_str, \ | |
| 243 | const char*: cx_hash_key_str, \ | |
| 244 | unsigned char*: cx_hash_key_ustr, \ | |
| 245 | const unsigned char*: cx_hash_key_ustr, \ | |
| 246 | uint32_t: cx_hash_key_u32, \ | |
| 247 | uint64_t: cx_hash_key_u64) \ | |
| 248 | (key) | |
| 249 | #endif // __cplusplus | |
| 250 | ||
| 251 | /** | |
| 252 | * Computes a hash key from a UCX string. | |
| 253 | * Convenience macro that accepts both cxstring and cxmutstr. | |
| 254 | * @deprecated use the CX_HASH_KEY() macro instead | |
|
101
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
255 | * @param str (@c cxstring or @c cxmutstr) the string |
|
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
256 | * @return (@c CxHashKey) the hash key |
|
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
257 | */ |
|
7b3a3130be44
update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
49
diff
changeset
|
258 | #define cx_hash_key_cxstr(str) cx_hash_key_cxstr(cx_strcast(str)) |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
259 | |
| 112 | 260 | /** |
| 261 | * Compare function for hash keys. | |
| 262 | * | |
| 263 | * @param left the first key | |
| 264 | * @param right the second key | |
| 265 | * @return zero when the keys equal, non-zero when they differ | |
| 266 | */ | |
| 267 | cx_attr_nodiscard | |
| 268 | cx_attr_nonnull | |
| 269 | cx_attr_export | |
| 270 | int cx_hash_key_cmp(const CxHashKey *left, const CxHashKey *right); | |
| 271 | ||
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
272 | #ifdef __cplusplus |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
273 | } // extern "C" |
| 112 | 274 | |
| 275 | // ---------------------------------------------------------- | |
| 276 | // Overloads of CX_HASH_KEY (the C++ version of a _Generic) | |
| 277 | // ---------------------------------------------------------- | |
| 278 | ||
| 279 | static inline CxHashKey CX_HASH_KEY(CxHashKey key) { | |
| 280 | return key; | |
| 281 | } | |
| 282 | ||
| 283 | static inline CxHashKey CX_HASH_KEY(cxstring str) { | |
| 284 | return cx_hash_key_cxstr(str); | |
| 285 | } | |
| 286 | ||
| 287 | static inline CxHashKey CX_HASH_KEY(cxmutstr str) { | |
| 288 | return cx_hash_key_mutstr(str); | |
| 289 | } | |
| 290 | ||
| 291 | static inline CxHashKey CX_HASH_KEY(const char *str) { | |
| 292 | return cx_hash_key_str(str); | |
| 293 | } | |
| 294 | ||
| 295 | static inline CxHashKey CX_HASH_KEY(const unsigned char *str) { | |
| 296 | return cx_hash_key_ustr(str); | |
| 297 | } | |
| 298 | ||
| 299 | static inline CxHashKey CX_HASH_KEY(uint32_t key) { | |
| 300 | return cx_hash_key_u32(key); | |
| 301 | } | |
| 302 | ||
| 303 | static inline CxHashKey CX_HASH_KEY(uint64_t key) { | |
| 304 | return cx_hash_key_u64(key); | |
| 305 | } | |
|
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
306 | #endif |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
307 | |
|
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
308 | #endif // UCX_HASH_KEY_H |