summaryrefslogtreecommitdiff
path: root/src/gevector.h
blob: ce47c6afde2d5b2d4ed6300f258d20f69422bf27 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifndef LIBGE_GEVECTOR_H
#define LIBGE_GEVECTOR_H

#include <stdlib.h>

#define GEVECTOR_API(t, n)                                                      \
	struct n {                                                              \
		double gfactor;                                                 \
		size_t capacity;                                                \
		size_t length;                                                  \
		t *items;                                                       \
	};                                                                      \
	                                                                        \
	int  n##_new(struct n *, size_t, double);                               \
	int  n##_append(struct n *, t);                                         \
	int  n##_insert(struct n *, t, size_t);                                 \
	void n##_remove(struct n *, size_t);                                    \
	int  n##_resize(struct n *, size_t);

#define GEVECTOR_IMPL(t, n)                                                     \
	int                                                                     \
	n##_new(struct n *vec, size_t capacity, double gfactor)                 \
	{                                                                       \
		*vec = (struct n) {                                             \
			.gfactor  = gfactor == 0 ? 1.5f : gfactor,              \
			.capacity = capacity == 0 ? 16 : capacity,              \
			.length   = 0,                                          \
			.items    = calloc(capacity == 0 ? 16 : capacity,       \
			                   sizeof(t))                           \
		};                                                              \
		return vec->items == NULL ? -1 : 0;                             \
	}                                                                       \
	                                                                        \
	int                                                                     \
	n##_append(struct n *vec, t item)                                       \
	{                                                                       \
		return n##_insert(vec, item, vec->length);                      \
	}                                                                       \
	                                                                        \
	int                                                                     \
	n##_insert(struct n *vec, t item, size_t i)                             \
	{                                                                       \
		if (vec->length == vec->capacity) {                             \
			size_t new_capacity =                                   \
				(size_t) (vec->capacity * vec->gfactor);        \
			if (new_capacity == vec->capacity)                      \
				new_capacity++;                                 \
			if (n##_resize(vec, new_capacity) == -1)                \
				return -1;                                      \
		}                                                               \
		for (size_t j = vec->length; j > i; j--)                        \
			vec->items[j] = vec->items[j - 1];                      \
		vec->items[i] = item;                                           \
		vec->length++;                                                  \
		return 0;                                                       \
	}                                                                       \
	                                                                        \
	void                                                                    \
	n##_remove(struct n *vec, size_t i)                                     \
	{                                                                       \
		for (size_t j = i; j < vec->length - 1; j++)                    \
			vec->items[j] = vec->items[j + 1];                      \
		vec->length--;                                                  \
	}                                                                       \
	                                                                        \
	int                                                                     \
	n##_resize(struct n *vec, size_t new_capacity)                          \
	{                                                                       \
		vec->items = realloc(vec->items, sizeof(t) * new_capacity);     \
		if (vec->items == NULL)                                         \
			return -1;                                              \
		vec->capacity = new_capacity;                                   \
		return 0;                                                       \
	}

#endif /* !LIBGE_GEVECTOR_H */