2 ** Copyright (C) 2002 by Kevin L. Mitchell <klmitch@mit.edu>
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Library General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2 of the License, or (at your option) any later version.
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Library General Public License for more details.
14 ** You should have received a copy of the GNU Library General Public
15 ** License along with this library; if not, write to the Free
16 ** Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 #include "dbprim_int.h"
26 /** \ingroup dbprim_link
27 * \brief Move an element within a linked list.
29 * This function moves a specified element within the linked list.
31 * \param list A pointer to a #link_head_t.
32 * \param elem A pointer to the #link_elem_t describing the element
34 * \param loc A #link_loc_t indicating where the entry should be
36 * \param elem2 A pointer to a #link_elem_t describing another element
37 * in the list if \p loc is #LINK_LOC_BEFORE or
40 * \retval DB_ERR_BADARGS An argument was invalid.
41 * \retval DB_ERR_BUSY \p elem and \p elem2 are the same
43 * \retval DB_ERR_WRONGTABLE \p elem or \p elem2 are in a different
45 * \retval DB_ERR_UNUSED \p elem or \p elem2 are not in any
49 ll_move(link_head_t *list, link_elem_t *new, link_loc_t loc,
52 initialize_dbpr_error_table(); /* initialize error table */
54 /* Verify arguments--if elem is set, must be a valid element; if
55 * location is before or after, elem must be set
57 if (!ll_verify(list) || !le_verify(new) || (elem && !le_verify(elem)) ||
58 ((loc == LINK_LOC_BEFORE || loc == LINK_LOC_AFTER) && !elem))
59 return DB_ERR_BADARGS;
61 /* elements can't be the same element */
62 if (elem && new == elem)
65 /* elements must be in the same list */
66 if (list != new->le_head)
67 return new->le_head ? DB_ERR_WRONGTABLE : DB_ERR_UNUSED;
68 if (elem && list != elem->le_head)
69 return elem->le_head ? DB_ERR_WRONGTABLE : DB_ERR_UNUSED;
71 if (new->le_next) /* Clip element out of it previous location */
72 new->le_next->le_prev = new->le_prev;
74 new->le_prev->le_next = new->le_next;
76 list->lh_first = new->le_next;
78 if (list->lh_last == new) /* Make sure we know where the last element is */
79 list->lh_last = new->le_prev;
81 switch (loc) { /* put it in the right place in the list */
83 if (!(elem = list->lh_first)) { /* insert before first element in list */
84 list->lh_first = new; /* list was empty, add element to list */
86 return 0; /* and return, since the list was empty before. */
90 new->le_next = elem; /* prepare new element for its location */
91 new->le_prev = elem->le_prev;
93 elem->le_prev = new; /* insert element into list */
95 new->le_prev->le_next = new; /* update previous element */
96 else /* update head of list */
101 if (!(elem = list->lh_last)) { /* insert after last element in list */
102 list->lh_first = new; /* list was empty, add element to list */
104 return 0; /* and return, since the list was empty before. */
108 new->le_next = elem->le_next; /* prepare new element for its location */
111 elem->le_next = new; /* insert element into list */
113 new->le_next->le_prev = new; /* update next element */
114 else /* update tail of list */