list.h 3.26 KB
Newer Older
wanli's avatar
wanli committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
/*
 * list.h - Definitions for list datastructures.
 *
 * Author:      Russ Fish
 *              Computer Science Dept.
 *              University of Utah
 * Date:        1 August 1980
 */

#ifndef	LISTLINKS                         /* only once */

/*****************************************************************/
/* TAG( list LISTHDR TLISTHDR NULLPTR P N  T_P T_N ) */
/* TAG( LISTLINKS TLISTLINKS ) */
typedef                                 /* Generalized list header. */
    struct listhdr
    {
	int t_tag;			/* object type tag */
        struct listhdr *next,*prev;     /* Backward & forward pointers */
    }
    list;

/* LISTLINKS = a type tag, followed by a LISTHDR */
#define LISTLINKS	int t_tag; struct tlist * next, * prev
#define	TLISTLINKS(type)    int t_tag; type * next, * prev

/* Access for List pointer manipulations. (Previous and Next.) */
#define P(node) ((node)->prev)
#define N(node) ((node)->next)
/*   #define P(node) (((list *)(node))->prev)
 *   #define N(node) (((list *)(node))->next)
 */

/* Versions of P and N with cast on result to set list subtype properly. */
#define T_P(type,node) ((type *)(node)->prev)
#define T_N(type,node) ((type *)(node)->next)

/* *****************************************************************
 * TAG( list_list LLIST NEW_LIST_LIST )
 * 
 * list of lists data structure.
 */

typedef struct list_list
{
        TLISTLINKS(struct list_list);
        list * l_list;
} list_list;

#define	LLIST(type,ll)	(type *)ll->l_list	/* get the list pointed to */

/*****************************************************************/

/* TAG( ADD INSERT REMOVE ) */
/* ADD links new members into a list, given a pointer to the new member, and
 * a pointer to the first (left) element of the list.
 */
#define ADD(new,first) ( P(new) = NULL ,N(new) = (first),\
 			(  ((first)!=NULL) ? (P(first)=(new), 0) :0 ),\
			first = (new) )

/* INSERT links members into the middle of a list, given a pointer to the new
 * member, and a pointer to the list element to insert after.
 */
#define INSERT(new,after) ( P(new) = (after),N(new) = N(after),\
	( (N(after)!=NULL) ? (P(N(after))=(new), 0) :0),\
	N(after) = (new) )

/* REMOVE unlinks a list element from a list, given a pointer to the element.
 */
#define REMOVE(elt) (  ( (P(elt)!=NULL) ? (N(P(elt))=N(elt), 0) :0 ),\
 		       ( (N(elt)!=NULL) ? (P(N(elt))=P(elt), 0) :0)  )


/****************************************************************
 * TAG( TRACE FREE_LIST FIRSTP ENDP )
 *
 *	TRACE - Traces a linear list.
 *      FREE_LIST - Walks a list, freeing the elements.
 *	FIRSTP - Tests an element, TRUE if it is the first.
 *	ENDP - Tests an element, TRUE if it is the end.
 */
#define TRACE(t_var,ini)   for((t_var)=(ini);(t_var)!=NULL;(t_var)=N(t_var))
#define FREE_LIST(lst)  {while(N(lst)){lst=N(lst);free(P(lst));} free(lst);}
#define FIRSTP(ptr)   (P(ptr) == NULL)
#define ENDP(ptr)     (N(ptr) == NULL)

/****************************************************************
 * TAGS( DEL )
 *      del   deletes an element from a list, updating the base 
 *	      pointer of the list if necesary.
 */
#define DEL(elt,base) (  ( (P(elt)!=NULL) ? (N(P(elt)) = N(elt)) :0 ),\
 		          ( (N(elt)!=NULL) ? (P(N(elt)) = P(elt)) :0) ,\
                          ( ((elt)==(base)) ? ((base)= N(base) ) :0)    )

#endif	LISTLINKS