1 #ifndef _LST_H_ 2 #define _LST_H_ 3 4 /* $OpenBSD: lst.h,v 1.33 2021/03/04 09:45:31 espie Exp $ */ 5 /* $NetBSD: lst.h,v 1.7 1996/11/06 17:59:12 christos Exp $ */ 6 7 /* 8 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 9 * Copyright (c) 1988, 1989 by Adam de Boor 10 * Copyright (c) 1989 by Berkeley Softworks 11 * All rights reserved. 12 * 13 * This code is derived from software contributed to Berkeley by 14 * Adam de Boor. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 3. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * from: @(#)lst.h 8.1 (Berkeley) 6/6/93 41 */ 42 43 /*- 44 * lst.h -- 45 * Header for using the list library 46 */ 47 48 /* These data structures are PRIVATE !!! 49 * Here for efficiency, so that some functions can be recoded as inlines, 50 * and so that lst headers don't need dynamic allocation most of the time. */ 51 struct ListNode_ { 52 struct ListNode_ *prevPtr; /* previous element in list */ 53 struct ListNode_ *nextPtr; /* next in list */ 54 void *datum; /* datum associated with this element */ 55 }; 56 57 #ifndef LIST_TYPE 58 #include "lst_t.h" 59 #endif 60 61 typedef void (*SimpleProc)(void *); 62 typedef bool (*FindProc)(void *, void *); 63 typedef bool (*FindProcConst)(void *, const void *); 64 typedef void (*ForEachProc)(void *, void *); 65 typedef void *(*DuplicateProc)(void *); 66 67 /* 68 * NOFREE can be used as the freeProc to Lst_Destroy when the elements are 69 * not to be freed. 70 * NOCOPY performs similarly when given as the copyProc to Lst_Duplicate. 71 */ 72 #define NOFREE ((SimpleProc) 0) 73 #define NOCOPY ((DuplicateProc) 0) 74 75 /* 76 * Creation/destruction functions 77 */ 78 /* Create a new list */ 79 #define Lst_Init(l) (l)->firstPtr = (l)->lastPtr = NULL 80 /* Static lists are already okay */ 81 #define Static_Lst_Init(l) 82 83 /* Duplicate an existing list */ 84 extern Lst Lst_Clone(Lst, Lst, DuplicateProc); 85 86 /* XXX: Lst_Destroy only destroys the list contents, which is appropriate 87 * as most use-cases are temporary lists. 88 * In case of a permanent list, Lst_Init must also be called ! 89 */ 90 /* Destroy an old one */ 91 extern void Lst_Destroy(LIST *, SimpleProc); 92 /* True if list is empty */ 93 #define Lst_IsEmpty(l) ((l)->firstPtr == NULL) 94 95 /* 96 * Functions to modify a list 97 */ 98 /* Insert an element before another */ 99 extern void Lst_Insert(Lst, LstNode, void *); 100 extern void Lst_AtFront(Lst, void *); 101 /* Insert an element after another */ 102 extern void Lst_Append(Lst, LstNode, void *); 103 extern void Lst_AtEnd(Lst, void *); 104 /* Remove an element */ 105 extern void Lst_Remove(Lst, LstNode); 106 /* Replace a node with a new value */ 107 extern void Lst_Replace(LstNode, void *); 108 /* Concatenate two lists, destructive. */ 109 extern void Lst_ConcatDestroy(Lst, Lst); 110 /* Concatenate two lists, non-destructive. */ 111 extern void Lst_Concat(Lst, Lst); 112 /* requeue element already in list at front of list */ 113 extern void Lst_Requeue(Lst, LstNode); 114 115 /* 116 * Node-specific functions 117 */ 118 /* Return first element in list */ 119 /* Return last element in list */ 120 /* Return successor to given element */ 121 extern LstNode Lst_Succ(LstNode); 122 123 /* 124 * Functions for entire lists 125 */ 126 /* Find an element starting from somewhere */ 127 extern LstNode Lst_FindFrom(LstNode, FindProc, void *); 128 /* 129 * See if the given datum is on the list. Returns the LstNode containing 130 * the datum 131 */ 132 extern LstNode Lst_Member(Lst, void *); 133 /* Apply a function to elements of a lst starting from a certain point. */ 134 extern void Lst_ForEachFrom(LstNode, ForEachProc, void *); 135 extern void Lst_Every(Lst, SimpleProc); 136 137 extern bool Lst_AddNew(Lst, void *); 138 /* 139 * for using the list as a queue 140 */ 141 /* Place an element at tail of queue */ 142 #define Lst_EnQueue Lst_AtEnd 143 #define Lst_QueueNew Lst_AddNew 144 145 /* 146 * for using the list as a stack 147 */ 148 #define Lst_Push Lst_AtFront 149 #define Lst_Pop Lst_DeQueue 150 151 /* Remove an element from head of queue */ 152 extern void * Lst_DeQueue(Lst); 153 154 #define Lst_Datum(ln) ((ln)->datum) 155 #define Lst_First(l) ((l)->firstPtr) 156 #define Lst_Last(l) ((l)->lastPtr) 157 #define Lst_ForEach(l, proc, d) Lst_ForEachFrom(Lst_First(l), proc, d) 158 #define Lst_Find(l, cProc, d) Lst_FindFrom(Lst_First(l), cProc, d) 159 #define Lst_Adv(ln) ((ln)->nextPtr) 160 #define Lst_Rev(ln) ((ln)->prevPtr) 161 162 163 /* Inlines are preferable to macros here because of the type checking. */ 164 #ifdef HAS_INLINES 165 static INLINE LstNode 166 Lst_FindConst(Lst l, FindProcConst cProc, const void *d) 167 { 168 return Lst_FindFrom(Lst_First(l), (FindProc)cProc, (void *)d); 169 } 170 171 static INLINE LstNode 172 Lst_FindFromConst(LstNode ln, FindProcConst cProc, const void *d) 173 { 174 return Lst_FindFrom(ln, (FindProc)cProc, (void *)d); 175 } 176 #else 177 #define Lst_FindConst(l, cProc, d) \ 178 Lst_FindFrom(Lst_First(l), (FindProc)cProc, (void *)d) 179 #define Lst_FindFromConst(ln, cProc, d) \ 180 Lst_FindFrom(ln, (FindProc)cProc, (void *)d) 181 #endif 182 183 #endif /* _LST_H_ */ 184