gl_list.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. /* Abstract sequential list data type. -*- coding: utf-8 -*-
  2. Copyright (C) 2006-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_LIST_H
  15. #define _GL_LIST_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_LIST_INLINE
  23. # define GL_LIST_INLINE _GL_INLINE
  24. #endif
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /* gl_list is an abstract list data type. It can contain any number of
  29. objects ('void *' or 'const void *' pointers) in any given order.
  30. Duplicates are allowed, but can optionally be forbidden.
  31. There are several implementations of this list datatype, optimized for
  32. different operations or for memory. You can start using the simplest list
  33. implementation, GL_ARRAY_LIST, and switch to a different implementation
  34. later, when you realize which operations are performed the most frequently.
  35. The API of the different implementations is exactly the same; when
  36. switching to a different implementation, you only have to change the
  37. gl_list_create call.
  38. The implementations are:
  39. GL_ARRAY_LIST a growable array
  40. GL_CARRAY_LIST a growable circular array
  41. GL_LINKED_LIST a linked list
  42. GL_AVLTREE_LIST a binary tree (AVL tree)
  43. GL_RBTREE_LIST a binary tree (red-black tree)
  44. GL_LINKEDHASH_LIST a hash table with a linked list
  45. GL_AVLTREEHASH_LIST a hash table with a binary tree (AVL tree)
  46. GL_RBTREEHASH_LIST a hash table with a binary tree (red-black tree)
  47. The memory consumption is asymptotically the same: O(1) for every object
  48. in the list. When looking more closely at the average memory consumed
  49. for an object, GL_ARRAY_LIST is the most compact representation, and
  50. GL_LINKEDHASH_LIST and GL_TREEHASH_LIST need more memory.
  51. The guaranteed average performance of the operations is, for a list of
  52. n elements:
  53. Operation ARRAY LINKED TREE LINKEDHASH TREEHASH
  54. CARRAY with|without with|without
  55. duplicates duplicates
  56. gl_list_size O(1) O(1) O(1) O(1) O(1)
  57. gl_list_node_value O(1) O(1) O(1) O(1) O(1)
  58. gl_list_node_set_value O(1) O(1) O(1) O(1) O((log n)²)/O(1)
  59. gl_list_next_node O(1) O(1) O(log n) O(1) O(log n)
  60. gl_list_previous_node O(1) O(1) O(log n) O(1) O(log n)
  61. gl_list_get_at O(1) O(n) O(log n) O(n) O(log n)
  62. gl_list_get_first O(1) O(1) O(log n) O(1) O(log n)
  63. gl_list_get_last O(1) O(1) O(log n) O(1) O(log n)
  64. gl_list_set_at O(1) O(n) O(log n) O(n) O((log n)²)/O(log n)
  65. gl_list_set_first O(1) O(1) O(log n) O(n)/O(1) O((log n)²)/O(log n)
  66. gl_list_set_last O(1) O(1) O(log n) O(n)/O(1) O((log n)²)/O(log n)
  67. gl_list_search O(n) O(n) O(n) O(n)/O(1) O(log n)/O(1)
  68. gl_list_search_from O(n) O(n) O(n) O(n)/O(1) O((log n)²)/O(log n)
  69. gl_list_search_from_to O(n) O(n) O(n) O(n)/O(1) O((log n)²)/O(log n)
  70. gl_list_indexof O(n) O(n) O(n) O(n) O(log n)
  71. gl_list_indexof_from O(n) O(n) O(n) O(n) O((log n)²)/O(log n)
  72. gl_list_indexof_from_to O(n) O(n) O(n) O(n) O((log n)²)/O(log n)
  73. gl_list_add_first O(n)/O(1) O(1) O(log n) O(1) O((log n)²)/O(log n)
  74. gl_list_add_last O(1) O(1) O(log n) O(1) O((log n)²)/O(log n)
  75. gl_list_add_before O(n) O(1) O(log n) O(1) O((log n)²)/O(log n)
  76. gl_list_add_after O(n) O(1) O(log n) O(1) O((log n)²)/O(log n)
  77. gl_list_add_at O(n) O(n) O(log n) O(n) O((log n)²)/O(log n)
  78. gl_list_remove_node O(n) O(1) O(log n) O(n)/O(1) O((log n)²)/O(log n)
  79. gl_list_remove_at O(n) O(n) O(log n) O(n) O((log n)²)/O(log n)
  80. gl_list_remove_first O(n)/O(1) O(1) O(log n) O(n)/O(1) O((log n)²)/O(log n)
  81. gl_list_remove_last O(1) O(1) O(log n) O(n)/O(1) O((log n)²)/O(log n)
  82. gl_list_remove O(n) O(n) O(n) O(n)/O(1) O((log n)²)/O(log n)
  83. gl_list_iterator O(1) O(1) O(log n) O(1) O(log n)
  84. gl_list_iterator_from_to O(1) O(n) O(log n) O(n) O(log n)
  85. gl_list_iterator_next O(1) O(1) O(log n) O(1) O(log n)
  86. gl_sortedlist_search O(log n) O(n) O(log n) O(n) O(log n)
  87. gl_sortedlist_search_from O(log n) O(n) O(log n) O(n) O(log n)
  88. gl_sortedlist_indexof O(log n) O(n) O(log n) O(n) O(log n)
  89. gl_sortedlist_indexof_fro O(log n) O(n) O(log n) O(n) O(log n)
  90. gl_sortedlist_add O(n) O(n) O(log n) O(n) O((log n)²)/O(log n)
  91. gl_sortedlist_remove O(n) O(n) O(log n) O(n) O((log n)²)/O(log n)
  92. */
  93. /* -------------------------- gl_list_t Data Type -------------------------- */
  94. /* Type of function used to compare two elements.
  95. NULL denotes pointer comparison. */
  96. typedef bool (*gl_listelement_equals_fn) (const void *elt1, const void *elt2);
  97. /* Type of function used to compute a hash code.
  98. NULL denotes a function that depends only on the pointer itself. */
  99. typedef size_t (*gl_listelement_hashcode_fn) (const void *elt);
  100. /* Type of function used to dispose an element once it's removed from a list.
  101. NULL denotes a no-op. */
  102. typedef void (*gl_listelement_dispose_fn) (const void *elt);
  103. struct gl_list_impl;
  104. /* Type representing an entire list. */
  105. typedef struct gl_list_impl * gl_list_t;
  106. struct gl_list_node_impl;
  107. /* Type representing the position of an element in the list, in a way that
  108. is more adapted to the list implementation than a plain index.
  109. Note: It is invalidated by insertions and removals! */
  110. typedef struct gl_list_node_impl * gl_list_node_t;
  111. struct gl_list_implementation;
  112. /* Type representing a list datatype implementation. */
  113. typedef const struct gl_list_implementation * gl_list_implementation_t;
  114. #if 0 /* Unless otherwise specified, these are defined inline below. */
  115. /* Creates an empty list.
  116. IMPLEMENTATION is one of GL_ARRAY_LIST, GL_CARRAY_LIST, GL_LINKED_LIST,
  117. GL_AVLTREE_LIST, GL_RBTREE_LIST, GL_LINKEDHASH_LIST, GL_AVLTREEHASH_LIST,
  118. GL_RBTREEHASH_LIST.
  119. EQUALS_FN is an element comparison function or NULL.
  120. HASHCODE_FN is an element hash code function or NULL.
  121. DISPOSE_FN is an element disposal function or NULL.
  122. ALLOW_DUPLICATES is false if duplicate elements shall not be allowed in
  123. the list. The implementation may verify this at runtime. */
  124. /* declared in gl_xlist.h */
  125. extern gl_list_t gl_list_create_empty (gl_list_implementation_t implementation,
  126. gl_listelement_equals_fn equals_fn,
  127. gl_listelement_hashcode_fn hashcode_fn,
  128. gl_listelement_dispose_fn dispose_fn,
  129. bool allow_duplicates);
  130. /* Likewise. Returns NULL upon out-of-memory. */
  131. extern gl_list_t gl_list_nx_create_empty (gl_list_implementation_t implementation,
  132. gl_listelement_equals_fn equals_fn,
  133. gl_listelement_hashcode_fn hashcode_fn,
  134. gl_listelement_dispose_fn dispose_fn,
  135. bool allow_duplicates);
  136. /* Creates a list with given contents.
  137. IMPLEMENTATION is one of GL_ARRAY_LIST, GL_CARRAY_LIST, GL_LINKED_LIST,
  138. GL_AVLTREE_LIST, GL_RBTREE_LIST, GL_LINKEDHASH_LIST, GL_AVLTREEHASH_LIST,
  139. GL_RBTREEHASH_LIST.
  140. EQUALS_FN is an element comparison function or NULL.
  141. HASHCODE_FN is an element hash code function or NULL.
  142. DISPOSE_FN is an element disposal function or NULL.
  143. ALLOW_DUPLICATES is false if duplicate elements shall not be allowed in
  144. the list. The implementation may verify this at runtime.
  145. COUNT is the number of initial elements.
  146. CONTENTS[0..COUNT-1] is the initial contents. */
  147. /* declared in gl_xlist.h */
  148. extern gl_list_t gl_list_create (gl_list_implementation_t implementation,
  149. gl_listelement_equals_fn equals_fn,
  150. gl_listelement_hashcode_fn hashcode_fn,
  151. gl_listelement_dispose_fn dispose_fn,
  152. bool allow_duplicates,
  153. size_t count, const void **contents);
  154. /* Likewise. Returns NULL upon out-of-memory. */
  155. extern gl_list_t gl_list_nx_create (gl_list_implementation_t implementation,
  156. gl_listelement_equals_fn equals_fn,
  157. gl_listelement_hashcode_fn hashcode_fn,
  158. gl_listelement_dispose_fn dispose_fn,
  159. bool allow_duplicates,
  160. size_t count, const void **contents);
  161. /* Returns the current number of elements in a list. */
  162. extern size_t gl_list_size (gl_list_t list);
  163. /* Returns the element value represented by a list node. */
  164. extern const void * gl_list_node_value (gl_list_t list, gl_list_node_t node);
  165. /* Replaces the element value represented by a list node. */
  166. /* declared in gl_xlist.h */
  167. extern void gl_list_node_set_value (gl_list_t list, gl_list_node_t node,
  168. const void *elt);
  169. /* Likewise. Returns 0 upon success, -1 upon out-of-memory. */
  170. extern int gl_list_node_nx_set_value (gl_list_t list, gl_list_node_t node,
  171. const void *elt)
  172. _GL_ATTRIBUTE_NODISCARD;
  173. /* Returns the node immediately after the given node in the list, or NULL
  174. if the given node is the last (rightmost) one in the list. */
  175. extern gl_list_node_t gl_list_next_node (gl_list_t list, gl_list_node_t node);
  176. /* Returns the node immediately before the given node in the list, or NULL
  177. if the given node is the first (leftmost) one in the list. */
  178. extern gl_list_node_t gl_list_previous_node (gl_list_t list, gl_list_node_t node);
  179. /* Returns the element at a given position in the list.
  180. POSITION must be >= 0 and < gl_list_size (list). */
  181. extern const void * gl_list_get_at (gl_list_t list, size_t position);
  182. /* Returns the element at the first position in the list.
  183. The list must be non-empty. */
  184. extern const void * gl_list_get_first (gl_list_t list);
  185. /* Returns the element at the last position in the list.
  186. The list must be non-empty. */
  187. extern const void * gl_list_get_last (gl_list_t list);
  188. /* Replaces the element at a given position in the list.
  189. POSITION must be >= 0 and < gl_list_size (list).
  190. Returns its node. */
  191. /* declared in gl_xlist.h */
  192. extern gl_list_node_t gl_list_set_at (gl_list_t list, size_t position,
  193. const void *elt);
  194. /* Likewise. Returns NULL upon out-of-memory. */
  195. extern gl_list_node_t gl_list_nx_set_at (gl_list_t list, size_t position,
  196. const void *elt)
  197. _GL_ATTRIBUTE_NODISCARD;
  198. /* Replaces the element at the first position in the list.
  199. Returns its node.
  200. The list must be non-empty. */
  201. /* declared in gl_xlist.h */
  202. extern gl_list_node_t gl_list_set_first (gl_list_t list, const void *elt);
  203. /* Likewise. Returns NULL upon out-of-memory. */
  204. extern gl_list_node_t gl_list_nx_set_first (gl_list_t list, const void *elt)
  205. _GL_ATTRIBUTE_NODISCARD;
  206. /* Replaces the element at the last position in the list.
  207. Returns its node.
  208. The list must be non-empty. */
  209. /* declared in gl_xlist.h */
  210. extern gl_list_node_t gl_list_set_last (gl_list_t list, const void *elt);
  211. /* Likewise. Returns NULL upon out-of-memory. */
  212. extern gl_list_node_t gl_list_nx_set_last (gl_list_t list, const void *elt)
  213. _GL_ATTRIBUTE_NODISCARD;
  214. /* Searches whether an element is already in the list.
  215. Returns its node if found, or NULL if not present in the list. */
  216. extern gl_list_node_t gl_list_search (gl_list_t list, const void *elt);
  217. /* Searches whether an element is already in the list,
  218. at a position >= START_INDEX.
  219. Returns its node if found, or NULL if not present in the list. */
  220. extern gl_list_node_t gl_list_search_from (gl_list_t list, size_t start_index,
  221. const void *elt);
  222. /* Searches whether an element is already in the list,
  223. at a position >= START_INDEX and < END_INDEX.
  224. Returns its node if found, or NULL if not present in the list. */
  225. extern gl_list_node_t gl_list_search_from_to (gl_list_t list,
  226. size_t start_index,
  227. size_t end_index,
  228. const void *elt);
  229. /* Searches whether an element is already in the list.
  230. Returns its position if found, or (size_t)(-1) if not present in the list. */
  231. extern size_t gl_list_indexof (gl_list_t list, const void *elt);
  232. /* Searches whether an element is already in the list,
  233. at a position >= START_INDEX.
  234. Returns its position if found, or (size_t)(-1) if not present in the list. */
  235. extern size_t gl_list_indexof_from (gl_list_t list, size_t start_index,
  236. const void *elt);
  237. /* Searches whether an element is already in the list,
  238. at a position >= START_INDEX and < END_INDEX.
  239. Returns its position if found, or (size_t)(-1) if not present in the list. */
  240. extern size_t gl_list_indexof_from_to (gl_list_t list,
  241. size_t start_index, size_t end_index,
  242. const void *elt);
  243. /* Adds an element as the first element of the list.
  244. Returns its node. */
  245. /* declared in gl_xlist.h */
  246. extern gl_list_node_t gl_list_add_first (gl_list_t list, const void *elt);
  247. /* Likewise. Returns NULL upon out-of-memory. */
  248. extern gl_list_node_t gl_list_nx_add_first (gl_list_t list, const void *elt)
  249. _GL_ATTRIBUTE_NODISCARD;
  250. /* Adds an element as the last element of the list.
  251. Returns its node. */
  252. /* declared in gl_xlist.h */
  253. extern gl_list_node_t gl_list_add_last (gl_list_t list, const void *elt);
  254. /* Likewise. Returns NULL upon out-of-memory. */
  255. extern gl_list_node_t gl_list_nx_add_last (gl_list_t list, const void *elt)
  256. _GL_ATTRIBUTE_NODISCARD;
  257. /* Adds an element before a given element node of the list.
  258. Returns its node. */
  259. /* declared in gl_xlist.h */
  260. extern gl_list_node_t gl_list_add_before (gl_list_t list, gl_list_node_t node,
  261. const void *elt);
  262. /* Likewise. Returns NULL upon out-of-memory. */
  263. extern gl_list_node_t gl_list_nx_add_before (gl_list_t list,
  264. gl_list_node_t node,
  265. const void *elt)
  266. _GL_ATTRIBUTE_NODISCARD;
  267. /* Adds an element after a given element node of the list.
  268. Returns its node. */
  269. /* declared in gl_xlist.h */
  270. extern gl_list_node_t gl_list_add_after (gl_list_t list, gl_list_node_t node,
  271. const void *elt);
  272. /* Likewise. Returns NULL upon out-of-memory. */
  273. extern gl_list_node_t gl_list_nx_add_after (gl_list_t list, gl_list_node_t node,
  274. const void *elt)
  275. _GL_ATTRIBUTE_NODISCARD;
  276. /* Adds an element at a given position in the list.
  277. POSITION must be >= 0 and <= gl_list_size (list). */
  278. /* declared in gl_xlist.h */
  279. extern gl_list_node_t gl_list_add_at (gl_list_t list, size_t position,
  280. const void *elt);
  281. /* Likewise. Returns NULL upon out-of-memory. */
  282. extern gl_list_node_t gl_list_nx_add_at (gl_list_t list, size_t position,
  283. const void *elt)
  284. _GL_ATTRIBUTE_NODISCARD;
  285. /* Removes an element from the list.
  286. Returns true. */
  287. extern bool gl_list_remove_node (gl_list_t list, gl_list_node_t node);
  288. /* Removes an element at a given position from the list.
  289. POSITION must be >= 0 and < gl_list_size (list).
  290. Returns true. */
  291. extern bool gl_list_remove_at (gl_list_t list, size_t position);
  292. /* Removes the element at the first position from the list.
  293. Returns true if it was found and removed, or false if the list was empty. */
  294. extern bool gl_list_remove_first (gl_list_t list);
  295. /* Removes the element at the last position from the list.
  296. Returns true if it was found and removed, or false if the list was empty. */
  297. extern bool gl_list_remove_last (gl_list_t list);
  298. /* Searches and removes an element from the list.
  299. Returns true if it was found and removed. */
  300. extern bool gl_list_remove (gl_list_t list, const void *elt);
  301. /* Frees an entire list.
  302. (But this call does not free the elements of the list. It only invokes
  303. the DISPOSE_FN on each of the elements of the list, and only if the list
  304. is not a sublist.) */
  305. extern void gl_list_free (gl_list_t list);
  306. #endif /* End of inline and gl_xlist.h-defined functions. */
  307. /* --------------------- gl_list_iterator_t Data Type --------------------- */
  308. /* Functions for iterating through a list. */
  309. /* Type of an iterator that traverses a list.
  310. This is a fixed-size struct, so that creation of an iterator doesn't need
  311. memory allocation on the heap. */
  312. typedef struct
  313. {
  314. /* For fast dispatch of gl_list_iterator_next. */
  315. const struct gl_list_implementation *vtable;
  316. /* For detecting whether the last returned element was removed. */
  317. gl_list_t list;
  318. size_t count;
  319. /* Other, implementation-private fields. */
  320. void *p; void *q;
  321. size_t i; size_t j;
  322. } gl_list_iterator_t;
  323. #if 0 /* These are defined inline below. */
  324. /* Creates an iterator traversing a list.
  325. The list contents must not be modified while the iterator is in use,
  326. except for replacing or removing the last returned element. */
  327. extern gl_list_iterator_t gl_list_iterator (gl_list_t list);
  328. /* Creates an iterator traversing the element with indices i,
  329. start_index <= i < end_index, of a list.
  330. The list contents must not be modified while the iterator is in use,
  331. except for replacing or removing the last returned element. */
  332. extern gl_list_iterator_t gl_list_iterator_from_to (gl_list_t list,
  333. size_t start_index,
  334. size_t end_index);
  335. /* If there is a next element, stores the next element in *ELTP, stores its
  336. node in *NODEP if NODEP is non-NULL, advances the iterator and returns true.
  337. Otherwise, returns false. */
  338. extern bool gl_list_iterator_next (gl_list_iterator_t *iterator,
  339. const void **eltp, gl_list_node_t *nodep);
  340. /* Frees an iterator. */
  341. extern void gl_list_iterator_free (gl_list_iterator_t *iterator);
  342. #endif /* End of inline functions. */
  343. /* ---------------------- Sorted gl_list_t Data Type ---------------------- */
  344. /* The following functions are for lists without duplicates where the
  345. order is given by a sort criterion. */
  346. /* Type of function used to compare two elements. Same as for qsort().
  347. NULL denotes pointer comparison. */
  348. typedef int (*gl_listelement_compar_fn) (const void *elt1, const void *elt2);
  349. #if 0 /* Unless otherwise specified, these are defined inline below. */
  350. /* Searches whether an element is already in the list.
  351. The list is assumed to be sorted with COMPAR.
  352. Returns its node if found, or NULL if not present in the list.
  353. If the list contains several copies of ELT, the node of the leftmost one is
  354. returned. */
  355. extern gl_list_node_t gl_sortedlist_search (gl_list_t list,
  356. gl_listelement_compar_fn compar,
  357. const void *elt);
  358. /* Searches whether an element is already in the list.
  359. The list is assumed to be sorted with COMPAR.
  360. Only list elements with indices >= START_INDEX and < END_INDEX are
  361. considered; the implementation uses these bounds to minimize the number
  362. of COMPAR invocations.
  363. Returns its node if found, or NULL if not present in the list.
  364. If the list contains several copies of ELT, the node of the leftmost one is
  365. returned. */
  366. extern gl_list_node_t gl_sortedlist_search_from_to (gl_list_t list,
  367. gl_listelement_compar_fn compar,
  368. size_t start_index,
  369. size_t end_index,
  370. const void *elt);
  371. /* Searches whether an element is already in the list.
  372. The list is assumed to be sorted with COMPAR.
  373. Returns its position if found, or (size_t)(-1) if not present in the list.
  374. If the list contains several copies of ELT, the position of the leftmost one
  375. is returned. */
  376. extern size_t gl_sortedlist_indexof (gl_list_t list,
  377. gl_listelement_compar_fn compar,
  378. const void *elt);
  379. /* Searches whether an element is already in the list.
  380. The list is assumed to be sorted with COMPAR.
  381. Only list elements with indices >= START_INDEX and < END_INDEX are
  382. considered; the implementation uses these bounds to minimize the number
  383. of COMPAR invocations.
  384. Returns its position if found, or (size_t)(-1) if not present in the list.
  385. If the list contains several copies of ELT, the position of the leftmost one
  386. is returned. */
  387. extern size_t gl_sortedlist_indexof_from_to (gl_list_t list,
  388. gl_listelement_compar_fn compar,
  389. size_t start_index,
  390. size_t end_index,
  391. const void *elt);
  392. /* Adds an element at the appropriate position in the list.
  393. The list is assumed to be sorted with COMPAR.
  394. Returns its node. */
  395. /* declared in gl_xlist.h */
  396. extern gl_list_node_t gl_sortedlist_add (gl_list_t list,
  397. gl_listelement_compar_fn compar,
  398. const void *elt);
  399. /* Likewise. Returns NULL upon out-of-memory. */
  400. extern gl_list_node_t gl_sortedlist_nx_add (gl_list_t list,
  401. gl_listelement_compar_fn compar,
  402. const void *elt)
  403. _GL_ATTRIBUTE_NODISCARD;
  404. /* Searches and removes an element from the list.
  405. The list is assumed to be sorted with COMPAR.
  406. Returns true if it was found and removed.
  407. If the list contains several copies of ELT, only the leftmost one is
  408. removed. */
  409. extern bool gl_sortedlist_remove (gl_list_t list,
  410. gl_listelement_compar_fn compar,
  411. const void *elt);
  412. #endif /* End of inline and gl_xlist.h-defined functions. */
  413. /* ------------------------ Implementation Details ------------------------ */
  414. struct gl_list_implementation
  415. {
  416. /* gl_list_t functions. */
  417. gl_list_t (*nx_create_empty) (gl_list_implementation_t implementation,
  418. gl_listelement_equals_fn equals_fn,
  419. gl_listelement_hashcode_fn hashcode_fn,
  420. gl_listelement_dispose_fn dispose_fn,
  421. bool allow_duplicates);
  422. gl_list_t (*nx_create) (gl_list_implementation_t implementation,
  423. gl_listelement_equals_fn equals_fn,
  424. gl_listelement_hashcode_fn hashcode_fn,
  425. gl_listelement_dispose_fn dispose_fn,
  426. bool allow_duplicates,
  427. size_t count, const void **contents);
  428. size_t (*size) (gl_list_t list);
  429. const void * (*node_value) (gl_list_t list, gl_list_node_t node);
  430. int (*node_nx_set_value) (gl_list_t list, gl_list_node_t node,
  431. const void *elt);
  432. gl_list_node_t (*next_node) (gl_list_t list, gl_list_node_t node);
  433. gl_list_node_t (*previous_node) (gl_list_t list, gl_list_node_t node);
  434. const void * (*get_at) (gl_list_t list, size_t position);
  435. gl_list_node_t (*nx_set_at) (gl_list_t list, size_t position,
  436. const void *elt);
  437. gl_list_node_t (*search_from_to) (gl_list_t list, size_t start_index,
  438. size_t end_index, const void *elt);
  439. size_t (*indexof_from_to) (gl_list_t list, size_t start_index,
  440. size_t end_index, const void *elt);
  441. gl_list_node_t (*nx_add_first) (gl_list_t list, const void *elt);
  442. gl_list_node_t (*nx_add_last) (gl_list_t list, const void *elt);
  443. gl_list_node_t (*nx_add_before) (gl_list_t list, gl_list_node_t node,
  444. const void *elt);
  445. gl_list_node_t (*nx_add_after) (gl_list_t list, gl_list_node_t node,
  446. const void *elt);
  447. gl_list_node_t (*nx_add_at) (gl_list_t list, size_t position,
  448. const void *elt);
  449. bool (*remove_node) (gl_list_t list, gl_list_node_t node);
  450. bool (*remove_at) (gl_list_t list, size_t position);
  451. bool (*remove_elt) (gl_list_t list, const void *elt);
  452. void (*list_free) (gl_list_t list);
  453. /* gl_list_iterator_t functions. */
  454. gl_list_iterator_t (*iterator) (gl_list_t list);
  455. gl_list_iterator_t (*iterator_from_to) (gl_list_t list,
  456. size_t start_index,
  457. size_t end_index);
  458. bool (*iterator_next) (gl_list_iterator_t *iterator,
  459. const void **eltp, gl_list_node_t *nodep);
  460. void (*iterator_free) (gl_list_iterator_t *iterator);
  461. /* Sorted gl_list_t functions. */
  462. gl_list_node_t (*sortedlist_search) (gl_list_t list,
  463. gl_listelement_compar_fn compar,
  464. const void *elt);
  465. gl_list_node_t (*sortedlist_search_from_to) (gl_list_t list,
  466. gl_listelement_compar_fn compar,
  467. size_t start_index,
  468. size_t end_index,
  469. const void *elt);
  470. size_t (*sortedlist_indexof) (gl_list_t list,
  471. gl_listelement_compar_fn compar,
  472. const void *elt);
  473. size_t (*sortedlist_indexof_from_to) (gl_list_t list,
  474. gl_listelement_compar_fn compar,
  475. size_t start_index, size_t end_index,
  476. const void *elt);
  477. gl_list_node_t (*sortedlist_nx_add) (gl_list_t list,
  478. gl_listelement_compar_fn compar,
  479. const void *elt);
  480. bool (*sortedlist_remove) (gl_list_t list,
  481. gl_listelement_compar_fn compar,
  482. const void *elt);
  483. };
  484. struct gl_list_impl_base
  485. {
  486. const struct gl_list_implementation *vtable;
  487. gl_listelement_equals_fn equals_fn;
  488. gl_listelement_hashcode_fn hashcode_fn;
  489. gl_listelement_dispose_fn dispose_fn;
  490. bool allow_duplicates;
  491. };
  492. /* Define all functions of this file as accesses to the
  493. struct gl_list_implementation. */
  494. GL_LIST_INLINE gl_list_t
  495. gl_list_nx_create_empty (gl_list_implementation_t implementation,
  496. gl_listelement_equals_fn equals_fn,
  497. gl_listelement_hashcode_fn hashcode_fn,
  498. gl_listelement_dispose_fn dispose_fn,
  499. bool allow_duplicates)
  500. {
  501. return implementation->nx_create_empty (implementation, equals_fn,
  502. hashcode_fn, dispose_fn,
  503. allow_duplicates);
  504. }
  505. GL_LIST_INLINE gl_list_t
  506. gl_list_nx_create (gl_list_implementation_t implementation,
  507. gl_listelement_equals_fn equals_fn,
  508. gl_listelement_hashcode_fn hashcode_fn,
  509. gl_listelement_dispose_fn dispose_fn,
  510. bool allow_duplicates,
  511. size_t count, const void **contents)
  512. {
  513. return implementation->nx_create (implementation, equals_fn, hashcode_fn,
  514. dispose_fn, allow_duplicates, count,
  515. contents);
  516. }
  517. GL_LIST_INLINE size_t
  518. gl_list_size (gl_list_t list)
  519. {
  520. return ((const struct gl_list_impl_base *) list)->vtable
  521. ->size (list);
  522. }
  523. GL_LIST_INLINE const void *
  524. gl_list_node_value (gl_list_t list, gl_list_node_t node)
  525. {
  526. return ((const struct gl_list_impl_base *) list)->vtable
  527. ->node_value (list, node);
  528. }
  529. GL_LIST_INLINE _GL_ATTRIBUTE_NODISCARD int
  530. gl_list_node_nx_set_value (gl_list_t list, gl_list_node_t node,
  531. const void *elt)
  532. {
  533. return ((const struct gl_list_impl_base *) list)->vtable
  534. ->node_nx_set_value (list, node, elt);
  535. }
  536. GL_LIST_INLINE gl_list_node_t
  537. gl_list_next_node (gl_list_t list, gl_list_node_t node)
  538. {
  539. return ((const struct gl_list_impl_base *) list)->vtable
  540. ->next_node (list, node);
  541. }
  542. GL_LIST_INLINE gl_list_node_t
  543. gl_list_previous_node (gl_list_t list, gl_list_node_t node)
  544. {
  545. return ((const struct gl_list_impl_base *) list)->vtable
  546. ->previous_node (list, node);
  547. }
  548. GL_LIST_INLINE const void *
  549. gl_list_get_at (gl_list_t list, size_t position)
  550. {
  551. return ((const struct gl_list_impl_base *) list)->vtable
  552. ->get_at (list, position);
  553. }
  554. GL_LIST_INLINE const void *
  555. gl_list_get_first (gl_list_t list)
  556. {
  557. return gl_list_get_at (list, 0);
  558. }
  559. GL_LIST_INLINE const void *
  560. gl_list_get_last (gl_list_t list)
  561. {
  562. return gl_list_get_at (list, gl_list_size (list) - 1);
  563. }
  564. GL_LIST_INLINE _GL_ATTRIBUTE_NODISCARD gl_list_node_t
  565. gl_list_nx_set_at (gl_list_t list, size_t position, const void *elt)
  566. {
  567. return ((const struct gl_list_impl_base *) list)->vtable
  568. ->nx_set_at (list, position, elt);
  569. }
  570. GL_LIST_INLINE _GL_ATTRIBUTE_NODISCARD gl_list_node_t
  571. gl_list_nx_set_first (gl_list_t list, const void *elt)
  572. {
  573. return gl_list_nx_set_at (list, 0, elt);
  574. }
  575. GL_LIST_INLINE _GL_ATTRIBUTE_NODISCARD gl_list_node_t
  576. gl_list_nx_set_last (gl_list_t list, const void *elt)
  577. {
  578. return gl_list_nx_set_at (list, gl_list_size (list) - 1, elt);
  579. }
  580. GL_LIST_INLINE gl_list_node_t
  581. gl_list_search (gl_list_t list, const void *elt)
  582. {
  583. size_t size = ((const struct gl_list_impl_base *) list)->vtable->size (list);
  584. return ((const struct gl_list_impl_base *) list)->vtable
  585. ->search_from_to (list, 0, size, elt);
  586. }
  587. GL_LIST_INLINE gl_list_node_t
  588. gl_list_search_from (gl_list_t list, size_t start_index, const void *elt)
  589. {
  590. size_t size = ((const struct gl_list_impl_base *) list)->vtable->size (list);
  591. return ((const struct gl_list_impl_base *) list)->vtable
  592. ->search_from_to (list, start_index, size, elt);
  593. }
  594. GL_LIST_INLINE gl_list_node_t
  595. gl_list_search_from_to (gl_list_t list, size_t start_index, size_t end_index,
  596. const void *elt)
  597. {
  598. return ((const struct gl_list_impl_base *) list)->vtable
  599. ->search_from_to (list, start_index, end_index, elt);
  600. }
  601. GL_LIST_INLINE size_t
  602. gl_list_indexof (gl_list_t list, const void *elt)
  603. {
  604. size_t size = ((const struct gl_list_impl_base *) list)->vtable->size (list);
  605. return ((const struct gl_list_impl_base *) list)->vtable
  606. ->indexof_from_to (list, 0, size, elt);
  607. }
  608. GL_LIST_INLINE size_t
  609. gl_list_indexof_from (gl_list_t list, size_t start_index, const void *elt)
  610. {
  611. size_t size = ((const struct gl_list_impl_base *) list)->vtable->size (list);
  612. return ((const struct gl_list_impl_base *) list)->vtable
  613. ->indexof_from_to (list, start_index, size, elt);
  614. }
  615. GL_LIST_INLINE size_t
  616. gl_list_indexof_from_to (gl_list_t list, size_t start_index, size_t end_index,
  617. const void *elt)
  618. {
  619. return ((const struct gl_list_impl_base *) list)->vtable
  620. ->indexof_from_to (list, start_index, end_index, elt);
  621. }
  622. GL_LIST_INLINE _GL_ATTRIBUTE_NODISCARD gl_list_node_t
  623. gl_list_nx_add_first (gl_list_t list, const void *elt)
  624. {
  625. return ((const struct gl_list_impl_base *) list)->vtable
  626. ->nx_add_first (list, elt);
  627. }
  628. GL_LIST_INLINE _GL_ATTRIBUTE_NODISCARD gl_list_node_t
  629. gl_list_nx_add_last (gl_list_t list, const void *elt)
  630. {
  631. return ((const struct gl_list_impl_base *) list)->vtable
  632. ->nx_add_last (list, elt);
  633. }
  634. GL_LIST_INLINE _GL_ATTRIBUTE_NODISCARD gl_list_node_t
  635. gl_list_nx_add_before (gl_list_t list, gl_list_node_t node, const void *elt)
  636. {
  637. return ((const struct gl_list_impl_base *) list)->vtable
  638. ->nx_add_before (list, node, elt);
  639. }
  640. GL_LIST_INLINE _GL_ATTRIBUTE_NODISCARD gl_list_node_t
  641. gl_list_nx_add_after (gl_list_t list, gl_list_node_t node, const void *elt)
  642. {
  643. return ((const struct gl_list_impl_base *) list)->vtable
  644. ->nx_add_after (list, node, elt);
  645. }
  646. GL_LIST_INLINE _GL_ATTRIBUTE_NODISCARD gl_list_node_t
  647. gl_list_nx_add_at (gl_list_t list, size_t position, const void *elt)
  648. {
  649. return ((const struct gl_list_impl_base *) list)->vtable
  650. ->nx_add_at (list, position, elt);
  651. }
  652. GL_LIST_INLINE bool
  653. gl_list_remove_node (gl_list_t list, gl_list_node_t node)
  654. {
  655. return ((const struct gl_list_impl_base *) list)->vtable
  656. ->remove_node (list, node);
  657. }
  658. GL_LIST_INLINE bool
  659. gl_list_remove_at (gl_list_t list, size_t position)
  660. {
  661. return ((const struct gl_list_impl_base *) list)->vtable
  662. ->remove_at (list, position);
  663. }
  664. GL_LIST_INLINE bool
  665. gl_list_remove_first (gl_list_t list)
  666. {
  667. size_t size = gl_list_size (list);
  668. if (size > 0)
  669. return gl_list_remove_at (list, 0);
  670. else
  671. return false;
  672. }
  673. GL_LIST_INLINE bool
  674. gl_list_remove_last (gl_list_t list)
  675. {
  676. size_t size = gl_list_size (list);
  677. if (size > 0)
  678. return gl_list_remove_at (list, size - 1);
  679. else
  680. return false;
  681. }
  682. GL_LIST_INLINE bool
  683. gl_list_remove (gl_list_t list, const void *elt)
  684. {
  685. return ((const struct gl_list_impl_base *) list)->vtable
  686. ->remove_elt (list, elt);
  687. }
  688. GL_LIST_INLINE void
  689. gl_list_free (gl_list_t list)
  690. {
  691. ((const struct gl_list_impl_base *) list)->vtable->list_free (list);
  692. }
  693. GL_LIST_INLINE gl_list_iterator_t
  694. gl_list_iterator (gl_list_t list)
  695. {
  696. return ((const struct gl_list_impl_base *) list)->vtable
  697. ->iterator (list);
  698. }
  699. GL_LIST_INLINE gl_list_iterator_t
  700. gl_list_iterator_from_to (gl_list_t list, size_t start_index, size_t end_index)
  701. {
  702. return ((const struct gl_list_impl_base *) list)->vtable
  703. ->iterator_from_to (list, start_index, end_index);
  704. }
  705. GL_LIST_INLINE bool
  706. gl_list_iterator_next (gl_list_iterator_t *iterator,
  707. const void **eltp, gl_list_node_t *nodep)
  708. {
  709. return iterator->vtable->iterator_next (iterator, eltp, nodep);
  710. }
  711. GL_LIST_INLINE void
  712. gl_list_iterator_free (gl_list_iterator_t *iterator)
  713. {
  714. iterator->vtable->iterator_free (iterator);
  715. }
  716. GL_LIST_INLINE gl_list_node_t
  717. gl_sortedlist_search (gl_list_t list, gl_listelement_compar_fn compar, const void *elt)
  718. {
  719. return ((const struct gl_list_impl_base *) list)->vtable
  720. ->sortedlist_search (list, compar, elt);
  721. }
  722. GL_LIST_INLINE gl_list_node_t
  723. gl_sortedlist_search_from_to (gl_list_t list, gl_listelement_compar_fn compar, size_t start_index, size_t end_index, const void *elt)
  724. {
  725. return ((const struct gl_list_impl_base *) list)->vtable
  726. ->sortedlist_search_from_to (list, compar, start_index, end_index,
  727. elt);
  728. }
  729. GL_LIST_INLINE size_t
  730. gl_sortedlist_indexof (gl_list_t list, gl_listelement_compar_fn compar, const void *elt)
  731. {
  732. return ((const struct gl_list_impl_base *) list)->vtable
  733. ->sortedlist_indexof (list, compar, elt);
  734. }
  735. GL_LIST_INLINE size_t
  736. gl_sortedlist_indexof_from_to (gl_list_t list, gl_listelement_compar_fn compar, size_t start_index, size_t end_index, const void *elt)
  737. {
  738. return ((const struct gl_list_impl_base *) list)->vtable
  739. ->sortedlist_indexof_from_to (list, compar, start_index, end_index,
  740. elt);
  741. }
  742. GL_LIST_INLINE _GL_ATTRIBUTE_NODISCARD gl_list_node_t
  743. gl_sortedlist_nx_add (gl_list_t list, gl_listelement_compar_fn compar, const void *elt)
  744. {
  745. return ((const struct gl_list_impl_base *) list)->vtable
  746. ->sortedlist_nx_add (list, compar, elt);
  747. }
  748. GL_LIST_INLINE bool
  749. gl_sortedlist_remove (gl_list_t list, gl_listelement_compar_fn compar, const void *elt)
  750. {
  751. return ((const struct gl_list_impl_base *) list)->vtable
  752. ->sortedlist_remove (list, compar, elt);
  753. }
  754. #ifdef __cplusplus
  755. }
  756. #endif
  757. _GL_INLINE_HEADER_END
  758. #endif /* _GL_LIST_H */