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 *);
45 /*
46 * sdb_llist_create, sdb_llist_destroy:
47 * Create and destroy a doubly linked list object.
48 *
49 * sdb_llist_create returns NULL on error.
50 * sdb_llist_destroy will also destroy all remaining elements, thus releasing
51 * the included objects (decrement the ref-count).
52 */
53 sdb_llist_t *
54 sdb_llist_create(void);
55 void
56 sdb_llist_destroy(sdb_llist_t *list);
58 /*
59 * sdb_llist_clone:
60 * Clone an existing list. The objects stored in the list will not be copied
61 * but rather their reference count incremented.
62 *
63 * Returns:
64 * - the copied list on success
65 * - NULL else
66 */
67 sdb_llist_t *
68 sdb_llist_clone(sdb_llist_t *list);
70 /*
71 * sdb_llist_append:
72 * Append the given 'obj' to the end of 'list'. The list will take ownership
73 * of the object, that is, increment the reference count by one. In case the
74 * caller does not longer use the object for other purposes, it should thus
75 * deref it.
76 *
77 * Returns:
78 * - 0 on success
79 * - a negative value on failure
80 */
81 int
82 sdb_llist_append(sdb_llist_t *list, sdb_object_t *obj);
84 /*
85 * sdb_llist_insert:
86 * Insert the new element at the specified position (zero being the head of
87 * the list; the length of the list being the tail)). If the index is greater
88 * than the length of the list (i.e. past the tail of the list), an error is
89 * returned. The list will take ownership of the object, that is, increment
90 * the reference count by one. In case the caller does not longer use the
91 * object for other purposes, it should thus deref it.
92 *
93 * Returns:
94 * - 0 on success
95 * - a negative value on failure
96 */
97 int
98 sdb_llist_insert(sdb_llist_t *list, sdb_object_t *obj, size_t idx);
100 /*
101 * sdb_llist_insert_sorted:
102 * Insert the given 'obj' in the 'list' using a sort order as determined by
103 * the 'compare' function. The function will insert the new entry before the
104 * first entry which sorts later than the new entry. It will not ensure that
105 * the rest of the list is sorted. The list will take ownership of the object,
106 * that is, increment the reference count by one. In case the caller does not
107 * longer use the object for other purposes, it should thus deref it.
108 *
109 * The 'compare' function should return less than zero, zero, greater than
110 * zero if the first argument sorts less than, equal or greater than the
111 * second argument respectively.
112 *
113 * Returns:
114 * - 0 on success
115 * - a negative value on failure
116 */
117 int
118 sdb_llist_insert_sorted(sdb_llist_t *list,
119 sdb_object_t *obj, sdb_llist_cmp_cb);
121 /*
122 * sdb_llist_search:
123 * Search for a 'key' in the given 'list'. The function will return the first
124 * entry that matches the specified 'key'. For that purpose, the 'compare'
125 * function is used. It should return 0 iff the two arguments compare equal.
126 *
127 * Returns:
128 * - a pointer to the first matching object
129 * - NULL else
130 */
131 sdb_object_t *
132 sdb_llist_search(sdb_llist_t *list,
133 const sdb_object_t *key, sdb_llist_cmp_cb);
135 /*
136 * sdb_llist_search_by_name:
137 * Search for an object named 'key' in the given 'list'. The function will
138 * return the first entry whose name matches the specified 'key' ignoring the
139 * case of the characters.
140 *
141 * Returns:
142 * - a pointer to the first matching object
143 * - NULL else
144 */
145 sdb_object_t *
146 sdb_llist_search_by_name(sdb_llist_t *list, const char *key);
148 /*
149 * sdb_llist_shift:
150 * Removes and returns the first element of the list. The ref-count of the
151 * item will not be changed, that is, if the element will not be used any
152 * further, it should be de-referenced by the caller.
153 *
154 * Returns:
155 * - the former first element of the list
156 * - NULL if the list is empty
157 */
158 sdb_object_t *
159 sdb_llist_shift(sdb_llist_t *list);
161 /* sdb_llist_get_iter, sdb_llist_iter_has_next, sdb_llist_iter_get_next:
162 * Iterate through the list, element by element.
163 *
164 * sdb_llist_iter_get_next returns NULL if there is no next element.
165 */
166 sdb_llist_iter_t *
167 sdb_llist_get_iter(sdb_llist_t *list);
168 void
169 sdb_llist_iter_destroy(sdb_llist_iter_t *iter);
171 _Bool
172 sdb_llist_iter_has_next(sdb_llist_iter_t *iter);
173 sdb_object_t *
174 sdb_llist_iter_get_next(sdb_llist_iter_t *iter);
176 #ifdef __cplusplus
177 } /* extern "C" */
178 #endif
180 #endif /* ! SDB_UTILS_LLIST_H */
182 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */