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