/*
 * Copyright (c) 1991, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	@(#)queue.h	8.5 (Berkeley) 8/20/94
 */

/* THIS IS NOT THE ORIGINAL FILE.  The unused data structures have
 * been removed and several 'missing' definitions added, for symmetry.
 *
 * Last edited: 2005-10-26 22:27:30 by piumarta on margaux.local
 */

#ifndef __ccg_queue_h
#define	__ccg_queue_h


/* singly-linked list */

#define SLIST_HEAD(name, type)						\
  struct name {								\
    struct type *slh_first;	/* first element */			\
  }
 
#define SLIST_HEAD_INITIALIZER()	{ 0 }

#define SLIST_ENTRY(type)						\
  struct {								\
    struct type *sle_next;	/* next element */			\
  }
 

#define	SLIST_EMPTY(head)	((head)->slh_first == 0)

#define	SLIST_FIRST(head)	((head)->slh_first)

#define SLIST_FOREACH(var, head, field)					\
  for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next)

#define SLIST_INIT(head) {						\
    (head)->slh_first = 0;						\
  }

#define SLIST_INSERT_AFTER(slistelm, elm, field) do {			\
    (elm)->field.sle_next = (slistelm)->field.sle_next;			\
    (slistelm)->field.sle_next = (elm);					\
  } while (0)

#define SLIST_INSERT_HEAD(head, elm, field) do {			\
    (elm)->field.sle_next = (head)->slh_first;				\
    (head)->slh_first = (elm);						\
  } while (0)

#define SLIST_NEXT(elm, field)	((elm)->field.sle_next)

#define SLIST_REMOVE_HEAD(head, field) do {				\
    (head)->slh_first = (head)->slh_first->field.sle_next;		\
  } while (0)

#define SLIST_REMOVE(head, elm, type, field) do {			\
    if ((head)->slh_first == (elm)) {					\
      SLIST_REMOVE_HEAD((head), field);					\
    }									\
    else {								\
      struct type *curelm = (head)->slh_first;				\
      while (curelm->field.sle_next != (elm))				\
	curelm = curelm->field.sle_next;				\
      curelm->field.sle_next =						\
	curelm->field.sle_next->field.sle_next;				\
    }									\
  } while (0)

/* ---------------------------------------------------------------- */

/* doubly-linked double-ended queue */

#define TAILQ_HEAD(name, type)						\
  struct name {								\
    struct type  *tqh_first;	/* first element */			\
    struct type **tqh_last;	/* addr of last next element */		\
  }

#define TAILQ_HEAD_INITIALIZER(head)	{ 0, &(head).tqh_first }

#define TAILQ_ENTRY(type)						\
  struct {								\
    struct type  *tqe_next;	/* next element */			\
    struct type **tqe_prev;	/* addr of previous next element */	\
  }


#define	TAILQ_EMPTY(head) ((head)->tqh_first == 0)

#define TAILQ_FOREACH(var, head, field)					\
  for (var = TAILQ_FIRST(head);						\
       var;								\
       var = TAILQ_NEXT(var, field))

#define TAILQ_FOREACH_REVERSE(var, head, field, headname)		\
  for (var = TAILQ_LAST(head, headname);				\
       var;								\
       var = TAILQ_PREV(var, headname, field))

#define	TAILQ_FIRST(head) ((head)->tqh_first)

#define	TAILQ_LAST(head, headname)					\
  (*(((struct headname *)((head)->tqh_last))->tqh_last))

#define	TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)

#define TAILQ_PREV(elm, headname, field)				\
  (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))

#define	TAILQ_INIT(head) do {						\
    (head)->tqh_first = 0;						\
    (head)->tqh_last = &(head)->tqh_first;				\
  } while (0)

#define TAILQ_INSERT_HEAD(head, elm, field) do {			\
    if (((elm)->field.tqe_next = (head)->tqh_first) != 0)		\
      (head)->tqh_first->field.tqe_prev =				\
	&(elm)->field.tqe_next;						\
    else								\
      (head)->tqh_last = &(elm)->field.tqe_next;			\
    (head)->tqh_first = (elm);						\
    (elm)->field.tqe_prev = &(head)->tqh_first;				\
  } while (0)

#define TAILQ_INSERT_TAIL(head, elm, field) do {			\
    (elm)->field.tqe_next = 0;						\
    (elm)->field.tqe_prev = (head)->tqh_last;				\
    *(head)->tqh_last = (elm);						\
    (head)->tqh_last = &(elm)->field.tqe_next;				\
  } while (0)

