annotate lib/gl_rbtreehash_list.c @ 10780:5c7a68d31801

Add support for Haiku.
author Bruno Haible <bruno@clisp.org>
date Mon, 10 Nov 2008 12:37:32 +0100
parents 25f7280c9cf0
children e8d2c6fc33ad
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.
9686
25f7280c9cf0 New abstract list operation 'node_set_value'.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
2 Copyright (C) 2006, 2008 Free Software Foundation, Inc.
6981
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
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
6981
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
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.
6981
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
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/>. */
6981
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
7304
1c4ed7637c24 Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 6981
diff changeset
18 #include <config.h>
6981
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 /* Specification. */
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #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
22
7383
2cdff416776f Use the stdint module.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
23 #include <stdint.h> /* for SIZE_MAX */
6981
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #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
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 "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
27 #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
28 #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
29
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 #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
31 # 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
32 #endif
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 #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
35
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 /* 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
37 #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
38
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 /* -------------------------- 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
40
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 /* 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
42 #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
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 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
45 #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
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 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
48 #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
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 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
51 #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
52
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 /* 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
54 #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
55
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 /* 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
57 #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
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 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
60 #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
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: 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
63 #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
64
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 /* For debugging. */
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 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
67 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
68 {
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 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
70 (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
71 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
72 (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
73
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 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
75 abort ();
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 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
77 == (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
78 + 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
79 abort ();
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 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
81 abort ();
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 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
83 abort ();
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 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
85 abort ();
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 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
88 }
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 void
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 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
91 {
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 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
93 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
94 }
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96
7400
a18184ca7fbb Fix a weird typo.
Bruno Haible <bruno@clisp.org>
parents: 7383
diff changeset
97 const struct gl_list_implementation gl_rbtreehash_list_implementation =
6981
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 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
100 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
101 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
102 gl_tree_node_value,
9686
25f7280c9cf0 New abstract list operation 'node_set_value'.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
103 gl_tree_node_set_value,
6981
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_next_node,
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_previous_node,
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_get_at,
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_set_at,
7405
0de49c40e105 Add searching operations, limited to a subsequence of the list.
Bruno Haible <bruno@clisp.org>
parents: 7400
diff changeset
108 gl_tree_search_from_to,
0de49c40e105 Add searching operations, limited to a subsequence of the list.
Bruno Haible <bruno@clisp.org>
parents: 7400
diff changeset
109 gl_tree_indexof_from_to,
6981
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_add_first,
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_add_last,
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_before,
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_after,
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_at,
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_remove_node,
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_remove_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,
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_list_free,
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_iterator,
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_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
121 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
122 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
123 gl_tree_sortedlist_search,
7410
9704ff2cbdfe Add bounded list search operations.
Bruno Haible <bruno@clisp.org>
parents: 7405
diff changeset
124 gl_tree_sortedlist_search_from_to,
6981
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_indexof,
7410
9704ff2cbdfe Add bounded list search operations.
Bruno Haible <bruno@clisp.org>
parents: 7405
diff changeset
126 gl_tree_sortedlist_indexof_from_to,
6981
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 };