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.

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