1 /* $NetBSD: lst.c,v 1.92 2020/11/08 01:29:26 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.92 2020/11/08 01:29:26 rillig Exp $");
39 #ifdef HAVE_INTTYPES_H
41 #elif defined(HAVE_STDINT_H)
46 LstNodeNew(ListNode *prev, ListNode *next, void *datum)
48 ListNode *ln = bmake_malloc(sizeof *ln);
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. */
73 for (ln = list->first; ln != NULL; ln = next) {
81 /* Destroy a list and free all its resources. The freeProc is called with the
82 * datum from each node in turn before the node is freed. */
84 Lst_Destroy(List *list, LstFreeProc freeProc)
88 for (ln = list->first; ln != NULL; ln = next) {
97 /* Insert a new node with the datum before the given node. */
99 Lst_InsertBefore(List *list, ListNode *ln, void *datum)
103 assert(datum != NULL);
105 newNode = LstNodeNew(ln->prev, ln, datum);
107 if (ln->prev != NULL)
108 ln->prev->next = newNode;
111 if (ln == list->first)
112 list->first = newNode;
115 /* Add a piece of data at the start of the given list. */
117 Lst_Prepend(List *list, void *datum)
121 assert(datum != NULL);
123 ln = LstNodeNew(NULL, list->first, datum);
125 if (list->first == NULL) {
129 list->first->prev = ln;
134 /* Add a piece of data at the end of the given list. */
136 Lst_Append(List *list, void *datum)
140 assert(datum != NULL);
142 ln = LstNodeNew(list->last, NULL, datum);
144 if (list->last == NULL) {
148 list->last->next = ln;
153 /* Remove the given node from the given list.
154 * The datum stored in the node must be freed by the caller, if necessary. */
156 Lst_Remove(List *list, ListNode *ln)
158 /* unlink it from its neighbors */
159 if (ln->next != NULL)
160 ln->next->prev = ln->prev;
161 if (ln->prev != NULL)
162 ln->prev->next = ln->next;
164 /* unlink it from the list */
165 if (list->first == ln)
166 list->first = ln->next;
167 if (list->last == ln)
168 list->last = ln->prev;
171 /* Replace the datum in the given node with the new datum. */
173 LstNode_Set(ListNode *ln, void *datum)
175 assert(datum != NULL);
180 /* Replace the datum in the given node with NULL.
181 * Having NULL values in a list is unusual though. */
183 LstNode_SetNull(ListNode *ln)
188 /* Return the first node that contains the given datum, or NULL.
190 * Time complexity: O(length(list)) */
192 Lst_FindDatum(List *list, const void *datum)
196 assert(datum != NULL);
198 for (ln = list->first; ln != NULL; ln = ln->next)
199 if (ln->datum == datum)
206 Lst_ForEachUntil(List *list, LstActionUntilProc proc, void *procData)
211 for (ln = list->first; ln != NULL; ln = ln->next) {
212 result = proc(ln->datum, procData);
219 /* Move all nodes from src to the end of dst.
220 * The source list is destroyed and freed. */
222 Lst_MoveAll(List *dst, List *src)
224 if (src->first != NULL) {
225 src->first->prev = dst->last;
226 if (dst->last != NULL)
227 dst->last->next = src->first;
229 dst->first = src->first;
231 dst->last = src->last;
236 /* Copy the element data from src to the start of dst. */
238 Lst_PrependAll(List *dst, List *src)
241 for (node = src->last; node != NULL; node = node->prev)
242 Lst_Prepend(dst, node->datum);
245 /* Copy the element data from src to the end of dst. */
247 Lst_AppendAll(List *dst, List *src)
250 for (node = src->first; node != NULL; node = node->next)
251 Lst_Append(dst, node->datum);
255 * for using the list as a queue
258 /* Add the datum to the tail of the given list. */
260 Lst_Enqueue(List *list, void *datum)
262 Lst_Append(list, datum);
265 /* Remove and return the datum at the head of the given list. */
267 Lst_Dequeue(List *list)
269 void *datum = list->first->datum;
270 Lst_Remove(list, list->first);
271 assert(datum != NULL); /* since NULL would mean end of the list */
276 Vector_Init(Vector *v, size_t itemSize)
280 v->itemSize = itemSize;
281 v->items = bmake_malloc(v->priv_cap * v->itemSize);
284 /* Add space for a new item to the vector and return a pointer to that space.
285 * The returned data is valid until the next modifying operation. */
287 Vector_Push(Vector *v)
289 if (v->len >= v->priv_cap) {
291 v->items = bmake_realloc(v->items, v->priv_cap * v->itemSize);
294 return Vector_Get(v, v->len - 1);
297 /* Return the pointer to the last item in the vector.
298 * The returned data is valid until the next modifying operation. */
300 Vector_Pop(Vector *v)
304 return Vector_Get(v, v->len);
308 Vector_Done(Vector *v)