Code

354e97665649fa915fdd67e7a08d330d73db5e71
[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_clone:
58  * Clone an existing list. The objects stored in the list will not be copied
59  * but rather their reference count incremented.
60  *
61  * Returns:
62  *  - the copied list on success
63  *  - NULL else
64  */
65 sdb_llist_t *
66 sdb_llist_clone(sdb_llist_t *list);
68 /*
69  * sdb_llist_append:
70  * Append the given 'obj' to the end of 'list'. The list will take ownership
71  * of the object, that is, increment the reference count by one. In case the
72  * caller does not longer use the object for other purposes, it should thus
73  * deref it.
74  *
75  * Returns:
76  *  - 0 on success
77  *  - a negative value on failure
78  */
79 int
80 sdb_llist_append(sdb_llist_t *list, sdb_object_t *obj);
82 /*
83  * sdb_llist_insert:
84  * Insert the new element at the specified position (zero being the head of
85  * the list; the length of the list being the tail)). If the index is greater
86  * than the length of the list (i.e. past the tail of the list), an error is
87  * returned. The list will take ownership of the object, that is, increment
88  * the reference count by one. In case the caller does not longer use the
89  * object for other purposes, it should thus deref it.
90  *
91  * Returns:
92  *  - 0 on success
93  *  - a negative value on failure
94  */
95 int
96 sdb_llist_insert(sdb_llist_t *list, sdb_object_t *obj, size_t index);
98 /*
99  * sdb_llist_insert_sorted:
100  * Insert the given 'obj' in the 'list' using a sort order as determined by
101  * the 'compare' function. The function will insert the new entry before the
102  * first entry which sorts later than the new entry. It will not ensure that
103  * the rest of the list is sorted. The list will take ownership of the object,
104  * that is, increment the reference count by one. In case the caller does not
105  * longer use the object for other purposes, it should thus deref it.
106  *
107  * The 'compare' function should return less than zero, zero, greater than
108  * zero if the first argument sorts less than, equal or greater than the
109  * second argument respectively.
110  *
111  * Returns:
112  *  - 0 on success
113  *  - a negative value on failure
114  */
115 int
116 sdb_llist_insert_sorted(sdb_llist_t *list, sdb_object_t *obj,
117                 int (*compare)(const sdb_object_t *, const sdb_object_t *));
119 /* sdb_llist_search:
120  * Search for a 'key' in the given 'list'. The function will return the first
121  * entry that matches the specified 'key'. For that purpose, the 'compare'
122  * function is used. It should return 0 iff the two arguments compare equal.
123  *
124  * Returns:
125  *  - a pointer the sdb_object_t containing the matching entry
126  *  - NULL else
127  */
128 sdb_object_t *
129 sdb_llist_search(sdb_llist_t *list, const sdb_object_t *key,
130                 int (*compare)(const sdb_object_t *, const sdb_object_t *));
132 /*
133  * sdb_llist_shift:
134  * Removes and returns the first element of the list. The ref-count of the
135  * item will not be changed, that is, if the element will not be used any
136  * further, it should be re-referenced by the caller.
137  *
138  * Returns:
139  *  - the former first element of the list
140  *  - NULL if the list is empty
141  */
142 sdb_object_t *
143 sdb_llist_shift(sdb_llist_t *list);
145 /* sdb_llist_get_iter, sdb_llist_iter_has_next, sdb_llist_iter_get_next:
146  * Iterate through the list, element by element.
147  *
148  * sdb_llist_iter_get_next returns NULL if there is no next element.
149  */
150 sdb_llist_iter_t *
151 sdb_llist_get_iter(sdb_llist_t *list);
152 void
153 sdb_llist_iter_destroy(sdb_llist_iter_t *iter);
155 _Bool
156 sdb_llist_iter_has_next(sdb_llist_iter_t *iter);
157 sdb_object_t *
158 sdb_llist_iter_get_next(sdb_llist_iter_t *iter);
160 #ifdef __cplusplus
161 } /* extern "C" */
162 #endif
164 #endif /* ! SDB_UTILS_LLIST_H */
166 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */