1 /* DLIFE Copyright (C) 2000 Richard W.M. Jones <rich@annexia.org>
2 * and other authors listed in the ``AUTHORS'' file.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program 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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * $Id: dlink.h,v 1.1 2002/04/05 14:40:26 rich Exp $
24 /* This was snarfed from the source for Python 1.5. */
30 #define offsetof(type, member) ( (int) & ((type*)0) -> member )
33 typedef struct dlink_list_element
37 } dlink_list_element_t;
39 typedef struct dlink_list
46 #define DLINK_LIST_INIT(offset) { 0, 0, (offset) }
48 #define _DLINK_ELEMENT_PTRS(element,list) ((dlink_list_element_t *) ((element) + (list)->offset))
49 #define _DLINK_FIRST_PTRS(list) _DLINK_ELEMENT_PTRS((list)->first,(list))
50 #define _DLINK_LAST_PTRS(list) _DLINK_ELEMENT_PTRS((list)->last,(list))
53 dlink_list_init (dlink_list_t *list, int offset)
55 list->first = list->last = 0;
56 list->offset = offset;
60 insert_before_first (void *element, dlink_list_t *list)
62 dlink_list_element_t *eptrs = _DLINK_ELEMENT_PTRS (element, list);
65 eptrs->next = list->first;
67 _DLINK_FIRST_PTRS(list)->prev = element;
70 list->first = element;
74 insert_before_last (void *element, dlink_list_t *list)
76 dlink_list_element_t *eptrs = _DLINK_ELEMENT_PTRS (element, list);
79 eptrs->prev = list->last;
81 _DLINK_LAST_PTRS(list)->next = element;
83 list->first = element;
88 move_towards_last (void *e, dlink_list_t *list)
90 dlink_list_element_t *eptrs = _DLINK_ELEMENT_PTRS (e, list);
92 void *e0 = eptrs->prev;
93 dlink_list_element_t *e0ptrs = _DLINK_ELEMENT_PTRS (e0, list);
95 void *e1 = eptrs->next;
96 dlink_list_element_t *e1ptrs = _DLINK_ELEMENT_PTRS (e1, list);
98 void *e2 = e1ptrs->next;
99 dlink_list_element_t *e2ptrs = _DLINK_ELEMENT_PTRS (e2, list);
101 /* The elements are: e0 <---> e <---> e1 <---> e2.
103 * We know that e and e1 exist, but e0 and e2 may or may not exist.
105 * We want the arrangement to be:
106 * e0 <---> e1 <---> e <---> e2.
125 remove_element (void *element, dlink_list_t *list)
127 dlink_list_element_t *eptrs = _DLINK_ELEMENT_PTRS (element, list);
129 void *eprev = eptrs->prev;
130 dlink_list_element_t *eprevptrs = _DLINK_ELEMENT_PTRS (eprev, list);
132 void *enext = eptrs->next;
133 dlink_list_element_t *enextptrs = _DLINK_ELEMENT_PTRS (enext, list);
136 enextptrs->prev = eprev;
141 eprevptrs->next = enext;