annotate lib/gl_rbtreehash_list.c @ 7405:0de49c40e105

Add searching operations, limited to a subsequence of the list.
author Bruno Haible <bruno@clisp.org>
date Thu, 05 Oct 2006 12:45:16 +0000
parents a18184ca7fbb
children 9704ff2cbdfe
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
7304
1c4ed7637c24 Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 6981
diff changeset
19 #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
20
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 /* Specification. */
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #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
23
7383
2cdff416776f Use the stdint module.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
24 #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
25 #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
26
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #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
28 #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
29 #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
30
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 #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
32 # 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
33 #endif
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 #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
36
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 /* 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
38 #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
39
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 /* -------------------------- 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
41
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 /* 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
43 #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
44
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 /* 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
46 #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
47
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 /* 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
49 #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
50
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 /* 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
52 #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
53
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 /* 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
55 #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
56
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 /* 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
58 #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
59
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 /* 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
61 #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
62
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 /* 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
64 #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
65
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 /* For debugging. */
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 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
68 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
69 {
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 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
71 (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
72 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
73 (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
74
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 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
76 abort ();
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->branch_size
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 == (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
79 + 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
80 abort ();
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 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
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 (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
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 (!(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
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
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 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
89 }
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 void
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 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
92 {
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 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
94 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
95 }
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97
7400
a18184ca7fbb Fix a weird typo.
Bruno Haible <bruno@clisp.org>
parents: 7383
diff changeset
98 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
99 {
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_empty,
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_create,
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_size,
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_node_value,
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,
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_sortedlist_indexof,
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_add,
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_remove
0bc4f2ff0eae Sequential list data type implemented by a hash table with a binary tree.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 };