Add copyright notices to long-enough files that lack them, since
[gnulib.git] / lib / gl_rbtree_list.c
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.
4
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)
8    any later version.
9
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.
14
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.  */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 /* Specification.  */
24 #include "gl_rbtree_list.h"
25
26 #include <stdlib.h>
27
28 #include "xalloc.h"
29
30 /* -------------------------- gl_list_t Data Type -------------------------- */
31
32 /* Generic red-black tree code.  */
33 #include "gl_anyrbtree_list1.h"
34 #include "gl_anyrbtree_list2.h"
35
36 /* Generic binary tree code.  */
37 #include "gl_anytree_list1.h"
38 #include "gl_anytree_list2.h"
39
40 /* For debugging.  */
41 static unsigned int
42 check_invariants (gl_list_node_t node, gl_list_node_t parent)
43 {
44   unsigned int left_blackheight =
45     (node->left != NULL ? check_invariants (node->left, node) : 0);
46   unsigned int right_blackheight =
47     (node->right != NULL ? check_invariants (node->right, node) : 0);
48
49   if (!(node->parent == parent))
50     abort ();
51   if (!(node->branch_size
52         == (node->left != NULL ? node->left->branch_size : 0)
53            + 1 + (node->right != NULL ? node->right->branch_size : 0)))
54     abort ();
55   if (!(node->color == BLACK || node->color == RED))
56     abort ();
57   if (parent == NULL && !(node->color == BLACK))
58     abort ();
59   if (!(left_blackheight == right_blackheight))
60     abort ();
61
62   return left_blackheight + (node->color == BLACK ? 1 : 0);
63 }
64 void
65 gl_rbtree_list_check_invariants (gl_list_t list)
66 {
67   if (list->root != NULL)
68     check_invariants (list->root, NULL);
69 }
70
71 const struct gl_list_implementation gl_rbtree_list_implementation =
72   {
73     gl_tree_create_empty,
74     gl_tree_create,
75     gl_tree_size,
76     gl_tree_node_value,
77     gl_tree_next_node,
78     gl_tree_previous_node,
79     gl_tree_get_at,
80     gl_tree_set_at,
81     gl_tree_search,
82     gl_tree_indexof,
83     gl_tree_add_first,
84     gl_tree_add_last,
85     gl_tree_add_before,
86     gl_tree_add_after,
87     gl_tree_add_at,
88     gl_tree_remove_node,
89     gl_tree_remove_at,
90     gl_tree_remove,
91     gl_tree_list_free,
92     gl_tree_iterator,
93     gl_tree_iterator_from_to,
94     gl_tree_iterator_next,
95     gl_tree_iterator_free,
96     gl_tree_sortedlist_search,
97     gl_tree_sortedlist_indexof,
98     gl_tree_sortedlist_add,
99     gl_tree_sortedlist_remove
100   };