File: tree.c

package info (click to toggle)
libnih 1.0.3-4.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 10,544 kB
  • sloc: ansic: 188,634; sh: 11,217; makefile: 1,116; yacc: 291; xml: 239; sed: 16
file content (621 lines) | stat: -rw-r--r-- 14,184 bytes parent folder | download | duplicates (6)
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/* libnih
 *
 * tree.c - generic binary tree implementation
 *
 * Copyright © 2009 Scott James Remnant <scott@netsplit.com>.
 * Copyright © 2009 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */


#include <nih/macros.h>
#include <nih/logging.h>
#include <nih/alloc.h>

#include "tree.h"


/**
 * nih_tree_init:
 * @tree: tree node to be initialised.
 *
 * Initialise an already allocated tree node, once done it can be used
 * as the start of a new binary tree or added to an existing tree.
 **/
void
nih_tree_init (NihTree *tree)
{
	nih_assert (tree != NULL);

	tree->parent = tree->left = tree->right = NULL;
}

/**
 * nih_tree_new:
 * @parent: parent object for new node.
 *
 * Allocates a new tree structure, usually used as the root of a new
 * binary tree.  You may prefer to allocate the NihTree structure statically
 * and use nih_tree_init() to initialise it instead.
 *
 * The structure is allocated using nih_alloc() so can be used as a context
 * to other allocations.
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned tree node.  When all parents
 * of the returned tree node are freed, the returned tree node will also be
 * freed.
 *
 * Returns: the new tree node or NULL if the allocation failed.
 **/
NihTree *
nih_tree_new (const void *parent)
{
	NihTree *tree;

	tree = nih_new (parent, NihTree);
	if (! tree)
		return NULL;

	nih_tree_init (tree);

	nih_alloc_set_destructor (tree, nih_tree_destroy);

	return tree;
}

/**
 * nih_tree_entry_new:
 * @parent: parent object for new entry.
 *
 * Allocates a new tree entry structure, leaving the caller to set the
 * data of the entry.
 *
 * The structure is allocated using nih_alloc() so can be used as a context
 * to other allocations.
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned tree entry.  When all parents
 * of the returned tree entry are freed, the returned tree entry will also be
 * freed.
 *
 * Returns: the new tree entry or NULL if the allocation failed.
 **/
NihTreeEntry *
nih_tree_entry_new (const void *parent)
{
	NihTreeEntry *tree;

	tree = nih_new (parent, NihTreeEntry);
	if (! tree)
		return NULL;

	nih_tree_init (&tree->node);

	nih_alloc_set_destructor (tree, nih_tree_destroy);

	tree->data = NULL;

	return tree;
}


/**
 * nih_tree_add:
 * @tree: node in the destination tree,
 * @node: node to be added to the tree,
 * @where: where @node should be added.
 *
 * Adds @node to a new binary tree, either as a child of or, or replacing,
 * the existing node @tree.  The exact position is determined by @where,
 * which may be NIH_TREE_LEFT or NIH_TREE_RIGHT to indicate that @node
 * should be a child of @tree or NIH_TREE_HERE to indicate that @node
 * should replace @tree.
 *
 * If @node is already in another tree it is removed so there is no need
 * to call nih_tree_remove() before this function.  There is also no
 * requirement that the trees be different, so this can be used to reorder
 * a tree.
 *
 * Returns: node replaced by @node, normally NULL.
 **/
NihTree *
nih_tree_add (NihTree      *tree,
	      NihTree      *node,
	      NihTreeWhere  where)
{
	NihTree *replaced = NULL;

	nih_assert (tree != NULL);

	if (node)
		nih_tree_remove (node);

	if (where == NIH_TREE_LEFT) {
		replaced = tree->left;
		if (replaced)
			replaced->parent = NULL;

		tree->left = node;
		if (node)
			node->parent = tree;

	} else if (where == NIH_TREE_RIGHT) {
		replaced = tree->right;
		if (replaced)
			replaced->parent = NULL;

		tree->right = node;
		if (node)
			node->parent = tree;

	}

	return replaced;
}


/**
 * nih_tree_remove:
 * @node: node to be removed.
 *
 * Removes @node and its children from the containing tree.  Neither the
 * node nor children are freed, and the children are not unlinked from the
 * node.  Instead the node is returned so that it can be added to another
 * tree (through there's no need to call nih_tree_remove() first if you
 * wanted to do that) or used as the root of a new tree.
 *
 * Returns: @node as a root node.
 **/
NihTree *
nih_tree_remove (NihTree *node)
{
	nih_assert (node != NULL);

	if (node->parent) {
		if (node->parent->left == node) {
			node->parent->left = NULL;
		} else if (node->parent->right == node) {
			node->parent->right = NULL;
		}

		node->parent = NULL;
	}

	return node;
}

