3 * libEtPan! -- a mail stuff library
5 * clist - Implements simple generic double-linked pointer lists
7 * Copyright (c) 1999-2000, Gaƫl Roualland <gael.roualland@iname.com>
8 * interface changes - 2002 - DINH Viet Hoa
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the libEtPan! project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 lst = (clist *) malloc(sizeof(clist));
47 if (!lst) return NULL;
49 lst->first = lst->last = NULL;
55 void clist_free(clist * lst) {
69 int clist_isempty(clist * lst) {
70 return ((lst->first==lst->last) && (lst->last==NULL));
73 clistiter * clist_begin(clist * lst) {
77 clistiter * clist_end(clist * lst) {
81 clistiter * clist_next(clistiter * iter) {
88 clistiter * clist_previous(clistiter * iter) {
90 return iter->previous;
95 void * clist_content(clistiter * iter) {
102 int clist_count(clist * lst) {
106 int clist_prepend(clist * lst, void * data) {
107 return clist_insert_before(lst, lst->first, data);
110 int clist_append(clist * lst, void * data) {
111 return clist_insert_after(lst, lst->last, data);
115 int clist_insert_before(clist * lst, clistiter * iter, void * data) {
118 c = (clistcell *) malloc(sizeof(clistcell));
124 if (clist_isempty(lst)) {
125 c->previous = c->next = NULL;
126 lst->first = lst->last = c;
131 c->previous = lst->last;
132 c->previous->next = c;
138 c->previous = iter->previous;
140 c->next->previous = c;
142 c->previous->next = c;
149 int clist_insert_after(clist * lst, clistiter * iter, void * data) {
152 c = (clistcell *) malloc(sizeof(clistcell));
158 if (clist_isempty(lst)) {
159 c->previous = c->next = NULL;
160 lst->first = lst->last = c;
165 c->previous = lst->last;
166 c->previous->next = c;
173 c->next = iter->next;
175 c->next->previous = c;
178 c->previous->next = c;
183 clistiter * clist_delete(clist * lst, clistiter * iter) {
186 if (!iter) return NULL;
189 iter->previous->next = iter->next;
191 lst->first = iter->next;
194 iter->next->previous = iter->previous;
197 lst->last = iter->previous;
209 void clist_foreach(clist * lst, clist_func func, void * data)
213 for(cur = clist_begin(lst) ; cur != NULL ; cur = cur->next)
214 func(cur->data, data);
217 void clist_concat(clist * dest, clist * src)
219 if (src->first == NULL) {
222 else if (dest->last == NULL) {
223 dest->first = src->first;
224 dest->last = src->last;
227 dest->last->next = src->first;
228 src->first->previous = dest->last;
229 dest->last = src->last;
232 dest->count += src->count;
233 src->last = src->first = NULL;
236 static inline clistiter * internal_clist_nth(clist * lst, int index)
240 cur = clist_begin(lst);
241 while ((index > 0) && (cur != NULL)) {
252 void * clist_nth_data(clist * lst, int index)
256 cur = internal_clist_nth(lst, index);
263 clistiter * clist_nth(clist * lst, int index)
265 return internal_clist_nth(lst, index);