* add documentation. The tutorial needs to be written, though

* rename eina_arra_count() to eina_array_count_get() and
   eina_arra_get() to eina_array_data_get()


SVN revision: 36003
This commit is contained in:
Vincent Torri 2008-09-15 19:17:15 +00:00
parent e3e2afca18
commit 78c9527052
7 changed files with 107 additions and 43 deletions

View File

@ -40,7 +40,7 @@ typedef struct _Eina_Array Eina_Array;
/**
* @typedef Eina_Array_Iterator
* Type for an iterator oon arrays.
* Type for an iterator on arrays, used with #EINA_ARRAY_ITER_NEXT.
*/
typedef void **Eina_Array_Iterator;
@ -68,15 +68,47 @@ EAPI Eina_Bool eina_array_remove (Eina_Array *array, Eina_Bool (*keep)(void
static inline Eina_Bool eina_array_push (Eina_Array *array, const void *data);
static inline void *eina_array_pop (Eina_Array *array);
static inline void *eina_array_get (const Eina_Array *array, unsigned int index);
static inline unsigned int eina_array_count (const Eina_Array *array);
static inline void *eina_array_data_get (const Eina_Array *array, unsigned int index);
static inline unsigned int eina_array_count_get (const Eina_Array *array);
EAPI Eina_Iterator *eina_array_iterator_new (const Eina_Array *array);
EAPI Eina_Accessor *eina_array_accessor_new (const Eina_Array *array);
/**
* @def EINA_ARRAY_ITER_NEXT
* @brief Macro to iterate over an array easily
*
* @param array The array to iterate over.
* @param index The integer number that is increased while itareting.
* @param item The data
* @param iterator The iterator
*
* This macro allow the iteration over @p array in an easy way. It
* iterates from the first element to the last one. @pindex is an
* integer that increase from 0 to the number of elements. @p item is
* the data of each element of @p array, so it is a pointer to a type
* chosen by the user. @p iterator is of type #Eina_Array_Iterator.
*
* This macro can be used for freeing the data of an array, like in
* the following example:
*
* @code
* Eina_Array *array;
* char *item;
* Eina_Array_Iterator *iterator;
* unsigned int i;
*
* // array is already filled,
* // its elements are just duplicated strings,
* // EINA_ARRAY_ITER_NEXT will be used to free those strings
*
* EINA_ARRAY_ITER_NEXT(array, i, item, iterator)
* free(item);
* @endcode
*/
#define EINA_ARRAY_ITER_NEXT(array, index, item, iterator) \
for (index = 0, iterator = (array)->data, item = iterator != NULL ? *(iterator) : NULL; \
index < eina_array_count(array); \
index < eina_array_count_get(array); \
++(index), item = *(++(iterator)))
#include "eina_inline_array.x"

View File

@ -96,7 +96,7 @@ eina_array_pop(Eina_Array *array)
* index. If it is @c NULL or invalid, the program may crash.
*/
static inline void *
eina_array_get(const Eina_Array *array, unsigned int index)
eina_array_data_get(const Eina_Array *array, unsigned int index)
{
return array->data[index];
}
@ -112,7 +112,7 @@ eina_array_get(const Eina_Array *array, unsigned int index)
* @c NULL or invalid, the program may crash.
*/
static inline unsigned int
eina_array_count(const Eina_Array *array)
eina_array_count_get(const Eina_Array *array)
{
return array->count;
}

View File

@ -19,6 +19,14 @@
* if not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page tutorial_array_page Array Tutorial
*
* to be written...
*
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
@ -60,10 +68,10 @@ struct _Eina_Accessor_Array
static Eina_Bool
eina_array_iterator_next(Eina_Iterator_Array *it, void **data)
{
if (!(it->index < eina_array_count(it->array)))
if (!(it->index < eina_array_count_get(it->array)))
return EINA_FALSE;
if (data)
*data = eina_array_get(it->array, it->index);
*data = eina_array_data_get(it->array, it->index);
it->index++;
return EINA_TRUE;
}
@ -83,10 +91,10 @@ eina_array_iterator_free(Eina_Iterator_Array *it)
static Eina_Bool
eina_array_accessor_get_at(Eina_Accessor_Array *it, unsigned int index, void **data)
{
if (!(index < eina_array_count(it->array)))
if (!(index < eina_array_count_get(it->array)))
return EINA_FALSE;
if (data)
*data = eina_array_get(it->array, index);
*data = eina_array_data_get(it->array, index);
return EINA_TRUE;
}
@ -140,6 +148,30 @@ eina_array_grow(Eina_Array *array)
*
* @brief These functions provide array management.
*
* To use the array data type, eina_array_init() must be called before
* any other array functions. When no more array function is used,
* eina_array_shutdown() must be called to free all the resources.
*
* An array must be created with eina_array_new(). It allocated all
* the necessary data for an array. When not needed anymore, an array
* is freed with eina_array_free(). This function does not free any
* allocated memory used to store the data of each element. For that,
* just iterate over the array to free them. A convenient way to do
* that is by using #EINA_ARRAY_ITER_NEXT. An example of code is given
* in the description of this macro.
*
* @warning All the other functions do not check if the used array is
* valid or not. It's up to the user to be sure of that. It is
* designed like that for performance reasons.
*
* The usual features of an array are classic ones: to append an
* element, use eina_array_push() and to remove the last element, use
* eina_array_pop(). To retrieve the element at a given positin, use
* eina_array_data_get(). The number of elements can be retrieved with
* eina_array_count_get().
*
* For more information, you can look at the @ref tutorial_array_page.
*
* @{
*/
@ -306,7 +338,7 @@ eina_array_remove(Eina_Array *array, Eina_Bool (*keep)(void *data, void *gdata),
for (i = 0; i < array->count; ++i)
{
data = eina_array_get(array, i);
data = eina_array_data_get(array, i);
if (keep(data, gdata) == EINA_FALSE) break;
}
@ -314,7 +346,7 @@ eina_array_remove(Eina_Array *array, Eina_Bool (*keep)(void *data, void *gdata),
if (i < array->count) ++i;
for (; i < array->count; ++i)
{
data = eina_array_get(array, i);
data = eina_array_data_get(array, i);
if (keep(data, gdata) == EINA_TRUE) break;
}
@ -351,7 +383,7 @@ eina_array_remove(Eina_Array *array, Eina_Bool (*keep)(void *data, void *gdata),
for (; i < array->count; ++i)
{
data = eina_array_get(array, i);
data = eina_array_data_get(array, i);
if (keep(data, gdata))
{
@ -390,7 +422,7 @@ eina_array_iterator_new(const Eina_Array *array)
Eina_Iterator_Array *it;
if (!array) return NULL;
if (eina_array_count(array) <= 0) return NULL;
if (eina_array_count_get(array) <= 0) return NULL;
eina_error_set(0);
it = calloc(1, sizeof (Eina_Iterator_Array));

View File

@ -74,8 +74,8 @@ _eina_rbtree_iterator_list_new(const Eina_Rbtree *tree)
static Eina_Rbtree *
_eina_rbtree_iterator_get_content(Eina_Iterator_Rbtree *it)
{
if (eina_array_count(it->stack) <= 0) return NULL;
return eina_array_get(it->stack, 0);
if (eina_array_count_get(it->stack) <= 0) return NULL;
return eina_array_data_get(it->stack, 0);
}
static void
@ -99,9 +99,9 @@ _eina_rbtree_iterator_next(Eina_Iterator_Rbtree *it, void **data)
Eina_Iterator_Rbtree_List *new;
Eina_Rbtree *tree;
if (eina_array_count(it->stack) <= 0) return EINA_FALSE;
if (eina_array_count_get(it->stack) <= 0) return EINA_FALSE;
last = eina_array_get(it->stack, eina_array_count(it->stack) - 1);
last = eina_array_data_get(it->stack, eina_array_count_get(it->stack) - 1);
tree = last->tree;
if (last->tree == NULL || last->up == EINA_TRUE)
@ -115,9 +115,9 @@ _eina_rbtree_iterator_next(Eina_Iterator_Rbtree *it, void **data)
{
free(last);
if (eina_array_count(it->stack) > 0)
if (eina_array_count_get(it->stack) > 0)
{
last = eina_array_get(it->stack, eina_array_count(it->stack) - 1);
last = eina_array_data_get(it->stack, eina_array_count_get(it->stack) - 1);
last->up = EINA_TRUE;
}

View File

@ -44,8 +44,8 @@ START_TEST(eina_array_simple)
eina_array_push(ea, tmp);
}
fail_if(eina_array_get(ea, 10) == NULL);
fail_if(atoi(eina_array_get(ea, 10)) != 10);
fail_if(eina_array_data_get(ea, 10) == NULL);
fail_if(atoi(eina_array_data_get(ea, 10)) != 10);
tmp = eina_array_pop(ea);
fail_if(tmp == NULL);
fail_if(atoi(tmp) != 200);
@ -87,8 +87,8 @@ START_TEST(eina_array_static)
eina_array_push(&sea, tmp);
}
fail_if(eina_array_get(&sea, 10) == NULL);
fail_if(atoi(eina_array_get(&sea, 10)) != 10);
fail_if(eina_array_data_get(&sea, 10) == NULL);
fail_if(atoi(eina_array_data_get(&sea, 10)) != 10);
EINA_ARRAY_ITER_NEXT(&sea, i, tmp, it)
{
@ -141,27 +141,27 @@ START_TEST(eina_array_remove_stuff)
// Remove the first 10 items
for (i = 0; i < 10; ++i)
{
tmp = eina_array_get(ea, i);
tmp = eina_array_data_get(ea, i);
fail_if(!tmp);
*tmp = 0;
}
fail_if(eina_array_remove(ea, keep_int, NULL) != EINA_TRUE);
fail_if(eina_array_count(ea) != 990);
fail_if(eina_array_count_get(ea) != 990);
EINA_ARRAY_ITER_NEXT(ea, i, tmp, it)
fail_if(*tmp == 0);
// Remove the last items
for (i = 980; i < 990; ++i)
{
tmp = eina_array_get(ea, i);
tmp = eina_array_data_get(ea, i);
fail_if(!tmp);
*tmp = 0;
}
eina_array_remove(ea, keep_int, NULL);
// Remove all items
fail_if(eina_array_count(ea) != 980);
fail_if(eina_array_count_get(ea) != 980);
EINA_ARRAY_ITER_NEXT(ea, i, tmp, it)
{
fail_if(*tmp == 0);
@ -170,7 +170,7 @@ START_TEST(eina_array_remove_stuff)
eina_array_remove(ea, keep_int, NULL);
fail_if(eina_array_count(ea) != 0);
fail_if(eina_array_count_get(ea) != 0);
eina_array_free(ea);

View File

@ -31,25 +31,25 @@ START_TEST(eina_file_split_simple)
ea = eina_file_split(strdup("/this/is/a/small/test"));
fail_if(!ea);
fail_if(eina_array_count(ea) != 5);
fail_if(strcmp(eina_array_get(ea, 0), "this"));
fail_if(strcmp(eina_array_get(ea, 1), "is"));
fail_if(strcmp(eina_array_get(ea, 2), "a"));
fail_if(strcmp(eina_array_get(ea, 3), "small"));
fail_if(strcmp(eina_array_get(ea, 4), "test"));
fail_if(eina_array_count_get(ea) != 5);
fail_if(strcmp(eina_array_data_get(ea, 0), "this"));
fail_if(strcmp(eina_array_data_get(ea, 1), "is"));
fail_if(strcmp(eina_array_data_get(ea, 2), "a"));
fail_if(strcmp(eina_array_data_get(ea, 3), "small"));
fail_if(strcmp(eina_array_data_get(ea, 4), "test"));
eina_array_free(ea);
ea = eina_file_split(strdup("this//is///a /more/complex///case///"));
fail_if(!ea);
fail_if(eina_array_count(ea) != 6);
fail_if(strcmp(eina_array_get(ea, 0), "this"));
fail_if(strcmp(eina_array_get(ea, 1), "is"));
fail_if(strcmp(eina_array_get(ea, 2), "a "));
fail_if(strcmp(eina_array_get(ea, 3), "more"));
fail_if(strcmp(eina_array_get(ea, 4), "complex"));
fail_if(strcmp(eina_array_get(ea, 5), "case"));
fail_if(eina_array_count_get(ea) != 6);
fail_if(strcmp(eina_array_data_get(ea, 0), "this"));
fail_if(strcmp(eina_array_data_get(ea, 1), "is"));
fail_if(strcmp(eina_array_data_get(ea, 2), "a "));
fail_if(strcmp(eina_array_data_get(ea, 3), "more"));
fail_if(strcmp(eina_array_data_get(ea, 4), "complex"));
fail_if(strcmp(eina_array_data_get(ea, 5), "case"));
eina_array_free(ea);

View File

@ -134,7 +134,7 @@ START_TEST(eina_stringshare_collision)
}
for (i = 0; i < 200; ++i)
eina_stringshare_del(eina_array_get(ea, i));
eina_stringshare_del(eina_array_data_get(ea, i));
eina_stringshare_shutdown();