]> Pileus Git - ~andy/gtk/blob - gtk/gtkrbtree.h
Merge branch 'master' into broadway
[~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   /* We keep track of whether the aggregate count of children plus 1
70    * for the node itself comes to an even number.  The parity flag is
71    * the total count of children mod 2, where the total count of
72    * children gets computed in the same way that the total offset gets
73    * computed. i.e. not the same as the "count" field below which
74    * doesn't include children. We could replace parity with a
75    * full-size int field here, and then take % 2 to get the parity flag,
76    * but that would use extra memory.
77    */
78
79   guint parity : 1;
80   
81   GtkRBNode *left;
82   GtkRBNode *right;
83   GtkRBNode *parent;
84
85   /* count is the number of nodes beneath us, plus 1 for ourselves.
86    * i.e. node->left->count + node->right->count + 1
87    */
88   gint count;
89   
90   /* this is the total of sizes of
91    * node->left, node->right, our own height, and the height
92    * of all trees in ->children, iff children exists because
93    * the thing is expanded.
94    */
95   gint offset;
96
97   /* Child trees */
98   GtkRBTree *children;
99 };
100
101
102 #define GTK_RBNODE_GET_COLOR(node)              (node?(((node->flags&GTK_RBNODE_RED)==GTK_RBNODE_RED)?GTK_RBNODE_RED:GTK_RBNODE_BLACK):GTK_RBNODE_BLACK)
103 #define GTK_RBNODE_SET_COLOR(node,color)        if((node->flags&color)!=color)node->flags=node->flags^(GTK_RBNODE_RED|GTK_RBNODE_BLACK)
104 #define GTK_RBNODE_GET_HEIGHT(node)             (node->offset-(node->left->offset+node->right->offset+(node->children?node->children->root->offset:0)))
105 #define GTK_RBNODE_SET_FLAG(node, flag)         G_STMT_START{ (node->flags|=flag); }G_STMT_END
106 #define GTK_RBNODE_UNSET_FLAG(node, flag)       G_STMT_START{ (node->flags&=~(flag)); }G_STMT_END
107 #define GTK_RBNODE_FLAG_SET(node, flag)         (node?(((node->flags&flag)==flag)?TRUE:FALSE):FALSE)
108
109
110 GtkRBTree *_gtk_rbtree_new              (void);
111 void       _gtk_rbtree_free             (GtkRBTree              *tree);
112 void       _gtk_rbtree_remove           (GtkRBTree              *tree);
113 void       _gtk_rbtree_destroy          (GtkRBTree              *tree);
114 GtkRBNode *_gtk_rbtree_insert_before    (GtkRBTree              *tree,
115                                          GtkRBNode              *node,
116                                          gint                    height,
117                                          gboolean                valid);
118 GtkRBNode *_gtk_rbtree_insert_after     (GtkRBTree              *tree,
119                                          GtkRBNode              *node,
120                                          gint                    height,
121                                          gboolean                valid);
122 void       _gtk_rbtree_remove_node      (GtkRBTree              *tree,
123                                          GtkRBNode              *node);
124 void       _gtk_rbtree_reorder          (GtkRBTree              *tree,
125                                          gint                   *new_order,
126                                          gint                    length);
127 GtkRBNode *_gtk_rbtree_find_count       (GtkRBTree              *tree,
128                                          gint                    count);
129 void       _gtk_rbtree_node_set_height  (GtkRBTree              *tree,
130                                          GtkRBNode              *node,
131                                          gint                    height);
132 void       _gtk_rbtree_node_mark_invalid(GtkRBTree              *tree,
133                                          GtkRBNode              *node);
134 void       _gtk_rbtree_node_mark_valid  (GtkRBTree              *tree,
135                                          GtkRBNode              *node);
136 void       _gtk_rbtree_column_invalid   (GtkRBTree              *tree);
137 void       _gtk_rbtree_mark_invalid     (GtkRBTree              *tree);
138 void       _gtk_rbtree_set_fixed_height (GtkRBTree              *tree,
139                                          gint                    height,
140                                          gboolean                mark_valid);
141 gint       _gtk_rbtree_node_find_offset (GtkRBTree              *tree,
142                                          GtkRBNode              *node);
143 gint       _gtk_rbtree_node_find_parity (GtkRBTree              *tree,
144                                          GtkRBNode              *node);
145 gint       _gtk_rbtree_find_offset      (GtkRBTree              *tree,
146                                          gint                    offset,
147                                          GtkRBTree             **new_tree,
148                                          GtkRBNode             **new_node);
149 void       _gtk_rbtree_traverse         (GtkRBTree              *tree,
150                                          GtkRBNode              *node,
151                                          GTraverseType           order,
152                                          GtkRBTreeTraverseFunc   func,
153                                          gpointer                data);
154 GtkRBNode *_gtk_rbtree_next             (GtkRBTree              *tree,
155                                          GtkRBNode              *node);
156 GtkRBNode *_gtk_rbtree_prev             (GtkRBTree              *tree,
157                                          GtkRBNode              *node);
158 void       _gtk_rbtree_next_full        (GtkRBTree              *tree,
159                                          GtkRBNode              *node,
160                                          GtkRBTree             **new_tree,
161                                          GtkRBNode             **new_node);
162 void       _gtk_rbtree_prev_full        (GtkRBTree              *tree,
163                                          GtkRBNode              *node,
164                                          GtkRBTree             **new_tree,
165                                          GtkRBNode             **new_node);
166
167 gint       _gtk_rbtree_get_depth        (GtkRBTree              *tree);
168
169 /* This func checks the integrity of the tree */
170 #ifdef G_ENABLE_DEBUG  
171 void       _gtk_rbtree_test             (const gchar            *where,
172                                          GtkRBTree              *tree);
173 void       _gtk_rbtree_debug_spew       (GtkRBTree              *tree);
174 #endif
175
176
177 G_END_DECLS
178
179
180 #endif /* __GTK_RBTREE_H__ */