-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_impl.h
281 lines (274 loc) · 19.3 KB
/
list_impl.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*******
* Copyright 2017 Laurent Farhi
*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, version 3.
*
* This file 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this file. If not, see <http://www.gnu.org/licenses/>.
*****/
////////////////////////////////////////////////////
// Original Author: Laurent Farhi
// Date: 22/1/2017
// Contact: [email protected]
//
// Based on design from Randy Gaul's Game Programming Blog http://cecilsunkure.blogspot.fr/2012/08/generic-programming-in-c.html
////////////////////////////////////////////////////
#pragma once
#ifndef __LIST_IMPL_H__
#define __LIST_IMPL_H__
#define _GNU_SOURCE
#include <stdlib.h>
#include <errno.h>
#include "bnode_impl.h"
#include "list.h"
#include "defops.h"
DEFINE_OPERATORS(__list_dummy__);
//
// DEFINE_LIST
// Purpose: Defines a linked list of a specific type on the heap.
//
#define DEFINE_LIST( TYPE ) \
\
DEFINE_BNODE(__list_dummy__, TYPE); \
\
static void LIST_CLEAR_##TYPE ( LIST_##TYPE *self ); \
static BNODE___list_dummy___##TYPE *LIST_INSERT_##TYPE ( LIST_##TYPE *self, BNODE___list_dummy___##TYPE *here, BNODE___list_dummy___##TYPE *node ); \
static int LIST_REMOVE_##TYPE ( LIST_##TYPE *self, BNODE___list_dummy___##TYPE *node ); \
static size_t LIST_UNIQUE_##TYPE ( LIST_##TYPE *self, int (*less_than) (TYPE, TYPE)); \
static void LIST_SORT_##TYPE ( LIST_##TYPE *self, int (*less_than) (TYPE, TYPE)); \
static int LIST_MOVE_##TYPE ( LIST_##TYPE *to, BNODE___list_dummy___##TYPE *hereto, LIST_##TYPE *from, BNODE___list_dummy___##TYPE *herefrom ); \
static int LIST_SWAP_##TYPE ( LIST_##TYPE *la, BNODE___list_dummy___##TYPE *nodea, LIST_##TYPE *lb, BNODE___list_dummy___##TYPE *nodeb ); \
static int LIST_REVERSE_##TYPE ( LIST_##TYPE *self, BNODE___list_dummy___##TYPE *nodea, BNODE___list_dummy___##TYPE *nodeb ); \
static BNODE___list_dummy___##TYPE *LIST_END_##TYPE ( LIST_##TYPE *self ); \
\
static BNODE___list_dummy___##TYPE LIST_NULL_##TYPE = { .vtable = &BNODE_VTABLE___list_dummy___##TYPE }; \
\
static const _LIST_VTABLE_##TYPE LIST_VTABLE_##TYPE = \
{ \
BNODE_CREATE___list_dummy___##TYPE, \
LIST_CLEAR_##TYPE, \
LIST_INSERT_##TYPE, \
LIST_REMOVE_##TYPE, \
LIST_UNIQUE_##TYPE, \
LIST_SORT_##TYPE, \
LIST_MOVE_##TYPE, \
LIST_SWAP_##TYPE, \
LIST_REVERSE_##TYPE, \
LIST_END_##TYPE, \
}; \
\
LIST_##TYPE *LIST_CREATE_##TYPE( void ) \
{ \
LIST_##TYPE *linkedList = malloc( sizeof( *linkedList ) ); \
if (!linkedList) \
return 0; \
\
linkedList->vtable = &LIST_VTABLE_##TYPE; \
linkedList->tree_locked = 0; \
linkedList->root = 0; \
linkedList->null = &LIST_NULL_##TYPE; \
\
return linkedList; \
} \
\
static BNODE___list_dummy___##TYPE *LIST_INSERT_##TYPE ( LIST_##TYPE *self, BNODE___list_dummy___##TYPE *here, BNODE___list_dummy___##TYPE *node ) \
{ \
if (node->parent) \
{ \
errno = EINVAL; \
return 0; \
} \
\
if (self->tree_locked) \
{ \
fprintf (stderr, "ERROR: " "Collection cannot be modified here.\n" \
"ABORT " "\n"); \
fflush (0) ; raise (SIGABRT); \
return 0; \
} \
\
if (self->root) \
{ \
if (BNODE_TREE_INSERT_BEFORE___list_dummy___##TYPE (&(self->root), here, node) != EXIT_SUCCESS) \
return 0; \
} \
else \
self->root = node; \
\
return node; \
} \
\
static int LIST_REMOVE_##TYPE ( LIST_##TYPE *self, BNODE___list_dummy___##TYPE *node ) \
{ \
if (!self->root) \
{ \
errno = EINVAL; \
return EXIT_FAILURE; \
} \
\
/* Go to root */ \
BNODE___list_dummy___##TYPE *n; \
for (n = node ; n->parent ; n = n->parent) /* nop */ ; \
/* Check that herefrom is owned by from */ \
if (n != self->root) \
{ \
errno = EINVAL; \
return EXIT_FAILURE; \
} \
\
if (self->tree_locked) \
{ \
fprintf (stderr, "ERROR: " "Collection cannot be modified here.\n" \
"ABORT " "\n"); \
fflush (0) ; raise (SIGABRT); \
return EXIT_FAILURE; \
} \
\
BNODE___list_dummy___##TYPE *new_node = BNODE_REMOVE___list_dummy___##TYPE(node); \
if (self->root == node) \
self->root = new_node; \
BNODE_DESTROY___list_dummy___##TYPE(node); \
\
return EXIT_SUCCESS; \
} \
\
static int LIST_MOVE_##TYPE ( LIST_##TYPE *to, BNODE___list_dummy___##TYPE *hereto, LIST_##TYPE *from, BNODE___list_dummy___##TYPE *herefrom ) \
{ \
if (!from->root) \
{ \
errno = EINVAL; \
return EXIT_FAILURE; \
} \
\
/* Go to root */ \
BNODE___list_dummy___##TYPE *n; \
for (n = herefrom ; n->parent ; n = n->parent) /* nop */ ; \
/* Check that herefrom is owned by from */ \
if (n != from->root) \
{ \
errno = EINVAL; \
return EXIT_FAILURE; \
} \
\
if (from->tree_locked) \
{ \
fprintf (stderr, "ERROR: " "Collection cannot be modified here.\n" \
"ABORT " "\n"); \
fflush (0) ; raise (SIGABRT); \
return EXIT_FAILURE; \
} \
\
BNODE___list_dummy___##TYPE *nextfrom = BNODE_NEXT(herefrom); \
BNODE___list_dummy___##TYPE *new_node = BNODE_REMOVE___list_dummy___##TYPE(herefrom); \
if (from->root == herefrom) \
from->root = new_node; \
if (LIST_INSERT_##TYPE (to, hereto, herefrom) == 0) \
{ \
errno = EINVAL; \
LIST_INSERT_##TYPE (from, nextfrom, herefrom); /* undo removal */ \
return EXIT_FAILURE; \
} \
\
return EXIT_SUCCESS; \
} \
\
static void LIST_CLEAR_##TYPE ( LIST_##TYPE *self ) \
{ \
if (self->root) \
BNODE_DESTROY___list_dummy___##TYPE(self->root); \
self->root = 0; \
} \
\
static size_t LIST_UNIQUE_##TYPE ( LIST_##TYPE *self, int (*less_than) (TYPE, TYPE)) \
{ \
if (LIST_SIZE (self) < 2) \
return 0; \
\
size_t ret = 0; \
LNODE(TYPE) *next; \
for (LNODE(TYPE) *node = LIST_BEGIN(self) ; node != LIST_END(self) && (next = BNODE_NEXT(node)) != LIST_END(self) ; /* nop */) \
if (BNODE_CMP_VALUE___list_dummy___##TYPE(&node, &next, &less_than) == 0) \
{ \
ret++; \
LIST_REMOVE (self, next); \
next = BNODE_NEXT(node); \
} \
else \
node = next; \
\
return ret; \
} \
\
static void LIST_SORT_##TYPE ( LIST_##TYPE *self, int (*less_than) (TYPE, TYPE)) \
{ \
if (LIST_SIZE (self) < 2) \
return; \
\
LNODE(TYPE) **array = malloc (LIST_SIZE (self) * sizeof (*array)); \
if (!array) \
return; \
\
size_t index = 0; \
for (LNODE(TYPE) *node = LIST_BEGIN(self) ; node != LIST_END(self) ; node = BNODE_NEXT(node)) \
{ \
array[index] = node; \
index++; \
} \
\
qsort_r (array, index, sizeof (*array), BNODE_CMP_VALUE___list_dummy___##TYPE, &less_than); \
\
for (size_t i = 0 ; i < index ; i++) \
LIST_MOVE (self, LIST_END (self), self, array[i]); \
\
free (array); \
} \
\
static int LIST_SWAP_##TYPE ( LIST_##TYPE *la, BNODE___list_dummy___##TYPE *nodea, LIST_##TYPE *lb, BNODE___list_dummy___##TYPE *nodeb ) \
{ \
if (nodea == nodeb) \
return EXIT_SUCCESS; \
\
BNODE___list_dummy___##TYPE *nextb = BNODE_NEXT (nodeb); \
\
if (LIST_MOVE (la, nodea, lb, nodeb) == EXIT_FAILURE) \
return EXIT_FAILURE; \
else if (LIST_MOVE (lb, nextb, la, nodea) == EXIT_FAILURE) \
{ \
LIST_MOVE (lb, nextb, la, nodeb); /* undo first move */ \
return EXIT_FAILURE; \
} \
\
return EXIT_SUCCESS; \
} \
\
static int LIST_REVERSE_##TYPE ( LIST_##TYPE *self, BNODE___list_dummy___##TYPE *nodea, BNODE___list_dummy___##TYPE *nodeb ) \
{ \
if (nodea == nodeb) \
return EXIT_SUCCESS; \
\
BNODE___list_dummy___##TYPE *n; \
BNODE___list_dummy___##TYPE *to = nodeb; \
for (BNODE___list_dummy___##TYPE *from = nodea ; from && BNODE_NEXT (from) != to ; to = from, from = n) \
{ \
n = BNODE_NEXT ( from ); \
if (LIST_MOVE (self, to, self, from) == EXIT_FAILURE) \
return EXIT_FAILURE; \
} \
\
return EXIT_SUCCESS; \
} \
\
static BNODE___list_dummy___##TYPE *LIST_END_##TYPE ( LIST_##TYPE *self ) \
{ \
(void)self; \
return 0; \
} \
struct __useless_struct_LIST_IMPL_##TYPE##_IMPL
#endif