1 /*
2 * SysDB - src/include/utils/llist.h
3 * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #ifndef SDB_UTILS_LLIST_H
29 #define SDB_UTILS_LLIST_H 1
31 #include "core/object.h"
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
37 struct sdb_llist;
38 typedef struct sdb_llist sdb_llist_t;
40 struct sdb_llist_iter;
41 typedef struct sdb_llist_iter sdb_llist_iter_t;
43 typedef int (*sdb_llist_cmp_cb)(const sdb_object_t *, const sdb_object_t *);
44 typedef int (*sdb_llist_lookup_cb)(const sdb_object_t *, const void *user_data);
46 /*
47 * sdb_llist_create, sdb_llist_destroy:
48 * Create and destroy a doubly linked list object.
49 *
50 * sdb_llist_create returns NULL on error.
51 * sdb_llist_destroy will also destroy all remaining elements, thus releasing
52 * the included objects (decrement the ref-count).
53 */
54 sdb_llist_t *
55 sdb_llist_create(void);
56 void
57 sdb_llist_destroy(sdb_llist_t *list);
59 /*
60 * sdb_llist_clear:
61 * Remove all elements from the list, releasing the included objects
62 * (decrement the ref-count).
63 */
64 void
65 sdb_llist_clear(sdb_llist_t *list);
67 /*
68 * sdb_llist_clone:
69 * Clone an existing list. The objects stored in the list will not be copied
70 * but rather their reference count incremented.
71 *
72 * Returns:
73 * - the copied list on success
74 * - NULL else
75 */
76 sdb_llist_t *
77 sdb_llist_clone(sdb_llist_t *list);
79 /*
80 * sdb_llist_append:
81 * Append the given 'obj' to the end of 'list'. The list will take ownership
82 * of the object, that is, increment the reference count by one. In case the
83 * caller does not longer use the object for other purposes, it should thus
84 * deref it.
85 *
86 * Returns:
87 * - 0 on success
88 * - a negative value on failure
89 */
90 int
91 sdb_llist_append(sdb_llist_t *list, sdb_object_t *obj);
93 /*
94 * sdb_llist_insert:
95 * Insert the new element at the specified position (zero being the head of
96 * the list; the length of the list being the tail)). If the index is greater
97 * than the length of the list (i.e. past the tail of the list), an error is
98 * returned. The list will take ownership of the object, that is, increment
99 * the reference count by one. In case the caller does not longer use the
100 * object for other purposes, it should thus deref it.
101 *
102 * Returns:
103 * - 0 on success
104 * - a negative value on failure
105 */
106 int
107 sdb_llist_insert(sdb_llist_t *list, sdb_object_t *obj, size_t idx);
109 /*
110 * sdb_llist_insert_sorted:
111 * Insert the given 'obj' in the 'list' using a sort order as determined by
112 * the 'compare' function. The function will insert the new entry before the
113 * first entry which sorts later than the new entry. It will not ensure that
114 * the rest of the list is sorted. The list will take ownership of the object,
115 * that is, increment the reference count by one. In case the caller does not
116 * longer use the object for other purposes, it should thus deref it.
117 *
118 * The 'compare' function should return less than zero, zero, greater than
119 * zero if the first argument sorts less than, equal or greater than the
120 * second argument respectively.
121 *
122 * Returns:
123 * - 0 on success
124 * - a negative value on failure
125 */
126 int
127 sdb_llist_insert_sorted(sdb_llist_t *list,
128 sdb_object_t *obj, sdb_llist_cmp_cb);
130 /*
131 * sdb_llist_get:
132 * Returns the i-th element of the list or NULL in case of an error. The
133 * reference count of the element is incremented before returning it to share
134 * ownership between the list and the caller.
135 */
136 sdb_object_t *
137 sdb_llist_get(sdb_llist_t *list, size_t i);
139 /*
140 * sdb_llist_search:
141 * Search for a object in the given 'list'. The function will return the first
142 * entry for which the 'lookup' callback returns 0. The 'user_data' is passed
143 * on to the lookup function on each invocation.
144 *
145 * Returns:
146 * - a pointer to the first matching object
147 * - NULL else
148 */
149 sdb_object_t *
150 sdb_llist_search(sdb_llist_t *list,
151 sdb_llist_lookup_cb lookup, const void *user_data);
153 /*
154 * sdb_llist_search_by_name:
155 * Search for an object named 'key' in the given 'list'. The function will
156 * return the first entry whose name matches the specified 'key' ignoring the
157 * case of the characters.
158 *
159 * Returns:
160 * - a pointer to the first matching object
161 * - NULL else
162 */
163 sdb_object_t *
164 sdb_llist_search_by_name(sdb_llist_t *list, const char *key);
166 /*
167 * sdb_llist_remove:
168 * Removes and returns the first matching element of the list. The ref-count
169 * of the item will not be changed, that is, if the element will not be used
170 * any further, it should be de-referenced by the caller.
171 *
172 * Returns:
173 * - a pointer to the first matching object
174 * - NULL else
175 */
176 sdb_object_t *
177 sdb_llist_remove(sdb_llist_t *list,
178 sdb_llist_lookup_cb lookup, const void *user_data);
180 /*
181 * sdb_llist_remove_by_name:
182 * Removes and returns the first element whose name matches the specified key.
183 * The ref-count of the item will not be changed, that is, if the element will
184 * not be used any further, it should be de-referenced by the caller.
185 *
186 * Returns:
187 * - a pointer to the first matching object
188 * - NULL else
189 */
190 sdb_object_t *
191 sdb_llist_remove_by_name(sdb_llist_t *list, const char *key);
193 /*
194 * sdb_llist_shift:
195 * Removes and returns the first element of the list. The ref-count of the
196 * item will not be changed, that is, if the element will not be used any
197 * further, it should be de-referenced by the caller.
198 *
199 * Returns:
200 * - the former first element of the list
201 * - NULL if the list is empty
202 */
203 sdb_object_t *
204 sdb_llist_shift(sdb_llist_t *list);
206 /*
207 * sdb_llist_get_iter, sdb_llist_iter_has_next, sdb_llist_iter_get_next:
208 * Iterate through the list, element by element.
209 *
210 * sdb_llist_iter_get_next returns NULL if there is no next element.
211 */
212 sdb_llist_iter_t *
213 sdb_llist_get_iter(sdb_llist_t *list);
214 void
215 sdb_llist_iter_destroy(sdb_llist_iter_t *iter);
217 _Bool
218 sdb_llist_iter_has_next(sdb_llist_iter_t *iter);
219 sdb_object_t *
220 sdb_llist_iter_get_next(sdb_llist_iter_t *iter);
222 /*
223 * sdb_llist_iter_remove_current:
224 * Remove the current object from the list, that is, the object which was
225 * returned by the last call to sdb_llist_iter_get_next().
226 *
227 * This operation is not safe if another iterator is in use at the same time.
228 *
229 * Returns:
230 * - 0 on success
231 * - a negative value else
232 */
233 int
234 sdb_llist_iter_remove_current(sdb_llist_iter_t *iter);
236 /*
237 * sdb_llist_len:
238 * Return the length (number of elements) of the list.
239 */
240 size_t
241 sdb_llist_len(sdb_llist_t *list);
243 #ifdef __cplusplus
244 } /* extern "C" */
245 #endif
247 #endif /* ! SDB_UTILS_LLIST_H */
249 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */