Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 1 | /* |
| 2 | * copyright (c) 2006 Michael Niedermayer <[email protected]> |
| 3 | * |
Mans Rullgard | 2912e87 | 2011-03-18 17:35:10 | [diff] [blame] | 4 | * This file is part of Libav. |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 5 | * |
Mans Rullgard | 2912e87 | 2011-03-18 17:35:10 | [diff] [blame] | 6 | * Libav is free software; you can redistribute it and/or |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 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 | * |
Mans Rullgard | 2912e87 | 2011-03-18 17:35:10 | [diff] [blame] | 11 | * Libav is distributed in the hope that it will be useful, |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 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 |
Mans Rullgard | 2912e87 | 2011-03-18 17:35:10 | [diff] [blame] | 17 | * License along with Libav; if not, write to the Free Software |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
Diego Biurrun | c2e936d | 2013-07-30 15:28:44 | [diff] [blame] | 21 | #include "error.h" |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 22 | #include "log.h" |
Martin Storsjö | 1d9c2dc | 2012-08-06 13:49:32 | [diff] [blame] | 23 | #include "mem.h" |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 24 | #include "tree.h" |
| 25 | |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 26 | typedef struct AVTreeNode { |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 27 | struct AVTreeNode *child[2]; |
| 28 | void *elem; |
| 29 | int state; |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 30 | } AVTreeNode; |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 31 | |
Martin Storsjö | 9a92aea | 2012-10-11 12:08:04 | [diff] [blame] | 32 | struct AVTreeNode *av_tree_node_alloc(void) |
| 33 | { |
| 34 | return av_mallocz(sizeof(struct AVTreeNode)); |
| 35 | } |
Michael Niedermayer | 6e8b982 | 2008-01-04 17:52:16 | [diff] [blame] | 36 | |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 37 | void *av_tree_find(const AVTreeNode *t, void *key, |
| 38 | int (*cmp)(void *key, const void *b), void *next[2]) |
| 39 | { |
| 40 | if (t) { |
| 41 | unsigned int v = cmp(key, t->elem); |
| 42 | if (v) { |
Diego Biurrun | 10db1a9 | 2012-10-16 14:08:05 | [diff] [blame] | 43 | if (next) |
| 44 | next[v >> 31] = t->elem; |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 45 | return av_tree_find(t->child[(v >> 31) ^ 1], key, cmp, next); |
| 46 | } else { |
| 47 | if (next) { |
Michael Niedermayer | 116d15cc | 2008-01-04 10:14:21 | [diff] [blame] | 48 | av_tree_find(t->child[0], key, cmp, next); |
| 49 | av_tree_find(t->child[1], key, cmp, next); |
| 50 | } |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 51 | return t->elem; |
| 52 | } |
| 53 | } |
| 54 | return NULL; |
| 55 | } |
| 56 | |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 57 | void *av_tree_insert(AVTreeNode **tp, void *key, |
| 58 | int (*cmp)(void *key, const void *b), AVTreeNode **next) |
| 59 | { |
| 60 | AVTreeNode *t = *tp; |
| 61 | if (t) { |
| 62 | unsigned int v = cmp(t->elem, key); |
Michael Niedermayer | f05dda3 | 2008-01-04 18:20:03 | [diff] [blame] | 63 | void *ret; |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 64 | if (!v) { |
| 65 | if (*next) |
Michael Niedermayer | f05dda3 | 2008-01-04 18:20:03 | [diff] [blame] | 66 | return t->elem; |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 67 | else if (t->child[0] || t->child[1]) { |
| 68 | int i = !t->child[0]; |
Michael Niedermayer | f05dda3 | 2008-01-04 18:20:03 | [diff] [blame] | 69 | void *next_elem[2]; |
Michael Niedermayer | 3f161c7 | 2008-01-16 01:54:18 | [diff] [blame] | 70 | av_tree_find(t->child[i], key, cmp, next_elem); |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 71 | key = t->elem = next_elem[i]; |
Diego Biurrun | 10db1a9 | 2012-10-16 14:08:05 | [diff] [blame] | 72 | v = -i; |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 73 | } else { |
| 74 | *next = t; |
Diego Biurrun | 10db1a9 | 2012-10-16 14:08:05 | [diff] [blame] | 75 | *tp = NULL; |
Michael Niedermayer | f05dda3 | 2008-01-04 18:20:03 | [diff] [blame] | 76 | return NULL; |
| 77 | } |
| 78 | } |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 79 | ret = av_tree_insert(&t->child[v >> 31], key, cmp, next); |
| 80 | if (!ret) { |
Diego Biurrun | 10db1a9 | 2012-10-16 14:08:05 | [diff] [blame] | 81 | int i = (v >> 31) ^ !!*next; |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 82 | AVTreeNode **child = &t->child[i]; |
| 83 | t->state += 2 * i - 1; |
Michael Niedermayer | f05dda3 | 2008-01-04 18:20:03 | [diff] [blame] | 84 | |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 85 | if (!(t->state & 1)) { |
| 86 | if (t->state) { |
Michael Niedermayer | 51198a8 | 2008-01-23 21:03:21 | [diff] [blame] | 87 | /* The following code is equivalent to |
Diego Biurrun | 10db1a9 | 2012-10-16 14:08:05 | [diff] [blame] | 88 | * if ((*child)->state * 2 == -t->state) |
| 89 | * rotate(child, i ^ 1); |
| 90 | * rotate(tp, i); |
| 91 | * |
| 92 | * with rotate(): |
| 93 | * static void rotate(AVTreeNode **tp, int i) |
| 94 | * { |
| 95 | * AVTreeNode *t= *tp; |
| 96 | * |
| 97 | * *tp = t->child[i]; |
| 98 | * t->child[i] = t->child[i]->child[i ^ 1]; |
| 99 | * (*tp)->child[i ^ 1] = t; |
| 100 | * i = 4 * t->state + 2 * (*tp)->state + 12; |
| 101 | * t->state = ((0x614586 >> i) & 3) - 1; |
| 102 | * (*tp)->state = ((0x400EEA >> i) & 3) - 1 + |
| 103 | * ((*tp)->state >> 1); |
| 104 | * } |
| 105 | * but such a rotate function is both bigger and slower |
| 106 | */ |
| 107 | if ((*child)->state * 2 == -t->state) { |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 108 | *tp = (*child)->child[i ^ 1]; |
| 109 | (*child)->child[i ^ 1] = (*tp)->child[i]; |
| 110 | (*tp)->child[i] = *child; |
Diego Biurrun | 10db1a9 | 2012-10-16 14:08:05 | [diff] [blame] | 111 | *child = (*tp)->child[i ^ 1]; |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 112 | (*tp)->child[i ^ 1] = t; |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 113 | |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 114 | (*tp)->child[0]->state = -((*tp)->state > 0); |
Diego Biurrun | 10db1a9 | 2012-10-16 14:08:05 | [diff] [blame] | 115 | (*tp)->child[1]->state = (*tp)->state < 0; |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 116 | (*tp)->state = 0; |
| 117 | } else { |
Diego Biurrun | 10db1a9 | 2012-10-16 14:08:05 | [diff] [blame] | 118 | *tp = *child; |
| 119 | *child = (*child)->child[i ^ 1]; |
| 120 | (*tp)->child[i ^ 1] = t; |
| 121 | if ((*tp)->state) |
| 122 | t->state = 0; |
| 123 | else |
| 124 | t->state >>= 1; |
| 125 | (*tp)->state = -t->state; |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 126 | } |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 127 | } |
| 128 | } |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 129 | if (!(*tp)->state ^ !!*next) |
Michael Niedermayer | a35bf97 | 2008-01-04 19:16:38 | [diff] [blame] | 130 | return key; |
| 131 | } |
| 132 | return ret; |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 133 | } else { |
| 134 | *tp = *next; |
| 135 | *next = NULL; |
| 136 | if (*tp) { |
| 137 | (*tp)->elem = key; |
Michael Niedermayer | eed3607 | 2008-09-19 12:41:12 | [diff] [blame] | 138 | return NULL; |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 139 | } else |
Michael Niedermayer | eed3607 | 2008-09-19 12:41:12 | [diff] [blame] | 140 | return key; |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 144 | void av_tree_destroy(AVTreeNode *t) |
| 145 | { |
| 146 | if (t) { |
Aurelien Jacobs | 045cbba | 2009-01-04 17:48:54 | [diff] [blame] | 147 | av_tree_destroy(t->child[0]); |
| 148 | av_tree_destroy(t->child[1]); |
| 149 | av_free(t); |
Aurelien Jacobs | d8bd113 | 2009-01-04 17:48:19 | [diff] [blame] | 150 | } |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 151 | } |
| 152 | |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 153 | void av_tree_enumerate(AVTreeNode *t, void *opaque, |
| 154 | int (*cmp)(void *opaque, void *elem), |
| 155 | int (*enu)(void *opaque, void *elem)) |
| 156 | { |
| 157 | if (t) { |
| 158 | int v = cmp ? cmp(opaque, t->elem) : 0; |
| 159 | if (v >= 0) |
| 160 | av_tree_enumerate(t->child[0], opaque, cmp, enu); |
| 161 | if (v == 0) |
| 162 | enu(opaque, t->elem); |
| 163 | if (v <= 0) |
| 164 | av_tree_enumerate(t->child[1], opaque, cmp, enu); |
Michael Niedermayer | edabf35 | 2009-11-14 19:14:14 | [diff] [blame] | 165 | } |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 166 | } |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 167 | |
| 168 | #ifdef TEST |
Diego Biurrun | 294eaa2 | 2009-03-20 11:48:27 | [diff] [blame] | 169 | |
Martin Storsjö | 1d9c2dc | 2012-08-06 13:49:32 | [diff] [blame] | 170 | #include "common.h" |
Diego Biurrun | 294eaa2 | 2009-03-20 11:48:27 | [diff] [blame] | 171 | #include "lfg.h" |
| 172 | |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 173 | static int check(AVTreeNode *t) |
| 174 | { |
| 175 | if (t) { |
| 176 | int left = check(t->child[0]); |
| 177 | int right = check(t->child[1]); |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 178 | |
Diego Biurrun | 10db1a9 | 2012-10-16 14:08:05 | [diff] [blame] | 179 | if (left > 999 || right > 999) |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 180 | return 1000; |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 181 | if (right - left != t->state) |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 182 | return 1000; |
Diego Biurrun | 10db1a9 | 2012-10-16 14:08:05 | [diff] [blame] | 183 | if (t->state > 1 || t->state < -1) |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 184 | return 1000; |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 185 | return FFMAX(left, right) + 1; |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 186 | } |
| 187 | return 0; |
| 188 | } |
| 189 | |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 190 | static void print(AVTreeNode *t, int depth) |
| 191 | { |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 192 | int i; |
Diego Biurrun | 10db1a9 | 2012-10-16 14:08:05 | [diff] [blame] | 193 | for (i = 0; i < depth * 4; i++) |
| 194 | av_log(NULL, AV_LOG_ERROR, " "); |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 195 | if (t) { |
Benoit Fouet | 168fffd | 2009-03-31 14:00:46 | [diff] [blame] | 196 | av_log(NULL, AV_LOG_ERROR, "Node %p %2d %p\n", t, t->state, t->elem); |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 197 | print(t->child[0], depth + 1); |
| 198 | print(t->child[1], depth + 1); |
| 199 | } else |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 200 | av_log(NULL, AV_LOG_ERROR, "NULL\n"); |
| 201 | } |
| 202 | |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 203 | static int cmp(void *a, const void *b) |
| 204 | { |
| 205 | return (uint8_t *) a - (const uint8_t *) b; |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 206 | } |
| 207 | |
Diego Biurrun | 10db1a9 | 2012-10-16 14:08:05 | [diff] [blame] | 208 | int main(void) |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 209 | { |
Benoit Fouet | 168fffd | 2009-03-31 14:00:46 | [diff] [blame] | 210 | int i; |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 211 | AVTreeNode *root = NULL, *node = NULL; |
Diego Biurrun | 64bde19 | 2009-04-10 17:23:38 | [diff] [blame] | 212 | AVLFG prng; |
Diego Biurrun | 294eaa2 | 2009-03-20 11:48:27 | [diff] [blame] | 213 | |
Diego Biurrun | 64bde19 | 2009-04-10 17:23:38 | [diff] [blame] | 214 | av_lfg_init(&prng, 1); |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 215 | |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 216 | for (i = 0; i < 10000; i++) { |
Diego Biurrun | c2e936d | 2013-07-30 15:28:44 | [diff] [blame] | 217 | AVTreeNode *node2 = NULL; |
| 218 | intptr_t j = av_lfg_get(&prng) % 86294; |
| 219 | void *ret, *jj = (void *)(j + 1); |
| 220 | |
| 221 | while (ret = av_tree_find(root, jj, cmp, NULL)) { |
| 222 | j = av_lfg_get(&prng) % 86294; |
| 223 | jj = (void *)(j + 1); |
| 224 | } |
| 225 | |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 226 | if (check(root) > 999) { |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 227 | av_log(NULL, AV_LOG_ERROR, "FATAL error %d\n", i); |
Diego Biurrun | 10db1a9 | 2012-10-16 14:08:05 | [diff] [blame] | 228 | print(root, 0); |
Derek Buitenhuis | 530cd28 | 2013-10-22 18:08:43 | [diff] [blame] | 229 | return 1; |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 230 | } |
Diego Biurrun | c2e936d | 2013-07-30 15:28:44 | [diff] [blame] | 231 | |
Yordan Makariev | 425b45d | 2011-12-03 18:25:57 | [diff] [blame] | 232 | if (!node) |
Martin Storsjö | e002e32 | 2012-10-11 12:10:45 | [diff] [blame] | 233 | node = av_tree_node_alloc(); |
Diego Biurrun | c2e936d | 2013-07-30 15:28:44 | [diff] [blame] | 234 | if (!node) { |
| 235 | av_log(NULL, AV_LOG_ERROR, "Memory allocation failure.\n"); |
Derek Buitenhuis | 530cd28 | 2013-10-22 18:08:43 | [diff] [blame] | 236 | return 1; |
Michael Niedermayer | f05dda3 | 2008-01-04 18:20:03 | [diff] [blame] | 237 | } |
Diego Biurrun | c2e936d | 2013-07-30 15:28:44 | [diff] [blame] | 238 | av_tree_insert(&root, jj, cmp, &node); |
| 239 | |
| 240 | while (ret = av_tree_find(root, jj, cmp, NULL)) { |
| 241 | j = av_lfg_get(&prng) % 86294; |
| 242 | jj = (void *)(j + 1); |
| 243 | } |
| 244 | |
| 245 | ret = av_tree_insert(&root, jj, cmp, &node2); |
| 246 | if (ret != jj) |
| 247 | av_tree_destroy(node2); |
| 248 | ret = av_tree_find(root, jj, cmp, NULL); |
| 249 | if (ret) |
| 250 | av_log(NULL, AV_LOG_ERROR, "removal failure %d\n", i); |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 251 | } |
Diego Biurrun | c2e936d | 2013-07-30 15:28:44 | [diff] [blame] | 252 | |
| 253 | av_tree_destroy(root); |
| 254 | |
Michael Niedermayer | 9eb88fd | 2006-11-14 01:02:30 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | #endif |