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