forked from enlightenment/efl
eina: add an example to demonstrate different eina insert and sort functions.
Summary: While going through eina for understanding, wrote a program to understand he differences between different eina inarray functions. Thought, this might be useful for others too, so adding the same. Signed-off-by: Srivardhan Hebbar <sri.hebbar@samsung.com> Reviewers: devilhorns Subscribers: cedric Differential Revision: https://phab.enlightenment.org/D1803 Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
This commit is contained in:
parent
62a6db2dac
commit
701f7b0737
|
@ -17,6 +17,7 @@
|
|||
* @li @ref eina_hash_08.c
|
||||
* @li @ref eina_inarray_01.c
|
||||
* @li @ref eina_inarray_02.c
|
||||
* @li @ref eina_inarray_03.c
|
||||
* @li @ref eina_inlist_01.c
|
||||
* @li @ref eina_inlist_02.c
|
||||
* @li @ref eina_inlist_03.c
|
||||
|
@ -73,6 +74,7 @@
|
|||
* @example eina_hash_08.c
|
||||
* @example eina_inarray_01.c
|
||||
* @example eina_inarray_02.c
|
||||
* @example eina_inarray_03.c
|
||||
* @example eina_inlist_01.c
|
||||
* @example eina_inlist_02.c
|
||||
* @example eina_inlist_03.c
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
/eina_hash_08
|
||||
/eina_inarray_01
|
||||
/eina_inarray_02
|
||||
/eina_inarray_03
|
||||
/eina_inlist_01
|
||||
/eina_inlist_02
|
||||
/eina_inlist_03
|
||||
|
|
|
@ -46,6 +46,7 @@ eina_value_02.c \
|
|||
eina_value_03.c \
|
||||
eina_inarray_01.c \
|
||||
eina_inarray_02.c \
|
||||
eina_inarray_03.c \
|
||||
eina_magic_01.c \
|
||||
eina_xattr_01.c \
|
||||
eina_xattr_02.c
|
||||
|
@ -90,6 +91,7 @@ eina_value_02 \
|
|||
eina_value_03 \
|
||||
eina_inarray_01 \
|
||||
eina_inarray_02 \
|
||||
eina_inarray_03 \
|
||||
eina_xattr_01 \
|
||||
eina_xattr_02
|
||||
|
||||
|
|
|
@ -17,6 +17,7 @@ EXAMPLES= eina_accessor_01 \
|
|||
eina_hash_08 \
|
||||
eina_inarray_01 \
|
||||
eina_inarray_02 \
|
||||
eina_inarray_03 \
|
||||
eina_inlist_01 \
|
||||
eina_inlist_02 \
|
||||
eina_inlist_03 \
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
//Compile with:
|
||||
//gcc -g eina_inarray_03.c -o eina_inarray_03 `pkg-config --cflags --libs eina`
|
||||
|
||||
#include <Eina.h>
|
||||
|
||||
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;
|
||||
char ch, *ch2;
|
||||
int a, *b, pos;
|
||||
|
||||
eina_init();
|
||||
iarr = eina_inarray_new(sizeof(int), 0);
|
||||
|
||||
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();
|
||||
}
|
|
@ -148,6 +148,38 @@
|
|||
* @example eina_inarray_02.c
|
||||
*/
|
||||
|
||||
/**
|
||||
* @page eina_inarray_example_03 Eina inline array insert, sort and search
|
||||
* @dontinclude eina_inarray_03.c
|
||||
*
|
||||
* 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.
|
||||
* @ref eina_inarray_example_01.
|
||||
*
|
||||
* We start with some variable declarations and eina initialization:
|
||||
* @skip int
|
||||
* @until eina_init
|
||||
*
|
||||
* We then create the array much like we did on @ref eina_inarray_example_01 :
|
||||
* @until inarray_new
|
||||
*
|
||||
* We then add element using eina_inarray_insert and print. Then remove that
|
||||
* element and add again using eina_inarray_insert_sorted and prints. This
|
||||
* shows the 2 different positions the elment gets added. Then searches 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.
|
||||
* @until }
|
||||
*
|
||||
* The source for this example: @ref eina_inarray_03_c
|
||||
*/
|
||||
|
||||
/**
|
||||
* @page eina_inarray_03_c eina_inarray_03.c
|
||||
* @include eina_inarray_03.c
|
||||
* @example eina_inarray_03.c
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup Eina_Inline_Array_Group Inline Array
|
||||
* @ingroup Eina_Containers_Group
|
||||
|
|
Loading…
Reference in New Issue