Code clean-up for crc.c, lfg.c, log.c, random_see.d, rational.c and tree.c.
[libav.git] / libavutil / tree.c
1 /*
2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of Libav.
5 *
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "log.h"
22 #include "tree.h"
23
24 typedef struct AVTreeNode {
25 struct AVTreeNode *child[2];
26 void *elem;
27 int state;
28 } AVTreeNode;
29
30 const int av_tree_node_size = sizeof(AVTreeNode);
31
32 void *av_tree_find(const AVTreeNode *t, void *key,
33 int (*cmp)(void *key, const void *b), void *next[2])
34 {
35 if (t) {
36 unsigned int v = cmp(key, t->elem);
37 if (v) {
38 if (next) next[v >> 31] = t->elem;
39 return av_tree_find(t->child[(v >> 31) ^ 1], key, cmp, next);
40 } else {
41 if (next) {
42 av_tree_find(t->child[0], key, cmp, next);
43 av_tree_find(t->child[1], key, cmp, next);
44 }
45 return t->elem;
46 }
47 }
48 return NULL;
49 }
50
51 void *av_tree_insert(AVTreeNode **tp, void *key,
52 int (*cmp)(void *key, const void *b), AVTreeNode **next)
53 {
54 AVTreeNode *t = *tp;
55 if (t) {
56 unsigned int v = cmp(t->elem, key);
57 void *ret;
58 if (!v) {
59 if (*next)
60 return t->elem;
61 else if (t->child[0] || t->child[1]) {
62 int i = !t->child[0];
63 void *next_elem[2];
64 av_tree_find(t->child[i], key, cmp, next_elem);
65 key = t->elem = next_elem[i];
66 v = -i;
67 } else {
68 *next = t;
69 *tp = NULL;
70 return NULL;
71 }
72 }
73 ret = av_tree_insert(&t->child[v >> 31], key, cmp, next);
74 if (!ret) {
75 int i = (v >> 31) ^ !!*next;
76 AVTreeNode **child = &t->child[i];
77 t->state += 2 * i - 1;
78
79 if (!(t->state & 1)) {
80 if (t->state) {
81 /* The following code is equivalent to
82 if((*child)->state*2 == -t->state)
83 rotate(child, i^1);
84 rotate(tp, i);
85
86 with rotate():
87 static void rotate(AVTreeNode **tp, int i) {
88 AVTreeNode *t= *tp;
89
90 *tp= t->child[i];
91 t->child[i]= t->child[i]->child[i^1];
92 (*tp)->child[i^1]= t;
93 i= 4*t->state + 2*(*tp)->state + 12;
94 t ->state= ((0x614586 >> i) & 3)-1;
95 (*tp)->state= ((*tp)->state>>1) + ((0x400EEA >> i) & 3)-1;
96 }
97 but such a rotate function is both bigger and slower
98 */
99 if (( *child )->state * 2 == -t->state) {
100 *tp = (*child)->child[i ^ 1];
101 (*child)->child[i ^ 1] = (*tp)->child[i];
102 (*tp)->child[i] = *child;
103 *child = ( *tp )->child[i ^ 1];
104 (*tp)->child[i ^ 1] = t;
105
106 (*tp)->child[0]->state = -((*tp)->state > 0);
107 (*tp)->child[1]->state = (*tp)->state < 0;
108 (*tp)->state = 0;
109 } else {
110 *tp = *child;
111 *child = (*child)->child[i ^ 1];
112 (*tp)->child[i ^ 1] = t;
113 if ((*tp)->state) t->state = 0;
114 else t->state >>= 1;
115 (*tp)->state = -t->state;
116 }
117 }
118 }
119 if (!(*tp)->state ^ !!*next)
120 return key;
121 }
122 return ret;
123 } else {
124 *tp = *next;
125 *next = NULL;
126 if (*tp) {
127 (*tp)->elem = key;
128 return NULL;
129 } else
130 return key;
131 }
132 }
133
134 void av_tree_destroy(AVTreeNode *t)
135 {
136 if (t) {
137 av_tree_destroy(t->child[0]);
138 av_tree_destroy(t->child[1]);
139 av_free(t);
140 }
141 }
142
143 void av_tree_enumerate(AVTreeNode *t, void *opaque,
144 int (*cmp)(void *opaque, void *elem),
145 int (*enu)(void *opaque, void *elem))
146 {
147 if (t) {
148 int v = cmp ? cmp(opaque, t->elem) : 0;
149 if (v >= 0)
150 av_tree_enumerate(t->child[0], opaque, cmp, enu);
151 if (v == 0)
152 enu(opaque, t->elem);
153 if (v <= 0)
154 av_tree_enumerate(t->child[1], opaque, cmp, enu);
155 }
156 }
157
158 #ifdef TEST
159
160 #include "lfg.h"
161
162 static int check(AVTreeNode *t)
163 {
164 if (t) {
165 int left = check(t->child[0]);
166 int right = check(t->child[1]);
167
168 if (left>999 || right>999)
169 return 1000;
170 if (right - left != t->state)
171 return 1000;
172 if (t->state>1 || t->state<-1)
173 return 1000;
174 return FFMAX(left, right) + 1;
175 }
176 return 0;
177 }
178
179 static void print(AVTreeNode *t, int depth)
180 {
181 int i;
182 for (i = 0; i < depth * 4; i++) av_log(NULL, AV_LOG_ERROR, " ");
183 if (t) {
184 av_log(NULL, AV_LOG_ERROR, "Node %p %2d %p\n", t, t->state, t->elem);
185 print(t->child[0], depth + 1);
186 print(t->child[1], depth + 1);
187 } else
188 av_log(NULL, AV_LOG_ERROR, "NULL\n");
189 }
190
191 static int cmp(void *a, const void *b)
192 {
193 return (uint8_t *) a - (const uint8_t *) b;
194 }
195
196 int main (void)
197 {
198 int i;
199 void *k;
200 AVTreeNode *root = NULL, *node = NULL;
201 AVLFG prng;
202
203 av_lfg_init(&prng, 1);
204
205 for (i = 0; i < 10000; i++) {
206 int j = av_lfg_get(&prng) % 86294;
207 if (check(root) > 999) {
208 av_log(NULL, AV_LOG_ERROR, "FATAL error %d\n", i);
209 print(root, 0);
210 return -1;
211 }
212 av_log(NULL, AV_LOG_ERROR, "inserting %4d\n", j);
213 if (!node)
214 node = av_mallocz(av_tree_node_size);
215 av_tree_insert(&root, (void *) (j + 1), cmp, &node);
216
217 j = av_lfg_get(&prng) % 86294;
218 {
219 AVTreeNode *node2 = NULL;
220 av_log(NULL, AV_LOG_ERROR, "removing %4d\n", j);
221 av_tree_insert(&root, (void *) (j + 1), cmp, &node2);
222 k = av_tree_find(root, (void *) (j + 1), cmp, NULL);
223 if (k)
224 av_log(NULL, AV_LOG_ERROR, "removal failure %d\n", i);
225 }
226 }
227 return 0;
228 }
229 #endif