/*
 * Copyright (C) 1994-2018 Altair Engineering, Inc.
 * For more information, contact Altair at www.altair.com.
 *
 * This file is part of the PBS Professional ("PBS Pro") software.
 *
 * Open Source License Information:
 *
 * PBS Pro is free software. You can redistribute it and/or modify it under the
 * terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * PBS Pro 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Commercial License Information:
 *
 * For a copy of the commercial license terms and conditions,
 * go to: (http://www.pbspro.com/UserArea/agreement.html)
 * or contact the Altair Legal Department.
 *
 * Altair’s dual-license business model allows companies, individuals, and
 * organizations to create proprietary derivative works of PBS Pro and
 * distribute them - whether embedded or bundled with other software -
 * under a commercial license agreement.
 *
 * Use of Altair’s trademarks, including but not limited to "PBS™",
 * "PBS Professional®", and "PBS Pro™" and Altair’s logos is subject to Altair's
 * trademark licensing policies.
 *
 */

#include <pbs_config.h>   /* the master config generated by configure */

#include "portability.h"
#include "list_link.h"
#ifndef NDEBUG
#include <stdio.h>
#include <stdlib.h>
#endif


/**
 * @file	list_link.c
 * @brief
 * list_link.c - general routines for maintenance of a double
 *	linked list.  A user defined structure can be managed as
 *	a double linked list if the first element in the user structure
 *	is the "pbs_list_link" struct defined in list_link.h and the list
 *	is headed by a "pbs_list_head" struct also defined in list_link.h.
 *
 * @par	These are the routines provided:
 *		insert_link - inserts a new entry before or after an old
 *		append_link - adds a new entry to the end of the list
 *		delete_link - removes an entry from the list
 *		is_linked   - returns 1 if entry is in the list
 */


/**
 * @brief
 * 	-insert_link - adds a new entry to a list.
 *	Entry is added either before (position=0) or after (position !=0)
 *	an old entry.
 *
 * @param[in] oldp	ptr to old entry in list
 * @param[in] newp	ptr to new link entry
 * @param[in] pobj 	ptr to object to link in
 * @param[in] position  0=before old, else after
 *
 * @return	Void
 */

void
insert_link(struct pbs_list_link *oldp, struct pbs_list_link *newp,
 		void *pobj, int position)
{

#ifndef NDEBUG
	/* first make sure unlinked entries are pointing to themselves	    */

	if ((pobj          == NULL)	      ||
		(oldp           == NULL) ||
		(oldp->ll_prior == NULL)        ||
		(oldp->ll_next  == NULL)        ||
		(newp->ll_prior != (pbs_list_link *)newp)      ||
		(newp->ll_next  != (pbs_list_link *)newp)) {
		(void)fprintf(stderr, "Assertion failed, bad pointer in insert_link\n");
		abort();
	}
#endif

	if (position == LINK_INSET_AFTER) {  /* insert newp after oldp */
		newp->ll_prior = oldp;
		newp->ll_next  = oldp->ll_next;
		(oldp->ll_next)->ll_prior = newp;
		oldp->ll_next = newp;
	} else {				/* insert newp before oldp */
		newp->ll_next = oldp;
		newp->ll_prior = oldp->ll_prior;
		(oldp->ll_prior)->ll_next = newp;
		oldp->ll_prior = newp;
	}
	/*
	 * its big trouble if ll_struct is null, it would make this
	 * entry appear to be the head, so we never let that happen
	 */
	if (pobj)
		newp->ll_struct = pobj;
	else
		newp->ll_struct = (void *)newp;
}

/**
 * @brief
 * 	-append_link - append a new entry to the end of the list
 *
 * @param[in] head		ptr to head of list
 * @param[in] newp		ptr to new link entry
 * @param[in] pobj		ptr to object to link in
 *
 * @return      Void
 */

void
append_link(pbs_list_head *head, pbs_list_head *newp, void *pobj)
{

#ifndef NDEBUG
	/* first make sure unlinked entries are pointing to themselves	    */

	if ((pobj == NULL) 		  ||
		(head->ll_prior == NULL)   ||
		(head->ll_next  == NULL)   ||
		(newp->ll_prior  != (pbs_list_link *)newp) ||
		(newp->ll_next   != (pbs_list_link *)newp)) {
		(void)fprintf(stderr, "Assertion failed, bad pointer in insert_link\n");
		abort();
	}
#endif

	(head->ll_prior)->ll_next = newp;
	newp->ll_prior = head->ll_prior;
	newp->ll_next  = head;
	head->ll_prior = newp;
	/*
	 * its big trouble if ll_struct is null, it would make this
	 * entry appear to be the head, so we never let that happen
	 */
	if (pobj)
		newp->ll_struct = pobj;
	else
		newp->ll_struct = (void *)newp;
}

