-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.h
More file actions
431 lines (345 loc) · 10.7 KB
/
node.h
File metadata and controls
431 lines (345 loc) · 10.7 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// node.h - A node implementation.
//
// Library macros:
// - STRUCTYPES_IMPLEMENTATION: defines all of structype's implementations.
// - STRUCTYPES_DEBUG: if defined, prints error messages.
#ifndef NODE_H_
#define NODE_H_
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
/** A node. */
typedef struct Node {
/** Value of the node. */
void *value;
/** Pointer to the parent node. */
struct Node *parent;
/** Array of children nodes. */
struct Node **nodes;
/** Number of child nodes. */
size_t size;
} Node;
/**
* Defines how a function should handle sorting algorithms:
* - `NONE`: insertion order (no sort).
* - `ASC` : ascending order.
* - `DESC`: descending order.
*/
typedef enum {
NONE,
ASC,
DESC,
} NodeSortOpts;
/**
* Defines how nodes will be printed:
* - indent: starting indentation.
* - indent_step: indent increment on each level.
* - sort: how nodes' values will be sorted. Check docs for `NodeSortOpts`.
*/
typedef struct {
unsigned int indent;
unsigned int indent_step;
NodeSortOpts sort;
} NodePrintOpts;
/**
* Creates a new `Node` with a `value`.
*
* Returns `NULL` when:
* - `malloc` fails to allocate a new `Node`.
*/
Node *node_new(void *value);
/**
* Frees `node->nodes` and `node`.
*
* To free the values of stack or heap allocated nodes, use `node_free_stack`
* or `node_free`, respectively.
*
* Returns `false` when:
* - `node` evaluates to false.
*/
bool node_free_struct(Node *node);
/**
* Frees each node inside `node->nodes` (and their values) and `node->value`,
* then calls `node_free_struct` to cleanup `node->nodes` and `node`.
*
* This is used to free nodes with `heap` allocated values.
* To free nodes with `stack` allocated values, use `node_free_stack`.
*
* Keep in mind that if you free a node that has a parent, not removing that
* index in the parent is a reference to a dead node.
*
* Returns `false` when:
* - `node` evaluates to false.
* - Recursion fails on `node_free`.
* - `node_free_struct` fails.
*/
bool node_free(Node *node);
/**
* Frees each node inside `node->nodes` (not their values), then calls
* `node_free_struct` to cleanup `node->nodes` and `node`.
*
* This is used to free nodes with `stack` allocated values.
* To free nodes with `heap` allocated values, use `node_free`.
*
* Keep in mind that if you free a node that has a parent, not removing that
* index in the parent is a reference to a dead node.
*
* Returns `false` when:
* - `node` evaluates to false.
* - Recursion fails on `node_free`.
* - `node_free_struct` fails.
*/
bool node_free_stack(Node *node);
/**
* Creates a new `Node` with a `parent` and a `value`.
*
* Returns `NULL` when:
* - `parent` evaluates to false.
* - `node_new` fails to create `node`.
* - `node_add_child` fails to append `node` to `parent`.
*/
Node *node_add(Node *parent, void *value);
/**
* Appends `child` to `parent`.
*
* Returns `false` when:
* - `parent` or `child` evaluate to false.
* - `parent` points to the same reference as `child`.
* - `realloc` for `node->nodes` fails.
*/
bool node_add_child(Node *parent, Node *child);
/**
* Same as `node_print_deep`, but with style options.
*
* Nodes with `size == 0` will be printed first always.
* `opts` changes the function's behaviour. Check docs for `NodePrintOpts`.
*
* Returns `false` when:
* - `node` evaluates to false.
* - `node->value` is `NULL`.
* - `opts.indent_step < 1`.
*/
bool node_print(Node *node, NodePrintOpts opts);
/**
* A "deep" print of a `Node`.
*
* Prints the value of `node` and recurses over each child node (and their
* child nodes too).
*
* Use `node_print_shallow` to print just the immediate child nodes of `node`.
*
* Returns `false` when:
* - `node` evaluates to false.
*/
bool node_print_deep(Node *node);
/**
* A "shallow" print of a `Node`.
*
* Prints the value of `node` and the value of its immediate child nodes, but
* it doesn't recurse into deeper nodes.
*
* Use `node_print_deep` to print deeper nodes of `node`.
*
* Returns `false` when:
* - `node` evaluates to false.
*/
bool node_print_shallow(Node *node);
/**
* Prints the values of `parent`, `value` and `size` of `node`.
*
* Returns `false` when:
* - `node` evaluates to false.
*/
bool node_info(Node *node);
/**
* Counts the total of child nodes in `node` (doesn't count `node`).
*
* Returns `0` when:
* - `node` evaluates to false.
*/
size_t node_total(Node *node);
/**
* Returns a NodePrintOpts with the defined default values:
*
* By defining the implementation macros, the default values are:
* - indent: 0
* - indent_step: 2
* - sort: `NONE`
*/
NodePrintOpts nodeprintopts_default();
#endif // NODE_H_
// This allows the user to use an implementation in more than one file without linker errors
#if !defined(__STRUCTYPES_NODE_IMPLEMENTED) && (defined(STRUCTYPES_IMPLEMENTATION))
#define __STRUCTYPES_NODE_IMPLEMENTED
#ifdef STRUCTYPES_DEBUG
static void _node_err(const char *file, int line, const char *func, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
fprintf(stderr, "[%s:%d] %s: ", file, line, func);
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
}
#define NODE_THROW(ret, fmt, ...) ({ _node_err(__FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__); return ret; })
#else
#define NODE_THROW(ret, fmt, ...) ({ return ret; })
#endif // STRUCTYPES_DEBUG
static int _indent = 0;
static int _indent_step = 2;
static NodeSortOpts _sort;
static int _node_comp(const void *a, const void *b) {
const Node *n1 = a;
const Node *n2 = b;
const char *s1 = *(const char **)n1->value;
const char *s2 = *(const char **)n2->value;
while (*s1 && *s2 && *s1 == *s2) s1++, s2++;
int diff = (unsigned char)*s1 - (unsigned char)*s2;
return (_sort == ASC) ? diff : -diff;
}
static void _print_indent(unsigned int indent, void *value) {
printf("%*s%s\n", indent, "", (char *)value);
}
Node *node_new(void *value) {
Node *node = malloc(sizeof(Node));
if (!node) NODE_THROW(NULL, "malloc error on initialize root");
node->value = value;
node->parent = NULL;
node->nodes = NULL;
node->size = 0;
return node;
}
bool node_free_struct(Node *node) {
if (!node) NODE_THROW(false, "node evaluates to false");
if (node->nodes) free(node->nodes);
free(node);
node = NULL;
return true;
}
bool node_free(Node *node) {
if (!node) NODE_THROW(false, "node evaluates to false");
if (node->nodes) {
for (size_t i = 0; i < node->size; i++) {
if (!node_free(node->nodes[i]))
NODE_THROW(false, "recursion error");
}
}
if (node->value != NULL) free(node->value);
return node_free_struct(node);
}
bool node_free_stack(Node *node) {
if (!node) NODE_THROW(false, "node evaluates to false");
if (node->nodes) {
for (size_t i = 0; i < node->size; i++) {
if (!node_free_stack(node->nodes[i]))
NODE_THROW(false, "recursion error");
}
}
return node_free_struct(node);
}
Node *node_add(Node *parent, void *value) {
if (!parent) NODE_THROW(NULL, "parent evaluates to false");
Node *node = node_new(value);
if (!node) NODE_THROW(NULL, "malloc error on initialize root");
if (!node_add_child(parent, node))
NODE_THROW(NULL, "error adding node to parent");
return node;
}
bool node_add_child(Node *parent, Node *child) {
if (!parent) NODE_THROW(false, "parent evaluates to false");
if (!child) NODE_THROW(false, "child evaluates to false");
if (parent == child)
NODE_THROW(false, "parent points to the same reference as child");
void *tmp = realloc(parent->nodes, (parent->size + 1) * sizeof(Node *));
if (!tmp) NODE_THROW(false, "realloc error");
child->parent = parent;
parent->nodes = tmp;
parent->nodes[parent->size++] = child;
return true;
}
bool node_print(Node *node, NodePrintOpts opts) {
if (!node) NODE_THROW(false, "node evaluates to false");
if (opts.indent_step < 1)
NODE_THROW(false, "indent_step must be greater than 0");
_print_indent(opts.indent, node->value);
if (!node->size) return true;
size_t sizeofNode = sizeof(void *);
Node **zero = malloc(sizeofNode * node->size);
if (!zero) NODE_THROW(false, "malloc error for zero");
Node **nonZero = malloc(sizeofNode * node->size);
if (!nonZero) {
free(zero);
NODE_THROW(false, "malloc error for nonZero");
}
size_t zeroLen = 0;
size_t nonZeroLen = 0;
for (size_t i = 0; i < node->size; i++) {
if (!node->nodes[i]->size) zero[zeroLen++] = node->nodes[i];
else nonZero[nonZeroLen++] = node->nodes[i];
}
if (opts.sort != NONE) {
_sort = opts.sort;
qsort(zero, zeroLen, sizeofNode, _node_comp);
qsort(nonZero, nonZeroLen, sizeofNode, _node_comp);
}
opts.indent += opts.indent_step;
for (size_t i = 0; i < zeroLen; i++)
_print_indent(opts.indent, zero[i]->value);
for (size_t i = 0; i < nonZeroLen; i++)
if (!node_print(nonZero[i], opts)) {
free(zero);
free(nonZero);
NODE_THROW(false, "recursion error");
}
free(zero);
free(nonZero);
return true;
}
bool node_print_deep(Node *node) {
if (!node) NODE_THROW(false, "node evaluates to false");
_print_indent(_indent, node->value);
_indent += _indent_step;
size_t local_indent = _indent;
for (size_t i = 0; i < node->size; i++) {
Node *child = node->nodes[i];
if (child->size < 1) {
_print_indent(_indent, child->value);
continue;
}
if (!node_print_deep(child)) NODE_THROW(false, "recursion error");
_indent = local_indent;
}
return true;
}
bool node_print_shallow(Node *node) {
if (!node) NODE_THROW(false, "node evaluates to false");
printf("%s\n", (char *)node->value);
for (size_t i = 0; i < node->size; i++)
_print_indent(_indent_step, node->nodes[i]->value);
return true;
}
bool node_info(Node *node) {
if (!node) NODE_THROW(false, "node evaluates to false");
printf("parent: %s\n", node->parent
? (char *)node->parent->value
: "null"
);
printf("value: %s\n", (char *)node->value);
printf("size: %zu\n", node->size);
return true;
}
size_t node_total(Node *node) {
if (!node) NODE_THROW(0, "node evaluates to false");
size_t total = node->size;
for (size_t i = 0; i < node->size; i++)
total += node_total(node->nodes[i]);
return total;
}
NodePrintOpts nodeprintopts_default() {
return (NodePrintOpts) {
.indent = 0,
.indent_step = 2,
.sort = ASC,
};
}
#endif // STRUCTYPES_IMPLEMENTATION