src/list.c

changeset 1426
3a89b31f0724
parent 1423
9a72258446cd
child 1428
0ac4aa1737fd
--- a/src/list.c	Wed Oct 15 22:45:21 2025 +0200
+++ b/src/list.c	Thu Oct 16 19:57:47 2025 +0200
@@ -29,6 +29,7 @@
 #include "cx/list.h"
 
 #include <string.h>
+#include <assert.h>
 
 // <editor-fold desc="Store Pointers Functionality">
 
@@ -566,36 +567,126 @@
     }
 }
 
-CxIterator cxListMutIteratorAt(
-        CxList *list,
-        size_t index
-) {
-    if (list == NULL) list = cxEmptyList;
-    CxIterator it = list->cl->iterator(list, index, false);
-    it.base.mutating = true;
-    return it;
+size_t cxListSize(const CxList *list) {
+    return list->collection.size;
+}
+
+int cxListAdd(CxList *list, const void *elem) {
+    list->collection.sorted = false;
+    return list->cl->insert_element(list, list->collection.size, elem) == NULL;
+}
+
+size_t cxListAddArray(CxList *list, const void *array, size_t n) {
+    list->collection.sorted = false;
+    return list->cl->insert_array(list, list->collection.size, array, n);
+}
+
+int cxListInsert(CxList *list, size_t index, const void *elem) {
+    list->collection.sorted = false;
+    return list->cl->insert_element(list, index, elem) == NULL;
+}
+
+void *cxListEmplaceAt(CxList *list, size_t index) {
+    list->collection.sorted = false;
+    return list->cl->insert_element(list, index, NULL);
+}
+
+void *cxListEmplace(CxList *list) {
+    list->collection.sorted = false;
+    return list->cl->insert_element(list, list->collection.size, NULL);
+}
+
+int cxListInsertSorted(CxList *list, const void *elem) {
+    assert(list->collection.sorted || list->collection.size == 0);
+    list->collection.sorted = true;
+    const void *data = list->collection.store_pointer ? &elem : elem;
+    return list->cl->insert_sorted(list, data, 1) == 0;
+}
+
+int cxListInsertUnique(CxList *list, const void *elem) {
+    assert(list->collection.sorted || list->collection.size == 0);
+    list->collection.sorted = true;
+    const void *data = list->collection.store_pointer ? &elem : elem;
+    return list->cl->insert_unique(list, data, 1) == 0;
+}
+
+size_t cxListInsertArray(CxList *list, size_t index, const void *array, size_t n) {
+    list->collection.sorted = false;
+    return list->cl->insert_array(list, index, array, n);
+}
+
+size_t cxListInsertSortedArray(CxList *list, const void *array, size_t n) {
+    assert(list->collection.sorted || list->collection.size == 0);
+    list->collection.sorted = true;
+    return list->cl->insert_sorted(list, array, n);
+}
+
+size_t cxListInsertUniqueArray(CxList *list, const void *array, size_t n) {
+    assert(list->collection.sorted || list->collection.size == 0);
+    list->collection.sorted = true;
+    return list->cl->insert_unique(list, array, n);
 }
 
-CxIterator cxListMutBackwardsIteratorAt(
-        CxList *list,
-        size_t index
-) {
-    if (list == NULL) list = cxEmptyList;
-    CxIterator it = list->cl->iterator(list, index, true);
-    it.base.mutating = true;
-    return it;
+int cxListInsertAfter(CxIterator *iter, const void *elem) {
+    CxList* list = (CxList*)iter->src_handle.m;
+    list->collection.sorted = false;
+    return list->cl->insert_iter(iter, elem, 0);
+}
+
+int cxListInsertBefore(CxIterator *iter, const void *elem) {
+    CxList* list = (CxList*)iter->src_handle.m;
+    list->collection.sorted = false;
+    return list->cl->insert_iter(iter, elem, 1);
+}
+
+int cxListRemove(CxList *list, size_t index) {
+    return list->cl->remove(list, index, 1, NULL) == 0;
+}
+
+int cxListRemoveAndGet(CxList *list, size_t index, void *targetbuf) {
+    return list->cl->remove(list, index, 1, targetbuf) == 0;
+}
+
+int cxListRemoveAndGetFirst(CxList *list, void *targetbuf) {
+    return list->cl->remove(list, 0, 1, targetbuf) == 0;
+}
+
+int cxListRemoveAndGetLast(CxList *list, void *targetbuf) {
+    // note: index may wrap - member function will catch that
+    return list->cl->remove(list, list->collection.size - 1, 1, targetbuf) == 0;
 }
 
-void cxListFree(CxList *list) {
-    if (list == NULL) return;
-    list->cl->deallocate(list);
+size_t cxListRemoveArray(CxList *list, size_t index, size_t num) {
+    return list->cl->remove(list, index, num, NULL);
+}
+
+size_t cxListRemoveArrayAndGet(CxList *list, size_t index, size_t num, void *targetbuf) {
+    return list->cl->remove(list, index, num, targetbuf);
+}
+
+void cxListClear(CxList *list) {
+    list->cl->clear(list);
+    list->collection.sorted = true; // empty lists are always sorted
 }
 
-int cxListSet(
-        CxList *list,
-        size_t index,
-        const void *elem
-) {
+int cxListSwap(CxList *list, size_t i, size_t j) {
+    list->collection.sorted = false;
+    return list->cl->swap(list, i, j);
+}
+
+void *cxListAt(const CxList *list, size_t index) {
+    return list->cl->at(list, index);
+}
+
+void *cxListFirst(const CxList *list) {
+    return list->cl->at(list, 0);
+}
+
+void *cxListLast(const CxList *list) {
+    return list->cl->at(list, list->collection.size - 1);
+}
+
+int cxListSet(CxList *list, size_t index, const void *elem) {
     if (index >= list->collection.size) {
         return 1;
     }
@@ -611,3 +702,80 @@
 
     return 0;
 }
+
+CxIterator cxListIteratorAt(const CxList *list, size_t index) {
+    if (list == NULL) list = cxEmptyList;
+    return list->cl->iterator(list, index, false);
+}
+
+CxIterator cxListBackwardsIteratorAt(const CxList *list, size_t index) {
+    if (list == NULL) list = cxEmptyList;
+    return list->cl->iterator(list, index, true);
+}
+
+CxIterator cxListMutIteratorAt(CxList *list, size_t index) {
+    if (list == NULL) list = cxEmptyList;
+    CxIterator it = list->cl->iterator(list, index, false);
+    it.base.mutating = true;
+    return it;
+}
+
+CxIterator cxListMutBackwardsIteratorAt(CxList *list, size_t index) {
+    if (list == NULL) list = cxEmptyList;
+    CxIterator it = list->cl->iterator(list, index, true);
+    it.base.mutating = true;
+    return it;
+}
+
+CxIterator cxListIterator(const CxList *list) {
+    if (list == NULL) list = cxEmptyList;
+    return list->cl->iterator(list, 0, false);
+}
+
+CxIterator cxListMutIterator(CxList *list) {
+    if (list == NULL) list = cxEmptyList;
+    return cxListMutIteratorAt(list, 0);
+}
+
+CxIterator cxListBackwardsIterator(const CxList *list) {
+    if (list == NULL) list = cxEmptyList;
+    return list->cl->iterator(list, list->collection.size - 1, true);
+}
+
+CxIterator cxListMutBackwardsIterator(CxList *list) {
+    if (list == NULL) list = cxEmptyList;
+    return cxListMutBackwardsIteratorAt(list, list->collection.size - 1);
+}
+
+size_t cxListFind(const CxList *list, const void *elem) {
+    return list->cl->find_remove((CxList*)list, elem, false);
+}
+
+bool cxListContains(const CxList* list, const void* elem) {
+    return list->cl->find_remove((CxList*)list, elem, false) < list->collection.size;
+}
+
+bool cxListIndexValid(const CxList *list, size_t index) {
+    return index < list->collection.size;
+}
+
+size_t cxListFindRemove(CxList *list, const void *elem) {
+    return list->cl->find_remove(list, elem, true);
+}
+
+void cxListSort(CxList *list) {
+    if (list->collection.sorted) return;
+    list->cl->sort(list);
+    list->collection.sorted = true;
+}
+
+void cxListReverse(CxList *list) {
+    // still sorted, but not according to the cmp_func
+    list->collection.sorted = false;
+    list->cl->reverse(list);
+}
+
+void cxListFree(CxList *list) {
+    if (list == NULL) return;
+    list->cl->deallocate(list);
+}

mercurial