2 * Copyright (C) 2007 Tobias Brunner
3 * Copyright (C) 2005-2006 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * @defgroup linked_list linked_list
25 #ifndef LINKED_LIST_H_
26 #define LINKED_LIST_H_
28 typedef struct linked_list_t linked_list_t
;
33 #include <utils/iterator.h>
34 #include <utils/enumerator.h>
38 * Method to match elements in a linked list (used in find_* functions)
40 * @param item current list item
41 * @param ... user supplied data (only pointers, at most 5)
43 * - TRUE, if the item matched
46 typedef bool (*linked_list_match_t
)(void *item
, ...);
49 * Class implementing a double linked list.
51 * General purpose linked list. This list is not synchronized.
53 struct linked_list_t
{
56 * Gets the count of items in the list.
58 * @return number of items in list
60 int (*get_count
) (linked_list_t
*this);
63 * Creates a iterator for the given list.
65 * @warning Created iterator_t object has to get destroyed by the caller.
67 * @deprecated Iterator is obsolete and will disappear, it is too
68 * complicated to implement. Use enumerator instead.
70 * @param forward iterator direction (TRUE: front to end)
71 * @return new iterator_t object
73 iterator_t
*(*create_iterator
) (linked_list_t
*this, bool forward
);
76 * Creates a iterator, locking a mutex.
78 * The supplied mutex is acquired immediately, and released
79 * when the iterator gets destroyed.
81 * @param mutex mutex to use for exclusive access
82 * @return new iterator_t object
84 iterator_t
*(*create_iterator_locked
) (linked_list_t
*this,
85 pthread_mutex_t
*mutex
);
88 * Create an enumerator over the list.
90 * The enumerator is a "lightweight" iterator. It only has two methods
91 * and should therefore be much easier to implement.
93 * @return enumerator over list items
95 enumerator_t
* (*create_enumerator
)(linked_list_t
*this);
98 * Inserts a new item at the beginning of the list.
100 * @param item item value to insert in list
102 void (*insert_first
) (linked_list_t
*this, void *item
);
105 * Removes the first item in the list and returns its value.
107 * @param item returned value of first item, or NULL
108 * @return SUCCESS, or NOT_FOUND if list is empty
110 status_t (*remove_first
) (linked_list_t
*this, void **item
);
113 * Remove an item from the list where the enumerator points to.
115 * @param enumerator enumerator with position
117 void (*remove_at
)(linked_list_t
*this, enumerator_t
*enumerator
);
120 * Remove items from the list matching item.
122 * If a compare function is given, it is called for each item, where
123 * the first parameter is the current list item and the second parameter
124 * is the supplied item parameter.
125 * If compare is NULL, compare is is done by pointer.
127 * @param item item to remove/pass to comparator
128 * @param compare compare function, or NULL
129 * @return number of removed items
131 int (*remove
)(linked_list_t
*this, void *item
, bool (*compare
)(void *,void*));
134 * Returns the value of the first list item without removing it.
136 * @param this calling object
137 * @param item returned value of first item
138 * @return SUCCESS, NOT_FOUND if list is empty
140 status_t (*get_first
) (linked_list_t
*this, void **item
);
143 * Inserts a new item at the end of the list.
145 * @param item value to insert into list
147 void (*insert_last
) (linked_list_t
*this, void *item
);
150 * Removes the last item in the list and returns its value.
152 * @param this calling object
153 * @param item returned value of last item, or NULL
154 * @return SUCCESS, NOT_FOUND if list is empty
156 status_t (*remove_last
) (linked_list_t
*this, void **item
);
159 * Returns the value of the last list item without removing it.
161 * @param this calling object
162 * @param item returned value of last item
163 * @return SUCCESS, NOT_FOUND if list is empty
165 status_t (*get_last
) (linked_list_t
*this, void **item
);
167 /** Find the first matching element in the list.
169 * The first object passed to the match function is the current list item,
170 * followed by the user supplied data.
171 * If the supplied function returns TRUE this function returns SUCCESS, and
172 * the current object is returned in the third parameter, otherwise,
173 * the next item is checked.
175 * @warning Only use pointers as user supplied data.
177 * @param match comparison function to call on each object
178 * @param item the list item, if found
179 * @param ... user data to supply to match function (limited to 5 arguments)
180 * @return SUCCESS if found, NOT_FOUND otherwise
182 status_t (*find_first
) (linked_list_t
*this, linked_list_match_t match
,
185 /** Find the last matching element in the list.
187 * The first object passed to the match function is the current list item,
188 * followed by the user supplied data.
189 * If the supplied function returns TRUE this function returns SUCCESS, and
190 * the current object is returned in the third parameter, otherwise,
191 * the next item is checked.
193 * @warning Only use pointers as user supplied data.
195 * @param match comparison function to call on each object
196 * @param item the list item, if found
197 * @param ... user data to supply to match function (limited to 5 arguments)
198 * @return SUCCESS if found, NOT_FOUND otherwise
200 status_t (*find_last
) (linked_list_t
*this, linked_list_match_t match
,
204 * Invoke a method on all of the contained objects.
206 * If a linked list contains objects with function pointers,
207 * invoke() can call a method on each of the objects. The
208 * method is specified by an offset of the function pointer,
209 * which can be evalutated at compile time using the offsetof
210 * macro, e.g.: list->invoke(list, offsetof(object_t, method));
212 * @param offset offset of the method to invoke on objects
214 void (*invoke_offset
) (linked_list_t
*this, size_t offset
);
217 * Invoke a function on all of the contained objects.
219 * @param offset offset of the method to invoke on objects
221 void (*invoke_function
) (linked_list_t
*this, void (*)(void*));
224 * Clones a list and its objects using the objects' clone method.
226 * @param offset offset ot the objects clone function
227 * @return cloned list
229 linked_list_t
*(*clone_offset
) (linked_list_t
*this, size_t offset
);
232 * Clones a list and its objects using a given function.
234 * @param function function that clones an object
235 * @return cloned list
237 linked_list_t
*(*clone_function
) (linked_list_t
*this, void*(*)(void*));
240 * Destroys a linked_list object.
242 void (*destroy
) (linked_list_t
*this);
245 * Destroys a list and its objects using the destructor.
247 * If a linked list and the contained objects should be destroyed, use
248 * destroy_offset. The supplied offset specifies the destructor to
249 * call on each object. The offset may be calculated using the offsetof
250 * macro, e.g.: list->destroy_offset(list, offsetof(object_t, destroy));
252 * @param offset offset of the objects destructor
254 void (*destroy_offset
) (linked_list_t
*this, size_t offset
);
257 * Destroys a list and its contents using a a cleanup function.
259 * If a linked list and its contents should get destroyed using a specific
260 * cleanup function, use destroy_function. This is useful when the
261 * list contains malloc()-ed blocks which should get freed,
262 * e.g.: list->destroy_function(list, free);
264 * @param function function to call on each object
266 void (*destroy_function
) (linked_list_t
*this, void (*)(void*));
270 * Creates an empty linked list object.
272 * @return linked_list_t object.
274 linked_list_t
*linked_list_create(void);
276 #endif /*LINKED_LIST_H_ @} */