Home | History | Annotate | Line # | Download | only in make
lst.c revision 1.69
      1  1.69  rillig /* $NetBSD: lst.c,v 1.69 2020/09/24 07:32:03 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.30   skrll #include <stdint.h>
     36   1.8  rillig 
     37  1.19  rillig #include "make.h"
     38   1.1  rillig 
     39  1.69  rillig MAKE_RCSID("$NetBSD: lst.c,v 1.69 2020/09/24 07:32:03 rillig Exp $");
     40   1.1  rillig 
     41  1.13  rillig struct ListNode {
     42  1.15  rillig     struct ListNode *prev;	/* previous element in list */
     43  1.15  rillig     struct ListNode *next;	/* next in list */
     44   1.7  rillig     uint8_t useCount;		/* Count of functions using the node.
     45   1.4  rillig 				 * node may not be deleted until count
     46   1.4  rillig 				 * goes to 0 */
     47   1.7  rillig     Boolean deleted;		/* List node should be removed when done */
     48  1.39  rillig     union {
     49  1.39  rillig 	void *datum;		/* datum associated with this element */
     50  1.39  rillig 	const GNode *gnode;	/* alias, just for debugging */
     51  1.39  rillig 	const char *str;	/* alias, just for debugging */
     52  1.39  rillig     };
     53  1.13  rillig };
     54   1.1  rillig 
     55   1.1  rillig typedef enum {
     56   1.1  rillig     Head, Middle, Tail, Unknown
     57   1.1  rillig } Where;
     58   1.1  rillig 
     59  1.13  rillig struct List {
     60  1.65  rillig     ListNode *first;		/* first node in list */
     61  1.65  rillig     ListNode *last;		/* last node in list */
     62  1.20  rillig 
     63  1.20  rillig     /* fields for sequential access */
     64  1.21  rillig     Boolean isOpen;		/* true if list has been Lst_Open'ed */
     65  1.15  rillig     Where lastAccess;		/* Where in the list the last access was */
     66  1.65  rillig     ListNode *curr;		/* current node, if open. NULL if
     67   1.4  rillig 				 * *just* opened */
     68  1.65  rillig     ListNode *prev;		/* Previous node, if open. Used by Lst_Remove */
     69  1.13  rillig };
     70   1.1  rillig 
     71  1.22  rillig /* Allocate and initialize a list node.
     72  1.22  rillig  *
     73  1.22  rillig  * The fields 'prev' and 'next' must be initialized by the caller.
     74  1.22  rillig  */
     75  1.65  rillig static ListNode *
     76  1.12  rillig LstNodeNew(void *datum)
     77  1.12  rillig {
     78  1.65  rillig     ListNode *node = bmake_malloc(sizeof *node);
     79  1.16  rillig     node->useCount = 0;
     80  1.16  rillig     node->deleted = FALSE;
     81  1.16  rillig     node->datum = datum;
     82  1.16  rillig     return node;
     83  1.12  rillig }
     84  1.12  rillig 
     85   1.2  rillig static Boolean
     86  1.65  rillig LstIsEmpty(List *list)
     87   1.2  rillig {
     88  1.16  rillig     return list->first == NULL;
     89   1.2  rillig }
     90   1.1  rillig 
     91   1.5  rillig /* Create and initialize a new, empty list. */
     92  1.65  rillig List *
     93   1.5  rillig Lst_Init(void)
     94   1.1  rillig {
     95  1.65  rillig     List *list = bmake_malloc(sizeof *list);
     96   1.1  rillig 
     97  1.16  rillig     list->first = NULL;
     98  1.16  rillig     list->last = NULL;
     99  1.16  rillig     list->isOpen = FALSE;
    100  1.16  rillig     list->lastAccess = Unknown;
    101   1.1  rillig 
    102  1.16  rillig     return list;
    103   1.1  rillig }
    104   1.1  rillig 
    105  1.14  rillig /* Duplicate an entire list, usually by copying the datum pointers.
    106  1.14  rillig  * If copyProc is given, that function is used to create the new datum from the
    107  1.35  rillig  * old datum, usually by creating a copy of it. */
    108  1.65  rillig List *
    109  1.65  rillig Lst_Copy(List *list, LstCopyProc copyProc)
    110   1.1  rillig {
    111  1.65  rillig     List *newList;
    112  1.65  rillig     ListNode *node;
    113   1.1  rillig 
    114  1.52  rillig     assert(list != NULL);
    115   1.1  rillig 
    116  1.16  rillig     newList = Lst_Init();
    117   1.1  rillig 
    118  1.35  rillig     for (node = list->first; node != NULL; node = node->next) {
    119  1.35  rillig 	void *datum = copyProc != NULL ? copyProc(node->datum) : node->datum;
    120  1.50  rillig 	Lst_Append(newList, datum);
    121   1.1  rillig     }
    122   1.1  rillig 
    123  1.16  rillig     return newList;
    124   1.1  rillig }
    125   1.1  rillig 
    126  1.42  rillig /* Free a list and all its nodes. The list data itself are not freed though. */
    127  1.42  rillig void
    128  1.65  rillig Lst_Free(List *list)
    129  1.42  rillig {
    130  1.65  rillig     ListNode *node;
    131  1.65  rillig     ListNode *next;
    132  1.42  rillig 
    133  1.52  rillig     assert(list != NULL);
    134  1.42  rillig 
    135  1.42  rillig     for (node = list->first; node != NULL; node = next) {
    136  1.42  rillig 	next = node->next;
    137  1.42  rillig 	free(node);
    138  1.42  rillig     }
    139  1.42  rillig 
    140  1.42  rillig     free(list);
    141  1.42  rillig }
    142  1.42  rillig 
    143  1.59  rillig /* Destroy a list and free all its resources. The freeProc is called with the
    144  1.59  rillig  * datum from each node in turn before the node is freed. */
    145   1.1  rillig void
    146  1.65  rillig Lst_Destroy(List *list, LstFreeProc freeProc)
    147   1.1  rillig {
    148  1.65  rillig     ListNode *node;
    149  1.65  rillig     ListNode *next;
    150   1.1  rillig 
    151  1.52  rillig     assert(list != NULL);
    152  1.42  rillig     assert(freeProc != NULL);
    153   1.1  rillig 
    154  1.42  rillig     for (node = list->first; node != NULL; node = next) {
    155  1.42  rillig 	next = node->next;
    156  1.42  rillig 	freeProc(node->datum);
    157  1.42  rillig 	free(node);
    158   1.1  rillig     }
    159   1.1  rillig 
    160   1.1  rillig     free(list);
    161   1.1  rillig }
    162   1.1  rillig 
    163   1.1  rillig /*
    164   1.1  rillig  * Functions to modify a list
    165   1.1  rillig  */
    166   1.1  rillig 
    167  1.14  rillig /* Insert a new node with the given piece of data before the given node in the
    168  1.14  rillig  * given list. */
    169  1.26  rillig void
    170  1.65  rillig Lst_InsertBefore(List *list, ListNode *node, void *datum)
    171  1.26  rillig {
    172  1.65  rillig     ListNode *newNode;
    173  1.26  rillig 
    174  1.52  rillig     assert(list != NULL);
    175  1.26  rillig     assert(!LstIsEmpty(list));
    176  1.52  rillig     assert(node != NULL);
    177  1.26  rillig     assert(datum != NULL);
    178  1.26  rillig 
    179  1.26  rillig     newNode = LstNodeNew(datum);
    180  1.26  rillig     newNode->prev = node->prev;
    181  1.26  rillig     newNode->next = node;
    182  1.26  rillig 
    183  1.26  rillig     if (node->prev != NULL) {
    184  1.26  rillig 	node->prev->next = newNode;
    185  1.26  rillig     }
    186  1.26  rillig     node->prev = newNode;
    187  1.26  rillig 
    188  1.26  rillig     if (node == list->first) {
    189  1.26  rillig 	list->first = newNode;
    190  1.26  rillig     }
    191  1.26  rillig }
    192  1.26  rillig 
    193  1.22  rillig /* Add a piece of data at the start of the given list. */
    194  1.22  rillig void
    195  1.65  rillig Lst_Prepend(List *list, void *datum)
    196  1.22  rillig {
    197  1.65  rillig     ListNode *node;
    198  1.22  rillig 
    199  1.52  rillig     assert(list != NULL);
    200  1.22  rillig     assert(datum != NULL);
    201  1.22  rillig 
    202  1.22  rillig     node = LstNodeNew(datum);
    203  1.22  rillig     node->prev = NULL;
    204  1.22  rillig     node->next = list->first;
    205  1.22  rillig 
    206  1.22  rillig     if (list->first == NULL) {
    207  1.22  rillig 	list->first = node;
    208  1.22  rillig 	list->last = node;
    209  1.22  rillig     } else {
    210  1.22  rillig 	list->first->prev = node;
    211  1.22  rillig 	list->first = node;
    212  1.22  rillig     }
    213  1.22  rillig }
    214  1.22  rillig 
    215  1.21  rillig /* Add a piece of data at the end of the given list. */
    216  1.21  rillig void
    217  1.65  rillig Lst_Append(List *list, void *datum)
    218  1.21  rillig {
    219  1.65  rillig     ListNode *node;
    220  1.21  rillig 
    221  1.52  rillig     assert(list != NULL);
    222  1.21  rillig     assert(datum != NULL);
    223  1.21  rillig 
    224  1.21  rillig     node = LstNodeNew(datum);
    225  1.21  rillig     node->prev = list->last;
    226  1.21  rillig     node->next = NULL;
    227  1.21  rillig 
    228  1.21  rillig     if (list->last == NULL) {
    229  1.21  rillig 	list->first = node;
    230  1.21  rillig 	list->last = node;
    231  1.21  rillig     } else {
    232  1.21  rillig 	list->last->next = node;
    233  1.21  rillig 	list->last = node;
    234  1.21  rillig     }
    235  1.21  rillig }
    236  1.21  rillig 
    237   1.8  rillig /* Remove the given node from the given list.
    238   1.8  rillig  * The datum stored in the node must be freed by the caller, if necessary. */
    239   1.8  rillig void
    240  1.65  rillig Lst_Remove(List *list, ListNode *node)
    241   1.1  rillig {
    242  1.52  rillig     assert(list != NULL);
    243  1.52  rillig     assert(node != NULL);
    244   1.1  rillig 
    245   1.1  rillig     /*
    246   1.1  rillig      * unlink it from the list
    247   1.1  rillig      */
    248  1.16  rillig     if (node->next != NULL) {
    249  1.16  rillig 	node->next->prev = node->prev;
    250   1.1  rillig     }
    251  1.16  rillig     if (node->prev != NULL) {
    252  1.16  rillig 	node->prev->next = node->next;
    253   1.1  rillig     }
    254   1.1  rillig 
    255   1.1  rillig     /*
    256  1.15  rillig      * if either the first or last of the list point to this node,
    257   1.1  rillig      * adjust them accordingly
    258   1.1  rillig      */
    259  1.16  rillig     if (list->first == node) {
    260  1.16  rillig 	list->first = node->next;
    261   1.1  rillig     }
    262  1.16  rillig     if (list->last == node) {
    263  1.16  rillig 	list->last = node->prev;
    264   1.1  rillig     }
    265   1.1  rillig 
    266   1.1  rillig     /*
    267   1.1  rillig      * Sequential access stuff. If the node we're removing is the current
    268   1.1  rillig      * node in the list, reset the current node to the previous one. If the
    269  1.15  rillig      * previous one was non-existent (prev == NULL), we set the
    270   1.1  rillig      * end to be Unknown, since it is.
    271   1.1  rillig      */
    272  1.16  rillig     if (list->isOpen && list->curr == node) {
    273  1.15  rillig 	list->curr = list->prev;
    274  1.15  rillig 	if (list->curr == NULL) {
    275  1.15  rillig 	    list->lastAccess = Unknown;
    276   1.1  rillig 	}
    277   1.1  rillig     }
    278   1.1  rillig 
    279   1.1  rillig     /*
    280   1.1  rillig      * note that the datum is unmolested. The caller must free it as
    281   1.1  rillig      * necessary and as expected.
    282   1.1  rillig      */
    283  1.16  rillig     if (node->useCount == 0) {
    284  1.16  rillig 	free(node);
    285   1.1  rillig     } else {
    286  1.16  rillig 	node->deleted = TRUE;
    287   1.1  rillig     }
    288   1.1  rillig }
    289   1.1  rillig 
    290   1.8  rillig /* Replace the datum in the given node with the new datum. */
    291   1.8  rillig void
    292  1.65  rillig LstNode_Set(ListNode *node, void *datum)
    293   1.1  rillig {
    294  1.52  rillig     assert(node != NULL);
    295  1.37  rillig     assert(datum != NULL);
    296  1.37  rillig 
    297  1.16  rillig     node->datum = datum;
    298   1.1  rillig }
    299   1.1  rillig 
    300  1.37  rillig /* Replace the datum in the given node to NULL. */
    301  1.37  rillig void
    302  1.65  rillig LstNode_SetNull(ListNode *node)
    303  1.37  rillig {
    304  1.52  rillig     assert(node != NULL);
    305  1.37  rillig 
    306  1.37  rillig     node->datum = NULL;
    307  1.37  rillig }
    308  1.37  rillig 
    309   1.1  rillig 
    310   1.1  rillig /*
    311   1.1  rillig  * Node-specific functions
    312   1.1  rillig  */
    313   1.1  rillig 
    314  1.42  rillig /* Return the first node from the given list, or NULL if the list is empty. */
    315  1.65  rillig ListNode *
    316  1.65  rillig Lst_First(List *list)
    317  1.42  rillig {
    318  1.52  rillig     assert(list != NULL);
    319  1.42  rillig 
    320  1.42  rillig     return list->first;
    321  1.42  rillig }
    322  1.42  rillig 
    323  1.42  rillig /* Return the last node from the given list, or NULL if the list is empty. */
    324  1.65  rillig ListNode *
    325  1.65  rillig Lst_Last(List *list)
    326  1.42  rillig {
    327  1.52  rillig     assert(list != NULL);
    328  1.42  rillig 
    329  1.42  rillig     return list->last;
    330  1.42  rillig }
    331  1.42  rillig 
    332   1.6  rillig /* Return the successor to the given node on its list, or NULL. */
    333  1.65  rillig ListNode *
    334  1.65  rillig LstNode_Next(ListNode *node)
    335  1.42  rillig {
    336  1.52  rillig     assert(node != NULL);
    337  1.42  rillig 
    338  1.42  rillig     return node->next;
    339  1.42  rillig }
    340  1.42  rillig 
    341   1.6  rillig /* Return the predecessor to the given node on its list, or NULL. */
    342  1.65  rillig ListNode *
    343  1.65  rillig LstNode_Prev(ListNode *node)
    344   1.1  rillig {
    345  1.52  rillig     assert(node != NULL);
    346  1.33  rillig     return node->prev;
    347   1.1  rillig }
    348   1.1  rillig 
    349  1.28  rillig /* Return the datum stored in the given node. */
    350   1.1  rillig void *
    351  1.65  rillig LstNode_Datum(ListNode *node)
    352   1.1  rillig {
    353  1.52  rillig     assert(node != NULL);
    354  1.28  rillig     return node->datum;
    355   1.1  rillig }
    356   1.1  rillig 
    357   1.1  rillig 
    358   1.1  rillig /*
    359   1.1  rillig  * Functions for entire lists
    360   1.1  rillig  */
    361   1.1  rillig 
    362  1.42  rillig /* Return TRUE if the given list is empty. */
    363  1.42  rillig Boolean
    364  1.65  rillig Lst_IsEmpty(List *list)
    365  1.42  rillig {
    366  1.52  rillig     assert(list != NULL);
    367  1.42  rillig 
    368  1.42  rillig     return LstIsEmpty(list);
    369  1.42  rillig }
    370  1.42  rillig 
    371  1.53  rillig /* Return the first node from the list for which the match function returns
    372  1.53  rillig  * TRUE, or NULL if none of the nodes matched. */
    373  1.65  rillig ListNode *
    374  1.65  rillig Lst_Find(List *list, LstFindProc match, const void *matchArgs)
    375  1.53  rillig {
    376  1.55  rillig     return Lst_FindFrom(list, Lst_First(list), match, matchArgs);
    377  1.53  rillig }
    378  1.53  rillig 
    379  1.53  rillig /* Return the first node from the list, starting at the given node, for which
    380  1.53  rillig  * the match function returns TRUE, or NULL if none of the nodes matches.
    381  1.53  rillig  *
    382  1.53  rillig  * The start node may be NULL, in which case nothing is found. This allows
    383  1.56  rillig  * for passing Lst_First or LstNode_Next as the start node. */
    384  1.65  rillig ListNode *
    385  1.65  rillig Lst_FindFrom(List *list, ListNode *node, LstFindProc match, const void *matchArgs)
    386  1.53  rillig {
    387  1.65  rillig     ListNode *tln;
    388  1.53  rillig 
    389  1.53  rillig     assert(list != NULL);
    390  1.53  rillig     assert(match != NULL);
    391  1.53  rillig 
    392  1.53  rillig     for (tln = node; tln != NULL; tln = tln->next) {
    393  1.53  rillig 	if (match(tln->datum, matchArgs))
    394  1.53  rillig 	    return tln;
    395  1.53  rillig     }
    396  1.53  rillig 
    397  1.53  rillig     return NULL;
    398  1.53  rillig }
    399  1.53  rillig 
    400  1.14  rillig /* Return the first node that contains the given datum, or NULL. */
    401  1.65  rillig ListNode *
    402  1.65  rillig Lst_FindDatum(List *list, const void *datum)
    403   1.1  rillig {
    404  1.65  rillig     ListNode *node;
    405   1.1  rillig 
    406  1.52  rillig     assert(list != NULL);
    407  1.29  rillig     assert(datum != NULL);
    408   1.1  rillig 
    409  1.29  rillig     for (node = list->first; node != NULL; node = node->next) {
    410  1.16  rillig 	if (node->datum == datum) {
    411  1.16  rillig 	    return node;
    412   1.1  rillig 	}
    413  1.29  rillig     }
    414   1.1  rillig 
    415   1.1  rillig     return NULL;
    416   1.1  rillig }
    417   1.1  rillig 
    418  1.69  rillig void
    419  1.69  rillig Lst_ForEach(List *list, LstActionProc proc, void *procData)
    420  1.69  rillig {
    421  1.69  rillig     ListNode *node;
    422  1.69  rillig     for (node = list->first; node != NULL; node = node->next)
    423  1.69  rillig         proc(node->datum, procData);
    424  1.69  rillig }
    425  1.69  rillig 
    426  1.14  rillig /* Apply the given function to each element of the given list. The function
    427  1.14  rillig  * should return 0 if traversal should continue and non-zero if it should
    428  1.14  rillig  * abort. */
    429   1.1  rillig int
    430  1.67  rillig Lst_ForEachUntil(List *list, LstActionUntilProc proc, void *procData)
    431  1.42  rillig {
    432  1.68  rillig     ListNode *tln = list->first;
    433  1.68  rillig     int result = 0;
    434   1.1  rillig 
    435  1.68  rillig     while (tln != NULL) {
    436   1.1  rillig 	/*
    437   1.1  rillig 	 * Take care of having the current element deleted out from under
    438   1.1  rillig 	 * us.
    439   1.1  rillig 	 */
    440  1.68  rillig     	ListNode *next = tln->next;
    441   1.1  rillig 
    442   1.1  rillig 	/*
    443   1.1  rillig 	 * We're done with the traversal if
    444  1.38  rillig 	 *  - the next node to examine doesn't exist and
    445   1.1  rillig 	 *  - nothing's been added after the current node (check this
    446   1.1  rillig 	 *    after proc() has been called).
    447   1.1  rillig 	 */
    448  1.68  rillig 	Boolean done = next == NULL;
    449   1.1  rillig 
    450  1.17  rillig 	tln->useCount++;
    451  1.16  rillig 	result = (*proc)(tln->datum, procData);
    452  1.17  rillig 	tln->useCount--;
    453   1.1  rillig 
    454   1.1  rillig 	/*
    455   1.1  rillig 	 * Now check whether a node has been added.
    456   1.1  rillig 	 * Note: this doesn't work if this node was deleted before
    457   1.1  rillig 	 *       the new node was added.
    458   1.1  rillig 	 */
    459  1.15  rillig 	if (next != tln->next) {
    460  1.15  rillig 	    next = tln->next;
    461   1.4  rillig 	    done = 0;
    462   1.1  rillig 	}
    463   1.1  rillig 
    464   1.7  rillig 	if (tln->deleted) {
    465   1.1  rillig 	    free((char *)tln);
    466   1.1  rillig 	}
    467   1.1  rillig 	tln = next;
    468  1.68  rillig 	if (result || LstIsEmpty(list) || done)
    469  1.68  rillig 	    break;
    470  1.68  rillig     }
    471   1.1  rillig 
    472   1.1  rillig     return result;
    473   1.1  rillig }
    474   1.1  rillig 
    475  1.34  rillig /* Move all nodes from list2 to the end of list1.
    476  1.34  rillig  * List2 is destroyed and freed. */
    477  1.34  rillig void
    478  1.65  rillig Lst_MoveAll(List *list1, List *list2)
    479   1.1  rillig {
    480  1.52  rillig     assert(list1 != NULL);
    481  1.52  rillig     assert(list2 != NULL);
    482   1.1  rillig 
    483  1.34  rillig     if (list2->first != NULL) {
    484  1.34  rillig 	list2->first->prev = list1->last;
    485  1.34  rillig 	if (list1->last != NULL) {
    486  1.34  rillig 	    list1->last->next = list2->first;
    487  1.34  rillig 	} else {
    488  1.34  rillig 	    list1->first = list2->first;
    489   1.1  rillig 	}
    490  1.34  rillig 	list1->last = list2->last;
    491   1.1  rillig     }
    492  1.34  rillig     free(list2);
    493   1.1  rillig }
    494   1.1  rillig 
    495  1.22  rillig /* Copy the element data from src to the start of dst. */
    496  1.22  rillig void
    497  1.65  rillig Lst_PrependAll(List *dst, List *src)
    498  1.22  rillig {
    499  1.65  rillig     ListNode *node;
    500  1.22  rillig     for (node = src->last; node != NULL; node = node->prev)
    501  1.50  rillig 	Lst_Prepend(dst, node->datum);
    502  1.22  rillig }
    503  1.22  rillig 
    504  1.22  rillig /* Copy the element data from src to the end of dst. */
    505  1.22  rillig void
    506  1.65  rillig Lst_AppendAll(List *dst, List *src)
    507  1.22  rillig {
    508  1.65  rillig     ListNode *node;
    509  1.22  rillig     for (node = src->first; node != NULL; node = node->next)
    510  1.50  rillig 	Lst_Append(dst, node->datum);
    511  1.22  rillig }
    512   1.1  rillig 
    513   1.1  rillig /*
    514   1.1  rillig  * these functions are for dealing with a list as a table, of sorts.
    515   1.1  rillig  * An idea of the "current element" is kept and used by all the functions
    516   1.1  rillig  * between Lst_Open() and Lst_Close().
    517   1.1  rillig  *
    518   1.1  rillig  * The sequential functions access the list in a slightly different way.
    519   1.1  rillig  * CurPtr points to their idea of the current node in the list and they
    520   1.1  rillig  * access the list based on it.
    521   1.1  rillig  */
    522   1.1  rillig 
    523  1.14  rillig /* Open a list for sequential access. A list can still be searched, etc.,
    524  1.14  rillig  * without confusing these functions. */
    525  1.10  rillig void
    526  1.65  rillig Lst_Open(List *list)
    527  1.10  rillig {
    528  1.52  rillig     assert(list != NULL);
    529  1.60  rillig     assert(!list->isOpen);
    530  1.10  rillig 
    531  1.16  rillig     list->isOpen = TRUE;
    532  1.16  rillig     list->lastAccess = LstIsEmpty(list) ? Head : Unknown;
    533  1.16  rillig     list->curr = NULL;
    534  1.10  rillig }
    535  1.10  rillig 
    536  1.10  rillig /* Return the next node for the given list, or NULL if the end has been
    537  1.10  rillig  * reached. */
    538  1.65  rillig ListNode *
    539  1.65  rillig Lst_Next(List *list)
    540   1.1  rillig {
    541  1.65  rillig     ListNode *node;
    542   1.1  rillig 
    543  1.52  rillig     assert(list != NULL);
    544   1.9  rillig     assert(list->isOpen);
    545   1.1  rillig 
    546  1.15  rillig     list->prev = list->curr;
    547   1.1  rillig 
    548  1.15  rillig     if (list->curr == NULL) {
    549  1.15  rillig 	if (list->lastAccess == Unknown) {
    550   1.1  rillig 	    /*
    551  1.15  rillig 	     * If we're just starting out, lastAccess will be Unknown.
    552   1.1  rillig 	     * Then we want to start this thing off in the right
    553  1.15  rillig 	     * direction -- at the start with lastAccess being Middle.
    554   1.1  rillig 	     */
    555  1.16  rillig 	    list->curr = node = list->first;
    556  1.15  rillig 	    list->lastAccess = Middle;
    557   1.1  rillig 	} else {
    558  1.16  rillig 	    node = NULL;
    559  1.15  rillig 	    list->lastAccess = Tail;
    560   1.1  rillig 	}
    561   1.1  rillig     } else {
    562  1.16  rillig 	node = list->curr->next;
    563  1.16  rillig 	list->curr = node;
    564   1.1  rillig 
    565  1.16  rillig 	if (node == list->first || node == NULL) {
    566   1.1  rillig 	    /*
    567   1.1  rillig 	     * If back at the front, then we've hit the end...
    568   1.1  rillig 	     */
    569  1.15  rillig 	    list->lastAccess = Tail;
    570   1.1  rillig 	} else {
    571   1.1  rillig 	    /*
    572   1.1  rillig 	     * Reset to Middle if gone past first.
    573   1.1  rillig 	     */
    574  1.15  rillig 	    list->lastAccess = Middle;
    575   1.1  rillig 	}
    576   1.1  rillig     }
    577   1.1  rillig 
    578  1.16  rillig     return node;
    579   1.1  rillig }
    580   1.1  rillig 
    581  1.10  rillig /* Close a list which was opened for sequential access. */
    582   1.1  rillig void
    583  1.65  rillig Lst_Close(List *list)
    584   1.1  rillig {
    585  1.52  rillig     assert(list != NULL);
    586  1.16  rillig     assert(list->isOpen);
    587   1.1  rillig 
    588  1.10  rillig     list->isOpen = FALSE;
    589  1.15  rillig     list->lastAccess = Unknown;
    590   1.1  rillig }
    591   1.1  rillig 
    592   1.1  rillig 
    593   1.1  rillig /*
    594   1.1  rillig  * for using the list as a queue
    595   1.1  rillig  */
    596   1.1  rillig 
    597  1.14  rillig /* Add the datum to the tail of the given list. */
    598  1.25  rillig void
    599  1.65  rillig Lst_Enqueue(List *list, void *datum)
    600   1.1  rillig {
    601  1.50  rillig     Lst_Append(list, datum);
    602   1.1  rillig }
    603   1.1  rillig 
    604  1.25  rillig /* Remove and return the datum at the head of the given list. */
    605   1.1  rillig void *
    606  1.65  rillig Lst_Dequeue(List *list)
    607   1.1  rillig {
    608  1.16  rillig     void *datum;
    609   1.1  rillig 
    610  1.52  rillig     assert(list != NULL);
    611  1.25  rillig     assert(!LstIsEmpty(list));
    612   1.1  rillig 
    613  1.25  rillig     datum = list->first->datum;
    614  1.50  rillig     Lst_Remove(list, list->first);
    615  1.25  rillig     assert(datum != NULL);
    616  1.16  rillig     return datum;
    617   1.1  rillig }
    618  1.61  rillig 
    619  1.61  rillig void
    620  1.61  rillig Stack_Init(Stack *stack)
    621  1.61  rillig {
    622  1.61  rillig     stack->len = 0;
    623  1.61  rillig     stack->cap = 10;
    624  1.61  rillig     stack->items = bmake_malloc(stack->cap * sizeof stack->items[0]);
    625  1.61  rillig }
    626  1.61  rillig 
    627  1.61  rillig Boolean Stack_IsEmpty(Stack *stack)
    628  1.61  rillig {
    629  1.61  rillig     return stack->len == 0;
    630  1.61  rillig }
    631  1.61  rillig 
    632  1.61  rillig void Stack_Push(Stack *stack, void *datum)
    633  1.61  rillig {
    634  1.61  rillig     if (stack->len >= stack->cap) {
    635  1.62  rillig 	stack->cap *= 2;
    636  1.61  rillig 	stack->items = bmake_realloc(stack->items,
    637  1.61  rillig 				     stack->cap * sizeof stack->items[0]);
    638  1.61  rillig     }
    639  1.61  rillig     stack->items[stack->len] = datum;
    640  1.61  rillig     stack->len++;
    641  1.61  rillig }
    642  1.61  rillig 
    643  1.61  rillig void *Stack_Pop(Stack *stack)
    644  1.61  rillig {
    645  1.64  rillig     void *datum;
    646  1.64  rillig 
    647  1.61  rillig     assert(stack->len > 0);
    648  1.61  rillig     stack->len--;
    649  1.64  rillig     datum = stack->items[stack->len];
    650  1.64  rillig #ifdef CLEANUP
    651  1.64  rillig     stack->items[stack->len] = NULL;
    652  1.64  rillig #endif
    653  1.64  rillig     return datum;
    654  1.61  rillig }
    655  1.61  rillig 
    656  1.61  rillig void Stack_Done(Stack *stack)
    657  1.61  rillig {
    658  1.61  rillig     free(stack->items);
    659  1.61  rillig }
    660