interval_tree.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*!
  2. Temelia - Interval tree interface.
  3. Copyright (C) 2008, 2009 Ceata (http://cod.ceata.org/proiecte/temelia).
  4. @author Dascalu Laurentiu
  5. This program is free software; you can redistribute it and
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 3
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. */
  17. #ifndef INTERVAL_TREE_H_
  18. #define INTERVAL_TREE_H_
  19. #include "platform.h"
  20. #include "common.h"
  21. struct _interval_tree_t;
  22. struct _interval_node_t;
  23. typedef struct _interval_node_t *interval_tree_node_t;
  24. typedef struct _interval_tree_t *interval_tree_t;
  25. /*!
  26. * @brief Builds an interval [inf, sup] containing key. Once you've build an
  27. * interval, you can't change it's content. If you want another interval,
  28. * delete this one and create another. An interval is defined by two integers and
  29. * not doubles because you may scale your values and cast them to integer, if
  30. * |X-integer(X)| <= EPS; another reason is the spaced wasted having double records.
  31. * Complexity O(1)
  32. *
  33. * @param Inf
  34. * @param Sup
  35. * @param Key
  36. */
  37. DECLSPEC interval_tree_node_t interval_tree_node_new(int inf, int sup, void *key);
  38. /*!
  39. * @brief Deletes node.
  40. * Complexity O(1)
  41. *
  42. * @param Interval node
  43. */
  44. DECLSPEC void interval_tree_node_delete(interval_tree_node_t interval);
  45. /*!
  46. * @brief Returns the low end of interval.
  47. * Complexity O(1)
  48. *
  49. * @param Interval node
  50. */
  51. DECLSPEC int interval_tree_node_get_inf(interval_tree_node_t interval);
  52. /*!
  53. * @brief Returns the high end of interval.
  54. * Complexity O(1)
  55. *
  56. * @param Interval node
  57. */
  58. DECLSPEC int interval_tree_node_get_sup(interval_tree_node_t interval);
  59. /*!
  60. * @brief Returns the key stored in this interval.
  61. * Complexity O(1)
  62. *
  63. * @param Interval node
  64. */
  65. DECLSPEC void *interval_tree_node_get_key(interval_tree_node_t interval);
  66. /*!
  67. * @brief Returns an authoritative interval tree on given input. If you want
  68. * to store intervals in nodes, like in a binary tree, use a red-black tree.
  69. * Complexity O(1)
  70. *
  71. * @param Inf
  72. * @param Sup
  73. */
  74. DECLSPEC interval_tree_t interval_tree_new(int inf, int sup);
  75. /*!
  76. * @brief Frees the memory occupied by this interval tree.
  77. * Complexity O(size)
  78. *
  79. * @param Interval tree
  80. */
  81. DECLSPEC void interval_tree_delete(interval_tree_t interval_tree);
  82. /*!
  83. * @brief Return 1 if the tree is empty, 0 if not and -1 if an error occurred.
  84. * Complexity O(log(size))
  85. *
  86. * @param Interval tree
  87. */
  88. DECLSPEC int interval_tree_is_empty(interval_tree_t interval_tree);
  89. /*!
  90. * @brief Returns the internal representation of data.
  91. * Complexity O(1)
  92. *
  93. * @param Interval tree
  94. */
  95. DECLSPEC interval_tree_node_t *interval_tree_get_data(interval_tree_t interval_tree);
  96. /*!
  97. * @brief Returns the size of interval tree.
  98. * Complexity O(1)
  99. *
  100. * @param Interval tree
  101. */
  102. DECLSPEC int interval_tree_get_size(interval_tree_t interval_tree);
  103. /*!
  104. * @brief Searches an interval in current interval tree. For-each interval in tree
  105. * overlapping this interval, the function calls the callback function with
  106. * the key associated in each node; key must be not NULL.
  107. * Complexity O(log(size))
  108. *
  109. * @param Interval tree
  110. * @param Inf
  111. * @param Sup
  112. * @param Key handling function
  113. * @param Context
  114. */
  115. DECLSPEC void interval_tree_search(interval_tree_t interval_tree, int inf, int sup,
  116. void key_handler(void *key, void *context), void *context);
  117. /*!
  118. * @brief Inserts in the interval tree a new interval with the associated key.
  119. * If the interval already exists, the key is adjusted.
  120. * Complexity O(log(size))
  121. *
  122. * @param Interval tree
  123. * @param Inf
  124. * @param Sup
  125. * @param Key
  126. */
  127. DECLSPEC void interval_tree_insert(interval_tree_t interval_tree, int inf, int sup,
  128. void *key);
  129. /*!
  130. * @brief Removes key stored for an interval. It calls interval_tree_insert with
  131. * NULL key.
  132. * @param Interval tree
  133. * @param Inf
  134. * @param Sup
  135. * @see interval_tree_insert
  136. */
  137. DECLSPEC void interval_tree_remove(interval_tree_t interval_tree, int inf, int sup);
  138. /*!
  139. * @brief Returns the node at a given index; root is at 0.
  140. * Complexity O(1)
  141. *
  142. * @param Interval tree
  143. * @param Index
  144. */
  145. DECLSPEC interval_tree_node_t interval_tree_get_node(interval_tree_t interval_tree,
  146. int index);
  147. /*!
  148. * @see binary_tree_preorder
  149. */
  150. DECLSPEC void interval_tree_preorder(interval_tree_t interval, void key_handler(void *x,
  151. void *context), void *context);
  152. /*!
  153. * @see binary_tree_inorder
  154. */
  155. DECLSPEC void interval_tree_inorder(interval_tree_t interval, void key_handler(void *x,
  156. void *context), void *context);
  157. /*!
  158. * @see binary_tree_reverse_inorder
  159. */
  160. DECLSPEC void interval_tree_reverse_inorder(interval_tree_t interval, void key_handler(
  161. void *x, void *context), void *context);
  162. /*!
  163. * @see binary_tree_postorder
  164. */
  165. DECLSPEC void interval_tree_postorder(interval_tree_t interval, void key_handler(
  166. void *x, void *context), void *context);
  167. /*!
  168. * @see binary_tree_level_order
  169. */
  170. DECLSPEC void interval_tree_level_order(interval_tree_t interval, void key_handler(
  171. void *x, void *context), void *context);
  172. /*!
  173. * @see binary_tree_show_indented
  174. */
  175. DECLSPEC void interval_tree_show_indented(interval_tree_t interval, void key_handler(
  176. void *x, int level, void *context), void *context);
  177. #endif /* INTERVAL_TREE_H_ */