Home | History | Annotate | Line # | Download | only in make
lst.c revision 1.72
      1  1.72  rillig /* $NetBSD: lst.c,v 1.72 2020/09/26 17:15:20 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.72  rillig MAKE_RCSID("$NetBSD: lst.c,v 1.72 2020/09/26 17:15:20 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.5  rillig Lst_Init(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.52  rillig     assert(list != NULL);
     83   1.1  rillig 
     84  1.16  rillig     newList = Lst_Init();
     85   1.1  rillig 
     86  1.35  rillig     for (node = list->first; node != NULL; node = node->next) {
     87  1.35  rillig 	void *datum = copyProc != NULL ? copyProc(node->datum) : node->datum;
     88  1.50  rillig 	Lst_Append(newList, datum);
     89   1.1  rillig     }
     90   1.1  rillig 
     91  1.16  rillig     return newList;
     92   1.1  rillig }
     93   1.1  rillig 
     94  1.42  rillig /* Free a list and all its nodes. The list data itself are not freed though. */
     95  1.42  rillig void
     96  1.65  rillig Lst_Free(List *list)
     97  1.42  rillig {
     98  1.65  rillig     ListNode *node;
     99  1.65  rillig     ListNode *next;
    100  1.42  rillig 
    101  1.52  rillig     assert(list != NULL);
    102  1.42  rillig 
    103  1.42  rillig     for (node = list->first; node != NULL; node = next) {
    104  1.42  rillig 	next = node->next;
    105  1.42  rillig 	free(node);
    106  1.42  rillig     }
    107  1.42  rillig 
    108  1.42  rillig     free(list);
    109  1.42  rillig }
    110  1.42  rillig 
    111  1.59  rillig /* Destroy a list and free all its resources. The freeProc is called with the
    112  1.59  rillig  * datum from each node in turn before the node is freed. */
    113   1.1  rillig void
    114  1.65  rillig Lst_Destroy(List *list, LstFreeProc freeProc)
    115   1.1  rillig {
    116  1.65  rillig     ListNode *node;
    117  1.65  rillig     ListNode *next;
    118   1.1  rillig 
    119  1.52  rillig     assert(list != NULL);
    120  1.42  rillig     assert(freeProc != NULL);
    121   1.1  rillig 
    122  1.42  rillig     for (node = list->first; node != NULL; node = next) {
    123  1.42  rillig 	next = node->next;
    124  1.42  rillig 	freeProc(node->datum);
    125  1.42  rillig 	free(node);
    126   1.1  rillig     }
    127   1.1  rillig 
    128   1.1  rillig     free(list);
    129   1.1  rillig }
    130   1.1  rillig 
    131   1.1  rillig /*
    132   1.1  rillig  * Functions to modify a list
    133   1.1  rillig  */
    134   1.1  rillig 
    135  1.14  rillig /* Insert a new node with the given piece of data before the given node in the
    136  1.14  rillig  * given list. */
    137  1.26  rillig void
    138  1.65  rillig Lst_InsertBefore(List *list, ListNode *node, void *datum)
    139  1.26  rillig {
    140  1.65  rillig     ListNode *newNode;
    141  1.26  rillig 
    142  1.52  rillig     assert(list != NULL);
    143  1.26  rillig     assert(!LstIsEmpty(list));
    144  1.52  rillig     assert(node != NULL);
    145  1.26  rillig     assert(datum != NULL);
    146  1.26  rillig 
    147  1.26  rillig     newNode = LstNodeNew(datum);
    148  1.26  rillig     newNode->prev = node->prev;
    149  1.26  rillig     newNode->next = node;
    150  1.26  rillig 
    151  1.26  rillig     if (node->prev != NULL) {
    152  1.26  rillig 	node->prev->next = newNode;
    153  1.26  rillig     }
    154  1.26  rillig     node->prev = newNode;
    155  1.26  rillig 
    156  1.26  rillig     if (node == list->first) {
    157  1.26  rillig 	list->first = newNode;
    158  1.26  rillig     }
    159  1.26  rillig }
    160  1.26  rillig 
    161  1.22  rillig /* Add a piece of data at the start of the given list. */
    162  1.22  rillig void
    163  1.65  rillig Lst_Prepend(List *list, void *datum)
    164  1.22  rillig {
    165  1.65  rillig     ListNode *node;
    166  1.22  rillig 
    167  1.52  rillig     assert(list != NULL);
    168  1.22  rillig     assert(datum != NULL);
    169  1.22  rillig 
    170  1.22  rillig     node = LstNodeNew(datum);
    171  1.22  rillig     node->prev = NULL;
    172  1.22  rillig     node->next = list->first;
    173  1.22  rillig 
    174  1.22  rillig     if (list->first == NULL) {
    175  1.22  rillig 	list->first = node;
    176  1.22  rillig 	list->last = node;
    177  1.22  rillig     } else {
    178  1.22  rillig 	list->first->prev = node;
    179  1.22  rillig 	list->first = node;
    180  1.22  rillig     }
    181  1.22  rillig }
    182  1.22  rillig 
    183  1.21  rillig /* Add a piece of data at the end of the given list. */
    184  1.21  rillig void
    185  1.65  rillig Lst_Append(List *list, void *datum)
    186  1.21  rillig {
    187  1.65  rillig     ListNode *node;
    188  1.21  rillig 
    189  1.52  rillig     assert(list != NULL);
    190  1.21  rillig     assert(datum != NULL);
    191  1.21  rillig 
    192  1.21  rillig     node = LstNodeNew(datum);
    193  1.21  rillig     node->prev = list->last;
    194  1.21  rillig     node->next = NULL;
    195  1.21  rillig 
    196  1.21  rillig     if (list->last == NULL) {
    197  1.21  rillig 	list->first = node;
    198  1.21  rillig 	list->last = node;
    199  1.21  rillig     } else {
    200  1.21  rillig 	list->last->next = node;
    201  1.21  rillig 	list->last = node;
    202  1.21  rillig     }
    203  1.21  rillig }
    204  1.21  rillig 
    205   1.8  rillig /* Remove the given node from the given list.
    206   1.8  rillig  * The datum stored in the node must be freed by the caller, if necessary. */
    207   1.8  rillig void
    208  1.65  rillig Lst_Remove(List *list, ListNode *node)
    209   1.1  rillig {
    210  1.52  rillig     assert(list != NULL);
    211  1.52  rillig     assert(node != NULL);
    212   1.1  rillig 
    213   1.1  rillig     /*
    214   1.1  rillig      * unlink it from the list
    215   1.1  rillig      */
    216  1.16  rillig     if (node->next != NULL) {
    217  1.16  rillig 	node->next->prev = node->prev;
    218   1.1  rillig     }
    219  1.16  rillig     if (node->prev != NULL) {
    220  1.16  rillig 	node->prev->next = node->next;
    221   1.1  rillig     }
    222   1.1  rillig 
    223   1.1  rillig     /*
    224  1.15  rillig      * if either the first or last of the list point to this node,
    225   1.1  rillig      * adjust them accordingly
    226   1.1  rillig      */
    227  1.16  rillig     if (list->first == node) {
    228  1.16  rillig 	list->first = node->next;
    229   1.1  rillig     }
    230  1.16  rillig     if (list->last == node) {
    231  1.16  rillig 	list->last = node->prev;
    232   1.1  rillig     }
    233   1.1  rillig 
    234   1.1  rillig     /*
    235   1.1  rillig      * Sequential access stuff. If the node we're removing is the current
    236   1.1  rillig      * node in the list, reset the current node to the previous one. If the
    237  1.15  rillig      * previous one was non-existent (prev == NULL), we set the
    238   1.1  rillig      * end to be Unknown, since it is.
    239   1.1  rillig      */
    240  1.70  rillig     if (list->priv_isOpen && list->priv_curr == node) {
    241  1.70  rillig 	list->priv_curr = list->priv_prev;
    242  1.70  rillig 	if (list->priv_curr == NULL) {
    243  1.70  rillig 	    list->priv_lastAccess = Unknown;
    244   1.1  rillig 	}
    245   1.1  rillig     }
    246   1.1  rillig 
    247   1.1  rillig     /*
    248   1.1  rillig      * note that the datum is unmolested. The caller must free it as
    249   1.1  rillig      * necessary and as expected.
    250   1.1  rillig      */
    251  1.70  rillig     if (node->priv_useCount == 0) {
    252  1.16  rillig 	free(node);
    253   1.1  rillig     } else {
    254  1.70  rillig 	node->priv_deleted = TRUE;
    255   1.1  rillig     }
    256   1.1  rillig }
    257   1.1  rillig 
    258   1.8  rillig /* Replace the datum in the given node with the new datum. */
    259   1.8  rillig void
    260  1.65  rillig LstNode_Set(ListNode *node, void *datum)
    261   1.1  rillig {
    262  1.52  rillig     assert(node != NULL);
    263  1.37  rillig     assert(datum != NULL);
    264  1.37  rillig 
    265  1.16  rillig     node->datum = datum;
    266   1.1  rillig }
    267   1.1  rillig 
    268  1.37  rillig /* Replace the datum in the given node to NULL. */
    269  1.37  rillig void
    270  1.65  rillig LstNode_SetNull(ListNode *node)
    271  1.37  rillig {
    272  1.52  rillig     assert(node != NULL);
    273  1.37  rillig 
    274  1.37  rillig     node->datum = NULL;
    275  1.37  rillig }
    276  1.37  rillig 
    277   1.1  rillig 
    278   1.1  rillig /*
    279   1.1  rillig  * Functions for entire lists
    280   1.1  rillig  */
    281   1.1  rillig 
    282  1.53  rillig /* Return the first node from the list for which the match function returns
    283  1.53  rillig  * TRUE, or NULL if none of the nodes matched. */
    284  1.65  rillig ListNode *
    285  1.65  rillig Lst_Find(List *list, LstFindProc match, const void *matchArgs)
    286  1.53  rillig {
    287  1.55  rillig     return Lst_FindFrom(list, Lst_First(list), match, matchArgs);
    288  1.53  rillig }
    289  1.53  rillig 
    290  1.53  rillig /* Return the first node from the list, starting at the given node, for which
    291  1.53  rillig  * the match function returns TRUE, or NULL if none of the nodes matches.
    292  1.53  rillig  *
    293  1.72  rillig  * The start node may be NULL, in which case nothing is found. */
    294  1.65  rillig ListNode *
    295  1.65  rillig Lst_FindFrom(List *list, ListNode *node, LstFindProc match, const void *matchArgs)
    296  1.53  rillig {
    297  1.65  rillig     ListNode *tln;
    298  1.53  rillig 
    299  1.53  rillig     assert(list != NULL);
    300  1.53  rillig     assert(match != NULL);
    301  1.53  rillig 
    302  1.53  rillig     for (tln = node; tln != NULL; tln = tln->next) {
    303  1.53  rillig 	if (match(tln->datum, matchArgs))
    304  1.53  rillig 	    return tln;
    305  1.53  rillig     }
    306  1.53  rillig 
    307  1.53  rillig     return NULL;
    308  1.53  rillig }
    309  1.53  rillig 
    310  1.14  rillig /* Return the first node that contains the given datum, or NULL. */
    311  1.65  rillig ListNode *
    312  1.65  rillig Lst_FindDatum(List *list, const void *datum)
    313   1.1  rillig {
    314  1.65  rillig     ListNode *node;
    315   1.1  rillig 
    316  1.52  rillig     assert(list != NULL);
    317  1.29  rillig     assert(datum != NULL);
    318   1.1  rillig 
    319  1.29  rillig     for (node = list->first; node != NULL; node = node->next) {
    320  1.16  rillig 	if (node->datum == datum) {
    321  1.16  rillig 	    return node;
    322   1.1  rillig 	}
    323  1.29  rillig     }
    324   1.1  rillig 
    325   1.1  rillig     return NULL;
    326   1.1  rillig }
    327   1.1  rillig 
    328  1.69  rillig void
    329  1.69  rillig Lst_ForEach(List *list, LstActionProc proc, void *procData)
    330  1.69  rillig {
    331  1.69  rillig     ListNode *node;
    332  1.69  rillig     for (node = list->first; node != NULL; node = node->next)
    333  1.69  rillig         proc(node->datum, procData);
    334  1.69  rillig }
    335  1.69  rillig 
    336  1.14  rillig /* Apply the given function to each element of the given list. The function
    337  1.14  rillig  * should return 0 if traversal should continue and non-zero if it should
    338  1.14  rillig  * abort. */
    339   1.1  rillig int
    340  1.67  rillig Lst_ForEachUntil(List *list, LstActionUntilProc proc, void *procData)
    341  1.42  rillig {
    342  1.68  rillig     ListNode *tln = list->first;
    343  1.68  rillig     int result = 0;
    344   1.1  rillig 
    345  1.68  rillig     while (tln != NULL) {
    346   1.1  rillig 	/*
    347   1.1  rillig 	 * Take care of having the current element deleted out from under
    348   1.1  rillig 	 * us.
    349   1.1  rillig 	 */
    350  1.68  rillig     	ListNode *next = tln->next;
    351   1.1  rillig 
    352   1.1  rillig 	/*
    353   1.1  rillig 	 * We're done with the traversal if
    354  1.38  rillig 	 *  - the next node to examine doesn't exist and
    355   1.1  rillig 	 *  - nothing's been added after the current node (check this
    356   1.1  rillig 	 *    after proc() has been called).
    357   1.1  rillig 	 */
    358  1.68  rillig 	Boolean done = next == NULL;
    359   1.1  rillig 
    360  1.70  rillig 	tln->priv_useCount++;
    361  1.16  rillig 	result = (*proc)(tln->datum, procData);
    362  1.70  rillig 	tln->priv_useCount--;
    363   1.1  rillig 
    364   1.1  rillig 	/*
    365   1.1  rillig 	 * Now check whether a node has been added.
    366   1.1  rillig 	 * Note: this doesn't work if this node was deleted before
    367   1.1  rillig 	 *       the new node was added.
    368   1.1  rillig 	 */
    369  1.15  rillig 	if (next != tln->next) {
    370  1.15  rillig 	    next = tln->next;
    371   1.4  rillig 	    done = 0;
    372   1.1  rillig 	}
    373   1.1  rillig 
    374  1.70  rillig 	if (tln->priv_deleted) {
    375   1.1  rillig 	    free((char *)tln);
    376   1.1  rillig 	}
    377   1.1  rillig 	tln = next;
    378  1.68  rillig 	if (result || LstIsEmpty(list) || done)
    379  1.68  rillig 	    break;
    380  1.68  rillig     }
    381   1.1  rillig 
    382   1.1  rillig     return result;
    383   1.1  rillig }
    384   1.1  rillig 
    385  1.34  rillig /* Move all nodes from list2 to the end of list1.
    386  1.34  rillig  * List2 is destroyed and freed. */
    387  1.34  rillig void
    388  1.65  rillig Lst_MoveAll(List *list1, List *list2)
    389   1.1  rillig {
    390  1.52  rillig     assert(list1 != NULL);
    391  1.52  rillig     assert(list2 != NULL);
    392   1.1  rillig 
    393  1.34  rillig     if (list2->first != NULL) {
    394  1.34  rillig 	list2->first->prev = list1->last;
    395  1.34  rillig 	if (list1->last != NULL) {
    396  1.34  rillig 	    list1->last->next = list2->first;
    397  1.34  rillig 	} else {
    398  1.34  rillig 	    list1->first = list2->first;
    399   1.1  rillig 	}
    400  1.34  rillig 	list1->last = list2->last;
    401   1.1  rillig     }
    402  1.34  rillig     free(list2);
    403   1.1  rillig }
    404   1.1  rillig 
    405  1.22  rillig /* Copy the element data from src to the start of dst. */
    406  1.22  rillig void
    407  1.65  rillig Lst_PrependAll(List *dst, List *src)
    408  1.22  rillig {
    409  1.65  rillig     ListNode *node;
    410  1.22  rillig     for (node = src->last; node != NULL; node = node->prev)
    411  1.50  rillig 	Lst_Prepend(dst, node->datum);
    412  1.22  rillig }
    413  1.22  rillig 
    414  1.22  rillig /* Copy the element data from src to the end of dst. */
    415  1.22  rillig void
    416  1.65  rillig Lst_AppendAll(List *dst, List *src)
    417  1.22  rillig {
    418  1.65  rillig     ListNode *node;
    419  1.22  rillig     for (node = src->first; node != NULL; node = node->next)
    420  1.50  rillig 	Lst_Append(dst, node->datum);
    421  1.22  rillig }
    422   1.1  rillig 
    423   1.1  rillig /*
    424   1.1  rillig  * these functions are for dealing with a list as a table, of sorts.
    425   1.1  rillig  * An idea of the "current element" is kept and used by all the functions
    426   1.1  rillig  * between Lst_Open() and Lst_Close().
    427   1.1  rillig  *
    428   1.1  rillig  * The sequential functions access the list in a slightly different way.
    429   1.1  rillig  * CurPtr points to their idea of the current node in the list and they
    430   1.1  rillig  * access the list based on it.
    431   1.1  rillig  */
    432   1.1  rillig 
    433  1.14  rillig /* Open a list for sequential access. A list can still be searched, etc.,
    434  1.14  rillig  * without confusing these functions. */
    435  1.10  rillig void
    436  1.65  rillig Lst_Open(List *list)
    437  1.10  rillig {
    438  1.52  rillig     assert(list != NULL);
    439  1.70  rillig     assert(!list->priv_isOpen);
    440  1.10  rillig 
    441  1.70  rillig     list->priv_isOpen = TRUE;
    442  1.70  rillig     list->priv_lastAccess = LstIsEmpty(list) ? Head : Unknown;
    443  1.70  rillig     list->priv_curr = NULL;
    444  1.10  rillig }
    445  1.10  rillig 
    446  1.10  rillig /* Return the next node for the given list, or NULL if the end has been
    447  1.10  rillig  * reached. */
    448  1.65  rillig ListNode *
    449  1.65  rillig Lst_Next(List *list)
    450   1.1  rillig {
    451  1.65  rillig     ListNode *node;
    452   1.1  rillig 
    453  1.52  rillig     assert(list != NULL);
    454  1.70  rillig     assert(list->priv_isOpen);
    455   1.1  rillig 
    456  1.70  rillig     list->priv_prev = list->priv_curr;
    457   1.1  rillig 
    458  1.70  rillig     if (list->priv_curr == NULL) {
    459  1.70  rillig 	if (list->priv_lastAccess == Unknown) {
    460   1.1  rillig 	    /*
    461  1.15  rillig 	     * If we're just starting out, lastAccess will be Unknown.
    462   1.1  rillig 	     * Then we want to start this thing off in the right
    463  1.15  rillig 	     * direction -- at the start with lastAccess being Middle.
    464   1.1  rillig 	     */
    465  1.70  rillig 	    list->priv_curr = node = list->first;
    466  1.70  rillig 	    list->priv_lastAccess = Middle;
    467   1.1  rillig 	} else {
    468  1.16  rillig 	    node = NULL;
    469  1.70  rillig 	    list->priv_lastAccess = Tail;
    470   1.1  rillig 	}
    471   1.1  rillig     } else {
    472  1.70  rillig 	node = list->priv_curr->next;
    473  1.70  rillig 	list->priv_curr = node;
    474   1.1  rillig 
    475  1.16  rillig 	if (node == list->first || node == NULL) {
    476   1.1  rillig 	    /*
    477   1.1  rillig 	     * If back at the front, then we've hit the end...
    478   1.1  rillig 	     */
    479  1.70  rillig 	    list->priv_lastAccess = Tail;
    480   1.1  rillig 	} else {
    481   1.1  rillig 	    /*
    482   1.1  rillig 	     * Reset to Middle if gone past first.
    483   1.1  rillig 	     */
    484  1.70  rillig 	    list->priv_lastAccess = Middle;
    485   1.1  rillig 	}
    486   1.1  rillig     }
    487   1.1  rillig 
    488  1.16  rillig     return node;
    489   1.1  rillig }
    490   1.1  rillig 
    491  1.10  rillig /* Close a list which was opened for sequential access. */
    492   1.1  rillig void
    493  1.65  rillig Lst_Close(List *list)
    494   1.1  rillig {
    495  1.52  rillig     assert(list != NULL);
    496  1.70  rillig     assert(list->priv_isOpen);
    497   1.1  rillig 
    498  1.70  rillig     list->priv_isOpen = FALSE;
    499  1.70  rillig     list->priv_lastAccess = Unknown;
    500   1.1  rillig }
    501   1.1  rillig 
    502   1.1  rillig 
    503   1.1  rillig /*
    504   1.1  rillig  * for using the list as a queue
    505   1.1  rillig  */
    506   1.1  rillig 
    507  1.14  rillig /* Add the datum to the tail of the given list. */
    508  1.25  rillig void
    509  1.65  rillig Lst_Enqueue(List *list, void *datum)
    510   1.1  rillig {
    511  1.50  rillig     Lst_Append(list, datum);
    512   1.1  rillig }
    513   1.1  rillig 
    514  1.25  rillig /* Remove and return the datum at the head of the given list. */
    515   1.1  rillig void *
    516  1.65  rillig Lst_Dequeue(List *list)
    517   1.1  rillig {
    518  1.16  rillig     void *datum;
    519   1.1  rillig 
    520  1.52  rillig     assert(list != NULL);
    521  1.25  rillig     assert(!LstIsEmpty(list));
    522   1.1  rillig 
    523  1.25  rillig     datum = list->first->datum;
    524  1.50  rillig     Lst_Remove(list, list->first);
    525  1.25  rillig     assert(datum != NULL);
    526  1.16  rillig     return datum;
    527   1.1  rillig }
    528  1.61  rillig 
    529  1.61  rillig void
    530  1.61  rillig Stack_Init(Stack *stack)
    531  1.61  rillig {
    532  1.61  rillig     stack->len = 0;
    533  1.61  rillig     stack->cap = 10;
    534  1.61  rillig     stack->items = bmake_malloc(stack->cap * sizeof stack->items[0]);
    535  1.61  rillig }
    536  1.61  rillig 
    537  1.61  rillig Boolean Stack_IsEmpty(Stack *stack)
    538  1.61  rillig {
    539  1.61  rillig     return stack->len == 0;
    540  1.61  rillig }
    541  1.61  rillig 
    542  1.61  rillig void Stack_Push(Stack *stack, void *datum)
    543  1.61  rillig {
    544  1.61  rillig     if (stack->len >= stack->cap) {
    545  1.62  rillig 	stack->cap *= 2;
    546  1.61  rillig 	stack->items = bmake_realloc(stack->items,
    547  1.61  rillig 				     stack->cap * sizeof stack->items[0]);
    548  1.61  rillig     }
    549  1.61  rillig     stack->items[stack->len] = datum;
    550  1.61  rillig     stack->len++;
    551  1.61  rillig }
    552  1.61  rillig 
    553  1.61  rillig void *Stack_Pop(Stack *stack)
    554  1.61  rillig {
    555  1.64  rillig     void *datum;
    556  1.64  rillig 
    557  1.61  rillig     assert(stack->len > 0);
    558  1.61  rillig     stack->len--;
    559  1.64  rillig     datum = stack->items[stack->len];
    560  1.64  rillig #ifdef CLEANUP
    561  1.64  rillig     stack->items[stack->len] = NULL;
    562  1.64  rillig #endif
    563  1.64  rillig     return datum;
    564  1.61  rillig }
    565  1.61  rillig 
    566  1.61  rillig void Stack_Done(Stack *stack)
    567  1.61  rillig {
    568  1.61  rillig     free(stack->items);
    569  1.61  rillig }
    570