2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
6 * FFmpeg 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.
11 * FFmpeg 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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 typedef struct AVTreeNode {
25 struct AVTreeNode *child[2];
30 const int av_tree_node_size = sizeof(AVTreeNode);
32 void *av_tree_find(const AVTreeNode *t, void *key,
33 int (*cmp)(void *key, const void *b), void *next[2])
36 unsigned int v = cmp(key, t->elem);
38 if (next) next[v >> 31] = t->elem;
39 return av_tree_find(t->child[(v >> 31) ^ 1], key, cmp, next);
42 av_tree_find(t->child[0], key, cmp, next);
43 av_tree_find(t->child[1], key, cmp, next);
51 void *av_tree_insert(AVTreeNode **tp, void *key,
52 int (*cmp)(void *key, const void *b), AVTreeNode **next)
56 unsigned int v = cmp(t->elem, key);
61 else if (t->child[0] || t->child[1]) {
64 av_tree_find(t->child[i], key, cmp, next_elem);
65 key = t->elem = next_elem[i];
73 ret = av_tree_insert(&t->child[v >> 31], key, cmp, next);
75 int i = (v >> 31) ^ !!*next;
76 AVTreeNode **child = &t->child[i];
77 t->state += 2 * i - 1;
79 if (!(t->state & 1)) {
81 /* The following code is equivalent to
82 if((*child)->state*2 == -t->state)
87 static void rotate(AVTreeNode **tp, int i) {
91 t->child[i]= t->child[i]->child[i^1];
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;
97 but such a rotate function is both bigger and slower
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;
106 (*tp)->child[0]->state = -((*tp)->state > 0);
107 (*tp)->child[1]->state = (*tp)->state < 0;
111 *child = (*child)->child[i ^ 1];
112 (*tp)->child[i ^ 1] = t;
113 if ((*tp)->state) t->state = 0;
115 (*tp)->state = -t->state;
119 if (!(*tp)->state ^ !!*next)
134 void av_tree_destroy(AVTreeNode *t)
137 av_tree_destroy(t->child[0]);
138 av_tree_destroy(t->child[1]);
143 void av_tree_enumerate(AVTreeNode *t, void *opaque,
144 int (*cmp)(void *opaque, void *elem),
145 int (*enu)(void *opaque, void *elem))
148 int v = cmp ? cmp(opaque, t->elem) : 0;
150 av_tree_enumerate(t->child[0], opaque, cmp, enu);
152 enu(opaque, t->elem);
154 av_tree_enumerate(t->child[1], opaque, cmp, enu);
162 static int check(AVTreeNode *t)
165 int left = check(t->child[0]);
166 int right = check(t->child[1]);
168 if (left>999 || right>999)
170 if (right - left != t->state)
172 if (t->state>1 || t->state<-1)
174 return FFMAX(left, right) + 1;
179 static void print(AVTreeNode *t, int depth)
182 for (i = 0; i < depth * 4; i++) av_log(NULL, AV_LOG_ERROR, " ");
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);
188 av_log(NULL, AV_LOG_ERROR, "NULL\n");
191 static int cmp(void *a, const void *b)
193 return (uint8_t *) a - (const uint8_t *) b;
200 AVTreeNode *root = NULL, *node = NULL;
203 av_lfg_init(&prng, 1);
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);
212 av_log(NULL, AV_LOG_ERROR, "inserting %4d\n", j);
214 node = av_mallocz(av_tree_node_size);
215 av_tree_insert(&root, (void *) (j + 1), cmp, &node);
217 j = av_lfg_get(&prng) % 86294;
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);
224 av_log(NULL, AV_LOG_ERROR, "removal failure %d\n", i);