GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_vec.h
Go to the documentation of this file.
1//SPDX-License-Identifier: LGPL-2.0-or-later
2/*
3
4 Copyright (C) 2020 Richard Palethorpe <richiejp@f-m.fm>
5 Copyright (C) 2020 Cyril Hrubis <metan@ucw.cz>
6
7*/
8
14#include <stddef.h>
15#include <core/gp_common.h>
16#include <core/gp_compiler.h>
17
18#ifndef GP_VEC_H
19#define GP_VEC_H
20
27#define GP_VEC(ptr) (GP_CONTAINER_OF(ptr, gp_vec, payload))
28
30typedef struct gp_vec {
32 size_t unit;
34 size_t capacity;
36 size_t length;
38 char payload[];
40
51GP_WUR gp_vec *gp_vec_expand_(gp_vec *self, size_t len);
52
63GP_WUR gp_vec *gp_vec_shrink_(gp_vec *self, size_t len);
64
73GP_WUR void *gp_vec_new(size_t len, size_t unit);
74
83GP_WUR void *gp_vec_dup(void *self);
84
90void gp_vec_free(void *self);
91
103GP_WUR void *gp_vec_resize(void *self, size_t len);
104
111static inline size_t gp_vec_len(const void *self)
112{
113 return GP_VEC(self)->length;
114}
115
122static inline size_t gp_vec_unit(const void *self)
123{
124 return GP_VEC(self)->unit;
125}
126
149GP_WUR void *gp_vec_ins(void *self, size_t off, size_t len);
150
163GP_WUR void *gp_vec_expand(void *self, size_t len);
164
177GP_WUR void *gp_vec_del(void *self, size_t off, size_t len);
178
188GP_WUR void *gp_vec_shrink(void *self, size_t len);
189
211#define GP_VEC_FOREACH(self, type, iterator) \
212 for (type *iterator = (self); iterator < (self) + gp_vec_len(self); iterator++)
213
221#define GP_VEC_APPEND(vec, val) ({ \
222 typeof(vec) gp_ret__ = gp_vec_expand(vec, 1); \
223 if (gp_ret__) { \
224 gp_ret__[gp_vec_len(gp_ret__)-1] = val; \
225 vec = gp_ret__; \
226 } \
227 gp_ret__; \
228})
229
230#endif /* GP_VEC_H */
Common macros.
A compiler dependent macros.
#define GP_WUR
Expands to warn_unused_result attribute when supported by the compiler.
Definition gp_compiler.h:26
void * gp_vec_shrink(void *self, size_t len)
Shrinks vector by length elements at the end of the vector.
#define GP_VEC(ptr)
Converts a vector pointer into the header i.e. struct gp_vec.
Definition gp_vec.h:27
void * gp_vec_resize(void *self, size_t len)
Resize vector.
gp_vec * gp_vec_shrink_(gp_vec *self, size_t len)
Shrinks vector length; does not touch vector data.
void * gp_vec_expand(void *self, size_t len)
Expands vector by length elements at the end of the vector.
void * gp_vec_dup(void *self)
Creates a duplicate of a vector.
static size_t gp_vec_len(const void *self)
Returns vector lenght.
Definition gp_vec.h:111
void * gp_vec_del(void *self, size_t off, size_t len)
Deletes a range from the vector.
static size_t gp_vec_unit(const void *self)
Returns vector unit.
Definition gp_vec.h:122
void * gp_vec_new(size_t len, size_t unit)
Allocates a new vector.
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 nee...
gp_vec * gp_vec_expand_(gp_vec *self, size_t len)
Expands vector length; does not touch vector data.
void gp_vec_free(void *self)
Frees the vector.
A growable vector.
Definition gp_vec.h:30
size_t unit
Definition gp_vec.h:32
char payload[]
Definition gp_vec.h:38
size_t capacity
Definition gp_vec.h:34
size_t length
Definition gp_vec.h:36