ucx
UAP Common Extensions
Loading...
Searching...
No Matches
iterator.h File Reference

Interface for iterator implementations. More...

#include "common.h"

Go to the source code of this file.

Data Structures

struct  cx_iterator_base_s
 Common data for all iterators. More...
 
struct  cx_iterator_s
 Internal iterator struct - use CxIterator. More...
 

Macros

#define CX_ITERATOR_BASE   struct cx_iterator_base_s base
 Declares base attributes for an iterator.
 
#define cxIteratorValid(iter)
 Checks if the iterator points to valid data.
 
#define cxIteratorCurrent(iter)
 Returns a pointer to the current element.
 
#define cxIteratorNext(iter)
 Advances the iterator to the next element.
 
#define cxIteratorFlagRemoval(iter)
 Flags the current element for removal if the iterator allows it.
 
#define cx_foreach(type, elem, iter)
 Loops over an iterator.
 

Typedefs

typedef struct cx_iterator_base_s CxIteratorBase
 Convenience type definition for the base structure of an iterator.
 
typedef struct cx_iterator_s CxIterator
 Iterator type.
 

Functions

CxIterator cxIterator (const void *array, size_t elem_size, size_t elem_count)
 Creates an iterator for the specified plain array.
 
CxIterator cxIteratorPtr (const void *array, size_t elem_count)
 Creates an iterator for the specified plain pointer array.
 

Detailed Description

Interface for iterator implementations.

Author
Mike Becker
Olaf Wintermann

Macro Definition Documentation

◆ cx_foreach

#define cx_foreach ( type,
elem,
iter )
Value:
for (type elem; cxIteratorValid(iter) && (elem = (type)cxIteratorCurrent(iter)) != NULL ; cxIteratorNext(iter))
#define cxIteratorCurrent(iter)
Returns a pointer to the current element.
Definition iterator.h:164
#define cxIteratorValid(iter)
Checks if the iterator points to valid data.
Definition iterator.h:153
#define cxIteratorNext(iter)
Advances the iterator to the next element.
Definition iterator.h:171

Loops over an iterator.

Parameters
typethe type of the elements
elemthe name of the iteration variable
iterthe iterator

◆ CX_ITERATOR_BASE

#define CX_ITERATOR_BASE   struct cx_iterator_base_s base

Declares base attributes for an iterator.

Must be the first member of an iterator structure.

◆ cxIteratorCurrent

#define cxIteratorCurrent ( iter)
Value:
(iter).base.current(&iter)

Returns a pointer to the current element.

The behavior is undefined if this iterator is invalid.

Parameters
iterthe iterator
Returns
a pointer to the current element
See also
cxIteratorValid()

◆ cxIteratorFlagRemoval

#define cxIteratorFlagRemoval ( iter)
Value:
((iter).base.remove = (iter).base.allow_remove)

Flags the current element for removal if the iterator allows it.

Parameters
iterthe iterator
Returns
true if removal is allowed, false otherwise

◆ cxIteratorNext

#define cxIteratorNext ( iter)
Value:
(iter).base.next(&iter)

Advances the iterator to the next element.

Parameters
iterthe iterator

◆ cxIteratorValid

#define cxIteratorValid ( iter)
Value:
(iter).base.valid(&(iter))

Checks if the iterator points to valid data.

Parameters
iterthe iterator
Return values
trueif the iterator points to valid data
falseif the iterator already moved past the end

Typedef Documentation

◆ CxIterator

typedef struct cx_iterator_s CxIterator

Iterator type.

An iterator points to a certain element in a (possibly unbounded) chain of elements. Iterators that are based on collections (which have a defined "first" element) are supposed to be "position-aware", which means that they keep track of the current index within the collection.

Note
Objects that are pointed to by an iterator are always mutable through that iterator. However, any concurrent mutation of the collection other than by this iterator makes this iterator obsolete, and it must not be used anymore.

◆ CxIteratorBase

Convenience type definition for the base structure of an iterator.

See also
CX_ITERATOR_BASE

Function Documentation

◆ cxIterator()

CxIterator cxIterator ( const void * array,
size_t elem_size,
size_t elem_count )

Creates an iterator for the specified plain array.

The array can be NULL, in which case the iterator will be immediately initialized such that cxIteratorValid() returns false.

This iterator yields the addresses of the array elements. If you want to iterator over an array of pointers, you can use cxIteratorPtr() to create an iterator which directly yields the stored pointers.

Parameters
arraya pointer to the array (can be NULL)
elem_sizethe size of one array element
elem_countthe number of elements in the array
Returns
an iterator for the specified array
See also
cxIteratorPtr()

◆ cxIteratorPtr()

CxIterator cxIteratorPtr ( const void * array,
size_t elem_count )

Creates an iterator for the specified plain pointer array.

This iterator assumes that every element in the array is a pointer and yields exactly those pointers during iteration (on the other hand, an iterator created with cxIterator() would return the addresses of those pointers within the array).

Parameters
arraya pointer to the array (can be NULL)
elem_countthe number of elements in the array
Returns
an iterator for the specified array
See also
cxIterator()