/* $NetBSD: lst.h,v 1.105 2024/04/27 17:33:46 rillig Exp $ */12/*3* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.4* All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Adam de Boor.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*33* from: @(#)lst.h 8.1 (Berkeley) 6/6/9334*/3536/*37* Copyright (c) 1988, 1989 by Adam de Boor38* Copyright (c) 1989 by Berkeley Softworks39* All rights reserved.40*41* This code is derived from software contributed to Berkeley by42* Adam de Boor.43*44* Redistribution and use in source and binary forms, with or without45* modification, are permitted provided that the following conditions46* are met:47* 1. Redistributions of source code must retain the above copyright48* notice, this list of conditions and the following disclaimer.49* 2. Redistributions in binary form must reproduce the above copyright50* notice, this list of conditions and the following disclaimer in the51* documentation and/or other materials provided with the distribution.52* 3. All advertising materials mentioning features or use of this software53* must display the following acknowledgement:54* This product includes software developed by the University of55* California, Berkeley and its contributors.56* 4. Neither the name of the University nor the names of its contributors57* may be used to endorse or promote products derived from this software58* without specific prior written permission.59*60* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND61* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE62* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE63* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE64* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL65* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS66* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)67* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT68* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY69* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF70* SUCH DAMAGE.71*72* from: @(#)lst.h 8.1 (Berkeley) 6/6/9373*/7475/* Doubly-linked lists of arbitrary pointers. */7677#ifndef MAKE_LST_H78#define MAKE_LST_H7980#ifdef HAVE_INTTYPES_H81#include <inttypes.h>82#elif defined(HAVE_STDINT_H)83#include <stdint.h>84#endif85#ifdef HAVE_STDLIB_H86#include <stdlib.h>87#endif8889/* A doubly-linked list of pointers. */90typedef struct List List;91/* A single node in the doubly-linked list. */92typedef struct ListNode ListNode;9394struct ListNode {95ListNode *prev; /* previous node in list, or NULL */96ListNode *next; /* next node in list, or NULL */97void *datum; /* datum associated with this element */98};99100struct List {101ListNode *first;102ListNode *last;103};104105/* Free the list nodes. */106void Lst_Done(List *);107/* Free the list nodes, as well as each node's datum. */108void Lst_DoneFree(List *);109110#define LST_INIT { NULL, NULL }111112/* Initialize a list, without memory allocation. */113MAKE_INLINE void114Lst_Init(List *list)115{116list->first = NULL;117list->last = NULL;118}119120/* Get information about a list */121122MAKE_INLINE bool MAKE_ATTR_USE123Lst_IsEmpty(List *list)124{125return list->first == NULL;126}127128/* Find the first node that contains the given datum, or NULL. */129ListNode *Lst_FindDatum(List *, const void *) MAKE_ATTR_USE;130131/* Modify a list */132133/* Insert a datum before the given node. */134void Lst_InsertBefore(List *, ListNode *, void *);135/* Add a datum at the head of the list. */136void Lst_Prepend(List *, void *);137/* Add a datum at the tail of the list. */138void Lst_Append(List *, void *);139/* Remove the node from the list. */140void Lst_Remove(List *, ListNode *);141void Lst_PrependAll(List *, List *);142void Lst_AppendAll(List *, List *);143void Lst_MoveAll(List *, List *);144145/* Node-specific functions */146147/* Replace the value of the node. */148void LstNode_Set(ListNode *, void *);149/* Set the value of the node to NULL. Having NULL in a list is unusual. */150void LstNode_SetNull(ListNode *);151152/* Using the list as a queue */153154/* Add a datum at the tail of the queue. */155MAKE_INLINE void156Lst_Enqueue(List *list, void *datum)157{158Lst_Append(list, datum);159}160161/* Remove the head node of the queue and return its datum. */162void *Lst_Dequeue(List *) MAKE_ATTR_USE;163164/*165* A vector is an ordered collection of items, allowing for fast indexed166* access.167*/168typedef struct Vector {169void *items; /* memory holding the items */170size_t itemSize; /* size of a single item */171size_t len; /* number of actually usable elements */172size_t cap; /* capacity */173} Vector;174175void Vector_Init(Vector *, size_t);176177/*178* Return the pointer to the given item in the vector.179* The returned data is valid until the next modifying operation.180*/181MAKE_INLINE void * MAKE_ATTR_USE182Vector_Get(Vector *v, size_t i)183{184unsigned char *items = v->items;185return items + i * v->itemSize;186}187188void *Vector_Push(Vector *);189void *Vector_Pop(Vector *);190191MAKE_INLINE void192Vector_Done(Vector *v)193{194free(v->items);195}196197#endif198199200