Thu, 23 May 2024 18:21:08 +0200
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 |
1.1 --- a/src/cx/iterator.h Thu May 23 15:05:24 2024 +0200 1.2 +++ b/src/cx/iterator.h Thu May 23 18:21:08 2024 +0200 1.3 @@ -274,6 +274,25 @@ 1.4 1.5 1.6 /** 1.7 + * Creates a mutating iterator for the specified plain array. 1.8 + * 1.9 + * The \p array can be \c NULL in which case the iterator will be immediately 1.10 + * initialized such that #cxIteratorValid() returns \c false. 1.11 + * 1.12 + * 1.13 + * @param array a pointer to the array (can be \c NULL) 1.14 + * @param elem_size the size of one array element 1.15 + * @param elem_count the number of elements in the array 1.16 + * @return an iterator for the specified array 1.17 + */ 1.18 +__attribute__((__warn_unused_result__)) 1.19 +CxIterator cxIterator( 1.20 + void const *array, 1.21 + size_t elem_size, 1.22 + size_t elem_count 1.23 +); 1.24 + 1.25 +/** 1.26 * Creates an iterator for the specified plain array. 1.27 * 1.28 * While the iterator is in use, the array may only be altered by removing 1.29 @@ -297,7 +316,7 @@ 1.30 * @return an iterator for the specified array 1.31 */ 1.32 __attribute__((__warn_unused_result__)) 1.33 -CxMutIterator cxIterator( // TODO: unify the iterator types 1.34 +CxMutIterator cxMutIterator( 1.35 void *array, 1.36 size_t elem_size, 1.37 size_t elem_count,
2.1 --- a/src/iterator.c Thu May 23 15:05:24 2024 +0200 2.2 +++ b/src/iterator.c Thu May 23 18:21:08 2024 +0200 2.3 @@ -83,7 +83,7 @@ 2.4 } 2.5 } 2.6 2.7 -CxMutIterator cxIterator( 2.8 +CxMutIterator cxMutIterator( 2.9 void *array, 2.10 size_t elem_size, 2.11 size_t elem_count, 2.12 @@ -104,3 +104,17 @@ 2.13 2.14 return iter; 2.15 } 2.16 + 2.17 +CxIterator cxIterator( 2.18 + void const *array, 2.19 + size_t elem_size, 2.20 + size_t elem_count 2.21 +) { 2.22 + CxMutIterator iter = cxMutIterator((void*)array, elem_size, elem_count, false); 2.23 + iter.base.mutating = false; 2.24 + 2.25 + // we know the iterators share the same memory layout 2.26 + CxIterator ret; 2.27 + memcpy(&ret, &iter, sizeof(CxIterator)); 2.28 + return ret; 2.29 +}
3.1 --- a/tests/test_iterator.c Thu May 23 15:05:24 2024 +0200 3.2 +++ b/tests/test_iterator.c Thu May 23 18:21:08 2024 +0200 3.3 @@ -35,7 +35,7 @@ 3.4 unsigned array[size]; 3.5 for (unsigned i = 0 ; i < size ; i++) array[i] = i; 3.6 3.7 - CxMutIterator iter = cxIterator(array, sizeof(unsigned), size, false); 3.8 + CxIterator iter = cxIterator(array, sizeof(unsigned), size); 3.9 CX_TEST_DO { 3.10 CX_TEST_ASSERT(iter.index == 0); 3.11 CX_TEST_ASSERT(iter.elem_size == sizeof(unsigned)); 3.12 @@ -47,7 +47,7 @@ 3.13 } 3.14 3.15 CX_TEST(test_iterator_create_null) { 3.16 - CxMutIterator iter = cxIterator(NULL, sizeof(unsigned), 47, false); 3.17 + CxIterator iter = cxIterator(NULL, sizeof(unsigned), 47); 3.18 CX_TEST_DO { 3.19 CX_TEST_ASSERT(iter.index == 0); 3.20 CX_TEST_ASSERT(iter.elem_size == sizeof(unsigned)); 3.21 @@ -63,7 +63,7 @@ 3.22 unsigned array[size]; 3.23 for (unsigned i = 0 ; i < size ; i++) array[i] = i; 3.24 3.25 - CxMutIterator iter = cxIterator(array, sizeof(unsigned), size, false); 3.26 + CxIterator iter = cxIterator(array, sizeof(unsigned), size); 3.27 CX_TEST_DO { 3.28 unsigned expected = 0; 3.29 cx_foreach(unsigned *, e, iter) { 3.30 @@ -96,7 +96,7 @@ 3.31 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 3.32 }; 3.33 3.34 - CxMutIterator iter = cxIterator(array, sizeof(unsigned), size, true); 3.35 + CxMutIterator iter = cxMutIterator(array, sizeof(unsigned), size, true); 3.36 CX_TEST_DO { 3.37 unsigned expected = 0; 3.38 cx_foreach(unsigned *, e, iter) { 3.39 @@ -141,7 +141,7 @@ 3.40 15, 6, 14, 7, 13, 8, 12, 9, 11, 10 3.41 }; 3.42 3.43 - CxMutIterator iter = cxIterator(array, sizeof(unsigned), size, false); 3.44 + CxMutIterator iter = cxMutIterator(array, sizeof(unsigned), size, false); 3.45 CX_TEST_DO { 3.46 unsigned expected = 0; 3.47 cx_foreach(unsigned *, e, iter) {