annotate lib/gl_avltree_list.c @ 17848:ab58d4870664

version-etc: new year * doc/gnulib.texi: * lib/version-etc.c (COPYRIGHT_YEAR): Update copyright date. * all files: Run 'make update-copyright'.
author Paul Eggert <eggert@cs.ucla.edu>
date Thu, 01 Jan 2015 01:38:23 +0000
parents af887cfa71dd
children
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.
17848
ab58d4870664 version-etc: new year
Paul Eggert <eggert@cs.ucla.edu>
parents: 17761
diff changeset
2 Copyright (C) 2006, 2008-2015 Free Software Foundation, Inc.
6980
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
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7410
diff changeset
5 This program is free software: you can redistribute it and/or modify
6980
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
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7410
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7410
diff changeset
8 (at your option) any later version.
6980
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
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7410
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
6980
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
7304
1c4ed7637c24 Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 6980
diff changeset
18 #include <config.h>
6980
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 /* Specification. */
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #include "gl_avltree_list.h"
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include <stdlib.h>
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 /* -------------------------- gl_list_t Data Type -------------------------- */
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 /* Generic AVL tree code. */
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include "gl_anyavltree_list1.h"
12445
a8c91b846640 Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
29
a8c91b846640 Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
30 /* Generic binary tree code. */
a8c91b846640 Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
31 #include "gl_anytree_list1.h"
a8c91b846640 Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
32
a8c91b846640 Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
33 /* Generic AVL tree code. */
6980
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 #include "gl_anyavltree_list2.h"
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 /* Generic binary tree code. */
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 #include "gl_anytree_list2.h"
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 /* For debugging. */
17761
af887cfa71dd avltree-list: avoid compiler warnings
Dylan Cali <calid1984@gmail.com>
parents: 17587
diff changeset
40 extern void gl_avltree_list_check_invariants (gl_list_t list);
af887cfa71dd avltree-list: avoid compiler warnings
Dylan Cali <calid1984@gmail.com>
parents: 17587
diff changeset
41
af887cfa71dd avltree-list: avoid compiler warnings
Dylan Cali <calid1984@gmail.com>
parents: 17587
diff changeset
42 static unsigned int _GL_ATTRIBUTE_PURE
6980
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 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
44 {
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 unsigned int left_height =
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 (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
47 unsigned int right_height =
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 (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
49 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
50
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 if (!(node->parent == parent))
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 abort ();
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 if (!(node->branch_size
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9686
diff changeset
54 == (node->left != NULL ? node->left->branch_size : 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9686
diff changeset
55 + 1 + (node->right != NULL ? node->right->branch_size : 0)))
6980
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 abort ();
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 if (!(balance >= -1 && balance <= 1))
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 abort ();
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 if (!(node->balance == balance))
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 abort ();
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 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
63 }
17761
af887cfa71dd avltree-list: avoid compiler warnings
Dylan Cali <calid1984@gmail.com>
parents: 17587
diff changeset
64
af887cfa71dd avltree-list: avoid compiler warnings
Dylan Cali <calid1984@gmail.com>
parents: 17587
diff changeset
65 void _GL_ATTRIBUTE_CONST
6980
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 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
67 {
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 if (list->root != NULL)
17761
af887cfa71dd avltree-list: avoid compiler warnings
Dylan Cali <calid1984@gmail.com>
parents: 17587
diff changeset
69 (void) check_invariants (list->root, NULL);
6980
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 }
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 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
73 {
12445
a8c91b846640 Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
74 gl_tree_nx_create_empty,
a8c91b846640 Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
75 gl_tree_nx_create,
6980
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 gl_tree_size,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 gl_tree_node_value,
12445
a8c91b846640 Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
78 gl_tree_node_nx_set_value,
6980
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 gl_tree_next_node,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 gl_tree_previous_node,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 gl_tree_get_at,
12445
a8c91b846640 Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
82 gl_tree_nx_set_at,
7405
0de49c40e105 Add searching operations, limited to a subsequence of the list.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
83 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
84 gl_tree_indexof_from_to,
12445
a8c91b846640 Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
85 gl_tree_nx_add_first,
a8c91b846640 Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
86 gl_tree_nx_add_last,
a8c91b846640 Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
87 gl_tree_nx_add_before,
a8c91b846640 Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
88 gl_tree_nx_add_after,
a8c91b846640 Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
89 gl_tree_nx_add_at,
6980
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 gl_tree_remove_node,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 gl_tree_remove_at,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 gl_tree_remove,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 gl_tree_list_free,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 gl_tree_iterator,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 gl_tree_iterator_from_to,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 gl_tree_iterator_next,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 gl_tree_iterator_free,
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 gl_tree_sortedlist_search,
7410
9704ff2cbdfe Add bounded list search operations.
Bruno Haible <bruno@clisp.org>
parents: 7405
diff changeset
99 gl_tree_sortedlist_search_from_to,
6980
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 gl_tree_sortedlist_indexof,
7410
9704ff2cbdfe Add bounded list search operations.
Bruno Haible <bruno@clisp.org>
parents: 7405
diff changeset
101 gl_tree_sortedlist_indexof_from_to,
12445
a8c91b846640 Move the malloc checking from module 'list' to new module 'xlist'.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
102 gl_tree_sortedlist_nx_add,
6980
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 gl_tree_sortedlist_remove
9ccb96800d02 Sequential list data type implemented by a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 };