-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist.h
More file actions
73 lines (70 loc) · 1.51 KB
/
list.h
File metadata and controls
73 lines (70 loc) · 1.51 KB
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
#ifndef LIST_H
#define LIST_H
#include <stdlib.h>
#include "mcc.h"
#define LIST(X, T, compare_func) \
struct X ## _list_t \
{ \
size_t used; \
size_t size; \
T *items; \
}; \
\
static inline void X ## _list_init(struct X ## _list_t *list) \
{ \
list->used = 0; \
list->size = 0; \
list->items = NULL; \
} \
\
static inline void X ## _list_free(struct X ## _list_t *list) \
{ \
free(list->items); \
} \
\
static inline void X ## _list_add(struct X ## _list_t *list, T item) \
{ \
if (list->used >= list->size) \
{ \
list->size += 64U; \
T *new_items = realloc(list->items, sizeof *list->items * list->size); \
if (new_items == NULL) \
{ \
LOG("Reallocating %s list to %zu items (%zu bytes) failed\n", #X, list->size, sizeof *list->items * list->size); \
list->size -= 64U; \
return; \
} \
list->items = new_items; \
} \
list->items[list->used++] = item; \
} \
\
static inline void X ## _list_del_item(struct X ## _list_t *list, T item) \
{ \
size_t i; \
for (i = 0; i < list->used; i++) \
{ \
if (compare_func(&list->items[i], &item)) { \
list->items[i] = list->items[--list->used]; \
return; \
} \
} \
} \
\
static inline void X ## _list_del_index(struct X ## _list_t *list, size_t index) \
{ \
list->items[index] = list->items[--list->used]; \
} \
\
static inline bool X ## _list_contains(struct X ## _list_t *list, T item) \
{ \
size_t i; \
for (i = 0; i < list->used; i++) \
{ \
if (compare_func(&list->items[i], &item)) { \
return true; \
} \
} \
return false; \
}
#endif /* LIST_H */