char *test_remove() { int *val_check = DArray_remove(array, 0); mu_assert(val_check != NULL, "Should not get NULL"); mu_assert(*val_check == *val1, "Should get the first value"); mu_assert(DArray_get(array,0) == NULL, "Should be gone."); DArray_free(val_check); val_check = DArray_remove(array, 1); mu_assert(val_check != NULL, "Should not get NULL"); mu_assert(*val_check == *val2, "Should get the second value"); mu_assert(DArray_get(array,1) == NULL, "Should be gone."); DArray_free(val_check); return NULL; }
DArray DArray_group_by(DArray array, DArray_group_by_fn *group_by_fn) { if (DArray_is_empty(array) || !group_by_fn) return NULL; DArray groups = DArray_create(sizeof(DArray), 10); DArray group; int idx; void *el; size_t count = (size_t) DArray_count(array); for (size_t i = 0; i < count; i++) { el = DArray_remove(array, i); if (!el) continue; idx = group_by_fn(el); if (idx == -1) continue; group = DArray_get(groups, idx); if (!group) { group = DArray_create(array->element_size, DArray_count(array) + 1); DArray_ensure_capacity(groups, idx + 1); DArray_set(groups, idx, group); } DArray_push(group, el); } return groups; }
void Hashmap_destroy(Hashmap *map) { int i = 0; int j = 0; if (map) { if (map->buckets) { for (i = 0; i < DArray_count(map->buckets); i++) { DArray *bucket = DArray_get(map->buckets, i); if (bucket) { for (j = 0; j < DArray_count(bucket); j++) { HashmapNode *node = DArray_get(bucket, j); if (node != NULL) { free(node->data); map->free_key(node->key); } } DArray_clear_destroy(bucket); DArray_remove(map->buckets, i); } } DArray_clear_destroy(map->buckets); } free(map); } }
char *test_remove() { DArray *array = DArray_create(sizeof(intptr_t), 100); DArray_remove(array, 66); assert(array->size == 0 && "Unexpected array size - must be 0 as no elements were added."); DArray_set(array, 20, (void *) (intptr_t) 9789); DArray_remove(array, 20); assert(array->size == 0 && "Unexpected array size - must be 0 again after removing element."); void *value = DArray_get(array, 20); assert(value == NULL && "Value at index 20 was not removed."); DArray_destroy(array); return NULL; }
void *DArray_pop(DArray *array) { check(array, "received darray null pointer"); return DArray_remove(array, array->count - 1); error: return NULL; }
void *DArray_pop(DArray *array) { check(array->end > 0, "can't shrink array beyond zero"); void *el = DArray_remove(array, array->end); array->end--; if (array->end > (int)array->expand_rate && array->end % array->expand_rate) { DArray_contract(array); } return el; error: return NULL; }
void *DArray_pop(DArray *array) { check(array->end - 1 >= 0, "Attempt to pop from empty array."); void *el = DArray_remove(array, array->end - 1); array->end--; if(DArray_end(array) > (int)array->expand_rate && DArray_end(array) % array->expand_rate) { DArray_contract(array); } return el; error: return NULL; }
char *test_iterationBackward_arrayContainingHoles_2() { DArray *array = DArray_create(sizeof(intptr_t), 3); DArray_set(array, 0, (void *) (intptr_t) 10); DArray_set(array, 1, (void *) (intptr_t) 20); DArray_set(array, 2, (void *) (intptr_t) 30); DArray_remove(array, 2); int index = -1; assert(DArray_iterator_prev(array, &index) == 1 && "Expected to continue to prev element."); assert(index == 1 && "Expected index was not set by iteration."); assert(DArray_iterator_prev(array, &index) == 1 && "Expected to continue to prev element."); assert(index == 0 && "Expected index was not set by iteration."); assert(DArray_iterator_prev(array, &index) == 0 && "Expected to end interation."); DArray_destroy(array); return NULL; }