annotate lib/gl_rbtreehash_list.c @ 6981:0bc4f2ff0eae

Sequential list data type implemented by a hash table with a binary tree.
author Bruno Haible <bruno@clisp.org>
date Mon, 17 Jul 2006 11:31:28 +0000
parents
children 1c4ed7637c24
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6981
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Sequential list data type implemented by a hash table with a binary tree.
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 Copyright (C) 2006 Free Software Foundation, Inc.
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2006.
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software; you can redistribute it and/or modify
0bc4f2ff0eae Sequential list data type implemented by a hash table with 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
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 2, or (at your option)
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 any later version.
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program; if not, write to the Free Software Foundation,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 #ifdef HAVE_CONFIG_H
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 # include <config.h>
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #endif
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 /* Specification. */
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include "gl_rbtreehash_list.h"
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include <stdlib.h>
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include "gl_rbtree_oset.h"
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #include "xalloc.h"
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 #include "xsize.h"
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 #include "size_max.h"
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 #ifndef uintptr_t
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 # define uintptr_t unsigned long
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 #endif
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 #define WITH_HASHTABLE 1
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 /* Which kind of binary trees to use for ordered sets. Quite arbitrary. */
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 #define OSET_TREE_FLAVOR GL_RBTREE_OSET
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 /* -------------------------- gl_list_t Data Type -------------------------- */
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 /* Generic hash-table code: Type definitions. */
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 #include "gl_anyhash_list1.h"
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 /* Generic red-black tree code: Type definitions. */
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 #include "gl_anyrbtree_list1.h"
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 /* Generic hash-table code: Low-level code. */
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 #include "gl_anyhash_list2.h"
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 /* Generic binary tree code: Type definitions. */
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 #include "gl_anytree_list1.h"
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 /* Hash-table with binary tree code: Handling of hash buckets. */
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 #include "gl_anytreehash_list1.h"
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 /* Generic red-black tree code: Insertion/deletion algorithms. */
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 #include "gl_anyrbtree_list2.h"
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 /* Generic binary tree code: Functions taking advantage of the hash table. */
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 #include "gl_anytreehash_list2.h"
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 /* Generic binary tree code: All other functions. */
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 #include "gl_anytree_list2.h"
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 /* For debugging. */
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 static unsigned int
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 check_invariants (gl_list_node_t node, gl_list_node_t parent)
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 {
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 unsigned int left_blackheight =
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 (node->left != NULL ? check_invariants (node->left, node) : 0);
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 unsigned int right_blackheight =
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 (node->right != NULL ? check_invariants (node->right, node) : 0);
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 if (!(node->parent == parent))
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 abort ();
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 if (!(node->branch_size
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 == (node->left != NULL ? node->left->branch_size : 0)
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 + 1 + (node->right != NULL ? node->right->branch_size : 0)))
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 abort ();
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 if (!(node->color == BLACK || node->color == RED))
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 abort ();
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 if (parent == NULL && !(node->color == BLACK))
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 abort ();
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 if (!(left_blackheight == right_blackheight))
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 abort ();
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 return left_blackheight + (node->color == BLACK ? 1 : 0);
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 }
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 void
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 gl_rbtreehash_list_check_invariants (gl_list_t list)
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 {
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 if (list->root != NULL)
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 check_invariants (list->root, NULL);
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 }
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 const struct gl_list_implementation gl_avltreehash_list_implementation =
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 {
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 gl_tree_create_empty,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 gl_tree_create,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 gl_tree_size,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 gl_tree_node_value,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 gl_tree_next_node,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 gl_tree_previous_node,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 gl_tree_get_at,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 gl_tree_set_at,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 gl_tree_search,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 gl_tree_indexof,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 gl_tree_add_first,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 gl_tree_add_last,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 gl_tree_add_before,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 gl_tree_add_after,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 gl_tree_add_at,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 gl_tree_remove_node,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 gl_tree_remove_at,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 gl_tree_remove,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 gl_tree_list_free,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 gl_tree_iterator,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 gl_tree_iterator_from_to,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 gl_tree_iterator_next,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 gl_tree_iterator_free,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 gl_tree_sortedlist_search,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 gl_tree_sortedlist_indexof,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 gl_tree_sortedlist_add,
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 gl_tree_sortedlist_remove
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 };