2 * Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #ifndef __GTK_RBTREE_H__
21 #define __GTK_RBTREE_H__
25 #endif /* __cplusplus */
30 GTK_RBNODE_BLACK = 1 << 0,
31 GTK_RBNODE_RED = 1 << 1,
32 GTK_RBNODE_IS_PARENT = 1 << 2,
33 GTK_RBNODE_IS_SELECTED = 1 << 3,
34 GTK_RBNODE_IS_PRELIT = 1 << 4,
35 GTK_RBNODE_IS_VIEW = 1 << 5
39 typedef struct _GtkRBTree GtkRBTree;
40 typedef struct _GtkRBNode GtkRBNode;
41 typedef struct _GtkRBTreeView GtkRBTreeView;
43 typedef void (*GtkRBTreeTraverseFunc) (GtkRBTree *tree,
51 GtkRBTree *parent_tree;
52 GtkRBNode *parent_node;
59 /* We keep track of whether the aggregate count of children plus 1
60 * for the node itself comes to an even number. The parity flag is
61 * the total count of children mod 2, where the total count of
62 * children gets computed in the same way that the total offset gets
63 * computed. i.e. not the same as the "count" field below which
64 * doesn't include children. We could replace parity with a
65 * full-size int field here, and then take % 2 to get the parity flag,
66 * but that would use extra memory.
75 /* count is the number of nodes beneath us, plus 1 for ourselves.
76 * i.e. node->left->count + node->right->count + 1
80 /* this is the total of sizes of
81 * node->left, node->right, our own height, and the height
82 * of all trees in ->children, iff children exists because
83 * the thing is expanded.
91 #define GTK_RBNODE_GET_COLOR(node) (node?(((node->flags>K_RBNODE_RED)==GTK_RBNODE_RED)?GTK_RBNODE_RED:GTK_RBNODE_BLACK):GTK_RBNODE_BLACK)
92 #define GTK_RBNODE_SET_COLOR(node,color) if((node->flags&color)!=color)node->flags=node->flags^(GTK_RBNODE_RED|GTK_RBNODE_BLACK)
93 #define GTK_RBNODE_GET_HEIGHT(node) (node->offset-(node->left->offset+node->right->offset+(node->children?node->children->root->offset:0)))
94 #define GTK_RBNODE_SET_FLAG(node, flag) G_STMT_START{ (node->flags|=flag); }G_STMT_END
95 #define GTK_RBNODE_UNSET_FLAG(node, flag) G_STMT_START{ (node->flags&=~(flag)); }G_STMT_END
96 #define GTK_RBNODE_FLAG_SET(node, flag) (node?(((node->flags&flag)==flag)?TRUE:FALSE):FALSE)
99 void _gtk_rbtree_push_allocator (GAllocator *allocator);
100 void _gtk_rbtree_pop_allocator (void);
101 GtkRBTree *_gtk_rbtree_new (void);
102 void _gtk_rbtree_free (GtkRBTree *tree);
103 void _gtk_rbtree_remove (GtkRBTree *tree);
104 void _gtk_rbtree_destroy (GtkRBTree *tree);
105 GtkRBNode *_gtk_rbtree_insert_before (GtkRBTree *tree,
108 GtkRBNode *_gtk_rbtree_insert_after (GtkRBTree *tree,
111 void _gtk_rbtree_remove_node (GtkRBTree *tree,
113 GtkRBNode *_gtk_rbtree_find_count (GtkRBTree *tree,
115 void _gtk_rbtree_node_set_height (GtkRBTree *tree,
118 gint _gtk_rbtree_node_find_offset (GtkRBTree *tree,
120 gint _gtk_rbtree_node_find_parity (GtkRBTree *tree,
122 gint _gtk_rbtree_find_offset (GtkRBTree *tree,
124 GtkRBTree **new_tree,
125 GtkRBNode **new_node);
126 void _gtk_rbtree_traverse (GtkRBTree *tree,
129 GtkRBTreeTraverseFunc func,
131 GtkRBNode *_gtk_rbtree_next (GtkRBTree *tree,
133 GtkRBNode *_gtk_rbtree_prev (GtkRBTree *tree,
135 void _gtk_rbtree_next_full (GtkRBTree *tree,
137 GtkRBTree **new_tree,
138 GtkRBNode **new_node);
139 void _gtk_rbtree_prev_full (GtkRBTree *tree,
141 GtkRBTree **new_tree,
142 GtkRBNode **new_node);
144 gint _gtk_rbtree_get_depth (GtkRBTree *tree);
146 /* This func just checks the integrity of the tree */
147 /* It will go away later. */
148 void _gtk_rbtree_test (const gchar *where,
154 #endif /* __cplusplus */
156 #endif /* __GTK_RBTREE_H__ */