src/cx/kv_list.h

changeset 1348
a1da355ed3b8
parent 1347
d02b1fde73ee
--- a/src/cx/kv_list.h	Sun Aug 24 17:11:53 2025 +0200
+++ b/src/cx/kv_list.h	Mon Aug 25 21:47:45 2025 +0200
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
+ * Copyright 2025 Mike Becker, Olaf Wintermann All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -176,6 +176,85 @@
 cx_attr_export
 CxMap *cxKvListAsMap(CxList *list);
 
+cx_attr_nonnull
+cx_attr_export
+int cx_kv_list_set_key(CxList *list, size_t index, CxHashKey key);
+
+cx_attr_nonnull
+cx_attr_export
+int cx_kv_list_remove_key(CxList *list, size_t index, CxHashKey key);
+
+cx_attr_nonnull
+cx_attr_export
+int cx_kv_list_insert(CxList *list, size_t index, CxHashKey key, void *value);
+
+// Generic Functions
+#ifdef __cplusplus
+
+cx_attr_nonnull
+static inline int cxKvListSetKey(CxList *list, size_t index, CxHashKey key) {
+    return cx_kv_list_set_key(list, index, key);
+}
+
+cx_attr_nonnull
+static inline int cxKvListSetKey(CxList *list, size_t index, cxstring key) {
+    return cx_kv_list_set_key(list, index, cx_hash_key_cxstr(key));
+}
+
+cx_attr_nonnull
+static inline int cxKvListSetKey(CxList *list, size_t index, cxmutstr key) {
+    return cx_kv_list_set_key(list, index, cx_hash_key_cxstr(key));
+}
+
+cx_attr_nonnull
+cx_attr_cstr_arg(2)
+static inline int cxKvListSetKey(CxList *list, size_t index, const char *key) {
+    return cx_kv_list_set_key(list, index, cx_hash_key_str(key));
+}
+
+cx_attr_nonnull
+static inline int cxKvListRemoveKey(CxList *list, size_t index, CxHashKey key) {
+    return cx_kv_list_remove_key(list, index, key);
+}
+
+cx_attr_nonnull
+static inline int cxKvListRemoveKey(CxList *list, size_t index, cxstring key) {
+    return cx_kv_list_remove_key(list, index, cx_hash_key_cxstr(key));
+}
+
+cx_attr_nonnull
+static inline int cxKvListRemoveKey(CxList *list, size_t index, cxmutstr key) {
+    return cx_kv_list_remove_key(list, index, cx_hash_key_cxstr(key));
+}
+
+cx_attr_nonnull
+cx_attr_cstr_arg(2)
+static inline int cxKvListRemoveKey(CxList *list, size_t index, const char *key) {
+    return cx_kv_list_remove_key(list, index, cx_hash_key_str(key));
+}
+
+cx_attr_nonnull
+static inline int cxKvListInsert(CxList *list, size_t index, CxHashKey key, void *value) {
+    return cx_kv_list_insert(list, index, key, value, value);
+}
+
+cx_attr_nonnull
+static inline int cxKvListInsert(CxList *list, size_t index, cxstring key, void *value) {
+    return cx_kv_list_insert(list, index, cx_hash_key_cxstr(key), value);
+}
+
+cx_attr_nonnull
+static inline int cxKvListInsert(CxList *list, size_t index, cxmutstr key, void *value) {
+    return cx_kv_list_insert(list, index, cx_hash_key_cxstr(key), value);
+}
+
+cx_attr_nonnull
+cx_attr_cstr_arg(2)
+static inline int cxKvListInsert(CxList *list, size_t index, const char *key, void *value) {
+    return cx_kv_list_insert(list, index, cx_hash_key_str(key), value);
+}
+
+#else /* ! __cplusplus */
 /**
  * Sets or updates the key of a list item.
  *
@@ -183,12 +262,30 @@
  * and now you want to associate this element with a key.
  *
  * @param list (@c CxList*) the list
+ * @param index (@c size_t) the index of the element in the list
  * @param key (@c CxHashKey, @c char*, @c cxstring, or @c cxmutstr) the key
- * @param index (@c size_t) the index of the element in the list
  * @retval zero success
  * @retval non-zero memory allocation failure or the index is out of bounds
  */
