aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/eina/src/include/eina_inarray.h
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/eina/src/include/eina_inarray.h')
-rw-r--r--libraries/eina/src/include/eina_inarray.h572
1 files changed, 572 insertions, 0 deletions
diff --git a/libraries/eina/src/include/eina_inarray.h b/libraries/eina/src/include/eina_inarray.h
new file mode 100644
index 0000000..d37c76b
--- /dev/null
+++ b/libraries/eina/src/include/eina_inarray.h
@@ -0,0 +1,572 @@
1/* EINA - EFL data type library
2 * Copyright (C) 2012 ProFUSION embedded systems
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_INARRAY_H_
20#define EINA_INARRAY_H_
21
22#include "eina_types.h"
23#include "eina_iterator.h"
24#include "eina_accessor.h"
25
26/**
27 * @addtogroup Eina_Data_Types_Group Data Types
28 *
29 * @since 1.2
30 *
31 * @{
32 */
33
34/**
35 * @addtogroup Eina_Containers_Group Containers
36 *
37 * @{
38 */
39
40/**
41 * @defgroup Eina_Inline_Array_Group Inline Array
42 *
43 * @{
44 */
45
46
47/**
48 * @typedef Eina_Inarray
49 * Inlined array type.
50 *
51 * @since 1.2
52 */
53typedef struct _Eina_Inarray Eina_Inarray;
54
55/**
56 * Inline array structure, use #Eina_Inarray typedef instead.
57 *
58 * Do not modify these fields directly, use eina_inarray_setup() or
59 * eina_inarray_new() instead.
60 *
61 * @since 1.2
62 */
63struct _Eina_Inarray
64{
65 unsigned int member_size; /**< byte size of each entry in members */
66 unsigned int len; /**< number of elements used in members */
67 unsigned int max; /**< number of elements allocated in members */
68 unsigned int step; /**< amount to grow number of members allocated */
69 void *members; /**< actual array of elements */
70 EINA_MAGIC
71};
72
73/**
74 * @brief Create new inline array.
75 *
76 * @param member_size size of each member in the array.
77 * @param step when resizing the array, do this using the following
78 * extra amount.
79 * @return The new inline array table or @c NULL on failure.
80 *
81 * Create a new array where members are inlined in a sequence. Each
82 * member has @a member_size bytes.
83 *
84 * If the @a step is 0, then a safe default is chosen.
85 *
86 * On failure, @c NULL is returned and #EINA_ERROR_OUT_OF_MEMORY is
87 * set. If @a member_size is zero, then @c NULL is returned.
88 *
89 * @see eina_inarray_free()
90 *
91 * @since 1.2
92 */
93EAPI Eina_Inarray *eina_inarray_new(unsigned int member_size,
94 unsigned int step) EINA_MALLOC EINA_WARN_UNUSED_RESULT;
95
96/**
97 * @brief Free array and its members.
98 * @param array array object
99 *
100 * @see eina_inarray_flush()
101 *
102 * @since 1.2
103 */
104EAPI void eina_inarray_free(Eina_Inarray *array) EINA_ARG_NONNULL(1);
105
106/**
107 * @brief Initialize inline array.
108 * @param array array object to initialize.
109 * @param member_size size of each member in the array.
110 * @param step when resizing the array, do this using the following
111 * extra amount.
112 *
113 * Initialize array. If the @a step is 0, then a safe default is
114 * chosen.
115 *
116 * This is useful for arrays inlined into other structures or
117 * allocated at stack.
118 *
119 * @see eina_inarray_flush()
120 *
121 * @since 1.2
122 */
123EAPI void eina_inarray_setup(Eina_Inarray *array,
124 unsigned int member_size,
125 unsigned int step) EINA_ARG_NONNULL(1);
126
127/**
128 * @brief Remove every member from array.
129 * @param array array object
130 *
131 * @since 1.2
132 */
133EAPI void eina_inarray_flush(Eina_Inarray *array) EINA_ARG_NONNULL(1);
134
135/**
136 * @brief Copy the data as the last member of the array.
137 * @param array array object
138 * @param data data to be copied at the end
139 * @return the index of the new member or -1 on errors.
140 *
141 * Copies the given pointer contents at the end of the array. The
142 * pointer is not referenced, instead it's contents is copied to the
143 * members array using the previously defined @c member_size.
144 *
145 * @see eina_inarray_insert_at().
146 *
147 * @since 1.2
148 */
149EAPI int eina_inarray_append(Eina_Inarray *array,
150 const void *data) EINA_ARG_NONNULL(1, 2);
151
152/**
153 * @brief Copy the data to array at position found by comparison function
154 * @param array array object
155 * @param data data to be copied
156 * @param compare compare function
157 * @return the index of the new member or -1 on errors.
158 *
159 * Copies the given pointer contents at the array position defined by
160 * given @a compare function. The pointer is not referenced, instead
161 * it's contents is copied to the members array using the previously
162 * defined @c member_size.
163 *
164 * The data given to @a compare function are the pointer to member
165 * memory itself, do no change it.
166 *
167 * @see eina_inarray_insert_sorted()
168 * @see eina_inarray_insert_at()
169 * @see eina_inarray_append()
170 *
171 * @since 1.2
172 */
173EAPI int eina_inarray_insert(Eina_Inarray *array,
174 const void *data,
175 Eina_Compare_Cb compare) EINA_ARG_NONNULL(1, 2, 3);
176
177/**
178 * @brief Copy the data to array at position found by comparison function
179 * @param array array object
180 * @param data data to be copied
181 * @param compare compare function
182 * @return the index of the new member or -1 on errors.
183 *
184 * Copies the given pointer contents at the array position defined by
185 * given @a compare function. The pointer is not referenced, instead
186 * it's contents is copied to the members array using the previously
187 * defined @c member_size.
188 *
189 * The data given to @a compare function are the pointer to member
190 * memory itself, do no change it.
191 *
192 * This variation will optimize insertion position assuming the array
193 * is already sorted by doing binary search.
194 *
195 * @see eina_inarray_sort()
196 *
197 * @since 1.2
198 */
199EAPI int eina_inarray_insert_sorted(Eina_Inarray *array,
200 const void *data,
201 Eina_Compare_Cb compare) EINA_ARG_NONNULL(1, 2, 3);
202
203/**
204 * @brief Find data and remove matching member
205 * @param array array object
206 * @param data data to be found and removed
207 * @return the index of the removed member or -1 on errors.
208 *
209 * Find data in the array and remove it. Data may be an existing
210 * member of array (then optimized) or the contents will be matched
211 * using memcmp().
212 *
213 * @see eina_inarray_pop()
214 * @see eina_inarray_remove_at()
215 *
216 * @since 1.2
217 */
218EAPI int eina_inarray_remove(Eina_Inarray *array,
219 const void *data) EINA_ARG_NONNULL(1, 2);
220
221/**
222 * @brief Removes the last member of the array
223 * @param array array object
224 * @return the index of the removed member or -1 on errors.
225 *
226 * @since 1.2
227 */
228EAPI int eina_inarray_pop(Eina_Inarray *array) EINA_ARG_NONNULL(1);
229
230/**
231 * @brief Get the member at given position
232 * @param array array object
233 * @param position member position
234 * @return pointer to current member memory.
235 *
236 * Gets the member given its position in the array. It is a pointer to
237 * its current memory, then it can be invalidated with functions that
238 * changes the array such as eina_inarray_append(),
239 * eina_inarray_insert_at() or eina_inarray_remove_at() or variants.
240 *
241 * See also eina_inarray_lookup() and eina_inarray_lookup_sorted().
242 *
243 * @since 1.2
244 */
245EAPI void *eina_inarray_nth(const Eina_Inarray *array,
246 unsigned int position) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
247
248/**
249 * @brief Copy the data at given position in the array
250 * @param array array object
251 * @param position where to insert the member
252 * @param data data to be copied at position
253 * @return #EINA_TRUE on success, #EINA_FALSE on failure.
254 *
255 * Copies the given pointer contents at the given @a position in the
256 * array. The pointer is not referenced, instead it's contents is
257 * copied to the members array using the previously defined
258 * @c member_size.
259 *
260 * All the members from @a position to the end of the array are
261 * shifted to the end.
262 *
263 * If @a position is equal to the end of the array (equals to
264 * eina_inarray_count()), then the member is appended.
265 *
266 * If @a position is bigger than the array length, it will fail.
267 *
268 * @since 1.2
269 */
270EAPI Eina_Bool eina_inarray_insert_at(Eina_Inarray *array,
271 unsigned int position,
272 const void *data) EINA_ARG_NONNULL(1, 3);
273
274/**
275 * @brief Opens a space at given position, returning its pointer.
276 * @param array array object
277 * @param position where to insert first member (open/allocate space)
278 * @param member_count how many times member_size bytes will be allocated.
279 * @return pointer to first member memory allocated or @c NULL on errors.
280 *
281 * This is similar to eina_inarray_insert_at(), but useful if the
282 * members contents are still unknown or unallocated. It will make
283 * room for the required number of items and return the pointer to the
284 * first item, similar to malloc(member_count * member_size), with the
285 * guarantee all memory is within members array.
286 *
287 * The new member memory is undefined, it's not automatically zeroed.
288 *
289 * All the members from @a position to the end of the array are
290 * shifted to the end.
291 *
292 * If @a position is equal to the end of the array (equals to
293 * eina_inarray_count()), then the member is appended.
294 *
295 * If @a position is bigger than the array length, it will fail.
296 *
297 * @since 1.2
298 */
299EAPI void *eina_inarray_alloc_at(Eina_Inarray *array,
300 unsigned int position,
301 unsigned int member_count) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
302
303/**
304 * @brief Copy the data over the given position.
305 * @param array array object
306 * @param position where to replace the member
307 * @param data data to be copied at position
308 * @return #EINA_TRUE on success, #EINA_FALSE on failure.
309 *
310 * Copies the given pointer contents at the given @a position in the
311 * array. The pointer is not referenced, instead it's contents is
312 * copied to the members array using the previously defined
313 * @c member_size.
314 *
315 * If @a position does not exist, it will fail.
316 *
317 * @since 1.2
318 */
319EAPI Eina_Bool eina_inarray_replace_at(Eina_Inarray *array,
320 unsigned int position,
321 const void *data) EINA_ARG_NONNULL(1, 3);
322
323/**
324 * @brief Remove member at given position
325 * @param array array object
326 * @param position position to be removed
327 * @return #EINA_TRUE on success, #EINA_FALSE on failure.
328 *
329 * The member is removed from array and any members after it are moved
330 * towards the array head.
331 *
332 * See also eina_inarray_pop() and eina_inarray_remove().
333 *
334 * @since 1.2
335 */
336EAPI Eina_Bool eina_inarray_remove_at(Eina_Inarray *array,
337 unsigned int position) EINA_ARG_NONNULL(1);
338
339/**
340 * @brief Reverse members in the array.
341 * @param array array object
342 *
343 * If you do not want to change the array, just walk its elements
344 * backwards, then use EINA_INARRAY_REVERSE_FOREACH() macro.
345 *
346 * @see EINA_INARRAY_REVERSE_FOREACH()
347 *
348 * @since 1.2
349 */
350EAPI void eina_inarray_reverse(Eina_Inarray *array) EINA_ARG_NONNULL(1);
351
352/**
353 * @brief Applies quick sort to array
354 * @param array array object
355 * @param compare compare function
356 *
357 * Applies quick sort to the @a array.
358 *
359 * The data given to @a compare function are the pointer to member
360 * memory itself, do no change it.
361 *
362 * @see eina_inarray_insert_sorted()
363 *
364 * @since 1.2
365 */
366EAPI void eina_inarray_sort(Eina_Inarray *array,
367 Eina_Compare_Cb compare) EINA_ARG_NONNULL(1, 2);
368
369/**
370 * @brief Search member (linear walk)
371 * @param array array object
372 * @param data member to search using @a compare function.
373 * @param compare compare function
374 * @return the member index or -1 if not found.
375 *
376 * Walks array linearly looking for given data as compared by
377 * @a compare function.
378 *
379 * The data given to @a compare function are the pointer to member
380 * memory itself, do no change it.
381 *
382 * See also eina_inarray_lookup_sorted().
383 *
384 * @since 1.2
385 */
386EAPI int eina_inarray_search(const Eina_Inarray *array,
387 const void *data,
388 Eina_Compare_Cb compare) EINA_ARG_NONNULL(1, 2, 3);
389
390/**
391 * @brief Search member (binary search walk)
392 * @param array array object
393 * @param data member to search using @a compare function.
394 * @param compare compare function
395 * @return the member index or -1 if not found.
396 *
397 * Uses binary search for given data as compared by @a compare function.
398 *
399 * The data given to @a compare function are the pointer to member
400 * memory itself, do no change it.
401 *
402 * @since 1.2
403 */
404EAPI int eina_inarray_search_sorted(const Eina_Inarray *array,
405 const void *data,
406 Eina_Compare_Cb compare) EINA_ARG_NONNULL(1, 2, 3);
407
408/**
409 * @brief Call function for each array member
410 * @param array array object
411 * @param function callback function
412 * @param user_data user data given to callback @a function
413 * @return #EINA_TRUE if it successfully iterate all items of the array.
414 *
415 * Call @a function for every given data in @a array.
416 *
417 * Safe way to iterate over an array. @p function should return
418 * #EINA_TRUE as long as you want the function to continue iterating,
419 * by returning #EINA_FALSE it will stop and return #EINA_FALSE as a
420 * result.
421 *
422 * The data given to @a function are the pointer to member memory
423 * itself.
424 *
425 * @see EINA_INARRAY_FOREACH()
426 *
427 * @since 1.2
428 */
429EAPI Eina_Bool eina_inarray_foreach(const Eina_Inarray *array,
430 Eina_Each_Cb function,
431 const void *user_data) EINA_ARG_NONNULL(1, 2);
432
433/**
434 * @brief Remove all members that matched.
435 * @param array array object
436 * @param match match function
437 * @param user_data user data given to callback @a match.
438 * @return number of removed entries or -1 on error.
439 *
440 * Remove all entries in the @a array where @a match function
441 * returns #EINA_TRUE.
442 *
443 * @since 1.2
444 */
445EAPI int eina_inarray_foreach_remove(Eina_Inarray *array,
446 Eina_Each_Cb match,
447 const void *user_data) EINA_ARG_NONNULL(1, 2);
448
449/**
450 * @brief number of members in array.
451 * @param array array object
452 * @return number of members in array.
453 *
454 * @since 1.2
455 */
456EAPI unsigned int eina_inarray_count(const Eina_Inarray *array) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
457
458/**
459 * @brief Returned a new iterator associated to an array.
460 * @param array array object
461 * @return A new iterator.
462 *
463 * This function returns a newly allocated iterator associated to
464 * @p array.
465 *
466 * If the memory can not be allocated, NULL is returned and
467 * #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
468 * returned.
469 *
470 * @warning if the array structure changes then the iterator becomes
471 * invalid! That is, if you add or remove members this
472 * iterator behavior is undefined and your program may crash!
473 *
474 * @since 1.2
475 */
476EAPI Eina_Iterator *eina_inarray_iterator_new(const Eina_Inarray *array) EINA_MALLOC EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1);
477
478/**
479 * @brief Returned a new reversed iterator associated to an array.
480 * @param array array object
481 * @return A new iterator.
482 *
483 * This function returns a newly allocated iterator associated to
484 * @p array.
485 *
486 * Unlike eina_inarray_iterator_new(), this will walk the array backwards.
487 *
488 * If the memory can not be allocated, NULL is returned and
489 * #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
490 * returned.
491 *
492 * @warning if the array structure changes then the iterator becomes
493 * invalid! That is, if you add or remove nodes this iterator
494 * behavior is undefined and your program may crash!
495 *
496 * @since 1.2
497 */
498EAPI Eina_Iterator *eina_inarray_iterator_reversed_new(const Eina_Inarray *array) EINA_MALLOC EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1);
499
500/**
501 * @brief Returned a new accessor associated to an array.
502 * @param array array object
503 * @return A new accessor.
504 *
505 * This function returns a newly allocated accessor associated to
506 * @p array.
507 *
508 * If the memory can not be allocated, NULL is returned and
509 * #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid accessor is
510 * returned.
511 *
512 * @since 1.2
513 */
514EAPI Eina_Accessor *eina_inarray_accessor_new(const Eina_Inarray *array) EINA_MALLOC EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1);
515
516/**
517 * @def EINA_INARRAY_FOREACH
518 * @brief walks array linearly from head to tail
519 * @param array array object
520 * @param itr the iterator pointer
521 *
522 * @a itr must be a pointer with sizeof(itr*) == array->member_size.
523 *
524 * @warning This is fast as it does direct pointer access, but it will
525 * not check for @c NULL pointers or invalid array object!
526 * See eina_inarray_foreach() to do that.
527 *
528 * @warning Do not modify array as you walk it! If that is desired,
529 * then use eina_inarray_foreach_remove()
530 *
531 * @since 1.2
532 */
533#define EINA_INARRAY_FOREACH(array, itr) \
534 for ((itr) = array->members; \
535 (itr) < (((typeof(*itr)*)array->members) + array->len); \
536 (itr)++)
537
538/**
539 * @def EINA_INARRAY_REVERSE_FOREACH
540 * @brief walks array linearly from tail to head
541 * @param array array object
542 * @param itr the iterator pointer
543 *
544 * @a itr must be a pointer with sizeof(itr*) == array->member_size.
545 *
546 * @warning This is fast as it does direct pointer access, but it will
547 * not check for @c NULL pointers or invalid array object!
548 *
549 * @warning Do not modify array as you walk it! If that is desired,
550 * then use eina_inarray_foreach_remove()
551 *
552 * @since 1.2
553 */
554#define EINA_INARRAY_REVERSE_FOREACH(array, itr) \
555 for ((itr) = ((((typeof(*(itr))*)array->members) + array->len) - 1); \
556 (((itr) >= (typeof(*(itr))*)array->members) \
557 && (array->members != NULL)); \
558 (itr)--)
559
560/**
561 * @}
562 */
563
564/**
565 * @}
566 */
567
568/**
569 * @}
570 */
571
572#endif /*EINA_INARRAY_H_*/