lst.h revision 1.25
1#ifndef _LST_H_
2#define _LST_H_
3
4/*	$OpenPackages$ */
5/*	$OpenBSD: lst.h,v 1.25 2003/06/03 02:56:11 millert Exp $ */
6/*	$NetBSD: lst.h,v 1.7 1996/11/06 17:59:12 christos Exp $ */
7
8/*
9 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
10 * Copyright (c) 1988, 1989 by Adam de Boor
11 * Copyright (c) 1989 by Berkeley Softworks
12 * All rights reserved.
13 *
14 * This code is derived from software contributed to Berkeley by
15 * Adam de Boor.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 *    notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 *    notice, this list of conditions and the following disclaimer in the
24 *    documentation and/or other materials provided with the distribution.
25 * 3. Neither the name of the University nor the names of its contributors
26 *    may be used to endorse or promote products derived from this software
27 *    without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 *	from: @(#)lst.h 8.1 (Berkeley) 6/6/93
42 */
43
44/*-
45 * lst.h --
46 *	Header for using the list library
47 */
48
49/* These data structures are PRIVATE !!!
50 * Here for efficiency, so that some functions can be recoded as inlines,
51 * and so that lst headers don't need dynamic allocation most of the time.  */
52struct ListNode_ {
53	struct ListNode_    *prevPtr;	/* previous element in list */
54	struct ListNode_    *nextPtr;	/* next in list */
55	void		    *datum;	/* datum associated with this element */
56};
57
58#ifndef LIST_TYPE
59#include "lst_t.h"
60#endif
61
62typedef void (*SimpleProc)(void *);
63typedef int (*FindProc)(void *, void *);
64typedef int (*FindProcConst)(void *, const void *);
65typedef void (*ForEachProc)(void *, void *);
66typedef void *(*DuplicateProc)(void *);
67
68/*
69 * NOFREE can be used as the freeProc to Lst_Destroy when the elements are
70 *	not to be freed.
71 * NOCOPY performs similarly when given as the copyProc to Lst_Duplicate.
72 */
73#define NOFREE		((SimpleProc) 0)
74#define NOCOPY		((DuplicateProc) 0)
75
76/*
77 * Creation/destruction functions
78 */
79/* Create a new list */
80#define Lst_Init(l)	(l)->firstPtr = (l)->lastPtr = NULL
81/* Static lists are already okay */
82#define Static_Lst_Init(l)
83
84/* Duplicate an existing list */
85extern Lst		Lst_Clone(Lst, Lst, DuplicateProc);
86/* Destroy an old one */
87extern void		Lst_Destroy(LIST *, SimpleProc);
88/* True if list is empty */
89#define 	Lst_IsEmpty(l)	((l)->firstPtr == NULL)
90
91/*
92 * Functions to modify a list
93 */
94/* Insert an element before another */
95extern void		Lst_Insert(Lst, LstNode, void *);
96extern void		Lst_AtFront(Lst, void *);
97/* Insert an element after another */
98extern void		Lst_Append(Lst, LstNode, void *);
99extern void		Lst_AtEnd(Lst, void *);
100/* Remove an element */
101extern void		Lst_Remove(Lst, LstNode);
102/* Replace a node with a new value */
103extern void		Lst_Replace(LstNode, void *);
104/* Concatenate two lists, destructive.	*/
105extern void		Lst_ConcatDestroy(Lst, Lst);
106/* Concatenate two lists, non-destructive.  */
107extern void		Lst_Concat(Lst, Lst);
108
109/*
110 * Node-specific functions
111 */
112/* Return first element in list */
113/* Return last element in list */
114/* Return successor to given element */
115extern LstNode		Lst_Succ(LstNode);
116
117/*
118 * Functions for entire lists
119 */
120/* Find an element starting from somewhere */
121extern LstNode		Lst_FindFrom(LstNode, FindProc, void *);
122/*
123 * See if the given datum is on the list. Returns the LstNode containing
124 * the datum
125 */
126extern LstNode		Lst_Member(Lst, void *);
127/* Apply a function to elements of a lst starting from a certain point.  */
128extern void		Lst_ForEachFrom(LstNode, ForEachProc, void *);
129extern void		Lst_Every(Lst, SimpleProc);
130
131extern bool		Lst_AddNew(Lst, void *);
132/*
133 * for using the list as a queue
134 */
135/* Place an element at tail of queue */
136#define Lst_EnQueue	Lst_AtEnd
137#define Lst_QueueNew	Lst_AddNew
138
139/*
140 * for using the list as a stack
141 */
142#define Lst_Push	Lst_AtFront
143#define Lst_Pop		Lst_DeQueue
144
145/* Remove an element from head of queue */
146extern void *	Lst_DeQueue(Lst);
147
148#define Lst_Datum(ln)	((ln)->datum)
149#define Lst_First(l)	((l)->firstPtr)
150#define Lst_Last(l)	((l)->lastPtr)
151#define Lst_ForEach(l, proc, d) Lst_ForEachFrom(Lst_First(l), proc, d)
152#define Lst_Find(l, cProc, d)	Lst_FindFrom(Lst_First(l), cProc, d)
153#define Lst_Adv(ln)	((ln)->nextPtr)
154#define Lst_Rev(ln)	((ln)->prevPtr)
155
156
157/* Inlines are preferable to macros here because of the type checking. */
158#ifdef HAS_INLINES
159static INLINE LstNode
160Lst_FindConst(Lst l, FindProcConst cProc, const void *d)
161{
162	return Lst_FindFrom(Lst_First(l), (FindProc)cProc, (void *)d);
163}
164
165static INLINE LstNode
166Lst_FindFromConst(LstNode ln, FindProcConst cProc, const void *d)
167{
168	return Lst_FindFrom(ln, (FindProc)cProc, (void *)d);
169}
170#else
171#define Lst_FindConst(l, cProc, d) \
172	Lst_FindFrom(Lst_First(l), (FindProc)cProc, (void *)d)
173#define Lst_FindFromConst(ln, cProc, d) \
174	Lst_FindFrom(ln, (FindProc)cProc, (void *)d)
175#endif
176
177#endif /* _LST_H_ */
178