2 * Authors: Michael Zucchi <notzed@ximian.com>
4 * Copyright 2004 Novell, Inc. (www.novell.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU Lesser General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 #ifndef _CAMEL_LIST_UTILS_H
23 #define _CAMEL_LIST_UTILS_H
27 /* This is a copy of Amiga's Exec lists, the head and tail nodes are
28 * overlapped and merged into a single list header. All operations
29 * are O(1), including node removal and addition from/to either end of
30 * the list. It can be used to implement O(1) queues, fifo's and
31 * normal lists. You don't need the list header to remove the node. */
33 typedef struct _CamelDList CamelDList;
34 typedef struct _CamelDListNode CamelDListNode;
37 * struct _CamelDListNode - A double-linked list node.
39 * @next: The next node link.
40 * @prev: The previous node link.
42 * A double-linked list node header. Put this at the start of the
43 * list node structure. Data is stored in the list by subclassing the
44 * node header rather than using a pointer. Or more normally by just
45 * duplicating the next and previous pointers for better type safety.
47 struct _CamelDListNode {
48 struct _CamelDListNode *next;
49 struct _CamelDListNode *prev;
53 * struct _CamelDList - A double-linked list header.
55 * @head: The head node's next pointer.
56 * @tail: The tail node's next pointer.
57 * @tailpred: The previous node to the tail node.
59 * This is the merging of two separate head and tail nodes into a
60 * single structure. i.e. if you ahve a NULL terminated head and tail
61 * node such as head = { first, NULL } and tail = { NULL, last } then
62 * overlap them at the common NULL, you get this structure.
64 * The list header must be initialised with camel_dlist_init, or by
65 * using the static CAMEL_DLIST_INITIALISER macro.
68 struct _CamelDListNode *head;
69 struct _CamelDListNode *tail;
70 struct _CamelDListNode *tailpred;
73 #define CAMEL_DLIST_INITIALISER(l) { (CamelDListNode *)&l.tail, 0, (CamelDListNode *)&l.head }
75 void camel_dlist_init(CamelDList *v);
76 CamelDListNode *camel_dlist_addhead(CamelDList *l, CamelDListNode *n);
77 CamelDListNode *camel_dlist_addtail(CamelDList *l, CamelDListNode *n);
78 CamelDListNode *camel_dlist_remove(CamelDListNode *n);
79 CamelDListNode *camel_dlist_remhead(CamelDList *l);
80 CamelDListNode *camel_dlist_remtail(CamelDList *l);
81 int camel_dlist_empty(CamelDList *l);
82 int camel_dlist_length(CamelDList *l);
84 /* This is provided mostly for orthogonality with the dlist structure.
85 * By making the nodes contain all of the data themselves it
86 * simplifies memory management. Removing and adding from/to the head
87 * of the list is O(1), the rest of the operations are O(n). */
89 typedef struct _CamelSListNode CamelSListNode;
90 typedef struct _CamelSList CamelSList;
93 * struct _CamelSListNode - A single-linked list node.
95 * @next: The next node in the list.
97 * A single-linked list node header. Put this at hte start of the
98 * actual list node structure, or more commonly, just a next pointer.
99 * Data is stored in the list node by subclassing the node-header
100 * rather than using a pointer.
102 struct _CamelSListNode {
103 struct _CamelSListNode *next;
107 * struct _CamelSList - A single-linked list header.
109 * @head: The head of the list.
111 * This is the header of a single-linked list.
114 struct _CamelSListNode *head;
117 #define CAMEL_SLIST_INITIALISER(l) { 0 }
119 void camel_slist_init(CamelSList *l);
120 CamelSListNode *camel_slist_addhead(CamelSList *l, CamelSListNode *n);
121 CamelSListNode *camel_slist_addtail(CamelSList *l, CamelSListNode *n);
122 CamelSListNode *camel_slist_remove(CamelSList *l, CamelSListNode *n);
123 CamelSListNode *camel_slist_remhead(CamelSList *l);
124 CamelSListNode *camel_slist_remtail(CamelSList *l);
125 int camel_slist_empty(CamelSList *l);
126 int camel_slist_length(CamelSList *l);