Originally committed as revision 11400 to svn://svn.ffmpeg.org/ffmpeg/trunk
Michael Niedermayer authored on 2008/01/05 03:20:03... | ... |
@@ -51,39 +51,55 @@ void *av_tree_insert(AVTreeNode **tp, void *key, int (*cmp)(void *key, const voi |
51 | 51 |
AVTreeNode *t= *tp; |
52 | 52 |
if(t){ |
53 | 53 |
unsigned int v= cmp(t->elem, key); |
54 |
- if(v){ |
|
55 |
- int i= v>>31; |
|
56 |
- AVTreeNode **child= &t->child[i]; |
|
57 |
- void *ret= av_tree_insert(child, key, cmp, next); |
|
54 |
+ void *ret; |
|
55 |
+ if(!v){ |
|
56 |
+ if(*next) |
|
57 |
+ return t->elem; |
|
58 |
+ else if(t->child[0]||t->child[1]){ |
|
59 |
+ int i= !t->child[0]; |
|
60 |
+ AVTreeNode **child= &t->child[i]; |
|
61 |
+ void *next_elem[2]; |
|
62 |
+ av_tree_find(*child, key, cmp, next_elem); |
|
63 |
+ key= t->elem= next_elem[i]; |
|
64 |
+ v= -i; |
|
65 |
+ }else{ |
|
66 |
+ *next= t; |
|
67 |
+ *tp=NULL; |
|
68 |
+ return NULL; |
|
69 |
+ } |
|
70 |
+ } |
|
71 |
+ ret= av_tree_insert(&t->child[v>>31], key, cmp, next); |
|
58 | 72 |
if(!ret){ |
59 |
- t->state -= ((int)v>>31)|1; |
|
73 |
+ int i= (v>>31) ^ !!*next; |
|
74 |
+ AVTreeNode **child= &t->child[i]; |
|
75 |
+ t->state += 2*i - 1; |
|
76 |
+ |
|
60 | 77 |
if(!(t->state&1)){ |
61 | 78 |
if(t->state){ |
62 |
- if((*child)->state*2 == t->state){ |
|
63 |
- *tp= *child; |
|
64 |
- *child= (*child)->child[i^1]; |
|
65 |
- (*tp)->child[i^1]= t; |
|
66 |
- t->state= 0; |
|
67 |
- }else{ |
|
79 |
+ if((*child)->state*2 == -t->state){ |
|
68 | 80 |
*tp= (*child)->child[i^1]; |
69 | 81 |
(*child)->child[i^1]= (*tp)->child[i]; |
70 | 82 |
(*tp)->child[i]= *child; |
71 | 83 |
*child= (*tp)->child[i^1]; |
72 | 84 |
(*tp)->child[i^1]= t; |
73 | 85 |
|
74 |
- i= (*tp)->state > 0; |
|
75 |
- (*tp)->child[i ]->state= 0; |
|
76 |
- (*tp)->child[i^1]->state= -(*tp)->state; |
|
86 |
+ (*tp)->child[0]->state= -((*tp)->state>0); |
|
87 |
+ (*tp)->child[1]->state= (*tp)->state<0 ; |
|
88 |
+ (*tp)->state=0; |
|
89 |
+ }else{ |
|
90 |
+ *tp= *child; |
|
91 |
+ *child= (*child)->child[i^1]; |
|
92 |
+ (*tp)->child[i^1]= t; |
|
93 |
+ if((*tp)->state) t->state = 0; |
|
94 |
+ else t->state>>= 1; |
|
95 |
+ (*tp)->state= -t->state; |
|
77 | 96 |
} |
78 |
- (*tp)->state=0; |
|
79 | 97 |
} |
80 |
- return key; |
|
81 | 98 |
} |
99 |
+ if(!(*tp)->state ^ !!*next) |
|
100 |
+ return key; |
|
82 | 101 |
} |
83 | 102 |
return ret; |
84 |
- }else{ |
|
85 |
- return t->elem; |
|
86 |
- } |
|
87 | 103 |
}else{ |
88 | 104 |
*tp= *next; *next= NULL; |
89 | 105 |
(*tp)->elem= key; |
... | ... |
@@ -140,17 +156,29 @@ int cmp(const void *a, const void *b){ |
140 | 140 |
|
141 | 141 |
int main(void){ |
142 | 142 |
int i,j,k; |
143 |
- AVTreeNode *root= NULL; |
|
143 |
+ AVTreeNode *root= NULL, *node=NULL; |
|
144 | 144 |
|
145 | 145 |
for(i=0; i<10000; i++){ |
146 |
- int j= (random()%863294); |
|
146 |
+ int j= (random()%86294); |
|
147 | 147 |
if(check(root) > 999){ |
148 | 148 |
av_log(NULL, AV_LOG_ERROR, "FATAL error %d\n", i); |
149 | 149 |
print(root, 0); |
150 | 150 |
return -1; |
151 | 151 |
} |
152 | 152 |
av_log(NULL, AV_LOG_ERROR, "inserting %4d\n", j); |
153 |
- av_tree_insert(&root, (void*)(j+1), cmp); |
|
153 |
+ if(!node) |
|
154 |
+ node= av_mallocz(av_tree_node_size); |
|
155 |
+ av_tree_insert(&root, (void*)(j+1), cmp, &node); |
|
156 |
+ |
|
157 |
+ j= (random()%86294); |
|
158 |
+ k= av_tree_find(root, (void*)(j+1), cmp, NULL); |
|
159 |
+ if(k){ |
|
160 |
+ AVTreeNode *node2=NULL; |
|
161 |
+ av_tree_insert(&root, (void*)(j+1), cmp, &node2); |
|
162 |
+ k= av_tree_find(root, (void*)(j+1), cmp, NULL); |
|
163 |
+ if(k) |
|
164 |
+ av_log(NULL, AV_LOG_ERROR, "removial failure %d\n", i); |
|
165 |
+ } |
|
154 | 166 |
} |
155 | 167 |
return 0; |
156 | 168 |
} |