1 /* $NetBSD: lst.c,v 1.100 2020/12/04 20:11:48 rillig Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1989, 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Adam de Boor. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 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. 21 * 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 32 * SUCH DAMAGE. 33 */ 34 35 #include "make.h" 36 37 MAKE_RCSID("$NetBSD: lst.c,v 1.100 2020/12/04 20:11:48 rillig Exp $"); 38 39 static ListNode * 40 LstNodeNew(ListNode *prev, ListNode *next, void *datum) 41 { 42 ListNode *ln = bmake_malloc(sizeof *ln); 43 44 ln->prev = prev; 45 ln->next = next; 46 ln->datum = datum; 47 48 return ln; 49 } 50 51 /* Create and initialize a new, empty list. */ 52 List * 53 Lst_New(void) 54 { 55 List *list = bmake_malloc(sizeof *list); 56 Lst_Init(list); 57 return list; 58 } 59 60 void 61 Lst_Done(List *list) 62 { 63 ListNode *ln, *next; 64 65 for (ln = list->first; ln != NULL; ln = next) { 66 next = ln->next; 67 free(ln); 68 } 69 } 70 71 void 72 Lst_DoneCall(List *list, LstFreeProc freeProc) 73 { 74 ListNode *ln, *next; 75 76 for (ln = list->first; ln != NULL; ln = next) { 77 next = ln->next; 78 freeProc(ln->datum); 79 free(ln); 80 } 81 } 82 83 /* Free a list and all its nodes. The node data are not freed though. */ 84 void 85 Lst_Free(List *list) 86 { 87 88 Lst_Done(list); 89 free(list); 90 } 91 92 /* Destroy a list and free all its resources. The freeProc is called with the 93 * datum from each node in turn before the node is freed. */ 94 void 95 Lst_Destroy(List *list, LstFreeProc freeProc) 96 { 97 Lst_DoneCall(list, freeProc); 98 free(list); 99 } 100 101 /* Insert a new node with the datum before the given node. */ 102 void 103 Lst_InsertBefore(List *list, ListNode *ln, void *datum) 104 { 105 ListNode *newNode; 106 107 assert(datum != NULL); 108 109 newNode = LstNodeNew(ln->prev, ln, datum); 110 111 if (ln->prev != NULL) 112 ln->prev->next = newNode; 113 ln->prev = newNode; 114 115 if (ln == list->first) 116 list->first = newNode; 117 } 118 119 /* Add a piece of data at the start of the given list. */ 120 void 121 Lst_Prepend(List *list, void *datum) 122 { 123 ListNode *ln; 124 125 assert(datum != NULL); 126 127 ln = LstNodeNew(NULL, list->first, datum); 128 129 if (list->first == NULL) { 130 list->first = ln; 131 list->last = ln; 132 } else { 133 list->first->prev = ln; 134 list->first = ln; 135 } 136 } 137 138 /* Add a piece of data at the end of the given list. */ 139 void 140 Lst_Append(List *list, void *datum) 141 { 142 ListNode *ln; 143 144 assert(datum != NULL); 145 146 ln = LstNodeNew(list->last, NULL, datum); 147 148 if (list->last == NULL) { 149 list->first = ln; 150 list->last = ln; 151 } else { 152 list->last->next = ln; 153 list->last = ln; 154 } 155 } 156 157 /* Remove the given node from the given list. 158 * The datum stored in the node must be freed by the caller, if necessary. */ 159 void 160 Lst_Remove(List *list, ListNode *ln) 161 { 162 /* unlink it from its neighbors */ 163 if (ln->next != NULL) 164 ln->next->prev = ln->prev; 165 if (ln->prev != NULL) 166 ln->prev->next = ln->next; 167 168 /* unlink it from the list */ 169 if (list->first == ln) 170 list->first = ln->next; 171 if (list->last == ln) 172 list->last = ln->prev; 173 } 174 175 /* Replace the datum in the given node with the new datum. */ 176 void 177 LstNode_Set(ListNode *ln, void *datum) 178 { 179 assert(datum != NULL); 180 181 ln->datum = datum; 182 } 183 184 /* Replace the datum in the given node with NULL. 185 * Having NULL values in a list is unusual though. */ 186 void 187 LstNode_SetNull(ListNode *ln) 188 { 189 ln->datum = NULL; 190 } 191 192 /* Return the first node that contains the given datum, or NULL. 193 * 194 * Time complexity: O(length(list)) */ 195 ListNode * 196 Lst_FindDatum(List *list, const void *datum) 197 { 198 ListNode *ln; 199 200 assert(datum != NULL); 201 202 for (ln = list->first; ln != NULL; ln = ln->next) 203 if (ln->datum == datum) 204 return ln; 205 206 return NULL; 207 } 208 209 /* Move all nodes from src to the end of dst. 210 * The source list becomes empty but is not freed. */ 211 void 212 Lst_MoveAll(List *dst, List *src) 213 { 214 if (src->first != NULL) { 215 src->first->prev = dst->last; 216 if (dst->last != NULL) 217 dst->last->next = src->first; 218 else 219 dst->first = src->first; 220 221 dst->last = src->last; 222 } 223 } 224 225 /* Copy the element data from src to the start of dst. */ 226 void 227 Lst_PrependAll(List *dst, List *src) 228 { 229 ListNode *ln; 230 231 for (ln = src->last; ln != NULL; ln = ln->prev) 232 Lst_Prepend(dst, ln->datum); 233 } 234 235 /* Copy the element data from src to the end of dst. */ 236 void 237 Lst_AppendAll(List *dst, List *src) 238 { 239 ListNode *ln; 240 241 for (ln = src->first; ln != NULL; ln = ln->next) 242 Lst_Append(dst, ln->datum); 243 } 244 245 /* Remove and return the datum at the head of the given list. */ 246 void * 247 Lst_Dequeue(List *list) 248 { 249 void *datum = list->first->datum; 250 Lst_Remove(list, list->first); 251 assert(datum != NULL); /* since NULL would mean end of the list */ 252 return datum; 253 } 254 255 void 256 Vector_Init(Vector *v, size_t itemSize) 257 { 258 v->len = 0; 259 v->priv_cap = 10; 260 v->itemSize = itemSize; 261 v->items = bmake_malloc(v->priv_cap * v->itemSize); 262 } 263 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. */ 266 void * 267 Vector_Push(Vector *v) 268 { 269 if (v->len >= v->priv_cap) { 270 v->priv_cap *= 2; 271 v->items = bmake_realloc(v->items, v->priv_cap * v->itemSize); 272 } 273 v->len++; 274 return Vector_Get(v, v->len - 1); 275 } 276 277 /* Return the pointer to the last item in the vector. 278 * The returned data is valid until the next modifying operation. */ 279 void * 280 Vector_Pop(Vector *v) 281 { 282 assert(v->len > 0); 283 v->len--; 284 return Vector_Get(v, v->len); 285 } 286