/**
 * @brief
 * 	-delete_link - delete an entry from the list
 *
 * @par	Checks to be sure links exist before breaking them\n
 *	Note: the oldp entry is unchanged other than the list links
 *	are cleared.
 *
 * @param[in] oldp       ptr to link to delete
 *
 * @return	Void
 *
 */

void
delete_link(struct pbs_list_link *oldp)
{

	if ((oldp->ll_prior != NULL) &&
		(oldp->ll_prior != oldp) && (oldp->ll_prior->ll_next == oldp))
		(oldp->ll_prior)->ll_next = oldp->ll_next;

	if ((oldp->ll_next != NULL) &&
		(oldp->ll_next != oldp) && (oldp->ll_next->ll_prior == oldp))
		(oldp->ll_next)->ll_prior = oldp->ll_prior;

	oldp->ll_next  = oldp;
	oldp->ll_prior = oldp;
}

/**
 * @brief
 * 	-swap_link - swap the positions of members of a list
 *
 * @param[in] pone - member one
 * @param[in] ptwo - member two
 *
 * @return	Void
 *
 */

void
swap_link(pbs_list_link *pone, pbs_list_link *ptwo)
{
	pbs_list_link *p1p;
	pbs_list_link *p2p;


	if (pone->ll_next == ptwo) {
		delete_link(pone);
		insert_link(ptwo, pone, pone->ll_struct, LINK_INSET_AFTER);
	} else if (ptwo->ll_next == pone) {
		delete_link(ptwo);
		insert_link(pone, ptwo, ptwo->ll_struct, LINK_INSET_AFTER);
	} else {
		p1p = pone->ll_prior;
		p2p = ptwo->ll_prior;
		delete_link(pone);
		insert_link(p2p, pone, pone->ll_struct, LINK_INSET_AFTER);
		delete_link(ptwo);
		insert_link(p1p, ptwo, ptwo->ll_struct, LINK_INSET_AFTER);
	}
}

/**
 * @brief
 * 	-is_linked - determine if entry is in the list
 *
 * @param[in] head - ptr to head of list
 * @param[in] entry - entry to be searched for
 *
 * @return	int
 * @retval	1 	if in list
 * @retval	0 	if not in list
 *
 */

int
is_linked(pbs_list_link *head, pbs_list_link *entry)
{
	pbs_list_link *pl;

	pl = head->ll_next;
	while (pl != head) {
		if (pl == entry)
			return (1);
		pl = pl->ll_next;
	}
	return (0);
}

/*
 * The following routines are replaced by in-line code with the
 * GET_NEXT / GET_PRIOR macroes when NDEBUG is defined, see list_link.h
 */

#ifndef NDEBUG
/**
 * @brief
 *	get next entry in list
 *
 * @param[in] pl - list variable
 * @param[in] file -file name
 * @param[in] line - line num
 *
 * @retuan 	Void
 *
 */
void *
get_next(pbs_list_link pl, char *file, int line)
{
	if ((pl.ll_next == NULL) ||
		((pl.ll_next == &pl) && (pl.ll_struct != NULL))) {
		(void)fprintf(stderr, "Assertion failed, bad pointer in link: file \"%s\", line %d\n", file, line);
		abort();
	}
	return (pl.ll_next->ll_struct);
}

/**
 * @brief
 *      get previous entry in list
 *
 * @param[in] pl - list variable
 * @param[in] file -file name
 * @param[in] line - line num
 *
 * @retuan      Void
 *
 */
void *
get_prior(pbs_list_link pl, char *file, int line)
{
	if ((pl.ll_prior == NULL) ||
		((pl.ll_prior == &pl) && (pl.ll_struct != NULL))) {
		(void)fprintf(stderr, "Assertion failed, null pointer in link: file \"%s\", line %d\n", file, line);
		abort();
	}
	return (pl.ll_prior->ll_struct);
}
#endif

/**
 * @brief
 * 	-list_move - move an entire list from one head to another
 *
 * @param[in] from - pointer to pbs_list_head
 * @param[in] to - pointer to pbs_list_head
 *
 * @return	Void
 */

void
list_move(pbs_list_head *from, pbs_list_head *to)
{
	if (from->ll_next == from) {
		to->ll_next = to;
		to->ll_prior = to;
	} else {
		to->ll_next = from->ll_next;
		to->ll_next->ll_prior = to;
		to->ll_prior = from->ll_prior;
		to->ll_prior->ll_next = to;
		CLEAR_HEAD((*from));
	}
}
