warnings: fix compilation with old autoconf
[gnulib/ericb.git] / lib / gl_avltreehash_list.c
blob02211ae2d1e9f13adc1e259971b4a3d02722be17
1 /* Sequential list data type implemented by a hash table with a binary tree.
2 Copyright (C) 2006, 2008-2017 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 3 of the License, or
8 (at your option) any later version.
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, see <http://www.gnu.org/licenses/>. */
18 #include <config.h>
20 /* Specification. */
21 #include "gl_avltreehash_list.h"
23 #include <stdint.h> /* for SIZE_MAX */
24 #include <stdlib.h>
26 #include "gl_avltree_oset.h"
27 #include "xsize.h"
29 #ifndef uintptr_t
30 # define uintptr_t unsigned long
31 #endif
33 #define WITH_HASHTABLE 1
35 /* Which kind of binary trees to use for ordered sets. Quite arbitrary. */
36 #define OSET_TREE_FLAVOR GL_AVLTREE_OSET
38 /* -------------------------- gl_list_t Data Type -------------------------- */
40 /* Generic hash-table code: Type definitions. */
41 #include "gl_anyhash_list1.h"
43 /* Generic AVL tree code: Type definitions. */
44 #include "gl_anyavltree_list1.h"
46 /* Generic hash-table code: Low-level code. */
47 #include "gl_anyhash_list2.h"
49 /* Generic binary tree code: Type definitions. */
50 #include "gl_anytree_list1.h"
52 /* Hash-table with binary tree code: Handling of hash buckets. */
53 #include "gl_anytreehash_list1.h"
55 /* Generic AVL tree code: Insertion/deletion algorithms. */
56 #include "gl_anyavltree_list2.h"
58 /* Generic binary tree code: Functions taking advantage of the hash table. */
59 #include "gl_anytreehash_list2.h"
61 /* Generic binary tree code: All other functions. */
62 #include "gl_anytree_list2.h"
64 /* For debugging. */
65 static unsigned int
66 check_invariants (gl_list_node_t node, gl_list_node_t parent)
68 unsigned int left_height =
69 (node->left != NULL ? check_invariants (node->left, node) : 0);
70 unsigned int right_height =
71 (node->right != NULL ? check_invariants (node->right, node) : 0);
72 int balance = (int)right_height - (int)left_height;
74 if (!(node->parent == parent))
75 abort ();
76 if (!(node->branch_size
77 == (node->left != NULL ? node->left->branch_size : 0)
78 + 1 + (node->right != NULL ? node->right->branch_size : 0)))
79 abort ();
80 if (!(balance >= -1 && balance <= 1))
81 abort ();
82 if (!(node->balance == balance))
83 abort ();
85 return 1 + (left_height > right_height ? left_height : right_height);
87 void
88 gl_avltreehash_list_check_invariants (gl_list_t list)
90 if (list->root != NULL)
91 check_invariants (list->root, NULL);
95 const struct gl_list_implementation gl_avltreehash_list_implementation =
97 gl_tree_nx_create_empty,
98 gl_tree_nx_create,
99 gl_tree_size,
100 gl_tree_node_value,
101 gl_tree_node_nx_set_value,
102 gl_tree_next_node,
103 gl_tree_previous_node,
104 gl_tree_get_at,
105 gl_tree_nx_set_at,
106 gl_tree_search_from_to,
107 gl_tree_indexof_from_to,
108 gl_tree_nx_add_first,
109 gl_tree_nx_add_last,
110 gl_tree_nx_add_before,
111 gl_tree_nx_add_after,
112 gl_tree_nx_add_at,
113 gl_tree_remove_node,
114 gl_tree_remove_at,
115 gl_tree_remove,
116 gl_tree_list_free,
117 gl_tree_iterator,
118 gl_tree_iterator_from_to,
119 gl_tree_iterator_next,
120 gl_tree_iterator_free,
121 gl_tree_sortedlist_search,
122 gl_tree_sortedlist_search_from_to,
123 gl_tree_sortedlist_indexof,
124 gl_tree_sortedlist_indexof_from_to,
125 gl_tree_sortedlist_nx_add,
126 gl_tree_sortedlist_remove