tree.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 "log.h"
  21. #include "tree.h"
  22. typedef struct AVTreeNode{
  23. struct AVTreeNode *child[2];
  24. void *elem;
  25. int state;
  26. }AVTreeNode;
  27. const int av_tree_node_size = sizeof(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(key, t->elem);
  31. if(v){
  32. if(next) next[v>>31]= t->elem;
  33. return av_tree_find(t->child[(v>>31)^1], 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), AVTreeNode **next){
  45. AVTreeNode *t= *tp;
  46. if(t){
  47. unsigned int v= cmp(t->elem, key);
  48. void *ret;
  49. if(!v){
  50. if(*next)
  51. return t->elem;
  52. else if(t->child[0]||t->child[1]){
  53. int i= !t->child[0];
  54. void *next_elem[2];
  55. av_tree_find(t->child[i], key, cmp, next_elem);
  56. key= t->elem= next_elem[i];
  57. v= -i;
  58. }else{
  59. *next= t;
  60. *tp=NULL;
  61. return NULL;
  62. }
  63. }
  64. ret= av_tree_insert(&t->child[v>>31], key, cmp, next);
  65. if(!ret){
  66. int i= (v>>31) ^ !!*next;
  67. AVTreeNode **child= &t->child[i];
  68. t->state += 2*i - 1;
  69. if(!(t->state&1)){
  70. if(t->state){
  71. /* The following code is equivalent to
  72. if((*child)->state*2 == -t->state)
  73. rotate(child, i^1);
  74. rotate(tp, i);
  75. with rotate():
  76. static void rotate(AVTreeNode **tp, int i){
  77. AVTreeNode *t= *tp;
  78. *tp= t->child[i];
  79. t->child[i]= t->child[i]->child[i^1];
  80. (*tp)->child[i^1]= t;
  81. i= 4*t->state + 2*(*tp)->state + 12;
  82. t ->state= ((0x614586 >> i) & 3)-1;
  83. (*tp)->state= ((*tp)->state>>1) + ((0x400EEA >> i) & 3)-1;
  84. }
  85. but such a rotate function is both bigger and slower
  86. */
  87. if((*child)->state*2 == -t->state){
  88. *tp= (*child)->child[i^1];
  89. (*child)->child[i^1]= (*tp)->child[i];
  90. (*tp)->child[i]= *child;
  91. *child= (*tp)->child[i^1];
  92. (*tp)->child[i^1]= t;
  93. (*tp)->child[0]->state= -((*tp)->state>0);
  94. (*tp)->child[1]->state= (*tp)->state<0 ;
  95. (*tp)->state=0;
  96. }else{
  97. *tp= *child;
  98. *child= (*child)->child[i^1];
  99. (*tp)->child[i^1]= t;
  100. if((*tp)->state) t->state = 0;
  101. else t->state>>= 1;
  102. (*tp)->state= -t->state;
  103. }
  104. }
  105. }
  106. if(!(*tp)->state ^ !!*next)
  107. return key;
  108. }
  109. return ret;
  110. }else{
  111. *tp= *next; *next= NULL;
  112. if(*tp){
  113. (*tp)->elem= key;
  114. return NULL;
  115. }else
  116. return key;
  117. }
  118. }
  119. void av_tree_destroy(AVTreeNode *t){
  120. if(t){
  121. av_tree_destroy(t->child[0]);
  122. av_tree_destroy(t->child[1]);
  123. av_free(t);
  124. }
  125. }
  126. void av_tree_enumerate(AVTreeNode *t, void *opaque, int (*cmp)(void *opaque, void *elem), int (*enu)(void *opaque, void *elem)){
  127. if(t){
  128. int v= cmp ? cmp(opaque, t->elem) : 0;
  129. if(v>=0) av_tree_enumerate(t->child[0], opaque, cmp, enu);
  130. if(v==0) enu(opaque, t->elem);
  131. if(v<=0) av_tree_enumerate(t->child[1], opaque, cmp, enu);
  132. }
  133. }
  134. #ifdef TEST
  135. #include "lfg.h"
  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 %p\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. static int cmp(void *a, const void *b){
  161. return (uint8_t*)a-(const uint8_t*)b;
  162. }
  163. int main(void){
  164. int i;
  165. void *k;
  166. AVTreeNode *root= NULL, *node=NULL;
  167. AVLFG prng;
  168. av_lfg_init(&prng, 1);
  169. for(i=0; i<10000; i++){
  170. int j = av_lfg_get(&prng) % 86294;
  171. if(check(root) > 999){
  172. av_log(NULL, AV_LOG_ERROR, "FATAL error %d\n", i);
  173. print(root, 0);
  174. return -1;
  175. }
  176. av_log(NULL, AV_LOG_ERROR, "inserting %4d\n", j);
  177. if(!node)
  178. node= av_mallocz(av_tree_node_size);
  179. av_tree_insert(&root, (void*)(j+1), cmp, &node);
  180. j = av_lfg_get(&prng) % 86294;
  181. {
  182. AVTreeNode *node2=NULL;
  183. av_log(NULL, AV_LOG_ERROR, "removing %4d\n", j);
  184. av_tree_insert(&root, (void*)(j+1), cmp, &node2);
  185. k= av_tree_find(root, (void*)(j+1), cmp, NULL);
  186. if(k)
  187. av_log(NULL, AV_LOG_ERROR, "removal failure %d\n", i);
  188. }
  189. }
  190. return 0;
  191. }
  192. #endif