-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathContainer.h
77 lines (60 loc) · 2.65 KB
/
Container.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
#ifndef CONTAINER_H
#define CONTAINER_H
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#define containerForeach(valueType, iterator, container) for (valueType *iterator = container.values; iterator < container.values + container.numValues; ++iterator)
#define declareContainerInternal(containerType, valueType, constructorTypeName, methodPrefix) \
typedef struct { \
valueType *values; \
int numValues; \
int capacity; \
} containerType; \
containerType make##constructorTypeName(const int capacity); \
valueType * methodPrefix##AddValue(containerType *container, const valueType value); \
void methodPrefix##AddValues(containerType *container, const containerType values); \
void methodPrefix##Clear(containerType *container); \
void methodPrefix##Destroy(containerType *container); \
#define defineContainerInternal(containerType, valueType, constructorTypeName, methodPrefix) \
containerType make##constructorTypeName(const int capacity) { \
\
return (containerType) {malloc(sizeof(valueType) * capacity), 0, capacity}; \
} \
valueType * methodPrefix##AddValue(containerType *container, const valueType value) { \
if (container->capacity > container->numValues) { \
container->values[container->numValues] = value; \
++ container->numValues; \
return &container->values[container->numValues - 1]; \
} else { \
/* TODO: Change this to grow the container dynamically. */ \
assert(0); \
} \
} \
void methodPrefix##AddValues(containerType *container, const containerType values) { \
if (container->capacity >= container->numValues + values.numValues) { \
memcpy(container->values, values.values, sizeof(valueType) * values.numValues); \
container->numValues += values.numValues; \
} else { \
/* TODO: Change this to grow the container dynamically. */ \
assert(0); \
} \
} \
void methodPrefix##Clear(containerType *container) { \
container->numValues = 0; \
} \
void methodPrefix##Destroy(containerType *container) { \
\
free(container->values); \
\
container->numValues = 0; \
container->capacity = 0; \
} \
#define declareContainer(type, lowerCaseType) \
declareContainerInternal(type##Container, type, type##Container, lowerCaseType##Container) \
#define defineContainer(type, lowerCaseType) \
defineContainerInternal(type##Container, type, type##Container, lowerCaseType##Container) \
#define declarePointerContainer(type, lowerCaseType) \
declareContainerInternal(type##PointerContainer, type *, type##PointerContainer, lowerCaseType##PointerContainer) \
#define definePointerContainer(type, lowerCaseType) \
defineContainerInternal(type##PointerContainer, type *, type##PointerContainer, lowerCaseType##PointerContainer) \
#endif // CONTAINER_H