version 1.18, 2000/09/14 13:32:07 |
version 1.19, 2001/05/03 13:41:07 |
|
|
/* $OpenBSD$ */ |
/* $OpenPackages$ */ |
/* $NetBSD: lst.h,v 1.7 1996/11/06 17:59:12 christos Exp $ */ |
/* $OpenBSD$ */ |
|
/* $NetBSD: lst.h,v 1.7 1996/11/06 17:59:12 christos Exp $ */ |
|
|
/* |
/* |
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California. |
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California. |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
* SUCH DAMAGE. |
* SUCH DAMAGE. |
* |
* |
* from: @(#)lst.h 8.1 (Berkeley) 6/6/93 |
* from: @(#)lst.h 8.1 (Berkeley) 6/6/93 |
*/ |
*/ |
|
|
/*- |
/*- |
|
|
* Here for efficiency, so that some functions can be recoded as inlines, |
* Here for efficiency, so that some functions can be recoded as inlines, |
* and so that lst headers don't need dynamic allocation most of the time. */ |
* and so that lst headers don't need dynamic allocation most of the time. */ |
typedef struct ListNode_ { |
typedef struct ListNode_ { |
struct ListNode_ *prevPtr; /* previous element in list */ |
struct ListNode_ *prevPtr; /* previous element in list */ |
struct ListNode_ *nextPtr; /* next in list */ |
struct ListNode_ *nextPtr; /* next in list */ |
short useCount:8, /* Count of functions using the node. |
|
* node may not be deleted until count |
|
* goes to 0 */ |
|
flags:8; /* Node status flags */ |
|
void *datum; /* datum associated with this element */ |
void *datum; /* datum associated with this element */ |
} *LstNode; |
} *LstNode; |
|
|
typedef enum { |
|
Head, Middle, Tail, Unknown |
|
} Where; |
|
|
|
typedef struct { |
typedef struct { |
LstNode firstPtr; /* first node in list */ |
LstNode firstPtr; /* first node in list */ |
LstNode lastPtr; /* last node in list */ |
LstNode lastPtr; /* last node in list */ |
/* |
|
* fields for sequential access |
|
*/ |
|
Where atEnd; /* Where in the list the last access was */ |
|
Boolean isOpen; /* true if list has been Lst_Open'ed */ |
|
LstNode curPtr; /* current node, if open. NULL if |
|
* *just* opened */ |
|
LstNode prevPtr; /* Previous node, if open. Used by |
|
* Lst_Remove */ |
|
} LIST; |
} LIST; |
|
|
typedef LIST *Lst; |
typedef LIST *Lst; |
/* |
|
* basic typedef. This is what the Lst_ functions handle |
|
*/ |
|
|
|
typedef int (*FindProc) __P((void *, void *)); |
typedef void (*SimpleProc)(void *); |
typedef void (*SimpleProc) __P((void *)); |
typedef int (*FindProc)(void *, void *); |
typedef void (*ForEachProc) __P((void *, void *)); |
typedef int (*FindProcConst)(void *, const void *); |
typedef void * (*DuplicateProc) __P((void *)); |
typedef void (*ForEachProc)(void *, void *); |
|
typedef void *(*DuplicateProc)(void *); |
|
|
/* |
/* |
* NOFREE can be used as the freeProc to Lst_Destroy when the elements are |
* NOFREE can be used as the freeProc to Lst_Destroy when the elements are |
* not to be freed. |
* not to be freed. |
* NOCOPY performs similarly when given as the copyProc to Lst_Clone. |
* NOCOPY performs similarly when given as the copyProc to Lst_Duplicate. |
*/ |
*/ |
#define NOFREE ((SimpleProc)0) |
#define NOFREE ((SimpleProc) 0) |
#define NOCOPY ((DuplicateProc)0) |
#define NOCOPY ((DuplicateProc) 0) |
|
|
/* |
/* |
* Constructors/destructors |
* Creation/destruction functions |
*/ |
*/ |
/* Create a new list */ |
/* Create a new list */ |
extern void Lst_Init __P((Lst)); |
extern void Lst_Init(LIST *); |
/* Destroy an old one */ |
|
extern void Lst_Destroy __P((Lst, SimpleProc)); |
|
|
|
/* Duplicate an existing list */ |
/* Duplicate an existing list */ |
extern Lst Lst_Clone __P((Lst, Lst, DuplicateProc)); |
extern Lst Lst_Clone(Lst, Lst, DuplicateProc); |
|
/* Destroy an old one */ |
|
extern void Lst_Destroy(LIST *, SimpleProc); |
/* True if list is empty */ |
/* True if list is empty */ |
extern Boolean Lst_IsEmpty __P((Lst)); |
#define Lst_IsEmpty(l) ((l)->firstPtr == NULL) |
|
|
/* |
/* |
* List modifications |
* Functions to modify a list |
*/ |
*/ |
/* Insert an element before another */ |
/* Insert an element before another */ |
extern void Lst_Insert __P((Lst, LstNode, void *)); |
extern void Lst_Insert(Lst, LstNode, void *); |
|
extern void Lst_AtFront(Lst, void *); |
/* Insert an element after another */ |
/* Insert an element after another */ |
extern void Lst_Append __P((Lst, LstNode, void *)); |
extern void Lst_Append(Lst, LstNode, void *); |
/* Place an element at the front of a lst. */ |
extern void Lst_AtEnd(Lst, void *); |
extern void Lst_AtFront __P((Lst, void *)); |
|
/* Place an element at the end of a lst. */ |
|
extern void Lst_AtEnd __P((Lst, void *)); |
|
/* Remove an element */ |
/* Remove an element */ |
extern void Lst_Remove __P((Lst, LstNode)); |
extern void Lst_Remove(Lst, LstNode); |
/* Replace a node with a new value */ |
/* Replace a node with a new value */ |
extern void Lst_Replace __P((LstNode, void *)); |
extern void Lst_Replace(LstNode, void *); |
/* Concatenate two lists, destructive. */ |
/* Concatenate two lists, destructive. */ |
extern void Lst_ConcatDestroy __P((Lst, Lst)); |
extern void Lst_ConcatDestroy(Lst, Lst); |
/* Concatenate two lists, non destructive */ |
/* Concatenate two lists, non-destructive. */ |
extern void Lst_Concat __P((Lst, Lst)); |
extern void Lst_Concat(Lst, Lst); |
|
|
/* |
/* |
* Node handling |
* Node-specific functions |
*/ |
*/ |
/* Return first element in list */ |
/* Return first element in list */ |
#define Lst_First(l) ((l)->firstPtr) |
|
/* Return last element in list */ |
/* Return last element in list */ |
#define Lst_Last(l) ((l)->lastPtr) |
|
/* Return successor to given element */ |
/* Return successor to given element */ |
extern LstNode Lst_Succ __P((LstNode)); |
extern LstNode Lst_Succ(LstNode); |
/* Return successor to existing element */ |
|
#define Lst_Adv(ln) ((ln)->nextPtr) |
|
/* Get datum from LstNode */ |
|
#define Lst_Datum(ln) ((ln)->datum) |
|
|
|
/* |
/* |
* Apply to entire lists |
* Functions for entire lists |
*/ |
*/ |
|
|
/* Find an element in a list */ |
|
#define Lst_Find(l, cProc, d) Lst_FindFrom(Lst_First(l), cProc, d) |
|
|
|
/* Find an element starting from somewhere */ |
/* Find an element starting from somewhere */ |
extern LstNode Lst_FindFrom __P((LstNode, FindProc, void *)); |
extern LstNode Lst_FindFrom(LstNode, FindProc, void *); |
|
|
/* Apply a function to all elements of a lst */ |
|
#define Lst_ForEach(l, proc, d) Lst_ForEachFrom(Lst_First(l), proc, d) |
|
/* Apply a function to all elements of a lst starting from a certain point. */ |
|
extern void Lst_ForEachFrom __P((LstNode, ForEachProc, void *)); |
|
extern void Lst_Every __P((Lst, SimpleProc)); |
|
|
|
|
|
/* Find datum in a list. Returns the LstNode containing the datum */ |
|
extern LstNode Lst_Member __P((Lst, void *)); |
|
|
|
/* |
/* |
* Visitor-like pattern. Except the visitor is kept in the list. |
* See if the given datum is on the list. Returns the LstNode containing |
* Error-prone and wasteful (used by only a few lists), to be killed. |
* the datum |
*/ |
*/ |
/* Open the list */ |
extern LstNode Lst_Member(Lst, void *); |
extern void Lst_Open __P((Lst)); |
/* Apply a function to elements of a lst starting from a certain point. */ |
/* Next element please */ |
extern void Lst_ForEachFrom(LstNode, ForEachProc, void *); |
extern LstNode Lst_Next __P((Lst)); |
extern void Lst_Every(Lst, SimpleProc); |
/* Done yet? */ |
|
extern Boolean Lst_IsAtEnd __P((Lst)); |
|
/* Finish table access */ |
|
extern void Lst_Close __P((Lst)); |
|
|
|
|
extern ReturnStatus Lst_AddNew(Lst, void *); |
/* |
/* |
* Queue manipulators |
* for using the list as a queue |
*/ |
*/ |
/* Place an element at tail of queue */ |
/* Place an element at tail of queue */ |
extern void Lst_EnQueue __P((Lst, void *)); |
#define Lst_EnQueue Lst_AtEnd |
|
#define Lst_QueueNew Lst_AddNew |
|
|
/* Remove an element from head of queue */ |
/* Remove an element from head of queue */ |
extern void * Lst_DeQueue __P((Lst)); |
extern void * Lst_DeQueue(Lst); |
|
|
|
#define Lst_Datum(ln) ((ln)->datum) |
|
#define Lst_First(l) ((l)->firstPtr) |
|
#define Lst_Last(l) ((l)->lastPtr) |
|
#define Lst_ForEach(l, proc, d) Lst_ForEachFrom(Lst_First(l), proc, d) |
|
#define Lst_Find(l, cProc, d) Lst_FindFrom(Lst_First(l), cProc, d) |
|
#define Lst_Adv(ln) ((ln)->nextPtr) |
|
#define Lst_Rev(ln) ((ln)->prevPtr) |
|
|
|
|
|
#ifdef HAS_INLINES |
|
static INLINE LstNode |
|
Lst_FindConst(Lst l, FindProcConst cProc, const void *d) |
|
{ |
|
return Lst_FindFrom(Lst_First(l), (FindProc)cProc, (void *)d); |
|
} |
|
|
|
static INLINE LstNode |
|
Lst_FindFromConst(LstNode ln, FindProcConst cProc, const void *d) |
|
{ |
|
return Lst_FindFrom(ln, (FindProc)cProc, (void *)d); |
|
} |
|
#else |
|
#define Lst_FindConst(l, cProc, d) \ |
|
Lst_FindFrom(Lst_First(l), (FindProc)cProc, (void *)d) |
|
#define Lst_FindFromConst(ln, cProc, d) \ |
|
Lst_FindFrom(ln, (FindProc)cProc, (void *)d) |
|
#endif |
|
|
#endif /* _LST_H_ */ |
#endif /* _LST_H_ */ |