tree.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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,
  29. int (*cmp)(void *key, const void *b), void *next[2])
  30. {
  31. if (t) {
  32. unsigned int v = cmp(key, t->elem);
  33. if (v) {
  34. if (next) next[v >> 31] = t->elem;
  35. return av_tree_find(t->child[(v >> 31) ^ 1], key, cmp, next);
  36. } else {
  37. if (next) {
  38. av_tree_find(t->child[0], key, cmp, next);
  39. av_tree_find(t->child[1], key, cmp, next);
  40. }
  41. return t->elem;
  42. }
  43. }
  44. return NULL;
  45. }
  46. void *av_tree_insert(AVTreeNode **tp, void *key,
  47. int (*cmp)(void *key, const void *b), AVTreeNode **next)
  48. {
  49. AVTreeNode *t = *tp;
  50. if (t) {
  51. unsigned int v = cmp(t->elem, key);
  52. void *ret;
  53. if (!v) {
  54. if (*next)
  55. return t->elem;
  56. else if (t->child[0] || t->child[1]) {
  57. int i = !t->child[0];
  58. void *next_elem[2];
  59. av_tree_find(t->child[i], key, cmp, next_elem);
  60. key = t->elem = next_elem[i];
  61. v = -i;
  62. } else {
  63. *next = t;
  64. *tp = NULL;
  65. return NULL;
  66. }
  67. }
  68. ret = av_tree_insert(&t->child[v >> 31], key, cmp, next);
  69. if (!ret) {
  70. int i = (v >> 31) ^ !!*next;
  71. AVTreeNode **child = &t->child[i];
  72. t->state += 2 * i - 1;
  73. if (!(t->state & 1)) {
  74. if (t->state) {
  75. /* The following code is equivalent to
  76. if((*child)->state*2 == -t->state)
  77. rotate(child, i^1);
  78. rotate(tp, i);
  79. with rotate():
  80. static void rotate(AVTreeNode **tp, int i) {
  81. AVTreeNode *t= *tp;
  82. *tp= t->child[i];
  83. t->child[i]= t->child[i]->child[i^1];
  84. (*tp)->child[i^1]= t;
  85. i= 4*t->state + 2*(*tp)->state + 12;
  86. t ->state= ((0x614586 >> i) & 3)-1;
  87. (*tp)->state= ((*tp)->state>>1) + ((0x400EEA >> i) & 3)-1;
  88. }
  89. but such a rotate function is both bigger and slower
  90. */
  91. if (( *child )->state * 2 == -t->state) {
  92. *tp = (*child)->child[i ^ 1];
  93. (*child)->child[i ^ 1] = (*tp)->child[i];
  94. (*tp)->child[i] = *child;
  95. *child = ( *tp )->child[i ^ 1];
  96. (*tp)->child[i ^ 1] = t;
  97. (*tp)->child[0]->state = -((*tp)->state > 0);
  98. (*tp)->child[1]->state = (*tp)->state < 0;
  99. (*tp)->state = 0;
  100. } else {
  101. *tp = *child;
  102. *child = (*child)->child[i ^ 1];
  103. (*tp)->child[i ^ 1] = t;
  104. if ((*tp)->state) t->state = 0;
  105. else t->state >>= 1;
  106. (*tp)->state = -t->state;
  107. }
  108. }
  109. }
  110. if (!(*tp)->state ^ !!*next)
  111. return key;
  112. }
  113. return ret;
  114. } else {
  115. *tp = *next;
  116. *next = NULL;
  117. if (*tp) {
  118. (*tp)->elem = key;
  119. return NULL;
  120. } else
  121. return key;
  122. }
  123. }
  124. void av_tree_destroy(AVTreeNode *t)
  125. {
  126. if (t) {
  127. av_tree_destroy(t->child[0]);
  128. av_tree_destroy(t->child[1]);
  129. av_free(t);
  130. }
  131. }
  132. void av_tree_enumerate(AVTreeNode *t, void *opaque,
  133. int (*cmp)(void *opaque, void *elem),
  134. int (*enu)(void *opaque, void *elem))
  135. {
  136. if (t) {
  137. int v = cmp ? cmp(opaque, t->elem) : 0;
  138. if (v >= 0)
  139. av_tree_enumerate(t->child[0], opaque, cmp, enu);
  140. if (v == 0)
  141. enu(opaque, t->elem);
  142. if (v <= 0)
  143. av_tree_enumerate(t->child[1], opaque, cmp, enu);
  144. }
  145. }
  146. #ifdef TEST
  147. #include "lfg.h"
  148. static int check(AVTreeNode *t)
  149. {
  150. if (t) {
  151. int left = check(t->child[0]);
  152. int right = check(t->child[1]);
  153. if (left>999 || right>999)
  154. return 1000;
  155. if (right - left != t->state)
  156. return 1000;
  157. if (t->state>1 || t->state<-1)
  158. return 1000;
  159. return FFMAX(left, right) + 1;
  160. }
  161. return 0;
  162. }
  163. static void print(AVTreeNode *t, int depth)
  164. {
  165. int i;
  166. for (i = 0; i < depth * 4; i++) av_log(NULL, AV_LOG_ERROR, " ");
  167. if (t) {
  168. av_log(NULL, AV_LOG_ERROR, "Node %p %2d %p\n", t, t->state, t->elem);
  169. print(t->child[0], depth + 1);
  170. print(t->child[1], depth + 1);
  171. } else
  172. av_log(NULL, AV_LOG_ERROR, "NULL\n");
  173. }
  174. static int cmp(void *a, const void *b)
  175. {
  176. return (uint8_t *) a - (const uint8_t *) b;
  177. }
  178. int main (void)
  179. {
  180. int i;
  181. void *k;
  182. AVTreeNode *root = NULL, *node = NULL;
  183. AVLFG prng;
  184. av_lfg_init(&prng, 1);
  185. for (i = 0; i < 10000; i++) {
  186. int j = av_lfg_get(&prng) % 86294;
  187. if (check(root) > 999) {
  188. av_log(NULL, AV_LOG_ERROR, "FATAL error %d\n", i);
  189. print(root, 0);
  190. return -1;
  191. }
  192. av_log(NULL, AV_LOG_ERROR, "inserting %4d\n", j);
  193. if (!node)
  194. node = av_mallocz(av_tree_node_size);
  195. av_tree_insert(&root, (void *) (j + 1), cmp, &node);
  196. j = av_lfg_get(&prng) % 86294;
  197. {
  198. AVTreeNode *node2 = NULL;
  199. av_log(NULL, AV_LOG_ERROR, "removing %4d\n", j);
  200. av_tree_insert(&root, (void *) (j + 1), cmp, &node2);
  201. k = av_tree_find(root, (void *) (j + 1), cmp, NULL);
  202. if (k)
  203. av_log(NULL, AV_LOG_ERROR, "removal failure %d\n", i);
  204. }
  205. }
  206. return 0;
  207. }
  208. #endif