ucx/cx/hash_key.h

changeset 852
83fdf679df99
parent 816
839fefbdedc7
--- a/ucx/cx/hash_key.h	Thu Nov 28 17:53:13 2024 +0100
+++ b/ucx/cx/hash_key.h	Mon Jan 06 21:18:36 2025 +0100
@@ -26,11 +26,11 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 /**
- * \file hash_key.h
- * \brief Interface for map implementations.
- * \author Mike Becker
- * \author Olaf Wintermann
- * \copyright 2-Clause BSD License
+ * @file hash_key.h
+ * @brief Interface for map implementations.
+ * @author Mike Becker
+ * @author Olaf Wintermann
+ * @copyright 2-Clause BSD License
  */
 
 
@@ -38,6 +38,7 @@
 #define UCX_HASH_KEY_H
 
 #include "common.h"
+#include "string.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -46,7 +47,7 @@
 /** Internal structure for a key within a hash map. */
 struct cx_hash_key_s {
     /** The key data. */
-    void const *data;
+    const void *data;
     /**
      * The key data length.
      */
@@ -61,15 +62,20 @@
 typedef struct cx_hash_key_s CxHashKey;
 
 /**
- * Computes a murmur2 32 bit hash.
+ * Computes a murmur2 32-bit hash.
  *
- * You need to initialize \c data and \c len in the key struct.
+ * You need to initialize @c data and @c len in the key struct.
  * The hash is then directly written to that struct.
  *
- * \note If \c data is \c NULL, the hash is defined as 1574210520.
+ * Usually you should not need this function.
+ * Use cx_hash_key(), instead.
+ *
+ * @note If @c data is @c NULL, the hash is defined as 1574210520.
  *
  * @param key the key, the hash shall be computed for
+ * @see cx_hash_key()
  */
+cx_attr_nonnull
 void cx_hash_murmur(CxHashKey *key);
 
 /**
@@ -80,8 +86,9 @@
  * @param str the string
  * @return the hash key
  */
-__attribute__((__warn_unused_result__))
-CxHashKey cx_hash_key_str(char const *str);
+cx_attr_nodiscard
+cx_attr_cstr_arg(1)
+CxHashKey cx_hash_key_str(const char *str);
 
 /**
  * Computes a hash key from a byte array.
@@ -90,9 +97,10 @@
  * @param len the length
  * @return the hash key
  */
-__attribute__((__warn_unused_result__))
+cx_attr_nodiscard
+cx_attr_access_r(1, 2)
 CxHashKey cx_hash_key_bytes(
-        unsigned char const *bytes,
+        const unsigned char *bytes,
         size_t len
 );
 
@@ -107,9 +115,10 @@
  * @param len the length of object in memory
  * @return the hash key
  */
-__attribute__((__warn_unused_result__))
+cx_attr_nodiscard
+cx_attr_access_r(1, 2)
 CxHashKey cx_hash_key(
-        void const *obj,
+        const void *obj,
         size_t len
 );
 
@@ -119,7 +128,18 @@
  * @param str the string
  * @return the hash key
  */
-#define cx_hash_key_cxstr(str) cx_hash_key((void*)(str).ptr, (str).length)
+cx_attr_nodiscard
+static inline CxHashKey cx_hash_key_cxstr(cxstring str) {
+    return cx_hash_key(str.ptr, str.length);
+}
+
+/**
+ * Computes a hash key from a UCX string.
+ *
+ * @param str (@c cxstring or @c cxmutstr) the string
+ * @return (@c CxHashKey) the hash key
+ */
+#define cx_hash_key_cxstr(str) cx_hash_key_cxstr(cx_strcast(str))
 
 #ifdef __cplusplus
 } // extern "C"

mercurial