wmail: bumped to 2.1.
[dockapps.git] / wmfu / list.h
blobc1296d39e80e02b129db7ea8f485841dedf1e596
1 /*
2 * Copyright (c) 2007 Daniel Borca All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #ifndef LIST_H_included
21 #define LIST_H_included
23 /**
24 * Remove an element from list.
26 * \param elem element to remove.
28 #define list_remove(elem) \
29 do { \
30 (elem)->next->prev = (elem)->prev; \
31 (elem)->prev->next = (elem)->next; \
32 } while (0)
34 /**
35 * Insert an element to the list head.
37 * \param list list.
38 * \param elem element to insert.
40 #define list_prepend(list, elem) \
41 do { \
42 (elem)->prev = list; \
43 (elem)->next = (list)->next; \
44 (list)->next->prev = elem; \
45 (list)->next = elem; \
46 } while(0)
48 /**
49 * Insert an element to the list tail.
51 * \param list list.
52 * \param elem element to insert.
54 #define list_append(list, elem) \
55 do { \
56 (elem)->next = list; \
57 (elem)->prev = (list)->prev; \
58 (list)->prev->next = elem; \
59 (list)->prev = elem; \
60 } while(0)
62 /**
63 * Make a empty list empty.
65 * \param sentinel list (sentinel element).
67 #define list_create(sentinel) \
68 do { \
69 (sentinel)->next = sentinel; \
70 (sentinel)->prev = sentinel; \
71 } while (0)
73 /**
74 * Get list first element.
76 * \param list list.
78 * \return pointer to first element.
80 #define list_first(list) ((list)->next)
82 /**
83 * Get list last element.
85 * \param list list.
87 * \return pointer to last element.
89 #define list_last(list) ((list)->prev)
91 /**
92 * Get next element.
94 * \param elem element.
96 * \return pointer to next element.
98 #define list_next(elem) ((elem)->next)
101 * Get previous element.
103 * \param elem element.
105 * \return pointer to previous element.
107 #define list_prev(elem) ((elem)->prev)
110 * Test whether element is at end of the list.
112 * \param list list.
113 * \param elem element.
115 * \return non-zero if element is at end of list, or zero otherwise.
117 #define list_at_end(list, elem) ((elem) == (list))
120 * Test if a list is empty.
122 * \param list list.
124 * \return non-zero if list empty, or zero otherwise.
126 #define list_is_empty(list) ((list)->next == (list))
129 * Walk through the elements of a list.
131 * \param ptr pointer to the current element.
132 * \param list list.
134 * \note It should be followed by a { } block or a single statement, as in a \c
135 * for loop.
137 #define list_foreach(ptr, list)\
138 for (ptr = (list)->next; ptr != list; ptr = (ptr)->next)
141 * Walk through the elements of a list.
143 * Same as #foreach but lets you unlink the current value during a list
144 * traversal. Useful for freeing a list, element by element.
146 * \param ptr pointer to the current element.
147 * \param t temporary pointer.
148 * \param list list.
150 * \note It should be followed by a { } block or a single statement, as in a \c
151 * for loop.
153 #define list_foreach_s(ptr, t, list)\
154 for (ptr = (list)->next, t = (ptr)->next; list != ptr; ptr = t, t = (t)->next)
156 #endif