GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
Data Structures | Macros | Typedefs | Functions
gp_vec.h File Reference

Vector a growable array. More...

#include <stddef.h>
#include <core/gp_common.h>
#include <core/gp_compiler.h>

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_vecgp_vec_expand_ (gp_vec *self, size_t len)
 Expands vector length; does not touch vector data.
 
gp_vecgp_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.
 

Detailed Description

Vector a growable array.

Definition in file gp_vec.h.

Macro Definition Documentation

◆ GP_VEC

#define GP_VEC (   ptr)    (GP_CONTAINER_OF(ptr, gp_vec, payload))

Converts a vector pointer into the header i.e. struct gp_vec.

Returns a pointer to the outer gp_vec struct from a pointer to the (start of) its data.

Definition at line 27 of file gp_vec.h.

◆ GP_VEC_APPEND

#define GP_VEC_APPEND (   vec,
  val 
)
Value:
({ \
typeof(vec) gp_ret__ = gp_vec_expand(vec, 1); \
if (gp_ret__) { \
gp_ret__[gp_vec_len(gp_ret__)-1] = val; \
vec = gp_ret__; \
} \
gp_ret__; \
})
void * gp_vec_expand(void *self, size_t len)
Expands vector by length elements at the end of the vector.
static size_t gp_vec_len(const void *self)
Returns vector lenght.
Definition gp_vec.h:111

Appends single element to a vector.

Parameters
vecA vector.
valA value to be appended.
Returns
NULL on a failure.

Definition at line 221 of file gp_vec.h.

◆ GP_VEC_FOREACH

#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.

Parameters
selfA vector.
typeA C type of the vector data.
iteratorThe iterator variable name.

For a vector of integers it's used as:

GP_VEC_FOREACH(int_vec, int, i)
printf("%i\n", *i);
#define GP_VEC_FOREACH(self, type, iterator)
A vector iterator.
Definition gp_vec.h:211

And for a vector of strings:

GP_VEC_FOREACH(strs, char*, str)
printf("%s\n", *str);

Definition at line 211 of file gp_vec.h.

Function Documentation

◆ gp_vec_del()

void * gp_vec_del ( void *  self,
size_t  off,
size_t  len 
)

Deletes a range from the vector.

Parameters
selfA vector.
offAn offset in the vector.
lengthA 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.

Returns
A (new) pointer to the vector.

Referenced by gp_matrix_cols_del(), and gp_vec_strdel().

◆ gp_vec_dup()

void * gp_vec_dup ( void *  self)

Creates a duplicate of a vector.

Creates a new vector with the exact same data.

Parameters
selfVector to be duplicated.
Returns
Returns a pointer to a new vector.

◆ gp_vec_expand()

void * gp_vec_expand ( void *  self,
size_t  len 
)

Expands vector by length elements at the end of the vector.

Parameters
selfA vector.
lengthA 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.

Returns
A (new) pointer to the vector.

◆ gp_vec_expand_()

gp_vec * gp_vec_expand_ ( gp_vec self,
size_t  len 
)

Expands vector length; does not touch vector data.

This is internal rutine, handle with care!

Parameters
selfPointer to the vector structure.
lenHow many units should be added to vector length.
Returns
A pointer to a vector data.

◆ gp_vec_free()

void gp_vec_free ( void *  self)

Frees the vector.

Parameters
selfA vector. The call is no-op on NULL vector.

Referenced by gp_matrix_free().

◆ gp_vec_ins()

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.

Parameters
selfA vector.
offAn offset in the vector.
lengthA number of elements to instert.
Returns
A (new) pointer to the vector.

Referenced by gp_matrix_cols_ins(), gp_vec_chins(), gp_vec_ins_utf8(), and gp_vec_strins().

◆ gp_vec_len()

static size_t gp_vec_len ( const void *  self)
inlinestatic

Returns vector lenght.

Parameters
selfA vector.
Returns
A number of elements in the vector.

Definition at line 111 of file gp_vec.h.

References GP_VEC.

Referenced by gp_vec_str_append(), gp_vec_strlen(), and gp_vec_strsize().

◆ gp_vec_new()

void * gp_vec_new ( size_t  len,
size_t  unit 
)

Allocates a new vector.

Parameters
lenAn initial lenght of the vector.
unitA size of a vector element.
Returns
Returns a pointer to the vector data.

Referenced by gp_matrix_new(), gp_vec_str_new(), and gp_vec_strdup().

◆ gp_vec_resize()

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.

Parameters
selfA vector.
lengthA new vector lenght.
Returns
A (new) pointer to the vector.

Referenced by gp_vec_strclr().

◆ gp_vec_shrink()

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.

Parameters
selfA vector.
lengthA number of elemements to remove.
Returns
A (new) pointer to the vector.

◆ gp_vec_shrink_()

gp_vec * gp_vec_shrink_ ( gp_vec self,
size_t  len 
)

Shrinks vector length; does not touch vector data.

This is internal rutine, handle with care!

Parameters
selfPointer to the vector structure.
lenHow many units should be removed from vector length.
Returns
A pointer to a vector data.

◆ gp_vec_unit()

static size_t gp_vec_unit ( const void *  self)
inlinestatic

Returns vector unit.

Parameters
selfA vector.
Returns
A vector unit, i.e. size of a vector element.

Definition at line 122 of file gp_vec.h.

References GP_VEC.