Move the malloc checking from module 'oset' to new module 'xoset'.
[gnulib.git] / lib / gl_anytreehash_list1.h
1 /* Sequential list data type implemented by a hash table with a binary tree.
2    Copyright (C) 2006-2007, 2009 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 3 of the License, or
8    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
17
18 /* Common code of gl_avltreehash_list.c and gl_rbtreehash_list.c.  */
19
20 /* Hash table entry representing the same value at more than one position.  */
21 struct gl_multiple_nodes
22 {
23   struct gl_hash_entry h;           /* hash table entry fields; must be first */
24   void *magic;                      /* used to distinguish from single node */
25   gl_oset_t nodes;                  /* set of nodes, sorted by position */
26 };
27 /* A value that cannot occur at the corresponding field (->left) in
28    gl_list_node_impl.  */
29 #define MULTIPLE_NODES_MAGIC  (void *) -1
30
31 /* Resize the hash table if needed, after list->count was incremented.  */
32 static inline void
33 hash_resize_after_add (gl_list_t list)
34 {
35   size_t count = (list->root != 0 ? list->root->branch_size : 0);
36   size_t estimate = xsum (count, count / 2); /* 1.5 * count */
37   if (estimate > list->table_size)
38     hash_resize (list, estimate);
39 }
40
41 /* Return the position of the given node in the tree.  */
42 static size_t
43 node_position (gl_list_node_t node)
44 {
45   size_t position = 0;
46
47   if (node->left != NULL)
48     position += node->left->branch_size;
49   for (;;)
50     {
51       gl_list_node_t parent = node->parent;
52
53       if (parent == NULL)
54         return position;
55       /* position is now relative to the subtree of node.  */
56       if (parent->right == node)
57         {
58           position += 1;
59           if (parent->left != NULL)
60             position += parent->left->branch_size;
61         }
62       /* position is now relative to the subtree of parent.  */
63       node = parent;
64     }
65 }
66
67 /* Compares two nodes by their position in the tree.  */
68 static int
69 compare_by_position (const void *x1, const void *x2)
70 {
71   gl_list_node_t node1 = (gl_list_node_t) x1;
72   gl_list_node_t node2 = (gl_list_node_t) x2;
73   size_t position1 = node_position (node1);
74   size_t position2 = node_position (node2);
75
76   return (position1 > position2 ? 1 :
77           position1 < position2 ? -1 : 0);
78 }
79
80 /* Compares a node's position in the tree with a given threshold.  */
81 static bool
82 compare_position_threshold (const void *x, const void *threshold)
83 {
84   gl_list_node_t node = (gl_list_node_t) x;
85   size_t position = node_position (node);
86   return (position >= (uintptr_t)threshold);
87 }
88
89 /* Return the first element of a non-empty ordered set of nodes.  */
90 static inline gl_list_node_t
91 gl_oset_first (gl_oset_t set)
92 {
93   gl_oset_iterator_t iter = gl_oset_iterator (set);
94   const void *first;
95
96   if (!gl_oset_iterator_next (&iter, &first))
97     abort ();
98   gl_oset_iterator_free (&iter);
99   return (gl_list_node_t) first;
100 }
101
102 /* Add a node to the hash table structure.
103    If duplicates are allowed, this function performs in average time
104    O((log n)^2): gl_oset_nx_add may need to add an element to an ordered set
105    of size O(n), needing O(log n) comparison function calls.  The comparison
106    function is compare_by_position, which is O(log n) worst-case.
107    If duplicates are forbidden, this function is O(1).  */
108 static void
109 add_to_bucket (gl_list_t list, gl_list_node_t new_node)
110 {
111   size_t bucket = new_node->h.hashcode % list->table_size;
112
113   /* If no duplicates are allowed, multiple nodes are not needed.  */
114   if (list->base.allow_duplicates)
115     {
116       size_t hashcode = new_node->h.hashcode;
117       const void *value = new_node->value;
118       gl_listelement_equals_fn equals = list->base.equals_fn;
119       gl_hash_entry_t *entryp;
120
121       for (entryp = &list->table[bucket]; *entryp != NULL; entryp = &(*entryp)->hash_next)
122         {
123           gl_hash_entry_t entry = *entryp;
124
125           if (entry->hashcode == hashcode)
126             {
127               if (((struct gl_multiple_nodes *) entry)->magic == MULTIPLE_NODES_MAGIC)
128                 {
129                   /* An entry representing multiple nodes.  */
130                   gl_oset_t nodes = ((struct gl_multiple_nodes *) entry)->nodes;
131                   /* Only the first node is interesting.  */
132                   gl_list_node_t node = gl_oset_first (nodes);
133                   if (equals != NULL ? equals (value, node->value) : value == node->value)
134                     {
135                       /* Found already multiple nodes with the same value.
136                          Add the new_node to it.  */
137                       if (gl_oset_nx_add (nodes, new_node) < 0)
138                         xalloc_die ();
139                       return;
140                     }
141                 }
142               else
143                 {
144                   /* An entry representing a single node.  */
145                   gl_list_node_t node = (struct gl_list_node_impl *) entry;
146                   if (equals != NULL ? equals (value, node->value) : value == node->value)
147                     {
148                       /* Found already a node with the same value.  Turn it
149                          into an ordered set, and add new_node to it.  */
150                       gl_oset_t nodes;
151                       struct gl_multiple_nodes *multi_entry;
152
153                       nodes =
154                         gl_oset_nx_create_empty (OSET_TREE_FLAVOR,
155                                                  compare_by_position, NULL);
156                       if (nodes == NULL)
157                         xalloc_die ();
158
159                       if (gl_oset_nx_add (nodes, node) < 0)
160                         xalloc_die ();
161                       if (gl_oset_nx_add (nodes, new_node) < 0)
162                         xalloc_die ();
163
164                       multi_entry = XMALLOC (struct gl_multiple_nodes);
165                       multi_entry->h.hash_next = entry->hash_next;
166                       multi_entry->h.hashcode = entry->hashcode;
167                       multi_entry->magic = MULTIPLE_NODES_MAGIC;
168                       multi_entry->nodes = nodes;
169                       *entryp = &multi_entry->h;
170                       return;
171                     }
172                 }
173             }
174         }
175     }
176   /* If no duplicates are allowed, multiple nodes are not needed.  */
177   new_node->h.hash_next = list->table[bucket];
178   list->table[bucket] = &new_node->h;
179 }
180
181 /* Remove a node from the hash table structure.
182    If duplicates are allowed, this function performs in average time
183    O((log n)^2): gl_oset_remove may need to remove an element from an ordered
184    set of size O(n), needing O(log n) comparison function calls.  The
185    comparison function is compare_by_position, which is O(log n) worst-case.
186    If duplicates are forbidden, this function is O(1) on average.  */
187 static void
188 remove_from_bucket (gl_list_t list, gl_list_node_t old_node)
189 {
190   size_t bucket = old_node->h.hashcode % list->table_size;
191
192   if (list->base.allow_duplicates)
193     {
194       size_t hashcode = old_node->h.hashcode;
195       const void *value = old_node->value;
196       gl_listelement_equals_fn equals = list->base.equals_fn;
197       gl_hash_entry_t *entryp;
198
199       for (entryp = &list->table[bucket]; ; entryp = &(*entryp)->hash_next)
200         {
201           gl_hash_entry_t entry = *entryp;
202
203           if (entry == &old_node->h)
204             {
205               /* Found old_node as a single node in the bucket.  Remove it.  */
206               *entryp = old_node->h.hash_next;
207               break;
208             }
209           if (entry == NULL)
210             /* node is not in the right bucket.  Did the hash codes
211                change inadvertently?  */
212             abort ();
213           if (((struct gl_multiple_nodes *) entry)->magic == MULTIPLE_NODES_MAGIC
214               && entry->hashcode == hashcode)
215             {
216               /* An entry representing multiple nodes.  */
217               gl_oset_t nodes = ((struct gl_multiple_nodes *) entry)->nodes;
218               /* Only the first node is interesting.  */
219               gl_list_node_t node = gl_oset_first (nodes);
220               if (equals != NULL ? equals (value, node->value) : value == node->value)
221                 {
222                   /* Found multiple nodes with the same value.
223                      old_node must be one of them.  Remove it.  */
224                   if (!gl_oset_remove (nodes, old_node))
225                     abort ();
226                   if (gl_oset_size (nodes) == 1)
227                     {
228                       /* Replace a one-element set with a single node.  */
229                       node = gl_oset_first (nodes);
230                       node->h.hash_next = entry->hash_next;
231                       *entryp = &node->h;
232                       gl_oset_free (nodes);
233                       free (entry);
234                     }
235                   break;
236                 }
237             }
238         }
239     }
240   else
241     {
242       /* If no duplicates are allowed, multiple nodes are not needed.  */
243       gl_hash_entry_t *entryp;
244
245       for (entryp = &list->table[bucket]; ; entryp = &(*entryp)->hash_next)
246         {
247           if (*entryp == &old_node->h)
248             {
249               *entryp = old_node->h.hash_next;
250               break;
251             }
252           if (*entryp == NULL)
253             /* node is not in the right bucket.  Did the hash codes
254                change inadvertently?  */
255             abort ();
256         }
257     }
258 }
259
260 /* Build up the hash table during initialization: Store all the nodes of
261    list->root in the hash table.  */
262 static inline void
263 add_nodes_to_buckets (gl_list_t list)
264 {
265   /* Iterate across all nodes.  */
266   gl_list_node_t node = list->root;
267   iterstack_t stack;
268   iterstack_item_t *stack_ptr = &stack[0];
269
270   for (;;)
271     {
272       /* Descend on left branch.  */
273       for (;;)
274         {
275           if (node == NULL)
276             break;
277           stack_ptr->node = node;
278           stack_ptr->rightp = false;
279           node = node->left;
280           stack_ptr++;
281         }
282       /* Climb up again.  */
283       for (;;)
284         {
285           if (stack_ptr == &stack[0])
286             return;
287           stack_ptr--;
288           if (!stack_ptr->rightp)
289             break;
290         }
291       node = stack_ptr->node;
292       /* Add the current node to the hash table.  */
293       node->h.hashcode =
294         (list->base.hashcode_fn != NULL
295          ? list->base.hashcode_fn (node->value)
296          : (size_t)(uintptr_t) node->value);
297       add_to_bucket (list, node);
298       /* Descend on right branch.  */
299       stack_ptr->rightp = true;
300       node = node->right;
301       stack_ptr++;
302     }
303 }