roll out the function stubs for the kv-list default tip

Mon, 25 Aug 2025 21:47:45 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 25 Aug 2025 21:47:45 +0200
changeset 1348
a1da355ed3b8
parent 1347
d02b1fde73ee

roll out the function stubs for the kv-list

relates to #461

src/Makefile file | annotate | diff | comparison | revisions
src/cx/kv_list.h file | annotate | diff | comparison | revisions
src/kv_list.c file | annotate | diff | comparison | revisions
--- a/src/Makefile	Sun Aug 24 17:11:53 2025 +0200
+++ b/src/Makefile	Mon Aug 25 21:47:45 2025 +0200
@@ -24,8 +24,8 @@
 include ../config.mk
 
 SRC = allocator.c array_list.c buffer.c compare.c hash_key.c hash_map.c \
-  iterator.c linked_list.c list.c map.c mempool.c printf.c string.c tree.c \
-  streams.c szmul.c properties.c json.c
+  iterator.c linked_list.c list.c map.c kv_list.c tree.c \
+  mempool.c printf.c string.c streams.c szmul.c properties.c json.c
 
 OBJ_EXT=.o
 OBJ=$(SRC:%.c=$(build_dir)/%$(OBJ_EXT))
@@ -109,6 +109,12 @@
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS)  -c $<
 
+$(build_dir)/kv_list$(OBJ_EXT): kv_list.c cx/kv_list.h cx/common.h \
+ cx/list.h cx/collection.h cx/allocator.h cx/iterator.h cx/compare.h \
+ cx/map.h cx/string.h cx/hash_key.h
+	@echo "Compiling $<"
+	$(CC) -o $@ $(CFLAGS)  -c $<
+
 $(build_dir)/linked_list$(OBJ_EXT): linked_list.c cx/linked_list.h \
  cx/common.h cx/list.h cx/collection.h cx/allocator.h cx/iterator.h \
  cx/compare.h cx/compare.h
--- 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.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/kv_list.c	Mon Aug 25 21:47:45 2025 +0200
@@ -0,0 +1,66 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * 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:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "cx/kv_list.h"
+
+CxList *cxKvListCreate(
+        const CxAllocator *allocator,
+        cx_compare_func comparator,
+        size_t elem_size
+) {
+    return NULL;
+}
+
+CxMap *cxKvListCreateAsMap(
+        const CxAllocator *allocator,
+        cx_compare_func comparator,
+        size_t elem_size
+) {
+    CxList *list = cxKvListCreate(allocator, comparator, elem_size);
+    return list == NULL ? NULL : cxKvListAsMap(list);
+}
+
+CxList *cxKvListAsList(CxMap *map) {
+    return NULL;
+}
+
+CxMap *cxKvListAsMap(CxList *list) {
+    return NULL;
+}
+
+int cx_kv_list_set_key(CxList *list, size_t index, CxHashKey key) {
+    return -1;
+}
+
+int cx_kv_list_remove_key(CxList *list, size_t index, CxHashKey key) {
+    return -1;
+}
+
+int cx_kv_list_insert(CxList *list, size_t index, CxHashKey key, void *value) {
+    return -1;
+}

mercurial