array: Add an array_get() function
This commit is contained in:
@@ -314,7 +314,7 @@ void array_insert(array_t *array, int idx, void *data)
|
||||
}
|
||||
}
|
||||
|
||||
bool array_remove(array_t *array, int idx, void *data)
|
||||
bool array_get(array_t *array, int idx, void *data)
|
||||
{
|
||||
if (!array)
|
||||
{
|
||||
@@ -337,12 +337,25 @@ bool array_remove(array_t *array, int idx, void *data)
|
||||
memcpy(data, array->data + get_size(array, array->head + idx),
|
||||
get_size(array, 1));
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
bool array_remove(array_t *array, int idx, void *data)
|
||||
{
|
||||
if (!array_get(array, idx, data))
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
if (idx > array_count(array) / 2)
|
||||
{
|
||||
remove_tail(array, idx);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (idx < 0)
|
||||
{
|
||||
idx = array_count(array) - 1;
|
||||
}
|
||||
remove_head(array, idx);
|
||||
}
|
||||
if (array->head + array->tail > ARRAY_MAX_UNUSED)
|
||||
|
||||
@@ -139,6 +139,18 @@ void array_insert_create(array_t **array, int idx, void *ptr);
|
||||
*/
|
||||
void array_insert_enumerator(array_t *array, int idx, enumerator_t *enumerator);
|
||||
|
||||
/**
|
||||
* Get an element from the array.
|
||||
*
|
||||
* If data is given, the element is copied to that position.
|
||||
*
|
||||
* @param array array to get element from, or NULL
|
||||
* @param idx index of the item to get
|
||||
* @param data data to copy element to, or NULL
|
||||
* @return TRUE if idx valid and item returned
|
||||
*/
|
||||
bool array_get(array_t *array, int idx, void *data);
|
||||
|
||||
/**
|
||||
* Remove an element from the array.
|
||||
*
|
||||
|
||||
@@ -35,6 +35,14 @@ START_TEST(test_append_ptr)
|
||||
|
||||
/* 3, 4 */
|
||||
|
||||
ck_assert(array_get(array, ARRAY_HEAD, &x));
|
||||
ck_assert_int_eq(x, 3);
|
||||
ck_assert(array_get(array, 1, &x));
|
||||
ck_assert_int_eq(x, 4);
|
||||
ck_assert(array_get(array, ARRAY_TAIL, &x));
|
||||
ck_assert_int_eq(x, 4);
|
||||
ck_assert(!array_get(array, 3, &x));
|
||||
|
||||
array_insert(array, ARRAY_HEAD, (void*)(uintptr_t)1);
|
||||
array_insert(array, 1, (void*)(uintptr_t)2);
|
||||
ck_assert_int_eq(array_count(array), 4);
|
||||
@@ -108,6 +116,14 @@ START_TEST(test_append_obj)
|
||||
|
||||
/* 3, 4 */
|
||||
|
||||
ck_assert(array_get(array, ARRAY_HEAD, &x));
|
||||
ck_assert_int_eq(x, 3);
|
||||
ck_assert(array_get(array, 1, &x));
|
||||
ck_assert_int_eq(x, 4);
|
||||
ck_assert(array_get(array, ARRAY_TAIL, &x));
|
||||
ck_assert_int_eq(x, 4);
|
||||
ck_assert(!array_get(array, 3, &x));
|
||||
|
||||
array_insert(array, ARRAY_HEAD, &y[1]);
|
||||
array_insert(array, 1, &y[2]);
|
||||
ck_assert_int_eq(array_count(array), 4);
|
||||
@@ -336,11 +352,11 @@ Suite *array_suite_create()
|
||||
|
||||
s = suite_create("array");
|
||||
|
||||
tc = tcase_create("add/remove ptr");
|
||||
tc = tcase_create("add/get/remove ptr");
|
||||
tcase_add_test(tc, test_append_ptr);
|
||||
suite_add_tcase(s, tc);
|
||||
|
||||
tc = tcase_create("add/remove obj");
|
||||
tc = tcase_create("add/get/remove obj");
|
||||
tcase_add_test(tc, test_append_obj);
|
||||
suite_add_tcase(s, tc);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user