24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
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 |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
27 */ |
27 */ |
28 /** |
28 /** |
29 * \file hash_key.h |
29 * @file hash_key.h |
30 * \brief Interface for map implementations. |
30 * @brief Interface for map implementations. |
31 * \author Mike Becker |
31 * @author Mike Becker |
32 * \author Olaf Wintermann |
32 * @author Olaf Wintermann |
33 * \copyright 2-Clause BSD License |
33 * @copyright 2-Clause BSD License |
34 */ |
34 */ |
35 |
35 |
36 |
36 |
37 #ifndef UCX_HASH_KEY_H |
37 #ifndef UCX_HASH_KEY_H |
38 #define UCX_HASH_KEY_H |
38 #define UCX_HASH_KEY_H |
39 |
39 |
40 #include "common.h" |
40 #include "common.h" |
|
41 #include "string.h" |
41 |
42 |
42 #ifdef __cplusplus |
43 #ifdef __cplusplus |
43 extern "C" { |
44 extern "C" { |
44 #endif |
45 #endif |
45 |
46 |
46 /** Internal structure for a key within a hash map. */ |
47 /** Internal structure for a key within a hash map. */ |
47 struct cx_hash_key_s { |
48 struct cx_hash_key_s { |
48 /** The key data. */ |
49 /** The key data. */ |
49 void const *data; |
50 const void *data; |
50 /** |
51 /** |
51 * The key data length. |
52 * The key data length. |
52 */ |
53 */ |
53 size_t len; |
54 size_t len; |
54 /** The hash value of the key data. */ |
55 /** The hash value of the key data. */ |
59 * Type for a hash key. |
60 * Type for a hash key. |
60 */ |
61 */ |
61 typedef struct cx_hash_key_s CxHashKey; |
62 typedef struct cx_hash_key_s CxHashKey; |
62 |
63 |
63 /** |
64 /** |
64 * Computes a murmur2 32 bit hash. |
65 * Computes a murmur2 32-bit hash. |
65 * |
66 * |
66 * You need to initialize \c data and \c len in the key struct. |
67 * You need to initialize @c data and @c len in the key struct. |
67 * The hash is then directly written to that struct. |
68 * The hash is then directly written to that struct. |
68 * |
69 * |
69 * \note If \c data is \c NULL, the hash is defined as 1574210520. |
70 * Usually you should not need this function. |
|
71 * Use cx_hash_key(), instead. |
|
72 * |
|
73 * @note If @c data is @c NULL, the hash is defined as 1574210520. |
70 * |
74 * |
71 * @param key the key, the hash shall be computed for |
75 * @param key the key, the hash shall be computed for |
|
76 * @see cx_hash_key() |
72 */ |
77 */ |
|
78 cx_attr_nonnull |
73 void cx_hash_murmur(CxHashKey *key); |
79 void cx_hash_murmur(CxHashKey *key); |
74 |
80 |
75 /** |
81 /** |
76 * Computes a hash key from a string. |
82 * Computes a hash key from a string. |
77 * |
83 * |
78 * The string needs to be zero-terminated. |
84 * The string needs to be zero-terminated. |
79 * |
85 * |
80 * @param str the string |
86 * @param str the string |
81 * @return the hash key |
87 * @return the hash key |
82 */ |
88 */ |
83 __attribute__((__warn_unused_result__)) |
89 cx_attr_nodiscard |
84 CxHashKey cx_hash_key_str(char const *str); |
90 cx_attr_cstr_arg(1) |
|
91 CxHashKey cx_hash_key_str(const char *str); |
85 |
92 |
86 /** |
93 /** |
87 * Computes a hash key from a byte array. |
94 * Computes a hash key from a byte array. |
88 * |
95 * |
89 * @param bytes the array |
96 * @param bytes the array |
90 * @param len the length |
97 * @param len the length |
91 * @return the hash key |
98 * @return the hash key |
92 */ |
99 */ |
93 __attribute__((__warn_unused_result__)) |
100 cx_attr_nodiscard |
|
101 cx_attr_access_r(1, 2) |
94 CxHashKey cx_hash_key_bytes( |
102 CxHashKey cx_hash_key_bytes( |
95 unsigned char const *bytes, |
103 const unsigned char *bytes, |
96 size_t len |
104 size_t len |
97 ); |
105 ); |
98 |
106 |
99 /** |
107 /** |
100 * Computes a hash key for an arbitrary object. |
108 * Computes a hash key for an arbitrary object. |
105 * |
113 * |
106 * @param obj a pointer to an arbitrary object |
114 * @param obj a pointer to an arbitrary object |
107 * @param len the length of object in memory |
115 * @param len the length of object in memory |
108 * @return the hash key |
116 * @return the hash key |
109 */ |
117 */ |
110 __attribute__((__warn_unused_result__)) |
118 cx_attr_nodiscard |
|
119 cx_attr_access_r(1, 2) |
111 CxHashKey cx_hash_key( |
120 CxHashKey cx_hash_key( |
112 void const *obj, |
121 const void *obj, |
113 size_t len |
122 size_t len |
114 ); |
123 ); |
115 |
124 |
116 /** |
125 /** |
117 * Computes a hash key from a UCX string. |
126 * Computes a hash key from a UCX string. |
118 * |
127 * |
119 * @param str the string |
128 * @param str the string |
120 * @return the hash key |
129 * @return the hash key |
121 */ |
130 */ |
122 #define cx_hash_key_cxstr(str) cx_hash_key((void*)(str).ptr, (str).length) |
131 cx_attr_nodiscard |
|
132 static inline CxHashKey cx_hash_key_cxstr(cxstring str) { |
|
133 return cx_hash_key(str.ptr, str.length); |
|
134 } |
|
135 |
|
136 /** |
|
137 * Computes a hash key from a UCX string. |
|
138 * |
|
139 * @param str (@c cxstring or @c cxmutstr) the string |
|
140 * @return (@c CxHashKey) the hash key |
|
141 */ |
|
142 #define cx_hash_key_cxstr(str) cx_hash_key_cxstr(cx_strcast(str)) |
123 |
143 |
124 #ifdef __cplusplus |
144 #ifdef __cplusplus |
125 } // extern "C" |
145 } // extern "C" |
126 #endif |
146 #endif |
127 |
147 |