Add bounded list search operations.
[gnulib.git] / lib / gl_avltree_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 #include <config.h>
20
21 /* Specification.  */
22 #include "gl_avltree_list.h"
23
24 #include <stdlib.h>
25
26 #include "xalloc.h"
27
28 /* -------------------------- gl_list_t Data Type -------------------------- */
29
30 /* Generic AVL tree code.  */
31 #include "gl_anyavltree_list1.h"
32 #include "gl_anyavltree_list2.h"
33
34 /* Generic binary tree code.  */
35 #include "gl_anytree_list1.h"
36 #include "gl_anytree_list2.h"
37
38 /* For debugging.  */
39 static unsigned int
40 check_invariants (gl_list_node_t node, gl_list_node_t parent)
41 {
42   unsigned int left_height =
43     (node->left != NULL ? check_invariants (node->left, node) : 0);
44   unsigned int right_height =
45     (node->right != NULL ? check_invariants (node->right, node) : 0);
46   int balance = (int)right_height - (int)left_height;
47
48   if (!(node->parent == parent))
49     abort ();
50   if (!(node->branch_size
51         == (node->left != NULL ? node->left->branch_size : 0)
52            + 1 + (node->right != NULL ? node->right->branch_size : 0)))
53     abort ();
54   if (!(balance >= -1 && balance <= 1))
55     abort ();
56   if (!(node->balance == balance))
57     abort ();
58
59   return 1 + (left_height > right_height ? left_height : right_height);
60 }
61 void
62 gl_avltree_list_check_invariants (gl_list_t list)
63 {
64   if (list->root != NULL)
65     check_invariants (list->root, NULL);
66 }
67
68 const struct gl_list_implementation gl_avltree_list_implementation =
69   {
70     gl_tree_create_empty,
71     gl_tree_create,
72     gl_tree_size,
73     gl_tree_node_value,
74     gl_tree_next_node,
75     gl_tree_previous_node,
76     gl_tree_get_at,
77     gl_tree_set_at,
78     gl_tree_search_from_to,
79     gl_tree_indexof_from_to,
80     gl_tree_add_first,
81     gl_tree_add_last,
82     gl_tree_add_before,
83     gl_tree_add_after,
84     gl_tree_add_at,
85     gl_tree_remove_node,
86     gl_tree_remove_at,
87     gl_tree_remove,
88     gl_tree_list_free,
89     gl_tree_iterator,
90     gl_tree_iterator_from_to,
91     gl_tree_iterator_next,
92     gl_tree_iterator_free,
93     gl_tree_sortedlist_search,
94     gl_tree_sortedlist_search_from_to,
95     gl_tree_sortedlist_indexof,
96     gl_tree_sortedlist_indexof_from_to,
97     gl_tree_sortedlist_add,
98     gl_tree_sortedlist_remove
99   };