chiark / gitweb /
[PATCH] update to libsysfs 1.2.0 and add some stuff klib_fixup
[elogind.git] / libsysfs / dlist.h
1 /*
2  * dlist.h
3  *
4  * Copyright (C) 2003 Eric J Bohm
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21 #ifndef _DLIST_H_
22 #define _DLIST_H_
23
24 /* Double linked list header.
25    
26 * navigate your list with DLIST_PREV and DLIST_NEXT.  These are macros
27 * so function call overhead is minimized.
28
29 * Supports perl style push, pop, shift, unshift list semantics.
30
31 * You allocate the data and give dlist the pointer.  If your data is
32 * complex set the dlist->del_func to a an appropriate delete using
33 * dlist_new_with_delete.  Your delete function must match 
34 (void * )(del(void *)
35 *Otherwise dlist will just use free.
36
37 * NOTE: The small amount of pain involved in doing that allows us to
38 * avoid copy in copy out semantics.
39
40 * Dlist uses an internal mark pointer to keep track of where you are
41 * in the list.
42
43 * insert and delete take a directional parameter. Where direction
44 * corresponds to the direction in which you want the list to go.
45 * true direction corresponded to progressing forward in the last
46 * false to regressing in the list.
47 * so a dlist_insert(yourlist,item,1) will insert it after the mark
48 * so a dlist_insert(yourlist,item,0) will insert it before the mark
49 * any insert will move the mark to the new node regardless of the direction.
50
51 * Just use the dlist_(insert|delete)_(before|after) macros if you do not want
52 * to think about it.
53
54 */
55 #include <malloc.h>
56 typedef struct dl_node {
57   struct dl_node *prev;
58   struct dl_node *next;
59   void *data;
60 } DL_node;
61
62 typedef struct dlist {
63   DL_node *marker;
64   unsigned long count;
65   size_t data_size;
66   void (*del_func)(void *);
67   DL_node headnode;
68   DL_node *head;
69 } Dlist;
70
71 Dlist *dlist_new(size_t datasize);
72 Dlist *dlist_new_with_delete(size_t datasize,void (*del_func)(void*));
73 void *_dlist_mark_move(Dlist *list,int direction);
74 void *dlist_mark(Dlist *);
75 void dlist_start(Dlist *);
76 void dlist_end(Dlist *);
77 void dlist_move(struct dlist *source, struct dlist *dest, struct dl_node *target,int direction);
78 void *dlist_insert(Dlist *,void *,int) ;
79
80 void *dlist_insert_sorted(struct dlist *list, void *new_elem, int (*sorter)(void *, void *));
81
82 void dlist_delete(Dlist *,int);
83
84 void dlist_push(Dlist *,void *);
85
86 void dlist_unshift(Dlist *,void *);
87 void dlist_unshift_sorted(Dlist *,void *,int (*sorter)(void *, void *));
88
89 void *dlist_pop(Dlist *);
90
91 void *dlist_shift(Dlist *);
92
93 void dlist_destroy(Dlist *);
94
95 int _dlist_merge(struct dlist *listsource, struct dlist *listdest, unsigned int passcount, int (*compare)(void *, void *));
96
97 void *dlist_find_custom(struct dlist *list, void *target, int (*comp)(void *, void *));
98
99 void dlist_sort_custom(struct dlist *list, int (*compare)(void *, void *));
100
101
102 void _dlist_swap(struct dlist *list, struct dl_node *a, struct dl_node *b);
103
104 void dlist_transform(struct dlist *list, void (*node_operation)(void *));
105
106
107 /* 
108  * _dlist_remove is for internal use only
109  * _dlist_mark_move is for internal use only
110  */
111 void *_dlist_remove(struct dlist *,struct dl_node *,int );
112 void *_dlist_insert_dlnode(struct dlist *list,struct dl_node *new_node,int direction);
113
114 #define dlist_prev(A) _dlist_mark_move((A),0)
115 #define dlist_next(A) _dlist_mark_move((A),1)
116
117 #define dlist_insert_before(A,B) dlist_insert((A),(B),0)
118 #define dlist_insert_after(A,B) dlist_insert((A),(B),1)
119
120 #define dlist_delete_before(A) dlist_delete((A),0)
121 #define dlist_delete_after(A) dlist_delete((A),1)
122
123 /**
124  * provide for loop header which iterates the mark from start to end
125  * list: the dlist pointer, use dlist_mark(list) to get iterator
126  */
127 #define dlist_for_each(list) \
128         for(dlist_start(list),dlist_next(list); \
129                 (list)->marker!=(list)->head;dlist_next(list))
130
131 /**
132  * provide for loop header which iterates the mark from end to start
133  * list: the dlist pointer, use dlist_mark(list) to get iterator
134  */
135 #define dlist_for_each_rev(list) \
136         for(dlist_end(list),dlist_prev(list); \
137                 (list)->marker!=(list)->head;dlist_prev(list))
138
139 /**
140  * provide for loop header which iterates through the list without moving mark
141  * list: the dlist_pointer
142  * iterator: dl_node pointer to iterate
143  */
144 #define dlist_for_each_nomark(list,iterator) \
145         for((iterator)=(list)->head->next; (iterator)!=(list)->head; \
146                 (iterator)=(iterator)->next)
147
148 /**
149  * provide for loop header which iterates through the list without moving mark
150  * in reverse
151  * list: the dlist_pointer
152  * iterator: dl_node pointer to iterate
153  */
154 #define dlist_for_each_nomark_rev(list,iterator) \
155         for((iterator)=(list)->head->prev; (iterator)!=(list)->head; \
156                 (iterator)=(iterator)->prev)
157 /**
158  * provide for loop header which iterates through the list providing a
159  * data iterator
160  * list: the dlist pointer
161  * data_iterator: the pointer of type datatype to iterate
162  * datatype:  actual type of the contents in the dl_node->data
163  */
164
165 #define dlist_for_each_data(list,data_iterator,datatype) \
166         for(dlist_start(list), (data_iterator)=(datatype *) dlist_next(list); \
167         (list)->marker!=(list)->head;(data_iterator)=(datatype *) dlist_next(list))
168
169 /**
170  * provide for loop header which iterates through the list providing a
171  * data iterator in reverse
172  * list: the dlist pointer
173  * data_iterator: the pointer of type datatype to iterate
174  * datatype:  actual type of the contents in the dl_node->data
175  */
176 #define dlist_for_each_data_rev(list,data_iterator,datatype) \
177         for(dlist_end(list), (data_iterator)=(datatype *) dlist_prev(list); \
178         (list)->marker!=(list)->head;(data_iterator)=(datatype *) dlist_prev(list))
179
180 /**
181  * provide for loop header which iterates through the list providing a
182  * data iterator without moving the mark
183  * list: the dlist pointer
184  * iterator: the dl_node pointer to iterate
185  * data_iterator: the pointer of type datatype to iterate
186  * datatype:  actual type of the contents in the dl_node->data
187  */
188
189 #define dlist_for_each_data_nomark(list,iterator,data_iterator,datatype) \
190         for((iterator)=(list)->head->next, (data_iterator)=(datatype *) (iterator)->data; \
191         (iterator)!=(list)->head;(iterator)=(iterator)->next,(data_iterator)=(datatype *) (iterator))
192
193 /**
194  * provide for loop header which iterates through the list providing a
195  * data iterator in reverse without moving the mark
196  * list: the dlist pointer
197  * iterator: the dl_node pointer to iterate
198  * data_iterator: the pointer of type datatype to iterate
199  * datatype:  actual type of the contents in the dl_node->data
200  */
201 #define dlist_for_each_data_nomark_rev(list,iterator, data_iterator,datatype) \
202         for((iterator)=(list)->head->prev, (data_iterator)=(datatype *) (iterator)->data; \
203         (iterator)!=(list)->head;(iterator)=(iterator)->prev,(data_iterator)=(datatype *) (iterator))
204
205 #endif /* _DLIST_H_ */