1 /* EINA - EFL data type library
2 * Copyright (C) 2008 Cedric Bail
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
24 #include "eina_config.h"
26 #include "eina_types.h"
27 #include "eina_error.h"
28 #include "eina_iterator.h"
29 #include "eina_accessor.h"
30 #include "eina_magic.h"
34 * @page eina_array_01_example_page Basic array usage
35 * @dontinclude eina_array_01.c
37 * For this example we add stdlib.h, stdio.h and string.h for some
38 * convenience functions. The first thing to do to be able to use an
39 * @ref Eina_Array is to include Eina.h:
43 * Here we have a callback that prints the element given to it:
46 * Now we create our entry point and declare some variables, nothing especial:
49 * Before we can start using any array function we need to initialize eina:
52 * So now to actually creating our array. The only interesting thing here is the
53 * argument given to the eina_array_new() function, this argument sets how fast
57 * If you know before hand how big the array will need to be you should set the
58 * step to that. In our case we can set it to the number of string we have and
59 * since we didn't do that in the eina_array_new() we can do it now:
60 * @until array_step_set
62 * Now let us populate our array with some strings:
64 * @note Notice we use strdup, so we will have to free that memory later on.
66 * Now lets check the size of the array:
69 * And now we call a function on every member of our array to print it:
72 * One of the strengths of @ref Eina_Array over @ref Eina_List is that it has
73 * very fast random access to elements, so this is very efficient:
76 * And now we free up the memory allocated with the strdup()s:
79 * And the array memory itself:
82 * And finally shutdown eina and exit:
85 * The full source code can be found on the examples folder
86 * on the @ref eina_array_01_c "eina_array_01.c" file.
90 * @page eina_array_01_c Basic array usage example
92 * @include eina_array_01.c
93 * @example eina_array_01.c
97 * @page eina_array_02_example_page Removing array elements
98 * @dontinclude eina_array_02.c
100 * Just the usual includes:
104 * This the callback we are going to use to decide which strings stay on the
105 * array and which will be removed, we use something simple, but this can be as
106 * complex as you like:
109 * This is the same code we used before to populate the list with the slight
110 * difference of not using strdup:
113 * So we have added all our elements to the array, but it turns out that is not
114 * the elements we wanted, so let's empty the array and add the correct strings:
117 * It seems we made a little mistake in one of our strings so we need to replace
121 * Now that there is a populated array we can remove elements from it easily:
122 * @until array_remove
124 * And check that the elements were actually removed:
127 * Since this time we didn't use strdup we don't need to free each string:
130 * The full source code can be found on the examples folder
131 * on the @ref eina_array_02_c "eina_array_02.c" file.
135 * @page eina_array_02_c Basic array usage example
137 * @include eina_array_02.c
138 * @example eina_array_02.c
142 * @addtogroup Eina_Array_Group Array
144 * @brief These functions provide array management.
146 * The Array data type in Eina is designed to have very fast access to
147 * its data (compared to the Eina @ref Eina_List_Group). On the other hand,
148 * data can be added or removed only at the end of the array. To insert
149 * data at any place, the Eina @ref Eina_List_Group is the correct container
152 * To use the array data type, eina_init() must be called before any
153 * other array functions. When no more eina array functions are used,
154 * eina_shutdown() must be called to free all the resources.
156 * An array must be created with eina_array_new(). It allocates all
157 * the necessary data for an array. When not needed anymore, an array
158 * is freed with eina_array_free(). This function does not free any
159 * allocated memory used to store the data of each element. For that,
160 * just iterate over the array to free them. A convenient way to do
161 * that is by using #EINA_ARRAY_ITER_NEXT. An example of code is given
162 * in the description of this macro.
164 * @warning Functions do not check if the used array is valid or not. It's up to
165 * the user to be sure of that. It is designed like that for performance
168 * The usual features of an array are classic ones: to append an
169 * element, use eina_array_push() and to remove the last element, use
170 * eina_array_pop(). To retrieve the element at a given position, use
171 * eina_array_data_get(). The number of elements can be retrieved with
172 * eina_array_count().
174 * Eina_Array is different from a conventional C array in a number of ways, most
175 * importantly they grow and shrink dynamically, this means that if you add an
176 * element to a full array it grows and that when you remove an element from an
177 * array it @b may shrink.
179 * When the array needs to grow it allocates memory not just for the element
180 * currently being added since that would mean allocating memory(which is
181 * computationally expensive) often, instead it grows to be able to hold @p step
182 * more elements. Similarly if you remove elements in such a way that that the
183 * array is left holding its capacity - @p step elements it will shrink.
185 * The following image illustrates how an Eina_Array grows:
187 * @image html eina_array-growth.png
188 * @image latex eina_array-growth.eps width=\textwidth
190 * Eina_Array only stores pointers but it can store data of any type in the form
193 * See here some examples:
194 * @li @ref eina_array_01_example_page
195 * @li @ref eina_array_02_example_page
199 * @addtogroup Eina_Data_Types_Group Data Types
205 * @addtogroup Eina_Containers_Group Containers
211 * @defgroup Eina_Array_Group Array
217 * @typedef Eina_Array
218 * Type for a generic vector.
220 typedef struct _Eina_Array Eina_Array;
223 * @typedef Eina_Array_Iterator
224 * Type for an iterator on arrays, used with #EINA_ARRAY_ITER_NEXT.
226 typedef void **Eina_Array_Iterator;
229 * @struct _Eina_Array
230 * Type for an array of data.
234 #define EINA_ARRAY_VERSION 1
235 int version; /**< Should match EINA_ARRAY_VERSION used when compiled your apps, provided for ABI compatibility */
237 void **data; /**< Pointer to a vector of pointer to payload */
238 unsigned int total; /**< Total number of slots in the vector */
239 unsigned int count; /**< Number of active slots in the vector */
240 unsigned int step; /**< How much must we grow the vector when it is full */
246 * @brief Create a new array.
248 * @param step The count of pointers to add when increasing the array size.
249 * @return @c NULL on failure, non @c NULL otherwise.
251 * This function creates a new array. When adding an element, the array
252 * allocates @p step elements. When that buffer is full, then adding
253 * another element will increase the buffer by @p step elements again.
255 * This function return a valid array on success, or @c NULL if memory
256 * allocation fails. In that case, the error is set
257 * to #EINA_ERROR_OUT_OF_MEMORY.
259 EAPI Eina_Array *eina_array_new(unsigned int step) EINA_WARN_UNUSED_RESULT EINA_MALLOC EINA_WARN_UNUSED_RESULT;
262 * @brief Free an array.
264 * @param array The array to free.
266 * This function frees @p array. It calls first eina_array_flush() then
267 * free the memory of the pointer. It does not free the memory
268 * allocated for the elements of @p array. To free them,
269 * use #EINA_ARRAY_ITER_NEXT. For performance reasons, there is no check
272 EAPI void eina_array_free(Eina_Array *array) EINA_ARG_NONNULL(1);
275 * @brief Set the step of an array.
277 * @param array The array.
278 * @param sizeof_eina_array Should be the value returned by sizeof(Eina_Array).
279 * @param step The count of pointers to add when increasing the array size.
281 * This function sets the step of @p array to @p step. For performance
282 * reasons, there is no check of @p array. If it is @c NULL or
283 * invalid, the program may crash.
285 * @warning This function can @b only be called on uninitialized arrays.
287 EAPI void eina_array_step_set(Eina_Array *array,
288 unsigned int sizeof_eina_array,
289 unsigned int step) EINA_ARG_NONNULL(1);
291 * @brief Clean an array.
293 * @param array The array to clean.
295 * This function sets the count member of @p array to 0, however it doesn't free
296 * any space. This is particularly useful if you need to empty the array and
297 * add lots of elements quickly. For performance reasons, there is no check of
298 * @p array. If it is @c NULL or invalid, the program may crash.
300 static inline void eina_array_clean(Eina_Array *array) EINA_ARG_NONNULL(1);
303 * @brief Flush an array.
305 * @param array The array to flush.
307 * This function sets the count and total members of @p array to 0,
308 * frees and set to NULL its data member. For performance reasons,
309 * there is no check of @p array. If it is @c NULL or invalid, the
312 EAPI void eina_array_flush(Eina_Array *array) EINA_ARG_NONNULL(1);
315 * @brief Rebuild an array by specifying the data to keep.
317 * @param array The array.
318 * @param keep The functions which selects the data to keep.
319 * @param gdata The data to pass to the function keep.
320 * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
322 * This function rebuilds @p array be specifying the elements to keep with the
323 * function @p keep. No empty/invalid fields are left in the array. @p gdata is
324 * an additional data to pass to @p keep. For performance reasons, there is no
325 * check of @p array. If it is @c NULL or invalid, the program may crash.
327 * If it wasn't able to remove items due to an allocation failure, it will
328 * return #EINA_FALSE and the error is set to #EINA_ERROR_OUT_OF_MEMORY.
330 EAPI Eina_Bool eina_array_remove(Eina_Array * array,
331 Eina_Bool (*keep)(void *data, void *gdata),
332 void *gdata) EINA_ARG_NONNULL(1, 2);
333 static inline Eina_Bool eina_array_push(Eina_Array *array,
334 const void *data) EINA_ARG_NONNULL(1, 2);
335 static inline void *eina_array_pop(Eina_Array *array) EINA_ARG_NONNULL(1);
336 static inline void *eina_array_data_get(const Eina_Array *array,
337 unsigned int idx) EINA_ARG_NONNULL(1);
339 * @brief Set the data at a given position in an array.
341 * @param array The array.
342 * @param idx The position of the data to set.
343 * @param data The data to set.
345 * This function sets the data at the position @p idx in @p
346 * array to @p data, this effectively replaces the previously held data, you
347 * must therefore get a pointer to it first if you need to free it. For
348 * performance reasons, there is no check of @p array or @p idx. If it is @c
349 * NULL or invalid, the program may crash.
351 static inline void eina_array_data_set(const Eina_Array *array,
353 const void *data) EINA_ARG_NONNULL(1);
354 static inline unsigned int eina_array_count_get(const Eina_Array *array) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
355 static inline unsigned int eina_array_count(const Eina_Array *array) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
358 * @brief Returned a new iterator associated to an array.
360 * @param array The array.
361 * @return A new iterator.
363 * This function returns a newly allocated iterator associated to
364 * @p array. If @p array is @c NULL or the count member of @p array is
365 * less or equal than 0, this function returns @c NULL. If the memory can
366 * not be allocated, NULL is returned and #EINA_ERROR_OUT_OF_MEMORY is
367 * set. Otherwise, a valid iterator is returned.
369 EAPI Eina_Iterator *eina_array_iterator_new(const Eina_Array *array) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
372 * @brief Returned a new accessor associated to an array.
374 * @param array The array.
375 * @return A new accessor.
377 * This function returns a newly allocated accessor associated to
378 * @p array. If @p array is @c NULL or the count member of @p array is
379 * less or equal than 0, this function returns @c NULL. If the memory can
380 * not be allocated, @c NULL is returned and #EINA_ERROR_OUT_OF_MEMORY is
381 * set. Otherwise, a valid accessor is returned.
383 EAPI Eina_Accessor *eina_array_accessor_new(const Eina_Array *array) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
385 * @brief Provide a safe way to iterate over an array
387 * @param array The array to iterate over.
388 * @param cb The callback to call for each item.
389 * @param fdata The user data to pass to the callback.
390 * @return #EINA_TRUE if it successfully iterate all items of the array.
392 * This function provide a safe way to iterate over an array. @p cb should
393 * return #EINA_TRUE as long as you want the function to continue iterating,
394 * by returning #EINA_FALSE it will stop and return #EINA_FALSE as a result.
396 static inline Eina_Bool eina_array_foreach(Eina_Array *array,
400 * @def EINA_ARRAY_ITER_NEXT
401 * @brief Macro to iterate over an array easily.
403 * @param array The array to iterate over.
404 * @param index The integer number that is increased while iterating.
405 * @param item The data
406 * @param iterator The iterator
408 * This macro allows the iteration over @p array in an easy way. It
409 * iterates from the first element to the last one. @p index is an
410 * integer that increases from 0 to the number of elements. @p item is
411 * the data of each element of @p array, so it is a pointer to a type
412 * chosen by the user. @p iterator is of type #Eina_Array_Iterator.
414 * This macro can be used for freeing the data of an array, like in
415 * the following example:
420 * Eina_Array_Iterator iterator;
423 * // array is already filled,
424 * // its elements are just duplicated strings,
425 * // EINA_ARRAY_ITER_NEXT will be used to free those strings
427 * EINA_ARRAY_ITER_NEXT(array, i, item, iterator)
431 #define EINA_ARRAY_ITER_NEXT(array, index, item, iterator) \
432 for (index = 0, iterator = (array)->data; \
433 (index < eina_array_count(array)) && ((item = *((iterator)++))); \
436 #include "eina_inline_array.x"