Code clean-up for crc.c, lfg.c, log.c, random_see.d, rational.c and tree.c.
[libav.git] / libavutil / tree.c
CommitLineData
9eb88fde
MN
1/*
2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3 *
2912e87a 4 * This file is part of Libav.
9eb88fde 5 *
2912e87a 6 * Libav is free software; you can redistribute it and/or
9eb88fde
MN
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 *
2912e87a 11 * Libav is distributed in the hope that it will be useful,
9eb88fde
MN
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
2912e87a 17 * License along with Libav; if not, write to the Free Software
9eb88fde
MN
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
9eb88fde
MN
21#include "log.h"
22#include "tree.h"
23
425b45d4 24typedef struct AVTreeNode {
9eb88fde
MN
25 struct AVTreeNode *child[2];
26 void *elem;
27 int state;
425b45d4 28} AVTreeNode;
9eb88fde 29
6e8b982b
MN
30const int av_tree_node_size = sizeof(AVTreeNode);
31
425b45d4
YM
32void *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) {
116d15cc
MN
42 av_tree_find(t->child[0], key, cmp, next);
43 av_tree_find(t->child[1], key, cmp, next);
44 }
9eb88fde
MN
45 return t->elem;
46 }
47 }
48 return NULL;
49}
50
425b45d4
YM
51void *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);
f05dda3b 57 void *ret;
425b45d4
YM
58 if (!v) {
59 if (*next)
f05dda3b 60 return t->elem;
425b45d4
YM
61 else if (t->child[0] || t->child[1]) {
62 int i = !t->child[0];
f05dda3b 63 void *next_elem[2];
3f161c7e 64 av_tree_find(t->child[i], key, cmp, next_elem);
425b45d4
YM
65 key = t->elem = next_elem[i];
66 v = -i;
67 } else {
68 *next = t;
69 *tp = NULL;
f05dda3b
MN
70 return NULL;
71 }
72 }
425b45d4
YM
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) {
51198a87
MN
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():
425b45d4 87 static void rotate(AVTreeNode **tp, int i) {
51198a87
MN
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 */
425b45d4
YM
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;
9eb88fde 116 }
9eb88fde
MN
117 }
118 }
425b45d4 119 if (!(*tp)->state ^ !!*next)
a35bf971
MN
120 return key;
121 }
122 return ret;
425b45d4
YM
123 } else {
124 *tp = *next;
125 *next = NULL;
126 if (*tp) {
127 (*tp)->elem = key;
eed36075 128 return NULL;
425b45d4 129 } else
eed36075 130 return key;
9eb88fde
MN
131 }
132}
133
425b45d4
YM
134void av_tree_destroy(AVTreeNode *t)
135{
136 if (t) {
045cbba9
AJ
137 av_tree_destroy(t->child[0]);
138 av_tree_destroy(t->child[1]);
139 av_free(t);
d8bd113e 140 }
9eb88fde
MN
141}
142
425b45d4
YM
143void 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);
edabf359 155 }
9eb88fde 156}
9eb88fde
MN
157
158#ifdef TEST
294eaa26
DB
159
160#include "lfg.h"
161
425b45d4
YM
162static int check(AVTreeNode *t)
163{
164 if (t) {
165 int left = check(t->child[0]);
166 int right = check(t->child[1]);
9eb88fde 167
425b45d4 168 if (left>999 || right>999)
9eb88fde 169 return 1000;
425b45d4 170 if (right - left != t->state)
9eb88fde 171 return 1000;
425b45d4 172 if (t->state>1 || t->state<-1)
9eb88fde 173 return 1000;
425b45d4 174 return FFMAX(left, right) + 1;
9eb88fde
MN
175 }
176 return 0;
177}
178
425b45d4
YM
179static void print(AVTreeNode *t, int depth)
180{
9eb88fde 181 int i;
425b45d4
YM
182 for (i = 0; i < depth * 4; i++) av_log(NULL, AV_LOG_ERROR, " ");
183 if (t) {
168fffdf 184 av_log(NULL, AV_LOG_ERROR, "Node %p %2d %p\n", t, t->state, t->elem);
425b45d4
YM
185 print(t->child[0], depth + 1);
186 print(t->child[1], depth + 1);
187 } else
9eb88fde
MN
188 av_log(NULL, AV_LOG_ERROR, "NULL\n");
189}
190
425b45d4
YM
191static int cmp(void *a, const void *b)
192{
193 return (uint8_t *) a - (const uint8_t *) b;
9eb88fde
MN
194}
195
425b45d4
YM
196int main (void)
197{
168fffdf
BF
198 int i;
199 void *k;
425b45d4 200 AVTreeNode *root = NULL, *node = NULL;
64bde197 201 AVLFG prng;
294eaa26 202
64bde197 203 av_lfg_init(&prng, 1);
9eb88fde 204
425b45d4 205 for (i = 0; i < 10000; i++) {
64bde197 206 int j = av_lfg_get(&prng) % 86294;
425b45d4 207 if (check(root) > 999) {
9eb88fde
MN
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);
425b45d4
YM
213 if (!node)
214 node = av_mallocz(av_tree_node_size);
215 av_tree_insert(&root, (void *) (j + 1), cmp, &node);
f05dda3b 216
64bde197 217 j = av_lfg_get(&prng) % 86294;
eed36075 218 {
425b45d4 219 AVTreeNode *node2 = NULL;
5d1e3d22 220 av_log(NULL, AV_LOG_ERROR, "removing %4d\n", j);
425b45d4
YM
221 av_tree_insert(&root, (void *) (j + 1), cmp, &node2);
222 k = av_tree_find(root, (void *) (j + 1), cmp, NULL);
223 if (k)
89c9ff50 224 av_log(NULL, AV_LOG_ERROR, "removal failure %d\n", i);
f05dda3b 225 }
9eb88fde
MN
226 }
227 return 0;
228}
229#endif