Allow the use of a destructor for the values stored in the list.
[gnulib.git] / lib / gl_list.c
1 /* Abstract sequential list data type.
2    Copyright (C) 2006-2007 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2006.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include "gl_list.h"
23
24 #if !HAVE_INLINE
25
26 /* Define all functions of this file as inline accesses to the
27    struct gl_list_implementation.
28    Use #define to avoid a warning because of extern vs. static.  */
29
30 gl_list_t
31 gl_list_create_empty (gl_list_implementation_t implementation,
32                       gl_listelement_equals_fn equals_fn,
33                       gl_listelement_hashcode_fn hashcode_fn,
34                       gl_listelement_dispose_fn dispose_fn,
35                       bool allow_duplicates)
36 {
37   return implementation->create_empty (implementation, equals_fn, hashcode_fn,
38                                        dispose_fn, allow_duplicates);
39 }
40
41 gl_list_t
42 gl_list_create (gl_list_implementation_t implementation,
43                 gl_listelement_equals_fn equals_fn,
44                 gl_listelement_hashcode_fn hashcode_fn,
45                 gl_listelement_dispose_fn dispose_fn,
46                 bool allow_duplicates,
47                 size_t count, const void **contents)
48 {
49   return implementation->create (implementation, equals_fn, hashcode_fn,
50                                  dispose_fn, allow_duplicates, count, contents);
51 }
52
53 size_t
54 gl_list_size (gl_list_t list)
55 {
56   return ((const struct gl_list_impl_base *) list)->vtable
57          ->size (list);
58 }
59
60 const void *
61 gl_list_node_value (gl_list_t list, gl_list_node_t node)
62 {
63   return ((const struct gl_list_impl_base *) list)->vtable
64          ->node_value (list, node);
65 }
66
67 gl_list_node_t
68 gl_list_next_node (gl_list_t list, gl_list_node_t node)
69 {
70   return ((const struct gl_list_impl_base *) list)->vtable
71          ->next_node (list, node);
72 }
73
74 gl_list_node_t
75 gl_list_previous_node (gl_list_t list, gl_list_node_t node)
76 {
77   return ((const struct gl_list_impl_base *) list)->vtable
78          ->previous_node (list, node);
79 }
80
81 const void *
82 gl_list_get_at (gl_list_t list, size_t position)
83 {
84   return ((const struct gl_list_impl_base *) list)->vtable
85          ->get_at (list, position);
86 }
87
88 gl_list_node_t
89 gl_list_set_at (gl_list_t list, size_t position, const void *elt)
90 {
91   return ((const struct gl_list_impl_base *) list)->vtable
92          ->set_at (list, position, elt);
93 }
94
95 gl_list_node_t
96 gl_list_search (gl_list_t list, const void *elt)
97 {
98   size_t size = ((const struct gl_list_impl_base *) list)->vtable->size (list);
99   return ((const struct gl_list_impl_base *) list)->vtable
100          ->search_from_to (list, 0, size, elt);
101 }
102
103 gl_list_node_t
104 gl_list_search_from (gl_list_t list, size_t start_index, const void *elt)
105 {
106   size_t size = ((const struct gl_list_impl_base *) list)->vtable->size (list);
107   return ((const struct gl_list_impl_base *) list)->vtable
108          ->search_from_to (list, start_index, size, elt);
109 }
110
111 gl_list_node_t
112 gl_list_search_from_to (gl_list_t list, size_t start_index, size_t end_index, const void *elt)
113 {
114   return ((const struct gl_list_impl_base *) list)->vtable
115          ->search_from_to (list, start_index, end_index, elt);
116 }
117
118 size_t
119 gl_list_indexof (gl_list_t list, const void *elt)
120 {
121   size_t size = ((const struct gl_list_impl_base *) list)->vtable->size (list);
122   return ((const struct gl_list_impl_base *) list)->vtable
123          ->indexof_from_to (list, 0, size, elt);
124 }
125
126 size_t
127 gl_list_indexof_from (gl_list_t list, size_t start_index, const void *elt)
128 {
129   size_t size = ((const struct gl_list_impl_base *) list)->vtable->size (list);
130   return ((const struct gl_list_impl_base *) list)->vtable
131          ->indexof_from_to (list, start_index, size, elt);
132 }
133
134 size_t
135 gl_list_indexof_from_to (gl_list_t list, size_t start_index, size_t end_index, const void *elt)
136 {
137   return ((const struct gl_list_impl_base *) list)->vtable
138          ->indexof_from_to (list, start_index, end_index, elt);
139 }
140
141 gl_list_node_t
142 gl_list_add_first (gl_list_t list, const void *elt)
143 {
144   return ((const struct gl_list_impl_base *) list)->vtable
145          ->add_first (list, elt);
146 }
147
148 gl_list_node_t
149 gl_list_add_last (gl_list_t list, const void *elt)
150 {
151   return ((const struct gl_list_impl_base *) list)->vtable
152          ->add_last (list, elt);
153 }
154
155 gl_list_node_t
156 gl_list_add_before (gl_list_t list, gl_list_node_t node, const void *elt)
157 {
158   return ((const struct gl_list_impl_base *) list)->vtable
159          ->add_before (list, node, elt);
160 }
161
162 gl_list_node_t
163 gl_list_add_after (gl_list_t list, gl_list_node_t node, const void *elt)
164 {
165   return ((const struct gl_list_impl_base *) list)->vtable
166          ->add_after (list, node, elt);
167 }
168
169 gl_list_node_t
170 gl_list_add_at (gl_list_t list, size_t position, const void *elt)
171 {
172   return ((const struct gl_list_impl_base *) list)->vtable
173          ->add_at (list, position, elt);
174 }
175
176 bool
177 gl_list_remove_node (gl_list_t list, gl_list_node_t node)
178 {
179   return ((const struct gl_list_impl_base *) list)->vtable
180          ->remove_node (list, node);
181 }
182
183 bool
184 gl_list_remove_at (gl_list_t list, size_t position)
185 {
186   return ((const struct gl_list_impl_base *) list)->vtable
187          ->remove_at (list, position);
188 }
189
190 bool
191 gl_list_remove (gl_list_t list, const void *elt)
192 {
193   return ((const struct gl_list_impl_base *) list)->vtable
194          ->remove (list, elt);
195 }
196
197 void
198 gl_list_free (gl_list_t list)
199 {
200   ((const struct gl_list_impl_base *) list)->vtable->list_free (list);
201 }
202
203 gl_list_iterator_t
204 gl_list_iterator (gl_list_t list)
205 {
206   return ((const struct gl_list_impl_base *) list)->vtable
207          ->iterator (list);
208 }
209
210 gl_list_iterator_t
211 gl_list_iterator_from_to (gl_list_t list, size_t start_index, size_t end_index)
212 {
213   return ((const struct gl_list_impl_base *) list)->vtable
214          ->iterator_from_to (list, start_index, end_index);
215 }
216
217 bool
218 gl_list_iterator_next (gl_list_iterator_t *iterator,
219                        const void **eltp, gl_list_node_t *nodep)
220 {
221   return iterator->vtable->iterator_next (iterator, eltp, nodep);
222 }
223
224 void
225 gl_list_iterator_free (gl_list_iterator_t *iterator)
226 {
227   iterator->vtable->iterator_free (iterator);
228 }
229
230 gl_list_node_t
231 gl_sortedlist_search (gl_list_t list, gl_listelement_compar_fn compar, const void *elt)
232 {
233   return ((const struct gl_list_impl_base *) list)->vtable
234          ->sortedlist_search (list, compar, elt);
235 }
236
237 gl_list_node_t
238 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)
239 {
240   return ((const struct gl_list_impl_base *) list)->vtable
241          ->sortedlist_search_from_to (list, compar, start_index, end_index,
242                                       elt);
243 }
244
245 size_t
246 gl_sortedlist_indexof (gl_list_t list, gl_listelement_compar_fn compar, const void *elt)
247 {
248   return ((const struct gl_list_impl_base *) list)->vtable
249          ->sortedlist_indexof (list, compar, elt);
250 }
251
252 size_t
253 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)
254 {
255   return ((const struct gl_list_impl_base *) list)->vtable
256          ->sortedlist_indexof_from_to (list, compar, start_index, end_index,
257                                        elt);
258 }
259
260 gl_list_node_t
261 gl_sortedlist_add (gl_list_t list, gl_listelement_compar_fn compar, const void *elt)
262 {
263   return ((const struct gl_list_impl_base *) list)->vtable
264          ->sortedlist_add (list, compar, elt);
265 }
266
267 bool
268 gl_sortedlist_remove (gl_list_t list, gl_listelement_compar_fn compar, const void *elt)
269 {
270   return ((const struct gl_list_impl_base *) list)->vtable
271          ->sortedlist_remove (list, compar, elt);
272 }
273
274 #endif