A dynamic array has a type, which is the type of its elements. The type of the dynamic array is declared when a new array instance is created via u_array_create and must be one of the types in u_array_type_t. Available types are the standard C types supported by the target platform, plus a generic pointer type for user defined types. A couple of getter/setter methods is provided for each u_array_type_t entry, e.g. see u_array_get_char and u_array_set_char.
The following is some toy code showing basic operations (create, set, get, destroy) using double precision complex numbers (C99):
u_array_t *a = NULL;
size_t idx;
long double _Complex c0, c1;
con_err_if (u_array_create(U_ARRAY_TYPE_LONG_DOUBLE_COMPLEX, 0, &a));
for (idx = 0; idx < 10; idx++)
{
c0 = idx + idx * _Complex_I;
con_err_if (u_array_set_long_double_complex(a, idx, c0, NULL));
con_err_if (u_array_get_long_double_complex(a, idx, &c1));
con_err_if (creal(c0) != creal(c1) || cimag(c0) != cimag(c1));
}
for (idx = 0; idx < 10; idx++)
{
long double _Complex c2;
c0 = (idx + 10) + (idx + 10) * _Complex_I;
con_err_if (u_array_set_long_double_complex(a, idx, c0, &c2));
u_con("overwrite %lf + %lfi at %zu with %lf + %lfi",
creal(c2), cimag(c2), idx, creal(c0), cimag(c0));
con_err_if (u_array_get_long_double_complex(a, idx, &c1));
con_err_if (creal(c0) != creal(c1) || cimag(c0) != cimag(c1));
}
u_array_free(a);
Defines |
#define | U_ARRAY_NSLOTS_DFL 512 |
| default number of slots on array creation (can be changed at compile time via -DU_ARRAY_NSLOTS_DFL=nnn flag)
|
#define | U_ARRAY_RESIZE_PAD 100 |
| right-pad when doing dynamic resize (can be changed at compile time via -DU_ARRAY_RESIZE_PAD=nnn flag)
|
Typedefs |
typedef struct u_array_s | u_array_t |
| Dynamic array base type.
|
Enumerations |
enum | u_array_type_t {
U_ARRAY_TYPE_UNSET = 0,
U_ARRAY_TYPE_CHAR,
U_ARRAY_TYPE_U_CHAR,
U_ARRAY_TYPE_SHORT,
U_ARRAY_TYPE_U_SHORT,
U_ARRAY_TYPE_INT,
U_ARRAY_TYPE_U_INT,
U_ARRAY_TYPE_LONG,
U_ARRAY_TYPE_U_LONG,
U_ARRAY_TYPE_FLOAT,
U_ARRAY_TYPE_DOUBLE,
U_ARRAY_TYPE_PTR
} |
| Available dynamic array types, i.e. the standard C types supported by the target platform, plus an any type pointer for user defined types.
More...
|
Functions |
int | u_array_create (u_array_type_t t, size_t nslots, u_array_t **pda) |
| Create a new array object.
|
void | u_array_free (u_array_t *da) |
| Free the array object: the array does not own the pointers in it, the client must free them explicitly.
|
int | u_array_resize (u_array_t *da, size_t idx) |
| Grow the array so that the supplied index can be accomodated.
|
int | u_array_set_char (u_array_t *da, size_t idx, char v, char *pold) |
| Put value v into the array da at index idx .
|
int | u_array_set_u_char (u_array_t *da, size_t idx, unsigned char v, unsigned char *pold) |
| Setter for the unsigned char type.
|
int | u_array_set_short (u_array_t *da, size_t idx, short v, short *pold) |
| Setter for the short type.
|
int | u_array_set_u_short (u_array_t *da, size_t idx, unsigned short v, unsigned short *pold) |
| Setter for the unsigned short type.
|
int | u_array_set_int (u_array_t *da, size_t idx, int v, int *pold) |
| Setter for the int type.
|
int | u_array_set_u_int (u_array_t *da, size_t idx, unsigned int v, unsigned int *pold) |
| Setter for the unsigned int type.
|
int | u_array_set_long (u_array_t *da, size_t idx, long v, long *pold) |
| Setter for the long type.
|
int | u_array_set_u_long (u_array_t *da, size_t idx, unsigned long v, unsigned long *pold) |
| Setter for the unsigned long type.
|
int | u_array_set_float (u_array_t *da, size_t idx, float v, float *pold) |
| Setter for the float type.
|
int | u_array_set_double (u_array_t *da, size_t idx, double v, double *pold) |
| Setter for the double type.
|
int | u_array_get_char (u_array_t *da, size_t idx, char *pv) |
| Get the element of char array da at index idx and save it at *pv .
|
int | u_array_get_u_char (u_array_t *da, size_t idx, unsigned char *pv) |
| Getter for the unsigned char type.
|
int | u_array_get_short (u_array_t *da, size_t idx, short *pv) |
| Getter for the short type.
|
int | u_array_get_u_short (u_array_t *da, size_t idx, unsigned short *pv) |
| Getter for the unsigned short type.
|
int | u_array_get_int (u_array_t *da, size_t idx, int *pv) |
| Getter for the int type.
|
int | u_array_get_u_int (u_array_t *da, size_t idx, unsigned int *pv) |
| Getter for the unsigned int type.
|
int | u_array_get_long (u_array_t *da, size_t idx, long *pv) |
| Getter for the long type.
|
int | u_array_get_u_long (u_array_t *da, size_t idx, unsigned long *pv) |
| Getter for the unsigned long type.
|
int | u_array_get_float (u_array_t *da, size_t idx, float *pv) |
| Getter for the float type.
|
int | u_array_get_double (u_array_t *da, size_t idx, double *pv) |
| Getter for the double type.
|
void * | u_array_set_ptr (u_array_t *da, size_t idx, void *v, int *prc) |
| Dynamic array setter interface for generic pointer values.
|
void * | u_array_get_ptr (u_array_t *da, size_t idx, int *prc) |
| Dynamic array getter interface for generic pointer values.
|