1 /* Sequential list data type implemented by a binary tree.
2 Copyright (C) 2006 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2006.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 #include "gl_rbtree_list.h"
28 /* -------------------------- gl_list_t Data Type -------------------------- */
30 /* Generic red-black tree code. */
31 #include "gl_anyrbtree_list1.h"
32 #include "gl_anyrbtree_list2.h"
34 /* Generic binary tree code. */
35 #include "gl_anytree_list1.h"
36 #include "gl_anytree_list2.h"
40 check_invariants (gl_list_node_t node, gl_list_node_t parent)
42 unsigned int left_blackheight =
43 (node->left != NULL ? check_invariants (node->left, node) : 0);
44 unsigned int right_blackheight =
45 (node->right != NULL ? check_invariants (node->right, node) : 0);
47 if (!(node->parent == parent))
49 if (!(node->branch_size
50 == (node->left != NULL ? node->left->branch_size : 0)
51 + 1 + (node->right != NULL ? node->right->branch_size : 0)))
53 if (!(node->color == BLACK || node->color == RED))
55 if (parent == NULL && !(node->color == BLACK))
57 if (!(left_blackheight == right_blackheight))
60 return left_blackheight + (node->color == BLACK ? 1 : 0);
63 gl_rbtree_list_check_invariants (gl_list_t list)
65 if (list->root != NULL)
66 check_invariants (list->root, NULL);
69 const struct gl_list_implementation gl_rbtree_list_implementation =
76 gl_tree_previous_node,
91 gl_tree_iterator_from_to,
92 gl_tree_iterator_next,
93 gl_tree_iterator_free,
94 gl_tree_sortedlist_search,
95 gl_tree_sortedlist_indexof,
96 gl_tree_sortedlist_add,
97 gl_tree_sortedlist_remove