Home | History | Annotate | Line # | Download | only in make
lst.h revision 1.93
      1 /*	$NetBSD: lst.h,v 1.93 2020/12/13 20:57:17 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 	union {
     94 		void *datum;	/* datum associated with this element */
     95 		const struct GNode *priv_gnode; /* alias, just for debugging */
     96 		const char *priv_str; /* alias, just for debugging */
     97 	};
     98 };
     99 
    100 struct List {
    101 	ListNode *first;
    102 	ListNode *last;
    103 };
    104 
    105 /* Free the datum of a node, called before freeing the node itself. */
    106 typedef void LstFreeProc(void *);
    107 
    108 /* Create or destroy a list */
    109 
    110 /* Create a new list. */
    111 List *Lst_New(void);
    112 /* Free the list nodes, but not the list itself. */
    113 void Lst_Done(List *);
    114 /* Free the list nodes, freeing the node data using the given function. */
    115 void Lst_DoneCall(List *, LstFreeProc);
    116 /* Free the list, leaving the node data unmodified. */
    117 void Lst_Free(List *);
    118 /* Free the list, freeing the node data using the given function. */
    119 void Lst_Destroy(List *, LstFreeProc);
    120 
    121 #define LST_INIT { NULL, NULL }
    122 
    123 /* Initialize a list, without memory allocation. */
    124 MAKE_INLINE void
    125 Lst_Init(List *list)
    126 {
    127     list->first = NULL;
    128     list->last = NULL;
    129 }
    130 
    131 /* Get information about a list */
    132 
    133 MAKE_INLINE Boolean
    134 Lst_IsEmpty(List *list)
    135 { return list->first == NULL; }
    136 
    137 /* Find the first node that contains the given datum, or NULL. */
    138 ListNode *Lst_FindDatum(List *, const void *);
    139 
    140 /* Modify a list */
    141 
    142 /* Insert a datum before the given node. */
    143 void Lst_InsertBefore(List *, ListNode *, void *);
    144 /* Place a datum at the front of the list. */
    145 void Lst_Prepend(List *, void *);
    146 /* Place a datum at the end of the list. */
    147 void Lst_Append(List *, void *);
    148 /* Remove the node from the list. */
    149 void Lst_Remove(List *, ListNode *);
    150 void Lst_PrependAll(List *, List *);
    151 void Lst_AppendAll(List *, List *);
    152 void Lst_MoveAll(List *, List *);
    153 
    154 /* Node-specific functions */
    155 
    156 /* Replace the value of the node. */
    157 void LstNode_Set(ListNode *, void *);
    158 /* Set the value of the node to NULL. Having NULL in a list is unusual. */
    159 void LstNode_SetNull(ListNode *);
    160 
    161 /* Using the list as a queue */
    162 
    163 /* Add a datum at the tail of the queue. */
    164 MAKE_INLINE void
    165 Lst_Enqueue(List *list, void *datum) {
    166 	Lst_Append(list, datum);
    167 }
    168 
    169 /* Remove the head node of the queue and return its datum. */
    170 void *Lst_Dequeue(List *);
    171 
    172 /* A vector is an ordered collection of items, allowing for fast indexed
    173  * access. */
    174 typedef struct Vector {
    175 	void *items;		/* memory holding the items */
    176 	size_t itemSize;	/* size of a single item */
    177 	size_t len;		/* number of actually usable elements */
    178 	size_t cap;		/* capacity */
    179 } Vector;
    180 
    181 void Vector_Init(Vector *, size_t);
    182 
    183 /* Return the pointer to the given item in the vector.
    184  * The returned data is valid until the next modifying operation. */
    185 MAKE_INLINE void *
    186 Vector_Get(Vector *v, size_t i)
    187 {
    188 	unsigned char *items = v->items;
    189 	return items + i * v->itemSize;
    190 }
    191 
    192 void *Vector_Push(Vector *);
    193 void *Vector_Pop(Vector *);
    194 
    195 MAKE_INLINE void
    196 Vector_Done(Vector *v) {
    197 	free(v->items);
    198 }
    199 
    200 #endif /* MAKE_LST_H */
    201