Code

1dddb7e0747391a677ac267f36560b2d385d2a89
[sysdb.git] / src / include / utils / llist.h
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 /*
44  * sdb_llist_create, sdb_llist_destroy:
45  * Create and destroy a doubly linked list object.
46  *
47  * sdb_llist_create returns NULL on error.
48  * sdb_llist_destroy will also destroy all remaining elements, thus releasing
49  * the included objects (decrement the ref-count).
50  */
51 sdb_llist_t *
52 sdb_llist_create(void);
53 void
54 sdb_llist_destroy(sdb_llist_t *list);
56 /*
57  * sdb_llist_clear:
58  * Remove all elements from the list, releasing the included objects
59  * (decrement the ref-count).
60  */
61 void
62 sdb_llist_clear(sdb_llist_t *list);
64 /*
65  * sdb_llist_clone:
66  * Clone an existing list. The objects stored in the list will not be copied
67  * but rather their reference count incremented.
68  *
69  * Returns:
70  *  - the copied list on success
71  *  - NULL else
72  */
73 sdb_llist_t *
74 sdb_llist_clone(sdb_llist_t *list);
76 /*
77  * sdb_llist_append:
78  * Append the given 'obj' to the end of 'list'. The list will take ownership
79  * of the object, that is, increment the reference count by one. In case the
80  * caller does not longer use the object for other purposes, it should thus
81  * deref it.
82  *
83  * Returns:
84  *  - 0 on success
85  *  - a negative value on failure
86  */
87 int
88 sdb_llist_append(sdb_llist_t *list, sdb_object_t *obj);
90 /*
91  * sdb_llist_insert:
92  * Insert the new element at the specified position (zero being the head of
93  * the list; the length of the list being the tail)). If the index is greater
94  * than the length of the list (i.e. past the tail of the list), an error is
95  * returned. The list will take ownership of the object, that is, increment
96  * the reference count by one. In case the caller does not longer use the
97  * object for other purposes, it should thus deref it.
98  *
99  * Returns:
100  *  - 0 on success
101  *  - a negative value on failure
102  */
103 int
104 sdb_llist_insert(sdb_llist_t *list, sdb_object_t *obj, size_t idx);
106 /*
107  * sdb_llist_insert_sorted:
108  * Insert the given 'obj' in the 'list' using a sort order as determined by
109  * the 'compare' function. The function will insert the new entry before the
110  * first entry which sorts later than the new entry. It will not ensure that
111  * the rest of the list is sorted. The list will take ownership of the object,
112  * that is, increment the reference count by one. In case the caller does not
113  * longer use the object for other purposes, it should thus deref it.
114  *
115  * The 'compare' function should return less than zero, zero, greater than
116  * zero if the first argument sorts less than, equal or greater than the
117  * second argument respectively.
118  *
119  * Returns:
120  *  - 0 on success
121  *  - a negative value on failure
122  */
123 int
124 sdb_llist_insert_sorted(sdb_llist_t *list,
125                 sdb_object_t *obj, sdb_object_cmp_cb);
127 /*
128  * sdb_llist_get:
129  * Returns the i-th element of the list or NULL in case of an error. The
130  * reference count of the element is incremented before returning it to share
131  * ownership between the list and the caller.
132  */
133 sdb_object_t *
134 sdb_llist_get(sdb_llist_t *list, size_t i);
136 /*
137  * sdb_llist_search:
138  * Search for a object in the given 'list'. The function will return the first
139  * entry for which the 'lookup' callback returns 0. The 'user_data' is passed
140  * on to the lookup function on each invocation.
141  *
142  * Returns:
143  *  - a pointer to the first matching object
144  *  - NULL else
145  */
146 sdb_object_t *
147 sdb_llist_search(sdb_llist_t *list,
148                 sdb_object_lookup_cb lookup, const void *user_data);
150 /*
151  * sdb_llist_search_by_name:
152  * Search for an object named 'key' in the given 'list'. The function will
153  * return the first entry whose name matches the specified 'key' ignoring the
154  * case of the characters.
155  *
156  * Returns:
157  *  - a pointer to the first matching object
158  *  - NULL else
159  */
160 sdb_object_t *
161 sdb_llist_search_by_name(sdb_llist_t *list, const char *key);
163 /*
164  * sdb_llist_remove:
165  * Removes and returns the first matching element of the list. The ref-count
166  * of the item will not be changed, that is, if the element will not be used
167  * any further, it should be de-referenced by the caller.
168  *
169  * Returns:
170  *  - a pointer to the first matching object
171  *  - NULL else
172  */
173 sdb_object_t *
174 sdb_llist_remove(sdb_llist_t *list,
175                 sdb_object_lookup_cb lookup, const void *user_data);
177 /*
178  * sdb_llist_remove_by_name:
179  * Removes and returns the first element whose name matches the specified key.
180  * The ref-count of the item will not be changed, that is, if the element will
181  * not be used any further, it should be de-referenced by the caller.
182  *
183  * Returns:
184  *  - a pointer to the first matching object
185  *  - NULL else
186  */
187 sdb_object_t *
188 sdb_llist_remove_by_name(sdb_llist_t *list, const char *key);
190 /*
191  * sdb_llist_shift:
192  * Removes and returns the first element of the list. The ref-count of the
193  * item will not be changed, that is, if the element will not be used any
194  * further, it should be de-referenced by the caller.
195  *
196  * Returns:
197  *  - the former first element of the list
198  *  - NULL if the list is empty
199  */
200 sdb_object_t *
201 sdb_llist_shift(sdb_llist_t *list);
203 /*
204  * sdb_llist_get_iter, sdb_llist_iter_has_next, sdb_llist_iter_get_next:
205  * Iterate through the list, element by element.
206  *
207  * sdb_llist_iter_get_next returns NULL if there is no next element.
208  */
209 sdb_llist_iter_t *
210 sdb_llist_get_iter(sdb_llist_t *list);
211 void
212 sdb_llist_iter_destroy(sdb_llist_iter_t *iter);
214 _Bool
215 sdb_llist_iter_has_next(sdb_llist_iter_t *iter);
216 sdb_object_t *
217 sdb_llist_iter_get_next(sdb_llist_iter_t *iter);
219 /*
220  * sdb_llist_iter_remove_current:
221  * Remove the current object from the list, that is, the object which was
222  * returned by the last call to sdb_llist_iter_get_next().
223  *
224  * This operation is not safe if another iterator is in use at the same time.
225  *
226  * Returns:
227  *  - 0 on success
228  *  - a negative value else
229  */
230 int
231 sdb_llist_iter_remove_current(sdb_llist_iter_t *iter);
233 /*
234  * sdb_llist_len:
235  * Return the length (number of elements) of the list.
236  */
237 size_t
238 sdb_llist_len(sdb_llist_t *list);
240 #ifdef __cplusplus
241 } /* extern "C" */
242 #endif
244 #endif /* ! SDB_UTILS_LLIST_H */
246 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */