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 static inline Eina_Bool
eina_array_push(Eina_Array * array,const void * data)45 eina_array_push(Eina_Array *array, const void *data)
46 {
47 if (EINA_UNLIKELY(data == NULL)) return EINA_FALSE;
48 if (EINA_UNLIKELY((array->count + 1) > array->total)) goto do_grow;
49 do_grow_back:
50
51 array->data[array->count++] = (void*) data;
52
53 return EINA_TRUE;
54 do_grow:
55 if (!eina_array_grow(array)) return EINA_FALSE;
56 goto do_grow_back;
57 }
58
59 static inline void *
eina_array_pop(Eina_Array * array)60 eina_array_pop(Eina_Array *array)
61 {
62 if (array->count > 0) return array->data[--array->count];
63 return NULL;
64 }
65
66 static inline void *
eina_array_data_get(const Eina_Array * array,unsigned int idx)67 eina_array_data_get(const Eina_Array *array, unsigned int idx)
68 {
69 return array->data[idx];
70 }
71
72 static inline void
eina_array_data_set(const Eina_Array * array,unsigned int idx,const void * data)73 eina_array_data_set(const Eina_Array *array, unsigned int idx, const void *data)
74 {
75 array->data[idx] = (void*) data;
76 }
77
78
79 static inline unsigned int
eina_array_count_get(const Eina_Array * array)80 eina_array_count_get(const Eina_Array *array)
81 {
82 return array->count;
83 }
84
85
86 static inline unsigned int
eina_array_count(const Eina_Array * array)87 eina_array_count(const Eina_Array *array)
88 {
89 if (!array) return 0;
90 return array->count;
91 }
92
93 static inline Eina_Bool
eina_array_find(const Eina_Array * array,const void * data,unsigned int * out_idx)94 eina_array_find(const Eina_Array *array, const void *data, unsigned int *out_idx)
95 {
96 unsigned int i;
97
98 if (!array) return EINA_FALSE;
99
100 for (i = 0; i < array->count; i++)
101 {
102 if (array->data[i] == data)
103 {
104 if (out_idx) *out_idx = i;
105
106 return EINA_TRUE;
107 }
108 }
109 return EINA_FALSE;
110 }
111
112 static inline Eina_Bool
eina_array_foreach(Eina_Array * array,Eina_Each_Cb cb,void * fdata)113 eina_array_foreach(Eina_Array *array, Eina_Each_Cb cb, void *fdata)
114 {
115 void *data;
116 Eina_Array_Iterator iterator;
117 unsigned int i;
118 Eina_Bool ret = EINA_TRUE;
119
120 EINA_ARRAY_ITER_NEXT(array, i, data, iterator)
121 {
122 if (cb(array, data, fdata) == EINA_TRUE) continue;
123 ret = EINA_FALSE;
124 break;
125 }
126 return ret;
127 }
128
129 static inline void
eina_array_clean(Eina_Array * array)130 eina_array_clean(Eina_Array *array)
131 {
132 array->count = 0;
133 }
134
135 /**
136 * @}
137 */
138
139 #endif
140