Home | History | Annotate | Line # | Download | only in make
lst.c revision 1.16
      1  1.16  rillig /* $NetBSD: lst.c,v 1.16 2020/08/21 07:00:32 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.8  rillig #include <assert.h>
     36   1.8  rillig 
     37   1.1  rillig #include "lst.h"
     38   1.1  rillig #include "make_malloc.h"
     39   1.1  rillig 
     40   1.1  rillig #ifndef MAKE_NATIVE
     41  1.16  rillig static char rcsid[] = "$NetBSD: lst.c,v 1.16 2020/08/21 07:00:32 rillig Exp $";
     42   1.1  rillig #else
     43   1.1  rillig #include <sys/cdefs.h>
     44   1.1  rillig #ifndef lint
     45  1.16  rillig __RCSID("$NetBSD: lst.c,v 1.16 2020/08/21 07:00:32 rillig Exp $");
     46   1.1  rillig #endif /* not lint */
     47   1.1  rillig #endif
     48   1.1  rillig 
     49  1.13  rillig struct ListNode {
     50  1.15  rillig     struct ListNode *prev;	/* previous element in list */
     51  1.15  rillig     struct ListNode *next;	/* next in list */
     52   1.7  rillig     uint8_t useCount;		/* Count of functions using the node.
     53   1.4  rillig 				 * node may not be deleted until count
     54   1.4  rillig 				 * goes to 0 */
     55   1.7  rillig     Boolean deleted;		/* List node should be removed when done */
     56   1.4  rillig     void *datum;		/* datum associated with this element */
     57  1.13  rillig };
     58   1.1  rillig 
     59   1.1  rillig typedef enum {
     60   1.1  rillig     Head, Middle, Tail, Unknown
     61   1.1  rillig } Where;
     62   1.1  rillig 
     63  1.13  rillig struct List {
     64  1.15  rillig     LstNode first;		/* first node in list */
     65  1.15  rillig     LstNode last;		/* last node in list */
     66   1.1  rillig /*
     67   1.1  rillig  * fields for sequential access
     68   1.1  rillig  */
     69  1.15  rillig     Where lastAccess;		/* Where in the list the last access was */
     70   1.4  rillig     Boolean isOpen;		/* true if list has been Lst_Open'ed */
     71  1.15  rillig     LstNode curr;		/* current node, if open. NULL if
     72   1.4  rillig 				 * *just* opened */
     73  1.15  rillig     LstNode prev;		/* Previous node, if open. Used by
     74   1.4  rillig 				 * Lst_Remove */
     75  1.13  rillig };
     76   1.1  rillig 
     77  1.14  rillig /* Return TRUE if the list is valid. */
     78   1.2  rillig static Boolean
     79  1.16  rillig LstValid(Lst list)
     80   1.2  rillig {
     81  1.16  rillig     return list != NULL;
     82   1.2  rillig }
     83   1.1  rillig 
     84  1.14  rillig /* Return TRUE if the list node is valid. */
     85   1.2  rillig static Boolean
     86  1.16  rillig LstNodeValid(LstNode node)
     87   1.2  rillig {
     88  1.16  rillig     return node != NULL;
     89   1.2  rillig }
     90   1.1  rillig 
     91  1.12  rillig static LstNode
     92  1.12  rillig LstNodeNew(void *datum)
     93  1.12  rillig {
     94  1.16  rillig     LstNode node = bmake_malloc(sizeof *node);
     95  1.15  rillig     /* prev will be initialized by the calling code. */
     96  1.15  rillig     /* next will be initialized by the calling code. */
     97  1.16  rillig     node->useCount = 0;
     98  1.16  rillig     node->deleted = FALSE;
     99  1.16  rillig     node->datum = datum;
    100  1.16  rillig     return node;
    101  1.12  rillig }
    102  1.12  rillig 
    103  1.14  rillig /* Return TRUE if the list is empty. */
    104   1.2  rillig static Boolean
    105  1.16  rillig LstIsEmpty(Lst list)
    106   1.2  rillig {
    107  1.16  rillig     return list->first == NULL;
    108   1.2  rillig }
    109   1.1  rillig 
    110   1.5  rillig /* Create and initialize a new, empty list. */
    111   1.1  rillig Lst
    112   1.5  rillig Lst_Init(void)
    113   1.1  rillig {
    114  1.16  rillig     Lst list = bmake_malloc(sizeof *list);
    115   1.1  rillig 
    116  1.16  rillig     list->first = NULL;
    117  1.16  rillig     list->last = NULL;
    118  1.16  rillig     list->isOpen = FALSE;
    119  1.16  rillig     list->lastAccess = Unknown;
    120   1.1  rillig 
    121  1.16  rillig     return list;
    122   1.1  rillig }
    123   1.1  rillig 
    124  1.14  rillig /* Duplicate an entire list, usually by copying the datum pointers.
    125  1.14  rillig  * If copyProc is given, that function is used to create the new datum from the
    126  1.14  rillig  * old datum, usually by creating a copy of it.
    127  1.14  rillig  * Return the new list, or NULL on failure. */
    128   1.1  rillig Lst
    129  1.16  rillig Lst_Duplicate(Lst list, DuplicateProc *copyProc)
    130   1.1  rillig {
    131  1.16  rillig     Lst newList;
    132  1.16  rillig     LstNode node;
    133   1.1  rillig 
    134  1.16  rillig     if (!LstValid(list)) {
    135   1.1  rillig 	return NULL;
    136   1.1  rillig     }
    137   1.1  rillig 
    138  1.16  rillig     newList = Lst_Init();
    139  1.16  rillig     if (newList == NULL) {
    140   1.1  rillig 	return NULL;
    141   1.1  rillig     }
    142   1.1  rillig 
    143  1.16  rillig     node = list->first;
    144  1.16  rillig     while (node != NULL) {
    145   1.1  rillig 	if (copyProc != NULL) {
    146  1.16  rillig 	    if (Lst_AtEnd(newList, copyProc(node->datum)) == FAILURE) {
    147   1.1  rillig 		return NULL;
    148   1.1  rillig 	    }
    149  1.16  rillig 	} else if (Lst_AtEnd(newList, node->datum) == FAILURE) {
    150   1.1  rillig 	    return NULL;
    151   1.1  rillig 	}
    152   1.1  rillig 
    153  1.16  rillig 	node = node->next;
    154   1.1  rillig     }
    155   1.1  rillig 
    156  1.16  rillig     return newList;
    157   1.1  rillig }
    158   1.1  rillig 
    159  1.14  rillig /* Destroy a list and free all its resources. If the freeProc is given, it is
    160  1.14  rillig  * called with the datum from each node in turn before the node is freed. */
    161   1.1  rillig void
    162   1.1  rillig Lst_Destroy(Lst list, FreeProc *freeProc)
    163   1.1  rillig {
    164  1.16  rillig     LstNode node;
    165  1.16  rillig     LstNode next = NULL;
    166   1.1  rillig 
    167   1.1  rillig     if (list == NULL)
    168   1.1  rillig 	return;
    169   1.1  rillig 
    170   1.1  rillig     /* To ease scanning */
    171  1.15  rillig     if (list->last != NULL)
    172  1.15  rillig 	list->last->next = NULL;
    173   1.1  rillig     else {
    174   1.1  rillig 	free(list);
    175   1.1  rillig 	return;
    176   1.1  rillig     }
    177   1.1  rillig 
    178   1.1  rillig     if (freeProc) {
    179  1.16  rillig 	for (node = list->first; node != NULL; node = next) {
    180  1.16  rillig 	    next = node->next;
    181  1.16  rillig 	    freeProc(node->datum);
    182  1.16  rillig 	    free(node);
    183   1.1  rillig 	}
    184   1.1  rillig     } else {
    185  1.16  rillig 	for (node = list->first; node != NULL; node = next) {
    186  1.16  rillig 	    next = node->next;
    187  1.16  rillig 	    free(node);
    188   1.1  rillig 	}
    189   1.1  rillig     }
    190   1.1  rillig 
    191   1.1  rillig     free(list);
    192   1.1  rillig }
    193   1.1  rillig 
    194   1.1  rillig /*
    195   1.1  rillig  * Functions to modify a list
    196   1.1  rillig  */
    197   1.1  rillig 
    198  1.14  rillig /* Insert a new node with the given piece of data before the given node in the
    199  1.14  rillig  * given list. */
    200   1.1  rillig ReturnStatus
    201  1.16  rillig Lst_InsertBefore(Lst list, LstNode node, void *datum)
    202   1.1  rillig {
    203  1.16  rillig     LstNode newNode;
    204   1.1  rillig 
    205   1.1  rillig     /*
    206   1.1  rillig      * check validity of arguments
    207   1.1  rillig      */
    208  1.16  rillig     if (LstValid(list) && (LstIsEmpty(list) && node == NULL))
    209   1.1  rillig 	goto ok;
    210   1.1  rillig 
    211  1.16  rillig     if (!LstValid(list) || LstIsEmpty(list) || !LstNodeValid(node)) {
    212   1.1  rillig 	return FAILURE;
    213   1.1  rillig     }
    214   1.1  rillig 
    215   1.1  rillig     ok:
    216  1.16  rillig     newNode = LstNodeNew(datum);
    217   1.1  rillig 
    218  1.16  rillig     if (node == NULL) {
    219  1.16  rillig 	newNode->prev = newNode->next = NULL;
    220  1.16  rillig 	list->first = list->last = newNode;
    221   1.1  rillig     } else {
    222  1.16  rillig 	newNode->prev = node->prev;
    223  1.16  rillig 	newNode->next = node;
    224   1.1  rillig 
    225  1.16  rillig 	if (newNode->prev != NULL) {
    226  1.16  rillig 	    newNode->prev->next = newNode;
    227   1.1  rillig 	}
    228  1.16  rillig 	node->prev = newNode;
    229   1.1  rillig 
    230  1.16  rillig 	if (node == list->first) {
    231  1.16  rillig 	    list->first = newNode;
    232   1.1  rillig 	}
    233   1.1  rillig     }
    234   1.1  rillig 
    235   1.1  rillig     return SUCCESS;
    236   1.1  rillig }
    237   1.1  rillig 
    238  1.14  rillig /* Insert a new node with the given piece of data after the given node in the
    239  1.14  rillig  * given list. */
    240   1.1  rillig ReturnStatus
    241  1.16  rillig Lst_InsertAfter(Lst list, LstNode node, void *datum)
    242   1.1  rillig {
    243  1.13  rillig     LstNode nLNode;
    244   1.1  rillig 
    245  1.16  rillig     if (LstValid(list) && (node == NULL && LstIsEmpty(list))) {
    246   1.1  rillig 	goto ok;
    247   1.1  rillig     }
    248   1.1  rillig 
    249  1.16  rillig     if (!LstValid(list) || LstIsEmpty(list) || !LstNodeValid(node)) {
    250   1.1  rillig 	return FAILURE;
    251   1.1  rillig     }
    252   1.1  rillig     ok:
    253   1.1  rillig 
    254  1.16  rillig     nLNode = LstNodeNew(datum);
    255   1.1  rillig 
    256  1.16  rillig     if (node == NULL) {
    257  1.15  rillig 	nLNode->next = nLNode->prev = NULL;
    258  1.15  rillig 	list->first = list->last = nLNode;
    259   1.1  rillig     } else {
    260  1.16  rillig 	nLNode->prev = node;
    261  1.16  rillig 	nLNode->next = node->next;
    262   1.1  rillig 
    263  1.16  rillig 	node->next = nLNode;
    264  1.15  rillig 	if (nLNode->next != NULL) {
    265  1.15  rillig 	    nLNode->next->prev = nLNode;
    266   1.1  rillig 	}
    267   1.1  rillig 
    268  1.16  rillig 	if (node == list->last) {
    269  1.15  rillig 	    list->last = nLNode;
    270   1.1  rillig 	}
    271   1.1  rillig     }
    272   1.1  rillig 
    273   1.1  rillig     return SUCCESS;
    274   1.1  rillig }
    275   1.1  rillig 
    276  1.14  rillig /* Add a piece of data at the front of the given list. */
    277   1.1  rillig ReturnStatus
    278  1.16  rillig Lst_AtFront(Lst list, void *datum)
    279   1.1  rillig {
    280  1.16  rillig     LstNode front = Lst_First(list);
    281  1.16  rillig     return Lst_InsertBefore(list, front, datum);
    282   1.1  rillig }
    283   1.1  rillig 
    284  1.14  rillig /* Add a piece of data at the end of the given list. */
    285   1.1  rillig ReturnStatus
    286  1.16  rillig Lst_AtEnd(Lst list, void *datum)
    287   1.1  rillig {
    288  1.16  rillig     LstNode end = Lst_Last(list);
    289  1.16  rillig     return Lst_InsertAfter(list, end, datum);
    290   1.1  rillig }
    291   1.1  rillig 
    292   1.8  rillig /* Remove the given node from the given list.
    293   1.8  rillig  * The datum stored in the node must be freed by the caller, if necessary. */
    294   1.8  rillig void
    295  1.16  rillig Lst_RemoveS(Lst list, LstNode node)
    296   1.1  rillig {
    297  1.16  rillig     assert(LstValid(list));
    298  1.16  rillig     assert(LstNodeValid(node));
    299   1.1  rillig 
    300   1.1  rillig     /*
    301   1.1  rillig      * unlink it from the list
    302   1.1  rillig      */
    303  1.16  rillig     if (node->next != NULL) {
    304  1.16  rillig 	node->next->prev = node->prev;
    305   1.1  rillig     }
    306  1.16  rillig     if (node->prev != NULL) {
    307  1.16  rillig 	node->prev->next = node->next;
    308   1.1  rillig     }
    309   1.1  rillig 
    310   1.1  rillig     /*
    311  1.15  rillig      * if either the first or last of the list point to this node,
    312   1.1  rillig      * adjust them accordingly
    313   1.1  rillig      */
    314  1.16  rillig     if (list->first == node) {
    315  1.16  rillig 	list->first = node->next;
    316   1.1  rillig     }
    317  1.16  rillig     if (list->last == node) {
    318  1.16  rillig 	list->last = node->prev;
    319   1.1  rillig     }
    320   1.1  rillig 
    321   1.1  rillig     /*
    322   1.1  rillig      * Sequential access stuff. If the node we're removing is the current
    323   1.1  rillig      * node in the list, reset the current node to the previous one. If the
    324  1.15  rillig      * previous one was non-existent (prev == NULL), we set the
    325   1.1  rillig      * end to be Unknown, since it is.
    326   1.1  rillig      */
    327  1.16  rillig     if (list->isOpen && list->curr == node) {
    328  1.15  rillig 	list->curr = list->prev;
    329  1.15  rillig 	if (list->curr == NULL) {
    330  1.15  rillig 	    list->lastAccess = Unknown;
    331   1.1  rillig 	}
    332   1.1  rillig     }
    333   1.1  rillig 
    334   1.1  rillig     /*
    335   1.1  rillig      * note that the datum is unmolested. The caller must free it as
    336   1.1  rillig      * necessary and as expected.
    337   1.1  rillig      */
    338  1.16  rillig     if (node->useCount == 0) {
    339  1.16  rillig 	free(node);
    340   1.1  rillig     } else {
    341  1.16  rillig 	node->deleted = TRUE;
    342   1.1  rillig     }
    343   1.1  rillig }
    344   1.1  rillig 
    345   1.8  rillig /* Replace the datum in the given node with the new datum. */
    346   1.8  rillig void
    347  1.16  rillig Lst_ReplaceS(LstNode node, void *datum)
    348   1.1  rillig {
    349  1.16  rillig     node->datum = datum;
    350   1.1  rillig }
    351   1.1  rillig 
    352   1.1  rillig 
    353   1.1  rillig /*
    354   1.1  rillig  * Node-specific functions
    355   1.1  rillig  */
    356   1.1  rillig 
    357  1.14  rillig /* Return the first node from the given list, or NULL if the list is empty or
    358  1.14  rillig  * invalid. */
    359   1.1  rillig LstNode
    360  1.16  rillig Lst_First(Lst list)
    361   1.1  rillig {
    362  1.16  rillig     if (!LstValid(list) || LstIsEmpty(list)) {
    363   1.1  rillig 	return NULL;
    364   1.1  rillig     } else {
    365  1.16  rillig 	return list->first;
    366   1.1  rillig     }
    367   1.1  rillig }
    368   1.1  rillig 
    369  1.14  rillig /* Return the last node from the given list, or NULL if the list is empty or
    370  1.14  rillig  * invalid. */
    371   1.1  rillig LstNode
    372  1.16  rillig Lst_Last(Lst list)
    373   1.1  rillig {
    374  1.16  rillig     if (!LstValid(list) || LstIsEmpty(list)) {
    375   1.1  rillig 	return NULL;
    376   1.1  rillig     } else {
    377  1.16  rillig 	return list->last;
    378   1.1  rillig     }
    379   1.1  rillig }
    380   1.1  rillig 
    381   1.6  rillig /* Return the successor to the given node on its list, or NULL. */
    382   1.1  rillig LstNode
    383  1.16  rillig Lst_Succ(LstNode node)
    384   1.1  rillig {
    385  1.16  rillig     if (node == NULL) {
    386   1.1  rillig 	return NULL;
    387   1.1  rillig     } else {
    388  1.16  rillig 	return node->next;
    389   1.1  rillig     }
    390   1.1  rillig }
    391   1.1  rillig 
    392   1.6  rillig /* Return the predecessor to the given node on its list, or NULL. */
    393   1.1  rillig LstNode
    394  1.16  rillig Lst_Prev(LstNode node)
    395   1.1  rillig {
    396  1.16  rillig     if (node == NULL) {
    397   1.1  rillig 	return NULL;
    398   1.1  rillig     } else {
    399  1.16  rillig 	return node->prev;
    400   1.1  rillig     }
    401   1.1  rillig }
    402   1.1  rillig 
    403  1.14  rillig /* Return the datum stored in the given node, or NULL if the node is invalid. */
    404   1.1  rillig void *
    405  1.16  rillig Lst_Datum(LstNode node)
    406   1.1  rillig {
    407  1.16  rillig     if (node != NULL) {
    408  1.16  rillig 	return node->datum;
    409   1.1  rillig     } else {
    410   1.1  rillig 	return NULL;
    411   1.1  rillig     }
    412   1.1  rillig }
    413   1.1  rillig 
    414   1.1  rillig 
    415   1.1  rillig /*
    416   1.1  rillig  * Functions for entire lists
    417   1.1  rillig  */
    418   1.1  rillig 
    419  1.14  rillig /* Return TRUE if the given list is empty or invalid. */
    420   1.1  rillig Boolean
    421  1.16  rillig Lst_IsEmpty(Lst list)
    422   1.1  rillig {
    423  1.16  rillig     return !LstValid(list) || LstIsEmpty(list);
    424   1.1  rillig }
    425   1.1  rillig 
    426  1.14  rillig /* Return the first node from the given list for which the given comparison
    427  1.14  rillig  * function returns 0, or NULL if none of the nodes matches. */
    428   1.1  rillig LstNode
    429  1.16  rillig Lst_Find(Lst list, const void *cmpData, int (*cmp)(const void *, const void *))
    430   1.1  rillig {
    431  1.16  rillig     return Lst_FindFrom(list, Lst_First(list), cmpData, cmp);
    432   1.1  rillig }
    433   1.1  rillig 
    434  1.14  rillig /* Return the first node from the given list, starting at the given node, for
    435  1.14  rillig  * which the given comparison function returns 0, or NULL if none of the nodes
    436  1.14  rillig  * matches. */
    437   1.1  rillig LstNode
    438  1.16  rillig Lst_FindFrom(Lst list, LstNode node, const void *cmpData,
    439  1.16  rillig 	     int (*cmp)(const void *, const void *))
    440   1.1  rillig {
    441  1.13  rillig     LstNode tln;
    442   1.1  rillig 
    443  1.16  rillig     if (!LstValid(list) || LstIsEmpty(list) || !LstNodeValid(node)) {
    444   1.1  rillig 	return NULL;
    445   1.1  rillig     }
    446   1.1  rillig 
    447  1.16  rillig     tln = node;
    448   1.1  rillig 
    449   1.1  rillig     do {
    450  1.16  rillig 	if ((*cmp)(tln->datum, cmpData) == 0)
    451   1.1  rillig 	    return tln;
    452  1.15  rillig 	tln = tln->next;
    453  1.16  rillig     } while (tln != node && tln != NULL);
    454   1.1  rillig 
    455   1.1  rillig     return NULL;
    456   1.1  rillig }
    457   1.1  rillig 
    458  1.14  rillig /* Return the first node that contains the given datum, or NULL. */
    459   1.1  rillig LstNode
    460  1.16  rillig Lst_Member(Lst list, void *datum)
    461   1.1  rillig {
    462  1.16  rillig     LstNode node;
    463   1.1  rillig 
    464   1.1  rillig     if (list == NULL) {
    465   1.1  rillig 	return NULL;
    466   1.1  rillig     }
    467  1.16  rillig     node = list->first;
    468  1.16  rillig     if (node == NULL) {
    469   1.1  rillig 	return NULL;
    470   1.1  rillig     }
    471   1.1  rillig 
    472   1.1  rillig     do {
    473  1.16  rillig 	if (node->datum == datum) {
    474  1.16  rillig 	    return node;
    475   1.1  rillig 	}
    476  1.16  rillig 	node = node->next;
    477  1.16  rillig     } while (node != NULL && node != list->first);
    478   1.1  rillig 
    479   1.1  rillig     return NULL;
    480   1.1  rillig }
    481   1.1  rillig 
    482  1.14  rillig /* Apply the given function to each element of the given list. The function
    483  1.14  rillig  * should return 0 if traversal should continue and non-zero if it should
    484  1.14  rillig  * abort. */
    485   1.1  rillig int
    486  1.16  rillig Lst_ForEach(Lst list, int (*proc)(void *, void *), void *procData)
    487   1.1  rillig {
    488  1.16  rillig     return Lst_ForEachFrom(list, Lst_First(list), proc, procData);
    489   1.1  rillig }
    490   1.1  rillig 
    491  1.14  rillig /* Apply the given function to each element of the given list, starting from
    492  1.14  rillig  * the given node. The function should return 0 if traversal should continue,
    493  1.14  rillig  * and non-zero if it should abort. */
    494   1.1  rillig int
    495  1.16  rillig Lst_ForEachFrom(Lst list, LstNode node,
    496  1.16  rillig 		int (*proc)(void *, void *), void *procData)
    497   1.1  rillig {
    498  1.16  rillig     LstNode tln = node;
    499  1.13  rillig     LstNode next;
    500   1.4  rillig     Boolean done;
    501   1.4  rillig     int result;
    502   1.1  rillig 
    503   1.4  rillig     if (!LstValid(list) || LstIsEmpty(list)) {
    504   1.1  rillig 	return 0;
    505   1.1  rillig     }
    506   1.1  rillig 
    507   1.1  rillig     do {
    508   1.1  rillig 	/*
    509   1.1  rillig 	 * Take care of having the current element deleted out from under
    510   1.1  rillig 	 * us.
    511   1.1  rillig 	 */
    512   1.1  rillig 
    513  1.15  rillig 	next = tln->next;
    514   1.1  rillig 
    515   1.1  rillig 	/*
    516   1.1  rillig 	 * We're done with the traversal if
    517   1.1  rillig 	 *  - the next node to examine is the first in the queue or
    518   1.1  rillig 	 *    doesn't exist and
    519   1.1  rillig 	 *  - nothing's been added after the current node (check this
    520   1.1  rillig 	 *    after proc() has been called).
    521   1.1  rillig 	 */
    522  1.15  rillig 	done = (next == NULL || next == list->first);
    523   1.1  rillig 
    524   1.4  rillig 	(void)tln->useCount++;
    525  1.16  rillig 	result = (*proc)(tln->datum, procData);
    526   1.4  rillig 	(void)tln->useCount--;
    527   1.1  rillig 
    528   1.1  rillig 	/*
    529   1.1  rillig 	 * Now check whether a node has been added.
    530   1.1  rillig 	 * Note: this doesn't work if this node was deleted before
    531   1.1  rillig 	 *       the new node was added.
    532   1.1  rillig 	 */
    533  1.15  rillig 	if (next != tln->next) {
    534  1.15  rillig 	    next = tln->next;
    535   1.4  rillig 	    done = 0;
    536   1.1  rillig 	}
    537   1.1  rillig 
    538   1.7  rillig 	if (tln->deleted) {
    539   1.1  rillig 	    free((char *)tln);
    540   1.1  rillig 	}
    541   1.1  rillig 	tln = next;
    542   1.1  rillig     } while (!result && !LstIsEmpty(list) && !done);
    543   1.1  rillig 
    544   1.1  rillig     return result;
    545   1.1  rillig }
    546   1.1  rillig 
    547  1.14  rillig /* Concatenate two lists. New nodes are created to hold the data elements,
    548  1.14  rillig  * if specified, but the data themselves are not copied. If the data
    549  1.14  rillig  * should be duplicated to avoid confusion with another list, the Lst_Duplicate
    550  1.14  rillig  * function should be called first. If LST_CONCLINK is specified, the second
    551  1.14  rillig  * list is destroyed since its pointers have been corrupted and the list is no
    552  1.14  rillig  * longer usable.
    553   1.1  rillig  *
    554   1.1  rillig  * Input:
    555  1.16  rillig  *	list1		The list to which list2 is to be appended
    556  1.16  rillig  *	list2		The list to append to list1
    557  1.14  rillig  *	flags		LST_CONCNEW if the list nodes should be duplicated
    558  1.14  rillig  *			LST_CONCLINK if the list nodes should just be relinked
    559   1.1  rillig  */
    560   1.1  rillig ReturnStatus
    561  1.16  rillig Lst_Concat(Lst list1, Lst list2, int flags)
    562   1.1  rillig {
    563  1.16  rillig     LstNode node;	/* original node */
    564  1.16  rillig     LstNode newNode;
    565  1.16  rillig     LstNode last;	/* the last element in the list.
    566  1.16  rillig 			 * Keeps bookkeeping until the end */
    567   1.1  rillig 
    568  1.16  rillig     if (!LstValid(list1) || !LstValid(list2)) {
    569   1.1  rillig 	return FAILURE;
    570   1.1  rillig     }
    571   1.1  rillig 
    572   1.1  rillig     if (flags == LST_CONCLINK) {
    573  1.15  rillig 	if (list2->first != NULL) {
    574   1.1  rillig 	    /*
    575   1.1  rillig 	     * So long as the second list isn't empty, we just link the
    576   1.1  rillig 	     * first element of the second list to the last element of the
    577   1.1  rillig 	     * first list. If the first list isn't empty, we then link the
    578   1.1  rillig 	     * last element of the list to the first element of the second list
    579   1.1  rillig 	     * The last element of the second list, if it exists, then becomes
    580   1.1  rillig 	     * the last element of the first list.
    581   1.1  rillig 	     */
    582  1.15  rillig 	    list2->first->prev = list1->last;
    583  1.15  rillig 	    if (list1->last != NULL) {
    584  1.15  rillig 		list1->last->next = list2->first;
    585   1.1  rillig 	    } else {
    586  1.15  rillig 		list1->first = list2->first;
    587   1.1  rillig 	    }
    588  1.15  rillig 	    list1->last = list2->last;
    589   1.1  rillig 	}
    590  1.16  rillig 	free(list2);
    591  1.15  rillig     } else if (list2->first != NULL) {
    592   1.1  rillig 	/*
    593  1.15  rillig 	 * We set the 'next' of the last element of list 2 to be nil to make
    594   1.1  rillig 	 * the loop less difficult. The loop simply goes through the entire
    595  1.15  rillig 	 * second list creating new LstNodes and filling in the 'next', and
    596  1.16  rillig 	 * 'prev' to fit into list1 and its datum field from the
    597  1.16  rillig 	 * datum field of the corresponding element in list2. The 'last' node
    598  1.16  rillig 	 * follows the last of the new nodes along until the entire list2 has
    599   1.1  rillig 	 * been appended. Only then does the bookkeeping catch up with the
    600   1.1  rillig 	 * changes. During the first iteration of the loop, if 'last' is nil,
    601   1.1  rillig 	 * the first list must have been empty so the newly-created node is
    602   1.1  rillig 	 * made the first node of the list.
    603   1.1  rillig 	 */
    604  1.15  rillig 	list2->last->next = NULL;
    605  1.16  rillig 	for (last = list1->last, node = list2->first;
    606  1.16  rillig 	     node != NULL;
    607  1.16  rillig 	     node = node->next)
    608  1.16  rillig 	{
    609  1.16  rillig 	    newNode = LstNodeNew(node->datum);
    610   1.1  rillig 	    if (last != NULL) {
    611  1.16  rillig 		last->next = newNode;
    612   1.1  rillig 	    } else {
    613  1.16  rillig 		list1->first = newNode;
    614   1.1  rillig 	    }
    615  1.16  rillig 	    newNode->prev = last;
    616  1.16  rillig 	    last = newNode;
    617   1.1  rillig 	}
    618   1.1  rillig 
    619   1.1  rillig 	/*
    620   1.1  rillig 	 * Finish bookkeeping. The last new element becomes the last element
    621   1.1  rillig 	 * of list one.
    622   1.1  rillig 	 */
    623  1.15  rillig 	list1->last = last;
    624  1.15  rillig 	last->next = NULL;
    625   1.1  rillig     }
    626   1.1  rillig 
    627   1.1  rillig     return SUCCESS;
    628   1.1  rillig }
    629   1.1  rillig 
    630   1.1  rillig 
    631   1.1  rillig /*
    632   1.1  rillig  * these functions are for dealing with a list as a table, of sorts.
    633   1.1  rillig  * An idea of the "current element" is kept and used by all the functions
    634   1.1  rillig  * between Lst_Open() and Lst_Close().
    635   1.1  rillig  *
    636   1.1  rillig  * The sequential functions access the list in a slightly different way.
    637   1.1  rillig  * CurPtr points to their idea of the current node in the list and they
    638   1.1  rillig  * access the list based on it.
    639   1.1  rillig  */
    640   1.1  rillig 
    641  1.14  rillig /* Open a list for sequential access. A list can still be searched, etc.,
    642  1.14  rillig  * without confusing these functions. */
    643   1.1  rillig ReturnStatus
    644  1.16  rillig Lst_Open(Lst list)
    645   1.1  rillig {
    646  1.16  rillig     if (!LstValid(list)) {
    647   1.4  rillig 	return FAILURE;
    648   1.4  rillig     }
    649  1.16  rillig     list->isOpen = TRUE;
    650  1.16  rillig     list->lastAccess = LstIsEmpty(list) ? Head : Unknown;
    651  1.16  rillig     list->curr = NULL;
    652   1.1  rillig 
    653   1.4  rillig     return SUCCESS;
    654   1.1  rillig }
    655   1.1  rillig 
    656  1.10  rillig /* Open a list for sequential access. A list can still be searched, etc.,
    657  1.10  rillig  * without confusing these functions. */
    658  1.10  rillig void
    659  1.16  rillig Lst_OpenS(Lst list)
    660  1.10  rillig {
    661  1.16  rillig     assert(LstValid(list));
    662  1.16  rillig     assert(!list->isOpen);
    663  1.10  rillig 
    664  1.16  rillig     list->isOpen = TRUE;
    665  1.16  rillig     list->lastAccess = LstIsEmpty(list) ? Head : Unknown;
    666  1.16  rillig     list->curr = NULL;
    667  1.10  rillig }
    668  1.10  rillig 
    669  1.10  rillig /* Return the next node for the given list, or NULL if the end has been
    670  1.10  rillig  * reached. */
    671   1.1  rillig LstNode
    672  1.16  rillig Lst_NextS(Lst list)
    673   1.1  rillig {
    674  1.16  rillig     LstNode node;
    675   1.1  rillig 
    676  1.16  rillig     assert(LstValid(list));
    677   1.9  rillig     assert(list->isOpen);
    678   1.1  rillig 
    679  1.15  rillig     list->prev = list->curr;
    680   1.1  rillig 
    681  1.15  rillig     if (list->curr == NULL) {
    682  1.15  rillig 	if (list->lastAccess == Unknown) {
    683   1.1  rillig 	    /*
    684  1.15  rillig 	     * If we're just starting out, lastAccess will be Unknown.
    685   1.1  rillig 	     * Then we want to start this thing off in the right
    686  1.15  rillig 	     * direction -- at the start with lastAccess being Middle.
    687   1.1  rillig 	     */
    688  1.16  rillig 	    list->curr = node = list->first;
    689  1.15  rillig 	    list->lastAccess = Middle;
    690   1.1  rillig 	} else {
    691  1.16  rillig 	    node = NULL;
    692  1.15  rillig 	    list->lastAccess = Tail;
    693   1.1  rillig 	}
    694   1.1  rillig     } else {
    695  1.16  rillig 	node = list->curr->next;
    696  1.16  rillig 	list->curr = node;
    697   1.1  rillig 
    698  1.16  rillig 	if (node == list->first || node == NULL) {
    699   1.1  rillig 	    /*
    700   1.1  rillig 	     * If back at the front, then we've hit the end...
    701   1.1  rillig 	     */
    702  1.15  rillig 	    list->lastAccess = Tail;
    703   1.1  rillig 	} else {
    704   1.1  rillig 	    /*
    705   1.1  rillig 	     * Reset to Middle if gone past first.
    706   1.1  rillig 	     */
    707  1.15  rillig 	    list->lastAccess = Middle;
    708   1.1  rillig 	}
    709   1.1  rillig     }
    710   1.1  rillig 
    711  1.16  rillig     return node;
    712   1.1  rillig }
    713   1.1  rillig 
    714  1.10  rillig /* Close a list which was opened for sequential access. */
    715   1.1  rillig void
    716  1.16  rillig Lst_CloseS(Lst list)
    717   1.1  rillig {
    718  1.16  rillig     assert(LstValid(list));
    719  1.16  rillig     assert(list->isOpen);
    720   1.1  rillig 
    721  1.10  rillig     list->isOpen = FALSE;
    722  1.15  rillig     list->lastAccess = Unknown;
    723   1.1  rillig }
    724   1.1  rillig 
    725   1.1  rillig 
    726   1.1  rillig /*
    727   1.1  rillig  * for using the list as a queue
    728   1.1  rillig  */
    729   1.1  rillig 
    730  1.14  rillig /* Add the datum to the tail of the given list. */
    731   1.1  rillig ReturnStatus
    732  1.16  rillig Lst_EnQueue(Lst list, void *datum)
    733   1.1  rillig {
    734  1.16  rillig     if (!LstValid(list)) {
    735   1.1  rillig 	return FAILURE;
    736   1.1  rillig     }
    737   1.1  rillig 
    738  1.16  rillig     return Lst_InsertAfter(list, Lst_Last(list), datum);
    739   1.1  rillig }
    740   1.1  rillig 
    741  1.14  rillig /* Remove and return the datum at the head of the given list, or NULL if the
    742  1.14  rillig  * list is empty. */
    743   1.1  rillig void *
    744  1.16  rillig Lst_DeQueue(Lst list)
    745   1.1  rillig {
    746  1.16  rillig     LstNode head;
    747  1.16  rillig     void *datum;
    748   1.1  rillig 
    749  1.16  rillig     head = Lst_First(list);
    750  1.16  rillig     if (head == NULL) {
    751   1.1  rillig 	return NULL;
    752   1.1  rillig     }
    753   1.1  rillig 
    754  1.16  rillig     datum = head->datum;
    755  1.16  rillig     Lst_RemoveS(list, head);
    756  1.16  rillig     return datum;
    757   1.1  rillig }
    758