eina: fix eina_inarray_search to do linear search
authorVivek Ellur <vivek.ellur@samsung.com>
Thu, 14 May 2015 17:37:06 +0000 (19:37 +0200)
committerCedric BAIL <cedric@osg.samsung.com>
Thu, 14 May 2015 17:37:08 +0000 (19:37 +0200)
Summary:
Currently eina_inarray_search was using binary search to search elements
which would not work on unsorted array so modified it to work as linear search.
There is already a function eina_inarray_search_sorted to work on sorted array.

Signed-off-by: Vivek Ellur <vivek.ellur@samsung.com>
Reviewers: cedric

Subscribers: cedric

Differential Revision: https://phab.enlightenment.org/D2501

Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
src/lib/eina/eina_inarray.c

index 2d52e6f..a216f02 100644 (file)
@@ -150,12 +150,23 @@ _eina_inarray_get(const Eina_Inarray *array, unsigned int position)
 static int
 _eina_inarray_search(const Eina_Inarray *array, const void *data, Eina_Compare_Cb compare)
 {
-   const unsigned char *start, *found;
-   start = array->members;
-   found = bsearch(data, start, array->len, array->member_size, compare);
-   if (!found)
+   unsigned int found, pos=-1;
+   void *p;
+
+   if (array->len == 0)
+     return -1;
+
+   for (pos = 0; pos < array->len; ++pos)
+     {
+        p = _eina_inarray_get(array, pos);
+        found = compare(data, p);
+        if (found == 0)
+          break;
+     }
+   if (pos < array->len)
+     return pos;
+   else
      return -1;
-   return (found - start) / array->member_size;
 }
 
 static unsigned int