Next: , Previous: Darray status, Up: Dynamic arrays


12.4 Indexing and updating darray nodes

The nomenclature for the functions used for indexing and updating the elements of a darray is modelled on that used in Emacs Lisp.

— Function: void * da_ref (const Darray *da, ssize_t index)

Returns the address of the indexth element in da. index must be non-negative and no greater than the length of da. The pointer returned is guaranteed to be valid only until the next operation is performed which may change the length of da.

— Function: void * da_last (const Darray *da)

Returns a reference to the last element in da. The pointer returned is guaranteed to be valid only until the next operation is performed which may change the length of da.

— Function: void da_set (Darray *da, ssize_t index, const void *new)

Copies the memory referenced by new into the indexth element of da. index must be non-negative and no greater than the length of da.

It is possible to apply a function to every element in a darray. Libretto defines the following type as a pointer to a function which can be applied to each element of a darray.

— Function pointer: Da_apply_f int (*) (const void *obj, const void *args)

The type of a function pointer used to perform arbitrary actions on an object in a darray. The function is passed the address of an object, obj, as well as a user-supplied pointer to parameters, args. The function should return zero on success, or -1 on some error which must terminate the traversal of the array holding obj.

— Function: int da_apply (const Darray *da, Da_apply_f func, void *apply_args)

Apply func to each member of da, stopping early if func should return -1. Returns the value returned by the last invocation of func. apply_args is passed as the last argument to func.