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 * \version 3.0 |
33 * @copyright 2-Clause BSD License |
34 * \copyright 2-Clause BSD License |
|
35 */ |
34 */ |
36 |
35 |
37 |
36 |
38 #ifndef UCX_HASH_KEY_H |
37 #ifndef UCX_HASH_KEY_H |
39 #define UCX_HASH_KEY_H |
38 #define UCX_HASH_KEY_H |
40 |
39 |
41 #include "common.h" |
40 #include "common.h" |
|
41 #include "string.h" |
42 |
42 |
43 #ifdef __cplusplus |
43 #ifdef __cplusplus |
44 extern "C" { |
44 extern "C" { |
45 #endif |
45 #endif |
46 |
46 |
47 /** Internal structure for a key within a hash map. */ |
47 /** Internal structure for a key within a hash map. */ |
48 struct cx_hash_key_s { |
48 struct cx_hash_key_s { |
49 /** The key data. */ |
49 /** The key data. */ |
50 void const *data; |
50 const void *data; |
51 /** |
51 /** |
52 * The key data length. |
52 * The key data length. |
53 */ |
53 */ |
54 size_t len; |
54 size_t len; |
55 /** The hash value of the key data. */ |
55 /** The hash value of the key data. */ |
60 * Type for a hash key. |
60 * Type for a hash key. |
61 */ |
61 */ |
62 typedef struct cx_hash_key_s CxHashKey; |
62 typedef struct cx_hash_key_s CxHashKey; |
63 |
63 |
64 /** |
64 /** |
65 * Computes a murmur2 32 bit hash. |
65 * Computes a murmur2 32-bit hash. |
66 * |
66 * |
67 * 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. |
68 * The hash is then directly written to that struct. |
68 * The hash is then directly written to that struct. |
69 * |
69 * |
70 * \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. |
71 * |
74 * |
72 * @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() |
73 */ |
77 */ |
|
78 cx_attr_nonnull |
|
79 cx_attr_export |
74 void cx_hash_murmur(CxHashKey *key); |
80 void cx_hash_murmur(CxHashKey *key); |
75 |
81 |
76 /** |
82 /** |
77 * Computes a hash key from a string. |
83 * Computes a hash key from a string. |
78 * |
84 * |
79 * The string needs to be zero-terminated. |
85 * The string needs to be zero-terminated. |
80 * |
86 * |
81 * @param str the string |
87 * @param str the string |
82 * @return the hash key |
88 * @return the hash key |
83 */ |
89 */ |
84 __attribute__((__warn_unused_result__)) |
90 cx_attr_nodiscard |
85 CxHashKey cx_hash_key_str(char const *str); |
91 cx_attr_cstr_arg(1) |
|
92 cx_attr_export |
|
93 CxHashKey cx_hash_key_str(const char *str); |
86 |
94 |
87 /** |
95 /** |
88 * Computes a hash key from a byte array. |
96 * Computes a hash key from a byte array. |
89 * |
97 * |
90 * @param bytes the array |
98 * @param bytes the array |
91 * @param len the length |
99 * @param len the length |
92 * @return the hash key |
100 * @return the hash key |
93 */ |
101 */ |
94 __attribute__((__warn_unused_result__)) |
102 cx_attr_nodiscard |
|
103 cx_attr_access_r(1, 2) |
|
104 cx_attr_export |
95 CxHashKey cx_hash_key_bytes( |
105 CxHashKey cx_hash_key_bytes( |
96 unsigned char const *bytes, |
106 const unsigned char *bytes, |
97 size_t len |
107 size_t len |
98 ); |
108 ); |
99 |
109 |
100 /** |
110 /** |
101 * Computes a hash key for an arbitrary object. |
111 * Computes a hash key for an arbitrary object. |
106 * |
116 * |
107 * @param obj a pointer to an arbitrary object |
117 * @param obj a pointer to an arbitrary object |
108 * @param len the length of object in memory |
118 * @param len the length of object in memory |
109 * @return the hash key |
119 * @return the hash key |
110 */ |
120 */ |
111 __attribute__((__warn_unused_result__)) |
121 cx_attr_nodiscard |
|
122 cx_attr_access_r(1, 2) |
|
123 cx_attr_export |
112 CxHashKey cx_hash_key( |
124 CxHashKey cx_hash_key( |
113 void const *obj, |
125 const void *obj, |
114 size_t len |
126 size_t len |
115 ); |
127 ); |
116 |
128 |
117 /** |
129 /** |
118 * Computes a hash key from a UCX string. |
130 * Computes a hash key from a UCX string. |
119 * |
131 * |
120 * @param str the string |
132 * @param str the string |
121 * @return the hash key |
133 * @return the hash key |
122 */ |
134 */ |
123 #define cx_hash_key_cxstr(str) cx_hash_key((void*)(str).ptr, (str).length) |
135 cx_attr_nodiscard |
|
136 static inline CxHashKey cx_hash_key_cxstr(cxstring str) { |
|
137 return cx_hash_key(str.ptr, str.length); |
|
138 } |
|
139 |
|
140 /** |
|
141 * Computes a hash key from a UCX string. |
|
142 * |
|
143 * @param str (@c cxstring or @c cxmutstr) the string |
|
144 * @return (@c CxHashKey) the hash key |
|
145 */ |
|
146 #define cx_hash_key_cxstr(str) cx_hash_key_cxstr(cx_strcast(str)) |
124 |
147 |
125 #ifdef __cplusplus |
148 #ifdef __cplusplus |
126 } // extern "C" |
149 } // extern "C" |
127 #endif |
150 #endif |
128 |
151 |