tree.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. if((*child)->state*2 == -t->state){
  73. *tp= (*child)->child[i^1];
  74. (*child)->child[i^1]= (*tp)->child[i];
  75. (*tp)->child[i]= *child;
  76. *child= (*tp)->child[i^1];
  77. (*tp)->child[i^1]= t;
  78. (*tp)->child[0]->state= -((*tp)->state>0);
  79. (*tp)->child[1]->state= (*tp)->state<0 ;
  80. (*tp)->state=0;
  81. }else{
  82. *tp= *child;
  83. *child= (*child)->child[i^1];
  84. (*tp)->child[i^1]= t;
  85. if((*tp)->state) t->state = 0;
  86. else t->state>>= 1;
  87. (*tp)->state= -t->state;
  88. }
  89. }
  90. }
  91. if(!(*tp)->state ^ !!*next)
  92. return key;
  93. }
  94. return ret;
  95. }else{
  96. *tp= *next; *next= NULL;
  97. (*tp)->elem= key;
  98. return NULL;
  99. }
  100. }
  101. void av_tree_destroy(AVTreeNode *t){
  102. av_tree_destroy(t->child[0]);
  103. av_tree_destroy(t->child[1]);
  104. av_free(t);
  105. }
  106. #if 0
  107. void av_tree_enumerate(AVTreeNode *t, void *opaque, int (*f)(void *opaque, void *elem)){
  108. int v= f(opaque, t->elem);
  109. if(v>=0) av_tree_enumerate(t->child[0], opaque, f);
  110. if(v<=0) av_tree_enumerate(t->child[1], opaque, f);
  111. }
  112. #endif
  113. #ifdef TEST
  114. #undef random
  115. static int check(AVTreeNode *t){
  116. if(t){
  117. int left= check(t->child[0]);
  118. int right= check(t->child[1]);
  119. if(left>999 || right>999)
  120. return 1000;
  121. if(right - left != t->state)
  122. return 1000;
  123. if(t->state>1 || t->state<-1)
  124. return 1000;
  125. return FFMAX(left, right)+1;
  126. }
  127. return 0;
  128. }
  129. static void print(AVTreeNode *t, int depth){
  130. int i;
  131. for(i=0; i<depth*4; i++) av_log(NULL, AV_LOG_ERROR, " ");
  132. if(t){
  133. av_log(NULL, AV_LOG_ERROR, "Node %p %2d %4d\n", t, t->state, t->elem);
  134. print(t->child[0], depth+1);
  135. print(t->child[1], depth+1);
  136. }else
  137. av_log(NULL, AV_LOG_ERROR, "NULL\n");
  138. }
  139. int cmp(const void *a, const void *b){
  140. return a-b;
  141. }
  142. int main(void){
  143. int i,k;
  144. AVTreeNode *root= NULL, *node=NULL;
  145. for(i=0; i<10000; i++){
  146. int j= (random()%86294);
  147. if(check(root) > 999){
  148. av_log(NULL, AV_LOG_ERROR, "FATAL error %d\n", i);
  149. print(root, 0);
  150. return -1;
  151. }
  152. av_log(NULL, AV_LOG_ERROR, "inserting %4d\n", j);
  153. if(!node)
  154. node= av_mallocz(av_tree_node_size);
  155. av_tree_insert(&root, (void*)(j+1), cmp, &node);
  156. j= (random()%86294);
  157. k= av_tree_find(root, (void*)(j+1), cmp, NULL);
  158. if(k){
  159. AVTreeNode *node2=NULL;
  160. av_log(NULL, AV_LOG_ERROR, "removing %4d\n", j);
  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. }
  166. }
  167. return 0;
  168. }
  169. #endif