]> git.sesse.net Git - ffmpeg/blob - libavutil/tree.c
Merge commit 'b146d74730ab9ec5abede9066f770ad851e45fbc'
[ffmpeg] / libavutil / tree.c
1 /*
2  * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "log.h"
22 #include "mem.h"
23 #include "tree.h"
24
25 typedef struct AVTreeNode {
26     struct AVTreeNode *child[2];
27     void *elem;
28     int state;
29 } AVTreeNode;
30
31 const int av_tree_node_size = sizeof(AVTreeNode);
32
33 void *av_tree_find(const AVTreeNode *t, void *key,
34                    int (*cmp)(void *key, const void *b), void *next[2])
35 {
36     if (t) {
37         unsigned int v = cmp(key, t->elem);
38         if (v) {
39             if (next) next[v >> 31] = t->elem;
40             return av_tree_find(t->child[(v >> 31) ^ 1], key, cmp, next);
41         } else {
42             if (next) {
43                 av_tree_find(t->child[0], key, cmp, next);
44                 av_tree_find(t->child[1], key, cmp, next);
45             }
46             return t->elem;
47         }
48     }
49     return NULL;
50 }
51
52 void *av_tree_insert(AVTreeNode **tp, void *key,
53                      int (*cmp)(void *key, const void *b), AVTreeNode **next)
54 {
55     AVTreeNode *t = *tp;
56     if (t) {
57         unsigned int v = cmp(t->elem, key);
58         void *ret;
59         if (!v) {
60             if (*next)
61                 return t->elem;
62             else if (t->child[0] || t->child[1]) {
63                 int i = !t->child[0];
64                 void *next_elem[2];
65                 av_tree_find(t->child[i], key, cmp, next_elem);
66                 key = t->elem = next_elem[i];
67                 v = -i;
68             } else {
69                 *next = t;
70                 *tp = NULL;
71                 return NULL;
72             }
73         }
74         ret = av_tree_insert(&t->child[v >> 31], key, cmp, next);
75         if (!ret) {
76             int i = (v >> 31) ^ !!*next;
77             AVTreeNode **child = &t->child[i];
78             t->state += 2 * i - 1;
79
80             if (!(t->state & 1)) {
81                 if (t->state) {
82                     /* The following code is equivalent to
83                     if((*child)->state*2 == -t->state)
84                         rotate(child, i^1);
85                     rotate(tp, i);
86
87                     with rotate():
88                     static void rotate(AVTreeNode **tp, int i) {
89                         AVTreeNode *t= *tp;
90
91                         *tp= t->child[i];
92                         t->child[i]= t->child[i]->child[i^1];
93                         (*tp)->child[i^1]= t;
94                         i= 4*t->state + 2*(*tp)->state + 12;
95                           t  ->state=                     ((0x614586 >> i) & 3)-1;
96                         (*tp)->state= ((*tp)->state>>1) + ((0x400EEA >> i) & 3)-1;
97                     }
98                     but such a rotate function is both bigger and slower
99                     */
100                     if (( *child )->state * 2 == -t->state) {
101                         *tp                    = (*child)->child[i ^ 1];
102                         (*child)->child[i ^ 1] = (*tp)->child[i];
103                         (*tp)->child[i]        = *child;
104                         *child                 = ( *tp )->child[i ^ 1];
105                         (*tp)->child[i ^ 1]    = t;
106
107                         (*tp)->child[0]->state = -((*tp)->state > 0);
108                         (*tp)->child[1]->state =   (*tp)->state < 0;
109                         (*tp)->state           = 0;
110                     } else {
111                         *tp                    = *child;
112                         *child                 = (*child)->child[i ^ 1];
113                         (*tp)->child[i ^ 1]    = t;
114                         if ((*tp)->state) t->state = 0;
115                         else              t->state >>= 1;
116                         (*tp)->state           = -t->state;
117                     }
118                 }
119             }
120             if (!(*tp)->state ^ !!*next)
121                 return key;
122         }
123         return ret;
124     } else {
125         *tp   = *next;
126         *next = NULL;
127         if (*tp) {
128             (*tp)->elem = key;
129             return NULL;
130         } else
131             return key;
132     }
133 }
134
135 void av_tree_destroy(AVTreeNode *t)
136 {
137     if (t) {
138         av_tree_destroy(t->child[0]);
139         av_tree_destroy(t->child[1]);
140         av_free(t);
141     }
142 }
143
144 void av_tree_enumerate(AVTreeNode *t, void *opaque,
145                        int (*cmp)(void *opaque, void *elem),
146                        int (*enu)(void *opaque, void *elem))
147 {
148     if (t) {
149         int v = cmp ? cmp(opaque, t->elem) : 0;
150         if (v >= 0)
151             av_tree_enumerate(t->child[0], opaque, cmp, enu);
152         if (v == 0)
153             enu(opaque, t->elem);
154         if (v <= 0)
155             av_tree_enumerate(t->child[1], opaque, cmp, enu);
156     }
157 }
158
159 #ifdef TEST
160
161 #include "common.h"
162 #include "lfg.h"
163
164 static int check(AVTreeNode *t)
165 {
166     if (t) {
167         int left  = check(t->child[0]);
168         int right = check(t->child[1]);
169
170         if (left>999 || right>999)
171             return 1000;
172         if (right - left != t->state)
173             return 1000;
174         if (t->state>1 || t->state<-1)
175             return 1000;
176         return FFMAX(left, right) + 1;
177     }
178     return 0;
179 }
180
181 static void print(AVTreeNode *t, int depth)
182 {
183     int i;
184     for (i = 0; i < depth * 4; i++) av_log(NULL, AV_LOG_ERROR, " ");
185     if (t) {
186         av_log(NULL, AV_LOG_ERROR, "Node %p %2d %p\n", t, t->state, t->elem);
187         print(t->child[0], depth + 1);
188         print(t->child[1], depth + 1);
189     } else
190         av_log(NULL, AV_LOG_ERROR, "NULL\n");
191 }
192
193 static int cmp(void *a, const void *b)
194 {
195     return (uint8_t *) a - (const uint8_t *) b;
196 }
197
198 int main (void)
199 {
200     int i;
201     void *k;
202     AVTreeNode *root = NULL, *node = NULL;
203     AVLFG prng;
204
205     av_lfg_init(&prng, 1);
206
207     for (i = 0; i < 10000; i++) {
208         int j = av_lfg_get(&prng) % 86294;
209         if (check(root) > 999) {
210             av_log(NULL, AV_LOG_ERROR, "FATAL error %d\n", i);
211         print(root, 0);
212             return -1;
213         }
214         av_log(NULL, AV_LOG_ERROR, "inserting %4d\n", j);
215         if (!node)
216             node = av_mallocz(av_tree_node_size);
217         av_tree_insert(&root, (void *) (j + 1), cmp, &node);
218
219         j = av_lfg_get(&prng) % 86294;
220         {
221             AVTreeNode *node2 = NULL;
222             av_log(NULL, AV_LOG_ERROR, "removing %4d\n", j);
223             av_tree_insert(&root, (void *) (j + 1), cmp, &node2);
224             k = av_tree_find(root, (void *) (j + 1), cmp, NULL);
225             if (k)
226                 av_log(NULL, AV_LOG_ERROR, "removal failure %d\n", i);
227         }
228     }
229     return 0;
230 }
231 #endif