| 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. */ |
| 68 * |
69 * |
| 69 * \note If \c data is \c NULL, the hash is defined as 1574210520. |
70 * \note If \c data is \c NULL, the hash is defined as 1574210520. |
| 70 * |
71 * |
| 71 * @param key the key, the hash shall be computed for |
72 * @param key the key, the hash shall be computed for |
| 72 */ |
73 */ |
| |
74 cx_attr_nonnull |
| 73 void cx_hash_murmur(CxHashKey *key); |
75 void cx_hash_murmur(CxHashKey *key); |
| 74 |
76 |
| 75 /** |
77 /** |
| 76 * Computes a hash key from a string. |
78 * Computes a hash key from a string. |
| 77 * |
79 * |
| 78 * The string needs to be zero-terminated. |
80 * The string needs to be zero-terminated. |
| 79 * |
81 * |
| 80 * @param str the string |
82 * @param str the string |
| 81 * @return the hash key |
83 * @return the hash key |
| 82 */ |
84 */ |
| 83 __attribute__((__warn_unused_result__)) |
85 cx_attr_nodiscard |
| 84 CxHashKey cx_hash_key_str(char const *str); |
86 cx_attr_cstr_arg(1) |
| |
87 CxHashKey cx_hash_key_str(const char *str); |
| 85 |
88 |
| 86 /** |
89 /** |
| 87 * Computes a hash key from a byte array. |
90 * Computes a hash key from a byte array. |
| 88 * |
91 * |
| 89 * @param bytes the array |
92 * @param bytes the array |
| 90 * @param len the length |
93 * @param len the length |
| 91 * @return the hash key |
94 * @return the hash key |
| 92 */ |
95 */ |
| 93 __attribute__((__warn_unused_result__)) |
96 cx_attr_nodiscard |
| |
97 cx_attr_access_r(1, 2) |
| 94 CxHashKey cx_hash_key_bytes( |
98 CxHashKey cx_hash_key_bytes( |
| 95 unsigned char const *bytes, |
99 const unsigned char *bytes, |
| 96 size_t len |
100 size_t len |
| 97 ); |
101 ); |
| 98 |
102 |
| 99 /** |
103 /** |
| 100 * Computes a hash key for an arbitrary object. |
104 * Computes a hash key for an arbitrary object. |
| 105 * |
109 * |
| 106 * @param obj a pointer to an arbitrary object |
110 * @param obj a pointer to an arbitrary object |
| 107 * @param len the length of object in memory |
111 * @param len the length of object in memory |
| 108 * @return the hash key |
112 * @return the hash key |
| 109 */ |
113 */ |
| 110 __attribute__((__warn_unused_result__)) |
114 cx_attr_nodiscard |
| |
115 cx_attr_access_r(1, 2) |
| 111 CxHashKey cx_hash_key( |
116 CxHashKey cx_hash_key( |
| 112 void const *obj, |
117 const void *obj, |
| 113 size_t len |
118 size_t len |
| 114 ); |
119 ); |
| 115 |
120 |
| 116 /** |
121 /** |
| 117 * Computes a hash key from a UCX string. |
122 * Computes a hash key from a UCX string. |
| 118 * |
123 * |
| 119 * @param str the string |
124 * @param str the string |
| 120 * @return the hash key |
125 * @return the hash key |
| 121 */ |
126 */ |
| 122 #define cx_hash_key_cxstr(str) cx_hash_key((void*)(str).ptr, (str).length) |
127 cx_attr_nodiscard |
| |
128 static inline CxHashKey cx_hash_key_cxstr(cxstring str) { |
| |
129 return cx_hash_key(str.ptr, str.length); |
| |
130 } |
| |
131 |
| |
132 /** |
| |
133 * Computes a hash key from a UCX string. |
| |
134 * |
| |
135 * @param str the string |
| |
136 * @return the hash key |
| |
137 */ |
| |
138 #define cx_hash_key_cxstr(str) cx_hash_key_cxstr(cx_strcast(str)) |
| 123 |
139 |
| 124 #ifdef __cplusplus |
140 #ifdef __cplusplus |
| 125 } // extern "C" |
141 } // extern "C" |
| 126 #endif |
142 #endif |
| 127 |
143 |