/** * Removes and outputs the last returned element pair by <code>array_zip_iter_next() * </code> without invalidating the iterator. * * @param[in] iter iterator on which this operation is being performed * @param[out] out1 output of the removed element from the first array * @param[out] out2 output of the removed element from the second array * * @return CC_OK if the element was successfully removed, CC_ERR_OUT_OF_RANGE if the * state of the iterator is invalid, or CC_ERR_VALUE_NOT_FOUND if the element was * already removed. */ enum cc_stat array_zip_iter_remove(ArrayZipIter *iter, void **out1, void **out2) { if ((iter->index - 1) >= iter->ar1->size || (iter->index - 1) >= iter->ar2->size) return CC_ERR_OUT_OF_RANGE; if (!iter->last_removed) { array_remove_at(iter->ar1, iter->index - 1, out1); array_remove_at(iter->ar2, iter->index - 1, out2); iter->last_removed = true; return CC_OK; } return CC_ERR_VALUE_NOT_FOUND; }
/** * Filter loaded test suites, either remove suites listed (exclude=TRUE), or all * that are not listed (exclude=FALSE). */ static void apply_filter(array_t *loaded, char *filter, bool exclude) { enumerator_t *enumerator, *names; hashtable_t *listed; test_suite_t *suite; char *name; listed = hashtable_create(hashtable_hash_str, hashtable_equals_str, 8); names = enumerator_create_token(filter, ",", " "); while (names->enumerate(names, &name)) { listed->put(listed, name, name); } enumerator = array_create_enumerator(loaded); while (enumerator->enumerate(enumerator, &suite)) { if ((exclude && listed->get(listed, suite->name)) || (!exclude && !listed->get(listed, suite->name))) { array_remove_at(loaded, enumerator); destroy_suite(suite); } } enumerator->destroy(enumerator); listed->destroy(listed); names->destroy(names); }
/** * Removes the last returned element by <code>array_iter_next()</code> * function without invalidating the iterator and optionally sets the out * parameter to the value of the removed element. * * @note This function should only ever be called after a call to <code> * array_iter_next()</code>. * @param[in] iter the iterator on which this operation is being performed * @param[out] out pointer to where the removed element is stored, or NULL * if it is to be ignored * * @return CC_OK if the element was successfully removed, or * CC_ERR_VALUE_NOT_FOUND. */ enum cc_stat array_iter_remove(ArrayIter *iter, void **out) { enum cc_stat status = CC_ERR_VALUE_NOT_FOUND; if (!iter->last_removed) { status = array_remove_at(iter->ar, iter->index - 1, out); if (status == CC_OK) iter->last_removed = true; } return status; }
int main(int argc, char *argv[]) { struct array a; struct array_iterator ai; int i; int *t, *t_arr; while ((i = getopt(argc, argv, "vi:")) != -1) { switch (i) { case 'v': ++verbose; break; case 'i': MAX_ITEMS = atoi(optarg); break; default: fprintf(stderr, "bad option\n"); exit(1); } } assert(t_arr = malloc(MAX_ITEMS * sizeof(int))); array_init(&a, 100); verbose_print(1, "start\n"); for (i = 0; i < MAX_ITEMS; i++) { t = t_arr + i; *t = i; assert(array_put(&a, t) == i); } for (i = 0; i < MAX_ITEMS; i += 2) { t = t_arr + i; assert(array_remove(&a, t) == t); } for (i = 1; i < MAX_ITEMS; i += 2) { t = t_arr + i; assert(array_pop(&a) == t); } for (i = 0; i < MAX_ITEMS; i += 2) { t = t_arr + i; assert(array_put_at(&a, i, t) == NULL); } for (i = 1; i < MAX_ITEMS; i += 2) { t = t_arr + i; assert(array_put(&a, t) == i); } for (i = 0; i < MAX_ITEMS; i += 2) { t = t_arr + i; assert(array_remove_at(&a, i) == t); } array_iter_set(&ai, &a); i = 1; while ((t = array_iter_get(&ai))) { assert(*t == i); i += 2; verbose_print(2, "%d ", *t); } array_iter_end(&ai); printf("\n"); array_free(&a); return 0; }
/** * Removes an Array element from the end of the array and optionally sets the * out parameter to the value of the removed element. * * @param[in] ar the array whose last element is being removed * @param[out] out pointer to where the removed value is stored, or NULL if it is * to be ignored * * @return CC_OK if the element was successfully removed, or CC_ERR_OUT_OF_RANGE * if the Array is already empty. */ enum cc_stat array_remove_last(Array *ar, void **out) { return array_remove_at(ar, ar->size - 1, out); }
END_TEST START_TEST(test_enumerate) { array_t *array; int i, *x, y[6] = {0, 1, 2, 3, 4, 5}; enumerator_t *enumerator; array = array_create(sizeof(y[0]), 0); array_insert(array, ARRAY_TAIL, &y[0]); array_insert(array, ARRAY_TAIL, &y[1]); array_insert(array, ARRAY_TAIL, &y[2]); array_insert(array, ARRAY_TAIL, &y[3]); array_insert(array, ARRAY_TAIL, &y[4]); array_insert(array, ARRAY_TAIL, &y[5]); ck_assert_int_eq(array_count(array), 6); /* 0, 1, 2, 3, 4, 5 */ i = 0; enumerator = array_create_enumerator(array); while (enumerator->enumerate(enumerator, &x)) { ck_assert_int_eq(*x, y[i]); i++; } enumerator->destroy(enumerator); ck_assert_int_eq(i, 6); i = 0; enumerator = array_create_enumerator(array); while (enumerator->enumerate(enumerator, &x)) { ck_assert_int_eq(*x, y[i]); if (i == 0 || i == 3 || i == 5) { array_remove_at(array, enumerator); } i++; } enumerator->destroy(enumerator); ck_assert_int_eq(i, 6); ck_assert_int_eq(array_count(array), 3); /* 1, 2, 4 */ i = 0; enumerator = array_create_enumerator(array); while (enumerator->enumerate(enumerator, &x)) { switch (i++) { case 0: ck_assert_int_eq(*x, y[1]); break; case 1: ck_assert_int_eq(*x, y[2]); break; case 2: ck_assert_int_eq(*x, y[4]); break; default: ck_assert(0); } } enumerator->destroy(enumerator); array_compress(array); i = 0; enumerator = array_create_enumerator(array); while (enumerator->enumerate(enumerator, &x)) { switch (i++) { case 0: ck_assert_int_eq(*x, y[1]); break; case 1: ck_assert_int_eq(*x, y[2]); break; case 2: ck_assert_int_eq(*x, y[4]); break; default: ck_assert(0); } } enumerator->destroy(enumerator); array_destroy(array); }