00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "log.h"
00022 #include "mem.h"
00023 #include "tree.h"
00024
00025 typedef struct AVTreeNode {
00026 struct AVTreeNode *child[2];
00027 void *elem;
00028 int state;
00029 } AVTreeNode;
00030
00031 const int av_tree_node_size = sizeof(AVTreeNode);
00032
00033 void *av_tree_find(const AVTreeNode *t, void *key,
00034 int (*cmp)(void *key, const void *b), void *next[2])
00035 {
00036 if (t) {
00037 unsigned int v = cmp(key, t->elem);
00038 if (v) {
00039 if (next) next[v >> 31] = t->elem;
00040 return av_tree_find(t->child[(v >> 31) ^ 1], key, cmp, next);
00041 } else {
00042 if (next) {
00043 av_tree_find(t->child[0], key, cmp, next);
00044 av_tree_find(t->child[1], key, cmp, next);
00045 }
00046 return t->elem;
00047 }
00048 }
00049 return NULL;
00050 }
00051
00052 void *av_tree_insert(AVTreeNode **tp, void *key,
00053 int (*cmp)(void *key, const void *b), AVTreeNode **next)
00054 {
00055 AVTreeNode *t = *tp;
00056 if (t) {
00057 unsigned int v = cmp(t->elem, key);
00058 void *ret;
00059 if (!v) {
00060 if (*next)
00061 return t->elem;
00062 else if (t->child[0] || t->child[1]) {
00063 int i = !t->child[0];
00064 void *next_elem[2];
00065 av_tree_find(t->child[i], key, cmp, next_elem);
00066 key = t->elem = next_elem[i];
00067 v = -i;
00068 } else {
00069 *next = t;
00070 *tp = NULL;
00071 return NULL;
00072 }
00073 }
00074 ret = av_tree_insert(&t->child[v >> 31], key, cmp, next);
00075 if (!ret) {
00076 int i = (v >> 31) ^ !!*next;
00077 AVTreeNode **child = &t->child[i];
00078 t->state += 2 * i - 1;
00079
00080 if (!(t->state & 1)) {
00081 if (t->state) {
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 if (( *child )->state * 2 == -t->state) {
00101 *tp = (*child)->child[i ^ 1];
00102 (*child)->child[i ^ 1] = (*tp)->child[i];
00103 (*tp)->child[i] = *child;
00104 *child = ( *tp )->child[i ^ 1];
00105 (*tp)->child[i ^ 1] = t;
00106
00107 (*tp)->child[0]->state = -((*tp)->state > 0);
00108 (*tp)->child[1]->state = (*tp)->state < 0;
00109 (*tp)->state = 0;
00110 } else {
00111 *tp = *child;
00112 *child = (*child)->child[i ^ 1];
00113 (*tp)->child[i ^ 1] = t;
00114 if ((*tp)->state) t->state = 0;
00115 else t->state >>= 1;
00116 (*tp)->state = -t->state;
00117 }
00118 }
00119 }
00120 if (!(*tp)->state ^ !!*next)
00121 return key;
00122 }
00123 return ret;
00124 } else {
00125 *tp = *next;
00126 *next = NULL;
00127 if (*tp) {
00128 (*tp)->elem = key;
00129 return NULL;
00130 } else
00131 return key;
00132 }
00133 }
00134
00135 void av_tree_destroy(AVTreeNode *t)
00136 {
00137 if (t) {
00138 av_tree_destroy(t->child[0]);
00139 av_tree_destroy(t->child[1]);
00140 av_free(t);
00141 }
00142 }
00143
00144 void av_tree_enumerate(AVTreeNode *t, void *opaque,
00145 int (*cmp)(void *opaque, void *elem),
00146 int (*enu)(void *opaque, void *elem))
00147 {
00148 if (t) {
00149 int v = cmp ? cmp(opaque, t->elem) : 0;
00150 if (v >= 0)
00151 av_tree_enumerate(t->child[0], opaque, cmp, enu);
00152 if (v == 0)
00153 enu(opaque, t->elem);
00154 if (v <= 0)
00155 av_tree_enumerate(t->child[1], opaque, cmp, enu);
00156 }
00157 }
00158
00159 #ifdef TEST
00160
00161 #include "common.h"
00162 #include "lfg.h"
00163
00164 static int check(AVTreeNode *t)
00165 {
00166 if (t) {
00167 int left = check(t->child[0]);
00168 int right = check(t->child[1]);
00169
00170 if (left>999 || right>999)
00171 return 1000;
00172 if (right - left != t->state)
00173 return 1000;
00174 if (t->state>1 || t->state<-1)
00175 return 1000;
00176 return FFMAX(left, right) + 1;
00177 }
00178 return 0;
00179 }
00180
00181 static void print(AVTreeNode *t, int depth)
00182 {
00183 int i;
00184 for (i = 0; i < depth * 4; i++) av_log(NULL, AV_LOG_ERROR, " ");
00185 if (t) {
00186 av_log(NULL, AV_LOG_ERROR, "Node %p %2d %p\n", t, t->state, t->elem);
00187 print(t->child[0], depth + 1);
00188 print(t->child[1], depth + 1);
00189 } else
00190 av_log(NULL, AV_LOG_ERROR, "NULL\n");
00191 }
00192
00193 static int cmp(void *a, const void *b)
00194 {
00195 return (uint8_t *) a - (const uint8_t *) b;
00196 }
00197
00198 int main (void)
00199 {
00200 int i;
00201 void *k;
00202 AVTreeNode *root = NULL, *node = NULL;
00203 AVLFG prng;
00204
00205 av_lfg_init(&prng, 1);
00206
00207 for (i = 0; i < 10000; i++) {
00208 int j = av_lfg_get(&prng) % 86294;
00209 if (check(root) > 999) {
00210 av_log(NULL, AV_LOG_ERROR, "FATAL error %d\n", i);
00211 print(root, 0);
00212 return -1;
00213 }
00214 av_log(NULL, AV_LOG_ERROR, "inserting %4d\n", j);
00215 if (!node)
00216 node = av_mallocz(av_tree_node_size);
00217 av_tree_insert(&root, (void *) (j + 1), cmp, &node);
00218
00219 j = av_lfg_get(&prng) % 86294;
00220 {
00221 AVTreeNode *node2 = NULL;
00222 av_log(NULL, AV_LOG_ERROR, "removing %4d\n", j);
00223 av_tree_insert(&root, (void *) (j + 1), cmp, &node2);
00224 k = av_tree_find(root, (void *) (j + 1), cmp, NULL);
00225 if (k)
00226 av_log(NULL, AV_LOG_ERROR, "removal failure %d\n", i);
00227 }
00228 }
00229 return 0;
00230 }
00231 #endif