}
}
-bool array_remove(array_t *array, int idx, void *data)
+bool array_get(array_t *array, int idx, void *data)
{
if (!array)
{
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)
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.
*
* If data is given, the element is copied to that position.
/* 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);
/* 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);
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);