annotate lib/gl_anytree_omap.h @ 40057:b06060465f09

maint: Run 'make update-copyright'
author Paul Eggert <eggert@cs.ucla.edu>
date Tue, 01 Jan 2019 00:25:11 +0100
parents 975fc034aecf
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
40006
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Ordered map data type implemented by a binary tree.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 40012
diff changeset
2 Copyright (C) 2006-2007, 2009-2019 Free Software Foundation, Inc.
40006
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2018.
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 (at your option) any later version.
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 /* Common code of gl_avltree_omap.c and gl_rbtree_omap.c. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 /* An item on the stack used for iterating across the elements. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 typedef struct
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 gl_omap_node_t node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 bool rightp;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 } iterstack_item_t;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 /* A stack used for iterating across the elements. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 typedef iterstack_item_t iterstack_t[MAXHEIGHT];
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 static gl_omap_t
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 gl_tree_nx_create_empty (gl_omap_implementation_t implementation,
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 gl_mapkey_compar_fn compar_fn,
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 gl_mapkey_dispose_fn kdispose_fn,
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 gl_mapvalue_dispose_fn vdispose_fn)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 struct gl_omap_impl *map =
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 (struct gl_omap_impl *) malloc (sizeof (struct gl_omap_impl));
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 if (map == NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 return NULL;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 map->base.vtable = implementation;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 map->base.compar_fn = compar_fn;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 map->base.kdispose_fn = kdispose_fn;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 map->base.vdispose_fn = vdispose_fn;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 map->root = NULL;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 map->count = 0;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 return map;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 static size_t
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 gl_tree_size (gl_omap_t map)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 return map->count;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 static bool
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 gl_tree_search (gl_omap_t map, const void *key, const void **valuep)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 gl_mapkey_compar_fn compar = map->base.compar_fn;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 gl_omap_node_t node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 for (node = map->root; node != NULL; )
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 int cmp = (compar != NULL
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 ? compar (node->key, key)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 : (node->key > key ? 1 :
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 node->key < key ? -1 : 0));
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 if (cmp < 0)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 node = node->right;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 else if (cmp > 0)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 node = node->left;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 else /* cmp == 0 */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 /* We have a key equal to KEY. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 *valuep = node->value;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 return true;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 return false;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 static bool
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 gl_tree_search_atleast (gl_omap_t map,
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 gl_mapkey_threshold_fn threshold_fn,
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 const void *threshold,
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 const void **keyp, const void **valuep)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 gl_omap_node_t node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 for (node = map->root; node != NULL; )
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 if (! threshold_fn (node->key, threshold))
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 node = node->right;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 else
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 /* We have a key >= THRESHOLD. But we need the leftmost such
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 element. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 gl_omap_node_t found = node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 node = node->left;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 for (; node != NULL; )
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 if (! threshold_fn (node->key, threshold))
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 node = node->right;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 else
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 found = node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 node = node->left;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 *keyp = found->key;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 *valuep = found->value;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 return true;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 return false;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 static int
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 gl_tree_nx_getput (gl_omap_t map, const void *key, const void *value,
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 const void **oldvaluep)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 gl_mapkey_compar_fn compar;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 gl_omap_node_t node = map->root;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 if (node == NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 if (gl_tree_nx_add_first (map, key, value) == NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 return -1;
40012
975fc034aecf array-omap, avltree-omap, rbtree-omap: Tweak style.
Bruno Haible <bruno@clisp.org>
parents: 40006
diff changeset
132 return 1;
40006
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135 compar = map->base.compar_fn;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 for (;;)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139 int cmp = (compar != NULL
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140 ? compar (node->key, key)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141 : (node->key > key ? 1 :
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 node->key < key ? -1 : 0));
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144 if (cmp < 0)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146 if (node->right == NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
148 if (gl_tree_nx_add_after (map, node, key, value) == NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
149 return -1;
40012
975fc034aecf array-omap, avltree-omap, rbtree-omap: Tweak style.
Bruno Haible <bruno@clisp.org>
parents: 40006
diff changeset
150 return 1;
40006
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
152 node = node->right;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
153 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154 else if (cmp > 0)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
156 if (node->left == NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
157 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
158 if (gl_tree_nx_add_before (map, node, key, value) == NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
159 return -1;
40012
975fc034aecf array-omap, avltree-omap, rbtree-omap: Tweak style.
Bruno Haible <bruno@clisp.org>
parents: 40006
diff changeset
160 return 1;
40006
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
161 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
162 node = node->left;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
163 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
164 else /* cmp == 0 */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
166 *oldvaluep = node->value;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
167 node->value = value;
40012
975fc034aecf array-omap, avltree-omap, rbtree-omap: Tweak style.
Bruno Haible <bruno@clisp.org>
parents: 40006
diff changeset
168 return 0;
40006
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
169 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
170 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
171 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
172
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
173 static bool
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
174 gl_tree_getremove (gl_omap_t map, const void *key, const void **oldvaluep)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
175 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
176 gl_mapkey_compar_fn compar = map->base.compar_fn;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
177 gl_omap_node_t node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
178
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
179 for (node = map->root; node != NULL; )
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
180 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
181 int cmp = (compar != NULL
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
182 ? compar (node->key, key)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
183 : (node->key > key ? 1 :
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
184 node->key < key ? -1 : 0));
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
185
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
186 if (cmp < 0)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
187 node = node->right;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
188 else if (cmp > 0)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
189 node = node->left;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
190 else /* cmp == 0 */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
191 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
192 /* We have a key equal to KEY. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
193 *oldvaluep = node->value;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
194 gl_tree_remove_node (map, node);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
195 return true;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
196 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
197 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
198 return false;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
199 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
200
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
201 static void
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
202 gl_tree_omap_free (gl_omap_t map)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
203 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
204 /* Iterate across all elements in post-order. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
205 gl_omap_node_t node = map->root;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
206 iterstack_t stack;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
207 iterstack_item_t *stack_ptr = &stack[0];
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
208
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
209 for (;;)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
210 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
211 /* Descend on left branch. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
212 for (;;)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
213 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
214 if (node == NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
215 break;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
216 stack_ptr->node = node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
217 stack_ptr->rightp = false;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
218 node = node->left;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
219 stack_ptr++;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
220 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
221 /* Climb up again. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
222 for (;;)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
223 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
224 if (stack_ptr == &stack[0])
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
225 goto done_iterate;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
226 stack_ptr--;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
227 node = stack_ptr->node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
228 if (!stack_ptr->rightp)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
229 break;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
230 /* Free the current node. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
231 if (map->base.vdispose_fn != NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
232 map->base.vdispose_fn (node->value);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
233 if (map->base.kdispose_fn != NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
234 map->base.kdispose_fn (node->key);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
235 free (node);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
236 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
237 /* Descend on right branch. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
238 stack_ptr->rightp = true;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
239 node = node->right;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
240 stack_ptr++;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
241 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
242 done_iterate:
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
243 free (map);
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
244 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
245
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
246 /* --------------------- gl_omap_iterator_t Data Type --------------------- */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
247
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
248 static gl_omap_iterator_t
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
249 gl_tree_iterator (gl_omap_t map)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
250 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
251 gl_omap_iterator_t result;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
252 gl_omap_node_t node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
253
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
254 result.vtable = map->base.vtable;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
255 result.map = map;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
256 /* Start node is the leftmost node. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
257 node = map->root;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
258 if (node != NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
259 while (node->left != NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
260 node = node->left;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
261 result.p = node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
262 /* End point is past the rightmost node. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
263 result.q = NULL;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
264 #if defined GCC_LINT || defined lint
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
265 result.i = 0;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
266 result.j = 0;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
267 result.count = 0;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
268 #endif
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
269
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
270 return result;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
271 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
272
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
273 static bool
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
274 gl_tree_iterator_next (gl_omap_iterator_t *iterator,
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
275 const void **keyp, const void **valuep)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
276 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
277 if (iterator->p != iterator->q)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
278 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
279 gl_omap_node_t node = (gl_omap_node_t) iterator->p;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
280 *keyp = node->key;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
281 *valuep = node->value;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
282 /* Advance to the next node. */
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
283 if (node->right != NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
284 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
285 node = node->right;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
286 while (node->left != NULL)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
287 node = node->left;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
288 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
289 else
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
290 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
291 while (node->parent != NULL && node->parent->right == node)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
292 node = node->parent;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
293 node = node->parent;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
294 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
295 iterator->p = node;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
296 return true;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
297 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
298 else
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
299 return false;
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
300 }
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
301
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
302 static void
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
303 gl_tree_iterator_free (gl_omap_iterator_t *iterator)
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
304 {
0caeb347e88e avltree-omap: New module.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
305 }