/*
Copyright (c) 2000,2003 dean gaudet <dean@arctic.org>

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef SPLIM_RING_H
#define SPLIM_RING_H 1

/* XXX: hrm might be able to clean this up some, not sure */

#include <stddef.h>	/* for offsetof */

/*
the macros in this file implement a doubly-linked ring data structure.
a doubly-linked ring is similar to a doubly-linked list except that the
end points back to the beginning, and vice-versa.  This eliminates a
bunch of special cases and makes them more efficient than lists.

the easiest way to understand this data structure is to get a piece of
paper and a pen and start drawing the elements and their links as you
step through the operations.

a few notes about the implementation:

many list implementations use a structure such as:

	struct list {
		struct list *next;
		struct list *prev;
		void *elt;
	};

this has its purposes and its benefits.  ring doesn't do this, ring
is embedded into the elements to save memory/cycles:

	struct elt {
		struct elt *next;
		struct elt *prev;
		... rest of element
	};

this requires us to know a priori what rings an element could possibly
be on.  (although it's pretty obvious how you'd use these templates
to generate a generic ring object.)

it is possible for an element to be on multiple rings, you just declare
foo_next/foo_prev fields within the element for each ring.

rings have a sentinel -- the list header itself is the sentinel.  it is
not an element, but we play typecasting games which make it appear to
be an element.

the sentinel is used when iterating around the ring -- you know you've
hit the end of the ring when you see the sentinel.

to use ring you have to embed a foo_next and foo_prev field into
your element struct -- they must be in that order, next then prev.

then you instantiate the template with the ring_template(base, foo)
macro where base is the type of your structure.  this defines
foo_ring_t which is the list header type; and it defines a slew
of foo_xxxx() inline functions which manipulate your foo.

thanks to the folks at WATCOM in Waterloo, Canada, which is where
i first learned of doubly-linked rings.
*/
/*
  base is the base type of the structure that will be placed into
  the ring

  foo is the name of the list
*/
#define ring_template(base, foo)	\
  typedef struct { \
    base *foo##_next; \
    base *foo##_prev; \
  } foo##_ring_t; \
 \
  static inline void foo##_ring_init(foo##_ring_t *l); \
  static inline void foo##_ring_init(foo##_ring_t *l) \
  { \
    l->foo##_next = (base *)((char *)l - offsetof(base, foo##_next)); \
    l->foo##_prev = (base *)((char *)l - offsetof(base, foo##_next)); \
  } \
 \
  static inline void foo##_elt_init(base *elt); \
  static inline void foo##_elt_init(base *elt) \
  { \
    elt->foo##_next = elt; \
    elt->foo##_prev = elt; \
  } \
 \
  static inline base *foo##_first(foo##_ring_t *l); \
  static inline base *foo##_first(foo##_ring_t *l) \
  { \
    return l->foo##_next; \
  } \
 \
  static inline base *foo##_last(foo##_ring_t *l); \
  static inline base *foo##_last(foo##_ring_t *l) \
  { \
    return l->foo##_prev; \
  } \
 \
  static inline base *foo##_sentinel(foo##_ring_t *l); \
  static inline base *foo##_sentinel(foo##_ring_t *l) \
  { \
    return (base *)((char *)l - offsetof(base, foo##_next)); \
  } \
 \
  static inline void foo##_unlink(base *elt); \
  static inline void foo##_unlink(base *elt) \
  { \
    elt->foo##_next->foo##_prev = elt->foo##_prev; \
    elt->foo##_prev->foo##_next = elt->foo##_next; \
    elt->foo##_next = elt; \
    elt->foo##_prev = elt; \
  } \
 \
  static inline void foo##_append(foo##_ring_t *l, base *elt); \
  static inline void foo##_append(foo##_ring_t *l, base *elt) \
  { \
    elt->foo##_prev = l->foo##_prev; \
    elt->foo##_next = foo##_sentinel(l); \
    l->foo##_prev->foo##_next = elt; \
    l->foo##_prev = elt; \
  } \
 \
  static inline void foo##_prepend(foo##_ring_t *l, base *elt); \
  static inline void foo##_prepend(foo##_ring_t *l, base *elt) \
  { \
    elt->foo##_next = l->foo##_next; \
    elt->foo##_prev = foo##_sentinel(l); \
    l->foo##_next->foo##_prev = elt; \
    l->foo##_next = elt; \
  } \
 \
  static inline void foo##_insert_before(base *x, base *y); \
  static inline void foo##_insert_before(base *x, base *y) \
  { \
    x->foo##_prev = y->foo##_prev; \
    x->foo##_next = y; \
    y->foo##_prev->foo##_next = x; \
    y->foo##_prev = x; \
  } \
 \
  static inline void foo##_insert_after(base *x, base *y); \
  static inline void foo##_insert_after(base *x, base *y) \
  { \
    x->foo##_next = y->foo##_next; \
    x->foo##_prev = y; \
    y->foo##_next->foo##_prev = x; \
    y->foo##_next = x; \
  } \
 \
  static inline int foo##_is_empty(foo##_ring_t *l); \
  static inline int foo##_is_empty(foo##_ring_t *l) \
  { \
    return l->foo##_next == foo##_sentinel(l); \
  } \
 \
  static inline base *foo##_dequeue(foo##_ring_t *l); \
  static inline base *foo##_dequeue(foo##_ring_t *l) \
  { \
    base *elt; \
 \
    elt = l->foo##_next; \
    foo##_unlink(elt); \
    return elt; \
  } \
 \
  /* caller must ensure x and y are not sentinels */ \
  /* x == y works perfectly fine */ \
  static inline void foo##_splice_after(base *z, base *x, base *y); \
  static inline void foo##_splice_after(base *z, base *x, base *y) \
  { \
    x->foo##_prev->foo##_next = y->foo##_next; \
    y->foo##_next->foo##_prev = x->foo##_prev; \
    x->foo##_prev = z; \
    z->foo##_next->foo##_prev = y; \
    y->foo##_next = z->foo##_next; \
    z->foo##_next = x; \
  } \
  /* caller must ensure x and y are not sentinels */ \
  /* x == y works perfectly fine */ \
  static inline void foo##_splice_before(base *z, base *x, base *y); \
  static inline void foo##_splice_before(base *z, base *x, base *y) \
  { \
    y->foo##_next->foo##_prev = x->foo##_prev; \
    x->foo##_prev->foo##_next = y->foo##_next; \
    y->foo##_next = z; \
    z->foo##_prev->foo##_next = x; \
    x->foo##_prev = z->foo##_prev; \
    z->foo##_prev = y; \
  }

#endif