-#define cxKvListSetKey(list, key, index)
+#define cxKvListSetKey(list, index, key) _Generic((key), \
+        CxHashKey: cx_kv_list_set_key,                   \
+        cxstring: cx_kv_list_set_key_cxstr,              \
+        cxmutstr: cx_kv_list_set_key_mustr,              \
+        char*: cx_kv_list_set_key_str,                   \
+        const char*: cx_kv_list_set_key_str)             \
+        (list, index, key)
+
+static inline int cx_kv_list_set_key_cxstr(CxList *list, size_t index, cxstring key) {
+    return cx_kv_list_set_key(list, index, cx_hash_key_cxstr(key));
+}
+
+static inline int cx_kv_list_set_key_mustr(CxList *list, size_t index, cxmutstr key) {
+    return cx_kv_list_set_key(list, index, cx_hash_key_cxstr(key));
+}
+
+static inline int cx_kv_list_set_key_str(CxList *list, size_t index, const char *key) {
+    return cx_kv_list_set_key(list, index, cx_hash_key_str(key));
+}
 
 /**
  * Removes the key for a list item.
@@ -197,12 +294,30 @@
  * for example, when you want to prevent a deletion by cxMapClear().
  *
  * @param list (@c CxList*) the list
+ * @param index (@c size_t) the index of the element in the list
  * @param key (@c CxHashKey, @c char*, @c cxstring, or @c cxmutstr) the key
- * @param index (@c size_t) the index of the element in the list
  * @retval zero success
  * @retval non-zero the index is out of bounds
  */
-#define cxKvListRemoveKey(list, key, index)
+#define cxKvListRemoveKey(list, index, key) _Generic((key), \
+        CxHashKey: cx_kv_list_remove_key,                   \
+        cxstring: cx_kv_list_remove_key_cxstr,              \
+        cxmutstr: cx_kv_list_remove_key_mustr,              \
+        char*: cx_kv_list_remove_key_str,                   \
+        const char*: cx_kv_list_remove_key_str)             \
+        (list, index, key)
+
+static inline int cx_kv_list_remove_key_cxstr(CxList *list, size_t index, cxstring key) {
+    return cx_kv_list_remove_key(list, index, cx_hash_key_cxstr(key));
+}
+
+static inline int cx_kv_list_remove_key_mustr(CxList *list, size_t index, cxmutstr key) {
+    return cx_kv_list_remove_key(list, index, cx_hash_key_cxstr(key));
+}
+
+static inline int cx_kv_list_remove_key_str(CxList *list, size_t index, const char *key) {
+    return cx_kv_list_remove_key(list, index, cx_hash_key_str(key));
+}
 
 /**
  * Inserts an item into the list at the specified index and associates it with the specified key.
@@ -214,7 +329,27 @@
  * @retval zero success
  * @retval non-zero memory allocation failure or the index is out of bounds
  */
-#define cxKvListInsert(list, index, key, value)
+#define cxKvListInsert(list, index, key, value) _Generic((key), \
+        CxHashKey: cx_kv_list_insert,                           \
+        cxstring: cx_kv_list_insert_cxstr,                      \
+        cxmutstr: cx_kv_list_insert_mustr,                      \
+        char*: cx_kv_list_insert_str,                           \
+        const char*: cx_kv_list_insert_str)                     \
+        (list, index, key, value)
+
+static inline int cx_kv_list_insert_cxstr(CxList *list, size_t index, cxstring key, void *value) {
+    return cx_kv_list_insert(list, index, cx_hash_key_cxstr(key), value);
+}
+
+static inline int cx_kv_list_insert_mustr(CxList *list, size_t index, cxmutstr key, void *value) {
+    return cx_kv_list_insert(list, index, cx_hash_key_cxstr(key), value);
+}
+
+static inline int cx_kv_list_insert_str(CxList *list, size_t index, const char *key, void *value) {
+    return cx_kv_list_insert(list, index, cx_hash_key_str(key), value);
+}
+
+#endif
 
 /**
  * Adds an item into the list and associates it with the specified key.

mercurial