2 * list.h - useful list handling header
4 * Copyright © 2008 Julien Danjou <julien@danjou.info>
5 * Copyright © 2006 Pierre Habouzit <madcoder@debian.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #ifndef AWESOME_LIST_H
24 #define AWESOME_LIST_H
26 #define DO_SLIST(type, prefix, dtor) \
27 static inline type *prefix##_list_pop(type **list) \
38 static inline void prefix##_list_push(type **list, type *item) \
44 static inline type **prefix##_list_last(type **list) \
46 while (*list && (*list)->next) \
47 list = &(*list)->next; \
51 static inline void prefix##_list_append(type **list, type *item) \
53 if(*(list = prefix##_list_last(list))) \
54 (*list)->next = item; \
58 static inline type *prefix##_list_rev(type *list) \
62 prefix##_list_push(&l, prefix##_list_pop(&list)); \
66 static inline type **prefix##_list_init(type **list) \
71 static inline void prefix##_list_wipe(type **list) \
75 type *item = prefix##_list_pop(list); \
79 static inline void prefix##_list_swap(type **list, type *item1, \
82 type *i1n = item1->next; \
83 type *i2n = item2->next; \
84 item1->next = i2n == item1 ? item2 : i2n; \
85 item2->next = i1n == item2 ? item1 : i1n; \
89 else if(*list == item2) \
92 static inline type *prefix##_list_prev(type **list, type *item) \
95 if(*list == item) return NULL; \
96 for(tmp = *list; tmp && tmp->next != item; tmp = tmp->next); \
99 static inline type *prefix##_list_prev_cycle(type **list, type *item) \
101 type *tmp = prefix##_list_prev(list, item); \
103 tmp = *prefix##_list_last(list); \
106 static inline void prefix##_list_detach(type **list, type *item) \
108 type *prev = prefix##_list_prev(list, item); \
110 prev->next = item->next; \
112 *list = item->next; \
117 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80