summaryrefslogtreecommitdiff
path: root/fitz/node_tree.c
blob: 6dc035e1dd49b79d4dadf412f1cca84cffd3be4e (plain)
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
#include "fitz_base.h"
#include "fitz_tree.h"

fz_error
fz_newtree(fz_tree **treep)
{
	fz_tree *tree;

	tree = *treep = fz_malloc(sizeof (fz_tree));
	if (!tree)
		return fz_rethrow(-1, "out of memory");

	tree->refs = 1;
	tree->root = nil;
	tree->head = nil;

	return fz_okay;
}

fz_tree *
fz_keeptree(fz_tree *tree)
{
	tree->refs ++;
	return tree;
}

void
fz_droptree(fz_tree *tree)
{
	if (--tree->refs == 0)
	{
		if (tree->root)
			fz_dropnode(tree->root);
		fz_free(tree);
	}
}

fz_rect
fz_boundtree(fz_tree *tree, fz_matrix ctm)
{
	if (tree->root)
		return fz_boundnode(tree->root, ctm);
	return fz_emptyrect;
}

void
fz_insertnodefirst(fz_node *parent, fz_node *child)
{
	child->parent = parent;
	child->next = parent->first;
	parent->first = child;
	if (!parent->last)
		parent->last = child;
}

void
fz_insertnodelast(fz_node *parent, fz_node *child)
{
	child->parent = parent;
	if (!parent->first)
		parent->first = child;
	else
		parent->last->next = child;
	parent->last = child;
}

void
fz_insertnodeafter(fz_node *prev, fz_node *child)
{
	fz_node *parent = prev->parent;
	child->parent = parent;
	if (parent->last == prev)
		parent->last = child;
	child->next = prev->next;
	prev->next = child;
}

void
fz_removenode(fz_node *child)
{
	fz_node *parent = child->parent;
	fz_node *prev;
	fz_node *node;

	if (parent->first == child)
	{
		parent->first = child->next;
		if (parent->last == child)
			parent->last = nil;
		return;
	}

	prev = parent->first;
	node = prev->next;

	while (node)
	{
		if (node == child)
		{
			prev->next = child->next;
		}
		prev = node;
		node = node->next;
	}

	parent->last = prev;
}