1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Authors: Michael Zucchi <notzed@ximian.com>
5 * Copyright 2004 Novell, Inc. (www.novell.com)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2 of the GNU Lesser General Public
9 * License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
29 #include "camel-list-utils.h"
35 * Initialise a double-linked list header. All list headers must be
36 * initialised before use.
38 void camel_dlist_init(CamelDList *v)
40 v->head = (CamelDListNode *)&v->tail;
42 v->tailpred = (CamelDListNode *)&v->head;
46 * camel_dlist_addhead:
47 * @l: An initialised list header.
48 * @n: A node, the next and prev pointers will be overwritten.
50 * Add the list node @n to the head (start) of the list @l.
54 CamelDListNode *camel_dlist_addhead(CamelDList *l, CamelDListNode *n)
57 n->prev = (CamelDListNode *)&l->head;
64 * camel_dlist_addtail:
65 * @l: An intialised list header.
66 * @n: A node, the next and prev pointers will be overwritten.
68 * Add the list onde @n to the tail (end) of the list @l.
72 CamelDListNode *camel_dlist_addtail(CamelDList *l, CamelDListNode *n)
74 n->next = (CamelDListNode *)&l->tail;
75 n->prev = l->tailpred;
76 l->tailpred->next = n;
83 * @n: A node which is part of a list.
85 * Remove @n from the list it's in. @n must belong to a list.
89 CamelDListNode *camel_dlist_remove(CamelDListNode *n)
91 n->next->prev = n->prev;
92 n->prev->next = n->next;
97 * camel_dlist_remhead:
98 * @l: An initialised list, maybe containing items.
100 * Remove the head node (start) of the list.
102 * xReturn value: The previously first-node in the list, or NULLif @l
105 CamelDListNode *camel_dlist_remhead(CamelDList *l)
107 CamelDListNode *n, *nn;
120 * camel_dlist_remtail:
121 * @l: An initialised list, maybe containing items.
123 * Remove the last node in the list.
125 * Return value: The previously last-node in the list, or NULL if @l
128 CamelDListNode *camel_dlist_remtail(CamelDList *l)
130 CamelDListNode *n, *np;
144 * @l: An initialised list header.
146 * Returns %TRUE if @l is an empty list.
148 * Return value: %TRUE if @l is an empty list, %FALSE otherwise.
150 int camel_dlist_empty(CamelDList *l)
152 return (l->head == (CamelDListNode *)&l->tail);
156 * camel_dlist_length:
157 * @l: An initialised list header.
159 * Returns the number of nodes in the list @l.
161 * Return value: The number of nodes.
163 int camel_dlist_length(CamelDList *l)
165 CamelDListNode *n, *nn;
179 /* This is just for orthogonal completeness */
181 void camel_slist_init(CamelSList *v)
186 CamelSListNode *camel_slist_addhead(CamelSList *l, CamelSListNode *n)
194 CamelSListNode *camel_slist_addtail(CamelSList *l, CamelSListNode *n)
198 p = (CamelSListNode *)l;
207 CamelSListNode *camel_slist_remove(CamelSList *l, CamelSListNode *n)
209 CamelSListNode *p, *q;
211 p = (CamelSListNode *)l;
212 while ( (q = p->next) ) {
220 g_warning("Trying to remove SList node not present in SList");
225 CamelSListNode *camel_slist_remhead(CamelSList *l)
236 CamelSListNode *camel_slist_remtail(CamelSList *l)
238 CamelSListNode *n, *p;
243 p = (CamelSListNode *)l;
253 int camel_slist_empty(CamelSList *l)
255 return (l->head == NULL);
258 int camel_slist_length(CamelSList *l)