summaryrefslogtreecommitdiff
path: root/src/libstrongswan/collections/linked_list.h
blob: 246b9a5c5f246130cce6d86f37e29734cfce1062 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * Copyright (C) 2007-2017 Tobias Brunner
 * Copyright (C) 2005-2008 Martin Willi
 * Copyright (C) 2005 Jan Hutter
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

/**
 * @defgroup linked_list linked_list
 * @{ @ingroup collections
 */

#ifndef LINKED_LIST_H_
#define LINKED_LIST_H_

typedef struct linked_list_t linked_list_t;

#include <collections/enumerator.h>

/**
 * Function to match elements in a linked list
 *
 * @param item			current list item
 * @param args			user supplied data
 * @return				TRUE, if the item matched, FALSE otherwise
 */
typedef bool (*linked_list_match_t)(void *item, va_list args);

/**
 * Helper function to match a string in a linked list of strings
 *
 * @param item			list item (char*)
 * @param args			user supplied data (char*)
 * @return
 */
bool linked_list_match_str(void *item, va_list args);

/**
 * Function to be invoked on elements in a linked list
 *
 * @param item			current list item
 * @param args			user supplied data
 */
typedef void (*linked_list_invoke_t)(void *item, va_list args);

/**
 * Class implementing a double linked list.
 *
 * General purpose linked list. This list is not synchronized.
 */
struct linked_list_t {

	/**
	 * Gets the count of items in the list.
	 *
	 * @return			number of items in list
	 */
	int (*get_count) (linked_list_t *this);

	/**
	 * Create an enumerator over the list.
	 *
	 * @note The enumerator's position is invalid before the first call
	 * to enumerate().
	 *
	 * @return			enumerator over list items
	 */
	enumerator_t* (*create_enumerator)(linked_list_t *this);

	/**
	 * Resets the enumerator's current position to the beginning of the list.
	 *
	 * @param enumerator	enumerator to reset
	 */
	void (*reset_enumerator)(linked_list_t *this, enumerator_t *enumerator);

	/**
	 * Inserts a new item at the beginning of the list.
	 *
	 * @param item		item value to insert in list
	 */
	void (*insert_first) (linked_list_t *this, void *item);

	/**
	 * Removes the first item in the list and returns its value.
	 *
	 * @param item		returned value of first item, or NULL
	 * @return			SUCCESS, or NOT_FOUND if list is empty
	 */
	status_t (*remove_first) (linked_list_t *this, void **item);

	/**
	 * Inserts a new item before the item the enumerator currently points to.
	 *
	 * If this method is called before starting the enumeration the item is
	 * inserted first. If it is called after all items have been enumerated
	 * the item is inserted last. This is helpful when inserting items into
	 * a sorted list.
	 *
	 * @note The position of the enumerator is not changed.
	 *
	 * @param enumerator	enumerator with position
	 * @param item			item value to insert in list
	 */
	void (*insert_before)(linked_list_t *this, enumerator_t *enumerator,
						  void *item);

	/**
	 * Remove an item from the list where the enumerator points to.
	 *
	 * @param enumerator enumerator with position
	 */
	void (*remove_at)(linked_list_t *this, enumerator_t *enumerator);

	/**
	 * Remove items from the list matching the given item.
	 *
	 * If a compare function is given, it is called for each item, with the
	 * first parameter being the current list item and the second parameter
	 * being the supplied item. Return TRUE from the compare function to remove
	 * the item, return FALSE to keep it in the list.
	 *
	 * If compare is NULL, comparison is done by pointers.
	 *
	 * @param item		item to remove/pass to comparator
	 * @param compare	compare function, or NULL
	 * @return			number of removed items
	 */
	int (*remove)(linked_list_t *this, void *item, bool (*compare)(void*,void*));

	/**
	 * Returns the value of the first list item without removing it.
	 *
	 * @param item		returned value of first item
	 * @return			SUCCESS, NOT_FOUND if list is empty
	 */
	status_t (*get_first) (linked_list_t *this, void **item);

	/**
	 * Inserts a new item at the end of the list.
	 *
	 * @param item		value to insert into list
	 */
	void (*insert_last) (linked_list_t *this, void *item);

	/**
	 * Removes the last item in the list and returns its value.
	 *
	 * @param item		returned value of last item, or NULL
	 * @return			SUCCESS, NOT_FOUND if list is empty
	 */
	status_t (*remove_last) (linked_list_t *this, void **item);

