-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlist.h
148 lines (134 loc) · 5.42 KB
/
list.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
#ifndef LIST_H
#define LIST_H
#include <inttypes.h>
#include <stdlib.h>
#include "defs.h"
#define LIST(type) struct { \
uint32_t length; \
uint32_t allocated_length; \
type* objects; \
}
#define initialize_list(list) do { \
(list)->length = 0; \
(list)->allocated_length = 16; \
(list)->objects = malloc(16 * sizeof((list)->objects[0])); \
} while (0)
#define initialize_list_size(list, size) do { \
(list)->length = 0; \
(list)->allocated_length = size; \
(list)->objects = malloc((size) * sizeof((list)->objects[0])); \
} while (0)
#define add_object(list, object) do { \
if ((list)->length == (list)->allocated_length) { \
(list)->objects = realloc((list)->objects, ((list)->allocated_length + ((list)->allocated_length >> 1)) * sizeof(*object)); \
(list)->allocated_length += (list)->allocated_length >> 1; \
} \
\
(list)->objects[(list)->length] = *object; \
(list)->length++; \
} while (0)
#define remove_object(list, index) do { \
(list)->objects[index] = (list)->objects[(list)->length - 1]; \
(list)->length--; \
if ((list)->allocated_length != 16 && (list)->length == (list)->allocated_length >> 1) { \
(list)->objects = realloc((list)->objects, max((list)->length + ((list)->length >> 1), (uint32_t) 16) * sizeof(typeof((list)->objects[0]))); \
} \
} while (0)
#define add_objects(list, position, amount) do { \
if ((list)->allocated_length - ((list)->length) < (amount)) { \
(list)->objects = realloc((list)->objects, ((list)->length + (amount)) * sizeof((list)->objects[0])); \
(list)->allocated_length = (list)->length + (amount); \
} \
\
memcpy(&(list)->objects[(list)->length], position, (amount) * sizeof((list)->objects[0])); \
(list)->length += amount; \
} while (0)
#define find_object_s(list, out_object, key, value) do { \
if ((list)->length != 0) { \
uint32_t position = (list)->length / 2; \
for (int step = position / 2; step > 2; step /= 2) { \
if ((list)->objects[position].key > (value)) \
position -= step; \
else \
position += step; \
} \
while (position > 0 && (list)->objects[position].key > (value)) \
position--; \
while (position < (list)->length-1 && (list)->objects[position].key < (value)) \
position++; \
if ((list)->objects[position].key == (value)) \
(out_object) = &(list)->objects[position]; \
} \
} while (0)
#define add_object_s(list, object, key) do { \
if ((list)->length == 0) { \
(list)->objects[(list)->length] = *object; \
(list)->length++; \
} else { \
if ((list)->length == (list)->allocated_length) { \
(list)->objects = realloc((list)->objects, ((list)->allocated_length + ((list)->allocated_length >> 1)) * sizeof(*object)); \
(list)->allocated_length += (list)->allocated_length >> 1; \
} \
\
uint32_t position = (list)->length / 2; \
for (int step = position / 2; step > 2; step /= 2) { \
if ((list)->objects[position].key > (object)->key) \
position -= step; \
else \
position += step; \
} \
while (position > 0 && (list)->objects[position].key > (object)->key) \
position--; \
while (position < (list)->length && (list)->objects[position].key < (object)->key) \
position++; \
if (position < (list)->length) \
memmove(&(list)->objects[position + 1], &(list)->objects[position], ((list)->length - position) * sizeof(*object)); \
(list)->objects[position] = *object; \
(list)->length++; \
} \
} while (0)
#define sort_list(list, key) do { \
int n = (list)->length; \
__typeof__(list) temp = malloc(sizeof(__typeof__(*list))); \
temp->length = (list)->length; temp->allocated_length = (list)->allocated_length; \
temp->objects = malloc((list)->allocated_length * sizeof(__typeof__((list)->objects[0]))); \
memcpy(temp->objects, (list)->objects, (list)->length * sizeof(__typeof__((list)->objects[0]))); \
int parity = 0; \
for (int width = 1; width < n; width = 2 * width) { \
parity++; \
for (int i = 0; i < n; i = i + 2 * width) { \
if (parity % 2 == 0) \
sortHelper(list, i, min(i+width, n), min(i+2*width, n), temp, key); \
else \
sortHelper(temp, i, min(i+width, n), min(i+2*width, n), list, key); \
} \
} \
if (parity % 2 == 0) { \
free((list)->objects); \
(list)->objects = temp->objects; \
free(temp); \
} else { \
free(temp->objects); \
free(temp); \
} \
} while (0)
#define sortHelper(listLeft, iLeft, iRight, iEnd, listRight, key) do { \
int ___i = iLeft, ___j = iRight; \
for (int ___k = iLeft; ___k < iEnd; ___k++) { \
if (___i < iRight && (___j >= iEnd || (listLeft)->objects[___i].key <= (listLeft)->objects[___j].key)) { \
(listRight)->objects[___k] = (listLeft)->objects[___i]; \
___i++; \
} else { \
(listRight)->objects[___k] = (listLeft)->objects[___j]; \
___j++; \
} \
} \
} while (0)
typedef LIST(uint8_t) uint8_list;
typedef LIST(uint16_t) uint16_list;
typedef LIST(uint32_t) uint32_list;
typedef LIST(uint64_t) uint64_list;
#ifdef __SIZEOF_INT128__
typedef LIST(__uint128_t) uint128_list;
#endif
#endif