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.

239 lines
7.1KB

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