tree.c 7.1 KB

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