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