libdacav 0.9.0
|
Iterators concept is the same provided by Java. More...
Typedefs | |
typedef struct diter | diter_t |
Opaque type for iterators. | |
Functions | |
int | diter_hasnext (diter_t *i) |
Has-Next predicate, checks if the iterator has at least another element. | |
void * | diter_next (diter_t *i) |
Get-Next function, extracts the next element of the iteration. | |
void | diter_remove (diter_t *i) |
Remove function, removes from the structure the current element. | |
void | diter_replace (diter_t *i, void *n) |
Replace function, replaces the current element of the structure. |
Iterators concept is the same provided by Java.
Every container structure declared in libdacav is provided with a dedicated iterator constructor and a dedicated iterator destructor, while functions that allows to iterate among elements are of general use.
By example, for the dlist_t data type, libdacav provides the dlist_iter_new and the dlist_free functions, while for dhash_t the dhash_iter_new and the dhash_iter_free functions are provided.
After allocating an iterator with the correct primitive (depending on which data structure you are working with), the iteration process is something like:
while (diter_hasnext(my_iterator)) { do_something(diter_next(my_iterator)); }
Once the iterator is allocated, the iterator user can access to the diter_hasnext, diter_next, diter_remove and diter_replace primitive.
The first two are always implemented, since they are somehow mandatory. The diter_remove and diter_replace ones, instead, can be left unimplemented by design. Requiring a diter_remove or a diter_replace operation on an iterator for which they are not implemented doesn't produce any consequence.
Apart from the functions listed above, the library provides some additional primitives that can be used to implement a similar iteration semantics.
typedef struct diter diter_t |
Opaque type for iterators.
Constructors and destructors for this type are strictly dependent from the kind of iterator used. Every type of container implements them and gives them an appropriate name.
int diter_hasnext | ( | diter_t * | i | ) |
Has-Next predicate, checks if the iterator has at least another element.
[in] | i | The iterator. |
1 | if there's at least another element to iterate on; |
0 | otherwise. |
void* diter_next | ( | diter_t * | i | ) |
Get-Next function, extracts the next element of the iteration.
[in] | i | The iterator. |
void diter_remove | ( | diter_t * | i | ) |
Remove function, removes from the structure the current element.
The removed element is deallocated according to a user-provided semantics which is part of the container.
[in] | i | The iterator. |
void diter_replace | ( | diter_t * | i, |
void * | n | ||
) |
Replace function, replaces the current element of the structure.
[in] | i | The iterator; |
[in] | n | The new element that will replace the next element. |