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


12.6 Manipulating a darray

12.6.1 Exchanging nodes

— Function: void da_exchange (Darray *da, ssize_t x, ssize_t y)

Exchanges elements x and y of da. Both x and y must be non-negative and less than the length of da.

12.6.2 Resizing

— Function: int da_resize (Darray *da, ssize_t newlen)

Sets the size of da to be newlen. newlen must be non-negative. If da shrinks, the elements deleted are no longer accessible. If da grows, the elements created are not initialised. Returns 0 if the function succeeded, or -1 if there was insufficient memory.

12.6.3 Adding elements

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

Copies the memory referenced by new into a new element at the end of da. Returns the index of the element added, or -1 if there was insufficient memory.

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

Inserts a new element at position index in da, and copies the memory referenced by new into that element. index must be non-negative and no greater than the length of da. Returns 0 if the function succeeded, or -1 if there was insufficient memory.

12.6.4 Deleting elements

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

Deletes the element at position index in da. index must be non-negative and less than the length of da. The deleted element is no longer accessible.

It is also possible to delete more than one element at once. This requires a `pruning' function.

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

The type of a function pointer used to determine whether to delete objects in a darray. It is passed a reference to the contents of an element in obj, as well as a pointer to user-supplied arguments in args. It should return zero iff the element should not be deleted. Note that if the elements of a darray were dynamically allocated, it is probably a good idea to make the pruning function free this dynamically allocated memory.

— Function: void da_prune (Darray *da, Da_prune_f pruner, void *prune_args)

Deletes any objects in da for which pruner is true. prune_args is passed as the last argument to pruner.

12.6.5 Adding whole darrays

— Function: int da_concat_da (Darray *dest, const Darray *src)

Concatenates copies of the elements of src onto the end of dest. src and dest must have the same object size. The caller is responsible for freeing the elements of src if necessary. Returns 0 if the function succeeded, or -1 if there was insufficient memory.

— Function: int da_insert_da (Darray *dest, const Darray *src, ssize_t index)

Inserts copies of the elements of src into dest, starting at element index. src and dest must have the same object size. index must be non-negative and no greater than the length of dest. The caller is responsible for freeing the elements of src if necessary. Returns 0 if the function succeeded, or -1 if there was insufficient memory.