eina: fix Eina_Inarray API to at least match Eina_Array API.

NOTE: some of this function should be moved as inline, but that's to late for a change
I think. So we will fix that if needed.

Second point, I am not happy with is eina_inarray_insert and eina_inarray_insert_at. The
naming is really poor.


SVN revision: 70352
This commit is contained in:
Cedric BAIL 2012-04-20 08:30:59 +00:00
parent f9f6d76e9a
commit d80dae3035
8 changed files with 70 additions and 49 deletions

View File

@ -19,27 +19,27 @@ int main(int argc, char **argv)
iarr = eina_inarray_new(sizeof(char), 0); iarr = eina_inarray_new(sizeof(char), 0);
ch = 'a'; ch = 'a';
eina_inarray_append(iarr, &ch); eina_inarray_push(iarr, &ch);
ch = 'b'; ch = 'b';
eina_inarray_append(iarr, &ch); eina_inarray_push(iarr, &ch);
ch = 'c'; ch = 'c';
eina_inarray_append(iarr, &ch); eina_inarray_push(iarr, &ch);
ch = 'd'; ch = 'd';
eina_inarray_append(iarr, &ch); eina_inarray_push(iarr, &ch);
printf("Inline array of chars:\n"); printf("Inline array of chars:\n");
EINA_INARRAY_FOREACH(iarr, ch2) EINA_INARRAY_FOREACH(iarr, ch2)
printf("char: %c(pointer: %p)\n", *ch2, ch2); printf("char: %c(pointer: %p)\n", *ch2, ch2);
eina_inarray_flush(iarr); eina_inarray_flush(iarr);
eina_inarray_setup(iarr, sizeof(int), 4); eina_inarray_step_set(iarr, sizeof(Eina_Inarray), sizeof(int), 4);
a = 97; a = 97;
eina_inarray_append(iarr, &a); eina_inarray_push(iarr, &a);
a = 98; a = 98;
eina_inarray_append(iarr, &a); eina_inarray_push(iarr, &a);
a = 100; a = 100;
eina_inarray_append(iarr, &a); eina_inarray_push(iarr, &a);
a = 99; a = 99;
eina_inarray_insert_sorted(iarr, &a, cmp); eina_inarray_insert_sorted(iarr, &a, cmp);

View File

@ -21,7 +21,7 @@ main(int argc, char **argv)
for (i = 0; i < 20; i++){ for (i = 0; i < 20; i++){
str = &strings[i]; str = &strings[i];
eina_inarray_append(iarr, str); eina_inarray_push(iarr, str);
} }
printf("Inline array of strings:\n"); printf("Inline array of strings:\n");

View File

