You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
4.5KB

  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(t->elem, key);
  32. if(v){
  33. if(next) next[(v>>31)^1]= t->elem;
  34. return av_tree_find(t->child[v>>31], 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. if(v){
  50. int i= v>>31;
  51. AVTreeNode **child= &t->child[i];
  52. void *ret= av_tree_insert(child, key, cmp, next);
  53. if(!ret){
  54. t->state -= ((int)v>>31)|1;
  55. if(!(t->state&1)){
  56. if(t->state){
  57. if((*child)->state*2 == t->state){
  58. *tp= *child;
  59. *child= (*child)->child[i^1];
  60. (*tp)->child[i^1]= t;
  61. t->state= 0;
  62. }else{
  63. *tp= (*child)->child[i^1];
  64. (*child)->child[i^1]= (*tp)->child[i];
  65. (*tp)->child[i]= *child;
  66. *child= (*tp)->child[i^1];
  67. (*tp)->child[i^1]= t;
  68. i= (*tp)->state > 0;
  69. (*tp)->child[i ]->state= 0;
  70. (*tp)->child[i^1]->state= -(*tp)->state;
  71. }
  72. (*tp)->state=0;
  73. }
  74. return key;
  75. }
  76. }
  77. return ret;
  78. }else{
  79. return t->elem;
  80. }
  81. }else{
  82. *tp= *next; *next= NULL;
  83. (*tp)->elem= key;
  84. return NULL;
  85. }
  86. }
  87. void av_tree_destroy(AVTreeNode *t){
  88. av_tree_destroy(t->child[0]);
  89. av_tree_destroy(t->child[1]);
  90. av_free(t);
  91. }
  92. #if 0
  93. void av_tree_enumerate(AVTreeNode *t, void *opaque, int (*f)(void *opaque, void *elem)){
  94. int v= f(opaque, t->elem);
  95. if(v>=0) av_tree_enumerate(t->child[0], opaque, f);
  96. if(v<=0) av_tree_enumerate(t->child[1], opaque, f);
  97. }
  98. #endif
  99. #ifdef TEST
  100. static int check(AVTreeNode *t){
  101. if(t){
  102. int left= check(t->child[0]);
  103. int right= check(t->child[1]);
  104. if(left>999 || right>999)
  105. return 1000;
  106. if(right - left != t->state)
  107. return 1000;
  108. if(t->state>1 || t->state<-1)
  109. return 1000;
  110. return FFMAX(left, right)+1;
  111. }
  112. return 0;
  113. }
  114. static void print(AVTreeNode *t, int depth){
  115. int i;
  116. for(i=0; i<depth*4; i++) av_log(NULL, AV_LOG_ERROR, " ");
  117. if(t){
  118. av_log(NULL, AV_LOG_ERROR, "Node %p %2d %4d\n", t, t->state, t->elem);
  119. print(t->child[0], depth+1);
  120. print(t->child[1], depth+1);
  121. }else
  122. av_log(NULL, AV_LOG_ERROR, "NULL\n");
  123. }
  124. int cmp(const void *a, const void *b){
  125. return a-b;
  126. }
  127. int main(void){
  128. int i,j,k;
  129. AVTreeNode *root= NULL;
  130. for(i=0; i<10000; i++){
  131. int j= (random()%863294);
  132. if(check(root) > 999){
  133. av_log(NULL, AV_LOG_ERROR, "FATAL error %d\n", i);
  134. print(root, 0);
  135. return -1;
  136. }
  137. av_log(NULL, AV_LOG_ERROR, "inserting %4d\n", j);
  138. av_tree_insert(&root, (void*)(j+1), cmp);
  139. }
  140. return 0;
  141. }
  142. #endif