issue #389 : add separate function for immutable arrays

Thu, 23 May 2024 18:21:08 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 23 May 2024 18:21:08 +0200
changeset 851
adb4e0737c33
parent 850
b2bc48c2b251
child 852
16e2a3391e88

issue #389 : add separate function for immutable arrays

src/cx/iterator.h file | annotate | diff | comparison | revisions
src/iterator.c file | annotate | diff | comparison | revisions
tests/test_iterator.c file | annotate | diff | comparison | revisions
--- a/src/cx/iterator.h	Thu May 23 15:05:24 2024 +0200
+++ b/src/cx/iterator.h	Thu May 23 18:21:08 2024 +0200
@@ -274,6 +274,25 @@
 
 
 /**
+ * Creates a mutating iterator for the specified plain array.
+ *
+ * The \p array can be \c NULL in which case the iterator will be immediately
+ * initialized such that #cxIteratorValid() returns \c false.
+ *
+ *
+ * @param array a pointer to the array (can be \c NULL)
+ * @param elem_size the size of one array element
+ * @param elem_count the number of elements in the array
+ * @return an iterator for the specified array
+ */
+__attribute__((__warn_unused_result__))
+CxIterator cxIterator(
+        void const *array,
+        size_t elem_size,
+        size_t elem_count
+);
+
+/**
  * Creates an iterator for the specified plain array.
  *
  * While the iterator is in use, the array may only be altered by removing
@@ -297,7 +316,7 @@
  * @return an iterator for the specified array
  */
 __attribute__((__warn_unused_result__))
-CxMutIterator cxIterator(  // TODO: unify the iterator types
+CxMutIterator cxMutIterator(
         void *array,
         size_t elem_size,
         size_t elem_count,
--- a/src/iterator.c	Thu May 23 15:05:24 2024 +0200
+++ b/src/iterator.c	Thu May 23 18:21:08 2024 +0200
@@ -83,7 +83,7 @@
     }
 }
 
-CxMutIterator cxIterator(
+CxMutIterator cxMutIterator(
         void *array,
         size_t elem_size,
         size_t elem_count,
@@ -104,3 +104,17 @@
 
     return iter;
 }
+
+CxIterator cxIterator(
+        void const *array,
+        size_t elem_size,
+        size_t elem_count
+) {
+    CxMutIterator iter = cxMutIterator((void*)array, elem_size, elem_count, false);
+    iter.base.mutating = false;
+
+    // we know the iterators share the same memory layout
+    CxIterator ret;
+    memcpy(&ret, &iter, sizeof(CxIterator));
+    return ret;
+}
--- a/tests/test_iterator.c	Thu May 23 15:05:24 2024 +0200
+++ b/tests/test_iterator.c	Thu May 23 18:21:08 2024 +0200
@@ -35,7 +35,7 @@
     unsigned array[size];
     for (unsigned i = 0 ; i < size ; i++) array[i] = i;
 
-    CxMutIterator iter = cxIterator(array, sizeof(unsigned), size, false);
+    CxIterator iter = cxIterator(array, sizeof(unsigned), size);
     CX_TEST_DO {
         CX_TEST_ASSERT(iter.index == 0);
         CX_TEST_ASSERT(iter.elem_size == sizeof(unsigned));
@@ -47,7 +47,7 @@
 }
 
 CX_TEST(test_iterator_create_null) {
-    CxMutIterator iter = cxIterator(NULL, sizeof(unsigned), 47, false);
+    CxIterator iter = cxIterator(NULL, sizeof(unsigned), 47);
     CX_TEST_DO {
         CX_TEST_ASSERT(iter.index == 0);
         CX_TEST_ASSERT(iter.elem_size == sizeof(unsigned));
@@ -63,7 +63,7 @@
     unsigned array[size];
     for (unsigned i = 0 ; i < size ; i++) array[i] = i;
 
-    CxMutIterator iter = cxIterator(array, sizeof(unsigned), size, false);
+    CxIterator iter = cxIterator(array, sizeof(unsigned), size);
     CX_TEST_DO {
         unsigned expected = 0;
         cx_foreach(unsigned *, e, iter) {
@@ -96,7 +96,7 @@
             0, 2, 4, 6, 8, 10, 12, 14, 16, 18
     };
 
-    CxMutIterator iter = cxIterator(array, sizeof(unsigned), size, true);
+    CxMutIterator iter = cxMutIterator(array, sizeof(unsigned), size, true);
     CX_TEST_DO {
         unsigned expected = 0;
         cx_foreach(unsigned *, e, iter) {
@@ -141,7 +141,7 @@
             15, 6, 14, 7, 13, 8, 12, 9, 11, 10
     };
 
-    CxMutIterator iter = cxIterator(array, sizeof(unsigned), size, false);
+    CxMutIterator iter = cxMutIterator(array, sizeof(unsigned), size, false);
     CX_TEST_DO {
         unsigned expected = 0;
         cx_foreach(unsigned *, e, iter) {

mercurial