@ -50,14 +50,14 @@
* insertion function expect a memory address we have to put the value we want * insertion function expect a memory address we have to put the value we want
* to store in a variable(this should be no problem since in real world usage * to store in a variable(this should be no problem since in real world usage
* that's usually where the value will be anyways): * that's usually where the value will be anyways):
* @until append * @until push
* @note Because the inline array copies the value given to it we can later * @note Because the inline array copies the value given to it we can later
* change @c ch, which we do, without affecting the contents of the array. * change @c ch, which we do, without affecting the contents of the array.
* *
* So let's add some more elements: * So let's add some more elements:
* @until append * @until push
* @until append * @until push
* @until append * @until push
* *
* We will then iterate over our array and print every position of it. The thing * We will then iterate over our array and print every position of it. The thing
* to note here is not so much the values which will be the expected 'a', 'b', * to note here is not so much the values which will be the expected 'a', 'b',
@ -71,26 +71,26 @@
* @until _flush * @until _flush
* *
* And then to be able to store a different type on the same array we use the * And then to be able to store a different type on the same array we use the
* eina_array_setup() function, which is just like the eina_inarray_new() * eina_inarray_step_set() function, which is just like the eina_inarray_new()
* function except it receives already allocated memory. This time we're going * function except it receives already allocated memory. This time we're going
* to ask eina to use a step of size 4 because that's how many elements we'll be * to ask eina to use a step of size 4 because that's how many elements we'll be
* putting on the array: * putting on the array:
* @until _setup * @until _step_set
* @note Strictly speaking the reason to call eina_inarray_setup() is not * @note Strictly speaking the reason to call eina_inarray_setup() is not
* because we're storing different type, but rather because our types have * because we're storing different type, but rather because our types have
* different sizes. Eina inline arrays don't actually know anything about types, * different sizes. Eina inline arrays don't actually know anything about types,
* they only deal in blocks of memory of a given size. * they only deal in blocks of memory of a given size.
* @note Since eina_array_setup() receives already allocated memory you can(and * @note Since eina_inarray_step_set() receives already allocated memory you can(and
* it is in fact good practice) use inline arrays not declared as pointers: * it is in fact good practice) use inline arrays not declared as pointers:
* @code * @code
* Eina_Inarray arr; * Eina_Inarray arr;
* eina_inarray_setup(&arr, sizeof(int), 4); * eina_inarray_step_set(&arr, sizeof(arr), sizeof(int), 4);
* @endcode * @endcode
* *
* And now to add our integer values to the array: * And now to add our integer values to the array:
* @until append * @until push
* @until append * @until push
* @until append * @until push
* *
* Just to change things up a bit we've left out the 99 value, but will still * Just to change things up a bit we've left out the 99 value, but will still
* add it in such a way to keep the array ordered. There are many ways to do * add it in such a way to keep the array ordered. There are many ways to do
@ -193,13 +193,16 @@ typedef struct _Eina_Inarray Eina_Inarray;
/** /**
* Inline array structure, use #Eina_Inarray typedef instead. * Inline array structure, use #Eina_Inarray typedef instead.
* *
* Do not modify these fields directly, use eina_inarray_setup() or * Do not modify these fields directly, use eina_inarray_step_set() or
* eina_inarray_new() instead. * eina_inarray_new() instead.
* *
* @since 1.2 * @since 1.2
*/ */
struct _Eina_Inarray struct _Eina_Inarray
{ {
#define EINA_ARRAY_VERSION 1
int version; /**< Should match EINA_ARRAY_VERSION used when compiled your apps, provided for ABI compatibility */
unsigned int member_size; /**< byte size of each entry in members */ unsigned int member_size; /**< byte size of each entry in members */
unsigned int len; /**< number of elements used in members */ unsigned int len; /**< number of elements used in members */
unsigned int max; /**< number of elements allocated in members */ unsigned int max; /**< number of elements allocated in members */
@ -258,9 +261,10 @@ EAPI void eina_inarray_free(Eina_Inarray *array) EINA_ARG_NONNULL(1);
* *
* @since 1.2 * @since 1.2
*/ */
EAPI void eina_inarray_setup(Eina_Inarray *array, EAPI void eina_inarray_step_set(Eina_Inarray *array,
unsigned int member_size, unsigned int sizeof_eina_inarray,
unsigned int step) EINA_ARG_NONNULL(1); unsigned int member_size,
unsigned int step) EINA_ARG_NONNULL(1);
/** /**
* @brief Remove every member from array. * @brief Remove every member from array.
@ -284,8 +288,8 @@ EAPI void eina_inarray_flush(Eina_Inarray *array) EINA_ARG_NONNULL(1);
* *
* @since 1.2 * @since 1.2
*/ */
EAPI int eina_inarray_append(Eina_Inarray *array, EAPI int eina_inarray_push(Eina_Inarray *array,
const void *data) EINA_ARG_NONNULL(1, 2); const void *data) EINA_ARG_NONNULL(1, 2);
/** /**
* @brief Copy the data to array at position found by comparison function * @brief Copy the data to array at position found by comparison function
@ -304,7 +308,7 @@ EAPI int eina_inarray_append(Eina_Inarray *array,
* *
* @see eina_inarray_insert_sorted() * @see eina_inarray_insert_sorted()
* @see eina_inarray_insert_at() * @see eina_inarray_insert_at()
* @see eina_inarray_append() * @see eina_inarray_push()
* *
* @since 1.2 * @since 1.2
*/ */
@ -359,11 +363,13 @@ EAPI int eina_inarray_remove(Eina_Inarray *array,
/** /**
* @brief Removes the last member of the array * @brief Removes the last member of the array
* @param array array object * @param array array object
* @return the index of the removed member or -1 on errors. * @return the data poped out of the array.
*
* Note: The data could be considered valid only until any other operation touch the Inarray.
* *
* @since 1.2 * @since 1.2
*/ */
EAPI int eina_inarray_pop(Eina_Inarray *array) EINA_ARG_NONNULL(1); EAPI void *eina_inarray_pop(Eina_Inarray *array) EINA_ARG_NONNULL(1);
/** /**
* @brief Get the member at given position * @brief Get the member at given position
@ -373,7 +379,7 @@ EAPI int eina_inarray_pop(Eina_Inarray *array) EINA_ARG_NONNULL(1);
* *
* Gets the member given its position in the array. It is a pointer to * Gets the member given its position in the array. It is a pointer to
* its current memory, then it can be invalidated with functions that * its current memory, then it can be invalidated with functions that
* changes the array such as eina_inarray_append(), * changes the array such as eina_inarray_push(),
* eina_inarray_insert_at() or eina_inarray_remove_at() or variants. * eina_inarray_insert_at() or eina_inarray_remove_at() or variants.
* *
* See also eina_inarray_lookup() and eina_inarray_lookup_sorted(). * See also eina_inarray_lookup() and eina_inarray_lookup_sorted().

View File

@ -114,6 +114,7 @@ static void
_eina_inarray_setup(Eina_Inarray *array, unsigned int member_size, unsigned int step) _eina_inarray_setup(Eina_Inarray *array, unsigned int member_size, unsigned int step)
{ {
EINA_MAGIC_SET(array, EINA_MAGIC_INARRAY); EINA_MAGIC_SET(array, EINA_MAGIC_INARRAY);
array->version = EINA_ARRAY_VERSION;
array->member_size = member_size; array->member_size = member_size;
array->len = 0; array->len = 0;
array->max = 0; array->max = 0;
@ -127,7 +128,7 @@ _eina_inarray_resize(Eina_Inarray *array, unsigned int new_size)
unsigned int new_max; unsigned int new_max;
void *tmp; void *tmp;
if (new_size < array->max) if (new_size < array->max) /* don't change this behaviour as eina_inarray_pop rely on it */
return EINA_TRUE; return EINA_TRUE;
if (new_size % array->step == 0) if (new_size % array->step == 0)
@ -360,10 +361,24 @@ eina_inarray_free(Eina_Inarray *array)
} }
EAPI void EAPI void
eina_inarray_setup(Eina_Inarray *array, unsigned int member_size, unsigned int step) eina_inarray_step_set(Eina_Inarray *array,
unsigned int sizeof_eina_inarray,
unsigned int member_size,
unsigned int step)
{ {
EINA_SAFETY_ON_NULL_RETURN(array); EINA_SAFETY_ON_NULL_RETURN(array);
EINA_SAFETY_ON_TRUE_RETURN(member_size == 0); EINA_SAFETY_ON_TRUE_RETURN(member_size == 0);
if (sizeof (Eina_Inarray) != sizeof_eina_inarray)
{
ERR("Unknow Eina_Inarray size ! Got %i, expected %i\n",
sizeof_eina_inarray,
(int) sizeof (Eina_Inarray));
/* Force memory to zero to provide a small layer of security */
memset(array, 0, sizeof_eina_inarray);
return ;
}
_eina_inarray_setup(array, member_size, step); _eina_inarray_setup(array, member_size, step);
} }
@ -378,7 +393,7 @@ eina_inarray_flush(Eina_Inarray *array)
} }
EAPI int EAPI int
eina_inarray_append(Eina_Inarray *array, const void *data) eina_inarray_push(Eina_Inarray *array, const void *data)
{ {
void *p; void *p;
@ -421,7 +436,7 @@ eina_inarray_insert(Eina_Inarray *array, const void *data, Eina_Compare_Cb compa
return -1; return -1;
return position; return position;
} }
return eina_inarray_append(array, data); return eina_inarray_push(array, data);
} }
EAPI int EAPI int
@ -481,15 +496,15 @@ found:
return position; return position;
} }
EAPI int EAPI void *
eina_inarray_pop(Eina_Inarray *array) eina_inarray_pop(Eina_Inarray *array)
{ {
EINA_MAGIC_CHECK_INARRAY(array, -1); EINA_MAGIC_CHECK_INARRAY(array, NULL);
EINA_SAFETY_ON_TRUE_RETURN_VAL(array->len == 0, -1); EINA_SAFETY_ON_TRUE_RETURN_VAL(array->len == 0, NULL);
if (!_eina_inarray_resize(array, array->len - 1)) if (!_eina_inarray_resize(array, array->len - 1))
return -1; return NULL;
array->len--; array->len--;
return array->len + 1; return _eina_inarray_get(array, array->len + 1);
} }
EAPI void * EAPI void *

