1 /* Ordered set data type implemented by a binary tree.
2 Copyright (C) 2006-2007, 2009-2021 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2006.
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.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Common code of gl_avltree_oset.c and gl_rbtree_oset.c. */
20 /* An item on the stack used for iterating across the elements. */
27 /* A stack used for iterating across the elements. */
28 typedef iterstack_item_t iterstack_t[MAXHEIGHT];
31 gl_tree_nx_create_empty (gl_oset_implementation_t implementation,
32 gl_setelement_compar_fn compar_fn,
33 gl_setelement_dispose_fn dispose_fn)
35 struct gl_oset_impl *set =
36 (struct gl_oset_impl *) malloc (sizeof (struct gl_oset_impl));
41 set->base.vtable = implementation;
42 set->base.compar_fn = compar_fn;
43 set->base.dispose_fn = dispose_fn;
50 static size_t _GL_ATTRIBUTE_PURE
51 gl_tree_size (gl_oset_t set)
56 /* Returns the next node in the tree, or NULL if there is none. */
57 static inline gl_oset_node_t _GL_ATTRIBUTE_PURE
58 gl_tree_next_node (gl_oset_node_t node)
60 if (node->right != NULL)
63 while (node->left != NULL)
68 while (node->parent != NULL && node->parent->right == node)
75 /* Returns the previous node in the tree, or NULL if there is none. */
76 static inline gl_oset_node_t _GL_ATTRIBUTE_PURE
77 gl_tree_prev_node (gl_oset_node_t node)
79 if (node->left != NULL)
82 while (node->right != NULL)
87 while (node->parent != NULL && node->parent->left == node)
95 gl_tree_search (gl_oset_t set, const void *elt)
97 gl_setelement_compar_fn compar = set->base.compar_fn;
100 for (node = set->root; node != NULL; )
102 int cmp = (compar != NULL
103 ? compar (node->value, elt)
104 : (node->value > elt ? 1 :
105 node->value < elt ? -1 : 0));
112 /* We have an element equal to ELT. */
119 gl_tree_search_atleast (gl_oset_t set,
120 gl_setelement_threshold_fn threshold_fn,
121 const void *threshold,
126 for (node = set->root; node != NULL; )
128 if (! threshold_fn (node->value, threshold))
132 /* We have an element >= THRESHOLD. But we need the leftmost such
134 gl_oset_node_t found = node;
136 for (; node != NULL; )
138 if (! threshold_fn (node->value, threshold))
146 *eltp = found->value;
153 static gl_oset_node_t
154 gl_tree_search_node (gl_oset_t set, const void *elt)
156 gl_setelement_compar_fn compar = set->base.compar_fn;
159 for (node = set->root; node != NULL; )
161 int cmp = (compar != NULL
162 ? compar (node->value, elt)
163 : (node->value > elt ? 1 :
164 node->value < elt ? -1 : 0));
171 /* We have an element equal to ELT. */
178 gl_tree_nx_add (gl_oset_t set, const void *elt)
180 gl_setelement_compar_fn compar;
181 gl_oset_node_t node = set->root;
185 if (gl_tree_nx_add_first (set, elt) == NULL)
190 compar = set->base.compar_fn;
194 int cmp = (compar != NULL
195 ? compar (node->value, elt)
196 : (node->value > elt ? 1 :
197 node->value < elt ? -1 : 0));
201 if (node->right == NULL)
203 if (gl_tree_nx_add_after (set, node, elt) == NULL)
211 if (node->left == NULL)
213 if (gl_tree_nx_add_before (set, node, elt) == NULL)
225 gl_tree_remove (gl_oset_t set, const void *elt)
227 gl_oset_node_t node = gl_tree_search_node (set, elt);
230 return gl_tree_remove_node (set, node);
236 gl_tree_update (gl_oset_t set, const void *elt,
237 void (*action) (const void * /*elt*/, void * /*action_data*/),
240 /* Like gl_tree_remove, action (...), gl_tree_nx_add, except that we don't
241 actually remove ELT. */
242 /* Remember the old node. Don't free it. */
243 gl_oset_node_t old_node = gl_tree_search_node (set, elt);
245 action (elt, action_data);
246 /* Determine where to put the node now. */
247 if (old_node != NULL)
251 gl_setelement_compar_fn compar = set->base.compar_fn;
253 gl_oset_node_t prev_node = gl_tree_prev_node (old_node);
254 gl_oset_node_t next_node = gl_tree_next_node (old_node);
256 ? (prev_node == NULL || compar (prev_node->value, elt) < 0)
257 && (next_node == NULL || compar (next_node->value, elt) > 0)
258 : (prev_node == NULL || prev_node->value < elt)
259 && (next_node == NULL || next_node->value > elt)))
261 /* old_node needs to move in the tree. */
264 /* Remove the node from the tree. Don't free it. */
265 gl_tree_remove_node_no_free (set, old_node);
271 int cmp = (compar != NULL
272 ? compar (node->value, elt)
273 : (node->value > elt ? 1 :
274 node->value < elt ? -1 : 0));
278 if (node->right == NULL)
280 gl_tree_add_node_after (set, node, old_node);
287 if (node->left == NULL)
289 gl_tree_add_node_before (set, node, old_node);
296 /* Two elements are the same. */
297 NODE_PAYLOAD_DISPOSE (set, old_node)
309 gl_tree_oset_free (gl_oset_t set)
311 /* Iterate across all elements in post-order. */
312 gl_oset_node_t node = set->root;
314 iterstack_item_t *stack_ptr = &stack[0];
318 /* Descend on left branch. */
323 stack_ptr->node = node;
324 stack_ptr->rightp = false;
328 /* Climb up again. */
331 if (stack_ptr == &stack[0])
334 node = stack_ptr->node;
335 if (!stack_ptr->rightp)
337 /* Free the current node. */
338 if (set->base.dispose_fn != NULL)
339 set->base.dispose_fn (node->value);
342 /* Descend on right branch. */
343 stack_ptr->rightp = true;
351 /* --------------------- gl_oset_iterator_t Data Type --------------------- */
353 static gl_oset_iterator_t _GL_ATTRIBUTE_PURE
354 gl_tree_iterator (gl_oset_t set)
356 gl_oset_iterator_t result;
359 result.vtable = set->base.vtable;
361 /* Start node is the leftmost node. */
364 while (node->left != NULL)
367 /* End point is past the rightmost node. */
369 #if defined GCC_LINT || defined lint
378 static gl_oset_iterator_t
379 gl_tree_iterator_atleast (gl_oset_t set,
380 gl_setelement_threshold_fn threshold_fn,
381 const void *threshold)
383 gl_oset_iterator_t result;
386 result.vtable = set->base.vtable;
388 /* End point is past the rightmost node. */
390 #if defined GCC_LINT || defined lint
396 for (node = set->root; node != NULL; )
398 if (! threshold_fn (node->value, threshold))
402 /* We have an element >= THRESHOLD. But we need the leftmost such
404 gl_oset_node_t found = node;
406 for (; node != NULL; )
408 if (! threshold_fn (node->value, threshold))
425 gl_tree_iterator_next (gl_oset_iterator_t *iterator, const void **eltp)
427 if (iterator->p != iterator->q)
429 gl_oset_node_t node = (gl_oset_node_t) iterator->p;
431 /* Advance to the next node. */
432 node = gl_tree_next_node (node);
441 gl_tree_iterator_free (gl_oset_iterator_t *iterator _GL_ATTRIBUTE_MAYBE_UNUSED)