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) 2024 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
113static inline size_t gp_vec_len(const void *self)
114{
115 if (!self)
116 return 0;
117
118 return GP_VEC(self)->length;
119}
120
129static inline size_t gp_vec_unit(const void *self)
130{
131 if (!self)
132 return 0;
133
134 return GP_VEC(self)->unit;
135}
136
159GP_WUR void *gp_vec_ins(void *self, size_t off, size_t len);
160
173GP_WUR void *gp_vec_expand(void *self, size_t len);
174
187GP_WUR void *gp_vec_del(void *self, size_t off, size_t len);
188
198GP_WUR void *gp_vec_shrink(void *self, size_t len);
199
211GP_WUR void *gp_vec_move_shrink(void *self, size_t idx);
212
234#define GP_VEC_FOREACH(self, type, iterator) \
235 for (type *iterator = (self); iterator < (self) + gp_vec_len(self); iterator++)
236
244#define GP_VEC_APPEND(vec, val) ({ \
245 typeof(vec) gp_ret__ = gp_vec_expand(vec, 1); \
246 if (gp_ret__) { \
247 gp_ret__[gp_vec_len(gp_ret__)-1] = val; \
248 vec = gp_ret__; \
249 } \
250 gp_ret__; \
251})
252
253#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:27
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:113
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:129
void * gp_vec_move_shrink(void *self, size_t idx)
Moves last object in vector to idx and shrinks the vector by one.
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