lst.c revision 1.77 1 1.77 rillig /* $NetBSD: lst.c,v 1.77 2020/10/18 13:02:10 rillig Exp $ */
2 1.1 rillig
3 1.1 rillig /*
4 1.1 rillig * Copyright (c) 1988, 1989, 1990, 1993
5 1.1 rillig * The Regents of the University of California. All rights reserved.
6 1.1 rillig *
7 1.1 rillig * This code is derived from software contributed to Berkeley by
8 1.1 rillig * Adam de Boor.
9 1.1 rillig *
10 1.1 rillig * Redistribution and use in source and binary forms, with or without
11 1.1 rillig * modification, are permitted provided that the following conditions
12 1.1 rillig * are met:
13 1.1 rillig * 1. Redistributions of source code must retain the above copyright
14 1.1 rillig * notice, this list of conditions and the following disclaimer.
15 1.1 rillig * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 rillig * notice, this list of conditions and the following disclaimer in the
17 1.1 rillig * documentation and/or other materials provided with the distribution.
18 1.1 rillig * 3. Neither the name of the University nor the names of its contributors
19 1.1 rillig * may be used to endorse or promote products derived from this software
20 1.1 rillig * without specific prior written permission.
21 1.1 rillig *
22 1.1 rillig * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 rillig * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 rillig * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 rillig * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 rillig * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 rillig * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 rillig * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 rillig * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 rillig * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 rillig * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 rillig * SUCH DAMAGE.
33 1.1 rillig */
34 1.1 rillig
35 1.19 rillig #include "make.h"
36 1.1 rillig
37 1.77 rillig MAKE_RCSID("$NetBSD: lst.c,v 1.77 2020/10/18 13:02:10 rillig Exp $");
38 1.1 rillig
39 1.22 rillig /* Allocate and initialize a list node.
40 1.22 rillig *
41 1.22 rillig * The fields 'prev' and 'next' must be initialized by the caller.
42 1.22 rillig */
43 1.65 rillig static ListNode *
44 1.12 rillig LstNodeNew(void *datum)
45 1.12 rillig {
46 1.65 rillig ListNode *node = bmake_malloc(sizeof *node);
47 1.70 rillig node->priv_useCount = 0;
48 1.70 rillig node->priv_deleted = FALSE;
49 1.16 rillig node->datum = datum;
50 1.16 rillig return node;
51 1.12 rillig }
52 1.12 rillig
53 1.2 rillig static Boolean
54 1.65 rillig LstIsEmpty(List *list)
55 1.2 rillig {
56 1.16 rillig return list->first == NULL;
57 1.2 rillig }
58 1.1 rillig
59 1.5 rillig /* Create and initialize a new, empty list. */
60 1.65 rillig List *
61 1.77 rillig Lst_New(void)
62 1.1 rillig {
63 1.65 rillig List *list = bmake_malloc(sizeof *list);
64 1.1 rillig
65 1.16 rillig list->first = NULL;
66 1.16 rillig list->last = NULL;
67 1.70 rillig list->priv_isOpen = FALSE;
68 1.70 rillig list->priv_lastAccess = Unknown;
69 1.1 rillig
70 1.16 rillig return list;
71 1.1 rillig }
72 1.1 rillig
73 1.14 rillig /* Duplicate an entire list, usually by copying the datum pointers.
74 1.14 rillig * If copyProc is given, that function is used to create the new datum from the
75 1.35 rillig * old datum, usually by creating a copy of it. */
76 1.65 rillig List *
77 1.65 rillig Lst_Copy(List *list, LstCopyProc copyProc)
78 1.1 rillig {
79 1.65 rillig List *newList;
80 1.65 rillig ListNode *node;
81 1.1 rillig
82 1.77 rillig newList = Lst_New();
83 1.1 rillig
84 1.35 rillig for (node = list->first; node != NULL; node = node->next) {
85 1.35 rillig void *datum = copyProc != NULL ? copyProc(node->datum) : node->datum;
86 1.50 rillig Lst_Append(newList, datum);
87 1.1 rillig }
88 1.1 rillig
89 1.16 rillig return newList;
90 1.1 rillig }
91 1.1 rillig
92 1.42 rillig /* Free a list and all its nodes. The list data itself are not freed though. */
93 1.42 rillig void
94 1.65 rillig Lst_Free(List *list)
95 1.42 rillig {
96 1.65 rillig ListNode *node;
97 1.65 rillig ListNode *next;
98 1.42 rillig
99 1.42 rillig for (node = list->first; node != NULL; node = next) {
100 1.42 rillig next = node->next;
101 1.42 rillig free(node);
102 1.42 rillig }
103 1.42 rillig
104 1.42 rillig free(list);
105 1.42 rillig }
106 1.42 rillig
107 1.59 rillig /* Destroy a list and free all its resources. The freeProc is called with the
108 1.59 rillig * datum from each node in turn before the node is freed. */
109 1.1 rillig void
110 1.65 rillig Lst_Destroy(List *list, LstFreeProc freeProc)
111 1.1 rillig {
112 1.65 rillig ListNode *node;
113 1.65 rillig ListNode *next;
114 1.1 rillig
115 1.42 rillig for (node = list->first; node != NULL; node = next) {
116 1.42 rillig next = node->next;
117 1.42 rillig freeProc(node->datum);
118 1.42 rillig free(node);
119 1.1 rillig }
120 1.1 rillig
121 1.1 rillig free(list);
122 1.1 rillig }
123 1.1 rillig
124 1.1 rillig /*
125 1.1 rillig * Functions to modify a list
126 1.1 rillig */
127 1.1 rillig
128 1.14 rillig /* Insert a new node with the given piece of data before the given node in the
129 1.14 rillig * given list. */
130 1.26 rillig void
131 1.65 rillig Lst_InsertBefore(List *list, ListNode *node, void *datum)
132 1.26 rillig {
133 1.65 rillig ListNode *newNode;
134 1.26 rillig
135 1.26 rillig assert(!LstIsEmpty(list));
136 1.26 rillig assert(datum != NULL);
137 1.26 rillig
138 1.26 rillig newNode = LstNodeNew(datum);
139 1.26 rillig newNode->prev = node->prev;
140 1.26 rillig newNode->next = node;
141 1.26 rillig
142 1.26 rillig if (node->prev != NULL) {
143 1.26 rillig node->prev->next = newNode;
144 1.26 rillig }
145 1.26 rillig node->prev = newNode;
146 1.26 rillig
147 1.26 rillig if (node == list->first) {
148 1.26 rillig list->first = newNode;
149 1.26 rillig }
150 1.26 rillig }
151 1.26 rillig
152 1.22 rillig /* Add a piece of data at the start of the given list. */
153 1.22 rillig void
154 1.65 rillig Lst_Prepend(List *list, void *datum)
155 1.22 rillig {
156 1.65 rillig ListNode *node;
157 1.22 rillig
158 1.22 rillig assert(datum != NULL);
159 1.22 rillig
160 1.22 rillig node = LstNodeNew(datum);
161 1.22 rillig node->prev = NULL;
162 1.22 rillig node->next = list->first;
163 1.22 rillig
164 1.22 rillig if (list->first == NULL) {
165 1.22 rillig list->first = node;
166 1.22 rillig list->last = node;
167 1.22 rillig } else {
168 1.22 rillig list->first->prev = node;
169 1.22 rillig list->first = node;
170 1.22 rillig }
171 1.22 rillig }
172 1.22 rillig
173 1.21 rillig /* Add a piece of data at the end of the given list. */
174 1.21 rillig void
175 1.65 rillig Lst_Append(List *list, void *datum)
176 1.21 rillig {
177 1.65 rillig ListNode *node;
178 1.21 rillig
179 1.21 rillig assert(datum != NULL);
180 1.21 rillig
181 1.21 rillig node = LstNodeNew(datum);
182 1.21 rillig node->prev = list->last;
183 1.21 rillig node->next = NULL;
184 1.21 rillig
185 1.21 rillig if (list->last == NULL) {
186 1.21 rillig list->first = node;
187 1.21 rillig list->last = node;
188 1.21 rillig } else {
189 1.21 rillig list->last->next = node;
190 1.21 rillig list->last = node;
191 1.21 rillig }
192 1.21 rillig }
193 1.21 rillig
194 1.8 rillig /* Remove the given node from the given list.
195 1.8 rillig * The datum stored in the node must be freed by the caller, if necessary. */
196 1.8 rillig void
197 1.65 rillig Lst_Remove(List *list, ListNode *node)
198 1.1 rillig {
199 1.1 rillig /*
200 1.1 rillig * unlink it from the list
201 1.1 rillig */
202 1.16 rillig if (node->next != NULL) {
203 1.16 rillig node->next->prev = node->prev;
204 1.1 rillig }
205 1.16 rillig if (node->prev != NULL) {
206 1.16 rillig node->prev->next = node->next;
207 1.1 rillig }
208 1.1 rillig
209 1.1 rillig /*
210 1.15 rillig * if either the first or last of the list point to this node,
211 1.1 rillig * adjust them accordingly
212 1.1 rillig */
213 1.16 rillig if (list->first == node) {
214 1.16 rillig list->first = node->next;
215 1.1 rillig }
216 1.16 rillig if (list->last == node) {
217 1.16 rillig list->last = node->prev;
218 1.1 rillig }
219 1.1 rillig
220 1.1 rillig /*
221 1.1 rillig * Sequential access stuff. If the node we're removing is the current
222 1.1 rillig * node in the list, reset the current node to the previous one. If the
223 1.15 rillig * previous one was non-existent (prev == NULL), we set the
224 1.1 rillig * end to be Unknown, since it is.
225 1.1 rillig */
226 1.70 rillig if (list->priv_isOpen && list->priv_curr == node) {
227 1.70 rillig list->priv_curr = list->priv_prev;
228 1.70 rillig if (list->priv_curr == NULL) {
229 1.70 rillig list->priv_lastAccess = Unknown;
230 1.1 rillig }
231 1.1 rillig }
232 1.1 rillig
233 1.1 rillig /*
234 1.1 rillig * note that the datum is unmolested. The caller must free it as
235 1.1 rillig * necessary and as expected.
236 1.1 rillig */
237 1.70 rillig if (node->priv_useCount == 0) {
238 1.16 rillig free(node);
239 1.1 rillig } else {
240 1.70 rillig node->priv_deleted = TRUE;
241 1.1 rillig }
242 1.1 rillig }
243 1.1 rillig
244 1.8 rillig /* Replace the datum in the given node with the new datum. */
245 1.8 rillig void
246 1.65 rillig LstNode_Set(ListNode *node, void *datum)
247 1.1 rillig {
248 1.37 rillig assert(datum != NULL);
249 1.37 rillig
250 1.16 rillig node->datum = datum;
251 1.1 rillig }
252 1.1 rillig
253 1.74 rillig /* Replace the datum in the given node to NULL.
254 1.74 rillig * Having NULL values in a list is unusual though. */
255 1.37 rillig void
256 1.65 rillig LstNode_SetNull(ListNode *node)
257 1.37 rillig {
258 1.37 rillig node->datum = NULL;
259 1.37 rillig }
260 1.37 rillig
261 1.1 rillig
262 1.1 rillig /*
263 1.1 rillig * Functions for entire lists
264 1.1 rillig */
265 1.1 rillig
266 1.53 rillig /* Return the first node from the list for which the match function returns
267 1.53 rillig * TRUE, or NULL if none of the nodes matched. */
268 1.65 rillig ListNode *
269 1.65 rillig Lst_Find(List *list, LstFindProc match, const void *matchArgs)
270 1.53 rillig {
271 1.55 rillig return Lst_FindFrom(list, Lst_First(list), match, matchArgs);
272 1.53 rillig }
273 1.53 rillig
274 1.53 rillig /* Return the first node from the list, starting at the given node, for which
275 1.53 rillig * the match function returns TRUE, or NULL if none of the nodes matches.
276 1.53 rillig *
277 1.72 rillig * The start node may be NULL, in which case nothing is found. */
278 1.65 rillig ListNode *
279 1.65 rillig Lst_FindFrom(List *list, ListNode *node, LstFindProc match, const void *matchArgs)
280 1.53 rillig {
281 1.65 rillig ListNode *tln;
282 1.53 rillig
283 1.53 rillig assert(list != NULL);
284 1.53 rillig assert(match != NULL);
285 1.53 rillig
286 1.53 rillig for (tln = node; tln != NULL; tln = tln->next) {
287 1.53 rillig if (match(tln->datum, matchArgs))
288 1.53 rillig return tln;
289 1.53 rillig }
290 1.53 rillig
291 1.53 rillig return NULL;
292 1.53 rillig }
293 1.53 rillig
294 1.14 rillig /* Return the first node that contains the given datum, or NULL. */
295 1.65 rillig ListNode *
296 1.65 rillig Lst_FindDatum(List *list, const void *datum)
297 1.1 rillig {
298 1.65 rillig ListNode *node;
299 1.1 rillig
300 1.29 rillig assert(datum != NULL);
301 1.1 rillig
302 1.29 rillig for (node = list->first; node != NULL; node = node->next) {
303 1.16 rillig if (node->datum == datum) {
304 1.16 rillig return node;
305 1.1 rillig }
306 1.29 rillig }
307 1.1 rillig
308 1.1 rillig return NULL;
309 1.1 rillig }
310 1.1 rillig
311 1.69 rillig void
312 1.69 rillig Lst_ForEach(List *list, LstActionProc proc, void *procData)
313 1.69 rillig {
314 1.69 rillig ListNode *node;
315 1.69 rillig for (node = list->first; node != NULL; node = node->next)
316 1.75 rillig proc(node->datum, procData);
317 1.69 rillig }
318 1.69 rillig
319 1.14 rillig /* Apply the given function to each element of the given list. The function
320 1.14 rillig * should return 0 if traversal should continue and non-zero if it should
321 1.14 rillig * abort. */
322 1.1 rillig int
323 1.67 rillig Lst_ForEachUntil(List *list, LstActionUntilProc proc, void *procData)
324 1.42 rillig {
325 1.68 rillig ListNode *tln = list->first;
326 1.68 rillig int result = 0;
327 1.1 rillig
328 1.68 rillig while (tln != NULL) {
329 1.1 rillig /*
330 1.1 rillig * Take care of having the current element deleted out from under
331 1.1 rillig * us.
332 1.1 rillig */
333 1.73 rillig ListNode *next = tln->next;
334 1.1 rillig
335 1.1 rillig /*
336 1.1 rillig * We're done with the traversal if
337 1.38 rillig * - the next node to examine doesn't exist and
338 1.1 rillig * - nothing's been added after the current node (check this
339 1.1 rillig * after proc() has been called).
340 1.1 rillig */
341 1.68 rillig Boolean done = next == NULL;
342 1.1 rillig
343 1.70 rillig tln->priv_useCount++;
344 1.74 rillig result = proc(tln->datum, procData);
345 1.70 rillig tln->priv_useCount--;
346 1.1 rillig
347 1.1 rillig /*
348 1.1 rillig * Now check whether a node has been added.
349 1.1 rillig * Note: this doesn't work if this node was deleted before
350 1.1 rillig * the new node was added.
351 1.1 rillig */
352 1.15 rillig if (next != tln->next) {
353 1.15 rillig next = tln->next;
354 1.4 rillig done = 0;
355 1.1 rillig }
356 1.1 rillig
357 1.70 rillig if (tln->priv_deleted) {
358 1.1 rillig free((char *)tln);
359 1.1 rillig }
360 1.1 rillig tln = next;
361 1.68 rillig if (result || LstIsEmpty(list) || done)
362 1.68 rillig break;
363 1.68 rillig }
364 1.1 rillig
365 1.1 rillig return result;
366 1.1 rillig }
367 1.1 rillig
368 1.34 rillig /* Move all nodes from list2 to the end of list1.
369 1.34 rillig * List2 is destroyed and freed. */
370 1.34 rillig void
371 1.65 rillig Lst_MoveAll(List *list1, List *list2)
372 1.1 rillig {
373 1.34 rillig if (list2->first != NULL) {
374 1.34 rillig list2->first->prev = list1->last;
375 1.34 rillig if (list1->last != NULL) {
376 1.34 rillig list1->last->next = list2->first;
377 1.34 rillig } else {
378 1.34 rillig list1->first = list2->first;
379 1.1 rillig }
380 1.34 rillig list1->last = list2->last;
381 1.1 rillig }
382 1.34 rillig free(list2);
383 1.1 rillig }
384 1.1 rillig
385 1.22 rillig /* Copy the element data from src to the start of dst. */
386 1.22 rillig void
387 1.65 rillig Lst_PrependAll(List *dst, List *src)
388 1.22 rillig {
389 1.65 rillig ListNode *node;
390 1.22 rillig for (node = src->last; node != NULL; node = node->prev)
391 1.50 rillig Lst_Prepend(dst, node->datum);
392 1.22 rillig }
393 1.22 rillig
394 1.22 rillig /* Copy the element data from src to the end of dst. */
395 1.22 rillig void
396 1.65 rillig Lst_AppendAll(List *dst, List *src)
397 1.22 rillig {
398 1.65 rillig ListNode *node;
399 1.22 rillig for (node = src->first; node != NULL; node = node->next)
400 1.50 rillig Lst_Append(dst, node->datum);
401 1.22 rillig }
402 1.1 rillig
403 1.1 rillig /*
404 1.1 rillig * these functions are for dealing with a list as a table, of sorts.
405 1.1 rillig * An idea of the "current element" is kept and used by all the functions
406 1.1 rillig * between Lst_Open() and Lst_Close().
407 1.1 rillig *
408 1.1 rillig * The sequential functions access the list in a slightly different way.
409 1.1 rillig * CurPtr points to their idea of the current node in the list and they
410 1.1 rillig * access the list based on it.
411 1.1 rillig */
412 1.1 rillig
413 1.14 rillig /* Open a list for sequential access. A list can still be searched, etc.,
414 1.14 rillig * without confusing these functions. */
415 1.10 rillig void
416 1.65 rillig Lst_Open(List *list)
417 1.10 rillig {
418 1.70 rillig assert(!list->priv_isOpen);
419 1.10 rillig
420 1.70 rillig list->priv_isOpen = TRUE;
421 1.70 rillig list->priv_lastAccess = LstIsEmpty(list) ? Head : Unknown;
422 1.70 rillig list->priv_curr = NULL;
423 1.10 rillig }
424 1.10 rillig
425 1.10 rillig /* Return the next node for the given list, or NULL if the end has been
426 1.10 rillig * reached. */
427 1.65 rillig ListNode *
428 1.65 rillig Lst_Next(List *list)
429 1.1 rillig {
430 1.65 rillig ListNode *node;
431 1.1 rillig
432 1.70 rillig assert(list->priv_isOpen);
433 1.1 rillig
434 1.70 rillig list->priv_prev = list->priv_curr;
435 1.1 rillig
436 1.70 rillig if (list->priv_curr == NULL) {
437 1.70 rillig if (list->priv_lastAccess == Unknown) {
438 1.1 rillig /*
439 1.15 rillig * If we're just starting out, lastAccess will be Unknown.
440 1.1 rillig * Then we want to start this thing off in the right
441 1.15 rillig * direction -- at the start with lastAccess being Middle.
442 1.1 rillig */
443 1.70 rillig list->priv_curr = node = list->first;
444 1.70 rillig list->priv_lastAccess = Middle;
445 1.1 rillig } else {
446 1.16 rillig node = NULL;
447 1.70 rillig list->priv_lastAccess = Tail;
448 1.1 rillig }
449 1.1 rillig } else {
450 1.70 rillig node = list->priv_curr->next;
451 1.70 rillig list->priv_curr = node;
452 1.1 rillig
453 1.16 rillig if (node == list->first || node == NULL) {
454 1.1 rillig /*
455 1.1 rillig * If back at the front, then we've hit the end...
456 1.1 rillig */
457 1.70 rillig list->priv_lastAccess = Tail;
458 1.1 rillig } else {
459 1.1 rillig /*
460 1.1 rillig * Reset to Middle if gone past first.
461 1.1 rillig */
462 1.70 rillig list->priv_lastAccess = Middle;
463 1.1 rillig }
464 1.1 rillig }
465 1.1 rillig
466 1.16 rillig return node;
467 1.1 rillig }
468 1.1 rillig
469 1.10 rillig /* Close a list which was opened for sequential access. */
470 1.1 rillig void
471 1.65 rillig Lst_Close(List *list)
472 1.1 rillig {
473 1.70 rillig assert(list->priv_isOpen);
474 1.1 rillig
475 1.70 rillig list->priv_isOpen = FALSE;
476 1.70 rillig list->priv_lastAccess = Unknown;
477 1.1 rillig }
478 1.1 rillig
479 1.1 rillig
480 1.1 rillig /*
481 1.1 rillig * for using the list as a queue
482 1.1 rillig */
483 1.1 rillig
484 1.14 rillig /* Add the datum to the tail of the given list. */
485 1.25 rillig void
486 1.65 rillig Lst_Enqueue(List *list, void *datum)
487 1.1 rillig {
488 1.50 rillig Lst_Append(list, datum);
489 1.1 rillig }
490 1.1 rillig
491 1.25 rillig /* Remove and return the datum at the head of the given list. */
492 1.1 rillig void *
493 1.65 rillig Lst_Dequeue(List *list)
494 1.1 rillig {
495 1.74 rillig void *datum = list->first->datum;
496 1.50 rillig Lst_Remove(list, list->first);
497 1.74 rillig assert(datum != NULL); /* since NULL would mean end of the list */
498 1.16 rillig return datum;
499 1.1 rillig }
500 1.61 rillig
501 1.61 rillig void
502 1.76 rillig Vector_Init(Vector *v)
503 1.61 rillig {
504 1.76 rillig v->len = 0;
505 1.76 rillig v->cap = 10;
506 1.76 rillig v->items = bmake_malloc(v->cap * sizeof v->items[0]);
507 1.61 rillig }
508 1.61 rillig
509 1.76 rillig Boolean Vector_IsEmpty(Vector *v)
510 1.61 rillig {
511 1.76 rillig return v->len == 0;
512 1.61 rillig }
513 1.61 rillig
514 1.76 rillig void Vector_Push(Vector *v, void *datum)
515 1.61 rillig {
516 1.76 rillig if (v->len >= v->cap) {
517 1.76 rillig v->cap *= 2;
518 1.76 rillig v->items = bmake_realloc(v->items,
519 1.76 rillig v->cap * sizeof v->items[0]);
520 1.61 rillig }
521 1.76 rillig v->items[v->len] = datum;
522 1.76 rillig v->len++;
523 1.61 rillig }
524 1.61 rillig
525 1.76 rillig void *Vector_Pop(Vector *v)
526 1.61 rillig {
527 1.64 rillig void *datum;
528 1.64 rillig
529 1.76 rillig assert(v->len > 0);
530 1.76 rillig v->len--;
531 1.76 rillig datum = v->items[v->len];
532 1.64 rillig #ifdef CLEANUP
533 1.76 rillig v->items[v->len] = NULL;
534 1.64 rillig #endif
535 1.64 rillig return datum;
536 1.61 rillig }
537 1.61 rillig
538 1.76 rillig void Vector_Done(Vector *v)
539 1.61 rillig {
540 1.76 rillig free(v->items);
541 1.61 rillig }
542