EFL 1.7 svn doobies
[profile/ivi/eina.git] / src / include / eina_inline_array.x
1 /* EINA - EFL data type library
2  * Copyright (C) 2008 Cedric Bail
3  *
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.
8  *
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.
13  *
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/>.
17  */
18
19 #ifndef EINA_INLINE_ARRAY_X_
20 #define EINA_INLINE_ARRAY_X_
21
22 #include <stddef.h>
23
24 #include <stdio.h>
25
26 /**
27  * @cond LOCAL
28  */
29
30 EAPI Eina_Bool eina_array_grow(Eina_Array *array);
31
32 /**
33  * @endcond
34  */
35
36 /**
37  * @addtogroup Eina_Array_Group Array
38  *
39  * @brief These functions provide array management.
40  *
41  * @{
42  */
43
44 /**
45  * @brief Append a data to an array.
46  *
47  * @param array The array.
48  * @param data The data to add.
49  * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
50  *
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
56  * returned.
57  */
58
59 static inline Eina_Bool
60 eina_array_push(Eina_Array *array, const void *data)
61 {
62    if (!data) return EINA_FALSE;
63
64    if (EINA_UNLIKELY((array->count + 1) > array->total))
65      if (!eina_array_grow(array))
66        return EINA_FALSE;
67
68    array->data[array->count++] = (void*) data;
69
70    return EINA_TRUE;
71 }
72
73 /**
74  * @brief Remove the last data of an array.
75  *
76  * @param array The array.
77  * @return The retrieved data.
78  *
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
83  * returned.
84  */
85 static inline void *
86 eina_array_pop(Eina_Array *array)
87 {
88    void *ret = NULL;
89
90    if (array->count <= 0)
91      goto on_empty;
92
93    ret = array->data[--array->count];
94
95  on_empty:
96    return ret;
97 }
98
99 /**
100  * @brief Return the data at a given position in an array.
101  *
102  * @param array The array.
103  * @param idx The potition of the data to retrieve.
104  * @return The retrieved data.
105  *
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.
109  */
110 static inline void *
111 eina_array_data_get(const Eina_Array *array, unsigned int idx)
112 {
113    return array->data[idx];
114 }
115
116 static inline void
117 eina_array_data_set(const Eina_Array *array, unsigned int idx, const void *data)
118 {
119    array->data[idx] = (void*) data;
120 }
121
122 /**
123  * @brief Return the number of elements in an array.
124  *
125  * @param array The array.
126  * @return The number of elements.
127  *
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.
131  *
132  * @deprecated use eina_array_count()
133  */
134 static inline unsigned int
135 eina_array_count_get(const Eina_Array *array)
136 {
137    return array->count;
138 }
139
140 /**
141  * @brief Return the number of elements in an array.
142  *
143  * @param array The array.
144  * @return The number of elements.
145  *
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.
149  */
150 static inline unsigned int
151 eina_array_count(const Eina_Array *array)
152 {
153    return array->count;
154 }
155
156 static inline Eina_Bool
157 eina_array_foreach(Eina_Array *array, Eina_Each_Cb cb, void *fdata)
158 {
159    void *data;
160    Eina_Array_Iterator iterator;
161    unsigned int i;
162    Eina_Bool ret = EINA_TRUE;
163
164    EINA_ARRAY_ITER_NEXT(array, i, data, iterator)
165      if (cb(array, data, fdata) != EINA_TRUE)
166        {
167           ret = EINA_FALSE;
168           break;
169        }
170
171    return ret;
172 }
173
174 static inline void
175 eina_array_clean(Eina_Array *array)
176 {
177    array->count = 0;
178 }
179
180 /**
181  * @}
182  */
183
184 #endif