/**
 * nih_tree_unlink:
 * @node: node to be removed.
 *
 * Removes @node from its containing tree, as nih_tree_remove() does, but
 * also unlinks the node's children from itself so that they don't have
 * a dangling pointer.
 *
 * Returns: @node.
 **/
NihTree *
nih_tree_unlink (NihTree *node)
{
	nih_assert (node != NULL);

	nih_tree_remove (node);

	if (node->left)
		node->left->parent = NULL;

	if (node->right)
		node->right->parent = NULL;

	node->left = node->right = NULL;

	return node;
}

/**
 * nih_tree_destroy:
 * @node: node to be removed.
 *
 * Removes @node from its containing tree.
 *
 * Normally used or called from an nih_alloc() destructor so that the list
 * item is automatically removed from its containing list when freed.
 *
 * Returns: zero.
 **/
int
nih_tree_destroy (NihTree *node)
{
	nih_assert (node != NULL);

	nih_tree_unlink (node);

	return 0;
}


/**
 * VISIT:
 * @_node: node to check.
 *
 * Macro to expand to check whether a node is set, and whether the filter is
 * either unset or says not to filter this node.
 **/
#define VISIT(_node) ((_node) && ((! filter) || (! filter (data, (_node)))))

/**
 * nih_tree_next_full:
 * @tree: tree to iterate,
 * @node: node just visited,
 * @filter: filter function to test each node,
 * @data: data pointer to pass to @filter.
 *
 * Iterates the @tree in-order non-recursively; to obtain the first node,
 * @tree should be set to the root of the tree and @node should be NULL.
 * Then for subsequent nodes, @node should be the previous return value
 * from this function.
 *
 * If @filter is given, it will be called for each node visited and must
 * return FALSE otherwise the node and its children will be ignored.
 *
 * Returns: next in-order node within @tree or NULL if no further nodes.
 **/
NihTree *
nih_tree_next_full (NihTree       *tree,
		    NihTree       *node,
		    NihTreeFilter  filter,
		    void          *data)
{
	NihTree *prev;

	nih_assert (tree != NULL);

	if (node) {
		prev = node;
		if (VISIT (node->right)) {
			node = node->right;
		} else {
			if (node == tree)
				return NULL;

			node = node->parent;
		}
	} else {
		prev = tree->parent;
		node = tree;
	}

	for (;;) {
		NihTree *tmp = node;

		if ((prev == node->parent) && VISIT (node->left)) {
			node = node->left;
		} else if (VISIT (node->right) && (prev == node->right)) {
			if (node == tree)
				return NULL;

			node = node->parent;
		} else if (VISIT (node)) {
			return node;
		} else {
			return NULL;
		}

		prev = tmp;
	}
}

/**
 * nih_tree_prev_full:
 * @tree: tree to iterate,
 * @node: node just visited,
 * @filter: filter function to test each node,
 * @data: data pointer to pass to @filter.
 *
 * Reverse-iterates the @tree in-order non-recursively; to obtain the last
 * node, @tree should be set to the root of the tree and @node should be NULL.
 * Then for subsequent nodes, @node should be the previous return value
 * from this function.
 *
 * If @filter is given, it will be called for each node visited and must
 * return FALSE otherwise the node and its children will be ignored.
 *
 * Returns: previous in-order node within @tree or NULL if no further nodes.
 **/
NihTree *
nih_tree_prev_full (NihTree       *tree,
		    NihTree       *node,
		    NihTreeFilter  filter,
		    void          *data)
{
	NihTree *prev;

	nih_assert (tree != NULL);

	if (node) {
		prev = node;
		if (VISIT (node->left)) {
			node = node->left;
		} else {
			if (node == tree)
				return NULL;

			node = node->parent;
		}
	} else {
		prev = tree->parent;
		node = tree;
	}

	for (;;) {
		NihTree *tmp = node;

		if ((prev == node->parent) && VISIT (node->right)) {
			node = node->right;
		} else if (VISIT (node->left) && (prev == node->left)) {
			if (node == tree)
				return NULL;

			node = node->parent;
		} else if (VISIT (node)) {
			return node;
		} else {
			return NULL;
		}

		prev = tmp;
	}

	return NULL;
}


/**
 * nih_tree_next_pre_full:
 * @tree: tree to iterate,
 * @node: node just visited,
 * @filter: filter function to test each node,
 * @data: data pointer to pass to @filter.
 *
 * Iterates the @tree in-order non-recursively; to obtain the first node,
 * @tree should be set to the root of the tree and @node should be NULL.
 * Then for subsequent nodes, @node should be the previous return value
 * from this function.
 *
 * If @filter is given, it will be called for each node visited and must
 * return FALSE otherwise the node and its children will be ignored.
 *
 * Returns: next in-order node within @tree or NULL if no further nodes.
 **/