	/**
	 * Returns the value of the last list item without removing it.
	 *
	 * @param item		returned value of last item
	 * @return			SUCCESS, NOT_FOUND if list is empty
	 */
	status_t (*get_last) (linked_list_t *this, void **item);

	/**
	 * Find the first matching element in the list.
	 *
	 * The first object passed to the match function is the current list item,
	 * followed by the user supplied data.
	 * If the supplied function returns TRUE so does this function, and the
	 * current object is returned in the third parameter (if given), otherwise,
	 * the next item is checked.
	 *
	 * If match is NULL, *item and the current object are compared.
	 *
	 * @param match			comparison function to call on each object, or NULL
	 * @param item			the list item, if found, or NULL
	 * @param ...			user data to supply to match function
	 * @return				TRUE if found, FALSE otherwise (or if neither match,
	 *						nor item is supplied)
	 */
	bool (*find_first)(linked_list_t *this, linked_list_match_t match,
					   void **item, ...);

	/**
	 * Invoke a method on all of the contained objects.
	 *
	 * If a linked list contains objects with function pointers,
	 * invoke() can call a method on each of the objects. The
	 * method is specified by an offset of the function pointer,
	 * which can be evalutated at compile time using the offsetof
	 * macro, e.g.: list->invoke(list, offsetof(object_t, method));
	 *
	 * @param offset	offset of the method to invoke on objects
	 */
	void (*invoke_offset)(linked_list_t *this, size_t offset);

	/**
	 * Invoke a function on all of the contained objects.
	 *
	 * @param function	function to call for each object
	 * @param ...		user data to supply to called function
	 */
	void (*invoke_function)(linked_list_t *this, linked_list_invoke_t function,
							...);

	/**
	 * Clones a list and its objects using the objects' clone method.
	 *
	 * @param offset	offset to the objects clone function
	 * @return			cloned list
	 */
	linked_list_t *(*clone_offset) (linked_list_t *this, size_t offset);

	/**
	 * Compare two lists and their objects for equality using the given equals
	 * method.
	 *
	 * @param other		list to compare
	 * @param offset	offset of the objects equals method
	 * @return			TRUE if lists and objects are equal, FALSE otherwise
	 */
	bool (*equals_offset) (linked_list_t *this, linked_list_t *other,
						   size_t offset);

	/**
	 * Compare two lists and their objects for equality using the given function.
	 *
	 * @param other		list to compare
	 * @param function	function to compare the objects
	 * @return			TRUE if lists and objects are equal, FALSE otherwise
	 */
	bool (*equals_function) (linked_list_t *this, linked_list_t *other,
							 bool (*)(void*,void*));

	/**
	 * Destroys a linked_list object.
	 */
	void (*destroy) (linked_list_t *this);

	/**
	 * Destroys a list and its objects using the destructor.
	 *
	 * If a linked list and the contained objects should be destroyed, use
	 * destroy_offset. The supplied offset specifies the destructor to
	 * call on each object. The offset may be calculated using the offsetof
	 * macro, e.g.: list->destroy_offset(list, offsetof(object_t, destroy));
	 *
	 * @param offset	offset of the objects destructor
	 */
	void (*destroy_offset) (linked_list_t *this, size_t offset);

	/**
	 * Destroys a list and its contents using a a cleanup function.
	 *
	 * If a linked list and its contents should get destroyed using a specific
	 * cleanup function, use destroy_function. This is useful when the
	 * list contains malloc()-ed blocks which should get freed,
	 * e.g.: list->destroy_function(list, free);
	 *
	 * @param function	function to call on each object
	 */
	void (*destroy_function) (linked_list_t *this, void (*)(void*));
};

/**
 * Creates an empty linked list object.
 *
 * @return		linked_list_t object.
 */
linked_list_t *linked_list_create(void);

/**
 * Creates a linked list from an enumerator.
 *
 * @param enumerator	enumerator over void*, gets destroyed
 * @return				linked_list_t object, containing enumerated values
 */
linked_list_t *linked_list_create_from_enumerator(enumerator_t *enumerator);

/**
 * Creates a linked list from a NULL terminated vararg list of items.
 *
 * @param first			first item
 * @param ...			subsequent items, terminated by NULL
 * @return				linked_list_t object, containing passed items
 */
linked_list_t *linked_list_create_with_items(void *first, ...);

#endif /** LINKED_LIST_H_ @}*/