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_clone:
61 * Clone an existing list. The objects stored in the list will not be copied
62 * but rather their reference count incremented.
63 *
64 * Returns:
65 * - the copied list on success
66 * - NULL else
67 */
68 sdb_llist_t *
69 sdb_llist_clone(sdb_llist_t *list);
71 /*
72 * sdb_llist_append:
73 * Append the given 'obj' to the end of 'list'. The list will take ownership
74 * of the object, that is, increment the reference count by one. In case the
75 * caller does not longer use the object for other purposes, it should thus
76 * deref it.
77 *
78 * Returns:
79 * - 0 on success
80 * - a negative value on failure
81 */
82 int
83 sdb_llist_append(sdb_llist_t *list, sdb_object_t *obj);
85 /*
86 * sdb_llist_insert:
87 * Insert the new element at the specified position (zero being the head of
88 * the list; the length of the list being the tail)). If the index is greater
89 * than the length of the list (i.e. past the tail of the list), an error is
90 * returned. The list will take ownership of the object, that is, increment
91 * the reference count by one. In case the caller does not longer use the
92 * object for other purposes, it should thus deref it.
93 *
94 * Returns:
95 * - 0 on success
96 * - a negative value on failure
97 */
98 int
99 sdb_llist_insert(sdb_llist_t *list, sdb_object_t *obj, size_t idx);
101 /*
102 * sdb_llist_insert_sorted:
103 * Insert the given 'obj' in the 'list' using a sort order as determined by
104 * the 'compare' function. The function will insert the new entry before the
105 * first entry which sorts later than the new entry. It will not ensure that
106 * the rest of the list is sorted. The list will take ownership of the object,
107 * that is, increment the reference count by one. In case the caller does not
108 * longer use the object for other purposes, it should thus deref it.
109 *
110 * The 'compare' function should return less than zero, zero, greater than
111 * zero if the first argument sorts less than, equal or greater than the
112 * second argument respectively.
113 *
114 * Returns:
115 * - 0 on success
116 * - a negative value on failure
117 */
118 int
119 sdb_llist_insert_sorted(sdb_llist_t *list,
120 sdb_object_t *obj, sdb_llist_cmp_cb);
122 /*
123 * sdb_llist_get:
124 * Returns the i-th element of the list or NULL in case of an error.
125 */
126 sdb_object_t *
127 sdb_llist_get(sdb_llist_t *list, size_t i);
129 /*
130 * sdb_llist_search:
131 * Search for a object in the given 'list'. The function will return the first
132 * entry for which the 'lookup' callback returns 0. The 'user_data' is passed
133 * on to the lookup function on each invocation.
134 *
135 * Returns:
136 * - a pointer to the first matching object
137 * - NULL else
138 */
139 sdb_object_t *
140 sdb_llist_search(sdb_llist_t *list,
141 sdb_llist_lookup_cb lookup, const void *user_data);
143 /*
144 * sdb_llist_search_by_name:
145 * Search for an object named 'key' in the given 'list'. The function will
146 * return the first entry whose name matches the specified 'key' ignoring the
147 * case of the characters.
148 *
149 * Returns:
150 * - a pointer to the first matching object
151 * - NULL else
152 */
153 sdb_object_t *
154 sdb_llist_search_by_name(sdb_llist_t *list, const char *key);
156 /*
157 * sdb_llist_remove:
158 * Removes and returns the first matchin element of the list. The ref-count of
159 * the item will not be changed, that is, if the element will not be used any
160 * further, it should be de-referenced by the caller.
161 *
162 * Returns:
163 * - a pointer to the first matching object
164 * - NULL else
165 */
166 sdb_object_t *
167 sdb_llist_remove(sdb_llist_t *list,
168 sdb_llist_lookup_cb lookup, const void *user_data);
170 /*
171 * sdb_llist_shift:
172 * Removes and returns the first element of the list. The ref-count of the
173 * item will not be changed, that is, if the element will not be used any
174 * further, it should be de-referenced by the caller.
175 *
176 * Returns:
177 * - the former first element of the list
178 * - NULL if the list is empty
179 */
180 sdb_object_t *
181 sdb_llist_shift(sdb_llist_t *list);
183 /*
184 * sdb_llist_get_iter, sdb_llist_iter_has_next, sdb_llist_iter_get_next:
185 * Iterate through the list, element by element.
186 *
187 * sdb_llist_iter_get_next returns NULL if there is no next element.
188 */
189 sdb_llist_iter_t *
190 sdb_llist_get_iter(sdb_llist_t *list);
191 void
192 sdb_llist_iter_destroy(sdb_llist_iter_t *iter);
194 _Bool
195 sdb_llist_iter_has_next(sdb_llist_iter_t *iter);
196 sdb_object_t *
197 sdb_llist_iter_get_next(sdb_llist_iter_t *iter);
199 /*
200 * sdb_llist_iter_remove_current:
201 * Remove the current object from the list, that is, the object which was
202 * returned by the last call to sdb_llist_iter_get_next().
203 *
204 * This operation is not safe if another iterator is in use at the same time.
205 *
206 * Returns:
207 * - 0 on success
208 * - a negative value else
209 */
210 int
211 sdb_llist_iter_remove_current(sdb_llist_iter_t *iter);
213 /*
214 * sdb_llist_len:
215 * Return the length (number of elements) of the list.
216 */
217 size_t
218 sdb_llist_len(sdb_llist_t *list);
220 #ifdef __cplusplus
221 } /* extern "C" */
222 #endif
224 #endif /* ! SDB_UTILS_LLIST_H */
226 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */