summaryrefslogtreecommitdiff
path: root/src/lib/eina
diff options
context:
space:
mode:
authorCedric BAIL <cedric@osg.samsung.com>2018-11-15 15:37:15 -0800
committerCedric BAIL <cedric@osg.samsung.com>2018-11-23 10:13:51 -0800
commit6b26b3330e8fddf56c8a1105c7145b4c42e9f0ac (patch)
tree0002821a904f5869aaca4093ea753a00e895d412 /src/lib/eina
parentcc07318377042f4c3a8fee75826180241419da07 (diff)
eina: add a way to iterate over a pure C array with no NUL terminating element.
Reviewed-by: Vitor Sousa da Silva <vitorsousa@expertisesolutions.com.br> Differential Revision: https://phab.enlightenment.org/D7284
Diffstat (limited to 'src/lib/eina')
-rw-r--r--src/lib/eina/eina_iterator.c61
-rw-r--r--src/lib/eina/eina_iterator.h35
2 files changed, 94 insertions, 2 deletions
diff --git a/src/lib/eina/eina_iterator.c b/src/lib/eina/eina_iterator.c
index 4a4e281079..d025b6ad5b 100644
--- a/src/lib/eina/eina_iterator.c
+++ b/src/lib/eina/eina_iterator.c
@@ -229,6 +229,67 @@ eina_carray_iterator_new(void** array)
229 return &it->iterator; 229 return &it->iterator;
230} 230}
231 231
232typedef struct _Eina_Iterator_CArray_Length Eina_Iterator_CArray_Length;
233
234struct _Eina_Iterator_CArray_Length
235{
236 Eina_Iterator iterator;
237
238 void** array;
239 uintptr_t current;
240
241 uintptr_t end;
242 unsigned int step;
243};
244
245static Eina_Bool
246eina_carray_length_iterator_next(Eina_Iterator_CArray_Length *it, void **data)
247{
248 if (it->current >= it->end)
249 return EINA_FALSE;
250
251 memcpy(data, (void*) it->current, it->step);
252 it->current += it->step;
253
254 return EINA_TRUE;
255}
256
257static void**
258eina_carray_length_iterator_get_container(Eina_Iterator_CArray_Length *it)
259{
260 return it->array;
261}
262
263static void
264eina_carray_length_iterator_free(Eina_Iterator_CArray_Length *it)
265{
266 free(it);
267}
268
269EAPI Eina_Iterator *
270eina_carray_length_iterator_new(void** array, unsigned int step, unsigned int length)
271{
272 Eina_Iterator_CArray_Length *it;
273
274 it = calloc(1, sizeof (Eina_Iterator_CArray_Length));
275 if (!it) return NULL;
276
277 EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR);
278
279 it->array = array;
280 it->current = (uintptr_t) it->array;
281 it->end = it->current + length * step;
282 it->step = step;
283
284 it->iterator.version = EINA_ITERATOR_VERSION;
285 it->iterator.next = FUNC_ITERATOR_NEXT(eina_carray_length_iterator_next);
286 it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER(
287 eina_carray_length_iterator_get_container);
288 it->iterator.free = FUNC_ITERATOR_FREE(eina_carray_length_iterator_free);
289
290 return &it->iterator;
291}
292
232typedef struct { 293typedef struct {
233 Eina_Iterator iterator; 294 Eina_Iterator iterator;
234 295
diff --git a/src/lib/eina/eina_iterator.h b/src/lib/eina/eina_iterator.h
index 6c33db1a63..1bed2bd33b 100644
--- a/src/lib/eina/eina_iterator.h
+++ b/src/lib/eina/eina_iterator.h
@@ -294,11 +294,42 @@ EAPI Eina_Bool eina_iterator_unlock(Eina_Iterator *iterator) EINA_ARG_NONNULL(1)
294 * int array[] = {1, 2, 3, 4}; 294 * int array[] = {1, 2, 3, 4};
295 * int* array2[] = {&array[0], &array[1], &array[2], &array[3], NULL}; 295 * int* array2[] = {&array[0], &array[1], &array[2], &array[3], NULL};
296 * 296 *
297 * Eina_Iterator* iterator = eina_carray_iterator_new((void**)array); 297 * Eina_Iterator* iterator = eina_carray_iterator_new((void**)array2);
298 * 298 *
299 * @since 1.18 299 * @since 1.18
300 */ 300 */
301EAPI Eina_Iterator* eina_carray_iterator_new(void** array) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT; 301EAPI Eina_Iterator *eina_carray_iterator_new(void** array) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
302
303/**
304 * @brief Creates an Eina_Iterator that iterates through a
305 * C array of specified size.
306 *
307 * @param[in] array The array
308 *
309 * You can create it like this:
310 * int array[] = {1, 2, 3, 4};
311 *
312 * Eina_Iterator* iterator = eina_carray_length_iterator_new((void**)array, sizeof (array[0]), (EINA_C_ARRAY_LENGTH(array));
313 *
314 * @since 1.22
315 */
316EAPI Eina_Iterator *eina_carray_length_iterator_new(void** array, unsigned int step, unsigned int length) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
317
318/**
319 * @def EINA_C_ARRAY_ITERATOR_NEW
320 * @brief Creates an Eina_Iterator that iterates through a
321 * NUL-terminated C array.
322 *
323 * @param[in] array The NUL-terminated array
324 *
325 * You can create it like this:
326 * int array[] = {1, 2, 3, 4};
327 *
328 * Eina_Iterator* iterator = EINA_C_ARRAY_ITERATOR_NEW(array);
329 *
330 * @since 1.22
331 */
332#define EINA_C_ARRAY_ITERATOR_NEW(Array) eina_carray_length_iterator_new((void**) Array, sizeof (Array[0]), EINA_C_ARRAY_LENGTH(Array))
302 333
303/** 334/**
304 * @brief Creates a new iterator which which iterates through all elements with are accepted by the filter callback 335 * @brief Creates a new iterator which which iterates through all elements with are accepted by the filter callback