summaryrefslogtreecommitdiffstats
path: root/src/tree.h
blob: 1ae0871ee8aa14eb30e1115abded8a1f43c65289 (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
/*
 * Tree data structure
 *
 * Copyright (C) 2017  Patrick McDermott
 *
 * This file is part of Marquee.
 *
 * Marquee is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Marquee 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 Marquee.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef MQ_TREE_H
#define MQ_TREE_H

typedef struct _MqTree MqTree;

#include <glib.h>

G_BEGIN_DECLS

#define MQ_TREE(obj) ((MqTree *) (obj))

#define MQ_TREE_CONTINUE TRUE
#define MQ_TREE_STOP     FALSE

struct _MqTree {
	MqTree   *root;
	MqTree   *parent;
	MqTree   *prev;
	MqTree   *next;
	MqTree   *first_child;
	MqTree   *last_child;
	gint      size;
	gint      position;
	gpointer  data;
};

MqTree *
mq_tree_insert_root_allocated(MqTree *node, gpointer data);

MqTree *
mq_tree_insert_child_allocated(MqTree *node, MqTree *parent, gpointer data);

MqTree *
mq_tree_insert_sibling_allocated(MqTree *node, MqTree *sibling, gpointer data);

#define mq_tree_insert_root(data) \
	mq_tree_insert_root_allocated(g_new0(MqTree, 1), data)
#define mq_tree_insert_child(parent, data) \
	mq_tree_insert_child_allocated(g_new0(MqTree, 1), parent, data)
#define mq_tree_insert_sibling(sibling, data) \
	mq_tree_insert_sibling_allocated(g_new0(MqTree, 1), sibling, data)

void
mq_tree_remove(MqTree *node);

#define mq_tree_size(node)     ((node) ? (((MqTree *) (node))->size) : -1)
#define mq_tree_position(node) ((node) ? (((MqTree *) (node))->position) : -1)
#define mq_tree_root(node)     ((node) ? (((MqTree *) (node))->root) : NULL)
#define mq_tree_previous(node) ((node) ? (((MqTree *) (node))->prev) : NULL)
#define mq_tree_next(node)     ((node) ? (((MqTree *) (node))->next) : NULL)
#define mq_tree_first_child(node) \
	((node) ? (((MqTree *) (node))->first_child) : NULL)

MqTree *
mq_tree_seek(MqTree *node, gint offset);

void
mq_tree_foreach(MqTree *node, gboolean (*cb)(MqTree *node, gpointer user_data),
	gpointer user_data);

void
mq_tree_foreach_from(MqTree *node,
	gboolean (*cb)(MqTree *node, gpointer user_data), gpointer user_data);

G_END_DECLS

#endif  /* MQ_TREE_H */