gl_oset.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* Abstract ordered set data type.
  2. Copyright (C) 2006-2007, 2009-2020 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2006.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. #ifndef _GL_OSET_H
  15. #define _GL_OSET_H
  16. #include <stdbool.h>
  17. #include <stddef.h>
  18. #ifndef _GL_INLINE_HEADER_BEGIN
  19. #error "Please include config.h first."
  20. #endif
  21. _GL_INLINE_HEADER_BEGIN
  22. #ifndef GL_OSET_INLINE
  23. # define GL_OSET_INLINE _GL_INLINE
  24. #endif
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /* gl_oset is an abstract ordered set data type. It can contain any number
  29. of objects ('void *' or 'const void *' pointers) in the order of a given
  30. comparator function. Duplicates (in the sense of the comparator) are
  31. forbidden.
  32. There are several implementations of this ordered set datatype, optimized
  33. for different operations or for memory. You can start using the simplest
  34. ordered set implementation, GL_ARRAY_OSET, and switch to a different
  35. implementation later, when you realize which operations are performed
  36. the most frequently. The API of the different implementations is exactly
  37. the same; when switching to a different implementation, you only have to
  38. change the gl_oset_create call.
  39. The implementations are:
  40. GL_ARRAY_OSET a growable array
  41. GL_AVLTREE_OSET a binary tree (AVL tree)
  42. GL_RBTREE_OSET a binary tree (red-black tree)
  43. The memory consumption is asymptotically the same: O(1) for every object
  44. in the set. When looking more closely at the average memory consumed
  45. for an object, GL_ARRAY_OSET is the most compact representation, and
  46. GL_AVLTREE_OSET, GL_RBTREE_OSET need more memory.
  47. The guaranteed average performance of the operations is, for a set of
  48. n elements:
  49. Operation ARRAY TREE
  50. gl_oset_size O(1) O(1)
  51. gl_oset_add O(n) O(log n)
  52. gl_oset_remove O(n) O(log n)
  53. gl_oset_update O(n) O(log n)
  54. gl_oset_search O(log n) O(log n)
  55. gl_oset_search_atleast O(log n) O(log n)
  56. gl_oset_iterator O(1) O(log n)
  57. gl_oset_iterator_atleast O(log n) O(log n)
  58. gl_oset_iterator_next O(1) O(log n)
  59. */
  60. /* -------------------------- gl_oset_t Data Type -------------------------- */
  61. /* Type of function used to compare two elements. Same as for qsort().
  62. NULL denotes pointer comparison. */
  63. typedef int (*gl_setelement_compar_fn) (const void *elt1, const void *elt2);
  64. #ifndef _GL_SETELEMENT_DISPOSE_FN_DEFINED
  65. /* Type of function used to dispose an element once it's removed from a set.
  66. NULL denotes a no-op. */
  67. typedef void (*gl_setelement_dispose_fn) (const void *elt);
  68. # define _GL_SETELEMENT_DISPOSE_FN_DEFINED 1
  69. #endif
  70. /* Type of function used to compare an element with a threshold.
  71. Returns true if the element is greater or equal than the threshold. */
  72. typedef bool (*gl_setelement_threshold_fn) (const void *elt, const void *threshold);
  73. struct gl_oset_impl;
  74. /* Type representing an entire ordered set. */
  75. typedef struct gl_oset_impl * gl_oset_t;
  76. struct gl_oset_implementation;
  77. /* Type representing a ordered set datatype implementation. */
  78. typedef const struct gl_oset_implementation * gl_oset_implementation_t;
  79. #if 0 /* Unless otherwise specified, these are defined inline below. */
  80. /* Creates an empty set.
  81. IMPLEMENTATION is one of GL_ARRAY_OSET, GL_AVLTREE_OSET, GL_RBTREE_OSET.
  82. COMPAR_FN is an element comparison function or NULL.
  83. DISPOSE_FN is an element disposal function or NULL. */
  84. /* declared in gl_xoset.h */
  85. extern gl_oset_t gl_oset_create_empty (gl_oset_implementation_t implementation,
  86. gl_setelement_compar_fn compar_fn,
  87. gl_setelement_dispose_fn dispose_fn);
  88. /* Likewise. Returns NULL upon out-of-memory. */
  89. extern gl_oset_t gl_oset_nx_create_empty (gl_oset_implementation_t implementation,
  90. gl_setelement_compar_fn compar_fn,
  91. gl_setelement_dispose_fn dispose_fn);
  92. /* Returns the current number of elements in an ordered set. */
  93. extern size_t gl_oset_size (gl_oset_t set);
  94. /* Searches whether an element is already in the ordered set.
  95. Returns true if found, or false if not present in the set. */
  96. extern bool gl_oset_search (gl_oset_t set, const void *elt);
  97. /* Searches the least element in the ordered set that compares greater or equal
  98. to the given THRESHOLD. The representation of the THRESHOLD is defined
  99. by the THRESHOLD_FN.
  100. Returns true and stores the found element in *ELTP if found, otherwise returns
  101. false. */
  102. extern bool gl_oset_search_atleast (gl_oset_t set,
  103. gl_setelement_threshold_fn threshold_fn,
  104. const void *threshold,
  105. const void **eltp);
  106. /* Adds an element to an ordered set.
  107. Returns true if it was not already in the set and added, false otherwise. */
  108. /* declared in gl_xoset.h */
  109. extern bool gl_oset_add (gl_oset_t set, const void *elt);
  110. /* Likewise. Returns -1 upon out-of-memory. */
  111. extern int gl_oset_nx_add (gl_oset_t set, const void *elt)
  112. _GL_ATTRIBUTE_NODISCARD;
  113. /* Removes an element from an ordered set.
  114. Returns true if it was found and removed. */
  115. extern bool gl_oset_remove (gl_oset_t set, const void *elt);
  116. /* Invokes ACTION (ELT, ACTION_DATA) and updates the given ordered set if,
  117. during this invocation, the attributes/properties of the element ELT change
  118. in a way that influences the comparison function.
  119. Warning: During the invocation of ACTION, the ordered set is inconsistent
  120. and must not be accessed!
  121. Returns 1 if the position of the element in the ordered set has changed as
  122. a consequence, 0 if the element stayed at the same position, or -1 if it
  123. collided with another element and was therefore removed. */
  124. extern int gl_oset_update (gl_oset_t set, const void *elt,
  125. void (*action) (const void *elt, void *action_data),
  126. void *action_data);
  127. /* Frees an entire ordered set.
  128. (But this call does not free the elements of the set. It only invokes
  129. the DISPOSE_FN on each of the elements of the set.) */
  130. extern void gl_oset_free (gl_oset_t set);
  131. #endif /* End of inline and gl_xoset.h-defined functions. */
  132. /* --------------------- gl_oset_iterator_t Data Type --------------------- */
  133. /* Functions for iterating through an ordered set. */
  134. /* Type of an iterator that traverses an ordered set.
  135. This is a fixed-size struct, so that creation of an iterator doesn't need
  136. memory allocation on the heap. */
  137. typedef struct
  138. {
  139. /* For fast dispatch of gl_oset_iterator_next. */
  140. const struct gl_oset_implementation *vtable;
  141. /* For detecting whether the last returned element was removed. */
  142. gl_oset_t set;
  143. size_t count;
  144. /* Other, implementation-private fields. */
  145. void *p; void *q;
  146. size_t i; size_t j;
  147. } gl_oset_iterator_t;
  148. #if 0 /* These are defined inline below. */
  149. /* Creates an iterator traversing an ordered set.
  150. The set's contents must not be modified while the iterator is in use,
  151. except for removing the last returned element. */
  152. extern gl_oset_iterator_t gl_oset_iterator (gl_oset_t set);
  153. /* Creates an iterator traversing the tail of an ordered set, that comprises
  154. the elements that compare greater or equal to the given THRESHOLD. The
  155. representation of the THRESHOLD is defined by the THRESHOLD_FN. */
  156. extern gl_oset_iterator_t gl_oset_iterator_atleast (gl_oset_t set,
  157. gl_setelement_threshold_fn threshold_fn,
  158. const void *threshold);
  159. /* If there is a next element, stores the next element in *ELTP, advances the
  160. iterator and returns true. Otherwise, returns false. */
  161. extern bool gl_oset_iterator_next (gl_oset_iterator_t *iterator,
  162. const void **eltp);
  163. /* Frees an iterator. */
  164. extern void gl_oset_iterator_free (gl_oset_iterator_t *iterator);
  165. #endif /* End of inline functions. */
  166. /* ------------------------ Implementation Details ------------------------ */
  167. struct gl_oset_implementation
  168. {
  169. /* gl_oset_t functions. */
  170. gl_oset_t (*nx_create_empty) (gl_oset_implementation_t implementation,
  171. gl_setelement_compar_fn compar_fn,
  172. gl_setelement_dispose_fn dispose_fn);
  173. size_t (*size) (gl_oset_t set);
  174. bool (*search) (gl_oset_t set, const void *elt);
  175. bool (*search_atleast) (gl_oset_t set,
  176. gl_setelement_threshold_fn threshold_fn,
  177. const void *threshold, const void **eltp);
  178. int (*nx_add) (gl_oset_t set, const void *elt);
  179. bool (*remove_elt) (gl_oset_t set, const void *elt);
  180. int (*update) (gl_oset_t set, const void *elt,
  181. void (*action) (const void * /*elt*/, void * /*action_data*/),
  182. void *action_data);
  183. void (*oset_free) (gl_oset_t set);
  184. /* gl_oset_iterator_t functions. */
  185. gl_oset_iterator_t (*iterator) (gl_oset_t set);
  186. gl_oset_iterator_t (*iterator_atleast) (gl_oset_t set,
  187. gl_setelement_threshold_fn threshold_fn,
  188. const void *threshold);
  189. bool (*iterator_next) (gl_oset_iterator_t *iterator, const void **eltp);
  190. void (*iterator_free) (gl_oset_iterator_t *iterator);
  191. };
  192. struct gl_oset_impl_base
  193. {
  194. const struct gl_oset_implementation *vtable;
  195. gl_setelement_compar_fn compar_fn;
  196. gl_setelement_dispose_fn dispose_fn;
  197. };
  198. /* Define all functions of this file as accesses to the
  199. struct gl_oset_implementation. */
  200. GL_OSET_INLINE gl_oset_t
  201. gl_oset_nx_create_empty (gl_oset_implementation_t implementation,
  202. gl_setelement_compar_fn compar_fn,
  203. gl_setelement_dispose_fn dispose_fn)
  204. {
  205. return implementation->nx_create_empty (implementation, compar_fn,
  206. dispose_fn);
  207. }
  208. GL_OSET_INLINE size_t
  209. gl_oset_size (gl_oset_t set)
  210. {
  211. return ((const struct gl_oset_impl_base *) set)->vtable->size (set);
  212. }
  213. GL_OSET_INLINE bool
  214. gl_oset_search (gl_oset_t set, const void *elt)
  215. {
  216. return ((const struct gl_oset_impl_base *) set)->vtable->search (set, elt);
  217. }
  218. GL_OSET_INLINE bool
  219. gl_oset_search_atleast (gl_oset_t set,
  220. gl_setelement_threshold_fn threshold_fn,
  221. const void *threshold, const void **eltp)
  222. {
  223. return ((const struct gl_oset_impl_base *) set)->vtable
  224. ->search_atleast (set, threshold_fn, threshold, eltp);
  225. }
  226. GL_OSET_INLINE _GL_ATTRIBUTE_NODISCARD int
  227. gl_oset_nx_add (gl_oset_t set, const void *elt)
  228. {
  229. return ((const struct gl_oset_impl_base *) set)->vtable->nx_add (set, elt);
  230. }
  231. GL_OSET_INLINE bool
  232. gl_oset_remove (gl_oset_t set, const void *elt)
  233. {
  234. return ((const struct gl_oset_impl_base *) set)->vtable
  235. ->remove_elt (set, elt);
  236. }
  237. GL_OSET_INLINE int
  238. gl_oset_update (gl_oset_t set, const void *elt,
  239. void (*action) (const void * /*elt*/, void * /*action_data*/),
  240. void *action_data)
  241. {
  242. return ((const struct gl_oset_impl_base *) set)->vtable
  243. ->update (set, elt, action, action_data);
  244. }
  245. GL_OSET_INLINE void
  246. gl_oset_free (gl_oset_t set)
  247. {
  248. ((const struct gl_oset_impl_base *) set)->vtable->oset_free (set);
  249. }
  250. GL_OSET_INLINE gl_oset_iterator_t
  251. gl_oset_iterator (gl_oset_t set)
  252. {
  253. return ((const struct gl_oset_impl_base *) set)->vtable->iterator (set);
  254. }
  255. GL_OSET_INLINE gl_oset_iterator_t
  256. gl_oset_iterator_atleast (gl_oset_t set,
  257. gl_setelement_threshold_fn threshold_fn,
  258. const void *threshold)
  259. {
  260. return ((const struct gl_oset_impl_base *) set)->vtable
  261. ->iterator_atleast (set, threshold_fn, threshold);
  262. }
  263. GL_OSET_INLINE bool
  264. gl_oset_iterator_next (gl_oset_iterator_t *iterator, const void **eltp)
  265. {
  266. return iterator->vtable->iterator_next (iterator, eltp);
  267. }
  268. GL_OSET_INLINE void
  269. gl_oset_iterator_free (gl_oset_iterator_t *iterator)
  270. {
  271. iterator->vtable->iterator_free (iterator);
  272. }
  273. #ifdef __cplusplus
  274. }
  275. #endif
  276. _GL_INLINE_HEADER_END
  277. #endif /* _GL_OSET_H */