NihTree *
nih_tree_next_pre_full (NihTree       *tree,
			NihTree       *node,
			NihTreeFilter  filter,
			void          *data)
{
	NihTree *prev;

	nih_assert (tree != NULL);

	if (node) {
		prev = node;
		if (VISIT (node->left)) {
			return node->left;
		} else if (VISIT (node->right)) {
			return node->right;
		} else {
			if (node == tree)
				return NULL;

			node = node->parent;
		}
	} else if (VISIT (tree)) {
		return tree;
	} else {
		return NULL;
	}

	for (;;) {
		NihTree *tmp = node;

		if ((prev != node->right) && VISIT (node->right)) {
			return node->right;
		} else {
			if (node == tree)
				return NULL;

			node = node->parent;
		}

		prev = tmp;
	}
}

/**
 * nih_tree_prev_pre_full:
 * @tree: tree to iterate,
 * @node: node just visited,
 * @filter: filter function to test each node,
 * @data: data pointer to pass to @filter.
 *
 * Reverse-iterates the @tree in-order non-recursively; to obtain the last
 * node, @tree should be set to the root of the tree and @node should be NULL.
 * Then for subsequent nodes, @node should be the previous return value
 * from this function.
 *
 * If @filter is given, it will be called for each node visited and must
 * return FALSE otherwise the node and its children will be ignored.
 *
 * Returns: previous in-order node within @tree or NULL if no further nodes.
 **/
NihTree *
nih_tree_prev_pre_full (NihTree       *tree,
			NihTree       *node,
			NihTreeFilter  filter,
			void          *data)
{
	NihTree *prev;

	nih_assert (tree != NULL);

	if (node) {
		prev = node;
		if (node == tree)
			return NULL;

		node = node->parent;
	} else {
		prev = tree->parent;
		node = tree;
	}

	for (;;) {
		NihTree *tmp = node;

		if ((prev == node->parent) && VISIT (node->right)) {
			node = node->right;
		} else if ((prev != node->left) && VISIT (node->left)) {
			node = node->left;
		} else if (VISIT (node)) {
			return node;
		} else {
			return NULL;
		}

		prev = tmp;
	}
}


/**
 * nih_tree_next_post_full:
 * @tree: tree to iterate,
 * @node: node just visited,
 * @filter: filter function to test each node,
 * @data: data pointer to pass to @filter.
 *
 * Iterates the @tree in-order non-recursively; to obtain the first node,
 * @tree should be set to the root of the tree and @node should be NULL.
 * Then for subsequent nodes, @node should be the previous return value
 * from this function.
 *
 * If @filter is given, it will be called for each node visited and must
 * return FALSE otherwise the node and its children will be ignored.
 *
 * Returns: next in-order node within @tree or NULL if no further nodes.
 **/
NihTree *
nih_tree_next_post_full (NihTree       *tree,
			 NihTree       *node,
			 NihTreeFilter  filter,
			 void          *data)
{
	NihTree *prev;

	nih_assert (tree != NULL);

	if (node) {
		prev = node;
		if (node == tree)
			return NULL;

		node = node->parent;
	} else {
		prev = tree->parent;
		node = tree;
	}

	for (;;) {
		NihTree *tmp = node;

		if ((prev == node->parent) && VISIT (node->left)) {
			node = node->left;
		} else if ((prev != node->right) && VISIT (node->right)) {
			node = node->right;
		} else if (VISIT (node)) {
			return node;
		} else {
			return NULL;
		}

		prev = tmp;
	}
}

/**
 * nih_tree_prev_post_full:
 * @tree: tree to iterate,
 * @node: node just visited,
 * @filter: filter function to test each node,
 * @data: data pointer to pass to @filter.
 *
 * Reverse-iterates the @tree in-order non-recursively; to obtain the last
 * node, @tree should be set to the root of the tree and @node should be NULL.
 * Then for subsequent nodes, @node should be the previous return value
 * from this function.
 *
 * If @filter is given, it will be called for each node visited and must
 * return FALSE otherwise the node and its children will be ignored.
 *
 * Returns: previous in-order node within @tree or NULL if no further nodes.
 **/
NihTree *
nih_tree_prev_post_full (NihTree       *tree,
			 NihTree       *node,
			 NihTreeFilter  filter,
			 void          *data)
{
	NihTree *prev;

	nih_assert (tree != NULL);

	if (node) {
		prev = node;
		if (VISIT (node->right)) {
			return node->right;
		} else if (VISIT (node->left)) {
			return node->left;
		} else {
			if (node == tree)
				return NULL;

			node = node->parent;
		}
	} else if (VISIT (tree)) {
		return tree;
	} else {
		return NULL;
	}

	for (;;) {
		NihTree *tmp = node;

		if ((prev != node->left) && VISIT (node->left)) {
			return node->left;
		} else {
			if (node == tree)
				return NULL;

			node = node->parent;
		}

		prev = tmp;
	}
}