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/>.
19 #ifndef EINA_INLINE_ARRAY_X_
20 #define EINA_INLINE_ARRAY_X_
30 EAPI Eina_Bool eina_array_grow(Eina_Array *array);
37 * @addtogroup Eina_Array_Group Array
39 * @brief These functions provide array management.
45 * @brief Append a data to an array.
47 * @param array The array.
48 * @param data The data to add.
49 * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
51 * This function appends @p data to @p array. For performance
52 * reasons, there is no check of @p array. If it is @c NULL or
53 * invalid, the program may crash. If @p data is @c NULL, or if an
54 * allocation is necessary and fails, #EINA_FALSE is returned
55 * and #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, #EINA_TRUE is
59 static inline Eina_Bool
60 eina_array_push(Eina_Array *array, const void *data)
62 if (!data) return EINA_FALSE;
64 if (EINA_UNLIKELY((array->count + 1) > array->total))
65 if (!eina_array_grow(array))
68 array->data[array->count++] = (void*) data;
74 * @brief Remove the last data of an array.
76 * @param array The array.
77 * @return The retrieved data.
79 * This function removes the last data of @p array, decreases the count
80 * of @p array and returns the data. For performance reasons, there
81 * is no check of @p array. If it is @c NULL or invalid, the program
82 * may crash. If the count member is less or equal than 0, @c NULL is
86 eina_array_pop(Eina_Array *array)
90 if (array->count <= 0)
93 ret = array->data[--array->count];
100 * @brief Return the data at a given position in an array.
102 * @param array The array.
103 * @param idx The potition of the data to retrieve.
104 * @return The retrieved data.
106 * This function returns the data at the position @p idx in @p
107 * array. For performance reasons, there is no check of @p array or @p
108 * idx. If it is @c NULL or invalid, the program may crash.
111 eina_array_data_get(const Eina_Array *array, unsigned int idx)
113 return array->data[idx];
117 eina_array_data_set(const Eina_Array *array, unsigned int idx, const void *data)
119 array->data[idx] = (void*) data;
123 * @brief Return the number of elements in an array.
125 * @param array The array.
126 * @return The number of elements.
128 * This function returns the number of elements in @p array. For
129 * performance reasons, there is no check of @p array. If it is
130 * @c NULL or invalid, the program may crash.
132 * @deprecated use eina_array_count()
134 static inline unsigned int
135 eina_array_count_get(const Eina_Array *array)
141 * @brief Return the number of elements in an array.
143 * @param array The array.
144 * @return The number of elements.
146 * This function returns the number of elements in @p array. For
147 * performance reasons, there is no check of @p array. If it is
148 * @c NULL or invalid, the program may crash.
150 static inline unsigned int
151 eina_array_count(const Eina_Array *array)
156 static inline Eina_Bool
157 eina_array_foreach(Eina_Array *array, Eina_Each_Cb cb, void *fdata)
160 Eina_Array_Iterator iterator;
162 Eina_Bool ret = EINA_TRUE;
164 EINA_ARRAY_ITER_NEXT(array, i, data, iterator)
165 if (cb(array, data, fdata) != EINA_TRUE)
175 eina_array_clean(Eina_Array *array)