-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvec.h
85 lines (69 loc) · 2.25 KB
/
vec.h
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
77
78
79
80
81
82
83
84
85
/* dvbindex - a program for indexing DVB streams
Copyright (C) 2017 Daniel Kamil Kozar
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef DVBINDEX_VEC_H
#define DVBINDEX_VEC_H
/* clang-format off */
#include <stddef.h>
#include <stdlib.h>
#define VEC_INITIAL_CAP 32
#define VEC_RESIZE_IF_NEEDED(v) do {\
if (v->size == v->cap) { \
v->cap *= 2; \
v->data = realloc(v->data, v->cap * sizeof(v->data[0])); \
if (v->data == 0) return 0; \
} \
} while(0)
#define VEC_DEFINE_PUSH(Type) \
static inline int vec_##Type##_push(vec_##Type *v, Type e) { \
VEC_RESIZE_IF_NEEDED(v); \
v->data[v->size++] = e; \
return 1; \
}
#define VEC_DEFINE_INIT(Type) \
static inline int vec_##Type##_init(vec_##Type *v) { \
v->data = malloc(VEC_INITIAL_CAP * sizeof(v->data[0])); \
if (v->data == 0) return 0; \
v->size = 0; \
v->cap = VEC_INITIAL_CAP; \
return 1; \
}
#define VEC_DEFINE_DESTROY(Type) \
static inline void vec_##Type##_destroy(vec_##Type *v) { \
free(v->data); \
v->data = 0; \
v->size = v->cap = 0; \
}
#define VEC_DEFINE_WRITE(Type) \
static inline Type* vec_##Type##_write(vec_##Type *v) { \
VEC_RESIZE_IF_NEEDED(v); \
return &v->data[v->size++]; \
}
#define VEC_DEFINE_BACK(Type) \
static inline Type* vec_##Type##_back(vec_##Type *v) { \
return v->size ? (v->data + (v->size - 1)) : 0; \
}
#define VEC_DEFINE_TYPE(Type) \
typedef struct { \
Type* data; \
size_t size; \
size_t cap; \
} vec_##Type;
#define VEC_DEFINE(Type) \
VEC_DEFINE_TYPE(Type) \
VEC_DEFINE_INIT(Type) \
VEC_DEFINE_DESTROY(Type) \
VEC_DEFINE_PUSH(Type) \
VEC_DEFINE_WRITE(Type) \
VEC_DEFINE_BACK(Type)
#endif