gl_list.h 35 KB

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