lst.h revision 1.67 1 1.67 rillig /* $NetBSD: lst.h,v 1.67 2020/09/25 04:18:11 rillig Exp $ */
2 1.5 christos
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5 1.10 agc * All rights reserved.
6 1.10 agc *
7 1.10 agc * This code is derived from software contributed to Berkeley by
8 1.10 agc * Adam de Boor.
9 1.10 agc *
10 1.10 agc * Redistribution and use in source and binary forms, with or without
11 1.10 agc * modification, are permitted provided that the following conditions
12 1.10 agc * are met:
13 1.10 agc * 1. Redistributions of source code must retain the above copyright
14 1.10 agc * notice, this list of conditions and the following disclaimer.
15 1.10 agc * 2. Redistributions in binary form must reproduce the above copyright
16 1.10 agc * notice, this list of conditions and the following disclaimer in the
17 1.10 agc * documentation and/or other materials provided with the distribution.
18 1.10 agc * 3. Neither the name of the University nor the names of its contributors
19 1.10 agc * may be used to endorse or promote products derived from this software
20 1.10 agc * without specific prior written permission.
21 1.10 agc *
22 1.10 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.10 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.10 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.10 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.10 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.10 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.10 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.10 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.10 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.10 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.10 agc * SUCH DAMAGE.
33 1.10 agc *
34 1.10 agc * from: @(#)lst.h 8.1 (Berkeley) 6/6/93
35 1.10 agc */
36 1.10 agc
37 1.10 agc /*
38 1.1 cgd * Copyright (c) 1988, 1989 by Adam de Boor
39 1.1 cgd * Copyright (c) 1989 by Berkeley Softworks
40 1.1 cgd * All rights reserved.
41 1.1 cgd *
42 1.1 cgd * This code is derived from software contributed to Berkeley by
43 1.1 cgd * Adam de Boor.
44 1.1 cgd *
45 1.1 cgd * Redistribution and use in source and binary forms, with or without
46 1.1 cgd * modification, are permitted provided that the following conditions
47 1.1 cgd * are met:
48 1.1 cgd * 1. Redistributions of source code must retain the above copyright
49 1.1 cgd * notice, this list of conditions and the following disclaimer.
50 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
51 1.1 cgd * notice, this list of conditions and the following disclaimer in the
52 1.1 cgd * documentation and/or other materials provided with the distribution.
53 1.1 cgd * 3. All advertising materials mentioning features or use of this software
54 1.1 cgd * must display the following acknowledgement:
55 1.1 cgd * This product includes software developed by the University of
56 1.1 cgd * California, Berkeley and its contributors.
57 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
58 1.1 cgd * may be used to endorse or promote products derived from this software
59 1.1 cgd * without specific prior written permission.
60 1.1 cgd *
61 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 1.1 cgd * SUCH DAMAGE.
72 1.1 cgd *
73 1.7 christos * from: @(#)lst.h 8.1 (Berkeley) 6/6/93
74 1.1 cgd */
75 1.1 cgd
76 1.60 rillig /* Doubly-linked lists of arbitrary pointers. */
77 1.60 rillig
78 1.21 rillig #ifndef MAKE_LST_H
79 1.21 rillig #define MAKE_LST_H
80 1.1 cgd
81 1.60 rillig #include <sys/param.h>
82 1.67 rillig #include <stdint.h>
83 1.60 rillig #include <stdlib.h>
84 1.1 cgd
85 1.60 rillig /* A doubly-linked list of pointers. */
86 1.62 rillig typedef struct List List;
87 1.60 rillig /* A single node in the doubly-linked list. */
88 1.62 rillig typedef struct ListNode ListNode;
89 1.1 cgd
90 1.66 rillig struct ListNode {
91 1.66 rillig ListNode *prev; /* previous element in list */
92 1.66 rillig ListNode *next; /* next in list */
93 1.66 rillig uint8_t priv_useCount; /* Count of functions using the node.
94 1.66 rillig * node may not be deleted until count
95 1.66 rillig * goes to 0 */
96 1.66 rillig Boolean priv_deleted; /* List node should be removed when done */
97 1.66 rillig union {
98 1.66 rillig void *datum; /* datum associated with this element */
99 1.66 rillig const struct GNode *priv_gnode; /* alias, just for debugging */
100 1.66 rillig const char *priv_str; /* alias, just for debugging */
101 1.66 rillig };
102 1.66 rillig };
103 1.66 rillig
104 1.66 rillig typedef enum {
105 1.66 rillig Head, Middle, Tail, Unknown
106 1.66 rillig } ListForEachUntilWhere;
107 1.66 rillig
108 1.66 rillig struct List {
109 1.66 rillig ListNode *first; /* first node in list */
110 1.66 rillig ListNode *last; /* last node in list */
111 1.66 rillig
112 1.66 rillig /* fields for sequential access */
113 1.66 rillig Boolean priv_isOpen; /* true if list has been Lst_Open'ed */
114 1.66 rillig ListForEachUntilWhere priv_lastAccess;
115 1.66 rillig ListNode *priv_curr; /* current node, if open. NULL if
116 1.66 rillig * *just* opened */
117 1.66 rillig ListNode *priv_prev; /* Previous node, if open. Used by Lst_Remove */
118 1.66 rillig };
119 1.66 rillig
120 1.60 rillig /* Copy a node, usually by allocating a copy of the given object.
121 1.60 rillig * For reference-counted objects, the original object may need to be
122 1.60 rillig * modified, therefore the parameter is not const. */
123 1.40 rillig typedef void *LstCopyProc(void *);
124 1.60 rillig /* Free the datum of a node, called before freeing the node itself. */
125 1.40 rillig typedef void LstFreeProc(void *);
126 1.60 rillig /* Return TRUE if the datum matches the args, for Lst_Find. */
127 1.60 rillig typedef Boolean LstFindProc(const void *datum, const void *args);
128 1.65 rillig /* An action for Lst_ForEach. */
129 1.65 rillig typedef void LstActionProc(void *datum, void *args);
130 1.64 rillig /* An action for Lst_ForEachUntil. */
131 1.64 rillig typedef int LstActionUntilProc(void *datum, void *args);
132 1.60 rillig
133 1.60 rillig /* Create or destroy a list */
134 1.60 rillig
135 1.60 rillig /* Create a new list. */
136 1.62 rillig List *Lst_Init(void);
137 1.60 rillig /* Duplicate an existing list. */
138 1.62 rillig List *Lst_Copy(List *, LstCopyProc);
139 1.60 rillig /* Free the list, leaving the node data unmodified. */
140 1.62 rillig void Lst_Free(List *);
141 1.60 rillig /* Free the list, freeing the node data using the given function. */
142 1.62 rillig void Lst_Destroy(List *, LstFreeProc);
143 1.60 rillig
144 1.60 rillig /* Get information about a list */
145 1.60 rillig
146 1.66 rillig static inline MAKE_ATTR_UNUSED Boolean
147 1.66 rillig Lst_IsEmpty(List *list) { return list->first == NULL; }
148 1.66 rillig /* Return the first node of the list, or NULL if the list is empty. */
149 1.66 rillig static inline MAKE_ATTR_UNUSED ListNode *
150 1.66 rillig Lst_First(List *list) { return list->first; }
151 1.66 rillig /* Return the last node of the list, or NULL if the list is empty. */
152 1.66 rillig static inline MAKE_ATTR_UNUSED ListNode *
153 1.66 rillig Lst_Last(List *list) { return list->last; }
154 1.60 rillig /* Find the first node for which the function returns TRUE, or NULL. */
155 1.62 rillig ListNode *Lst_Find(List *, LstFindProc, const void *);
156 1.60 rillig /* Find the first node for which the function returns TRUE, or NULL.
157 1.60 rillig * The search starts at the given node, towards the end of the list. */
158 1.62 rillig ListNode *Lst_FindFrom(List *, ListNode *, LstFindProc, const void *);
159 1.60 rillig /* Find the first node that contains the given datum, or NULL. */
160 1.62 rillig ListNode *Lst_FindDatum(List *, const void *);
161 1.60 rillig
162 1.60 rillig /* Modify a list */
163 1.60 rillig
164 1.60 rillig /* Insert a datum before the given node. */
165 1.62 rillig void Lst_InsertBefore(List *, ListNode *, void *);
166 1.60 rillig /* Place a datum at the front of the list. */
167 1.62 rillig void Lst_Prepend(List *, void *);
168 1.60 rillig /* Place a datum at the end of the list. */
169 1.62 rillig void Lst_Append(List *, void *);
170 1.60 rillig /* Remove the node from the list. */
171 1.62 rillig void Lst_Remove(List *, ListNode *);
172 1.62 rillig void Lst_PrependAll(List *, List *);
173 1.62 rillig void Lst_AppendAll(List *, List *);
174 1.62 rillig void Lst_MoveAll(List *, List *);
175 1.60 rillig
176 1.60 rillig /* Node-specific functions */
177 1.60 rillig
178 1.60 rillig /* Return the successor of the node, or NULL. */
179 1.66 rillig static inline MAKE_ATTR_UNUSED ListNode *
180 1.66 rillig LstNode_Next(ListNode *node) { return node->next; }
181 1.60 rillig /* Return the predecessor of the node, or NULL. */
182 1.66 rillig static inline MAKE_ATTR_UNUSED ListNode *
183 1.66 rillig LstNode_Prev(ListNode *node) { return node->prev; }
184 1.60 rillig /* Return the datum of the node. Usually not NULL. */
185 1.66 rillig static inline MAKE_ATTR_UNUSED void *
186 1.66 rillig LstNode_Datum(ListNode *node) { return node->datum; }
187 1.60 rillig /* Replace the value of the node. */
188 1.62 rillig void LstNode_Set(ListNode *, void *);
189 1.60 rillig /* Set the value of the node to NULL. Having NULL in a list is unusual. */
190 1.62 rillig void LstNode_SetNull(ListNode *);
191 1.60 rillig
192 1.60 rillig /* Iterating over a list, using a callback function */
193 1.60 rillig
194 1.65 rillig /* Apply a function to each datum of the list.
195 1.65 rillig * The function must not modify the structure of the list, for example by
196 1.65 rillig * adding or removing nodes. */
197 1.65 rillig void Lst_ForEach(List *, LstActionProc, void *);
198 1.60 rillig /* Apply a function to each datum of the list, until the callback function
199 1.60 rillig * returns non-zero. */
200 1.64 rillig int Lst_ForEachUntil(List *, LstActionUntilProc, void *);
201 1.60 rillig
202 1.60 rillig /* Iterating over a list while keeping track of the current node and possible
203 1.60 rillig * concurrent modifications */
204 1.60 rillig
205 1.60 rillig /* Start iterating the list. */
206 1.62 rillig void Lst_Open(List *);
207 1.60 rillig /* Return the next node, or NULL. */
208 1.62 rillig ListNode *Lst_Next(List *);
209 1.60 rillig /* Finish iterating the list. */
210 1.62 rillig void Lst_Close(List *);
211 1.60 rillig
212 1.60 rillig /* Using the list as a queue */
213 1.60 rillig
214 1.60 rillig /* Add a datum at the tail of the queue. */
215 1.62 rillig void Lst_Enqueue(List *, void *);
216 1.60 rillig /* Remove the head node of the queue and return its datum. */
217 1.62 rillig void *Lst_Dequeue(List *);
218 1.1 cgd
219 1.61 rillig /* A stack is a very simple collection of items that only allows access to the
220 1.61 rillig * top-most item. */
221 1.61 rillig typedef struct {
222 1.61 rillig void **items;
223 1.61 rillig size_t len;
224 1.61 rillig size_t cap;
225 1.61 rillig } Stack;
226 1.61 rillig
227 1.61 rillig void Stack_Init(Stack *);
228 1.61 rillig Boolean Stack_IsEmpty(Stack *);
229 1.61 rillig void Stack_Push(Stack *, void *);
230 1.61 rillig void *Stack_Pop(Stack *);
231 1.61 rillig void Stack_Done(Stack *);
232 1.61 rillig
233 1.21 rillig #endif /* MAKE_LST_H */
234