tree.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #include "common.h"
  21. #include "log.h"
  22. #include "tree.h"
  23. typedef struct AVTreeNode{
  24. struct AVTreeNode *child[2];
  25. void *elem;
  26. int state;
  27. }AVTreeNode;
  28. const int av_tree_node_size = sizeof(AVTreeNode);
  29. void *av_tree_find(const AVTreeNode *t, void *key, int (*cmp)(void *key, const void *b), void *next[2]){
  30. if(t){
  31. unsigned int v= cmp(key, t->elem);
  32. if(v){
  33. if(next) next[v>>31]= t->elem;
  34. return av_tree_find(t->child[(v>>31)^1], key, cmp, next);
  35. }else{
  36. if(next){
  37. av_tree_find(t->child[0], key, cmp, next);
  38. av_tree_find(t->child[1], key, cmp, next);
  39. }
  40. return t->elem;
  41. }
  42. }
  43. return NULL;
  44. }
  45. void *av_tree_insert(AVTreeNode **tp, void *key, int (*cmp)(void *key, const void *b), AVTreeNode **next){
  46. AVTreeNode *t= *tp;
  47. if(t){
  48. unsigned int v= cmp(t->elem, key);
  49. void *ret;
  50. if(!v){
  51. if(*next)
  52. return t->elem;
  53. else if(t->child[0]||t->child[1]){
  54. int i= !t->child[0];
  55. void *next_elem[2];
  56. av_tree_find(t->child[i], key, cmp, next_elem);
  57. key= t->elem= next_elem[i];
  58. v= -i;
  59. }else{
  60. *next= t;
  61. *tp=NULL;
  62. return NULL;
  63. }
  64. }
  65. ret= av_tree_insert(&t->child[v>>31], key, cmp, next);
  66. if(!ret){
  67. int i= (v>>31) ^ !!*next;
  68. AVTreeNode **child= &t->child[i];
  69. t->state += 2*i - 1;
  70. if(!(t->state&1)){
  71. if(t->state){
  72. /* The following code is equivalent to
  73. if((*child)->state*2 == -t->state)
  74. rotate(child, i^1);
  75. rotate(tp, i);
  76. with rotate():
  77. static void rotate(AVTreeNode **tp, int i){
  78. AVTreeNode *t= *tp;
  79. *tp= t->child[i];
  80. t->child[i]= t->child[i]->child[i^1];
  81. (*tp)->child[i^1]= t;
  82. i= 4*t->state + 2*(*tp)->state + 12;
  83. t ->state= ((0x614586 >> i) & 3)-1;
  84. (*tp)->state= ((*tp)->state>>1) + ((0x400EEA >> i) & 3)-1;
  85. }
  86. but such a rotate function is both bigger and slower
  87. */
  88. if((*child)->state*2 == -t->state){
  89. *tp= (*child)->child[i^1];
  90. (*child)->child[i^1]= (*tp)->child[i];
  91. (*tp)->child[i]= *child;
  92. *child= (*tp)->child[i^1];
  93. (*tp)->child[i^1]= t;
  94. (*tp)->child[0]->state= -((*tp)->state>0);
  95. (*tp)->child[1]->state= (*tp)->state<0 ;
  96. (*tp)->state=0;
  97. }else{
  98. *tp= *child;
  99. *child= (*child)->child[i^1];
  100. (*tp)->child[i^1]= t;
  101. if((*tp)->state) t->state = 0;
  102. else t->state>>= 1;
  103. (*tp)->state= -t->state;
  104. }
  105. }
  106. }
  107. if(!(*tp)->state ^ !!*next)
  108. return key;
  109. }
  110. return ret;
  111. }else{
  112. *tp= *next; *next= NULL;
  113. if(*tp){
  114. (*tp)->elem= key;
  115. return NULL;
  116. }else
  117. return key;
  118. }
  119. }
  120. void av_tree_destroy(AVTreeNode *t){
  121. if(t){
  122. av_tree_destroy(t->child[0]);
  123. av_tree_destroy(t->child[1]);
  124. av_free(t);
  125. }
  126. }
  127. #if 0
  128. void av_tree_enumerate(AVTreeNode *t, void *opaque, int (*f)(void *opaque, void *elem)){
  129. int v= f(opaque, t->elem);
  130. if(v>=0) av_tree_enumerate(t->child[0], opaque, f);
  131. if(v<=0) av_tree_enumerate(t->child[1], opaque, f);
  132. }
  133. #endif
  134. #ifdef TEST
  135. #undef random
  136. static int check(AVTreeNode *t){
  137. if(t){
  138. int left= check(t->child[0]);
  139. int right= check(t->child[1]);
  140. if(left>999 || right>999)
  141. return 1000;
  142. if(right - left != t->state)
  143. return 1000;
  144. if(t->state>1 || t->state<-1)
  145. return 1000;
  146. return FFMAX(left, right)+1;
  147. }
  148. return 0;
  149. }
  150. static void print(AVTreeNode *t, int depth){
  151. int i;
  152. for(i=0; i<depth*4; i++) av_log(NULL, AV_LOG_ERROR, " ");
  153. if(t){
  154. av_log(NULL, AV_LOG_ERROR, "Node %p %2d %4d\n", t, t->state, t->elem);
  155. print(t->child[0], depth+1);
  156. print(t->child[1], depth+1);
  157. }else
  158. av_log(NULL, AV_LOG_ERROR, "NULL\n");
  159. }
  160. int cmp(const void *a, const void *b){
  161. return a-b;
  162. }
  163. int main(void){
  164. int i,k;
  165. AVTreeNode *root= NULL, *node=NULL;
  166. for(i=0; i<10000; i++){
  167. int j= (random()%86294);
  168. if(check(root) > 999){
  169. av_log(NULL, AV_LOG_ERROR, "FATAL error %d\n", i);
  170. print(root, 0);
  171. return -1;
  172. }
  173. av_log(NULL, AV_LOG_ERROR, "inserting %4d\n", j);
  174. if(!node)
  175. node= av_mallocz(av_tree_node_size);
  176. av_tree_insert(&root, (void*)(j+1), cmp, &node);
  177. j= (random()%86294);
  178. {
  179. AVTreeNode *node2=NULL;
  180. av_log(NULL, AV_LOG_ERROR, "removing %4d\n", j);
  181. av_tree_insert(&root, (void*)(j+1), cmp, &node2);
  182. k= av_tree_find(root, (void*)(j+1), cmp, NULL);
  183. if(k)
  184. av_log(NULL, AV_LOG_ERROR, "removal failure %d\n", i);
  185. }
  186. }
  187. return 0;
  188. }
  189. #endif