1/*-
2 * Copyright (c) 1988, 1989, 1990, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 * Copyright (c) 1988, 1989 by Adam de Boor
5 * Copyright (c) 1989 by Berkeley Softworks
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Adam de Boor.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *	This product includes software developed by the University of
22 *	California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 *    may be used to endorse or promote products derived from this software
25 *    without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 *	@(#)lst.h	8.2 (Berkeley) 4/28/95
40 * $FreeBSD$
41 */
42
43#ifndef lst_h_38f3ead1
44#define	lst_h_38f3ead1
45
46/*-
47 * lst.h --
48 *	Header for using the list library
49 */
50
51/*
52 * Structure of a list node.
53 */
54struct LstNode {
55	struct LstNode	*prevPtr;   /* previous element in list */
56	struct LstNode	*nextPtr;   /* next in list */
57	void		*datum;	    /* datum associated with this element */
58};
59typedef	struct	LstNode	LstNode;
60
61/*
62 * The list itself
63 */
64struct Lst {
65	LstNode  	*firstPtr; /* first node in list */
66	LstNode  	*lastPtr;  /* last node in list */
67};
68typedef	struct	Lst Lst;
69
70typedef	void *DuplicateProc(void *);
71typedef	void FreeProc(void *);
72
73/*
74 * NOFREE can be used as the freeProc to Lst_Destroy when the elements are
75 *	not to be freed.
76 * NOCOPY performs similarly when given as the copyProc to Lst_Duplicate.
77 */
78#define	NOFREE		((FreeProc *)NULL)
79#define	NOCOPY		((DuplicateProc *)NULL)
80
81#define	LST_CONCNEW	0   /* create new LstNode's when using Lst_Concat */
82#define	LST_CONCLINK	1   /* relink LstNode's when using Lst_Concat */
83
84/*
85 * Creation/destruction functions
86 */
87/* Create a new list */
88#define	Lst_Init(LST)	do {						\
89				(LST)->firstPtr = NULL;			\
90				(LST)->lastPtr = NULL;			\
91			} while (0)
92#define	Lst_Initializer(NAME)	{ NULL, NULL }
93
94/* Duplicate an existing list */
95void	Lst_Duplicate(Lst *, Lst *, DuplicateProc *);
96
97/* Destroy an old one */
98void	Lst_Destroy(Lst *, FreeProc *);
99
100/*
101 * Functions to modify a list
102 */
103/* Insert an element before another */
104void		Lst_Insert(Lst *, LstNode *, void *);
105/* Insert an element after another */
106void		Lst_Append(Lst *, LstNode *, void *);
107/* Place an element at the front of a lst. */
108#define	Lst_AtFront(LST, D)	(Lst_Insert((LST), Lst_First(LST), (D)))
109/* Place an element at the end of a lst. */
110#define	Lst_AtEnd(LST, D) 	(Lst_Append((LST), Lst_Last(LST), (D)))
111/* Remove an element */
112void		Lst_Remove(Lst *, LstNode *);
113/* Replace a node with a new value */
114#define	Lst_Replace(NODE, D)	((void)((NODE)->datum = (D)))
115/* Concatenate two lists */
116void	Lst_Concat(Lst *, Lst *, int);
117
118/*
119 * Node-specific functions
120 */
121/* Return first element in list */
122#define	Lst_First(LST)	((Lst_Valid(LST) && !Lst_IsEmpty(LST)) \
123			    ? (LST)->firstPtr : NULL)
124/* Return last element in list */
125#define	Lst_Last(LST)	((Lst_Valid(LST) && !Lst_IsEmpty(LST)) \
126			    ? (LST)->lastPtr : NULL)
127/* Return successor to given element */
128#define	Lst_Succ(NODE)	(((NODE) == NULL) ? NULL : (NODE)->nextPtr)
129#define	LST_NEXT(NODE)	((NODE)->nextPtr)
130/* Get datum from LstNode */
131#define	Lst_Datum(NODE)	((NODE)->datum)
132
133/*
134 * Functions for entire lists
135 */
136
137/*
138 * See if the given datum is on the list. Returns the LstNode containing
139 * the datum
140 */
141LstNode		*Lst_Member(Lst *, void *);
142
143/* Loop through a list. Note, that you may not delete the list element. */
144#define	LST_FOREACH(PTR, LST)						\
145	for ((PTR) = (LST)->firstPtr; (PTR) != NULL; (PTR) = (PTR)->nextPtr)
146
147/*
148 * for using the list as a queue
149 */
150/* Place an element at tail of queue */
151#define	Lst_EnQueue(LST, D)	(Lst_Valid(LST) \
152				    ? Lst_Append((LST), Lst_Last(LST), (D)) \
153				    : (void)0)
154/* Remove an element from head of queue */
155void		*Lst_DeQueue(Lst *);
156
157/*
158 * LstValid (L) --
159 *	Return TRUE if the list L is valid
160 */
161#define Lst_Valid(L)	(((L) == NULL) ? FALSE : TRUE)
162
163/*
164 * LstNodeValid (LN, L) --
165 *	Return TRUE if the LstNode LN is valid with respect to L
166 */
167#define Lst_NodeValid(LN, L)	(((LN) == NULL) ? FALSE : TRUE)
168
169/*
170 * Lst_IsEmpty(L) --
171 *	TRUE if the list L is empty.
172 */
173#define Lst_IsEmpty(L)	(!Lst_Valid(L) || (L)->firstPtr == NULL)
174
175#endif /* lst_h_38f3ead1 */
176