array: Add array_bsearch function
[strongswan.git] / src / libstrongswan / collections / array.h
1 /*
2 * Copyright (C) 2014 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * Copyright (C) 2013 Martin Willi
6 * Copyright (C) 2013 revosec AG
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 /**
20 * @defgroup array array
21 * @{ @ingroup collections
22 */
23
24 #ifndef ARRAY_H_
25 #define ARRAY_H_
26
27 #include <collections/enumerator.h>
28
29 /**
30 * Variable sized array with fixed size elements.
31 *
32 * An array is a primitive object with associated functions to avoid the
33 * overhead of an object with methods. It is efficient in memory usage, but
34 * less efficient than a linked list in manipulating elements.
35 */
36 typedef struct array_t array_t;
37
38 typedef enum array_idx_t array_idx_t;
39
40 /**
41 * Special array index values for insert/remove.
42 */
43 enum array_idx_t {
44 ARRAY_HEAD = 0,
45 ARRAY_TAIL = -1,
46 };
47
48 /**
49 * Callback function invoked for each array element.
50 *
51 * Data is a pointer to the array element. If this is a pointer based array,
52 * (esize is zero), data is the pointer itself.
53 *
54 * @param data pointer to array data, or the pointer itself
55 * @param idx array index
56 * @param user user data passed with callback
57 */
58 typedef void (*array_callback_t)(void *data, int idx, void *user);
59
60 /**
61 * Create a array instance.
62 *
63 * Elements get tight packed to each other. If any alignment is required, pass
64 * appropriate padding to each element. The reserved space does not affect
65 * array_count(), but just preallocates buffer space.
66 *
67 * @param esize element size for this array, use 0 for a pointer array
68 * @param reserve number of items to allocate space for
69 * @return array instance
70 */
71 array_t *array_create(u_int esize, u_int8_t reserve);
72
73 /**
74 * Get the number of elements currently in the array.
75 *
76 * @return number of elements
77 */
78 int array_count(array_t *array);
79
80 /**
81 * Compress an array, remove unused head/tail space.
82 *
83 * @param array array to compress, or NULL
84 */
85 void array_compress(array_t *array);
86
87 /**
88 * Create an enumerator over an array.
89 *
90 * The enumerater enumerates directly over the array element (pass a pointer to
91 * element types), unless the array is pointer based. If zero is passed as
92 * element size during construction, the enumerator enumerates over the
93 * dereferenced pointer values.
94 *
95 * @param array array to create enumerator for, or NULL
96 * @return enumerator, over elements or pointers
97 */
98 enumerator_t* array_create_enumerator(array_t *array);
99
100 /**
101 * Remove an element at enumerator position.
102 *
103 * @param array array to remove element in
104 * @param enumerator enumerator position, from array_create_enumerator()
105 */
106 void array_remove_at(array_t *array, enumerator_t *enumerator);
107
108 /**
109 * Insert an element to an array.
110 *
111 * If the array is pointer based (esize = 0), the pointer itself is appended.
112 * Otherwise the element gets copied from the pointer.
113 * The idx must be either within array_count() or one above to append the item.
114 * Passing -1 has the same effect as passing array_count(), i.e. appends the
115 * item. It is always valid to pass idx 0 to prepend the item.
116 *
117 * @param array array to append element to
118 * @param idx index to insert item at
119 * @param data pointer to array element to copy
120 */
121 void array_insert(array_t *array, int idx, void *data);
122
123 /**
124 * Create an pointer based array if it does not exist, insert pointer.
125 *
126 * This is a convenience function for insert a pointer and implicitly
127 * create a pointer based array if array is NULL. Array is set the the newly
128 * created array, if any.
129 *
130 * @param array pointer to array reference, potentially NULL
131 * @param idx index to insert item at
132 * @param ptr pointer to append
133 */
134 void array_insert_create(array_t **array, int idx, void *ptr);
135
136 /**
137 * Insert all items from an enumerator to an array.
138 *
139 * @param array array to add items to
140 * @param idx index to insert each item with
141 * @param enumerator enumerator over void*, gets destroyed
142 */
143 void array_insert_enumerator(array_t *array, int idx, enumerator_t *enumerator);
144
145 /**
146 * Get an element from the array.
147 *
148 * If data is given, the element is copied to that position.
149 *
150 * @param array array to get element from, or NULL
151 * @param idx index of the item to get
152 * @param data data to copy element to, or NULL
153 * @return TRUE if idx valid and item returned
154 */
155 bool array_get(array_t *array, int idx, void *data);
156
157 /**
158 * Remove an element from the array.
159 *
160 * If data is given, the element is copied to that position.
161 *
162 * @param array array to remove element from, or NULL
163 * @param idx index of the item to remove
164 * @param data data to copy element to, or NULL
165 * @return TRUE if idx existed and item removed
166 */
167 bool array_remove(array_t *array, int idx, void *data);
168
169 /**
170 * Sort the array.
171 *
172 * The comparison function must return an integer less than, equal to, or
173 * greater than zero if the first argument is considered to be respectively less
174 * than, equal to, or greater than the second. If two elements compare as
175 * equal, their order in the sorted array is undefined.
176 *
177 * The comparison function receives pointers to the array elements (esize != 0)
178 * or the actual pointers (esize = 0). The third argument is the user data
179 * supplied to this function.
180 *
181 * @param array array to sort, or NULL
182 * @param cmp comparison function
183 * @param user user data to pass to comparison function
184 */
185 void array_sort(array_t *array, int (*cmp)(const void*,const void*,void*),
186 void *user);
187
188 /**
189 * Binary search of a sorted array.
190 *
191 * The array should be sorted in ascending order according to the given
192 * comparison function.
193 *
194 * The comparison function must return an integer less than, equal to, or
195 * greater than zero if the first argument (the key) is considered to be
196 * respectively less than, equal to, or greater than the second.
197 *
198 * If there are multiple elements that match the key it is not specified which
199 * element is returned.
200 *
201 * The comparison function receives the key object and a pointer to an array
202 * element (esize != 0) or an actual pointer (esize = 0).
203 *
204 * @param array array to search, or NULL
205 * @param key key to search for
206 * @param cmp comparison function
207 * @param data data to copy element to, or NULL
208 * @return index of the element if found, -1 if not
209 */
210 int array_bsearch(array_t *array, const void *key,
211 int (*cmp)(const void*,const void*), void *data);
212
213 /**
214 * Invoke a callback for all array members.
215 *
216 * @param array array to traverse, or NULL
217 * @param cb callback function to invoke each element with
218 * @param user user data to pass to callback
219 */
220 void array_invoke(array_t *array, array_callback_t cb, void *user);
221
222 /**
223 * Invoke a method of each element defined with offset.
224 *
225 * @param array array to traverse, or NULL
226 * @param offset offset of element method, use offsetof()
227 */
228 void array_invoke_offset(array_t *array, size_t offset);
229
230 /**
231 * Destroy an array.
232 *
233 * @param array array to destroy, or NULL
234 */
235 void array_destroy(array_t *array);
236
237 /**
238 * Destroy an array, call a function to clean up all elements.
239 *
240 * @param array array to destroy, or NULL
241 * @param cb callback function to free element data
242 * @param user user data to pass to callback
243 */
244 void array_destroy_function(array_t *array, array_callback_t cb, void *user);
245
246 /**
247 * Destroy an array, call element method defined with offset.
248 *
249 * @param array array to destroy, or NULL
250 * @param offset offset of element method, use offsetof()
251 */
252 void array_destroy_offset(array_t *array, size_t offset);
253
254 #endif /** ARRAY_H_ @}*/