Eina inline array insert, sort and search

This example creates an inline array of integers, and demonstrates the difference between eina_inarray_insert and eina_inarray_sort, and eina_inarray_search and eina_inarray_search_sort. Eina inline array usage.

We start with some variable declarations and eina initialization:

int
cmp(const void *a, const void *b)
{
return *(int*)a > *(int*)b;
}
int main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
{
Eina_Inarray *iarr;
int a, *b;
eina_init();
Eina_Inarray
struct _Eina_Inarray Eina_Inarray
Type for the inlined array.
Definition eina_inarray.h:212
eina_init
EINA_API int eina_init(void)
Initializes the Eina library.
Definition eina_main.c:291
EINA_UNUSED
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition eina_types.h:339

We then create the array much like we did on Eina inline array usage :

iarr = eina_inarray_new(sizeof(int), 0);
eina_inarray_new
EINA_API Eina_Inarray * eina_inarray_new(unsigned int member_size, unsigned int step)
Creates a new inline array.
Definition eina_inarray.c:342

We then add an element using eina_inarray_insert and print. Then remove that element and add it again using eina_inarray_insert_sorted and print. This shows the 2 different positions the element gets added. Then search for an element in the unsorted array using eina_inarray_search, then sorts the array and then searches the same element using eina_inarray_search_sorted.

a = 1;
eina_inarray_push(iarr, &a);
a = 9;
eina_inarray_push(iarr, &a);
a = 6;
eina_inarray_push(iarr, &a);
a = 4;
eina_inarray_push(iarr, &a);
a = 10;
eina_inarray_push(iarr, &a);
printf("Inline array of integers with %d elements:\n", eina_inarray_count(iarr));
EINA_INARRAY_FOREACH(iarr, b)
printf("int: %d(pointer: %p)\n", *b, b);
a = 8;
eina_inarray_insert(iarr, &a, cmp);
printf("Inserting %d to inline array using eina_inarray_insert.\n", a);
EINA_INARRAY_FOREACH(iarr, b)
printf("int: %d(pointer: %p)\n", *b, b);
eina_inarray_remove(iarr, &a);
printf("Removed %d from inline array using eina_inarray_remove.\n", a);
EINA_INARRAY_FOREACH(iarr, b)
printf("int: %d(pointer: %p)\n", *b, b);
eina_inarray_insert_sorted(iarr, &a, cmp);
printf("Inserting %d to inline array using eina_inarray_insert_sorted.\n",a);
EINA_INARRAY_FOREACH(iarr, b)
printf("int: %d(pointer: %p)\n", *b, b);
printf("Position of element %d in the inline array is %d\n", a, eina_inarray_search(iarr, &a, cmp));
eina_inarray_sort(iarr, cmp);
printf("Sorted inline array:\n");
EINA_INARRAY_FOREACH(iarr, b)
printf("int: %d(pointer: %p)\n", *b, b);
printf("Position of element %d in the sorted inline array is %d\n", a, eina_inarray_search_sorted(iarr, &a, cmp));
eina_inarray_free(iarr);
eina_shutdown();
}
eina_inarray_free
EINA_API void eina_inarray_free(Eina_Inarray *array)
Frees an array and its members.
Definition eina_inarray.c:355
eina_inarray_push
EINA_API int eina_inarray_push(Eina_Inarray *array, const void *data)
Copies the data as the last member of the array.
Definition eina_inarray.c:411
eina_inarray_sort
EINA_API void eina_inarray_sort(Eina_Inarray *array, Eina_Compare_Cb compare)
Applies a quick sort to the array.
Definition eina_inarray.c:657
eina_inarray_count
EINA_API unsigned int eina_inarray_count(const Eina_Inarray *array)
Counts the number of members in an array.
Definition eina_inarray.c:732
eina_inarray_remove
EINA_API int eina_inarray_remove(Eina_Inarray *array, const void *data)
Finds data and removes the matching member.
Definition eina_inarray.c:494
eina_inarray_insert_sorted
EINA_API int eina_inarray_insert_sorted(Eina_Inarray *array, const void *data, Eina_Compare_Cb compare)
Copies the data to the array at a position found by the comparison function.
Definition eina_inarray.c:475
eina_inarray_search_sorted
EINA_API int eina_inarray_search_sorted(const Eina_Inarray *array, const void *data, Eina_Compare_Cb compare)
Searches for member (binary search walk).
Definition eina_inarray.c:674
eina_inarray_search
EINA_API int eina_inarray_search(const Eina_Inarray *array, const void *data, Eina_Compare_Cb compare)
Searches for a member (linear walk).
Definition eina_inarray.c:665
EINA_INARRAY_FOREACH
#define EINA_INARRAY_FOREACH(array, itr)
Walks through an array linearly from head to tail.
Definition eina_inarray.h:724
eina_inarray_insert
EINA_API int eina_inarray_insert(Eina_Inarray *array, const void *data, Eina_Compare_Cb compare)
Copies the data to the array at a position found by the comparison function.
Definition eina_inarray.c:446
eina_shutdown
EINA_API int eina_shutdown(void)
Shuts down the Eina library.
Definition eina_main.c:379

The source for this example: eina_inarray_03.c