Value:struct name { \
type *data_; \
size_t size_; \
size_t capacity_; \
}; \
typedef struct name *name; \
qual name prefix ## _create(void); \
qual void prefix ## _destroy(name v); \
qual void prefix ## _push(name v, type elem); \
qual void prefix ## _pop(name v); \
qual void prefix ## _append(name v1, name v2); \
qual void prefix ## _resize(name v, size_t newsz); \
qual void prefix ## _shrink(name v, size_t newsz); \
qual void prefix ## _reserve(name v, size_t cap); \
qual void prefix ## _clear(name v); \
qual void prefix ## _copy(name src, name dest); \
qual void prefix ## _swap(name v1, name v2); \
qual void prefix ## _sort(name v, int (*cmp)(type a, type b, void *p), \
void *p); \
qual void prefix ## _acquire(name v, type *arr, size_t sz); \
qual type *prefix ## _release(name v);
Public interface for a generic vector class.
- Author:
- Alberto Griggio The macro DECLARE_VECTOR(type, name, prefix) declares a new vector class of elements of type "type", of name "name". All the methods of the class will begin with "prefix". For example:
DECLARE_VECTOR(void *, Vector_ptr, Vector)
will declare a Vector_ptr class of "void *" elements, with methods like Vector_push(), Vector_pop(), and so on.
The macro DEFINE_VECTOR(type, name, prefix) expands to the implementation of the methods, and so must be placed in a .c file
The macro DEFINE_STATIC_VECTOR(type, name, prefix) declares and defines a vector that is local to the current translation unit (i.e. its methods are static).
There is one predefined vector class, corresponding to the declaration:
DECLARE_VECTOR(void *, Vector_ptr, Vector)
The methods of the class are:
- name prefix_create() create a new empty vector
- void prefix_destroy(name v) destroy the vector
- void prefix_push(name v, type elem) append elem at the end of v
- void prefix_pop(name v) remove the last element
- void prefix_append(name v1, name v2) append "v2" at the end of "v1"
- void prefix_resize(name v, size_t newsz) resize the vector, allocating memory if needed, but not deallocating in case of shrinking
- void prefix_shrink(name v, size_t newsz) shrink the vector reducing also the memory occupied
- void prefix_reserve(name v, size_t cap) reserve space for "cap" elements, but do not resize the vector
- void prefix_clear(name v) empty the vector and free memory
- void prefix_copy(name v1, name v2) copy v1 into v2
- void prefix_swap(name v1, name v2) efficiently swap the contents of v1 and v2
- void prefix_sort(name v, int (*cmp)(type a, type b, void *p), void *p) sort the vector in-place, using the given comparison function "cmp". "p" is a user-defined parameter that is simply passed to "cmp"
- void prefix_acquire(name v, type *arr, size_t sz) take ownership of the array "arr"
- type *prefix_release(name v) release ownership of the underlying array
The following macros are defined: