1 /* $NetBSD: lst.h,v 1.85 2020/11/10 00:32:12 rillig Exp $ */
4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * from: @(#)lst.h 8.1 (Berkeley) 6/6/93
38 * Copyright (c) 1988, 1989 by Adam de Boor
39 * Copyright (c) 1989 by Berkeley Softworks
40 * All rights reserved.
42 * This code is derived from software contributed to Berkeley by
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. All advertising materials mentioning features or use of this software
54 * must display the following acknowledgement:
55 * This product includes software developed by the University of
56 * California, Berkeley and its contributors.
57 * 4. Neither the name of the University nor the names of its contributors
58 * may be used to endorse or promote products derived from this software
59 * without specific prior written permission.
61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * from: @(#)lst.h 8.1 (Berkeley) 6/6/93
76 /* Doubly-linked lists of arbitrary pointers. */
81 #include <sys/param.h>
85 /* A doubly-linked list of pointers. */
86 typedef struct List List;
87 /* A single node in the doubly-linked list. */
88 typedef struct ListNode ListNode;
91 ListNode *prev; /* previous node in list, or NULL */
92 ListNode *next; /* next node in list, or NULL */
94 void *datum; /* datum associated with this element */
95 const struct GNode *priv_gnode; /* alias, just for debugging */
96 const char *priv_str; /* alias, just for debugging */
101 ListNode *first; /* first node in list */
102 ListNode *last; /* last node in list */
105 /* Free the datum of a node, called before freeing the node itself. */
106 typedef void LstFreeProc(void *);
107 /* An action for Lst_ForEachUntil and Lst_ForEachUntilConcurrent. */
108 typedef int LstActionUntilProc(void *datum, void *args);
110 /* Create or destroy a list */
112 /* Create a new list. */
114 /* Free the list, leaving the node data unmodified. */
115 void Lst_Free(List *);
116 /* Free the list, freeing the node data using the given function. */
117 void Lst_Destroy(List *, LstFreeProc);
119 /* Get information about a list */
122 Lst_IsEmpty(List *list) { return list->first == NULL; }
124 /* Find the first node that contains the given datum, or NULL. */
125 ListNode *Lst_FindDatum(List *, const void *);
129 /* Insert a datum before the given node. */
130 void Lst_InsertBefore(List *, ListNode *, void *);
131 /* Place a datum at the front of the list. */
132 void Lst_Prepend(List *, void *);
133 /* Place a datum at the end of the list. */
134 void Lst_Append(List *, void *);
135 /* Remove the node from the list. */
136 void Lst_Remove(List *, ListNode *);
137 void Lst_PrependAll(List *, List *);
138 void Lst_AppendAll(List *, List *);
139 void Lst_MoveAll(List *, List *);
141 /* Node-specific functions */
143 /* Replace the value of the node. */
144 void LstNode_Set(ListNode *, void *);
145 /* Set the value of the node to NULL. Having NULL in a list is unusual. */
146 void LstNode_SetNull(ListNode *);
148 /* Iterating over a list, using a callback function */
150 /* Run the action for each datum of the list, until the action returns
153 * During this iteration, the list must not be modified structurally. */
154 int Lst_ForEachUntil(List *, LstActionUntilProc, void *);
156 /* Using the list as a queue */
158 /* Add a datum at the tail of the queue. */
159 void Lst_Enqueue(List *, void *);
160 /* Remove the head node of the queue and return its datum. */
161 void *Lst_Dequeue(List *);
163 /* A vector is an ordered collection of items, allowing for fast indexed
165 typedef struct Vector {
166 void *items; /* memory holding the items */
167 size_t itemSize; /* size of a single item in bytes */
168 size_t len; /* number of actually usable elements */
169 size_t priv_cap; /* capacity */
172 void Vector_Init(Vector *, size_t);
174 /* Return the pointer to the given item in the vector.
175 * The returned data is valid until the next modifying operation. */
177 Vector_Get(Vector *v, size_t i)
179 unsigned char *items = v->items;
180 return items + i * v->itemSize;
183 void *Vector_Push(Vector *);
184 void *Vector_Pop(Vector *);
185 void Vector_Done(Vector *);
187 #endif /* MAKE_LST_H */