]> Pileus Git - ~andy/gtk/blob - gtk/gtkrbtree.h
Add support for invalid nodes. (_gtk_rbnode_rotate_right): Ditto.
[~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 #ifdef __cplusplus
26 extern "C" {
27 #endif /* __cplusplus */
28
29 #include <glib.h>
30 typedef enum
31 {
32   GTK_RBNODE_BLACK = 1 << 0,
33   GTK_RBNODE_RED = 1 << 1,
34   GTK_RBNODE_IS_PARENT = 1 << 2,
35   GTK_RBNODE_IS_SELECTED = 1 << 3,
36   GTK_RBNODE_IS_PRELIT = 1 << 4,
37   GTK_RBNODE_IS_SEMI_COLLAPSED = 1 << 5,
38   GTK_RBNODE_IS_SEMI_EXPANDED = 1 << 6,
39   GTK_RBNODE_INVALID = 1 << 7,
40   GTK_RBNODE_DESCENDANTS_INVALID = 1 << 8,
41   GTK_RBNODE_NON_COLORS = GTK_RBNODE_IS_PARENT | GTK_RBNODE_IS_SELECTED | GTK_RBNODE_IS_PRELIT | GTK_RBNODE_IS_SEMI_COLLAPSED | GTK_RBNODE_IS_SEMI_EXPANDED | GTK_RBNODE_INVALID | GTK_RBNODE_DESCENDANTS_INVALID
42 } GtkRBNodeColor;
43
44 typedef struct _GtkRBTree GtkRBTree;
45 typedef struct _GtkRBNode GtkRBNode;
46 typedef struct _GtkRBTreeView GtkRBTreeView;
47
48 typedef void (*GtkRBTreeTraverseFunc) (GtkRBTree  *tree,
49                                        GtkRBNode  *node,
50                                        gpointer  data);
51
52 struct _GtkRBTree
53 {
54   GtkRBNode *root;
55   GtkRBNode *nil;
56   GtkRBTree *parent_tree;
57   GtkRBNode *parent_node;
58 };
59
60 struct _GtkRBNode
61 {
62   guint flags : 14;
63
64   /* We keep track of whether the aggregate count of children plus 1
65    * for the node itself comes to an even number.  The parity flag is
66    * the total count of children mod 2, where the total count of
67    * children gets computed in the same way that the total offset gets
68    * computed. i.e. not the same as the "count" field below which
69    * doesn't include children. We could replace parity with a
70    * full-size int field here, and then take % 2 to get the parity flag,
71    * but that would use extra memory.
72    */
73
74   guint parity : 1;
75   
76   GtkRBNode *left;
77   GtkRBNode *right;
78   GtkRBNode *parent;
79
80   /* count is the number of nodes beneath us, plus 1 for ourselves.
81    * i.e. node->left->count + node->right->count + 1
82    */
83   gint count;
84   
85   /* this is the total of sizes of
86    * node->left, node->right, our own height, and the height
87    * of all trees in ->children, iff children exists because
88    * the thing is expanded.
89    */
90   gint offset;
91
92   /* Child trees */
93   GtkRBTree *children;
94 };
95
96 #define GTK_RBNODE_GET_COLOR(node)              (node?(((node->flags&GTK_RBNODE_RED)==GTK_RBNODE_RED)?GTK_RBNODE_RED:GTK_RBNODE_BLACK):GTK_RBNODE_BLACK)
97 #define GTK_RBNODE_SET_COLOR(node,color)        if((node->flags&color)!=color)node->flags=node->flags^(GTK_RBNODE_RED|GTK_RBNODE_BLACK)
98 #define GTK_RBNODE_GET_HEIGHT(node)             (node->offset-(node->left->offset+node->right->offset+(node->children?node->children->root->offset:0)))
99 #define GTK_RBNODE_SET_FLAG(node, flag)         G_STMT_START{ (node->flags|=flag); }G_STMT_END
100 #define GTK_RBNODE_UNSET_FLAG(node, flag)       G_STMT_START{ (node->flags&=~(flag)); }G_STMT_END
101 #define GTK_RBNODE_FLAG_SET(node, flag)         (node?(((node->flags&flag)==flag)?TRUE:FALSE):FALSE)
102
103
104 void       _gtk_rbtree_push_allocator   (GAllocator             *allocator);
105 void       _gtk_rbtree_pop_allocator    (void);
106 GtkRBTree *_gtk_rbtree_new              (void);
107 void       _gtk_rbtree_free             (GtkRBTree              *tree);
108 void       _gtk_rbtree_remove           (GtkRBTree              *tree);
109 void       _gtk_rbtree_destroy          (GtkRBTree              *tree);
110 GtkRBNode *_gtk_rbtree_insert_before    (GtkRBTree              *tree,
111                                          GtkRBNode              *node,
112                                          gint                    height);
113 GtkRBNode *_gtk_rbtree_insert_after     (GtkRBTree              *tree,
114                                          GtkRBNode              *node,
115                                          gint                    height);
116 void       _gtk_rbtree_remove_node      (GtkRBTree              *tree,
117                                          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 gint       _gtk_rbtree_node_find_offset (GtkRBTree              *tree,
131                                          GtkRBNode              *node);
132 gint       _gtk_rbtree_node_find_parity (GtkRBTree              *tree,
133                                          GtkRBNode              *node);
134 gint       _gtk_rbtree_find_offset      (GtkRBTree              *tree,
135                                          gint                    offset,
136                                          GtkRBTree             **new_tree,
137                                          GtkRBNode             **new_node);
138 void       _gtk_rbtree_traverse         (GtkRBTree              *tree,
139                                          GtkRBNode              *node,
140                                          GTraverseType           order,
141                                          GtkRBTreeTraverseFunc   func,
142                                          gpointer                data);
143 GtkRBNode *_gtk_rbtree_next             (GtkRBTree              *tree,
144                                          GtkRBNode              *node);
145 GtkRBNode *_gtk_rbtree_prev             (GtkRBTree              *tree,
146                                          GtkRBNode              *node);
147 void       _gtk_rbtree_next_full        (GtkRBTree              *tree,
148                                          GtkRBNode              *node,
149                                          GtkRBTree             **new_tree,
150                                          GtkRBNode             **new_node);
151 void       _gtk_rbtree_prev_full        (GtkRBTree              *tree,
152                                          GtkRBNode              *node,
153                                          GtkRBTree             **new_tree,
154                                          GtkRBNode             **new_node);
155
156 gint       _gtk_rbtree_get_depth        (GtkRBTree              *tree);
157
158 /* This func just checks the integrity of the tree */
159 /* It will go away later. */
160 void       _gtk_rbtree_test             (const gchar            *where,
161                                          GtkRBTree              *tree);
162
163
164 #ifdef __cplusplus
165 }
166 #endif /* __cplusplus */
167
168 #endif /* __GTK_RBTREE_H__ */