tree.c 7.0 KB

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