Home | History | Annotate | Line # | Download | only in make
lst.h revision 1.75
      1 /*	$NetBSD: lst.h,v 1.75 2020/10/21 07:14:22 rillig Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Adam de Boor.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	from: @(#)lst.h	8.1 (Berkeley) 6/6/93
     35  */
     36 
     37 /*
     38  * Copyright (c) 1988, 1989 by Adam de Boor
     39  * Copyright (c) 1989 by Berkeley Softworks
     40  * All rights reserved.
     41  *
     42  * This code is derived from software contributed to Berkeley by
     43  * Adam de Boor.
     44  *
     45  * Redistribution and use in source and binary forms, with or without
     46  * modification, are permitted provided that the following conditions
     47  * are met:
     48  * 1. Redistributions of source code must retain the above copyright
     49  *    notice, this list of conditions and the following disclaimer.
     50  * 2. Redistributions in binary form must reproduce the above copyright
     51  *    notice, this list of conditions and the following disclaimer in the
     52  *    documentation and/or other materials provided with the distribution.
     53  * 3. All advertising materials mentioning features or use of this software
     54  *    must display the following acknowledgement:
     55  *	This product includes software developed by the University of
     56  *	California, Berkeley and its contributors.
     57  * 4. Neither the name of the University nor the names of its contributors
     58  *    may be used to endorse or promote products derived from this software
     59  *    without specific prior written permission.
     60  *
     61  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     62  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     63  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     64  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     65  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     66  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     67  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     68  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     69  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     70  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     71  * SUCH DAMAGE.
     72  *
     73  *	from: @(#)lst.h	8.1 (Berkeley) 6/6/93
     74  */
     75 
     76 /* Doubly-linked lists of arbitrary pointers. */
     77 
     78 #ifndef MAKE_LST_H
     79 #define MAKE_LST_H
     80 
     81 #include <sys/param.h>
     82 #include <stdint.h>
     83 #include <stdlib.h>
     84 
     85 /* A doubly-linked list of pointers. */
     86 typedef	struct List	List;
     87 /* A single node in the doubly-linked list. */
     88 typedef	struct ListNode	ListNode;
     89 
     90 struct ListNode {
     91     ListNode *prev;		/* previous node in list, or NULL */
     92     ListNode *next;		/* next node in list, or NULL */
     93     uint8_t priv_useCount;	/* Count of functions using the node.
     94 				 * node may not be deleted until count
     95 				 * goes to 0 */
     96     Boolean priv_deleted;	/* List node should be removed when done */
     97     union {
     98 	void *datum;		/* datum associated with this element */
     99 	const struct GNode *priv_gnode; /* alias, just for debugging */
    100 	const char *priv_str;	/* alias, just for debugging */
    101     };
    102 };
    103 
    104 typedef enum ListForEachUntilWhere {
    105     Head, Middle, Tail, Unknown
    106 } ListForEachUntilWhere;
    107 
    108 struct List {
    109     ListNode *first;		/* first node in list */
    110     ListNode *last;		/* last node in list */
    111 
    112     /* fields for sequential access */
    113     Boolean priv_isOpen;	/* true if list has been Lst_Open'ed */
    114     ListForEachUntilWhere priv_lastAccess;
    115     ListNode *priv_curr;	/* current node, if open. NULL if
    116 				 * *just* opened */
    117     ListNode *priv_prev;	/* Previous node, if open. Used by Lst_Remove */
    118 };
    119 
    120 /* Copy a node, usually by allocating a copy of the given object.
    121  * For reference-counted objects, the original object may need to be
    122  * modified, therefore the parameter is not const. */
    123 typedef void *LstCopyProc(void *);
    124 /* Free the datum of a node, called before freeing the node itself. */
    125 typedef void LstFreeProc(void *);
    126 /* Return TRUE if the datum matches the args, for Lst_Find. */
    127 typedef Boolean LstFindProc(const void *datum, const void *args);
    128 /* An action for Lst_ForEachUntil. */
    129 typedef int LstActionUntilProc(void *datum, void *args);
    130 
    131 /* Create or destroy a list */
    132 
    133 /* Create a new list. */
    134 List *Lst_New(void);
    135 /* Duplicate an existing list. */
    136 List *Lst_Copy(List *, LstCopyProc);
    137 /* Free the list, leaving the node data unmodified. */
    138 void Lst_Free(List *);
    139 /* Free the list, freeing the node data using the given function. */
    140 void Lst_Destroy(List *, LstFreeProc);
    141 
    142 /* Get information about a list */
    143 
    144 static inline MAKE_ATTR_UNUSED Boolean
    145 Lst_IsEmpty(List *list) { return list->first == NULL; }
    146 
    147 /* Find the first node for which the function returns TRUE, or NULL. */
    148 ListNode *Lst_Find(List *, LstFindProc, const void *);
    149 /* Find the first node for which the function returns TRUE, or NULL.
    150  * The search starts at the given node, towards the end of the list. */
    151 ListNode *Lst_FindFrom(List *, ListNode *, LstFindProc, const void *);
    152 /* Find the first node that contains the given datum, or NULL. */
    153 ListNode *Lst_FindDatum(List *, const void *);
    154 
    155 /* Modify a list */
    156 
    157 /* Insert a datum before the given node. */
    158 void Lst_InsertBefore(List *, ListNode *, void *);
    159 /* Place a datum at the front of the list. */
    160 void Lst_Prepend(List *, void *);
    161 /* Place a datum at the end of the list. */
    162 void Lst_Append(List *, void *);
    163 /* Remove the node from the list. */
    164 void Lst_Remove(List *, ListNode *);
    165 void Lst_PrependAll(List *, List *);
    166 void Lst_AppendAll(List *, List *);
    167 void Lst_MoveAll(List *, List *);
    168 
    169 /* Node-specific functions */
    170 
    171 /* Replace the value of the node. */
    172 void LstNode_Set(ListNode *, void *);
    173 /* Set the value of the node to NULL. Having NULL in a list is unusual. */
    174 void LstNode_SetNull(ListNode *);
    175 
    176 /* Iterating over a list, using a callback function */
    177 
    178 /* Apply a function to each datum of the list, until the callback function
    179  * returns non-zero. */
    180 int Lst_ForEachUntil(List *, LstActionUntilProc, void *);
    181 
    182 /* Iterating over a list while keeping track of the current node and possible
    183  * concurrent modifications */
    184 
    185 /* Start iterating the list. */
    186 void Lst_Open(List *);
    187 /* Return the next node, or NULL. */
    188 ListNode *Lst_Next(List *);
    189 /* Finish iterating the list. */
    190 void Lst_Close(List *);
    191 
    192 /* Using the list as a queue */
    193 
    194 /* Add a datum at the tail of the queue. */
    195 void Lst_Enqueue(List *, void *);
    196 /* Remove the head node of the queue and return its datum. */
    197 void *Lst_Dequeue(List *);
    198 
    199 /* A vector is an ordered collection of items, allowing fast indexed access. */
    200 typedef struct Vector {
    201     void **items;
    202     size_t len;
    203     size_t cap;
    204 } Vector;
    205 
    206 void Vector_Init(Vector *);
    207 Boolean Vector_IsEmpty(Vector *);
    208 void Vector_Push(Vector *, void *);
    209 void *Vector_Pop(Vector *);
    210 void Vector_Done(Vector *);
    211 
    212 #endif /* MAKE_LST_H */
    213