-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.h
180 lines (151 loc) · 5.17 KB
/
array.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Nikita Belov <[email protected]>
* 2016
*/
#ifndef ARRAY_H
#define ARRAY_H
#include <stdint.h>
#include <stdlib.h>
/* Use: array(typename) array = array_new(ARRAY_VECTOR, typename). */
#define ARRAY_VECTOR -1
/* Use to indicate these arrays. */
#define array(type) type *
/* Internal use only. */
#define ARRAY_HEADER_SIZE \
(sizeof(void *) < 8 ? 8 : sizeof(void *))
typedef struct {
/*
* first sizeof(void *) bytes is pointer to this struct,
* next bytes are user data
*/
void *data;
int type;
int used;
int allocated;
int item_size;
} array_t;
/* For convenience. */
typedef unsigned char uchar;
/* Internal use only. */
void *array_get_data(array_t *array)
{
return (uchar *)array->data + ARRAY_HEADER_SIZE;
}
/* Internal use only. */
array_t *array_get_ptr(void *array_data)
{
return (array_t *)(*(uintptr_t *)((uchar *)array_data - ARRAY_HEADER_SIZE));
}
/* Use: array(typename) array = array_new(count, typename). */
#define array_new(count, type) \
array_new_func(count, sizeof(type))
/* Internal use only. */
void *array_new_func(int size, int item_size)
{
array_t *array = calloc(sizeof(*array), 1);
if (array == NULL) {
printf("Failed to allocate %zu bytes.\n", sizeof(*array));
exit(4);
}
if (size == ARRAY_VECTOR) {
size = 0;
array->type = ARRAY_VECTOR;
}
size_t allocate = (size_t)ARRAY_HEADER_SIZE + size * item_size;
array->data = calloc(allocate, 1);
if (array->data == NULL) {
printf("Failed to allocate %zu bytes.\n", allocate);
exit(4);
}
array->used = 0;
array->allocated = size;
array->item_size = item_size;
*(uintptr_t *)array->data = (uintptr_t)array;
return array_get_data(array);
}
/* Use: array_fill(array, value). */
#define array_fill(array_data, value) \
do { \
for (int i = 0; i < array_size(array_data); i++) { \
array_data[i] = value; \
} \
} while(0)
/* Use: array_concatenate(&result_array, array1, array2). */
void array_concatenate(void *result_data_ptr, void *array1_data, void *array2_data)
{
void **result_data = (void **)result_data_ptr;
array_t *result = array_get_ptr(*result_data);
array_t *array1 = array_get_ptr(array1_data);
array_t *array2 = array_get_ptr(array2_data);
int item_size = result->item_size;
size_t allocate = (size_t)sizeof(void *) + (array1->allocated + array2->allocated) * item_size;
result->data = realloc(result->data, allocate);
if (result->data == NULL) {
printf("Failed to allocate %zu bytes.\n", allocate);
exit(4);
}
memcpy((uchar *)result->data + sizeof(void *),
array1_data, array1->allocated * item_size);
memcpy((uchar *)result->data + sizeof(void *) + array1->allocated * item_size,
array2_data, array2->allocated * item_size);
result->allocated = array1->allocated + array2->allocated;
*result_data = array_get_data(result);
}
/* Use: array_copy(destination_array, source_array, index of first item, count of items). */
void array_copy(void *destination_data, void *source_data, int first, int count)
{
int item_size = array_get_ptr(destination_data)->item_size;
memcpy(destination_data, (uchar *)source_data + first * item_size, count * item_size);
}
/* Use: array_delete(&array). */
void array_delete(void *array_data_ptr)
{
void **array_data = (void **)array_data_ptr;
array_t *array = array_get_ptr(*array_data);
free(array->data);
free(array);
*array_data = NULL;
}
/* Internal use only. */
void array_expand(void *array_data_ptr)
{
void **array_data = (void **)array_data_ptr;
array_t *array = array_get_ptr(*array_data);
if (array->used < array->allocated) {
return;
}
if (array->allocated == 0) {
array->allocated = 16;
} else {
array->allocated *= 2;
}
size_t allocate = (size_t)sizeof(void *) + array->allocated * array->item_size;
array->data = realloc(array->data, allocate);
if (array->data == NULL) {
printf("Failed to allocate %zu bytes.\n", allocate);
exit(4);
}
*array_data = array_get_data(array);
}
/* Use: int item_size = array_item_size(array). */
int array_item_size(void *array_data)
{
return array_get_ptr(array_data)->item_size;
}
/* Use: array_push(&array, value). */
#define array_push(array_data, value) \
do { \
array_expand(array_data); \
(*array_data)[array_get_ptr(*array_data)->used++] = value; \
} while (0)
/* Use: int size = array_size(array). */
int array_size(void *array_data)
{
array_t *array = array_get_ptr(array_data);
if (array->type == ARRAY_VECTOR) {
return array->used;
} else {
return array->allocated;
}
}
#endif /* ARRAY_H */