po: Update Lithuanian translation.
[wine.git] / include / wine / list.h
blobb4d681fe0f31a9e1a555e8ee4e182297a633dab4
1 /*
2 * Linked lists support
4 * Copyright (C) 2002 Alexandre Julliard
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #ifndef __WINE_SERVER_LIST_H
22 #define __WINE_SERVER_LIST_H
24 #include <stddef.h>
26 struct list
28 struct list *next;
29 struct list *prev;
32 /* Define a list like so:
34 * struct gadget
35 * {
36 * struct list entry; <-- doesn't have to be the first item in the struct
37 * int a, b;
38 * };
40 * static struct list global_gadgets = LIST_INIT( global_gadgets );
42 * or
44 * struct some_global_thing
45 * {
46 * struct list gadgets;
47 * };
49 * list_init( &some_global_thing->gadgets );
51 * Manipulate it like this:
53 * list_add_head( &global_gadgets, &new_gadget->entry );
54 * list_remove( &new_gadget->entry );
55 * list_add_after( &some_random_gadget->entry, &new_gadget->entry );
57 * And to iterate over it:
59 * struct gadget *gadget;
60 * LIST_FOR_EACH_ENTRY( gadget, &global_gadgets, struct gadget, entry )
61 * {
62 * ...
63 * }
67 /* add an element after the specified one */
68 static inline void list_add_after( struct list *elem, struct list *to_add )
70 to_add->next = elem->next;
71 to_add->prev = elem;
72 elem->next->prev = to_add;
73 elem->next = to_add;
76 /* add an element before the specified one */
77 static inline void list_add_before( struct list *elem, struct list *to_add )
79 to_add->next = elem;
80 to_add->prev = elem->prev;
81 elem->prev->next = to_add;
82 elem->prev = to_add;
85 /* add element at the head of the list */
86 static inline void list_add_head( struct list *list, struct list *elem )
88 list_add_after( list, elem );
91 /* add element at the tail of the list */
92 static inline void list_add_tail( struct list *list, struct list *elem )
94 list_add_before( list, elem );
97 /* remove an element from its list */
98 static inline void list_remove( struct list *elem )
100 elem->next->prev = elem->prev;
101 elem->prev->next = elem->next;
104 /* get the next element */
105 static inline struct list *list_next( const struct list *list, const struct list *elem )
107 struct list *ret = elem->next;
108 if (elem->next == list) ret = NULL;
109 return ret;
112 /* get the previous element */
113 static inline struct list *list_prev( const struct list *list, const struct list *elem )
115 struct list *ret = elem->prev;
116 if (elem->prev == list) ret = NULL;
117 return ret;
120 /* get the first element */
121 static inline struct list *list_head( const struct list *list )
123 return list_next( list, list );
126 /* get the last element */
127 static inline struct list *list_tail( const struct list *list )
129 return list_prev( list, list );
132 /* check if a list is empty */
133 static inline int list_empty( const struct list *list )
135 return list->next == list;
138 /* initialize a list */
139 static inline void list_init( struct list *list )
141 list->next = list->prev = list;
144 /* count the elements of a list */
145 static inline unsigned int list_count( const struct list *list )
147 unsigned count = 0;
148 const struct list *ptr;
149 for (ptr = list->next; ptr != list; ptr = ptr->next) count++;
150 return count;
153 /* move all elements from src to the tail of dst */
154 static inline void list_move_tail( struct list *dst, struct list *src )
156 if (list_empty(src)) return;
158 dst->prev->next = src->next;
159 src->next->prev = dst->prev;
160 dst->prev = src->prev;
161 src->prev->next = dst;
162 list_init(src);
165 /* move all elements from src to the head of dst */
166 static inline void list_move_head( struct list *dst, struct list *src )
168 if (list_empty(src)) return;
170 dst->next->prev = src->prev;
171 src->prev->next = dst->next;
172 dst->next = src->next;
173 src->next->prev = dst;
174 list_init(src);
177 /* iterate through the list */
178 #define LIST_FOR_EACH(cursor,list) \
179 for ((cursor) = (list)->next; (cursor) != (list); (cursor) = (cursor)->next)
181 /* iterate through the list, with safety against removal */
182 #define LIST_FOR_EACH_SAFE(cursor, cursor2, list) \
183 for ((cursor) = (list)->next, (cursor2) = (cursor)->next; \
184 (cursor) != (list); \
185 (cursor) = (cursor2), (cursor2) = (cursor)->next)
187 /* iterate through the list using a list entry */
188 #define LIST_FOR_EACH_ENTRY(elem, list, type, field) \
189 for ((elem) = LIST_ENTRY((list)->next, type, field); \
190 &(elem)->field != (list); \
191 (elem) = LIST_ENTRY((elem)->field.next, type, field))
193 /* iterate through the list using a list entry, with safety against removal */
194 #define LIST_FOR_EACH_ENTRY_SAFE(cursor, cursor2, list, type, field) \
195 for ((cursor) = LIST_ENTRY((list)->next, type, field), \
196 (cursor2) = LIST_ENTRY((cursor)->field.next, type, field); \
197 &(cursor)->field != (list); \
198 (cursor) = (cursor2), \
199 (cursor2) = LIST_ENTRY((cursor)->field.next, type, field))
201 /* iterate through the list in reverse order */
202 #define LIST_FOR_EACH_REV(cursor,list) \
203 for ((cursor) = (list)->prev; (cursor) != (list); (cursor) = (cursor)->prev)
205 /* iterate through the list in reverse order, with safety against removal */
206 #define LIST_FOR_EACH_SAFE_REV(cursor, cursor2, list) \
207 for ((cursor) = (list)->prev, (cursor2) = (cursor)->prev; \
208 (cursor) != (list); \
209 (cursor) = (cursor2), (cursor2) = (cursor)->prev)
211 /* iterate through the list in reverse order using a list entry */
212 #define LIST_FOR_EACH_ENTRY_REV(elem, list, type, field) \
213 for ((elem) = LIST_ENTRY((list)->prev, type, field); \
214 &(elem)->field != (list); \
215 (elem) = LIST_ENTRY((elem)->field.prev, type, field))
217 /* iterate through the list in reverse order using a list entry, with safety against removal */
218 #define LIST_FOR_EACH_ENTRY_SAFE_REV(cursor, cursor2, list, type, field) \
219 for ((cursor) = LIST_ENTRY((list)->prev, type, field), \
220 (cursor2) = LIST_ENTRY((cursor)->field.prev, type, field); \
221 &(cursor)->field != (list); \
222 (cursor) = (cursor2), \
223 (cursor2) = LIST_ENTRY((cursor)->field.prev, type, field))
225 /* macros for statically initialized lists */
226 #undef LIST_INIT
227 #define LIST_INIT(list) { &(list), &(list) }
229 /* get pointer to object containing list element */
230 #undef LIST_ENTRY
231 #define LIST_ENTRY(elem, type, field) \
232 ((type *)((char *)(elem) - offsetof(type, field)))
234 #endif /* __WINE_SERVER_LIST_H */