csnip
0.1
|
Dynamically allocated resizable arrays. More...
Macros | |
#define | csnip_arr_Init(a, n, cap, initial_cap, err) |
Initialize an array. More... | |
#define | csnip_arr_Reserve(a, n, cap, least_cap, err) |
Reserve space for members to be added. More... | |
#define | csnip_arr_Push(a, n, cap, value, err) |
Append a new value at the end of the array. More... | |
#define | csnip_arr_Pop(a, n, cap, err) |
Delete the value at the end of the array. More... | |
#define | csnip_arr_InsertAt(a, n, cap, index, val, err) |
Insert a new member at a given position in the array. More... | |
#define | csnip_arr_DeleteAt(a, n, cap, index, err) |
Remove an array member at a given index. More... | |
#define | csnip_arr_Free(a, n, cap) |
Delete an array. More... | |
#define | CSNIP_ARR_DECL_FUNCS(scope, prefix, val_type, gen_args) |
Declare array managment functions. More... | |
#define | CSNIP_ARR_DEF_FUNCS(scope, prefix, val_type, gen_args, a, n, cap, err) |
Define dynamic array managment functions. More... | |
A csnip dynamic array is represented as a triple (a, n, cap), where "a" is a pointer to the array members, n is an integer value containing the current size of the dynamic array (in number of members), and cap is another integer value containing the current capacity of the array, i.e., the number of members that can be stored without reallocating the array.
The array macros assume the triple is given as lvalues, and will suitably modify them. The pointer to the array must be either a NULL pointer (allowable for empty arrays), or a pointer suitable for modification with realloc or free.
It is of course OK to modify the array directly without using the csnip_arr_* macros. No "SetAt" and "GetAt" methods are provided, since the user can trivially access the array members like a C array.
#define CSNIP_ARR_DECL_FUNCS | ( | scope, | |
prefix, | |||
val_type, | |||
gen_args | |||
) |
This macro declares array manipulation functions corresponding to such functions defined with CSNIP_ARR_DEF_FUNCS. See CSNIP_ARR_DEF_FUNCS for details.
scope | declaration scope of the functions; leave empty for global scope, "static" for file scope, etc. |
prefix | prefix to use for all function names. |
val_type | the type used for array values. |
gen_args | either a list of arguments specified as args(list) to add to all functions, or noargs() if no such arguments are to be used. |
#define CSNIP_ARR_DEF_FUNCS | ( | scope, | |
prefix, | |||
val_type, | |||
gen_args, | |||
a, | |||
n, | |||
cap, | |||
err | |||
) |
scope | declaration scope of the functions; leave empty for global scope, "static" for file scope, etc. |
prefix | prefix to use for all function names. |
val_type | the type used for array values. |
gen_args | either a list of arguments specified as args(list) to add to all functions, or noargs() if no such arguments are to be used. |
a,n,cap,err | those are the expressions as passed to the csnip_arr_* macros used to define the functions; they can be functions of the arguments as specified with gen_args . |
#define csnip_arr_DeleteAt | ( | a, | |
n, | |||
cap, | |||
index, | |||
err | |||
) |
Array members appearing behind the given index are moved forward one position.
Complexity: n - index element moves.
#define csnip_arr_Free | ( | a, | |
n, | |||
cap | |||
) |
This function deletes the memory associated with an array. The result is an array of size 0 with no allocated memory associated, i.e., it is like a freshly allocated array of capacity 0.
#define csnip_arr_Init | ( | a, | |
n, | |||
cap, | |||
initial_cap, | |||
err | |||
) |
An empty array of the given initial capacity is allocated. No memory is allocated if the initial_cap value provided is 0.
#define csnip_arr_InsertAt | ( | a, | |
n, | |||
cap, | |||
index, | |||
val, | |||
err | |||
) |
The members currently at the given position and after are moved back one index.
Complexity: n - index element moves.
#define csnip_arr_Pop | ( | a, | |
n, | |||
cap, | |||
err | |||
) |
This removes the last element of the array. The array capacity is not reduced accordingly.
Complexity: O(1)
#define csnip_arr_Push | ( | a, | |
n, | |||
cap, | |||
value, | |||
err | |||
) |
Complexity: amortized O(1).
#define csnip_arr_Reserve | ( | a, | |
n, | |||
cap, | |||
least_cap, | |||
err | |||
) |
Increase the capacity of the (a, n, cap) to at least least_cap, possibly reallocating the array if necessary.
Reallocation is designed to be efficient even if the provided increments are small. The csnip_arr_Reserve() method achieves this by increasing the capacity often to more than the least_cap value that was requested.
It is possible to shrink the reserved size of an array by calling csnip_arr_Reserve, but not to shrink the array itself. That is, requests for least_cap < n are not honoured.