1 /* $NetBSD: lst.c,v 1.91 2020/10/28 02:43:16 rillig Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
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
37 MAKE_RCSID("$NetBSD: lst.c,v 1.91 2020/10/28 02:43:16 rillig Exp $");
39 #ifdef HAVE_INTTYPES_H
41 #elif defined(HAVE_STDINT_H)
46 LstNodeNew(ListNode *prev, ListNode *next, void *datum)
48 ListNode *node = bmake_malloc(sizeof *node);
55 /* Create and initialize a new, empty list. */
59 List *list = bmake_malloc(sizeof *list);
67 /* Free a list and all its nodes. The node data are not freed though. */
74 for (node = list->first; node != NULL; node = next) {
82 /* Destroy a list and free all its resources. The freeProc is called with the
83 * datum from each node in turn before the node is freed. */
85 Lst_Destroy(List *list, LstFreeProc freeProc)
90 for (node = list->first; node != NULL; node = next) {
92 freeProc(node->datum);
100 * Functions to modify a list
103 /* Insert a new node with the datum before the given node. */
105 Lst_InsertBefore(List *list, ListNode *node, void *datum)
109 assert(datum != NULL);
111 newNode = LstNodeNew(node->prev, node, datum);
113 if (node->prev != NULL)
114 node->prev->next = newNode;
115 node->prev = newNode;
117 if (node == list->first)
118 list->first = newNode;
121 /* Add a piece of data at the start of the given list. */
123 Lst_Prepend(List *list, void *datum)
127 assert(datum != NULL);
129 node = LstNodeNew(NULL, list->first, datum);
131 if (list->first == NULL) {
135 list->first->prev = node;
140 /* Add a piece of data at the end of the given list. */
142 Lst_Append(List *list, void *datum)
146 assert(datum != NULL);
148 node = LstNodeNew(list->last, NULL, datum);
150 if (list->last == NULL) {
154 list->last->next = node;
159 /* Remove the given node from the given list.
160 * The datum stored in the node must be freed by the caller, if necessary. */
162 Lst_Remove(List *list, ListNode *node)
164 /* unlink it from its neighbors */
165 if (node->next != NULL)
166 node->next->prev = node->prev;
167 if (node->prev != NULL)
168 node->prev->next = node->next;
170 /* unlink it from the list */
171 if (list->first == node)
172 list->first = node->next;
173 if (list->last == node)
174 list->last = node->prev;
177 /* Replace the datum in the given node with the new datum. */
179 LstNode_Set(ListNode *node, void *datum)
181 assert(datum != NULL);
186 /* Replace the datum in the given node to NULL.
187 * Having NULL values in a list is unusual though. */
189 LstNode_SetNull(ListNode *node)
195 * Functions for entire lists
198 /* Return the first node that contains the given datum, or NULL. */
200 Lst_FindDatum(List *list, const void *datum)
204 assert(datum != NULL);
206 for (node = list->first; node != NULL; node = node->next)
207 if (node->datum == datum)
214 Lst_ForEachUntil(List *list, LstActionUntilProc proc, void *procData)
219 for (node = list->first; node != NULL; node = node->next) {
220 result = proc(node->datum, procData);
227 /* Move all nodes from list2 to the end of list1.
228 * List2 is destroyed and freed. */
230 Lst_MoveAll(List *list1, List *list2)
232 if (list2->first != NULL) {
233 list2->first->prev = list1->last;
234 if (list1->last != NULL)
235 list1->last->next = list2->first;
237 list1->first = list2->first;
239 list1->last = list2->last;
244 /* Copy the element data from src to the start of dst. */
246 Lst_PrependAll(List *dst, List *src)
249 for (node = src->last; node != NULL; node = node->prev)
250 Lst_Prepend(dst, node->datum);
253 /* Copy the element data from src to the end of dst. */
255 Lst_AppendAll(List *dst, List *src)
258 for (node = src->first; node != NULL; node = node->next)
259 Lst_Append(dst, node->datum);
263 * for using the list as a queue
266 /* Add the datum to the tail of the given list. */
268 Lst_Enqueue(List *list, void *datum)
270 Lst_Append(list, datum);
273 /* Remove and return the datum at the head of the given list. */
275 Lst_Dequeue(List *list)
277 void *datum = list->first->datum;
278 Lst_Remove(list, list->first);
279 assert(datum != NULL); /* since NULL would mean end of the list */
284 Vector_Init(Vector *v, size_t itemSize)
288 v->itemSize = itemSize;
289 v->items = bmake_malloc(v->priv_cap * v->itemSize);
292 /* Add space for a new item to the vector and return a pointer to that space.
293 * The returned data is valid until the next modifying operation. */
295 Vector_Push(Vector *v)
297 if (v->len >= v->priv_cap) {
299 v->items = bmake_realloc(v->items, v->priv_cap * v->itemSize);
302 return Vector_Get(v, v->len - 1);
305 /* Return the pointer to the last item in the vector.
306 * The returned data is valid until the next modifying operation. */
308 Vector_Pop(Vector *v)
312 return Vector_Get(v, v->len);
316 Vector_Done(Vector *v)