View File

@ -2666,7 +2666,7 @@ _eina_value_type_array_pset(const Eina_Value_Type *type, void *mem, const void *
if (tmem->array) if (tmem->array)
{ {
_eina_value_type_array_flush_elements(tmem); _eina_value_type_array_flush_elements(tmem);
eina_inarray_setup(tmem->array, desc->subtype->value_size, desc->step); eina_inarray_step_set(tmem->array, sizeof (Eina_Inarray), desc->subtype->value_size, desc->step);
} }
else else
{ {

View File

@ -46,7 +46,7 @@ START_TEST(eina_inarray_test_simple)
for (i = 0; i < test_members; i++) for (i = 0; i < test_members; i++)
{ {
pos = eina_inarray_append(array, &i); pos = eina_inarray_push(array, &i);
fail_unless(pos == i); fail_unless(pos == i);
} }
fail_unless(eina_inarray_count(array) == (unsigned)test_members); fail_unless(eina_inarray_count(array) == (unsigned)test_members);
@ -251,7 +251,7 @@ START_TEST(eina_inarray_test_sort)
for (i = 0; i < numbers_count; i++) for (i = 0; i < numbers_count; i++)
{ {
short val = rand_numbers[i]; short val = rand_numbers[i];
eina_inarray_append(array, &val); eina_inarray_push(array, &val);
} }
eina_inarray_sort(array, short_cmp); eina_inarray_sort(array, short_cmp);
fail_unless(check_short_sorted(array)); fail_unless(check_short_sorted(array));
@ -273,7 +273,7 @@ START_TEST(eina_inarray_test_reverse)
for (i = 0; i < numbers_count; i++) for (i = 0; i < numbers_count; i++)
{ {
short val = i; short val = i;
eina_inarray_append(array, &val); eina_inarray_push(array, &val);
} }
eina_inarray_reverse(array); eina_inarray_reverse(array);
@ -326,7 +326,7 @@ START_TEST(eina_inarray_test_itr)
for (i = 0; i < numbers_count; i++) for (i = 0; i < numbers_count; i++)
{ {
short val = i; short val = i;
eina_inarray_append(array, &val); eina_inarray_push(array, &val);
} }
i = 0; i = 0;
EINA_INARRAY_FOREACH(array, member) EINA_INARRAY_FOREACH(array, member)

View File

@ -1133,11 +1133,11 @@ START_TEST(eina_value_test_array)
inarray = eina_inarray_new(sizeof(char), 0); inarray = eina_inarray_new(sizeof(char), 0);
fail_unless(inarray != NULL); fail_unless(inarray != NULL);
c = 11; c = 11;
fail_unless(eina_inarray_append(inarray, &c) >= 0); fail_unless(eina_inarray_push(inarray, &c) >= 0);
c = 21; c = 21;
fail_unless(eina_inarray_append(inarray, &c) >= 0); fail_unless(eina_inarray_push(inarray, &c) >= 0);
c = 31; c = 31;
fail_unless(eina_inarray_append(inarray, &c) >= 0); fail_unless(eina_inarray_push(inarray, &c) >= 0);
desc.subtype = EINA_VALUE_TYPE_CHAR; desc.subtype = EINA_VALUE_TYPE_CHAR;
desc.step = 0; desc.step = 0;
desc.array = inarray; desc.array = inarray;

View File

@ -119,7 +119,7 @@ _output_setup(int w,
} }
re->tb = evas_common_tilebuf_new(w, h); re->tb = evas_common_tilebuf_new(w, h);
evas_common_tilebuf_set_tile_size(re->tb, TILESIZE, TILESIZE); evas_common_tilebuf_set_tile_size(re->tb, TILESIZE, TILESIZE);
eina_inarray_setup(&re->previous_rects, sizeof (Eina_Rectangle), 8); eina_inarray_step_set(&re->previous_rects, sizeof (Eina_Inarray), sizeof (Eina_Rectangle), 8);
return re; return re;
} }
@ -312,7 +312,7 @@ eng_output_redraws_next_update_get(void *data, int *x, int *y, int *w, int *h, i
Eina_Rectangle local; Eina_Rectangle local;
EINA_RECTANGLE_SET(&local, rect->x, rect->y, rect->w, rect->h); EINA_RECTANGLE_SET(&local, rect->x, rect->y, rect->w, rect->h);
eina_inarray_append(&re->previous_rects, &local); eina_inarray_push(&re->previous_rects, &local);
} }
/* and regenerate the damage list by tacking into account the damage over two frames */ /* and regenerate the damage list by tacking into account the damage over two frames */