Home | History | Annotate | Line # | Download | only in make
lst.h revision 1.93
      1  1.93    rillig /*	$NetBSD: lst.h,v 1.93 2020/12/13 20:57:17 rillig Exp $	*/
      2   1.5  christos 
      3   1.1       cgd /*
      4   1.1       cgd  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
      5  1.10       agc  * All rights reserved.
      6  1.10       agc  *
      7  1.10       agc  * This code is derived from software contributed to Berkeley by
      8  1.10       agc  * Adam de Boor.
      9  1.10       agc  *
     10  1.10       agc  * Redistribution and use in source and binary forms, with or without
     11  1.10       agc  * modification, are permitted provided that the following conditions
     12  1.10       agc  * are met:
     13  1.10       agc  * 1. Redistributions of source code must retain the above copyright
     14  1.10       agc  *    notice, this list of conditions and the following disclaimer.
     15  1.10       agc  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.10       agc  *    notice, this list of conditions and the following disclaimer in the
     17  1.10       agc  *    documentation and/or other materials provided with the distribution.
     18  1.10       agc  * 3. Neither the name of the University nor the names of its contributors
     19  1.10       agc  *    may be used to endorse or promote products derived from this software
     20  1.10       agc  *    without specific prior written permission.
     21  1.10       agc  *
     22  1.10       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.10       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.10       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.10       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.10       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.10       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.10       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.10       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.10       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.10       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.10       agc  * SUCH DAMAGE.
     33  1.10       agc  *
     34  1.10       agc  *	from: @(#)lst.h	8.1 (Berkeley) 6/6/93
     35  1.10       agc  */
     36  1.10       agc 
     37  1.10       agc /*
     38   1.1       cgd  * Copyright (c) 1988, 1989 by Adam de Boor
     39   1.1       cgd  * Copyright (c) 1989 by Berkeley Softworks
     40   1.1       cgd  * All rights reserved.
     41   1.1       cgd  *
     42   1.1       cgd  * This code is derived from software contributed to Berkeley by
     43   1.1       cgd  * Adam de Boor.
     44   1.1       cgd  *
     45   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     46   1.1       cgd  * modification, are permitted provided that the following conditions
     47   1.1       cgd  * are met:
     48   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     49   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     50   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     51   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     52   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     53   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     54   1.1       cgd  *    must display the following acknowledgement:
     55   1.1       cgd  *	This product includes software developed by the University of
     56   1.1       cgd  *	California, Berkeley and its contributors.
     57   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     58   1.1       cgd  *    may be used to endorse or promote products derived from this software
     59   1.1       cgd  *    without specific prior written permission.
     60   1.1       cgd  *
     61   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     62   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     63   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     64   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     65   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     66   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     67   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     68   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     69   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     70   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     71   1.1       cgd  * SUCH DAMAGE.
     72   1.1       cgd  *
     73   1.7  christos  *	from: @(#)lst.h	8.1 (Berkeley) 6/6/93
     74   1.1       cgd  */
     75   1.1       cgd 
     76  1.60    rillig /* Doubly-linked lists of arbitrary pointers. */
     77  1.60    rillig 
     78  1.21    rillig #ifndef MAKE_LST_H
     79  1.21    rillig #define MAKE_LST_H
     80   1.1       cgd 
     81  1.60    rillig #include <sys/param.h>
     82  1.67    rillig #include <stdint.h>
     83  1.60    rillig #include <stdlib.h>
     84   1.1       cgd 
     85  1.60    rillig /* A doubly-linked list of pointers. */
     86  1.86    rillig typedef struct List List;
     87  1.60    rillig /* A single node in the doubly-linked list. */
     88  1.86    rillig typedef struct ListNode ListNode;
     89   1.1       cgd 
     90  1.66    rillig struct ListNode {
     91  1.86    rillig 	ListNode *prev;		/* previous node in list, or NULL */
     92  1.86    rillig 	ListNode *next;		/* next node in list, or NULL */
     93  1.86    rillig 	union {
     94  1.86    rillig 		void *datum;	/* datum associated with this element */
     95  1.86    rillig 		const struct GNode *priv_gnode; /* alias, just for debugging */
     96  1.86    rillig 		const char *priv_str; /* alias, just for debugging */
     97  1.86    rillig 	};
     98  1.66    rillig };
     99  1.66    rillig 
    100  1.66    rillig struct List {
    101  1.93    rillig 	ListNode *first;
    102  1.93    rillig 	ListNode *last;
    103  1.66    rillig };
    104  1.66    rillig 
    105  1.60    rillig /* Free the datum of a node, called before freeing the node itself. */
    106  1.40    rillig typedef void LstFreeProc(void *);
    107  1.60    rillig 
    108  1.60    rillig /* Create or destroy a list */
    109  1.60    rillig 
    110  1.60    rillig /* Create a new list. */
    111  1.71    rillig List *Lst_New(void);
    112  1.88    rillig /* Free the list nodes, but not the list itself. */
    113  1.88    rillig void Lst_Done(List *);
    114  1.89    rillig /* Free the list nodes, freeing the node data using the given function. */
    115  1.89    rillig void Lst_DoneCall(List *, LstFreeProc);
    116  1.60    rillig /* Free the list, leaving the node data unmodified. */
    117  1.62    rillig void Lst_Free(List *);
    118  1.60    rillig /* Free the list, freeing the node data using the given function. */
    119  1.62    rillig void Lst_Destroy(List *, LstFreeProc);
    120  1.60    rillig 
    121  1.90    rillig #define LST_INIT { NULL, NULL }
    122  1.90    rillig 
    123  1.88    rillig /* Initialize a list, without memory allocation. */
    124  1.88    rillig MAKE_INLINE void
    125  1.88    rillig Lst_Init(List *list)
    126  1.88    rillig {
    127  1.88    rillig     list->first = NULL;
    128  1.88    rillig     list->last = NULL;
    129  1.88    rillig }
    130  1.88    rillig 
    131  1.60    rillig /* Get information about a list */
    132  1.60    rillig 
    133  1.85    rillig MAKE_INLINE Boolean
    134  1.86    rillig Lst_IsEmpty(List *list)
    135  1.86    rillig { return list->first == NULL; }
    136  1.74    rillig 
    137  1.60    rillig /* Find the first node that contains the given datum, or NULL. */
    138  1.62    rillig ListNode *Lst_FindDatum(List *, const void *);
    139  1.60    rillig 
    140  1.60    rillig /* Modify a list */
    141  1.60    rillig 
    142  1.60    rillig /* Insert a datum before the given node. */
    143  1.62    rillig void Lst_InsertBefore(List *, ListNode *, void *);
    144  1.60    rillig /* Place a datum at the front of the list. */
    145  1.62    rillig void Lst_Prepend(List *, void *);
    146  1.60    rillig /* Place a datum at the end of the list. */
    147  1.62    rillig void Lst_Append(List *, void *);
    148  1.60    rillig /* Remove the node from the list. */
    149  1.62    rillig void Lst_Remove(List *, ListNode *);
    150  1.62    rillig void Lst_PrependAll(List *, List *);
    151  1.62    rillig void Lst_AppendAll(List *, List *);
    152  1.62    rillig void Lst_MoveAll(List *, List *);
    153  1.60    rillig 
    154  1.60    rillig /* Node-specific functions */
    155  1.60    rillig 
    156  1.60    rillig /* Replace the value of the node. */
    157  1.62    rillig void LstNode_Set(ListNode *, void *);
    158  1.60    rillig /* Set the value of the node to NULL. Having NULL in a list is unusual. */
    159  1.62    rillig void LstNode_SetNull(ListNode *);
    160  1.60    rillig 
    161  1.60    rillig /* Using the list as a queue */
    162  1.60    rillig 
    163  1.60    rillig /* Add a datum at the tail of the queue. */
    164  1.92    rillig MAKE_INLINE void
    165  1.92    rillig Lst_Enqueue(List *list, void *datum) {
    166  1.92    rillig 	Lst_Append(list, datum);
    167  1.92    rillig }
    168  1.92    rillig 
    169  1.60    rillig /* Remove the head node of the queue and return its datum. */
    170  1.62    rillig void *Lst_Dequeue(List *);
    171   1.1       cgd 
    172  1.82    rillig /* A vector is an ordered collection of items, allowing for fast indexed
    173  1.82    rillig  * access. */
    174  1.82    rillig typedef struct Vector {
    175  1.86    rillig 	void *items;		/* memory holding the items */
    176  1.93    rillig 	size_t itemSize;	/* size of a single item */
    177  1.86    rillig 	size_t len;		/* number of actually usable elements */
    178  1.93    rillig 	size_t cap;		/* capacity */
    179  1.82    rillig } Vector;
    180  1.82    rillig 
    181  1.82    rillig void Vector_Init(Vector *, size_t);
    182  1.84    rillig 
    183  1.84    rillig /* Return the pointer to the given item in the vector.
    184  1.84    rillig  * The returned data is valid until the next modifying operation. */
    185  1.85    rillig MAKE_INLINE void *
    186  1.84    rillig Vector_Get(Vector *v, size_t i)
    187  1.84    rillig {
    188  1.86    rillig 	unsigned char *items = v->items;
    189  1.86    rillig 	return items + i * v->itemSize;
    190  1.84    rillig }
    191  1.84    rillig 
    192  1.82    rillig void *Vector_Push(Vector *);
    193  1.82    rillig void *Vector_Pop(Vector *);
    194  1.91    rillig 
    195  1.91    rillig MAKE_INLINE void
    196  1.91    rillig Vector_Done(Vector *v) {
    197  1.91    rillig 	free(v->items);
    198  1.91    rillig }
    199  1.61    rillig 
    200  1.21    rillig #endif /* MAKE_LST_H */
    201