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.

152 lines
4.3KB

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