17 CX_ITERATOR_BASE; // the base members used by cx_foreach() |
17 CX_ITERATOR_BASE; // the base members used by cx_foreach() |
18 // ... custom fields ... |
18 // ... custom fields ... |
19 }; |
19 }; |
20 ``` |
20 ``` |
21 |
21 |
22 ## Overview |
22 ## Creating an Iterator |
23 |
23 |
24 |
24 The following functions create iterators over plain C arrays: |
|
25 |
|
26 ```C |
|
27 #include <cx/iterator.h> |
|
28 |
|
29 CxIterator cxIterator(const void *array, |
|
30 size_t elem_size, size_t elem_count); |
|
31 |
|
32 CxIterator cxMutIterator(void *array, |
|
33 size_t elem_size, size_t elem_count, bool remove_keeps_order); |
|
34 |
|
35 CxIterator cxIteratorPtr(const void *array, size_t elem_count); |
|
36 |
|
37 CxIterator cxMutIteratorPtr(void *array, size_t elem_count, |
|
38 bool remove_keeps_order); |
|
39 ``` |
|
40 |
|
41 The `cxIterator()` function creates an iterator over the elements of `array` where |
|
42 each element is `elem_size` bytes large and the array contains a total of `elem_count` elements. |
|
43 The `cxMutIterator()` function creates an equivalent [mutating iterator](#mutating-iterators). |
|
44 |
|
45 The `cxIteratorPtr()` and `cxMutIteratorPtr()` functions are equivalent to |
|
46 the `cxIteratorPtr()` and `cxMutIteratorPtr()`, except they assume `sizeof(void*)` as the `elem_size`. |
|
47 |
|
48 The UCX collections also define functions for creating iterators over their items. |
|
49 You can read more about them in the respective Sections of the documentation. |
25 |
50 |
26 ## Using an Iterator |
51 ## Using an Iterator |
27 |
52 |
28 The following macros work with arbitrary structures using `CX_ITERATOR_BASE` |
53 The following macros work with arbitrary structures using `CX_ITERATOR_BASE` |
29 and invoke the respective function pointers `valid`, `current`, or `next`. |
54 and invoke the respective function pointers `valid`, `current`, or `next`. |