/* -*- c -*-

    This file is part of GPCT.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 3 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    02110-1301 USA

    Copyright Alexandre Becoulet <alexandre.becoulet@free.fr> (C) 2009

    As a special exception, if other file access functions, variables
    or types which are instanciated by direct invocation of a GPCT
    macro from source code covered by the GNU LGPL version 2 or
    later, the file does not by itself cause the resulting work to
    be covered by the GNU General Public License. However the source
    code for this file must still be made available in accordance
    with section (3) of the GNU General Public License. This
    exception does not invalidate any other reasons why a work based
    on this file might be covered by the GNU General Public License.

    Singly linked list

*/

#ifndef GPCT_SLIST_H_
#define GPCT_SLIST_H_

#include <gpct/_cont_access.h>
#include <gpct/_cont_keyfield.h>
#include <gpct/_cont_entry.h>
#include <gpct/_cont_lookup.h>
#include <gpct/_cont_pushpop_array.h>
#include <gpct/_cont_lists.h>

#define GPCT_CONT_SLIST_ARRAY_BASED     0
#define GPCT_CONT_SLIST_POINTER_BASED   1
#define GPCT_CONT_SLIST_ORDERED         1
#define GPCT_CONT_SLIST_SORTED          0
#define GPCT_CONT_SLIST_REORDER         1

/***********************************************************************
 *      types
 */

typedef struct                          gpct_slist_entry_s
{
  struct gpct_slist_entry_s             *next;
}                                       gpct_SLIST_entry_t;

/* backslash-region-begin */
#define GPCT_CONT_SLIST_TYPE(name, type, entryfield)

