annotate lib/gl_avltree_list.c @ 7405:0de49c40e105

Add searching operations, limited to a subsequence of the list.
author Bruno Haible <bruno@clisp.org>
date Thu, 05 Oct 2006 12:45:16 +0000
parents 1c4ed7637c24
children 9704ff2cbdfe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6980
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Sequential list data type implemented by a binary tree.
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 Copyright (C) 2006 Free Software Foundation, Inc.
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2006.
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software; you can redistribute it and/or modify
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 2, or (at your option)
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 any later version.
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program; if not, write to the Free Software Foundation,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
7304
1c4ed7637c24 Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 6980
diff changeset
19 #include <config.h>
6980
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 /* Specification. */
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include "gl_avltree_list.h"
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include <stdlib.h>
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include "xalloc.h"
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 /* -------------------------- gl_list_t Data Type -------------------------- */
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 /* Generic AVL tree code. */
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 #include "gl_anyavltree_list1.h"
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 #include "gl_anyavltree_list2.h"
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 /* Generic binary tree code. */
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 #include "gl_anytree_list1.h"
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 #include "gl_anytree_list2.h"
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 /* For debugging. */
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 static unsigned int
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 check_invariants (gl_list_node_t node, gl_list_node_t parent)
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 {
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 unsigned int left_height =
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 (node->left != NULL ? check_invariants (node->left, node) : 0);
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 unsigned int right_height =
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 (node->right != NULL ? check_invariants (node->right, node) : 0);
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 int balance = (int)right_height - (int)left_height;
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 if (!(node->parent == parent))
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 abort ();
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 if (!(node->branch_size
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 == (node->left != NULL ? node->left->branch_size : 0)
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 + 1 + (node->right != NULL ? node->right->branch_size : 0)))
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 abort ();
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 if (!(balance >= -1 && balance <= 1))
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 abort ();
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 if (!(node->balance == balance))
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 abort ();
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 return 1 + (left_height > right_height ? left_height : right_height);
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 }
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 void
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 gl_avltree_list_check_invariants (gl_list_t list)
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 {
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 if (list->root != NULL)
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 check_invariants (list->root, NULL);
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 }
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 const struct gl_list_implementation gl_avltree_list_implementation =
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 {
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 gl_tree_create_empty,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 gl_tree_create,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 gl_tree_size,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 gl_tree_node_value,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 gl_tree_next_node,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 gl_tree_previous_node,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 gl_tree_get_at,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 gl_tree_set_at,
7405
0de49c40e105 Add searching operations, limited to a subsequence of the list.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
78 gl_tree_search_from_to,
0de49c40e105 Add searching operations, limited to a subsequence of the list.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
79 gl_tree_indexof_from_to,
6980
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 gl_tree_add_first,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 gl_tree_add_last,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 gl_tree_add_before,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 gl_tree_add_after,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 gl_tree_add_at,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 gl_tree_remove_node,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 gl_tree_remove_at,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 gl_tree_remove,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 gl_tree_list_free,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 gl_tree_iterator,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 gl_tree_iterator_from_to,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 gl_tree_iterator_next,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 gl_tree_iterator_free,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 gl_tree_sortedlist_search,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 gl_tree_sortedlist_indexof,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 gl_tree_sortedlist_add,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 gl_tree_sortedlist_remove
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 };