1 /* Ordered set data type implemented by a binary tree.
2 Copyright (C) 2006 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 2, or (at your option)
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, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /* Common code of gl_avltree_oset.c and gl_rbtree_oset.c. */
21 /* An item on the stack used for iterating across the elements. */
28 /* A stack used for iterating across the elements. */
29 typedef iterstack_item_t iterstack_t[MAXHEIGHT];
32 gl_tree_create_empty (gl_oset_implementation_t implementation,
33 gl_setelement_compar_fn compar_fn)
35 struct gl_oset_impl *set =
36 (struct gl_oset_impl *) xmalloc (sizeof (struct gl_oset_impl));
38 set->base.vtable = implementation;
39 set->base.compar_fn = compar_fn;
47 gl_tree_size (gl_oset_t set)
53 gl_tree_search (gl_oset_t set, const void *elt)
55 gl_setelement_compar_fn compar = set->base.compar_fn;
58 for (node = set->root; node != NULL; )
60 int cmp = (compar != NULL
61 ? compar (node->value, elt)
62 : (node->value > elt ? 1 :
63 node->value < elt ? -1 : 0));
70 /* We have an element equal to ELT. */
77 gl_tree_search_node (gl_oset_t set, const void *elt)
79 gl_setelement_compar_fn compar = set->base.compar_fn;
82 for (node = set->root; node != NULL; )
84 int cmp = (compar != NULL
85 ? compar (node->value, elt)
86 : (node->value > elt ? 1 :
87 node->value < elt ? -1 : 0));
94 /* We have an element equal to ELT. */
101 gl_tree_add (gl_oset_t set, const void *elt)
103 gl_setelement_compar_fn compar;
104 gl_oset_node_t node = set->root;
108 gl_tree_add_first (set, elt);
112 compar = set->base.compar_fn;
116 int cmp = (compar != NULL
117 ? compar (node->value, elt)
118 : (node->value > elt ? 1 :
119 node->value < elt ? -1 : 0));
123 if (node->right == NULL)
125 gl_tree_add_after (set, node, elt);
132 if (node->left == NULL)
134 gl_tree_add_before (set, node, elt);
145 gl_tree_remove (gl_oset_t set, const void *elt)
147 gl_oset_node_t node = gl_tree_search_node (set, elt);
150 return gl_tree_remove_node (set, node);
156 gl_tree_oset_free (gl_oset_t set)
158 /* Iterate across all elements in post-order. */
159 gl_oset_node_t node = set->root;
161 iterstack_item_t *stack_ptr = &stack[0];
165 /* Descend on left branch. */
170 stack_ptr->node = node;
171 stack_ptr->rightp = false;
175 /* Climb up again. */
178 if (stack_ptr == &stack[0])
181 node = stack_ptr->node;
182 if (!stack_ptr->rightp)
184 /* Free the current node. */
187 /* Descend on right branch. */
188 stack_ptr->rightp = true;
196 /* --------------------- gl_oset_iterator_t Data Type --------------------- */
198 static gl_oset_iterator_t
199 gl_tree_iterator (gl_oset_t set)
201 gl_oset_iterator_t result;
204 result.vtable = set->base.vtable;
206 /* Start node is the leftmost node. */
209 while (node->left != NULL)
212 /* End point is past the rightmost node. */
224 gl_tree_iterator_next (gl_oset_iterator_t *iterator, const void **eltp)
226 if (iterator->p != iterator->q)
228 gl_oset_node_t node = (gl_oset_node_t) iterator->p;
230 /* Advance to the next node. */
231 if (node->right != NULL)
234 while (node->left != NULL)
239 while (node->parent != NULL && node->parent->right == node)
251 gl_tree_iterator_free (gl_oset_iterator_t *iterator)