2 * list.h: Simple generic list manipulation
4 * Copyright (C) 2007-2015 David Lutterkort
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Author: David Lutterkort <dlutter@redhat.com>
26 #define list_append(head, tail) \
28 if ((head) == NULL) { \
33 for (_p = (head); _p->next != NULL; _p = _p->next); \
37 #define list_for_each(iter, list) \
38 for (typeof(list) (iter) = list; (iter) != NULL; (iter) = (iter)->next)
40 #define list_remove(elt, list) \
42 typeof(elt) _e = (elt); \
47 for (_p = (list); _p != NULL && _p->next != _e; _p = _p->next); \
49 _p->next = _e->next; \
54 /* Insert NEW in list LIST before element AC. NEW->next must be null,
55 and ELT must really be on LIST, otherwise chaos will ensue
57 #define list_insert_before(new, elt, list) \
59 if ((list) == NULL) { \
61 } else if ((elt) == (list)) { \
62 (new)->next = (elt); \
66 for (_p = (list); _p != NULL && _p->next != (elt); _p = _p->next); \
68 (new)->next = (elt); \
76 #define list_free(list) \
77 while ((list) != NULL) { \
78 typeof(list) _p = list; \
79 (list) = (list)->next; \
83 #define list_length(len, list) \
86 for (len=0, _p = (list); _p != NULL; len += 1, _p = _p->next); \
89 /* Make ELT the new head of LIST and set LIST to it */
90 #define list_cons(list, elt) \
92 typeof(elt) _e = (elt); \
97 #define list_reverse(list) \
99 typeof(list) _head = (list); \
100 typeof(list) _prev = NULL; \
101 while (_head != NULL) { \
102 typeof(list) _next = _head->next; \
103 _head->next = _prev; \
110 /* Append ELT to the end of LIST. TAIL must be NULL or a pointer to
111 the last element of LIST. ELT may also be a list
113 #define list_tail_cons(list, tail, elt) \
115 /* Append ELT at the end of LIST */ \
116 if ((list) == NULL) { \
119 if ((tail) == NULL) \
120 for ((tail) = (list); (tail)->next != NULL; \
121 (tail) = (tail)->next); \
122 (tail)->next = (elt); \
124 /* Make sure TAIL is the last element on the combined LIST */ \
126 if ((tail) != NULL) \
127 while ((tail)->next != NULL) \
128 (tail) = (tail)->next; \
133 * indent-tabs-mode: nil