aRts audio server
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.

293 lines
9.7KB

  1. /* GBSearchArray - Binary Searchable Array implementation
  2. * Copyright (C) 2000-2002 Tim Janik
  3. *
  4. * This software is provided "as is"; redistribution and modification
  5. * is permitted, provided that the following disclaimer is retained.
  6. *
  7. * This software is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. * In no event shall the authors or contributors be liable for any
  11. * direct, indirect, incidental, special, exemplary, or consequential
  12. * damages (including, but not limited to, procurement of substitute
  13. * goods or services; loss of use, data, or profits; or business
  14. * interruption) however caused and on any theory of liability, whether
  15. * in contract, strict liability, or tort (including negligence or
  16. * otherwise) arising in any way out of the use of this software, even
  17. * if advised of the possibility of such damage.
  18. */
  19. #ifndef __G_BSEARCH_ARRAY_H__
  20. #define __G_BSEARCH_ARRAY_H__
  21. #include <gsl/gsldefs.h>
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif /* __cplusplus */
  25. /* this implementation is intended to be usable in third-party code
  26. * simply by pasting the contents of this file. as such, the
  27. * implementation needs to be self-contained within this file.
  28. */
  29. /* convenience macro to avoid signed overflow for value comparisons */
  30. #define G_BSEARCH_ARRAY_CMP(v1,v2) ((v1) > (v2) ? +1 : (v1) == (v2) ? 0 : -1)
  31. /* --- typedefs --- */
  32. typedef gint (*GBSearchCompareFunc) (gconstpointer bsearch_node1, /* key */
  33. gconstpointer bsearch_node2);
  34. typedef enum
  35. {
  36. G_BSEARCH_ARRAY_ALIGN_POWER2 = 1 << 0, /* memory is power2 aligned */
  37. G_BSEARCH_ARRAY_FORCE_SHRINK = 1 << 1 /* removal shrinks with realloc() */
  38. } GBSearchArrayFlags;
  39. /* --- structures --- */
  40. typedef struct
  41. {
  42. guint sizeof_node;
  43. GBSearchCompareFunc cmp_nodes;
  44. guint flags;
  45. } GBSearchConfig;
  46. typedef union
  47. {
  48. guint n_nodes;
  49. /*< private >*/
  50. gpointer alignment_dummy1;
  51. glong alignment_dummy2;
  52. gdouble alignment_dummy3;
  53. } GBSearchArray;
  54. /* --- public API --- */
  55. static inline GBSearchArray* g_bsearch_array_create (const GBSearchConfig *bconfig);
  56. static inline gpointer g_bsearch_array_get_nth (GBSearchArray *barray,
  57. const GBSearchConfig *bconfig,
  58. guint nth);
  59. static inline guint g_bsearch_array_get_index (GBSearchArray *barray,
  60. const GBSearchConfig *bconfig,
  61. gconstpointer node_in_array);
  62. static inline GBSearchArray* g_bsearch_array_remove (GBSearchArray *barray,
  63. const GBSearchConfig *bconfig,
  64. guint index);
  65. /* preserve uninitialized space at index for node insertion
  66. */
  67. static inline GBSearchArray* g_bsearch_array_grow (GBSearchArray *barray,
  68. const GBSearchConfig *bconfig,
  69. guint index);
  70. /* insert key_node into array, or if it exists, replace
  71. * the existing node's contents with key_node
  72. */
  73. static inline GBSearchArray* g_bsearch_array_insert (GBSearchArray *barray,
  74. const GBSearchConfig *bconfig,
  75. gconstpointer key_node);
  76. static inline void g_bsearch_array_free (GBSearchArray *barray,
  77. const GBSearchConfig *bconfig);
  78. #define g_bsearch_array_get_n_nodes(barray) (((GBSearchArray*) (barray))->n_nodes)
  79. /* g_bsearch_array_lookup():
  80. * return NULL or exact match node
  81. */
  82. #define g_bsearch_array_lookup(barray, bconfig, key_node) \
  83. g_bsearch_array_lookup_fuzzy ((barray), (bconfig), (key_node), 0)
  84. /* g_bsearch_array_lookup_sibling():
  85. * return NULL for barray->n_nodes==0, otherwise return the
  86. * exact match node, or, if there's no such node, return the
  87. * node last visited, which is pretty close to an exact match
  88. * (will be one off into either direction).
  89. */
  90. #define g_bsearch_array_lookup_sibling(barray, bconfig, key_node) \
  91. g_bsearch_array_lookup_fuzzy ((barray), (bconfig), (key_node), 1)
  92. /* g_bsearch_array_lookup_insertion():
  93. * return NULL for barray->n_nodes==0 or exact match, otherwise
  94. * return the node where key_node should be inserted (may be one
  95. * after end, i.e. g_bsearch_array_get_index(result) <= barray->n_nodes).
  96. */
  97. #define g_bsearch_array_lookup_insertion(barray, bconfig, key_node) \
  98. g_bsearch_array_lookup_fuzzy ((barray), (bconfig), (key_node), 2)
  99. /* --- implementation --- */
  100. /* helper macro to cut down realloc()s */
  101. #ifdef DISABLE_MEM_POOLS
  102. #define G_BSEARCH_UPPER_POWER2(n) (n)
  103. #else /* !DISABLE_MEM_POOLS */
  104. #define G_BSEARCH_UPPER_POWER2(n) ((n) ? 1 << g_bit_storage ((n) - 1) : 0)
  105. #endif /* !DISABLE_MEM_POOLS */
  106. #define G_BSEARCH_ARRAY_NODES(barray) (((guint8*) (barray)) + sizeof (GBSearchArray))
  107. static inline GBSearchArray*
  108. g_bsearch_array_create (const GBSearchConfig *bconfig)
  109. {
  110. GBSearchArray *barray;
  111. guint size;
  112. g_return_val_if_fail (bconfig != NULL, NULL);
  113. size = sizeof (GBSearchArray) + bconfig->sizeof_node;
  114. if (bconfig->flags & G_BSEARCH_ARRAY_ALIGN_POWER2)
  115. size = G_BSEARCH_UPPER_POWER2 (size);
  116. barray = g_realloc (NULL, size);
  117. memset (barray, 0, sizeof (GBSearchArray));
  118. return barray;
  119. }
  120. static inline gpointer
  121. g_bsearch_array_lookup_fuzzy (GBSearchArray *barray,
  122. const GBSearchConfig *bconfig,
  123. gconstpointer key_node,
  124. const guint sibling_or_after);
  125. static inline gpointer
  126. g_bsearch_array_lookup_fuzzy (GBSearchArray *barray,
  127. const GBSearchConfig *bconfig,
  128. gconstpointer key_node,
  129. const guint sibling_or_after)
  130. {
  131. GBSearchCompareFunc cmp_nodes = bconfig->cmp_nodes;
  132. guint8 *check = NULL, *nodes = G_BSEARCH_ARRAY_NODES (barray);
  133. guint n_nodes = barray->n_nodes, offs = 0;
  134. guint sizeof_node = bconfig->sizeof_node;
  135. gint cmp = 0;
  136. while (offs < n_nodes)
  137. {
  138. guint i = (offs + n_nodes) >> 1;
  139. check = nodes + i * sizeof_node;
  140. cmp = cmp_nodes (key_node, check);
  141. if (cmp == 0)
  142. return sibling_or_after > 1 ? NULL : check;
  143. else if (cmp < 0)
  144. n_nodes = i;
  145. else /* (cmp > 0) */
  146. offs = i + 1;
  147. }
  148. /* check is last mismatch, cmp > 0 indicates greater key */
  149. return !sibling_or_after ? NULL : (sibling_or_after > 1 && cmp > 0) ? check + sizeof_node : check;
  150. }
  151. static inline gpointer
  152. g_bsearch_array_get_nth (GBSearchArray *barray,
  153. const GBSearchConfig *bconfig,
  154. guint nth)
  155. {
  156. return (nth < barray->n_nodes ?
  157. G_BSEARCH_ARRAY_NODES (barray) + nth * bconfig->sizeof_node :
  158. NULL);
  159. }
  160. static inline guint
  161. g_bsearch_array_get_index (GBSearchArray *barray,
  162. const GBSearchConfig *bconfig,
  163. gconstpointer node_in_array)
  164. {
  165. guint distance = ((guint8*) node_in_array) - G_BSEARCH_ARRAY_NODES (barray);
  166. g_return_val_if_fail (node_in_array != NULL, barray->n_nodes);
  167. distance /= bconfig->sizeof_node;
  168. return MIN (distance, barray->n_nodes + 1); /* may return one after end */
  169. }
  170. static inline GBSearchArray*
  171. g_bsearch_array_grow (GBSearchArray *barray,
  172. const GBSearchConfig *bconfig,
  173. guint index)
  174. {
  175. guint old_size = barray->n_nodes * bconfig->sizeof_node;
  176. guint new_size = old_size + bconfig->sizeof_node;
  177. guint8 *node;
  178. g_return_val_if_fail (index <= barray->n_nodes, NULL);
  179. if (bconfig->flags & G_BSEARCH_ARRAY_ALIGN_POWER2)
  180. {
  181. new_size = G_BSEARCH_UPPER_POWER2 (sizeof (GBSearchArray) + new_size);
  182. old_size = G_BSEARCH_UPPER_POWER2 (sizeof (GBSearchArray) + old_size);
  183. if (old_size != new_size)
  184. barray = g_realloc (barray, new_size);
  185. }
  186. else
  187. barray = g_realloc (barray, sizeof (GBSearchArray) + new_size);
  188. node = G_BSEARCH_ARRAY_NODES (barray) + index * bconfig->sizeof_node;
  189. g_memmove (node + bconfig->sizeof_node, node, (barray->n_nodes - index) * bconfig->sizeof_node);
  190. barray->n_nodes += 1;
  191. return barray;
  192. }
  193. static inline GBSearchArray*
  194. g_bsearch_array_insert (GBSearchArray *barray,
  195. const GBSearchConfig *bconfig,
  196. gconstpointer key_node)
  197. {
  198. guint8 *node;
  199. if (!barray->n_nodes)
  200. {
  201. barray = g_bsearch_array_grow (barray, bconfig, 0);
  202. node = G_BSEARCH_ARRAY_NODES (barray);
  203. }
  204. else
  205. {
  206. node = g_bsearch_array_lookup_insertion (barray, bconfig, key_node);
  207. if (node)
  208. {
  209. guint index = g_bsearch_array_get_index (barray, bconfig, node);
  210. /* grow and insert */
  211. barray = g_bsearch_array_grow (barray, bconfig, index);
  212. node = G_BSEARCH_ARRAY_NODES (barray) + index * bconfig->sizeof_node;
  213. }
  214. else /* replace (relookup is ok, replace is comparatively cheap anyways) */
  215. node = g_bsearch_array_lookup (barray, bconfig, key_node);
  216. }
  217. memcpy (node, key_node, bconfig->sizeof_node);
  218. return barray;
  219. }
  220. static inline GBSearchArray*
  221. g_bsearch_array_remove (GBSearchArray *barray,
  222. const GBSearchConfig *bconfig,
  223. guint index)
  224. {
  225. guint8 *node;
  226. g_return_val_if_fail (index < barray->n_nodes, NULL);
  227. barray->n_nodes -= 1;
  228. node = G_BSEARCH_ARRAY_NODES (barray) + index * bconfig->sizeof_node;
  229. g_memmove (node, node + bconfig->sizeof_node, (barray->n_nodes - index) * bconfig->sizeof_node);
  230. if (bconfig->flags & G_BSEARCH_ARRAY_FORCE_SHRINK)
  231. {
  232. guint new_size = barray->n_nodes * bconfig->sizeof_node;
  233. guint old_size = new_size + bconfig->sizeof_node;
  234. if (bconfig->flags & G_BSEARCH_ARRAY_ALIGN_POWER2)
  235. {
  236. new_size = G_BSEARCH_UPPER_POWER2 (sizeof (GBSearchArray) + new_size);
  237. old_size = G_BSEARCH_UPPER_POWER2 (sizeof (GBSearchArray) + old_size);
  238. if (old_size != new_size)
  239. barray = g_realloc (barray, new_size);
  240. }
  241. else
  242. barray = g_realloc (barray, sizeof (GBSearchArray) + new_size);
  243. }
  244. return barray;
  245. }
  246. static inline void
  247. g_bsearch_array_free (GBSearchArray *barray,
  248. const GBSearchConfig *bconfig)
  249. {
  250. g_return_if_fail (barray != NULL);
  251. g_free (barray);
  252. }
  253. #ifdef __cplusplus
  254. }
  255. #endif /* __cplusplus */
  256. #endif /* !__G_BSEARCH_ARRAY_H__ */