]> Pileus Git - ~andy/gtk/blob - gtk/gtkrbtree.h
rbtree: Introduce _gtk_rbtree_is_nil()
[~andy/gtk] / gtk / gtkrbtree.h
1 /* gtkrbtree.h
2  * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /* A Red-Black Tree implementation used specifically by GtkTreeView.
21  */
22 #ifndef __GTK_RBTREE_H__
23 #define __GTK_RBTREE_H__
24
25 #include <glib.h>
26
27
28 G_BEGIN_DECLS
29
30
31 typedef enum
32 {
33   GTK_RBNODE_BLACK = 1 << 0,
34   GTK_RBNODE_RED = 1 << 1,
35   GTK_RBNODE_IS_PARENT = 1 << 2,
36   GTK_RBNODE_IS_SELECTED = 1 << 3,
37   GTK_RBNODE_IS_PRELIT = 1 << 4,
38   GTK_RBNODE_INVALID = 1 << 7,
39   GTK_RBNODE_COLUMN_INVALID = 1 << 8,
40   GTK_RBNODE_DESCENDANTS_INVALID = 1 << 9,
41   GTK_RBNODE_NON_COLORS = GTK_RBNODE_IS_PARENT |
42                           GTK_RBNODE_IS_SELECTED |
43                           GTK_RBNODE_IS_PRELIT |
44                           GTK_RBNODE_INVALID |
45                           GTK_RBNODE_COLUMN_INVALID |
46                           GTK_RBNODE_DESCENDANTS_INVALID
47 } GtkRBNodeColor;
48
49 typedef struct _GtkRBTree GtkRBTree;
50 typedef struct _GtkRBNode GtkRBNode;
51 typedef struct _GtkRBTreeView GtkRBTreeView;
52
53 typedef void (*GtkRBTreeTraverseFunc) (GtkRBTree  *tree,
54                                        GtkRBNode  *node,
55                                        gpointer  data);
56
57 struct _GtkRBTree
58 {
59   GtkRBNode *root;
60   GtkRBNode *nil;
61   GtkRBTree *parent_tree;
62   GtkRBNode *parent_node;
63 };
64
65 struct _GtkRBNode
66 {
67   guint flags : 14;
68
69   GtkRBNode *left;
70   GtkRBNode *right;
71   GtkRBNode *parent;
72
73   /* count is the number of nodes beneath us, plus 1 for ourselves.
74    * i.e. node->left->count + node->right->count + 1
75    */
76   gint count;
77   /* count the number of total nodes beneath us, including nodes
78    * of children trees.
79    * i.e. node->left->count + node->right->count + node->children->root->count + 1
80    */
81   guint total_count;
82   
83   /* this is the total of sizes of
84    * node->left, node->right, our own height, and the height
85    * of all trees in ->children, iff children exists because
86    * the thing is expanded.
87    */
88   gint offset;
89
90   /* Child trees */
91   GtkRBTree *children;
92 };
93
94
95 #define GTK_RBNODE_GET_COLOR(node)              (node?(((node->flags&GTK_RBNODE_RED)==GTK_RBNODE_RED)?GTK_RBNODE_RED:GTK_RBNODE_BLACK):GTK_RBNODE_BLACK)
96 #define GTK_RBNODE_SET_COLOR(node,color)        if((node->flags&color)!=color)node->flags=node->flags^(GTK_RBNODE_RED|GTK_RBNODE_BLACK)
97 #define GTK_RBNODE_GET_HEIGHT(node)             (node->offset-(node->left->offset+node->right->offset+(node->children?node->children->root->offset:0)))
98 #define GTK_RBNODE_SET_FLAG(node, flag)         G_STMT_START{ (node->flags|=flag); }G_STMT_END
99 #define GTK_RBNODE_UNSET_FLAG(node, flag)       G_STMT_START{ (node->flags&=~(flag)); }G_STMT_END
100 #define GTK_RBNODE_FLAG_SET(node, flag)         (node?(((node->flags&flag)==flag)?TRUE:FALSE):FALSE)
101
102
103 GtkRBTree *_gtk_rbtree_new              (void);
104 void       _gtk_rbtree_free             (GtkRBTree              *tree);
105 void       _gtk_rbtree_remove           (GtkRBTree              *tree);
106 void       _gtk_rbtree_destroy          (GtkRBTree              *tree);
107 GtkRBNode *_gtk_rbtree_insert_before    (GtkRBTree              *tree,
108                                          GtkRBNode              *node,
109                                          gint                    height,
110                                          gboolean                valid);
111 GtkRBNode *_gtk_rbtree_insert_after     (GtkRBTree              *tree,
112                                          GtkRBNode              *node,
113                                          gint                    height,
114                                          gboolean                valid);
115 void       _gtk_rbtree_remove_node      (GtkRBTree              *tree,
116                                          GtkRBNode              *node);
117 gboolean   _gtk_rbtree_is_nil           (GtkRBNode              *node);
118 void       _gtk_rbtree_reorder          (GtkRBTree              *tree,
119                                          gint                   *new_order,
120                                          gint                    length);
121 GtkRBNode *_gtk_rbtree_find_count       (GtkRBTree              *tree,
122                                          gint                    count);
123 void       _gtk_rbtree_node_set_height  (GtkRBTree              *tree,
124                                          GtkRBNode              *node,
125                                          gint                    height);
126 void       _gtk_rbtree_node_mark_invalid(GtkRBTree              *tree,
127                                          GtkRBNode              *node);
128 void       _gtk_rbtree_node_mark_valid  (GtkRBTree              *tree,
129                                          GtkRBNode              *node);
130 void       _gtk_rbtree_column_invalid   (GtkRBTree              *tree);
131 void       _gtk_rbtree_mark_invalid     (GtkRBTree              *tree);
132 void       _gtk_rbtree_set_fixed_height (GtkRBTree              *tree,
133                                          gint                    height,
134                                          gboolean                mark_valid);
135 gint       _gtk_rbtree_node_find_offset (GtkRBTree              *tree,
136                                          GtkRBNode              *node);
137 guint      _gtk_rbtree_node_get_index   (GtkRBTree              *tree,
138                                          GtkRBNode              *node);
139 gboolean   _gtk_rbtree_find_index       (GtkRBTree              *tree,
140                                          guint                   index,
141                                          GtkRBTree             **new_tree,
142                                          GtkRBNode             **new_node);
143 gint       _gtk_rbtree_find_offset      (GtkRBTree              *tree,
144                                          gint                    offset,
145                                          GtkRBTree             **new_tree,
146                                          GtkRBNode             **new_node);
147 void       _gtk_rbtree_traverse         (GtkRBTree              *tree,
148                                          GtkRBNode              *node,
149                                          GTraverseType           order,
150                                          GtkRBTreeTraverseFunc   func,
151                                          gpointer                data);
152 GtkRBNode *_gtk_rbtree_first            (GtkRBTree              *tree);
153 GtkRBNode *_gtk_rbtree_next             (GtkRBTree              *tree,
154                                          GtkRBNode              *node);
155 GtkRBNode *_gtk_rbtree_prev             (GtkRBTree              *tree,
156                                          GtkRBNode              *node);
157 void       _gtk_rbtree_next_full        (GtkRBTree              *tree,
158                                          GtkRBNode              *node,
159                                          GtkRBTree             **new_tree,
160                                          GtkRBNode             **new_node);
161 void       _gtk_rbtree_prev_full        (GtkRBTree              *tree,
162                                          GtkRBNode              *node,
163                                          GtkRBTree             **new_tree,
164                                          GtkRBNode             **new_node);
165
166 gint       _gtk_rbtree_get_depth        (GtkRBTree              *tree);
167
168
169 G_END_DECLS
170
171
172 #endif /* __GTK_RBTREE_H__ */