GFXprim
2D bitmap graphics library with emphasis on speed and correctness
|
Vector a growable and shrinkable array. More...
Go to the source code of this file.
Data Structures | |
struct | gp_vec |
A growable vector. More... | |
Macros | |
#define | GP_VEC(ptr) (GP_CONTAINER_OF(ptr, gp_vec, payload)) |
Converts a vector pointer into the header i.e. struct gp_vec. | |
#define | GP_VEC_FOREACH(self, type, iterator) for (type *iterator = (self); iterator < (self) + gp_vec_len(self); iterator++) |
A vector iterator. | |
#define | GP_VEC_APPEND(vec, val) |
Appends single element to a vector. | |
Typedefs | |
typedef struct gp_vec | gp_vec |
A growable vector. | |
Functions | |
gp_vec * | gp_vec_expand_ (gp_vec *self, size_t len) |
Expands vector length; does not touch vector data. | |
gp_vec * | gp_vec_shrink_ (gp_vec *self, size_t len) |
Shrinks vector length; does not touch vector data. | |
void * | gp_vec_new (size_t len, size_t unit) |
Allocates a new vector. | |
void * | gp_vec_dup (void *self) |
Creates a duplicate of a vector. | |
void | gp_vec_free (void *self) |
Frees the vector. | |
void * | gp_vec_resize (void *self, size_t len) |
Resize vector. | |
static size_t | gp_vec_len (const void *self) |
Returns vector lenght. | |
static size_t | gp_vec_unit (const void *self) |
Returns vector unit. | |
void * | gp_vec_ins (void *self, size_t off, size_t len) |
Insert a gap into the vec of new elements, reallocating the underlying memory if more capacity is needed. | |
void * | gp_vec_expand (void *self, size_t len) |
Expands vector by length elements at the end of the vector. | |
void * | gp_vec_del (void *self, size_t off, size_t len) |
Deletes a range from the vector. | |
void * | gp_vec_shrink (void *self, size_t len) |
Shrinks vector by length elements at the end of the vector. | |
void * | gp_vec_move_shrink (void *self, size_t idx) |
Moves last object in vector to idx and shrinks the vector by one. | |
Vector a growable and shrinkable array.
Definition in file gp_vec.h.
#define GP_VEC | ( | ptr | ) | (GP_CONTAINER_OF(ptr, gp_vec, payload)) |
#define GP_VEC_APPEND | ( | vec, | |
val | |||
) |
Appends single element to a vector.
vec | A vector. |
val | A value to be appended. |
#define GP_VEC_FOREACH | ( | self, | |
type, | |||
iterator | |||
) | for (type *iterator = (self); iterator < (self) + gp_vec_len(self); iterator++) |
A vector iterator.
A ready made iterator for the vector data.
self | A vector. |
type | A C type of the vector data. |
iterator | The iterator variable name. |
For a vector of integers it's used as:
And for a vector of strings:
void * gp_vec_del | ( | void * | self, |
size_t | off, | ||
size_t | len | ||
) |
Deletes a range from the vector.
self | A vector. |
off | An offset in the vector. |
length | A number of elements to delete. |
Returns a pointer to the vector, possibly a different from the previous one. May return NULL if block is outside of the vector.
Referenced by gp_matrix_cols_del(), and gp_vec_strdel().
void * gp_vec_dup | ( | void * | self | ) |
Creates a duplicate of a vector.
Creates a new vector with the exact same data.
self | Vector to be duplicated. |
void * gp_vec_expand | ( | void * | self, |
size_t | len | ||
) |
Expands vector by length elements at the end of the vector.
self | A vector. |
length | A number of elements to append. |
Returns a pointer to the vector, possibly a different from the previous one. May return NULL if underlying call to realloc() has failed.
Expands vector length; does not touch vector data.
This is internal rutine, handle with care!
self | Pointer to the vector structure. |
len | How many units should be added to vector length. |
void gp_vec_free | ( | void * | self | ) |
Frees the vector.
self | A vector. The call is no-op on NULL vector. |
Referenced by gp_matrix_free().
void * gp_vec_ins | ( | void * | self, |
size_t | off, | ||
size_t | len | ||
) |
Insert a gap into the vec of new elements, reallocating the underlying memory if more capacity is needed.
If more capacity is required, then this will reallocate the gp_vec thus invalidating *self. Therefor the caller should update any pointers it has to the vec data with the return value of this function.
Newly allocated capacity, which is not within the gap, will be set to 0xff. The memory within the gap will be zeroed. If allocation fails or i is invalid this will return 0. The offset 'off' should be <= length.
Returns a pointer to the vector, possibly a different from the previous one. May return NULL if underlying call to realloc() has failed or if off is outside of the vector.
self | A vector. |
off | An offset in the vector. |
length | A number of elements to instert. |
Referenced by gp_matrix_cols_ins(), gp_vec_chins(), gp_vec_ins_utf8(), and gp_vec_strins().
|
inlinestatic |
Returns vector lenght.
Returns 0 for a NULL pointer, i.e. vector that hasn't been allocated yet.
self | A vector. |
Definition at line 113 of file gp_vec.h.
References GP_VEC.
Referenced by gp_vec_str_append(), gp_vec_strlen(), and gp_vec_strsize().
void * gp_vec_move_shrink | ( | void * | self, |
size_t | idx | ||
) |
Moves last object in vector to idx and shrinks the vector by one.
This is fast operation to remove an object from the middle of the vector by replacing it by last element in vector and then shrinking it.
vec | A vector. |
idx | An index into the vector to be removed. |
void * gp_vec_new | ( | size_t | len, |
size_t | unit | ||
) |
Allocates a new vector.
len | An initial lenght of the vector. |
unit | A size of a vector element. |
Referenced by gp_matrix_new(), gp_vec_str_new(), and gp_vec_strdup().
void * gp_vec_resize | ( | void * | self, |
size_t | len | ||
) |
Resize vector.
Returns a pointer to the vector, possibly a different from the previous one. May return NULL if vector grows and underlying call to realloc() has failed.
self | A vector. |
length | A new vector lenght. |
Referenced by gp_vec_strclr().
void * gp_vec_shrink | ( | void * | self, |
size_t | len | ||
) |
Shrinks vector by length elements at the end of the vector.
Returns a pointer to the vector, possibly a different from the previous one.
self | A vector. |
length | A number of elemements to remove. |
Shrinks vector length; does not touch vector data.
This is internal rutine, handle with care!
self | Pointer to the vector structure. |
len | How many units should be removed from vector length. |