#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
    if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != 0)	\
      (elm)->field.tqe_next->field.tqe_prev =				\
	&(elm)->field.tqe_next;						\
    else								\
      (head)->tqh_last = &(elm)->field.tqe_next;			\
    (listelm)->field.tqe_next = (elm);					\
    (elm)->field.tqe_prev = &(listelm)->field.tqe_next;			\
  } while (0)

#define TAILQ_INSERT_BEFORE(listelm, elm, field) do {			\
    (elm)->field.tqe_prev = (listelm)->field.tqe_prev;			\
    (elm)->field.tqe_next = (listelm);					\
    *(listelm)->field.tqe_prev = (elm);					\
    (listelm)->field.tqe_prev = &(elm)->field.tqe_next;			\
  } while (0)

#define TAILQ_REMOVE(head, elm, field) do {				\
    if (((elm)->field.tqe_next) != 0)					\
      (elm)->field.tqe_next->field.tqe_prev =				\
	(elm)->field.tqe_prev;						\
    else								\
      (head)->tqh_last = (elm)->field.tqe_prev;				\
    *(elm)->field.tqe_prev = (elm)->field.tqe_next;			\
  } while (0)

#define TAILQ_REMOVE_HEAD(head, field)					\
  TAILQ_REMOVE(head, TAILQ_FIRST(head), field)

/* ---------------------------------------------------------------- */

/* singly-linked double-ended queue */

#define STAILQ_HEAD(name, type)						\
  struct name {								\
    struct type  *stqh_first;	/* first element */			\
    struct type **stqh_last;	/* addr of last next element */		\
  }

#define STAILQ_HEAD_INITIALIZER(head)	{ 0, &(head).stqh_first }

#define STAILQ_ENTRY(type)						\
  struct {								\
    struct type *stqe_next;	/* next element */			\
  }


#define STAILQ_EMPTY(head)	((head)->stqh_first == 0)

#define STAILQ_FOREACH(var, head, field)				\
  for (var = STAILQ_FIRST(head);					\
       var;								\
       var = STAILQ_NEXT(var, field))

#define	STAILQ_INIT(head) do {						\
    (head)->stqh_first = 0;						\
    (head)->stqh_last = &(head)->stqh_first;				\
  } while (0)

#define STAILQ_FIRST(head)	((head)->stqh_first)

#define STAILQ_LAST(head)	(*(head)->stqh_last)

#define STAILQ_INSERT_HEAD(head, elm, field) do {			\
    if (((elm)->field.stqe_next = (head)->stqh_first) == 0)		\
      (head)->stqh_last = &(elm)->field.stqe_next;			\
    (head)->stqh_first = (elm);						\
  } while (0)

#define STAILQ_INSERT_TAIL(head, elm, field) do {			\
    (elm)->field.stqe_next = 0;					\
    *(head)->stqh_last = (elm);						\
    (head)->stqh_last = &(elm)->field.stqe_next;			\
  } while (0)

#define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do {		\
    if (((elm)->field.stqe_next = (tqelm)->field.stqe_next) == 0)	\
      (head)->stqh_last = &(elm)->field.stqe_next;			\
    (tqelm)->field.stqe_next = (elm);					\
  } while (0)

#define STAILQ_NEXT(elm, field)	((elm)->field.stqe_next)

#define STAILQ_REMOVE_HEAD(head, field) do {				\
    if (((head)->stqh_first =						\
	 (head)->stqh_first->field.stqe_next) == 0)			\
      (head)->stqh_last = &(head)->stqh_first;				\
  } while (0)

#define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do {			\
    if (((head)->stqh_first = (elm)->field.stqe_next) == 0)		\
      (head)->stqh_last = &(head)->stqh_first;				\
  } while (0)

#define STAILQ_REMOVE(head, elm, type, field) do {			\
    if ((head)->stqh_first == (elm)) {					\
      STAILQ_REMOVE_HEAD(head, field);					\
    }									\
    else {								\
      struct type *curelm = (head)->stqh_first;				\
      while (curelm->field.stqe_next != (elm))				\
	curelm = curelm->field.stqe_next;				\
      if ((curelm->field.stqe_next =					\
	   curelm->field.stqe_next->field.stqe_next) == 0)		\
	(head)->stqh_last = &(curelm)->field.stqe_next;			\
    }									\
  } while (0)


#endif /* __ccg_queue_h */


/*
 * Local Variables:
 * c-backslash-column: 72
 * End:
 */

