1 /* $NetBSD: lst.c,v 1.104 2021/02/01 19:39:31 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.104 2021/02/01 19:39:31 rillig Exp $");
40 LstNodeNew(ListNode *prev, ListNode *next, void *datum)
42 ListNode *ln = bmake_malloc(sizeof *ln);
51 /* Create and initialize a new, empty list. */
55 List *list = bmake_malloc(sizeof *list);
65 for (ln = list->first; ln != NULL; ln = next) {
72 Lst_DoneCall(List *list, LstFreeProc freeProc)
76 for (ln = list->first; ln != NULL; ln = next) {
83 /* Free a list and all its nodes. The node data are not freed though. */
92 /* Insert a new node with the datum before the given node. */
94 Lst_InsertBefore(List *list, ListNode *ln, void *datum)
98 assert(datum != NULL);
100 newNode = LstNodeNew(ln->prev, ln, datum);
102 if (ln->prev != NULL)
103 ln->prev->next = newNode;
106 if (ln == list->first)
107 list->first = newNode;
110 /* Add a piece of data at the start of the given list. */
112 Lst_Prepend(List *list, void *datum)
116 assert(datum != NULL);
118 ln = LstNodeNew(NULL, list->first, datum);
120 if (list->first == NULL) {
124 list->first->prev = ln;
129 /* Add a piece of data at the end of the given list. */
131 Lst_Append(List *list, void *datum)
135 assert(datum != NULL);
137 ln = LstNodeNew(list->last, NULL, datum);
139 if (list->last == NULL) {
143 list->last->next = ln;
149 * Remove the given node from the given list.
150 * The datum stored in the node must be freed by the caller, if necessary.
153 Lst_Remove(List *list, ListNode *ln)
155 /* unlink it from its neighbors */
156 if (ln->next != NULL)
157 ln->next->prev = ln->prev;
158 if (ln->prev != NULL)
159 ln->prev->next = ln->next;
161 /* unlink it from the list */
162 if (list->first == ln)
163 list->first = ln->next;
164 if (list->last == ln)
165 list->last = ln->prev;
168 /* Replace the datum in the given node with the new datum. */
170 LstNode_Set(ListNode *ln, void *datum)
172 assert(datum != NULL);
178 * Replace the datum in the given node with NULL.
179 * Having NULL values in a list is unusual though.
182 LstNode_SetNull(ListNode *ln)
188 * Return the first node that contains the given datum, or NULL.
190 * Time complexity: O(length(list))
193 Lst_FindDatum(List *list, const void *datum)
197 assert(datum != NULL);
199 for (ln = list->first; ln != NULL; ln = ln->next)
200 if (ln->datum == datum)
207 * Move all nodes from src to the end of dst.
208 * The source list becomes empty but is not freed.
211 Lst_MoveAll(List *dst, List *src)
213 if (src->first != NULL) {
214 src->first->prev = dst->last;
215 if (dst->last != NULL)
216 dst->last->next = src->first;
218 dst->first = src->first;
220 dst->last = src->last;
224 /* Copy the element data from src to the start of dst. */
226 Lst_PrependAll(List *dst, List *src)
230 for (ln = src->last; ln != NULL; ln = ln->prev)
231 Lst_Prepend(dst, ln->datum);
234 /* Copy the element data from src to the end of dst. */
236 Lst_AppendAll(List *dst, List *src)
240 for (ln = src->first; ln != NULL; ln = ln->next)
241 Lst_Append(dst, ln->datum);
244 /* Remove and return the datum at the head of the given list. */
246 Lst_Dequeue(List *list)
248 void *datum = list->first->datum;
249 Lst_Remove(list, list->first);
250 assert(datum != NULL); /* since NULL would mean end of the list */
255 Vector_Init(Vector *v, size_t itemSize)
259 v->itemSize = itemSize;
260 v->items = bmake_malloc(v->cap * v->itemSize);
264 * Add space for a new item to the vector and return a pointer to that space.
265 * The returned data is valid until the next modifying operation.
268 Vector_Push(Vector *v)
270 if (v->len >= v->cap) {
272 v->items = bmake_realloc(v->items, v->cap * v->itemSize);
275 return Vector_Get(v, v->len - 1);
279 * Remove the last item from the vector, return the pointer to it.
280 * The returned data is valid until the next modifying operation.
283 Vector_Pop(Vector *v)
287 return Vector_Get(v, v->len);