Sequential list data type implemented by a hash table with a binary tree.
[gnulib.git] / lib / gl_avltreehash_list.c
1 /* Sequential list data type implemented by a hash table with 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_avltreehash_list.h"
25
26 #include <stdlib.h>
27
28 #include "gl_avltree_oset.h"
29 #include "xalloc.h"
30 #include "xsize.h"
31 #include "size_max.h"
32
33 #ifndef uintptr_t
34 # define uintptr_t unsigned long
35 #endif
36
37 #define WITH_HASHTABLE 1
38
39 /* Which kind of binary trees to use for ordered sets.  Quite arbitrary.  */
40 #define OSET_TREE_FLAVOR GL_AVLTREE_OSET
41
42 /* -------------------------- gl_list_t Data Type -------------------------- */
43
44 /* Generic hash-table code: Type definitions.  */
45 #include "gl_anyhash_list1.h"
46
47 /* Generic AVL tree code: Type definitions.  */
48 #include "gl_anyavltree_list1.h"
49
50 /* Generic hash-table code: Low-level code.  */
51 #include "gl_anyhash_list2.h"
52
53 /* Generic binary tree code: Type definitions.  */
54 #include "gl_anytree_list1.h"
55
56 /* Hash-table with binary tree code: Handling of hash buckets.  */
57 #include "gl_anytreehash_list1.h"
58
59 /* Generic AVL tree code: Insertion/deletion algorithms.  */
60 #include "gl_anyavltree_list2.h"
61
62 /* Generic binary tree code: Functions taking advantage of the hash table.  */
63 #include "gl_anytreehash_list2.h"
64
65 /* Generic binary tree code: All other functions.  */
66 #include "gl_anytree_list2.h"
67
68 /* For debugging.  */
69 static unsigned int
70 check_invariants (gl_list_node_t node, gl_list_node_t parent)
71 {
72   unsigned int left_height =
73     (node->left != NULL ? check_invariants (node->left, node) : 0);
74   unsigned int right_height =
75     (node->right != NULL ? check_invariants (node->right, node) : 0);
76   int balance = (int)right_height - (int)left_height;
77
78   if (!(node->parent == parent))
79     abort ();
80   if (!(node->branch_size
81         == (node->left != NULL ? node->left->branch_size : 0)
82            + 1 + (node->right != NULL ? node->right->branch_size : 0)))
83     abort ();
84   if (!(balance >= -1 && balance <= 1))
85     abort ();
86   if (!(node->balance == balance))
87     abort ();
88
89   return 1 + (left_height > right_height ? left_height : right_height);
90 }
91 void
92 gl_avltreehash_list_check_invariants (gl_list_t list)
93 {
94   if (list->root != NULL)
95     check_invariants (list->root, NULL);
96 }
97
98
99 const struct gl_list_implementation gl_avltreehash_list_implementation =
100   {
101     gl_tree_create_empty,
102     gl_tree_create,
103     gl_tree_size,
104     gl_tree_node_value,
105     gl_tree_next_node,
106     gl_tree_previous_node,
107     gl_tree_get_at,
108     gl_tree_set_at,
109     gl_tree_search,
110     gl_tree_indexof,
111     gl_tree_add_first,
112     gl_tree_add_last,
113     gl_tree_add_before,
114     gl_tree_add_after,
115     gl_tree_add_at,
116     gl_tree_remove_node,
117     gl_tree_remove_at,
118     gl_tree_remove,
119     gl_tree_list_free,
120     gl_tree_iterator,
121     gl_tree_iterator_from_to,
122     gl_tree_iterator_next,
123     gl_tree_iterator_free,
124     gl_tree_sortedlist_search,
125     gl_tree_sortedlist_indexof,
126     gl_tree_sortedlist_add,
127     gl_tree_sortedlist_remove
128   };