eina: add an example to demonstrate different eina insert and sort functions.
authorSrivardhan Hebbar <sri.hebbar@samsung.com>
Mon, 22 Dec 2014 14:08:41 +0000 (15:08 +0100)
committerCedric BAIL <cedric@osg.samsung.com>
Mon, 22 Dec 2014 14:09:59 +0000 (15:09 +0100)
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>
doc/eina_examples.dox
src/examples/eina/.gitignore
src/examples/eina/Makefile.am
src/examples/eina/Makefile.examples
src/examples/eina/eina_inarray_03.c [new file with mode: 0644]
src/lib/eina/eina_inarray.h

index 95f64fd4084d87c40f176897a3a5e6657b782d4e..33e83de4659640e0daf402c7c5eb246d98bdbda1 100644 (file)
@@ -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
index d744655a078a897275664f200343adfeb4647e05..b35df8fb529b63bc9b900ec8ff2cab8da960ef1c 100644 (file)
@@ -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
index 46fa9255350c20c2b50e90373fcbf21235d74ae1..227dba90b10eac96224e5627c0c29830b2afb4b0 100644 (file)
@@ -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
 
index 928dae41d794014fcc143085f07da46149d3b35e..e6614de053f23a089d1fd6999401e1ba06bfadf7 100644 (file)
@@ -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 \
diff --git a/src/examples/eina/eina_inarray_03.c b/src/examples/eina/eina_inarray_03.c
new file mode 100644 (file)
index 0000000..f0be8fd
--- /dev/null
@@ -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();
+}
index 34e73808416fc5565cb983e0f38c4c338584f4d7..2f956650497da9ab7fffa28468a6a70c1cd53648 100644 (file)
  * @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