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.
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)
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, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 #include "gl_avltreehash_list.h"
24 #include <stdint.h> /* for SIZE_MAX */
27 #include "gl_avltree_oset.h"
32 # define uintptr_t unsigned long
35 #define WITH_HASHTABLE 1
37 /* Which kind of binary trees to use for ordered sets. Quite arbitrary. */
38 #define OSET_TREE_FLAVOR GL_AVLTREE_OSET
40 /* -------------------------- gl_list_t Data Type -------------------------- */
42 /* Generic hash-table code: Type definitions. */
43 #include "gl_anyhash_list1.h"
45 /* Generic AVL tree code: Type definitions. */
46 #include "gl_anyavltree_list1.h"
48 /* Generic hash-table code: Low-level code. */
49 #include "gl_anyhash_list2.h"
51 /* Generic binary tree code: Type definitions. */
52 #include "gl_anytree_list1.h"
54 /* Hash-table with binary tree code: Handling of hash buckets. */
55 #include "gl_anytreehash_list1.h"
57 /* Generic AVL tree code: Insertion/deletion algorithms. */
58 #include "gl_anyavltree_list2.h"
60 /* Generic binary tree code: Functions taking advantage of the hash table. */
61 #include "gl_anytreehash_list2.h"
63 /* Generic binary tree code: All other functions. */
64 #include "gl_anytree_list2.h"
68 check_invariants (gl_list_node_t node, gl_list_node_t parent)
70 unsigned int left_height =
71 (node->left != NULL ? check_invariants (node->left, node) : 0);
72 unsigned int right_height =
73 (node->right != NULL ? check_invariants (node->right, node) : 0);
74 int balance = (int)right_height - (int)left_height;
76 if (!(node->parent == parent))
78 if (!(node->branch_size
79 == (node->left != NULL ? node->left->branch_size : 0)
80 + 1 + (node->right != NULL ? node->right->branch_size : 0)))
82 if (!(balance >= -1 && balance <= 1))
84 if (!(node->balance == balance))
87 return 1 + (left_height > right_height ? left_height : right_height);
90 gl_avltreehash_list_check_invariants (gl_list_t list)
92 if (list->root != NULL)
93 check_invariants (list->root, NULL);
97 const struct gl_list_implementation gl_avltreehash_list_implementation =
104 gl_tree_previous_node,
119 gl_tree_iterator_from_to,
120 gl_tree_iterator_next,
121 gl_tree_iterator_free,
122 gl_tree_sortedlist_search,
123 gl_tree_sortedlist_indexof,
124 gl_tree_sortedlist_add,
125 gl_tree_sortedlist_remove