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.

166 lines
5.4KB

  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 "error.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. struct AVTreeNode *av_tree_node_alloc(void)
  29. {
  30. return av_mallocz(sizeof(struct AVTreeNode));
  31. }
  32. void *av_tree_find(const AVTreeNode *t, void *key,
  33. int (*cmp)(void *key, const void *b), void *next[2])
  34. {
  35. if (t) {
  36. unsigned int v = cmp(key, t->elem);
  37. if (v) {
  38. if (next)
  39. 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. *
  85. * with rotate():
  86. * static void rotate(AVTreeNode **tp, int i)
  87. * {
  88. * AVTreeNode *t= *tp;
  89. *
  90. * *tp = t->child[i];
  91. * t->child[i] = t->child[i]->child[i ^ 1];
  92. * (*tp)->child[i ^ 1] = t;
  93. * i = 4 * t->state + 2 * (*tp)->state + 12;
  94. * t->state = ((0x614586 >> i) & 3) - 1;
  95. * (*tp)->state = ((0x400EEA >> i) & 3) - 1 +
  96. * ((*tp)->state >> 1);
  97. * }
  98. * but such a rotate function is both bigger and slower
  99. */
  100. if ((*child)->state * 2 == -t->state) {
  101. *tp = (*child)->child[i ^ 1];
  102. (*child)->child[i ^ 1] = (*tp)->child[i];
  103. (*tp)->child[i] = *child;
  104. *child = (*tp)->child[i ^ 1];
  105. (*tp)->child[i ^ 1] = t;
  106. (*tp)->child[0]->state = -((*tp)->state > 0);
  107. (*tp)->child[1]->state = (*tp)->state < 0;
  108. (*tp)->state = 0;
  109. } else {
  110. *tp = *child;
  111. *child = (*child)->child[i ^ 1];
  112. (*tp)->child[i ^ 1] = t;
  113. if ((*tp)->state)
  114. t->state = 0;
  115. else
  116. t->state >>= 1;
  117. (*tp)->state = -t->state;
  118. }
  119. }
  120. }
  121. if (!(*tp)->state ^ !!*next)
  122. return key;
  123. }
  124. return ret;
  125. } else {
  126. *tp = *next;
  127. *next = NULL;
  128. if (*tp) {
  129. (*tp)->elem = key;
  130. return NULL;
  131. } else
  132. return key;
  133. }
  134. }
  135. void av_tree_destroy(AVTreeNode *t)
  136. {
  137. if (t) {
  138. av_tree_destroy(t->child[0]);
  139. av_tree_destroy(t->child[1]);
  140. av_free(t);
  141. }
  142. }
  143. void av_tree_enumerate(AVTreeNode *t, void *opaque,
  144. int (*cmp)(void *opaque, void *elem),
  145. int (*enu)(void *opaque, void *elem))
  146. {
  147. if (t) {
  148. int v = cmp ? cmp(opaque, t->elem) : 0;
  149. if (v >= 0)
  150. av_tree_enumerate(t->child[0], opaque, cmp, enu);
  151. if (v == 0)
  152. enu(opaque, t->elem);
  153. if (v <= 0)
  154. av_tree_enumerate(t->child[1], opaque, cmp, enu);
  155. }
  156. }