aboutsummaryrefslogtreecommitdiff
path: root/include/fwd/vec.h
blob: 1b78c590333b13cc1cd8e97e9e0a86490b14b0e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* SPDX-License-Identifier: copyleft-next-0.3.1 */
/* Copyright 2024 Kim Kuparinen < kimi.h.kuparinen@gmail.com > */

#ifndef VEC_H
#define VEC_H

#include <stddef.h>

struct vec {
	size_t n;
	size_t s;
	size_t ns;
	void *buf;
};

struct vec vec_create(size_t s);
void vec_destroy(struct vec *v);
void vec_reset(struct vec *v);

size_t vec_len(struct vec *v);
void *vec_at(struct vec *v, size_t i);
void *vec_back(struct vec *v);
void *vec_pop(struct vec *v);
void vec_append(struct vec *v, void *n);

typedef int (*vec_comp_t)(const void *, const void *);
void vec_sort(struct vec *v, vec_comp_t comp);

#define foreach_vec(iter, v) \
	for (size_t iter = 0; iter < vec_len(&v); ++iter)

#define vect_at(type, v, i) \
	*(type *)vec_at(&v, i)

#define vect_append(type, v, e) \
	vec_append(&v, (type *)(e))

#define vect_back(type, v) \
	*(type *)vec_back(&v)

#define vect_pop(type, v) \
	*(type *)vec_pop(&v)

#define vec_uninit(v) \
	(v.buf == NULL)

#endif /* VEC_H */