typedef struct                          name##_root_s
{
  GPCT_LOCK_TYPE(CONTAINER_LOCK_##name) lock;
  struct gpct_slist_entry_s             *head;
}                                       name##_root_t;

typedef struct gpct_slist_entry_s       name##_entry_t;
typedef type                            name##_itembase_t;
typedef name##_itembase_t *             name##_item_t;
typedef name##_itembase_t *             name##_index_t;

static const name##_item_t
name##_gpct_null_item = NULL;

static const name##_index_t
name##_gpct_null_index = NULL;

static const uintptr_t
name##_gpct_offsetof = offsetof(name##_itembase_t, entryfield);

GPCT_CONT_ORPHAN_EMPTY_FUNC(SLIST, name, CONTAINER_ORPHAN_CHK_##name)
/* backslash-region-end */

/***********************************************************************
 *      orphan checking
 */

GPCT_INTERNAL void
gpct_orphan_SLIST__set(struct gpct_slist_entry_s *e)
{
  e->next = e;
}

GPCT_INTERNAL gpct_bool_t
gpct_orphan_SLIST__chk(struct gpct_slist_entry_s *e)
{
  return e != e->next;
}

/***********************************************************************
 *      intializer
 */

/* backslash-region-begin */
#define GPCT_CONT_SLIST_ROOT_INITIALIZER(name, lockname)
{
  .lock = GPCT_LOCK_INITIALIZER(lockname),
  .head = NULL,
}
/* backslash-region-end */

#define GPCT_CONT_SLIST_ROOT_DECLARATOR(name, lockname, symbol) \
 name##_root_t symbol = GPCT_CONT_SLIST_ROOT_INITIALIZER(name, lockname)

/***********************************************************************
 *      access functions
 */

#define GPCT_CONT_SLIST_ISEND(root, entry)      ((entry) == NULL)
#define GPCT_CONT_SLIST_EMPTY(root)             ((root)->head = NULL)

/* backslash-region-begin */
#define GPCT_CONT_SLIST_FUNC(attr, name, prefix, lockname, ...)

GPCT_CONT_LISTS_INTERNAL_FUNC(attr, name, prefix)

GPCT_CONT_LISTS_INIT_FUNC(attr, name, SLIST, prefix, lockname)

GPCT_CONT_LISTS_ROOT_FUNC(attr, name, SLIST, prefix, lockname, head)

GPCT_NONNULL(1, 2, 3)
GPCT_DEPRECATED
attr GPCT_CONT_PROTO_SET(name, prefix)
{
  gpct_error_t          res = -1;

  GPCT_LOCK_WRLOCK(lockname, &root->lock);

  {
    name##_entry_t *index_e = GPCT_CONT_GET_ENTRY(name, index);
    name##_entry_t *item_e = GPCT_CONT_GET_ENTRY(name, item);

    if (GPCT_CONT_ORPHAN_CHK(SLIST, name, index_e))
      {
        name##_entry_t **prev;

        GPCT_CONT_REFNEW(name, item);

        prev = prefix##_gpct_pprev(root, index_e);
        item_e->next = index_e->next;
        *prev = item_e;

        GPCT_CONT_ORPHAN_SET(SLIST, name, index_e);
        GPCT_CONT_REFDROP(name, index);

        res = 0;
      }
  }

  GPCT_LOCK_UNLOCK(lockname, &root->lock);

  return res;
}

GPCT_CONT_LISTS_NEXT_HEAD_FUNC(attr, name, SLIST,
                               prefix, lockname,
                               NEXT, HEAD, next, head)

GPCT_CONT_LISTS_PREV_TAIL_FUNC(attr, name, SLIST, prefix, lockname)

GPCT_CONT_LISTS_COUNT_FUNC(attr, name, SLIST, prefix, lockname, head)

GPCT_DEPRECATED
GPCT_NONNULL(1, 2, 3)
attr GPCT_CONT_PROTO_INSERT_PRE(name, prefix)
{
  name##_entry_t        *index_e;
  size_t                res = 0;

  GPCT_LOCK_WRLOCK(lockname, &root->lock);

  index_e = GPCT_CONT_GET_ENTRY(name, index);

  if (GPCT_CONT_ORPHAN_CHK(SLIST, name, index_e))
    {
      name##_entry_t    **pprev;
      name##_entry_t    *item_e;

      GPCT_CONT_REFNEW(name, item);

      pprev = prefix##_gpct_pprev(root, index_e);
      item_e = GPCT_CONT_GET_ENTRY(name, item);
      item_e->next = index_e;
      *pprev = item_e;

      res = 1;
    }

  GPCT_LOCK_UNLOCK(lockname, &root->lock);

  return res;
}

GPCT_NONNULL(1, 2, 3)
attr GPCT_CONT_PROTO_INSERT_POST(name, prefix)
{
  name##_entry_t        *index_e;
  size_t                res = 0;

  GPCT_LOCK_WRLOCK(lockname, &root->lock);

  index_e = GPCT_CONT_GET_ENTRY(name, index);

  if (GPCT_CONT_ORPHAN_CHK(SLIST, name, index_e))
    {
      name##_entry_t    *item_e;

      GPCT_CONT_REFNEW(name, item);

      item_e = GPCT_CONT_GET_ENTRY(name, item);
      item_e->next = index_e->next;
      index_e->next = item_e;

      res = 1;
    }

  GPCT_LOCK_UNLOCK(lockname, &root->lock);

  return res;
}

GPCT_CONT_LISTS_CLEAR_FUNC(attr, name, SLIST, prefix, lockname, head)

GPCT_DEPRECATED
GPCT_NONNULL(1, 2)
attr GPCT_CONT_PROTO_REMOVE(name, prefix)
{
  gpct_error_t          res = -1;
  name##_entry_t        *index_e;

  GPCT_LOCK_WRLOCK(lockname, &root->lock);

  index_e = GPCT_CONT_GET_ENTRY(name, index);

  if (GPCT_CONT_ORPHAN_CHK(SLIST, name, index_e))
    {
      name##_entry_t **prev = prefix##_gpct_pprev(root, index_e);

      *prev = index_e->next;

      GPCT_CONT_ORPHAN_SET(SLIST, name, index_e);
      GPCT_CONT_REFDROP(name, index);

      res = 0;
    }

  GPCT_LOCK_UNLOCK(lockname, &root->lock);

  return res;
}

GPCT_NONNULL(1, 2)
attr GPCT_CONT_PROTO_PUSH(name, prefix)
{
  name##_entry_t    *item_e;

  GPCT_LOCK_WRLOCK(lockname, &root->lock);
  GPCT_CONT_REFNEW(name, item);

  item_e = GPCT_CONT_GET_ENTRY(name, item);
  item_e->next = root->head;
  root->head = item_e;

  GPCT_LOCK_UNLOCK(lockname, &root->lock);

  return 1;
}

GPCT_NONNULL(1)
attr GPCT_CONT_PROTO_POP(name, prefix)
{
  name##_item_t item = NULL;
  name##_entry_t *entry;

  GPCT_LOCK_WRLOCK(lockname, &root->lock);

  if ((entry = root->head) != NULL)
    {
      name##_entry_t *next = entry->next;

      root->head = next;

      item = GPCT_CONT_GET_ITEM(name, entry);
      GPCT_CONT_ORPHAN_SET(SLIST, name, entry);
    }

  GPCT_LOCK_UNLOCK(lockname, &root->lock);

  return item;
}

GPCT_INTERNAL
GPCT_CONT_PROTO_PUSHBACK(name, prefix##_gpct)
{
  name##_entry_t        *item_e;
  name##_entry_t        **plast;

  GPCT_LOCK_WRLOCK(lockname, &root->lock);
  GPCT_CONT_REFNEW(name, item);

  item_e = GPCT_CONT_GET_ENTRY(name, item);
  plast = prefix##_gpct_pprev(root, NULL);
  item_e->next = NULL;
  *plast = item_e;

  GPCT_LOCK_UNLOCK(lockname, &root->lock);

  return 1;
}

GPCT_NONNULL(1, 2)
GPCT_DEPRECATED
attr GPCT_CONT_PROTO_PUSHBACK(name, prefix)
{
  return prefix##_gpct_pushback(root, item);
}

GPCT_INTERNAL
GPCT_CONT_PROTO_POPBACK(name, prefix##_gpct)
{
  name##_entry_t        **ptail, *tail;
  name##_item_t         item = NULL;

  GPCT_LOCK_WRLOCK(lockname, &root->lock);

  if (root->head && (ptail = prefix##_gpct_ptail(root)))
    {
      tail = *ptail;
      *ptail = NULL;
      item = GPCT_CONT_GET_ITEM(name, tail);
      GPCT_CONT_ORPHAN_SET(SLIST, name, tail);
    }

  GPCT_LOCK_UNLOCK(lockname, &root->lock);

  return item;
}

GPCT_NONNULL(1)
GPCT_DEPRECATED
attr GPCT_CONT_PROTO_POPBACK(name, prefix)
{
  return prefix##_gpct_popback(root);
}

GPCT_DEPRECATED attr GPCT_CONT_PROTO_PUSHBACK_ARRAY(name, prefix);
GPCT_DEPRECATED attr GPCT_CONT_PROTO_POPBACK_ARRAY(name, prefix);
GPCT_CONT_PUSHPOP_ARRAY_FUNC(attr, name, prefix,
                             prefix##_gpct, lockname);

/* use same foreach functions as SLIST */
GPCT_NONNULL(1, 2)
attr GPCT_CONT_PROTO_FOREACH(name, prefix)
GPCT_CONT_LISTS_FOREACH_FUNCCODE(attr, name, SLIST, prefix,
                                 lockname, head, next)

GPCT_NONNULL(1, 2)
GPCT_DEPRECATED
attr GPCT_CONT_PROTO_FOREACH_REVERSE(name, prefix)
GPCT_CONT_LISTS_FOREACH_REVERSE_FUNCCODE(attr, name, SLIST,
                                         prefix, lockname)

GPCT_NONNULL(1)
attr GPCT_CONT_PROTO_CHECK(name, prefix)
{
  name##_entry_t        *entry;
  gpct_error_t          res = 0;

  GPCT_LOCK_RDLOCK(lockname, &root->lock);

  for (entry = root->head; !res && entry != NULL; entry = entry->next)
    {
      if (entry->next == entry)
        res |= 1;

      if (entry->next == root->head)
        res |= 2;
    }

  GPCT_LOCK_UNLOCK(lockname, &root->lock);

  return res;
}
/* backslash-region-end */


/***********************************************************************
 *      iteration macros
 */

/* backslash-region-begin */
#define GPCT_CONT_SLIST_FOREACH(name, root, ...)
GPCT_CONT_LISTS_FOREACH_BASE(SLIST, name, root, (root)->head, next, __VA_ARGS__)
/* backslash-region-end */


/* backslash-region-begin */
#define GPCT_CONT_SLIST_FOREACH_FROM(name, root, index, ...)
 GPCT_CONT_LISTS_FOREACH_BASE(SLIST, name, root,
			      GPCT_CONT_GET_ENTRY(name, index)->next, next, __VA_ARGS__)
/* backslash-region-end */


/* backslash-region-begin */
#define GPCT_CONT_SLIST_FOREACH_REVERSE(name, root, ...)
GPCT_CONT_LISTS_FOREACH_REVERSE_BASE(name, root, NULL, __VA_ARGS__)
/* backslash-region-end */


/* backslash-region-begin */
#define GPCT_CONT_SLIST_FOREACH_REVERSE_FROM(name, root, index, ...)
 GPCT_CONT_LISTS_FOREACH_REVERSE_BASE(name, root,
				      GPCT_CONT_GET_ENTRY(name, index), __VA_ARGS__)
/* backslash-region-end */


#define GPCT_CONT_SLIST_FOREACH_UNORDERED(name, root, ...)         \
 GPCT_CONT_SLIST_FOREACH(name, root, __VA_ARGS__)


/***********************************************************************
 *      key field based functions
 */

GPCT_CONT_LISTS_SORT_FUNC(SLIST, gpct_SLIST_entry_t)






/*
 * SLIST sort glue function template, call sort and rebuild prev links
 */

/* backslash-region-begin */
#define GPCT_CONT_SLIST_SORT_FUNCCODE(name, lockname, cmp)
{
  GPCT_LOCK_WRLOCK(lockname, &root->lock);

  if (root->head != NULL && root->head->next != NULL)
    root->head = gpct_SLIST_sort(root->head, cmp);

  GPCT_LOCK_UNLOCK(lockname, &root->lock);
}
/* backslash-region-end */





/*
 * SLIST merge glue function template, call merge and rebuild prev links
 */

/* backslash-region-begin */
#define GPCT_CONT_SLIST_MERGE_FUNCCODE(name, lockname, cmp)
{
  GPCT_LOCK_WRLOCK(lockname, &src->lock);
  GPCT_LOCK_WRLOCK(lockname, &dest->lock);

  if (src->head != NULL)
    {
      dest->head = (dest->head == NULL)
	? src->head
	: gpct_SLIST_merge(dest->head, src->head, cmp);

      GPCT_CONT_SLIST_EMPTY(src);
    }

  GPCT_LOCK_UNLOCK(lockname, &dest->lock);
  GPCT_LOCK_UNLOCK(lockname, &src->lock);
}
/* backslash-region-end */





/*
 * SLIST Ordered insertion function template
 */

/* backslash-region-begin */
#define GPCT_CONT_SLIST_ORDERED_INSERT_FUNCCODE(name, lockname, cmp)
{
  name##_entry_t        *item_e, **prev;

  GPCT_LOCK_WRLOCK(lockname, &root->lock);

  item_e = GPCT_CONT_GET_ENTRY(name, item);

  for (prev = &root->head; *prev != NULL; prev = &(*prev)->next)
    if (cmp(item_e, *prev))
      break;

  GPCT_CONT_REFNEW(name, item);

  item_e->next = *prev;
  *prev = item_e;

  GPCT_LOCK_UNLOCK(lockname, &root->lock);
}
/* backslash-region-end */





/* backslash-region-begin */
#define GPCT_CONT_KEY_SLIST_FUNC(attr, name, prefix,
                                 lockname, keyfield, ...)

GPCT_CONT_LOOKUP_FUNC(attr, name, SLIST, prefix, lockname, keyfield);

GPCT_CONT_LISTS_CMP_FUNC(name, prefix, SLIST, keyfield)

attr GPCT_CONT_PROTO_SORT_ASCEND(name, prefix, keyfield)
GPCT_CONT_SLIST_SORT_FUNCCODE(name, lockname, prefix##_gpct_cmp_ascend)

attr GPCT_CONT_PROTO_SORT_DESCEND(name, prefix, keyfield)
GPCT_CONT_SLIST_SORT_FUNCCODE(name, lockname, prefix##_gpct_cmp_descend)

attr GPCT_CONT_PROTO_SORT_STABLE_ASCEND(name, prefix, keyfield)
{
  prefix##_sort_ascend(root);
}

attr GPCT_CONT_PROTO_SORT_STABLE_DESCEND(name, prefix, keyfield)
{
  prefix##_sort_descend(root);
}

attr GPCT_CONT_PROTO_SORTED_ASCEND(name, prefix, keyfield)
GPCT_CONT_LISTS_SORTED_FUNCCODE(SLIST, name, lockname, prefix##_gpct_cmp_ascend, root->head)

attr GPCT_CONT_PROTO_SORTED_DESCEND(name, prefix, keyfield)
GPCT_CONT_LISTS_SORTED_FUNCCODE(SLIST, name, lockname, prefix##_gpct_cmp_descend, root->head)

attr GPCT_CONT_PROTO_INSERT_ASCEND(name, prefix, keyfield)
GPCT_CONT_SLIST_ORDERED_INSERT_FUNCCODE(name, lockname, prefix##_gpct_cmp_ascend)

attr GPCT_CONT_PROTO_INSERT_DESCEND(name, prefix, keyfield)
GPCT_CONT_SLIST_ORDERED_INSERT_FUNCCODE(name, lockname, prefix##_gpct_cmp_descend)

attr GPCT_CONT_PROTO_MERGE_ASCEND(name, prefix, keyfield)
GPCT_CONT_SLIST_MERGE_FUNCCODE(name, lockname, prefix##_gpct_cmp_ascend)

attr GPCT_CONT_PROTO_MERGE_DESCEND(name, prefix, keyfield)
GPCT_CONT_SLIST_MERGE_FUNCCODE(name, lockname, prefix##_gpct_cmp_descend)
/* backslash-region-end */


#endif

