OpenGATE Contents | GATE development concepts: Enumerators
Container content iteration
Problem description
Iterating the elements of containers is very different depending on the implementation details of container itself.
In C, we use indexes, pointers and other native structures to access collections of items.
This leads to inflexibility when one container is replaced with another one.
Solution
All containers shall provide one generic interface that allows to iterate elements with only one API.
The GATE framework provides the gate_enumerator_t
type that fulfills this
requirement.
C Example
1#include <gate/arrays.h> 2 3static gate_int32_t sum_int_array(gate_array_t const* arr) 4{ 5 int sum = 0; 6 gate_enumerator_t e; 7 gate_int32_t const* ptr_item; 8 9 gate_array_enumerate(arr, &e); 10 11 while(gate_enumerator_valid(&e)) 12 { 13 ptr_item = (gate_int32_t const*)gate_enumerator_get(&e); 14 sum += *ptr_item; 15 gate_enumerator_next(&e); 16 } 17 return sum; 18}