2
0

tree.c 5.5 KB

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